mutex for events, re-enable dsa-watcher
This commit is contained in:
11
events.py
11
events.py
@@ -1,4 +1,5 @@
|
|||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
import logging
|
||||||
import time
|
import time
|
||||||
import sched
|
import sched
|
||||||
import threading
|
import threading
|
||||||
@@ -8,7 +9,7 @@ EVENTLOOP_DELAY = 0.100 # seconds
|
|||||||
event_list = sched.scheduler(time.time, time.sleep)
|
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
|
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)
|
action = callback(*func_args)
|
||||||
if action:
|
if action:
|
||||||
action_runner(action=action, plugin=plugin, msg_obj=msg_obj)
|
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)
|
event_list.enterabs(t, 0, callback, args)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -363,7 +363,8 @@ def command_teatimer(argv, **args):
|
|||||||
),
|
),
|
||||||
'event': {
|
'event': {
|
||||||
'time': ready,
|
'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))
|
msg = 'next crawl set to %s' % time.strftime('%Y-%m-%d %H:%M', time.localtime(crawl_at))
|
||||||
out.append(msg)
|
out.append(msg)
|
||||||
return {
|
return {
|
||||||
|
# 'msg': out,
|
||||||
'event': {
|
'event': {
|
||||||
'time': crawl_at,
|
'time': crawl_at,
|
||||||
'command': (command_dsa_watcher, ([],))
|
'command': (command_dsa_watcher, ([],)),
|
||||||
|
'mutex': 'dsa'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
11
urlbot.py
11
urlbot.py
@@ -12,7 +12,9 @@ from collections import deque
|
|||||||
from lxml import etree
|
from lxml import etree
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
|
from sleekxmpp.plugins import PluginNotFound
|
||||||
|
|
||||||
|
import plugins # force initialization
|
||||||
from plugin_system import plugin_storage, ptypes, plugin_enabled_get
|
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
|
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:
|
for room in self.rooms:
|
||||||
self.add_event_handler('muc::%s::got_online' % room, self.muc_online)
|
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):
|
def muc_message(self, msg_obj):
|
||||||
"""
|
"""
|
||||||
Handle muc messages, return if irrelevant content or die by hangup.
|
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")
|
other_bots = config.runtimeconf_get("other_bots")
|
||||||
if not other_bots:
|
if not other_bots:
|
||||||
return False
|
return False
|
||||||
|
try:
|
||||||
users = self.plugin['xep_0045'].getRoster(room)
|
users = self.plugin['xep_0045'].getRoster(room)
|
||||||
|
except PluginNotFound:
|
||||||
|
users = []
|
||||||
return set(users).intersection(set(other_bots))
|
return set(users).intersection(set(other_bots))
|
||||||
|
|
||||||
def _prevent_panic(message, room):
|
def _prevent_panic(message, room):
|
||||||
@@ -349,7 +357,8 @@ class UrlBot(IdleBot):
|
|||||||
args=command[1],
|
args=command[1],
|
||||||
action_runner=self._run_action,
|
action_runner=self._run_action,
|
||||||
plugin=plugin,
|
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):
|
if 'msg' in action and rate_limit(RATE_CHAT | plugin.ratelimit_class):
|
||||||
|
|||||||
Reference in New Issue
Block a user