countdown: use new event addressed stuff

lets countdown.ircplugin use the regex handler and do less checking
itself
This commit is contained in:
Brian S. Stephan 2017-03-10 18:18:39 -06:00
parent 1e428b77db
commit 55c1cf01a2
1 changed files with 60 additions and 77 deletions

View File

@ -5,14 +5,13 @@ import re
import threading import threading
import time import time
import irc.client
import parsedatetime as pdt import parsedatetime as pdt
from dateutil.parser import parse from dateutil.parser import parse
from dateutil.relativedelta import relativedelta from dateutil.relativedelta import relativedelta
from django.utils import timezone from django.utils import timezone
from countdown.models import CountdownItem from countdown.models import CountdownItem
from ircbot.lib import Plugin, reply_destination_for_event from ircbot.lib import Plugin, most_specific_message
log = logging.getLogger('countdown.ircplugin') log = logging.getLogger('countdown.ircplugin')
@ -45,10 +44,8 @@ class Countdown(Plugin):
self.handle_item_list, -20) self.handle_item_list, -20)
self.connection.reactor.add_global_regex_handler(['pubmsg', 'privmsg'], r'^!countdown\s+(\S+)$', self.connection.reactor.add_global_regex_handler(['pubmsg', 'privmsg'], r'^!countdown\s+(\S+)$',
self.handle_item_detail, -20) self.handle_item_detail, -20)
self.connection.reactor.add_global_regex_handler(['pubmsg', 'privmsg'], self.new_reminder_regex,
# let this interrupt markov self.handle_new_reminder, -50)
self.connection.add_global_handler('pubmsg', self.handle_new_reminder, -50)
self.connection.add_global_handler('privmsg', self.handle_new_reminder, -50)
super(Countdown, self).start() super(Countdown, self).start()
@ -59,9 +56,7 @@ class Countdown(Plugin):
self.connection.reactor.remove_global_regex_handler(['pubmsg', 'privmsg'], self.handle_item_list) self.connection.reactor.remove_global_regex_handler(['pubmsg', 'privmsg'], self.handle_item_list)
self.connection.reactor.remove_global_regex_handler(['pubmsg', 'privmsg'], self.handle_item_detail) self.connection.reactor.remove_global_regex_handler(['pubmsg', 'privmsg'], self.handle_item_detail)
self.connection.reactor.remove_global_regex_handler(['pubmsg', 'privmsg'], self.handle_new_reminder)
self.connection.remove_global_handler('pubmsg', self.handle_new_reminder)
self.connection.remove_global_handler('privmsg', self.handle_new_reminder)
super(Countdown, self).stop() super(Countdown, self).stop()
@ -94,83 +89,71 @@ class Countdown(Plugin):
reminder.save() reminder.save()
time.sleep(1) time.sleep(1)
def handle_new_reminder(self, connection, event): def handle_new_reminder(self, connection, event, match):
"""Watch IRC for requests to remind new things, create countdown items.""" """Watch IRC for requests to remind new things, create countdown items."""
what = event.arguments[0] if event.in_privmsg or event.addressed:
my_nick = connection.get_nickname() log.debug("%s is a new reminder request", most_specific_message(event))
addressed_my_nick = r'^{0:s}[:,]\s+'.format(my_nick) who = match.group('who')
sender_nick = irc.client.NickMask(event.source).nick when_type = match.group('when_type')
sent_location = reply_destination_for_event(event) when = match.group('when')
recurring_period = match.group('recurring_period')
recurring_until = match.group('recurring_until')
text = match.group('text')
log.debug("%s / %s / %s", who, when, text)
in_privmsg = sender_nick == sent_location item_name = '{0:s}-{1:s}'.format(event.sender_nick, timezone.now().strftime('%s'))
if re.search(addressed_my_nick, what, re.IGNORECASE) is not None or in_privmsg: # parse when to send the notification
# we were addressed, were we told to add a reminder? if when_type == 'in':
trimmed_what = re.sub(addressed_my_nick, '', what) # relative time
log.debug(trimmed_what) calendar = pdt.Calendar()
when_t = calendar.parseDT(when, timezone.localtime(timezone.now()),
tzinfo=timezone.get_current_timezone())[0]
else:
# absolute time
when_t = timezone.make_aware(parse(when))
match = re.match(self.new_reminder_regex, trimmed_what, re.IGNORECASE) # parse the person to address, if anyone, when sending the notification
if match: if who == 'us' or who == event.sent_location or event.in_privmsg:
log.debug("%s is a new reminder request", trimmed_what) message = text
who = match.group('who') elif who == 'me':
when_type = match.group('when_type') message = '{0:s}: {1:s}'.format(event.sender_nick, text)
when = match.group('when') else:
recurring_period = match.group('recurring_period') message = '{0:s}: {1:s}'.format(who, text)
recurring_until = match.group('recurring_until')
text = match.group('text')
log.debug("%s / %s / %s", who, when, text)
item_name = '{0:s}-{1:s}'.format(sender_nick, timezone.now().strftime('%s')) # replace pronouns and stuff
if who == 'me':
message = re.sub(r'\bI\b', r'you', message, flags=re.IGNORECASE)
message = re.sub(r'\bme\b', r'you', message, flags=re.IGNORECASE)
message = re.sub(r'\bmy\b', r'your', message, flags=re.IGNORECASE)
message = re.sub(r'\bmyself\b', r'yourself', message, flags=re.IGNORECASE)
elif who == 'us':
message = re.sub(r'\bwe\b', r'you', message, flags=re.IGNORECASE)
message = re.sub(r'\bus\b', r'you', message, flags=re.IGNORECASE)
message = re.sub(r'\bour\b', r'your', message, flags=re.IGNORECASE)
message = re.sub(r'\bourselves\b', r'yourselves', message, flags=re.IGNORECASE)
# parse when to send the notification log.debug("%s / %s / %s", item_name, when_t, message)
if when_type == 'in':
# relative time
calendar = pdt.Calendar()
when_t = calendar.parseDT(when, timezone.localtime(timezone.now()),
tzinfo=timezone.get_current_timezone())[0]
else:
# absolute time
when_t = timezone.make_aware(parse(when))
# parse the person to address, if anyone, when sending the notification countdown_item = CountdownItem.objects.create(name=item_name, at_time=when_t, is_reminder=True,
if who == 'us' or who == sent_location or in_privmsg: reminder_message=message, reminder_target=event.sent_location)
message = text if recurring_period:
elif who == 'me': countdown_item.recurring_period = recurring_period
message = '{0:s}: {1:s}'.format(sender_nick, text) if recurring_until:
else: recurring_until_t = timezone.make_aware(parse(recurring_until))
message = '{0:s}: {1:s}'.format(who, text) countdown_item.recurring_until = recurring_until_t
countdown_item.save()
log.info("created countdown item %s", str(countdown_item))
# replace pronouns and stuff if event.in_privmsg:
if who == 'me': self.bot.reply(event, "ok, i'll message you at {0:s} 14[{1:s}]".format(str(when_t),
message = re.sub(r'\bI\b', r'you', message, flags=re.IGNORECASE) countdown_item.name))
message = re.sub(r'\bme\b', r'you', message, flags=re.IGNORECASE) else:
message = re.sub(r'\bmy\b', r'your', message, flags=re.IGNORECASE) self.bot.reply(event, "ok, i'll message {0:s} at {1:s} 14[{2:s}]".format(event.sent_location,
message = re.sub(r'\bmyself\b', r'yourself', message, flags=re.IGNORECASE) str(when_t),
elif who == 'us': countdown_item.name))
message = re.sub(r'\bwe\b', r'you', message, flags=re.IGNORECASE)
message = re.sub(r'\bus\b', r'you', message, flags=re.IGNORECASE)
message = re.sub(r'\bour\b', r'your', message, flags=re.IGNORECASE)
message = re.sub(r'\bourselves\b', r'yourselves', message, flags=re.IGNORECASE)
log.debug("%s / %s / %s", item_name, when_t, message) return 'NO MORE'
countdown_item = CountdownItem.objects.create(name=item_name, at_time=when_t, is_reminder=True,
reminder_message=message, reminder_target=sent_location)
if recurring_period:
countdown_item.recurring_period = recurring_period
if recurring_until:
recurring_until_t = timezone.make_aware(parse(recurring_until))
countdown_item.recurring_until = recurring_until_t
countdown_item.save()
log.info("created countdown item %s", str(countdown_item))
if in_privmsg:
self.bot.reply(event, "ok, i'll message you at {0:s} 14[{1:s}]".format(str(when_t), countdown_item.name))
else:
self.bot.reply(event, "ok, i'll message {0:s} at {1:s} 14[{2:s}]".format(sent_location, str(when_t),
countdown_item.name))
return 'NO MORE'
def handle_item_detail(self, connection, event, match): def handle_item_detail(self, connection, event, match):
"""Provide the details of one countdown item.""" """Provide the details of one countdown item."""