2010-01-22 09:56:20 +00:00
|
|
|
import random
|
|
|
|
import re
|
|
|
|
import time
|
2015-01-20 06:37:48 +00:00
|
|
|
import unittest
|
2010-01-22 09:56:20 +00:00
|
|
|
|
|
|
|
from util import hook
|
|
|
|
|
|
|
|
|
2010-02-02 04:42:34 +00:00
|
|
|
def add_quote(db, chan, nick, add_nick, msg):
|
|
|
|
db.execute('''insert or fail into quote (chan, nick, add_nick,
|
2010-03-01 02:32:41 +00:00
|
|
|
msg, time) values(?,?,?,?,?)''',
|
2014-01-14 21:12:37 +00:00
|
|
|
(chan, nick, add_nick, msg, time.time()))
|
2010-02-02 04:42:34 +00:00
|
|
|
db.commit()
|
2010-01-22 09:56:20 +00:00
|
|
|
|
2010-03-01 02:32:41 +00:00
|
|
|
|
2014-04-30 20:17:39 +00:00
|
|
|
def del_quote(db, chan, nick, msg):
|
|
|
|
updated = db.execute('''update quote set deleted = 1 where
|
2013-04-07 07:48:57 +00:00
|
|
|
chan=? and lower(nick)=lower(?) and msg=?''',
|
2014-04-30 20:17:39 +00:00
|
|
|
(chan, nick, msg))
|
2011-01-12 02:16:17 +00:00
|
|
|
db.commit()
|
2014-04-30 20:17:39 +00:00
|
|
|
|
2013-04-07 07:48:57 +00:00
|
|
|
if updated.rowcount == 0:
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
return True
|
2014-04-30 20:17:39 +00:00
|
|
|
|
|
|
|
|
2010-02-02 04:42:34 +00:00
|
|
|
def get_quotes_by_nick(db, chan, nick):
|
|
|
|
return db.execute("select time, nick, msg from quote where deleted!=1 "
|
2014-01-14 21:12:37 +00:00
|
|
|
"and chan=? and lower(nick)=lower(?) order by time",
|
|
|
|
(chan, nick)).fetchall()
|
2010-01-22 11:19:16 +00:00
|
|
|
|
2010-03-01 02:32:41 +00:00
|
|
|
|
2010-02-02 04:42:34 +00:00
|
|
|
def get_quotes_by_chan(db, chan):
|
|
|
|
return db.execute("select time, nick, msg from quote where deleted!=1 "
|
2014-01-14 21:12:37 +00:00
|
|
|
"and chan=? order by time", (chan,)).fetchall()
|
2010-01-22 11:19:16 +00:00
|
|
|
|
|
|
|
|
2015-01-20 06:37:48 +00:00
|
|
|
def get_quote_by_id(db, num):
|
|
|
|
return db.execute("select time, nick, msg from quote where deleted!=1 "
|
|
|
|
"and rowid=?", (num,)).fetchall()
|
|
|
|
|
|
|
|
|
2010-01-22 11:19:16 +00:00
|
|
|
def format_quote(q, num, n_quotes):
|
|
|
|
ctime, nick, msg = q
|
2010-03-01 02:32:41 +00:00
|
|
|
return "[%d/%d] %s <%s> %s" % (num, n_quotes,
|
2014-01-14 21:12:37 +00:00
|
|
|
time.strftime("%Y-%m-%d", time.gmtime(ctime)), nick, msg)
|
2010-01-22 11:19:16 +00:00
|
|
|
|
2010-03-01 02:32:41 +00:00
|
|
|
|
2010-01-22 09:56:20 +00:00
|
|
|
@hook.command('q')
|
|
|
|
@hook.command
|
2014-04-30 20:17:39 +00:00
|
|
|
def quote(inp, nick='', chan='', db=None, admin=False):
|
|
|
|
".q/.quote [#chan] [nick] [#n]/.quote add|delete <nick> <msg> -- gets " \
|
|
|
|
"random or [#n]th quote by <nick> or from <#chan>/adds or deletes " \
|
|
|
|
"quote"
|
2010-01-22 09:56:20 +00:00
|
|
|
|
2010-02-02 04:42:34 +00:00
|
|
|
db.execute("create table if not exists quote"
|
2014-01-14 21:12:37 +00:00
|
|
|
"(chan, nick, add_nick, msg, time real, deleted default 0, "
|
|
|
|
"primary key (chan, nick, msg))")
|
2010-02-02 04:42:34 +00:00
|
|
|
db.commit()
|
2010-01-22 09:56:20 +00:00
|
|
|
|
2010-04-25 21:40:59 +00:00
|
|
|
add = re.match(r"add[^\w@]+(\S+?)>?\s+(.*)", inp, re.I)
|
2013-04-07 07:48:57 +00:00
|
|
|
delete = re.match(r"delete[^\w@]+(\S+?)>?\s+(.*)", inp, re.I)
|
2010-03-18 21:11:20 +00:00
|
|
|
retrieve = re.match(r"(\S+)(?:\s+#?(-?\d+))?$", inp)
|
2010-12-02 07:50:05 +00:00
|
|
|
retrieve_chan = re.match(r"(#\S+)\s+(\S+)(?:\s+#?(-?\d+))?$", inp)
|
2015-01-20 06:37:48 +00:00
|
|
|
retrieve_id = re.match(r"(\d+)$", inp)
|
2010-03-18 21:11:20 +00:00
|
|
|
|
|
|
|
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."
|
2013-04-07 07:48:57 +00:00
|
|
|
if delete:
|
2014-04-30 20:17:39 +00:00
|
|
|
if not admin:
|
|
|
|
return 'only admins can delete quotes'
|
2013-04-07 07:48:57 +00:00
|
|
|
quoted_nick, msg = delete.groups()
|
2014-04-30 20:17:39 +00:00
|
|
|
if del_quote(db, chan, quoted_nick, msg):
|
|
|
|
return "deleted quote '%s'" % msg
|
2013-04-07 07:48:57 +00:00
|
|
|
else:
|
|
|
|
return "found no matching quotes to delete"
|
2015-01-20 06:37:48 +00:00
|
|
|
elif retrieve_id:
|
|
|
|
quote_id, = retrieve_id.groups()
|
|
|
|
num = 1
|
|
|
|
quotes = get_quote_by_id(db, quote_id)
|
2010-03-18 21:11:20 +00:00
|
|
|
elif retrieve:
|
|
|
|
select, num = retrieve.groups()
|
|
|
|
if select.startswith('#'):
|
|
|
|
quotes = get_quotes_by_chan(db, select)
|
|
|
|
else:
|
|
|
|
quotes = get_quotes_by_nick(db, chan, select)
|
2010-12-02 07:50:05 +00:00
|
|
|
elif retrieve_chan:
|
|
|
|
chan, nick, num = retrieve_chan.groups()
|
2010-01-22 09:56:20 +00:00
|
|
|
|
2010-12-02 07:50:05 +00:00
|
|
|
quotes = get_quotes_by_nick(db, chan, nick)
|
|
|
|
else:
|
|
|
|
return quote.__doc__
|
2010-01-22 09:56:20 +00:00
|
|
|
|
2015-01-20 06:37:48 +00:00
|
|
|
if num:
|
|
|
|
num = int(num)
|
|
|
|
|
2010-12-02 07:50:05 +00:00
|
|
|
n_quotes = len(quotes)
|
2010-01-22 09:56:20 +00:00
|
|
|
|
2010-12-02 07:50:05 +00:00
|
|
|
if not n_quotes:
|
|
|
|
return "no quotes found"
|
2010-01-22 09:56:20 +00:00
|
|
|
|
2010-12-02 07:50:05 +00:00
|
|
|
if num:
|
|
|
|
if num > n_quotes or (num < 0 and num < -n_quotes):
|
|
|
|
return "I only have %d quote%s for %s" % (n_quotes,
|
2014-01-14 21:12:37 +00:00
|
|
|
('s', '')[n_quotes == 1], select)
|
2010-12-02 07:50:05 +00:00
|
|
|
elif num < 0:
|
|
|
|
selected_quote = quotes[num]
|
|
|
|
num = n_quotes + num + 1
|
2010-01-22 09:56:20 +00:00
|
|
|
else:
|
2010-03-18 21:11:20 +00:00
|
|
|
selected_quote = quotes[num - 1]
|
2010-12-02 07:50:05 +00:00
|
|
|
else:
|
|
|
|
num = random.randint(1, n_quotes)
|
|
|
|
selected_quote = quotes[num - 1]
|
2010-03-18 21:11:20 +00:00
|
|
|
|
2010-12-02 07:50:05 +00:00
|
|
|
return format_quote(selected_quote, num, n_quotes)
|
2015-01-20 06:37:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
class QuoteTest(unittest.TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
import sqlite3
|
|
|
|
self.db = sqlite3.connect(':memory:')
|
|
|
|
|
|
|
|
quote('', db=self.db) # init DB
|
|
|
|
|
|
|
|
def quote(self, arg, **kwargs):
|
|
|
|
return quote(arg, chan='#test', nick='alice', db=self.db, **kwargs)
|
|
|
|
|
|
|
|
def add_quote(self, msg=''):
|
|
|
|
add_quote(self.db, '#test', 'Socrates', 'Plato',
|
|
|
|
msg or 'Education is the kindling of a flame,'
|
|
|
|
' not the filling of a vessel.')
|
|
|
|
|
|
|
|
def test_retrieve_chan(self):
|
|
|
|
self.add_quote()
|
|
|
|
assert '<Socrates> Education' in self.quote('#test')
|
|
|
|
|
|
|
|
def test_retrieve_user(self):
|
|
|
|
self.add_quote()
|
|
|
|
assert '<Socrates> Education' in self.quote('socrates')
|
|
|
|
|
|
|
|
def test_no_quotes(self):
|
|
|
|
assert "no quotes found" in self.quote("#notachan")
|
|
|
|
|
|
|
|
def test_quote_too_high(self):
|
|
|
|
self.add_quote()
|
|
|
|
assert 'I only have 1 quote for #test' in self.quote('#test 4')
|
|
|
|
|
|
|
|
def test_add(self):
|
|
|
|
self.quote("add <someone> witty phrase")
|
|
|
|
assert 'witty' in self.quote('#test')
|
|
|
|
|
|
|
|
def test_add_twice(self):
|
|
|
|
self.quote("add <someone> lol")
|
|
|
|
assert 'already stored' in self.quote("add <someone> lol")
|
|
|
|
|
|
|
|
def test_del_not_admin(self):
|
|
|
|
assert 'only admins' in self.quote('delete whoever 4')
|
|
|
|
|
|
|
|
def test_del_not_exists(self):
|
|
|
|
assert 'found no matching' in self.quote(
|
|
|
|
'delete whoever 4', admin=True)
|
|
|
|
|
|
|
|
def test_del(self):
|
|
|
|
self.add_quote("hi")
|
|
|
|
assert "deleted quote 'hi'" in self.quote(
|
|
|
|
'delete socrates hi', admin=True)
|
|
|
|
|
|
|
|
def test_retrieve_id(self):
|
|
|
|
self.add_quote()
|
|
|
|
assert 'Education is' in self.quote('1')
|
|
|
|
|
|
|
|
def test_retrieve_chan_user(self):
|
|
|
|
self.add_quote()
|
|
|
|
assert 'Education' in self.quote('#test socrates')
|
|
|
|
assert 'Education' in self.quote('#test socrates 1')
|
|
|
|
|
|
|
|
def test_nth(self):
|
|
|
|
self.add_quote('first quote')
|
|
|
|
self.add_quote('second quote')
|
|
|
|
self.add_quote('third quote')
|
|
|
|
self.add_quote('fourth quote')
|
|
|
|
assert 'third' in self.quote('socrates -2')
|
|
|
|
assert 'only have 4' in self.quote('socrates -9')
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|