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,1079 @@
|
|
|
1
|
+
"""Inbound stanza dispatch: acks, receipts, failures, notifications, calls.
|
|
2
|
+
|
|
3
|
+
Mirrors ``src/client/events/incoming.ts``: the ``buildInboundAck`` /
|
|
4
|
+
``createIncomingBaseEvent`` / ``sendSafeAck`` helpers, the
|
|
5
|
+
``createUnhandledIncomingNodeEvent`` / ``createInfoBulletinNotificationEvent``
|
|
6
|
+
factories, and one ``create*Handler`` per stanza family. Handlers emit typed
|
|
7
|
+
events through caller-supplied callbacks and always ack the stanza.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from __future__ import annotations
|
|
11
|
+
|
|
12
|
+
import asyncio
|
|
13
|
+
from collections.abc import Awaitable, Callable
|
|
14
|
+
from dataclasses import dataclass, field
|
|
15
|
+
from typing import TYPE_CHECKING, Any, Protocol, TypeVar
|
|
16
|
+
|
|
17
|
+
from zapy._client.events.business import (
|
|
18
|
+
WaBusinessEvent,
|
|
19
|
+
parse_business_notification_events,
|
|
20
|
+
)
|
|
21
|
+
from zapy._client.events.call import CALL_RECEIPT_PAYLOAD_TAGS, parse_call_node
|
|
22
|
+
from zapy._client.events.group import WaGroupEvent, parse_group_notification_events
|
|
23
|
+
from zapy._client.events.mex_notification import (
|
|
24
|
+
WaMexNotificationParsed,
|
|
25
|
+
parse_mex_notification,
|
|
26
|
+
)
|
|
27
|
+
from zapy._client.events.picture import (
|
|
28
|
+
WaPictureEvent,
|
|
29
|
+
parse_picture_notification_events,
|
|
30
|
+
)
|
|
31
|
+
from zapy._client.events.receipt import extract_receipt_ids
|
|
32
|
+
from zapy._client.events.registration import (
|
|
33
|
+
ParsedAccountTakeoverNotice,
|
|
34
|
+
ParsedRegistrationCode,
|
|
35
|
+
parse_registration_notification,
|
|
36
|
+
)
|
|
37
|
+
from zapy._client.events.username import normalize_username
|
|
38
|
+
from zapy._infra.log import Logger
|
|
39
|
+
from zapy._protocol import defaults as _defaults
|
|
40
|
+
from zapy._protocol.nodes import WA_NODE_TAGS
|
|
41
|
+
from zapy._protocol.notification import WA_NOTIFICATION_TYPES
|
|
42
|
+
from zapy._transport.node.builders.global_ import build_ack_node
|
|
43
|
+
from zapy._transport.node.helpers import (
|
|
44
|
+
get_first_node_child,
|
|
45
|
+
get_node_children_non_empty_attr_values_by_tag,
|
|
46
|
+
)
|
|
47
|
+
from zapy._transport.types import BinaryNode
|
|
48
|
+
from zapy._util.primitives import parse_optional_int, to_error
|
|
49
|
+
|
|
50
|
+
if TYPE_CHECKING:
|
|
51
|
+
from zapy._auth.types import WaAuthCredentials
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def _error_message(error: object) -> str:
|
|
55
|
+
normalized = to_error(error)
|
|
56
|
+
message = getattr(normalized, "message", None)
|
|
57
|
+
return message if isinstance(message, str) else str(normalized)
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
SendNode = Callable[[BinaryNode], Awaitable[None]]
|
|
61
|
+
|
|
62
|
+
# Mirrors ``WA_MESSAGE_TAGS`` in ``src/protocol/message.ts``.
|
|
63
|
+
_MESSAGE_TAG = "message"
|
|
64
|
+
_RECEIPT_TAG = "receipt"
|
|
65
|
+
_ACK_TAG = "ack"
|
|
66
|
+
|
|
67
|
+
# Mirrors ``WA_MESSAGE_TYPES`` in ``src/protocol/message.ts`` (values only).
|
|
68
|
+
_ACK_CLASS_MESSAGE = "message"
|
|
69
|
+
_ACK_CLASS_CALL = "call"
|
|
70
|
+
_RECEIPT_TYPE_RETRY = "retry"
|
|
71
|
+
_RECEIPT_TYPE_ENC_REKEY_RETRY = "enc_rekey_retry"
|
|
72
|
+
_RECEIPT_TYPE_PEER = "peer_msg"
|
|
73
|
+
_RECEIPT_TYPE_SENDER = "sender"
|
|
74
|
+
_RECEIPT_TYPE_HISTORY_SYNC = "hist_sync"
|
|
75
|
+
_RECEIPT_TYPE_SERVER_ERROR = "server-error"
|
|
76
|
+
_RECEIPT_TYPE_DELIVERY = "delivery"
|
|
77
|
+
_RECEIPT_TYPE_READ = "read"
|
|
78
|
+
_RECEIPT_TYPE_READ_SELF = "read-self"
|
|
79
|
+
_RECEIPT_TYPE_PLAYED = "played"
|
|
80
|
+
_RECEIPT_TYPE_PLAYED_SELF = "played-self"
|
|
81
|
+
_RECEIPT_TYPE_INACTIVE = "inactive"
|
|
82
|
+
_RECEIPT_TYPE_VIEW = "view"
|
|
83
|
+
|
|
84
|
+
# Mirrors ``WA_DISCONNECT_REASONS`` in ``src/protocol/stream.ts``. The shared
|
|
85
|
+
# stream table lands with the protocol layer; values copied verbatim.
|
|
86
|
+
DISCONNECT_CLIENT_DISCONNECTED = "client_disconnected"
|
|
87
|
+
DISCONNECT_COMMS_STOPPED = "comms_stopped"
|
|
88
|
+
DISCONNECT_STREAM_ERROR_REPLACED = "stream_error_replaced"
|
|
89
|
+
DISCONNECT_STREAM_ERROR_DEVICE_REMOVED = "stream_error_device_removed"
|
|
90
|
+
DISCONNECT_STREAM_ERROR_ACK = "stream_error_ack"
|
|
91
|
+
DISCONNECT_STREAM_ERROR_XML_NOT_WELL_FORMED = "stream_error_xml_not_well_formed"
|
|
92
|
+
DISCONNECT_STREAM_ERROR_OTHER = "stream_error_other"
|
|
93
|
+
DISCONNECT_STREAM_ERROR_FORCE_LOGIN = "stream_error_force_login"
|
|
94
|
+
DISCONNECT_STREAM_ERROR_FORCE_LOGOUT = "stream_error_force_logout"
|
|
95
|
+
DISCONNECT_FAILURE_LOCKED = "failure_locked"
|
|
96
|
+
DISCONNECT_FAILURE_NOT_AUTHORIZED = "failure_not_authorized"
|
|
97
|
+
DISCONNECT_FAILURE_BANNED = "failure_banned"
|
|
98
|
+
DISCONNECT_FAILURE_CLIENT_TOO_OLD = "failure_client_too_old"
|
|
99
|
+
DISCONNECT_FAILURE_BAD_USER_AGENT = "failure_bad_user_agent"
|
|
100
|
+
DISCONNECT_FAILURE_SERVICE_UNAVAILABLE = "failure_service_unavailable"
|
|
101
|
+
|
|
102
|
+
WaDisconnectReason = str
|
|
103
|
+
WaConnectionCode = int
|
|
104
|
+
|
|
105
|
+
_FAILURE_REASON_TO_DISCONNECT: dict[int, str] = {
|
|
106
|
+
401: DISCONNECT_FAILURE_NOT_AUTHORIZED,
|
|
107
|
+
403: DISCONNECT_FAILURE_LOCKED,
|
|
108
|
+
406: DISCONNECT_FAILURE_BANNED,
|
|
109
|
+
405: DISCONNECT_FAILURE_CLIENT_TOO_OLD,
|
|
110
|
+
409: DISCONNECT_FAILURE_BAD_USER_AGENT,
|
|
111
|
+
503: DISCONNECT_FAILURE_SERVICE_UNAVAILABLE,
|
|
112
|
+
}
|
|
113
|
+
_LOGOUT_FAILURE_REASONS = frozenset({401, 403, 406})
|
|
114
|
+
_DISCONNECT_FAILURE_REASONS = frozenset({405, 409, 503})
|
|
115
|
+
|
|
116
|
+
_CORE_NOTIFICATION_TYPES = frozenset(
|
|
117
|
+
{
|
|
118
|
+
"server_sync",
|
|
119
|
+
"contacts",
|
|
120
|
+
"devices",
|
|
121
|
+
"disappearing_mode",
|
|
122
|
+
"mediaretry",
|
|
123
|
+
"encrypt",
|
|
124
|
+
"server",
|
|
125
|
+
"status",
|
|
126
|
+
"account_sync",
|
|
127
|
+
"privacy_token",
|
|
128
|
+
"newsletter",
|
|
129
|
+
"w:growth",
|
|
130
|
+
"registration",
|
|
131
|
+
"mex",
|
|
132
|
+
}
|
|
133
|
+
)
|
|
134
|
+
_OUT_OF_SCOPE_NOTIFICATION_TYPES = frozenset({"pay", "psa", "waffle", "hosted"})
|
|
135
|
+
_NOTIFICATION_TYPES_WITH_PARTICIPANT_ACK = frozenset({"mediaretry", "psa"})
|
|
136
|
+
_NOTIFICATION_TYPES_WITHOUT_TYPE_ACK = frozenset({"encrypt", "devices"})
|
|
137
|
+
_RECEIPT_RETRY_TYPES = frozenset({_RECEIPT_TYPE_RETRY, _RECEIPT_TYPE_ENC_REKEY_RETRY})
|
|
138
|
+
_INTERNAL_ONLY_RECEIPT_TYPES = frozenset(
|
|
139
|
+
{
|
|
140
|
+
_RECEIPT_TYPE_RETRY,
|
|
141
|
+
_RECEIPT_TYPE_ENC_REKEY_RETRY,
|
|
142
|
+
_RECEIPT_TYPE_PEER,
|
|
143
|
+
_RECEIPT_TYPE_SENDER,
|
|
144
|
+
_RECEIPT_TYPE_HISTORY_SYNC,
|
|
145
|
+
_RECEIPT_TYPE_SERVER_ERROR,
|
|
146
|
+
}
|
|
147
|
+
)
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
def _server_of(jid: str) -> str:
|
|
151
|
+
return jid.rsplit("@", 1)[-1] if "@" in jid else ""
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
def _is_lid_jid(jid: str) -> bool:
|
|
155
|
+
return _server_of(jid) == _defaults.LID_SERVER
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
def _to_user_jid(jid: str) -> str:
|
|
159
|
+
at_index = jid.find("@")
|
|
160
|
+
if at_index == -1:
|
|
161
|
+
return jid
|
|
162
|
+
user = jid[:at_index]
|
|
163
|
+
server = jid[at_index + 1 :]
|
|
164
|
+
colon_index = user.find(":")
|
|
165
|
+
if colon_index != -1:
|
|
166
|
+
user = user[:colon_index]
|
|
167
|
+
return f"{user}@{server}"
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
def _normalize_device_jid(jid: str) -> str:
|
|
171
|
+
at_index = jid.find("@")
|
|
172
|
+
if at_index == -1:
|
|
173
|
+
return jid
|
|
174
|
+
user = jid[:at_index]
|
|
175
|
+
server = jid[at_index + 1 :]
|
|
176
|
+
if ":" not in user:
|
|
177
|
+
return f"{user}@{server}"
|
|
178
|
+
head, device = user.split(":", 1)
|
|
179
|
+
if not device:
|
|
180
|
+
return f"{head}@{server}"
|
|
181
|
+
return f"{head}:{device}@{server}"
|
|
182
|
+
|
|
183
|
+
|
|
184
|
+
@dataclass(frozen=True, slots=True)
|
|
185
|
+
class WaIncomingBaseEvent:
|
|
186
|
+
"""The raw stanza plus its addressing envelope; every inbound event extends this."""
|
|
187
|
+
|
|
188
|
+
raw_node: BinaryNode
|
|
189
|
+
stanza_id: str | None = None
|
|
190
|
+
chat_jid: str | None = None
|
|
191
|
+
stanza_type: str | None = None
|
|
192
|
+
offline: bool = False
|
|
193
|
+
|
|
194
|
+
|
|
195
|
+
@dataclass(frozen=True, slots=True)
|
|
196
|
+
class WaIncomingReceiptEvent(WaIncomingBaseEvent):
|
|
197
|
+
"""A delivery/read/played receipt, with every id it acknowledges."""
|
|
198
|
+
|
|
199
|
+
status: str = "delivered"
|
|
200
|
+
from_self_device: bool = False
|
|
201
|
+
participant_jid: str | None = None
|
|
202
|
+
participant_username: str | None = None
|
|
203
|
+
recipient_jid: str | None = None
|
|
204
|
+
message_ids: list[str] = field(default_factory=list)
|
|
205
|
+
|
|
206
|
+
|
|
207
|
+
@dataclass(frozen=True, slots=True)
|
|
208
|
+
class WaIncomingFailureEvent(WaIncomingBaseEvent):
|
|
209
|
+
"""A stream ``<failure>`` stanza; specific reasons trigger logout/disconnect."""
|
|
210
|
+
|
|
211
|
+
failure_reason: int | None = None
|
|
212
|
+
failure_code: int | None = None
|
|
213
|
+
failure_message: str | None = None
|
|
214
|
+
failure_url: str | None = None
|
|
215
|
+
|
|
216
|
+
|
|
217
|
+
@dataclass(frozen=True, slots=True)
|
|
218
|
+
class WaIncomingNotificationEvent(WaIncomingBaseEvent):
|
|
219
|
+
"""Debug mirror fired for every notification, plus routing metadata."""
|
|
220
|
+
|
|
221
|
+
notification_type: str | None = None
|
|
222
|
+
classification: str | None = None
|
|
223
|
+
details: dict[str, Any] | None = None
|
|
224
|
+
|
|
225
|
+
|
|
226
|
+
@dataclass(frozen=True, slots=True)
|
|
227
|
+
class WaIncomingCallEvent(WaIncomingBaseEvent):
|
|
228
|
+
"""A ``<call>`` stanza with its typed payload discriminator and metadata."""
|
|
229
|
+
|
|
230
|
+
type: str = "unknown"
|
|
231
|
+
is_video: bool = False
|
|
232
|
+
payload_tag: str | None = None
|
|
233
|
+
call_id: str | None = None
|
|
234
|
+
call_creator_jid: str | None = None
|
|
235
|
+
sender_lid_jid: str | None = None
|
|
236
|
+
caller_pn_jid: str | None = None
|
|
237
|
+
group_jid: str | None = None
|
|
238
|
+
caller_username: str | None = None
|
|
239
|
+
caller_country_code: str | None = None
|
|
240
|
+
caller_push_name: str | None = None
|
|
241
|
+
peer_platform: str | None = None
|
|
242
|
+
peer_app_version: str | None = None
|
|
243
|
+
timestamp_seconds: int | None = None
|
|
244
|
+
end_timestamp_seconds: int | None = None
|
|
245
|
+
silence_reason: str | None = None
|
|
246
|
+
group_info: Any = None
|
|
247
|
+
|
|
248
|
+
|
|
249
|
+
@dataclass(frozen=True, slots=True)
|
|
250
|
+
class WaIncomingUnhandledStanzaEvent(WaIncomingBaseEvent):
|
|
251
|
+
"""Fired when the dispatcher has no typed handler for a stanza."""
|
|
252
|
+
|
|
253
|
+
reason: str = ""
|
|
254
|
+
|
|
255
|
+
|
|
256
|
+
@dataclass(frozen=True, slots=True)
|
|
257
|
+
class WaRegistrationCodeEvent(WaIncomingBaseEvent):
|
|
258
|
+
"""A ``wa_old_registration`` code to confirm on the primary device."""
|
|
259
|
+
|
|
260
|
+
code: str = ""
|
|
261
|
+
expiry_timestamp_ms: int = 0
|
|
262
|
+
from_device_id: str = ""
|
|
263
|
+
|
|
264
|
+
|
|
265
|
+
@dataclass(frozen=True, slots=True)
|
|
266
|
+
class WaAccountTakeoverNoticeEvent(WaIncomingBaseEvent):
|
|
267
|
+
"""A ``device_logout`` notice: another device is taking over the account."""
|
|
268
|
+
|
|
269
|
+
server_token: str = ""
|
|
270
|
+
attempt_timestamp_ms: int = 0
|
|
271
|
+
new_device_name: str | None = None
|
|
272
|
+
new_device_platform: str | None = None
|
|
273
|
+
new_device_app_version: str | None = None
|
|
274
|
+
|
|
275
|
+
|
|
276
|
+
@dataclass(frozen=True, slots=True)
|
|
277
|
+
class WaMexNotificationEvent(WaIncomingBaseEvent):
|
|
278
|
+
"""A MEX notification with its operation-specific normalized payload."""
|
|
279
|
+
|
|
280
|
+
kind: str = "unknown"
|
|
281
|
+
operation_name: str = ""
|
|
282
|
+
errors: list[Any] = field(default_factory=list)
|
|
283
|
+
data: Any = None
|
|
284
|
+
username: str | None = None
|
|
285
|
+
lid_jid: str | None = None
|
|
286
|
+
display_name: str | None = None
|
|
287
|
+
contact_hash: str | None = None
|
|
288
|
+
own_lid_jid: str | None = None
|
|
289
|
+
state: str | None = None
|
|
290
|
+
pin: str | None = None
|
|
291
|
+
jid: str | None = None
|
|
292
|
+
text: str | None = None
|
|
293
|
+
emoji: str | None = None
|
|
294
|
+
ephemeral_duration_sec: float | None = None
|
|
295
|
+
last_update_time: float | None = None
|
|
296
|
+
old_lid_jid: str | None = None
|
|
297
|
+
new_lid_jid: str | None = None
|
|
298
|
+
capping_status: str | None = None
|
|
299
|
+
ote_status: str | None = None
|
|
300
|
+
mv_status: str | None = None
|
|
301
|
+
total_quota: float | None = None
|
|
302
|
+
used_quota: float | None = None
|
|
303
|
+
cycle_start_timestamp: float | None = None
|
|
304
|
+
cycle_end_timestamp: float | None = None
|
|
305
|
+
server_sent_timestamp: float | None = None
|
|
306
|
+
|
|
307
|
+
|
|
308
|
+
def create_incoming_base_event(node: BinaryNode) -> WaIncomingBaseEvent:
|
|
309
|
+
"""The raw stanza plus its addressing envelope."""
|
|
310
|
+
return WaIncomingBaseEvent(
|
|
311
|
+
raw_node=node,
|
|
312
|
+
stanza_id=node.attrs.get("id"),
|
|
313
|
+
chat_jid=node.attrs.get("from"),
|
|
314
|
+
stanza_type=node.attrs.get("type"),
|
|
315
|
+
offline="offline" in node.attrs,
|
|
316
|
+
)
|
|
317
|
+
|
|
318
|
+
|
|
319
|
+
def create_unhandled_incoming_node_event(
|
|
320
|
+
node: BinaryNode, reason: str | None = None
|
|
321
|
+
) -> WaIncomingUnhandledStanzaEvent:
|
|
322
|
+
"""An unhandled-stanza event defaulting to ``unhandled.<tag>``."""
|
|
323
|
+
return WaIncomingUnhandledStanzaEvent(
|
|
324
|
+
raw_node=node,
|
|
325
|
+
stanza_id=node.attrs.get("id"),
|
|
326
|
+
chat_jid=node.attrs.get("from"),
|
|
327
|
+
stanza_type=node.attrs.get("type"),
|
|
328
|
+
offline="offline" in node.attrs,
|
|
329
|
+
reason=reason if reason is not None else f"unhandled.{node.tag}",
|
|
330
|
+
)
|
|
331
|
+
|
|
332
|
+
|
|
333
|
+
def create_info_bulletin_notification_event(
|
|
334
|
+
node: BinaryNode, type: str, details: dict[str, Any] | None = None
|
|
335
|
+
) -> WaIncomingNotificationEvent:
|
|
336
|
+
"""Debug mirror for info-bulletin (``ib``) notifications."""
|
|
337
|
+
base = create_incoming_base_event(node)
|
|
338
|
+
return WaIncomingNotificationEvent(
|
|
339
|
+
raw_node=base.raw_node,
|
|
340
|
+
stanza_id=base.stanza_id,
|
|
341
|
+
chat_jid=base.chat_jid,
|
|
342
|
+
stanza_type=base.stanza_type,
|
|
343
|
+
offline=base.offline,
|
|
344
|
+
notification_type=f"ib.{type}",
|
|
345
|
+
classification="info_bulletin",
|
|
346
|
+
details=details,
|
|
347
|
+
)
|
|
348
|
+
|
|
349
|
+
|
|
350
|
+
def _build_message_ack(node: BinaryNode, stanza_id: str, to: str) -> BinaryNode:
|
|
351
|
+
# Mirrors the ``message`` branch of ``buildAckNode`` in builders/global.ts.
|
|
352
|
+
attrs: dict[str, str] = {"id": stanza_id, "to": to, "class": _ACK_CLASS_MESSAGE}
|
|
353
|
+
stanza_type = node.attrs.get("type")
|
|
354
|
+
if stanza_type:
|
|
355
|
+
attrs["type"] = stanza_type
|
|
356
|
+
participant = node.attrs.get("participant")
|
|
357
|
+
if participant:
|
|
358
|
+
attrs["participant"] = participant
|
|
359
|
+
return BinaryNode(tag=_ACK_TAG, attrs=attrs)
|
|
360
|
+
|
|
361
|
+
|
|
362
|
+
def _build_receipt_ack(
|
|
363
|
+
node: BinaryNode, *, retry_type: bool = False, include_participant: bool = True
|
|
364
|
+
) -> BinaryNode:
|
|
365
|
+
# Mirrors the ``receipt`` branch of ``buildAckNode`` in builders/global.ts.
|
|
366
|
+
attrs: dict[str, str] = {"class": _RECEIPT_TAG}
|
|
367
|
+
type_attr = node.attrs.get("type")
|
|
368
|
+
if retry_type:
|
|
369
|
+
attrs["type"] = type_attr or _RECEIPT_TYPE_RETRY
|
|
370
|
+
elif type_attr:
|
|
371
|
+
attrs["type"] = type_attr
|
|
372
|
+
stanza_id = node.attrs.get("id")
|
|
373
|
+
if stanza_id:
|
|
374
|
+
attrs["id"] = stanza_id
|
|
375
|
+
from_jid = node.attrs.get("from")
|
|
376
|
+
if from_jid:
|
|
377
|
+
attrs["to"] = from_jid
|
|
378
|
+
participant = node.attrs.get("participant")
|
|
379
|
+
if retry_type:
|
|
380
|
+
if participant:
|
|
381
|
+
attrs["participant"] = participant
|
|
382
|
+
elif (
|
|
383
|
+
include_participant
|
|
384
|
+
and participant
|
|
385
|
+
and (not from_jid or participant != from_jid)
|
|
386
|
+
):
|
|
387
|
+
attrs["participant"] = participant
|
|
388
|
+
return BinaryNode(tag=_ACK_TAG, attrs=attrs)
|
|
389
|
+
|
|
390
|
+
|
|
391
|
+
def _build_custom_ack(
|
|
392
|
+
*,
|
|
393
|
+
ack_class: str,
|
|
394
|
+
to: str,
|
|
395
|
+
stanza_id: str | None = None,
|
|
396
|
+
type: str | None = None,
|
|
397
|
+
content: list[BinaryNode] | None = None,
|
|
398
|
+
) -> BinaryNode:
|
|
399
|
+
# Mirrors the ``custom`` branch of ``buildAckNode`` in builders/global.ts.
|
|
400
|
+
attrs: dict[str, str] = {"class": ack_class, "to": to}
|
|
401
|
+
if stanza_id:
|
|
402
|
+
attrs["id"] = stanza_id
|
|
403
|
+
if type:
|
|
404
|
+
attrs["type"] = type
|
|
405
|
+
return BinaryNode(tag=_ACK_TAG, attrs=attrs, content=content)
|
|
406
|
+
|
|
407
|
+
|
|
408
|
+
def _build_custom_receipt(
|
|
409
|
+
attrs: dict[str, str], content: list[BinaryNode] | None = None
|
|
410
|
+
) -> BinaryNode:
|
|
411
|
+
# Mirrors the ``custom`` branch of ``buildReceiptNode`` in builders/global.ts.
|
|
412
|
+
return BinaryNode(tag=_RECEIPT_TAG, attrs=dict(attrs), content=content)
|
|
413
|
+
|
|
414
|
+
|
|
415
|
+
def build_inbound_ack(node: BinaryNode) -> BinaryNode | None:
|
|
416
|
+
"""Build the inbound ack for ``message``/``receipt``/``notification`` tags.
|
|
417
|
+
|
|
418
|
+
Returns ``None`` for other tags or when required attrs are missing.
|
|
419
|
+
"""
|
|
420
|
+
if node.tag == _MESSAGE_TAG:
|
|
421
|
+
stanza_id = node.attrs.get("id")
|
|
422
|
+
from_jid = node.attrs.get("from")
|
|
423
|
+
if not stanza_id or not from_jid:
|
|
424
|
+
return None
|
|
425
|
+
return _build_message_ack(node, stanza_id, from_jid)
|
|
426
|
+
if node.tag == _RECEIPT_TAG:
|
|
427
|
+
if not node.attrs.get("id") or not node.attrs.get("from"):
|
|
428
|
+
return None
|
|
429
|
+
receipt_type = node.attrs.get("type")
|
|
430
|
+
if receipt_type and receipt_type in _RECEIPT_RETRY_TYPES:
|
|
431
|
+
return _build_receipt_ack(node, retry_type=True)
|
|
432
|
+
return _build_receipt_ack(
|
|
433
|
+
node,
|
|
434
|
+
include_participant=receipt_type != _RECEIPT_TYPE_SERVER_ERROR,
|
|
435
|
+
)
|
|
436
|
+
if node.tag == WA_NODE_TAGS["NOTIFICATION"]:
|
|
437
|
+
stanza_type = node.attrs.get("type") or ""
|
|
438
|
+
return build_ack_node(
|
|
439
|
+
kind="notification",
|
|
440
|
+
node=node,
|
|
441
|
+
include_participant=(
|
|
442
|
+
stanza_type == WA_NOTIFICATION_TYPES["GROUP"]
|
|
443
|
+
or stanza_type in _NOTIFICATION_TYPES_WITH_PARTICIPANT_ACK
|
|
444
|
+
),
|
|
445
|
+
include_type=stanza_type not in _NOTIFICATION_TYPES_WITHOUT_TYPE_ACK,
|
|
446
|
+
)
|
|
447
|
+
return None
|
|
448
|
+
|
|
449
|
+
|
|
450
|
+
async def send_safe_ack(logger: Logger, send_node: SendNode, node: BinaryNode) -> None:
|
|
451
|
+
"""Send an ack node, logging instead of throwing on transport errors."""
|
|
452
|
+
try:
|
|
453
|
+
await send_node(node)
|
|
454
|
+
except Exception as error:
|
|
455
|
+
logger.warn(
|
|
456
|
+
"failed to send inbound ack",
|
|
457
|
+
{
|
|
458
|
+
"tag": node.tag,
|
|
459
|
+
"class": node.attrs.get("class"),
|
|
460
|
+
"type": node.attrs.get("type"),
|
|
461
|
+
"id": node.attrs.get("id"),
|
|
462
|
+
"message": _error_message(error),
|
|
463
|
+
},
|
|
464
|
+
)
|
|
465
|
+
|
|
466
|
+
|
|
467
|
+
def _classify_notification_type(notification_type: str) -> str:
|
|
468
|
+
if notification_type in _CORE_NOTIFICATION_TYPES:
|
|
469
|
+
return "core"
|
|
470
|
+
if notification_type in _OUT_OF_SCOPE_NOTIFICATION_TYPES:
|
|
471
|
+
return "out_of_scope"
|
|
472
|
+
return "unknown"
|
|
473
|
+
|
|
474
|
+
|
|
475
|
+
def _map_receipt_status(type_: str | None) -> tuple[str, bool] | None:
|
|
476
|
+
if type_ is None or type_ == _RECEIPT_TYPE_DELIVERY:
|
|
477
|
+
return ("delivered", False)
|
|
478
|
+
if type_ == _RECEIPT_TYPE_READ:
|
|
479
|
+
return ("read", False)
|
|
480
|
+
if type_ == _RECEIPT_TYPE_READ_SELF:
|
|
481
|
+
return ("read", True)
|
|
482
|
+
if type_ == _RECEIPT_TYPE_PLAYED:
|
|
483
|
+
return ("played", False)
|
|
484
|
+
if type_ == _RECEIPT_TYPE_PLAYED_SELF:
|
|
485
|
+
return ("played", True)
|
|
486
|
+
if type_ == _RECEIPT_TYPE_INACTIVE:
|
|
487
|
+
return ("inactive", False)
|
|
488
|
+
return None
|
|
489
|
+
|
|
490
|
+
|
|
491
|
+
@dataclass(frozen=True, slots=True)
|
|
492
|
+
class IncomingReceiptHandlerOptions:
|
|
493
|
+
"""Options for :func:`create_incoming_receipt_handler`."""
|
|
494
|
+
|
|
495
|
+
logger: Logger
|
|
496
|
+
send_node: SendNode
|
|
497
|
+
emit_incoming_receipt: Callable[[WaIncomingReceiptEvent], None]
|
|
498
|
+
handle_incoming_retry_receipt: Callable[[BinaryNode], Awaitable[None]] | None = None
|
|
499
|
+
track_outbound_receipt: Callable[[BinaryNode], Awaitable[None]] | None = None
|
|
500
|
+
|
|
501
|
+
|
|
502
|
+
def create_incoming_receipt_handler(
|
|
503
|
+
options: IncomingReceiptHandlerOptions,
|
|
504
|
+
) -> Callable[[BinaryNode], Awaitable[bool]]:
|
|
505
|
+
"""Handle inbound ``<receipt>`` stanzas: emit, track, ack, retry-route."""
|
|
506
|
+
|
|
507
|
+
async def handle(node: BinaryNode) -> bool:
|
|
508
|
+
if not node.attrs.get("id") or not node.attrs.get("from"):
|
|
509
|
+
options.logger.warn(
|
|
510
|
+
"incoming receipt missing required attrs for ack",
|
|
511
|
+
{
|
|
512
|
+
"hasFrom": node.attrs.get("from") is not None,
|
|
513
|
+
"hasId": node.attrs.get("id") is not None,
|
|
514
|
+
"type": node.attrs.get("type"),
|
|
515
|
+
},
|
|
516
|
+
)
|
|
517
|
+
return True
|
|
518
|
+
mapped = _map_receipt_status(node.attrs.get("type"))
|
|
519
|
+
if mapped is not None:
|
|
520
|
+
status, from_self_device = mapped
|
|
521
|
+
participant_username = node.attrs.get("participant_username")
|
|
522
|
+
options.emit_incoming_receipt(
|
|
523
|
+
WaIncomingReceiptEvent(
|
|
524
|
+
raw_node=node,
|
|
525
|
+
stanza_id=node.attrs.get("id"),
|
|
526
|
+
chat_jid=node.attrs.get("from"),
|
|
527
|
+
stanza_type=node.attrs.get("type"),
|
|
528
|
+
offline="offline" in node.attrs,
|
|
529
|
+
status=status,
|
|
530
|
+
from_self_device=from_self_device,
|
|
531
|
+
participant_jid=node.attrs.get("participant"),
|
|
532
|
+
participant_username=normalize_username(participant_username)
|
|
533
|
+
if participant_username is not None
|
|
534
|
+
else None,
|
|
535
|
+
recipient_jid=node.attrs.get("recipient"),
|
|
536
|
+
message_ids=extract_receipt_ids(node),
|
|
537
|
+
)
|
|
538
|
+
)
|
|
539
|
+
elif (
|
|
540
|
+
node.attrs.get("type")
|
|
541
|
+
and node.attrs.get("type") not in _INTERNAL_ONLY_RECEIPT_TYPES
|
|
542
|
+
):
|
|
543
|
+
options.logger.warn(
|
|
544
|
+
"unrecognized receipt type suppressed",
|
|
545
|
+
{
|
|
546
|
+
"id": node.attrs.get("id"),
|
|
547
|
+
"from": node.attrs.get("from"),
|
|
548
|
+
"type": node.attrs.get("type"),
|
|
549
|
+
},
|
|
550
|
+
)
|
|
551
|
+
try:
|
|
552
|
+
if options.track_outbound_receipt is not None:
|
|
553
|
+
await options.track_outbound_receipt(node)
|
|
554
|
+
except Exception as error:
|
|
555
|
+
options.logger.warn(
|
|
556
|
+
"failed to track outbound message receipt state",
|
|
557
|
+
{
|
|
558
|
+
"id": node.attrs.get("id"),
|
|
559
|
+
"from": node.attrs.get("from"),
|
|
560
|
+
"type": node.attrs.get("type"),
|
|
561
|
+
"message": _error_message(error),
|
|
562
|
+
},
|
|
563
|
+
)
|
|
564
|
+
receipt_type = node.attrs.get("type")
|
|
565
|
+
if receipt_type in _RECEIPT_RETRY_TYPES:
|
|
566
|
+
if options.handle_incoming_retry_receipt is not None:
|
|
567
|
+
await options.handle_incoming_retry_receipt(node)
|
|
568
|
+
else:
|
|
569
|
+
ack = build_inbound_ack(node)
|
|
570
|
+
if ack is not None:
|
|
571
|
+
await send_safe_ack(options.logger, options.send_node, ack)
|
|
572
|
+
return True
|
|
573
|
+
ack = build_inbound_ack(node)
|
|
574
|
+
if ack is not None:
|
|
575
|
+
await send_safe_ack(options.logger, options.send_node, ack)
|
|
576
|
+
return True
|
|
577
|
+
|
|
578
|
+
return handle
|
|
579
|
+
|
|
580
|
+
|
|
581
|
+
@dataclass(frozen=True, slots=True)
|
|
582
|
+
class IncomingFailureHandlerOptions:
|
|
583
|
+
"""Options for :func:`create_incoming_failure_handler`."""
|
|
584
|
+
|
|
585
|
+
logger: Logger
|
|
586
|
+
emit_incoming_failure: Callable[[WaIncomingFailureEvent], None]
|
|
587
|
+
stop_comms: Callable[[], None]
|
|
588
|
+
disconnect: Callable[
|
|
589
|
+
[WaDisconnectReason, bool, WaConnectionCode | None], Awaitable[None]
|
|
590
|
+
]
|
|
591
|
+
clear_stored_credentials: Callable[[], Awaitable[None]]
|
|
592
|
+
|
|
593
|
+
|
|
594
|
+
def create_incoming_failure_handler(
|
|
595
|
+
options: IncomingFailureHandlerOptions,
|
|
596
|
+
) -> Callable[[BinaryNode], Awaitable[bool]]:
|
|
597
|
+
"""Handle stream ``<failure>`` stanzas: emit, then logout/disconnect per reason."""
|
|
598
|
+
|
|
599
|
+
async def handle(node: BinaryNode) -> bool:
|
|
600
|
+
reason = parse_optional_int(node.attrs.get("reason"))
|
|
601
|
+
code = parse_optional_int(node.attrs.get("code"))
|
|
602
|
+
options.emit_incoming_failure(
|
|
603
|
+
WaIncomingFailureEvent(
|
|
604
|
+
raw_node=node,
|
|
605
|
+
stanza_id=node.attrs.get("id"),
|
|
606
|
+
chat_jid=node.attrs.get("from"),
|
|
607
|
+
stanza_type=node.attrs.get("type"),
|
|
608
|
+
offline="offline" in node.attrs,
|
|
609
|
+
failure_reason=reason,
|
|
610
|
+
failure_code=code,
|
|
611
|
+
failure_message=node.attrs.get("message"),
|
|
612
|
+
failure_url=node.attrs.get("url"),
|
|
613
|
+
)
|
|
614
|
+
)
|
|
615
|
+
should_clear = reason is not None and reason in _LOGOUT_FAILURE_REASONS
|
|
616
|
+
if should_clear or (
|
|
617
|
+
reason is not None and reason in _DISCONNECT_FAILURE_REASONS
|
|
618
|
+
):
|
|
619
|
+
assert reason is not None
|
|
620
|
+
try:
|
|
621
|
+
options.stop_comms()
|
|
622
|
+
disconnect_reason = _FAILURE_REASON_TO_DISCONNECT.get(
|
|
623
|
+
reason, DISCONNECT_STREAM_ERROR_OTHER
|
|
624
|
+
)
|
|
625
|
+
await options.disconnect(disconnect_reason, should_clear, reason)
|
|
626
|
+
if should_clear:
|
|
627
|
+
await options.clear_stored_credentials()
|
|
628
|
+
except Exception as error:
|
|
629
|
+
options.logger.warn(
|
|
630
|
+
"failed applying failure stanza action",
|
|
631
|
+
{
|
|
632
|
+
"reason": reason,
|
|
633
|
+
"clearStoredCredentials": should_clear,
|
|
634
|
+
"message": _error_message(error),
|
|
635
|
+
},
|
|
636
|
+
)
|
|
637
|
+
return True
|
|
638
|
+
|
|
639
|
+
return handle
|
|
640
|
+
|
|
641
|
+
|
|
642
|
+
@dataclass(frozen=True, slots=True)
|
|
643
|
+
class IncomingNotificationHandlerOptions:
|
|
644
|
+
"""Options for :func:`create_incoming_notification_handler`."""
|
|
645
|
+
|
|
646
|
+
logger: Logger
|
|
647
|
+
send_node: SendNode
|
|
648
|
+
emit_incoming_notification: Callable[[WaIncomingNotificationEvent], None]
|
|
649
|
+
emit_mex_notification: Callable[[WaMexNotificationEvent], None]
|
|
650
|
+
emit_unhandled_stanza: Callable[[WaIncomingUnhandledStanzaEvent], None]
|
|
651
|
+
sync_app_state: Callable[[], Awaitable[None]] | None = None
|
|
652
|
+
handle_media_retry_notification: Callable[[BinaryNode], None] | None = None
|
|
653
|
+
|
|
654
|
+
|
|
655
|
+
def create_incoming_notification_handler(
|
|
656
|
+
options: IncomingNotificationHandlerOptions,
|
|
657
|
+
) -> Callable[[BinaryNode], Awaitable[bool]]:
|
|
658
|
+
"""Handle inbound notifications: debug-mirror, MEX/mediaretry routing, ack."""
|
|
659
|
+
|
|
660
|
+
async def handle(node: BinaryNode) -> bool:
|
|
661
|
+
notification_type = node.attrs.get("type") or ""
|
|
662
|
+
classification = _classify_notification_type(notification_type)
|
|
663
|
+
first_child_tag = (
|
|
664
|
+
first_child.tag if (first_child := get_first_node_child(node)) else None
|
|
665
|
+
)
|
|
666
|
+
base = create_incoming_base_event(node)
|
|
667
|
+
server_sync_collections = (
|
|
668
|
+
get_node_children_non_empty_attr_values_by_tag(
|
|
669
|
+
node, WA_NODE_TAGS["COLLECTION"], "name"
|
|
670
|
+
)
|
|
671
|
+
if notification_type == "server_sync"
|
|
672
|
+
else []
|
|
673
|
+
)
|
|
674
|
+
details: dict[str, Any] | None = None
|
|
675
|
+
if first_child_tag or server_sync_collections:
|
|
676
|
+
details = {}
|
|
677
|
+
if first_child_tag:
|
|
678
|
+
details["firstChildTag"] = first_child_tag
|
|
679
|
+
if server_sync_collections:
|
|
680
|
+
details["collections"] = server_sync_collections
|
|
681
|
+
options.emit_incoming_notification(
|
|
682
|
+
WaIncomingNotificationEvent(
|
|
683
|
+
raw_node=base.raw_node,
|
|
684
|
+
stanza_id=base.stanza_id,
|
|
685
|
+
chat_jid=base.chat_jid,
|
|
686
|
+
stanza_type=base.stanza_type,
|
|
687
|
+
offline=base.offline,
|
|
688
|
+
notification_type=notification_type,
|
|
689
|
+
classification=classification,
|
|
690
|
+
details=details,
|
|
691
|
+
)
|
|
692
|
+
)
|
|
693
|
+
if notification_type == "mex":
|
|
694
|
+
parsed = parse_mex_notification(node)
|
|
695
|
+
if parsed is not None:
|
|
696
|
+
options.emit_mex_notification(_mex_event(base, parsed))
|
|
697
|
+
else:
|
|
698
|
+
options.emit_unhandled_stanza(
|
|
699
|
+
_unhandled(base, "notification.mex.parse_failed")
|
|
700
|
+
)
|
|
701
|
+
if notification_type == WA_NOTIFICATION_TYPES.get("MEDIA_RETRY"):
|
|
702
|
+
if options.handle_media_retry_notification is not None:
|
|
703
|
+
options.handle_media_retry_notification(node)
|
|
704
|
+
if classification == "out_of_scope":
|
|
705
|
+
options.emit_unhandled_stanza(
|
|
706
|
+
_unhandled(base, f"notification.{notification_type}.out_of_scope")
|
|
707
|
+
)
|
|
708
|
+
elif classification == "unknown":
|
|
709
|
+
options.emit_unhandled_stanza(
|
|
710
|
+
_unhandled(
|
|
711
|
+
base, f"notification.{notification_type or 'unknown'}.not_supported"
|
|
712
|
+
)
|
|
713
|
+
)
|
|
714
|
+
ack = build_inbound_ack(node)
|
|
715
|
+
if ack is not None:
|
|
716
|
+
await send_safe_ack(options.logger, options.send_node, ack)
|
|
717
|
+
if notification_type == "server_sync" and server_sync_collections:
|
|
718
|
+
collections_csv = ",".join(server_sync_collections)
|
|
719
|
+
if options.sync_app_state is None:
|
|
720
|
+
options.logger.warn(
|
|
721
|
+
"received server_sync notification without app-state sync runtime",
|
|
722
|
+
{"collections": collections_csv},
|
|
723
|
+
)
|
|
724
|
+
return True
|
|
725
|
+
|
|
726
|
+
async def _sync() -> None:
|
|
727
|
+
assert options.sync_app_state is not None
|
|
728
|
+
try:
|
|
729
|
+
await options.sync_app_state()
|
|
730
|
+
except Exception as error:
|
|
731
|
+
options.logger.warn(
|
|
732
|
+
"failed to sync app-state after server_sync notification",
|
|
733
|
+
{
|
|
734
|
+
"collections": collections_csv,
|
|
735
|
+
"message": _error_message(error),
|
|
736
|
+
},
|
|
737
|
+
)
|
|
738
|
+
|
|
739
|
+
asyncio.ensure_future(_sync())
|
|
740
|
+
return True
|
|
741
|
+
|
|
742
|
+
return handle
|
|
743
|
+
|
|
744
|
+
|
|
745
|
+
def _unhandled(
|
|
746
|
+
base: WaIncomingBaseEvent, reason: str
|
|
747
|
+
) -> WaIncomingUnhandledStanzaEvent:
|
|
748
|
+
return WaIncomingUnhandledStanzaEvent(
|
|
749
|
+
raw_node=base.raw_node,
|
|
750
|
+
stanza_id=base.stanza_id,
|
|
751
|
+
chat_jid=base.chat_jid,
|
|
752
|
+
stanza_type=base.stanza_type,
|
|
753
|
+
offline=base.offline,
|
|
754
|
+
reason=reason,
|
|
755
|
+
)
|
|
756
|
+
|
|
757
|
+
|
|
758
|
+
def _mex_event(
|
|
759
|
+
base: WaIncomingBaseEvent, parsed: WaMexNotificationParsed
|
|
760
|
+
) -> WaMexNotificationEvent:
|
|
761
|
+
return WaMexNotificationEvent(
|
|
762
|
+
raw_node=base.raw_node,
|
|
763
|
+
stanza_id=base.stanza_id,
|
|
764
|
+
chat_jid=base.chat_jid,
|
|
765
|
+
stanza_type=base.stanza_type,
|
|
766
|
+
offline=base.offline,
|
|
767
|
+
kind=parsed.kind,
|
|
768
|
+
operation_name=parsed.operation_name,
|
|
769
|
+
errors=list(parsed.errors),
|
|
770
|
+
data=parsed.data,
|
|
771
|
+
username=parsed.username,
|
|
772
|
+
lid_jid=parsed.lid_jid,
|
|
773
|
+
display_name=parsed.display_name,
|
|
774
|
+
contact_hash=parsed.contact_hash,
|
|
775
|
+
own_lid_jid=parsed.own_lid_jid,
|
|
776
|
+
state=parsed.state,
|
|
777
|
+
pin=parsed.pin,
|
|
778
|
+
jid=parsed.jid,
|
|
779
|
+
text=parsed.text,
|
|
780
|
+
emoji=parsed.emoji,
|
|
781
|
+
ephemeral_duration_sec=parsed.ephemeral_duration_sec,
|
|
782
|
+
last_update_time=parsed.last_update_time,
|
|
783
|
+
old_lid_jid=parsed.old_lid_jid,
|
|
784
|
+
new_lid_jid=parsed.new_lid_jid,
|
|
785
|
+
capping_status=parsed.capping_status,
|
|
786
|
+
ote_status=parsed.ote_status,
|
|
787
|
+
mv_status=parsed.mv_status,
|
|
788
|
+
total_quota=parsed.total_quota,
|
|
789
|
+
used_quota=parsed.used_quota,
|
|
790
|
+
cycle_start_timestamp=parsed.cycle_start_timestamp,
|
|
791
|
+
cycle_end_timestamp=parsed.cycle_end_timestamp,
|
|
792
|
+
server_sent_timestamp=parsed.server_sent_timestamp,
|
|
793
|
+
)
|
|
794
|
+
|
|
795
|
+
|
|
796
|
+
@dataclass(frozen=True, slots=True)
|
|
797
|
+
class IncomingRegistrationNotificationHandlerOptions:
|
|
798
|
+
"""Options for :func:`create_incoming_registration_notification_handler`."""
|
|
799
|
+
|
|
800
|
+
logger: Logger
|
|
801
|
+
send_node: SendNode
|
|
802
|
+
emit_registration_code: Callable[[WaRegistrationCodeEvent], None]
|
|
803
|
+
emit_account_takeover_notice: Callable[[WaAccountTakeoverNoticeEvent], None]
|
|
804
|
+
|
|
805
|
+
|
|
806
|
+
def create_incoming_registration_notification_handler(
|
|
807
|
+
options: IncomingRegistrationNotificationHandlerOptions,
|
|
808
|
+
) -> Callable[[BinaryNode], Awaitable[bool]]:
|
|
809
|
+
"""Handle ``registration`` notifications: codes and takeover notices."""
|
|
810
|
+
|
|
811
|
+
async def handle(node: BinaryNode) -> bool:
|
|
812
|
+
if node.attrs.get("type") != WA_NOTIFICATION_TYPES.get("REGISTRATION"):
|
|
813
|
+
return False
|
|
814
|
+
parsed = parse_registration_notification(node)
|
|
815
|
+
if parsed is None:
|
|
816
|
+
return False
|
|
817
|
+
base = create_incoming_base_event(node)
|
|
818
|
+
if isinstance(parsed, ParsedRegistrationCode):
|
|
819
|
+
options.emit_registration_code(
|
|
820
|
+
WaRegistrationCodeEvent(
|
|
821
|
+
raw_node=base.raw_node,
|
|
822
|
+
stanza_id=base.stanza_id,
|
|
823
|
+
chat_jid=base.chat_jid,
|
|
824
|
+
stanza_type=base.stanza_type,
|
|
825
|
+
offline=base.offline,
|
|
826
|
+
code=parsed.code,
|
|
827
|
+
expiry_timestamp_ms=parsed.expiry_timestamp_ms,
|
|
828
|
+
from_device_id=parsed.from_device_id,
|
|
829
|
+
)
|
|
830
|
+
)
|
|
831
|
+
elif isinstance(parsed, ParsedAccountTakeoverNotice):
|
|
832
|
+
options.emit_account_takeover_notice(
|
|
833
|
+
WaAccountTakeoverNoticeEvent(
|
|
834
|
+
raw_node=base.raw_node,
|
|
835
|
+
stanza_id=base.stanza_id,
|
|
836
|
+
chat_jid=base.chat_jid,
|
|
837
|
+
stanza_type=base.stanza_type,
|
|
838
|
+
offline=base.offline,
|
|
839
|
+
server_token=parsed.server_token,
|
|
840
|
+
attempt_timestamp_ms=parsed.attempt_timestamp_ms,
|
|
841
|
+
new_device_name=parsed.new_device_name,
|
|
842
|
+
new_device_platform=parsed.new_device_platform,
|
|
843
|
+
new_device_app_version=parsed.new_device_app_version,
|
|
844
|
+
)
|
|
845
|
+
)
|
|
846
|
+
ack = build_inbound_ack(node)
|
|
847
|
+
if ack is not None:
|
|
848
|
+
await send_safe_ack(options.logger, options.send_node, ack)
|
|
849
|
+
return True
|
|
850
|
+
|
|
851
|
+
return handle
|
|
852
|
+
|
|
853
|
+
|
|
854
|
+
E = TypeVar("E")
|
|
855
|
+
|
|
856
|
+
|
|
857
|
+
class _ParsedNotifications(Protocol[E]):
|
|
858
|
+
@property
|
|
859
|
+
def events(self) -> list[E]: ...
|
|
860
|
+
@property
|
|
861
|
+
def unhandled(self) -> list[WaIncomingUnhandledStanzaEvent]: ...
|
|
862
|
+
|
|
863
|
+
|
|
864
|
+
def _create_typed_notification_handler(
|
|
865
|
+
*,
|
|
866
|
+
logger: Logger,
|
|
867
|
+
send_node: SendNode,
|
|
868
|
+
type: str,
|
|
869
|
+
parse: Callable[[BinaryNode], _ParsedNotifications[E]],
|
|
870
|
+
emit: Callable[[E], None],
|
|
871
|
+
emit_unhandled_stanza: Callable[[WaIncomingUnhandledStanzaEvent], None],
|
|
872
|
+
) -> Callable[[BinaryNode], Awaitable[bool]]:
|
|
873
|
+
async def handle(node: BinaryNode) -> bool:
|
|
874
|
+
if node.attrs.get("type") != type:
|
|
875
|
+
return False
|
|
876
|
+
base = create_incoming_base_event(node)
|
|
877
|
+
parsed = parse(node)
|
|
878
|
+
for event in parsed.events:
|
|
879
|
+
emit(event)
|
|
880
|
+
for unhandled_event in parsed.unhandled:
|
|
881
|
+
emit_unhandled_stanza(unhandled_event)
|
|
882
|
+
if not parsed.events and not parsed.unhandled:
|
|
883
|
+
emit_unhandled_stanza(_unhandled(base, f"notification.{type}.empty"))
|
|
884
|
+
ack = build_inbound_ack(node)
|
|
885
|
+
if ack is not None:
|
|
886
|
+
await send_safe_ack(logger, send_node, ack)
|
|
887
|
+
return True
|
|
888
|
+
|
|
889
|
+
return handle
|
|
890
|
+
|
|
891
|
+
|
|
892
|
+
@dataclass(frozen=True, slots=True)
|
|
893
|
+
class IncomingBusinessNotificationHandlerOptions:
|
|
894
|
+
"""Options for :func:`create_incoming_business_notification_handler`."""
|
|
895
|
+
|
|
896
|
+
logger: Logger
|
|
897
|
+
send_node: SendNode
|
|
898
|
+
emit_business_event: Callable[[WaBusinessEvent], None]
|
|
899
|
+
emit_unhandled_stanza: Callable[[WaIncomingUnhandledStanzaEvent], None]
|
|
900
|
+
|
|
901
|
+
|
|
902
|
+
def create_incoming_business_notification_handler(
|
|
903
|
+
options: IncomingBusinessNotificationHandlerOptions,
|
|
904
|
+
) -> Callable[[BinaryNode], Awaitable[bool]]:
|
|
905
|
+
"""Handle ``business`` notifications."""
|
|
906
|
+
return _create_typed_notification_handler(
|
|
907
|
+
logger=options.logger,
|
|
908
|
+
send_node=options.send_node,
|
|
909
|
+
type=WA_NOTIFICATION_TYPES["BUSINESS"],
|
|
910
|
+
parse=parse_business_notification_events,
|
|
911
|
+
emit=options.emit_business_event,
|
|
912
|
+
emit_unhandled_stanza=options.emit_unhandled_stanza,
|
|
913
|
+
)
|
|
914
|
+
|
|
915
|
+
|
|
916
|
+
@dataclass(frozen=True, slots=True)
|
|
917
|
+
class IncomingGroupNotificationHandlerOptions:
|
|
918
|
+
"""Options for :func:`create_incoming_group_notification_handler`."""
|
|
919
|
+
|
|
920
|
+
logger: Logger
|
|
921
|
+
send_node: SendNode
|
|
922
|
+
emit_group_event: Callable[[WaGroupEvent], None]
|
|
923
|
+
emit_unhandled_stanza: Callable[[WaIncomingUnhandledStanzaEvent], None]
|
|
924
|
+
|
|
925
|
+
|
|
926
|
+
def create_incoming_group_notification_handler(
|
|
927
|
+
options: IncomingGroupNotificationHandlerOptions,
|
|
928
|
+
) -> Callable[[BinaryNode], Awaitable[bool]]:
|
|
929
|
+
"""Handle ``w:gp2`` group notifications."""
|
|
930
|
+
return _create_typed_notification_handler(
|
|
931
|
+
logger=options.logger,
|
|
932
|
+
send_node=options.send_node,
|
|
933
|
+
type=WA_NOTIFICATION_TYPES["GROUP"],
|
|
934
|
+
parse=parse_group_notification_events,
|
|
935
|
+
emit=options.emit_group_event,
|
|
936
|
+
emit_unhandled_stanza=options.emit_unhandled_stanza,
|
|
937
|
+
)
|
|
938
|
+
|
|
939
|
+
|
|
940
|
+
@dataclass(frozen=True, slots=True)
|
|
941
|
+
class IncomingPictureNotificationHandlerOptions:
|
|
942
|
+
"""Options for :func:`create_incoming_picture_notification_handler`."""
|
|
943
|
+
|
|
944
|
+
logger: Logger
|
|
945
|
+
send_node: SendNode
|
|
946
|
+
emit_picture_event: Callable[[WaPictureEvent], None]
|
|
947
|
+
emit_unhandled_stanza: Callable[[WaIncomingUnhandledStanzaEvent], None]
|
|
948
|
+
|
|
949
|
+
|
|
950
|
+
def create_incoming_picture_notification_handler(
|
|
951
|
+
options: IncomingPictureNotificationHandlerOptions,
|
|
952
|
+
) -> Callable[[BinaryNode], Awaitable[bool]]:
|
|
953
|
+
"""Handle ``picture`` notifications."""
|
|
954
|
+
return _create_typed_notification_handler(
|
|
955
|
+
logger=options.logger,
|
|
956
|
+
send_node=options.send_node,
|
|
957
|
+
type=WA_NOTIFICATION_TYPES["PICTURE"],
|
|
958
|
+
parse=parse_picture_notification_events,
|
|
959
|
+
emit=options.emit_picture_event,
|
|
960
|
+
emit_unhandled_stanza=options.emit_unhandled_stanza,
|
|
961
|
+
)
|
|
962
|
+
|
|
963
|
+
|
|
964
|
+
@dataclass(frozen=True, slots=True)
|
|
965
|
+
class IncomingCallHandlerOptions:
|
|
966
|
+
"""Options for :func:`create_incoming_call_handler`."""
|
|
967
|
+
|
|
968
|
+
logger: Logger
|
|
969
|
+
send_node: SendNode
|
|
970
|
+
emit_incoming_call: Callable[[WaIncomingCallEvent], None]
|
|
971
|
+
get_current_credentials: Callable[[], WaAuthCredentials | None]
|
|
972
|
+
|
|
973
|
+
|
|
974
|
+
def create_incoming_call_handler(
|
|
975
|
+
options: IncomingCallHandlerOptions,
|
|
976
|
+
) -> Callable[[BinaryNode], Awaitable[bool]]:
|
|
977
|
+
"""Handle inbound ``<call>`` stanzas: emit, then ack or receipt-respond."""
|
|
978
|
+
|
|
979
|
+
async def handle(node: BinaryNode) -> bool:
|
|
980
|
+
parsed = parse_call_node(node)
|
|
981
|
+
base = create_incoming_base_event(node)
|
|
982
|
+
options.emit_incoming_call(
|
|
983
|
+
WaIncomingCallEvent(
|
|
984
|
+
raw_node=base.raw_node,
|
|
985
|
+
stanza_id=base.stanza_id,
|
|
986
|
+
chat_jid=base.chat_jid,
|
|
987
|
+
stanza_type=base.stanza_type,
|
|
988
|
+
offline=base.offline,
|
|
989
|
+
type=parsed.type,
|
|
990
|
+
is_video=parsed.is_video,
|
|
991
|
+
payload_tag=parsed.payload_tag,
|
|
992
|
+
call_id=parsed.call_id,
|
|
993
|
+
call_creator_jid=parsed.call_creator_jid,
|
|
994
|
+
sender_lid_jid=parsed.sender_lid_jid,
|
|
995
|
+
caller_pn_jid=parsed.caller_pn_jid,
|
|
996
|
+
group_jid=parsed.group_jid,
|
|
997
|
+
caller_username=parsed.caller_username,
|
|
998
|
+
caller_country_code=parsed.caller_country_code,
|
|
999
|
+
caller_push_name=parsed.caller_push_name,
|
|
1000
|
+
peer_platform=parsed.peer_platform,
|
|
1001
|
+
peer_app_version=parsed.peer_app_version,
|
|
1002
|
+
timestamp_seconds=parsed.timestamp_seconds,
|
|
1003
|
+
end_timestamp_seconds=parsed.end_timestamp_seconds,
|
|
1004
|
+
silence_reason=parsed.silence_reason,
|
|
1005
|
+
group_info=parsed.group_info,
|
|
1006
|
+
)
|
|
1007
|
+
)
|
|
1008
|
+
peer_jid = node.attrs.get("from")
|
|
1009
|
+
stanza_id = node.attrs.get("id")
|
|
1010
|
+
if not peer_jid or not stanza_id:
|
|
1011
|
+
options.logger.warn(
|
|
1012
|
+
"incoming call missing required attrs for ack",
|
|
1013
|
+
{
|
|
1014
|
+
"hasFrom": peer_jid is not None,
|
|
1015
|
+
"hasId": stanza_id is not None,
|
|
1016
|
+
"payloadTag": parsed.payload_tag,
|
|
1017
|
+
},
|
|
1018
|
+
)
|
|
1019
|
+
return True
|
|
1020
|
+
payload_tag = parsed.payload_tag
|
|
1021
|
+
is_receipt_type = (
|
|
1022
|
+
parsed.type != "unknown" and parsed.type in CALL_RECEIPT_PAYLOAD_TAGS
|
|
1023
|
+
)
|
|
1024
|
+
if (
|
|
1025
|
+
is_receipt_type
|
|
1026
|
+
and payload_tag
|
|
1027
|
+
and parsed.call_id
|
|
1028
|
+
and parsed.call_creator_jid
|
|
1029
|
+
):
|
|
1030
|
+
credentials = options.get_current_credentials()
|
|
1031
|
+
from_jid: str | None = None
|
|
1032
|
+
try:
|
|
1033
|
+
me_lid = credentials.me_lid if credentials is not None else None
|
|
1034
|
+
me_jid = credentials.me_jid if credentials is not None else None
|
|
1035
|
+
if _is_lid_jid(peer_jid):
|
|
1036
|
+
from_jid = _normalize_device_jid(me_lid) if me_lid else None
|
|
1037
|
+
else:
|
|
1038
|
+
from_jid = _to_user_jid(me_jid) if me_jid else None
|
|
1039
|
+
except Exception as error:
|
|
1040
|
+
options.logger.warn(
|
|
1041
|
+
"failed to derive call receipt from jid",
|
|
1042
|
+
{
|
|
1043
|
+
"peerJid": peer_jid,
|
|
1044
|
+
"meLid": credentials.me_lid
|
|
1045
|
+
if credentials is not None
|
|
1046
|
+
else None,
|
|
1047
|
+
"meJid": credentials.me_jid
|
|
1048
|
+
if credentials is not None
|
|
1049
|
+
else None,
|
|
1050
|
+
"message": _error_message(error),
|
|
1051
|
+
},
|
|
1052
|
+
)
|
|
1053
|
+
from_jid = None
|
|
1054
|
+
receipt_attrs: dict[str, str] = {"id": stanza_id, "to": peer_jid}
|
|
1055
|
+
if from_jid:
|
|
1056
|
+
receipt_attrs["from"] = from_jid
|
|
1057
|
+
response = _build_custom_receipt(
|
|
1058
|
+
receipt_attrs,
|
|
1059
|
+
content=[
|
|
1060
|
+
BinaryNode(
|
|
1061
|
+
tag=payload_tag,
|
|
1062
|
+
attrs={
|
|
1063
|
+
"call-id": parsed.call_id,
|
|
1064
|
+
"call-creator": parsed.call_creator_jid,
|
|
1065
|
+
},
|
|
1066
|
+
)
|
|
1067
|
+
],
|
|
1068
|
+
)
|
|
1069
|
+
else:
|
|
1070
|
+
response = _build_custom_ack(
|
|
1071
|
+
ack_class=_ACK_CLASS_CALL,
|
|
1072
|
+
to=peer_jid,
|
|
1073
|
+
stanza_id=stanza_id,
|
|
1074
|
+
type=payload_tag,
|
|
1075
|
+
)
|
|
1076
|
+
await send_safe_ack(options.logger, options.send_node, response)
|
|
1077
|
+
return True
|
|
1078
|
+
|
|
1079
|
+
return handle
|