Architecture
Kantoor is one Elixir release. It contains a Phoenix application, a CalDAV/CardDAV endpoint, a Karta inbound webhook, and optional IMAP and POP3 listeners. Postgres holds the mailbox index, the accounts, the DAV data and a bounded cache of message bodies. The mail source (today Karta) remains the store of record for bodies and the transport for incoming and outgoing mail.
The release
The application starts, in order:
KantoorWeb.Telemetry;Kantoor.Repo;Kantoor.Cluster.children/0(nothing unlessKANTOOR_CLUSTER=true; then the Postgres discovery of other replicas, see Running several replicas);Kantoor.PubSub;Kantoor.RateLimiter(the in-memory login throttle);Kantoor.Karta.TaskSupervisorandKantoor.Karta.BodyFetcher(background body fetches with retry);Kantoor.Bodies.Sweeper(evicts the least recently read cached bodies);Kantoor.Maintenance(scheduled, idempotent jobs);KantoorWeb.Endpoint(Bandit);Kantoor.MailServer.Supervisor, only when IMAP or POP3 is enabled.
The OIDC provider worker starts before all of this only when OIDC is
configured. Without it the application still boots and /health works.
The HTTP endpoint
KantoorWeb.Endpoint runs these plugs in order:
Plug.Staticservespriv/static, including the generated documentation underdocs;- the client IP plug resolves the address only through trusted proxies;
KantoorWeb.Plugs.DavGuarddrops cookies and caps the body for/davand the two.well-knownpaths;KantoorWeb.Plugs.KartaRawBodykeeps the raw body of the inbound webhook for signature checking;- the usual parsers, method override, session and router.
KantoorWeb.Router mounts:
| Path | What it is |
|---|---|
/health | unauthenticated health probe |
/ | public home page |
/auth/* | OIDC login, callback and logout |
/locale/:locale | stores the interface language in the session |
/ and /mail | webmail LiveViews and attachment downloads |
/calendar, /tasks, /contacts | LiveViews over the DAV data |
/settings, /settings/* | My settings (index, profile, mail, calendars, devices, sharing) |
/admin/* | admin LiveViews |
/karta/inbound | Karta push receiver |
/dav, /.well-known/caldav, /.well-known/carddav | CalDAV/CardDAV |
/docs | redirect to the generated documentation |
/dev/dashboard | LiveDashboard, development only |
Every LiveView session re-checks authorization in its context; the router guard is not the only check.
Contexts
| Module | Responsibility |
|---|---|
Kantoor.Accounts | users, roles, mailbox attachment, OIDC claims, scopes |
Kantoor.AppPasswords | app password creation, verification and throttling |
Kantoor.Mailboxes | domains, mailboxes, folders, messages, UIDs, MODSEQ, tombstones |
Kantoor.Ingest | turns a verified push into an index record in one transaction |
Kantoor.Bodies | the bounded, zlib-compressed body cache |
Kantoor.Webmail | every webmail action, authorized per mailbox |
Kantoor.Search | Postgres full-text search |
Kantoor.Mime | MIME parsing, charsets and text extraction |
Kantoor.Quota | virtual quotas and held mail |
Kantoor.MailServer | shared IMAP/POP3 configuration, connections, TLS, proxy header |
Kantoor.IMAP | parser, session and handlers for IMAP4rev1 |
Kantoor.POP3 | session and handler for POP3 |
Kantoor.Dav | the DAV store, validators, scheduling and iMIP |
Kantoor.Sharing | collection shares and free/busy opt-in |
Kantoor.Events, Kantoor.Tasks, Kantoor.Contacts | writes from the web pages |
Kantoor.Invitations | the invitation card and its answer |
Kantoor.FreeBusy | busy blocks between users who opted in |
Kantoor.Identities | sending identities |
Kantoor.Audit | the audit log (admins, API keys, system tasks) |
Kantoor.ApiKeys | operator API keys for the provisioning API |
Kantoor.Provisioning | what an operator may do: Domains, Users, Mailboxes, Usage, Idempotency |
Kantoor.Maintenance | scheduled sweep and release jobs |
Kantoor.Karta | the mail-source client, inbound payload and body fetcher |
The database
The migrations create these tables:
domains,domain_settings,mailboxes;folders,messages,tombstones,message_bodies;users,user_mailboxes,app_passwords,known_login_ips,identities;audit_events,api_keys,api_idempotency_keys;dav_collections,dav_objects,dav_tombstones,dav_properties,dav_shares.
Two counters make the index safe for concurrent clients. Each folder row
carries uid_next and highest_modseq, advanced with one atomic
UPDATE ... RETURNING inside the caller's transaction. A UID is never
reused, a rolled-back transaction consumes no value, and a modseq is used by
every visibility change, flag change and expunge. Messages held over quota
have no UID until they are released.
Each DAV collection carries a revision counter. The sync token and the ctag
are urn:kantoor:sync:<collection-id>:<rev>; a deletion leaves a tombstone
with the revision of the deletion.
How mail arrives
- Karta posts a signed Standard Webhooks request to
/karta/inbound. KantoorWeb.Plugs.KartaRawBodykeeps the raw body;KantoorWeb.KartaInboundControllerverifies the signature and builds aKantoor.Karta.InboundPayload.Kantoor.Ingest.ingest/3looks up the recipient, parses the MIME outside the transaction, then in one transaction locks the mailbox, re-checks the delivery id, applies the quota, allocates a UID for visible mail and inserts the row.- The body is stored in the cache if the push carried it; otherwise the
record is
pending_fetchandKantoor.Karta.BodyFetcherfetches it with retries. - Kantoor answers 2xx only after the transaction commits. The delivery id is unique, so a repeated push is ignored.
A received calendar invitation is applied inside this path, through
Kantoor.Dav.Itip, only when the push's authentication result passes.
How the clients see changes
- Webmail and the web pages subscribe to
Kantoor.PubSubthroughKantoor.MailboxEventsandKantoor.DavEvents. Events carry ids and revisions, never content; the page re-reads what it shows from the database. - IMAP publishes per-mailbox changes and uses the folder UID and modseq counters, so IDLE, CONDSTORE and QRESYNC clients see a consistent state.
- DAV publishes a collection revision on commit, which becomes the sync token and ctag.
- Sharing publishes a data-free notification, and every page re-reads the grants; a revoked grant stops working on the next request.
Testing
The test suite uses ExUnit, with Kantoor.DataCase for database tests and
KantoorWeb.ConnCase for endpoint tests. External services are replaced by
fakes: tests use an OIDC double and a Karta double.
Protocol behaviour is also checked with Python's imaplib and poplib
through test/scripts/interop_server.ex and test/scripts/interop.py.
The quality gate is one command:
mise exec -- mix precommit
It runs hex.audit, format --check-formatted, compile --warnings-as-errors, Credo, Sobelow, deps.audit and the test suite with a
coverage threshold.