setup.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. """Interactive ``hermes gateway setup`` wizard for Signal. Helpers resolve late through the
  2. ``hermes_cli.gateway`` facade so the plugin stays importable outside the CLI and the seams tests
  3. patch there keep intercepting."""
  4. import shutil
  5. def _gw():
  6. from hermes_cli import gateway # late: CLI-only dependency
  7. return gateway
  8. def _signal_line_input(prompt_text: str) -> str | None:
  9. """``line_input`` for the Signal wizard; None (after printing the cancel line) on EOF/Ctrl+C."""
  10. try:
  11. return _gw().line_input(prompt_text).strip()
  12. except (EOFError, KeyboardInterrupt):
  13. print("\n Setup cancelled.")
  14. return None
  15. def interactive_setup() -> None:
  16. """``hermes gateway setup`` flow for Signal (registered as the plugin's ``setup_fn``)."""
  17. _gw().print_setup_header("📡 Signal")
  18. existing_url = _gw().get_env_value("SIGNAL_HTTP_URL")
  19. existing_account = _gw().get_env_value("SIGNAL_ACCOUNT")
  20. if not _gw().confirm_reconfigure("Signal", "SIGNAL_HTTP_URL", "SIGNAL_ACCOUNT"):
  21. return
  22. print()
  23. if shutil.which("signal-cli"):
  24. _gw().print_success("signal-cli found on PATH.")
  25. else:
  26. _gw().print_warning("signal-cli not found on PATH.")
  27. _gw()._print_info_lines(
  28. " Signal requires signal-cli running as an HTTP daemon.", " Install options:",
  29. " Linux: download from https://github.com/AsamK/signal-cli/releases",
  30. " macOS: brew install signal-cli", " Docker: bbernhard/signal-cli-rest-api",
  31. )
  32. print()
  33. _gw()._print_info_lines(
  34. " After installing, link your account and start the daemon:",
  35. ' signal-cli link -n "HermesAgent"',
  36. " signal-cli --account +YOURNUMBER daemon --http 127.0.0.1:8080",
  37. )
  38. print()
  39. print()
  40. _gw().print_info(" Enter the URL where signal-cli HTTP daemon is running.")
  41. default_url = existing_url or "http://127.0.0.1:8080"
  42. url = _signal_line_input(f" HTTP URL [{default_url}]: ")
  43. if url is None:
  44. return
  45. url = url or default_url
  46. _gw().print_info(" Testing connection...")
  47. try:
  48. import httpx
  49. resp = httpx.get(f"{url.rstrip('/')}/api/v1/check", timeout=10.0)
  50. if resp.status_code == 200:
  51. _gw().print_success(" signal-cli daemon is reachable!")
  52. else:
  53. _gw().print_warning(f" signal-cli responded with status {resp.status_code}.")
  54. if not _gw().prompt_yes_no(" Continue anyway?", False):
  55. return
  56. except Exception as e: # health: allow BLE001 -- connectivity probe in a wizard: any failure is "unreachable", the user decides
  57. _gw().print_warning(f" Could not reach signal-cli at {url}: {e}")
  58. if not _gw().prompt_yes_no(" Save this URL anyway? (you can start signal-cli later)", True):
  59. return
  60. _gw().save_env_value("SIGNAL_HTTP_URL", url)
  61. print()
  62. _gw()._print_info_lines(" Enter your Signal account phone number in E.164 format.", " Example: +15551234567")
  63. default_account = existing_account or ""
  64. account = _signal_line_input(f" Account number{f' [{default_account}]' if default_account else ''}: ")
  65. if account is None:
  66. return
  67. account = account or default_account
  68. if not account:
  69. _gw().print_error(" Account number is required.")
  70. return
  71. _gw().save_env_value("SIGNAL_ACCOUNT", account)
  72. print()
  73. _gw()._print_info_lines(
  74. " The gateway DENIES all users by default for security.",
  75. " Enter phone numbers or UUIDs of allowed users (comma-separated).",
  76. )
  77. default_allowed = _gw().get_env_value("SIGNAL_ALLOWED_USERS") or account
  78. allowed = _signal_line_input(f" Allowed users [{default_allowed}]: ")
  79. if allowed is None:
  80. return
  81. _gw().save_env_value("SIGNAL_ALLOWED_USERS", allowed or default_allowed)
  82. print()
  83. if _gw().prompt_yes_no(" Enable group messaging? (disabled by default for security)", False):
  84. print()
  85. _gw().print_info(" Enter group IDs to allow, or * for all groups.")
  86. existing_groups = _gw().get_env_value("SIGNAL_GROUP_ALLOWED_USERS") or ""
  87. groups = _signal_line_input(f" Group IDs [{existing_groups or '*'}]: ")
  88. if groups is None:
  89. return
  90. _gw().save_env_value("SIGNAL_GROUP_ALLOWED_USERS", groups or existing_groups or "*")
  91. print()
  92. _gw().print_success("Signal configured!")
  93. _gw()._print_info_lines(
  94. f" URL: {url}", f" Account: {account}", " DM auth: via SIGNAL_ALLOWED_USERS + DM pairing",
  95. f" Groups: {'enabled' if _gw().get_env_value('SIGNAL_GROUP_ALLOWED_USERS') else 'disabled'}",
  96. )