h/plugins/remember.py

67 lines
1.7 KiB
Python
Raw Normal View History

2009-04-03 03:50:38 +00:00
"""
remember.py: written by Scaevolus 2010
2009-04-03 03:50:38 +00:00
"""
from util import hook
def db_init(db):
db.execute("create table if not exists memory(chan, word, data, nick,"
" primary key(chan, word))")
db.commit()
2009-04-03 03:50:38 +00:00
def get_memory(db, chan, word):
row = db.execute("select data from memory where chan=? and word=lower(?)",
(chan, word)).fetchone()
if row:
return row[0].encode('utf8')
else:
return None
2009-04-03 03:50:38 +00:00
@hook.command
def remember(inp, nick='', chan='', db=None):
2009-04-03 03:50:38 +00:00
".remember <word> <data> -- maps word to data in the memory"
db_init(db)
try:
head, tail = inp.split(None, 1)
except ValueError:
return remember.__doc__
data = get_memory(db, chan, head)
db.execute("replace into memory(chan, word, data, nick) values"
" (?,lower(?),?,?)", (chan, head, head + ' ' + tail, nick))
db.commit()
if data:
return 'forgetting that %r, remembering this instead.' % data
else:
return 'done.'
2009-04-03 03:50:38 +00:00
@hook.command
def forget(inp, chan='', db=None):
2009-04-03 03:50:38 +00:00
".forget <word> -- forgets the mapping that word had"
if not inp:
return forget.__doc__
2009-04-03 03:50:38 +00:00
db_init(db)
data = get_memory(db, chan, inp)
if not chan.startswith('#'):
return "I won't forget anything in private."
2009-04-03 03:50:38 +00:00
if data:
db.execute("delete from memory where chan=? and word=lower(?)",
(chan, inp))
db.commit()
return 'forgot that %r' % data
else:
return "I don't know about that."
2009-04-03 03:50:38 +00:00
@hook.command(hook='\?(.+)', prefix=False)
def question(inp, chan='', say=None, db=None):
2009-04-03 03:50:38 +00:00
"?<word> -- shows what data is associated with word"
db_init(db)
2009-04-03 03:50:38 +00:00
data = get_memory(db, chan, inp)
if data:
say(data)