diff sat/plugins/plugin_xep_0329.py @ 2923:1fd3ecb3351a

plugin XEP-0329: use local part of jid for components: when used by a component, local part can now be used to specify owner of the file. Local part is escaped using XEP-0106, then: - if the unescaped text contain a "@", it is used as a jid - else, a jid is build by using this text and host of the component
author Goffi <goffi@goffi.org>
date Fri, 26 Apr 2019 11:57:26 +0200
parents 90146552cde5
children db0890c9c7db
line wrap: on
line diff
--- a/sat/plugins/plugin_xep_0329.py	Fri Apr 26 11:57:26 2019 +0200
+++ b/sat/plugins/plugin_xep_0329.py	Fri Apr 26 11:57:26 2019 +0200
@@ -42,7 +42,7 @@
     C.PI_TYPE: "XEP",
     C.PI_MODES: C.PLUG_MODE_BOTH,
     C.PI_PROTOCOLS: ["XEP-0329"],
-    C.PI_DEPENDENCIES: ["XEP-0234", "XEP-0300"],
+    C.PI_DEPENDENCIES: ["XEP-0234", "XEP-0300", "XEP-0106"],
     C.PI_MAIN: "XEP_0329",
     C.PI_HANDLER: "yes",
     C.PI_DESCRIPTION: _(u"""Implementation of File Information Sharing"""),
@@ -394,12 +394,11 @@
 
     def _requestHandler(self, client, iq_elt, root_nodes_cb, files_from_node_cb):
         iq_elt.handled = True
-        owner = jid.JID(iq_elt["from"]).userhostJID()
         node = iq_elt.query.getAttribute("node")
         if not node:
-            d = defer.maybeDeferred(root_nodes_cb, client, iq_elt, owner)
+            d = defer.maybeDeferred(root_nodes_cb, client, iq_elt)
         else:
-            d = defer.maybeDeferred(files_from_node_cb, client, iq_elt, owner, node)
+            d = defer.maybeDeferred(files_from_node_cb, client, iq_elt, node)
         d.addErrback(
             lambda failure_: log.error(
                 _(u"error while retrieving files: {msg}").format(msg=failure_)
@@ -477,7 +476,7 @@
                     _(u"unexpected type: {type}").format(type=node_type)
                 )
 
-    def _getRootNodesCb(self, client, iq_elt, owner):
+    def _getRootNodesCb(self, client, iq_elt):
         peer_jid = jid.JID(iq_elt["from"])
         iq_result_elt = xmlstream.toResponse(iq_elt, "result")
         query_elt = iq_result_elt.addElement((NS_FIS, "query"))
@@ -488,7 +487,7 @@
             directory_elt["name"] = name
         client.send(iq_result_elt)
 
-    def _getFilesFromNodeCb(self, client, iq_elt, owner, node_path):
+    def _getFilesFromNodeCb(self, client, iq_elt, node_path):
         """Main method to retrieve files/directories from a node_path"""
         peer_jid = jid.JID(iq_elt[u"from"])
         try:
@@ -525,9 +524,30 @@
 
     # Component
 
+    def _compParseJids(self, client, iq_elt):
+        """Retrieve peer_jid and owner to use from IQ stanza
+
+        @param iq_elt(domish.Element): IQ stanza of the FIS request
+        @return (tuple[jid.JID, jid.JID]): peer_jid and owner
+        """
+        to_jid = jid.JID(iq_elt['to'])
+        if to_jid.user:
+            user = self.host.plugins['XEP-0106'].unescape(to_jid.user)
+            if u'@' in user:
+                # a full jid is specified
+                owner = jid.JID(user)
+            else:
+                # only user part is specified, we use our own host to build the full jid
+                owner = jid.JID(None, (user, client.host, None))
+        else:
+            owner = jid.JID(iq_elt["from"]).userhostJID()
+
+        peer_jid = jid.JID(iq_elt["from"])
+        return peer_jid, owner
+
     @defer.inlineCallbacks
-    def _compGetRootNodesCb(self, client, iq_elt, owner):
-        peer_jid = jid.JID(iq_elt["from"])
+    def _compGetRootNodesCb(self, client, iq_elt):
+        peer_jid, owner = self._compParseJids(client, iq_elt)
         files_data = yield self.host.memory.getFiles(
             client,
             peer_jid=peer_jid,
@@ -544,15 +564,15 @@
         client.send(iq_result_elt)
 
     @defer.inlineCallbacks
-    def _compGetFilesFromNodeCb(self, client, iq_elt, owner, node_path):
-        """retrieve files from local files repository according to permissions
+    def _compGetFilesFromNodeCb(self, client, iq_elt, node_path):
+        """Retrieve files from local files repository according to permissions
 
         result stanza is then built and sent to requestor
         @trigger XEP-0329_compGetFilesFromNode(client, iq_elt, owner, node_path,
                                                files_data):
             can be used to add data/elements
         """
-        peer_jid = jid.JID(iq_elt["from"])
+        peer_jid, owner = self._compParseJids(client, iq_elt)
         try:
             files_data = yield self.host.memory.getFiles(
                 client, peer_jid=peer_jid, path=node_path, owner=owner