QuoteGrabs plugin: Add an `ungrab' command.

Also add a missing error check in the `list' db method.

Signed-off-by: James Vega <jamessan@users.sourceforge.net>
(cherry picked from commit e922918564)
This commit is contained in:
Štěpán Němec 2009-08-19 16:00:57 +02:00 committed by James Vega
parent d7d5ccea50
commit bc1c159d27
2 changed files with 72 additions and 1 deletions

View File

@ -128,6 +128,8 @@ class SqliteQuoteGrabsDB(object):
cursor.execute("""SELECT id, quote FROM quotegrabs cursor.execute("""SELECT id, quote FROM quotegrabs
WHERE nickeq(nick, %s) WHERE nickeq(nick, %s)
ORDER BY id DESC""", nick) ORDER BY id DESC""", nick)
if cursor.rowcount == 0:
raise dbi.NoRecordError
return [QuoteGrabsRecord(id, text=quote) return [QuoteGrabsRecord(id, text=quote)
for (id, quote) in cursor.fetchall()] for (id, quote) in cursor.fetchall()]
@ -167,6 +169,27 @@ class SqliteQuoteGrabsDB(object):
msg.nick, msg.prefix, by, int(time.time()), text) msg.nick, msg.prefix, by, int(time.time()), text)
db.commit() db.commit()
def remove(self, channel, grab=None):
db = self._getDb(channel)
cursor = db.cursor()
if grab is not None:
# the testing if there actually *is* the to-be-deleted record is
# strictly unnecessary -- the DELETE operation would "succeed"
# anyway, but it's silly to just keep saying 'OK' no matter what,
# so...
cursor.execute("""SELECT * FROM quotegrabs WHERE id = %s""", grab)
if cursor.rowcount == 0:
raise dbi.NoRecordError
cursor.execute("""DELETE FROM quotegrabs WHERE id = %s""", grab)
else:
cursor.execute("""SELECT * FROM quotegrabs WHERE id = (SELECT MAX(id)
FROM quotegrabs)""")
if cursor.rowcount == 0:
raise dbi.NoRecordError
cursor.execute("""DELETE FROM quotegrabs WHERE id = (SELECT MAX(id)
FROM quotegrabs)""")
db.commit()
def search(self, channel, text): def search(self, channel, text):
db = self._getDb(channel) db = self._getDb(channel)
cursor = db.cursor() cursor = db.cursor()
@ -241,6 +264,23 @@ class QuoteGrabs(callbacks.Plugin):
irc.error('I couldn\'t find a proper message to grab.') irc.error('I couldn\'t find a proper message to grab.')
grab = wrap(grab, ['channeldb', 'nick']) grab = wrap(grab, ['channeldb', 'nick'])
def ungrab(self, irc, msg, args, channel, grab):
"""[<channel>] <number>
Removes the grab <number> (the last by default) on <channel>.
<channel> is only necessary if the message isn't sent in the channel
itself.
"""
try:
self.db.remove(channel, grab)
irc.replySuccess()
except dbi.NoRecordError:
if grab is None:
irc.error('Nothing to ungrab.')
else:
irc.error('Invalid grab number.')
ungrab = wrap(ungrab, ['channeldb', optional('id')])
def quote(self, irc, msg, args, channel, nick): def quote(self, irc, msg, args, channel, nick):
"""[<channel>] <nick> """[<channel>] <nick>

View File

@ -51,6 +51,38 @@ class QuoteGrabsTestCase(ChannelPluginTestCase):
self.assertNotError('grab foo') self.assertNotError('grab foo')
self.assertResponse('quote foo', '* foo moos') self.assertResponse('quote foo', '* foo moos')
def testUngrab(self):
testPrefix = 'foo!bar@baz'
# nothing yet
self.assertError('ungrab')
self.assertError('ungrab 2')
self.irc.feedMsg(ircmsgs.privmsg(self.channel, 'something',
prefix=testPrefix))
# still not grabbed
self.assertError('ungrab')
self.assertError('ungrab 3')
# grab and ungrab a quote
self.assertNotError('grab foo')
self.assertNotError('ungrab')
self.assertNotError('grab foo')
# this is not there...
self.assertError('ungrab 8883')
# ...unlike this...
self.assertNotError('ungrab 1')
# ...but not now anymore :-D
self.assertError('ungrab')
# grab two quotes and ungrab them by id
self.irc.feedMsg(ircmsgs.privmsg(self.channel, 'something',
prefix=testPrefix))
self.assertNotError('grab foo')
self.irc.feedMsg(ircmsgs.privmsg(self.channel, 'another',
prefix=testPrefix))
self.assertNotError('grab foo')
self.assertNotError('ungrab 1')
self.assertNotError('ungrab 2')
self.assertError('ungrab')
def testList(self): def testList(self):
testPrefix = 'foo!bar@baz' testPrefix = 'foo!bar@baz'
self.irc.feedMsg(ircmsgs.privmsg(self.channel, 'testList', self.irc.feedMsg(ircmsgs.privmsg(self.channel, 'testList',
@ -111,7 +143,6 @@ class QuoteGrabsTestCase(ChannelPluginTestCase):
self.assertNotError('grab foo') self.assertNotError('grab foo')
self.assertNotError('quotegrabs search test') self.assertNotError('quotegrabs search test')
class QuoteGrabsNonChannelTestCase(QuoteGrabsTestCase): class QuoteGrabsNonChannelTestCase(QuoteGrabsTestCase):
config = { 'databases.plugins.channelSpecific' : False } config = { 'databases.plugins.channelSpecific' : False }