2012-05-09 12:51:44 -07:00
# Written by Scaevolus 2010
2012-05-10 07:47:57 +12:00
import string
2011-11-20 22:23:31 +13:00
import re
2014-02-14 16:36:57 +13:00
from util import hook , http , text , pyexec
2012-05-13 17:11:59 +12:00
re_lineends = re . compile ( r ' [ \ r \ n]* ' )
2011-11-20 22:23:31 +13:00
2014-02-14 19:43:36 +13:00
db_ready = False
2012-03-13 07:23:41 +13:00
# some simple "shortcodes" for formatting purposes
2012-02-21 08:17:32 +13:00
shortcodes = {
2013-09-04 18:30:04 +08:00
' [b] ' : ' \x02 ' ,
' [/b] ' : ' \x02 ' ,
' [u] ' : ' \x1F ' ,
' [/u] ' : ' \x1F ' ,
' [i] ' : ' \x16 ' ,
' [/i] ' : ' \x16 ' }
2012-02-21 08:17:32 +13:00
2011-11-20 22:23:31 +13:00
def db_init ( db ) :
2014-02-14 19:43:36 +13:00
global db_ready
if not db_ready :
db . execute ( " create table if not exists mem(word, data, nick, "
" primary key(word)) " )
db . commit ( )
db_ready = True
2011-11-20 22:23:31 +13:00
def get_memory ( db , word ) :
2012-03-23 13:32:48 +13:00
row = db . execute ( " select data from mem where word=lower(?) " ,
[ word ] ) . fetchone ( )
2011-11-20 22:23:31 +13:00
if row :
return row [ 0 ]
else :
return None
2012-02-29 00:29:53 -08:00
2013-08-01 19:03:40 +12:00
@hook.command ( " r " , permissions = [ " addfactoid " ] )
@hook.command ( permissions = [ " addfactoid " ] )
2013-09-04 18:30:04 +08:00
def remember ( inp , nick = ' ' , db = None , notice = None ) :
""" remember <word> [+]<data> -- Remembers <data> with <word>. Add +
to < data > to append . """
2011-11-20 22:23:31 +13:00
db_init ( db )
append = False
try :
2012-09-05 08:09:08 +12:00
word , data = inp . split ( None , 1 )
2011-11-20 22:23:31 +13:00
except ValueError :
return remember . __doc__
2012-09-05 08:09:08 +12:00
old_data = get_memory ( db , word )
2011-11-20 22:23:31 +13:00
2012-09-05 08:09:08 +12:00
if data . startswith ( ' + ' ) and old_data :
2011-11-20 22:23:31 +13:00
append = True
2012-09-05 08:09:08 +12:00
# remove + symbol
new_data = data [ 1 : ]
# append new_data to the old_data
if len ( new_data ) > 1 and new_data [ 1 ] in ( string . punctuation + ' ' ) :
data = old_data + new_data
2011-11-20 22:23:31 +13:00
else :
2012-09-05 08:09:08 +12:00
data = old_data + ' ' + new_data
2011-11-20 22:23:31 +13:00
db . execute ( " replace into mem(word, data, nick) values "
2012-09-05 08:09:08 +12:00
" (lower(?),?,?) " , ( word , data , nick ) )
2011-11-20 22:23:31 +13:00
db . commit ( )
2012-09-05 08:09:08 +12:00
if old_data :
2011-11-20 22:23:31 +13:00
if append :
2013-09-05 09:46:49 +08:00
notice ( " Appending \x02 {} \x02 to \x02 {} \x02 " . format ( new_data , old_data ) )
2011-11-20 22:23:31 +13:00
else :
2013-09-05 09:46:49 +08:00
notice ( ' Remembering \x02 {} \x02 for \x02 {} \x02 . Type ? {} to see it. ' . format ( data , word , word ) )
notice ( ' Previous data was \x02 {} \x02 ' . format ( old_data ) )
2011-11-20 22:23:31 +13:00
else :
2013-09-05 09:46:49 +08:00
notice ( ' Remembering \x02 {} \x02 for \x02 {} \x02 . Type ? {} to see it. ' . format ( data , word , word ) )
2011-11-20 22:23:31 +13:00
2012-02-29 00:29:53 -08:00
2013-08-01 19:03:40 +12:00
@hook.command ( " f " , permissions = [ " delfactoid " ] )
@hook.command ( permissions = [ " delfactoid " ] )
2013-09-04 18:30:04 +08:00
def forget ( inp , db = None , notice = None ) :
""" forget <word> -- Forgets a remembered <word>. """
2011-11-20 22:23:31 +13:00
db_init ( db )
2012-02-03 02:05:11 +13:00
data = get_memory ( db , inp )
2011-11-20 22:23:31 +13:00
if data :
db . execute ( " delete from mem where word=lower(?) " ,
2012-02-03 02:05:11 +13:00
[ inp ] )
2011-11-20 22:23:31 +13:00
db . commit ( )
2012-03-13 07:23:41 +13:00
notice ( ' " %s " has been forgotten. ' % data . replace ( ' ` ' , " ' " ) )
2012-02-03 02:05:11 +13:00
return
2011-11-20 22:23:31 +13:00
else :
2012-02-03 02:05:11 +13:00
notice ( " I don ' t know about that. " )
return
2011-11-20 22:23:31 +13:00
2012-08-21 08:32:12 +12:00
@hook.command
2012-05-13 17:21:03 +12:00
def info ( inp , notice = None , db = None ) :
2013-09-04 18:30:04 +08:00
""" info <factoid> -- Shows the source of a factoid. """
2012-05-13 17:21:03 +12:00
db_init ( db )
# attempt to get the factoid from the database
data = get_memory ( db , inp . strip ( ) )
if data :
notice ( data )
else :
2012-08-21 08:32:12 +12:00
notice ( " Unknown Factoid. " )
2012-05-13 17:21:03 +12:00
2012-02-29 00:29:53 -08:00
2011-11-20 22:23:31 +13:00
@hook.regex ( r ' ^ \ ? ?(.+) ' )
2013-10-01 16:55:18 +13:00
def factoid ( inp , message = None , db = None , bot = None , action = None , conn = None , input = None ) :
2014-02-14 16:49:41 +13:00
""" ?<word> -- Shows what data is associated with <word>. """
2012-04-01 15:39:34 +12:00
try :
2012-04-02 09:17:55 -07:00
prefix_on = bot . config [ " plugins " ] [ " factoids " ] . get ( " prefix " , False )
2012-04-01 15:39:34 +12:00
except KeyError :
prefix_on = False
2011-11-20 22:23:31 +13:00
db_init ( db )
2012-08-21 08:32:12 +12:00
2012-05-13 17:40:17 +12:00
# split up the input
split = inp . group ( 1 ) . strip ( ) . split ( " " )
factoid_id = split [ 0 ]
2012-08-21 08:32:12 +12:00
2012-05-13 17:40:17 +12:00
if len ( split ) > = 1 :
arguments = " " . join ( split [ 1 : ] )
else :
2012-05-20 16:26:17 +12:00
arguments = " "
2011-11-20 22:23:31 +13:00
2012-05-13 17:40:17 +12:00
data = get_memory ( db , factoid_id )
2012-05-13 13:50:28 +12:00
2011-11-20 22:23:31 +13:00
if data :
2012-09-10 12:12:45 +12:00
# factoid preprocessors
2012-05-13 17:11:59 +12:00
if data . startswith ( " <py> " ) :
2012-09-10 12:12:45 +12:00
code = data [ 4 : ] . strip ( )
2013-09-05 09:46:49 +08:00
variables = ' input= " " " {} " " " ; nick= " {} " ; chan= " {} " ; bot_nick= " {} " ; ' . format ( arguments . replace ( ' " ' , ' \\ " ' ) ,
2014-02-14 17:03:08 +13:00
input . nick , input . chan ,
input . conn . nick )
2013-12-12 19:00:14 +13:00
result = pyexec . eval_py ( variables + code )
2012-05-13 17:11:59 +12:00
else :
result = data
2012-08-21 08:32:12 +12:00
2012-09-10 12:12:45 +12:00
# factoid postprocessors
2012-11-12 23:46:38 +13:00
result = text . multiword_replace ( result , shortcodes )
2012-05-13 17:11:59 +12:00
if result . startswith ( " <act> " ) :
result = result [ 5 : ] . strip ( )
2013-10-01 15:41:54 +13:00
action ( result )
2013-09-02 21:19:47 -06:00
elif result . startswith ( " <url> " ) :
url = result [ 5 : ] . strip ( )
try :
2013-10-01 16:55:18 +13:00
message ( http . get ( url ) )
2013-09-02 21:19:47 -06:00
except http . HttpError :
2013-10-01 16:55:18 +13:00
message ( " Could not fetch URL. " )
2012-03-02 15:38:32 +13:00
else :
2012-05-13 13:09:14 +12:00
if prefix_on :
2013-10-01 16:55:18 +13:00
message ( " \x02 [ {} ]: \x02 {} " . format ( factoid_id , result ) )
2012-05-13 13:09:14 +12:00
else :
2013-10-01 16:55:18 +13:00
message ( result )