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,525 @@
|
|
|
1
|
+
"""Connection lifecycle manager (mirrors ``WaConnectionManager``).
|
|
2
|
+
|
|
3
|
+
Owns the websocket/noise comms lifetime: connect, post-pairing restart, and
|
|
4
|
+
disconnect. Transport objects are injected as structural protocols so the
|
|
5
|
+
manager stays decoupled from the concrete ``WaComms`` port.
|
|
6
|
+
|
|
7
|
+
DEVIATION: the TS chains lifecycle work through a promise queue
|
|
8
|
+
(``lifecycleQueue.then(operation, operation)``); here an ``asyncio.Lock``
|
|
9
|
+
serializes the same operations with the same observable ordering. The
|
|
10
|
+
post-pairing restart window reads ``WA_DEFAULTS.PAIRING_RESTART_WAIT_MS``
|
|
11
|
+
(15s); the constructor accepts an override purely as a test hook.
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
from __future__ import annotations
|
|
15
|
+
|
|
16
|
+
import asyncio
|
|
17
|
+
import time
|
|
18
|
+
from collections.abc import Awaitable, Callable, Mapping
|
|
19
|
+
from typing import Protocol
|
|
20
|
+
|
|
21
|
+
from zapy._infra.log import Logger
|
|
22
|
+
from zapy._transport.comms import WaCommsConfig
|
|
23
|
+
from zapy._util.primitives import to_error
|
|
24
|
+
|
|
25
|
+
# Verbatim ``src/protocol/defaults.ts`` (lands with the protocol layer).
|
|
26
|
+
PAIRING_RESTART_WAIT_MS: float = 15_000
|
|
27
|
+
|
|
28
|
+
FrameHandler = Callable[[bytes], Awaitable[None]]
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def _consume_task_outcome(task: asyncio.Task[object]) -> None:
|
|
32
|
+
"""Swallow a background task outcome (its own body already logged)."""
|
|
33
|
+
if not task.cancelled():
|
|
34
|
+
task.exception()
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
class CommsState(Protocol):
|
|
38
|
+
"""Connectivity snapshot reported by the comms object."""
|
|
39
|
+
|
|
40
|
+
connected: bool
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
class WaCommsLike(Protocol):
|
|
44
|
+
"""Structural surface of ``WaComms`` used by the manager."""
|
|
45
|
+
|
|
46
|
+
def start_comms(self, handler: FrameHandler) -> None: ...
|
|
47
|
+
async def wait_for_connection(self, timeout_ms: float) -> None: ...
|
|
48
|
+
def start_handling_requests(self) -> None: ...
|
|
49
|
+
async def close_socket_and_resume(self) -> None: ...
|
|
50
|
+
async def stop_comms(self) -> None: ...
|
|
51
|
+
def hold_for_server_restart(self, on_closed: Callable[[], None]) -> None: ...
|
|
52
|
+
def get_comms_state(self) -> CommsState: ...
|
|
53
|
+
def get_server_static_key(self) -> bytes | None: ...
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
class WaCredentialsLike(Protocol):
|
|
57
|
+
"""Credentials surface read by the manager."""
|
|
58
|
+
|
|
59
|
+
me_jid: str | None
|
|
60
|
+
routing_info: object
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
class WaAuthClientLike(Protocol):
|
|
64
|
+
"""Auth-client surface used by the manager."""
|
|
65
|
+
|
|
66
|
+
async def load_or_create_credentials(self) -> WaCredentialsLike: ...
|
|
67
|
+
async def clear_routing_info(self) -> WaCredentialsLike: ...
|
|
68
|
+
def get_current_credentials(self) -> WaCredentialsLike | None: ...
|
|
69
|
+
async def build_comms_config(
|
|
70
|
+
self, options: object, overrides: Mapping[str, object]
|
|
71
|
+
) -> WaCommsConfig: ...
|
|
72
|
+
async def persist_server_static_key(self, key: bytes) -> None: ...
|
|
73
|
+
async def clear_transient_state(self) -> None: ...
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
class WaKeepAliveLike(Protocol):
|
|
77
|
+
"""Keep-alive surface used by the manager."""
|
|
78
|
+
|
|
79
|
+
def start(self) -> None: ...
|
|
80
|
+
def stop(self) -> None: ...
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
class WaNodeOrchestratorLike(Protocol):
|
|
84
|
+
"""Orchestrator surface used by the manager."""
|
|
85
|
+
|
|
86
|
+
def clear_pending(self, error: Exception) -> None: ...
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
class WaNodeTransportLike(Protocol):
|
|
90
|
+
"""Node-transport surface used by the manager."""
|
|
91
|
+
|
|
92
|
+
def bind_comms(self, comms: WaCommsLike | None) -> None: ...
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
class WaPassiveTasksLike(Protocol):
|
|
96
|
+
"""Passive-tasks surface used by the manager."""
|
|
97
|
+
|
|
98
|
+
def reset_in_flight_state(self) -> None: ...
|
|
99
|
+
def start_passive_tasks_after_connect(self) -> None: ...
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
class WaConnectionManager:
|
|
103
|
+
"""Serializes connect/disconnect lifecycles and the pairing restart."""
|
|
104
|
+
|
|
105
|
+
def __init__(
|
|
106
|
+
self,
|
|
107
|
+
*,
|
|
108
|
+
logger: Logger,
|
|
109
|
+
options: object,
|
|
110
|
+
auth_client: WaAuthClientLike,
|
|
111
|
+
keep_alive: WaKeepAliveLike,
|
|
112
|
+
node_orchestrator: WaNodeOrchestratorLike,
|
|
113
|
+
node_transport: WaNodeTransportLike,
|
|
114
|
+
get_passive_tasks: Callable[[], WaPassiveTasksLike | None],
|
|
115
|
+
clear_stored_credentials: Callable[[], Awaitable[None]],
|
|
116
|
+
on_post_pair_reconnected: Callable[[], None],
|
|
117
|
+
create_comms: Callable[[WaCommsConfig, Logger], WaCommsLike],
|
|
118
|
+
pairing_restart_wait_ms: float = PAIRING_RESTART_WAIT_MS,
|
|
119
|
+
) -> None:
|
|
120
|
+
self._logger = logger
|
|
121
|
+
self._options = options
|
|
122
|
+
self._auth_client = auth_client
|
|
123
|
+
self._keep_alive = keep_alive
|
|
124
|
+
self._node_orchestrator = node_orchestrator
|
|
125
|
+
self._node_transport = node_transport
|
|
126
|
+
self._get_passive_tasks = get_passive_tasks
|
|
127
|
+
self._clear_stored_credentials_cb = clear_stored_credentials
|
|
128
|
+
self._on_post_pair_reconnected = on_post_pair_reconnected
|
|
129
|
+
self._create_comms = create_comms
|
|
130
|
+
self._pairing_restart_wait_ms = pairing_restart_wait_ms
|
|
131
|
+
self._comms: WaCommsLike | None = None
|
|
132
|
+
self._clock_skew_ms: float | None = None
|
|
133
|
+
self._media_conn_cache: object = None
|
|
134
|
+
self._connect_task: asyncio.Task[None] | None = None
|
|
135
|
+
self._pairing_reconnect_task: asyncio.Task[None] | None = None
|
|
136
|
+
self._pair_restart_timer: asyncio.TimerHandle | None = None
|
|
137
|
+
self._frame_handler: FrameHandler | None = None
|
|
138
|
+
self._pending_comms: WaCommsLike | None = None
|
|
139
|
+
self._lifecycle_generation = 0
|
|
140
|
+
self._lifecycle_lock = asyncio.Lock()
|
|
141
|
+
|
|
142
|
+
async def connect(self, frame_handler: FrameHandler) -> None:
|
|
143
|
+
"""Connect; concurrent callers join the in-flight attempt."""
|
|
144
|
+
if self._connect_task is not None:
|
|
145
|
+
self._logger.trace("wa client connect already in-flight")
|
|
146
|
+
await asyncio.shield(self._connect_task)
|
|
147
|
+
return
|
|
148
|
+
self._frame_handler = frame_handler
|
|
149
|
+
generation = self._next_lifecycle_generation()
|
|
150
|
+
|
|
151
|
+
async def _run() -> None:
|
|
152
|
+
await self._connect_internal(frame_handler, generation)
|
|
153
|
+
|
|
154
|
+
task = asyncio.ensure_future(self._run_lifecycle_operation(_run))
|
|
155
|
+
self._connect_task = task
|
|
156
|
+
try:
|
|
157
|
+
await asyncio.shield(task)
|
|
158
|
+
finally:
|
|
159
|
+
if self._connect_task is task:
|
|
160
|
+
self._connect_task = None
|
|
161
|
+
|
|
162
|
+
def await_server_restart_after_pairing(self) -> None:
|
|
163
|
+
"""Start the post-pairing restart window; the server ends the socket.
|
|
164
|
+
|
|
165
|
+
The unregistered socket stays open until the ``stream:error`` 515
|
|
166
|
+
signal, a server close, or the fallback timer, whichever comes first.
|
|
167
|
+
"""
|
|
168
|
+
comms = self._comms
|
|
169
|
+
if comms is None:
|
|
170
|
+
self._logger.trace("pairing restart wait skipped: no active comms")
|
|
171
|
+
return
|
|
172
|
+
self._clear_pair_restart_timer()
|
|
173
|
+
self._logger.debug(
|
|
174
|
+
"wa client waiting for server restart after pairing",
|
|
175
|
+
{"fallbackMs": self._pairing_restart_wait_ms},
|
|
176
|
+
)
|
|
177
|
+
loop = asyncio.get_running_loop()
|
|
178
|
+
self._pair_restart_timer = loop.call_later(
|
|
179
|
+
self._pairing_restart_wait_ms / 1000.0,
|
|
180
|
+
lambda: self._start_pair_restart("fallback_timeout"),
|
|
181
|
+
)
|
|
182
|
+
comms.hold_for_server_restart(lambda: self._start_pair_restart("socket_closed"))
|
|
183
|
+
|
|
184
|
+
def handle_server_restart_request(self) -> bool:
|
|
185
|
+
"""Claim a ``stream:error`` 515 for the pairing restart window."""
|
|
186
|
+
if self._pair_restart_timer is not None:
|
|
187
|
+
self._start_pair_restart("stream_error_515")
|
|
188
|
+
return True
|
|
189
|
+
return self._pairing_reconnect_task is not None
|
|
190
|
+
|
|
191
|
+
async def disconnect(self) -> None:
|
|
192
|
+
"""Tear down comms, timers, and transient auth state."""
|
|
193
|
+
self._clear_pair_restart_timer()
|
|
194
|
+
generation = self._next_lifecycle_generation()
|
|
195
|
+
pending = self._pending_comms
|
|
196
|
+
if pending is not None:
|
|
197
|
+
self._pending_comms = None
|
|
198
|
+
self._stop_comms_in_background(
|
|
199
|
+
pending, "failed to stop pending comms during disconnect"
|
|
200
|
+
)
|
|
201
|
+
await self._run_lifecycle_operation(
|
|
202
|
+
lambda: self._disconnect_internal(generation)
|
|
203
|
+
)
|
|
204
|
+
|
|
205
|
+
async def clear_stored_credentials(self) -> None:
|
|
206
|
+
"""Clear persisted credentials via the injected callback."""
|
|
207
|
+
await self._clear_stored_credentials_cb()
|
|
208
|
+
|
|
209
|
+
def is_connected(self) -> bool:
|
|
210
|
+
"""Whether live comms report a connected socket."""
|
|
211
|
+
return bool(self._comms is not None and self._comms.get_comms_state().connected)
|
|
212
|
+
|
|
213
|
+
def get_comms(self) -> WaCommsLike | None:
|
|
214
|
+
"""The active comms object, if any."""
|
|
215
|
+
return self._comms
|
|
216
|
+
|
|
217
|
+
def get_clock_skew_ms(self) -> float | None:
|
|
218
|
+
"""Last measured server-minus-local clock skew, if any."""
|
|
219
|
+
return self._clock_skew_ms
|
|
220
|
+
|
|
221
|
+
def get_media_conn_cache(self) -> object:
|
|
222
|
+
"""Cached media-connection endpoints, if any."""
|
|
223
|
+
return self._media_conn_cache
|
|
224
|
+
|
|
225
|
+
def set_media_conn_cache(self, value: object) -> None:
|
|
226
|
+
"""Replace the cached media-connection endpoints."""
|
|
227
|
+
self._media_conn_cache = value
|
|
228
|
+
|
|
229
|
+
def update_clock_skew_from_success(self, server_unix_seconds: float) -> None:
|
|
230
|
+
"""Recompute skew from a server timestamp (seconds)."""
|
|
231
|
+
server_ms = server_unix_seconds * 1000.0
|
|
232
|
+
self._clock_skew_ms = server_ms - time.time() * 1000.0
|
|
233
|
+
self._logger.debug(
|
|
234
|
+
"updated clock skew from success",
|
|
235
|
+
{
|
|
236
|
+
"serverUnixSeconds": server_unix_seconds,
|
|
237
|
+
"clockSkewMs": self._clock_skew_ms,
|
|
238
|
+
},
|
|
239
|
+
)
|
|
240
|
+
|
|
241
|
+
def set_clock_skew_ms(self, clock_skew_ms: float, source: str) -> None:
|
|
242
|
+
"""Record skew unless non-finite; logs only on material changes."""
|
|
243
|
+
import math
|
|
244
|
+
|
|
245
|
+
if not math.isfinite(clock_skew_ms):
|
|
246
|
+
return
|
|
247
|
+
previous = self._clock_skew_ms
|
|
248
|
+
self._clock_skew_ms = clock_skew_ms
|
|
249
|
+
if previous is None or abs(previous - clock_skew_ms) >= 1000.0:
|
|
250
|
+
self._logger.debug(
|
|
251
|
+
"clock skew updated",
|
|
252
|
+
{"source": source, "clockSkewMs": clock_skew_ms, "previous": previous},
|
|
253
|
+
)
|
|
254
|
+
|
|
255
|
+
async def _connect_internal(
|
|
256
|
+
self, frame_handler: FrameHandler, generation: int
|
|
257
|
+
) -> None:
|
|
258
|
+
self._assert_lifecycle_current(generation, "connect")
|
|
259
|
+
if self._comms is not None or self._pending_comms is not None:
|
|
260
|
+
self._logger.trace("wa client connect skipped: comms already created")
|
|
261
|
+
return
|
|
262
|
+
self._logger.debug("wa client connect start")
|
|
263
|
+
credentials = await self._auth_client.load_or_create_credentials()
|
|
264
|
+
self._assert_lifecycle_current(generation, "connect")
|
|
265
|
+
try:
|
|
266
|
+
await self._start_comms_with_credentials(
|
|
267
|
+
credentials, frame_handler, generation
|
|
268
|
+
)
|
|
269
|
+
except Exception as error:
|
|
270
|
+
if self._is_lifecycle_superseded_error(error):
|
|
271
|
+
raise
|
|
272
|
+
if credentials.routing_info:
|
|
273
|
+
self._logger.warn(
|
|
274
|
+
"connect failed with routing info, retrying without routing info",
|
|
275
|
+
{
|
|
276
|
+
"message": to_error(error).args[0]
|
|
277
|
+
if to_error(error).args
|
|
278
|
+
else ""
|
|
279
|
+
},
|
|
280
|
+
)
|
|
281
|
+
await self._disconnect_internal(generation)
|
|
282
|
+
self._assert_lifecycle_current(generation, "connect")
|
|
283
|
+
credentials = await self._auth_client.clear_routing_info()
|
|
284
|
+
self._assert_lifecycle_current(generation, "connect")
|
|
285
|
+
await self._start_comms_with_credentials(
|
|
286
|
+
credentials, frame_handler, generation
|
|
287
|
+
)
|
|
288
|
+
else:
|
|
289
|
+
await self._disconnect_internal(generation)
|
|
290
|
+
raise
|
|
291
|
+
self._assert_lifecycle_current(generation, "connect")
|
|
292
|
+
self._logger.info("wa client connected")
|
|
293
|
+
|
|
294
|
+
def _start_pair_restart(self, trigger: str) -> None:
|
|
295
|
+
if self._pair_restart_timer is None:
|
|
296
|
+
return
|
|
297
|
+
self._clear_pair_restart_timer()
|
|
298
|
+
self._logger.debug("wa client restarting after pairing", {"trigger": trigger})
|
|
299
|
+
if self._pairing_reconnect_task is not None:
|
|
300
|
+
self._logger.trace("pairing reconnect already in-flight")
|
|
301
|
+
return
|
|
302
|
+
task = asyncio.ensure_future(
|
|
303
|
+
self._run_lifecycle_operation(
|
|
304
|
+
self._reconnect_as_registered_after_pairing_internal
|
|
305
|
+
)
|
|
306
|
+
)
|
|
307
|
+
self._pairing_reconnect_task = task
|
|
308
|
+
task.add_done_callback(self._on_pairing_reconnect_done)
|
|
309
|
+
|
|
310
|
+
def _log_pairing_reconnect_failure(self, task: asyncio.Task[None]) -> None:
|
|
311
|
+
if task.cancelled():
|
|
312
|
+
return
|
|
313
|
+
error = task.exception()
|
|
314
|
+
if error is None:
|
|
315
|
+
return
|
|
316
|
+
if self._is_lifecycle_superseded_error(error):
|
|
317
|
+
self._logger.trace("pairing reconnect canceled by newer lifecycle")
|
|
318
|
+
return
|
|
319
|
+
current = self._auth_client.get_current_credentials()
|
|
320
|
+
self._logger.warn(
|
|
321
|
+
"failed to reconnect after pairing",
|
|
322
|
+
{
|
|
323
|
+
"meJid": current.me_jid if current is not None else None,
|
|
324
|
+
"message": to_error(error).args[0] if to_error(error).args else "",
|
|
325
|
+
},
|
|
326
|
+
)
|
|
327
|
+
|
|
328
|
+
def _clear_pair_restart_timer(self) -> None:
|
|
329
|
+
if self._pair_restart_timer is None:
|
|
330
|
+
return
|
|
331
|
+
self._pair_restart_timer.cancel()
|
|
332
|
+
self._pair_restart_timer = None
|
|
333
|
+
|
|
334
|
+
def _on_pairing_reconnect_done(self, task: asyncio.Task[None]) -> None:
|
|
335
|
+
if self._pairing_reconnect_task is task:
|
|
336
|
+
self._pairing_reconnect_task = None
|
|
337
|
+
self._log_pairing_reconnect_failure(task)
|
|
338
|
+
|
|
339
|
+
async def _reconnect_as_registered_after_pairing_internal(self) -> None:
|
|
340
|
+
frame_handler = self._frame_handler
|
|
341
|
+
if frame_handler is None:
|
|
342
|
+
raise ValueError("pairing reconnect requires an active frame handler")
|
|
343
|
+
credentials = self._auth_client.get_current_credentials()
|
|
344
|
+
if credentials is None or not credentials.me_jid:
|
|
345
|
+
self._logger.trace("pairing reconnect skipped: still unregistered")
|
|
346
|
+
return
|
|
347
|
+
current_comms = self._comms
|
|
348
|
+
if current_comms is None:
|
|
349
|
+
self._logger.trace("pairing reconnect skipped: no active comms")
|
|
350
|
+
return
|
|
351
|
+
generation = self._next_lifecycle_generation()
|
|
352
|
+
self._assert_lifecycle_current(generation, "pairing reconnect")
|
|
353
|
+
self._logger.info("pairing completed, restarting comms as registered")
|
|
354
|
+
self._keep_alive.stop()
|
|
355
|
+
self._node_orchestrator.clear_pending(
|
|
356
|
+
ValueError("restarting comms after pairing")
|
|
357
|
+
)
|
|
358
|
+
self._clear_comms_binding(current_comms)
|
|
359
|
+
await self._stop_comms_quietly(
|
|
360
|
+
current_comms, "failed to stop pre-registration comms"
|
|
361
|
+
)
|
|
362
|
+
self._assert_lifecycle_current(generation, "pairing reconnect")
|
|
363
|
+
await self._start_comms_with_credentials(credentials, frame_handler, generation)
|
|
364
|
+
self._assert_lifecycle_current(generation, "pairing reconnect")
|
|
365
|
+
self._on_post_pair_reconnected()
|
|
366
|
+
|
|
367
|
+
async def _disconnect_internal(self, generation: int) -> None:
|
|
368
|
+
if not self._is_lifecycle_current(generation):
|
|
369
|
+
return
|
|
370
|
+
self._logger.debug("wa client disconnect start")
|
|
371
|
+
self._clear_pair_restart_timer()
|
|
372
|
+
self._keep_alive.stop()
|
|
373
|
+
self._node_orchestrator.clear_pending(ValueError("client disconnected"))
|
|
374
|
+
self._clock_skew_ms = None
|
|
375
|
+
self._media_conn_cache = None
|
|
376
|
+
self._frame_handler = None
|
|
377
|
+
passive = self._get_passive_tasks()
|
|
378
|
+
if passive is not None:
|
|
379
|
+
passive.reset_in_flight_state()
|
|
380
|
+
current_comms = self._comms
|
|
381
|
+
pending_comms = self._pending_comms
|
|
382
|
+
self._pending_comms = None
|
|
383
|
+
self._clear_comms_binding(None if current_comms is None else current_comms)
|
|
384
|
+
await self._auth_client.clear_transient_state()
|
|
385
|
+
stops: list[Awaitable[None]] = []
|
|
386
|
+
if pending_comms is not None and pending_comms is not current_comms:
|
|
387
|
+
stops.append(
|
|
388
|
+
self._stop_comms_quietly(
|
|
389
|
+
pending_comms, "failed to stop pending comms during disconnect"
|
|
390
|
+
)
|
|
391
|
+
)
|
|
392
|
+
if current_comms is not None:
|
|
393
|
+
stops.append(
|
|
394
|
+
self._stop_comms_quietly(
|
|
395
|
+
current_comms, "failed to stop comms during disconnect"
|
|
396
|
+
)
|
|
397
|
+
)
|
|
398
|
+
if stops:
|
|
399
|
+
await asyncio.gather(*stops)
|
|
400
|
+
if self._is_lifecycle_current(generation):
|
|
401
|
+
self._logger.debug("wa client disconnected")
|
|
402
|
+
|
|
403
|
+
async def _start_comms_with_credentials(
|
|
404
|
+
self,
|
|
405
|
+
credentials: WaCredentialsLike,
|
|
406
|
+
frame_handler: FrameHandler,
|
|
407
|
+
generation: int,
|
|
408
|
+
) -> None:
|
|
409
|
+
self._assert_lifecycle_current(generation, "start comms")
|
|
410
|
+
self._logger.debug(
|
|
411
|
+
"starting comms with credentials",
|
|
412
|
+
{"registered": credentials.me_jid is not None},
|
|
413
|
+
)
|
|
414
|
+
options = self._options
|
|
415
|
+
test_hooks = getattr(options, "test_hooks", None)
|
|
416
|
+
noise_root_ca = getattr(test_hooks, "noise_root_ca", None)
|
|
417
|
+
dangerous = getattr(options, "dangerous", None)
|
|
418
|
+
comms_config = await self._auth_client.build_comms_config(
|
|
419
|
+
options,
|
|
420
|
+
{
|
|
421
|
+
"noiseTrustedRootCa": noise_root_ca,
|
|
422
|
+
"disableNoiseCertificateChainVerification": getattr(
|
|
423
|
+
dangerous, "disable_noise_certificate_chain_verification", None
|
|
424
|
+
),
|
|
425
|
+
},
|
|
426
|
+
)
|
|
427
|
+
comms = self._create_comms(comms_config, self._logger)
|
|
428
|
+
self._pending_comms = comms
|
|
429
|
+
self._media_conn_cache = None
|
|
430
|
+
comms.start_comms(frame_handler)
|
|
431
|
+
try:
|
|
432
|
+
await comms.wait_for_connection(float(comms_config.connect_timeout_ms))
|
|
433
|
+
self._assert_lifecycle_current(generation, "start comms")
|
|
434
|
+
if self._pending_comms is not comms:
|
|
435
|
+
raise self._create_lifecycle_superseded_error("start comms")
|
|
436
|
+
self._comms = comms
|
|
437
|
+
self._pending_comms = None
|
|
438
|
+
self._node_transport.bind_comms(comms)
|
|
439
|
+
self._logger.debug("comms connected")
|
|
440
|
+
comms.start_handling_requests()
|
|
441
|
+
if credentials.me_jid:
|
|
442
|
+
self._keep_alive.start()
|
|
443
|
+
else:
|
|
444
|
+
self._keep_alive.stop()
|
|
445
|
+
server_static_key = comms.get_server_static_key()
|
|
446
|
+
if not server_static_key:
|
|
447
|
+
self._logger.trace("no server static key available to persist")
|
|
448
|
+
elif not credentials.me_jid:
|
|
449
|
+
self._logger.trace(
|
|
450
|
+
"skipping server static key persist while unregistered"
|
|
451
|
+
)
|
|
452
|
+
else:
|
|
453
|
+
await self._auth_client.persist_server_static_key(server_static_key)
|
|
454
|
+
self._assert_lifecycle_current(generation, "start comms")
|
|
455
|
+
self._logger.debug("persisted server static key after comms connect")
|
|
456
|
+
passive = self._get_passive_tasks()
|
|
457
|
+
if passive is not None:
|
|
458
|
+
passive.start_passive_tasks_after_connect()
|
|
459
|
+
except Exception as error:
|
|
460
|
+
if self._pending_comms is comms:
|
|
461
|
+
self._pending_comms = None
|
|
462
|
+
self._clear_comms_binding(comms)
|
|
463
|
+
await self._stop_comms_quietly(
|
|
464
|
+
comms, "failed to cleanup comms after connection start failure"
|
|
465
|
+
)
|
|
466
|
+
if self._is_lifecycle_current(generation):
|
|
467
|
+
raise
|
|
468
|
+
raise self._create_lifecycle_superseded_error("start comms") from error
|
|
469
|
+
|
|
470
|
+
def _clear_comms_binding(self, expected_comms: WaCommsLike | None) -> None:
|
|
471
|
+
if (
|
|
472
|
+
expected_comms is not None
|
|
473
|
+
and self._comms is not None
|
|
474
|
+
and self._comms is not expected_comms
|
|
475
|
+
):
|
|
476
|
+
return
|
|
477
|
+
self._comms = None
|
|
478
|
+
self._node_transport.bind_comms(None)
|
|
479
|
+
|
|
480
|
+
async def _stop_comms_quietly(
|
|
481
|
+
self, comms: WaCommsLike, warning_message: str
|
|
482
|
+
) -> None:
|
|
483
|
+
try:
|
|
484
|
+
await comms.stop_comms()
|
|
485
|
+
except Exception as error:
|
|
486
|
+
normalized = to_error(error)
|
|
487
|
+
self._logger.warn(
|
|
488
|
+
warning_message,
|
|
489
|
+
{"message": normalized.args[0] if normalized.args else ""},
|
|
490
|
+
)
|
|
491
|
+
|
|
492
|
+
def _stop_comms_in_background(
|
|
493
|
+
self, comms: WaCommsLike, warning_message: str
|
|
494
|
+
) -> None:
|
|
495
|
+
task = asyncio.ensure_future(self._stop_comms_quietly(comms, warning_message))
|
|
496
|
+
task.add_done_callback(_consume_task_outcome)
|
|
497
|
+
|
|
498
|
+
def _next_lifecycle_generation(self) -> int:
|
|
499
|
+
self._lifecycle_generation += 1
|
|
500
|
+
return self._lifecycle_generation
|
|
501
|
+
|
|
502
|
+
async def _run_lifecycle_operation(
|
|
503
|
+
self, operation: Callable[[], Awaitable[None]]
|
|
504
|
+
) -> None:
|
|
505
|
+
async with self._lifecycle_lock:
|
|
506
|
+
await operation()
|
|
507
|
+
|
|
508
|
+
def _is_lifecycle_current(self, generation: int) -> bool:
|
|
509
|
+
return self._lifecycle_generation == generation
|
|
510
|
+
|
|
511
|
+
def _assert_lifecycle_current(self, generation: int, context: str) -> None:
|
|
512
|
+
if not self._is_lifecycle_current(generation):
|
|
513
|
+
raise self._create_lifecycle_superseded_error(context)
|
|
514
|
+
|
|
515
|
+
@staticmethod
|
|
516
|
+
def _create_lifecycle_superseded_error(context: str) -> ValueError:
|
|
517
|
+
return ValueError(f"{context} superseded by newer lifecycle")
|
|
518
|
+
|
|
519
|
+
def _is_lifecycle_superseded_error(self, error: object) -> bool:
|
|
520
|
+
normalized = to_error(error)
|
|
521
|
+
message = normalized.args[0] if normalized.args else ""
|
|
522
|
+
return "superseded by newer lifecycle" in str(message)
|
|
523
|
+
|
|
524
|
+
|
|
525
|
+
__all__ = ["PAIRING_RESTART_WAIT_MS", "WaConnectionManager"]
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
"""Dangling receipt queue (mirrors ``WaReceiptQueue``).
|
|
2
|
+
|
|
3
|
+
Receipts that fail to send because the socket dropped are buffered here and
|
|
4
|
+
replayed after reconnect instead of thrown.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
from zapy._transport.types import BinaryNode
|
|
10
|
+
|
|
11
|
+
# Verbatim ``src/protocol/defaults.ts`` (lands with the protocol layer).
|
|
12
|
+
_MAX_DANGLING_RECEIPTS: int = 2_048
|
|
13
|
+
|
|
14
|
+
# Verbatim ``src/protocol/message.ts`` (receipt tag).
|
|
15
|
+
_RECEIPT_TAG: str = "receipt"
|
|
16
|
+
|
|
17
|
+
# Verbatim error shapes the oracle treats as a dropped socket.
|
|
18
|
+
_SOCKET_CLOSED_PREFIX: str = "socket closed ("
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def _is_socket_closed_error(message: str) -> bool:
|
|
22
|
+
normalized = message.strip().lower()
|
|
23
|
+
return (
|
|
24
|
+
normalized == "comms is not connected"
|
|
25
|
+
or normalized == "websocket is not connected"
|
|
26
|
+
or normalized == "noise session socket closed"
|
|
27
|
+
or normalized.startswith(_SOCKET_CLOSED_PREFIX)
|
|
28
|
+
)
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
class WaReceiptQueue:
|
|
32
|
+
"""Bounded ring of receipts that failed to send on a dead socket."""
|
|
33
|
+
|
|
34
|
+
def __init__(self, *, max_size: int = _MAX_DANGLING_RECEIPTS) -> None:
|
|
35
|
+
self._max_size = max_size
|
|
36
|
+
self._nodes: list[BinaryNode] = []
|
|
37
|
+
self._head = 0
|
|
38
|
+
|
|
39
|
+
def should_queue(self, node: BinaryNode, error: BaseException) -> bool:
|
|
40
|
+
"""Whether a failed ``node`` send with ``error`` should be buffered."""
|
|
41
|
+
if node.tag != _RECEIPT_TAG:
|
|
42
|
+
return False
|
|
43
|
+
message = str(error.args[0]) if error.args else str(error)
|
|
44
|
+
return _is_socket_closed_error(message)
|
|
45
|
+
|
|
46
|
+
def enqueue(self, node: BinaryNode) -> None:
|
|
47
|
+
"""Buffer one receipt, dropping the oldest past the max size."""
|
|
48
|
+
if self._max_size <= 0:
|
|
49
|
+
return
|
|
50
|
+
if self.size() >= self._max_size:
|
|
51
|
+
self._head += 1
|
|
52
|
+
if self._head > 64 and self._head * 2 >= len(self._nodes):
|
|
53
|
+
live = self._nodes[self._head :]
|
|
54
|
+
self._nodes = live
|
|
55
|
+
self._head = 0
|
|
56
|
+
self._nodes.append(node)
|
|
57
|
+
|
|
58
|
+
def take(self) -> list[BinaryNode]:
|
|
59
|
+
"""Drain the queue in insertion order."""
|
|
60
|
+
if self._head >= len(self._nodes):
|
|
61
|
+
self._nodes = []
|
|
62
|
+
self._head = 0
|
|
63
|
+
return []
|
|
64
|
+
out = self._nodes[self._head :]
|
|
65
|
+
self._nodes = []
|
|
66
|
+
self._head = 0
|
|
67
|
+
return out
|
|
68
|
+
|
|
69
|
+
def size(self) -> int:
|
|
70
|
+
"""Number of buffered receipts."""
|
|
71
|
+
return len(self._nodes) - self._head
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
__all__ = ["WaReceiptQueue"]
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
"""Connection coordinators (mirrors ``src/client/coordinators``)."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from zapy._client.coordinators.ab_props import WaAbPropsCoordinator
|
|
6
|
+
from zapy._client.coordinators.broadcast_list import create_broadcast_list_coordinator
|
|
7
|
+
from zapy._client.coordinators.business import create_business_coordinator
|
|
8
|
+
from zapy._client.coordinators.email import create_email_coordinator
|
|
9
|
+
from zapy._client.coordinators.group import (
|
|
10
|
+
WaGroupCoordinator,
|
|
11
|
+
create_group_coordinator,
|
|
12
|
+
)
|
|
13
|
+
from zapy._client.coordinators.incoming_node import WaIncomingNodeCoordinator
|
|
14
|
+
from zapy._client.coordinators.message import WaMessageCoordinator
|
|
15
|
+
from zapy._client.coordinators.message_dispatch import (
|
|
16
|
+
WaGroupHistoryAudience,
|
|
17
|
+
WaMessageDispatchCoordinator,
|
|
18
|
+
WaMessageDispatchOptions,
|
|
19
|
+
resolve_outbound_stanza_shape,
|
|
20
|
+
restrict_group_history_targets,
|
|
21
|
+
)
|
|
22
|
+
from zapy._client.coordinators.low_level import create_low_level_coordinator
|
|
23
|
+
from zapy._client.coordinators.newsletter import create_newsletter_coordinator
|
|
24
|
+
from zapy._client.coordinators.offline_resume import WaOfflineResumeCoordinator
|
|
25
|
+
from zapy._client.coordinators.presence import create_presence_coordinator
|
|
26
|
+
from zapy._client.coordinators.profile import create_profile_coordinator
|
|
27
|
+
from zapy._client.coordinators.retry import (
|
|
28
|
+
WaParsedRetryRequest,
|
|
29
|
+
WaRetryCoordinator,
|
|
30
|
+
WaRetryCoordinatorOptions,
|
|
31
|
+
WaRetryDecryptFailureContext,
|
|
32
|
+
WaRetryOutboundMessageRecord,
|
|
33
|
+
map_retry_reason_from_error,
|
|
34
|
+
parse_retry_receipt_request,
|
|
35
|
+
pick_retry_state_max,
|
|
36
|
+
)
|
|
37
|
+
from zapy._client.coordinators import (
|
|
38
|
+
app_state_mutation as app_state_mutation,
|
|
39
|
+
bot as bot,
|
|
40
|
+
passive_tasks as passive_tasks,
|
|
41
|
+
privacy as privacy,
|
|
42
|
+
trusted_contact_token as trusted_contact_token,
|
|
43
|
+
)
|
|
44
|
+
from zapy._client.coordinators.status import create_status_coordinator
|
|
45
|
+
from zapy._client.coordinators.stream_control import create_stream_control_handler
|
|
46
|
+
|
|
47
|
+
__all__ = [
|
|
48
|
+
"WaAbPropsCoordinator",
|
|
49
|
+
"WaGroupCoordinator",
|
|
50
|
+
"WaGroupHistoryAudience",
|
|
51
|
+
"WaIncomingNodeCoordinator",
|
|
52
|
+
"WaMessageCoordinator",
|
|
53
|
+
"WaMessageDispatchCoordinator",
|
|
54
|
+
"WaMessageDispatchOptions",
|
|
55
|
+
"WaOfflineResumeCoordinator",
|
|
56
|
+
"WaParsedRetryRequest",
|
|
57
|
+
"WaRetryCoordinator",
|
|
58
|
+
"WaRetryCoordinatorOptions",
|
|
59
|
+
"WaRetryDecryptFailureContext",
|
|
60
|
+
"WaRetryOutboundMessageRecord",
|
|
61
|
+
"app_state_mutation",
|
|
62
|
+
"bot",
|
|
63
|
+
"create_broadcast_list_coordinator",
|
|
64
|
+
"create_business_coordinator",
|
|
65
|
+
"create_email_coordinator",
|
|
66
|
+
"create_group_coordinator",
|
|
67
|
+
"create_low_level_coordinator",
|
|
68
|
+
"create_newsletter_coordinator",
|
|
69
|
+
"create_presence_coordinator",
|
|
70
|
+
"create_profile_coordinator",
|
|
71
|
+
"create_status_coordinator",
|
|
72
|
+
"create_stream_control_handler",
|
|
73
|
+
"map_retry_reason_from_error",
|
|
74
|
+
"parse_retry_receipt_request",
|
|
75
|
+
"passive_tasks",
|
|
76
|
+
"pick_retry_state_max",
|
|
77
|
+
"privacy",
|
|
78
|
+
"resolve_outbound_stanza_shape",
|
|
79
|
+
"restrict_group_history_targets",
|
|
80
|
+
"trusted_contact_token",
|
|
81
|
+
]
|