Massive code dump :o
This commit is contained in:
parent
185c1d5ae3
commit
9bc8901972
60 changed files with 5781 additions and 125 deletions
156
plugins/quote.py
156
plugins/quote.py
|
@ -4,47 +4,110 @@ import time
|
|||
|
||||
from util import hook
|
||||
|
||||
def format_quote(q, num, n_quotes):
|
||||
"""Returns a formatted string of a quote"""
|
||||
ctime, nick, msg = q
|
||||
return "[%d/%d] <%s> %s" % (num, n_quotes,
|
||||
nick, msg)
|
||||
|
||||
def create_table_if_not_exists(db):
|
||||
"""Creates an empty quote table if one does not already exist"""
|
||||
db.execute('''CREATE TABLE IF NOT EXISTS quote (
|
||||
chan,
|
||||
nick,
|
||||
add_nick,
|
||||
msg,
|
||||
time real,
|
||||
deleted default 0,
|
||||
PRIMARY KEY (chan, nick, msg)
|
||||
)''')
|
||||
db.commit()
|
||||
|
||||
def add_quote(db, chan, nick, add_nick, msg):
|
||||
db.execute('''insert or fail into quote (chan, nick, add_nick,
|
||||
msg, time) values(?,?,?,?,?)''',
|
||||
(chan, nick, add_nick, msg, time.time()))
|
||||
db.commit()
|
||||
|
||||
"""Adds a quote to a nick, returns message string"""
|
||||
try:
|
||||
db.execute('''INSERT OR FAIL INTO quote
|
||||
(chan, nick, add_nick, msg, time)
|
||||
VALUES(?,?,?,?,?)''',
|
||||
(chan, nick, add_nick, msg, time.time()))
|
||||
db.commit()
|
||||
except db.IntegrityError:
|
||||
return "message already stored, doing nothing."
|
||||
return "quote added."
|
||||
|
||||
def del_quote(db, chan, nick, add_nick, msg):
|
||||
db.execute('''update quote set deleted = 1 where
|
||||
chan=? and lower(nick)=lower(?) and msg=msg''')
|
||||
"""Deletes a quote from a nick"""
|
||||
db.execute('''UPDATE quote
|
||||
SET deleted = 1
|
||||
WHERE chan=?
|
||||
AND lower(nick)=lower(?)
|
||||
AND msg=msg''')
|
||||
db.commit()
|
||||
|
||||
def get_quote_num(num, count, name):
|
||||
"""Returns the quote number desired from the database"""
|
||||
if num: # Make sure num is a number if it isn't false
|
||||
num = int(num)
|
||||
if count == 0: # If there are no quotes in the database, raise an Exception.
|
||||
raise Exception("No quotes found for %s" % name)
|
||||
if num and num < 0: # If the selected quote is less than 0, count back if possible.
|
||||
num = count + num + 1 if num + count > -1 else count + 1
|
||||
if num and num > count: # If a number is given and and there are not enough quotes, raise an Exception.
|
||||
raise Exception("I only have %d quote%s for %s" % (count, ('s', '')[count == 1], name))
|
||||
if num and num == 0: # If the number is zero, set it to one
|
||||
num = 1
|
||||
if not num: # If a number is not given, select a random one
|
||||
num = random.randint(1, count)
|
||||
return num
|
||||
|
||||
def get_quotes_by_nick(db, chan, nick):
|
||||
return db.execute("select time, nick, msg from quote where deleted!=1 "
|
||||
"and chan=? and lower(nick)=lower(?) order by time",
|
||||
(chan, nick)).fetchall()
|
||||
def get_quote_by_nick(db, chan, nick, num=False):
|
||||
"""Returns a formatted quote from a nick, random or selected by number"""
|
||||
count = db.execute('''SELECT COUNT(*)
|
||||
FROM quote
|
||||
WHERE deleted != 1
|
||||
AND chan = ?
|
||||
AND lower(nick) = lower(?)''', (chan, nick)).fetchall()[0][0]
|
||||
|
||||
try:
|
||||
num = get_quote_num(num, count, nick)
|
||||
except Exception as error_message:
|
||||
return error_message
|
||||
|
||||
def get_quotes_by_chan(db, chan):
|
||||
return db.execute("select time, nick, msg from quote where deleted!=1 "
|
||||
"and chan=? order by time", (chan,)).fetchall()
|
||||
quote = db.execute('''SELECT time, nick, msg
|
||||
FROM quote
|
||||
WHERE deleted != 1
|
||||
AND chan = ?
|
||||
AND lower(nick) = lower(?)
|
||||
ORDER BY time
|
||||
LIMIT ?, 1''', (chan, nick, (num-1))).fetchall()[0]
|
||||
return format_quote(quote, num, count)
|
||||
|
||||
def get_quote_by_chan(db, chan, num=False):
|
||||
"""Returns a formatted quote from a channel, random or selected by number"""
|
||||
count = db.execute('''SELECT COUNT(*)
|
||||
FROM quote
|
||||
WHERE deleted != 1
|
||||
AND chan = ?''', (chan,)).fetchall()[0][0]
|
||||
|
||||
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)
|
||||
try:
|
||||
num = get_quote_num(num, count, chan)
|
||||
except Exception as error_message:
|
||||
return error_message
|
||||
|
||||
quote = db.execute('''SELECT time, nick, msg
|
||||
FROM quote
|
||||
WHERE deleted != 1
|
||||
AND chan = ?
|
||||
ORDER BY time
|
||||
LIMIT ?, 1''', (chan, (num -1))).fetchall()[0]
|
||||
return format_quote(quote, num, count)
|
||||
|
||||
@hook.command('q')
|
||||
@hook.command
|
||||
def quote(inp, nick='', chan='', db=None):
|
||||
".q/.quote [#chan] [nick] [#n]/.quote add <nick> <msg> -- gets " \
|
||||
"random or [#n]th quote by <nick> or from <#chan>/adds quote"
|
||||
|
||||
db.execute("create table if not exists quote"
|
||||
"(chan, nick, add_nick, msg, time real, deleted default 0, "
|
||||
"primary key (chan, nick, msg))")
|
||||
db.commit()
|
||||
create_table_if_not_exists(db)
|
||||
|
||||
add = re.match(r"add[^\w@]+(\S+?)>?\s+(.*)", inp, re.I)
|
||||
retrieve = re.match(r"(\S+)(?:\s+#?(-?\d+))?$", inp)
|
||||
|
@ -52,47 +115,16 @@ def quote(inp, nick='', chan='', db=None):
|
|||
|
||||
if add:
|
||||
quoted_nick, msg = add.groups()
|
||||
try:
|
||||
add_quote(db, chan, quoted_nick, nick, msg)
|
||||
db.commit()
|
||||
except db.IntegrityError:
|
||||
return "message already stored, doing nothing."
|
||||
return "quote added."
|
||||
return add_quote(db, chan, quoted_nick, nick, msg)
|
||||
elif retrieve:
|
||||
select, num = retrieve.groups()
|
||||
|
||||
by_chan = False
|
||||
if select.startswith('#'):
|
||||
by_chan = True
|
||||
quotes = get_quotes_by_chan(db, select)
|
||||
by_chan = True if select.startswith('#') else False
|
||||
if by_chan:
|
||||
return get_quote_by_chan(db, select, num)
|
||||
else:
|
||||
quotes = get_quotes_by_nick(db, chan, select)
|
||||
return get_quote_by_nick(db, chan, select, num)
|
||||
elif retrieve_chan:
|
||||
chan, nick, num = retrieve_chan.groups()
|
||||
|
||||
quotes = get_quotes_by_nick(db, chan, nick)
|
||||
else:
|
||||
return quote.__doc__
|
||||
|
||||
n_quotes = len(quotes)
|
||||
|
||||
if not n_quotes:
|
||||
return "no quotes found"
|
||||
|
||||
if num:
|
||||
num = int(num)
|
||||
|
||||
if num:
|
||||
if num > n_quotes or (num < 0 and num < -n_quotes):
|
||||
return "I only have %d quote%s for %s" % (n_quotes,
|
||||
('s', '')[n_quotes == 1], select)
|
||||
elif num < 0:
|
||||
selected_quote = quotes[num]
|
||||
num = n_quotes + num + 1
|
||||
else:
|
||||
selected_quote = quotes[num - 1]
|
||||
else:
|
||||
num = random.randint(1, n_quotes)
|
||||
selected_quote = quotes[num - 1]
|
||||
|
||||
return format_quote(selected_quote, num, n_quotes)
|
||||
return get_quote_by_nick(db, chan, nick, num)
|
||||
|
||||
return quote.__doc__
|
||||
|
|
Reference in a new issue