2003-03-12 07:26:59 +01:00
|
|
|
###
|
2004-08-23 15:14:06 +02: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.
|
|
|
|
###
|
|
|
|
|
2003-03-25 07:53:51 +01:00
|
|
|
"""
|
|
|
|
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
|
2003-08-22 08:08:55 +02:00
|
|
|
import sets
|
2004-08-06 13:16:05 +02:00
|
|
|
import random
|
|
|
|
import itertools
|
2004-01-21 03:19:26 +01:00
|
|
|
|
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
|
|
|
|
2004-09-24 22:05:34 +02:00
|
|
|
class FunDBRecord(dbi.Record):
|
2004-09-17 06:21:32 +02:00
|
|
|
__fields__ = [
|
|
|
|
'by',
|
|
|
|
'text',
|
|
|
|
]
|
|
|
|
|
2004-08-11 08:17:11 +02:00
|
|
|
class DbiFunDBDB(object):
|
|
|
|
class FunDBDB(dbi.DB):
|
2004-09-17 06:21:32 +02:00
|
|
|
Record = FunDBRecord
|
2004-08-23 21:23:01 +02:00
|
|
|
|
2004-09-17 06:21:32 +02:00
|
|
|
def __init__(self, filename):
|
2004-08-06 13:16:05 +02:00
|
|
|
self.dbs = ircutils.IrcDict()
|
2004-09-17 06:21:32 +02:00
|
|
|
self.filename = filename
|
2004-08-06 13:16:05 +02:00
|
|
|
|
|
|
|
def close(self):
|
2004-09-17 06:21:32 +02:00
|
|
|
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]:
|
2004-09-23 18:18:41 +02:00
|
|
|
filename = self.filename.replace('FunDB', type.capitalize())
|
2004-09-12 22:26:08 +02:00
|
|
|
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)
|
2004-09-18 17:36:52 +02:00
|
|
|
|
|
|
|
def search(self, channel, type, p):
|
|
|
|
db = self._getDb(channel, type)
|
|
|
|
return db.select(p)
|
2004-08-06 13:16:05 +02:00
|
|
|
|
2004-09-17 06:21:32 +02:00
|
|
|
FunDBDB = plugins.DB('FunDB',
|
|
|
|
{'flat': DbiFunDBDB})
|
2004-01-21 03:19:26 +01:00
|
|
|
|
|
|
|
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):
|
2004-09-18 17:36:52 +02:00
|
|
|
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()
|
2004-09-18 17:36:52 +02:00
|
|
|
self.__parent.die()
|
2004-08-06 13:16:05 +02:00
|
|
|
|
2004-08-23 21:23:01 +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):
|
2004-09-18 17:36:52 +02:00
|
|
|
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
|
|
|
|
2003-10-23 04:12:07 +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.
|
2004-04-13 06:08:15 +02:00
|
|
|
'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:
|
2003-08-22 23:34:33 +02:00
|
|
|
return
|
2004-08-06 13:16:05 +02:00
|
|
|
if not self._validType(irc, type):
|
|
|
|
return
|
|
|
|
if type == "lart" or type == "praise":
|
2003-08-22 07:36:53 +02:00
|
|
|
if '$who' not in s:
|
2004-01-09 00:03:48 +01:00
|
|
|
irc.error('There must be a $who in the lart/praise somewhere')
|
2003-08-22 07:36:53 +02:00
|
|
|
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
|
|
|
|
2003-10-23 04:12:07 +02: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
|
|
|
|
2003-08-22 07:36:53 +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):
|
2003-08-22 23:34:33 +02:00
|
|
|
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:
|
2003-08-22 07:36:53 +02:00
|
|
|
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
|
|
|
|
2004-09-18 17:36:52 +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))
|
|
|
|
|
2003-10-23 04:12:07 +02:00
|
|
|
def change(self, irc, msg, args):
|
2004-08-06 13:16:05 +02:00
|
|
|
"""[<channel>] <lart|insult|praise> <id> <regexp>
|
2003-09-16 18:49:17 +02:00
|
|
|
|
|
|
|
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-09-16 18:49: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, regexp) = privmsgs.getArgs(args, required=3)
|
|
|
|
if not self._validType(irc, type):
|
2003-09-16 18:49:17 +02:00
|
|
|
return
|
2004-08-06 13:16:05 +02:00
|
|
|
id = self._validId(irc, id)
|
|
|
|
if id is None:
|
2003-09-16 18:49:17 +02:00
|
|
|
return
|
2004-08-06 13:16:05 +02:00
|
|
|
if not self._isRegistered(irc, msg):
|
2003-09-16 18:49:17 +02:00
|
|
|
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
|
2003-09-16 18:49:17 +02:00
|
|
|
except re.error, e:
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.error(utils.exnToString(e))
|
2003-09-16 18:49:17 +02:00
|
|
|
return
|
2004-08-06 13:16:05 +02:00
|
|
|
try:
|
|
|
|
self.db.change(channel, type, id, replacer)
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.replySuccess()
|
2004-08-06 13:16:05 +02:00
|
|
|
except KeyError:
|
|
|
|
irc.error('There is no %s with that id.' % type)
|
2003-09-16 18:49:17 +02:00
|
|
|
|
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>
|
2003-04-05 12:24:40 +02:00
|
|
|
|
2003-08-22 07:36:53 +02:00
|
|
|
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):
|
2003-08-22 07:36:53 +02:00
|
|
|
return
|
2004-08-06 13:16:05 +02:00
|
|
|
total = self.db.size(channel, type)
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.reply('There %s currently %s in my database.' %
|
2004-08-06 13:16:05 +02:00
|
|
|
(utils.be(total), utils.nItems(type, total)))
|
2003-08-22 07:36:53 +02:00
|
|
|
|
2003-10-23 04:12:07 +02:00
|
|
|
def get(self, irc, msg, args):
|
2004-08-06 13:16:05 +02:00
|
|
|
"""[<channel>] <lart|insult|praise> <id>
|
2003-08-22 07:36:53 +02:00
|
|
|
|
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-08-22 07:36:53 +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._validType(irc, type):
|
2003-08-22 07:36:53 +02:00
|
|
|
return
|
2004-08-06 13:16:05 +02:00
|
|
|
id = self._validId(irc, id)
|
|
|
|
if id is None:
|
2003-08-22 07:36:53 +02:00
|
|
|
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)
|
2003-08-22 23:34:33 +02:00
|
|
|
|
2003-10-23 04:12:07 +02:00
|
|
|
def info(self, irc, msg, args):
|
2004-08-06 13:16:05 +02:00
|
|
|
"""[<channel>] <lart|insult|praise> <id>
|
2003-08-22 23:34:33 +02:00
|
|
|
|
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-08-22 23:34:33 +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._validType(irc, type):
|
2003-08-22 23:34:33 +02:00
|
|
|
return
|
2004-08-06 13:16:05 +02:00
|
|
|
id = self._validId(irc, id)
|
|
|
|
if id is None:
|
2003-08-22 23:34:33 +02:00
|
|
|
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)
|
2004-08-23 21:23:01 +02:00
|
|
|
reply = '%s #%s: %r; Created by %s.' % (type, x.id, x.text,
|
|
|
|
self._getBy(x.by))
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.reply(reply)
|
2004-08-06 13:16:05 +02:00
|
|
|
except KeyError:
|
|
|
|
irc.error('There is no %s with that id.' % type)
|
2003-04-05 12:24:40 +02:00
|
|
|
|
2004-08-06 13:16:05 +02:00
|
|
|
def _formatResponse(self, s, id, channel):
|
|
|
|
if self.registryValue('showIds', channel):
|
2003-11-25 12:43:09 +01:00
|
|
|
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
|
|
|
|
|
2003-11-25 12:43:09 +01:00
|
|
|
def insult(self, irc, msg, args):
|
2003-12-02 14:54:57 +01:00
|
|
|
"""[<channel>] <nick>
|
2003-11-25 12:43:09 +01:00
|
|
|
|
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-11-25 12:43:09 +01:00
|
|
|
"""
|
2003-12-02 14:54:57 +01:00
|
|
|
channel = privmsgs.getChannel(msg, args)
|
2003-11-25 12:43:09 +01:00
|
|
|
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:
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.error('There are currently no available insults.')
|
2003-11-25 12:43:09 +01:00
|
|
|
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-11-25 12:43:09 +01:00
|
|
|
|
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
|
|
|
|
2003-10-21 15:23:26 +02: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)
|
2003-10-21 17:36:39 +02:00
|
|
|
(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)
|
2004-01-01 20:08:08 +01:00
|
|
|
nick = nick.rstrip('.')
|
2003-10-21 17:36:39 +02:00
|
|
|
if not nick:
|
|
|
|
raise callbacks.ArgumentError
|
2004-08-31 19:23:33 +02:00
|
|
|
if ircutils.strEqual(nick, irc.nick):
|
2003-11-21 18:42:41 +01:00
|
|
|
nick = msg.nick
|
2003-09-01 08:11:02 +02:00
|
|
|
try:
|
2004-08-06 13:16:05 +02:00
|
|
|
(nick, reason) = itertools.imap(' '.join,
|
2003-10-21 17:36:39 +02:00
|
|
|
utils.itersplit('for'.__eq__, nick.split(), 1))
|
2003-09-01 08:11:02 +02:00
|
|
|
except ValueError:
|
2003-09-18 09:26:21 +02:00
|
|
|
reason = ''
|
2003-10-21 17:36:39 +02:00
|
|
|
if id:
|
2004-08-06 13:16:05 +02:00
|
|
|
try:
|
|
|
|
lart = self.db.get(channel, 'lart', id)
|
|
|
|
except KeyError:
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.error('There is no such lart.')
|
2003-10-21 17:36:39 +02:00
|
|
|
return
|
2003-10-21 15:23:26 +02:00
|
|
|
else:
|
2004-08-11 08:17:11 +02:00
|
|
|
lart = self.db.random(channel, 'lart')
|
|
|
|
if lart is None:
|
2004-01-08 04:12:14 +01:00
|
|
|
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)
|
2003-11-28 20:04:24 +01:00
|
|
|
if reason:
|
2003-11-25 12:43:09 +01:00
|
|
|
s = '%s for %s' % (s, reason)
|
2003-11-28 20:04:24 +01:00
|
|
|
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
|
|
|
|
2003-08-22 23:34:33 +02:00
|
|
|
def praise(self, irc, msg, args):
|
2003-12-02 14:54:57 +01:00
|
|
|
"""[<channel>] [<id>] <text> [for <reason>]
|
2003-08-22 23:34:33 +02:00
|
|
|
|
2003-10-21 15:23:26 +02:00
|
|
|
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-08-22 23:34:33 +02:00
|
|
|
"""
|
2003-12-02 14:54:57 +01:00
|
|
|
channel = privmsgs.getChannel(msg, args)
|
2003-10-21 17:36:39 +02:00
|
|
|
(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)
|
2004-01-01 20:08:08 +01:00
|
|
|
nick = nick.rstrip('.')
|
2003-10-21 17:36:39 +02:00
|
|
|
if not nick:
|
|
|
|
raise callbacks.ArgumentError
|
2003-09-01 08:11:02 +02:00
|
|
|
try:
|
2004-08-06 13:16:05 +02:00
|
|
|
(nick, reason) = itertools.imap(' '.join,
|
2003-10-21 17:36:39 +02:00
|
|
|
utils.itersplit('for'.__eq__, nick.split(), 1))
|
2003-09-01 08:11:02 +02:00
|
|
|
except ValueError:
|
2003-09-18 09:26:21 +02:00
|
|
|
reason = ''
|
2003-10-21 17:36:39 +02:00
|
|
|
if id:
|
2004-08-06 13:16:05 +02:00
|
|
|
try:
|
|
|
|
praise = self.db.get(channel, 'praise', id)
|
|
|
|
except KeyError:
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.error('There is no such praise.')
|
2003-10-21 17:36:39 +02:00
|
|
|
return
|
2003-10-21 15:23:26 +02:00
|
|
|
else:
|
2004-08-11 08:17:11 +02:00
|
|
|
praise = self.db.random(channel, 'praise')
|
|
|
|
if praise is None:
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.error('There are currently no available praises.')
|
2003-08-22 23:34:33 +02: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 = praise.text.replace('$who', nick)
|
2003-11-28 20:04:24 +01:00
|
|
|
if reason:
|
2003-11-25 12:43:09 +01:00
|
|
|
s = '%s for %s' % (s, reason)
|
2003-11-28 20:04:24 +01:00
|
|
|
s = s.rstrip('.')
|
2004-08-11 08:17:11 +02:00
|
|
|
irc.reply(self._formatResponse(s, praise.id, channel), action=True)
|
2003-08-22 23:34:33 +02:00
|
|
|
|
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:
|