('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()
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):
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):
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']:
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")
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
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)
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 "‽"