comparison src/plugins/plugin_misc_text_commands.py @ 1370:53c7678c27a6

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
author Goffi <goffi@goffi.org>
date Thu, 19 Mar 2015 14:29:03 +0100
parents dd1a148bd3d8
children 876c9fbd0b3d
comparison
equal deleted inserted replaced
1369:dd1a148bd3d8 1370:53c7678c27a6
211 211
212 def genericErrback(failure): 212 def genericErrback(failure):
213 self.feedBack("Command failed with condition '%s'" % failure.value.condition, mess_data, profile) 213 self.feedBack("Command failed with condition '%s'" % failure.value.condition, mess_data, profile)
214 return False 214 return False
215 215
216 mess_data["unparsed"] = msg[1 + len(command):] # part not yet parsed of the message
216 try: 217 try:
217 mess_data["unparsed"] = msg[1 + len(command):] # part not yet parsed of the message 218 cmd_data = self._commands[command]
218 d = defer.maybeDeferred(self._commands[command]["callback"], mess_data, profile)
219 d.addErrback(genericErrback)
220 d.addCallback(retHandling)
221 except KeyError: 219 except KeyError:
222 self.feedBack(_("Unknown command /%s. ") % command + self.HELP_SUGGESTION, mess_data, profile) 220 self.feedBack(_("Unknown command /%s. ") % command + self.HELP_SUGGESTION, mess_data, profile)
223 log.debug("text commands took over") 221 log.debug("text commands took over")
224 raise failure.Failure(exceptions.CancelError()) 222 raise failure.Failure(exceptions.CancelError())
223 else:
224 if not self._contextValid(mess_data, cmd_data):
225 # The command is not launched in the right context, we throw a message with help instructions
226 context_txt = _("group discussions") if cmd_data["type"] == "group" else _("one to one discussions")
227 feedback = _("/{command} command only applies in {context}.").format(command=command, context=context_txt)
228 self.feedBack(u"{} {}".format(feedback, self.HELP_SUGGESTION), mess_data, profile)
229 log.debug("text commands took over")
230 raise failure.Failure(exceptions.CancelError())
231 else:
232 d = defer.maybeDeferred(cmd_data["callback"], mess_data, profile)
233 d.addErrback(genericErrback)
234 d.addCallback(retHandling)
225 235
226 return d or mess_data # if a command is detected, we should have a deferred, else we send the message normally 236 return d or mess_data # if a command is detected, we should have a deferred, else we send the message normally
237
238 def _contextValid(self, mess_data, cmd_data):
239 """Tell if a command can be used in the given context
240
241 @param mess_data(dict): message data as given in sendMessage trigger
242 @param cmd_data(dict): command data as returned by self._parseDocString
243 @return (bool): True if command can be used in this context
244 """
245 if ((cmd_data['type'] == "group" and mess_data["type"] != "groupchat") or
246 (cmd_data['type'] == 'one2one' and mess_data["type"] == "groupchat")):
247 return False
248 return True
227 249
228 def getRoomJID(self, arg, service_jid): 250 def getRoomJID(self, arg, service_jid):
229 """Return a room jid with a shortcut 251 """Return a room jid with a shortcut
230 252
231 @param arg: argument: can be a full room jid (e.g.: sat@chat.jabberfr.org) 253 @param arg: argument: can be a full room jid (e.g.: sat@chat.jabberfr.org)
333 longuest = max([len(command) for command in self._commands]) 355 longuest = max([len(command) for command in self._commands])
334 help_cmds = [] 356 help_cmds = []
335 357
336 for command in sorted(self._commands): 358 for command in sorted(self._commands):
337 cmd_data = self._commands[command] 359 cmd_data = self._commands[command]
360 if not self._contextValid(mess_data, cmd_data):
361 continue
338 spaces = (longuest - len(command)) * ' ' 362 spaces = (longuest - len(command)) * ' '
339 help_cmds.append(" /{command}: {spaces} {short_help}".format( 363 help_cmds.append(" /{command}: {spaces} {short_help}".format(
340 command=command, 364 command=command,
341 spaces=spaces, 365 spaces=spaces,
342 short_help=cmd_data["doc_short_help"] 366 short_help=cmd_data["doc_short_help"]