plugin attach, aesgcm: attachments refactoring:

attachment handling has been simplified, and now use a "register" method similar as the
ones used for download or upload.
A default method (for unencrypted messages) will try a simple upload and will copy the
links to body.

AESGCM plugin has been adapted to be used for encrypted files. If more than one file is
sent with AESGCM plugin, they will be split in several messages as current de-facto
standard (OMEMO media sharing) doesn't support several files per message.
This commit is contained in:
Goffi 2020-03-18 20:25:02 +01:00
commit 06516eeb1d

View file

@ -18,10 +18,14 @@
from functools import partial
from pathlib import Path
from collections import namedtuple
from twisted.internet import defer
from sat.core.i18n import _
from sat.core import exceptions
from sat.core.constants import Const as C
from sat.core.log import getLogger
from sat.tools import utils
log = getLogger(__name__)
@ -37,6 +41,9 @@ PLUGIN_INFO = {
}
AttachmentHandler = namedtuple('AttachmentHandler', ['can_handle', 'attach', 'priority'])
class AttachPlugin:
def __init__(self, host):
@ -44,15 +51,81 @@ class AttachPlugin:
self.host = host
self._u = host.plugins["UPLOAD"]
host.trigger.add("sendMessage", self._sendMessageTrigger)
self._attachments_handlers = {'clear': [], 'encrypted': []}
self.register(self.defaultCanHandle, self.defaultAttach, False, -1000)
def register(self, can_handle, attach, encrypted=False, priority=0):
"""Register an attachments handler
@param can_handle(callable, coroutine, Deferred): a method which must return True
if this plugin can handle the upload, otherwise next ones will be tried.
This method will get client and mess_data as arguments, before the XML is
generated
@param attach(callable, coroutine, Deferred): attach the file
this method will get client and mess_data as arguments, after XML is
generated. Upload operation must be handled
hint: "UPLOAD" plugin can be used
@param encrypted(bool): True if the handler manages encrypted files
A handler can be registered twice if it handle both encrypted and clear
attachments
@param priority(int): priority of this handler, handler with higher priority will
be tried first
"""
handler = AttachmentHandler(can_handle, attach, priority)
handlers = (
self._attachments_handlers['encrypted']
if encrypted else self._attachments_handlers['clear']
)
if handler in handlers:
raise exceptions.InternalError(
'Attachment handler has been registered twice, this should never happen'
)
handlers.append(handler)
handlers.sort(key=lambda h: h.priority, reverse=True)
log.debug(f"new attachments handler: {handler}")
async def attachFiles(self, client, data):
if client.encryption.isEncryptionRequested(data):
handlers = self._attachments_handlers['encrypted']
else:
handlers = self._attachments_handlers['clear']
for handler in handlers:
can_handle = await utils.asDeferred(handler.can_handle, client, data)
if can_handle:
break
else:
raise exceptions.NotFound(
_("No plugin can handle attachment with {destinee}").format(
destinee = data['to']
))
await utils.asDeferred(handler.attach, client, data)
def _attachFiles(self, client, data):
# TODO: handle xhtml-im
body_elt = next(data["xml"].elements((C.NS_CLIENT, "body")))
for attachment in data["extra"][C.MESS_KEY_ATTACHMENTS]:
body_elt.addContent(f'\n{attachment["url"]}')
return data
async def uploadFiles(self, client, data):
async def uploadFiles(self, client, data, upload_cb=None):
"""Upload file, and update attachments
invalid attachments will be removed
@param client:
@param data(dict): message data
@param upload_cb(coroutine, Deferred, None): method to use for upload
if None, upload method from UPLOAD plugin will be used.
Otherwise, following kwargs will be use with the cb:
- client
- filepath
- filename
- options
the method must return a tuple similar to UPLOAD plugin's upload method,
it must contain:
- progress_id
- a deferred which fire download URL
"""
if upload_cb is None:
upload_cb = self._u.upload
uploads_d = []
to_delete = []
attachments = data["extra"]["attachments"]
@ -79,23 +152,18 @@ class AttachPlugin:
name = attachment["name"] = path.name
options = {}
progress_id = attachment.get("progress_id")
progress_id = attachment.pop("progress_id", None)
if progress_id:
options["progress_id"] = attachment["progress_id"]
options["progress_id"] = progress_id
check_certificate = self.host.memory.getParamA(
"check_certificate", "Connection", profile_key=client.profile)
if not check_certificate:
options['ignore_tls_errors'] = True
log.warning(
_("certificate check disabled for upload, this is dangerous!"))
if client.encryption.isEncryptionRequested(data):
# FIXME: we should not use implementation specific value here
# but for now it's the only file encryption method available with
# with upload.
options['encryption'] = C.ENC_AES_GCM
__, upload_d = await self._u.upload(
client,
__, upload_d = await upload_cb(
client=client,
filepath=path,
filename=name,
options=options,
@ -118,12 +186,26 @@ class AttachPlugin:
return data
def _uploadFiles(self, client, data):
return defer.ensureDeferred(self.uploadFiles(client, data))
def _attachFiles(self, client, data):
return defer.ensureDeferred(self.attachFiles(client, data))
def _sendMessageTrigger(
self, client, mess_data, pre_xml_treatments, post_xml_treatments):
if mess_data['extra'].get(C.MESS_KEY_ATTACHMENTS):
pre_xml_treatments.addCallback(partial(self._uploadFiles, client))
post_xml_treatments.addCallback(partial(self._attachFiles, client))
return True
async def defaultCanHandle(self, client, data):
return True
async def defaultAttach(self, client, data):
await self.uploadFiles(client, data)
# TODO: handle xhtml-im
body_elt = next(data["xml"].elements((C.NS_CLIENT, "body")))
attachments = data["extra"][C.MESS_KEY_ATTACHMENTS]
if attachments:
body_links = '\n'.join(a['url'] for a in attachments)
if str(body_elt).strip():
# if there is already a body, we add a line feed before the first link
body_elt.addContent('\n')
body_elt.addContent(body_links)