# Roundcube in a session

Roundcube webmail runs in the person's own session Pod, as them, and Ivy frames it at the
instance's own origin. It opens their Microsoft 365 mailbox, signed in with the Microsoft account
they connected in Ivy.

## How the image and the app fit together

- **The image.** `ivy-runner-php` (`api/Dockerfile`) is the Python runner plus PHP 8.2 and a
  pinned Roundcube release unpacked at `/opt/roundcube`, so it carries the tens of megabytes of
  PHP and JavaScript and this app doesn't. Sessions start from it where an installation makes
  `php` the session environment (`ivy:sandboxPhpRunner` in production, kind's `MAIL=1`); its
  `provides = [python, shell]` keeps Python and Python apps working in those sessions.
- **The app.** `roundcube.ivy-app.toml` declares `environment = "php"`, so a chat runs it only
  when its session environment provides PHP, and two processes: `launch.sh`, ready once port
  8080 listens, and `tunnel.py`. Its root is staged read-only at `/app/roundcube`.
- **The launcher** starts afresh in `/tmp/roundcube` each time, generates `des_key` (never
  committed) and runs PHP's built-in server with 4 workers on 127.0.0.1:8080, with
  `prepend.php` pointing Roundcube at this app's `plugins/`. Roundcube reads
  `/opt/roundcube/config`'s defaults, then `config.inc.php` (`RCUBE_CONFIG_PATH`), and creates
  its SQLite database there on first use.

## Mail and sign-in

- **Mail servers.** Microsoft 365's IMAP (`outlook.office365.com:993`) and SMTP submission
  (`smtp.office365.com:587`), the manifest's `egress`. Sandboxes reach nothing but the egress
  proxy, and PHP's mail sockets don't speak its CONNECT, so `tunnel.py` listens on
  127.0.0.1:10993 and :10587 and carries each connection through the proxy; Roundcube checks the
  servers' certificates against their real names. The proxy opens 993 and 587 only for an app
  that names them for a host (`sandbox.egressProxy.appPorts`), and a space admin allows both
  hosts under Settings → Network access for code before the app starts.
- **Sign-in.** Microsoft 365 takes only OAuth for IMAP and SMTP. The person signs in to
  Microsoft once in Ivy, under Settings → Connected accounts (the manifest's `microsoft`
  secret, Ivy's Entra app: `MICROSOFT_APPS_CLIENT_ID`). The `ivy_microsoft` plugin asks Ivy for
  a short-lived access token through `IVY_API_URL` when Roundcube's login page opens, signs in
  with XOAUTH2 as the account Ivy names, and asks again before the token expires. The refresh
  token never leaves Ivy, and no password is typed anywhere.
- **Without a sign-in.** The secret is `optional`, so Roundcube starts anyway and shows its own
  form with a hint to sign in to Microsoft in Ivy. That is what an instance started from the
  Apps tab shows: it runs for the whole space, as no person, so it has no one's token.

## Try it on kind

```bash
MAIL=1 make k8s-up
```

This builds `ivy-runner-php`, starts sessions from it, and runs GreenMail in the `ivy-mail`
namespace with three accounts, `alice@ivy.test`, `bob@ivy.test` and `carol@ivy.test`, each with
its name as the password; `launch.sh` finds it by name and adds it to Roundcube's servers. Allow
`outlook.office365.com` and `smtp.office365.com` in the space's network access, import **Email**
and start Roundcube from its Apps tab, then choose GreenMail on its form and sign in as one of
them. kind's scripted model calls no tools, so to start it in a chat's session there, use the
API: `POST /api/v1/organisations/{organisation_id}/spaces/{space_id}/apps/roundcube/instances`
with `{"conversation_id": "…"}`. Its row then shows under the conversation, with its logs. To
send a message from outside:

```bash
kubectl --context kind-ivy-sandbox -n ivy-mail port-forward svc/greenmail 3025 &
swaks --server localhost:3025 --from bob@ivy.test --to alice@ivy.test --header "Subject: Hello"
```

Framing the app needs the app gateway, which serves instances only over HTTPS at
`https://<instance>.run.<domain>`, with a wildcard certificate.
