changeset 922:c897c8d321b3

core: sendMessageTrigger now manage pre and post treatments, which happen before or after XML generation
author Goffi <goffi@goffi.org>
date Sat, 22 Mar 2014 15:34:05 +0100
parents 8dd168c7741c
children e77948faaef3
files src/core/sat_main.py src/plugins/plugin_misc_text_commands.py src/plugins/plugin_xep_0033.py src/plugins/plugin_xep_0071.py src/plugins/plugin_xep_0085.py
diffstat 5 files changed, 29 insertions(+), 23 deletions(-) [+]
line wrap: on
line diff
--- a/src/core/sat_main.py	Sat Mar 22 13:47:33 2014 +0100
+++ b/src/core/sat_main.py	Sat Mar 22 15:34:05 2014 +0100
@@ -489,7 +489,8 @@
             "type": mess_type,
             "extra": extra,
         }
-        treatments = defer.Deferred() # XXX: plugin can add their treatments to this deferred
+        pre_xml_treatments = defer.Deferred() # XXX: plugin can add their pre XML treatments to this deferred
+        post_xml_treatments = defer.Deferred() # XXX: plugin can add their post XML treatments to this deferred
 
         if mess_data["type"] == "auto":
             # we try to guess the type
@@ -512,19 +513,22 @@
             mess_data["type"] == "chat" if mess_data["subject"] else "normal"
 
         if not no_trigger:
-            if not self.trigger.point("sendMessage", mess_data, treatments, profile):
+            if not self.trigger.point("sendMessage", mess_data, pre_xml_treatments, post_xml_treatments, profile):
                 return defer.succeed(None)
 
         debug(_("Sending jabber message of type [%(type)s] to %(to)s...") % {"type": mess_data["type"], "to": to_jid.full()})
-        mess_data['xml'] = domish.Element((None, 'message'))
-        mess_data['xml']["to"] = mess_data["to"].full()
-        mess_data['xml']["from"] = current_jid.full()
-        mess_data['xml']["type"] = mess_data["type"]
-        if mess_data["subject"]:
-            mess_data['xml'].addElement("subject", None, subject)
-        # message without body are used to send chat states
-        if mess_data["message"]:
-            mess_data['xml'].addElement("body", None, mess_data["message"])
+
+        def generateXML(mess_data):
+            mess_data['xml'] = domish.Element((None, 'message'))
+            mess_data['xml']["to"] = mess_data["to"].full()
+            mess_data['xml']["from"] = current_jid.full()
+            mess_data['xml']["type"] = mess_data["type"]
+            if mess_data["subject"]:
+                mess_data['xml'].addElement("subject", None, subject)
+            # message without body are used to send chat states
+            if mess_data["message"]:
+                mess_data['xml'].addElement("body", None, mess_data["message"])
+            return mess_data
 
         def sendErrback(e):
             text = '%s: %s' % (e.value.__class__.__name__, e.getErrorMessage())
@@ -536,10 +540,12 @@
             else:
                 error("Unmanaged exception: %s" % text)
                 return e
-
-        treatments.addCallbacks(self.sendAndStoreMessage, sendErrback, [False, profile])
-        treatments.callback(mess_data)
-        return treatments
+        pre_xml_treatments.addCallback(generateXML)
+        pre_xml_treatments.chainDeferred(post_xml_treatments)
+        post_xml_treatments.addCallback(self.sendAndStoreMessage, False, profile)
+        post_xml_treatments.addErrback(sendErrback)
+        pre_xml_treatments.callback(mess_data)
+        return pre_xml_treatments
 
     def sendAndStoreMessage(self, mess_data, skip_send=False, profile=None):
         """Actually send and store the message to history, after all the treatments have been done.
--- a/src/plugins/plugin_misc_text_commands.py	Sat Mar 22 13:47:33 2014 +0100
+++ b/src/plugins/plugin_misc_text_commands.py	Sat Mar 22 15:34:05 2014 +0100
@@ -46,9 +46,9 @@
         self.host = host
         host.trigger.add("sendMessage", self.sendMessageTrigger)
 
-    def sendMessageTrigger(self, mess_data, treatments, profile):
+    def sendMessageTrigger(self, mess_data, pre_xml_treatments, post_xml_treatments, profile):
         """ Install SendMessage command hook """
-        treatments.addCallback(self._sendMessageCmdHook, profile)
+        pre_xml_treatments.addCallback(self._sendMessageCmdHook, profile)
         return True
 
     def _sendMessageCmdHook(self, mess_data, profile):
--- a/src/plugins/plugin_xep_0033.py	Sat Mar 22 13:47:33 2014 +0100
+++ b/src/plugins/plugin_xep_0033.py	Sat Mar 22 15:34:05 2014 +0100
@@ -76,7 +76,7 @@
         host.trigger.add("sendMessage", self.sendMessageTrigger, TriggerManager.MIN_PRIORITY)
         host.trigger.add("MessageReceived", self.messageReceivedTrigger)
 
-    def sendMessageTrigger(self, mess_data, treatments, profile):
+    def sendMessageTrigger(self, mess_data, pre_xml_treatments, post_xml_treatments, profile):
         """Process the XEP-0033 related data to be sent"""
 
         def treatment(mess_data):
@@ -101,7 +101,7 @@
             d.addCallbacks(discoCallback, lambda dummy: discoCallback(None))
             return d
 
-        treatments.addCallback(treatment)
+        post_xml_treatments.addCallback(treatment)
         return True
 
     def sendAndStoreMessage(self, mess_data, entries, profile):
--- a/src/plugins/plugin_xep_0071.py	Sat Mar 22 13:47:33 2014 +0100
+++ b/src/plugins/plugin_xep_0071.py	Sat Mar 22 15:34:05 2014 +0100
@@ -130,11 +130,11 @@
             pass
         return True
 
-    def sendMessageTrigger(self, mess_data, treatments, profile):
+    def sendMessageTrigger(self, mess_data, pre_xml_treatments, post_xml_treatments, profile):
         """ Check presence of rich text in extra
         """
         if 'rich' in mess_data['extra'] or 'xhtml' in mess_data['extra']:
-            treatments.addCallback(self._sendMessageAddRich, profile)
+            post_xml_treatments.addCallback(self._sendMessageAddRich, profile)
         return True
 
     def _purgeStyle(self, styles_raw):
--- a/src/plugins/plugin_xep_0085.py	Sat Mar 22 13:47:33 2014 +0100
+++ b/src/plugins/plugin_xep_0085.py	Sat Mar 22 15:34:05 2014 +0100
@@ -175,7 +175,7 @@
             break
         return True
 
-    def sendMessageTrigger(self, mess_data, treatments, profile):
+    def sendMessageTrigger(self, mess_data, pre_xml_treatments, post_xml_treatments, profile):
         """
         Eventually add the chat state to the message and initiate
         the state machine when sending an "active" state.
@@ -198,7 +198,7 @@
                     message.addElement(state, NS_CHAT_STATES)
             return mess_data
 
-        treatments.addCallback(treatment)
+        post_xml_treatments.addCallback(treatment)
         return True
 
     def __checkActivation(self, to_jid, forceEntityData, profile):