dr.botzo/ircbot/modules/Seen.py

66 lines
2.3 KiB
Python

"""
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 <http://www.gnu.org/licenses/>.
"""
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 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()
match = re.search('^!seen\s+(\S+)$', what)
if match:
nick = match.group(1)
try:
seen_nick = SeenNick.objects.get(nick=nick, channel=where)
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(nick, where))
# vi:tabstop=4:expandtab:autoindent