mailio_adapter behaviour (mailio v0.2.0)

Protocol adapter behaviour.

Normalizes IMAP (now), JMAP and POP3 (later) into one callback contract the sync engine drives. All ids on this boundary are protocol-native opaque terms; the mapping to immutable JMAP email ids happens in the sync engine, which persists the IMAP uid to emailId map.

The key idea is that adapters are dumb: they translate protocol to these calls and report changes since an opaque cursor. All cursoring, dedup, and conflict logic lives in the sync engine, so a new adapter only has to implement these callbacks.

Summary

Types

change/0

-type change() ::
          {added,
           #{uid := term(),
             keywords := [binary()],
             internal_date => integer(),
             size => non_neg_integer(),
             uidvalidity => non_neg_integer()}} |
          {updated, #{uid := term(), keywords := [binary()]}} |
          {removed, term()} |
          reset | resync.

conn/0

-type conn() :: term().

fetch_sink/0

-type fetch_sink() :: fun((iodata()) -> ok).

remote_mailbox/0

-type remote_mailbox() ::
          #{id := binary(),
            name := binary(),
            role => atom() | undefined,
            parent_id => binary() | null,
            delimiter => binary(),
            attrs => [binary()]}.

remote_state/0

-type remote_state() :: binary() | initial.

Callbacks

append/4

(optional)
-callback append(conn(), MailboxId :: binary(), Raw :: binary(), Keywords :: [binary()]) ->
                    {ok, Uid :: term() | undefined} | {error, mailio_error:error_map()}.

capabilities/1

-callback capabilities(conn()) -> #{atom() => boolean()}.

close/1

-callback close(conn()) -> ok.

connect/1

-callback connect(AccountCfg :: map()) -> {ok, conn()} | {error, mailio_error:error_map()}.

expunge/3

(optional)
-callback expunge(conn(), MailboxId :: binary(), Uids :: [term()]) -> ok | {error, mailio_error:error_map()}.

fetch_changes_since/4

-callback fetch_changes_since(conn(), MailboxId :: binary(), remote_state(), Opts :: map()) ->
                                 {ok, [change()], remote_state(), more | done} |
                                 {error, mailio_error:error_map()}.

fetch_message/4

-callback fetch_message(conn(), MailboxId :: binary(), Uid :: term(), fetch_sink()) ->
                           {ok, #{size := non_neg_integer()}} | {error, mailio_error:error_map()}.

fetch_message/5

(optional)
-callback fetch_message(conn(), MailboxId :: binary(), Uid :: term(), fetch_sink(), Opts :: map()) ->
                           {ok, #{size := non_neg_integer()}} | {error, mailio_error:error_map()}.

list_mailboxes/1

-callback list_mailboxes(conn()) -> {ok, [remote_mailbox()]} | {error, mailio_error:error_map()}.

list_uids/2

(optional)
-callback list_uids(conn(), MailboxId :: binary()) -> {ok, [term()]} | {error, mailio_error:error_map()}.

list_uids/3

(optional)
-callback list_uids(conn(), MailboxId :: binary(), #{max => pos_integer()}) ->
                       {ok, [term()]} | {error, mailio_error:error_map()}.

mailbox_info/2

(optional)
-callback mailbox_info(conn(), MailboxId :: binary()) ->
                          {ok, #{exists := non_neg_integer(), uidvalidity := term()}} |
                          {error, mailio_error:error_map()}.

move/4

(optional)
-callback move(conn(), FromMailbox :: binary(), Uid :: term(), ToMailbox :: binary()) ->
                  {ok, NewUid :: term() | undefined} | {error, mailio_error:error_map()}.

set_keywords/5

-callback set_keywords(conn(),
                       MailboxId :: binary(),
                       Uid :: term(),
                       Add :: [binary()],
                       Remove :: [binary()]) ->
                          ok | {error, mailio_error:error_map()}.

state_uidvalidity/1

(optional)
-callback state_uidvalidity(remote_state()) -> term().

unwatch/2

(optional)
-callback unwatch(conn(), WatchRef :: term()) -> ok.

watch/3

(optional)
-callback watch(conn(), MailboxId :: binary(), Notify :: pid()) ->
                   {ok, WatchRef :: term()} | {error, not_supported | mailio_error:error_map()}.