diff src/core/xmpp.py @ 1955:633b5c21aefd

backend, frontend: messages refactoring (huge commit, not finished): /!\ database schema has been modified, do a backup before updating message have been refactored, here are the main changes: - languages are now handled - all messages have an uid (internal to SàT) - message updating is anticipated - subject is now first class - new naming scheme is used newMessage => messageNew, getHistory => historyGet, sendMessage => messageSend - minimal compatibility refactoring in quick_frontend/Primitivus, better refactoring should follow - threads handling - delayed messages are saved into history - info messages may also be saved in history (e.g. to keep track of people joining/leaving a room) - duplicate messages should be avoided - historyGet return messages in right order, no need to sort again - plugins have been updated to follow new features, some of them need to be reworked (e.g. OTR) - XEP-0203 (Delayed Delivery) is now fully handled in core, the plugin just handle disco and creation of a delay element - /!\ jp and Libervia are currently broken, as some features of Primitivus It has been put in one huge commit to avoid breaking messaging between changes. This is the main part of message refactoring, other commits will follow to take profit of the new features/behaviour.
author Goffi <goffi@goffi.org>
date Tue, 24 May 2016 22:11:04 +0200
parents 2daf7b4c6756
children a2bc5089c2eb
line wrap: on
line diff
--- a/src/core/xmpp.py	Mon Apr 18 18:35:19 2016 +0200
+++ b/src/core/xmpp.py	Tue May 24 22:11:04 2016 +0200
@@ -20,14 +20,20 @@
 from sat.core.i18n import _
 from sat.core.constants import Const as C
 from twisted.internet import task, defer
-from twisted.words.protocols.jabber import jid, xmlstream
+from twisted.words.protocols.jabber.xmlstream import XMPPHandler
+from twisted.words.protocols.jabber import xmlstream
 from twisted.words.protocols.jabber import error
-from wokkel import client, disco, xmppim, generic, delay, iwokkel
+from twisted.words.protocols.jabber import jid
+from twisted.python import failure
+from wokkel import client, disco, xmppim, generic, iwokkel
+from wokkel import delay
 from sat.core.log import getLogger
 log = getLogger(__name__)
 from sat.core import exceptions
 from zope.interface import implements
-from twisted.words.protocols.jabber.xmlstream import XMPPHandler
+import time
+import calendar
+import uuid
 
 
 class SatXMPPClient(client.XMPPClient):
@@ -42,6 +48,7 @@
         self.__connected = False
         self.profile = profile
         self.host_app = host_app
+        self._mess_id_uid = {} # map from message id to uid use in history. Key: (full_jid,message_id) Value: uid
         self.conn_deferred = defer.Deferred()
         self._waiting_conf = {}  # callback called when a confirmation is received
         self._progress_cb = {}  # callback called when a progress is requested (key = progress id)
@@ -131,53 +138,85 @@
         xmppim.MessageProtocol.__init__(self)
         self.host = host
 
-    def onMessage(self, message):
-        if not message.hasAttribute('from'):
-            message['from'] = self.parent.jid.host
-        log.debug(_(u"got message from: %s") % message["from"])
+    def onMessage(self, message_elt):
+        # TODO: handle threads
+        client = self.parent
+        if not 'from' in message_elt.attributes:
+            message_elt['from'] = client.jid.host
+        log.debug(_(u"got message from: {from_}").format(from_=message_elt['from']))
         post_treat = defer.Deferred() # XXX: plugin can add their treatments to this deferred
 
-        if not self.host.trigger.point("MessageReceived", message, post_treat, profile=self.parent.profile):
+        if not self.host.trigger.point("MessageReceived", client, message_elt, post_treat):
             return
 
