Mercurial > libervia-backend
diff libervia/backend/plugins/plugin_xep_0234.py @ 4270:0d7bb4df2343
Reformatted code base using black.
author | Goffi <goffi@goffi.org> |
---|---|
date | Wed, 19 Jun 2024 18:44:57 +0200 |
parents | 79c8a70e1813 |
children | 111dce64dcb5 |
line wrap: on
line diff
--- a/libervia/backend/plugins/plugin_xep_0234.py Tue Jun 18 12:06:45 2024 +0200 +++ b/libervia/backend/plugins/plugin_xep_0234.py Wed Jun 19 18:44:57 2024 +0200 @@ -63,8 +63,15 @@ # TODO: use a Pydantic model for extra EXTRA_ALLOWED = { - "path", "namespace", "file_desc", "file_hash", "hash_algo", "webrtc", "call_data", - "size", "media_type" + "path", + "namespace", + "file_desc", + "file_hash", + "hash_algo", + "webrtc", + "call_data", + "size", + "media_type", } Range = namedtuple("Range", ("offset", "length")) @@ -125,9 +132,21 @@ # generic methods def build_file_element( - self, client, name=None, file_hash=None, hash_algo=None, size=None, - mime_type=None, desc=None, modified=None, transfer_range=None, path=None, - namespace=None, file_elt=None, **kwargs): + self, + client, + name=None, + file_hash=None, + hash_algo=None, + size=None, + mime_type=None, + desc=None, + modified=None, + transfer_range=None, + path=None, + namespace=None, + file_elt=None, + **kwargs, + ): """Generate a <file> element with available metadata @param file_hash(unicode, None): hash of the file @@ -183,7 +202,8 @@ elif hash_algo is not None: file_elt.addChild(self._hash.build_hash_used_elt(hash_algo)) self.host.trigger.point( - "XEP-0234_buildFileElement", client, file_elt, extra_args=kwargs) + "XEP-0234_buildFileElement", client, file_elt, extra_args=kwargs + ) if kwargs: for kw in kwargs: log.debug("ignored keyword: {}".format(kw)) @@ -209,11 +229,11 @@ async def parse_file_element( self, client: SatXMPPEntity, - file_elt: domish.Element|None, + file_elt: domish.Element | None, file_data: dict | None = None, given: bool = False, parent_elt: domish.Element | None = None, - keep_empty_range: bool = False + keep_empty_range: bool = False, ) -> dict: """Parse a <file> element and updates file dictionary accordingly. @@ -299,7 +319,7 @@ elif name == "..": # we don't want to go to parent dir when joining to a path file_data["name"] = "--" - elif "/" in name or "\\" in name: + elif "/" in name or "\\" in name: file_data["name"] = regex.path_escape(name) try: @@ -310,9 +330,7 @@ pass try: - file_data["size"] = int( - str(next(file_elt.elements(NS_JINGLE_FT, "size"))) - ) + file_data["size"] = int(str(next(file_elt.elements(NS_JINGLE_FT, "size")))) except StopIteration: pass @@ -359,14 +377,16 @@ ) -> defer.Deferred[str]: client = self.host.get_client(profile) extra = data_format.deserialise(extra_s) - d = defer.ensureDeferred(self.file_send( - client, - jid.JID(peer_jid_s), - filepath, - name or None, - file_desc or None, - extra, - )) + d = defer.ensureDeferred( + self.file_send( + client, + jid.JID(peer_jid_s), + filepath, + name or None, + file_desc or None, + extra, + ) + ) d.addCallback(data_format.serialise) return d @@ -375,9 +395,9 @@ client: SatXMPPEntity, peer_jid: jid.JID, filepath: str, - name: str|None, - file_desc: str|None = None, - extra: dict|None = None + name: str | None, + file_desc: str | None = None, + extra: dict | None = None, ) -> dict: """Send a file using jingle file transfer @@ -406,39 +426,48 @@ } await self.host.trigger.async_point( - "XEP-0234_file_jingle_send", - client, peer_jid, content + "XEP-0234_file_jingle_send", client, peer_jid, content ) session_id = await self._j.initiate( - client, - peer_jid, - [content], - encrypted = encrypted + client, peer_jid, [content], encrypted=encrypted ) progress_id = await progress_id_d - return { - "progress": progress_id, - "session_id": session_id - } + return {"progress": progress_id, "session_id": session_id} def _file_jingle_request( - self, peer_jid, filepath, name="", file_hash="", hash_algo="", extra=None, - profile=C.PROF_KEY_NONE): + self, + peer_jid, + filepath, + name="", + file_hash="", + hash_algo="", + extra=None, + profile=C.PROF_KEY_NONE, + ): client = self.host.get_client(profile) - return defer.ensureDeferred(self.file_jingle_request( - client, - jid.JID(peer_jid), - filepath, - name or None, - file_hash or None, - hash_algo or None, - extra or None, - )) + return defer.ensureDeferred( + self.file_jingle_request( + client, + jid.JID(peer_jid), + filepath, + name or None, + file_hash or None, + hash_algo or None, + extra or None, + ) + ) async def file_jingle_request( - self, client, peer_jid, filepath, name=None, file_hash=None, hash_algo=None, - extra=None): + self, + client, + peer_jid, + filepath, + name=None, + file_hash=None, + hash_algo=None, + extra=None, + ): """Request a file using jingle file transfer @param peer_jid(jid.JID): destinee jid @@ -480,23 +509,20 @@ # jingle callbacks def _get_confirm_msg( - self, - client: SatXMPPEntity, - peer_jid: jid.JID, - file_data: dict + self, client: SatXMPPEntity, peer_jid: jid.JID, file_data: dict ) -> tuple[bool, str, str]: """Get confirmation message to display to user. This is the message to show when a file sending request is received.""" - file_name = file_data.get('name') - file_size = file_data.get('size') + file_name = file_data.get("name") + file_size = file_data.get("size") if file_name: file_name_msg = D_('wants to send you the file "{file_name}"').format( file_name=file_name ) else: - file_name_msg = D_('wants to send you an unnamed file') + file_name_msg = D_("wants to send you an unnamed file") if file_size is not None: file_size_msg = D_("which has a size of {file_size_human}").format( @@ -505,7 +531,7 @@ else: file_size_msg = D_("which has an unknown size") - file_description = file_data.get('desc') + file_description = file_data.get("desc") if file_description: description_msg = " Description: {}.".format(file_description) else: @@ -516,23 +542,30 @@ confirm_msg = D_( "Somebody not in your contact list ({peer_jid}) {file_name_msg} {file_size_msg}.{description_msg} " "Accepting this could leak your presence and possibly your IP address. Do you accept?" - ).format(peer_jid=peer_jid, file_name_msg=file_name_msg, file_size_msg=file_size_msg, description_msg=description_msg) + ).format( + peer_jid=peer_jid, + file_name_msg=file_name_msg, + file_size_msg=file_size_msg, + description_msg=description_msg, + ) confirm_title = D_("File sent from an unknown contact") else: is_in_roster = True confirm_msg = D_( "{peer_jid} {file_name_msg} {file_size_msg}.{description_msg} Do you " "accept?" - ).format(peer_jid=peer_jid, file_name_msg=file_name_msg, file_size_msg=file_size_msg, description_msg=description_msg) + ).format( + peer_jid=peer_jid, + file_name_msg=file_name_msg, + file_size_msg=file_size_msg, + description_msg=description_msg, + ) confirm_title = D_("File Proposed") return (is_in_roster, confirm_msg, confirm_title) async def jingle_preflight( - self, - client: SatXMPPEntity, - session: dict, - description_elt: domish.Element + self, client: SatXMPPEntity, session: dict, description_elt: domish.Element ) -> None: """Perform preflight checks for an incoming call session. @@ -568,7 +601,7 @@ "type": action_type, "session_id": session_id, "from_jid": peer_jid.full(), - "file_data": file_data + "file_data": file_data, } action_extra["subtype"] = C.META_TYPE_FILE accepted = await xml_tools.defer_confirm( @@ -576,7 +609,7 @@ confirm_msg, confirm_title, profile=client.profile, - action_extra=action_extra + action_extra=action_extra, ) if accepted: session["pre_accepted"] = True @@ -587,15 +620,12 @@ client: SatXMPPEntity, session: dict, info_type: str, - info_data: dict|None = None + info_data: dict | None = None, ) -> None: pass async def jingle_preflight_cancel( - self, - client: SatXMPPEntity, - session: dict, - cancel_error: exceptions.CancelError + self, client: SatXMPPEntity, session: dict, cancel_error: exceptions.CancelError ) -> None: pass @@ -626,7 +656,8 @@ application_data["file_path"] = filepath file_data = application_data["file_data"] = {} desc_elt = self.jingle_description_elt( - client, session, content_name, filepath, name, extra, progress_id_d) + client, session, content_name, filepath, name, extra, progress_id_d + ) file_elt = desc_elt.addElement("file") if content_data["senders"] == self._j.ROLE_INITIATOR: @@ -650,7 +681,8 @@ if "path" in extra: file_data["path"] = extra["path"] self.build_file_element_from_dict( - client, file_data, file_elt=file_elt, file_hash="") + client, file_data, file_elt=file_elt, file_hash="" + ) else: # we request a file file_hash = extra.pop("file_hash", "") @@ -740,7 +772,7 @@ content_data: dict, content_name: str, file_data: dict, - file_elt: domish.Element + file_elt: domish.Element, ) -> bool: """parse file_elt, and handle user permission/file opening""" transport_data = content_data["transport_data"] @@ -781,7 +813,7 @@ confirm_msg, confirm_title, profile=client.profile, - action_extra=action_extra + action_extra=action_extra, ) else: @@ -806,7 +838,10 @@ if confirmed: await self.host.trigger.async_point( "XEP-0234_file_receiving_request_conf", - client, session, content_data, file_elt + client, + session, + content_data, + file_elt, ) args = [client, session, content_name, content_data] finished_d.addCallbacks( @@ -870,7 +905,8 @@ progress_id, C.PROGRESS_ERROR_FAILED, client.profile ) await self._j.terminate( - client, self._j.REASON_FAILED_APPLICATION, session) + client, self._j.REASON_FAILED_APPLICATION, session + ) raise e else: # we are sending the file @@ -889,10 +925,11 @@ finished_d = content_data["finished_d"] = defer.Deferred() args = [client, session, content_name, content_data] - finished_d.addCallbacks(self._finished_cb, self._finished_eb, args, None, args) + finished_d.addCallbacks( + self._finished_cb, self._finished_eb, args, None, args + ) await self.host.trigger.async_point( - "XEP-0234_jingle_handler", - client, session, content_data, desc_elt + "XEP-0234_jingle_handler", client, session, content_data, desc_elt ) else: log.warning("FIXME: unmanaged action {}".format(action)) @@ -959,9 +996,7 @@ reason = f"{reason} - {reason_elt.text}" else: reason = C.PROGRESS_ERROR_FAILED - self.host.bridge.progress_error( - progress_id, reason, client.profile - ) + self.host.bridge.progress_error(progress_id, reason, client.profile) def _send_check_sum(self, client, session, content_name, content_data): """Send the session-info with the hash checksum"""