Added ability to configure showing of ids on or off.

This commit is contained in:
Jeremy Fincher 2003-11-25 11:43:09 +00:00
parent 4770ef1b3f
commit 5564e43239
3 changed files with 139 additions and 120 deletions

View File

@ -1,3 +1,6 @@
* Added the ability to turn on/off the showing of ids in FunDB
excuse/insult/praise/lart.
* Added the to= keyword argument to the various reply functions to
you can specify a target to send the message to.

View File

@ -121,98 +121,31 @@ def addWord(db, word, commit=False):
db.commit()
class FunDB(callbacks.Privmsg):
class FunDB(callbacks.Privmsg, plugins.Configurable):
"""
Contains the 'fun' commands that require a database. Currently includes
database-backed commands for crossword puzzle solving, anagram searching,
larting, praising, excusing, and insulting.
"""
configurables = plugins.ConfigurableDictionary(
[('show-ids', plugins.ConfigurableBoolType, False,
"""Determines whether the bot will show the id of an
excuse/insult/praise/lart.""")]
)
_tables = sets.Set(['lart', 'insult', 'excuse', 'praise'])
def __init__(self):
callbacks.Privmsg.__init__(self)
plugins.Configurable.__init__(self)
self.dbHandler = FunDBDB(os.path.join(conf.dataDir, 'FunDB'))
def die(self):
callbacks.Privmsg.die(self)
plugins.Configurable.die(self)
db = self.dbHandler.getDb()
db.commit()
db.close()
del db
def insult(self, irc, msg, args):
"""<nick>
Insults <nick>.
"""
nick = privmsgs.getArgs(args)
if not nick:
raise callbacks.ArgumentError
db = self.dbHandler.getDb()
cursor = db.cursor()
cursor.execute("""SELECT id, insult FROM insults
WHERE insult NOT NULL
ORDER BY random()
LIMIT 1""")
if cursor.rowcount == 0:
irc.error(msg, 'There are currently no available insults.')
else:
(id, insult) = cursor.fetchone()
nick = nick.strip()
nick = re.sub(r'\bme\b', msg.nick, nick)
nick = re.sub(r'\bmy\b', '%s\'s' % msg.nick, nick)
insultee = nick
insult = insult.replace("$who", insultee)
s = '%s: %s (#%s)' % (insultee, insult, id)
irc.reply(msg, s, prefixName=False)
def crossword(self, irc, msg, args):
"""<word>
Gives the possible crossword completions for <word>; use underscores
('_') to denote blank spaces.
"""
word = privmsgs.getArgs(args).lower()
db = self.dbHandler.getDb()
cursor = db.cursor()
if '%' in word:
irc.error(msg, '"%" isn\'t allowed in the word.')
return
cursor.execute("""SELECT word FROM words
WHERE word LIKE %s
ORDER BY word""", word)
words = [t[0] for t in cursor.fetchall()]
irc.reply(msg, ', '.join(words))
def excuse(self, irc, msg, args):
"""[<id>]
Gives you a standard, random BOFH excuse or the excuse with the given
<id>.
"""
id = privmsgs.getArgs(args, required=0, optional=1)
db = self.dbHandler.getDb()
cursor = db.cursor()
if id:
try:
id = int(id)
except ValueError:
irc.error(msg, 'The <id> argument must be an integer.')
return
cursor.execute("""SELECT id, excuse FROM excuses WHERE id=%s""",
id)
if cursor.rowcount == 0:
irc.error(msg, 'There is no such excuse.')
return
else:
cursor.execute("""SELECT id, excuse FROM excuses
WHERE excuse NOTNULL
ORDER BY random()
LIMIT 1""")
if cursor.rowcount == 0:
irc.error(msg, 'There are currently no available excuses.')
else:
(id, excuse) = cursor.fetchone()
irc.reply(msg, '%s (#%s)' % (excuse, id))
def add(self, irc, msg, args):
"""<lart|excuse|insult|praise> <text>
@ -398,6 +331,66 @@ class FunDB(callbacks.Privmsg):
reply = '%s #%s: Created by %s.' % (table, id, add)
irc.reply(msg, reply)
def _formatResponse(self, s, id):
if self.configurables.get('show-ids'):
return '%s (#%s)' % (s, id)
else:
return s
def insult(self, irc, msg, args):
"""<nick>
Insults <nick>.
"""
nick = privmsgs.getArgs(args)
if not nick:
raise callbacks.ArgumentError
db = self.dbHandler.getDb()
cursor = db.cursor()
cursor.execute("""SELECT id, insult FROM insults
WHERE insult NOT NULL
ORDER BY random()
LIMIT 1""")
if cursor.rowcount == 0:
irc.error(msg, 'There are currently no available insults.')
else:
(id, insult) = cursor.fetchone()
nick = re.sub(r'\bme\b', msg.nick, nick)
nick = re.sub(r'\bmy\b', '%s\'s' % msg.nick, nick)
insult = insult.replace('$who', nick)
irc.reply(msg, self._formatResponse(insult, id), to=nick)
def excuse(self, irc, msg, args):
"""[<id>]
Gives you a standard, random BOFH excuse or the excuse with the given
<id>.
"""
id = privmsgs.getArgs(args, required=0, optional=1)
db = self.dbHandler.getDb()
cursor = db.cursor()
if id:
try:
id = int(id)
except ValueError:
irc.error(msg, 'The <id> argument must be an integer.')
return
cursor.execute("""SELECT id, excuse FROM excuses WHERE id=%s""",
id)
if cursor.rowcount == 0:
irc.error(msg, 'There is no such excuse.')
return
else:
cursor.execute("""SELECT id, excuse FROM excuses
WHERE excuse NOTNULL
ORDER BY random()
LIMIT 1""")
if cursor.rowcount == 0:
irc.error(msg, 'There are currently no available excuses.')
else:
(id, excuse) = cursor.fetchone()
irc.reply(msg, self._formatResponse(excuse, id))
def lart(self, irc, msg, args):
"""[<id>] <text> [for <reason>]
@ -443,12 +436,10 @@ class FunDB(callbacks.Privmsg):
nick = re.sub(r'\bmy\b', '%s\'s' % msg.nick, nick)
reason = re.sub(r'\bmy\b', '%s\'s' % msg.nick, reason)
lartee = nick
lart = lart.replace("$who", lartee)
s = lart.replace('$who', lartee)
if len(reason) > 0:
s = '%s for %s (#%s)' % (lart, reason, id)
else:
s = '%s (#%s)' % (lart, id)
irc.reply(msg, s, action=True)
s = '%s for %s' % (s, reason)
irc.reply(msg, self._formatResponse(s, id), action=True)
def praise(self, irc, msg, args):
"""[<id>] <text> [for <reason>]
@ -493,12 +484,10 @@ class FunDB(callbacks.Privmsg):
nick = re.sub(r'\bmy\b', '%s\'s' % msg.nick, nick)
reason = re.sub(r'\bmy\b', '%s\'s' % msg.nick, reason)
praisee = nick
praise = praise.replace("$who", praisee)
s = praise.replace('$who', praisee)
if len(reason) > 0:
s = '%s for %s (#%s)' % (praise, reason, id)
else:
s = '%s (#%s)' % (praise, id)
irc.reply(msg, s, action=True)
s = '%s for %s' % (s, reason)
irc.reply(msg, self._formatResponse(s, id), action=True)
def addword(self, irc, msg, args):
"""<word>
@ -512,6 +501,24 @@ class FunDB(callbacks.Privmsg):
addWord(self.dbHandler.getDb(), word, commit=True)
irc.reply(msg, conf.replySuccess)
def crossword(self, irc, msg, args):
"""<word>
Gives the possible crossword completions for <word>; use underscores
('_') to denote blank spaces.
"""
word = privmsgs.getArgs(args).lower()
db = self.dbHandler.getDb()
cursor = db.cursor()
if '%' in word:
irc.error(msg, '"%" isn\'t allowed in the word.')
return
cursor.execute("""SELECT word FROM words
WHERE word LIKE %s
ORDER BY word""", word)
words = [t[0] for t in cursor.fetchall()]
irc.reply(msg, utils.commaAndify(words))
def anagram(self, irc, msg, args):
"""<word>
@ -530,7 +537,7 @@ class FunDB(callbacks.Privmsg):
except ValueError:
pass
if words:
irc.reply(msg, ', '.join(words))
irc.reply(msg, utils.commaAndify(words))
else:
irc.reply(msg, 'That word has no anagrams that I know of.')
@ -562,22 +569,22 @@ if __name__ == '__main__':
addWord(db, line)
elif category == 'larts':
if '$who' in line:
cursor.execute("""INSERT INTO larts VALUES (NULL, %s,
%s)""", line, added_by)
cursor.execute("""INSERT INTO larts VALUES (NULL, %s, %s)""",
line, added_by)
else:
print 'Invalid lart: %s' % line
elif category == 'praises':
if '$who' in line:
cursor.execute("""INSERT INTO praises VALUES (NULL, %s,
%s)""", line, added_by)
cursor.execute("""INSERT INTO praises VALUES (NULL, %s, %s)""",
line, added_by)
else:
print 'Invalid praise: %s' % line
elif category == 'insults':
cursor.execute("""INSERT INTO insults VALUES (NULL, %s, %s
)""", line, added_by)
cursor.execute("""INSERT INTO insults VALUES (NULL, %s, %s)""",
line, added_by)
elif category == 'excuses':
cursor.execute("""INSERT INTO excuses VALUES (NULL, %s, %s
)""", line, added_by)
cursor.execute("""INSERT INTO excuses VALUES (NULL, %s, %s )""",
line, added_by)
db.commit()
db.close()

View File

@ -31,6 +31,8 @@
from test import *
import ircdb
try:
import sqlite
except ImportError:
@ -39,11 +41,12 @@ except ImportError:
if sqlite is not None:
class TestFunDB(PluginTestCase, PluginDocumentation):
plugins = ('FunDB','User','Utilities')
def setUp(self):
PluginTestCase.setUp(self)
self.prefix = 't3st!bar@foo.com'
self.assertNotError('register t3st moo')
ircdb.users.getUser('t3st').addCapability('admin')
self.assertNotError('fundb config show-ids on')
def testAdd(self):
self.assertError('add l4rt foo')
@ -82,29 +85,29 @@ if sqlite is not None:
self.assertNotError('add lart jabs $who')
self.assertNotError('add praise pets $who')
self.assertNotError('add insult foo')
self.assertRegexp('lart me', r'jabs t3st \(#1\)')
self.assertRegexp('praise me', r'pets t3st \(#1\)')
self.assertResponse('insult me', 't3st: foo (#1)')
self.assertRegexp('lart whamme', r'jabs whamme \(#1\)')
self.assertRegexp('praise whamme', r'pets whamme \(#1\)')
self.assertResponse('insult whamme', 'whamme: foo (#1)')
self.assertRegexp('lart my knee', r'jabs t3st\'s knee \(#1\)')
self.assertRegexp('praise my knee', r'pets t3st\'s knee \(#1\)')
self.assertResponse('insult my knee', 't3st\'s knee: foo (#1)')
self.assertRegexp('lart sammy the snake',
r'jabs sammy the snake \(#1\)')
self.assertRegexp('praise sammy the snake',
r'pets sammy the snake \(#1\)')
self.assertResponse('insult sammy the snake',
'sammy the snake: foo (#1)')
self.assertRegexp('lart me for my',
r'jabs t3st for t3st\'s \(#1\)')
self.assertRegexp('praise me for my',
r'pets t3st for t3st\'s \(#1\)')
self.assertRegexp('lart me and %s' % self.irc.nick,
r'jabs t3st and %s \(#1\)' % self.irc.nick)
self.assertRegexp('praise me and %s' % self.irc.nick,
r'pets t3st and %s \(#1\)' % self.irc.nick)
self.assertAction('lart me', 'jabs t3st (#1)')
self.assertAction('praise me', 'pets t3st (#1)')
#self.assertResponse('insult me', 't3st: foo (#1)')
self.assertAction('lart whamme', 'jabs whamme (#1)')
self.assertAction('praise whamme', 'pets whamme (#1)')
#self.assertResponse('insult whamme', 'whamme: foo (#1)')
self.assertAction('lart my knee', 'jabs t3st\'s knee (#1)')
self.assertAction('praise my knee', 'pets t3st\'s knee (#1)')
#self.assertResponse('insult my knee', 't3st\'s knee: foo (#1)')
self.assertAction('lart sammy the snake',
'jabs sammy the snake (#1)')
self.assertAction('praise sammy the snake',
'pets sammy the snake (#1)')
#self.assertResponse('insult sammy the snake',
# 'sammy the snake: foo (#1)')
self.assertAction('lart me for my',
'jabs t3st for t3st\'s (#1)')
self.assertAction('praise me for my',
'pets t3st for t3st\'s (#1)')
self.assertAction('lart me and %s' % self.irc.nick,
'jabs t3st and %s (#1)' % self.irc.nick)
self.assertAction('praise me and %s' % self.irc.nick,
'pets t3st and %s (#1)' % self.irc.nick)
self.assertNotError('remove lart 1')
self.assertNotError('remove praise 1')
self.assertNotError('remove insult 1')
@ -126,7 +129,7 @@ if sqlite is not None:
def testInsult(self):
self.assertNotError('add insult Fatty McFatty')
self.assertResponse('insult jemfinch',
'jemfinch: Fatty McFatty (#1)')
'Fatty McFatty (#1)')
self.assertRegexp('num insult', r'currently 1')
self.assertNotError('remove insult 1')
self.assertRegexp('num insult', 'currently 0')
@ -185,5 +188,11 @@ if sqlite is not None:
'\x01ACTION teaches jemfinch python (#1)\x01')
self.assertNotError('remove praise 1')
def testConfig(self):
self.assertNotError('add praise teaches $who perl')
self.assertRegexp('praise jemfinch', r'\(#1\)')
self.assertNotError('fundb config show-ids off')
self.assertNotRegexp('praise jemfinch', r'\(#1\)')
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78: