diff sat/core/xmpp.py @ 3795:967a8e109cda

core (xmpp): adapt message workflow to components: message workflow were not used for components so far. This patch activate it, normalise namespace to the client one to simplify parsing, and use SatXMPPComponent.sendHistory and SatXMPPComponent.receiveHistory to indicate if a component should store sent and received message in database (they both default to False). OTR plugin is deactivated for components. rel 367
author Goffi <goffi@goffi.org>
date Fri, 17 Jun 2022 14:15:23 +0200
parents efc34a89e70b
children 8289ac1b34f4
line wrap: on
line diff
--- a/sat/core/xmpp.py	Fri Jun 17 14:15:23 2022 +0200
+++ b/sat/core/xmpp.py	Fri Jun 17 14:15:23 2022 +0200
@@ -23,7 +23,7 @@
 from pathlib import Path
 import sys
 import time
-from typing import Tuple
+from typing import Tuple, Optional
 from urllib.parse import unquote, urlparse
 import uuid
 
@@ -972,10 +972,10 @@
         "Component"
     )  # used for to distinguish some trigger points set in SatXMPPEntity
     is_component = True
-    sendHistory = (
-        False
-    )  # XXX: set to True from entry plugin to keep messages in history for received
-       #      messages
+    # XXX: set to True from entry plugin to keep messages in history for sent messages
+    sendHistory = False
+    # XXX: same as sendHistory but for received messaged
+    receiveHistory = False
 
     def __init__(self, host_app, profile, component_jid, password, host=None, port=None,
                  max_retries=C.XMPP_MAX_RETRIES):
@@ -1016,6 +1016,10 @@
     def is_admin(self) -> bool:
         return False
 
+    def _createSubProtocols(self):
+        self.messageProt = SatMessageProtocol(self.host_app)
+        self.messageProt.setHandlerParent(self)
+
     def _buildDependencies(self, current, plugins, required=True):
         """build recursively dependencies needed for a plugin
 
@@ -1151,6 +1155,12 @@
     def client(self):
         return self.parent
 
+    def normalizeNS(self, elt: domish.Element, namespace: Optional[str]) -> None:
+        if elt.uri == namespace:
+            elt.defaultUri = elt.uri = C.NS_CLIENT
+        for child in elt.elements():
+            self.normalizeNS(child, namespace)
+
     def parseMessage(self, message_elt):
         """Parse a message XML and return message_data
 
@@ -1165,12 +1175,9 @@
                 .format(xml=message_elt.toXml())))
             return {}
 
-        if message_elt.uri is None:
-            # wokkel element parsing strip out root namespace
-            message_elt.defaultUri = message_elt.uri = C.NS_CLIENT
-            for c in message_elt.elements():
-                if c.uri is None:
-                    c.uri = C.NS_CLIENT
+        if message_elt.uri == None:
+            # xmlns may be None when wokkel element parsing strip out root namespace
+            self.normalizeNS(message_elt, None)
         elif message_elt.uri != C.NS_CLIENT:
             log.warning(_(
                 "received <message> with a wrong namespace: {xml}"
@@ -1249,10 +1256,12 @@
         data = self.parseMessage(message_elt)
         post_treat.addCallback(self.completeAttachments)
         post_treat.addCallback(self.skipEmptyMessage)
-        post_treat.addCallback(
-            lambda ret: defer.ensureDeferred(self.addToHistory(ret))
-        )
-        post_treat.addCallback(self.bridgeSignal, data)
+        if not client.is_component or client.receiveHistory:
+            post_treat.addCallback(
+                lambda ret: defer.ensureDeferred(self.addToHistory(ret))
+            )
+        if not client.is_component:
+            post_treat.addCallback(self.bridgeSignal, data)
         post_treat.addErrback(self.cancelErrorTrap)
         post_treat.callback(data)
 
@@ -1263,6 +1272,9 @@
         if not "from" in message_elt.attributes:
             message_elt["from"] = client.jid.host
         log.debug(_("got message from: {from_}").format(from_=message_elt["from"]))
+        if self.client.is_component and message_elt.uri == component.NS_COMPONENT_ACCEPT:
+            # we use client namespace all the time to simplify parsing
+            self.normalizeNS(message_elt, component.NS_COMPONENT_ACCEPT)
 
         # plugin can add their treatments to this deferred
         post_treat = defer.Deferred()