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.
Files changed (345) hide show
  1. shellzapy-0.1.0.dist-info/METADATA +112 -0
  2. shellzapy-0.1.0.dist-info/RECORD +345 -0
  3. shellzapy-0.1.0.dist-info/WHEEL +4 -0
  4. shellzapy-0.1.0.dist-info/licenses/LICENSE +21 -0
  5. shellzapy-0.1.0.dist-info/licenses/NOTICE +9 -0
  6. zapy/__init__.py +58 -0
  7. zapy/_appstate/__init__.py +59 -0
  8. zapy/_appstate/constants.py +82 -0
  9. zapy/_appstate/crypto/__init__.py +17 -0
  10. zapy/_appstate/crypto/app_state_crypto.py +401 -0
  11. zapy/_appstate/parsers/__init__.py +23 -0
  12. zapy/_appstate/parsers/encoding.py +157 -0
  13. zapy/_appstate/parsers/response_parser.py +135 -0
  14. zapy/_appstate/sync/__init__.py +11 -0
  15. zapy/_appstate/sync/sync_client.py +1559 -0
  16. zapy/_appstate/types.py +137 -0
  17. zapy/_appstate/utils.py +100 -0
  18. zapy/_auth/__init__.py +3 -0
  19. zapy/_auth/auth_client.py +618 -0
  20. zapy/_auth/credentials_flow.py +290 -0
  21. zapy/_auth/pairing/__init__.py +3 -0
  22. zapy/_auth/pairing/companion_host.py +239 -0
  23. zapy/_auth/pairing/flow.py +808 -0
  24. zapy/_auth/pairing/pairing_code_crypto.py +143 -0
  25. zapy/_auth/pairing/qr_flow.py +123 -0
  26. zapy/_auth/pairing/shortcake_crypto.py +122 -0
  27. zapy/_auth/pairing/shortcake_flow.py +393 -0
  28. zapy/_auth/types.py +110 -0
  29. zapy/_client/__init__.py +26 -0
  30. zapy/_client/client.py +972 -0
  31. zapy/_client/connection/__init__.py +6 -0
  32. zapy/_client/connection/manager.py +525 -0
  33. zapy/_client/connection/receipt_queue.py +74 -0
  34. zapy/_client/coordinators/__init__.py +81 -0
  35. zapy/_client/coordinators/ab_props.py +260 -0
  36. zapy/_client/coordinators/app_state_mutation.py +2538 -0
  37. zapy/_client/coordinators/bot.py +830 -0
  38. zapy/_client/coordinators/broadcast_list.py +101 -0
  39. zapy/_client/coordinators/business.py +247 -0
  40. zapy/_client/coordinators/email.py +158 -0
  41. zapy/_client/coordinators/group.py +1478 -0
  42. zapy/_client/coordinators/incoming_node.py +758 -0
  43. zapy/_client/coordinators/low_level.py +119 -0
  44. zapy/_client/coordinators/message.py +1153 -0
  45. zapy/_client/coordinators/message_dispatch.py +1637 -0
  46. zapy/_client/coordinators/newsletter.py +110 -0
  47. zapy/_client/coordinators/offline_resume.py +271 -0
  48. zapy/_client/coordinators/passive_tasks.py +447 -0
  49. zapy/_client/coordinators/presence.py +103 -0
  50. zapy/_client/coordinators/privacy.py +623 -0
  51. zapy/_client/coordinators/profile.py +1024 -0
  52. zapy/_client/coordinators/retry.py +1878 -0
  53. zapy/_client/coordinators/status.py +132 -0
  54. zapy/_client/coordinators/stream_control.py +260 -0
  55. zapy/_client/coordinators/trusted_contact_token.py +417 -0
  56. zapy/_client/events/__init__.py +266 -0
  57. zapy/_client/events/abprops.py +58 -0
  58. zapy/_client/events/appstate_mutation.py +248 -0
  59. zapy/_client/events/business.py +565 -0
  60. zapy/_client/events/call.py +144 -0
  61. zapy/_client/events/chatstate.py +41 -0
  62. zapy/_client/events/devices.py +122 -0
  63. zapy/_client/events/dirty.py +384 -0
  64. zapy/_client/events/group.py +655 -0
  65. zapy/_client/events/identity.py +40 -0
  66. zapy/_client/events/incoming.py +1079 -0
  67. zapy/_client/events/mex_notification.py +317 -0
  68. zapy/_client/events/offline.py +123 -0
  69. zapy/_client/events/picture.py +67 -0
  70. zapy/_client/events/presence.py +74 -0
  71. zapy/_client/events/privacy.py +187 -0
  72. zapy/_client/events/privacy_token.py +72 -0
  73. zapy/_client/events/receipt.py +109 -0
  74. zapy/_client/events/registration.py +70 -0
  75. zapy/_client/events/username.py +76 -0
  76. zapy/_client/factory.py +2020 -0
  77. zapy/_client/media.py +829 -0
  78. zapy/_client/newsletter/__init__.py +90 -0
  79. zapy/_client/newsletter/admin.py +323 -0
  80. zapy/_client/newsletter/content.py +499 -0
  81. zapy/_client/newsletter/discovery.py +233 -0
  82. zapy/_client/newsletter/messaging.py +396 -0
  83. zapy/_client/newsletter/mex.py +99 -0
  84. zapy/_client/newsletter/parse.py +406 -0
  85. zapy/_client/newsletter/types.py +393 -0
  86. zapy/_client/persistence/__init__.py +89 -0
  87. zapy/_client/persistence/companion_host.py +133 -0
  88. zapy/_client/persistence/ephemeral_setting.py +245 -0
  89. zapy/_client/persistence/group_history.py +361 -0
  90. zapy/_client/persistence/history_blob.py +169 -0
  91. zapy/_client/persistence/history_sync.py +646 -0
  92. zapy/_client/persistence/mailbox.py +232 -0
  93. zapy/_client/persistence/write_behind_persistence.py +368 -0
  94. zapy/_client/plugins/__init__.py +23 -0
  95. zapy/_client/plugins/define.py +44 -0
  96. zapy/_client/plugins/install.py +254 -0
  97. zapy/_client/plugins/types.py +64 -0
  98. zapy/_client/tokens/__init__.py +21 -0
  99. zapy/_client/tokens/cs_token.py +47 -0
  100. zapy/_client/tokens/tc_token.py +43 -0
  101. zapy/_client/types.py +988 -0
  102. zapy/_crypto/__init__.py +65 -0
  103. zapy/_crypto/core/__init__.py +62 -0
  104. zapy/_crypto/core/hkdf.py +41 -0
  105. zapy/_crypto/core/keys.py +51 -0
  106. zapy/_crypto/core/nonce.py +25 -0
  107. zapy/_crypto/core/primitives.py +179 -0
  108. zapy/_crypto/core/random.py +32 -0
  109. zapy/_crypto/core/xeddsa.py +72 -0
  110. zapy/_crypto/curves/__init__.py +3 -0
  111. zapy/_crypto/curves/constants.py +10 -0
  112. zapy/_crypto/curves/ed25519.py +61 -0
  113. zapy/_crypto/curves/types.py +18 -0
  114. zapy/_crypto/curves/x25519.py +117 -0
  115. zapy/_crypto/math/__init__.py +3 -0
  116. zapy/_crypto/math/constants.py +35 -0
  117. zapy/_crypto/math/edwards.py +174 -0
  118. zapy/_crypto/math/fe.py +120 -0
  119. zapy/_crypto/math/le.py +13 -0
  120. zapy/_crypto/math/mod.py +15 -0
  121. zapy/_crypto/math/prime.py +11 -0
  122. zapy/_crypto/math/types.py +25 -0
  123. zapy/_crypto/native_backend.py +40 -0
  124. zapy/_infra/__init__.py +3 -0
  125. zapy/_infra/log.py +142 -0
  126. zapy/_infra/perf.py +77 -0
  127. zapy/_media/__init__.py +64 -0
  128. zapy/_media/constants.py +133 -0
  129. zapy/_media/container.py +110 -0
  130. zapy/_media/crypto/__init__.py +19 -0
  131. zapy/_media/crypto/crypto.py +633 -0
  132. zapy/_media/crypto/media_retry.py +107 -0
  133. zapy/_media/processor.py +100 -0
  134. zapy/_media/sticker/__init__.py +8 -0
  135. zapy/_media/sticker/pack.py +188 -0
  136. zapy/_media/transfer/__init__.py +15 -0
  137. zapy/_media/transfer/client.py +635 -0
  138. zapy/_media/transfer/conn.py +48 -0
  139. zapy/_media/types.py +169 -0
  140. zapy/_message/__init__.py +121 -0
  141. zapy/_message/addons/__init__.py +5 -0
  142. zapy/_message/addons/link_preview/__init__.py +54 -0
  143. zapy/_message/addons/link_preview/builder.py +56 -0
  144. zapy/_message/addons/link_preview/detect.py +72 -0
  145. zapy/_message/addons/link_preview/fetcher.py +553 -0
  146. zapy/_message/addons/link_preview/types.py +100 -0
  147. zapy/_message/client.py +486 -0
  148. zapy/_message/context_info.py +424 -0
  149. zapy/_message/crypto/__init__.py +71 -0
  150. zapy/_message/crypto/addon_crypto.py +656 -0
  151. zapy/_message/crypto/icdc.py +138 -0
  152. zapy/_message/crypto/phash.py +87 -0
  153. zapy/_message/crypto/reporting_token.py +498 -0
  154. zapy/_message/crypto/use_case_secret.py +113 -0
  155. zapy/_message/encode/__init__.py +84 -0
  156. zapy/_message/encode/content.py +636 -0
  157. zapy/_message/encode/device_sent.py +88 -0
  158. zapy/_message/encode/media_payload.py +122 -0
  159. zapy/_message/encode/padding.py +27 -0
  160. zapy/_message/kinds/__init__.py +1 -0
  161. zapy/_message/kinds/bot.py +179 -0
  162. zapy/_message/kinds/group_history.py +206 -0
  163. zapy/_message/kinds/newsletter.py +388 -0
  164. zapy/_message/kinds/sticker_pack.py +74 -0
  165. zapy/_message/messaging/__init__.py +336 -0
  166. zapy/_message/messaging/companion_host.py +117 -0
  167. zapy/_message/messaging/fanout.py +311 -0
  168. zapy/_message/messaging/group_metadata.py +444 -0
  169. zapy/_message/messaging/ignore_key.py +264 -0
  170. zapy/_message/messaging/key_protocol.py +217 -0
  171. zapy/_message/messaging/link_preview.py +448 -0
  172. zapy/_message/messaging/messages.py +1429 -0
  173. zapy/_message/primitives/__init__.py +1 -0
  174. zapy/_message/primitives/ack.py +232 -0
  175. zapy/_message/primitives/incoming.py +1141 -0
  176. zapy/_message/primitives/media_retry.py +451 -0
  177. zapy/_message/primitives/peer_data_operation.py +271 -0
  178. zapy/_message/types.py +434 -0
  179. zapy/_proto/WAProto_pb2.py +2584 -0
  180. zapy/_proto/__init__.py +1 -0
  181. zapy/_protocol/__init__.py +3 -0
  182. zapy/_protocol/abprops_spec.py +6216 -0
  183. zapy/_protocol/auth.py +17 -0
  184. zapy/_protocol/browser.py +57 -0
  185. zapy/_protocol/defaults.py +38 -0
  186. zapy/_protocol/device_identity.py +54 -0
  187. zapy/_protocol/jid.py +241 -0
  188. zapy/_protocol/message.py +127 -0
  189. zapy/_protocol/nodes.py +144 -0
  190. zapy/_protocol/notification.py +18 -0
  191. zapy/_retry/__init__.py +77 -0
  192. zapy/_retry/codec.py +209 -0
  193. zapy/_retry/constants.py +28 -0
  194. zapy/_retry/parse.py +132 -0
  195. zapy/_retry/reason.py +65 -0
  196. zapy/_retry/replay.py +483 -0
  197. zapy/_retry/tracker.py +161 -0
  198. zapy/_retry/types.py +122 -0
  199. zapy/_signal/__init__.py +171 -0
  200. zapy/_signal/api/__init__.py +133 -0
  201. zapy/_signal/api/codec.py +164 -0
  202. zapy/_signal/api/constants.py +14 -0
  203. zapy/_signal/api/device_sync.py +915 -0
  204. zapy/_signal/api/digest_sync.py +363 -0
  205. zapy/_signal/api/identity_sync.py +250 -0
  206. zapy/_signal/api/missing_pre_keys_sync.py +267 -0
  207. zapy/_signal/api/prekeys.py +191 -0
  208. zapy/_signal/api/result_map.py +40 -0
  209. zapy/_signal/api/rotate_key.py +111 -0
  210. zapy/_signal/api/session_sync.py +297 -0
  211. zapy/_signal/attestation.py +145 -0
  212. zapy/_signal/constants.py +22 -0
  213. zapy/_signal/encoding.py +78 -0
  214. zapy/_signal/group/__init__.py +35 -0
  215. zapy/_signal/group/chain.py +139 -0
  216. zapy/_signal/group/codec.py +78 -0
  217. zapy/_signal/group/encoding.py +166 -0
  218. zapy/_signal/group/manager.py +361 -0
  219. zapy/_signal/registration/__init__.py +44 -0
  220. zapy/_signal/registration/keygen.py +46 -0
  221. zapy/_signal/registration/reg_encoding.py +66 -0
  222. zapy/_signal/registration/reg_utils.py +52 -0
  223. zapy/_signal/session/__init__.py +114 -0
  224. zapy/_signal/session/encoding.py +385 -0
  225. zapy/_signal/session/protocol.py +590 -0
  226. zapy/_signal/session/ratchet.py +381 -0
  227. zapy/_signal/session/resolver.py +630 -0
  228. zapy/_signal/session/serializer.py +96 -0
  229. zapy/_signal/session/session.py +272 -0
  230. zapy/_signal/types.py +214 -0
  231. zapy/_store/__init__.py +118 -0
  232. zapy/_store/cache/__init__.py +166 -0
  233. zapy/_store/cache/identity.py +130 -0
  234. zapy/_store/cache/privacy_token.py +116 -0
  235. zapy/_store/cache/sender_key.py +168 -0
  236. zapy/_store/cache/session.py +142 -0
  237. zapy/_store/contracts/__init__.py +84 -0
  238. zapy/_store/contracts/appstate.py +82 -0
  239. zapy/_store/contracts/auth.py +16 -0
  240. zapy/_store/contracts/chat_metadata.py +29 -0
  241. zapy/_store/contracts/contact.py +31 -0
  242. zapy/_store/contracts/device_list.py +35 -0
  243. zapy/_store/contracts/group_metadata.py +33 -0
  244. zapy/_store/contracts/identity.py +18 -0
  245. zapy/_store/contracts/message.py +36 -0
  246. zapy/_store/contracts/message_secret.py +41 -0
  247. zapy/_store/contracts/pre_key.py +32 -0
  248. zapy/_store/contracts/privacy_token.py +32 -0
  249. zapy/_store/contracts/retry.py +47 -0
  250. zapy/_store/contracts/sender_key.py +37 -0
  251. zapy/_store/contracts/session.py +21 -0
  252. zapy/_store/contracts/signal.py +23 -0
  253. zapy/_store/contracts/thread.py +35 -0
  254. zapy/_store/create_store.py +890 -0
  255. zapy/_store/locks.py +1154 -0
  256. zapy/_store/memory/__init__.py +40 -0
  257. zapy/_store/memory/appstate.py +200 -0
  258. zapy/_store/memory/auth.py +24 -0
  259. zapy/_store/memory/chat_metadata.py +124 -0
  260. zapy/_store/memory/contact.py +125 -0
  261. zapy/_store/memory/device_list.py +161 -0
  262. zapy/_store/memory/group_metadata.py +125 -0
  263. zapy/_store/memory/identity.py +86 -0
  264. zapy/_store/memory/message.py +85 -0
  265. zapy/_store/memory/message_secret.py +145 -0
  266. zapy/_store/memory/pre_key.py +112 -0
  267. zapy/_store/memory/privacy_token.py +103 -0
  268. zapy/_store/memory/retry.py +250 -0
  269. zapy/_store/memory/sender_key.py +162 -0
  270. zapy/_store/memory/session.py +111 -0
  271. zapy/_store/memory/signal.py +45 -0
  272. zapy/_store/memory/thread.py +66 -0
  273. zapy/_store/noop.py +279 -0
  274. zapy/_transport/__init__.py +3 -0
  275. zapy/_transport/binary/__init__.py +77 -0
  276. zapy/_transport/binary/constants.py +80 -0
  277. zapy/_transport/binary/decoder.py +260 -0
  278. zapy/_transport/binary/encoder.py +279 -0
  279. zapy/_transport/binary/tokens.py +1289 -0
  280. zapy/_transport/comms.py +759 -0
  281. zapy/_transport/keepalive/__init__.py +3 -0
  282. zapy/_transport/keepalive/keep_alive.py +252 -0
  283. zapy/_transport/node/__init__.py +3 -0
  284. zapy/_transport/node/builders/__init__.py +9 -0
  285. zapy/_transport/node/builders/abprops.py +33 -0
  286. zapy/_transport/node/builders/account_sync.py +120 -0
  287. zapy/_transport/node/builders/bot.py +56 -0
  288. zapy/_transport/node/builders/business.py +261 -0
  289. zapy/_transport/node/builders/chatstate.py +74 -0
  290. zapy/_transport/node/builders/community.py +116 -0
  291. zapy/_transport/node/builders/device.py +30 -0
  292. zapy/_transport/node/builders/email.py +181 -0
  293. zapy/_transport/node/builders/global_.py +74 -0
  294. zapy/_transport/node/builders/group.py +279 -0
  295. zapy/_transport/node/builders/media.py +21 -0
  296. zapy/_transport/node/builders/message.py +436 -0
  297. zapy/_transport/node/builders/mobile.py +227 -0
  298. zapy/_transport/node/builders/newsletter.py +293 -0
  299. zapy/_transport/node/builders/offline.py +20 -0
  300. zapy/_transport/node/builders/pairing.py +130 -0
  301. zapy/_transport/node/builders/passive.py +28 -0
  302. zapy/_transport/node/builders/prekeys.py +215 -0
  303. zapy/_transport/node/builders/presence.py +100 -0
  304. zapy/_transport/node/builders/privacy.py +243 -0
  305. zapy/_transport/node/builders/privacy_token.py +94 -0
  306. zapy/_transport/node/builders/profile.py +176 -0
  307. zapy/_transport/node/builders/retry.py +156 -0
  308. zapy/_transport/node/builders/shortcake.py +87 -0
  309. zapy/_transport/node/builders/tos.py +92 -0
  310. zapy/_transport/node/builders/usync.py +185 -0
  311. zapy/_transport/node/helpers.py +201 -0
  312. zapy/_transport/node/orchestrator.py +210 -0
  313. zapy/_transport/node/query.py +82 -0
  314. zapy/_transport/node/transport.py +105 -0
  315. zapy/_transport/node/usync.py +33 -0
  316. zapy/_transport/node/xml.py +38 -0
  317. zapy/_transport/noise/__init__.py +7 -0
  318. zapy/_transport/noise/cert.py +85 -0
  319. zapy/_transport/noise/client_payload.py +344 -0
  320. zapy/_transport/noise/constants.py +12 -0
  321. zapy/_transport/noise/frame_codec.py +120 -0
  322. zapy/_transport/noise/handshake.py +92 -0
  323. zapy/_transport/noise/session.py +493 -0
  324. zapy/_transport/noise/socket.py +45 -0
  325. zapy/_transport/noise/types.py +66 -0
  326. zapy/_transport/noise/version.json +3 -0
  327. zapy/_transport/proxy.py +35 -0
  328. zapy/_transport/types.py +19 -0
  329. zapy/_transport/wa_version_fetcher.py +159 -0
  330. zapy/_transport/web_socket.py +602 -0
  331. zapy/_util/__init__.py +3 -0
  332. zapy/_util/bytes.py +131 -0
  333. zapy/_util/coercion.py +29 -0
  334. zapy/_util/collections.py +45 -0
  335. zapy/_util/primitives.py +49 -0
  336. zapy/client.py +840 -0
  337. zapy/errors.py +148 -0
  338. zapy/events.py +564 -0
  339. zapy/jid.py +140 -0
  340. zapy/py.typed +0 -0
  341. zapy/stores/__init__.py +50 -0
  342. zapy/stores/base.py +1627 -0
  343. zapy/stores/json_file.py +359 -0
  344. zapy/stores/memory.py +322 -0
  345. zapy/stores/sqlite.py +343 -0
