diff --git a/plugins/Factoids.py b/plugins/Factoids.py index cadfa07c9..f10d51df5 100644 --- a/plugins/Factoids.py +++ b/plugins/Factoids.py @@ -163,7 +163,19 @@ class Factoids(ChannelDBHandler, callbacks.Privmsg): necessary if the message isn't sent in the channel itself. """ channel = privmsgs.getChannel(msg, args) + if args[-1].isdigit(): + number = args.pop() + else: + number = '' key = privmsgs.getArgs(args) + if number: + try: + number = int(number) + except ValueError: + irc.error(msg, '%s is not a valid number.' % number) + return + else: + number = 0 db = self.getDb(channel) cursor = db.cursor() cursor.execute("""SELECT factoids.fact FROM factoids, keys WHERE @@ -173,12 +185,19 @@ class Factoids(ChannelDBHandler, callbacks.Privmsg): if cursor.rowcount == 0: irc.error(msg, 'No factoid matches that key.') else: - factoids = [] - counter = 0 - for result in cursor.fetchall(): - factoids.append('(#%s) %s' % (counter, result[0])) - counter += 1 - irc.reply(msg, '%r could be %s' % (key, ', or '.join(factoids))) + if not number: + factoids = [] + counter = 1 + for result in cursor.fetchall(): + factoids.append('(#%s) %s' % (counter, result[0])) + counter += 1 + irc.reply(msg,'%r could be %s' % (key, ', or '.join(factoids))) + else: + try: + irc.reply(msg, cursor.fetchall()[number-1]) + except IndexError: + irc.error(msg, 'That\'s not a valid number for this key.') + return def lock(self, irc, msg, args): """[] @@ -230,6 +249,10 @@ class Factoids(ChannelDBHandler, callbacks.Privmsg): channel = privmsgs.getChannel(msg, args) if args[-1].isdigit(): number = int(args.pop()) + number -= 1 + if number < 0: + irc.error(msg, 'Negative numbers aren\'t valid.') + return elif args[-1] == '*': del args[-1] number = True diff --git a/test/test_Factoids.py b/test/test_Factoids.py index 9cc9b71ec..b6a0f71cc 100644 --- a/test/test_Factoids.py +++ b/test/test_Factoids.py @@ -46,9 +46,10 @@ class FactoidsTestCase(ChannelPluginTestCase, PluginDocumentation): self.assertNotError('learn jemfinch as a crappy assembly programmer') self.assertRegexp('whatis jemfinch', r'.*primary author.*assembly') self.assertError('forget jemfinch') - self.assertError('forget jemfinch 2') + self.assertError('forget jemfinch 3') + self.assertError('forget jemfinch 0') + self.assertNotError('forget jemfinch 2') self.assertNotError('forget jemfinch 1') - self.assertNotError('forget jemfinch 0') self.assertError('whatis jemfinch') self.assertError('factoidinfo jemfinch') @@ -58,8 +59,8 @@ class FactoidsTestCase(ChannelPluginTestCase, PluginDocumentation): self.assertNotError('learn foo bar as quux') self.assertRegexp('whatis foo bar', '.*baz.*quux') self.assertError('forget foo bar') + self.assertNotError('forget foo bar 2') self.assertNotError('forget foo bar 1') - self.assertNotError('forget foo bar 0') self.assertError('whatis foo bar') self.assertError('factoidinfo foo bar') @@ -79,6 +80,14 @@ class FactoidsTestCase(ChannelPluginTestCase, PluginDocumentation): self.assertRegexp('searchfactoids ke', 'inkedmn.*strike|strike.*inkedmn') + def testNotZeroIndexed(self): + self.assertNotError('learn foo as bar') + self.assertNotRegexp('factoidinfo foo', '#0') + self.assertNotRegexp('whatis foo', '#0') + self.assertNotError('learn foo as baz') + self.assertNotRegexp('factoidinfo foo', '#0') + self.assertNotRegexp('whatis foo', '#0') + # vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78: