Kantoor.MailSource behaviour (Kantoor v1.0.0)

What Kantoor needs from the system that stores and transports its mail, independent of that system's URLs. Kantoor.Karta.Client (and its implementation Kantoor.Karta.ReqClient) is the only implementation.

Identifiers are opaque strings the source chose: a message id names a stored message body (Kantoor keeps it in messages.karta_message_id), a delivery id names one recipient's copy of it (messages.karta_delivery_id, unique).

This behaviour covers the calls Kantoor makes. It does not cover how new mail reaches Kantoor: Karta pushes it (KantoorWeb.KartaInboundController, Kantoor.Karta.InboundPayload, Kantoor.Ingest). A source that has to be polled needs its own ingest path. See docs/mail-sources.md.

Summary

Types

A domain's route in the source: the domain's name and the endpoint it points at.

Callbacks

Makes the source deliver a domain's mail to this Kantoor (push sources only).

Forgets this Kantoor's copy of one delivery. Idempotent: an unknown delivery is :ok. Option :domain names the mail domain, for a source with one organisation per domain.

The message's raw RFC 822 bytes; option :range is {first, last} (inclusive byte offsets).

Reachability and version of the source.

The mail domains this Kantoor may serve.

One message's metadata.

Sends a complete message. Options: :mail_from, :rcpt_to (list), :retention ("keep" or a number of days), :auto_reply (send with the null envelope sender, for an automatic reply; a source that cannot falls back to :mail_from). Returns the source's answer; an "id" or "message_id" key, when present, names the stored sent copy.

Switches a domain's delivery to this Kantoor on or off (push sources only).

Types

error()

@type error() ::
  :not_configured
  | {:http, pos_integer()}
  | {:refused, pos_integer(), String.t(), String.t() | nil}
  | {:transport, Exception.t()}
  | term()

route()

@type route() :: %{domain: String.t(), endpoint_id: String.t()}

A domain's route in the source: the domain's name and the endpoint it points at.

Callbacks

attach_domain(domain, opts)

(optional)
@callback attach_domain(domain :: map(), opts :: keyword()) ::
  {:ok,
   %{
     :route_id => String.t() | nil,
     :endpoint_id => String.t() | nil,
     optional(:secret) => String.t() | nil
   }}
  | {:error, error()}

Makes the source deliver a domain's mail to this Kantoor (push sources only).

delete_delivery(delivery_id, opts)

@callback delete_delivery(delivery_id :: String.t(), opts :: keyword()) ::
  :ok | {:error, error()}

Forgets this Kantoor's copy of one delivery. Idempotent: an unknown delivery is :ok. Option :domain names the mail domain, for a source with one organisation per domain.

fetch_raw(message_id, opts)

@callback fetch_raw(message_id :: String.t(), opts :: keyword()) ::
  {:ok, binary()} | {:error, error()}

The message's raw RFC 822 bytes; option :range is {first, last} (inclusive byte offsets).

health()

@callback health() :: {:ok, map()} | {:error, error()}

Reachability and version of the source.

list_domains()

@callback list_domains() :: {:ok, [map()]} | {:error, error()}

The mail domains this Kantoor may serve.

message(message_id)

(optional)
@callback message(message_id :: String.t()) :: {:ok, map()} | {:error, error()}

One message's metadata.

send_raw(raw, opts)

@callback send_raw(raw :: binary(), opts :: keyword()) :: {:ok, map()} | {:error, error()}

Sends a complete message. Options: :mail_from, :rcpt_to (list), :retention ("keep" or a number of days), :auto_reply (send with the null envelope sender, for an automatic reply; a source that cannot falls back to :mail_from). Returns the source's answer; an "id" or "message_id" key, when present, names the stored sent copy.

set_route_mode(route, mode)

(optional)
@callback set_route_mode(route :: route(), mode :: :endpoint | :hold) ::
  :ok | {:error, error()}

Switches a domain's delivery to this Kantoor on or off (push sources only).