Moved ambiguous command handling to Owner; fixed handling of empty nested command.

This commit is contained in:
Jeremy Fincher 2003-10-29 23:02:27 +00:00
parent 0011fff94e
commit e556e783b1
3 changed files with 51 additions and 49 deletions

View File

@ -53,7 +53,8 @@ class Misc(callbacks.Privmsg):
priority = sys.maxint
def invalidCommand(self, irc, msg, tokens):
if conf.replyWhenNotCommand:
irc.error(msg, '%r is not a valid command.' % tokens[0])
command = tokens and tokens[0] or ''
irc.error(msg, '%r is not a valid command.' % command)
else:
if not isinstance(irc.irc, irclib.Irc):
irc.reply(msg, '[%s]' % ' '.join(tokens))

View File

@ -88,11 +88,37 @@ class Owner(privmsgs.CapabilityCheckingPrivmsg):
def doPrivmsg(self, irc, msg):
callbacks.Privmsg.handled = False
notCommands = []
ambiguousCommands = {}
s = callbacks.addressed(irc.nick, msg)
if s:
callbacks.IrcObjectProxy(irc, msg, callbacks.tokenize(s))
tokens = callbacks.tokenize(s)
ambiguousCommands = {}
commands = callbacks.getCommands(tokens)
for command in commands:
command = callbacks.canonicalName(command)
cbs = callbacks.findCallbackForCommand(irc, command)
if len(cbs) > 1:
ambiguousCommands[command] = [cb.name() for cb in cbs]
if ambiguousCommands:
if len(ambiguousCommands) == 1: # Common case.
(command, names) = ambiguousCommands.popitem()
names.sort()
s = 'The command %r is available in the %s plugins. '\
'Please specify the plugin whose command you ' \
'wish to call by using its name as a command ' \
'before calling it.' % \
(command, utils.commaAndify(names))
else:
L = []
for (command, names) in ambiguousCommands.iteritems():
names.sort()
L.append('The command %r is available in the %s '
'plugins' %
(command, utils.commaAndify(names)))
s = '%s; please specify from which plugins to ' \
'call these commands.' % '; '.join(L)
irc.queueMsg(callbacks.error(msg, s))
else:
callbacks.IrcObjectProxy(irc, msg, tokens)
def eval(self, irc, msg, args):
"""<expression>

View File

@ -341,49 +341,21 @@ class IrcObjectProxy:
"A proxy object to allow proper nested of commands (even threaded ones)."
def __init__(self, irc, msg, args):
#debug.printf('__init__: %s' % args)
self.irc = irc
self.msg = msg
self.args = args
self.counter = 0
self.action = False
self.notice = False
self.private = False
self.finished = False
self.prefixName = True
self.noLengthCheck = False
if not args:
irc.reply(msg, '[]')
self.finalEvaled = True
self._callInvalidCommands()
else:
if isinstance(irc, irclib.Irc):
# Let's check for {ambiguous,non}Commands.
ambiguousCommands = {}
commands = getCommands(args)
for command in commands:
command = canonicalName(command)
cbs = findCallbackForCommand(irc, command)
if len(cbs) > 1:
ambiguousCommands[command] = [cb.name() for cb in cbs]
if ambiguousCommands:
if len(ambiguousCommands) == 1: # Common case.
(command, names) = ambiguousCommands.popitem()
names.sort()
s = 'The command %r is available in the %s plugins. '\
'Please specify the plugin whose command you ' \
'wish to call by using its name as a command ' \
'before calling it.' % \
(command, utils.commaAndify(names))
else:
L = []
for (command, names) in ambiguousCommands.iteritems():
names.sort()
L.append('The command %r is available in the %s '
'plugins' %
(command, utils.commaAndify(names)))
s = '%s; please specify from which plugins to ' \
'call these commands.' % '; '.join(L)
irc.queueMsg(error(msg, s))
return
self.irc = irc
self.msg = msg
self.args = args
self.counter = 0
self.action = False
self.notice = False
self.private = False
self.finished = False
self.prefixName = True
self.finalEvaled = False
self.noLengthCheck = False
world.commandsProcessed += 1
self.evalArgs()
@ -396,6 +368,13 @@ class IrcObjectProxy:
return
self.finalEval()
def _callInvalidCommands(self):
for cb in self.irc.callbacks:
if self.finished:
break
if hasattr(cb, 'invalidCommand'):
cb.invalidCommand(self, self.msg, self.args)
def finalEval(self):
assert not self.finalEvaled, 'finalEval called twice.'
self.finalEvaled = True
@ -417,11 +396,7 @@ class IrcObjectProxy:
if r.search(self.msg.args[1]):
return
# Ok, no regexp-based things matched.
for cb in self.irc.callbacks:
if self.finished:
break
if hasattr(cb, 'invalidCommand'):
cb.invalidCommand(self, self.msg, self.args)
self._callInvalidCommands()
else:
try:
assert len(cbs) == 1