Mercurial > libervia-backend
diff libervia/backend/plugins/plugin_sec_otr.py @ 4270:0d7bb4df2343
Reformatted code base using black.
author | Goffi <goffi@goffi.org> |
---|---|
date | Wed, 19 Jun 2024 18:44:57 +0200 |
parents | 4b842c1fb686 |
children |
line wrap: on
line diff
--- a/libervia/backend/plugins/plugin_sec_otr.py Tue Jun 18 12:06:45 2024 +0200 +++ b/libervia/backend/plugins/plugin_sec_otr.py Wed Jun 19 18:44:57 2024 +0200 @@ -109,7 +109,7 @@ message data when an encrypted message is going to be sent """ assert isinstance(self.peer, jid.JID) - msg = msg_str.decode('utf-8') + msg = msg_str.decode("utf-8") client = self.user.client log.debug("injecting encrypted message to {to}".format(to=self.peer)) if appdata is None: @@ -124,11 +124,11 @@ "timestamp": time.time(), } client.generate_message_xml(mess_data) - xml = mess_data['xml'] + xml = mess_data["xml"] self._p_carbons.set_private(xml) - self._p_hints.add_hint_elements(xml, [ - self._p_hints.HINT_NO_COPY, - self._p_hints.HINT_NO_PERMANENT_STORE]) + self._p_hints.add_hint_elements( + xml, [self._p_hints.HINT_NO_COPY, self._p_hints.HINT_NO_PERMANENT_STORE] + ) client.send(mess_data["xml"]) else: message_elt = appdata["xml"] @@ -153,11 +153,10 @@ trusted = self.getCurrentTrust() if trusted is None: return False - elif trusted == 'trusted': + elif trusted == "trusted": return True else: - log.error("Unexpected getCurrentTrust() value: {value}".format( - value=trusted)) + log.error("Unexpected getCurrentTrust() value: {value}".format(value=trusted)) return False def set_state(self, state): @@ -291,9 +290,7 @@ def start_context(self, other_jid): assert isinstance(other_jid, jid.JID) - context = self.contexts.setdefault( - other_jid, Context(self, other_jid) - ) + context = self.contexts.setdefault(other_jid, Context(self, other_jid)) return context def get_context_for_user(self, other): @@ -314,7 +311,9 @@ ) # Â FIXME: OTR should not be skipped per profile, this need to be refactored self._p_hints = host.plugins["XEP-0334"] self._p_carbons = host.plugins["XEP-0280"] - host.trigger.add("message_received", self.message_received_trigger, priority=100000) + host.trigger.add( + "message_received", self.message_received_trigger, priority=100000 + ) host.trigger.add("sendMessage", self.send_message_trigger, priority=100000) host.trigger.add("send_message_data", self._send_message_data_trigger) host.bridge.add_method( @@ -374,11 +373,9 @@ yield client._otr_data.load() encrypted_priv_key = client._otr_data.get(PRIVATE_KEY, None) if encrypted_priv_key is not None: - priv_key = self.host.memory.decrypt_value( - encrypted_priv_key, client.profile - ) + priv_key = self.host.memory.decrypt_value(encrypted_priv_key, client.profile) ctxMng.account.privkey = potr.crypt.PK.parsePrivateKey( - unhexlify(priv_key.encode('utf-8')) + unhexlify(priv_key.encode("utf-8")) )[0] else: ctxMng.account.privkey = None @@ -405,7 +402,7 @@ entity_jid.resource = self.host.memory.main_resource_get( client, entity_jid ) # FIXME: temporary and unsecure, must be changed when frontends - # are refactored + # are refactored ctxMng = client._otr_context_manager otrctx = ctxMng.get_context_for_user(entity_jid) priv_key = ctxMng.account.privkey @@ -517,15 +514,18 @@ @param to_jid(jid.JID): jid to start encrypted session with """ encrypted_session = client.encryption.getSession(to_jid.userhostJID()) - if encrypted_session and encrypted_session['plugin'].namespace != NS_OTR: - raise exceptions.ConflictError(_( - "Can't start an OTR session, there is already an encrypted session " - "with {name}").format(name=encrypted_session['plugin'].name)) + if encrypted_session and encrypted_session["plugin"].namespace != NS_OTR: + raise exceptions.ConflictError( + _( + "Can't start an OTR session, there is already an encrypted session " + "with {name}" + ).format(name=encrypted_session["plugin"].name) + ) if not to_jid.resource: to_jid.resource = self.host.memory.main_resource_get( client, to_jid ) # FIXME: temporary and unsecure, must be changed when frontends - # are refactored + # are refactored otrctx = client._otr_context_manager.get_context_for_user(to_jid) query = otrctx.sendMessage(0, b"?OTRv?") otrctx.inject(query) @@ -551,7 +551,7 @@ to_jid.resource = self.host.memory.main_resource_get( client, to_jid ) # FIXME: temporary and unsecure, must be changed when frontends - # are refactored + # are refactored otrctx = client._otr_context_manager.get_context_for_user(to_jid) otrctx.disconnect() return {} @@ -588,7 +588,7 @@ to_jid.resource = self.host.memory.main_resource_get( client, to_jid ) # FIXME: temporary and unsecure, must be changed when frontends - # are refactored + # are refactored except KeyError: log.error(_("jid key is not present !")) return defer.fail(exceptions.DataError) @@ -606,7 +606,7 @@ context.disconnect() ctxMng.account.privkey = None ctxMng.account.getPrivkey() # as account.privkey is None, getPrivkey - # will generate a new key, and save it + # will generate a new key, and save it return { "xmlui": xml_tools.note( D_("Your private key has been dropped") @@ -630,8 +630,8 @@ otrctx = client._otr_context_manager.get_context_for_user(from_jid) try: - message = ( - next(iter(data["message"].values())) + message = next( + iter(data["message"].values()) ) # FIXME: Q&D fix for message refactoring, message is now a dict res = otrctx.receiveMessage(message.encode("utf-8")) except (potr.context.UnencryptedMessage, potr.context.NotOTRMessage): @@ -642,8 +642,9 @@ encrypted = False if otrctx.state == potr.context.STATE_ENCRYPTED: log.warning( - "Received unencrypted message in an encrypted context (from {jid})" - .format(jid=from_jid.full()) + "Received unencrypted message in an encrypted context (from {jid})".format( + jid=from_jid.full() + ) ) feedback = ( @@ -692,8 +693,8 @@ pass # TODO: add skip history as an option, but by default we don't skip it # data[u'history'] = C.HISTORY_SKIP # we send the decrypted message to - # frontends, but we don't want it in - # history + # frontends, but we don't want it in + # history else: raise failure.Failure( exceptions.CancelError("Cancelled by OTR") @@ -737,7 +738,7 @@ if message_elt.getAttribute("type") == C.MESS_TYPE_GROUPCHAT: # OTR is not possible in group chats return True - from_jid = jid.JID(message_elt['from']) + from_jid = jid.JID(message_elt["from"]) if not from_jid.resource or from_jid.userhostJID() == client.jid.userhostJID(): # OTR is only usable when resources are present return True @@ -751,9 +752,9 @@ if client.is_component: return True encryption = mess_data.get(C.MESS_KEY_ENCRYPTION) - if encryption is None or encryption['plugin'].namespace != NS_OTR: + if encryption is None or encryption["plugin"].namespace != NS_OTR: return - to_jid = mess_data['to'] + to_jid = mess_data["to"] if not to_jid.resource: to_jid.resource = self.host.memory.main_resource_get( client, to_jid @@ -777,9 +778,10 @@ log.warning("No message found") else: self._p_carbons.set_private(message_elt) - self._p_hints.add_hint_elements(message_elt, [ - self._p_hints.HINT_NO_COPY, - self._p_hints.HINT_NO_PERMANENT_STORE]) + self._p_hints.add_hint_elements( + message_elt, + [self._p_hints.HINT_NO_COPY, self._p_hints.HINT_NO_PERMANENT_STORE], + ) otrctx.sendMessage(0, str(body).encode("utf-8"), appdata=mess_data) else: feedback = D_( @@ -791,8 +793,9 @@ client.feedback(to_jid, feedback) raise failure.Failure(exceptions.CancelError("Cancelled by OTR plugin")) - def send_message_trigger(self, client, mess_data, pre_xml_treatments, - post_xml_treatments): + def send_message_trigger( + self, client, mess_data, pre_xml_treatments, post_xml_treatments + ): if client.is_component: return True if mess_data["type"] == "groupchat": @@ -830,7 +833,7 @@ entity.resource = self.host.memory.main_resource_get( client, entity ) # FIXME: temporary and unsecure, must be changed when frontends - # are refactored + # are refactored except exceptions.UnknownEntityError: return True # entity was not connected if entity in client._otr_context_manager.contexts: