Fix for RFE #801934: Response in case of non-commands.

This commit is contained in:
Jeremy Fincher 2003-09-10 08:32:20 +00:00
parent ffefdf1ab7
commit 176b9b30b6
6 changed files with 54 additions and 0 deletions

View File

@ -51,6 +51,26 @@ import privmsgs
import callbacks import callbacks
class MiscCommands(callbacks.Privmsg): 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): def list(self, irc, msg, args):
"""[<module name>] """[<module name>]

View File

@ -262,6 +262,14 @@ def tokenize(s):
debug.msg('tokenize took %s seconds.' % (time.time() - start), 'verbose') debug.msg('tokenize took %s seconds.' % (time.time() - start), 'verbose')
return args 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): def findCallbackForCommand(irc, commandName):
"""Given a command name and an Irc object, returns the callback that """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.""" command is in. Returns None if there is no callback with that command."""

View File

@ -84,6 +84,13 @@ throttleTime = 1.0
### ###
allowEval = False 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 # defaultCapabilities: Capabilities allowed to everyone by default. You almost
# certainly want to have !owner and !admin in here. # certainly want to have !owner and !admin in here.

View File

@ -37,6 +37,7 @@ if 'test' not in sys.path:
import conf import conf
conf.dataDir = 'test-data' conf.dataDir = 'test-data'
conf.replyWhenNotCommand = False
from fix import * from fix import *

View File

@ -33,6 +33,13 @@ from test import *
class MiscCommandsTestCase(PluginTestCase, PluginDocumentation): class MiscCommandsTestCase(PluginTestCase, PluginDocumentation):
plugins = ('MiscCommands', 'Utilities') 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): def testHelp(self):
self.assertNotError('help list') self.assertNotError('help list')
self.assertNotError('help help') self.assertNotError('help help')

View File

@ -128,6 +128,17 @@ class FunctionsTestCase(unittest.TestCase):
'%s: foo' % channelMsg.nick), '%s: foo' % channelMsg.nick),
callbacks.reply(channelMsg, 'foo')) 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): class PrivmsgTestCase(PluginTestCase):
plugins = ('Utilities',) plugins = ('Utilities',)
def testEmptySquareBrackets(self): def testEmptySquareBrackets(self):