1
0
mirror of http://aero2k.de/t/repos/urlbot-native.git synced 2017-09-06 15:25:38 +02:00

patch by braph: refactor muc_online events into own plugin class

This commit is contained in:
Thorsten
2016-04-02 00:50:29 +02:00
parent 4150137414
commit 89db85ad59
4 changed files with 77 additions and 42 deletions

View File

@@ -215,4 +215,5 @@ def get_nick_from_object(message_obj):
ptypes_PARSE = 'parser' ptypes_PARSE = 'parser'
ptypes_COMMAND = 'command' ptypes_COMMAND = 'command'
ptypes = [ptypes_PARSE, ptypes_COMMAND] ptypes_MUC_ONLINE = 'muc_online'
ptypes = [ptypes_PARSE, ptypes_COMMAND, ptypes_MUC_ONLINE]

View File

@@ -5,8 +5,8 @@ import traceback
import types import types
import config import config
from common import RATE_NO_LIMIT, pluginfunction, ptypes_PARSE, ptypes_COMMAND, ptypes from common import RATE_NO_LIMIT, pluginfunction, ptypes_PARSE, ptypes_COMMAND, ptypes_MUC_ONLINE, ptypes
from plugins import commands, parsers from plugins import commands, parsers, muc_online
joblist = [] joblist = []
plugins = {p: [] for p in ptypes} plugins = {p: [] for p in ptypes}
@@ -77,6 +77,8 @@ def register(func_type):
plugin_funcs = list(commands.__dict__.values()) + local_commands plugin_funcs = list(commands.__dict__.values()) + local_commands
elif func_type == ptypes_PARSE: elif func_type == ptypes_PARSE:
plugin_funcs = parsers.__dict__.values() plugin_funcs = parsers.__dict__.values()
elif func_type == ptypes_MUC_ONLINE:
plugin_funcs = muc_online.__dict__.values()
else: else:
raise RuntimeError("invalid func type: {}".format(func_type)) raise RuntimeError("invalid func type: {}".format(func_type))
@@ -107,6 +109,7 @@ def register_plugin(function, func_type):
def register_all(): def register_all():
register(ptypes_PARSE) register(ptypes_PARSE)
register(ptypes_COMMAND) register(ptypes_COMMAND)
register(ptypes_MUC_ONLINE)
def event_trigger(): def event_trigger():

45
plugins/muc_online.py Normal file
View File

@@ -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

View File

@@ -25,6 +25,7 @@ from plugins import (
ptypes_COMMAND, ptypes_COMMAND,
plugin_enabled_get, plugin_enabled_get,
ptypes_PARSE, ptypes_PARSE,
ptypes_MUC_ONLINE,
register_event, register_event,
register_active_event, register_active_event,
else_command, else_command,
@@ -73,46 +74,9 @@ class UrlBot(IdleBot):
def muc_online(self, msg_obj): def muc_online(self, msg_obj):
""" """
Hook for muc event "user joins" Hook for muc event "user joins"
:param msg_obj:
""" """
# don't react to yourself self.handle_muc_online(msg_obj)
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)
# @rate_limited(10) # @rate_limited(10)
def send_reply(self, message, msg_obj=None): def send_reply(self, message, msg_obj=None):
@@ -245,6 +209,28 @@ class UrlBot(IdleBot):
self.message_stack.pop(0) self.message_stack.pop(0)
self.message_stack.append(msg_obj) 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): def data_parse_commands(self, msg_obj):
""" """
react to a message with the bots nick react to a message with the bots nick