Tern in one page
Tern is one program and a store on disk. This page says who comes in at which door and where mail ends up. The other pages go into the protocol, the storage engine, search, and how it stands against Cyrus IMAP.
One process, five doors
The server is a single process, maild. It opens the store and serves everything from it: the gRPC API at two doors, LMTP for arriving mail, IMAP for other mail clients, and the page people sign in at.
The two gRPC doors serve the same API, mail.v1. It is the native protocol: the apps use nothing else, and IMAP is implemented beside it on the same store, not underneath it. See the protocol.
maild store --blobs blobs \ --grpc 127.0.0.1:2500 --lmtp 127.0.0.1:2400 \ --token-file token
The names in the commands are from before the project was called Tern: the daemon is still maild and the API's package mail.v1.
Labels, conversations, one state
- A message can be in any number of folders, which is to say they are labels. Folder 0 is All mail: every message is in it. Over IMAP each label shows as a mailbox.
- Every message has a thread, kept with it across all its labels, so a conversation is one read and not a search of every folder.
- Ids are never reused. A message is known by an id unique within its user, given in the order of arrival.
- A mailbox has one state: the number of changes it has seen. Everything that keeps a copy or follows a mailbox goes by it.
Mail coming in
Mail arrives by LMTP. Each message goes through the person's rules, which say what it is labelled with and whether it is kept at all. It is then taken apart, put together again, compared with what arrived, and written in one batch with its index rows and the words of its text. When LMTP answers, the message is on disk and can be found by its words.
- A delivery that comes again is recognised, and is not stored twice or filed where the person took it from.
- A delivery that would take a person past their quota is refused: LMTP answers
452 4.2.2. - Rules can be read from a Sieve script (RFC 5228), for a mailbox that comes from a server that ran one.
Mail going out
Send hands a message to the server's relay while the caller waits, and keeps a copy, with its Bcc, in Sent. The server keeps no queue. A caller that got no answer asks again, and a message whose Message-ID the person has sent already is not sent twice.
Signing in
- Every app signs in at the server's own page, as a native application does with OAuth: the authorization code with PKCE.
- A device gets tokens of its own. An access token is good for an hour, a refresh token once. Only hashes are kept. A person sees their devices and can end any of them, which also ends the streams it has open.
- Passkeys. A passkey signs a person in with no address and no password typed. From a person's first passkey on, their password lets nobody in, at the page or over IMAP.
- Scopes. A call needs one of
mail.read,mail.write,mail.sendorsettings, and a token without it is refused. Only asking who the token belongs to needs none. - A mail client that can only give a password gets one made for that device: 24 letters and digits, shown once.
The clients
The apps are called tmail. A user interface calls only the core, never gRPC: the core, in Rust, holds the connection, the copy of the mailbox, the outbox and what each view shows.
| Client | Built with | Copy of the mailbox |
|---|---|---|
| Linux | GTK 4, libadwaita, Rust | whole, on the computer |
| iPhone | SwiftUI over the core | whole, on the phone |
| Mac | SwiftUI and AppKit over the core | whole, on the Mac |
| Webmail | Svelte 5 and TypeScript; a small server in Rust | none: everything is asked of the server |
tmail | a command for agents, one for each tool of the assistant | none |
The core finds out by itself that a server no longer answers: it asks its connection every 20 seconds whether the other end is there, and gives it up after 20 seconds without an answer.
The parts
| Crate | What it is |
|---|---|
lsm | The sorted key-value engine: immutable runs with merge rules |
mailstore | Mail rows on the engine, delivery through a person's rules, reads, settings |
blobstore | Where large parts are kept: a directory, an S3-like store, or memory |
msgparts | A raw message split into a skeleton and content-addressed parts, and put back byte for byte |
mailql | The query language: filters and pipes. No dependencies |
sieve | Sieve as far as it is read: a script into its commands, to make rules of it |
imapwire, mailimap | IMAP on the wire, and IMAP sessions on the store |
mailapi | The gRPC API and maild |
mailclient | The clients' core, and the tmail command |
cyrus-migrate | Moves a mailbox from a Cyrus spool |
What it takes to run
- Memory. An empty server uses 2.9 MB. The engine holds a block index and a filter for each run, about 4.6 bytes a message, and a cache of decompressed blocks, 256 MiB by default.
- Disk. A little over a third of the mail as it arrived, on the example mailbox. See storage.
- Privileges. None. The systemd unit it is deployed with has no capabilities, a system call filter, and scores 1.4 in
systemd-analyze security.
Not built yet
- Replication and high availability.
- Shared folders, and rights on folders between users.
- Ranking of search results, tolerance of typing mistakes, word forms.
- The text of attachments in the index.
- Sign-in through an identity provider.
The API is declared unstable until the first release: mail.v1 may still change.