shellzapy 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- shellzapy-0.1.0.dist-info/METADATA +112 -0
- shellzapy-0.1.0.dist-info/RECORD +345 -0
- shellzapy-0.1.0.dist-info/WHEEL +4 -0
- shellzapy-0.1.0.dist-info/licenses/LICENSE +21 -0
- shellzapy-0.1.0.dist-info/licenses/NOTICE +9 -0
- zapy/__init__.py +58 -0
- zapy/_appstate/__init__.py +59 -0
- zapy/_appstate/constants.py +82 -0
- zapy/_appstate/crypto/__init__.py +17 -0
- zapy/_appstate/crypto/app_state_crypto.py +401 -0
- zapy/_appstate/parsers/__init__.py +23 -0
- zapy/_appstate/parsers/encoding.py +157 -0
- zapy/_appstate/parsers/response_parser.py +135 -0
- zapy/_appstate/sync/__init__.py +11 -0
- zapy/_appstate/sync/sync_client.py +1559 -0
- zapy/_appstate/types.py +137 -0
- zapy/_appstate/utils.py +100 -0
- zapy/_auth/__init__.py +3 -0
- zapy/_auth/auth_client.py +618 -0
- zapy/_auth/credentials_flow.py +290 -0
- zapy/_auth/pairing/__init__.py +3 -0
- zapy/_auth/pairing/companion_host.py +239 -0
- zapy/_auth/pairing/flow.py +808 -0
- zapy/_auth/pairing/pairing_code_crypto.py +143 -0
- zapy/_auth/pairing/qr_flow.py +123 -0
- zapy/_auth/pairing/shortcake_crypto.py +122 -0
- zapy/_auth/pairing/shortcake_flow.py +393 -0
- zapy/_auth/types.py +110 -0
- zapy/_client/__init__.py +26 -0
- zapy/_client/client.py +972 -0
- zapy/_client/connection/__init__.py +6 -0
- zapy/_client/connection/manager.py +525 -0
- zapy/_client/connection/receipt_queue.py +74 -0
- zapy/_client/coordinators/__init__.py +81 -0
- zapy/_client/coordinators/ab_props.py +260 -0
- zapy/_client/coordinators/app_state_mutation.py +2538 -0
- zapy/_client/coordinators/bot.py +830 -0
- zapy/_client/coordinators/broadcast_list.py +101 -0
- zapy/_client/coordinators/business.py +247 -0
- zapy/_client/coordinators/email.py +158 -0
- zapy/_client/coordinators/group.py +1478 -0
- zapy/_client/coordinators/incoming_node.py +758 -0
- zapy/_client/coordinators/low_level.py +119 -0
- zapy/_client/coordinators/message.py +1153 -0
- zapy/_client/coordinators/message_dispatch.py +1637 -0
- zapy/_client/coordinators/newsletter.py +110 -0
- zapy/_client/coordinators/offline_resume.py +271 -0
- zapy/_client/coordinators/passive_tasks.py +447 -0
- zapy/_client/coordinators/presence.py +103 -0
- zapy/_client/coordinators/privacy.py +623 -0
- zapy/_client/coordinators/profile.py +1024 -0
- zapy/_client/coordinators/retry.py +1878 -0
- zapy/_client/coordinators/status.py +132 -0
- zapy/_client/coordinators/stream_control.py +260 -0
- zapy/_client/coordinators/trusted_contact_token.py +417 -0
- zapy/_client/events/__init__.py +266 -0
- zapy/_client/events/abprops.py +58 -0
- zapy/_client/events/appstate_mutation.py +248 -0
- zapy/_client/events/business.py +565 -0
- zapy/_client/events/call.py +144 -0
- zapy/_client/events/chatstate.py +41 -0
- zapy/_client/events/devices.py +122 -0
- zapy/_client/events/dirty.py +384 -0
- zapy/_client/events/group.py +655 -0
- zapy/_client/events/identity.py +40 -0
- zapy/_client/events/incoming.py +1079 -0
- zapy/_client/events/mex_notification.py +317 -0
- zapy/_client/events/offline.py +123 -0
- zapy/_client/events/picture.py +67 -0
- zapy/_client/events/presence.py +74 -0
- zapy/_client/events/privacy.py +187 -0
- zapy/_client/events/privacy_token.py +72 -0
- zapy/_client/events/receipt.py +109 -0
- zapy/_client/events/registration.py +70 -0
- zapy/_client/events/username.py +76 -0
- zapy/_client/factory.py +2020 -0
- zapy/_client/media.py +829 -0
- zapy/_client/newsletter/__init__.py +90 -0
- zapy/_client/newsletter/admin.py +323 -0
- zapy/_client/newsletter/content.py +499 -0
- zapy/_client/newsletter/discovery.py +233 -0
- zapy/_client/newsletter/messaging.py +396 -0
- zapy/_client/newsletter/mex.py +99 -0
- zapy/_client/newsletter/parse.py +406 -0
- zapy/_client/newsletter/types.py +393 -0
- zapy/_client/persistence/__init__.py +89 -0
- zapy/_client/persistence/companion_host.py +133 -0
- zapy/_client/persistence/ephemeral_setting.py +245 -0
- zapy/_client/persistence/group_history.py +361 -0
- zapy/_client/persistence/history_blob.py +169 -0
- zapy/_client/persistence/history_sync.py +646 -0
- zapy/_client/persistence/mailbox.py +232 -0
- zapy/_client/persistence/write_behind_persistence.py +368 -0
- zapy/_client/plugins/__init__.py +23 -0
- zapy/_client/plugins/define.py +44 -0
- zapy/_client/plugins/install.py +254 -0
- zapy/_client/plugins/types.py +64 -0
- zapy/_client/tokens/__init__.py +21 -0
- zapy/_client/tokens/cs_token.py +47 -0
- zapy/_client/tokens/tc_token.py +43 -0
- zapy/_client/types.py +988 -0
- zapy/_crypto/__init__.py +65 -0
- zapy/_crypto/core/__init__.py +62 -0
- zapy/_crypto/core/hkdf.py +41 -0
- zapy/_crypto/core/keys.py +51 -0
- zapy/_crypto/core/nonce.py +25 -0
- zapy/_crypto/core/primitives.py +179 -0
- zapy/_crypto/core/random.py +32 -0
- zapy/_crypto/core/xeddsa.py +72 -0
- zapy/_crypto/curves/__init__.py +3 -0
- zapy/_crypto/curves/constants.py +10 -0
- zapy/_crypto/curves/ed25519.py +61 -0
- zapy/_crypto/curves/types.py +18 -0
- zapy/_crypto/curves/x25519.py +117 -0
- zapy/_crypto/math/__init__.py +3 -0
- zapy/_crypto/math/constants.py +35 -0
- zapy/_crypto/math/edwards.py +174 -0
- zapy/_crypto/math/fe.py +120 -0
- zapy/_crypto/math/le.py +13 -0
- zapy/_crypto/math/mod.py +15 -0
- zapy/_crypto/math/prime.py +11 -0
- zapy/_crypto/math/types.py +25 -0
- zapy/_crypto/native_backend.py +40 -0
- zapy/_infra/__init__.py +3 -0
- zapy/_infra/log.py +142 -0
- zapy/_infra/perf.py +77 -0
- zapy/_media/__init__.py +64 -0
- zapy/_media/constants.py +133 -0
- zapy/_media/container.py +110 -0
- zapy/_media/crypto/__init__.py +19 -0
- zapy/_media/crypto/crypto.py +633 -0
- zapy/_media/crypto/media_retry.py +107 -0
- zapy/_media/processor.py +100 -0
- zapy/_media/sticker/__init__.py +8 -0
- zapy/_media/sticker/pack.py +188 -0
- zapy/_media/transfer/__init__.py +15 -0
- zapy/_media/transfer/client.py +635 -0
- zapy/_media/transfer/conn.py +48 -0
- zapy/_media/types.py +169 -0
- zapy/_message/__init__.py +121 -0
- zapy/_message/addons/__init__.py +5 -0
- zapy/_message/addons/link_preview/__init__.py +54 -0
- zapy/_message/addons/link_preview/builder.py +56 -0
- zapy/_message/addons/link_preview/detect.py +72 -0
- zapy/_message/addons/link_preview/fetcher.py +553 -0
- zapy/_message/addons/link_preview/types.py +100 -0
- zapy/_message/client.py +486 -0
- zapy/_message/context_info.py +424 -0
- zapy/_message/crypto/__init__.py +71 -0
- zapy/_message/crypto/addon_crypto.py +656 -0
- zapy/_message/crypto/icdc.py +138 -0
- zapy/_message/crypto/phash.py +87 -0
- zapy/_message/crypto/reporting_token.py +498 -0
- zapy/_message/crypto/use_case_secret.py +113 -0
- zapy/_message/encode/__init__.py +84 -0
- zapy/_message/encode/content.py +636 -0
- zapy/_message/encode/device_sent.py +88 -0
- zapy/_message/encode/media_payload.py +122 -0
- zapy/_message/encode/padding.py +27 -0
- zapy/_message/kinds/__init__.py +1 -0
- zapy/_message/kinds/bot.py +179 -0
- zapy/_message/kinds/group_history.py +206 -0
- zapy/_message/kinds/newsletter.py +388 -0
- zapy/_message/kinds/sticker_pack.py +74 -0
- zapy/_message/messaging/__init__.py +336 -0
- zapy/_message/messaging/companion_host.py +117 -0
- zapy/_message/messaging/fanout.py +311 -0
- zapy/_message/messaging/group_metadata.py +444 -0
- zapy/_message/messaging/ignore_key.py +264 -0
- zapy/_message/messaging/key_protocol.py +217 -0
- zapy/_message/messaging/link_preview.py +448 -0
- zapy/_message/messaging/messages.py +1429 -0
- zapy/_message/primitives/__init__.py +1 -0
- zapy/_message/primitives/ack.py +232 -0
- zapy/_message/primitives/incoming.py +1141 -0
- zapy/_message/primitives/media_retry.py +451 -0
- zapy/_message/primitives/peer_data_operation.py +271 -0
- zapy/_message/types.py +434 -0
- zapy/_proto/WAProto_pb2.py +2584 -0
- zapy/_proto/__init__.py +1 -0
- zapy/_protocol/__init__.py +3 -0
- zapy/_protocol/abprops_spec.py +6216 -0
- zapy/_protocol/auth.py +17 -0
- zapy/_protocol/browser.py +57 -0
- zapy/_protocol/defaults.py +38 -0
- zapy/_protocol/device_identity.py +54 -0
- zapy/_protocol/jid.py +241 -0
- zapy/_protocol/message.py +127 -0
- zapy/_protocol/nodes.py +144 -0
- zapy/_protocol/notification.py +18 -0
- zapy/_retry/__init__.py +77 -0
- zapy/_retry/codec.py +209 -0
- zapy/_retry/constants.py +28 -0
- zapy/_retry/parse.py +132 -0
- zapy/_retry/reason.py +65 -0
- zapy/_retry/replay.py +483 -0
- zapy/_retry/tracker.py +161 -0
- zapy/_retry/types.py +122 -0
- zapy/_signal/__init__.py +171 -0
- zapy/_signal/api/__init__.py +133 -0
- zapy/_signal/api/codec.py +164 -0
- zapy/_signal/api/constants.py +14 -0
- zapy/_signal/api/device_sync.py +915 -0
- zapy/_signal/api/digest_sync.py +363 -0
- zapy/_signal/api/identity_sync.py +250 -0
- zapy/_signal/api/missing_pre_keys_sync.py +267 -0
- zapy/_signal/api/prekeys.py +191 -0
- zapy/_signal/api/result_map.py +40 -0
- zapy/_signal/api/rotate_key.py +111 -0
- zapy/_signal/api/session_sync.py +297 -0
- zapy/_signal/attestation.py +145 -0
- zapy/_signal/constants.py +22 -0
- zapy/_signal/encoding.py +78 -0
- zapy/_signal/group/__init__.py +35 -0
- zapy/_signal/group/chain.py +139 -0
- zapy/_signal/group/codec.py +78 -0
- zapy/_signal/group/encoding.py +166 -0
- zapy/_signal/group/manager.py +361 -0
- zapy/_signal/registration/__init__.py +44 -0
- zapy/_signal/registration/keygen.py +46 -0
- zapy/_signal/registration/reg_encoding.py +66 -0
- zapy/_signal/registration/reg_utils.py +52 -0
- zapy/_signal/session/__init__.py +114 -0
- zapy/_signal/session/encoding.py +385 -0
- zapy/_signal/session/protocol.py +590 -0
- zapy/_signal/session/ratchet.py +381 -0
- zapy/_signal/session/resolver.py +630 -0
- zapy/_signal/session/serializer.py +96 -0
- zapy/_signal/session/session.py +272 -0
- zapy/_signal/types.py +214 -0
- zapy/_store/__init__.py +118 -0
- zapy/_store/cache/__init__.py +166 -0
- zapy/_store/cache/identity.py +130 -0
- zapy/_store/cache/privacy_token.py +116 -0
- zapy/_store/cache/sender_key.py +168 -0
- zapy/_store/cache/session.py +142 -0
- zapy/_store/contracts/__init__.py +84 -0
- zapy/_store/contracts/appstate.py +82 -0
- zapy/_store/contracts/auth.py +16 -0
- zapy/_store/contracts/chat_metadata.py +29 -0
- zapy/_store/contracts/contact.py +31 -0
- zapy/_store/contracts/device_list.py +35 -0
- zapy/_store/contracts/group_metadata.py +33 -0
- zapy/_store/contracts/identity.py +18 -0
- zapy/_store/contracts/message.py +36 -0
- zapy/_store/contracts/message_secret.py +41 -0
- zapy/_store/contracts/pre_key.py +32 -0
- zapy/_store/contracts/privacy_token.py +32 -0
- zapy/_store/contracts/retry.py +47 -0
- zapy/_store/contracts/sender_key.py +37 -0
- zapy/_store/contracts/session.py +21 -0
- zapy/_store/contracts/signal.py +23 -0
- zapy/_store/contracts/thread.py +35 -0
- zapy/_store/create_store.py +890 -0
- zapy/_store/locks.py +1154 -0
- zapy/_store/memory/__init__.py +40 -0
- zapy/_store/memory/appstate.py +200 -0
- zapy/_store/memory/auth.py +24 -0
- zapy/_store/memory/chat_metadata.py +124 -0
- zapy/_store/memory/contact.py +125 -0
- zapy/_store/memory/device_list.py +161 -0
- zapy/_store/memory/group_metadata.py +125 -0
- zapy/_store/memory/identity.py +86 -0
- zapy/_store/memory/message.py +85 -0
- zapy/_store/memory/message_secret.py +145 -0
- zapy/_store/memory/pre_key.py +112 -0
- zapy/_store/memory/privacy_token.py +103 -0
- zapy/_store/memory/retry.py +250 -0
- zapy/_store/memory/sender_key.py +162 -0
- zapy/_store/memory/session.py +111 -0
- zapy/_store/memory/signal.py +45 -0
- zapy/_store/memory/thread.py +66 -0
- zapy/_store/noop.py +279 -0
- zapy/_transport/__init__.py +3 -0
- zapy/_transport/binary/__init__.py +77 -0
- zapy/_transport/binary/constants.py +80 -0
- zapy/_transport/binary/decoder.py +260 -0
- zapy/_transport/binary/encoder.py +279 -0
- zapy/_transport/binary/tokens.py +1289 -0
- zapy/_transport/comms.py +759 -0
- zapy/_transport/keepalive/__init__.py +3 -0
- zapy/_transport/keepalive/keep_alive.py +252 -0
- zapy/_transport/node/__init__.py +3 -0
- zapy/_transport/node/builders/__init__.py +9 -0
- zapy/_transport/node/builders/abprops.py +33 -0
- zapy/_transport/node/builders/account_sync.py +120 -0
- zapy/_transport/node/builders/bot.py +56 -0
- zapy/_transport/node/builders/business.py +261 -0
- zapy/_transport/node/builders/chatstate.py +74 -0
- zapy/_transport/node/builders/community.py +116 -0
- zapy/_transport/node/builders/device.py +30 -0
- zapy/_transport/node/builders/email.py +181 -0
- zapy/_transport/node/builders/global_.py +74 -0
- zapy/_transport/node/builders/group.py +279 -0
- zapy/_transport/node/builders/media.py +21 -0
- zapy/_transport/node/builders/message.py +436 -0
- zapy/_transport/node/builders/mobile.py +227 -0
- zapy/_transport/node/builders/newsletter.py +293 -0
- zapy/_transport/node/builders/offline.py +20 -0
- zapy/_transport/node/builders/pairing.py +130 -0
- zapy/_transport/node/builders/passive.py +28 -0
- zapy/_transport/node/builders/prekeys.py +215 -0
- zapy/_transport/node/builders/presence.py +100 -0
- zapy/_transport/node/builders/privacy.py +243 -0
- zapy/_transport/node/builders/privacy_token.py +94 -0
- zapy/_transport/node/builders/profile.py +176 -0
- zapy/_transport/node/builders/retry.py +156 -0
- zapy/_transport/node/builders/shortcake.py +87 -0
- zapy/_transport/node/builders/tos.py +92 -0
- zapy/_transport/node/builders/usync.py +185 -0
- zapy/_transport/node/helpers.py +201 -0
- zapy/_transport/node/orchestrator.py +210 -0
- zapy/_transport/node/query.py +82 -0
- zapy/_transport/node/transport.py +105 -0
- zapy/_transport/node/usync.py +33 -0
- zapy/_transport/node/xml.py +38 -0
- zapy/_transport/noise/__init__.py +7 -0
- zapy/_transport/noise/cert.py +85 -0
- zapy/_transport/noise/client_payload.py +344 -0
- zapy/_transport/noise/constants.py +12 -0
- zapy/_transport/noise/frame_codec.py +120 -0
- zapy/_transport/noise/handshake.py +92 -0
- zapy/_transport/noise/session.py +493 -0
- zapy/_transport/noise/socket.py +45 -0
- zapy/_transport/noise/types.py +66 -0
- zapy/_transport/noise/version.json +3 -0
- zapy/_transport/proxy.py +35 -0
- zapy/_transport/types.py +19 -0
- zapy/_transport/wa_version_fetcher.py +159 -0
- zapy/_transport/web_socket.py +602 -0
- zapy/_util/__init__.py +3 -0
- zapy/_util/bytes.py +131 -0
- zapy/_util/coercion.py +29 -0
- zapy/_util/collections.py +45 -0
- zapy/_util/primitives.py +49 -0
- zapy/client.py +840 -0
- zapy/errors.py +148 -0
- zapy/events.py +564 -0
- zapy/jid.py +140 -0
- zapy/py.typed +0 -0
- zapy/stores/__init__.py +50 -0
- zapy/stores/base.py +1627 -0
- zapy/stores/json_file.py +359 -0
- zapy/stores/memory.py +322 -0
- zapy/stores/sqlite.py +343 -0
|
@@ -0,0 +1,1141 @@
|
|
|
1
|
+
"""Inbound message stanza handling (mirrors ``src/message/primitives/incoming.ts``).
|
|
2
|
+
|
|
3
|
+
Messages are plain ``dict`` trees (``MessageDict``); wire bytes are decoded
|
|
4
|
+
with :func:`proto_message_to_dict`. Event shapes mirror ``src/client/types.ts``;
|
|
5
|
+
they travel with the client layer (``zapy._client``) when it lands.
|
|
6
|
+
``SignalProtocol`` / ``SenderKeyManager`` are structural protocols over the
|
|
7
|
+
signal layer (``zapy._signal``).
|
|
8
|
+
|
|
9
|
+
TEMPORARY (sibling layers not yet ported): the ``WA_*`` tables and JID /
|
|
10
|
+
username helpers below are verbatim ports of ``src/protocol/message.ts``,
|
|
11
|
+
``src/protocol/jid.ts`` and ``src/protocol/username.ts``. They move to their
|
|
12
|
+
canonical modules (``zapy._protocol.*``) once those land; the call sites
|
|
13
|
+
already match the canonical shapes.
|
|
14
|
+
"""
|
|
15
|
+
|
|
16
|
+
from __future__ import annotations
|
|
17
|
+
|
|
18
|
+
import inspect
|
|
19
|
+
|
|
20
|
+
from collections.abc import Awaitable, Callable
|
|
21
|
+
from dataclasses import dataclass
|
|
22
|
+
from typing import Any, Protocol
|
|
23
|
+
|
|
24
|
+
from zapy._infra.log import Logger
|
|
25
|
+
from zapy._message.context_info import (
|
|
26
|
+
_long_to_number,
|
|
27
|
+
pick_incoming_expiration_seconds,
|
|
28
|
+
proto_message_to_dict,
|
|
29
|
+
)
|
|
30
|
+
from zapy._message.encode.content import MessageDict
|
|
31
|
+
from zapy._message.encode.device_sent import unwrap_device_sent_message
|
|
32
|
+
from zapy._message.encode.padding import unpad_pkcs7
|
|
33
|
+
from zapy._message.kinds.newsletter import (
|
|
34
|
+
ProcessNewsletterMessageOptions,
|
|
35
|
+
process_incoming_newsletter_message,
|
|
36
|
+
)
|
|
37
|
+
from zapy._message.primitives.ack import (
|
|
38
|
+
build_delivery_receipt_node,
|
|
39
|
+
build_message_ack_node,
|
|
40
|
+
build_retry_receipt_node,
|
|
41
|
+
)
|
|
42
|
+
from zapy._proto import WAProto_pb2
|
|
43
|
+
from zapy._signal.types import SignalAddress
|
|
44
|
+
from zapy._transport.node.helpers import (
|
|
45
|
+
decode_node_content_base64_or_bytes,
|
|
46
|
+
find_node_child,
|
|
47
|
+
)
|
|
48
|
+
from zapy._transport.types import BinaryNode
|
|
49
|
+
from zapy._util.primitives import parse_optional_int, to_error
|
|
50
|
+
|
|
51
|
+
# Verbatim ``src/protocol/message.ts``.
|
|
52
|
+
WA_MESSAGE_TAGS: dict[str, str] = {
|
|
53
|
+
"MESSAGE": "message",
|
|
54
|
+
"ENC": "enc",
|
|
55
|
+
"RECEIPT": "receipt",
|
|
56
|
+
"ACK": "ack",
|
|
57
|
+
"ERROR": "error",
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
# Verbatim ``src/protocol/message.ts`` (rows used by the incoming path).
|
|
61
|
+
WA_MESSAGE_TYPES: dict[str, str] = {
|
|
62
|
+
"MEDIA_NOTIFY": "medianotify",
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
# Verbatim ``src/protocol/defaults.ts`` server names used by the JID helpers.
|
|
66
|
+
_HOST_DOMAIN = "s.whatsapp.net"
|
|
67
|
+
_GROUP_SERVER = "g.us"
|
|
68
|
+
_BROADCAST_SERVER = "broadcast"
|
|
69
|
+
_BOT_SERVER = "bot"
|
|
70
|
+
_NEWSLETTER_SERVER = "newsletter"
|
|
71
|
+
_LID_SERVER = "lid"
|
|
72
|
+
_HOSTED_SERVER = "hosted"
|
|
73
|
+
_HOSTED_LID_SERVER = "hosted.lid"
|
|
74
|
+
_MAX_SAFE_INTEGER = 2**53 - 1
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def _is_jid_type(jid: str, server: str) -> bool:
|
|
78
|
+
return jid.endswith(f"@{server}")
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
def is_group_jid(jid: str) -> bool:
|
|
82
|
+
"""Whether the JID lives on the group server."""
|
|
83
|
+
return _is_jid_type(jid, _GROUP_SERVER)
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
def is_broadcast_jid(jid: str) -> bool:
|
|
87
|
+
"""Whether the JID lives on the broadcast server."""
|
|
88
|
+
return _is_jid_type(jid, _BROADCAST_SERVER)
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
def is_newsletter_jid(jid: str) -> bool:
|
|
92
|
+
"""Whether the JID lives on the newsletter server."""
|
|
93
|
+
return _is_jid_type(jid, _NEWSLETTER_SERVER)
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
def is_group_or_broadcast_jid(jid: str) -> bool:
|
|
97
|
+
"""Whether the JID lives on the group or broadcast server."""
|
|
98
|
+
return is_group_jid(jid) or is_broadcast_jid(jid)
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
def parse_signal_address_from_jid(jid: str) -> SignalAddress:
|
|
102
|
+
"""Parse a JID into a Signal-style address (device ``0`` when absent)."""
|
|
103
|
+
at_index = jid.find("@")
|
|
104
|
+
if at_index < 1 or at_index >= len(jid) - 1:
|
|
105
|
+
raise ValueError(f"invalid jid: {jid}")
|
|
106
|
+
colon_index = jid.find(":")
|
|
107
|
+
server = jid[at_index + 1 :]
|
|
108
|
+
if colon_index == -1 or colon_index > at_index:
|
|
109
|
+
return SignalAddress(user=jid[:at_index], device=0, server=server)
|
|
110
|
+
if colon_index >= at_index - 1:
|
|
111
|
+
raise ValueError(f"invalid jid device: {jid}")
|
|
112
|
+
device = 0
|
|
113
|
+
for pos in range(colon_index + 1, at_index):
|
|
114
|
+
digit = ord(jid[pos]) - 48
|
|
115
|
+
if digit < 0 or digit > 9:
|
|
116
|
+
raise ValueError(f"invalid jid device: {jid}")
|
|
117
|
+
device = device * 10 + digit
|
|
118
|
+
if device > _MAX_SAFE_INTEGER:
|
|
119
|
+
raise ValueError(f"invalid jid device: {jid}")
|
|
120
|
+
return SignalAddress(user=jid[:colon_index], device=device, server=server)
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
def to_user_jid(jid: str) -> str:
|
|
124
|
+
"""Strip the ``:device`` segment, returning the bare ``user@server`` form."""
|
|
125
|
+
at_index = jid.find("@")
|
|
126
|
+
if at_index < 1 or at_index >= len(jid) - 1:
|
|
127
|
+
raise ValueError(f"invalid jid: {jid}")
|
|
128
|
+
colon_index = jid.find(":")
|
|
129
|
+
if colon_index == -1 or colon_index > at_index:
|
|
130
|
+
return jid
|
|
131
|
+
user = jid[:colon_index]
|
|
132
|
+
if not jid[colon_index + 1 : at_index].isdigit():
|
|
133
|
+
raise ValueError(f"invalid jid device: {jid}")
|
|
134
|
+
return f"{user}@{jid[at_index + 1 :]}"
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
def _canonicalize_signal_server(server: str) -> str:
|
|
138
|
+
if server == _HOSTED_SERVER:
|
|
139
|
+
return _HOST_DOMAIN
|
|
140
|
+
if server == _HOSTED_LID_SERVER:
|
|
141
|
+
return _LID_SERVER
|
|
142
|
+
return server
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
def is_own_account_jid(jid: str, me_jid: str | None, me_lid: str | None) -> bool:
|
|
146
|
+
"""Whether ``jid`` is the account's own user.
|
|
147
|
+
|
|
148
|
+
Device-insensitive and hosted-aware.
|
|
149
|
+
"""
|
|
150
|
+
address = parse_signal_address_from_jid(jid)
|
|
151
|
+
candidate = (
|
|
152
|
+
f"{address.user}@{_canonicalize_signal_server(address.server or _HOST_DOMAIN)}"
|
|
153
|
+
)
|
|
154
|
+
for me in (me_jid, me_lid):
|
|
155
|
+
if not me:
|
|
156
|
+
continue
|
|
157
|
+
try:
|
|
158
|
+
me_address = parse_signal_address_from_jid(me)
|
|
159
|
+
except ValueError:
|
|
160
|
+
continue
|
|
161
|
+
me_user = _canonicalize_signal_server(me_address.server or _HOST_DOMAIN)
|
|
162
|
+
if f"{me_address.user}@{me_user}" == candidate:
|
|
163
|
+
return True
|
|
164
|
+
return False
|
|
165
|
+
|
|
166
|
+
|
|
167
|
+
def canonicalize_own_account_jid(
|
|
168
|
+
jid: str, me_jid: str | None, me_lid: str | None
|
|
169
|
+
) -> str:
|
|
170
|
+
"""Rewrite the account's own PN device JID to its LID equivalent."""
|
|
171
|
+
if not me_jid or not me_lid:
|
|
172
|
+
return jid
|
|
173
|
+
try:
|
|
174
|
+
address = parse_signal_address_from_jid(jid)
|
|
175
|
+
except ValueError:
|
|
176
|
+
return jid
|
|
177
|
+
if (address.server or _HOST_DOMAIN) != _HOST_DOMAIN:
|
|
178
|
+
return jid
|
|
179
|
+
try:
|
|
180
|
+
me_pn_user = parse_signal_address_from_jid(me_jid).user
|
|
181
|
+
me_lid_user = parse_signal_address_from_jid(me_lid).user
|
|
182
|
+
except ValueError:
|
|
183
|
+
return jid
|
|
184
|
+
if address.user != me_pn_user:
|
|
185
|
+
return jid
|
|
186
|
+
if address.device == 0:
|
|
187
|
+
return f"{me_lid_user}@{_LID_SERVER}"
|
|
188
|
+
return f"{me_lid_user}:{address.device}@{_LID_SERVER}"
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+
def normalize_username(username: str) -> str:
|
|
192
|
+
"""Strip the display-only ``@`` prefix (verbatim ``src/protocol/username.ts``)."""
|
|
193
|
+
return username[1:] if username[:1] == "@" else username
|
|
194
|
+
|
|
195
|
+
|
|
196
|
+
@dataclass(frozen=True, slots=True)
|
|
197
|
+
class WaIncomingMessageKey:
|
|
198
|
+
"""A received message's key: proto fields plus chat/sender addressing metadata."""
|
|
199
|
+
|
|
200
|
+
remote_jid: str = ""
|
|
201
|
+
id: str = ""
|
|
202
|
+
from_me: bool = False
|
|
203
|
+
is_group: bool = False
|
|
204
|
+
is_broadcast: bool = False
|
|
205
|
+
is_newsletter: bool = False
|
|
206
|
+
sender_device: int = 0
|
|
207
|
+
participant: str | None = None
|
|
208
|
+
remote_jid_alt: str | None = None
|
|
209
|
+
participant_alt: str | None = None
|
|
210
|
+
sender_username: str | None = None
|
|
211
|
+
recipient_jid: str | None = None
|
|
212
|
+
recipient_alt: str | None = None
|
|
213
|
+
recipient_username: str | None = None
|
|
214
|
+
server_id: int | None = None
|
|
215
|
+
|
|
216
|
+
|
|
217
|
+
@dataclass(frozen=True, slots=True)
|
|
218
|
+
class WaIncomingMessageEvent:
|
|
219
|
+
raw_node: BinaryNode | None = None
|
|
220
|
+
key: WaIncomingMessageKey | None = None
|
|
221
|
+
stanza_type: str | None = None
|
|
222
|
+
offline: bool = False
|
|
223
|
+
timestamp_seconds: int | None = None
|
|
224
|
+
expiration_seconds: int | None = None
|
|
225
|
+
push_name: str | None = None
|
|
226
|
+
message: MessageDict | None = None
|
|
227
|
+
|
|
228
|
+
|
|
229
|
+
@dataclass(frozen=True, slots=True)
|
|
230
|
+
class WaIncomingProtocolMessageEvent(WaIncomingMessageEvent):
|
|
231
|
+
protocol_message: MessageDict | None = None
|
|
232
|
+
|
|
233
|
+
|
|
234
|
+
@dataclass(frozen=True, slots=True)
|
|
235
|
+
class WaIncomingDecryptedPayloadEvent:
|
|
236
|
+
raw_node: BinaryNode | None = None
|
|
237
|
+
stanza_id: str | None = None
|
|
238
|
+
chat_jid: str | None = None
|
|
239
|
+
stanza_type: str | None = None
|
|
240
|
+
offline: bool = False
|
|
241
|
+
enc_index: int = 0
|
|
242
|
+
enc_type: str = ""
|
|
243
|
+
plaintext: bytes = b""
|
|
244
|
+
|
|
245
|
+
|
|
246
|
+
@dataclass(frozen=True, slots=True)
|
|
247
|
+
class WaIncomingUnavailableMessageEvent:
|
|
248
|
+
raw_node: BinaryNode | None = None
|
|
249
|
+
key: WaIncomingMessageKey | None = None
|
|
250
|
+
kind: str = "other"
|
|
251
|
+
resend_requested: bool = False
|
|
252
|
+
stanza_type: str | None = None
|
|
253
|
+
offline: bool = False
|
|
254
|
+
timestamp_seconds: int | None = None
|
|
255
|
+
push_name: str | None = None
|
|
256
|
+
|
|
257
|
+
|
|
258
|
+
@dataclass(frozen=True, slots=True)
|
|
259
|
+
class WaIncomingUnhandledStanzaEvent:
|
|
260
|
+
raw_node: BinaryNode | None = None
|
|
261
|
+
stanza_id: str | None = None
|
|
262
|
+
chat_jid: str | None = None
|
|
263
|
+
stanza_type: str | None = None
|
|
264
|
+
offline: bool = False
|
|
265
|
+
reason: str = ""
|
|
266
|
+
|
|
267
|
+
|
|
268
|
+
@dataclass(frozen=True, slots=True)
|
|
269
|
+
class WaRetryDecryptFailureContext:
|
|
270
|
+
"""Mirrors ``src/retry/types.ts`` until the retry layer lands."""
|
|
271
|
+
|
|
272
|
+
message_node: BinaryNode | None = None
|
|
273
|
+
stanza_id: str = ""
|
|
274
|
+
from_: str = ""
|
|
275
|
+
participant: str | None = None
|
|
276
|
+
recipient: str | None = None
|
|
277
|
+
t: str | None = None
|
|
278
|
+
|
|
279
|
+
@property
|
|
280
|
+
def sender_jid(self) -> str:
|
|
281
|
+
"""Sender JID alias (the coordinator reads ``sender_jid``)."""
|
|
282
|
+
return self.from_
|
|
283
|
+
|
|
284
|
+
@property
|
|
285
|
+
def stanza_from(self) -> str:
|
|
286
|
+
"""Alias matching the coordinator-side context shape."""
|
|
287
|
+
return self.from_
|
|
288
|
+
|
|
289
|
+
|
|
290
|
+
@dataclass(frozen=True, slots=True)
|
|
291
|
+
class SignalDecryptEnvelope:
|
|
292
|
+
type: str = "msg"
|
|
293
|
+
ciphertext: bytes = b""
|
|
294
|
+
|
|
295
|
+
|
|
296
|
+
class SignalProtocol(Protocol):
|
|
297
|
+
"""Structural protocol over ``zapy._signal`` session decryption."""
|
|
298
|
+
|
|
299
|
+
async def decrypt_message(
|
|
300
|
+
self, address: SignalAddress, envelope: SignalDecryptEnvelope
|
|
301
|
+
) -> bytes: ...
|
|
302
|
+
|
|
303
|
+
|
|
304
|
+
@dataclass(frozen=True, slots=True)
|
|
305
|
+
class GroupSenderKeyCiphertext:
|
|
306
|
+
group_id: str = ""
|
|
307
|
+
sender: SignalAddress | None = None
|
|
308
|
+
ciphertext: bytes = b""
|
|
309
|
+
|
|
310
|
+
|
|
311
|
+
class SenderKeyManager(Protocol):
|
|
312
|
+
"""Structural protocol over ``zapy._signal`` sender-key decryption."""
|
|
313
|
+
|
|
314
|
+
async def decrypt_group_message(
|
|
315
|
+
self, payload: GroupSenderKeyCiphertext
|
|
316
|
+
) -> bytes: ...
|
|
317
|
+
|
|
318
|
+
async def process_sender_key_distribution_payload(
|
|
319
|
+
self, group_id: str, sender: SignalAddress, payload: bytes
|
|
320
|
+
) -> Any: ...
|
|
321
|
+
|
|
322
|
+
|
|
323
|
+
@dataclass(frozen=True, slots=True)
|
|
324
|
+
class WaIncomingMessageAckHandlerOptions:
|
|
325
|
+
logger: Logger
|
|
326
|
+
send_node: Callable[[BinaryNode], Awaitable[None]]
|
|
327
|
+
get_me_jid: Callable[[], str | None] | None = None
|
|
328
|
+
get_me_lid: Callable[[], str | None] | None = None
|
|
329
|
+
signal_protocol: SignalProtocol | None = None
|
|
330
|
+
sender_key_manager: SenderKeyManager | None = None
|
|
331
|
+
on_decrypt_failure: (
|
|
332
|
+
Callable[[WaRetryDecryptFailureContext, Any], Awaitable[bool]] | None
|
|
333
|
+
) = None
|
|
334
|
+
request_placeholder_resend: (
|
|
335
|
+
Callable[[WaRetryDecryptFailureContext], bool] | None
|
|
336
|
+
) = None
|
|
337
|
+
emit_incoming_message: Callable[[WaIncomingMessageEvent], None] | None = None
|
|
338
|
+
emit_newsletter_message_update: Callable[[Any], None] | None = None
|
|
339
|
+
emit_unavailable_message: (
|
|
340
|
+
Callable[[WaIncomingUnavailableMessageEvent], None] | None
|
|
341
|
+
) = None
|
|
342
|
+
emit_unhandled_stanza: Callable[[WaIncomingUnhandledStanzaEvent], None] | None = (
|
|
343
|
+
None
|
|
344
|
+
)
|
|
345
|
+
emit_decrypted_payload: (
|
|
346
|
+
Callable[[Callable[[], WaIncomingDecryptedPayloadEvent]], None] | None
|
|
347
|
+
) = None
|
|
348
|
+
|
|
349
|
+
|
|
350
|
+
def _extract_message_identity_attrs(attrs: dict[str, str]) -> dict[str, Any]:
|
|
351
|
+
raw_remote_jid_alt = attrs.get("sender_pn", attrs.get("sender_lid"))
|
|
352
|
+
raw_participant_alt = attrs.get("participant_pn", attrs.get("participant_lid"))
|
|
353
|
+
raw_recipient_alt = attrs.get("peer_recipient_pn", attrs.get("peer_recipient_lid"))
|
|
354
|
+
raw_recipient = attrs.get("recipient")
|
|
355
|
+
raw_sender_username = attrs.get("participant_username", attrs.get("username"))
|
|
356
|
+
raw_recipient_username = attrs.get(
|
|
357
|
+
"recipient_username", attrs.get("peer_recipient_username")
|
|
358
|
+
)
|
|
359
|
+
identity: dict[str, Any] = {}
|
|
360
|
+
if raw_remote_jid_alt:
|
|
361
|
+
identity["remote_jid_alt"] = to_user_jid(raw_remote_jid_alt)
|
|
362
|
+
if raw_participant_alt:
|
|
363
|
+
identity["participant_alt"] = to_user_jid(raw_participant_alt)
|
|
364
|
+
if raw_sender_username is not None:
|
|
365
|
+
identity["sender_username"] = normalize_username(raw_sender_username)
|
|
366
|
+
if raw_recipient:
|
|
367
|
+
identity["recipient_jid"] = to_user_jid(raw_recipient)
|
|
368
|
+
if raw_recipient_alt:
|
|
369
|
+
identity["recipient_alt"] = to_user_jid(raw_recipient_alt)
|
|
370
|
+
if raw_recipient_username is not None:
|
|
371
|
+
identity["recipient_username"] = normalize_username(raw_recipient_username)
|
|
372
|
+
return identity
|
|
373
|
+
|
|
374
|
+
|
|
375
|
+
def _promote_recipient_addressing(identity: dict[str, Any]) -> dict[str, Any]:
|
|
376
|
+
promoted: dict[str, Any] = {}
|
|
377
|
+
if identity.get("recipient_alt") is not None:
|
|
378
|
+
promoted["remote_jid_alt"] = identity["recipient_alt"]
|
|
379
|
+
if identity.get("recipient_username") is not None:
|
|
380
|
+
promoted["recipient_username"] = identity["recipient_username"]
|
|
381
|
+
return promoted
|
|
382
|
+
|
|
383
|
+
|
|
384
|
+
def _split_sender(sender_jid: str | None) -> dict[str, Any] | None:
|
|
385
|
+
"""Split a sender JID into the ``user_jid``/``device`` pair key building needs."""
|
|
386
|
+
if not sender_jid:
|
|
387
|
+
return None
|
|
388
|
+
try:
|
|
389
|
+
address = parse_signal_address_from_jid(sender_jid)
|
|
390
|
+
except ValueError:
|
|
391
|
+
return None
|
|
392
|
+
return {"user_jid": f"{address.user}@{address.server}", "device": address.device}
|
|
393
|
+
|
|
394
|
+
|
|
395
|
+
def _build_incoming_message_key(
|
|
396
|
+
node: BinaryNode,
|
|
397
|
+
sender: dict[str, Any] | None,
|
|
398
|
+
options: WaIncomingMessageAckHandlerOptions,
|
|
399
|
+
destination_jid: str | None = None,
|
|
400
|
+
) -> tuple[WaIncomingMessageKey, str | None]:
|
|
401
|
+
from_attr = node.attrs.get("from")
|
|
402
|
+
from_user_jid = to_user_jid(from_attr) if from_attr else None
|
|
403
|
+
is_group = is_group_jid(from_user_jid) if from_user_jid else False
|
|
404
|
+
is_broadcast = is_broadcast_jid(from_user_jid) if from_user_jid else False
|
|
405
|
+
from_me = (
|
|
406
|
+
is_own_account_jid(
|
|
407
|
+
sender["user_jid"],
|
|
408
|
+
options.get_me_jid() if options.get_me_jid else None,
|
|
409
|
+
options.get_me_lid() if options.get_me_lid else None,
|
|
410
|
+
)
|
|
411
|
+
if sender
|
|
412
|
+
else False
|
|
413
|
+
)
|
|
414
|
+
is_self_sent_direct = from_me and not is_group and not is_broadcast
|
|
415
|
+
self_sent_chat = (
|
|
416
|
+
node.attrs.get("recipient", destination_jid) if is_self_sent_direct else None
|
|
417
|
+
)
|
|
418
|
+
chat_jid = to_user_jid(self_sent_chat) if self_sent_chat else from_user_jid
|
|
419
|
+
identity = _extract_message_identity_attrs(node.attrs)
|
|
420
|
+
push_name = node.attrs.get("notify")
|
|
421
|
+
key_identity = (
|
|
422
|
+
_promote_recipient_addressing(identity) if is_self_sent_direct else identity
|
|
423
|
+
)
|
|
424
|
+
participant: str | None = (
|
|
425
|
+
sender["user_jid"] if ((is_group or is_broadcast) and sender) else None
|
|
426
|
+
)
|
|
427
|
+
return (
|
|
428
|
+
WaIncomingMessageKey(
|
|
429
|
+
remote_jid=chat_jid if chat_jid is not None else "",
|
|
430
|
+
id=node.attrs.get("id", ""),
|
|
431
|
+
from_me=from_me,
|
|
432
|
+
is_group=is_group,
|
|
433
|
+
is_broadcast=is_broadcast,
|
|
434
|
+
is_newsletter=False,
|
|
435
|
+
sender_device=sender["device"] if sender else 0,
|
|
436
|
+
participant=participant,
|
|
437
|
+
**key_identity,
|
|
438
|
+
),
|
|
439
|
+
push_name,
|
|
440
|
+
)
|
|
441
|
+
|
|
442
|
+
|
|
443
|
+
def _pick_next_retry_count(node: BinaryNode) -> int:
|
|
444
|
+
retry_node = find_node_child(node, "retry")
|
|
445
|
+
parsed = parse_optional_int(retry_node.attrs.get("count") if retry_node else None)
|
|
446
|
+
if not parsed or parsed < 1:
|
|
447
|
+
return 1
|
|
448
|
+
return parsed + 1
|
|
449
|
+
|
|
450
|
+
|
|
451
|
+
def _build_incoming_event_raw_node(node: BinaryNode) -> BinaryNode:
|
|
452
|
+
content = node.content
|
|
453
|
+
if not isinstance(content, list) or len(content) == 0:
|
|
454
|
+
return node
|
|
455
|
+
children: list[BinaryNode] | None = None
|
|
456
|
+
for index, child in enumerate(content):
|
|
457
|
+
should_redact = child.tag == WA_MESSAGE_TAGS["ENC"] and isinstance(
|
|
458
|
+
child.content, (bytes, bytearray, str)
|
|
459
|
+
)
|
|
460
|
+
if not should_redact:
|
|
461
|
+
if children is not None:
|
|
462
|
+
children.append(child)
|
|
463
|
+
continue
|
|
464
|
+
if children is None:
|
|
465
|
+
children = list(content[:index])
|
|
466
|
+
children.append(BinaryNode(tag=child.tag, attrs=dict(child.attrs)))
|
|
467
|
+
if children is None:
|
|
468
|
+
return node
|
|
469
|
+
return BinaryNode(tag=node.tag, attrs=dict(node.attrs), content=children)
|
|
470
|
+
|
|
471
|
+
|
|
472
|
+
def resolve_web_message_info_author(
|
|
473
|
+
web_message_info: MessageDict,
|
|
474
|
+
me_jid: str | None = None,
|
|
475
|
+
thread_jid: str | None = None,
|
|
476
|
+
) -> str | None:
|
|
477
|
+
"""Resolve the author of a ``WebMessageInfo`` (history-sync participant rules)."""
|
|
478
|
+
key = web_message_info.get("key")
|
|
479
|
+
key = key if isinstance(key, dict) else {}
|
|
480
|
+
chat_jid = thread_jid if thread_jid is not None else key.get("remoteJid")
|
|
481
|
+
is_group_or_broadcast = (
|
|
482
|
+
(is_group_jid(chat_jid) or is_broadcast_jid(chat_jid)) if chat_jid else False
|
|
483
|
+
)
|
|
484
|
+
raw_self_author = web_message_info.get("originalSelfAuthorUserJidString", me_jid)
|
|
485
|
+
self_author = (
|
|
486
|
+
to_user_jid(raw_self_author)
|
|
487
|
+
if key.get("fromMe") is True and is_group_or_broadcast and raw_self_author
|
|
488
|
+
else None
|
|
489
|
+
)
|
|
490
|
+
return web_message_info.get("participant") or key.get("participant") or self_author
|
|
491
|
+
|
|
492
|
+
|
|
493
|
+
def build_recovered_incoming_event(
|
|
494
|
+
web_message_info: MessageDict, me_jid: str | None = None
|
|
495
|
+
) -> WaIncomingMessageEvent:
|
|
496
|
+
"""Rebuild a ``message`` event from a recovered ``WebMessageInfo``."""
|
|
497
|
+
key = web_message_info.get("key")
|
|
498
|
+
key = key if isinstance(key, dict) else {}
|
|
499
|
+
chat_jid = key.get("remoteJid")
|
|
500
|
+
from_me = key.get("fromMe") is True
|
|
501
|
+
is_group = is_group_jid(chat_jid) if chat_jid else False
|
|
502
|
+
is_broadcast = is_broadcast_jid(chat_jid) if chat_jid else False
|
|
503
|
+
participant = resolve_web_message_info_author(web_message_info, me_jid)
|
|
504
|
+
push_name = web_message_info.get("pushName")
|
|
505
|
+
raw_sender = None
|
|
506
|
+
if not from_me:
|
|
507
|
+
raw_sender = participant if (is_group or is_broadcast) else chat_jid
|
|
508
|
+
sender_device = 0
|
|
509
|
+
if raw_sender:
|
|
510
|
+
try:
|
|
511
|
+
sender_device = parse_signal_address_from_jid(raw_sender).device
|
|
512
|
+
except ValueError:
|
|
513
|
+
sender_device = 0
|
|
514
|
+
timestamp = web_message_info.get("messageTimestamp")
|
|
515
|
+
timestamp_seconds = _long_to_number(timestamp) if timestamp is not None else None
|
|
516
|
+
stanza_id = key.get("id")
|
|
517
|
+
message = web_message_info.get("message")
|
|
518
|
+
message = dict(message) if isinstance(message, dict) else None
|
|
519
|
+
expiration_seconds = pick_incoming_expiration_seconds(message)
|
|
520
|
+
raw_attrs: dict[str, str] = {}
|
|
521
|
+
if stanza_id is not None:
|
|
522
|
+
raw_attrs["id"] = stanza_id
|
|
523
|
+
if chat_jid is not None:
|
|
524
|
+
raw_attrs["from"] = chat_jid
|
|
525
|
+
if participant is not None:
|
|
526
|
+
raw_attrs["participant"] = participant
|
|
527
|
+
if push_name is not None:
|
|
528
|
+
raw_attrs["notify"] = push_name
|
|
529
|
+
return WaIncomingMessageEvent(
|
|
530
|
+
raw_node=BinaryNode(tag=WA_MESSAGE_TAGS["MESSAGE"], attrs=raw_attrs),
|
|
531
|
+
key=WaIncomingMessageKey(
|
|
532
|
+
remote_jid=chat_jid if chat_jid is not None else "",
|
|
533
|
+
id=stanza_id if stanza_id is not None else "",
|
|
534
|
+
from_me=from_me,
|
|
535
|
+
is_group=is_group,
|
|
536
|
+
is_broadcast=is_broadcast,
|
|
537
|
+
is_newsletter=False,
|
|
538
|
+
participant=participant,
|
|
539
|
+
sender_device=sender_device,
|
|
540
|
+
),
|
|
541
|
+
timestamp_seconds=timestamp_seconds,
|
|
542
|
+
expiration_seconds=expiration_seconds,
|
|
543
|
+
push_name=push_name,
|
|
544
|
+
message=message,
|
|
545
|
+
)
|
|
546
|
+
|
|
547
|
+
|
|
548
|
+
def _pick_sender_key_distribution_payload(
|
|
549
|
+
message: MessageDict,
|
|
550
|
+
) -> tuple[str, bytes] | None:
|
|
551
|
+
direct = _pick_direct_sender_key_distribution_payload(message)
|
|
552
|
+
if direct is not None:
|
|
553
|
+
return direct
|
|
554
|
+
device_sent = message.get("deviceSentMessage")
|
|
555
|
+
nested = device_sent.get("message") if isinstance(device_sent, dict) else None
|
|
556
|
+
if isinstance(nested, dict):
|
|
557
|
+
return _pick_sender_key_distribution_payload(nested)
|
|
558
|
+
return None
|
|
559
|
+
|
|
560
|
+
|
|
561
|
+
def _pick_direct_sender_key_distribution_payload(
|
|
562
|
+
message: MessageDict,
|
|
563
|
+
) -> tuple[str, bytes] | None:
|
|
564
|
+
for name in (
|
|
565
|
+
"senderKeyDistributionMessage",
|
|
566
|
+
"fastRatchetKeySenderKeyDistributionMessage",
|
|
567
|
+
):
|
|
568
|
+
distribution = message.get(name)
|
|
569
|
+
if not isinstance(distribution, dict):
|
|
570
|
+
continue
|
|
571
|
+
group_id = distribution.get("groupId")
|
|
572
|
+
payload = distribution.get("axolotlSenderKeyDistributionMessage")
|
|
573
|
+
if group_id and isinstance(payload, (bytes, bytearray)):
|
|
574
|
+
return group_id, bytes(payload)
|
|
575
|
+
return None
|
|
576
|
+
|
|
577
|
+
|
|
578
|
+
def _should_emit_incoming_message(message: MessageDict) -> bool:
|
|
579
|
+
if _pick_direct_sender_key_distribution_payload(message) is None:
|
|
580
|
+
return True
|
|
581
|
+
for name, value in message.items():
|
|
582
|
+
if name in (
|
|
583
|
+
"senderKeyDistributionMessage",
|
|
584
|
+
"fastRatchetKeySenderKeyDistributionMessage",
|
|
585
|
+
"messageContextInfo",
|
|
586
|
+
"$$unknownFieldCount",
|
|
587
|
+
):
|
|
588
|
+
continue
|
|
589
|
+
if value is None:
|
|
590
|
+
continue
|
|
591
|
+
return True
|
|
592
|
+
return False
|
|
593
|
+
|
|
594
|
+
|
|
595
|
+
async def _send_retry_receipt_for_decrypt_failure(
|
|
596
|
+
node: BinaryNode,
|
|
597
|
+
options: WaIncomingMessageAckHandlerOptions,
|
|
598
|
+
error: Any,
|
|
599
|
+
enc_type: str,
|
|
600
|
+
) -> bool:
|
|
601
|
+
stanza_id = node.attrs.get("id")
|
|
602
|
+
from_jid = node.attrs.get("from")
|
|
603
|
+
if not stanza_id or not from_jid:
|
|
604
|
+
return False
|
|
605
|
+
retry_context = WaRetryDecryptFailureContext(
|
|
606
|
+
message_node=node,
|
|
607
|
+
stanza_id=stanza_id,
|
|
608
|
+
from_=from_jid,
|
|
609
|
+
participant=node.attrs.get("participant"),
|
|
610
|
+
recipient=node.attrs.get("recipient"),
|
|
611
|
+
t=node.attrs.get("t"),
|
|
612
|
+
)
|
|
613
|
+
if options.on_decrypt_failure is not None:
|
|
614
|
+
return await options.on_decrypt_failure(retry_context, error)
|
|
615
|
+
retry_receipt = build_retry_receipt_node(
|
|
616
|
+
node, id=stanza_id, to=from_jid, retry_count=_pick_next_retry_count(node)
|
|
617
|
+
)
|
|
618
|
+
try:
|
|
619
|
+
await options.send_node(retry_receipt)
|
|
620
|
+
options.logger.trace(
|
|
621
|
+
"sent retry receipt for undecryptable incoming message",
|
|
622
|
+
{
|
|
623
|
+
"id": stanza_id,
|
|
624
|
+
"to": from_jid,
|
|
625
|
+
"participant": retry_receipt.attrs.get("participant"),
|
|
626
|
+
"encType": enc_type,
|
|
627
|
+
},
|
|
628
|
+
)
|
|
629
|
+
return True
|
|
630
|
+
except Exception as retry_error:
|
|
631
|
+
options.logger.warn(
|
|
632
|
+
"failed to send retry receipt for incoming message",
|
|
633
|
+
{
|
|
634
|
+
"id": stanza_id,
|
|
635
|
+
"from": from_jid,
|
|
636
|
+
"participant": node.attrs.get("participant"),
|
|
637
|
+
"encType": enc_type,
|
|
638
|
+
"message": str(to_error(retry_error)),
|
|
639
|
+
},
|
|
640
|
+
)
|
|
641
|
+
return False
|
|
642
|
+
|
|
643
|
+
|
|
644
|
+
async def _process_msmsg_enc_node(
|
|
645
|
+
node: BinaryNode,
|
|
646
|
+
enc_node: BinaryNode,
|
|
647
|
+
sender_jid: str | None,
|
|
648
|
+
options: WaIncomingMessageAckHandlerOptions,
|
|
649
|
+
) -> tuple[bool, str, Any]:
|
|
650
|
+
try:
|
|
651
|
+
payload = decode_node_content_base64_or_bytes(enc_node.content, "message.enc")
|
|
652
|
+
decoded = WAProto_pb2.MessageSecretMessage.FromString(payload)
|
|
653
|
+
if (
|
|
654
|
+
not decoded.HasField("encIv")
|
|
655
|
+
or not decoded.encIv
|
|
656
|
+
or not decoded.HasField("encPayload")
|
|
657
|
+
or not decoded.encPayload
|
|
658
|
+
):
|
|
659
|
+
options.logger.warn(
|
|
660
|
+
"msmsg payload missing encIv/encPayload",
|
|
661
|
+
{"id": node.attrs.get("id"), "from": node.attrs.get("from")},
|
|
662
|
+
)
|
|
663
|
+
await _emit_unhandled(node, options, "message.msmsg.missing_payload")
|
|
664
|
+
return False, "msmsg", None
|
|
665
|
+
inner: MessageDict = {
|
|
666
|
+
"secretEncryptedMessage": {
|
|
667
|
+
"encIv": bytes(decoded.encIv),
|
|
668
|
+
"encPayload": bytes(decoded.encPayload),
|
|
669
|
+
}
|
|
670
|
+
}
|
|
671
|
+
key, push_name = _build_incoming_message_key(
|
|
672
|
+
node, _split_sender(sender_jid), options
|
|
673
|
+
)
|
|
674
|
+
if options.emit_incoming_message is not None:
|
|
675
|
+
await _await_if_needed(
|
|
676
|
+
options.emit_incoming_message(
|
|
677
|
+
WaIncomingMessageEvent(
|
|
678
|
+
raw_node=_build_incoming_event_raw_node(node),
|
|
679
|
+
key=key,
|
|
680
|
+
stanza_type=node.attrs.get("type"),
|
|
681
|
+
offline="offline" in node.attrs,
|
|
682
|
+
timestamp_seconds=parse_optional_int(node.attrs.get("t")),
|
|
683
|
+
push_name=push_name,
|
|
684
|
+
message=inner,
|
|
685
|
+
)
|
|
686
|
+
)
|
|
687
|
+
)
|
|
688
|
+
return True, "msmsg", None
|
|
689
|
+
except Exception as error:
|
|
690
|
+
options.logger.warn(
|
|
691
|
+
"failed to decode msmsg payload",
|
|
692
|
+
{
|
|
693
|
+
"id": node.attrs.get("id"),
|
|
694
|
+
"from": node.attrs.get("from"),
|
|
695
|
+
"message": str(to_error(error)),
|
|
696
|
+
},
|
|
697
|
+
)
|
|
698
|
+
await _emit_unhandled(node, options, "message.msmsg.decode_failed")
|
|
699
|
+
return False, "msmsg", error
|
|
700
|
+
|
|
701
|
+
|
|
702
|
+
async def _await_if_needed(value: Any) -> None:
|
|
703
|
+
"""Await ``value`` when awaitable (sync-or-async callback bridge)."""
|
|
704
|
+
if inspect.isawaitable(value):
|
|
705
|
+
await value
|
|
706
|
+
|
|
707
|
+
|
|
708
|
+
async def _emit_unhandled(
|
|
709
|
+
node: BinaryNode, options: WaIncomingMessageAckHandlerOptions, reason: str
|
|
710
|
+
) -> None:
|
|
711
|
+
if options.emit_unhandled_stanza is not None:
|
|
712
|
+
await _await_if_needed(
|
|
713
|
+
options.emit_unhandled_stanza(
|
|
714
|
+
WaIncomingUnhandledStanzaEvent(
|
|
715
|
+
raw_node=_build_incoming_event_raw_node(node),
|
|
716
|
+
stanza_id=node.attrs.get("id"),
|
|
717
|
+
chat_jid=node.attrs.get("from"),
|
|
718
|
+
stanza_type=node.attrs.get("type"),
|
|
719
|
+
offline="offline" in node.attrs,
|
|
720
|
+
reason=reason,
|
|
721
|
+
)
|
|
722
|
+
)
|
|
723
|
+
)
|
|
724
|
+
|
|
725
|
+
|
|
726
|
+
async def _emit_decrypted_payload(
|
|
727
|
+
node: BinaryNode,
|
|
728
|
+
options: WaIncomingMessageAckHandlerOptions,
|
|
729
|
+
enc_index: int,
|
|
730
|
+
enc_type: str,
|
|
731
|
+
plaintext: bytes,
|
|
732
|
+
) -> None:
|
|
733
|
+
if options.emit_decrypted_payload is None:
|
|
734
|
+
return
|
|
735
|
+
try:
|
|
736
|
+
await _await_if_needed(
|
|
737
|
+
options.emit_decrypted_payload(
|
|
738
|
+
lambda: WaIncomingDecryptedPayloadEvent(
|
|
739
|
+
raw_node=_build_incoming_event_raw_node(node),
|
|
740
|
+
stanza_id=node.attrs.get("id"),
|
|
741
|
+
chat_jid=node.attrs.get("from"),
|
|
742
|
+
stanza_type=node.attrs.get("type"),
|
|
743
|
+
offline="offline" in node.attrs,
|
|
744
|
+
enc_index=enc_index,
|
|
745
|
+
enc_type=enc_type,
|
|
746
|
+
plaintext=bytes(plaintext),
|
|
747
|
+
)
|
|
748
|
+
)
|
|
749
|
+
)
|
|
750
|
+
except Exception as error:
|
|
751
|
+
options.logger.warn(
|
|
752
|
+
"decrypted payload observer threw",
|
|
753
|
+
{
|
|
754
|
+
"id": node.attrs.get("id"),
|
|
755
|
+
"encType": enc_type,
|
|
756
|
+
"message": str(to_error(error)),
|
|
757
|
+
},
|
|
758
|
+
)
|
|
759
|
+
|
|
760
|
+
|
|
761
|
+
async def _decrypt_and_process_enc_node(
|
|
762
|
+
node: BinaryNode,
|
|
763
|
+
enc_node: BinaryNode,
|
|
764
|
+
enc_type: str,
|
|
765
|
+
enc_index: int,
|
|
766
|
+
sender_jid: str,
|
|
767
|
+
options: WaIncomingMessageAckHandlerOptions,
|
|
768
|
+
decrypt: Callable[[bytes, SignalAddress], Awaitable[bytes]],
|
|
769
|
+
) -> tuple[bool, str, Any]:
|
|
770
|
+
log_context = {
|
|
771
|
+
"id": node.attrs.get("id"),
|
|
772
|
+
"from": node.attrs.get("from"),
|
|
773
|
+
"participant": node.attrs.get("participant"),
|
|
774
|
+
}
|
|
775
|
+
try:
|
|
776
|
+
sender_address = parse_signal_address_from_jid(sender_jid)
|
|
777
|
+
decrypted_payload = await decrypt(
|
|
778
|
+
decode_node_content_base64_or_bytes(enc_node.content, "message.enc"),
|
|
779
|
+
sender_address,
|
|
780
|
+
)
|
|
781
|
+
unpadded = unpad_pkcs7(decrypted_payload)
|
|
782
|
+
await _emit_decrypted_payload(
|
|
783
|
+
node, options, enc_index, enc_type, bytes(unpadded)
|
|
784
|
+
)
|
|
785
|
+
decoded_message = proto_message_to_dict(
|
|
786
|
+
WAProto_pb2.Message.FromString(bytes(unpadded))
|
|
787
|
+
)
|
|
788
|
+
unwrapped = unwrap_device_sent_message(decoded_message)
|
|
789
|
+
message = unwrapped if unwrapped is not None else decoded_message
|
|
790
|
+
sender_key_distribution = _pick_sender_key_distribution_payload(message)
|
|
791
|
+
if (
|
|
792
|
+
sender_key_distribution is not None
|
|
793
|
+
and options.sender_key_manager is not None
|
|
794
|
+
):
|
|
795
|
+
group_id, payload_bytes = sender_key_distribution
|
|
796
|
+
try:
|
|
797
|
+
await (
|
|
798
|
+
options.sender_key_manager.process_sender_key_distribution_payload(
|
|
799
|
+
group_id,
|
|
800
|
+
sender_address,
|
|
801
|
+
payload_bytes,
|
|
802
|
+
)
|
|
803
|
+
)
|
|
804
|
+
options.logger.trace(
|
|
805
|
+
"processed incoming sender key distribution",
|
|
806
|
+
{**log_context, "groupId": group_id},
|
|
807
|
+
)
|
|
808
|
+
except Exception as error:
|
|
809
|
+
options.logger.warn(
|
|
810
|
+
"failed to process incoming sender key distribution",
|
|
811
|
+
{
|
|
812
|
+
**log_context,
|
|
813
|
+
"groupId": group_id,
|
|
814
|
+
"message": str(to_error(error)),
|
|
815
|
+
},
|
|
816
|
+
)
|
|
817
|
+
if _should_emit_incoming_message(message):
|
|
818
|
+
sender_info = {
|
|
819
|
+
"user_jid": f"{sender_address.user}@{sender_address.server}",
|
|
820
|
+
"device": sender_address.device,
|
|
821
|
+
}
|
|
822
|
+
device_sent = decoded_message.get("deviceSentMessage")
|
|
823
|
+
raw_destination = (
|
|
824
|
+
device_sent.get("destinationJid")
|
|
825
|
+
if isinstance(device_sent, dict)
|
|
826
|
+
else None
|
|
827
|
+
)
|
|
828
|
+
destination = raw_destination if isinstance(raw_destination, str) else None
|
|
829
|
+
key, push_name = _build_incoming_message_key(
|
|
830
|
+
node, sender_info, options, destination
|
|
831
|
+
)
|
|
832
|
+
expiration_seconds = pick_incoming_expiration_seconds(message)
|
|
833
|
+
if options.emit_incoming_message is not None:
|
|
834
|
+
await _await_if_needed(
|
|
835
|
+
options.emit_incoming_message(
|
|
836
|
+
WaIncomingMessageEvent(
|
|
837
|
+
raw_node=_build_incoming_event_raw_node(node),
|
|
838
|
+
key=key,
|
|
839
|
+
stanza_type=node.attrs.get("type"),
|
|
840
|
+
offline="offline" in node.attrs,
|
|
841
|
+
timestamp_seconds=parse_optional_int(node.attrs.get("t")),
|
|
842
|
+
expiration_seconds=expiration_seconds,
|
|
843
|
+
push_name=push_name,
|
|
844
|
+
message=message,
|
|
845
|
+
)
|
|
846
|
+
)
|
|
847
|
+
)
|
|
848
|
+
return True, enc_type, None
|
|
849
|
+
except Exception as error:
|
|
850
|
+
options.logger.warn(
|
|
851
|
+
"failed to decrypt incoming message",
|
|
852
|
+
{**log_context, "encType": enc_type, "message": str(to_error(error))},
|
|
853
|
+
)
|
|
854
|
+
await _emit_unhandled(node, options, f"message.decrypt_failed.{enc_type}")
|
|
855
|
+
return False, enc_type, error
|
|
856
|
+
|
|
857
|
+
|
|
858
|
+
def resolve_unavailable_message_kind(node: BinaryNode) -> str | None:
|
|
859
|
+
"""Classify a stanza carrying ``<unavailable/>`` (``None`` when absent)."""
|
|
860
|
+
unavailable = find_node_child(node, "unavailable")
|
|
861
|
+
if unavailable is None:
|
|
862
|
+
return None
|
|
863
|
+
if find_node_child(node, "bot") is not None:
|
|
864
|
+
return "bot"
|
|
865
|
+
if unavailable.attrs.get("hosted") == "true":
|
|
866
|
+
return "hosted"
|
|
867
|
+
unavailable_type = unavailable.attrs.get("type")
|
|
868
|
+
if unavailable_type in ("view_once", "sender_drop"):
|
|
869
|
+
return unavailable_type
|
|
870
|
+
return "other"
|
|
871
|
+
|
|
872
|
+
|
|
873
|
+
async def handle_incoming_message_ack(
|
|
874
|
+
node: BinaryNode, options: WaIncomingMessageAckHandlerOptions
|
|
875
|
+
) -> bool:
|
|
876
|
+
"""Ack/receipt path for one inbound ``<message>`` stanza."""
|
|
877
|
+
if node.tag != WA_MESSAGE_TAGS["MESSAGE"]:
|
|
878
|
+
return False
|
|
879
|
+
from_jid = node.attrs.get("from")
|
|
880
|
+
if from_jid and is_newsletter_jid(from_jid):
|
|
881
|
+
return await _handle_incoming_newsletter_message(node, options)
|
|
882
|
+
|
|
883
|
+
should_send_standard_receipt = True
|
|
884
|
+
content = node.content
|
|
885
|
+
if isinstance(content, list) and len(content) > 0:
|
|
886
|
+
sender_jid = node.attrs.get("participant", node.attrs.get("from"))
|
|
887
|
+
has_successful_decrypt = False
|
|
888
|
+
first_decrypt_failure: tuple[bool, str, Any] | None = None
|
|
889
|
+
skmsg_failure: tuple[bool, str, Any] | None = None
|
|
890
|
+
enc_count = 0
|
|
891
|
+
first_enc_type: str | None = None
|
|
892
|
+
for child in content:
|
|
893
|
+
if child.tag != WA_MESSAGE_TAGS["ENC"]:
|
|
894
|
+
continue
|
|
895
|
+
enc_count += 1
|
|
896
|
+
if first_enc_type is None:
|
|
897
|
+
first_enc_type = child.attrs.get("type")
|
|
898
|
+
result: tuple[bool, str, Any] | None = None
|
|
899
|
+
child_type = child.attrs.get("type")
|
|
900
|
+
if child_type == "skmsg":
|
|
901
|
+
if (
|
|
902
|
+
not sender_jid
|
|
903
|
+
or not node.attrs.get("from")
|
|
904
|
+
or options.sender_key_manager is None
|
|
905
|
+
):
|
|
906
|
+
await _emit_unhandled(
|
|
907
|
+
node, options, "message.skmsg.missing_group_context"
|
|
908
|
+
)
|
|
909
|
+
continue
|
|
910
|
+
group_id = node.attrs.get("from", "")
|
|
911
|
+
manager = options.sender_key_manager
|
|
912
|
+
sender_value = sender_jid
|
|
913
|
+
|
|
914
|
+
async def decrypt_group(
|
|
915
|
+
ciphertext: bytes, sender_address: SignalAddress
|
|
916
|
+
) -> bytes:
|
|
917
|
+
return await manager.decrypt_group_message(
|
|
918
|
+
GroupSenderKeyCiphertext(
|
|
919
|
+
group_id=group_id,
|
|
920
|
+
sender=sender_address,
|
|
921
|
+
ciphertext=ciphertext,
|
|
922
|
+
)
|
|
923
|
+
)
|
|
924
|
+
|
|
925
|
+
result = await _decrypt_and_process_enc_node(
|
|
926
|
+
node,
|
|
927
|
+
child,
|
|
928
|
+
"skmsg",
|
|
929
|
+
enc_count - 1,
|
|
930
|
+
sender_value,
|
|
931
|
+
options,
|
|
932
|
+
decrypt_group,
|
|
933
|
+
)
|
|
934
|
+
elif child_type in ("msg", "pkmsg"):
|
|
935
|
+
if not sender_jid or options.signal_protocol is None:
|
|
936
|
+
continue
|
|
937
|
+
enc_type_value = child_type
|
|
938
|
+
protocol = options.signal_protocol
|
|
939
|
+
sender_value = sender_jid
|
|
940
|
+
|
|
941
|
+
async def decrypt_pairwise(
|
|
942
|
+
ciphertext: bytes, sender_address: SignalAddress
|
|
943
|
+
) -> bytes:
|
|
944
|
+
session_jid = canonicalize_own_account_jid(
|
|
945
|
+
sender_value,
|
|
946
|
+
options.get_me_jid() if options.get_me_jid else None,
|
|
947
|
+
options.get_me_lid() if options.get_me_lid else None,
|
|
948
|
+
)
|
|
949
|
+
if session_jid == sender_value:
|
|
950
|
+
address = sender_address
|
|
951
|
+
else:
|
|
952
|
+
address = parse_signal_address_from_jid(session_jid)
|
|
953
|
+
return await protocol.decrypt_message(
|
|
954
|
+
address,
|
|
955
|
+
SignalDecryptEnvelope(
|
|
956
|
+
type=enc_type_value, ciphertext=ciphertext
|
|
957
|
+
),
|
|
958
|
+
)
|
|
959
|
+
|
|
960
|
+
result = await _decrypt_and_process_enc_node(
|
|
961
|
+
node,
|
|
962
|
+
child,
|
|
963
|
+
enc_type_value,
|
|
964
|
+
enc_count - 1,
|
|
965
|
+
sender_value,
|
|
966
|
+
options,
|
|
967
|
+
decrypt_pairwise,
|
|
968
|
+
)
|
|
969
|
+
elif child_type == "msmsg":
|
|
970
|
+
result = await _process_msmsg_enc_node(node, child, sender_jid, options)
|
|
971
|
+
else:
|
|
972
|
+
continue
|
|
973
|
+
if result[0]:
|
|
974
|
+
has_successful_decrypt = True
|
|
975
|
+
else:
|
|
976
|
+
if first_decrypt_failure is None:
|
|
977
|
+
first_decrypt_failure = result
|
|
978
|
+
if child.attrs.get("type") == "skmsg" and skmsg_failure is None:
|
|
979
|
+
skmsg_failure = result
|
|
980
|
+
if enc_count > 1 and first_enc_type == "skmsg":
|
|
981
|
+
options.logger.warn(
|
|
982
|
+
"incoming message enc order is unexpected: skmsg first",
|
|
983
|
+
{
|
|
984
|
+
"id": node.attrs.get("id"),
|
|
985
|
+
"from": node.attrs.get("from"),
|
|
986
|
+
"participant": node.attrs.get("participant"),
|
|
987
|
+
"encCount": enc_count,
|
|
988
|
+
},
|
|
989
|
+
)
|
|
990
|
+
decrypt_failure = (
|
|
991
|
+
skmsg_failure if skmsg_failure is not None else first_decrypt_failure
|
|
992
|
+
)
|
|
993
|
+
if decrypt_failure is not None and (
|
|
994
|
+
not has_successful_decrypt or skmsg_failure is not None
|
|
995
|
+
):
|
|
996
|
+
await _send_retry_receipt_for_decrypt_failure(
|
|
997
|
+
node, options, decrypt_failure[2], decrypt_failure[1]
|
|
998
|
+
)
|
|
999
|
+
should_send_standard_receipt = False
|
|
1000
|
+
|
|
1001
|
+
stanza_id = node.attrs.get("id")
|
|
1002
|
+
if not stanza_id or not from_jid:
|
|
1003
|
+
options.logger.warn(
|
|
1004
|
+
"incoming message missing required attrs for ack/receipt",
|
|
1005
|
+
{
|
|
1006
|
+
"hasId": bool(stanza_id),
|
|
1007
|
+
"hasFrom": bool(from_jid),
|
|
1008
|
+
"type": node.attrs.get("type"),
|
|
1009
|
+
},
|
|
1010
|
+
)
|
|
1011
|
+
return True
|
|
1012
|
+
|
|
1013
|
+
if node.attrs.get("type") == WA_MESSAGE_TYPES["MEDIA_NOTIFY"]:
|
|
1014
|
+
ack_node = build_message_ack_node(
|
|
1015
|
+
node,
|
|
1016
|
+
id=stanza_id,
|
|
1017
|
+
to=from_jid,
|
|
1018
|
+
from_=options.get_me_jid() if options.get_me_jid else None,
|
|
1019
|
+
)
|
|
1020
|
+
options.logger.trace(
|
|
1021
|
+
"sending inbound message ack",
|
|
1022
|
+
{
|
|
1023
|
+
"id": stanza_id,
|
|
1024
|
+
"to": from_jid,
|
|
1025
|
+
"type": ack_node.attrs.get("type"),
|
|
1026
|
+
"participant": ack_node.attrs.get("participant"),
|
|
1027
|
+
},
|
|
1028
|
+
)
|
|
1029
|
+
await options.send_node(ack_node)
|
|
1030
|
+
return True
|
|
1031
|
+
|
|
1032
|
+
kind = resolve_unavailable_message_kind(node)
|
|
1033
|
+
if kind is not None:
|
|
1034
|
+
sender_jid = node.attrs.get("participant", node.attrs.get("from"))
|
|
1035
|
+
key, push_name = _build_incoming_message_key(
|
|
1036
|
+
node, _split_sender(sender_jid), options
|
|
1037
|
+
)
|
|
1038
|
+
request_resend = options.request_placeholder_resend
|
|
1039
|
+
resend_requested = (
|
|
1040
|
+
request_resend(
|
|
1041
|
+
WaRetryDecryptFailureContext(
|
|
1042
|
+
message_node=node,
|
|
1043
|
+
stanza_id=stanza_id,
|
|
1044
|
+
from_=from_jid,
|
|
1045
|
+
participant=node.attrs.get("participant"),
|
|
1046
|
+
recipient=node.attrs.get("recipient"),
|
|
1047
|
+
t=node.attrs.get("t"),
|
|
1048
|
+
)
|
|
1049
|
+
)
|
|
1050
|
+
is True
|
|
1051
|
+
if request_resend is not None
|
|
1052
|
+
else False
|
|
1053
|
+
)
|
|
1054
|
+
if options.emit_unavailable_message is not None:
|
|
1055
|
+
await _await_if_needed(
|
|
1056
|
+
options.emit_unavailable_message(
|
|
1057
|
+
WaIncomingUnavailableMessageEvent(
|
|
1058
|
+
raw_node=_build_incoming_event_raw_node(node),
|
|
1059
|
+
key=key,
|
|
1060
|
+
kind=kind,
|
|
1061
|
+
resend_requested=resend_requested,
|
|
1062
|
+
stanza_type=node.attrs.get("type"),
|
|
1063
|
+
offline="offline" in node.attrs,
|
|
1064
|
+
timestamp_seconds=parse_optional_int(node.attrs.get("t")),
|
|
1065
|
+
push_name=push_name,
|
|
1066
|
+
)
|
|
1067
|
+
)
|
|
1068
|
+
)
|
|
1069
|
+
ack_node = build_message_ack_node(
|
|
1070
|
+
node,
|
|
1071
|
+
id=stanza_id,
|
|
1072
|
+
to=from_jid,
|
|
1073
|
+
from_=options.get_me_jid() if options.get_me_jid else None,
|
|
1074
|
+
)
|
|
1075
|
+
options.logger.trace(
|
|
1076
|
+
"acking unavailable incoming message",
|
|
1077
|
+
{
|
|
1078
|
+
"id": stanza_id,
|
|
1079
|
+
"to": from_jid,
|
|
1080
|
+
"type": ack_node.attrs.get("type"),
|
|
1081
|
+
"participant": ack_node.attrs.get("participant"),
|
|
1082
|
+
"unavailableKind": kind,
|
|
1083
|
+
"resendRequested": resend_requested,
|
|
1084
|
+
},
|
|
1085
|
+
)
|
|
1086
|
+
await options.send_node(ack_node)
|
|
1087
|
+
return True
|
|
1088
|
+
|
|
1089
|
+
if not should_send_standard_receipt:
|
|
1090
|
+
return True
|
|
1091
|
+
|
|
1092
|
+
receipt_node = build_delivery_receipt_node(node, id=stanza_id, to=from_jid)
|
|
1093
|
+
options.logger.trace(
|
|
1094
|
+
"sending inbound message receipt",
|
|
1095
|
+
{
|
|
1096
|
+
"id": stanza_id,
|
|
1097
|
+
"to": from_jid,
|
|
1098
|
+
"type": receipt_node.attrs.get("type"),
|
|
1099
|
+
"participant": receipt_node.attrs.get("participant"),
|
|
1100
|
+
},
|
|
1101
|
+
)
|
|
1102
|
+
await options.send_node(receipt_node)
|
|
1103
|
+
return True
|
|
1104
|
+
|
|
1105
|
+
|
|
1106
|
+
async def _handle_incoming_newsletter_message(
|
|
1107
|
+
node: BinaryNode, options: WaIncomingMessageAckHandlerOptions
|
|
1108
|
+
) -> bool:
|
|
1109
|
+
# The newsletter plaintext path emits through the same callback (the key
|
|
1110
|
+
# shapes match ``src/client/types.ts``); the ignore covers the nominal
|
|
1111
|
+
# dataclass difference.
|
|
1112
|
+
process_incoming_newsletter_message(
|
|
1113
|
+
node,
|
|
1114
|
+
ProcessNewsletterMessageOptions(
|
|
1115
|
+
logger=options.logger,
|
|
1116
|
+
# The newsletter plaintext path emits through the same callback;
|
|
1117
|
+
# both event shapes mirror ``src/client/types.ts`` field-for-field.
|
|
1118
|
+
emit_incoming_message=options.emit_incoming_message, # type: ignore[arg-type]
|
|
1119
|
+
emit_newsletter_message_update=options.emit_newsletter_message_update,
|
|
1120
|
+
emit_unhandled_stanza=options.emit_unhandled_stanza, # type: ignore[arg-type]
|
|
1121
|
+
),
|
|
1122
|
+
)
|
|
1123
|
+
stanza_id = node.attrs.get("id")
|
|
1124
|
+
from_jid = node.attrs.get("from")
|
|
1125
|
+
if not stanza_id or not from_jid:
|
|
1126
|
+
options.logger.warn(
|
|
1127
|
+
"incoming newsletter message missing required attrs for ack",
|
|
1128
|
+
{
|
|
1129
|
+
"hasId": bool(stanza_id),
|
|
1130
|
+
"hasFrom": bool(from_jid),
|
|
1131
|
+
"type": node.attrs.get("type"),
|
|
1132
|
+
},
|
|
1133
|
+
)
|
|
1134
|
+
return True
|
|
1135
|
+
ack_node = build_message_ack_node(node, id=stanza_id, to=from_jid)
|
|
1136
|
+
options.logger.trace(
|
|
1137
|
+
"sending inbound newsletter message ack",
|
|
1138
|
+
{"id": stanza_id, "to": from_jid, "type": ack_node.attrs.get("type")},
|
|
1139
|
+
)
|
|
1140
|
+
await options.send_node(ack_node)
|
|
1141
|
+
return True
|