]> git.aero2k.de Git - urlbot-v3.git/commitdiff
move topic code to own function, fix off by one
authorThorsten S <mail@aero2k.de>
Wed, 25 Mar 2020 20:50:10 +0000 (21:50 +0100)
committerThorsten S <mail@aero2k.de>
Wed, 25 Mar 2020 20:50:10 +0000 (21:50 +0100)
distbot/bot/bot.py

index 0bd609427480c76569f66292a1ad011d9bb149d6..52ba9239e0dde8b9e363437025ab415edbd07b8b 100644 (file)
@@ -107,6 +107,24 @@ class Bot(sleekxmpp.ClientXMPP):
             })
         )
 
+    def get_amqp_routing_key(self, msg):
+        # simplify the key significantly
+        key = shlex.split(re.sub(r'[^a-zäöüß0-9 "\']', '', msg["body"].lower()))
+        if not key:
+            # simple dots, smilies, emoji shit, pass through
+            key = shlex.split(msg["body"].lower())
+
+        # cut the nick from the message
+        if self.nick.lower() in key[0].lower():
+            if msg["body"][0] == '>':
+                key.pop(0)
+                key.insert(0, "quoted_nick")
+            else:
+                key.pop(0)
+                key.insert(0, "nick")
+        routing_key = '.'.join(key)[:254]
+        return routing_key
+
     def message(self, msg):
         logger.debug("msg is " + str(msg))
         logger.debug("from: " + str(msg['from']))
@@ -123,30 +141,16 @@ class Bot(sleekxmpp.ClientXMPP):
                 self.initialize_actionthreads()
                 self.echo("Worker One available for master.")
 
-            # simplify the key significantly
-            key = shlex.split(re.sub(r'[^a-zäöüß0-9 "\']', '', msg["body"].lower()))
-            if not key:
-                # simple dots, smilies, emoji shit, pass through
-                key = shlex.split(msg["body"].lower())
-
-            # cut the nick from the message
-            offset = 0
-            if self.nick.lower() in key[0].lower():
-                if message[0] == '>':
-                    key.pop(0)
-                    key.insert(0, "quoted_nick")
-                else:
-                    key.pop(0)
-                    key.insert(0, "nick")
-                offset = message.find(self.nick) + len(self.nick) + 1
+            routing_key = self.get_amqp_routing_key(msg)
 
             if msg["type"] == "groupchat":
                 recipient = msg["mucroom"]
             else:
                 recipient = msg["from"].jid
 
+            offset = msg["body"].find(self.nick) + len(self.nick) + 1
             process_message(
-                routing_key='.'.join(key)[:255],
+                routing_key=routing_key,
                 body=json.dumps({
                     "from": msg["from"].jid,
                     "to": recipient,