2010-01-22 09:56:20 +00:00
|
|
|
import os
|
|
|
|
import sqlite3
|
|
|
|
import random
|
|
|
|
import re
|
|
|
|
import time
|
|
|
|
|
|
|
|
from util import hook
|
|
|
|
|
|
|
|
dbname = "skybot.db"
|
|
|
|
|
|
|
|
def db_connect(db):
|
|
|
|
conn = sqlite3.connect(db)
|
2010-01-22 11:19:16 +00:00
|
|
|
conn.execute('''create table if not exists quotes
|
|
|
|
(server, chan, nick, add_nick, msg, time real, deleted default 0,
|
|
|
|
primary key (server, chan, nick, msg))''')
|
2010-01-22 09:56:20 +00:00
|
|
|
conn.commit()
|
|
|
|
return conn
|
|
|
|
|
2010-01-22 11:19:16 +00:00
|
|
|
def add_quote(conn, server, chan, nick, add_nick, msg):
|
2010-01-22 09:56:20 +00:00
|
|
|
now = time.time()
|
2010-01-22 11:19:16 +00:00
|
|
|
print repr((conn, server, add_nick, nick, msg, time))
|
|
|
|
conn.execute('''insert or fail into quotes (server, chan, nick, add_nick,
|
|
|
|
msg, time) values(?,?,?,?,?,?)''',
|
|
|
|
(server, chan, nick, add_nick, msg, now))
|
2010-01-22 09:56:20 +00:00
|
|
|
conn.commit()
|
|
|
|
|
2010-01-22 11:19:16 +00:00
|
|
|
def get_quotes_by_nick(conn, server, chan, nick):
|
|
|
|
return conn.execute("select time, nick, msg from quotes where deleted!=1 "
|
|
|
|
"and server=? and chan=? and lower(nick)=lower(?) order by time",
|
|
|
|
(server, chan, nick)).fetchall()
|
|
|
|
|
|
|
|
def get_quotes_by_chan(conn, server, chan):
|
|
|
|
return conn.execute("select time, nick, msg from quotes where deleted!=1 "
|
|
|
|
"and server=? and chan=? order by time", (server, chan)).fetchall()
|
|
|
|
|
|
|
|
|
|
|
|
def format_quote(q, num, n_quotes):
|
|
|
|
ctime, nick, msg = q
|
|
|
|
return "[%d/%d] %s <%s> %s" % (num, n_quotes,
|
|
|
|
time.strftime("%Y-%m-%d", time.gmtime(ctime)), nick, msg)
|
|
|
|
|
|
|
|
|
2010-01-22 09:56:20 +00:00
|
|
|
@hook.command('q')
|
|
|
|
@hook.command
|
|
|
|
def quote(bot, input):
|
2010-01-22 11:19:16 +00:00
|
|
|
".q/.quote <nick/#chan> [#n]/.quote add <nick> <msg> -- gets " \
|
|
|
|
"random or [#n]th quote by <nick> or from <#chan>/adds quote"
|
2010-01-22 09:56:20 +00:00
|
|
|
|
|
|
|
dbpath = os.path.join(bot.persist_dir, dbname)
|
|
|
|
conn = db_connect(dbpath)
|
|
|
|
|
|
|
|
try:
|
|
|
|
add = re.match(r"add\s+<?[^\w]?(\S+?)>?\s+(.*)", input.inp, re.I)
|
2010-01-22 11:19:16 +00:00
|
|
|
retrieve = re.match(r"(\S+)(?:\s+#?(-?\d+))?", input.inp)
|
|
|
|
chan = input.chan
|
2010-01-22 09:56:20 +00:00
|
|
|
|
|
|
|
if add:
|
|
|
|
nick, msg = add.groups()
|
|
|
|
try:
|
2010-01-22 11:19:16 +00:00
|
|
|
add_quote(conn, input.server, chan, nick, input.nick, msg)
|
|
|
|
except sqlite3.IntegrityError:
|
2010-01-22 09:56:20 +00:00
|
|
|
return "message already stored, doing nothing."
|
|
|
|
return "quote added."
|
|
|
|
elif retrieve:
|
2010-01-22 11:19:16 +00:00
|
|
|
select, num = retrieve.groups()
|
|
|
|
|
|
|
|
by_chan = False
|
|
|
|
if select.startswith('#'):
|
|
|
|
by_chan = True
|
|
|
|
quotes = get_quotes_by_chan(conn, input.server, select)
|
|
|
|
else:
|
|
|
|
quotes = get_quotes_by_nick(conn, input.server, chan, select)
|
2010-01-22 09:56:20 +00:00
|
|
|
|
|
|
|
n_quotes = len(quotes)
|
|
|
|
|
|
|
|
if not n_quotes:
|
|
|
|
return "no quotes found"
|
|
|
|
|
|
|
|
if num:
|
|
|
|
num = int(num)
|
|
|
|
|
|
|
|
if num:
|
|
|
|
if num > n_quotes:
|
|
|
|
return "I only have %d quote%s for %s" % (n_quotes,
|
2010-01-22 11:19:16 +00:00
|
|
|
('s', '')[n_quotes == 1], select)
|
2010-01-22 09:56:20 +00:00
|
|
|
else:
|
|
|
|
selected_quote = quotes[num - 1]
|
|
|
|
else:
|
|
|
|
num = random.randint(1, n_quotes)
|
|
|
|
selected_quote = quotes[num - 1]
|
|
|
|
|
2010-01-22 11:19:16 +00:00
|
|
|
return format_quote(selected_quote, num, n_quotes)
|
2010-01-22 09:56:20 +00:00
|
|
|
else:
|
|
|
|
return quote.__doc__
|
|
|
|
finally:
|
|
|
|
conn.commit()
|
|
|
|
conn.close()
|