--- lib/python2.5/site-packages/supybot/plugins/QuoteGrabs/plugin.py.orig 2007-01-15 19:06:40.000000000 +0200 +++ lib/python2.5/site-packages/supybot/plugins/QuoteGrabs/plugin.py 2007-01-25 19:58:01.000000000 +0200 @@ -108,17 +108,41 @@ return QuoteGrabsRecord(id, by=by, text=quote, hostmask=hostmask, at=at, grabber=grabber) - def random(self, channel, nick): + def stat(self, channel, nick): db = self._getDb(channel) cursor = db.cursor() + title = channel if nick: - cursor.execute("""SELECT quote FROM quotegrabs - WHERE nickeq(nick, %s) - ORDER BY random() LIMIT 1""", + title = nick + cursor.execute("""SELECT COUNT(*) FROM quotegrabs + WHERE nickeq(nick, %s)""", nick) else: - cursor.execute("""SELECT quote FROM quotegrabs - ORDER BY random() LIMIT 1""") + cursor.execute("""SELECT COUNT(*) FROM quotegrabs""") + if cursor.rowcount == 0: + raise dbi.NoRecordError + return title + ': ' + str(cursor.fetchone()[0]) + ' quotes.' + + def random(self, channel, nick, text): + db = self._getDb(channel) + query = 'SELECT quote FROM quotegrabs' + args = [] + if nick: + query += ' WHERE nickeq(nick, %s)' + args.append(nick) + if text: + if nick: + query += ' AND ' + else: + query += ' WHERE ' + query += 'quote LIKE %s' + args.append('%' + text + '%') + query += ' ORDER BY random() LIMIT 1' + cursor = db.cursor() + if nick: + cursor.execute(query, args) + else: + cursor.execute(query, args) if cursor.rowcount == 0: raise dbi.NoRecordError return cursor.fetchone()[0] @@ -280,22 +304,39 @@ Raise=True) list = wrap(list, ['channeldb', 'nick']) - def random(self, irc, msg, args, channel, nick): - """[] [] + def random(self, irc, msg, args, channel, nick, text): + """[] [ []] Returns a randomly grabbed quote, optionally choosing only from those - quotes grabbed for . is only necessary if the message - isn't sent in the channel itself. + quotes grabbed for (optionally filtering for ). + is only necessary if the message isn't sent in the channel itself. """ try: - irc.reply(self.db.random(channel, nick)) + irc.reply(self.db.random(channel, nick, text)) except dbi.NoRecordError: if nick: irc.error('Couldn\'t get a random quote for that nick.') else: irc.error('Couldn\'t get a random quote. Are there any ' 'grabbed quotes in the database?') - random = wrap(random, ['channeldb', additional('nick')]) + random = wrap(random, ['channeldb', additional('nick'), additional('text')]) + + def stat(self, irc, msg, args, channel, nick): + """[] [] + + Returns the number of stored quotes, optionally filtering only from those + quotes grabbed for . is only necessary if the message + isn't sent in the channel itself. + """ + try: + irc.reply(self.db.stat(channel, nick)) + except dbi.NoRecordError: + if nick: + irc.error('Couldn\'t get quotes stats for that nick.') + else: + irc.error('Couldn\'t get quotes stats. Are there any ' + 'grabbed quotes in the database?') + stat = wrap(stat, ['channeldb', additional('nick')]) def get(self, irc, msg, args, channel, id): """[]