Limnoria/plugins/FunDB.py

442 lines
16 KiB
Python
Raw Normal View History

2003-03-12 07:26:59 +01:00
###
# Copyright (c) 2002-2004, Jeremiah Fincher
2003-03-12 07:26:59 +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.
###
"""
Provides fun commands that require a database to operate.
"""
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
2003-10-11 23:03:02 +02:00
import re
2004-08-06 13:16:05 +02:00
import csv
import sets
2004-08-06 13:16:05 +02:00
import random
import itertools
2004-08-11 08:17:11 +02:00
import supybot.dbi as dbi
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.world as world
import supybot.ircmsgs as ircmsgs
import supybot.ircutils as ircutils
import supybot.privmsgs as privmsgs
2004-08-06 13:16:05 +02:00
import supybot.registry as registry
2004-07-24 07:18:26 +02:00
import supybot.callbacks as callbacks
2003-03-12 07:26:59 +01:00
class FunDBRecord(object):
__metaclass__ = dbi.Record
__fields__ = [
'by',
'text',
]
2004-08-11 08:17:11 +02:00
class DbiFunDBDB(object):
class FunDBDB(dbi.DB):
Record = FunDBRecord
def __init__(self, filename):
2004-08-06 13:16:05 +02:00
self.dbs = ircutils.IrcDict()
self.filename = filename
2004-08-06 13:16:05 +02:00
def close(self):
for type in self.dbs.itervalues():
for db in type.itervalues():
2004-08-06 13:16:05 +02:00
db.close()
def _getDb(self, channel, type):
type = type.lower()
if channel not in self.dbs:
self.dbs[channel] = {}
if type not in self.dbs[channel]:
filename = self.filename.replace('db', '%s.db' % type.capitalize())
filename = plugins.makeChannelFilename(filename, channel)
2004-08-06 13:16:05 +02:00
self.dbs[channel][type] = self.FunDBDB(filename)
return self.dbs[channel][type]
def get(self, channel, type, id):
db = self._getDb(channel, type)
2004-08-11 08:17:11 +02:00
return db.get(id)
2004-08-06 13:16:05 +02:00
def add(self, channel, type, text, by):
db = self._getDb(channel, type)
2004-08-11 08:17:11 +02:00
return db.add(db.Record(by=by, text=text))
2004-08-06 13:16:05 +02:00
def remove(self, channel, type, id):
db = self._getDb(channel, type)
2004-08-11 08:17:11 +02:00
db.remove(id)
2004-08-06 13:16:05 +02:00
def change(self, channel, type, id, f):
db = self._getDb(channel, type)
2004-08-11 08:17:11 +02:00
record = db.get(id)
record.text = f(record.text)
db.set(id, record)
2004-08-06 13:16:05 +02:00
def random(self, channel, type):
db = self._getDb(channel, type)
2004-08-11 08:17:11 +02:00
return db.random()
2004-08-06 13:16:05 +02:00
def size(self, channel, type):
db = self._getDb(channel, type)
2004-08-11 08:17:11 +02:00
return itertools.ilen(db)
def search(self, channel, type, p):
db = self._getDb(channel, type)
return db.select(p)
2004-08-06 13:16:05 +02:00
FunDBDB = plugins.DB('FunDB',
{'flat': DbiFunDBDB})
conf.registerPlugin('FunDB')
conf.registerChannelValue(conf.supybot.plugins.FunDB, 'showIds',
2004-08-06 13:16:05 +02:00
registry.Boolean(True, """Determines whether the bot will show the id of an
insult/praise/lart."""))
2004-07-21 21:36:35 +02:00
2004-08-06 13:16:05 +02:00
class FunDB(callbacks.Privmsg):
2003-03-12 07:26:59 +01:00
"""
Contains the 'fun' commands that require a database. Currently includes
2004-08-06 14:41:07 +02:00
commands for larting, praising, and insulting.
2003-03-12 07:26:59 +01:00
"""
2004-08-06 13:16:05 +02:00
_types = ('insult', 'lart', 'praise')
2003-03-12 07:26:59 +01:00
def __init__(self):
self.__parent = super(FunDB, self)
self.__parent.__init__()
2004-08-06 13:16:05 +02:00
self.db = FunDBDB()
2003-03-12 07:26:59 +01:00
def die(self):
2004-08-06 13:23:25 +02:00
self.db.close()
self.__parent.die()
2004-08-06 13:16:05 +02:00
def _getBy(self, by):
try:
return ircdb.users.getUser(int(by)).name
except ValueError:
return by
2004-08-06 13:16:05 +02:00
def _validType(self, irc, type, error=True):
lowerTypes = [t.lower() for t in self._types]
if type.lower() not in lowerTypes:
2004-08-06 13:16:05 +02:00
if error:
irc.error('%r is not a valid type. Valid types include %s.' %
(type, utils.commaAndify(self._types)))
return False
else:
return True
def _validId(self, irc, id, error=True):
try:
return int(id)
except ValueError:
if error:
irc.error('The <id> argument must be an integer.')
return None
def _isRegistered(self, irc, msg):
try:
return ircdb.users.getUserId(msg.prefix)
except KeyError:
irc.errorNotRegistered()
return None
2003-08-20 18:26:23 +02:00
def add(self, irc, msg, args):
2004-08-06 13:16:05 +02:00
"""[<channel>] <lart|insult|praise> <text>
2003-04-20 08:26:17 +02:00
2003-08-25 22:13:04 +02:00
Adds another record to the data referred to in the first argument. For
commands that will later respond with an ACTION (lart and praise), $who
should be in the message to show who should be larted or praised. I.e.
'fundb add lart slices $who in half with a free AOL cd' would make the
bot, when it used that lart against, say, jemfinch, to say '/me slices
2003-12-02 14:54:57 +01:00
jemfinch in half with a free AOL cd' <channel> is only necessary if
the message isn't sent in the channel itself.
2003-04-20 08:26:17 +02:00
"""
2003-12-02 14:54:57 +01:00
channel = privmsgs.getChannel(msg, args)
2004-08-06 13:16:05 +02:00
(type, s) = privmsgs.getArgs(args, required=2)
type = type.lower()
userId = self._isRegistered(irc, msg)
if not userId:
return
2004-08-06 13:16:05 +02:00
if not self._validType(irc, type):
return
if type == "lart" or type == "praise":
if '$who' not in s:
irc.error('There must be a $who in the lart/praise somewhere')
return
2004-08-06 13:16:05 +02:00
id = self.db.add(channel, type, s, userId)
irc.replySuccess('(%s #%s added)' % (type, id))
2003-03-12 07:26:59 +01:00
def remove(self, irc, msg, args):
2004-08-06 13:16:05 +02:00
"""[<channel>] <lart|insult|praise> <id>
2003-04-20 08:26:17 +02:00
Removes the data, referred to in the first argument, with the id
2003-12-02 14:54:57 +01:00
number <id> from the database. <channel> is only necessary if the
message isn't sent in the channel itself.
2003-04-20 08:26:17 +02:00
"""
2003-12-02 14:54:57 +01:00
channel = privmsgs.getChannel(msg, args)
2004-08-06 13:16:05 +02:00
(type, id) = privmsgs.getArgs(args, required=2)
if not self._isRegistered(irc, msg):
return
2004-08-06 13:16:05 +02:00
if not self._validType(irc, type):
2003-03-12 07:26:59 +01:00
return
2004-08-06 13:16:05 +02:00
id = self._validId(irc, id)
if id is None:
return
2004-08-06 13:16:05 +02:00
try:
self.db.remove(channel, type, id)
irc.replySuccess()
except KeyError:
irc.error('There is no %s with that id.' % type)
2003-08-20 18:26:23 +02:00
def search(self, irc, msg, args):
"""[<channel>] <lart|insult|praise> <text>
Searches the database specified for <text>, and returns the records
with matching ids as well as a snippet of the text of each record.
"""
channel = privmsgs.getChannel(msg, args)
(type, text) = privmsgs.getArgs(args, required=2)
if not self._validType(irc, type):
return
def p(record):
return text in record.text
results = self.db.search(channel, type, p)
L = ['#%s: %s' % (record.id, utils.ellipsisify(record.text, 50)) \
for record in results]
if not L:
irc.error('There is no %s containing that text.' % type)
else:
irc.reply(utils.commaAndify(L))
def change(self, irc, msg, args):
2004-08-06 13:16:05 +02:00
"""[<channel>] <lart|insult|praise> <id> <regexp>
Changes the data, referred to in the first argument, with the id
number <id> according to the regular expression <regexp>. <id> is the
zero-based index into the db; <regexp> is a regular expression of the
2003-12-02 14:54:57 +01:00
form s/regexp/replacement/flags. <channel> is only necessary if the
message isn't sent in the channel itself.
"""
2003-12-02 14:54:57 +01:00
channel = privmsgs.getChannel(msg, args)
2004-08-06 13:16:05 +02:00
(type, id, regexp) = privmsgs.getArgs(args, required=3)
if not self._validType(irc, type):
return
2004-08-06 13:16:05 +02:00
id = self._validId(irc, id)
if id is None:
return
2004-08-06 13:16:05 +02:00
if not self._isRegistered(irc, msg):
return
try:
replacer = utils.perlReToReplacer(regexp)
except ValueError, e:
2004-08-06 13:16:05 +02:00
irc.error('That regexp wasn\'t valid: %s.' % e.args[0])
return
except re.error, e:
irc.error(utils.exnToString(e))
return
2004-08-06 13:16:05 +02:00
try:
self.db.change(channel, type, id, replacer)
irc.replySuccess()
2004-08-06 13:16:05 +02:00
except KeyError:
irc.error('There is no %s with that id.' % type)
2004-01-18 09:19:44 +01:00
def stats(self, irc, msg, args):
2004-08-06 13:16:05 +02:00
"""[<channel>] <lart|insult|praise>
Returns the number of records, of the type specified, currently in
2003-12-02 14:54:57 +01:00
the database. <channel> is only necessary if the message isn't sent
in the channel itself.
2003-04-20 08:26:17 +02:00
"""
2003-12-02 14:54:57 +01:00
channel = privmsgs.getChannel(msg, args)
2004-08-06 13:16:05 +02:00
type = privmsgs.getArgs(args)
if not self._validType(irc, type):
return
2004-08-06 13:16:05 +02:00
total = self.db.size(channel, type)
irc.reply('There %s currently %s in my database.' %
2004-08-06 13:16:05 +02:00
(utils.be(total), utils.nItems(type, total)))
def get(self, irc, msg, args):
2004-08-06 13:16:05 +02:00
"""[<channel>] <lart|insult|praise> <id>
2004-08-06 13:16:05 +02:00
Gets the record with id <id> from the type specified. <channel> is
2003-12-02 14:54:57 +01:00
only necessary if the message isn't sent in the channel itself.
"""
2003-12-02 14:54:57 +01:00
channel = privmsgs.getChannel(msg, args)
2004-08-06 13:16:05 +02:00
(type, id) = privmsgs.getArgs(args, required=2)
if not self._validType(irc, type):
return
2004-08-06 13:16:05 +02:00
id = self._validId(irc, id)
if id is None:
return
2004-08-06 13:16:05 +02:00
try:
2004-08-11 08:17:11 +02:00
x = self.db.get(channel, type, id)
irc.reply(x.text)
2004-08-06 13:16:05 +02:00
except KeyError:
irc.error('There is no %s with that id.' % type)
def info(self, irc, msg, args):
2004-08-06 13:16:05 +02:00
"""[<channel>] <lart|insult|praise> <id>
2004-08-06 13:16:05 +02:00
Gets the info for the record with id <id> from the type specified.
2003-12-02 14:54:57 +01:00
<channel> is only necessary if the message isn't sent in the channel
itself.
"""
2003-12-02 14:54:57 +01:00
channel = privmsgs.getChannel(msg, args)
2004-08-06 13:16:05 +02:00
(type, id) = privmsgs.getArgs(args, required=2)
if not self._validType(irc, type):
return
2004-08-06 13:16:05 +02:00
id = self._validId(irc, id)
if id is None:
return
2004-08-06 13:16:05 +02:00
try:
2004-08-11 08:17:11 +02:00
x = self.db.get(channel, type, id)
reply = '%s #%s: %r; Created by %s.' % (type, x.id, x.text,
self._getBy(x.by))
irc.reply(reply)
2004-08-06 13:16:05 +02:00
except KeyError:
irc.error('There is no %s with that id.' % type)
2004-08-06 13:16:05 +02:00
def _formatResponse(self, s, id, channel):
if self.registryValue('showIds', channel):
return '%s (#%s)' % (s, id)
else:
return s
2004-07-21 21:36:35 +02:00
2004-08-06 13:16:05 +02:00
_meRe = re.compile(r'\bme\b', re.I)
_myRe = re.compile(r'\bmy\b', re.I)
def _replaceFirstPerson(self, s, nick):
s = self._meRe.sub(nick, s)
s = self._myRe.sub('%s\'s' % nick, s)
return s
def insult(self, irc, msg, args):
2003-12-02 14:54:57 +01:00
"""[<channel>] <nick>
2003-12-02 14:54:57 +01:00
Insults <nick>. <channel> is only necessary if the message isn't
sent in the channel itself.
"""
2003-12-02 14:54:57 +01:00
channel = privmsgs.getChannel(msg, args)
nick = privmsgs.getArgs(args)
if not nick:
raise callbacks.ArgumentError
2004-08-11 08:17:11 +02:00
insult = self.db.random(channel, 'insult')
if insult is None:
irc.error('There are currently no available insults.')
else:
2004-08-06 13:16:05 +02:00
nick = self._replaceFirstPerson(nick, msg.nick)
2004-08-11 08:17:11 +02:00
s = '%s: %s' % (nick, insult.text.replace('$who', nick))
irc.reply(self._formatResponse(s, insult.id, channel),
2004-06-07 07:17:50 +02:00
prefixName=False)
2003-03-12 07:26:59 +01:00
def lart(self, irc, msg, args):
2003-12-02 14:54:57 +01:00
"""[<channel>] [<id>] <text> [for <reason>]
2003-03-12 07:26:59 +01:00
Uses a lart on <text> (giving the reason, if offered). Will use lart
2003-12-02 14:54:57 +01:00
number <id> from the database when <id> is given. <channel> is only
necessary if the message isn't sent in the channel itself.
2003-03-12 07:26:59 +01:00
"""
2003-12-02 14:54:57 +01:00
channel = privmsgs.getChannel(msg, args)
(id, nick) = privmsgs.getArgs(args, optional=1)
2004-08-06 13:16:05 +02:00
id = self._validId(irc, id, error=False)
if id is None:
nick = privmsgs.getArgs(args)
nick = nick.rstrip('.')
if not nick:
raise callbacks.ArgumentError
if ircutils.strEqual(nick, irc.nick):
2003-11-21 18:42:41 +01:00
nick = msg.nick
try:
2004-08-06 13:16:05 +02:00
(nick, reason) = itertools.imap(' '.join,
utils.itersplit('for'.__eq__, nick.split(), 1))
except ValueError:
reason = ''
if id:
2004-08-06 13:16:05 +02:00
try:
lart = self.db.get(channel, 'lart', id)
except KeyError:
irc.error('There is no such lart.')
return
else:
2004-08-11 08:17:11 +02:00
lart = self.db.random(channel, 'lart')
if lart is None:
irc.error('There are currently no available larts.')
2003-03-12 07:26:59 +01:00
else:
2004-08-06 13:16:05 +02:00
nick = self._replaceFirstPerson(nick, msg.nick)
reason = self._replaceFirstPerson(reason, msg.nick)
2004-08-11 08:17:11 +02:00
s = lart.text.replace('$who', nick)
if reason:
s = '%s for %s' % (s, reason)
s = s.rstrip('.')
2004-08-11 08:17:11 +02:00
irc.reply(self._formatResponse(s, lart.id, channel), action=True)
2003-03-12 07:26:59 +01:00
def praise(self, irc, msg, args):
2003-12-02 14:54:57 +01:00
"""[<channel>] [<id>] <text> [for <reason>]
Uses a praise on <text> (giving the reason, if offered). Will use
praise number <id> from the database when <id> is given.
2003-12-02 14:54:57 +01:00
<channel> is only necessary if the message isn't sent in the channel
itself.
"""
2003-12-02 14:54:57 +01:00
channel = privmsgs.getChannel(msg, args)
(id, nick) = privmsgs.getArgs(args, optional=1)
2004-08-06 13:16:05 +02:00
id = self._validId(irc, id, error=False)
if id is None:
nick = privmsgs.getArgs(args)
nick = nick.rstrip('.')
if not nick:
raise callbacks.ArgumentError
try:
2004-08-06 13:16:05 +02:00
(nick, reason) = itertools.imap(' '.join,
utils.itersplit('for'.__eq__, nick.split(), 1))
except ValueError:
reason = ''
if id:
2004-08-06 13:16:05 +02:00
try:
praise = self.db.get(channel, 'praise', id)
except KeyError:
irc.error('There is no such praise.')
return
else:
2004-08-11 08:17:11 +02:00
praise = self.db.random(channel, 'praise')
if praise is None:
irc.error('There are currently no available praises.')
else:
2004-08-06 13:16:05 +02:00
nick = self._replaceFirstPerson(nick, msg.nick)
reason = self._replaceFirstPerson(reason, msg.nick)
2004-08-11 08:17:11 +02:00
s = praise.text.replace('$who', nick)
if reason:
s = '%s for %s' % (s, reason)
s = s.rstrip('.')
2004-08-11 08:17:11 +02:00
irc.reply(self._formatResponse(s, praise.id, channel), action=True)
2003-03-12 07:26:59 +01:00
Class = FunDB
2003-08-20 18:26:23 +02:00
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78: