Kantoor. Mailboxes
(Kantoor v1.0.0)
The virtual-mailbox index: domains, mailboxes, folders and message records.
UID and MODSEQ guarantees
Every folder row carries uid_next and highest_modseq. They are advanced with a
single atomic UPDATE ... RETURNING inside the caller's transaction, so:
- concurrent writers to one folder serialise on the folder row lock;
- a rolled-back transaction rolls the counters back too (no gaps from failures);
- counters only ever increase, and expunged messages keep their row and their tombstone, so a UID is never reused;
uid_validityis set once at folder creation and never touched again.
A modseq is consumed by every insert/visibility change, flag change and expunge. Messages that are held over quota have no UID and no modseq until released.
Summary
Functions
Inserts a message hidden over quota: stored, but with no UID and no modseq.
Inserts a visible message into folder, allocating its UID and modseq.
attrs is a map of Message fields (without uid/modseq/state).
The domain's catch-all mailbox, or nil when the catch-all is off (or the mailbox it pointed at is gone).
Creates a domain.
Creates a folder with a fresh UIDVALIDITY.
Creates a mailbox on domain together with its default folders.
The default folders created with every mailbox, as {name, special_use}.
Expunges a message. A visible message leaves a tombstone and bumps the modseq; a held message (never visible to any client) is just marked expunged. The row is kept so the UID is never reused and the Karta delivery stays deduped.
A domain by its name (case-insensitive), or nil.
The settings row of a domain, or nil when none was saved yet.
Fetches a folder by exact name (e.g. "INBOX"). Returns nil when absent.
Fetches a folder by special-use role (e.g. "trash"). Returns nil when absent.
Looks a mailbox up by address. The address is normalised and matched by
equality, never with LIKE/ILIKE, so %, _ and * have no special meaning.
Returns nil for unknown or malformed addresses.
Fetches a message by Karta delivery id (the dedupe key).
Lists the folders of a mailbox.
Locks the mailbox row for the rest of the transaction and returns it fresh.
Makes a held message visible, assigning its UID and modseq now.
Moves a visible message to another folder of the same mailbox. IMAP MOVE
semantics: the message gets a NEW uid and modseq in target, and the old uid
is tombstoned in the source folder (never reused). The row (and so its Karta
ids and cached body) is kept. The two folder counters are advanced in id order
so concurrent opposite moves cannot deadlock.
Forgets tombstones older than days days and records, per folder, the highest
modseq pruned (tombstone_floor) so QRESYNC can tell when it must fall back.
Returns the number of tombstones removed. Not scheduled by default; the
recommended retention is 90 days (a client offline longer resynchronises with
the complement fallback of vanished_uids/2, which is still correct).
Sets the domain's catch-all to mailbox_id (a mailbox id, or nil to turn it
off). The mailbox must be one of the domain's; {:error, :not_in_domain}
otherwise. A bad id is :not_in_domain too, so a caller cannot probe ids.
Sets (or clears with nil) the default quota for a domain.
Unseen visible message counts per folder: %{folder_id => count}.
Visible messages in folder without the \Seen flag, oldest first.
Creates or updates the settings of a domain (default_quota_bytes, retention_days).
Changes flags on a visible message. op is :add, :remove or :set.
Bumps the folder modseq only when the flag set actually changes.
Replaces the indexed fields of a visible message without giving it a new UID (used for a draft saved again in place). Bumps the folder modseq so IMAP clients see the change.
UIDs expunged from folder after since_modseq (QRESYNC VANISHED), ascending.
The UIDs of folder that vanished after since_modseq, for QRESYNC:
{:ok, uids} ascending.
Functions
Inserts a message hidden over quota: stored, but with no UID and no modseq.
Inserts a visible message into folder, allocating its UID and modseq.
attrs is a map of Message fields (without uid/modseq/state).
The domain's catch-all mailbox, or nil when the catch-all is off (or the mailbox it pointed at is gone).
Creates a domain.
Creates a folder with a fresh UIDVALIDITY.
Creates a mailbox on domain together with its default folders.
The default folders created with every mailbox, as {name, special_use}.
Expunges a message. A visible message leaves a tombstone and bumps the modseq; a held message (never visible to any client) is just marked expunged. The row is kept so the UID is never reused and the Karta delivery stays deduped.
Deleting visible mail frees quota: call Kantoor.Quota.release_held/1 afterwards.
A domain by its name (case-insensitive), or nil.
The settings row of a domain, or nil when none was saved yet.
Fetches a folder by exact name (e.g. "INBOX"). Returns nil when absent.
Fetches a folder by special-use role (e.g. "trash"). Returns nil when absent.
Looks a mailbox up by address. The address is normalised and matched by
equality, never with LIKE/ILIKE, so %, _ and * have no special meaning.
Returns nil for unknown or malformed addresses.
Fetches a message by Karta delivery id (the dedupe key).
@spec list_folders(Kantoor.Mailboxes.Mailbox.t()) :: [Kantoor.Mailboxes.Folder.t()]
Lists the folders of a mailbox.
Locks the mailbox row for the rest of the transaction and returns it fresh.
Makes a held message visible, assigning its UID and modseq now.
@spec move_message(Kantoor.Mailboxes.Message.t(), Kantoor.Mailboxes.Folder.t()) :: {:ok, Kantoor.Mailboxes.Message.t()} | {:error, :same_folder | :wrong_mailbox | :not_visible}
Moves a visible message to another folder of the same mailbox. IMAP MOVE
semantics: the message gets a NEW uid and modseq in target, and the old uid
is tombstoned in the source folder (never reused). The row (and so its Karta
ids and cached body) is kept. The two folder counters are advanced in id order
so concurrent opposite moves cannot deadlock.
@spec prune_tombstones(pos_integer()) :: non_neg_integer()
Forgets tombstones older than days days and records, per folder, the highest
modseq pruned (tombstone_floor) so QRESYNC can tell when it must fall back.
Returns the number of tombstones removed. Not scheduled by default; the
recommended retention is 90 days (a client offline longer resynchronises with
the complement fallback of vanished_uids/2, which is still correct).
Sets the domain's catch-all to mailbox_id (a mailbox id, or nil to turn it
off). The mailbox must be one of the domain's; {:error, :not_in_domain}
otherwise. A bad id is :not_in_domain too, so a caller cannot probe ids.
Sets (or clears with nil) the default quota for a domain.
@spec unseen_counts(Kantoor.Mailboxes.Mailbox.t()) :: %{ required(Ecto.UUID.t()) => non_neg_integer() }
Unseen visible message counts per folder: %{folder_id => count}.
@spec unseen_in_folder(Kantoor.Mailboxes.Folder.t()) :: [ Kantoor.Mailboxes.Message.t() ]
Visible messages in folder without the \Seen flag, oldest first.
Creates or updates the settings of a domain (default_quota_bytes, retention_days).
Changes flags on a visible message. op is :add, :remove or :set.
Bumps the folder modseq only when the flag set actually changes.
Replaces the indexed fields of a visible message without giving it a new UID (used for a draft saved again in place). Bumps the folder modseq so IMAP clients see the change.
UIDs expunged from folder after since_modseq (QRESYNC VANISHED), ascending.
@spec vanished_uids(Kantoor.Mailboxes.Folder.t(), non_neg_integer()) :: {:ok, [pos_integer()]}
The UIDs of folder that vanished after since_modseq, for QRESYNC:
{:ok, uids} ascending.
Normally these are the tombstones newer than since_modseq. When tombstones
newer than that have been pruned (since_modseq below the folder's
tombstone_floor) they can no longer answer exactly, so the result is every
UID the folder ever allocated (1..uid_next-1) that is not visible now. That
is a superset of what the client lost, never a subset: UIDs the client never
saw are harmless to report, and message rows are kept forever, so nothing is
missed.