diff src/core/sat_main.py @ 697:0c84fb112d70

core: sendMessage triggers now use a treatments deferred; - treaments deferred can be used by plugins to change XML elements before sending them, in a similar way as for newMessage - sendMessageXml trigger became useless, so it as been removed in favor of the new deferred
author Goffi <goffi@goffi.org>
date Wed, 13 Nov 2013 13:57:36 +0100
parents 9a50aa7feefb
children d731ae066158
line wrap: on
line diff
--- a/src/core/sat_main.py	Tue Nov 12 16:56:34 2013 +0100
+++ b/src/core/sat_main.py	Wed Nov 13 13:57:36 2013 +0100
@@ -490,6 +490,7 @@
             "type": mess_type,
             "options": options,
         }
+        treatments = defer.Deferred() # XXX: plugin can add their treatments to this deferred
 
         if mess_data["type"] == "auto":
             # we try to guess the type
@@ -512,38 +513,39 @@
             mess_data["type"] == "chat" if mess_data["subject"] else "normal"
 
         if not no_trigger:
-            if not self.trigger.point("sendMessage", mess_data, profile):
+            if not self.trigger.point("sendMessage", mess_data, treatments, profile):
                 return
 
         debug(_("Sending jabber message of type [%(type)s] to %(to)s...") % {"type": mess_data["type"], "to": to_jid.full()})
-        message = domish.Element((None, 'message'))
-        message["to"] = mess_data["to"].full()
-        message["from"] = current_jid.full()
-        message["type"] = mess_data["type"]
+        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"]:
-            message.addElement("subject", None, subject)
+            mess_data['xml'].addElement("subject", None, subject)
         # message without body are used to send chat states
         if mess_data["message"]:
-            message.addElement("body", None, mess_data["message"])
-        if not no_trigger:
-            if not self.trigger.point("sendMessageXml", message,
-                                      mess_data, profile):
-                return
-        client.xmlstream.send(message)
-        if mess_data["type"] != "groupchat":
-            # we don't add groupchat message to history, as we get them back
-            # and they will be added then
-            self.memory.addToHistory(current_jid, mess_data['to'],
-                                     unicode(mess_data["message"]),
-                                     unicode(mess_data["type"]),
-                                     profile=profile)
-            # We send back the message, so all clients are aware of it
-            if mess_data["message"]:
-                self.bridge.newMessage(message['from'],
-                                       unicode(mess_data["message"]),
-                                       mess_type=mess_data["type"],
-                                       to_jid=message['to'], extra={},
-                                       profile=profile)
+            mess_data['xml'].addElement("body", None, mess_data["message"])
+
+        def sendAndStore(mess_data):
+            client.xmlstream.send(mess_data['xml'])
+            if mess_data["type"] != "groupchat":
+                # we don't add groupchat message to history, as we get them back
+                # and they will be added then
+                self.memory.addToHistory(current_jid, mess_data['to'],
+                                         unicode(mess_data["message"]),
+                                         unicode(mess_data["type"]),
+                                         profile=profile)
+                # We send back the message, so all clients are aware of it
+                if mess_data["message"]:
+                    self.bridge.newMessage(mess_data['xml']['from'],
+                                           unicode(mess_data["message"]),
+                                           mess_type=mess_data["type"],
+                                           to_jid=mess_data['xml']['to'], extra={},
+                                           profile=profile)
+
+        treatments.addCallback(sendAndStore)
+        treatments.callback(mess_data)
 
     def setPresence(self, to="", show="", priority=0, statuses={}, profile_key='@DEFAULT@'):
         """Send our presence information"""