plugin text commands: added _contextValid method:
- _contextValid check if a method can be called in this context, according to docstring declaration
- use it to parse results in /help command
- use it avoid use of command in wrong context, and help message is sent in this case
This commit is contained in:
parent
c4cd6fd5f3
commit
b2bb3ed564
1 changed files with 28 additions and 4 deletions
|
|
@ -213,18 +213,40 @@ class TextCommands(object):
|
|||
self.feedBack("Command failed with condition '%s'" % failure.value.condition, mess_data, profile)
|
||||
return False
|
||||
|
||||
mess_data["unparsed"] = msg[1 + len(command):] # part not yet parsed of the message
|
||||
try:
|
||||
mess_data["unparsed"] = msg[1 + len(command):] # part not yet parsed of the message
|
||||
d = defer.maybeDeferred(self._commands[command]["callback"], mess_data, profile)
|
||||
d.addErrback(genericErrback)
|
||||
d.addCallback(retHandling)
|
||||
cmd_data = self._commands[command]
|
||||
except KeyError:
|
||||
self.feedBack(_("Unknown command /%s. ") % command + self.HELP_SUGGESTION, mess_data, profile)
|
||||
log.debug("text commands took over")
|
||||
raise failure.Failure(exceptions.CancelError())
|
||||
else:
|
||||
if not self._contextValid(mess_data, cmd_data):
|
||||
# The command is not launched in the right context, we throw a message with help instructions
|
||||
context_txt = _("group discussions") if cmd_data["type"] == "group" else _("one to one discussions")
|
||||
feedback = _("/{command} command only applies in {context}.").format(command=command, context=context_txt)
|
||||
self.feedBack(u"{} {}".format(feedback, self.HELP_SUGGESTION), mess_data, profile)
|
||||
log.debug("text commands took over")
|
||||
raise failure.Failure(exceptions.CancelError())
|
||||
else:
|
||||
d = defer.maybeDeferred(cmd_data["callback"], mess_data, profile)
|
||||
d.addErrback(genericErrback)
|
||||
d.addCallback(retHandling)
|
||||
|
||||
return d or mess_data # if a command is detected, we should have a deferred, else we send the message normally
|
||||
|
||||
def _contextValid(self, mess_data, cmd_data):
|
||||
"""Tell if a command can be used in the given context
|
||||
|
||||
@param mess_data(dict): message data as given in sendMessage trigger
|
||||
@param cmd_data(dict): command data as returned by self._parseDocString
|
||||
@return (bool): True if command can be used in this context
|
||||
"""
|
||||
if ((cmd_data['type'] == "group" and mess_data["type"] != "groupchat") or
|
||||
(cmd_data['type'] == 'one2one' and mess_data["type"] == "groupchat")):
|
||||
return False
|
||||
return True
|
||||
|
||||
def getRoomJID(self, arg, service_jid):
|
||||
"""Return a room jid with a shortcut
|
||||
|
||||
|
|
@ -335,6 +357,8 @@ class TextCommands(object):
|
|||
|
||||
for command in sorted(self._commands):
|
||||
cmd_data = self._commands[command]
|
||||
if not self._contextValid(mess_data, cmd_data):
|
||||
continue
|
||||
spaces = (longuest - len(command)) * ' '
|
||||
help_cmds.append(" /{command}: {spaces} {short_help}".format(
|
||||
command=command,
|
||||
|
|
|
|||
Loading…
Reference in a new issue