changeset 1743:4c48468ead4c

plugin XEP-0203, core (xmpp): leave the management of delay element to XEP-0203 (fix bug 59): - this patch reveals a bug in primitivus Chat.printMessage relative to MUC history (the test to discard duplicates is wrong)
author souliane <souliane@mailoo.org>
date Fri, 11 Dec 2015 11:25:29 +0100
parents 244a605623d6
children 9a48e09044eb
files src/core/xmpp.py src/plugins/plugin_xep_0203.py
diffstat 2 files changed, 32 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/src/core/xmpp.py	Tue Dec 15 13:30:47 2015 +0100
+++ b/src/core/xmpp.py	Fri Dec 11 11:25:29 2015 +0100
@@ -26,7 +26,6 @@
 from sat.core.log import getLogger
 log = getLogger(__name__)
 from sat.core import exceptions
-from calendar import timegm
 from zope.interface import implements
 from twisted.words.protocols.jabber.xmlstream import XMPPHandler
 
@@ -160,17 +159,15 @@
             return data
 
         def addToHistory(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)
+                timestamp = data['extra']['timestamp']  # timestamp added by XEP-0203
+            except KeyError:
+                self.host.memory.addToHistory(jid.JID(data['from']), jid.JID(data['to']), data['body'], data['type'], data['extra'], profile=self.parent.profile)
+            else:
                 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
+                    data['extra']['archive'] = timestamp  # FIXME: this "archive" is actually never used
                     self.host.memory.addToHistory(jid.JID(data['from']), jid.JID(data['to']), data['body'], data['type'], data['extra'], timestamp, profile=self.parent.profile)
-            except IndexError:
-                self.host.memory.addToHistory(jid.JID(data['from']), jid.JID(data['to']), data['body'], data['type'], data['extra'], profile=self.parent.profile)
             return data
 
         def treatmentsEb(failure):
--- a/src/plugins/plugin_xep_0203.py	Tue Dec 15 13:30:47 2015 +0100
+++ b/src/plugins/plugin_xep_0203.py	Fri Dec 11 11:25:29 2015 +0100
@@ -22,6 +22,7 @@
 from sat.core.log import getLogger
 log = getLogger(__name__)
 
+from calendar import timegm
 from wokkel import disco, iwokkel, delay
 try:
     from twisted.words.protocols.xmlstream import XMPPHandler
@@ -48,6 +49,8 @@
     def __init__(self, host):
         log.info(_("Delayed Delivery plugin initialization"))
         self.host = host
+        host.trigger.add("MessageReceived", self.messageReceivedTrigger)
+
 
     def getHandler(self, profile):
         return XEP_0203_handler(self, profile)
@@ -68,6 +71,30 @@
             parent.addChild(elt)
         return elt
 
+    def messagePostTreat(self, data, timestamp):
+        """Set the timestamp of a received message.
+
+        @param data (dict): data send by MessageReceived trigger through post_treat deferred
+        @param timestamp (int): original timestamp of a delayed message
+        @return: dict
+        """
+        data['extra']['timestamp'] = unicode(timestamp)
+        return data
+
+    def messageReceivedTrigger(self, message, post_treat, profile):
+        """Process a delay element from incoming message.
+
+        @param message (domish.Element): message element
+        @param post_treat (Deferred): deferred instance to add post treatments
+        """
+        try:
+            delay_ = delay.Delay.fromElement([elm for elm in message.elements() if elm.name == 'delay'][0])
+        except IndexError:
+            return True
+        else:
+            timestamp = timegm(delay_.stamp.utctimetuple())
+            post_treat.addCallback(self.messagePostTreat, timestamp)
+        return True
 
 class XEP_0203_handler(XMPPHandler):
     implements(iwokkel.IDisco)