This repository has been archived on 2023-04-13. You can view files and clone it, but cannot push or open issues or pull requests.
CloudBot/plugins/factoids.py

161 lines
4.2 KiB
Python
Raw Normal View History

2012-05-09 21:51:44 +02:00
# Written by Scaevolus 2010
from util import hook, http
2012-06-11 23:45:23 +02:00
from util.text import multiword_replace
from util.execute import eval_py
2012-05-09 21:47:57 +02:00
import string
2012-08-20 22:32:12 +02:00
import sqlite3
2011-11-20 10:23:31 +01:00
import re
re_lineends = re.compile(r'[\r\n]*')
2011-11-20 10:23:31 +01:00
# some simple "shortcodes" for formatting purposes
shortcodes = {
2012-05-13 03:50:28 +02:00
'[b]': '\x02',
'[/b]': '\x02',
'[u]': '\x1F',
'[/u]': '\x1F',
'[i]': '\x16',
'[/i]': '\x16'}
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
@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:
2012-09-04 22:09:08 +02:00
word, data = inp.split(None, 1)
2011-11-20 10:23:31 +01:00
except ValueError:
return remember.__doc__
2012-09-04 22:09:08 +02:00
old_data = get_memory(db, word)
2011-11-20 10:23:31 +01:00
2012-09-04 22:09:08 +02:00
if data.startswith('+') and old_data:
2011-11-20 10:23:31 +01:00
append = True
2012-09-04 22:09:08 +02: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 10:23:31 +01:00
else:
2012-09-04 22:09:08 +02:00
data = old_data + ' ' + new_data
2011-11-20 10:23:31 +01:00
db.execute("replace into mem(word, data, nick) values"
2012-09-04 22:09:08 +02:00
" (lower(?),?,?)", (word, data, nick))
2011-11-20 10:23:31 +01:00
db.commit()
2012-09-04 22:09:08 +02:00
if old_data:
2011-11-20 10:23:31 +01:00
if append:
2012-09-04 22:09:08 +02:00
notice("Appending \x02%s\x02 to \x02%s\x02" % (new_data, old_data))
2011-11-20 10:23:31 +01:00
else:
2012-08-20 22:32:12 +02:00
notice('Remembering \x02%s\x02 for \x02%s\x02. Type ?%s to see it.'
2012-09-04 22:09:08 +02:00
% (data, word, word))
notice('Previous data was \x02%s\x02' % old_data)
2011-11-20 10:23:31 +01:00
else:
2012-08-20 22:32:12 +02:00
notice('Remembering \x02%s\x02 for \x02%s\x02. Type ?%s to see it.'
2012-09-04 22:09:08 +02:00
% (data, word, word))
2011-11-20 10:23:31 +01:00
2012-02-29 09:29:53 +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()
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-08-20 22:32:12 +02:00
@hook.command
2012-05-13 07:21:03 +02:00
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:
2012-08-20 22:32:12 +02:00
notice("Unknown Factoid.")
2012-05-13 07:21:03 +02:00
2012-02-29 09:29:53 +01:00
2011-11-20 10:23:31 +01:00
@hook.regex(r'^\? ?(.+)')
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-08-20 22:32:12 +02:00
# split up the input
split = inp.group(1).strip().split(" ")
factoid_id = split[0]
2012-08-20 22:32:12 +02:00
if len(split) >= 1:
arguments = " ".join(split[1:])
else:
arguments = ""
2011-11-20 10:23:31 +01: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-09-10 02:12:45 +02:00
# factoid preprocessors
if data.startswith("<py>"):
2012-09-10 02:12:45 +02:00
code = data[4:].strip()
2012-08-28 07:19:13 +02:00
variables = 'input="""%s"""; nick="%s"; chan="%s"; bot_nick="%s";' % (arguments.replace('"', '\\"'),
input.nick, input.chan, input.conn.nick)
2012-09-10 02:12:45 +02:00
result = eval_py(variables + code)
elif data.startswith("<url>"):
url = data[5:].strip()
try:
result = http.get(url)
except http.HttpError:
result = "Could not fetch URL."
else:
result = data
2012-08-20 22:32:12 +02:00
2012-09-10 02:12:45 +02:00
# factoid postprocessors
2012-06-11 23:45:23 +02:00
result = multiword_replace(result, shortcodes)
if result.startswith("<act>"):
result = result[5:].strip()
me(result)
else:
if prefix_on:
say("\x02[%s]:\x02 %s" % (factoid_id, result))
else:
say(result)