|
| 1 | +"""Runtime compatibility patches for the external LXMF package.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import importlib |
| 6 | +from functools import wraps |
| 7 | +from typing import Any |
| 8 | +from typing import Callable |
| 9 | + |
| 10 | +_OFFER_RESPONSE_PATCHED = "__rch_offer_response_patched__" |
| 11 | + |
| 12 | + |
| 13 | +def apply_lxmf_runtime_patches() -> None: |
| 14 | + """Patch external LXMF runtime behavior required by the hub.""" |
| 15 | + |
| 16 | + lxmpeer_module = importlib.import_module("LXMF.LXMPeer") |
| 17 | + _patch_offer_response_class(lxmpeer_module.LXMPeer) |
| 18 | + |
| 19 | + |
| 20 | +def _patch_offer_response_class(peer_class: type[Any]) -> None: |
| 21 | + """Wrap ``offer_response`` so integer error codes are handled safely.""" |
| 22 | + |
| 23 | + offer_response = getattr(peer_class, "offer_response", None) |
| 24 | + if not callable(offer_response): |
| 25 | + return |
| 26 | + |
| 27 | + if getattr(offer_response, _OFFER_RESPONSE_PATCHED, False): |
| 28 | + return |
| 29 | + |
| 30 | + patched_offer_response = _build_offer_response_wrapper(offer_response) |
| 31 | + setattr(patched_offer_response, _OFFER_RESPONSE_PATCHED, True) |
| 32 | + setattr(peer_class, "offer_response", patched_offer_response) |
| 33 | + |
| 34 | + |
| 35 | +def _build_offer_response_wrapper( |
| 36 | + original_offer_response: Callable[..., Any], |
| 37 | +) -> Callable[..., Any]: |
| 38 | + """Build a wrapper around the installed LXMF ``offer_response`` handler.""" |
| 39 | + |
| 40 | + @wraps(original_offer_response) |
| 41 | + def patched_offer_response(self: Any, request_receipt: Any) -> Any: |
| 42 | + response = getattr(request_receipt, "response", None) |
| 43 | + if type(response) is int and _handle_integer_offer_response(self, response): |
| 44 | + return None |
| 45 | + |
| 46 | + return original_offer_response(self, request_receipt) |
| 47 | + |
| 48 | + return patched_offer_response |
| 49 | + |
| 50 | + |
| 51 | +def _handle_integer_offer_response(peer: Any, response: int) -> bool: |
| 52 | + """Return True when an integer response has been handled locally.""" |
| 53 | + |
| 54 | + peer_class = peer.__class__ |
| 55 | + known_error_responses = ( |
| 56 | + ( |
| 57 | + getattr(peer_class, "ERROR_INVALID_KEY", None), |
| 58 | + "Remote indicated that the peering key was invalid, sync aborted", |
| 59 | + ), |
| 60 | + ( |
| 61 | + getattr(peer_class, "ERROR_INVALID_DATA", None), |
| 62 | + "Remote indicated that the sync offer data was invalid, sync aborted", |
| 63 | + ), |
| 64 | + ( |
| 65 | + getattr(peer_class, "ERROR_INVALID_STAMP", None), |
| 66 | + "Remote indicated that the sync offer stamp was invalid, sync aborted", |
| 67 | + ), |
| 68 | + ( |
| 69 | + getattr(peer_class, "ERROR_TIMEOUT", None), |
| 70 | + "Remote indicated that the sync offer timed out, sync aborted", |
| 71 | + ), |
| 72 | + ) |
| 73 | + for expected_response, message in known_error_responses: |
| 74 | + if expected_response is not None and response == expected_response: |
| 75 | + _abort_sync_offer(peer, message) |
| 76 | + return True |
| 77 | + |
| 78 | + pass_through_responses = { |
| 79 | + expected_response |
| 80 | + for expected_response in ( |
| 81 | + getattr(peer_class, "ERROR_NO_IDENTITY", None), |
| 82 | + getattr(peer_class, "ERROR_NO_ACCESS", None), |
| 83 | + getattr(peer_class, "ERROR_THROTTLED", None), |
| 84 | + ) |
| 85 | + if expected_response is not None |
| 86 | + } |
| 87 | + if response in pass_through_responses: |
| 88 | + return False |
| 89 | + |
| 90 | + _abort_sync_offer( |
| 91 | + peer, |
| 92 | + f"Remote returned unknown sync offer response {response}, sync aborted", |
| 93 | + ) |
| 94 | + return True |
| 95 | + |
| 96 | + |
| 97 | +def _abort_sync_offer(peer: Any, message: str) -> None: |
| 98 | + """Abort the current LXMF sync and tear down the active peer link.""" |
| 99 | + |
| 100 | + import RNS |
| 101 | + |
| 102 | + RNS.log(message, RNS.LOG_VERBOSE) |
| 103 | + |
| 104 | + link = getattr(peer, "link", None) |
| 105 | + if link is not None: |
| 106 | + link.teardown() |
| 107 | + |
| 108 | + peer.link = None |
| 109 | + peer.state = getattr(peer.__class__, "IDLE", getattr(peer, "state", None)) |
0 commit comments