]> kaliko git repositories - sid.git/commitdiff
Keep track of MUC participant in the sid.MUCBot
authorkaliko <kaliko@azylum.org>
Sat, 25 Mar 2023 10:20:27 +0000 (11:20 +0100)
committerkaliko <kaliko@azylum.org>
Sat, 25 Mar 2023 10:20:27 +0000 (11:20 +0100)
sid/rtbl.py
sid/sid.py

index 99095eacf679244a2b221afe8f2905e3fe916206..6b33344800fbbc0022d479d2af257171ab667c6d 100644 (file)
@@ -77,16 +77,14 @@ class RTBL(Plugin):
                 ('pubsub_retract', self._retract),
                 ('pubsub_publish', self._publish),
                 (f'muc::{self.bot.room}::presence', self.got_presence),
-                (f'muc::{self.bot.room}::got_offline', self.got_offline),
                 (f'muc::{self.bot.room}::got_online', self.got_online)
         ]
         self.add_handlers()
         self.bot = bot
-        self.participants = set()
         self.moderator = False
         self.blocklist: BL = None
         self.hits = 0
-        self.presences = {}
+        self.presences = bot.muc_presences
 
     def _exit(self):
         self.rm_handlers()
@@ -113,7 +111,7 @@ class RTBL(Plugin):
         mess = f'Got {len(self.blocklist)} items in block list'
         self.log.info(mess)
         # Are current participants in the block list
-        for jid in list(self.participants):
+        for jid in [pres['muc']['jid'] for pres in self.presences.values()]:
             await self.rtbl_ban(jid)
 
     async def _create(self):
@@ -145,7 +143,7 @@ class RTBL(Plugin):
             return
         self.blocklist.insert_item(msg['pubsub_event']['items']['item'])
         # Are current participants in the block list
-        for jid in list(self.participants):
+        for jid in [pres['muc']['jid'] for pres in self.presences.values()]:
             await self.rtbl_ban(jid)
 
     async def rtbl_ban(self, jid):
@@ -164,17 +162,6 @@ class RTBL(Plugin):
             self.hits += 1
             self.log.info(f'{jid} banned!')
 
-    def got_offline(self, pres):
-        """Handler method for leaving MUC participants"""
-        fjid = pres['muc']['jid']
-        user = fjid if fjid.full else pres['muc']['nick']
-        try:
-            self.participants.remove(user)
-            self.presences.pop(pres['muc']['nick'])
-        except KeyError:
-            self.log.error('KeyError removing participant: "%s"', user)
-        self.log.debug(f'participants: - {user} got offline (len:{len(self.participants)})')
-
     def got_presence(self, pres):
         """Does bot have required permissions"""
         if 110 in pres['muc']['status_codes']:
@@ -187,24 +174,12 @@ class RTBL(Plugin):
             else:
                 self.log.info('Got moderator permissions.')
                 self.moderator = True
-        nick = pres['muc']['nick']
-        fjid = pres['muc']['jid']
-        role = pres['muc']['role']
-        affi = pres['muc']['affiliation']
-        user = fjid if fjid.full else nick
-        self.log.debug(f'participants: u {user}:{role}/{affi} (len:{len(self.participants)})')
-        self.presences.update({nick: pres})
 
     async def got_online(self, pres):
         """Handler method for new MUC participants"""
         fjid = pres['muc']['jid']
         nick = pres['muc']['nick']
-        role = pres['muc']['role']
-        affi = pres['muc']['affiliation']
         user = fjid if fjid.full else nick
-        self.participants.add(user)
-        self.presences.update({nick: pres})
-        self.log.debug(f'participants: + {user}:{role}/{affi} (len:{len(self.participants)})')
         await self.rtbl_ban(user)
 
     @botcmd(name="rtbl-info")
index d74eaf89a7250d2b06e5ac7a3183e5913e0d1987..9453db6a66c4fa4614e02d470eee42a9efd2dc94 100644 (file)
@@ -62,6 +62,8 @@ class MUCBot(slixmpp.ClientXMPP):
         self.commands = dict()
         self.room = room
         self.nick = nick
+        #: Keep track of MUC presences: {'nick': presence}
+        self.muc_presences = {}
         self.__set_logger(log_level)
         self.__seen = dict()
         self.register_plugin('xep_0030')  # Service Discovery
@@ -80,6 +82,11 @@ class MUCBot(slixmpp.ClientXMPP):
         self.add_event_handler('message', self.message)
         self.add_event_handler('got_online', self._view)
 
+        # keep track of join/parts
+        self.add_event_handler(f'muc::{self.room}::got_offline', self._muc_got_offline)
+        self.add_event_handler(f'muc::{self.room}::got_online', self._muc_got_online)
+        self.add_event_handler(f'muc::{self.room}::presence', self._muc_got_presence)
+
         # Handles disconnection
         self.add_event_handler('disconnected', self.disconn)
 
@@ -96,6 +103,39 @@ class MUCBot(slixmpp.ClientXMPP):
         log.setLevel(log_level)
         log.debug('set logger, log level : %s', log_level)
 
+    def _muc_got_online(self, pres):
+        """Keep track of MUC participants"""
+        fjid = pres['muc']['jid']
+        nick = pres['muc']['nick']
+        role = pres['muc']['role']
+        affi = pres['muc']['affiliation']
+        user = fjid if fjid.full else nick
+        self.muc_presences.update({nick: pres})
+        log.debug('Participants: + %s:%s/%s (len:%s)',
+                   user, role, affi,len(self.muc_presences))
+
+    def _muc_got_offline(self, pres):
+        """Keep track of MUC participants"""
+        fjid = pres['muc']['jid']
+        user = fjid if fjid.full else pres['muc']['nick']
+        try:
+            self.muc_presences.pop(pres['muc']['nick'])
+        except KeyError:
+            log.error('KeyError removing participant: "%s"', user)
+        log.debug('Participants: - %s got offline (len:%s)',
+                  user, len(self.muc_presences))
+
+    def _muc_got_presence(self, pres):
+        """Keep track of MUC participants"""
+        nick = pres['muc']['nick']
+        fjid = pres['muc']['jid']
+        role = pres['muc']['role']
+        affi = pres['muc']['affiliation']
+        user = fjid if fjid.full else nick
+        log.debug('Participants: u %s:%s/%s (len:%s)',
+                   user, role, affi,len(self.muc_presences))
+        self.muc_presences.update({nick: pres})
+
     def disconn(self, event):
         """disconnected handler"""
         msg = ": %s" % event if event else "‽"