# HG changeset patch # User Goffi # Date 1426771743 -3600 # Node ID 53c7678c27a6ad888e9693852e29f79516b7754b # Parent dd1a148bd3d8eb75a72e88713f5174b64a21bee7 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 diff -r dd1a148bd3d8 -r 53c7678c27a6 src/plugins/plugin_misc_text_commands.py --- a/src/plugins/plugin_misc_text_commands.py Thu Mar 19 14:02:37 2015 +0100 +++ b/src/plugins/plugin_misc_text_commands.py Thu Mar 19 14:29:03 2015 +0100 @@ -213,18 +213,40 @@ 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 @@ 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,