]> git.aero2k.de Git - urlbot-v3.git/commitdiff
bunch of fixes regarding to multi-room usage and logging
authorThorsten <mail@aero2k.de>
Thu, 4 Oct 2018 19:44:27 +0000 (21:44 +0200)
committerThorsten <mail@aero2k.de>
Thu, 4 Oct 2018 19:44:27 +0000 (21:44 +0200)
21 files changed:
distbot/__init__.py
distbot/bot/action_worker.py
distbot/bot/bot.py
distbot/bot/worker.py
distbot/common/action.py
distbot/common/config.py
distbot/common/config/local_config.ini.spec
distbot/common/message.py
distbot/common/utils.py
distbot/plugins/basic.py
distbot/plugins/bofh.py
distbot/plugins/bugtracker.py
distbot/plugins/feeds.py
distbot/plugins/fun.py
distbot/plugins/lookup.py
distbot/plugins/morse.py
distbot/plugins/plugin_help.py
distbot/plugins/searx.py
distbot/plugins/translation.py
distbot/plugins/url.py
logging.ini [new file with mode: 0644]

index 40a96afc6ff09d58a702b76e3f7dd412fe975e26..caef0fd3b1b5af22009f3a2e368f4319ffa65dad 100644 (file)
@@ -1 +1,3 @@
 # -*- coding: utf-8 -*-
+from logging.config import fileConfig
+fileConfig('logging.ini')
index 94406dc4dc274c63368516f8133e0c5ba6fe2916..3248f7fe0b36d36034c2a5be54d796b0ae065156 100644 (file)
@@ -8,17 +8,11 @@ import time
 import pika
 
 from distbot.common.action import Action, send_action
-from distbot.common.config import conf_set, conf_get, log_format
+from distbot.common.config import conf_set, conf_get
 from distbot.common.message import process_message
 
-logging.basicConfig(
-    level=conf_get("loglevel"),
-    format=log_format
-)
-
 logger = logging.getLogger(__name__)
 
-
 EVENTLOOP_DELAY = 0.1
 
 
@@ -132,7 +126,7 @@ class ActionThread(threading.Thread):
 
         if action.msg:  # and rate_limit(RATE_CHAT | plugin.ratelimit_class):
             time.sleep(delay)
-            self.bot.echo(action.msg)
+            self.bot.echo(action.msg, action.recipient)
 
         # TODO test that
         if action.priv_msg:  # and rate_limit(RATE_CHAT | plugin.ratelimit_class):
@@ -150,4 +144,3 @@ class ActionThread(threading.Thread):
 
         request_counter = int(conf_get('request_counter'))
         conf_set('request_counter', request_counter + 1)
-
index 1be502365868c81cf15a61545fb6503b87cd6586..46e577ac68b821f11a2ce9947e21e6982ec94384 100644 (file)
@@ -7,17 +7,12 @@ import pika
 import sleekxmpp
 
 from distbot.bot import action_worker, worker as worker_mod
-from distbot.common.config import conf_get, log_format
+from distbot.common.config import conf_get
 from distbot.common.message import process_message, get_nick_from_message
 from distbot.plugins import basic, fun, lookup, url, feeds, muc, translation, searx, queue_management, plugin_help, \
     morse, meta, \
     extended, bugtracker, bots, bofh
 
-logging.basicConfig(
-    level=conf_get("loglevel"),
-    format=log_format
-)
-
 logger = logging.getLogger(__name__)
 
 logging.getLogger("pika").setLevel(logging.WARN)
@@ -59,6 +54,7 @@ class Bot(sleekxmpp.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)
         for room in self.rooms:
             self.add_event_handler('muc::%s::got_online' % room, self.muc_online)
 
@@ -151,6 +147,15 @@ class Bot(sleekxmpp.ClientXMPP):
             return False
         return self.message(msg)
 
+    def message_handler(self, msg):
+        # disabled, as currently history leaks from groupchat chats
+        return False
+        # if msg["type"] == "groupchat":
+        #     # that's a dup
+        #     return False
+        # else:
+        #     self.message(msg)
+
     def muc_online(self, msg):
         sender = get_nick_from_message(msg)
         if sender == self.nick:
