changeset 2522:95c31756944c

component file sharing, plugin XEP-0231: thumbnail are now returned by component using Bits of Binary: thumbnails are saved in common cache, and sent blindly on request. There is no access check for thumbnails yet, which is a security/privacy issue, but mitigated by the fact that content id must be known to retrieve the file. Proper access check is planed.
author Goffi <goffi@goffi.org>
date Wed, 14 Mar 2018 08:14:32 +0100
parents 327bbbe793ce
children 21d43eab3fb9
files src/plugins/plugin_comp_file_sharing.py src/plugins/plugin_xep_0231.py
diffstat 2 files changed, 85 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/src/plugins/plugin_comp_file_sharing.py	Wed Mar 14 08:11:20 2018 +0100
+++ b/src/plugins/plugin_comp_file_sharing.py	Wed Mar 14 08:14:32 2018 +0100
@@ -36,7 +36,7 @@
     C.PI_MODES: [C.PLUG_MODE_COMPONENT],
     C.PI_TYPE: C.PLUG_TYPE_ENTRY_POINT,
     C.PI_PROTOCOLS: [],
-    C.PI_DEPENDENCIES: ["FILE", "XEP-0234", "XEP-0260", "XEP-0261", "XEP-0264", "XEP-0329"],
+    C.PI_DEPENDENCIES: ["FILE", "XEP-0231", "XEP-0234", "XEP-0260", "XEP-0261", "XEP-0264", "XEP-0329"],
     C.PI_RECOMMENDATIONS: [],
     C.PI_MAIN: "FileSharing",
     C.PI_HANDLER: "no",
--- a/src/plugins/plugin_xep_0231.py	Wed Mar 14 08:11:20 2018 +0100
+++ b/src/plugins/plugin_xep_0231.py	Wed Mar 14 08:14:32 2018 +0100
@@ -28,22 +28,26 @@
 from twisted.python import failure
 from twisted.words.protocols.jabber import xmlstream
 from twisted.words.protocols.jabber import jid
+from twisted.words.protocols.jabber import error as jabber_error
+from twisted.internet import defer
 from functools import partial
 import base64
+import time
 
 
 PLUGIN_INFO = {
     C.PI_NAME: "Bits of Binary",
     C.PI_IMPORT_NAME: "XEP-0231",
     C.PI_TYPE: "XEP",
+    C.PI_MODES: C.PLUG_MODE_BOTH,
     C.PI_PROTOCOLS: ["XEP-0231"],
-    C.PI_DEPENDENCIES: ["XEP-0071"],
     C.PI_MAIN: "XEP_0231",
     C.PI_HANDLER: "yes",
     C.PI_DESCRIPTION: _("""Implementation of bits of binary (used for small images/files)""")
 }
 
 NS_BOB = u'urn:xmpp:bob'
+IQ_BOB_REQUEST = C.IQ_GET + '/data[@xmlns="' + NS_BOB + '"]'
 
 
 class XEP_0231(object):
@@ -51,7 +55,12 @@
     def __init__(self, host):
         log.info(_(u"plugin Bits of Binary initialization"))
         self.host = host
+        host.registerNamespace('bob', NS_BOB)
         host.trigger.add("xhtml_post_treat", self.XHTMLTrigger)
+        host.bridge.addMethod("bobGetFile", ".plugin",
+                              in_sign='sss', out_sign='s',
+                              method=self._getFile,
+                              async=True)
 
     def dumpData(self, cache, data_elt, cid):
         """save file encoded in data_elt to cache
@@ -83,7 +92,7 @@
         return file_path
 
     def getHandler(self, client):
-        return XEP_0231_handler()
+        return XEP_0231_handler(self)
 
     def _requestCb(self, iq_elt, cache, cid):
         for data_elt in iq_elt.elements(NS_BOB, u'data'):
@@ -109,6 +118,7 @@
         @param cid(unicode): content id
         @param cache(memory.cache.Cache, None): cache to use
             client.cache will be used if None
+        @return D(unicode): path to file with data
         """
         if cache is None:
             cache = client.cache
