comparison libervia/backend/plugins/plugin_xep_0380.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
comparison
equal deleted inserted replaced
4269:64a85ce8be70 4270:0d7bb4df2343
50 host.trigger.add("sendMessage", self._send_message_trigger) 50 host.trigger.add("sendMessage", self._send_message_trigger)
51 host.trigger.add("message_received", self._message_received_trigger, priority=100) 51 host.trigger.add("message_received", self._message_received_trigger, priority=100)
52 host.register_namespace("eme", NS_EME) 52 host.register_namespace("eme", NS_EME)
53 53
54 def _add_eme_element(self, mess_data, namespace, name): 54 def _add_eme_element(self, mess_data, namespace, name):
55 message_elt = mess_data['xml'] 55 message_elt = mess_data["xml"]
56 encryption_elt = message_elt.addElement((NS_EME, 'encryption')) 56 encryption_elt = message_elt.addElement((NS_EME, "encryption"))
57 encryption_elt['namespace'] = namespace 57 encryption_elt["namespace"] = namespace
58 if name is not None: 58 if name is not None:
59 encryption_elt['name'] = name 59 encryption_elt["name"] = name
60 return mess_data 60 return mess_data
61 61
62 def _send_message_trigger(self, client, mess_data, __, post_xml_treatments): 62 def _send_message_trigger(self, client, mess_data, __, post_xml_treatments):
63 encryption = mess_data.get(C.MESS_KEY_ENCRYPTION) 63 encryption = mess_data.get(C.MESS_KEY_ENCRYPTION)
64 if encryption is not None: 64 if encryption is not None:
65 namespace = encryption['plugin'].namespace 65 namespace = encryption["plugin"].namespace
66 if namespace not in KNOWN_NAMESPACES: 66 if namespace not in KNOWN_NAMESPACES:
67 name = encryption['plugin'].name 67 name = encryption["plugin"].name
68 else: 68 else:
69 name = None 69 name = None
70 post_xml_treatments.addCallback( 70 post_xml_treatments.addCallback(
71 self._add_eme_element, namespace=namespace, name=name) 71 self._add_eme_element, namespace=namespace, name=name
72 )
72 return True 73 return True
73 74
74 def _message_received_trigger(self, client, message_elt, post_treat): 75 def _message_received_trigger(self, client, message_elt, post_treat):
75 try: 76 try:
76 encryption_elt = next(message_elt.elements(NS_EME, 'encryption')) 77 encryption_elt = next(message_elt.elements(NS_EME, "encryption"))
77 except StopIteration: 78 except StopIteration:
78 return True 79 return True
79 80
80 namespace = encryption_elt['namespace'] 81 namespace = encryption_elt["namespace"]
81 if namespace in client.encryption.get_namespaces(): 82 if namespace in client.encryption.get_namespaces():
82 # message is encrypted and we can decrypt it 83 # message is encrypted and we can decrypt it
83 return True 84 return True
84 85
85 name = KNOWN_NAMESPACES.get(namespace, encryption_elt.getAttribute("name")) 86 name = KNOWN_NAMESPACES.get(namespace, encryption_elt.getAttribute("name"))
86 87
87 # at this point, message is encrypted but we know that we can't decrypt it, 88 # at this point, message is encrypted but we know that we can't decrypt it,
88 # we need to notify the user 89 # we need to notify the user
89 sender_s = message_elt['from'] 90 sender_s = message_elt["from"]
90 to_jid = jid.JID(message_elt['from']) 91 to_jid = jid.JID(message_elt["from"])
91 algorithm = "{} [{}]".format(name, namespace) if name else namespace 92 algorithm = "{} [{}]".format(name, namespace) if name else namespace
92 log.warning( 93 log.warning(
93 _("Message from {sender} is encrypted with {algorithm} and we can't " 94 _(
94 "decrypt it.".format(sender=message_elt['from'], algorithm=algorithm))) 95 "Message from {sender} is encrypted with {algorithm} and we can't "
96 "decrypt it.".format(sender=message_elt["from"], algorithm=algorithm)
97 )
98 )
95 99
96 user_msg = D_( 100 user_msg = D_(
97 "User {sender} sent you an encrypted message (encrypted with {algorithm}), " 101 "User {sender} sent you an encrypted message (encrypted with {algorithm}), "
98 "and we can't decrypt it.").format(sender=sender_s, algorithm=algorithm) 102 "and we can't decrypt it."
103 ).format(sender=sender_s, algorithm=algorithm)
99 104
100 extra = {C.MESS_EXTRA_INFO: C.EXTRA_INFO_DECR_ERR} 105 extra = {C.MESS_EXTRA_INFO: C.EXTRA_INFO_DECR_ERR}
101 client.feedback(to_jid, user_msg, extra) 106 client.feedback(to_jid, user_msg, extra)
102 return False 107 return False