From 6bd74a49f0c0a599423d52cb0d59b0620b00118d Mon Sep 17 00:00:00 2001 From: kaliko Date: Sat, 25 Mar 2023 11:20:27 +0100 Subject: [PATCH] Keep track of MUC participant in the sid.MUCBot --- sid/rtbl.py | 31 +++---------------------------- sid/sid.py | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 28 deletions(-) diff --git a/sid/rtbl.py b/sid/rtbl.py index 99095ea..6b33344 100644 --- a/sid/rtbl.py +++ b/sid/rtbl.py @@ -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") diff --git a/sid/sid.py b/sid/sid.py index d74eaf8..9453db6 100644 --- a/sid/sid.py +++ b/sid/sid.py @@ -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 "‽" -- 2.39.5