dr.botzo/modules/Seen.py

125 lines
4.8 KiB
Python
Raw Normal View History

"""
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-28 23:47:29 -05:00
import re
2011-02-25 23:09:41 -06:00
import sqlite3
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-27 20:35:01 -05:00
class Seen(Module):
"""Track when people say things in public channels, and report on it."""
2011-02-25 21:54:09 -06:00
def db_init(self):
"""Create the table to store seen data."""
version = self.db_module_registered(self.__class__.__name__)
if version == None:
db = self.get_db()
try:
db.execute('''
CREATE TABLE seen_nicks (
nick TEXT NOT NULL PRIMARY KEY,
host TEXT NOT NULL,
time TEXT DEFAULT CURRENT_TIMESTAMP,
what TEXT NOT NULL
)''')
sql = 'INSERT INTO drbotzo_modules VALUES (?,?)'
db.execute(sql, (self.__class__.__name__, 1))
db.commit()
db.close()
2011-02-25 21:54:09 -06:00
version = 1
except sqlite3.Error as e:
db.rollback()
db.close()
self.log.error("sqlite error: " + str(e))
2011-02-25 21:54:09 -06:00
raise
if version < 2:
db = self.get_db()
try:
version = 2
db.execute('''DROP TABLE seen_nicks''')
db.execute('''
CREATE TABLE seen_nicks (
nick TEXT NOT NULL,
location TEXT NOT NULL,
host TEXT NOT NULL,
time TEXT DEFAULT CURRENT_TIMESTAMP,
what TEXT NOT NULL
)''')
db.execute('''
CREATE UNIQUE INDEX seen_nicks_nick_and_location_index
ON seen_nicks (nick, location)''')
db.commit()
db.close()
self.db_register_module_version(self.__class__.__name__, version)
except sqlite3.Error as e:
db.rollback()
db.close()
self.log.error("sqlite error: " + str(e))
raise
2011-02-25 21:54:09 -06:00
def do(self, connection, event, nick, userhost, what, admin_unlocked):
2011-02-25 21:54:09 -06:00
"""Track pubmsg/privmsg events, and if asked, report on someone."""
2010-07-27 20:35:01 -05:00
where = event.target()
2011-02-25 21:54:09 -06:00
# whatever it is, store it
try:
# if there's no where, this is probably a sub-command. don't learn it
if where:
db = self.get_db()
cur = db.cursor()
statement = 'REPLACE INTO seen_nicks (nick, location, host, what) VALUES (?, ?, ?, ?)'
cur.execute(statement, (nick, where, userhost, what.decode('utf-8', 'replace')))
db.commit()
db.close()
2011-02-25 21:54:09 -06:00
except sqlite3.Error as e:
db.rollback()
db.close()
self.log.error("sqlite error: " + str(e))
2011-02-25 21:54:09 -06:00
raise
2010-07-27 20:35:01 -05:00
match = re.search('^!seen\s+(\S+)$', what)
if match:
2011-02-25 21:54:09 -06:00
nick = match.group(1)
try:
db = self.get_db()
query = 'SELECT * FROM seen_nicks WHERE nick = ? AND location = ?'
cursor = db.execute(query, (nick,where))
2011-02-25 21:54:09 -06:00
result = cursor.fetchone()
db.close()
2011-02-25 21:54:09 -06:00
if result:
seentime = datetime.strptime(result['time'], '%Y-%m-%d %H:%M:%S').replace(tzinfo=tzutc())
replystr = 'last saw {0:s} in {3:s} at {1:s} saying \'{2:s}\'.'.format(result['nick'], seentime.astimezone(tzlocal()).strftime('%Y/%m/%d %H:%M:%S %Z'), result['what'].encode('utf-8', 'ignore'), result['location'].encode('utf-8', 'ignore'))
return self.reply(connection, event, replystr)
else:
return self.reply(connection, event, 'i have not seen {0:s} in {1:s}.'.format(nick, where))
2011-02-25 21:54:09 -06:00
except sqlite3.Error as e:
db.rollback()
db.close()
self.log.error("sqlite error: " + str(e))
2011-02-25 21:54:09 -06:00
raise
2010-07-27 20:35:01 -05:00
2010-07-28 23:48:47 -05:00
# vi:tabstop=4:expandtab:autoindent
# kate: indent-mode python;indent-width 4;replace-tabs on;