Mail sources
Kantoor keeps an index (folders, flags, UIDs, search text) in Postgres and leaves message bodies in a mail source. Today the only source is Karta (see Connecting to Karta and the Karta interface contract). This page says what a source must do, what in the code is specific to Karta, and what a second source (plain IMAP, JMAP, a Maildir) would cost. No second source exists, and none is planned yet.
The seam: Kantoor.MailSource
lib/kantoor/mail_source.ex is a behaviour. Kantoor.Karta.Client, the
dispatcher the rest of the code calls, declares it, so the compiler checks
that it offers every callback with the right arity. Kantoor.Karta.ReqClient
(the HTTP implementation) implements Kantoor.Karta.Client, whose callbacks
have the same names and signatures. Callers still go through
Kantoor.Karta.Client; the implementation is chosen with
config :kantoor, Kantoor.Karta, client: Module.
| Callback | Used by | What it must do |
|---|---|---|
fetch_raw(message_id, range: {a, b}) | Bodies.fetch/1, Karta.BodyFetcher | the raw bytes, or a byte range, of a stored message |
delete_delivery(delivery_id) | Webmail.delete/2 (best effort, in a task) | forget this Kantoor's copy; idempotent |
list_domains() | admin Domains page | the domains this Kantoor may serve |
send_raw(raw, mail_from:, rcpt_to:, retention:) | Webmail.send_message/3, iMIP in Dav.Itip | send a finished MIME message; the answer may name the stored copy ("id") |
health() | admin dashboard | reachability and version |
attach_domain/2, set_route_mode/2 (optional) | admin Domains page | make a push source deliver a domain to Kantoor, or stop |
What is Karta-shaped
These are the assumptions a second source would run into.
- Ids.
messages.karta_message_id(a body) andmessages.karta_delivery_id(one recipient's copy, unique) are opaque strings from Karta, used inMailboxes,Mailstore,Ingest,Bodies,Webmail,Karta.BodyFetcherand theMessageschema. Messages that exist only in Kantoor (drafts,APPENDed mail) carry the prefixkantoor-local:in the delivery id and are never sent to the source. The names are Karta's; the semantics (a stable body id plus a per-recipient id) are what a source must provide. - Push ingest. New mail arrives as a signed push to
/karta/inbound(KartaInboundController,Karta.InboundPayload,Karta.WebhookSignature), de-duplicated on the delivery id and acknowledged only after the index transaction commits.Kantoor.Ingest.ingest/3takes anInboundPayloadand does not care where it came from, so a polling source can build the same struct. What it needs is a scheduler and a "since" cursor, which do not exist. - Reference-counted deletes. Deleting in Kantoor deletes one delivery; Karta removes the body only when every recipient's delivery is gone. A mailbox-based source (IMAP) has no such notion: deleting means expunging a message in a mailbox, and the same body is not shared.
- Per-route retention. Kantoor answers each push with a retention
(
keepor days;Kantoor.Retention, per-domain setting) and sends with a retention. Sources with no retention control would ignore it; "keep" would simply be true. - Authentication verdict. Received calendar invitations are applied only
when the source says the sender passed SPF, DKIM or DMARC
(
data.authentication). A source that cannot say must make Kantoor treat every invitation as unauthenticated, which means it applies none. - Domain management. The admin pages list domains from the source and
attach Kantoor to them (
attach_domain). Other sources would set the domain list by hand. - No second message store. Quota, held mail and the UID rules
(
Mailboxes) are Kantoor's own and source independent.
What a second source needs
- An implementation of
Kantoor.MailSource(fetch, send, delete, domains, health). - An ingest path: either a poller that turns new messages into
Kantoor.Karta.InboundPayloadstructs and callsIngest.ingest/3(needs a cursor per mailbox and a maintenance job), or an endpoint like the Karta one. - A decision for each of items 3 to 6 above, written down per source.
- A way to tell the verdict of a message (item 5), or to accept that invitations are never applied.
- Configuration: today
Kantoor.Karta.Settings(KARTA_BASE_URL,KARTA_API_KEY,KARTA_INBOUND_SECRETS).
Size of the refactor
- Done here: the behaviour, and the Karta client declares it (about 60 lines, no behaviour change, no migration). The two behaviours duplicate each other's callbacks; a second source would merge them.
- Small (about a day): rename
karta_message_idandkarta_delivery_idto source-neutral names (a migration with two column renames and the code references), and move the moduleKantoor.Karta.ClientbehindKantoor.MailSourceand aconfig :kantoor, :mail_sourcekey. - Medium (a few days, mostly tests): an ingest entry point that is not the Karta controller, the poller and its cursor, and the verdict rules for a source that gives none.
- Not needed to start: changing
Ingest,MailstoreorMailboxes; they are already source independent apart from the column names above.
Nothing here has been run against a second source; the estimates come from reading the call sites listed above.