changeset 663:8004c7d4aba7

core: Deferred in onMessage. onMessage now use a deferred which is passed to MessageReceived trigger through the post_treat parameter. This can be used by plugins to add deferred in the callback chain.
author Goffi <goffi@goffi.org>
date Thu, 31 Oct 2013 17:18:04 +0100
parents 4f747d7fde8c
children cac98ca76479
files src/core/xmpp.py src/plugins/plugin_exp_command_export.py src/plugins/plugin_exp_parrot.py src/plugins/plugin_misc_maildir.py src/plugins/plugin_xep_0085.py
diffstat 5 files changed, 32 insertions(+), 21 deletions(-) [+]
line wrap: on
line diff
--- a/src/core/xmpp.py	Thu Oct 31 17:14:15 2013 +0100
+++ b/src/core/xmpp.py	Thu Oct 31 17:18:04 2013 +0100
@@ -106,28 +106,39 @@
 
     def onMessage(self, message):
         debug(_(u"got message from: %s"), message["from"])
-        if not self.host.trigger.point("MessageReceived", message, profile=self.parent.profile):
+        post_treat = defer.Deferred() # XXX: plugin can add there treatments to this deferred
+
+        if not self.host.trigger.point("MessageReceived", message, post_treat, profile=self.parent.profile):
             return
-        # set message body to empty string by default, and later
-        # also forward message without body (chat state notification...)
-        mess_body = ""
+
+        data = {"from": message['from'],
+                "to": message['to'],
+                "body": "",
+                "extra": {}}
+
         for e in message.elements():
             if e.name == "body":
-                mess_body = e.children[0] if e.children else ""
+                data['body'] = e.children[0] if e.children else ""
                 break
 
-        mess_type = message['type'] if message.hasAttribute('type') else 'normal'
-        try:
-            _delay = delay.Delay.fromElement(filter(lambda elm: elm.name == 'delay', message.elements())[0])
-            timestamp = timegm(_delay.stamp.utctimetuple())
-            extra = {"archive": str(timestamp)}
-            if mess_type != 'groupchat':  # XXX: we don't save delayed messages in history for groupchats
-                #TODO: add delayed messages to history if they aren't already in it
-                self.host.memory.addToHistory(jid.JID(message["from"]), jid.JID(message["to"]), mess_body, mess_type, timestamp, profile=self.parent.profile)
-        except IndexError:
-            extra = {}
-            self.host.memory.addToHistory(jid.JID(message["from"]), jid.JID(message["to"]), mess_body, mess_type, profile=self.parent.profile)
-        self.host.bridge.newMessage(message["from"], mess_body, mess_type, message['to'], extra, profile=self.parent.profile)
+        data['type'] = message['type'] if message.hasAttribute('type') else 'normal'
+
+        def after_treatments(data):
+            # set message body to empty string by default, and later
+            # also forward message without body (chat state notification...)
+            try:
+                _delay = delay.Delay.fromElement(filter(lambda elm: elm.name == 'delay', message.elements())[0])
+                timestamp = timegm(_delay.stamp.utctimetuple())
+                data['extra']['archive'] = str(timestamp)
+                if data['type'] != 'groupchat':  # XXX: we don't save delayed messages in history for groupchats
+                    #TODO: add delayed messages to history if they aren't already in it
+                    self.host.memory.addToHistory(jid.JID(data['from']), jid.JID(data['to']), data['body'], data['type'], timestamp, profile=self.parent.profile)
+            except IndexError:
+                self.host.memory.addToHistory(jid.JID(data['from']), jid.JID(data['to']), data['body'], data['type'], profile=self.parent.profile)
+            self.host.bridge.newMessage(data['from'], data['body'], data['type'], data['to'], data['extra'], profile=self.parent.profile)
+
+        post_treat.addCallback(after_treatments)
+        post_treat.callback(data)
 
 
 class SatRosterProtocol(xmppim.RosterClientProtocol):
--- a/src/plugins/plugin_exp_command_export.py	Thu Oct 31 17:14:15 2013 +0100
+++ b/src/plugins/plugin_exp_command_export.py	Thu Oct 31 17:18:04 2013 +0100
@@ -100,7 +100,7 @@
         except ValueError:
             pass
 
-    def MessageReceivedTrigger(self, message, profile):
+    def MessageReceivedTrigger(self, message, post_treat, profile):
         """ Check if source is linked and repeat message, else do nothing  """
         from_jid = jid.JID(message["from"])
         spawned_key = (from_jid.userhostJID(), profile)
--- a/src/plugins/plugin_exp_parrot.py	Thu Oct 31 17:14:15 2013 +0100
+++ b/src/plugins/plugin_exp_parrot.py	Thu Oct 31 17:18:04 2013 +0100
@@ -60,7 +60,7 @@
     #        debug("Parrot link detected, skipping other triggers")
     #        raise SkipOtherTriggers
 
-    def MessageReceivedTrigger(self, message, profile):
+    def MessageReceivedTrigger(self, message, post_treat, profile):
         """ Check if source is linked and repeat message, else do nothing  """
         client = self.host.getClient(profile)
         from_jid = jid.JID(message["from"])
--- a/src/plugins/plugin_misc_maildir.py	Thu Oct 31 17:14:15 2013 +0100
+++ b/src/plugins/plugin_misc_maildir.py	Thu Oct 31 17:18:04 2013 +0100
@@ -86,7 +86,7 @@
         del self.__mailboxes[profile]
         del self.data[profile]
 
-    def messageReceivedTrigger(self, message, profile):
+    def messageReceivedTrigger(self, message, post_treat, profile):
         """This trigger catch normal message and put the in the Maildir box.
         If the message is not of "normal" type, do nothing
         @param message: message xmlstrem
--- a/src/plugins/plugin_xep_0085.py	Thu Oct 31 17:14:15 2013 +0100
+++ b/src/plugins/plugin_xep_0085.py	Thu Oct 31 17:18:04 2013 +0100
@@ -134,7 +134,7 @@
         if (category, name) == (PARAM_KEY, PARAM_NAME):
             self.updateEntityData("@ALL@", True if value == "true" else "@NONE@", profile)
 
-    def messageReceivedTrigger(self, message, profile):
+    def messageReceivedTrigger(self, message, post_treat, profile):
         """
         Update the entity cache when we receive a message with body.
         Check for a check state in the incoming message and broadcast signal.