From e755a9b0b96fd85c912c850a863605eb2afffc54 Mon Sep 17 00:00:00 2001 From: Thorsten Date: Sat, 20 Jun 2020 17:38:01 +0200 Subject: [PATCH] slix --- distbot/bot/action_worker.py | 6 ++-- distbot/bot/bot.py | 53 ++++++++++++++++++++---------------- distbot/common/message.py | 6 ++-- setup.py | 2 +- 4 files changed, 38 insertions(+), 29 deletions(-) diff --git a/distbot/bot/action_worker.py b/distbot/bot/action_worker.py index e6d6869..ade4283 100644 --- a/distbot/bot/action_worker.py +++ b/distbot/bot/action_worker.py @@ -70,7 +70,9 @@ class ActionThread(threading.Thread): self.channel.start_consuming() def die(self): + logger.debug("Quitting...") self.channel.stop_consuming() + logger.debug("action_worker gone") def find_scheduled_action_by_mutex(self, mutex): action_item = None @@ -104,7 +106,7 @@ class ActionThread(threading.Thread): if item: self.event_list.cancel(item) - def run_action(self, action): + def run_action(self, action: Action): logger.debug("Executing action %s", action) # to prevent really stupid clients not scrolling at incoming messages very quickly. @@ -129,7 +131,7 @@ class ActionThread(threading.Thread): "to": action.recipient, "body": ' '.join(action.command.split('.')[1:]), }) - process_message(routing_key=action.command, body=replay_body) + process_message(routing_key=action.command.encode("UTF-8"), body=replay_body) if action.msg: # and rate_limit(RATE_CHAT | plugin.ratelimit_class): time.sleep(delay) diff --git a/distbot/bot/bot.py b/distbot/bot/bot.py index 768d050..cf1af70 100644 --- a/distbot/bot/bot.py +++ b/distbot/bot/bot.py @@ -5,7 +5,7 @@ import re import shlex import pika -import sleekxmpp +import slixmpp from distbot.bot import action_worker from distbot.common.config import conf_get @@ -14,10 +14,11 @@ from distbot.common.message import process_message, get_nick_from_message logger = logging.getLogger(__name__) logging.getLogger("pika").setLevel(logging.WARN) -logging.getLogger('sleekxmpp').setLevel(logging.INFO) +logging.getLogger('slixmpp').setLevel(logging.DEBUG) +logging.getLogger('slixmpp.xmlstream.xmlstream').setLevel(logging.DEBUG) -class Bot(sleekxmpp.ClientXMPP): +class Bot(slixmpp.ClientXMPP): def __init__(self, jid, password, rooms, nick): super(Bot, self).__init__(jid, password) @@ -28,6 +29,8 @@ class Bot(sleekxmpp.ClientXMPP): self.rooms = rooms self.nick = nick + # from slixmpp.plugins.xep_0045 import XEP_0045 + # self.add_event_handler('handle_groupchat_presence', XEP_0045.handle_groupchat_presence) 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) @@ -35,8 +38,8 @@ class Bot(sleekxmpp.ClientXMPP): self.add_event_handler('muc::%s::got_online' % room, self.muc_online) self._initialize_plugins() - import ssl - self.ssl_version = ssl.PROTOCOL_TLSv1_2 + # import ssl + # self.ssl_version = ssl.PROTOCOL_TLSv1_2 def _initialize_plugins(self): self.register_plugin('xep_0045') @@ -63,7 +66,7 @@ class Bot(sleekxmpp.ClientXMPP): logger.info("Stopping all workers...") self.kill_workers() logger.info("Stopping self...") - super(Bot, self)._disconnect(reconnect, wait=False, send_close=True) + super(Bot, self).disconnect(reconnect) logger.info("Gudbai...") raise SystemExit() @@ -72,11 +75,7 @@ class Bot(sleekxmpp.ClientXMPP): self.send_presence(ppriority=0, pstatus=None, pshow=None) for room in self.rooms: logger.info('%s: joining' % room) - ret = self.plugin['xep_0045'].joinMUC( - room, - self.nick, - wait=True - ) + ret = self.plugin['xep_0045'].join_muc(room, self.nick, wait=True) logger.info('%s: joined with code %s' % (room, ret)) self.initialize_actionthreads() @@ -95,17 +94,20 @@ class Bot(sleekxmpp.ClientXMPP): # self.message(msg) def muc_online(self, msg): - sender = get_nick_from_message(msg) - if sender == self.nick: - return - process_message( - 'userjoin.{}'.format(msg["from"]), - body=json.dumps({ - "from": msg["from"].jid, - "to": msg["to"].jid, - "body": msg["body"].strip() - }) - ) + try: + sender = get_nick_from_message(msg) + if sender == self.nick: + return + process_message( + 'userjoin.{}'.format(msg["from"]), + body=json.dumps({ + "from": msg["from"].jid, + "to": msg["to"].jid, + "body": msg["body"].strip() + }) + ) + except ValueError as e: + logger.exception(e) @staticmethod def get_amqp_routing_key(nick, msg): @@ -180,7 +182,12 @@ def run(): bot = Bot(jid=conf_get("jid"), password=conf_get("password"), rooms=conf_get("rooms"), nick=conf_get("bot_nickname")) bot.connect() - bot.process() + try: + bot.process() + except KeyboardInterrupt: + bot.disconnect() + finally: + bot.abort() if __name__ == '__main__': diff --git a/distbot/common/message.py b/distbot/common/message.py index 6b131ab..d46b01a 100644 --- a/distbot/common/message.py +++ b/distbot/common/message.py @@ -6,8 +6,8 @@ import shlex import pika from distbot.common.config import conf_get -from sleekxmpp import Message, Presence -from sleekxmpp.jid import JID +from slixmpp import Message, Presence +from slixmpp.jid import JID logger = logging.getLogger(__name__) @@ -39,7 +39,7 @@ def get_nick_from_message(message_obj): jid = JID(message_obj["from"]) return jid.resource else: - raise Exception("Message type is: " + str(type(message_obj))) + raise ValueError("Invalid message type found: " + str(type(message_obj))) def process_message(routing_key, body): diff --git a/setup.py b/setup.py index 696551e..ae96beb 100644 --- a/setup.py +++ b/setup.py @@ -8,7 +8,7 @@ setup( test_requires=["pytest"], extras_require={ 'test': ["pytest"], - 'chatbot': ["sleekxmpp", "pyopenssl"], + 'chatbot': ["slixmpp", "pyopenssl"], 'worker': ["lxml"], }, scripts=[ -- 2.39.2