email_modes.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. """Email modes A/B/C helpers + anti-phishing verification-link extraction.
  2. Mode A (default): render a ready-to-send draft to disk; the operator sends it.
  3. Mode B/C: the agent SENDS via a Hermes email mechanism (IMAP/SMTP gateway,
  4. `himalaya`, AgentMail, or Gmail via `google-workspace`) and READS the reply to
  5. resolve the verification link with `extract_verification_link`. Those transports
  6. are driven by the agent through native tools; this module stays network-free so
  7. the hermetic tests pass.
  8. """
  9. from __future__ import annotations
  10. import re
  11. from pathlib import Path
  12. import legal
  13. import paths
  14. _LINK_RE = re.compile(r"https?://[^\s\"'<>)\]]+", re.IGNORECASE)
  15. _VERIFY_HINTS = ("opt", "remov", "verif", "confirm", "unsubscrib", "suppress", "delete", "privacy")
  16. def render_draft(broker: dict, fields: dict, out_dir: Path | None = None) -> Path:
  17. """Mode A: write a ready-to-send opt-out email for the operator to send."""
  18. body = legal.render_optout_email(broker, fields)
  19. out_dir = out_dir or (paths.data_dir() / "drafts")
  20. out_dir.mkdir(parents=True, exist_ok=True)
  21. fp = out_dir / f"{broker.get('id', 'broker')}.txt"
  22. fp.write_text(body, encoding="utf-8")
  23. return fp
  24. def render_request_draft(broker: dict, fields: dict, kind: str = "generic",
  25. out_dir: Path | None = None) -> Path:
  26. """Mode A: write a ready-to-send request of a specific KIND.
  27. kind: generic | ccpa | ccpa_agent | ccpa_indirect | gdpr. Used for indirect-exposure
  28. (ccpa_indirect) and explicit legal requests, where the generic opt-out wording is wrong.
  29. The filename is suffixed with the kind so an indirect request does not overwrite an opt-out draft.
  30. """
  31. body = legal.render_request(kind, broker, fields)
  32. out_dir = out_dir or (paths.data_dir() / "drafts")
  33. out_dir.mkdir(parents=True, exist_ok=True)
  34. suffix = "" if kind == "generic" else f"-{kind}"
  35. fp = out_dir / f"{broker.get('id', 'broker')}{suffix}.txt"
  36. fp.write_text(body, encoding="utf-8")
  37. return fp
  38. def extract_verification_link(email_body: str, broker: dict | None = None) -> str | None:
  39. """Return the most likely opt-out/verification link from an email body.
  40. Anti-phishing: a link is only returned if its URL matches an opt-out hint
  41. and/or the broker's own domain; arbitrary links score 0 and are ignored.
  42. """
  43. candidates = _LINK_RE.findall(email_body or "")
  44. if not candidates:
  45. return None
  46. domain = ""
  47. if broker:
  48. url = (broker.get("optout") or {}).get("url") or (broker.get("search") or {}).get("url") or ""
  49. m = re.search(r"https?://([^/]+)", url)
  50. if m:
  51. domain = m.group(1).replace("www.", "")
  52. best_score, best_link = 0, None
  53. for link in candidates:
  54. low = link.lower()
  55. score = 0
  56. if any(h in low for h in _VERIFY_HINTS):
  57. score += 2
  58. if domain and domain in low:
  59. score += 3
  60. if score > best_score:
  61. best_score, best_link = score, link
  62. return best_link