2003-03-12 07:26:59 +01:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
###
|
|
|
|
# Copyright (c) 2002, Jeremiah Fincher
|
|
|
|
# All rights reserved.
|
|
|
|
#
|
|
|
|
# Redistribution and use in source and binary forms, with or without
|
|
|
|
# modification, are permitted provided that the following conditions are met:
|
|
|
|
#
|
|
|
|
# * Redistributions of source code must retain the above copyright notice,
|
|
|
|
# this list of conditions, and the following disclaimer.
|
|
|
|
# * Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
# this list of conditions, and the following disclaimer in the
|
|
|
|
# documentation and/or other materials provided with the distribution.
|
|
|
|
# * Neither the name of the author of this software nor the name of
|
|
|
|
# contributors to this software may be used to endorse or promote products
|
|
|
|
# derived from this software without specific prior written consent.
|
|
|
|
#
|
|
|
|
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
# POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
###
|
|
|
|
|
2003-03-25 07:53:51 +01:00
|
|
|
"""
|
|
|
|
Maintains a Quotes database for each channel.
|
|
|
|
"""
|
|
|
|
|
2003-11-25 09:23:47 +01:00
|
|
|
__revision__ = "$Id$"
|
|
|
|
|
2004-07-24 07:18:26 +02:00
|
|
|
import supybot.plugins as plugins
|
2003-03-12 07:26:59 +01:00
|
|
|
|
|
|
|
import re
|
|
|
|
import time
|
2003-08-26 19:07:37 +02:00
|
|
|
import getopt
|
2003-03-12 07:26:59 +01:00
|
|
|
import os.path
|
|
|
|
|
2004-08-13 05:50:38 +02:00
|
|
|
import supybot.dbi as dbi
|
2004-07-24 07:18:26 +02:00
|
|
|
import supybot.conf as conf
|
|
|
|
import supybot.utils as utils
|
|
|
|
import supybot.ircdb as ircdb
|
|
|
|
import supybot.privmsgs as privmsgs
|
|
|
|
import supybot.callbacks as callbacks
|
2003-03-12 07:26:59 +01:00
|
|
|
|
2003-12-04 00:48:00 +01:00
|
|
|
try:
|
|
|
|
import sqlite
|
|
|
|
except ImportError:
|
2004-08-16 18:36:18 +02:00
|
|
|
raise callbacks.Error, 'You need to have PySQLite installed to use this '\
|
2003-12-04 00:48:00 +01:00
|
|
|
'plugin. Download it at <http://pysqlite.sf.net/>'
|
|
|
|
|
2004-08-16 18:36:18 +02:00
|
|
|
class QuotesError(Exception):
|
|
|
|
pass
|
|
|
|
|
2004-08-13 05:50:38 +02:00
|
|
|
class QuoteRecord(object):
|
|
|
|
__metaclass__ = dbi.Record
|
|
|
|
__fields__ = [
|
|
|
|
'at',
|
|
|
|
'by',
|
|
|
|
'text'
|
|
|
|
]
|
|
|
|
def __str__(self):
|
|
|
|
format = conf.supybot.humanTimestampFormat()
|
|
|
|
return 'Quote %r added by %s at %s.' % \
|
2004-08-16 18:36:18 +02:00
|
|
|
(self.text, self.by,
|
2004-08-13 05:50:38 +02:00
|
|
|
time.strftime(format, time.localtime(float(self.at))))
|
2003-03-12 07:26:59 +01:00
|
|
|
|
2004-08-13 05:50:38 +02:00
|
|
|
class SqliteQuotesDB(object):
|
|
|
|
def _getDb(self, channel):
|
|
|
|
filename = plugins.makeChannelFilename('Quotes.db', channel)
|
2003-03-12 07:26:59 +01:00
|
|
|
if os.path.exists(filename):
|
|
|
|
return sqlite.connect(db=filename, mode=0755,
|
|
|
|
converters={'bool': bool})
|
|
|
|
#else:
|
|
|
|
db = sqlite.connect(db=filename, mode=0755, coverters={'bool': bool})
|
|
|
|
cursor = db.cursor()
|
|
|
|
cursor.execute("""CREATE TABLE quotes (
|
|
|
|
id INTEGER PRIMARY KEY,
|
2003-08-26 19:07:37 +02:00
|
|
|
added_by TEXT,
|
2003-03-12 07:26:59 +01:00
|
|
|
added_at TIMESTAMP,
|
2003-08-26 19:07:37 +02:00
|
|
|
quote TEXT
|
|
|
|
);""")
|
2003-03-12 07:26:59 +01:00
|
|
|
db.commit()
|
|
|
|
return db
|
|
|
|
|
2004-08-13 05:50:38 +02:00
|
|
|
def add(self, channel, by, quote):
|
|
|
|
db = self._getDb(channel)
|
|
|
|
cursor = db.cursor()
|
|
|
|
at = int(time.time())
|
|
|
|
cursor.execute("""INSERT INTO quotes VALUES (NULL, %s, %s, %s)""",
|
|
|
|
by, at, quote)
|
|
|
|
cursor.execute("""SELECT id FROM quotes
|
|
|
|
WHERE added_by=%s AND added_at=%s AND quote=%s""",
|
|
|
|
by, at, quote)
|
|
|
|
db.commit()
|
|
|
|
return int(cursor.fetchone()[0])
|
|
|
|
|
|
|
|
def size(self, channel):
|
|
|
|
db = self._getDb(channel)
|
|
|
|
cursor = db.cursor()
|
|
|
|
cursor.execute("""SELECT COUNT(*) FROM quotes""")
|
|
|
|
return int(cursor.fetchone()[0])
|
|
|
|
|
|
|
|
def random(self, channel):
|
|
|
|
db = self._getDb(channel)
|
|
|
|
cursor = db.cursor()
|
|
|
|
cursor.execute("""SELECT id, added_by, added_at, quote FROM quotes
|
|
|
|
ORDER BY random() LIMIT 1""")
|
|
|
|
(id, by, at, text) = cursor.fetchone()
|
|
|
|
return QuoteRecord(id, by=by, at=int(at), text=text)
|
|
|
|
|
|
|
|
def search(self, channel, **kwargs):
|
|
|
|
criteria = []
|
|
|
|
formats = []
|
|
|
|
predicateName = ''
|
|
|
|
db = self._getDb(channel)
|
|
|
|
for v in kwargs['id']:
|
|
|
|
criteria.append('id=%s' % v)
|
|
|
|
for v in kwargs['with']:
|
|
|
|
criteria.append('quote LIKE %s')
|
|
|
|
formats.append('%%%s%%' % v)
|
|
|
|
for v in kwargs['by']:
|
|
|
|
criteria.append('added_by=%s')
|
|
|
|
formats.append(arg)
|
2004-08-16 18:36:18 +02:00
|
|
|
for p in kwargs['predicate']:
|
2004-08-13 05:50:38 +02:00
|
|
|
predicateName += 'p'
|
|
|
|
db.create_function(predicateName, 1, p)
|
|
|
|
criteria.append('%s(quote)' % predicateName)
|
|
|
|
for s in kwargs['args']:
|
|
|
|
try:
|
|
|
|
i = int(s)
|
|
|
|
criteria.append('id=%s' % i)
|
|
|
|
except ValueError:
|
|
|
|
s = '%%%s%%' % s
|
|
|
|
criteria.append('quote LIKE %s')
|
|
|
|
formats.append(s)
|
|
|
|
sql = """SELECT id, added_by, added_at, quote FROM quotes
|
|
|
|
WHERE %s""" % ' AND '.join(criteria)
|
|
|
|
cursor = db.cursor()
|
|
|
|
cursor.execute(sql, *formats)
|
|
|
|
if cursor.rowcount == 0:
|
|
|
|
return None
|
|
|
|
elif cursor.rowcount == 1:
|
|
|
|
(id, by, at, text) = cursor.fetchone()
|
|
|
|
return QuoteRecord(id, by=by, at=int(at), text=text)
|
|
|
|
else:
|
|
|
|
quotes = []
|
|
|
|
for (id, by, at, text) in cursor.fetchall():
|
|
|
|
quotes.append(QuoteRecord(id, by=by, at=int(at), text=text))
|
|
|
|
return quotes
|
|
|
|
|
|
|
|
def get(self, channel, id):
|
|
|
|
db = self._getDb(channel)
|
|
|
|
cursor = db.cursor()
|
|
|
|
cursor.execute("""SELECT added_by, added_at, quote FROM quotes
|
|
|
|
WHERE id=%s""", id)
|
|
|
|
if cursor.rowcount == 0:
|
2004-08-16 18:36:18 +02:00
|
|
|
raise QuotesDBError, id
|
2004-08-13 05:50:38 +02:00
|
|
|
(by, at, text) = cursor.fetchone()
|
|
|
|
return QuoteRecord(id, by=by, at=int(at), text=text)
|
|
|
|
|
|
|
|
def remove(self, channel, id):
|
|
|
|
db = self._getDb(channel)
|
|
|
|
cursor = db.cursor()
|
|
|
|
cursor.execute("""DELETE FROM quotes WHERE id=%s""", id)
|
|
|
|
if cursor.rowcount == 0:
|
2004-08-16 18:36:18 +02:00
|
|
|
raise QuotesDBError, id
|
2004-08-13 05:50:38 +02:00
|
|
|
db.commit()
|
|
|
|
|
|
|
|
def QuotesDB():
|
|
|
|
return SqliteQuotesDB()
|
2004-08-16 18:36:18 +02:00
|
|
|
|
2004-08-13 05:50:38 +02:00
|
|
|
class Quotes(callbacks.Privmsg):
|
|
|
|
def __init__(self):
|
|
|
|
self.db = QuotesDB()
|
|
|
|
callbacks.Privmsg.__init__(self)
|
|
|
|
|
2003-10-21 07:19:54 +02:00
|
|
|
def add(self, irc, msg, args):
|
2003-08-26 19:07:37 +02:00
|
|
|
"""[<channel>] <quote>
|
|
|
|
|
|
|
|
Adds <quote> to the quotes database for <channel>. <channel> is only
|
|
|
|
necessary if the message isn't sent in the channel itself.
|
|
|
|
"""
|
2003-03-12 07:26:59 +01:00
|
|
|
channel = privmsgs.getChannel(msg, args)
|
|
|
|
quote = privmsgs.getArgs(args)
|
2004-08-13 05:50:38 +02:00
|
|
|
id = self.db.add(channel, msg.nick, quote)
|
|
|
|
irc.replySuccess('(Quote #%s added)' % id)
|
2003-03-12 07:26:59 +01:00
|
|
|
|
2004-01-18 09:19:44 +01:00
|
|
|
def stats(self, irc, msg, args):
|
2003-08-26 19:07:37 +02:00
|
|
|
"""[<channel>]
|
|
|
|
|
|
|
|
Returns the numbers of quotes in the quote database for <channel>.
|
|
|
|
<channel> is only necessary if the message isn't sent in the channel
|
|
|
|
itself.
|
|
|
|
"""
|
2003-03-12 07:26:59 +01:00
|
|
|
channel = privmsgs.getChannel(msg, args)
|
2004-08-13 05:50:38 +02:00
|
|
|
size = self.db.size(channel)
|
2003-12-12 16:41:33 +01:00
|
|
|
s = 'There %s %s in my database.' % \
|
2004-08-13 05:50:38 +02:00
|
|
|
(utils.be(size), utils.nItems('quote', size))
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.reply(s)
|
2003-03-12 07:26:59 +01:00
|
|
|
|
2004-08-13 05:50:38 +02:00
|
|
|
def _replyQuote(self, irc, quote):
|
|
|
|
if isinstance(quote, QuoteRecord):
|
|
|
|
irc.reply('#%s: %s' % (quote.id, quote.text))
|
|
|
|
elif len(quote) > 10:
|
|
|
|
irc.reply('More than 10 quotes matched your criteria. '
|
|
|
|
'Please narrow your query.')
|
|
|
|
else:
|
|
|
|
quotes = ['#%s: "%s"' % (q.id, utils.ellipsisify(q.text, 30))
|
|
|
|
for q in quote]
|
|
|
|
irc.reply(utils.commaAndify(quotes))
|
|
|
|
|
|
|
|
def search(self, irc, msg, args):
|
2003-09-23 00:11:05 +02:00
|
|
|
"""[<channel>] --{id,regexp,from,with}=<value> ]
|
2003-08-26 19:07:37 +02:00
|
|
|
|
|
|
|
Returns quote(s) matching the given criteria. --from is who added the
|
|
|
|
quote; --id is the id number of the quote; --regexp is a regular
|
|
|
|
expression to search for.
|
|
|
|
"""
|
2003-03-12 07:26:59 +01:00
|
|
|
channel = privmsgs.getChannel(msg, args)
|
2003-09-23 00:11:05 +02:00
|
|
|
(optlist, rest) = getopt.getopt(args, '', ['id=', 'regexp=',
|
|
|
|
'from=', 'with='])
|
2003-08-26 19:07:37 +02:00
|
|
|
if not optlist and not rest:
|
|
|
|
raise callbacks.ArgumentError
|
2004-08-13 05:50:38 +02:00
|
|
|
kwargs = {'args': rest, 'id': [], 'with': [], 'by': [], 'predicate': []}
|
|
|
|
for (option, arg) in optlist:
|
2003-08-26 19:07:37 +02:00
|
|
|
option = option.lstrip('-')
|
|
|
|
if option == 'id':
|
|
|
|
try:
|
2004-08-13 05:50:38 +02:00
|
|
|
arg = int(arg)
|
|
|
|
kwargs[option].append(arg)
|
2003-08-26 19:07:37 +02:00
|
|
|
except ValueError:
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.error('--id value must be an integer.')
|
2003-08-26 19:07:37 +02:00
|
|
|
return
|
2003-09-23 00:11:05 +02:00
|
|
|
elif option == 'with':
|
2004-08-13 05:50:38 +02:00
|
|
|
kwargs[option].append(arg)
|
2003-08-26 19:07:37 +02:00
|
|
|
elif option == 'from':
|
2004-08-13 05:50:38 +02:00
|
|
|
kwargs['by'].append(arg)
|
2003-08-26 19:07:37 +02:00
|
|
|
elif option == 'regexp':
|
|
|
|
try:
|
2004-08-13 05:50:38 +02:00
|
|
|
r = utils.perlReToPythonRe(arg)
|
2003-08-26 20:10:17 +02:00
|
|
|
except ValueError:
|
|
|
|
try:
|
2004-08-13 05:50:38 +02:00
|
|
|
r = re.compile(arg, re.I)
|
2003-08-26 20:10:17 +02:00
|
|
|
except re.error, e:
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.error(str(e))
|
2003-08-26 20:10:17 +02:00
|
|
|
return
|
2004-08-16 18:36:18 +02:00
|
|
|
def p(s):
|
|
|
|
return int(bool(r.search(s)))
|
|
|
|
kwargs['predicate'].append(p)
|
2004-08-13 05:50:38 +02:00
|
|
|
quote = self.db.search(channel, **kwargs)
|
|
|
|
if quote is None:
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.reply('No quotes matched that criteria.')
|
2003-08-26 19:07:37 +02:00
|
|
|
else:
|
2004-08-13 05:50:38 +02:00
|
|
|
self._replyQuote(irc, quote)
|
2003-08-26 19:07:37 +02:00
|
|
|
### FIXME: we need to remove those predicates from the database.
|
2003-03-12 07:26:59 +01:00
|
|
|
|
2003-10-21 07:19:54 +02:00
|
|
|
def random(self, irc, msg, args):
|
2003-08-26 19:07:37 +02:00
|
|
|
"""[<channel>]
|
|
|
|
|
|
|
|
Returns a random quote from <channel>. <channel> is only necessary if
|
|
|
|
the message isn't sent in the channel itself.
|
|
|
|
"""
|
2003-03-12 07:26:59 +01:00
|
|
|
channel = privmsgs.getChannel(msg, args)
|
2004-08-13 05:50:38 +02:00
|
|
|
quote = self.db.random(channel)
|
|
|
|
if quote:
|
|
|
|
self._replyQuote(irc, quote)
|
|
|
|
else:
|
|
|
|
self.error('I have no quotes for this channel.')
|
2003-03-12 07:26:59 +01:00
|
|
|
|
2003-10-21 07:19:54 +02:00
|
|
|
def info(self, irc, msg, args):
|
2003-08-26 19:07:37 +02:00
|
|
|
"""[<channel>] <id>
|
|
|
|
|
|
|
|
Returns the metadata about the quote <id> in the quotes
|
|
|
|
database for <channel>. <channel> is only necessary if the message
|
|
|
|
isn't sent in the channel itself.
|
|
|
|
"""
|
2003-03-12 07:26:59 +01:00
|
|
|
channel = privmsgs.getChannel(msg, args)
|
|
|
|
id = privmsgs.getArgs(args)
|
2004-08-13 05:50:38 +02:00
|
|
|
try:
|
|
|
|
id = int(id)
|
|
|
|
except ValueError:
|
|
|
|
irc.error('Invalid id: %r' % id)
|
|
|
|
return
|
|
|
|
try:
|
|
|
|
quote = self.db.get(channel, id)
|
|
|
|
irc.reply(str(quote))
|
2004-08-16 18:36:18 +02:00
|
|
|
except QuotesDBError, e:
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.error('There isn\'t a quote with that id.')
|
2003-03-12 07:26:59 +01:00
|
|
|
|
2004-01-09 00:03:48 +01:00
|
|
|
def remove(self, irc, msg, args):
|
2003-08-26 19:07:37 +02:00
|
|
|
"""[<channel>] <id>
|
|
|
|
|
|
|
|
Removes quote <id> from the quotes database for <channel>. <channel>
|
|
|
|
is only necessary if the message isn't sent in the channel itself.
|
|
|
|
"""
|
2004-01-09 00:03:48 +01:00
|
|
|
channel = privmsgs.getChannel(msg, args)
|
2003-03-12 07:26:59 +01:00
|
|
|
id = privmsgs.getArgs(args)
|
2004-08-13 05:50:38 +02:00
|
|
|
try:
|
|
|
|
id = int(id)
|
|
|
|
except ValueError:
|
|
|
|
irc.error('That\'s not a valid id: %r' % id)
|
|
|
|
try:
|
|
|
|
self.db.remove(channel, id)
|
2004-01-08 16:24:56 +01:00
|
|
|
irc.replySuccess()
|
2004-08-13 05:50:38 +02:00
|
|
|
except KeyError:
|
|
|
|
irc.error('There was no such quote.')
|
2003-08-20 18:26:23 +02:00
|
|
|
|
2003-03-12 07:26:59 +01:00
|
|
|
|
|
|
|
Class = Quotes
|
2003-03-24 09:41:19 +01:00
|
|
|
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78:
|