From 176b9b30b67b76e5c15edda076dd9560bf90d37a Mon Sep 17 00:00:00 2001 From: Jeremy Fincher Date: Wed, 10 Sep 2003 08:32:20 +0000 Subject: [PATCH] Fix for RFE #801934: Response in case of non-commands. --- src/MiscCommands.py | 20 ++++++++++++++++++++ src/callbacks.py | 8 ++++++++ src/conf.py | 7 +++++++ test/test.py | 1 + test/test_MiscCommands.py | 7 +++++++ test/test_callbacks.py | 11 +++++++++++ 6 files changed, 54 insertions(+) diff --git a/src/MiscCommands.py b/src/MiscCommands.py index 51f9eb65c..943ca790e 100755 --- a/src/MiscCommands.py +++ b/src/MiscCommands.py @@ -51,6 +51,26 @@ import privmsgs import callbacks class MiscCommands(callbacks.Privmsg): + def doPrivmsg(self, irc, msg): + # This exists to be able to respond to attempts to command the bot + # with a "That's not a command!" if the proper conf.variable is set. + callbacks.Privmsg.doPrivmsg(self, irc, msg) + if conf.replyWhenNotCommand: + s = callbacks.addressed(irc.nick, msg) + if s: + tokens = callbacks.tokenize(s) + notCommands = [] + for command in callbacks.getCommands(tokens): + if not callbacks.findCallbackForCommand(irc, command): + notCommands.append(command) + if notCommands: + if len(notCommands) == 1: + s = '%r is not a command.' % notCommands[0] + else: + s = '%s are not commands' % \ + utils.commaAndify(notCommands) + irc.queueMsg(callbacks.reply(msg, s)) + def list(self, irc, msg, args): """[] diff --git a/src/callbacks.py b/src/callbacks.py index 452d0af7b..24df66666 100644 --- a/src/callbacks.py +++ b/src/callbacks.py @@ -262,6 +262,14 @@ def tokenize(s): debug.msg('tokenize took %s seconds.' % (time.time() - start), 'verbose') return args +def getCommands(tokens): + L = [] + if tokens and isinstance(tokens, list): + L.append(tokens[0]) + for elt in tokens: + L.extend(getCommands(elt)) + return L + def findCallbackForCommand(irc, commandName): """Given a command name and an Irc object, returns the callback that command is in. Returns None if there is no callback with that command.""" diff --git a/src/conf.py b/src/conf.py index fda0c0147..972f77c7e 100644 --- a/src/conf.py +++ b/src/conf.py @@ -84,6 +84,13 @@ throttleTime = 1.0 ### allowEval = False +### +# replyWhenNotCommand: True if you want the bot reply when someone apparently +# addresses him but there is no command. Otherwise he'll +# just remain silent. +### +replyWhenNotCommand = True + ### # defaultCapabilities: Capabilities allowed to everyone by default. You almost # certainly want to have !owner and !admin in here. diff --git a/test/test.py b/test/test.py index 79eabf39c..1c13fb887 100755 --- a/test/test.py +++ b/test/test.py @@ -37,6 +37,7 @@ if 'test' not in sys.path: import conf conf.dataDir = 'test-data' +conf.replyWhenNotCommand = False from fix import * diff --git a/test/test_MiscCommands.py b/test/test_MiscCommands.py index 622760c43..ce0e5ea16 100644 --- a/test/test_MiscCommands.py +++ b/test/test_MiscCommands.py @@ -33,6 +33,13 @@ from test import * class MiscCommandsTestCase(PluginTestCase, PluginDocumentation): plugins = ('MiscCommands', 'Utilities') + def testReplyWhenNotCommand(self): + conf.replyWhenNotCommand = True + self.assertRegexp('foo bar baz', 'not.*command') + self.assertRegexp('foo | bar | baz', 'not.*commands') + self.assertRegexp('baz [foo] [bar]', 'not.*commands') + conf.replyWhenNotCommand = False + def testHelp(self): self.assertNotError('help list') self.assertNotError('help help') diff --git a/test/test_callbacks.py b/test/test_callbacks.py index 3c5bfc81b..d95448465 100644 --- a/test/test_callbacks.py +++ b/test/test_callbacks.py @@ -128,6 +128,17 @@ class FunctionsTestCase(unittest.TestCase): '%s: foo' % channelMsg.nick), callbacks.reply(channelMsg, 'foo')) + def testGetCommands(self): + self.assertEqual(callbacks.getCommands(['foo']), ['foo']) + self.assertEqual(callbacks.getCommands(['foo', 'bar']), ['foo']) + self.assertEqual(callbacks.getCommands(['foo', ['bar', 'baz']]), + ['foo', 'bar']) + self.assertEqual(callbacks.getCommands(['foo', 'bar', ['baz']]), + ['foo', 'baz']) + self.assertEqual(callbacks.getCommands(['foo', ['bar'], ['baz']]), + ['foo', 'bar', 'baz']) + + class PrivmsgTestCase(PluginTestCase): plugins = ('Utilities',) def testEmptySquareBrackets(self):