changeset 1002:291eb8216f6e

plugins TEXT-COMMANDS, XEP-0045, XEP-0048, XEP-0249: - give a feedback instead of sending the message when the command is invalid or used in a wrong context - add command /join for XEP-0249
author souliane <souliane@mailoo.org>
date Wed, 30 Apr 2014 16:34:09 +0200
parents eb3601ff73bc
children 52ec79aa5bbe
files src/plugins/plugin_misc_text_commands.py src/plugins/plugin_xep_0045.py src/plugins/plugin_xep_0048.py src/plugins/plugin_xep_0249.py
diffstat 4 files changed, 62 insertions(+), 21 deletions(-) [+]
line wrap: on
line diff
--- a/src/plugins/plugin_misc_text_commands.py	Sun Apr 27 18:51:03 2014 +0200
+++ b/src/plugins/plugin_misc_text_commands.py	Wed Apr 30 16:34:09 2014 +0200
@@ -43,6 +43,8 @@
     #       plugins need a dynamic translation system (translation
     #       should be downloadable independently)
 
+    HELP_SUGGESTION = _("Type '/help' to get a list of the available commands. If you didn't want to use a command, please start your message with '//' to escape the slash.")
+
     def __init__(self, host):
         log.info(_("Text commands initialization"))
         self.host = host
@@ -133,9 +135,10 @@
                 d = defer.maybeDeferred(self._commands[command], mess_data, profile)
                 d.addCallback(retHandling)
             except KeyError:
-                pass
+                self.feedBack(_("Unknown command /%s. ") % command + self.HELP_SUGGESTION, mess_data, profile)
+                return Failure(MessageSentAndStored("text commands took over", mess_data))
 
