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/quote.py

131 lines
4.8 KiB
Python
Raw Normal View History

2011-11-20 10:23:31 +01:00
import random
import re
import time
from util import hook
2012-02-02 14:05:11 +01:00
def format_quote(q, num, n_quotes):
2012-02-28 03:03:43 +01:00
"Returns a formatted string of a quote"
2012-02-02 14:05:11 +01:00
ctime, nick, msg = q
return "[%d/%d] <%s> %s" % (num, n_quotes,
nick, msg)
def create_table_if_not_exists(db):
2012-02-28 03:03:43 +01:00
"Creates an empty quote table if one does not already exist"
2012-02-02 14:05:11 +01:00
db.execute('''CREATE TABLE IF NOT EXISTS quote (
chan,
nick,
add_nick,
msg,
time real,
deleted default 0,
PRIMARY KEY (chan, nick, msg)
)''')
2011-11-20 10:23:31 +01:00
db.commit()
2012-02-02 14:05:11 +01:00
def add_quote(db, chan, nick, add_nick, msg):
2012-02-28 03:03:43 +01:00
"Adds a quote to a nick, returns message string"
2012-02-02 14:05:11 +01:00
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:
2012-02-22 22:41:54 +01:00
return "Message already stored, doing nothing."
return "Quote added."
2011-11-20 10:23:31 +01:00
def del_quote(db, chan, nick, add_nick, msg):
2012-02-28 03:03:43 +01:00
"Deletes a quote from a nick"
2012-02-02 14:05:11 +01:00
db.execute('''UPDATE quote
SET deleted = 1
WHERE chan=?
AND lower(nick)=lower(?)
AND msg=msg''')
2011-11-20 10:23:31 +01:00
db.commit()
2012-02-02 14:05:11 +01:00
def get_quote_num(num, count, name):
2012-02-28 03:03:43 +01:00
"Returns the quote number desired from the database"
2012-02-29 09:29:53 +01:00
if num: # Make sure num is a number if it isn't false
2012-02-02 14:05:11 +01:00
num = int(num)
2012-02-29 09:29:53 +01:00
if count == 0: # If there are no quotes in the database, raise an Exception.
2012-02-19 12:00:51 +01:00
raise Exception("No quotes found for %s." % name)
2012-02-29 09:29:53 +01:00
if num and num < 0: # If the selected quote is less than 0, count back if possible.
2012-02-02 14:05:11 +01:00
num = count + num + 1 if num + count > -1 else count + 1
2012-02-29 09:29:53 +01:00
if num and num > count: # If a number is given and and there are not enough quotes, raise an Exception.
2012-02-19 12:00:51 +01:00
raise Exception("I only have %d quote%s for %s." % (count, ('s', '')[count == 1], name))
2012-02-29 09:29:53 +01:00
if num and num == 0: # If the number is zero, set it to one
2012-02-02 14:05:11 +01:00
num = 1
2012-02-29 09:29:53 +01:00
if not num: # If a number is not given, select a random one
2012-02-02 14:05:11 +01:00
num = random.randint(1, count)
return num
def get_quote_by_nick(db, chan, nick, num=False):
2012-02-28 03:03:43 +01:00
"Returns a formatted quote from a nick, random or selected by number"
2012-02-02 14:05:11 +01:00
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
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):
2012-02-28 03:03:43 +01:00
"Returns a formatted quote from a channel, random or selected by number"
2012-02-02 14:05:11 +01:00
count = db.execute('''SELECT COUNT(*)
FROM quote
WHERE deleted != 1
AND chan = ?''', (chan,)).fetchall()[0][0]
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)
2011-11-20 10:23:31 +01:00
@hook.command('q')
@hook.command
2012-02-19 12:00:51 +01:00
def quote(inp, nick='', chan='', db=None, notice=None):
2012-02-28 03:03:43 +01:00
".quote [#chan] [nick] [#n]/.quote add <nick> <msg> -- Gets " \
"random or [#n]th quote by <nick> or from <#chan>/adds quote."
2012-02-02 14:05:11 +01:00
create_table_if_not_exists(db)
2011-11-20 10:23:31 +01:00
add = re.match(r"add[^\w@]+(\S+?)>?\s+(.*)", inp, re.I)
retrieve = re.match(r"(\S+)(?:\s+#?(-?\d+))?$", inp)
retrieve_chan = re.match(r"(#\S+)\s+(\S+)(?:\s+#?(-?\d+))?$", inp)
if add:
quoted_nick, msg = add.groups()
2012-02-02 14:05:11 +01:00
return add_quote(db, chan, quoted_nick, nick, msg)
2011-11-20 10:23:31 +01:00
elif retrieve:
select, num = retrieve.groups()
2012-02-02 14:05:11 +01:00
by_chan = True if select.startswith('#') else False
if by_chan:
return get_quote_by_chan(db, select, num)
2011-11-20 10:23:31 +01:00
else:
2012-02-02 14:05:11 +01:00
return get_quote_by_nick(db, chan, select, num)
2011-11-20 10:23:31 +01:00
elif retrieve_chan:
chan, nick, num = retrieve_chan.groups()
2012-02-02 14:05:11 +01:00
return get_quote_by_nick(db, chan, nick, num)
2012-02-19 12:00:51 +01:00
notice(quote.__doc__)