2012-05-09 21:51:44 +02:00
|
|
|
# Written by Scaevolus 2010
|
2012-05-13 07:11:59 +02:00
|
|
|
from util import hook, http
|
2012-05-09 21:47:57 +02:00
|
|
|
import string
|
2011-11-20 10:23:31 +01:00
|
|
|
import re
|
|
|
|
|
2012-05-13 07:11:59 +02:00
|
|
|
re_lineends = re.compile(r'[\r\n]*')
|
2011-11-20 10:23:31 +01:00
|
|
|
|
2012-03-12 19:23:41 +01:00
|
|
|
# some simple "shortcodes" for formatting purposes
|
2012-02-20 20:17:32 +01:00
|
|
|
shortcodes = {
|
2012-05-13 03:50:28 +02:00
|
|
|
'[b]': '\x02',
|
|
|
|
'[/b]': '\x02',
|
|
|
|
'[u]': '\x1F',
|
|
|
|
'[/u]': '\x1F',
|
|
|
|
'[i]': '\x16',
|
|
|
|
'[/i]': '\x16'}
|
2012-02-20 20:17:32 +01:00
|
|
|
|
|
|
|
|
2011-11-20 10:23:31 +01:00
|
|
|
def db_init(db):
|
|
|
|
db.execute("create table if not exists mem(word, data, nick,"
|
|
|
|
" primary key(word))")
|
|
|
|
db.commit()
|
|
|
|
|
|
|
|
|
|
|
|
def get_memory(db, word):
|
|
|
|
|
2012-03-23 01:32:48 +01:00
|
|
|
row = db.execute("select data from mem where word=lower(?)",
|
|
|
|
[word]).fetchone()
|
2011-11-20 10:23:31 +01:00
|
|
|
if row:
|
|
|
|
return row[0]
|
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
2012-02-29 09:29:53 +01:00
|
|
|
|
2012-02-20 20:17:32 +01:00
|
|
|
def multiwordReplace(text, wordDic):
|
|
|
|
"""
|
|
|
|
take a text and replace words that match a key in a dictionary with
|
|
|
|
the associated value, return the changed text
|
|
|
|
"""
|
|
|
|
rc = re.compile('|'.join(map(re.escape, wordDic)))
|
2012-02-29 09:29:53 +01:00
|
|
|
|
2012-02-20 20:17:32 +01:00
|
|
|
def translate(match):
|
|
|
|
return wordDic[match.group(0)]
|
|
|
|
return rc.sub(translate, text)
|
|
|
|
|
2011-11-20 10:23:31 +01:00
|
|
|
|
2012-03-12 21:31:41 +01:00
|
|
|
@hook.command("r", adminonly=True)
|
|
|
|
@hook.command(adminonly=True)
|
2011-11-20 10:23:31 +01:00
|
|
|
def remember(inp, nick='', db=None, say=None, input=None, notice=None):
|
2012-05-16 05:07:27 +02:00
|
|
|
"remember <word> [+]<data> -- Remembers <data> with <word>. Add +"
|
2012-03-23 01:32:48 +01:00
|
|
|
" to <data> to append."
|
2011-11-20 10:23:31 +01:00
|
|
|
db_init(db)
|
|
|
|
|
|
|
|
append = False
|
|
|
|
|
|
|
|
try:
|
2011-11-25 16:42:05 +01:00
|
|
|
head, tail = inp.split(None, 1)
|
2011-11-20 10:23:31 +01:00
|
|
|
except ValueError:
|
|
|
|
return remember.__doc__
|
|
|
|
|
|
|
|
data = get_memory(db, head)
|
|
|
|
|
|
|
|
if tail[0] == '+':
|
|
|
|
append = True
|
|
|
|
# ignore + symbol
|
|
|
|
new = tail[1:]
|
|
|
|
_head, _tail = data.split(None, 1)
|
|
|
|
# data is stored with the input so ignore it when re-adding it
|
|
|
|
if len(tail) > 1 and tail[1] in (string.punctuation + ' '):
|
|
|
|
tail = _tail + new
|
|
|
|
else:
|
|
|
|
tail = _tail + ' ' + new
|
|
|
|
|
|
|
|
db.execute("replace into mem(word, data, nick) values"
|
|
|
|
" (lower(?),?,?)", (head, tail, nick))
|
|
|
|
db.commit()
|
|
|
|
|
|
|
|
if data:
|
|
|
|
if append:
|
|
|
|
notice("Appending %s to %s" % (new, data.replace('"', "''")))
|
|
|
|
else:
|
2012-03-23 01:32:48 +01:00
|
|
|
notice('Forgetting existing data (%s), remembering this instead!'
|
|
|
|
% data.replace('"', "''"))
|
2011-11-20 10:23:31 +01:00
|
|
|
return
|
|
|
|
else:
|
|
|
|
notice('Remembered!')
|
|
|
|
return
|
|
|
|
|
2012-02-29 09:29:53 +01:00
|
|
|
|
2012-03-12 21:31:41 +01:00
|
|
|
@hook.command("f", adminonly=True)
|
|
|
|
@hook.command(adminonly=True)
|
2012-02-02 14:05:11 +01:00
|
|
|
def forget(inp, db=None, input=None, notice=None):
|
2012-05-16 05:07:27 +02:00
|
|
|
"forget <word> -- Forgets a remembered <word>."
|
2011-11-20 10:23:31 +01:00
|
|
|
|
|
|
|
db_init(db)
|
2012-02-02 14:05:11 +01:00
|
|
|
data = get_memory(db, inp)
|
2011-11-20 10:23:31 +01:00
|
|
|
|
|
|
|
if data:
|
|
|
|
db.execute("delete from mem where word=lower(?)",
|
2012-02-02 14:05:11 +01:00
|
|
|
[inp])
|
2011-11-20 10:23:31 +01:00
|
|
|
db.commit()
|
2012-03-12 19:23:41 +01:00
|
|
|
notice('"%s" has been forgotten.' % data.replace('`', "'"))
|
2012-02-02 14:05:11 +01:00
|
|
|
return
|
2011-11-20 10:23:31 +01:00
|
|
|
else:
|
2012-02-02 14:05:11 +01:00
|
|
|
notice("I don't know about that.")
|
|
|
|
return
|
2011-11-20 10:23:31 +01:00
|
|
|
|
2012-05-13 07:21:03 +02:00
|
|
|
@hook.command()
|
|
|
|
def info(inp, notice=None, db=None):
|
2012-05-16 05:07:27 +02:00
|
|
|
"info <factoid> -- Shows the source of a factoid."
|
2012-05-13 07:21:03 +02:00
|
|
|
|
|
|
|
db_init(db)
|
|
|
|
|
|
|
|
# attempt to get the factoid from the database
|
|
|
|
data = get_memory(db, inp.strip())
|
|
|
|
|
|
|
|
if data:
|
|
|
|
notice(data)
|
|
|
|
else:
|
|
|
|
notice("Unknown Factoid")
|
|
|
|
|
2012-02-29 09:29:53 +01:00
|
|
|
|
2011-11-20 10:23:31 +01:00
|
|
|
@hook.regex(r'^\? ?(.+)')
|
2012-05-13 03:09:14 +02:00
|
|
|
def factoid(inp, say=None, db=None, bot=None, me=None, conn=None, input=None):
|
2012-02-28 03:03:43 +01:00
|
|
|
"?<word> -- Shows what data is associated with <word>."
|
2012-04-01 05:39:34 +02:00
|
|
|
try:
|
2012-04-02 18:17:55 +02:00
|
|
|
prefix_on = bot.config["plugins"]["factoids"].get("prefix", False)
|
2012-04-01 05:39:34 +02:00
|
|
|
except KeyError:
|
|
|
|
prefix_on = False
|
|
|
|
|
2011-11-20 10:23:31 +01:00
|
|
|
db_init(db)
|
2012-05-13 07:40:17 +02:00
|
|
|
|
|
|
|
# split up the input
|
|
|
|
split = inp.group(1).strip().split(" ")
|
|
|
|
factoid_id = split[0]
|
|
|
|
|
|
|
|
if len(split) >= 1:
|
|
|
|
arguments = " ".join(split[1:])
|
|
|
|
else:
|
|
|
|
arguments = None
|
2011-11-20 10:23:31 +01:00
|
|
|
|
2012-05-13 03:50:28 +02:00
|
|
|
# attempt to get the factoid from the database
|
2012-05-13 07:40:17 +02:00
|
|
|
data = get_memory(db, factoid_id)
|
2012-05-13 03:50:28 +02:00
|
|
|
|
2011-11-20 10:23:31 +01:00
|
|
|
if data:
|
2012-05-13 03:31:37 +02:00
|
|
|
|
2012-05-13 03:09:14 +02:00
|
|
|
# dynamic variables for factoids
|
2012-05-13 07:11:59 +02:00
|
|
|
data = data.replace("$nick", input.nick)
|
|
|
|
data = data.replace("$chan", input.chan)
|
|
|
|
data = data.replace("$botnick", conn.nick)
|
|
|
|
|
2012-05-13 09:58:17 +02:00
|
|
|
# if factoid needs input, do that
|
|
|
|
if "$inp" in data:
|
|
|
|
if arguments:
|
|
|
|
data = data.replace("$inp", arguments)
|
|
|
|
else:
|
|
|
|
return "This factoid requires input. You can provide this" \
|
|
|
|
" with ?%s <input>" % factoid_id
|
2012-05-13 07:40:17 +02:00
|
|
|
|
2012-05-13 07:11:59 +02:00
|
|
|
# if <py>, execute python code
|
|
|
|
if data.startswith("<py>"):
|
|
|
|
data = data[4:].strip()
|
|
|
|
res = http.get("http://eval.appspot.com/eval", statement=data).splitlines()
|
|
|
|
if len(res) == 0:
|
|
|
|
return
|
|
|
|
res[0] = re_lineends.split(res[0])[0]
|
|
|
|
if not res[0] == 'Traceback (most recent call last):':
|
|
|
|
result = res[0].decode('utf8', 'ignore')
|
|
|
|
else:
|
|
|
|
result = res[-1].decode('utf8', 'ignore')
|
|
|
|
else:
|
|
|
|
result = data
|
|
|
|
|
|
|
|
# process formatting "shortcodes"
|
|
|
|
result = multiwordReplace(result, shortcodes)
|
|
|
|
|
|
|
|
# return final dataput
|
|
|
|
if result.startswith("<act>"):
|
|
|
|
result = result[5:].strip()
|
|
|
|
me(result)
|
2012-03-02 03:38:32 +01:00
|
|
|
else:
|
2012-05-13 03:09:14 +02:00
|
|
|
if prefix_on:
|
2012-05-13 07:40:17 +02:00
|
|
|
say("\x02[%s]:\x02 %s" % (factoid_id, result))
|
2012-05-13 03:09:14 +02:00
|
|
|
else:
|
2012-05-13 07:11:59 +02:00
|
|
|
say(result)
|