dr.botzo/modules/Seen.py

84 lines
3.2 KiB
Python
Raw Normal View History

2010-07-28 23:47:29 -05:00
# Seen - track when a person speaks, and allow data to be queried
# Copyright (C) 2010 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/>.
2010-07-27 20:35:01 -05:00
from ConfigParser import NoOptionError
import re
2010-07-27 20:35:01 -05:00
from datetime import datetime
from dateutil.tz import *
2010-07-30 18:34:10 -05:00
from extlib import irclib
2010-07-27 20:35:01 -05:00
from Module import Module
2010-07-29 00:04:01 -05:00
# Keeps track of when people say things in public channels, and reports on when
# they last said something.
2010-07-27 20:35:01 -05:00
class Seen(Module):
# Overriding the default because we need stuff to occur before the addressed
# and so on checks.
def on_pubmsg(self, connection, event):
nick = irclib.nm_to_n(event.source())
userhost = irclib.nm_to_uh(event.source())
replypath = event.target()
what = event.arguments()[0]
admin_unlocked = False
2010-07-27 20:35:01 -05:00
# whatever it is, store it
if not self.config.has_section(self.__class__.__name__):
self.config.add_section(self.__class__.__name__)
2010-07-27 20:35:01 -05:00
self.config.set(self.__class__.__name__, nick, userhost + '|:|' + datetime.utcnow().isoformat() + '|:|' + what)
2010-07-27 20:35:01 -05:00
try:
if userhost == self.config.get('dr.botzo', 'admin_userhost'):
admin_unlocked = True
except NoOptionError: pass
# only do commands if the bot has been addressed directly
addressed_pattern = '^' + connection.get_nickname() + '[:,]?\s+'
addressed_re = re.compile(addressed_pattern)
if not addressed_re.match(what):
return
else:
what = addressed_re.sub('', what)
if replypath is not None:
what = self.try_recursion(connection, event, nick, userhost, replypath, what, admin_unlocked)
# try reloading
self.reload(connection, event, nick, userhost, replypath, what, admin_unlocked)
self.do(connection, event, nick, userhost, replypath, what, admin_unlocked)
def do(self, connection, event, nick, userhost, replypath, what, admin_unlocked):
2010-07-27 20:35:01 -05:00
whats = what.split(' ')
if whats[0] == 'seen' and len(whats) >= 2:
query = whats[1]
if query != 'debug':
try:
seendata = self.config.get(self.__class__.__name__, query).split('|:|')
2010-07-27 20:35:01 -05:00
converted = datetime.strptime(seendata[1], "%Y-%m-%dT%H:%M:%S.%f").replace(tzinfo=tzutc())
replystr = 'last saw ' + query + ' at ' + converted.astimezone(tzlocal()).strftime("%Y/%m/%d %H:%M:%S %Z") + ' saying \'' + seendata[2] + '\''
return self.reply(connection, replypath, replystr)
2010-07-27 20:35:01 -05:00
except NoOptionError: pass
2010-07-28 23:48:47 -05:00
# vi:tabstop=4:expandtab:autoindent
# kate: indent-mode python;indent-width 4;replace-tabs on;