@@ -0,0 +1,758 @@
1
+ """Inbound stanza dispatcher (mirrors ``WaIncomingNodeCoordinator``).
2
+
3
+ Routes every incoming stanza: stream-control results, stanza filters,
4
+ success/info-bulletin handling, pending-node resolution, and the registered
5
+ tag/subtype handlers (receipt, message, notification, presence, chatstate,
6
+ call, failure, error).
7
+ """
8
+
9
+ from __future__ import annotations
10
+
11
+ import asyncio
12
+ import inspect
13
+ from collections.abc import Callable
14
+ from dataclasses import dataclass
15
+ from typing import TYPE_CHECKING, Protocol
16
+
17
+ # NOTE: ``zapy._client.types`` stays the first ``zapy`` import: importing it
18
+ # completes the ``zapy._auth.types`` import cycle before the stanza-event
19
+ # modules below execute (mirrors the import order in ``zapy._client.types``).
20
+ from zapy._client import types as _client_types
21
+ from zapy._auth.types import WaSuccessPersistAttributes
22
+ from zapy._client.events import incoming as _incoming
23
+ from zapy._client.events.business import WaBusinessEvent
24
+ from zapy._client.events.chatstate import parse_chatstate_node
25
+ from zapy._client.events.dirty import WaDirtyBit
26
+ from zapy._client.events.group import WaGroupEvent
27
+ from zapy._client.events.offline import (
28
+ WaOfflineThreadMetadataEvent,
29
+ parse_offline_thread_metadata,
30
+ )
31
+ from zapy._client.events.picture import WaPictureEvent
32
+ from zapy._client.events.presence import parse_presence_node
33
+ from zapy._infra.log import Logger
34
+
35
+ if TYPE_CHECKING:
36
+ from zapy._auth.types import WaAuthCredentials
37
+ from zapy._protocol.nodes import WA_NODE_TAGS
38
+ from zapy._transport.node.helpers import (
39
+ decode_node_content_base64_or_bytes,
40
+ find_node_child,
41
+ get_node_children,
42
+ get_node_children_by_tag,
43
+ )
44
+ from zapy._transport.types import BinaryNode
45
+ from zapy._util.primitives import parse_optional_int, to_error
46
+
47
+ # TEMPORARY (sibling transport layer not yet ported): verbatim ports of the
48
+ # stream-control parser from ``src/transport/stream/parse.ts``. They move to
49
+ # ``zapy._transport.stream.parse`` once that lands; the call sites already
50
+ # match the canonical shapes.
51
+ _STREAM_ERROR_TAG = "stream:error"
52
+ _XML_STREAM_END_TAG = "xmlstreamend"
53
+ _CONFLICT_TAG = "conflict"
54
+ _ACK_TAG = "ack"
55
+ _XML_NOT_WELL_FORMED_TAG = "xml-not-well-formed"
56
+ _REPLACED_TYPE = "replaced"
57
+
58
+ _WA_MESSAGE_TAGS: dict[str, str] = {"MESSAGE": "message", "RECEIPT": "receipt"}
59
+ _WA_RECEIPT_RETRY_TYPES: frozenset[str] = frozenset({"retry", "enc_rekey_retry"})
60
+
61
+
62
+ @dataclass(frozen=True, slots=True)
63
+ class WaStreamControlNodeResult:
64
+ """Parsed stream-control stanza: xmlstreamend or one stream-error variant."""
65
+
66
+ kind: str
67
+ code: int | None = None
68
+ id: str | None = None
69
+
70
+
71
+ def _parse_stream_control_node(node: BinaryNode) -> WaStreamControlNodeResult | None:
72
+ """Parse a stream-control stanza; ``None`` for ordinary stanzas."""
73
+ if node.tag == _XML_STREAM_END_TAG:
74
+ return WaStreamControlNodeResult(kind="xmlstreamend")
75
+ if node.tag != _STREAM_ERROR_TAG:
76
+ return None
77
+ conflict = find_node_child(node, _CONFLICT_TAG)
78
+ if conflict is not None:
79
+ if conflict.attrs.get("type") == _REPLACED_TYPE:
80
+ return WaStreamControlNodeResult(kind="stream_error_replaced")
81
+ return WaStreamControlNodeResult(kind="stream_error_device_removed")
82
+ code = parse_optional_int(node.attrs.get("code"))
83
+ if code is not None:
84
+ return WaStreamControlNodeResult(kind="stream_error_code", code=code)
85
+ ack_node = find_node_child(node, _ACK_TAG)
86
+ if ack_node is not None:
87
+ return WaStreamControlNodeResult(
88
+ kind="stream_error_ack", id=ack_node.attrs.get("id")
89
+ )
90
+ for child in get_node_children(node):
91
+ if child.tag == _XML_NOT_WELL_FORMED_TAG:
92
+ return WaStreamControlNodeResult(kind="stream_error_xml_not_well_formed")
93
+ return WaStreamControlNodeResult(kind="stream_error_other")
94
+
95
+
96
+ def _parse_companion_enc_static(
97
+ value: str | None, on_error: Callable[[Exception], None] | None = None
98
+ ) -> bytes | None:
99
+ """Base64-decode ``companion_enc_static``; ``None`` when absent/invalid."""
100
+ if not value:
101
+ return None
102
+ try:
103
+ return decode_node_content_base64_or_bytes(
104
+ value, "success.companion_enc_static"
105
+ )
106
+ except Exception as error:
107
+ if on_error is not None and isinstance(error, Exception):
108
+ on_error(error)
109
+ return None
110
+
111
+
112
+ def _parse_success_persist_attributes(
113
+ node: BinaryNode,
114
+ on_companion_parse_error: Callable[[Exception], None] | None = None,
115
+ ) -> WaSuccessPersistAttributes:
116
+ """Split a ``<success>`` stanza into the attributes the auth layer persists."""
117
+ return WaSuccessPersistAttributes(
118
+ me_lid=node.attrs.get("lid"),
119
+ me_display_name=node.attrs.get("display_name"),
120
+ companion_enc_static=_parse_companion_enc_static(
121
+ node.attrs.get("companion_enc_static"), on_companion_parse_error
122
+ ),
123
+ last_success_ts=parse_optional_int(node.attrs.get("t")),
124
+ props_version=parse_optional_int(node.attrs.get("props")),
125
+ ab_props_version=parse_optional_int(node.attrs.get("abprops")),
126
+ connection_location=node.attrs.get("location"),
127
+ account_creation_ts=parse_optional_int(node.attrs.get("creation")),
128
+ )
129
+
130
+
131
+ def _error_message(error: object) -> str:
132
+ """Readable message for a caught error (mirrors ``toError().message``)."""
133
+ return str(to_error(error))
134
+
135
+
136
+ _INFO_BULLETIN_CHILD_TAGS: frozenset[str] = frozenset(
137
+ {
138
+ "offline",
139
+ "offline_preview",
140
+ "priority_offline_complete",
141
+ "tos",
142
+ "thread_metadata",
143
+ "client_expiration",
144
+ }
145
+ )
146
+
147
+ _FILTER_PROTECTED_TAGS: frozenset[str] = frozenset({WA_NODE_TAGS["SUCCESS"], "failure"})
148
+
149
+
150
+ class WaIncomingNodeRuntime(Protocol):
151
+ """Structural runtime the dispatcher delegates every stanza effect to."""
152
+
153
+ async def handle_stream_control_result(
154
+ self, result: WaStreamControlNodeResult
155
+ ) -> None: ...
156
+ async def persist_success_attributes(
157
+ self, attributes: WaSuccessPersistAttributes
158
+ ) -> None: ...
159
+ def emit_success_node(self, node: BinaryNode) -> None: ...
160
+ def update_clock_skew_from_success(self, server_unix_seconds: int) -> None: ...
161
+ def should_warmup_media_conn(self) -> bool: ...
162
+ async def warmup_media_conn(self) -> None: ...
163
+ async def persist_routing_info(self, routing_info: bytes) -> None: ...
164
+ def try_resolve_pending_node(self, node: BinaryNode) -> bool: ...
165
+ async def handle_generic_incoming_node(self, node: BinaryNode) -> bool: ...
166
+ async def handle_incoming_iq_set_node(self, node: BinaryNode) -> bool: ...
167
+ async def handle_link_code_notification_node(self, node: BinaryNode) -> bool: ...
168
+ async def handle_companion_reg_refresh_notification_node(
169
+ self, node: BinaryNode
170
+ ) -> bool: ...
171
+ async def handle_incoming_message_node(self, node: BinaryNode) -> bool: ...
172
+ async def send_node(self, node: BinaryNode) -> None: ...
173
+ async def handle_incoming_retry_receipt(self, node: BinaryNode) -> None: ...
174
+ def handle_media_retry_notification(self, node: BinaryNode) -> None: ...
175
+ async def track_outbound_receipt(self, node: BinaryNode) -> None: ...
176
+ def emit_incoming_receipt(
177
+ self, event: _incoming.WaIncomingReceiptEvent
178
+ ) -> None: ...
179
+ def emit_incoming_presence(
180
+ self, event: _client_types.WaIncomingPresenceEvent
181
+ ) -> None: ...
182
+ def emit_incoming_chatstate(
183
+ self, event: _client_types.WaIncomingChatstateEvent
184
+ ) -> None: ...
185
+ def emit_incoming_call(self, event: _incoming.WaIncomingCallEvent) -> None: ...
186
+ def get_current_credentials(self) -> WaAuthCredentials | None: ...
187
+ def emit_incoming_failure(
188
+ self, event: _incoming.WaIncomingFailureEvent
189
+ ) -> None: ...
190
+ def emit_incoming_error_stanza(
191
+ self, event: _client_types.WaIncomingErrorStanzaEvent
192
+ ) -> None: ...
193
+ def emit_incoming_notification(
194
+ self, event: _incoming.WaIncomingNotificationEvent
195
+ ) -> None: ...
196
+ def emit_offline_thread_metadata(
197
+ self, event: WaOfflineThreadMetadataEvent
198
+ ) -> None: ...
199
+ def emit_mex_notification(
200
+ self, event: _incoming.WaMexNotificationEvent
201
+ ) -> None: ...
202
+ def emit_registration_code(
203
+ self, event: _incoming.WaRegistrationCodeEvent
204
+ ) -> None: ...
205
+ def emit_account_takeover_notice(
206
+ self, event: _incoming.WaAccountTakeoverNoticeEvent
207
+ ) -> None: ...
208
+ def emit_group_event(self, event: WaGroupEvent) -> None: ...
209
+ def emit_business_event(self, event: WaBusinessEvent) -> None: ...
210
+ def emit_picture_event(self, event: WaPictureEvent) -> None: ...
211
+ def emit_unhandled_incoming_node(
212
+ self, event: _incoming.WaIncomingUnhandledStanzaEvent
213
+ ) -> None: ...
214
+ async def sync_app_state(self) -> None: ...
215
+ def stop_comms(self) -> None: ...
216
+ async def disconnect(
217
+ self, reason: str, is_logout: bool, code: int | None
218
+ ) -> None: ...
219
+ async def clear_stored_credentials(self) -> None: ...
220
+ def parse_dirty_bits(self, nodes: list[BinaryNode]) -> list[WaDirtyBit]: ...
221
+ async def handle_dirty_bits(self, dirty_bits: list[WaDirtyBit]) -> None: ...
222
+
223
+
224
+ class WaOfflineResumeCoordinator(Protocol):
225
+ """Structural offline-resume surface used by the dispatcher."""
226
+
227
+ def track_offline_stanza(self) -> None: ...
228
+ def handle_offline_preview(self, stanza_count: int) -> None: ...
229
+ def handle_offline_complete(self, stanza_count: int) -> None: ...
230
+ def reset(self) -> None: ...
231
+ @property
232
+ def is_complete(self) -> bool: ...
233
+ @property
234
+ def is_resuming(self) -> bool: ...
235
+
236
+
237
+ @dataclass(frozen=True, slots=True)
238
+ class WaIncomingNodeCoordinatorOptions:
239
+ """Dependencies for :class:`WaIncomingNodeCoordinator`."""
240
+
241
+ logger: Logger
242
+ runtime: WaIncomingNodeRuntime
243
+ offline_resume: WaOfflineResumeCoordinator
244
+
245
+
246
+ @dataclass
247
+ class _HandlerEntry:
248
+ """One registered tag/subtype handler."""
249
+
250
+ subtype: str | None
251
+ handler: _client_types.WaIncomingNodeHandler
252
+
253
+
254
+ class WaIncomingNodeCoordinator:
255
+ """Dispatches every inbound stanza to stream control, filters, or handlers."""
256
+
257
+ def __init__(self, options: WaIncomingNodeCoordinatorOptions) -> None:
258
+ self._logger = options.logger
259
+ self._runtime = options.runtime
260
+ self._offline_resume = options.offline_resume
261
+ self._node_handlers: dict[str, list[_HandlerEntry]] = {}
262
+ self._stanza_filters: list[_client_types.WaIncomingStanzaFilter] = []
263
+ self._register_default_incoming_handlers()
264
+ self._media_conn_warmup_task: asyncio.Task[None] | None = None
265
+
266
+ async def handle_incoming_node(self, node: BinaryNode) -> None:
267
+ """Route one incoming stanza through the full dispatch pipeline."""
268
+ self._logger.trace(
269
+ "wa client incoming node",
270
+ {
271
+ "tag": node.tag,
272
+ "id": node.attrs.get("id"),
273
+ "type": node.attrs.get("type"),
274
+ },
275
+ )
276
+ if "offline" in node.attrs:
277
+ self._offline_resume.track_offline_stanza()
278
+ stream_control_result = _parse_stream_control_node(node)
279
+ if stream_control_result is not None:
280
+ await self._runtime.handle_stream_control_result(stream_control_result)
281
+ return
282
+ if await self._apply_stanza_filters(node):
283
+ return
284
+ if await self._handle_success_node(node):
285
+ return
286
+ if await self._handle_info_bulletin_node(node):
287
+ return
288
+ if await self._dispatch_incoming_node(node):
289
+ return
290
+ self._runtime.emit_unhandled_incoming_node(
291
+ _incoming.create_unhandled_incoming_node_event(node)
292
+ )
293
+
294
+ def register_incoming_stanza_filter(
295
+ self, filter: _client_types.WaIncomingStanzaFilter
296
+ ) -> Callable[[], None]:
297
+ """Add a stanza filter; returns an unregister closure."""
298
+ self._stanza_filters.append(filter)
299
+
300
+ def _unregister() -> None:
301
+ try:
302
+ self._stanza_filters.remove(filter)
303
+ except ValueError:
304
+ pass
305
+
306
+ return _unregister
307
+
308
+ async def _apply_stanza_filters(self, node: BinaryNode) -> bool:
309
+ if len(self._stanza_filters) == 0 or node.tag in _FILTER_PROTECTED_TAGS:
310
+ return False
311
+ filter_logger = self._logger.child(
312
+ {"tag": node.tag, "id": node.attrs.get("id")}
313
+ )
314
+ for stanza_filter in self._stanza_filters:
315
+ try:
316
+ verdict = stanza_filter(node)
317
+ if inspect.isawaitable(verdict):
318
+ verdict = await verdict
319
+ except Exception as error:
320
+ filter_logger.warn(
321
+ "incoming stanza filter threw", {"message": _error_message(error)}
322
+ )
323
+ continue
324
+ if not verdict:
325
+ continue
326
+ filter_logger.trace(
327
+ "incoming stanza dropped by filter",
328
+ {"type": node.attrs.get("type"), "from": node.attrs.get("from")},
329
+ )
330
+ ack = _incoming.build_inbound_ack(node)
331
+ if ack is not None:
332
+ await _incoming.send_safe_ack(
333
+ self._logger, self._runtime.send_node, ack
334
+ )
335
+ return True
336
+ return False
337
+
338
+ def register_incoming_handler(
339
+ self, registration: _client_types.WaIncomingNodeHandlerRegistration
340
+ ) -> Callable[[], None]:
341
+ """Register a tag/subtype handler; returns an unregister closure."""
342
+ entries = self._node_handlers.get(registration.tag)
343
+ entry = _HandlerEntry(
344
+ subtype=registration.subtype, handler=registration.handler
345
+ )
346
+ if entries is None:
347
+ self._node_handlers[registration.tag] = [entry]
348
+ elif registration.prepend:
349
+ entries.insert(0, entry)
350
+ else:
351
+ entries.append(entry)
352
+
353
+ def _unregister() -> None:
354
+ self.unregister_incoming_handler(registration)
355
+
356
+ return _unregister
357
+
358
+ def unregister_incoming_handler(
359
+ self, registration: _client_types.WaIncomingNodeHandlerRegistration
360
+ ) -> bool:
361
+ """Remove a registered handler; ``False`` when it was not registered."""
362
+ entries = self._node_handlers.get(registration.tag)
363
+ if not entries:
364
+ return False
365
+ for index, entry in enumerate(entries):
366
+ if (
367
+ entry.subtype == registration.subtype
368
+ and entry.handler == registration.handler
369
+ ):
370
+ del entries[index]
371
+ if len(entries) == 0:
372
+ del self._node_handlers[registration.tag]
373
+ return True
374
+ return False
375
+
376
+ async def _dispatch_incoming_node(self, node: BinaryNode) -> bool:
377
+ entries = self._node_handlers.get(node.tag)
378
+ node_subtype = node.attrs.get("type")
379
+ if node.tag == _WA_MESSAGE_TAGS["RECEIPT"]:
380
+ if entries:
381
+ for entry in entries:
382
+ if entry.subtype is not None and entry.subtype != node_subtype:
383
+ continue
384
+ if await entry.handler(node):
385
+ if not self._is_retry_receipt_type(node_subtype):
386
+ self._runtime.try_resolve_pending_node(node)
387
+ return True
388
+ return self._runtime.try_resolve_pending_node(node)
389
+ if self._runtime.try_resolve_pending_node(node):
390
+ return True
391
+ if await self._runtime.handle_generic_incoming_node(node):
392
+ return True
393
+ if not entries:
394
+ return False
395
+ for entry in entries:
396
+ if entry.subtype is not None and entry.subtype != node_subtype:
397
+ continue
398
+ if await entry.handler(node):
399
+ return True
400
+ return False
401
+
402
+ def _is_retry_receipt_type(self, type_: str | None) -> bool:
403
+ return type_ in _WA_RECEIPT_RETRY_TYPES
404
+
405
+ def _register_default_incoming_handlers(self) -> None:
406
+ runtime = self._runtime
407
+ logger = self._logger
408
+ self.register_incoming_handler(
409
+ _client_types.WaIncomingNodeHandlerRegistration(
410
+ tag=WA_NODE_TAGS["IQ"],
411
+ subtype="set",
412
+ handler=runtime.handle_incoming_iq_set_node,
413
+ )
414
+ )
415
+ self.register_incoming_handler(
416
+ _client_types.WaIncomingNodeHandlerRegistration(
417
+ tag=WA_NODE_TAGS["NOTIFICATION"],
418
+ handler=runtime.handle_link_code_notification_node,
419
+ )
420
+ )
421
+ self.register_incoming_handler(
422
+ _client_types.WaIncomingNodeHandlerRegistration(
423
+ tag=WA_NODE_TAGS["NOTIFICATION"],
424
+ subtype="companion_reg_refresh",
425
+ handler=runtime.handle_companion_reg_refresh_notification_node,
426
+ )
427
+ )
428
+ self.register_incoming_handler(
429
+ _client_types.WaIncomingNodeHandlerRegistration(
430
+ tag=WA_NODE_TAGS["NOTIFICATION"],
431
+ subtype="w:gp2",
432
+ handler=_incoming.create_incoming_group_notification_handler(
433
+ _incoming.IncomingGroupNotificationHandlerOptions(
434
+ logger=logger,
435
+ send_node=runtime.send_node,
436
+ emit_group_event=runtime.emit_group_event,
437
+ emit_unhandled_stanza=runtime.emit_unhandled_incoming_node,
438
+ )
439
+ ),
440
+ )
441
+ )
442
+ self.register_incoming_handler(
443
+ _client_types.WaIncomingNodeHandlerRegistration(
444
+ tag=WA_NODE_TAGS["NOTIFICATION"],
445
+ subtype="business",
446
+ handler=_incoming.create_incoming_business_notification_handler(
447
+ _incoming.IncomingBusinessNotificationHandlerOptions(
448
+ logger=logger,
449
+ send_node=runtime.send_node,
450
+ emit_business_event=runtime.emit_business_event,
451
+ emit_unhandled_stanza=runtime.emit_unhandled_incoming_node,
452
+ )
453
+ ),
454
+ )
455
+ )
456
+ self.register_incoming_handler(
457
+ _client_types.WaIncomingNodeHandlerRegistration(
458
+ tag=WA_NODE_TAGS["NOTIFICATION"],
459
+ subtype="picture",
460
+ handler=_incoming.create_incoming_picture_notification_handler(
461
+ _incoming.IncomingPictureNotificationHandlerOptions(
462
+ logger=logger,
463
+ send_node=runtime.send_node,
464
+ emit_picture_event=runtime.emit_picture_event,
465
+ emit_unhandled_stanza=runtime.emit_unhandled_incoming_node,
466
+ )
467
+ ),
468
+ )
469
+ )
470
+ self.register_incoming_handler(
471
+ _client_types.WaIncomingNodeHandlerRegistration(
472
+ tag=WA_NODE_TAGS["NOTIFICATION"],
473
+ subtype="registration",
474
+ handler=_incoming.create_incoming_registration_notification_handler(
475
+ _incoming.IncomingRegistrationNotificationHandlerOptions(
476
+ logger=logger,
477
+ send_node=runtime.send_node,
478
+ emit_registration_code=runtime.emit_registration_code,
479
+ emit_account_takeover_notice=(
480
+ runtime.emit_account_takeover_notice
481
+ ),
482
+ )
483
+ ),
484
+ )
485
+ )
486
+ self.register_incoming_handler(
487
+ _client_types.WaIncomingNodeHandlerRegistration(
488
+ tag=WA_NODE_TAGS["NOTIFICATION"],
489
+ handler=_incoming.create_incoming_notification_handler(
490
+ _incoming.IncomingNotificationHandlerOptions(
491
+ logger=logger,
492
+ send_node=runtime.send_node,
493
+ emit_incoming_notification=(runtime.emit_incoming_notification),
494
+ emit_mex_notification=runtime.emit_mex_notification,
495
+ emit_unhandled_stanza=runtime.emit_unhandled_incoming_node,
496
+ sync_app_state=runtime.sync_app_state,
497
+ handle_media_retry_notification=(
498
+ runtime.handle_media_retry_notification
499
+ ),
500
+ )
501
+ ),
502
+ )
503
+ )
504
+ self.register_incoming_handler(
505
+ _client_types.WaIncomingNodeHandlerRegistration(
506
+ tag=_WA_MESSAGE_TAGS["MESSAGE"],
507
+ handler=runtime.handle_incoming_message_node,
508
+ )
509
+ )
510
+ self.register_incoming_handler(
511
+ _client_types.WaIncomingNodeHandlerRegistration(
512
+ tag=_WA_MESSAGE_TAGS["RECEIPT"],
513
+ handler=_incoming.create_incoming_receipt_handler(
514
+ _incoming.IncomingReceiptHandlerOptions(
515
+ logger=logger,
516
+ send_node=runtime.send_node,
517
+ handle_incoming_retry_receipt=(
518
+ runtime.handle_incoming_retry_receipt
519
+ ),
520
+ track_outbound_receipt=runtime.track_outbound_receipt,
521
+ emit_incoming_receipt=runtime.emit_incoming_receipt,
522
+ )
523
+ ),
524
+ )
525
+ )
526
+
527
+ async def _presence_handler(node: BinaryNode) -> bool:
528
+ parsed = parse_presence_node(node)
529
+ runtime.emit_incoming_presence(
530
+ _client_types.WaIncomingPresenceEvent(
531
+ raw_node=node,
532
+ stanza_id=node.attrs.get("id"),
533
+ chat_jid=node.attrs.get("from"),
534
+ stanza_type=node.attrs.get("type"),
535
+ offline="offline" in node.attrs,
536
+ type=parsed.type,
537
+ last_seen=parsed.last_seen,
538
+ group_online_count=parsed.group_online_count,
539
+ )
540
+ )
541
+ return True
542
+
543
+ self.register_incoming_handler(
544
+ _client_types.WaIncomingNodeHandlerRegistration(
545
+ tag=WA_NODE_TAGS["PRESENCE"], handler=_presence_handler
546
+ )
547
+ )
548
+
549
+ async def _chatstate_handler(node: BinaryNode) -> bool:
550
+ parsed = parse_chatstate_node(node)
551
+ if parsed is None:
552
+ return False
553
+ runtime.emit_incoming_chatstate(
554
+ _client_types.WaIncomingChatstateEvent(
555
+ raw_node=node,
556
+ stanza_id=node.attrs.get("id"),
557
+ chat_jid=node.attrs.get("from"),
558
+ stanza_type=node.attrs.get("type"),
559
+ offline="offline" in node.attrs,
560
+ state=parsed.state,
561
+ media=parsed.media,
562
+ participant_jid=parsed.participant_jid,
563
+ )
564
+ )
565
+ return True
566
+
567
+ self.register_incoming_handler(
568
+ _client_types.WaIncomingNodeHandlerRegistration(
569
+ tag=WA_NODE_TAGS["CHATSTATE"], handler=_chatstate_handler
570
+ )
571
+ )
572
+ self.register_incoming_handler(
573
+ _client_types.WaIncomingNodeHandlerRegistration(
574
+ tag=WA_NODE_TAGS["CALL"],
575
+ handler=_incoming.create_incoming_call_handler(
576
+ _incoming.IncomingCallHandlerOptions(
577
+ logger=logger,
578
+ send_node=runtime.send_node,
579
+ emit_incoming_call=runtime.emit_incoming_call,
580
+ get_current_credentials=runtime.get_current_credentials,
581
+ )
582
+ ),
583
+ )
584
+ )
585
+ self.register_incoming_handler(
586
+ _client_types.WaIncomingNodeHandlerRegistration(
587
+ tag="failure",
588
+ handler=_incoming.create_incoming_failure_handler(
589
+ _incoming.IncomingFailureHandlerOptions(
590
+ logger=logger,
591
+ emit_incoming_failure=runtime.emit_incoming_failure,
592
+ stop_comms=runtime.stop_comms,
593
+ disconnect=runtime.disconnect,
594
+ clear_stored_credentials=runtime.clear_stored_credentials,
595
+ )
596
+ ),
597
+ )
598
+ )
599
+
600
+ async def _error_handler(node: BinaryNode) -> bool:
601
+ runtime.emit_incoming_error_stanza(
602
+ _client_types.WaIncomingErrorStanzaEvent(
603
+ raw_node=node,
604
+ stanza_id=node.attrs.get("id"),
605
+ chat_jid=node.attrs.get("from"),
606
+ stanza_type=node.attrs.get("type"),
607
+ offline="offline" in node.attrs,
608
+ code=parse_optional_int(node.attrs.get("code")),
609
+ text=node.attrs.get("text"),
610
+ )
611
+ )
612
+ return True
613
+
614
+ self.register_incoming_handler(
615
+ _client_types.WaIncomingNodeHandlerRegistration(
616
+ tag=WA_NODE_TAGS["ERROR"], handler=_error_handler
617
+ )
618
+ )
619
+
620
+ async def _handle_success_node(self, node: BinaryNode) -> bool:
621
+ if node.tag != WA_NODE_TAGS["SUCCESS"]:
622
+ return False
623
+
624
+ def _on_companion_error(error: Exception) -> None:
625
+ self._logger.warn(
626
+ "invalid companion_enc_static in success node",
627
+ {"message": _error_message(error)},
628
+ )
629
+
630
+ persist_attributes = _parse_success_persist_attributes(
631
+ node, _on_companion_error
632
+ )
633
+ self._logger.info(
634
+ "received success node",
635
+ {
636
+ "t": node.attrs.get("t"),
637
+ "props": node.attrs.get("props"),
638
+ "abprops": node.attrs.get("abprops"),
639
+ "location": node.attrs.get("location"),
640
+ "hasCompanionEncStatic": (
641
+ persist_attributes.companion_enc_static is not None
642
+ ),
643
+ "meLid": persist_attributes.me_lid,
644
+ "meDisplayName": persist_attributes.me_display_name,
645
+ },
646
+ )
647
+ self._runtime.emit_success_node(node)
648
+ if persist_attributes.last_success_ts is not None:
649
+ self._runtime.update_clock_skew_from_success(
650
+ persist_attributes.last_success_ts
651
+ )
652
+ await self._runtime.persist_success_attributes(persist_attributes)
653
+ self._schedule_media_conn_warmup()
654
+ return True
655
+
656
+ def _schedule_media_conn_warmup(self) -> None:
657
+ if (
658
+ self._media_conn_warmup_task is not None
659
+ and not self._media_conn_warmup_task.done()
660
+ ):
661
+ return
662
+
663
+ async def _warmup() -> None:
664
+ try:
665
+ if not self._runtime.should_warmup_media_conn():
666
+ return
667
+ await self._runtime.warmup_media_conn()
668
+ self._logger.debug("post-login media_conn warmup completed")
669
+ except Exception as error:
670
+ self._logger.warn(
671
+ "post-login media_conn warmup failed",
672
+ {"message": _error_message(error)},
673
+ )
674
+ finally:
675
+ self._media_conn_warmup_task = None
676
+
677
+ self._media_conn_warmup_task = asyncio.ensure_future(_warmup())
678
+
679
+ async def _handle_info_bulletin_node(self, node: BinaryNode) -> bool:
680
+ if node.tag != WA_NODE_TAGS["INFO_BULLETIN"]:
681
+ return False
682
+ handled = False
683
+ for child in get_node_children(node):
684
+ if child.tag in _INFO_BULLETIN_CHILD_TAGS:
685
+ self._runtime.emit_incoming_notification(
686
+ _incoming.create_info_bulletin_notification_event(
687
+ node,
688
+ child.tag,
689
+ {
690
+ "count": parse_optional_int(child.attrs.get("count")),
691
+ "message": parse_optional_int(child.attrs.get("message")),
692
+ "receipt": parse_optional_int(child.attrs.get("receipt")),
693
+ "notification": parse_optional_int(
694
+ child.attrs.get("notification")
695
+ ),
696
+ "t": parse_optional_int(child.attrs.get("t")),
697
+ },
698
+ )
699
+ )
700
+ if child.tag == "offline_preview":
701
+ self._offline_resume.handle_offline_preview(
702
+ parse_optional_int(child.attrs.get("count")) or 0
703
+ )
704
+ if child.tag == "offline":
705
+ self._offline_resume.handle_offline_complete(
706
+ parse_optional_int(child.attrs.get("count")) or 0
707
+ )
708
+ if child.tag == "thread_metadata":
709
+ self._runtime.emit_offline_thread_metadata(
710
+ parse_offline_thread_metadata(child)
711
+ )
712
+ handled = True
713
+ edge_routing = find_node_child(node, WA_NODE_TAGS["EDGE_ROUTING"])
714
+ if edge_routing is not None:
715
+ routing_info_node = find_node_child(
716
+ edge_routing, WA_NODE_TAGS["ROUTING_INFO"]
717
+ )
718
+ if routing_info_node is not None:
719
+ try:
720
+ routing_info = decode_node_content_base64_or_bytes(
721
+ routing_info_node.content,
722
+ f"ib.{WA_NODE_TAGS['EDGE_ROUTING']}."
723
+ f"{WA_NODE_TAGS['ROUTING_INFO']}",
724
+ )
725
+ await self._runtime.persist_routing_info(routing_info)
726
+ self._logger.debug(
727
+ "updated routing info from info bulletin",
728
+ {"byteLength": len(routing_info)},
729
+ )
730
+ except Exception as error:
731
+ self._logger.warn(
732
+ "failed to process routing info from info bulletin",
733
+ {"message": _error_message(error)},
734
+ )
735
+ handled = True
736
+ dirty_nodes = get_node_children_by_tag(node, WA_NODE_TAGS["DIRTY"])
737
+ dirty_bits = self._runtime.parse_dirty_bits(dirty_nodes)
738
+ if len(dirty_bits) > 0:
739
+ asyncio.ensure_future(self._sync_dirty_bits(dirty_bits))
740
+ handled = True
741
+ return handled
742
+
743
+ async def _sync_dirty_bits(self, dirty_bits: list[WaDirtyBit]) -> None:
744
+ try:
745
+ await self._runtime.handle_dirty_bits(dirty_bits)
746
+ except Exception as error:
747
+ self._logger.warn(
748
+ "dirty bits sync failed", {"message": _error_message(error)}
749
+ )
750
+
751
+
752
+ __all__ = [
753
+ "WaIncomingNodeCoordinator",
754
+ "WaIncomingNodeCoordinatorOptions",
755
+ "WaIncomingNodeRuntime",
756
+ "WaOfflineResumeCoordinator",
757
+ "WaStreamControlNodeResult",
758
+ ]