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:

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.Static serves priv/static, including the generated documentation under docs;
  • the client IP plug resolves the address only through trusted proxies;
  • KantoorWeb.Plugs.DavGuard drops cookies and caps the body for /dav and the two .well-known paths;
  • KantoorWeb.Plugs.KartaRawBody keeps the raw body of the inbound webhook for signature checking;
  • the usual parsers, method override, session and router.

KantoorWeb.Router mounts:

PathWhat it is
/healthunauthenticated health probe
/public home page
/auth/*OIDC login, callback and logout
/locale/:localestores the interface language in the session
/ and /mailwebmail LiveViews and attachment downloads
/calendar, /tasks, /contactsLiveViews over the DAV data
/settings, /settings/*My settings (index, profile, mail, calendars, devices, sharing)
/admin/*admin LiveViews
/karta/inboundKarta push receiver
/dav, /.well-known/caldav, /.well-known/carddavCalDAV/CardDAV
/docsredirect to the generated documentation
/dev/dashboardLiveDashboard, development only

Every LiveView session re-checks authorization in its context; the router guard is not the only check.

Contexts

ModuleResponsibility
Kantoor.Accountsusers, roles, mailbox attachment, OIDC claims, scopes
Kantoor.AppPasswordsapp password creation, verification and throttling
Kantoor.Mailboxesdomains, mailboxes, folders, messages, UIDs, MODSEQ, tombstones
Kantoor.Ingestturns a verified push into an index record in one transaction
Kantoor.Bodiesthe bounded, zlib-compressed body cache
Kantoor.Webmailevery webmail action, authorized per mailbox
Kantoor.SearchPostgres full-text search
Kantoor.MimeMIME parsing, charsets and text extraction
Kantoor.Quotavirtual quotas and held mail
Kantoor.MailServershared IMAP/POP3 configuration, connections, TLS, proxy header
Kantoor.IMAPparser, session and handlers for IMAP4rev1
Kantoor.POP3session and handler for POP3
Kantoor.Davthe DAV store, validators, scheduling and iMIP
Kantoor.Sharingcollection shares and free/busy opt-in
Kantoor.Events, Kantoor.Tasks, Kantoor.Contactswrites from the web pages
Kantoor.Invitationsthe invitation card and its answer
Kantoor.FreeBusybusy blocks between users who opted in
Kantoor.Identitiessending identities
Kantoor.Auditthe audit log (admins, API keys, system tasks)
Kantoor.ApiKeysoperator API keys for the provisioning API
Kantoor.Provisioningwhat an operator may do: Domains, Users, Mailboxes, Usage, Idempotency
Kantoor.Maintenancescheduled sweep and release jobs
Kantoor.Kartathe 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

  1. Karta posts a signed Standard Webhooks request to /karta/inbound.
  2. KantoorWeb.Plugs.KartaRawBody keeps the raw body; KantoorWeb.KartaInboundController verifies the signature and builds a Kantoor.Karta.InboundPayload.
  3. Kantoor.Ingest.ingest/3 looks 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.
  4. The body is stored in the cache if the push carried it; otherwise the record is pending_fetch and Kantoor.Karta.BodyFetcher fetches it with retries.
  5. 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.PubSub through Kantoor.MailboxEvents and Kantoor.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.