diff --git a/common.py b/common.py index 16d6bf8..dbe5414 100644 --- a/common.py +++ b/common.py @@ -215,4 +215,5 @@ def get_nick_from_object(message_obj): ptypes_PARSE = 'parser' ptypes_COMMAND = 'command' -ptypes = [ptypes_PARSE, ptypes_COMMAND] +ptypes_MUC_ONLINE = 'muc_online' +ptypes = [ptypes_PARSE, ptypes_COMMAND, ptypes_MUC_ONLINE] diff --git a/plugins/__init__.py b/plugins/__init__.py index 87791ff..a654b84 100644 --- a/plugins/__init__.py +++ b/plugins/__init__.py @@ -5,8 +5,8 @@ import traceback import types import config -from common import RATE_NO_LIMIT, pluginfunction, ptypes_PARSE, ptypes_COMMAND, ptypes -from plugins import commands, parsers +from common import RATE_NO_LIMIT, pluginfunction, ptypes_PARSE, ptypes_COMMAND, ptypes_MUC_ONLINE, ptypes +from plugins import commands, parsers, muc_online joblist = [] plugins = {p: [] for p in ptypes} @@ -77,6 +77,8 @@ def register(func_type): plugin_funcs = list(commands.__dict__.values()) + local_commands elif func_type == ptypes_PARSE: plugin_funcs = parsers.__dict__.values() + elif func_type == ptypes_MUC_ONLINE: + plugin_funcs = muc_online.__dict__.values() else: raise RuntimeError("invalid func type: {}".format(func_type)) @@ -107,6 +109,7 @@ def register_plugin(function, func_type): def register_all(): register(ptypes_PARSE) register(ptypes_COMMAND) + register(ptypes_MUC_ONLINE) def event_trigger(): diff --git a/plugins/muc_online.py b/plugins/muc_online.py new file mode 100644 index 0000000..d9f14fc --- /dev/null +++ b/plugins/muc_online.py @@ -0,0 +1,45 @@ +import logging + +import config +from common import ( + pluginfunction, + ptypes_MUC_ONLINE +) + +log = logging.getLogger(__name__) + + +@pluginfunction('send_record', 'delivers previously saved message to user', ptypes_MUC_ONLINE) +def send_record(**args): + arg_user = args['reply_user'] + arg_user_key = arg_user.lower() + user_records = config.runtimeconf_get('user_records') + + if arg_user_key in user_records: + records = user_records[arg_user_key] + + if not records: + return None + + response = { + 'msg': '%s, there %s %d message%s for you:\n%s' % ( + arg_user, + 'is' if len(records) == 1 else 'are', + len(records), + '' if len(records) == 1 else 's', + '\n'.join(records) + ) + } + + if config.conf_get('persistent_locked'): + log.warning("couldn't get exclusive lock") + return None + + config.conf_set('persistent_locked', True) + + user_records.pop(arg_user_key) + config.runtimeconf_persist() + + config.conf_set('persistent_locked', False) + + return response diff --git a/urlbot.py b/urlbot.py index 999ff97..9df2efb 100755 --- a/urlbot.py +++ b/urlbot.py @@ -25,6 +25,7 @@ from plugins import ( ptypes_COMMAND, plugin_enabled_get, ptypes_PARSE, + ptypes_MUC_ONLINE, register_event, register_active_event, else_command, @@ -73,46 +74,9 @@ class UrlBot(IdleBot): def muc_online(self, msg_obj): """ Hook for muc event "user joins" + :param msg_obj: """ - # don't react to yourself - if msg_obj['muc']['nick'] == self.nick: - return - - # TODO: move this to a undirected plugin, maybe new plugin type - arg_user = msg_obj['muc']['nick'] - arg_user_key = arg_user.lower() - user_records = config.runtimeconf_get('user_records') - - if arg_user_key in user_records: - records = user_records[arg_user_key] - - if not records: - return - - self.send_message( - mto=msg_obj['from'].bare, - mbody='%s, there %s %d message%s for you:\n%s' % ( - arg_user, - 'is' if len(records) == 1 else 'are', - len(records), - '' if len(records) == 1 else 's', - '\n'.join(records) - ), - mtype='groupchat' - ) - self.logger.info('sent %d offline records to room %s', - len(records), msg_obj['from'].bare) - - if config.conf_get('persistent_locked'): - self.logger.warning("couldn't get exclusive lock") - return False - - config.conf_set('persistent_locked', True) - - user_records.pop(arg_user_key) - config.runtimeconf_persist() - - config.conf_set('persistent_locked', False) + self.handle_muc_online(msg_obj) # @rate_limited(10) def send_reply(self, message, msg_obj=None): @@ -245,6 +209,28 @@ class UrlBot(IdleBot): self.message_stack.pop(0) self.message_stack.append(msg_obj) + def handle_muc_online(self, msg_obj): + """ + react to users that got online + + :param msg_obj: incoming message parameters + :return: + """ + + # don't react to yourself + if msg_obj['muc']['nick'] == self.nick: + return + + reply_user = get_nick_from_object(msg_obj) + + for plugin in plugin_storage[ptypes_MUC_ONLINE]: + if not plugin_enabled_get(plugin): + continue + + ret = plugin(reply_user=reply_user) + if ret: + self._run_action(ret, plugin, msg_obj) + def data_parse_commands(self, msg_obj): """ react to a message with the bots nick