mailio_imap_conn (mailio v0.2.0)
IMAP connection FSM (gen_statem).
Owns one TCP/TLS socket and serializes commands over it. The core primitive is run/3: the caller supplies a builder that, given a freshly allocated tag, returns the command bytes (and optional literal payload to send after a + continuation). The FSM sends the command, accumulates untagged responses, and replies with them plus the matching tagged result. Response interpretation lives in mailio_imap so this module only deals with framing and command/response correlation.
States: connect -> greeting -> auth -> ready; running while a command is in flight; idle while in IDLE; backoff between reconnect attempts.
Summary
Functions
A child specification for a supervisor. A connection started with start_link/1 is linked to the process that started it and goes down with it; an application that keeps a connection for a long time starts it under its own supervisor with this. The connection handles its own reconnects, so it is restarted only when it crashes.
Enter IDLE; unsolicited events are sent to NotifyPid as {imap_idle_event, self(), Event}.
Run a command. Builder(Tag) -> {iodata(), Literal | undefined}.
Functions
-spec child_spec(map()) -> supervisor:child_spec().
A child specification for a supervisor. A connection started with start_link/1 is linked to the process that started it and goes down with it; an application that keeps a connection for a long time starts it under its own supervisor with this. The connection handles its own reconnects, so it is restarted only when it crashes.
A supervisor puts the start arguments in its reports, so they must not hold a secret, nor the arguments of a provider, which may be one (the bundled static provider keeps passwords there). Keep the credential under a name with mailio_credentials:register_ref/2 and give credentials => {ref, Name}: the connection looks it up inside its own worker each time it logs in. A token_fun is accepted too, as the fun shows no secret.
-spec close(pid()) -> ok.
Enter IDLE; unsolicited events are sent to NotifyPid as {imap_idle_event, self(), Event}.
-spec run(pid(), fun((binary()) -> {iodata(), binary() | undefined})) -> {ok, [term()], {atom(), term(), binary()}} | {error, term()}.
Run a command. Builder(Tag) -> {iodata(), Literal | undefined}.