From 20a8618d090976c35ad3d92605bf9212387e13ee Mon Sep 17 00:00:00 2001 From: Thorsten Date: Thu, 4 Oct 2018 21:44:27 +0200 Subject: [PATCH] bunch of fixes regarding to multi-room usage and logging --- distbot/__init__.py | 2 + distbot/bot/action_worker.py | 11 +----- distbot/bot/bot.py | 41 +++++++++++++-------- distbot/bot/worker.py | 8 +--- distbot/common/action.py | 2 + distbot/common/config.py | 13 +------ distbot/common/config/local_config.ini.spec | 3 -- distbot/common/message.py | 7 ---- distbot/common/utils.py | 6 --- distbot/plugins/basic.py | 6 +-- distbot/plugins/bofh.py | 6 +-- distbot/plugins/bugtracker.py | 6 +-- distbot/plugins/feeds.py | 6 +-- distbot/plugins/fun.py | 6 +-- distbot/plugins/lookup.py | 6 +-- distbot/plugins/morse.py | 6 +-- distbot/plugins/plugin_help.py | 6 +-- distbot/plugins/searx.py | 6 +-- distbot/plugins/translation.py | 6 +-- distbot/plugins/url.py | 6 +-- logging.ini | 21 +++++++++++ 21 files changed, 68 insertions(+), 112 deletions(-) create mode 100644 logging.ini diff --git a/distbot/__init__.py b/distbot/__init__.py index 40a96af..caef0fd 100644 --- a/distbot/__init__.py +++ b/distbot/__init__.py @@ -1 +1,3 @@ # -*- coding: utf-8 -*- +from logging.config import fileConfig +fileConfig('logging.ini') diff --git a/distbot/bot/action_worker.py b/distbot/bot/action_worker.py index 94406dc..3248f7f 100644 --- a/distbot/bot/action_worker.py +++ b/distbot/bot/action_worker.py @@ -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) - diff --git a/distbot/bot/bot.py b/distbot/bot/bot.py index 1be5023..46e577a 100644 --- a/distbot/bot/bot.py +++ b/distbot/bot/bot.py @@ -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(): diff --git a/distbot/bot/worker.py b/distbot/bot/worker.py index 647028e..d4bdcf8 100644 --- a/distbot/bot/worker.py +++ b/distbot/bot/worker.py @@ -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: diff --git a/distbot/common/action.py b/distbot/common/action.py index e0e2a52..13dfd1b 100644 --- a/distbot/common/action.py +++ b/distbot/common/action.py @@ -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') diff --git a/distbot/common/config.py b/distbot/common/config.py index 2d5d38e..d2b9b82 100644 --- a/distbot/common/config.py +++ b/distbot/common/config.py @@ -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: diff --git a/distbot/common/config/local_config.ini.spec b/distbot/common/config/local_config.ini.spec index ccc6767..c912b62 100644 --- a/distbot/common/config/local_config.ini.spec +++ b/distbot/common/config/local_config.ini.spec @@ -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) diff --git a/distbot/common/message.py b/distbot/common/message.py index a0e7b4c..1366d3e 100644 --- a/distbot/common/message.py +++ b/distbot/common/message.py @@ -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__) diff --git a/distbot/common/utils.py b/distbot/common/utils.py index 2f8b607..b3bac4d 100644 --- a/distbot/common/utils.py +++ b/distbot/common/utils.py @@ -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 diff --git a/distbot/plugins/basic.py b/distbot/plugins/basic.py index a6667fd..1376274 100644 --- a/distbot/plugins/basic.py +++ b/distbot/plugins/basic.py @@ -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__) diff --git a/distbot/plugins/bofh.py b/distbot/plugins/bofh.py index ec63322..39424a9 100644 --- a/distbot/plugins/bofh.py +++ b/distbot/plugins/bofh.py @@ -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__) diff --git a/distbot/plugins/bugtracker.py b/distbot/plugins/bugtracker.py index 08927b9..ff83dae 100644 --- a/distbot/plugins/bugtracker.py +++ b/distbot/plugins/bugtracker.py @@ -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__) diff --git a/distbot/plugins/feeds.py b/distbot/plugins/feeds.py index a839ca2..4bf537b 100644 --- a/distbot/plugins/feeds.py +++ b/distbot/plugins/feeds.py @@ -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__) diff --git a/distbot/plugins/fun.py b/distbot/plugins/fun.py index 474763f..0199773 100644 --- a/distbot/plugins/fun.py +++ b/distbot/plugins/fun.py @@ -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__) diff --git a/distbot/plugins/lookup.py b/distbot/plugins/lookup.py index c49080c..cc7e583 100644 --- a/distbot/plugins/lookup.py +++ b/distbot/plugins/lookup.py @@ -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__) diff --git a/distbot/plugins/morse.py b/distbot/plugins/morse.py index b193ab2..394b892 100644 --- a/distbot/plugins/morse.py +++ b/distbot/plugins/morse.py @@ -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__) diff --git a/distbot/plugins/plugin_help.py b/distbot/plugins/plugin_help.py index ce52450..b60374f 100644 --- a/distbot/plugins/plugin_help.py +++ b/distbot/plugins/plugin_help.py @@ -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__) diff --git a/distbot/plugins/searx.py b/distbot/plugins/searx.py index 4f20265..30fe698 100644 --- a/distbot/plugins/searx.py +++ b/distbot/plugins/searx.py @@ -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__) diff --git a/distbot/plugins/translation.py b/distbot/plugins/translation.py index c4430bd..a3c2d2d 100644 --- a/distbot/plugins/translation.py +++ b/distbot/plugins/translation.py @@ -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__) diff --git a/distbot/plugins/url.py b/distbot/plugins/url.py index f3aece5..68e3a37 100644 --- a/distbot/plugins/url.py +++ b/distbot/plugins/url.py @@ -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 index 0000000..22c4a68 --- /dev/null +++ b/logging.ini @@ -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 -- 2.39.2