@@ -119,6 +129,7 @@
         d = iq_elt.send()
         d.addCallback(self._requestCb, cache, cid)
         d.addErrback(self._requestEb)
+        return d
 
     def _setImgEltSrc(self, path, img_elt):
         img_elt[u'src'] = u'file://{}'.format(path)
@@ -130,7 +141,7 @@
                 cid = source[4:]
                 file_path = client.cache.getFilePath(cid)
                 if file_path is not None:
-                    # image is in cache, we change change the url
+                    # image is in cache, we change the url
                     img_elt[u'src'] = u'file://{}'.format(file_path)
                     continue
                 else:
@@ -147,10 +158,80 @@
                         d.addCallback(partial(self._setImgEltSrc, img_elt=img_elt))
                         treat_d.addCallback(lambda dummy: d)
 
+    def onComponentRequest(self, iq_elt, client):
+        """cache data is retrieve from common cache for components"""
+        # FIXME: this is a security/privacy issue as no access check is done
+        #        but this is mitigated by the fact that the cid must be known.
+        #        An access check should be implemented though.
+
+        iq_elt.handled = True
+        data_elt = next(iq_elt.elements(NS_BOB, 'data'))
+        try:
+            cid = data_elt[u'cid']
+        except KeyError:
+            error_elt = jabber_error.StanzaError('not-acceptable').toResponse(iq_elt)
+            client.send(error_elt)
+            return
+
+        metadata = self.host.common_cache.getMetadata(cid)
+        if metadata is None:
+            error_elt = jabber_error.StanzaError('item-not-found').toResponse(iq_elt)
+            client.send(error_elt)
+            return
+
+        with open(metadata['path']) as f:
+            data = f.read()
+
+        result_elt = xmlstream.toResponse(iq_elt, 'result')
+        data_elt = result_elt.addElement((NS_BOB, 'data'), content = data.encode('base64'))
+        data_elt[u'cid'] = cid
+        data_elt[u'type'] = metadata[u'mime_type']
+        data_elt[u'max-age'] = unicode(int(max(0, metadata['eol'] - time.time())))
+        client.send(result_elt)
+
+    def _getFile(self, peer_jid_s, cid, profile):
+        peer_jid = jid.JID(peer_jid_s)
+        assert cid
+        client = self.host.getClient(profile)
+        return self.getFile(client, peer_jid, cid)
+
+    def getFile(self, client, peer_jid, cid, parent_elt=None):
+        """Retrieve a file from it's content-id
+
+        @param peer_jid(jid.JID): jid of the entity offering the data
+        @param cid(unicode): content-id of file data
+        @param parent_elt(domish.Element, None): if file is not in cache,
+            data will be looked after in children of this elements.
+            None to ignore
+        @return D(unicode): path to cached data
+        """
+        file_path = client.cache.getFilePath(cid)
+        if file_path is not None:
+            # file is in cache
+            return defer.succeed(file_path)
+        else:
+            # file not in cache, is it given locally?
+            if parent_elt is not None:
+                for data_elt in parent_elt.elements(NS_BOB, u'data'):
+                    if data_elt.getAttribute('cid') == cid:
+                        return defer.succeed(self.dumpData(client.cache, data_elt, cid))
+
+            # cid not found locally, we need to request it
+            # so we use the deferred
+            return self.requestData(client, peer_jid, cid)
+
 
 class XEP_0231_handler(xmlstream.XMPPHandler):
     implements(iwokkel.IDisco)
 
+    def __init__(self, plugin_parent):
+        self.plugin_parent = plugin_parent
+        self.host = plugin_parent.host
+
+    def connectionInitialized(self):
+        if self.parent.is_component:
+            self.xmlstream.addObserver(IQ_BOB_REQUEST, self.plugin_parent.onComponentRequest, client=self.parent)
+
     def getDiscoInfo(self, requestor, target, nodeIdentifier=''):
         return [disco.DiscoFeature(NS_BOB)]