2009-04-03 03:50:38 +00:00
|
|
|
"""
|
|
|
|
remember.py: written by Scaevolus 2009
|
|
|
|
"""
|
|
|
|
|
|
|
|
import os
|
|
|
|
import thread
|
|
|
|
import codecs
|
|
|
|
|
2009-07-08 17:04:30 +00:00
|
|
|
from util import hook
|
|
|
|
|
2009-04-03 03:50:38 +00:00
|
|
|
|
|
|
|
lock = thread.allocate_lock()
|
|
|
|
memory = {}
|
|
|
|
|
2009-04-18 00:57:18 +00:00
|
|
|
|
2009-04-03 03:50:38 +00:00
|
|
|
def load_memory(filename, mtimes={}):
|
|
|
|
if not os.path.exists(filename):
|
|
|
|
return {}
|
|
|
|
mtime = os.stat(filename).st_mtime
|
|
|
|
if mtimes.get(filename, 0) != mtime:
|
|
|
|
mtimes[filename] = mtime
|
2009-04-18 00:57:18 +00:00
|
|
|
return dict((x.split(None, 1)[0].lower(), x.strip()) for x in
|
2009-04-03 03:50:38 +00:00
|
|
|
codecs.open(filename, 'r', 'utf-8'))
|
|
|
|
|
2009-04-18 00:57:18 +00:00
|
|
|
|
2009-04-03 03:50:38 +00:00
|
|
|
def save_memory(filename, memory):
|
|
|
|
out = codecs.open(filename, 'w', 'utf-8')
|
|
|
|
out.write('\n'.join(sorted(memory.itervalues())))
|
|
|
|
out.flush()
|
|
|
|
out.close()
|
|
|
|
|
2009-04-18 00:57:18 +00:00
|
|
|
|
2009-04-03 03:50:38 +00:00
|
|
|
def make_filename(dir, chan):
|
|
|
|
return os.path.join(dir, 'memory')
|
|
|
|
|
2009-04-18 00:57:18 +00:00
|
|
|
|
2009-04-03 03:50:38 +00:00
|
|
|
@hook.command
|
|
|
|
def remember(bot, input):
|
|
|
|
".remember <word> <data> -- maps word to data in the memory"
|
2009-04-18 00:57:18 +00:00
|
|
|
with lock:
|
2009-04-03 03:50:38 +00:00
|
|
|
filename = make_filename(bot.persist_dir, input.chan)
|
|
|
|
memory.setdefault(filename, load_memory(filename))
|
2009-04-18 00:57:18 +00:00
|
|
|
|
2009-04-03 03:50:38 +00:00
|
|
|
try:
|
|
|
|
head, tail = input.inp.split(None, 1)
|
|
|
|
except ValueError:
|
|
|
|
return remember.__doc__
|
2009-04-18 00:57:18 +00:00
|
|
|
|
2009-04-03 03:50:38 +00:00
|
|
|
tail = tail.strip()
|
|
|
|
low = head.lower()
|
|
|
|
if low not in memory[filename]:
|
2009-11-19 01:42:37 +00:00
|
|
|
input.reply("done.")
|
2009-04-03 03:50:38 +00:00
|
|
|
else:
|
2009-11-19 01:42:37 +00:00
|
|
|
input.reply('forgetting that "%s", remembering this instead.' %
|
2009-04-03 03:50:38 +00:00
|
|
|
memory[filename][low])
|
|
|
|
memory[filename][low] = input.inp.strip()
|
|
|
|
save_memory(filename, memory[filename])
|
|
|
|
|
2009-04-18 00:57:18 +00:00
|
|
|
|
2009-04-03 03:50:38 +00:00
|
|
|
@hook.command
|
2009-04-18 00:57:18 +00:00
|
|
|
def forget(bot, input):
|
2009-04-03 03:50:38 +00:00
|
|
|
".forget <word> -- forgets the mapping that word had"
|
|
|
|
with lock:
|
|
|
|
filename = make_filename(bot.persist_dir, input.chan)
|
|
|
|
memory.setdefault(filename, load_memory(filename))
|
|
|
|
|
|
|
|
if not input.inp.strip():
|
|
|
|
return forget.__doc__
|
2009-04-18 00:57:18 +00:00
|
|
|
|
2009-04-03 03:50:38 +00:00
|
|
|
low = input.inp.strip().lower()
|
|
|
|
if low not in memory[filename]:
|
|
|
|
return "I don't know about that."
|
|
|
|
if not hasattr(input, 'chan'):
|
|
|
|
return "I won't forget anything in private."
|
2009-11-19 01:42:37 +00:00
|
|
|
input.say("Forgot that %s" % memory[filename][low])
|
2009-04-03 03:50:38 +00:00
|
|
|
del memory[filename][low]
|
|
|
|
save_memory(filename, memory[filename])
|
|
|
|
|
2009-04-18 00:57:18 +00:00
|
|
|
|
2009-04-03 03:50:38 +00:00
|
|
|
@hook.command(hook='\?(.+)', prefix=False)
|
|
|
|
def question(bot, input):
|
|
|
|
"?<word> -- shows what data is associated with word"
|
|
|
|
with lock:
|
|
|
|
filename = make_filename(bot.persist_dir, input.chan)
|
|
|
|
memory.setdefault(filename, load_memory(filename))
|
|
|
|
|
|
|
|
word = input.inp.split()[0].lower()
|
|
|
|
if word in memory[filename]:
|
2009-11-19 01:42:37 +00:00
|
|
|
input.say("%s" % memory[filename][word])
|