send_targets.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. """Public send_message contract pieces a platform plugin declares on its ``PlatformEntry``.
  2. Plugins import these instead of core-private helpers in ``tools/send_message_*``:
  3. * ``target_ref_syntax_fn`` parsers — ``(target_ref) -> (chat_id, thread_id) | None | NOT_EXPLICIT``.
  4. None falls through to core's generic id rules (E.164, numeric, Matrix ``!``/``@``); ``NOT_EXPLICIT``
  5. skips them and goes straight to the channel directory.
  6. * ``StandaloneSendPolicy`` — when send_message bypasses the live adapter for ``standalone_sender_fn``.
  7. Pure ``re``/dataclasses: core imports this module before any adapter is materialized.
  8. """
  9. from dataclasses import dataclass
  10. NOT_EXPLICIT = object() # sentinel: stop parsing, target is NOT explicit (skip generic rules)
  11. def regex_groups_parser(regex, *, thread_group=True):
  12. """Explicit when ``regex`` fully matches: chat_id = group 1, thread = group 2 (or None)."""
  13. return lambda ref: ((m.group(1), m.group(2) if thread_group else None)
  14. if (m := regex.fullmatch(ref)) else None)
  15. def regex_stripped_parser(regex):
  16. """Explicit when ``regex`` fully matches; returns the stripped ref verbatim."""
  17. return lambda ref: (ref.strip(), None) if regex.fullmatch(ref) else None
  18. def parse_nonempty(ref):
  19. # ntfy topics and WeCom ids (the adapter picks the send command) are explicit when non-empty.
  20. stripped = ref.strip()
  21. return (stripped, None) if stripped else None
  22. @dataclass(frozen=True)
  23. class StandaloneSendPolicy:
  24. """How send_message routes a platform through its ``standalone_sender_fn`` instead of the
  25. live adapter (Discord: forums/threads/multipart; Slack: files_upload_v2; WhatsApp: Baileys
  26. /send-media; Signal: attachments ride the JSON-RPC param on the final chunk)."""
  27. label: str = "" # names the platform in "<label> plugin not registered ..." errors
  28. media: bool = False # sends that carry media bypass the live adapter
  29. all_sends: bool = False # every send bypasses it, text included (Discord)
  30. captionable: bool = False # one captionable file + short text ride as the media caption
  31. # Non-final chunks get ``media_files=[]`` (True) or ``media_files=None`` (False).
  32. empty_list_between_chunks: bool = False
  33. forward_force_document: bool = False # pass ``force_document`` through to the sender
  34. # ``mentions`` (re-pinged per message, so only the first payload carries them) force this route.
  35. mentions: bool = False
  36. # Generic text-only path: call the sender per chunk directly, skipping the live adapter and
  37. # any ``send_message_handler``.
  38. text_direct: bool = False