From fc68fb05af319202cb18fba36dd8f95d77673cda Mon Sep 17 00:00:00 2001 From: kaliko Date: Sat, 25 Mar 2023 10:55:44 +0100 Subject: [PATCH] rtbl: Better presence handling --- sid/rtbl.py | 43 +++++++++++++++++++++++++++++++++++++------ 1 file changed, 37 insertions(+), 6 deletions(-) diff --git a/sid/rtbl.py b/sid/rtbl.py index bb4427e..99095ea 100644 --- a/sid/rtbl.py +++ b/sid/rtbl.py @@ -10,7 +10,7 @@ from slixmpp import JID from slixmpp.exceptions import XMPPError from slixmpp.xmlstream import tostring -from .plugin import Plugin +from .plugin import Plugin, botcmd def jid_to_sha256(jid: JID) -> str: @@ -85,6 +85,8 @@ class RTBL(Plugin): self.participants = set() self.moderator = False self.blocklist: BL = None + self.hits = 0 + self.presences = {} def _exit(self): self.rm_handlers() @@ -150,8 +152,8 @@ class RTBL(Plugin): """Ban jid in RTBL""" if not self.moderator: return - if not self.blocklist: - self.log.debug("block list not populated yet") + if self.blocklist is None: + self.log.info('Not checking %s, block list not populated yet', jid) return if self.blocklist.check(jid): self.log.debug(f'About to ban {jid}') @@ -159,6 +161,8 @@ class RTBL(Plugin): if reason is not None: reason = f'rtbl {reason}' await self.ban(jid.bare, reason=reason) + self.hits += 1 + self.log.info(f'{jid} banned!') def got_offline(self, pres): """Handler method for leaving MUC participants""" @@ -166,9 +170,10 @@ class RTBL(Plugin): 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} (len:{len(self.participants)})') + self.log.debug(f'participants: - {user} got offline (len:{len(self.participants)})') def got_presence(self, pres): """Does bot have required permissions""" @@ -182,15 +187,41 @@ 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'] - user = fjid if fjid.full else pres['muc']['nick'] + nick = pres['muc']['nick'] + role = pres['muc']['role'] + affi = pres['muc']['affiliation'] + user = fjid if fjid.full else nick self.participants.add(user) - self.log.debug(f'participants: +{user} (len:{len(self.participants)})') + 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") + def rtbl_info(self, rcv, _): + """Show RTBL info""" + if self.blocklist is None: + msg = 'Block list not populated yet' + self.log.warning(msg) + self.reply(rcv, msg) + return + msg = f'Got {len(self.blocklist)} items in {RTBL.pubsub_server}/{RTBL.node}' + if self.hits > 0: + msg+=f' (hits {self.hits})' + if not self.moderator: + msg+='\nBot has no moderator permissions!' + self.reply(rcv, msg) + if __name__ == '__main__': from .cli.rtbl import main -- 2.39.5