diff src/core/sat_main.py @ 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 ed9841e6d84a
children e77948faaef3
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.