comparison libervia/backend/plugins/plugin_xep_0384.py @ 4164:15482dc0b5d1

plugin XEP-0384: fix trigger used for OLDMEMO: OLDMEMO was applied at the `send_message_data` trigger level, however this was missing stanza constructed without message data (which is notably the case for XEP-0308). This has been fixed by using the `send` trigger instead. As a side effect, the same code is now used for TWOMEMO and OLDMEMO, making it simpler.
author Goffi <goffi@goffi.org>
date Tue, 28 Nov 2023 17:41:02 +0100
parents 26534d959d2d
children 1a7a3e4b52a4
comparison
equal deleted inserted replaced
4163:3b3cd9453d9b 4164:15482dc0b5d1
1582 sat.trigger.add( 1582 sat.trigger.add(
1583 "message_received", 1583 "message_received",
1584 self._message_received_trigger, 1584 self._message_received_trigger,
1585 priority=100050 1585 priority=100050
1586 ) 1586 )
1587 sat.trigger.add( 1587
1588 "send_message_data",
1589 self.__send_message_data_trigger,
1590 priority=100050
1591 )
1592
1593 # These triggers are used by twomemo, which does do SCE
1594 sat.trigger.add("send", self.__send_trigger, priority=0) 1588 sat.trigger.add("send", self.__send_trigger, priority=0)
1595 # TODO: Add new triggers here for freshly received and about-to-be-sent stanzas, 1589 # TODO: Add new triggers here for freshly received and about-to-be-sent stanzas,
1596 # including IQs. 1590 # including IQs.
1597 1591
1598 # Give twomemo a (slightly) higher priority than oldmemo 1592 # Give twomemo a (slightly) higher priority than oldmemo
2416 2410
2417 if encryption is None: 2411 if encryption is None:
2418 # Encryption is not requested for this recipient 2412 # Encryption is not requested for this recipient
2419 return True 2413 return True
2420 2414
2421 if encryption["plugin"].namespace != twomemo.twomemo.NAMESPACE: 2415 encryption_ns = encryption["plugin"].namespace
2416 # All pre-checks done, we can start encrypting!
2417 if encryption_ns in (twomemo.twomemo.NAMESPACE, oldmemo.oldmemo.NAMESPACE):
2418 await self.encrypt(
2419 client,
2420 encryption_ns,
2421 stanza,
2422 recipient_bare_jid,
2423 stanza.getAttribute("type", C.MESS_TYPE_NORMAL) == C.MESS_TYPE_GROUPCHAT,
2424 stanza.getAttribute("id", None)
2425 )
2426 else:
2422 # Encryption is requested for this recipient, but not with twomemo 2427 # Encryption is requested for this recipient, but not with twomemo
2423 return True 2428 return True
2424 2429
2425 # All pre-checks done, we can start encrypting! 2430
2426 await self.encrypt(
2427 client,
2428 twomemo.twomemo.NAMESPACE,
2429 stanza,
2430 recipient_bare_jid,
2431 stanza.getAttribute("type", C.MESS_TYPE_NORMAL) == C.MESS_TYPE_GROUPCHAT,
2432 stanza.getAttribute("id", None)
2433 )
2434 2431
2435 # Add a store hint if this is a message stanza 2432 # Add a store hint if this is a message stanza
2436 if stanza.name == "message": 2433 if stanza.name == "message":
2437 self.__xep_0334.add_hint_elements(stanza, [ "store" ]) 2434 self.__xep_0334.add_hint_elements(stanza, [ "store" ])
2438 2435
2439 # Let the flow continue. 2436 # Let the flow continue.
2440 return True 2437 return True
2441
2442 async def __send_message_data_trigger(
2443 self,
2444 client: SatXMPPClient,
2445 mess_data: MessageData
2446 ) -> None:
2447 """
2448 @param client: The client sending this message.
2449 @param mess_data: The message data that is about to be sent. Can be modified.
2450 """
2451
2452 # Check whether encryption is requested for this message
2453 try:
2454 namespace = mess_data[C.MESS_KEY_ENCRYPTION]["plugin"].namespace
2455 except KeyError:
2456 return
2457
2458 # If encryption is requested, check whether it's oldmemo
2459 if namespace != oldmemo.oldmemo.NAMESPACE:
2460 return
2461
2462 # All pre-checks done, we can start encrypting!
2463 stanza = mess_data["xml"]
2464 recipient_jid = mess_data["to"]
2465 is_muc_message = mess_data["type"] == C.MESS_TYPE_GROUPCHAT
2466 stanza_id = mess_data["uid"]
2467
2468 await self.encrypt(
2469 client,
2470 oldmemo.oldmemo.NAMESPACE,
2471 stanza,
2472 recipient_jid,
2473 is_muc_message,
2474 stanza_id
2475 )
2476
2477 # Add a store hint
2478 self.__xep_0334.add_hint_elements(stanza, [ "store" ])
2479 2438
2480 async def encrypt( 2439 async def encrypt(
2481 self, 2440 self,
2482 client: SatXMPPClient, 2441 client: SatXMPPClient,
2483 namespace: Literal["urn:xmpp:omemo:2", "eu.siacs.conversations.axolotl"], 2442 namespace: Literal["urn:xmpp:omemo:2", "eu.siacs.conversations.axolotl"],