From f556cdb3a1634fce2095b614dd5c31f625b1deae Mon Sep 17 00:00:00 2001 From: Thorsten Date: Thu, 29 Jul 2021 18:57:08 +0200 Subject: [PATCH] some more debug logs, add group invite --- distbot/bot/action_worker.py | 10 ++++++++-- distbot/bot/bot.py | 16 +++++++++++++--- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/distbot/bot/action_worker.py b/distbot/bot/action_worker.py index 207ae0b..a57d47c 100644 --- a/distbot/bot/action_worker.py +++ b/distbot/bot/action_worker.py @@ -35,16 +35,21 @@ class ActionWorker(object): self.idle_task = self.loop.create_task(self.tick()) def open_connection(self, connection): + logger.debug("opened connection for actionworker") self._connection = connection connection.channel(on_open_callback=self.open_channel) def open_channel(self, channel: Channel): + logger.debug("opened channel") # self.add_on_channel_close_callback() # self.setup_exchange(self.EXCHANGE) self._channel = channel channel.add_on_close_callback(lambda *_: self._bot.disconnect()) channel.queue_declare(queue=self._queue_name, durable=True, callback=lambda _: self.start_consuming()) - channel.add_on_cancel_callback(lambda _: channel.close()) + def cancel(_): + logger.debug("cancel channel") + channel.close() + channel.add_on_cancel_callback(lambda _: cancel) def start_consuming(self): """This method sets up the consumer by first calling @@ -164,4 +169,5 @@ class ActionWorker(object): conf_set('request_counter', request_counter + 1) def die(self): - self._connection.close() + if self._connection: + self._connection.close() diff --git a/distbot/bot/bot.py b/distbot/bot/bot.py index e551dee..c5170ae 100644 --- a/distbot/bot/bot.py +++ b/distbot/bot/bot.py @@ -32,6 +32,7 @@ class Bot(slixmpp.ClientXMPP): self.add_event_handler('session_start', self.session_start) self.add_event_handler('groupchat_message', self.muc_message) self.add_event_handler('message', self.message_handler) + self.add_event_handler("groupchat_invite", self.group_invite) for room in self.rooms: self.add_event_handler('muc::%s::got_online' % room, self.muc_online) @@ -50,8 +51,11 @@ class Bot(slixmpp.ClientXMPP): def initialize_actionworker(self): connection = AsyncioConnection( pika.URLParameters(conf_get("amqp_uri")), - on_open_callback=self.action_worker.open_connection + on_open_callback=self.action_worker.open_connection, + on_open_error_callback=lambda con, err: logger.error("Could not connect: %s", err), + on_close_callback=lambda con, e, x: logger.info("closing connection of actionworker (%s)", e) ) + logger.debug("connection state: %s", connection.connection_state) def disconnect(self, reconnect=False, wait=None, send_close=True): logger.info("Stopping all workers...") @@ -59,22 +63,27 @@ class Bot(slixmpp.ClientXMPP): logger.info("Stopping self...") super(Bot, self).disconnect(reconnect) logger.info("Gudbai...") + # TODO: cleaner shutdown? raise SystemExit() def session_start(self, _): self.get_roster() self.send_presence(ppriority=0, pstatus=None, pshow=None) + self.initialize_actionworker() for room in self.rooms: logger.info('%s: joining' % room) - ret = self.plugin['xep_0045'].join_muc(room, self.nick, wait=True) + ret = self.plugin['xep_0045'].join_muc(room, self.nick) logger.info('%s: joined with code %s' % (room, ret)) - self.initialize_actionworker() def muc_message(self, msg): if msg['mucnick'] == self.nick or 'groupchat' != msg['type']: return False return self.message(msg) + def group_invite(self, msg): + self.plugin["xep_0045"].join_muc(msg["from"], self.nick) + logger.info("Joining %s (by invitation)" % (msg["from"])) + def message_handler(self, msg): # disabled, as currently history leaks from groupchat chats return False @@ -164,6 +173,7 @@ class Bot(slixmpp.ClientXMPP): ) def echo(self, body, recipient=None): + logger.debug("echo: %s", body) if not recipient: rooms = self.rooms else: -- 2.39.2