From 32c718ca666038c869d027cc7d130370c6c48fac 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 2e6f5b3c8..374a84978 100644 --- a/plugins/Factoids/config.py +++ b/plugins/Factoids/config.py @@ -58,6 +58,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 0718ca610..37f655aba 100644 --- a/plugins/Factoids/plugin.py +++ b/plugins/Factoids/plugin.py @@ -197,6 +197,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) @@ -246,7 +256,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)) 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)