From df91495652da531c2b308f23da42236ba898b574 Mon Sep 17 00:00:00 2001 From: "Brian S. Stephan" Date: Sun, 25 Jul 2010 21:51:31 -0500 Subject: [PATCH] making seen stuff a Module --- dr.botzo.py | 72 +++++++++++++++++++++++++---------------------------- 1 file changed, 34 insertions(+), 38 deletions(-) diff --git a/dr.botzo.py b/dr.botzo.py index ff33416..2bc2291 100755 --- a/dr.botzo.py +++ b/dr.botzo.py @@ -261,6 +261,36 @@ class Dice(Module): connection.privmsg(replypath, result) +class Seen(Module): + """Keeps track of when people say things in public channels, and reports on when + they last said something. + """ + + def __init__(self, config, server): + super(Seen, self).__init__(config, server) + + def register_handlers(self, server): + server.add_global_handler('pubmsg', self.on_pubmsg) + server.add_global_handler('privmsg', self.on_privmsg) + + def do(self, connection, event, nick, userhost, replypath, what, admin_unlocked): + # whatever it is, store it + if not self.config.has_section('seen'): + self.config.add_section('seen') + + self.config.set('seen', nick, userhost + '|:|' + datetime.utcnow().isoformat() + '|:|' + what) + + # also see if it's a query + whats = what.split(' ') + if whats[0] == 'seen' and len(whats) >= 2: + query = whats[1] + if query != 'debug': + try: + seendata = self.config.get('seen', query).split('|:|') + converted = datetime.strptime(seendata[1], "%Y-%m-%dT%H:%M:%S.%f").replace(tzinfo=tzutc()) + connection.privmsg(replypath, 'last saw ' + query + ' at ' + converted.astimezone(tzlocal()).strftime("%Y/%m/%d %H:%M:%S %Z") + ' saying \'' + seendata[2] + '\'') + except NoOptionError: pass + ##### # sub_join_channel # join a channel when told to by an admin @@ -329,33 +359,6 @@ def sub_autojoin_manipulate(connection, event, nick, userhost, replypath, what, connection.privmsg(replypath, 'removed ' + channel + ' from autojoin') except NoOptionError: pass -##### -# sub_add_to_seen -# when someone says a pubmsg, keep it in the config -##### - -def sub_add_to_seen(connection, event, nick, userhost, what): - if not config.has_section('seen'): - config.add_section('seen') - - config.set('seen', nick, userhost + '|:|' + datetime.utcnow().isoformat() + '|:|' + what) - -##### -# sub_report_seen -# report when a person has been seen, based on config -##### - -def sub_report_seen(connection, event, nick, userhost, replypath, what, admin_unlocked): - whats = what.split(' ') - if whats[0] == 'seen' and len(whats) >= 2: - query = whats[1] - if query != 'debug': - try: - seendata = config.get('seen', query).split('|:|') - converted = datetime.strptime(seendata[1], "%Y-%m-%dT%H:%M:%S.%f").replace(tzinfo=tzutc()) - connection.privmsg(replypath, 'last saw ' + query + ' at ' + converted.astimezone(tzlocal()).strftime("%Y/%m/%d %H:%M:%S %Z") + ' saying \'' + seendata[2] + '\'') - except NoOptionError: pass - ##### # sub_save_config # save the config file @@ -427,9 +430,6 @@ def on_privmsg(connection, event): sub_save_config(connection, event, nick, userhost, replypath, what, admin_unlocked) sub_change_nick(connection, event, nick, userhost, replypath, what, admin_unlocked) - # standard commands - sub_report_seen(connection, event, nick, userhost, replypath, what, admin_unlocked) - ##### # on_pubmsg # public messages in a channel where the bot is @@ -448,8 +448,6 @@ def on_pubmsg(connection, event): admin_unlocked = True except NoOptionError: pass - sub_add_to_seen(connection, event, nick, userhost, what) - # only do commands if the bot has been addressed directly addressed_pattern = '^' + connection.get_nickname() + '[:,]?\s+' addressed_re = re.compile(addressed_pattern) @@ -467,9 +465,6 @@ def on_pubmsg(connection, event): sub_save_config(connection, event, nick, userhost, replypath, what, admin_unlocked) sub_change_nick(connection, event, nick, userhost, replypath, what, admin_unlocked) - # standard commands - sub_report_seen(connection, event, nick, userhost, replypath, what, admin_unlocked) - ##### # init ##### @@ -505,9 +500,10 @@ server.add_global_handler("welcome", on_connect) server.add_global_handler('privmsg', on_privmsg) server.add_global_handler('pubmsg', on_pubmsg) -count = Countdown(config, server) -dice = Dice(config, server) -gt = GoogleTranslate(config, server) +count = Countdown(config, server) +dice = Dice(config, server) +gt = GoogleTranslate(config, server) +seen = Seen(config, server) # loop forever irc.process_forever()