@@ -196,27 +201,33 @@ class Bot(sleekxmpp.ClientXMPP):
                 key.insert(0, "nick")
                 offset = len(self.nick) + 1
 
+            if msg["type"] == "groupchat":
+                recipient = msg["mucroom"]
+            else:
+                recipient = msg["from"].jid
+
             process_message(
                 routing_key='.'.join(key)[:255],
                 body=json.dumps({
                     "from": msg["from"].jid,
-                    "to": msg["to"].jid,
+                    "to": recipient,
                     "body": msg["body"][offset:].strip()
                 })
             )
 
-    def echo(self, body, room=None):
-        if not room:
+    def echo(self, body, recipient=None):
+        if not recipient:
             rooms = self.rooms
         else:
-            rooms = [room]
+            rooms = [recipient]
 
         for r in rooms:
-            self.send_message(
-                mto=r,
-                mbody=body,
-                mtype='groupchat'
-            )
+            if recipient.split("/")[0] in self.rooms:
+                self.send_message(
+                    mto=r,
+                    mbody=body,
+                    mtype='groupchat' if recipient in self.rooms else 'chat'
+                )
 
 
 def run():
index 647028e2b794b4bdd72291a043be2c499a54b79c..d4bdcf8278c3bc496e2c5882653de16e145b11b6 100644 (file)
@@ -8,12 +8,7 @@ import pika
 from functools import partial
 
 from distbot.common.action import Action, send_action
-from distbot.common.config import conf_get, log_format
-
-logging.basicConfig(
-    level=conf_get("loglevel"),
-    format=log_format
-)
+from distbot.common.config import conf_get
 
 logger = logging.getLogger(__name__)
 
@@ -84,6 +79,7 @@ class Worker(threading.Thread):
             logger.debug("no action taken")
             return
         action.sender = body["from"]
+        action.recipient = body["to"]
         send_action(self.actionqueue, action)
 
         if ack:
index e0e2a52639703361d4bcbeeeb79c81efa9dac645..13dfd1b09654af7a6c612c4aea6e164f48a2d07c 100644 (file)
@@ -11,6 +11,7 @@ class Action:
     priv_msg = None
     presence = None
     sender = None
+    recipient = None
 
     # TODO: Event(Action)?
     stop_event = None
@@ -23,6 +24,7 @@ class Action:
         self.priv_msg = kwargs.get('priv_msg')
         self.presence = kwargs.get('presence')
         self.sender = kwargs.get('sender')
+        self.recipient = kwargs.get('recipient')
 
         self.event = kwargs.get('event')
         self.stop_event = kwargs.get('stop_event')
index 2d5d38e5ad997a4babb1a83cfbea0317b190b2bc..d2b9b82f799573daabcf152fc1026857c2456cc9 100644 (file)
@@ -5,18 +5,8 @@ import os
 from configobj import ConfigObj
 from validate import Validator
 
-# log_format = ' %(asctime)s %(process)d %(thread)d %(levelname).1s %(funcName)-15s %(message)s'
-# TODO: log config files?
-log_format = '%(levelname).1s %(funcName)-15s %(message)s'
-logging.basicConfig(
-    level=logging.INFO,
-    format=log_format
-)
-
 logger = logging.getLogger(__name__)
 
-logging.getLogger('sleekxmpp').setLevel(logging.INFO)
-
 
 def conf_get(key):
     return Config().get(key)
@@ -50,7 +40,8 @@ class Config:
             # TODO: can't remember the reason for this one
             # disabled for now.
             # self.config_store.write()
-            self.runtime_config_store.write()
+            # self.runtime_config_store.write()
+            pass
 
     def _set(self, cfg, path, value):
         if len(path) == 1:
index ccc676705d549e504f14e3f51b71068cc0b379f2..c912b627c47c69bc26ac0bc92ed841c55e661a65 100644 (file)
@@ -9,9 +9,6 @@ bot_owner = string
 bot_owner_email = string
 detectlanguage_api_key = string
 
