Updated a whole lot of stuff.

This commit is contained in:
Jeremy Fincher 2003-08-26 17:07:37 +00:00
parent 898b219871
commit 3dd176ab63

View File

@ -37,10 +37,12 @@ from baseplugin import *
import re import re
import time import time
import getopt
import os.path import os.path
import sqlite import sqlite
import utils
import ircdb import ircdb
import privmsgs import privmsgs
import callbacks import callbacks
@ -59,15 +61,25 @@ class Quotes(ChannelDBHandler, callbacks.Privmsg):
cursor = db.cursor() cursor = db.cursor()
cursor.execute("""CREATE TABLE quotes ( cursor.execute("""CREATE TABLE quotes (
id INTEGER PRIMARY KEY, id INTEGER PRIMARY KEY,
added_by VARCHAR(255), added_by TEXT,
added_at TIMESTAMP, added_at TIMESTAMP,
quote TEXT quote TEXT
);""") );""")
cursor.execute("""CREATE TABLE quotegrabs (
id INTEGER PRIMARY KEY,
nick TEXT,
added_by TEXT,
quote TEXT
);""")
db.commit() db.commit()
return db return db
def addquote(self, irc, msg, args): def addquote(self, irc, msg, args):
"[<channel>] (if not sent through the channel itself) <quote>" """[<channel>] <quote>
Adds <quote> to the quotes database for <channel>. <channel> is only
necessary if the message isn't sent in the channel itself.
"""
channel = privmsgs.getChannel(msg, args) channel = privmsgs.getChannel(msg, args)
quote = privmsgs.getArgs(args) quote = privmsgs.getArgs(args)
db = self.getDb(channel) db = self.getDb(channel)
@ -78,52 +90,99 @@ class Quotes(ChannelDBHandler, callbacks.Privmsg):
db.commit() db.commit()
irc.reply(msg, conf.replySuccess) irc.reply(msg, conf.replySuccess)
def maxquote(self, irc, msg, args): def numquotes(self, irc, msg, args):
"[<channel>] (if not sent through the channel itself)" """[<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.
"""
channel = privmsgs.getChannel(msg, args) channel = privmsgs.getChannel(msg, args)
db = self.getDb(channel) db = self.getDb(channel)
cursor = db.cursor() cursor = db.cursor()
cursor.execute("""SELECT max(id) FROM quotes""") cursor.execute("""SELECT max(id) FROM quotes""")
maxid = cursor.fetchone()[0] maxid = int(cursor.fetchone()[0])
if maxid is None: if maxid is None:
maxid = 0 maxid = 0
s = 'There are approximately %s quotes in the database.' % maxid if maxid == 1:
IS = 'is'
QUOTE = 'quote'
else:
IS = 'are'
QUOTE = 'quotes'
s = 'There %s %s %s in my database.' % (IS, maxid, QUOTE)
irc.reply(msg, s) irc.reply(msg, s)
def quote(self, irc, msg, args): def quote(self, irc, msg, args):
"[<channel>] (if not sent through the channel itself) <number|regexp>" """[<channel>] --{id,regexp,from}=<value> [--{id,regexp,from}=<value>]
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.
"""
channel = privmsgs.getChannel(msg, args) channel = privmsgs.getChannel(msg, args)
value = privmsgs.getArgs(args) (optlist, rest) = getopt.getopt(args, '', ['id=', 'regexp=', 'from='])
if not optlist and not rest:
raise callbacks.ArgumentError
criteria = []
formats = []
predicateName = ''
db = self.getDb(channel) db = self.getDb(channel)
for (option, argument) in optlist:
option = option.lstrip('-')
if option == 'id':
try:
argument = int(argument)
criteria.append('id=%s' % argument)
except ValueError:
irc.error(msg, '--id value must be an integer.')
return
elif option == 'from':
criteria.append('added_by=%s')
formats.append(argument)
elif option == 'regexp':
try:
r = re.compile(argument, re.I)
except re.error, e:
irc.error(msg, str(e))
return
def p(s):
return bool(r.match(s))
predicateName += 'p'
db.create_function(predicateName, 1, p)
criteria.append('%s(quote)' % predicateName)
for s in rest:
s = '%%%s%%' % s
criteria.append('quote LIKE %s')
formats.append(s)
sql = """SELECT id, quote FROM quotes
WHERE %s""" % ' AND '.join(criteria)
debug.printf(sql)
cursor = db.cursor() cursor = db.cursor()
try: cursor.execute(sql, *formats)
id = int(value) if cursor.rowcount == 0:
cursor.execute("""SELECT quote FROM quotes WHERE id=%s""", id) irc.reply(msg, 'No quotes matched that criteria.')
ret = cursor.fetchall() elif cursor.rowcount == 1:
if ret: (id, quote) = cursor.fetchone()
irc.reply(msg, ret[0][0]) irc.reply(msg, '#%s: %s' % (id, quote))
else: elif cursor.rowcount > 10:
irc.reply(msg, "That quote doesn't exist.") irc.reply(msg, 'More than 10 quotes matched your criteria. '
except ValueError: # It's not an int. 'Please narrow your query.')
r = re.compile(value, re.I) else:
def p(s): results = cursor.fetchall()
return bool(r.match(s)) idsWithSnippets = []
db.create_function('p', 1, p) for (id, quote) in results:
cursor.execute("""SELECT id, quote FROM quotes WHERE p(quote)""") s = '#%s: "%s..."' % (id, quote[:30])
if cursor.rowcount == 0: idsWithSnippets.append(s)
irc.reply(msg, 'No quotes matched that regexp.') irc.reply(msg, utils.commaAndify(idsWithSnippets))
elif cursor.rowcount == 1: ### FIXME: we need to remove those predicates from the database.
(id, quote) = cursor.fetchone()
irc.reply(msg, 'Quote %s: %s' % (id, quote))
elif cursor.rowcount > 5:
ids = [t[0] for t in cursor.fetchall()]
irc.reply(msg, 'Quotes %s matched.' % ', '.join(ids))
else:
L = ['%s: %s' % (id,s[:30]) for (id,s) in cursor.fetchall()]
irc.reply(msg, 'These quotes matched: %s' % ', '.join(L))
def randomquote(self, irc, msg, args): def randomquote(self, irc, msg, args):
"[<channel>] (if not sent through the channel itself)" """[<channel>]
Returns a random quote from <channel>. <channel> is only necessary if
the message isn't sent in the channel itself.
"""
channel = privmsgs.getChannel(msg, args) channel = privmsgs.getChannel(msg, args)
db = self.getDb(channel) db = self.getDb(channel)
cursor = db.cursor() cursor = db.cursor()
@ -134,10 +193,15 @@ class Quotes(ChannelDBHandler, callbacks.Privmsg):
irc.error(msg, 'It seems that quote database is empty.') irc.error(msg, 'It seems that quote database is empty.')
return return
(id, quote) = cursor.fetchone() (id, quote) = cursor.fetchone()
irc.reply(msg, '%s [#%s]' % (quote, id)) irc.reply(msg, '%s (#%s)' % (quote, id))
def quoteinfo(self, irc, msg, args): def quoteinfo(self, irc, msg, args):
"[<channel>] (if not sent through the channel itself) <number>" """[<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.
"""
channel = privmsgs.getChannel(msg, args) channel = privmsgs.getChannel(msg, args)
id = privmsgs.getArgs(args) id = privmsgs.getArgs(args)
db = self.getDb(channel) db = self.getDb(channel)
@ -153,7 +217,11 @@ class Quotes(ChannelDBHandler, callbacks.Privmsg):
irc.reply(msg, 'There isn\'t a quote with that id.') irc.reply(msg, 'There isn\'t a quote with that id.')
def removequote(self, irc, msg, args): def removequote(self, irc, msg, args):
"[<channel>] (if not sent through the channel itself) <number>" """[<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.
"""
channel = privmsgs.getChannel(msg, args) channel = privmsgs.getChannel(msg, args)
id = privmsgs.getArgs(args) id = privmsgs.getArgs(args)
db = self.getDb(channel) db = self.getDb(channel)