From 277f4564a2a651f4394e2f8f24d542c8bb0c2274 Mon Sep 17 00:00:00 2001 From: Thorsten Date: Sat, 28 Nov 2015 13:11:22 +0100 Subject: [PATCH] conf foo, setting the jid to sth with the botclass, presence support (doesn't work in MUC) --- common.py | 10 ++++++++++ idlebot.py | 15 +++++++++------ plugins.py | 6 +++--- urlbot.py | 25 ++++++++++++++++++------- 4 files changed, 40 insertions(+), 16 deletions(-) diff --git a/common.py b/common.py index df6c78f..6a886b7 100644 --- a/common.py +++ b/common.py @@ -44,6 +44,16 @@ def conf_load(): return {} +def conf_set(key, value): + blob = conf_load() + blob[key] = value + conf_save(blob) + + +def conf_get(key): + blob = conf_load() + return blob.get(key) + Bucket = namedtuple("BucketConfig", ["history", "period", "max_hist_len"]) buckets = { diff --git a/idlebot.py b/idlebot.py index 60a4293..607b668 100755 --- a/idlebot.py +++ b/idlebot.py @@ -2,9 +2,7 @@ # -*- coding: utf-8 -*- import logging import time - import sys - from common import VERSION, EVENTLOOP_DELAY, conf_load try: @@ -18,8 +16,7 @@ except ImportError: sys.argv[0], ' ' * len(sys.argv[0]), ' ' * len(sys.argv[0]) - ) - ) + )) sys.exit(1) from sleekxmpp import ClientXMPP @@ -36,12 +33,15 @@ class IdleBot(ClientXMPP): self.add_event_handler('session_start', self.session_start) self.add_event_handler('groupchat_message', self.muc_message) + self.priority = 0 + self.status = None + self.show = None self.logger = logging.getLogger(__name__) def session_start(self, _): self.get_roster() - self.send_presence() + self.send_presence(ppriority=self.priority, pstatus=self.status, pshow=self.show) for room in self.rooms: self.logger.info('%s: joining' % room) @@ -83,8 +83,11 @@ def start(botclass, active=False): logger = logging.getLogger(__name__) logger.info(VERSION) + jid = conf('jid') + if '/' not in jid: + jid = '%s/%s' % (jid, botclass.__name__) bot = botclass( - jid=conf('jid'), + jid=jid, password=conf('password'), rooms=conf('rooms'), nick=conf('bot_user') diff --git a/plugins.py b/plugins.py index 882cbd1..77f6806 100644 --- a/plugins.py +++ b/plugins.py @@ -983,21 +983,21 @@ def recognize_bots(**args): } -@pluginfunction("set_status", "", ptypes_COMMAND) +@pluginfunction("set_status", "set bot status", ptypes_COMMAND) def set_status(argv, **args): if 'set_status' != argv[0]: return if argv[1] == 'mute' and args['reply_user'] == conf('bot_owner'): return { 'presence': { - 'status': 'AWAY', + 'status': 'xa', 'message': 'I\'m muted now. You can unmute me with "%s: set_status unmute"' % conf("bot_user") } } elif argv[1] == 'unmute' and args['reply_user'] == conf('bot_owner'): return { 'presence': { - 'status': 'ONLINE', + 'status': None, 'message': '' } } diff --git a/urlbot.py b/urlbot.py index 9930833..9ff763e 100755 --- a/urlbot.py +++ b/urlbot.py @@ -15,7 +15,7 @@ from common import ( RATE_EVENT, # rate_limited, rate_limit, - RATE_URL) + RATE_URL, conf_set) from idlebot import IdleBot, start from plugins import ( plugins as plugin_storage, @@ -49,6 +49,7 @@ class UrlBot(IdleBot): self.hist_flag = {p: True for p in rate_limit_classes} self.add_event_handler('message', self.message) + self.priority = 100 for r in self.rooms: self.add_event_handler('muc::%s::got_online' % r, self.muc_online) @@ -317,15 +318,15 @@ class UrlBot(IdleBot): if ret: self._run_action(ret, plugin, msg_obj) - def _run_action(self, plugin_action, plugin, msg_obj): + def _run_action(self, action, plugin, msg_obj): """ Execute the plugin's execution plan - :param plugin_action: dict with event and/or msg + :param action: dict with event and/or msg :param plugin: plugin obj :param msg_obj: xmpp message obj """ - if 'event' in plugin_action: - event = plugin_action["event"] + if 'event' in action: + event = action["event"] if 'msg' in event: register_event(event["time"], self.send_reply, [event['msg']]) elif 'command' in event: @@ -333,8 +334,18 @@ class UrlBot(IdleBot): if rate_limit(RATE_EVENT): register_event(event["time"], command[0], command[1]) - if 'msg' in plugin_action and rate_limit(RATE_CHAT | plugin.ratelimit_class): - self.send_reply(plugin_action['msg'], msg_obj) + if 'msg' in action and rate_limit(RATE_CHAT | plugin.ratelimit_class): + self.send_reply(action['msg'], msg_obj) + + if 'presence' in action: + presence = action['presence'] + conf_set('presence', presence) + + self.status = presence['msg'] + self.show = presence['status'] + + self.send_presence(pstatus=presence['msg'], pshow=presence['status']) + # self.reconnect(wait=True) if '__main__' == __name__: start(UrlBot, True)