-        data = {"from": message['from'],
-                "to": message['to'],
-                "body": "",
-                "extra": {}}
+        message = {}
+        subject = {}
+        extra = {}
+        data = {"from": message_elt['from'],
+                "to": message_elt['to'],
+                "uid": message_elt.getAttribute('uid', unicode(uuid.uuid4())), # XXX: uid is not a standard attribute but may be added by plugins
+                "message": message,
+                "subject": subject,
+                "type": message_elt.getAttribute('type', 'normal'),
+                "extra": extra}
+
+        try:
+            data['stanza_id'] = message_elt['id']
+        except KeyError:
+            pass
+        else:
+            client._mess_id_uid[(data['from'], data['stanza_id'])] = data['uid']
 
-        for e in message.elements():
-            if e.name == "body":
-                data['body'] = e.children[0] if e.children else ""
-            elif e.name == "subject" and e.children:
-                data['extra']['subject'] = e.children[0]
+        # message
+        for e in message_elt.elements(C.NS_CLIENT, 'body'):
+            message[e.getAttribute('xml:lang','')] = unicode(e)
+
+        # subject
+        for e in message_elt.elements(C.NS_CLIENT, 'subject'):
+            subject[e.getAttribute('xml:lang','')] = unicode(e)
 
-        data['type'] = message['type'] if message.hasAttribute('type') else 'normal'
+        # delay and timestamp
+        try:
+            delay_elt = message_elt.elements(delay.NS_DELAY, 'delay').next()
+        except StopIteration:
+            data['timestamp'] = time.time()
+        else:
+            parsed_delay = delay.Delay.fromElement(delay_elt)
+            data['timestamp'] = calendar.timegm(parsed_delay.stamp.utctimetuple())
+            data['received_timestamp'] = unicode(time.time())
+            if parsed_delay.sender:
+                data['delay_sender'] = parsed_delay.sender.full()
+
+        def skipEmptyMessage(data):
+            if not data['message'] and not data['extra']:
+                raise failure.Failure(exceptions.CancelError())
+            return data
 
         def bridgeSignal(data):
+            try:
+                data['extra']['received_timestamp'] = data['received_timestamp']
+                data['extra']['delay_sender'] = data['delay_sender']
+            except KeyError:
+                pass
             if data is not None:
-                self.host.bridge.newMessage(data['from'], data['body'], data['type'], data['to'], data['extra'], profile=self.parent.profile)
+                self.host.bridge.messageNew(data['uid'], data['timestamp'], data['from'].full(), data['to'].full(), data['message'], data['subject'], data['type'], data['extra'], profile=client.profile)
             return data
 
         def addToHistory(data):
-            try:
-                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)
+            data['from'] = jid.JID(data['from'])
+            data['to'] = jid.JID(data['to'])
+            self.host.memory.addToHistory(client, data)
             return data
 
-        def treatmentsEb(failure):
-            failure.trap(exceptions.SkipHistory)
+        def treatmentsEb(failure_):
+            failure_.trap(exceptions.SkipHistory)
             return data
 
-        def cancelErrorTrap(failure):
+        def cancelErrorTrap(failure_):
             """A message sending can be cancelled by a plugin treatment"""
-            failure.trap(exceptions.CancelError)
+            failure_.trap(exceptions.CancelError)
 
+        post_treat.addCallback(skipEmptyMessage)
         post_treat.addCallback(addToHistory)
         post_treat.addErrback(treatmentsEb)
         post_treat.addCallback(bridgeSignal)
@@ -506,7 +545,7 @@
 
     def connectionMade(self):
         log.debug(_(u"Connection made with %s" % self.jabber_host))
-        self.xmlstream.namespace = "jabber:client"
+        self.xmlstream.namespace = C.NS_CLIENT
         self.xmlstream.sendHeader()
 
         iq = xmlstream.IQ(self.xmlstream, 'set')
@@ -526,10 +565,10 @@
         log.debug(_(u"Registration answer: %s") % answer.toXml())
         self.xmlstream.sendFooter()
 
-    def registrationFailure(self, failure):
-        log.info(_("Registration failure: %s") % unicode(failure.value))
+    def registrationFailure(self, failure_):
+        log.info(_("Registration failure: %s") % unicode(failure_.value))
         self.xmlstream.sendFooter()
-        raise failure.value
+        raise failure_.value
 
 
 class SatVersionHandler(generic.VersionHandler):