diff --git a/contrib/boobot.py b/contrib/boobot.py index 6defa6cb57878305aa1cf568781d083b710741fd..1c6dbaca736427b6c2a50f754eb77aa66a4b7e33 100755 --- a/contrib/boobot.py +++ b/contrib/boobot.py @@ -21,6 +21,7 @@ from __future__ import print_function from datetime import datetime, timedelta +from dateutil.parser import parse as parse_date import logging import re import os @@ -140,6 +141,13 @@ def human_size(self, size): return '0 B' +class Task(object): + def __init__(self, datetime, message, channel=None): + self.datetime = datetime + self.message = message + self.channel = channel + + class MyThread(Thread): daemon = True @@ -154,6 +162,7 @@ def run(self): for ev in self.bot.joined.itervalues(): ev.wait() + self.weboob.repeat(5, self.check_tasks) self.weboob.repeat(300, self.check_board) self.weboob.repeat(600, self.check_dlfp) self.weboob.repeat(600, self.check_twitter) @@ -212,6 +221,12 @@ def iter_messages(backend): message = msg.message.replace(word, '\002%s\002' % word) self.bot.send_message('[DLFP] <%s> %s' % (msg.login, message)) + def check_tasks(self): + for task in list(self.bot.tasks_queue): + if task.datetime < datetime.now(): + self.bot.send_message(task.message, task.channel) + self.bot.tasks_queue.remove(task) + def stop(self): self.weboob.want_stop() self.weboob.deinit() @@ -231,6 +246,8 @@ def __init__(self, channels, nickname, server, port=6667): self.weboob = None self.storage = None + self.tasks_queue = [] + def set_weboob(self, weboob): self.weboob = weboob self.storage = ApplicationStorage('boobot', weboob.storage) @@ -250,7 +267,8 @@ def on_join(self, c, event): def send_message(self, msg, channel=None): for m in msg.splitlines(): - self.connection.privmsg(to_unicode(channel or self.mainchannel), to_unicode(m)[:450]) + msg = to_unicode(m).encode('utf-8')[:450].decode('utf-8') + self.connection.privmsg(to_unicode(channel or self.mainchannel), msg) def on_pubmsg(self, c, event): # irclib 5.0 compatibility @@ -276,6 +294,21 @@ def on_pubmsg(self, c, event): if m and hasattr(self, 'cmd_%s' % m.groupdict()['cmd']): getattr(self, 'cmd_%s' % m.groupdict()['cmd'])(nick, channel, m.groupdict()['args'].strip()) + def cmd_at(self, nick, channel, text): + try: + datetime, message = text.split(' ', 1) + except ValueError: + self.send_message('Syntax: %at [YYYY-MM-DDT]HH:MM[:SS] message', channel) + return + + try: + datetime = parse_date(datetime) + except ValueError: + self.send_message('Unable to read date %r' % datetime) + return + + self.tasks_queue.append(Task(datetime, message, channel)) + def cmd_addquote(self, nick, channel, text): quotes = self.storage.get(channel, 'quotes', default=[]) quotes.append({'author': nick, 'timestamp': datetime.now(), 'text': text})