-loglevel = option('ERROR', WARN', 'INFO', 'DEBUG', default='INFO')
-debug_mode = boolean(default=false)
-
 # rate limiting, TODO
 hist_max_count = integer(default=5)
 hist_max_time = integer(default=10*60)
index a0e7b4c1b78ad2f88711825f2c1a04cd0021c6d1..1366d3e0fb5eb71322b8e3efd9042cc168e0884d 100644 (file)
@@ -8,13 +8,6 @@ import pika
 from sleekxmpp import Message, Presence
 from sleekxmpp.jid import JID
 
-from distbot.common.config import conf_get, log_format
-
-logging.basicConfig(
-    level=conf_get("loglevel"),
-    format=log_format
-)
-
 logger = logging.getLogger(__name__)
 
 
index 2f8b60745d8c4c126bb3e9121cefffa6d6ff9f49..b3bac4d59f26922d49809f95e26ea69f45dedc87 100644 (file)
@@ -8,12 +8,6 @@ import re
 import requests
 from functools import wraps
 
-from distbot.common.config import log_format, conf_get
-
-logging.basicConfig(
-    level=conf_get("loglevel"),
-    format=log_format
-)
 logger = logging.getLogger(__name__)
 
 BUFSIZ = 8192
index a6667fd264cffc26540d6586f0b504dc0816a10b..13762748c1b35bf4fdbae319c5b31cd5fe28a01a 100644 (file)
@@ -7,13 +7,9 @@ import random
 from distbot.bot.worker import Worker
 from distbot.common import config
 from distbot.common.action import Action
-from distbot.common.config import conf_get, log_format
+from distbot.common.config import conf_get
 from distbot.common.message import get_nick_from_message, get_words
 
-logging.basicConfig(
-    level=conf_get("loglevel"),
-    format=log_format
-)
 
 logger = logging.getLogger(__name__)
 
index ec63322d165c55a614db1ce45e25430cd29628d4..39424a9e0062a60bdea405b9063d8fcc7a8c2885 100644 (file)
@@ -5,13 +5,9 @@ import random
 
 from distbot.bot.worker import Worker
 from distbot.common.action import Action
-from distbot.common.config import conf_get, log_format
+from distbot.common.config import conf_get
 from distbot.common.message import get_nick_from_message
 
-logging.basicConfig(
-    level=conf_get("loglevel"),
-    format=log_format
-)
 
 logger = logging.getLogger(__name__)
 
index 08927b9e37877acdf40e33dac82740aa1f945974..ff83dae9705c1b677d5e1d5bf2d9866e67e863c2 100644 (file)
@@ -6,13 +6,9 @@ import re
 
 from distbot.bot.worker import Worker
 from distbot.common.action import Action
-from distbot.common.config import conf_get, log_format
+from distbot.common.config import conf_get
 from distbot.common.utils import extract_title
 
-logging.basicConfig(
-    level=conf_get("loglevel"),
-    format=log_format
-)
 
 logger = logging.getLogger(__name__)
 
index a839ca2a054b71af7b6fd3b9dfdf89a0461ca157..4bf537bc7788b95943ccdfa2201967860268687c 100644 (file)
@@ -8,13 +8,9 @@ from lxml import etree
 
 from distbot.bot.worker import Worker
 from distbot.common.action import Action
-from distbot.common.config import conf_get, conf_set, log_format
+from distbot.common.config import conf_get, conf_set
 from distbot.common.message import get_words
 
-logging.basicConfig(
-    level=conf_get("loglevel"),
-    format=log_format
-)
 
 logger = logging.getLogger(__name__)
 
index 474763f69ff7ae52690948bc2eab6b87a4d0b70c..0199773e7196a16cfcd1c0d7fc4053492deaa1b0 100644 (file)
@@ -5,14 +5,10 @@ import random
 
 from distbot.bot.worker import Worker
 from distbot.common.action import Action
-from distbot.common.config import conf_get, log_format
+from distbot.common.config import conf_get
 from distbot.common.message import get_nick_from_message, get_words
 from distbot.common.utils import giphy
 
-logging.basicConfig(
-    level=conf_get("loglevel"),
-    format=log_format
-)
 
 logger = logging.getLogger(__name__)
 
index c49080cfac1a216747c00ab43c526686eb05b2ab..cc7e5838548c85282c17cab355a733ba33292817 100644 (file)
@@ -11,13 +11,9 @@ import requests
 
 from distbot.bot.worker import Worker
 from distbot.common.action import Action
-from distbot.common.config import conf_get, log_format
+from distbot.common.config import conf_get
 from distbot.common.message import get_nick_from_message, get_words
 
-logging.basicConfig(
-    level=conf_get("loglevel"),
-    format=log_format
-)
 
 logger = logging.getLogger(__name__)
 
index b193ab26f27d824e3d97a114b33482c11fa75e05..394b8929a23da826baefb80682d81585db12dc0b 100644 (file)
@@ -5,13 +5,9 @@ import re
 
 from distbot.bot.worker import Worker
 from distbot.common.action import Action
-from distbot.common.config import conf_get, log_format
+from distbot.common.config import conf_get
 from distbot.common.message import get_nick_from_message, get_words
 
-logging.basicConfig(
-    level=conf_get("loglevel"),
-    format=log_format
-)
 
 logger = logging.getLogger(__name__)
 
index ce52450b8415915cfc3f5bbe670b50c3280c6642..b60374f3f85857a2fb35fafb8c01d301720eb6e7 100644 (file)
@@ -4,12 +4,8 @@ import logging
 
 from distbot.bot.worker import Worker
 from distbot.common.action import Action
-from distbot.common.config import conf_get, log_format
+from distbot.common.config import conf_get
 
-logging.basicConfig(
-    level=conf_get("loglevel"),
-    format=log_format
-)
 logger = logging.getLogger(__name__)
 
 
index 4f20265cf862287d87431b2b4a4d3344fd1fcdf4..30fe6982500a0b00778c3d427f2feef38fada285 100644 (file)
@@ -9,14 +9,10 @@ from requests.exceptions import SSLError
 
 from distbot.bot.worker import Worker
 from distbot.common.action import Action
-from distbot.common.config import conf_get, log_format
+from distbot.common.config import conf_get
 from distbot.common.message import get_words
 from distbot.common.utils import retry
 
-logging.basicConfig(
-    level=conf_get("loglevel"),
-    format=log_format
-)
 
 logger = logging.getLogger(__name__)
 
index c4430bd4de87791378a47ff477719e4f12221f7d..a3c2d2d7a30a4b01774f136dc290abd0e0e66b4e 100644 (file)
@@ -7,13 +7,9 @@ import requests
 
 from distbot.bot.worker import Worker
 from distbot.common.action import Action
-from distbot.common.config import conf_get, log_format
+from distbot.common.config import conf_get
 from distbot.common.message import get_nick_from_message, get_words
 
-logging.basicConfig(
-    level=conf_get("loglevel"),
-    format=log_format
-)
 
 logger = logging.getLogger(__name__)
 
index f3aece5b28b98d65782640097d1ebbf3cf249e81..68e3a3766d91cdc387c472cf4288a0bd9436fe20 100644 (file)
@@ -9,13 +9,9 @@ import requests
 
 from distbot.bot.worker import Worker
 from distbot.common.action import Action
-from distbot.common.config import conf_get, conf_set, log_format
+from distbot.common.config import conf_get, conf_set
 from distbot.common.message import get_nick_from_message, get_words
 
-logging.basicConfig(
-    level=conf_get("loglevel"),
-    format=log_format
-)
 
 logger = logging.getLogger(__name__)
 
diff --git a/logging.ini b/logging.ini
new file mode 100644 (file)
index 0000000..22c4a68
--- /dev/null
@@ -0,0 +1,21 @@
+[loggers]
+keys=root
+
+[handlers]
+keys=stream_handler
+
+[formatters]
+keys=formatter
+
+[logger_root]
+level=DEBUG
+handlers=stream_handler
+
+[handler_stream_handler]
+class=StreamHandler
+level=DEBUG
+formatter=formatter
+args=(sys.stderr,)
+
+[formatter_formatter]
+format=%(levelname).1s %(funcName)-15s %(message)s