""" Seen - track when a person speaks, and allow data to be queried Copyright (C) 2014 Brian S. Stephan This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . """ import re from django.utils import timezone from seen.models import SeenNick from Module import Module class Seen(Module): """Track when people say things in public channels, and report on it.""" def register_handlers(self): """Hook handler functions into the IRC library.""" self.irc.add_global_handler('pubmsg', self.on_pub_or_privmsg) self.irc.add_global_handler('privmsg', self.on_pub_or_privmsg) self.irc.add_global_regex_handler(['pubmsg', 'privmsg'], r'^!seen\s+(\S+)$', self.seen) def unregister_handlers(self): """Unhook handler functions into the IRC library.""" self.irc.remove_global_handler('pubmsg', self.on_pub_or_privmsg) self.irc.remove_global_handler('privmsg', self.on_pub_or_privmsg) self.irc.remove_global_regex_handler(['pubmsg', 'privmsg'], r'^!seen\s+(\S+)$', self.seen) def do(self, connection, event, nick, userhost, what, admin_unlocked): """Track pubmsg/privmsg events, and if asked, report on someone.""" where = event.target() # store the event. only learn events with real wheres if where: try: seen_nick = SeenNick.objects.get(nick=nick, channel=where) except SeenNick.DoesNotExist: seen_nick = SeenNick() seen_nick.nick = nick seen_nick.channel = where seen_nick.host = userhost seen_nick.what = what seen_nick.seen_time = timezone.now() seen_nick.save() def seen(self, nick, userhost, event, from_admin, groups): """Report on when a nick was last seen in a channel.""" query_nick, = groups source = event.target() try: seen_nick = SeenNick.objects.get(nick=query_nick, channel=source) local_time = timezone.localtime(seen_nick.seen_time).strftime('%Y-%m-%d %H:%M:%S %Z') return self.irc.reply(event, "last saw {0:s} in {1:s} at {2:s} saying '{3:s}'." "".format(seen_nick.nick, seen_nick.channel, local_time, seen_nick.what)) except SeenNick.DoesNotExist: return self.irc.reply(event, "i have not seen {0:s} in {1:s}.".format(query_nick, source)) # vi:tabstop=4:expandtab:autoindent