diff sat/core/xmpp.py @ 3366:e09cb08166a3

plugin XEP-0329, core(xmpp): moved `_compParseJids` to `SatXMPPComponent`: This method to retrieve owner and peer JID from an element is generally useful for components, thus it has been moved. The part to retrieve owner JID from local JID has been splitted in its own `getOwnerFromJid` method.
author Goffi <goffi@goffi.org>
date Sun, 20 Sep 2020 14:04:11 +0200
parents 5369ce5bcecf
children dea30b1eb166
line wrap: on
line diff
--- a/sat/core/xmpp.py	Sun Sep 20 11:03:24 2020 +0200
+++ b/sat/core/xmpp.py	Sun Sep 20 14:04:11 2020 +0200
@@ -21,6 +21,7 @@
 import calendar
 import uuid
 import mimetypes
+from typing import Tuple
 from urllib.parse import urlparse, unquote
 from functools import partial
 from pathlib import Path
@@ -1059,6 +1060,46 @@
         if self.sendHistory:
             post_xml_treatments.addCallback(self.messageAddToHistory)
 
+    def getOwnerFromJid(self, to_jid: jid.JID) -> jid.JID:
+        """Retrieve "owner" of a component resource from the destination jid of the request
+
+        This method needs plugin XEP-0106 for unescaping, if you use it you must add the
+        plugin to your dependencies.
+        A "user" part must be present in "to_jid" (otherwise, the component itself is addressed)
+        @param to_jid: destination JID of the request
+        """
+        try:
+            unescape = self.host_app.plugins['XEP-0106'].unescape
+        except KeyError:
+            raise exceptions.MissingPlugin("Plugin XEP-0106 is needed to retrieve owner")
+        else:
+            user = unescape(to_jid.user)
+        if '@' in user:
+            # a full jid is specified
+            return jid.JID(user)
+        else:
+            # only user part is specified, we use our own host to build the full jid
+            return jid.JID(None, (user, self.host, None))
+
+    def getOwnerAndPeer(self, iq_elt: domish.Element) -> Tuple[jid.JID, jid.JID]:
+        """Retrieve owner of a component jid, and the jid of the requesting peer
+
+        "owner" is found by either unescaping full jid from node, or by combining node
+        with our host.
+        Peer jid is the requesting jid from the IQ element
+        @param iq_elt: IQ stanza sent from the requested
+        @return: owner and peer JIDs
+        """
+        to_jid = jid.JID(iq_elt['to'])
+        if to_jid.user:
+            owner = self.getOwnerFromJid(to_jid)
+        else:
+            owner = jid.JID(iq_elt["from"]).userhostJID()
+
+        peer_jid = jid.JID(iq_elt["from"])
+        return peer_jid, owner
+
+
 
 class SatMessageProtocol(xmppim.MessageProtocol):