2003-11-03 07:59:38 +01:00
|
|
|
###
|
2004-08-23 15:14:06 +02:00
|
|
|
# Copyright (c) 2002-2004, Jeremiah Fincher
|
2003-11-03 07:59:38 +01:00
|
|
|
# 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-11-18 00:17:58 +01:00
|
|
|
Quotegrabs are like IRC sound bites. When someone says something funny,
|
|
|
|
incriminating, stupid, outrageous, ... anything that might be worth
|
|
|
|
remembering, you can grab that quote for that person. With this plugin, you
|
|
|
|
can store many quotes per person and display their most recent quote, as well
|
|
|
|
as see who "grabbed" the quote in the first place.
|
2003-11-03 07:59:38 +01:00
|
|
|
"""
|
|
|
|
|
2003-11-25 09:23:47 +01:00
|
|
|
__revision__ = "$Id$"
|
|
|
|
|
2004-07-24 07:18:26 +02:00
|
|
|
import supybot.plugins as plugins
|
2003-11-03 07:59:38 +01:00
|
|
|
|
|
|
|
import os
|
|
|
|
import time
|
2003-11-03 17:17:40 +01:00
|
|
|
import random
|
2003-11-03 07:59:38 +01:00
|
|
|
|
2004-08-02 18:19:32 +02:00
|
|
|
import supybot.registry as registry
|
2004-01-28 01:08:43 +01:00
|
|
|
|
2004-07-24 07:18:26 +02:00
|
|
|
import supybot.conf as conf
|
|
|
|
import supybot.ircdb as ircdb
|
|
|
|
import supybot.utils as utils
|
|
|
|
import supybot.ircmsgs as ircmsgs
|
|
|
|
import supybot.plugins as plugins
|
|
|
|
import supybot.ircutils as ircutils
|
|
|
|
import supybot.callbacks as callbacks
|
2004-10-24 04:35:59 +02:00
|
|
|
from supybot.commands import wrap, additional
|
2003-11-03 07:59:38 +01:00
|
|
|
|
2003-12-04 00:48:00 +01:00
|
|
|
try:
|
|
|
|
import sqlite
|
|
|
|
except ImportError:
|
|
|
|
raise callbacks.Error, 'You need to have PySQLite installed to use this ' \
|
|
|
|
'plugin. Download it at <http://pysqlite.sf.net/>'
|
2003-11-03 07:59:38 +01:00
|
|
|
|
2004-01-28 01:08:43 +01:00
|
|
|
conf.registerPlugin('QuoteGrabs')
|
|
|
|
conf.registerChannelValue(conf.supybot.plugins.QuoteGrabs, 'randomGrabber',
|
|
|
|
registry.Boolean(False, """Determines whether the bot will randomly grab
|
2004-01-28 15:05:21 +01:00
|
|
|
possibly-suitable quotes on occasion. The suitability of a given message
|
|
|
|
is determined by ..."""))
|
|
|
|
conf.registerChannelValue(conf.supybot.plugins.QuoteGrabs.randomGrabber,
|
|
|
|
'averageTimeBetweenGrabs',
|
|
|
|
registry.PositiveInteger(864000, """Determines about how many seconds, on
|
|
|
|
average, should elapse between random grabs. This is only an average
|
|
|
|
value; grabs can happen from any time after half this time until never,
|
|
|
|
although that's unlikely to occur."""))
|
|
|
|
conf.registerChannelValue(conf.supybot.plugins.QuoteGrabs.randomGrabber,
|
|
|
|
'minimumWords', registry.PositiveInteger(3, """Determines the minimum
|
|
|
|
number of words in a message for it to be considered for random
|
|
|
|
grabbing."""))
|
|
|
|
conf.registerChannelValue(conf.supybot.plugins.QuoteGrabs.randomGrabber,
|
|
|
|
'minimumCharacters', registry.PositiveInteger(8, """Determines the
|
|
|
|
minimum number of characters in a message for it to be considered for
|
|
|
|
random grabbing."""))
|
2004-01-28 01:08:43 +01:00
|
|
|
|
2004-01-28 15:05:21 +01:00
|
|
|
class QuoteGrabs(plugins.ChannelDBHandler, callbacks.Privmsg):
|
2003-11-03 07:59:38 +01:00
|
|
|
def __init__(self):
|
|
|
|
plugins.ChannelDBHandler.__init__(self)
|
|
|
|
callbacks.Privmsg.__init__(self)
|
|
|
|
|
|
|
|
def makeDb(self, filename):
|
|
|
|
if os.path.exists(filename):
|
2003-11-05 07:29:07 +01:00
|
|
|
db = sqlite.connect(filename, converters={'bool': bool})
|
|
|
|
else:
|
|
|
|
db = sqlite.connect(filename, coverters={'bool': bool})
|
|
|
|
cursor = db.cursor()
|
|
|
|
cursor.execute("""CREATE TABLE quotegrabs (
|
|
|
|
id INTEGER PRIMARY KEY,
|
|
|
|
nick TEXT,
|
|
|
|
hostmask TEXT,
|
|
|
|
added_by TEXT,
|
|
|
|
added_at TIMESTAMP,
|
|
|
|
quote TEXT
|
|
|
|
);""")
|
|
|
|
def p(s1, s2):
|
|
|
|
return int(ircutils.nickEqual(s1, s2))
|
|
|
|
db.create_function('nickeq', 2, p)
|
2003-11-03 07:59:38 +01:00
|
|
|
db.commit()
|
|
|
|
return db
|
2004-07-21 21:36:35 +02:00
|
|
|
|
2003-11-03 07:59:38 +01:00
|
|
|
def doPrivmsg(self, irc, msg):
|
2004-09-01 14:37:07 +02:00
|
|
|
irc = callbacks.SimpleProxy(irc, msg)
|
2003-11-03 07:59:38 +01:00
|
|
|
if ircutils.isChannel(msg.args[0]):
|
2004-01-28 15:05:21 +01:00
|
|
|
(channel, payload) = msg.args
|
2004-02-11 07:08:25 +01:00
|
|
|
words = self.registryValue('randomGrabber.minimumWords',
|
|
|
|
channel)
|
|
|
|
length = self.registryValue('randomGrabber.minimumCharacters',
|
|
|
|
channel)
|
2004-08-02 19:00:40 +02:00
|
|
|
grabTime = \
|
|
|
|
self.registryValue('randomGrabber.averageTimeBetweenGrabs',
|
|
|
|
channel)
|
2004-01-28 15:05:21 +01:00
|
|
|
if self.registryValue('randomGrabber', channel):
|
|
|
|
if len(payload) > length and len(payload.split()) > words:
|
2003-11-03 07:59:38 +01:00
|
|
|
db = self.getDb(channel)
|
|
|
|
cursor = db.cursor()
|
|
|
|
cursor.execute("""SELECT added_at FROM quotegrabs
|
|
|
|
WHERE nick=%s
|
|
|
|
ORDER BY id DESC LIMIT 1""", msg.nick)
|
|
|
|
if cursor.rowcount == 0:
|
2003-11-04 05:38:40 +01:00
|
|
|
self._grab(irc, msg, irc.prefix)
|
2003-11-03 17:17:40 +01:00
|
|
|
self._sendGrabMsg(irc, msg)
|
2003-11-03 07:59:38 +01:00
|
|
|
else:
|
|
|
|
last = int(cursor.fetchone()[0])
|
|
|
|
elapsed = int(time.time()) - last
|
|
|
|
if random.random()*elapsed > grabTime/2:
|
2003-11-04 05:38:40 +01:00
|
|
|
self._grab(irc, msg, irc.prefix)
|
2003-11-03 17:17:40 +01:00
|
|
|
self._sendGrabMsg(irc, msg)
|
2003-11-03 07:59:38 +01:00
|
|
|
|
2003-11-04 05:38:40 +01:00
|
|
|
def _grab(self, irc, msg, addedBy):
|
2003-11-03 07:59:38 +01:00
|
|
|
channel = msg.args[0]
|
|
|
|
db = self.getDb(channel)
|
|
|
|
cursor = db.cursor()
|
|
|
|
text = ircmsgs.prettyPrint(msg)
|
2003-11-04 05:38:40 +01:00
|
|
|
# Check to see if the latest quotegrab is identical
|
|
|
|
cursor.execute("""SELECT quote FROM quotegrabs
|
|
|
|
WHERE nick=%s
|
|
|
|
ORDER BY id DESC LIMIT 1""", msg.nick)
|
|
|
|
if cursor.rowcount != 0:
|
|
|
|
if text == cursor.fetchone()[0]:
|
|
|
|
return
|
2003-11-03 07:59:38 +01:00
|
|
|
cursor.execute("""INSERT INTO quotegrabs
|
|
|
|
VALUES (NULL, %s, %s, %s, %s, %s)""",
|
|
|
|
msg.nick, msg.prefix, addedBy, int(time.time()), text)
|
|
|
|
db.commit()
|
|
|
|
|
2003-11-03 17:17:40 +01:00
|
|
|
def _sendGrabMsg(self, irc, msg):
|
2004-07-21 21:36:35 +02:00
|
|
|
s = 'jots down a new quote for %s' % msg.nick
|
2004-09-09 01:23:25 +02:00
|
|
|
irc.reply(s, action=True, prefixName=False)
|
2003-11-03 17:17:40 +01:00
|
|
|
|
2004-10-23 23:27:01 +02:00
|
|
|
def grab(self, irc, msg, args, channel, nick):
|
2003-11-03 07:59:38 +01:00
|
|
|
"""[<channel>] <nick>
|
|
|
|
|
|
|
|
Grabs a quote from <channel> by <nick> for the quotegrabs table.
|
|
|
|
<channel> is only necessary if the message isn't sent in the channel
|
|
|
|
itself.
|
|
|
|
"""
|
2004-08-31 19:23:33 +02:00
|
|
|
if ircutils.strEqual(nick, msg.nick):
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.error('You can\'t quote grab yourself.')
|
2003-11-03 07:59:38 +01:00
|
|
|
return
|
2004-04-22 04:55:46 +02:00
|
|
|
for m in reversed(irc.state.history):
|
2004-10-23 23:27:01 +02:00
|
|
|
# XXX Note that the channel isn't used here. Also note that
|
|
|
|
# channel might be None since we use channeldb
|
2004-08-31 19:23:33 +02:00
|
|
|
if m.command == 'PRIVMSG' and ircutils.strEqual(m.nick, nick):
|
2003-11-04 05:38:40 +01:00
|
|
|
self._grab(irc, m, msg.prefix)
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.replySuccess()
|
2003-11-03 07:59:38 +01:00
|
|
|
return
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.error('I couldn\'t find a proper message to grab.')
|
2004-10-23 23:27:01 +02:00
|
|
|
grab = wrap(grab, ['channeldb', 'nick'])
|
2003-11-03 07:59:38 +01:00
|
|
|
|
2004-10-23 23:27:01 +02:00
|
|
|
def quote(self, irc, msg, args, channel, nick):
|
2003-11-03 07:59:38 +01:00
|
|
|
"""[<channel>] <nick>
|
|
|
|
|
|
|
|
Returns <nick>'s latest quote grab in <channel>. <channel> is only
|
|
|
|
necessary if the message isn't sent in the channel itself.
|
|
|
|
"""
|
|
|
|
db = self.getDb(channel)
|
|
|
|
cursor = db.cursor()
|
|
|
|
cursor.execute("""SELECT quote FROM quotegrabs
|
2003-11-05 07:29:07 +01:00
|
|
|
WHERE nickeq(nick, %s)
|
2003-11-03 07:59:38 +01:00
|
|
|
ORDER BY id DESC LIMIT 1""", nick)
|
|
|
|
if cursor.rowcount == 0:
|
2004-08-19 14:19:18 +02:00
|
|
|
irc.error('I couldn\'t find a matching quotegrab for %s.' % nick)
|
2003-11-03 07:59:38 +01:00
|
|
|
else:
|
|
|
|
text = cursor.fetchone()[0]
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.reply(text)
|
2004-10-23 23:27:01 +02:00
|
|
|
quote = wrap(quote, ['channeldb', 'nick'])
|
2003-11-03 07:59:38 +01:00
|
|
|
|
2004-10-23 23:27:01 +02:00
|
|
|
def list(self, irc, msg, args, channel, nick):
|
|
|
|
"""[<channel>] <nick>
|
2003-11-04 05:38:40 +01:00
|
|
|
|
|
|
|
Returns a list of shortened quotes that have been grabbed for <nick>
|
|
|
|
as well as the id of each quote. These ids can be used to get the
|
2004-10-23 23:27:01 +02:00
|
|
|
full quote. <channel> is only necessary if the message isn't sent in
|
|
|
|
the channel itself.
|
2003-11-04 05:38:40 +01:00
|
|
|
"""
|
2004-10-23 23:27:01 +02:00
|
|
|
# XXX This doesn't seem to be channel-specific in practice.
|
2003-11-04 05:38:40 +01:00
|
|
|
db = self.getDb(channel)
|
|
|
|
cursor = db.cursor()
|
|
|
|
cursor.execute("""SELECT id, quote FROM quotegrabs
|
2004-01-13 07:39:58 +01:00
|
|
|
WHERE nick LIKE %s
|
2003-11-04 05:38:40 +01:00
|
|
|
ORDER BY id ASC""", nick)
|
|
|
|
if cursor.rowcount == 0:
|
2004-08-19 14:19:18 +02:00
|
|
|
irc.error('I couldn\'t find any quotegrabs for %s.' % nick)
|
2003-11-04 05:38:40 +01:00
|
|
|
else:
|
|
|
|
l = []
|
|
|
|
for (id, quote) in cursor.fetchall():
|
|
|
|
# strip the nick from the quote
|
|
|
|
quote = quote.replace('<%s> ' % nick, '', 1)
|
|
|
|
item_str = utils.ellipsisify('#%s: %s' % (id, quote), 50)
|
|
|
|
l.append(item_str)
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.reply(utils.commaAndify(l))
|
2004-10-23 23:27:01 +02:00
|
|
|
list = wrap(list, ['channeldb', 'nick'])
|
2003-11-04 05:38:40 +01:00
|
|
|
|
2004-10-23 23:27:01 +02:00
|
|
|
# XXX Could we rename this "random", to fit in more with the rest of
|
|
|
|
# our plugins?
|
|
|
|
def randomquote(self, irc, msg, args, channel, nick):
|
|
|
|
"""[<channel>] [<nick>]
|
2003-12-29 12:12:37 +01:00
|
|
|
|
|
|
|
Returns a randomly grabbed quote, optionally choosing only from those
|
2004-10-23 23:27:01 +02:00
|
|
|
quotes grabbed for <nick>. <channel> is only necessary if the message
|
|
|
|
isn't sent in the channel itself.
|
2003-12-29 12:12:37 +01:00
|
|
|
"""
|
|
|
|
db = self.getDb(channel)
|
|
|
|
cursor = db.cursor()
|
|
|
|
if nick:
|
|
|
|
cursor.execute("""SELECT quote FROM quotegrabs
|
|
|
|
WHERE nick LIKE %s ORDER BY random() LIMIT 1""",
|
|
|
|
nick)
|
|
|
|
else:
|
|
|
|
cursor.execute("""SELECT quote FROM quotegrabs
|
|
|
|
ORDER BY random() LIMIT 1""")
|
|
|
|
if cursor.rowcount == 0:
|
|
|
|
if nick:
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.error('Couldn\'t get a random quote for that nick.')
|
2003-12-29 12:12:37 +01:00
|
|
|
else:
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.error('Couldn\'t get a random quote. Are there any'
|
2003-12-29 12:12:37 +01:00
|
|
|
'grabbed quotes in the database?')
|
|
|
|
return
|
|
|
|
quote = cursor.fetchone()[0]
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.reply(quote)
|
2004-10-23 23:27:01 +02:00
|
|
|
randomquote = wrap(randomquote, ['channeldb', additional('nick')])
|
2003-12-29 12:12:37 +01:00
|
|
|
|
2004-10-23 23:27:01 +02:00
|
|
|
def get(self, irc, msg, args, channel, id):
|
|
|
|
"""[<channel>] <id>
|
2003-11-06 00:56:30 +01:00
|
|
|
|
2004-10-23 23:27:01 +02:00
|
|
|
Return the quotegrab with the given <id>. <channel> is only necessary
|
|
|
|
if the message isn't sent in the channel itself.
|
2003-11-06 00:56:30 +01:00
|
|
|
"""
|
|
|
|
db = self.getDb(channel)
|
|
|
|
cursor = db.cursor()
|
2004-01-28 01:08:43 +01:00
|
|
|
cursor.execute("""SELECT quote, hostmask, added_at, added_by
|
2003-11-06 22:50:23 +01:00
|
|
|
FROM quotegrabs WHERE id = %s""", id)
|
2003-11-06 00:56:30 +01:00
|
|
|
if cursor.rowcount == 0:
|
2004-10-24 00:07:50 +02:00
|
|
|
irc.error('No quotegrab for id %s' % utils.quoted(id))
|
2003-11-06 00:56:30 +01:00
|
|
|
return
|
2004-01-28 01:08:43 +01:00
|
|
|
quote, hostmask, timestamp, grabber_mask = cursor.fetchone()
|
2004-01-18 08:58:26 +01:00
|
|
|
time_str = time.strftime(conf.supybot.humanTimestampFormat(),
|
2003-11-06 22:50:23 +01:00
|
|
|
time.localtime(float(timestamp)))
|
2004-01-28 01:08:43 +01:00
|
|
|
try:
|
|
|
|
grabber = ircdb.users.getUser(grabber_mask).name
|
|
|
|
except:
|
2004-07-21 21:36:35 +02:00
|
|
|
grabber = grabber_mask
|
2004-02-13 11:33:18 +01:00
|
|
|
irc.reply('%s (Said by: %s; grabbed by %s at %s)' % \
|
2004-01-28 01:08:43 +01:00
|
|
|
(quote, hostmask, grabber, time_str))
|
2004-10-23 23:27:01 +02:00
|
|
|
get = wrap(get, ['channeldb', 'id'])
|
2003-11-06 00:56:30 +01:00
|
|
|
|
2003-11-03 07:59:38 +01:00
|
|
|
|
|
|
|
Class = QuoteGrabs
|
|
|
|
|
|
|
|
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78:
|