-        return d or mess_data # if a command is detected, we should have a deferred, else be send the message normally
+        return d or mess_data  # if a command is detected, we should have a deferred, else we send the message normally
 
     def getRoomJID(self, arg, service_jid):
         """Return a room jid with a shortcut
@@ -159,6 +162,19 @@
 
         self.host.bridge.newMessage(unicode(mess_data["to"]), message, mess_data['type'], unicode(_from), {}, profile=profile)
 
+    def feedBackWrongContext(self, command, types, mess_data, profile):
+        """Give a generic message to the user when a command has been used in a wrong context.
+
+        @param command (string): the command name (without the slash)
+        @param types (string, list): the message types to which the command applies.
+        @param mess_data (dict): original message data
+        @param profile: %(doc_profile)s
+        """
+        if not isinstance(types, str):
+            types = _(' or ').join(types)
+        feedback = _("/%(command)s command only applies on %(type)s messages. ") % {'command': command, 'type': types}
+        self.host.plugins[C.TEXT_CMDS].feedBack(feedback + self.HELP_SUGGESTION, mess_data, profile)
+
     def cmd_whois(self, mess_data, profile):
         """show informations on entity"""
         log.debug("Catched whois command")
--- a/src/plugins/plugin_xep_0045.py	Sun Apr 27 18:51:03 2014 +0200
+++ b/src/plugins/plugin_xep_0045.py	Wed Apr 30 16:34:09 2014 +0200
@@ -379,10 +379,8 @@
         log.debug("Catched nick command")
 
         if mess_data['type'] != "groupchat":
-            #/nick command does nothing if we are not on a group chat
-            log.info("Ignoring /nick command on a non groupchat message")
-
-            return True
+            self.host.plugins[C.TEXT_CMDS].feedBackWrongContext('nick', 'groupchat', mess_data, profile)
+            return False
 
         nick = mess_data["unparsed"].strip()
         room = mess_data["to"]
@@ -396,9 +394,8 @@
         log.debug("Catched join command")
 
         if mess_data['type'] != "groupchat":
-            #/leave command does nothing if we are not on a group chat
-            log.info("Ignoring /join command on a non groupchat message")
-            return True
+            self.host.plugins[C.TEXT_CMDS].feedBackWrongContext('join', 'groupchat', mess_data, profile)
+            return False
 
         if mess_data["unparsed"].strip():
             room = self.host.plugins[C.TEXT_CMDS].getRoomJID(mess_data["unparsed"].strip(), mess_data["to"].host)
@@ -413,9 +410,8 @@
         log.debug("Catched leave command")
 
         if mess_data['type'] != "groupchat":
-            #/leave command does nothing if we are not on a group chat
-            log.info("Ignoring /leave command on a non groupchat message")
-            return True
+            self.host.plugins[C.TEXT_CMDS].feedBackWrongContext('leave', 'groupchat', mess_data, profile)
+            return False
 
         if mess_data["unparsed"].strip():
             room = self.host.plugins[C.TEXT_CMDS].getRoomJID(mess_data["unparsed"].strip(), mess_data["to"].host)
@@ -435,9 +431,8 @@
         log.debug("Catched title command")
 
         if mess_data['type'] != "groupchat":
-            #/leave command does nothing if we are not on a group chat
-            log.info("Ignoring /title command on a non groupchat message")
-            return True
+            self.host.plugins[C.TEXT_CMDS].feedBackWrongContext('title', 'groupchat', mess_data, profile)
+            return False
 
         subject = mess_data["unparsed"].strip()
 
--- a/src/plugins/plugin_xep_0048.py	Sun Apr 27 18:51:03 2014 +0200
+++ b/src/plugins/plugin_xep_0048.py	Wed Apr 30 16:34:09 2014 +0200
@@ -428,9 +428,8 @@
         txt_cmd = self.host.plugins[C.TEXT_CMDS]
 
         if mess_data['type'] != "groupchat":
-            #/bookmark command does nothing if we are not on a group chat
-            log.info("Ignoring /bookmark command on a non groupchat message")
-            return True
+            self.host.plugins[C.TEXT_CMDS].feedBackWrongContext('bookmark', 'groupchat', mess_data, profile)
+            return False
 
         options = mess_data["unparsed"].strip().split()
         if options and options[0] not in ('autojoin', 'remove'):
--- a/src/plugins/plugin_xep_0249.py	Sun Apr 27 18:51:03 2014 +0200
+++ b/src/plugins/plugin_xep_0249.py	Wed Apr 30 16:34:09 2014 +0200
@@ -47,6 +47,7 @@
     "type": "XEP",
     "protocols": ["XEP-0249"],
     "dependencies": ["XEP-0045"],
+    "recommendations": [C.TEXT_CMDS],
     "main": "XEP_0249",
     "handler": "yes",
     "description": _("""Implementation of Direct MUC Invitations""")
@@ -80,6 +81,10 @@
         self.host = host
         host.memory.updateParams(self.params)
         host.bridge.addMethod("inviteMUC", ".plugin", in_sign='sssa{ss}s', out_sign='', method=self._invite)
+        try:
+            self.host.plugins[C.TEXT_CMDS].registerTextCommands(self)
+        except KeyError:
+            log.info(_("Text commands not available"))
 
     def getHandler(self, profile):
         return XEP_0249_handler(self)
@@ -153,11 +158,37 @@
 
         if autojoin == "always":
             self._accept(room, profile)
-        elif autojoin == "ask":
+        elif autojoin == "never":
+            self.host.bridge.newAlert(_("An invitation from %(user)s to join the room %(room)s has been declined according to your personal settings.") % {'user': from_, 'room': room}, _("MUC invitation"), "INFO", profile)
+        else:  # leave the default value here
             data = {"message": _("You have been invited by %(user)s to join the room %(room)s. Do you accept?") % {'user': from_, 'room': room}, "title": _("MUC invitation")}
             self.host.askConfirmation(room, "YES/NO", data, accept_cb, profile)
-        else:
-            self.host.bridge.newAlert(_("An invitation from %(user)s to join the room %(room)s has been declined according to your personal settings.") % {'user': from_, 'room': room}, _("MUC invitation"), "INFO", profile)
+
+    def cmd_invite(self, mess_data, profile):
+        """invite someone in the room
+
+        @command (group): (JID)
+            - JID: the JID of the person to invite
+        """
+        log.debug("Catched invite command")
+
+        if mess_data['type'] != "groupchat":
+            self.host.plugins[C.TEXT_CMDS].feedBackWrongContext('invite', 'groupchat', mess_data, profile)
+            return False
+
+        jid_s = mess_data["unparsed"].strip()
+        try:
+            assert(jid_s)
+            jid_ = jid.JID(jid_s)
+            assert(jid_.user)
+            assert(jid_.host)
+        except (jid.InvalidFormat, AssertionError):
+            feedback = _(u"You must provide a valid JID to invite, like in '/invite contact@example.net'")
+            self.host.plugins[C.TEXT_CMDS].feedBack(feedback, mess_data, profile)
+            return False
+
+        self.invite(jid_, mess_data["to"], {}, profile)
+        return False
 
 
 class XEP_0249_handler(XMPPHandler):