Got most of the dunno stuff in place.

This commit is contained in:
Daniel DiPaolo 2003-10-21 15:44:17 +00:00
parent a67748ec5f
commit d959f84b73
2 changed files with 81 additions and 5 deletions

View File

@ -205,7 +205,8 @@ class MoobotFactoids(callbacks.PrivmsgCommandAndRegexp):
cursor = self.db.cursor()
cursor.execute("""SELECT fact FROM factoids WHERE key = %s""", key)
if cursor.rowcount == 0:
irc.reply(msg, "Would reply with a dunno here")
text = self._getDunno(msg.nick)
irc.reply(msg, text)
else:
fact = cursor.fetchone()[0]
# Update the requested count/requested by for this key
@ -222,6 +223,20 @@ class MoobotFactoids(callbacks.PrivmsgCommandAndRegexp):
else:
irc.error(msg, "Spurious type from parseFactoid.")
def _getDunno(self, nick):
"""Retrieves a "dunno" from the database."""
cursor = self.db.cursor()
cursor.execute("""SELECT id, dunno
FROM dunnos
ORDER BY random()
LIMIT 1""")
if cursor.rowcount == 0:
return "No dunno's available, add some with dunnoadd."
(id, dunno) = cursor.fetchone()
dunno = dunno.replace('$who', nick)
dunno += " (#%d)" % id
return dunno
def addFactoid(self, irc, msg, match):
r"^(?!no\s+)(.+)\s+is\s+(?!also)(.+)"
# Must be registered!
@ -553,6 +568,55 @@ class MoobotFactoids(callbacks.PrivmsgCommandAndRegexp):
self.db.commit()
irc.reply(msg, conf.replySuccess)
def dunnoadd(self, irc, msg, args):
"""<text>
Adds <text> as a "dunno" to be used as a random response when no
command or factoid key matches.
"""
# Must be registered to use this
try:
id = ircdb.users.getUserId(msg.prefix)
except KeyError:
irc.error(msg, conf.replyNotRegistered)
return
text = privmsgs.getArgs(args, needed=1)
cursor = self.db.cursor()
cursor.execute("""INSERT INTO dunnos
VALUES(NULL, %s, %s, %s)""",
id, int(time.time()), text)
self.db.commit()
irc.reply(msg, conf.replySuccess)
def dunnoremove(self, irc, msg, args):
"""<id>
Removes dunno with the given <id>.
"""
# Must be registered to use this
try:
user_id = ircdb.users.getUserId(msg.prefix)
except KeyError:
irc.error(msg, conf.replyNotRegistered)
return
dunno_id = privmsgs.getArgs(args, needed=1)
cursor = self.db.cursor()
cursor.execute("""SELECT added_by, dunno
FROM dunnos
WHERE id = %s""" % dunno_id)
if cursor.rowcount == 0:
irc.error(msg, 'No dunno with id: %d' % dunno_id)
return
(added_by, dunno) = cursor.fetchone()
if not (ircdb.checkCapability(user_id, 'admin') or \
added_by == user_id):
irc.error(msg, 'Only admins and the dunno creator may delete a '
'dunno.')
return
cursor.execute("""DELETE FROM dunnos WHERE id = %s""" % dunno_id)
self.db.commit()
irc.reply(msg, conf.replySuccess)
Class = MoobotFactoids
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78:

View File

@ -44,6 +44,8 @@ if sqlite is not None:
# Create a valid user to use
self.prefix = 'foo!bar@baz'
self.assertNotError('register tester moo')
self.assertNotError('dunnoadd not moo') # don't change to "moo"
# or testDelete will fail
def testLiteral(self):
self.assertError('literal moo') # no factoids yet
@ -183,9 +185,19 @@ if sqlite is not None:
self.assertNotError('lock moo')
self.assertError('no moo is qux')
# class DunnoTestCase(PluginTestCase, PluginDocumentation):
# plugins = ('Misc', 'MoobotFactoids', 'User')
# def testDunno(self):
# self.assertNotError('apfasdfjoia') # Should say a dunno, no error
class DunnoTestCase(PluginTestCase, PluginDocumentation):
plugins = ('MiscCommands', 'MoobotFactoids', 'UserCommands')
def setUp(self):
PluginTestCase.setUp(self)
self.prefix = 'foo!bar@baz'
self.assertNotError('register tester moo')
def testDunnoAdd(self):
self.assertNotError('dunnoadd moo')
self.assertResponse('asdfagagfosdfk', 'moo (#1)')
def testDunnoRemove(self):
self.assertNotError('dunnoadd moo')
self.assertNotError('dunnoremove 1')
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78: