patch by braph: use Lib/sched.py instead of own scheduler
This commit is contained in:
11
idlebot.py
11
idlebot.py
@@ -18,6 +18,7 @@ class IdleBot(ClientXMPP):
|
|||||||
|
|
||||||
self.add_event_handler('session_start', self.session_start)
|
self.add_event_handler('session_start', self.session_start)
|
||||||
self.add_event_handler('groupchat_message', self.muc_message)
|
self.add_event_handler('groupchat_message', self.muc_message)
|
||||||
|
self.add_event_handler('disconnected', self.disconnected)
|
||||||
self.priority = 0
|
self.priority = 0
|
||||||
self.status = None
|
self.status = None
|
||||||
self.show = None
|
self.show = None
|
||||||
@@ -26,6 +27,9 @@ class IdleBot(ClientXMPP):
|
|||||||
for room in self.rooms:
|
for room in self.rooms:
|
||||||
self.add_event_handler('muc::%s::got_offline' % room, self.muc_offline)
|
self.add_event_handler('muc::%s::got_offline' % room, self.muc_offline)
|
||||||
|
|
||||||
|
def disconnected(self, _):
|
||||||
|
exit(0)
|
||||||
|
|
||||||
def session_start(self, _):
|
def session_start(self, _):
|
||||||
self.get_roster()
|
self.get_roster()
|
||||||
self.send_presence(ppriority=self.priority, pstatus=self.status, pshow=self.show)
|
self.send_presence(ppriority=self.priority, pstatus=self.status, pshow=self.show)
|
||||||
@@ -113,12 +117,7 @@ def start(botclass, active=False):
|
|||||||
|
|
||||||
while 1:
|
while 1:
|
||||||
try:
|
try:
|
||||||
# print("hangup: %s" % got_hangup)
|
plugins.joblist.run(False)
|
||||||
if not plugins.event_trigger():
|
|
||||||
bot.hangup()
|
|
||||||
if bot.state.current_state() == 'disconnected':
|
|
||||||
exit(0)
|
|
||||||
|
|
||||||
time.sleep(EVENTLOOP_DELAY)
|
time.sleep(EVENTLOOP_DELAY)
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
print('')
|
print('')
|
||||||
|
|||||||
@@ -3,12 +3,13 @@ import logging
|
|||||||
import time
|
import time
|
||||||
import traceback
|
import traceback
|
||||||
import types
|
import types
|
||||||
|
import sched
|
||||||
|
|
||||||
import config
|
import config
|
||||||
from common import RATE_NO_LIMIT, pluginfunction, config_locked, ptypes_PARSE, ptypes_COMMAND, ptypes_MUC_ONLINE, ptypes
|
from common import RATE_NO_LIMIT, pluginfunction, config_locked, ptypes_PARSE, ptypes_COMMAND, ptypes_MUC_ONLINE, ptypes
|
||||||
from plugins import commands, parsers, muc_online
|
from plugins import commands, parsers, muc_online
|
||||||
|
|
||||||
joblist = []
|
joblist = sched.scheduler(time.time, time.sleep)
|
||||||
plugins = {p: [] for p in ptypes}
|
plugins = {p: [] for p in ptypes}
|
||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
@@ -47,11 +48,11 @@ 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)
|
||||||
joblist.append((t, func, args))
|
joblist.enterabs(t, 0, func, args)
|
||||||
|
|
||||||
|
|
||||||
def register_event(t, callback, args):
|
def register_event(t, callback, args):
|
||||||
joblist.append((t, callback, args))
|
joblist.enterabs(t, 0, callback, args)
|
||||||
|
|
||||||
|
|
||||||
def else_command(args):
|
def else_command(args):
|
||||||
@@ -108,19 +109,6 @@ def register_all():
|
|||||||
register(ptypes_MUC_ONLINE)
|
register(ptypes_MUC_ONLINE)
|
||||||
|
|
||||||
|
|
||||||
def event_trigger():
|
|
||||||
if 0 == len(joblist):
|
|
||||||
return True
|
|
||||||
|
|
||||||
now = time.time()
|
|
||||||
|
|
||||||
for (i, (t, callback, args)) in enumerate(joblist):
|
|
||||||
if t < now:
|
|
||||||
callback(*args)
|
|
||||||
del (joblist[i])
|
|
||||||
return True
|
|
||||||
|
|
||||||
|
|
||||||
@pluginfunction('help', 'print help for a command or all known commands', ptypes_COMMAND)
|
@pluginfunction('help', 'print help for a command or all known commands', ptypes_COMMAND)
|
||||||
def command_help(argv, **args):
|
def command_help(argv, **args):
|
||||||
what = argv[0] if argv else None
|
what = argv[0] if argv else None
|
||||||
@@ -236,5 +224,7 @@ def reset_jobs(argv, **args):
|
|||||||
if args['reply_user'] != config.conf_get('bot_owner'):
|
if args['reply_user'] != config.conf_get('bot_owner'):
|
||||||
return
|
return
|
||||||
else:
|
else:
|
||||||
joblist.clear()
|
for event in joblist.queue:
|
||||||
|
joblist.cancel(event)
|
||||||
|
|
||||||
return {'msg': 'done.'}
|
return {'msg': 'done.'}
|
||||||
|
|||||||
Reference in New Issue
Block a user