2011-01-07 20:37:24 -06: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-28 23:47:29 -05:00
2010-07-29 00:42:44 -05:00
import re
2012-07-27 02:18:01 -05:00
2010-07-27 20:35:01 -05:00
from dateutil . tz import *
2012-07-27 20:38:45 -05:00
import MySQLdb as mdb
2010-07-27 20:35:01 -05:00
2010-07-29 00:18:20 -05:00
from Module import Module
2010-07-27 20:35:01 -05:00
class Seen ( Module ) :
2011-01-07 20:37:24 -06:00
""" 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 :
version = 1
2012-07-27 02:18:01 -05:00
cur = db . cursor ( mdb . cursors . DictCursor )
cur . execute ( '''
2011-06-15 11:07:32 -05:00
CREATE TABLE seen_nicks (
2012-07-27 11:37:29 -05:00
nick VARCHAR ( 64 ) NOT NULL ,
2012-07-27 02:18:01 -05:00
location VARCHAR ( 64 ) NOT NULL ,
host VARCHAR ( 256 ) NOT NULL ,
time TIMESTAMP DEFAULT CURRENT_TIMESTAMP ,
what LONGTEXT NOT NULL
2012-07-27 14:57:41 -05:00
) ENGINE = InnoDB CHARACTER SET utf8 COLLATE utf8_bin
2012-07-27 02:18:01 -05:00
''' )
cur . execute ( '''
2011-06-15 11:07:32 -05:00
CREATE UNIQUE INDEX seen_nicks_nick_and_location_index
2012-07-27 02:18:01 -05:00
ON seen_nicks ( nick , location )
''' )
2011-06-15 11:07:32 -05:00
db . commit ( )
self . db_register_module_version ( self . __class__ . __name__ , version )
2012-07-27 02:18:01 -05:00
except mdb . Error as e :
2011-06-15 11:07:32 -05:00
db . rollback ( )
2012-07-27 02:18:01 -05:00
self . log . error ( " database error trying to create tables " )
self . log . exception ( e )
2011-06-15 11:07:32 -05:00
raise
2012-07-27 02:18:01 -05:00
finally : cur . close ( )
2011-02-25 21:54:09 -06:00
2011-01-07 23:09:07 -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
2011-06-15 11:07:32 -05:00
where = event . target ( )
2012-07-27 02:18:01 -05:00
db = self . get_db ( )
2011-02-25 21:54:09 -06:00
# whatever it is, store it
try :
2011-06-16 21:26:13 -05:00
# if there's no where, this is probably a sub-command. don't learn it
if where :
2012-07-27 02:18:01 -05:00
cur = db . cursor ( mdb . cursors . DictCursor )
statement = ' REPLACE INTO seen_nicks (nick, location, host, what) VALUES ( %s , %s , %s , %s ) '
2012-07-27 16:34:57 -05:00
cur . execute ( statement , ( nick , where , userhost , what ) )
2011-06-16 21:26:13 -05:00
db . commit ( )
2012-07-27 02:18:01 -05:00
except mdb . Error as e :
2011-02-25 21:54:09 -06:00
db . rollback ( )
2012-07-27 02:18:01 -05:00
self . log . error ( " database error storing seen data " )
self . log . exception ( e )
2011-02-25 21:54:09 -06:00
raise
2012-07-27 02:18:01 -05:00
finally : cur . close ( )
2010-07-27 20:35:01 -05:00
2011-01-07 20:37:24 -06:00
match = re . search ( ' ^!seen \ s+( \ S+)$ ' , what )
if match :
2011-02-25 21:54:09 -06:00
nick = match . group ( 1 )
2012-07-27 02:18:01 -05:00
db = self . get_db ( )
2011-02-25 21:54:09 -06:00
try :
2012-07-27 02:18:01 -05:00
cur = db . cursor ( mdb . cursors . DictCursor )
query = ' SELECT * FROM seen_nicks WHERE nick = %s AND location = %s '
cur . execute ( query , ( nick , where ) )
result = cur . fetchone ( )
2011-02-25 21:54:09 -06:00
if result :
2012-07-27 02:18:01 -05:00
seentime = result [ ' time ' ] . replace ( tzinfo = tzlocal ( ) )
2012-07-27 16:34:57 -05:00
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 ' ] , result [ ' location ' ] )
rewrite recursion/alias code for the 500th time.
more of a moving of the code, actually, it now exists in (an overridden)
_handle_event, so that recursions happen against irc events directly,
rather than an already partially interpreted object.
with this change, modules don't need to implement do() nor do we have a
need for the internal_bus, which was doing an additional walk of the
modules after the irc event was already handled and turned into text. now
the core event handler does the recursion scans.
to support this, we bring back the old replypath trick and use it again,
so we know when to send a privmsg reply and when to return text so that
it may be chained in recursion. this feels old hat by now, but if you
haven't been following along, you should really look at the diff.
that's the meat of the change. the rest is updating modules to use
self.reply() and reimplementing (un)register_handlers where appropriate
2011-02-17 01:08:45 -06:00
return self . reply ( connection , event , replystr )
2012-07-10 17:15:52 -05:00
else :
return self . reply ( connection , event , ' i have not seen {0:s} in {1:s} . ' . format ( nick , where ) )
2012-07-27 02:18:01 -05:00
except mdb . Error as e :
2011-02-25 21:54:09 -06:00
db . rollback ( )
2012-07-27 02:18:01 -05:00
self . log . error ( " database error retrieving seen data " )
self . log . exception ( e )
2011-02-25 21:54:09 -06:00
raise
2012-07-27 02:18:01 -05:00
finally : cur . close ( )
2010-07-27 20:35:01 -05:00
2010-07-28 23:48:47 -05:00
# vi:tabstop=4:expandtab:autoindent
2010-07-28 00:11:58 -05:00
# kate: indent-mode python;indent-width 4;replace-tabs on;