Reformatted code base using black.
This commit is contained in:
parent
56319cbad3
commit
53fdaa9d09
1 changed files with 12604 additions and 8966 deletions
|
|
@ -66,19 +66,21 @@ class XEP_0447:
|
|||
self._m = host.plugins["XEP-0446"]
|
||||
self._http_upload = host.plugins.get("XEP-0363")
|
||||
self._attach = host.plugins["ATTACH"]
|
||||
self._attach.register(
|
||||
self.can_handle_attachment, self.attach, priority=1000
|
||||
)
|
||||
self._attach.register(self.can_handle_attachment, self.attach, priority=1000)
|
||||
self.register_source_handler(
|
||||
self._u.namespace, "url-data", self._u.parse_url_data_elt
|
||||
)
|
||||
host.plugins["DOWNLOAD"].register_download_handler(self._u.namespace, self.download)
|
||||
host.plugins["DOWNLOAD"].register_download_handler(
|
||||
self._u.namespace, self.download
|
||||
)
|
||||
host.trigger.add("message_received", self._message_received_trigger)
|
||||
|
||||
def register_source_handler(
|
||||
self, namespace: str, element_name: str,
|
||||
self,
|
||||
namespace: str,
|
||||
element_name: str,
|
||||
callback: Callable[[domish.Element], Dict[str, Any]],
|
||||
encrypted: bool = False
|
||||
encrypted: bool = False,
|
||||
) -> None:
|
||||
"""Register a handler for file source
|
||||
|
||||
|
|
@ -103,7 +105,7 @@ class XEP_0447:
|
|||
attachment: Dict[str, Any],
|
||||
source: Dict[str, Any],
|
||||
dest_path: Union[Path, str],
|
||||
extra: Optional[Dict[str, Any]] = None
|
||||
extra: Optional[Dict[str, Any]] = None,
|
||||
) -> Tuple[str, defer.Deferred]:
|
||||
# TODO: handle url-data headers
|
||||
if extra is None:
|
||||
|
|
@ -113,10 +115,8 @@ class XEP_0447:
|
|||
except KeyError:
|
||||
raise ValueError(f"{source} has missing URL")
|
||||
|
||||
if extra.get('ignore_tls_errors', False):
|
||||
log.warning(
|
||||
"TLS certificate check disabled, this is highly insecure"
|
||||
)
|
||||
if extra.get("ignore_tls_errors", False):
|
||||
log.warning("TLS certificate check disabled, this is highly insecure")
|
||||
treq_client = treq_client_no_ssl
|
||||
else:
|
||||
treq_client = treq
|
||||
|
|
@ -125,14 +125,14 @@ class XEP_0447:
|
|||
file_size = int(attachment["size"])
|
||||
except (KeyError, ValueError):
|
||||
head_data = await treq_client.head(download_url)
|
||||
file_size = int(head_data.headers.getRawHeaders('content-length')[0])
|
||||
file_size = int(head_data.headers.getRawHeaders("content-length")[0])
|
||||
|
||||
file_obj = stream.SatFile(
|
||||
self.host,
|
||||
client,
|
||||
dest_path,
|
||||
mode="wb",
|
||||
size = file_size,
|
||||
size=file_size,
|
||||
)
|
||||
|
||||
progress_id = file_obj.uid
|
||||
|
|
@ -157,8 +157,7 @@ class XEP_0447:
|
|||
return True
|
||||
|
||||
def get_sources_elt(
|
||||
self,
|
||||
children: Optional[List[domish.Element]] = None
|
||||
self, children: Optional[List[domish.Element]] = None
|
||||
) -> domish.Element:
|
||||
"""Generate <sources> element"""
|
||||
sources_elt = domish.Element((NS_SFS, "sources"))
|
||||
|
|
@ -211,20 +210,13 @@ class XEP_0447:
|
|||
file_sharing_elt.addChild(sources_elt)
|
||||
for source_data in sources:
|
||||
if "url" in source_data:
|
||||
sources_elt.addChild(
|
||||
self._u.get_url_data_elt(**source_data)
|
||||
)
|
||||
sources_elt.addChild(self._u.get_url_data_elt(**source_data))
|
||||
else:
|
||||
raise NotImplementedError(
|
||||
f"source data not implemented: {source_data}"
|
||||
)
|
||||
raise NotImplementedError(f"source data not implemented: {source_data}")
|
||||
|
||||
return file_sharing_elt
|
||||
|
||||
def parse_sources_elt(
|
||||
self,
|
||||
sources_elt: domish.Element
|
||||
) -> List[Dict[str, Any]]:
|
||||
def parse_sources_elt(self, sources_elt: domish.Element) -> List[Dict[str, Any]]:
|
||||
"""Parse <sources/> element
|
||||
|
||||
@param sources_elt: <sources/> element, or a direct parent element
|
||||
|
|
@ -236,7 +228,8 @@ class XEP_0447:
|
|||
sources_elt = next(sources_elt.elements(NS_SFS, "sources"))
|
||||
except StopIteration:
|
||||
raise exceptions.NotFound(
|
||||
f"<sources/> element is missing: {sources_elt.toXml()}")
|
||||
f"<sources/> element is missing: {sources_elt.toXml()}"
|
||||
)
|
||||
sources = []
|
||||
for elt in sources_elt.elements():
|
||||
if not elt.uri:
|
||||
|
|
@ -257,10 +250,7 @@ class XEP_0447:
|
|||
sources.append(source_data)
|
||||
return sources
|
||||
|
||||
def parse_file_sharing_elt(
|
||||
self,
|
||||
file_sharing_elt: domish.Element
|
||||
) -> Dict[str, Any]:
|
||||
def parse_file_sharing_elt(self, file_sharing_elt: domish.Element) -> Dict[str, Any]:
|
||||
"""Parse <file-sharing/> element and return file-sharing data
|
||||
|
||||
@param file_sharing_elt: <file-sharing/> element
|
||||
|
|
@ -269,9 +259,7 @@ class XEP_0447:
|
|||
"""
|
||||
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")
|
||||
)
|
||||
file_sharing_elt = next(file_sharing_elt.elements(NS_SFS, "file-sharing"))
|
||||
except StopIteration:
|
||||
raise exceptions.NotFound
|
||||
try:
|
||||
|
|
@ -289,10 +277,7 @@ class XEP_0447:
|
|||
return data
|
||||
|
||||
def _add_file_sharing_attachments(
|
||||
self,
|
||||
client: SatXMPPEntity,
|
||||
message_elt: domish.Element,
|
||||
data: Dict[str, Any]
|
||||
self, client: SatXMPPEntity, message_elt: domish.Element, 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, for now
|
||||
|
|
@ -302,15 +287,14 @@ class XEP_0447:
|
|||
attachment = self.parse_file_sharing_elt(message_elt)
|
||||
|
||||
if any(
|
||||
s.get(C.MESS_KEY_ENCRYPTED, False)
|
||||
for s in attachment["sources"]
|
||||
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.KEY_ATTACHMENTS, [])
|
||||
attachments = data["extra"].setdefault(C.KEY_ATTACHMENTS, [])
|
||||
attachments.append(attachment)
|
||||
|
||||
return data
|
||||
|
|
@ -319,7 +303,7 @@ class XEP_0447:
|
|||
# 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.KEY_ATTACHMENTS]
|
||||
if not data['message'] or data['message'] == {'': ''}:
|
||||
if not data["message"] or data["message"] == {"": ""}:
|
||||
extra_attachments = attachments[1:]
|
||||
del attachments[1:]
|
||||
else:
|
||||
|
|
@ -346,23 +330,25 @@ class XEP_0447:
|
|||
size=attachment.get("size"),
|
||||
desc=attachment.get("desc"),
|
||||
media_type=attachment.get("media_type"),
|
||||
file_hash=file_hash
|
||||
file_hash=file_hash,
|
||||
)
|
||||
data["xml"].addChild(file_sharing_elt)
|
||||
|
||||
for attachment in extra_attachments:
|
||||
# we send all remaining attachment in a separate message
|
||||
await client.sendMessage(
|
||||
to_jid=data['to'],
|
||||
message={'': ''},
|
||||
subject=data['subject'],
|
||||
mess_type=data['type'],
|
||||
to_jid=data["to"],
|
||||
message={"": ""},
|
||||
subject=data["subject"],
|
||||
mess_type=data["type"],
|
||||
extra={C.KEY_ATTACHMENTS: [attachment]},
|
||||
)
|
||||
|
||||
if ((not data['extra']
|
||||
and (not data['message'] or data['message'] == {'': ''})
|
||||
and not data['subject'])):
|
||||
if (
|
||||
not data["extra"]
|
||||
and (not data["message"] or data["message"] == {"": ""})
|
||||
and not data["subject"]
|
||||
):
|
||||
# nothing left to send, we can cancel the message
|
||||
raise exceptions.CancelError("Cancelled by XEP_0447 attachment handling")
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue