Limnoria/plugins/Quotes.py

186 lines
6.8 KiB
Python
Raw Normal View History

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.
###
"""
Maintains a Quotes database for each channel.
Commands include:
quote
addquote
removequote
maxquote
randomquote
quoteinfo
"""
2003-03-12 07:26:59 +01:00
from baseplugin import *
import re
import time
import os.path
import sqlite
import ircdb
import privmsgs
import callbacks
2003-03-28 06:15:08 +01:00
class Quotes(ChannelDBHandler, callbacks.Privmsg):
2003-03-12 07:26:59 +01:00
def __init__(self):
2003-03-28 06:15:08 +01:00
ChannelDBHandler.__init__(self)
2003-03-12 07:26:59 +01:00
callbacks.Privmsg.__init__(self)
def makeDb(self, filename):
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,
added_by VARCHAR(255),
added_at TIMESTAMP,
quote TEXT
);""")
db.commit()
return db
def addquote(self, irc, msg, args):
"[<channel>] (if not sent through the channel itself) <quote>"
channel = privmsgs.getChannel(msg, args)
quote = privmsgs.getArgs(args)
db = self.getDb(channel)
cursor = db.cursor()
cursor.execute("""INSERT INTO quotes
VALUES(NULL, %s, %s, %s)""",
msg.nick, int(time.time()), quote)
db.commit()
irc.reply(msg, conf.replySuccess)
def maxquote(self, irc, msg, args):
"[<channel>] (if not sent through the channel itself)"
channel = privmsgs.getChannel(msg, args)
db = self.getDb(channel)
cursor = db.cursor()
cursor.execute("""SELECT max(id) FROM quotes""")
maxid = cursor.fetchone()[0]
if maxid is None:
maxid = 0
s = 'There are approximately %s quotes in the database.' % maxid
irc.reply(msg, s)
def quote(self, irc, msg, args):
"[<channel>] (if not sent through the channel itself) <number|regexp>"
channel = privmsgs.getChannel(msg, args)
value = privmsgs.getArgs(args)
db = self.getDb(channel)
cursor = db.cursor()
try:
id = int(value)
cursor.execute("""SELECT quote FROM quotes WHERE id=%s""", id)
ret = cursor.fetchall()
if ret:
irc.reply(msg, ret[0][0])
return
else:
irc.reply(msg, "That quote doesn't exist.")
return
except ValueError: # It's not an int.
r = re.compile(value, re.I)
def p(s):
return bool(r.match(s))
db.create_function('p', 1, p)
cursor.execute("""SELECT id, quote FROM quotes WHERE p(quote)""")
if cursor.rowcount == 0:
irc.reply(msg, 'No quotes matched that regexp.')
return
elif cursor.rowcount == 1:
(id, quote) = cursor.fetchone()
irc.reply(msg, 'Quote %s: %s' % (id, quote))
return
elif cursor.rowcount > 5:
ids = [t[0] for t in cursor.fetchall()]
irc.reply(msg, 'Quotes %s matched.' % ', '.join(ids))
return
else:
L = ['%s: %s' % (id,s[:30]) for (id,s) in cursor.fetchall()]
irc.reply(msg, 'These quotes matched: %s' % ', '.join(L))
return
def randomquote(self, irc, msg, args):
"[<channel>] (if not sent through the channel itself)"
channel = privmsgs.getChannel(msg, args)
db = self.getDb(channel)
cursor = db.cursor()
2003-04-21 15:07:01 +02:00
cursor.execute("""SELECT id, quote FROM quotes
2003-03-12 07:26:59 +01:00
ORDER BY random()
LIMIT 1""")
if cursor.rowcount != 1:
irc.error(msg, 'It seems that quote database is empty.')
return
(id, quote) = cursor.fetchone()
irc.reply(msg, '%s [#%s]' % (quote, id))
2003-03-12 07:26:59 +01:00
return
def quoteinfo(self, irc, msg, args):
"[<channel>] (if not sent through the channel itself) <number>"
channel = privmsgs.getChannel(msg, args)
id = privmsgs.getArgs(args)
db = self.getDb(channel)
cursor = db.cursor()
cursor.execute("""SELECT * FROM quotes WHERE id=%s""", id)
row = cursor.fetchone()
if row:
irc.reply(msg, 'Quote %r added by %s at %s.' % \
(row.quote, row.added_by, time.strftime(conf.timestampFormat)))
return
else:
irc.reply(msg, 'There isn\'t a quote with that id.')
return
def removequote(self, irc, msg, args):
"[<channel>] (if not sent through the channel itself) <number>"
channel = privmsgs.getChannel(msg, args)
id = privmsgs.getArgs(args)
db = self.getDb(channel)
cursor = db.cursor()
capability = ircdb.makeChannelCapability(channel, 'op')
if ircdb.checkCapability(msg.prefix, capability):
cursor.execute("""DELETE FROM quotes WHERE id=%s""", id)
irc.reply(msg, conf.replySuccess)
else:
irc.error(msg, conf.replyNoCapability % capability)
Class = Quotes
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78: