2013-09-04 18:30:04 +08:00
""" tell.py: written by sklnd in July 2009
2010.01 .25 - modified by Scaevolus """
2011-11-20 22:23:31 +13:00
import time
import re
from util import hook , timesince
2014-03-02 19:25:28 +01:00
db_ready = [ ]
2011-11-20 22:23:31 +13:00
2014-03-02 19:25:28 +01:00
def db_init ( db , conn ) :
""" Check that our db has the tell table, create it if not. """
2014-02-14 19:43:36 +13:00
global db_ready
2014-03-02 19:25:28 +01:00
if not conn . name in db_ready :
2014-02-14 19:43:36 +13:00
db . execute ( " create table if not exists tell "
" (user_to, user_from, message, chan, time, "
" primary key(user_to, message)) " )
db . commit ( )
2014-03-02 19:25:28 +01:00
db_ready . append ( conn . name )
2011-11-20 22:23:31 +13:00
def get_tells ( db , user_to ) :
return db . execute ( " select user_from, message, time, chan from tell where "
2013-09-04 18:30:04 +08:00
" user_to=lower(?) order by time " ,
( user_to . lower ( ) , ) ) . fetchall ( )
2011-11-20 22:23:31 +13:00
@hook.singlethread
@hook.event ( ' PRIVMSG ' )
2014-03-02 19:25:28 +01:00
def tellinput ( inp , input = None , notice = None , db = None , nick = None , conn = None ) :
2011-11-20 22:23:31 +13:00
if ' showtells ' in input . msg . lower ( ) :
return
2014-03-02 19:25:28 +01:00
db_init ( db , conn )
2011-11-20 22:23:31 +13:00
2012-03-31 16:01:23 -07:00
tells = get_tells ( db , nick )
2011-11-20 22:23:31 +13:00
if tells :
user_from , message , time , chan = tells [ 0 ]
reltime = timesince . timesince ( time )
2013-09-05 10:36:25 +08:00
reply = " {} sent you a message {} ago from {} : {} " . format ( user_from , reltime , chan ,
2014-02-14 17:03:08 +13:00
message )
2011-11-20 22:23:31 +13:00
if len ( tells ) > 1 :
2013-09-05 10:36:25 +08:00
reply + = " (+ {} more, {} showtells to view) " . format ( len ( tells ) - 1 , conn . conf [ " command_prefix " ] )
2011-11-20 22:23:31 +13:00
db . execute ( " delete from tell where user_to=lower(?) and message=? " ,
2013-09-04 18:30:04 +08:00
( nick , message ) )
2011-11-20 22:23:31 +13:00
db . commit ( )
2012-03-31 09:22:06 -07:00
notice ( reply )
2011-11-20 22:23:31 +13:00
@hook.command ( autohelp = False )
2014-03-02 19:25:28 +01:00
def showtells ( inp , nick = ' ' , chan = ' ' , notice = None , db = None , conn = None ) :
2014-02-14 00:47:01 +13:00
""" showtells -- View all pending tell messages (sent in a notice). """
2011-11-20 22:23:31 +13:00
2014-03-02 19:25:28 +01:00
db_init ( db , conn )
2011-11-20 22:23:31 +13:00
tells = get_tells ( db , nick )
if not tells :
notice ( " You have no pending tells. " )
return
for tell in tells :
user_from , message , time , chan = tell
past = timesince . timesince ( time )
2013-09-05 10:36:25 +08:00
notice ( " {} sent you a message {} ago from {} : {} " . format ( user_from , past , chan , message ) )
2011-11-20 22:23:31 +13:00
db . execute ( " delete from tell where user_to=lower(?) " ,
2013-09-04 18:30:04 +08:00
( nick , ) )
2011-11-20 22:23:31 +13:00
db . commit ( )
@hook.command
2014-03-02 19:25:28 +01:00
def tell ( inp , nick = ' ' , chan = ' ' , db = None , input = None , notice = None , conn = None ) :
2013-09-04 18:30:04 +08:00
""" tell <nick> <message> -- Relay <message> to <nick> when <nick> is around. """
2011-11-20 22:23:31 +13:00
query = inp . split ( ' ' , 1 )
if len ( query ) != 2 :
2012-02-03 02:05:11 +13:00
notice ( tell . __doc__ )
2011-12-30 22:10:13 +13:00
return
2011-11-20 22:23:31 +13:00
user_to = query [ 0 ] . lower ( )
message = query [ 1 ] . strip ( )
user_from = nick
if chan . lower ( ) == user_from . lower ( ) :
chan = ' a pm '
if user_to == user_from . lower ( ) :
2012-03-31 08:57:51 -07:00
notice ( " Have you looked in a mirror lately? " )
2011-11-20 22:23:31 +13:00
return
2012-02-03 16:41:07 +13:00
if user_to . lower ( ) == input . conn . nick . lower ( ) :
2014-02-14 16:49:41 +13:00
# user is looking for us, being a smart-ass
2013-09-05 10:36:25 +08:00
notice ( " Thanks for the message, {} ! " . format ( user_from ) )
2011-11-20 22:23:31 +13:00
return
2013-09-05 16:04:03 +10:00
if not re . match ( " ^[A-Za-z0-9_|. \ - \ ] \ []*$ " , user_to . lower ( ) ) :
2014-03-02 19:25:28 +01:00
notice ( " I can ' t send a message to that user! " )
2011-11-20 22:23:31 +13:00
return
2014-03-02 19:25:28 +01:00
db_init ( db , conn )
2011-11-20 22:23:31 +13:00
if db . execute ( " select count() from tell where user_to=? " ,
2013-09-04 18:30:04 +08:00
( user_to , ) ) . fetchone ( ) [ 0 ] > = 10 :
2012-05-17 07:36:17 +12:00
notice ( " That person has too many messages queued. " )
2011-11-20 22:23:31 +13:00
return
try :
db . execute ( " insert into tell(user_to, user_from, message, chan, "
2013-09-04 18:30:04 +08:00
" time) values(?,?,?,?,?) " , ( user_to , user_from , message ,
chan , time . time ( ) ) )
2011-11-20 22:23:31 +13:00
db . commit ( )
except db . IntegrityError :
notice ( " Message has already been queued. " )
return
2012-02-03 16:41:07 +13:00
notice ( " Your message has been sent! " )