diff sat/plugins/plugin_xep_0447.py @ 4023:78b5f356900c

component AP gateway: handle attachments
author Goffi <goffi@goffi.org>
date Thu, 23 Mar 2023 15:42:21 +0100
parents 0ff265725489
children 524856bd7b19
line wrap: on
line diff
--- a/sat/plugins/plugin_xep_0447.py	Thu Mar 23 15:39:48 2023 +0100
+++ b/sat/plugins/plugin_xep_0447.py	Thu Mar 23 15:42:21 2023 +0100
@@ -267,7 +267,7 @@
         @return: file-sharing data. It a dict whose keys correspond to
             [get_file_sharing_elt] parameters
         """
-        if file_sharing_elt.name != "file-sharing":
+        if file_sharing_elt.name != "file-sharing" or file_sharing_elt.uri != NS_SFS:
             try:
                 file_sharing_elt = next(
                     file_sharing_elt.elements(NS_SFS, "file-sharing")
@@ -295,31 +295,30 @@
             data: Dict[str, Any]
     ) -> Dict[str, Any]:
         """Check <message> for a shared file, and add it as an attachment"""
-        # XXX: XEP-0447 doesn't support several attachments in a single message, thus only
-        #   one attachment can be added
-        try:
+        # XXX: XEP-0447 doesn't support several attachments in a single message, for now
+        #   however that should be fixed in future version, and so we accept several
+        #   <file-sharing> element in a message.
+        for file_sharing_elt in message_elt.elements(NS_SFS, "file-sharing"):
             attachment = self.parse_file_sharing_elt(message_elt)
-        except exceptions.NotFound:
-            return data
 
-        if any(
-                s.get(C.MESS_KEY_ENCRYPTED, False)
-                for s in attachment["sources"]
-        ) and client.encryption.isEncrypted(data):
-            # we don't add the encrypted flag if the message itself is not encrypted,
-            # because the decryption key is part of the link, so sending it over
-            # unencrypted channel is like having no encryption at all.
-            attachment[C.MESS_KEY_ENCRYPTED] = True
+            if any(
+                    s.get(C.MESS_KEY_ENCRYPTED, False)
+                    for s in attachment["sources"]
+            ) and client.encryption.isEncrypted(data):
+                # we don't add the encrypted flag if the message itself is not encrypted,
+                # because the decryption key is part of the link, so sending it over
+                # unencrypted channel is like having no encryption at all.
+                attachment[C.MESS_KEY_ENCRYPTED] = True
 
-        attachments = data['extra'].setdefault(C.MESS_KEY_ATTACHMENTS, [])
-        attachments.append(attachment)
+            attachments = data['extra'].setdefault(C.KEY_ATTACHMENTS, [])
+            attachments.append(attachment)
 
         return data
 
     async def attach(self, client, data):
         # XXX: for now, XEP-0447 only allow to send one file per <message/>, thus we need
         #   to send each file in a separate message
-        attachments = data["extra"][C.MESS_KEY_ATTACHMENTS]
+        attachments = data["extra"][C.KEY_ATTACHMENTS]
         if not data['message'] or data['message'] == {'': ''}:
             extra_attachments = attachments[1:]
             del attachments[1:]
@@ -327,7 +326,7 @@
             # we have a message, we must send first attachment separately
             extra_attachments = attachments[:]
             attachments.clear()
-            del data["extra"][C.MESS_KEY_ATTACHMENTS]
+            del data["extra"][C.KEY_ATTACHMENTS]
 
         if attachments:
             if len(attachments) > 1:
@@ -343,8 +342,10 @@
                     file_hash = None
                 file_sharing_elt = self.get_file_sharing_elt(
                     [{"url": attachment["url"]}],
-                    name=attachment["name"],
-                    size=attachment["size"],
+                    name=attachment.get("name"),
+                    size=attachment.get("size"),
+                    desc=attachment.get("desc"),
+                    media_type=attachment.get("media_type"),
                     file_hash=file_hash
                 )
                 data["xml"].addChild(file_sharing_elt)
@@ -356,7 +357,7 @@
                 message={'': ''},
                 subject=data['subject'],
                 mess_type=data['type'],
-                extra={C.MESS_KEY_ATTACHMENTS: [attachment]},
+                extra={C.KEY_ATTACHMENTS: [attachment]},
             )
 
         if ((not data['extra']