diff src/plugins/plugin_exp_parrot.py @ 926:d609581bf74a

plugin text commands: refactoring, text now only contain main commands, and other plugin can add commands themselve: - registerTextCommands can be called by a plugin in its __init__ method, it looks for methods starting with "cmd_" and register them as new commands - addWhoIsCb: add a callback to /whois command, the callback can complete whois informations - plugins parrot, XEP-0045 and XEP-0092 now manage their own commands
author Goffi <goffi@goffi.org>
date Mon, 24 Mar 2014 10:57:15 +0100
parents 1fe00f0c9a91
children cd150dd947e3
line wrap: on
line diff
--- a/src/plugins/plugin_exp_parrot.py	Sun Mar 23 10:02:50 2014 +0100
+++ b/src/plugins/plugin_exp_parrot.py	Mon Mar 24 10:57:15 2014 +0100
@@ -18,6 +18,7 @@
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 from sat.core.i18n import _
+from sat.core.constants import Const as C
 from logging import debug, info, warning, error
 from twisted.words.protocols.jabber import jid
 
@@ -30,6 +31,7 @@
     "type": "EXP",
     "protocols": [],
     "dependencies": ["XEP-0045"],
+    "recommendations": [C.TEXT_CMDS],
     "main": "Exp_Parrot",
     "handler": "no",
     "description": _("""Implementation of parrot mode (repeat messages between 2 entities)""")
@@ -48,6 +50,10 @@
         self.host = host
         host.trigger.add("MessageReceived", self.MessageReceivedTrigger, priority=100)
         #host.trigger.add("sendMessage", self.sendMessageTrigger, priority=100)
+        try:
+            self.host.plugins[C.TEXT_CMDS].registerTextCommands(self)
+        except KeyError:
+            info(_("Text commands not available"))
 
     #def sendMessageTrigger(self, mess_data, treatments, profile):
     #    """ Deactivate other triggers if recipient is in parrot links """
@@ -122,3 +128,50 @@
             del client.parrot_links[source_jid.userhostJID()]
         except (AttributeError, KeyError):
             pass
+
+    def cmd_parrot(self, mess_data, profile):
+        """activate Parrot mode between 2 entities, in both directions."""
+        #TODO: these commands must not be hardcoded, an interface should be made
+        #      to allow plugins to register simple commands like this.
+
+        debug("Catched parrot command")
+        txt_cmd = self.host.plugins[C.TEXT_CMDS]
+
+        try:
+            link_left_jid = jid.JID(mess_data["unparsed"].strip())
+            if not link_left_jid.user or not link_left_jid.host:
+                raise jid.InvalidFormat
+        except jid.InvalidFormat:
+            txt_cmd.feedBack("Can't activate Parrot mode for invalid jid", mess_data, profile)
+            return False
+
+        link_right_jid = mess_data['to']
+
+        self.addParrot(link_left_jid, link_right_jid, profile)
+        self.addParrot(link_right_jid, link_left_jid, profile)
+
+        txt_cmd.feedBack("Parrot mode activated for %s" % (unicode(link_left_jid), ), mess_data, profile)
+
+        return False
+
+    def cmd_unparrot(self, mess_data, profile):
+        """remove Parrot mode between 2 entities, in both directions."""
+        debug("Catched unparrot command")
+        txt_cmd = self.host.plugins[C.TEXT_CMDS]
+
+        try:
+            link_left_jid = jid.JID(mess_data["unparsed"].strip())
+            if not link_left_jid.user or not link_left_jid.host:
+                raise jid.InvalidFormat
+        except jid.InvalidFormat:
+            txt_cmd.feedBack("Can't deactivate Parrot mode for invalid jid", mess_data, profile)
+            return False
+
+        link_right_jid = mess_data['to']
+
+        self.removeParrot(link_left_jid, profile)
+        self.removeParrot(link_right_jid, profile)
+
+        txt_cmd.feedBack("Parrot mode deactivated for %s and %s" % (unicode(link_left_jid), unicode(link_right_jid)), mess_data, profile)
+
+        return False