diff --git a/events.py b/events.py index 13f122f..a00cf63 100644 --- a/events.py +++ b/events.py @@ -1,4 +1,5 @@ # -*- coding: utf-8 -*- +import logging import time import sched import threading @@ -8,7 +9,7 @@ EVENTLOOP_DELAY = 0.100 # seconds event_list = sched.scheduler(time.time, time.sleep) -def register_active_event(t, callback, args, action_runner, plugin, msg_obj): +def register_active_event(t, callback, args, action_runner, plugin, msg_obj, mutex=None): """ Execute a callback at a given time and react on the output @@ -24,10 +25,14 @@ def register_active_event(t, callback, args, action_runner, plugin, msg_obj): action = callback(*func_args) if action: action_runner(action=action, plugin=plugin, msg_obj=msg_obj) - event_list.enterabs(t, 0, func, args) + register_event(t, func, args, mutex=mutex) -def register_event(t, callback, args): +def register_event(t, callback, args, **kwargs): + for pending_event in event_list.queue: + if kwargs.get('mutex') and pending_event.kwargs.get('mutex', None) == kwargs.get('mutex'): + logging.debug("Dropped event: %s", kwargs.get('mutex')) + return event_list.enterabs(t, 0, callback, args) diff --git a/plugins/commands.py b/plugins/commands.py index 5bd51f7..90882d6 100644 --- a/plugins/commands.py +++ b/plugins/commands.py @@ -363,7 +363,8 @@ def command_teatimer(argv, **args): ), 'event': { 'time': ready, - 'msg': (args['reply_user'] + ': Your tea is ready!') + 'msg': (args['reply_user'] + ': Your tea is ready!'), + 'mutex': 'teatimer_{}'.format(args['reply_user']) } } @@ -629,9 +630,11 @@ def command_dsa_watcher(argv=None, **_): msg = 'next crawl set to %s' % time.strftime('%Y-%m-%d %H:%M', time.localtime(crawl_at)) out.append(msg) return { + # 'msg': out, 'event': { 'time': crawl_at, - 'command': (command_dsa_watcher, ([],)) + 'command': (command_dsa_watcher, ([],)), + 'mutex': 'dsa' } } diff --git a/urlbot.py b/urlbot.py index 96ec0d3..833bc7c 100755 --- a/urlbot.py +++ b/urlbot.py @@ -12,7 +12,9 @@ from collections import deque from lxml import etree import requests +from sleekxmpp.plugins import PluginNotFound +import plugins # force initialization from plugin_system import plugin_storage, ptypes, plugin_enabled_get from rate_limit import rate_limit_classes, RATE_GLOBAL, RATE_CHAT, RATE_EVENT, rate_limit @@ -44,6 +46,9 @@ class UrlBot(IdleBot): for room in self.rooms: self.add_event_handler('muc::%s::got_online' % room, self.muc_online) + dsa_plugin = list(filter(lambda x: x.plugin_name == 'dsa-watcher', plugin_storage[ptypes.COMMAND]))[0] + self._run_action(dsa_plugin(), dsa_plugin, None) + def muc_message(self, msg_obj): """ Handle muc messages, return if irrelevant content or die by hangup. @@ -109,7 +114,10 @@ class UrlBot(IdleBot): other_bots = config.runtimeconf_get("other_bots") if not other_bots: return False - users = self.plugin['xep_0045'].getRoster(room) + try: + users = self.plugin['xep_0045'].getRoster(room) + except PluginNotFound: + users = [] return set(users).intersection(set(other_bots)) def _prevent_panic(message, room): @@ -349,7 +357,8 @@ class UrlBot(IdleBot): args=command[1], action_runner=self._run_action, plugin=plugin, - msg_obj=msg_obj + msg_obj=msg_obj, + mutex=event.get('mutex') ) if 'msg' in action and rate_limit(RATE_CHAT | plugin.ratelimit_class):