adapter.py 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984
  1. """Signal messenger platform adapter for a signal-cli daemon in HTTP mode (``signal-cli daemon
  2. --http 127.0.0.1:8080``): inbound via SSE, outbound via JSON-RPC 2.0. Requires SIGNAL_HTTP_URL
  3. and SIGNAL_ACCOUNT."""
  4. import asyncio
  5. import base64
  6. import itertools
  7. import json
  8. import logging
  9. import os
  10. import random
  11. import shutil
  12. import subprocess
  13. import tempfile
  14. import time
  15. import uuid
  16. from collections import OrderedDict
  17. from contextlib import suppress
  18. from datetime import datetime, timezone, UTC
  19. from pathlib import Path
  20. from typing import Any, Dict, List, Optional, Tuple
  21. from urllib.parse import quote, unquote
  22. import httpx
  23. from gateway.config import Platform, PlatformConfig
  24. from agent.i18n import t
  25. from gateway.platforms.base import (
  26. BasePlatformAdapter, SendResult, cache_image_from_bytes_async,
  27. cache_audio_from_bytes_async, cache_document_from_bytes_async, cache_image_from_url, utf16_len,
  28. )
  29. from gateway.platforms.event import MessageEvent, MessageType, ProcessingOutcome
  30. from gateway.platforms.helpers import redact_phone
  31. from gateway.platforms.helpers import cancel_task
  32. from gateway.platforms.media_cache import mime_for_ext
  33. from tools.audio_container import CONTAINER_TO_EXT, sniff_container
  34. from plugins.platforms.signal.signal_format import markdown_to_signal
  35. from plugins.platforms.signal.signal_rate_limit import (
  36. SIGNAL_BATCH_PACING_NOTICE_THRESHOLD, SIGNAL_MAX_ATTACHMENTS_PER_MSG, SIGNAL_RATE_LIMIT_MAX_ATTEMPTS,
  37. SignalRateLimitError, _extract_retry_after_seconds, _format_wait, _is_signal_rate_limit_error,
  38. _signal_send_timeout, get_scheduler)
  39. from gateway.platforms._shared import get_scoped_secret as _sig_secret
  40. from utils import TRUTHY_STRINGS
  41. logger = logging.getLogger(__name__)
  42. SIGNAL_MAX_ATTACHMENT_SIZE = 100 * 1024 * 1024 # 100 MB
  43. MAX_MESSAGE_LENGTH = 8000 # Signal message size limit
  44. SSE_RETRY_DELAY_INITIAL = 2.0
  45. SSE_RETRY_DELAY_MAX = 60.0
  46. HEALTH_CHECK_INTERVAL = 30.0 # seconds between health checks
  47. HEALTH_CHECK_STALE_THRESHOLD = 120.0 # seconds without SSE activity before concern
  48. # Magic-byte prefixes checked before delegating to the shared audio/AV sniffer.
  49. _MAGIC_EXTENSIONS = ((b"\x89PNG", ".png"), (b"\xff\xd8", ".jpg"), (b"GIF8", ".gif"), (b"%PDF", ".pdf"))
  50. _MEDIA_TYPE_BY_MIME_PREFIX = (
  51. ("audio/", MessageType.VOICE), ("image/", MessageType.PHOTO), ("video/", MessageType.VIDEO))
  52. _OUTCOME_REACTION = {ProcessingOutcome.SUCCESS: "✅", ProcessingOutcome.FAILURE: "❌"}
  53. # send_multiple_images skip reasons → logger.warning args (url, detail).
  54. _SKIP_IMAGE_LOG = {
  55. "download": lambda url, detail: ("Signal: failed to download image %s: %s", url, detail),
  56. "missing": lambda url, detail: ("Signal: image file not found for %s", url),
  57. "oversize": lambda url, detail: ("Signal: image too large (%d bytes), skipping %s", detail, url)}
  58. _QUOTE_AUTHOR_KEYS = (
  59. "author", "authorNumber", "authorUuid", "authorAci", "authorServiceId", "authorServiceIdString")
  60. def _parse_comma_list(value: str) -> list[str]:
  61. """Split a comma-separated string into a list, stripping whitespace."""
  62. return [v.strip() for v in value.split(",") if v.strip()]
  63. def _guess_extension(data: bytes) -> str:
  64. """Guess file extension from magic bytes. WEBP is claimed before the shared audio/AV sniffer
  65. (it shares RIFF with WAVE); tools/audio_container.py owns MP3-vs-ADTS-AAC disambiguation."""
  66. for magic, ext in _MAGIC_EXTENSIONS:
  67. if data.startswith(magic):
  68. return ext
  69. if len(data) >= 12 and data[:4] == b"RIFF" and data[8:12] == b"WEBP":
  70. return ".webp"
  71. if (container := sniff_container(data)) is not None:
  72. return CONTAINER_TO_EXT[container]
  73. return ".zip" if data[:2] == b"PK" else ".bin"
  74. def _is_image_ext(ext: str) -> bool:
  75. return ext.lower() in {".jpg", ".jpeg", ".png", ".gif", ".webp"}
  76. def _is_audio_ext(ext: str) -> bool:
  77. return ext.lower() in {".mp3", ".wav", ".ogg", ".m4a", ".aac"}
  78. def _ext_to_mime(ext: str) -> str:
  79. """Map file extension to MIME type (shared table matches Signal's historical map)."""
  80. return mime_for_ext(ext, fallback="application/octet-stream")
  81. def _remux_aac_to_m4a(aac_data: bytes) -> Optional[tuple[bytes, str]]:
  82. """Losslessly remux raw ADTS AAC (Android voice notes, rejected by most STT APIs) to .m4a.
  83. Returns ``(m4a_bytes, ".m4a")``, or ``None`` when ffmpeg is missing/fails (caller keeps the input)."""
  84. # Fall back to common Homebrew/local prefixes on macOS dev hosts.
  85. ffmpeg = shutil.which("ffmpeg") or next(
  86. (p for p in ("/opt/homebrew/bin/ffmpeg", "/usr/local/bin/ffmpeg")
  87. if os.path.isfile(p) and os.access(p, os.X_OK)), None)
  88. if not ffmpeg:
  89. logger.debug("Signal: ffmpeg not found, skipping AAC→M4A remux")
  90. return None
  91. try:
  92. with tempfile.NamedTemporaryFile(suffix=".aac", delete=False) as src:
  93. src.write(aac_data)
  94. src_path = src.name
  95. dst_path = src_path[:-4] + ".m4a"
  96. try:
  97. proc = subprocess.run([ffmpeg, "-y", "-loglevel", "error", "-i", src_path, "-c:a", "copy", "-movflags",
  98. "+faststart", dst_path], stdin=subprocess.DEVNULL, capture_output=True, timeout=10, check=False)
  99. if proc.returncode != 0:
  100. logger.warning("Signal: AAC→M4A remux failed (ffmpeg exit %d): %s",
  101. proc.returncode, proc.stderr.decode("utf-8", "replace")[:300])
  102. return None
  103. with open(dst_path, "rb") as f:
  104. return f.read(), ".m4a"
  105. finally:
  106. for p in (src_path, dst_path):
  107. with suppress(OSError):
  108. os.unlink(p)
  109. except subprocess.TimeoutExpired:
  110. logger.warning("Signal: AAC→M4A remux timed out (>10s)")
  111. return None
  112. except Exception:
  113. logger.exception("Signal: AAC→M4A remux error")
  114. return None
  115. def _render_mentions(text: str, mentions: list) -> str:
  116. """Replace \\uFFFC mention placeholders with readable @identifiers (from the end so indices hold)."""
  117. if not mentions or "\uFFFC" not in text:
  118. return text
  119. for mention in sorted(mentions, key=lambda m: m.get("start", 0), reverse=True):
  120. start, length = mention.get("start", 0), mention.get("length", 1)
  121. identifier = mention.get("number") or mention.get("uuid") or "user"
  122. text = text[:start] + f"@{identifier}" + text[start + length:]
  123. return text
  124. def _is_signal_service_id(value: str) -> bool:
  125. """Return True if *value* already looks like a Signal service identifier (PNI:/u: prefix or UUID)."""
  126. if not value:
  127. return False
  128. if value.startswith(("PNI:", "u:")):
  129. return True
  130. with suppress(ValueError, AttributeError, TypeError):
  131. uuid.UUID(value)
  132. return True
  133. return False
  134. def _looks_like_e164_number(value: str) -> bool:
  135. """Return True for a plausible E.164 phone number."""
  136. return bool(value) and value.startswith("+") and value[1:].isdigit() and 7 <= len(value) - 1 <= 15
  137. def check_signal_requirements() -> bool:
  138. """Check if Signal runtime dependencies are available."""
  139. return True
  140. def validate_signal_config(config: PlatformConfig) -> bool:
  141. """Check if Signal has enough config to connect."""
  142. extra = getattr(config, "extra", {}) or {}
  143. http_url = (extra.get("http_url", "") or _sig_secret("SIGNAL_HTTP_URL", "")).strip()
  144. account = (extra.get("account", "") or _sig_secret("SIGNAL_ACCOUNT", "")).strip()
  145. return bool(http_url and account)
  146. class SignalAdapter(BasePlatformAdapter):
  147. """Signal messenger adapter using signal-cli HTTP daemon."""
  148. platform = Platform.SIGNAL
  149. MAX_MESSAGE_LENGTH = MAX_MESSAGE_LENGTH
  150. splits_long_messages = True # send() chunks after markdown → Signal formatting conversion
  151. serial_receive_loop = True # _sse_listener awaits each envelope before reading the next
  152. # No real edit API; declaring it lets streaming suppress the cursor instead of a stale tofu square.
  153. SUPPORTS_MESSAGE_EDITING = False
  154. def __init__(self, config: PlatformConfig):
  155. super().__init__(config, Platform.SIGNAL)
  156. extra = config.extra or {}
  157. self.http_url = extra.get("http_url", "http://127.0.0.1:8080").rstrip("/")
  158. self.account = extra.get("account", "")
  159. self.ignore_stories = extra.get("ignore_stories", True)
  160. # Allowlists are per-profile (scoped reads); group policy derives from the group allowlist's
  161. # presence. The DM allowlist mirrors run.py's SIGNAL_ALLOWED_USERS so reaction hooks (which
  162. # fire before run.py's auth gate) can skip unauthorized senders; "*" = open.
  163. self.group_allow_from = set(_parse_comma_list(_sig_secret("SIGNAL_GROUP_ALLOWED_USERS", "")))
  164. _rm_cfg = extra.get("require_mention")
  165. self.require_mention = (bool(_rm_cfg) if _rm_cfg is not None
  166. else (_sig_secret("SIGNAL_REQUIRE_MENTION", "false") or "false").lower() in TRUTHY_STRINGS)
  167. self.dm_allow_from = set(_parse_comma_list(_sig_secret("SIGNAL_ALLOWED_USERS", "*")))
  168. self.client: Optional[httpx.AsyncClient] = None
  169. self._sse_task: Optional[asyncio.Task] = None
  170. self._health_monitor_task: Optional[asyncio.Task] = None
  171. self._typing_tasks: dict[str, asyncio.Task] = {}
  172. # Per-chat typing backoff: on NETWORK_FAILURE base.py's _keep_typing would hammer sendTyping every ~2s.
  173. self._typing_failures: dict[str, int] = {}
  174. self._typing_skip_until: dict[str, float] = {}
  175. self._running = False
  176. self._last_sse_activity = 0.0
  177. self._sse_response: Optional[httpx.Response] = None
  178. self._account_normalized = self.account.strip()
  179. # Recently sent timestamps filter echo-backs (Note to Self / linked-device sync-sents); LRU + TTL
  180. # so a pending echo in a chatty group isn't evicted by many outbounds.
  181. self._recent_sent_timestamps: OrderedDict[int, float] = OrderedDict()
  182. self._max_recent_timestamps = 512
  183. self._recent_sent_ttl_seconds = 300.0
  184. # Separate FIFO of outbound timestamps: Signal quote.id is the quoted message's timestamp, so
  185. # replies to this bot are recognised after the echo was consumed.
  186. self._sent_message_timestamps: OrderedDict[str, None] = OrderedDict()
  187. self._max_sent_message_timestamps = 500
  188. # Best-effort number↔ACI/PNI UUID mapping so sends can upgrade a number to the UUID signal-cli prefers.
  189. self._recipient_uuid_by_number: dict[str, str] = {}
  190. self._recipient_number_by_uuid: dict[str, str] = {}
  191. self._recipient_cache_lock = asyncio.Lock()
  192. logger.info("Signal adapter initialized: url=%s account=%s groups=%s", self.http_url,
  193. redact_phone(self.account), "enabled" if self.group_allow_from else "disabled")
  194. async def connect(self, *, is_reconnect: bool = False) -> bool:
  195. """Connect to signal-cli daemon and start SSE listener."""
  196. if not self.http_url or not self.account:
  197. logger.error("Signal: SIGNAL_HTTP_URL and SIGNAL_ACCOUNT are required")
  198. return False
  199. lock_acquired = False # scoped lock prevents duplicate Signal listeners for the same phone
  200. try:
  201. if not self._acquire_platform_lock('signal-phone', self.account, 'Signal account'):
  202. return False
  203. lock_acquired = True
  204. except Exception as e:
  205. logger.warning("Signal: Could not acquire phone lock (non-fatal): %s", e)
  206. # Tighter keepalive so idle CLOSE_WAIT drains promptly.
  207. # See #18451.
  208. from gateway.platforms._http_client_limits import platform_httpx_limits
  209. self.client = httpx.AsyncClient(timeout=30.0, limits=platform_httpx_limits())
  210. try:
  211. try:
  212. resp = await self.client.get(f"{self.http_url}/api/v1/check", timeout=10.0)
  213. except Exception as e:
  214. logger.error("Signal: cannot reach signal-cli at %s: %s", self.http_url, e)
  215. return False
  216. if resp.status_code != 200:
  217. logger.error("Signal: health check failed (status %d)", resp.status_code)
  218. return False
  219. self._running = True
  220. self._last_sse_activity = time.time()
  221. self._sse_task = asyncio.create_task(self._sse_listener())
  222. self._health_monitor_task = asyncio.create_task(self._health_monitor())
  223. logger.info("Signal: connected to %s", self.http_url)
  224. # Plugin-registered native handlers (ctx.register_platform_handler).
  225. self._wire_plugin_handlers(None)
  226. return True
  227. finally:
  228. if not self._running:
  229. await self._close_client()
  230. if lock_acquired:
  231. self._release_platform_lock()
  232. async def _close_client(self) -> None:
  233. if self.client:
  234. await self.client.aclose()
  235. self.client = None
  236. async def disconnect(self) -> None:
  237. """Stop SSE listener and clean up."""
  238. self._running = False
  239. for task in (self._sse_task, self._health_monitor_task):
  240. await cancel_task(task)
  241. for task in self._typing_tasks.values():
  242. task.cancel()
  243. self._typing_tasks.clear()
  244. await self._close_client()
  245. self._release_platform_lock()
  246. logger.info("Signal: disconnected")
  247. async def _handle_sse_line(self, line: str) -> None:
  248. """Dispatch one SSE line; keepalive comments (":") count as activity so the health monitor stays quiet."""
  249. if line.startswith(":"):
  250. self._last_sse_activity = time.time()
  251. elif line.startswith("data:") and (data_str := line[5:].strip()):
  252. self._last_sse_activity = time.time()
  253. try:
  254. await self._handle_envelope(json.loads(data_str))
  255. except json.JSONDecodeError:
  256. logger.debug("Signal SSE: invalid JSON: %s", data_str[:100])
  257. except Exception:
  258. logger.exception("Signal SSE: error handling event")
  259. async def _sse_listener(self) -> None:
  260. """Listen for SSE events from signal-cli daemon, reconnecting with jittered backoff."""
  261. url = f"{self.http_url}/api/v1/events?account={quote(self.account, safe='')}"
  262. backoff = SSE_RETRY_DELAY_INITIAL
  263. while self._running:
  264. try:
  265. logger.debug("Signal SSE: connecting to %s", url)
  266. async with self.client.stream("GET", url, headers={"Accept": "text/event-stream"},
  267. timeout=None) as response:
  268. self._sse_response = response
  269. backoff = SSE_RETRY_DELAY_INITIAL # Reset on successful connection
  270. self._last_sse_activity = time.time()
  271. logger.info("Signal SSE: connected")
  272. buffer = ""
  273. async for chunk in response.aiter_text():
  274. if not self._running:
  275. break
  276. buffer += chunk
  277. while "\n" in buffer:
  278. line, buffer = buffer.split("\n", 1)
  279. if line := line.strip():
  280. await self._handle_sse_line(line)
  281. except asyncio.CancelledError:
  282. break
  283. except httpx.HTTPError as e:
  284. if self._running:
  285. logger.warning("Signal SSE: HTTP error: %s (reconnecting in %.0fs)", e, backoff)
  286. except Exception as e:
  287. if self._running:
  288. logger.warning("Signal SSE: error: %s (reconnecting in %.0fs)", e, backoff)
  289. if self._running:
  290. await asyncio.sleep(backoff + backoff * 0.2 * random.random()) # 20% jitter vs thundering herd
  291. backoff = min(backoff * 2, SSE_RETRY_DELAY_MAX)
  292. self._sse_response = None
  293. async def _health_monitor(self) -> None:
  294. """Monitor SSE connection health and force reconnect if stale."""
  295. while self._running:
  296. await asyncio.sleep(HEALTH_CHECK_INTERVAL)
  297. if not self._running:
  298. break
  299. if (elapsed := time.time() - self._last_sse_activity) <= HEALTH_CHECK_STALE_THRESHOLD:
  300. continue
  301. logger.warning("Signal: SSE idle for %.0fs, checking daemon health", elapsed)
  302. try:
  303. resp = await self.client.get(f"{self.http_url}/api/v1/check", timeout=10.0)
  304. except Exception as e:
  305. logger.warning("Signal: health check error: %s, forcing reconnect", e)
  306. self._force_reconnect()
  307. continue
  308. if resp.status_code == 200: # daemon alive but SSE quiet — reset activity to avoid repeated warnings
  309. self._last_sse_activity = time.time()
  310. logger.debug("Signal: daemon healthy, SSE idle")
  311. else:
  312. logger.warning("Signal: health check failed (%d), forcing reconnect", resp.status_code)
  313. self._force_reconnect()
  314. def _force_reconnect(self) -> None:
  315. """Force SSE reconnection by closing the current response."""
  316. if self._sse_response and not self._sse_response.is_stream_consumed:
  317. with suppress(Exception):
  318. task = asyncio.create_task(self._sse_response.aclose())
  319. self._background_tasks.add(task)
  320. task.add_done_callback(self._background_tasks.discard)
  321. self._sse_response = None
  322. def _unwrap_sync_message(self, envelope_data: dict) -> Optional[dict]:
  323. """Promote a "Note to Self" / group sync-sent to a dataMessage envelope; None for other
  324. sync events (read receipts, typing, our own outbound echoes)."""
  325. sync_msg = envelope_data.get("syncMessage")
  326. sent_msg = sync_msg.get("sentMessage") if isinstance(sync_msg, dict) else None
  327. if not sent_msg or not isinstance(sent_msg, dict):
  328. return None
  329. dest = sent_msg.get("destinationNumber") or sent_msg.get("destination")
  330. if dest != self._account_normalized and not (sent_msg.get("groupInfo") or {}).get("groupId"):
  331. return None
  332. if self._consume_sent_timestamp(sent_msg.get("timestamp")):
  333. return None # echo of our own outbound reply
  334. return {**envelope_data, "dataMessage": sent_msg}
  335. def _apply_group_mention_rules(self, text: str, data_message: dict) -> tuple[bool, str]:
  336. """Gate on require_mention (False = drop) and strip the bot's own @mention from every group
  337. message, so the agent doesn't read "@+155****4567 say hello" as a directive to contact that number."""
  338. account_norm = self._account_normalized
  339. if self.require_mention:
  340. mentioned_in_text = account_norm and (f"@{account_norm}" in (text or ""))
  341. mentioned_in_metadata = any(account_norm in (m.get("number"), m.get("uuid"))
  342. for m in (data_message.get("mentions") or []))
  343. if not mentioned_in_text and not mentioned_in_metadata:
  344. logger.debug("Signal: ignoring group message (require_mention=true, bot not mentioned)")
  345. return False, text
  346. if text and account_norm:
  347. text = text.replace(f"@{account_norm}", "")
  348. if bot_uuid := self._recipient_uuid_by_number.get(account_norm):
  349. text = text.replace(f"@{bot_uuid}", "")
  350. text = text.replace(" ", " ").strip() # collapse only the doubled space; newlines preserved
  351. return True, text
  352. async def _collect_attachments(self, attachments_data: list) -> tuple[list[str], list[str]]:
  353. """Fetch + cache inbound attachments; returns (media_urls, media_types)."""
  354. media_urls: list[str] = []
  355. media_types: list[str] = []
  356. for att in attachments_data:
  357. att_id, att_size = att.get("id"), att.get("size", 0)
  358. if not att_id:
  359. continue
  360. if att_size > SIGNAL_MAX_ATTACHMENT_SIZE:
  361. logger.warning("Signal: attachment too large (%d bytes), skipping", att_size)
  362. continue
  363. try:
  364. cached_path, ext = await self._fetch_attachment(att_id)
  365. if cached_path:
  366. media_urls.append(cached_path)
  367. media_types.append(att.get("contentType") or _ext_to_mime(ext))
  368. except Exception:
  369. logger.exception("Signal: failed to fetch attachment %s", att_id)
  370. return media_urls, media_types
  371. async def _handle_envelope(self, envelope: dict) -> None:
  372. """Process an incoming signal-cli envelope."""
  373. envelope_data = envelope.get("envelope", envelope)
  374. is_note_to_self = "syncMessage" in envelope_data
  375. if is_note_to_self:
  376. envelope_data = self._unwrap_sync_message(envelope_data)
  377. if envelope_data is None:
  378. return
  379. sender = (envelope_data.get("sourceNumber") or envelope_data.get("sourceUuid")
  380. or envelope_data.get("source"))
  381. sender_name = envelope_data.get("sourceName", "")
  382. sender_uuid = envelope_data.get("sourceUuid", "")
  383. self._remember_recipient_identifiers(sender, sender_uuid)
  384. if not sender:
  385. logger.debug("Signal: ignoring envelope with no sender")
  386. return
  387. # Self-message filtering prevents reply loops (Note to Self is allowed)
  388. if self._account_normalized and sender == self._account_normalized and not is_note_to_self:
  389. return
  390. if self.ignore_stories and envelope_data.get("storyMessage"):
  391. return
  392. # Edited messages carry their updated dataMessage inside editMessage
  393. data_message = (envelope_data.get("dataMessage")
  394. or (envelope_data.get("editMessage") or {}).get("dataMessage"))
  395. if not data_message:
  396. return
  397. group_info = data_message.get("groupInfo")
  398. group_id = group_info.get("groupId") if group_info else None
  399. is_group = bool(group_id)
  400. if is_group and not self._group_allowed(group_id):
  401. return
  402. chat_id = f"group:{group_id}" if is_group else sender
  403. text = data_message.get("message", "")
  404. if text and (mentions := data_message.get("mentions", [])):
  405. text = _render_mentions(text, mentions)
  406. if is_group:
  407. mentioned, text = self._apply_group_mention_rules(text, data_message)
  408. if not mentioned:
  409. return
  410. # quote.id is the quoted message's timestamp, quote.author the quoted sender —
  411. # both are preserved so the gateway can tell the agent which message was replied to.
  412. quote_data = data_message.get("quote") or {}
  413. reply_to_id = str(quote_data.get("id")) if quote_data.get("id") else None
  414. reply_to_author = self._extract_quote_author(quote_data)
  415. attachments_data = data_message.get("attachments", [])
  416. media_urls, media_types = [], []
  417. if attachments_data and not getattr(self, "ignore_attachments", False):
  418. media_urls, media_types = await self._collect_attachments(attachments_data)
  419. # Skip contentless envelopes (profile key updates, empty messages) that still carry a
  420. # dataMessage wrapper — otherwise msg='' triggers a full agent turn.
  421. if (not text or not text.strip()) and not media_urls:
  422. logger.debug("Signal: skipping contentless envelope from %s (%d attachments)", redact_phone(sender),
  423. len(media_urls) if media_urls else 0)
  424. return
  425. source = self.build_source(
  426. chat_id=chat_id, chat_name=group_info.get("groupName") if group_info else sender_name,
  427. chat_type="group" if is_group else "dm", user_id=sender,
  428. user_name=sender_name or sender, user_id_alt=sender_uuid if sender_uuid else None,
  429. chat_id_alt=group_id if is_group else None)
  430. # First matching MIME prefix wins; everything else (application/*, text/*, unknown) is a DOCUMENT
  431. # so run.py's document-context injection surfaces the cached path.
  432. msg_type = MessageType.TEXT if not media_types else next(
  433. (mt for prefix, mt in _MEDIA_TYPE_BY_MIME_PREFIX if any(m.startswith(prefix) for m in media_types)),
  434. MessageType.DOCUMENT)
  435. ts_ms = envelope_data.get("timestamp", 0) # milliseconds since epoch
  436. timestamp = datetime.now(tz=UTC)
  437. if ts_ms:
  438. with suppress(ValueError, OSError):
  439. timestamp = datetime.fromtimestamp(ts_ms / 1000, tz=UTC)
  440. # raw_message keeps sender + timestamp_ms so processing hooks can build sendReaction targets.
  441. event = MessageEvent(
  442. source=source, text=text or "", message_type=msg_type, media_urls=media_urls,
  443. media_types=media_types, timestamp=timestamp,
  444. raw_message={"sender": sender, "timestamp_ms": ts_ms, "quote": quote_data if quote_data else None},
  445. reply_to_message_id=reply_to_id, reply_to_text=quote_data.get("text"),
  446. reply_to_author_id=reply_to_author,
  447. reply_to_author_name=quote_data.get("authorName") or quote_data.get("authorProfileName"),
  448. reply_to_is_own_message=self._quote_references_own_message(reply_to_id, reply_to_author),
  449. )
  450. logger.debug("Signal: message from %s in %s: %s", redact_phone(sender), chat_id[:20], (text or "")[:50])
  451. await self.handle_message(event)
  452. def _group_allowed(self, group_id: str) -> bool:
  453. """Group policy from SIGNAL_GROUP_ALLOWED_USERS: unset → groups disabled; IDs → only those
  454. groups; "*" → all. DM auth is run.py's (_is_user_authorized)."""
  455. if not self.group_allow_from:
  456. logger.debug("Signal: ignoring group message (no SIGNAL_GROUP_ALLOWED_USERS)")
  457. return False
  458. if "*" not in self.group_allow_from and group_id not in self.group_allow_from:
  459. logger.debug("Signal: group %s not in allowlist", group_id[:8] if group_id else "?")
  460. return False
  461. return True
  462. def _remember_recipient_identifiers(self, number: Optional[str], service_id: Optional[str]) -> None:
  463. """Cache any number↔UUID mapping observed from Signal envelopes."""
  464. if number and service_id and _is_signal_service_id(service_id):
  465. self._recipient_uuid_by_number[number] = service_id
  466. self._recipient_number_by_uuid[service_id] = number
  467. @staticmethod
  468. def _extract_quote_author(quote_data: Any) -> Optional[str]:
  469. """Return the best available Signal sender identifier from quote metadata."""
  470. keys = _QUOTE_AUTHOR_KEYS if isinstance(quote_data, dict) else ()
  471. return next((str(quote_data[k]) for k in keys if quote_data.get(k)), None)
  472. def _quote_references_own_message(self, reply_to_id: Optional[str], reply_to_author: Optional[str]) -> bool:
  473. """True when a Signal quote points at this adapter's outbound message."""
  474. if reply_to_id and str(reply_to_id) in self._sent_message_timestamps:
  475. return True
  476. if not reply_to_author:
  477. return False
  478. author, acct = str(reply_to_author).strip(), self._account_normalized
  479. # Cached number↔UUID mappings are only ever stored with truthy keys and values.
  480. return bool(acct) and (author == acct or author == self._recipient_uuid_by_number.get(acct)
  481. or self._recipient_number_by_uuid.get(author) == acct)
  482. def _remember_sent_message_timestamp(self, timestamp: Any) -> None:
  483. """Keep a bounded cache of outbound Signal timestamps for quote matching."""
  484. if timestamp is None:
  485. return
  486. key = str(timestamp)
  487. self._sent_message_timestamps.pop(key, None) # re-insert as most-recently-used so eviction drops old ones
  488. self._sent_message_timestamps[key] = None
  489. while len(self._sent_message_timestamps) > self._max_sent_message_timestamps:
  490. self._sent_message_timestamps.popitem(last=False)
  491. def _extract_contact_uuid(self, contact: Any, phone_number: str) -> Optional[str]:
  492. """Best-effort extraction of a Signal service ID from listContacts output."""
  493. if not isinstance(contact, dict):
  494. return None
  495. service_id = contact.get("uuid") or contact.get("serviceId")
  496. profile = contact.get("profile")
  497. if not service_id and isinstance(profile, dict):
  498. service_id = profile.get("serviceId") or profile.get("uuid")
  499. if service_id and _is_signal_service_id(service_id) and phone_number in (
  500. contact.get("number"), contact.get("recipient")):
  501. return service_id
  502. return None
  503. async def _resolve_recipient(self, chat_id: str) -> str:
  504. """Return the preferred Signal recipient identifier for a direct chat."""
  505. if not chat_id or chat_id.startswith("group:") or not _looks_like_e164_number(chat_id):
  506. return chat_id
  507. if cached := self._recipient_uuid_by_number.get(chat_id):
  508. return cached
  509. async with self._recipient_cache_lock:
  510. if cached := self._recipient_uuid_by_number.get(chat_id):
  511. return cached
  512. contacts = await self._rpc("listContacts", {"account": self.account, "allRecipients": True})
  513. for contact in contacts if isinstance(contacts, list) else ():
  514. number = contact.get("number") if isinstance(contact, dict) else None
  515. service_id = self._extract_contact_uuid(contact, chat_id)
  516. if number and service_id:
  517. self._remember_recipient_identifiers(number, service_id)
  518. return self._recipient_uuid_by_number.get(chat_id, chat_id)
  519. async def _with_target(self, params: dict[str, Any], chat_id: str, *, resolve: bool = True) -> dict[str, Any]:
  520. """Add the groupId / recipient routing key for *chat_id* to *params* (in place)."""
  521. if chat_id.startswith("group:"):
  522. params["groupId"] = chat_id[6:]
  523. else:
  524. params["recipient"] = [await self._resolve_recipient(chat_id) if resolve else chat_id]
  525. return params
  526. async def _fetch_attachment(self, attachment_id: str) -> tuple:
  527. """Fetch an attachment via JSON-RPC and cache it. Returns (path, ext)."""
  528. result = await self._rpc("getAttachment", {"account": self.account, "id": attachment_id})
  529. if not result:
  530. return None, ""
  531. if isinstance(result, dict): # signal-cli returns {"data": "base64..."}
  532. result = result.get("data")
  533. if not result:
  534. logger.warning("Signal: attachment response missing 'data' key")
  535. return None, ""
  536. raw_data = base64.b64decode(result)
  537. ext = _guess_extension(raw_data)
  538. # Android voice notes are raw ADTS AAC, which Whisper-style STT rejects; remux losslessly
  539. # to .m4a. Without ffmpeg the raw file is cached as-is (no downstream remux fallback).
  540. if ext == ".aac":
  541. raw_data, ext = (await asyncio.to_thread(_remux_aac_to_m4a, raw_data)) or (raw_data, ext)
  542. cache = (cache_image_from_bytes_async if _is_image_ext(ext)
  543. else cache_audio_from_bytes_async if _is_audio_ext(ext) else cache_document_from_bytes_async)
  544. return await cache(raw_data, ext), ext
  545. async def _rpc(self, method: str, params: dict, rpc_id: str | None = None, *, log_failures: bool = True,
  546. raise_on_rate_limit: bool = False, timeout: float = 30.0) -> Any:
  547. """Send a JSON-RPC 2.0 request to signal-cli. ``log_failures=False`` logs failures at DEBUG (typing
  548. path: silence NETWORK_FAILURE spam); ``raise_on_rate_limit=True`` raises ``SignalRateLimitError``
  549. on a 429 / RateLimitException instead of swallowing it."""
  550. if not self.client:
  551. logger.warning("Signal: RPC called but client not connected")
  552. return None
  553. payload = {"jsonrpc": "2.0", "method": method, "params": params,
  554. "id": rpc_id if rpc_id is not None else f"{method}_{int(time.time() * 1000)}"}
  555. fail_level = logging.WARNING if log_failures else logging.DEBUG
  556. try:
  557. resp = await self.client.post(f"{self.http_url}/api/v1/rpc", json=payload, timeout=timeout)
  558. resp.raise_for_status()
  559. data = resp.json()
  560. if "error" in data:
  561. err = data["error"]
  562. if raise_on_rate_limit and _is_signal_rate_limit_error(err):
  563. err_msg = str(err.get("message", "")) if isinstance(err, dict) else str(err)
  564. raise SignalRateLimitError(err_msg, retry_after=_extract_retry_after_seconds(err))
  565. logger.log(fail_level, "Signal RPC error (%s): %s", method, err)
  566. return None
  567. result = data.get("result")
  568. if isinstance(result, dict) and raise_on_rate_limit:
  569. for r in result.get("results") if isinstance(result.get("results"), list) else ():
  570. if isinstance(r, dict) and r.get("type") == "RATE_LIMIT_FAILURE":
  571. raise SignalRateLimitError("Rate limit exceeded for recipient",
  572. retry_after=r.get("retryAfterSeconds"))
  573. return result
  574. except SignalRateLimitError:
  575. raise
  576. except Exception as e:
  577. logger.log(fail_level, "Signal RPC %s failed: %s", method, e)
  578. return None
  579. def format_message(self, content: str) -> str:
  580. """Plain-text fallback for the base-class send path; send() applies rich styles itself."""
  581. return content
  582. def _validate_send_result(self, result: Any) -> tuple[bool, Optional[str]]:
  583. """Validate signal-cli send response results. Returns (success, error_message)."""
  584. results = result.get("results") if isinstance(result, dict) else None
  585. for r in results if isinstance(results, list) else ():
  586. if not isinstance(r, dict):
  587. continue
  588. rtype = r.get("type")
  589. if rtype and rtype != "SUCCESS":
  590. return False, str(rtype)
  591. if "success" in r and not r.get("success"):
  592. return False, str(r.get("failure") or "Recipient delivery failed")
  593. return True, None
  594. @staticmethod
  595. def _utf16_offsets(text: str) -> list[int]:
  596. """Return cumulative UTF-16 offsets for every Python character boundary."""
  597. return [0, *itertools.accumulate(utf16_len(char) for char in text)]
  598. @staticmethod
  599. def _styles_for_chunk(text_styles: list[str], chunk_start: int, chunk_end: int) -> list[str]:
  600. """Translate full-message Signal styles into a chunk-local range list."""
  601. adjusted: list[str] = []
  602. for style_string in text_styles:
  603. try:
  604. start_s, length_s, style_type = style_string.split(":", 2)
  605. style_start, style_end = int(start_s), int(start_s) + int(length_s)
  606. except (TypeError, ValueError):
  607. logger.debug("[Signal] Ignoring malformed textStyle range: %r", style_string)
  608. continue
  609. overlap_start, overlap_end = max(style_start, chunk_start), min(style_end, chunk_end)
  610. if overlap_start < overlap_end:
  611. adjusted.append(f"{overlap_start - chunk_start}:{overlap_end - overlap_start}:{style_type}")
  612. return adjusted
  613. @classmethod
  614. def _split_signal_formatted_message(
  615. cls, plain_text: str, text_styles: list[str], max_length: int) -> list[tuple[str, list[str]]]:
  616. """Split converted Signal text into chunks, translating body ranges per chunk. Splitting after
  617. conversion keeps styles that cross a chunk boundary intact instead of leaking Markdown markers."""
  618. if utf16_len(plain_text) <= max_length:
  619. return [(plain_text, text_styles)]
  620. body_limit = max(1, max_length - 10) # 10 = indicator reserve, mirrors truncate_message().
  621. offsets = cls._utf16_offsets(plain_text)
  622. chunks: list[tuple[str, list[str]]] = []
  623. start_idx, total_u16 = 0, offsets[-1]
  624. while offsets[start_idx] < total_u16:
  625. end_budget = min(total_u16, offsets[start_idx] + body_limit)
  626. end_idx = start_idx + 1
  627. while end_idx < len(offsets) and offsets[end_idx] <= end_budget:
  628. end_idx += 1
  629. end_idx = max(end_idx - 1, start_idx + 1)
  630. chunk_styles = cls._styles_for_chunk(text_styles, offsets[start_idx], offsets[end_idx])
  631. chunks.append((plain_text[start_idx:end_idx], chunk_styles))
  632. start_idx = end_idx
  633. if len(chunks) == 1:
  634. return chunks
  635. return [(f"{txt} ({idx}/{len(chunks)})", st) for idx, (txt, st) in enumerate(chunks, start=1)]
  636. async def _rpc_send(self, params: dict[str, Any], fail_error: str) -> tuple[Any, Optional[SendResult]]:
  637. """Run a ``send`` RPC, validate and track it; ``(result, None)`` or ``(None, failed SendResult)``."""
  638. if (result := await self._rpc("send", params)) is None:
  639. return None, SendResult(success=False, error=fail_error)
  640. success, err_msg = self._validate_send_result(result)
  641. if not success:
  642. return None, SendResult(success=False, error=err_msg, raw_response=result)
  643. self._track_sent_timestamp(result)
  644. return result, None
  645. async def send(self, chat_id: str, content: str, reply_to: Optional[str] = None,
  646. metadata: Optional[dict[str, Any]] = None) -> SendResult:
  647. """Send a text message with native Signal formatting."""
  648. await self._stop_typing_indicator(chat_id)
  649. if not content or not content.strip():
  650. return SendResult(success=True, message_id=None)
  651. base_params = await self._with_target({"account": self.account}, chat_id)
  652. chunks = self._split_signal_formatted_message(*markdown_to_signal(content), self.MAX_MESSAGE_LENGTH)
  653. last_result = None
  654. for idx, (plain_text, text_styles) in enumerate(chunks, start=1):
  655. params: dict[str, Any] = dict(base_params, message=plain_text)
  656. if len(text_styles) == 1:
  657. params["textStyle"] = text_styles[0]
  658. elif text_styles:
  659. params["textStyles"] = text_styles
  660. logger.info("[Signal] Sending response chunk %d/%d (%d chars) to %s", idx, len(chunks), len(plain_text),
  661. chat_id)
  662. last_result, err = await self._rpc_send(params, "RPC send failed")
  663. if err:
  664. return err
  665. # No editable message identifier; message_id=None keeps the stream consumer on the non-edit path.
  666. return SendResult(success=True, message_id=None, raw_response=last_result)
  667. def _track_sent_timestamp(self, rpc_result) -> None:
  668. """Record outbound message timestamp for echo-back filtering."""
  669. ts = rpc_result.get("timestamp") if isinstance(rpc_result, dict) else None
  670. if not ts:
  671. return
  672. self._remember_sent_message_timestamp(ts)
  673. now, recent = time.monotonic(), self._recent_sent_timestamps
  674. recent.pop(ts, None) # re-insert to mark as most-recently-used
  675. recent[ts] = now
  676. # Drop entries older than TTL first, then enforce the hard cap.
  677. cutoff = now - self._recent_sent_ttl_seconds
  678. while recent and next(iter(recent.values())) < cutoff:
  679. recent.popitem(last=False)
  680. while len(recent) > self._max_recent_timestamps:
  681. recent.popitem(last=False)
  682. def _consume_sent_timestamp(self, ts) -> bool:
  683. """Pop a timestamp if it matches one we sent. Returns True on echo."""
  684. return bool(ts) and self._recent_sent_timestamps.pop(ts, None) is not None
  685. async def send_typing(self, chat_id: str, metadata=None) -> None:
  686. """Typing indicator (called every ~2s by base.py's ``_keep_typing``). Only the first consecutive
  687. failure logs at WARNING; after three, the RPC is skipped for an exponential cooldown; success resets."""
  688. now = time.monotonic()
  689. if now < self._typing_skip_until.get(chat_id, 0.0):
  690. return
  691. params = await self._with_target({"account": self.account}, chat_id)
  692. fails = self._typing_failures.get(chat_id, 0)
  693. if await self._rpc("sendTyping", params, rpc_id="typing", log_failures=(fails == 0)) is not None:
  694. self._typing_failures.pop(chat_id, None)
  695. self._typing_skip_until.pop(chat_id, None)
  696. return
  697. fails = self._typing_failures[chat_id] = fails + 1
  698. if fails >= 3: # exponential backoff: 16s, 32s, 60s cap
  699. self._typing_skip_until[chat_id] = now + min(60.0, 16.0 * (2 ** (fails - 3)))
  700. async def _resolve_image_path(self, image_url: str) -> tuple[Optional[str], Optional[str], Any]:
  701. """``(path, None, None)`` or ``(None, reason, detail)``: reason download (exc) / missing / oversize (size)."""
  702. if image_url.startswith("file://"):
  703. file_path = unquote(image_url[7:])
  704. else:
  705. try:
  706. file_path = await cache_image_from_url(image_url)
  707. except Exception as e:
  708. return None, "download", e
  709. if not file_path or not Path(file_path).exists():
  710. return None, "missing", None
  711. if (file_size := Path(file_path).stat().st_size) > SIGNAL_MAX_ATTACHMENT_SIZE:
  712. return None, "oversize", file_size
  713. return file_path, None, None
  714. async def send_multiple_images(self, chat_id: str, images: list[tuple[str, str]],
  715. metadata: Optional[dict[str, Any]] = None, human_delay: float = 0.0) -> SendResult:
  716. """Send a batch of images via chunked Signal RPC calls. Alt texts are dropped (one shared body
  717. per send); bad images are skipped with a warning; ``human_delay`` is ignored (scheduler paces).
  718. Returns success when at least one batch was accepted, so media-only turns report SUCCESS."""
  719. if not images:
  720. return SendResult(success=False, error="no images to send")
  721. scheduler = get_scheduler()
  722. logger.info("Signal send_multiple_images: received %d image(s) for %s — scheduler state: %s", len(images),
  723. chat_id[:30], scheduler.state())
  724. await self._stop_typing_indicator(chat_id)
  725. attachments: list[str] = []
  726. skipped = {"download": 0, "missing": 0, "oversize": 0}
  727. for image_url, _alt_text in images:
  728. file_path, reason, detail = await self._resolve_image_path(image_url)
  729. if not reason:
  730. attachments.append(file_path)
  731. continue
  732. skipped[reason] += 1
  733. logger.warning(*_SKIP_IMAGE_LOG[reason](image_url, detail))
  734. if not attachments:
  735. logger.error("Signal: no valid images in batch of %d (download=%d missing=%d oversize=%d)", len(images),
  736. skipped["download"], skipped["missing"], skipped["oversize"])
  737. return SendResult(success=False, error="no valid images in batch")
  738. logger.info("Signal send_multiple_images: %d/%d images valid, sending in chunks", len(attachments), len(images))
  739. base_params = await self._with_target({"account": self.account, "message": ""}, chat_id)
  740. per = SIGNAL_MAX_ATTACHMENTS_PER_MSG
  741. att_batches = [attachments[i:i + per] for i in range(0, len(attachments), per)]
  742. n_batches = len(att_batches)
  743. delivered = False
  744. for idx, att_batch in enumerate(att_batches, start=1):
  745. n = len(att_batch)
  746. estimated = scheduler.estimate_wait(n)
  747. logger.debug("Signal batch %d/%d: %d attachments, estimated wait=%.1fs", idx, n_batches, n, estimated)
  748. if estimated >= SIGNAL_BATCH_PACING_NOTICE_THRESHOLD:
  749. await self._notify_batch_pacing(chat_id, idx, n_batches, estimated)
  750. if await self._send_attachment_batch(scheduler, dict(base_params, attachments=att_batch), n,
  751. f"{idx}/{n_batches}"):
  752. delivered = True
  753. return SendResult(
  754. success=delivered,
  755. error=None if delivered else "all Signal attachment batches failed")
  756. async def _send_attachment_batch(self, scheduler, params: dict[str, Any], n: int, label: str) -> bool:
  757. """Send one attachment batch with rate-limit pacing and a single transient retry. Tokens are
  758. deducted only on validated success (None = server never accepted it); 429s feed the scheduler.
  759. Returns True when the server accepted the batch."""
  760. send_timeout, max_attempts = _signal_send_timeout(n), SIGNAL_RATE_LIMIT_MAX_ATTEMPTS
  761. for attempt in range(1, max_attempts + 1):
  762. await scheduler.acquire(n)
  763. t0 = time.monotonic()
  764. try:
  765. result = await self._rpc("send", params, raise_on_rate_limit=True, timeout=send_timeout)
  766. except SignalRateLimitError as e:
  767. scheduler.feedback(e.retry_after, n)
  768. retry_after = f"{e.retry_after:.0f}s" if e.retry_after else "unknown"
  769. if attempt >= max_attempts:
  770. logger.error("Signal: rate-limit retries exhausted on batch %s (%d attachments lost, "
  771. "server retry_after=%s)", label, n, retry_after)
  772. return False
  773. logger.warning("Signal: rate-limited on batch %s (attempt %d/%d, server retry_after=%s); "
  774. "scheduler will pace the retry", label, attempt, max_attempts, retry_after)
  775. continue
  776. duration = time.monotonic() - t0
  777. success, err_msg = self._validate_send_result(result) if result is not None else (False, None)
  778. if success:
  779. self._track_sent_timestamp(result)
  780. await scheduler.report_rpc_duration(duration, n)
  781. logger.info("Signal batch %s: %d attachments sent in %.1fs (attempt %d/%d)", label, n, duration,
  782. attempt, max_attempts)
  783. return True
  784. logger.error("Signal: RPC send failed for batch %s (%d attachments, attempt %d/%d, rpc_duration=%.1fs)%s",
  785. label, n, attempt, max_attempts, duration, f": {err_msg}" if result is not None else "")
  786. if attempt >= max_attempts:
  787. return False
  788. logger.info("Signal: retrying batch %s after %.1fs backoff", label, 2.0 ** attempt)
  789. await asyncio.sleep(2.0 ** attempt)
  790. return False
  791. async def _notify_batch_pacing(self, chat_id: str, next_batch_idx: int, total_batches: int, wait_s: float) -> None:
  792. """Tell the user about an inter-batch pacing wait over the notice threshold (best-effort)."""
  793. try:
  794. await self.emit_warning(chat_id, t(
  795. "platform.signal.batch_pacing_notice",
  796. wait=_format_wait(wait_s), batch=next_batch_idx, total=total_batches))
  797. except Exception as e:
  798. logger.warning("Signal: failed to send pacing notice: %s", e)
  799. async def send_image(self, chat_id: str, image_url: str, caption: Optional[str] = None, **kwargs) -> SendResult:
  800. """Send an image. Supports http(s):// and file:// URLs."""
  801. await self._stop_typing_indicator(chat_id)
  802. file_path, reason, detail = await self._resolve_image_path(image_url)
  803. if reason == "download":
  804. logger.warning("Signal: failed to download image: %s", detail)
  805. if reason:
  806. return SendResult(success=False, error={
  807. "download": str(detail), "missing": "Image file not found",
  808. "oversize": f"Image too large ({detail} bytes)"}[reason])
  809. return await self._send_file(chat_id, file_path, caption, "RPC send with attachment failed")
  810. async def _send_file(self, chat_id: str, file_path: str, caption: Optional[str], fail_error: str) -> SendResult:
  811. """Send one local file as a Signal attachment via the ``send`` RPC."""
  812. params = await self._with_target(
  813. {"account": self.account, "message": caption or "", "attachments": [file_path]}, chat_id)
  814. _, err = await self._rpc_send(params, fail_error)
  815. return err or SendResult(success=True)
  816. async def _send_attachment(self, chat_id: str, file_path: str, media_label: str,
  817. caption: Optional[str] = None) -> SendResult:
  818. """Send any local file as a Signal attachment (shared by send_document/image_file/voice/video)."""
  819. await self._stop_typing_indicator(chat_id)
  820. try:
  821. file_size = Path(file_path).stat().st_size
  822. except FileNotFoundError:
  823. return SendResult(success=False, error=f"{media_label} file not found: {file_path}")
  824. if file_size > SIGNAL_MAX_ATTACHMENT_SIZE:
  825. return SendResult(success=False, error=f"{media_label} too large ({file_size} bytes)")
  826. return await self._send_file(chat_id, file_path, caption, f"RPC send {media_label.lower()} failed")
  827. async def send_document(self, chat_id, file_path, caption=None, filename=None, **kwargs) -> SendResult:
  828. return await self._send_attachment(chat_id, file_path, "File", caption)
  829. async def send_image_file(self, chat_id, image_path, caption=None, reply_to=None, **kwargs) -> SendResult:
  830. """Native Signal attachment for the gateway MEDIA: delivery path."""
  831. return await self._send_attachment(chat_id, image_path, "Image", caption)
  832. async def send_voice(self, chat_id, audio_path, caption=None, reply_to=None, **kwargs) -> SendResult:
  833. """Audio attachment — Signal has no distinct voice-message API."""
  834. return await self._send_attachment(chat_id, audio_path, "Audio", caption)
  835. async def send_video(self, chat_id, video_path, caption=None, reply_to=None, **kwargs) -> SendResult:
  836. return await self._send_attachment(chat_id, video_path, "Video", caption)
  837. async def _stop_typing_indicator(self, chat_id: str) -> None:
  838. """Stop a typing indicator loop for a chat."""
  839. await cancel_task(self._typing_tasks.pop(chat_id, None))
  840. # Explicit stop-typing RPC so the recipient drops the indicator now instead of after
  841. # Signal's ~5s timeout. Best-effort: failures must not prevent the backoff cleanup below.
  842. with suppress(Exception):
  843. params = await self._with_target({"account": self.account}, chat_id)
  844. params["stop"] = True
  845. await self._rpc("sendTyping", params, rpc_id="typing-stop", log_failures=False)
  846. self._typing_failures.pop(chat_id, None)
  847. self._typing_skip_until.pop(chat_id, None)
  848. async def stop_typing(self, chat_id: str) -> None:
  849. """Public stop-typing hook called from the base adapter's _keep_typing finally block."""
  850. await self._stop_typing_indicator(chat_id)
  851. async def _send_reaction_rpc(self, chat_id: str, params: dict[str, Any]) -> bool:
  852. """Route a ``sendReaction`` RPC to *chat_id* (no UUID upgrade — author IDs come from the envelope)."""
  853. await self._with_target(params, chat_id, resolve=False)
  854. return await self._rpc("sendReaction", params) is not None
  855. async def send_reaction(self, chat_id: str, emoji: str, target_author: str, target_timestamp: int) -> bool:
  856. """React to the message (author number/UUID, Signal ms timestamp) via signal-cli RPC."""
  857. ok = await self._send_reaction_rpc(chat_id, {
  858. "account": self.account, "emoji": emoji, "targetAuthor": target_author,
  859. "targetTimestamp": target_timestamp})
  860. if not ok:
  861. logger.debug("Signal: sendReaction failed (chat=%s, emoji=%s)", chat_id[:20], emoji)
  862. return ok
  863. async def remove_reaction(self, chat_id: str, target_author: str, target_timestamp: int) -> bool:
  864. """Remove a reaction by sending an empty-string emoji."""
  865. return await self._send_reaction_rpc(chat_id, {
  866. "account": self.account, "emoji": "", "targetAuthor": target_author, "targetTimestamp": target_timestamp,
  867. "remove": True})
  868. def _extract_reaction_target(self, event: MessageEvent) -> Optional[tuple]:
  869. """Extract (target_author, target_timestamp) from a MessageEvent, or None."""
  870. raw = event.raw_message
  871. ok = isinstance(raw, dict) and raw.get("sender") and raw.get("timestamp_ms")
  872. return (raw["sender"], raw["timestamp_ms"]) if ok else None
  873. def _reactions_enabled(self, event: MessageEvent = None) -> bool:
  874. """SIGNAL_REACTIONS env gate, then the DM allowlist: reactions fire before run.py's auth gate,
  875. so an unauthorized contact's 👀 would otherwise reveal a listening bot."""
  876. if str(_sig_secret("SIGNAL_REACTIONS", "true")).lower() in {"false", "0", "no"}:
  877. return False
  878. sender = getattr(getattr(event, "source", None), "user_id", None) if event is not None else None
  879. return not (sender and "*" not in self.dm_allow_from and sender not in self.dm_allow_from)
  880. async def on_processing_start(self, event: MessageEvent) -> None:
  881. """React with 👀 when processing begins."""
  882. if self._reactions_enabled(event) and (target := self._extract_reaction_target(event)):
  883. await self.send_reaction(event.source.chat_id, "👀", *target)
  884. async def on_processing_complete(self, event: MessageEvent, outcome: ProcessingOutcome) -> None:
  885. """Swap 👀 for ✅/❌; on CANCELLED the 👀 stays to keep reflecting "in progress" (matches Telegram)."""
  886. if outcome == ProcessingOutcome.CANCELLED or not self._reactions_enabled(event):
  887. return
  888. if not (target := self._extract_reaction_target(event)):
  889. return
  890. await self.remove_reaction(event.source.chat_id, *target)
  891. if emoji := _OUTCOME_REACTION.get(outcome):
  892. await self.send_reaction(event.source.chat_id, emoji, *target)
  893. async def get_chat_info(self, chat_id: str) -> dict[str, Any]:
  894. """Get information about a chat/contact."""
  895. if chat_id.startswith("group:"):
  896. return {"name": chat_id, "type": "group", "chat_id": chat_id}
  897. result = await self._rpc("getContact", {"account": self.account, "contactAddress": chat_id})
  898. name = (result.get("name") or result.get("profileName")) if isinstance(result, dict) else None
  899. return {"name": name or chat_id, "type": "dm", "chat_id": chat_id}