2009-04-03 03:50:38 +00:00
|
|
|
"""
|
2010-02-02 05:41:51 +00:00
|
|
|
remember.py: written by Scaevolus 2010
|
2009-04-03 03:50:38 +00:00
|
|
|
"""
|
|
|
|
|
2009-07-08 17:04:30 +00:00
|
|
|
from util import hook
|
|
|
|
|
2010-03-01 02:32:41 +00:00
|
|
|
|
2010-02-02 05:41:51 +00:00
|
|
|
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
|
|
|
|
2010-03-01 02:32:41 +00:00
|
|
|
|
2010-02-02 05:41:51 +00:00
|
|
|
def get_memory(db, chan, word):
|
2010-08-24 22:14:55 +00:00
|
|
|
row = db.execute("select data from memory where chan=? and word=lower(?)",
|
2010-02-02 05:41:51 +00:00
|
|
|
(chan, word)).fetchone()
|
|
|
|
if row:
|
2010-02-23 02:01:27 +00:00
|
|
|
return row[0]
|
2010-02-02 05:41:51 +00:00
|
|
|
else:
|
|
|
|
return None
|
2009-04-18 00:57:18 +00:00
|
|
|
|
2010-03-01 02:32:41 +00:00
|
|
|
|
2009-04-03 03:50:38 +00:00
|
|
|
@hook.command
|
2011-06-10 03:51:25 +00:00
|
|
|
@hook.command("r")
|
2010-02-02 05:41:51 +00:00
|
|
|
def remember(inp, nick='', chan='', db=None):
|
2011-03-16 07:04:37 +00:00
|
|
|
".remember <word> [+]<data> -- maps word to data in the memory"
|
2010-02-02 05:41:51 +00:00
|
|
|
db_init(db)
|
|
|
|
|
2011-03-16 07:04:37 +00:00
|
|
|
append = False
|
|
|
|
|
2010-02-02 05:41:51 +00:00
|
|
|
try:
|
|
|
|
head, tail = inp.split(None, 1)
|
|
|
|
except ValueError:
|
|
|
|
return remember.__doc__
|
|
|
|
|
|
|
|
data = get_memory(db, chan, head)
|
2011-03-16 07:04:37 +00:00
|
|
|
|
2011-07-25 07:06:52 +00:00
|
|
|
if tail[0] == '+':
|
2011-03-16 07:04:37 +00:00
|
|
|
append = True
|
|
|
|
# ignore + symbol
|
|
|
|
new = tail[1:]
|
2011-07-25 07:06:52 +00:00
|
|
|
_head, _tail = data.split(None, 1)
|
2011-03-16 07:04:37 +00:00
|
|
|
# data is stored with the input so ignore it when re-adding it
|
2011-07-25 07:06:52 +00:00
|
|
|
import string
|
|
|
|
if len(tail) > 1 and tail[1] in (string.punctuation + ' '):
|
|
|
|
tail = _tail + new
|
|
|
|
else:
|
|
|
|
tail = _tail + ' ' + new
|
2011-03-16 07:04:37 +00:00
|
|
|
|
2010-02-02 05:41:51 +00:00
|
|
|
db.execute("replace into memory(chan, word, data, nick) values"
|
|
|
|
" (?,lower(?),?,?)", (chan, head, head + ' ' + tail, nick))
|
|
|
|
db.commit()
|
2011-03-16 07:04:37 +00:00
|
|
|
|
2010-02-02 05:41:51 +00:00
|
|
|
if data:
|
2011-03-16 07:04:37 +00:00
|
|
|
if append:
|
2011-07-25 07:06:52 +00:00
|
|
|
return "appending %s to %s" % (new, data.replace('"', "''"))
|
2011-03-16 07:04:37 +00:00
|
|
|
else:
|
|
|
|
return 'forgetting "%s", remembering this instead.' % \
|
|
|
|
data.replace('"', "''")
|
2010-02-02 05:41:51 +00:00
|
|
|
else:
|
|
|
|
return 'done.'
|
2009-04-18 00:57:18 +00:00
|
|
|
|
2010-03-01 02:32:41 +00:00
|
|
|
|
2009-04-03 03:50:38 +00:00
|
|
|
@hook.command
|
2011-06-10 03:51:25 +00:00
|
|
|
@hook.command("f")
|
2010-02-02 05:41:51 +00:00
|
|
|
def forget(inp, chan='', db=None):
|
2009-04-03 03:50:38 +00:00
|
|
|
".forget <word> -- forgets the mapping that word had"
|
|
|
|
|
2010-02-02 05:41:51 +00:00
|
|
|
db_init(db)
|
|
|
|
data = get_memory(db, chan, inp)
|
2009-04-18 00:57:18 +00:00
|
|
|
|
2010-02-02 05:41:51 +00:00
|
|
|
if not chan.startswith('#'):
|
|
|
|
return "I won't forget anything in private."
|
2009-04-03 03:50:38 +00:00
|
|
|
|
2010-02-02 05:41:51 +00:00
|
|
|
if data:
|
|
|
|
db.execute("delete from memory where chan=? and word=lower(?)",
|
|
|
|
(chan, inp))
|
|
|
|
db.commit()
|
2010-08-10 19:38:09 +00:00
|
|
|
return 'forgot `%s`' % data.replace('`', "'")
|
2010-02-02 05:41:51 +00:00
|
|
|
else:
|
|
|
|
return "I don't know about that."
|
2009-04-18 00:57:18 +00:00
|
|
|
|
2010-03-01 02:32:41 +00:00
|
|
|
|
2010-03-16 20:06:20 +00:00
|
|
|
@hook.regex(r'^\? ?(.+)')
|
2010-02-02 05:41:51 +00:00
|
|
|
def question(inp, chan='', say=None, db=None):
|
2009-04-03 03:50:38 +00:00
|
|
|
"?<word> -- shows what data is associated with word"
|
2010-02-02 05:41:51 +00:00
|
|
|
db_init(db)
|
2009-04-03 03:50:38 +00:00
|
|
|
|
2010-04-13 21:39:40 +00:00
|
|
|
data = get_memory(db, chan, inp.group(1).strip())
|
2010-02-02 05:41:51 +00:00
|
|
|
if data:
|
|
|
|
say(data)
|