From e71ee8fbb166b7ed81f960063d33f3a74c8a6aed Mon Sep 17 00:00:00 2001 From: nanotube Date: Tue, 23 Mar 2010 15:46:22 -0400 Subject: [PATCH] don't give up too easily with invalid command, instead search factoid keys with wildcard first. --- plugins/Factoids/config.py | 5 +++++ plugins/Factoids/plugin.py | 21 ++++++++++++++++++++- plugins/Factoids/test.py | 3 +++ 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/plugins/Factoids/config.py b/plugins/Factoids/config.py index bb16421de..d6be96f79 100644 --- a/plugins/Factoids/config.py +++ b/plugins/Factoids/config.py @@ -60,6 +60,11 @@ conf.registerChannelValue(Factoids, 'replyWhenInvalidCommand', registry.Boolean(True, _("""Determines whether the bot will reply to invalid commands by searching for a factoid; basically making the whatis unnecessary when you want all factoids for a given key."""))) +conf.registerChannelValue(Factoids, 'replyWhenInvalidCommandSearchKeys', + registry.Boolean(True, _("""If replyWhenInvalidCommand is True, and you + supply a nonexistent factoid as a command, this setting make the bot try a + wildcard search for factoid keys, returning a list of matching keys, + before giving up with an invalid command error."""))) conf.registerChannelValue(Factoids, 'format', FactoidFormat(_('$key could be $value.'), _("""Determines the format of the response given when a factoid's value is requested. All the standard diff --git a/plugins/Factoids/plugin.py b/plugins/Factoids/plugin.py index 63a532429..7d87db439 100644 --- a/plugins/Factoids/plugin.py +++ b/plugins/Factoids/plugin.py @@ -199,6 +199,16 @@ class Factoids(callbacks.Plugin, plugins.ChannelDBHandler): return cursor.fetchall() #return [t[0] for t in cursor.fetchall()] + def _searchFactoid(self, channel, key): + db = self.getDb(channel) + cursor = db.cursor() + key = '%' + key + '%' + cursor.execute("""SELECT key FROM keys + WHERE key LIKE ? + LIMIT 20""", (key,)) + return cursor.fetchall() + + def _updateRank(self, channel, factoids): if self.registryValue('keepRankInfo', channel): db = self.getDb(channel) @@ -248,7 +258,16 @@ class Factoids(callbacks.Plugin, plugins.ChannelDBHandler): if self.registryValue('replyWhenInvalidCommand', channel): key = ' '.join(tokens) factoids = self._lookupFactoid(channel, key) - self._replyFactoids(irc, msg, key, channel, factoids, error=False) + if factoids: + self._replyFactoids(irc, msg, key, channel, factoids, error=False) + else: + if self.registryValue('replyWhenInvalidCommandSearchKeys'): + factoids = self._searchFactoid(channel, key) + #print 'searchfactoids result:', factoids, '>' + if factoids: + keylist = ["'%s'" % (fact[0],) for fact in factoids] + keylist = ', '.join(keylist) + irc.reply("I do not know about '%s', but I do know about these similar topics: %s" % (key, keylist)) @internationalizeDocstring def whatis(self, irc, msg, args, channel, words): diff --git a/plugins/Factoids/test.py b/plugins/Factoids/test.py index 5cafe3ce9..ae35271a9 100644 --- a/plugins/Factoids/test.py +++ b/plugins/Factoids/test.py @@ -157,6 +157,9 @@ class FactoidsTestCase(ChannelPluginTestCase): replyWhenInvalidCommand.setValue(True) self.assertNotError('learn foo as bar') self.assertRegexp('foo', 'bar') + self.assertNotError('learn mooz as cowz') + self.assertRegexp('moo', 'mooz') + self.assertError('nosuchthing') finally: conf.supybot.plugins.Factoids.\ replyWhenInvalidCommand.setValue(orig)