]> git.aero2k.de Git - urlbot-v3.git/commitdiff
slix
authorThorsten <mail@aero2k.de>
Sat, 20 Jun 2020 15:38:01 +0000 (17:38 +0200)
committerThorsten <mail@aero2k.de>
Sat, 20 Jun 2020 15:38:01 +0000 (17:38 +0200)
distbot/bot/action_worker.py
distbot/bot/bot.py
distbot/common/message.py
setup.py

index e6d68699a06748078e3a4340bb966e9358691e95..ade42838def65cea8bac4967bd48b092e311f42b 100644 (file)
@@ -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)
index 768d0500c60c82b20b73dd758b3a13d40ca0dce1..cf1af7042ef5e223ea291c6456b2d8849c9c0d0c 100644 (file)
@@ -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__':
index 6b131ab6ef311d79309cfd8e8e2de2bbef391c14..d46b01a11ac8eb8669807b92af1cc097f6b620ca 100644 (file)
@@ -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):
index 696551e38ac0fa619e6b851855a35ce9018d30ef..ae96beb8102732603b475120d07c29bdfddd57f8 100644 (file)
--- 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=[