Karta.SMTP.Session (Karta v1.0.0)

The SMTP server protocol as a pure state machine.

handle_line/2, data_received/2 and bdat_received/4 return {session, actions}, where actions are:

  • {:reply, Reply.t()} - send this reply
  • :start_tls - after flushing replies, discard buffered input and do the TLS handshake, then call tls_started/1
  • {:start_data, max} - read a DATA section with DataReader, then call data_received/2
  • {:start_bdat, size, last?} - read exactly size bytes, then call bdat_received/4
  • {:discard, size} - read exactly size bytes and drop them (a refused BDAT chunk)
  • :close - flush and close

Summary

Functions

config: hostname, listener (:mx | :submission | :submissions), tls_available, auth (:disabled | :tls_only), limits, and the opt-in extensions dsn and requiretls. Both default to off. Advertising DSN obliges Karta to honour the requests: it does for mail submitted to be delivered (the submission listeners enable it, and write the delivery status notifications), and not yet for mail it receives and passes on (the mx listener does not). Advertising REQUIRETLS (after STARTTLS only) obliges it to deliver such messages only over verified TLS to servers that support REQUIRETLS (RFC 8689), which outbound delivery does: the submission listeners may enable it, the mx listener not before mail it receives can be passed on. scram_nonce (a function returning a nonce; default Karta.SASL.SCRAM.nonce/0) exists for test vectors.

Records what the TLS handshake negotiated, %{version: "TLSv1.3", cipher: "TLS_AES_256_GCM_SHA384"}, for the Received: field. The connection calls it once the handshake is done.

Types

action()

@type action() ::
  {:reply, Karta.SMTP.Reply.t()}
  | :start_tls
  | {:start_data, pos_integer()}
  | {:start_bdat, non_neg_integer(), boolean()}
  | {:discard, non_neg_integer()}
  | :close

t()

@type t() :: %Karta.SMTP.Session{
  auth: term(),
  auth_failures: term(),
  config: term(),
  errors: term(),
  helo: term(),
  helo_valid: term(),
  peer: term(),
  phase: term(),
  policy: term(),
  rejections: term(),
  tls: term(),
  tls_info: term(),
  transaction: term()
}

Functions

bdat_received(s, size, last, body)

@spec bdat_received(t(), non_neg_integer(), boolean(), term()) :: {t(), [action()]}

data_received(s, map)

@spec data_received(t(), map()) :: {t(), [action()]}

greeting(s)

@spec greeting(t()) :: {t(), [action()]}

handle_line(s, arg)

@spec handle_line(t(), {:line, binary()} | {:bare_newline, binary()}) ::
  {t(), [action()]}

new(config, policy, peer)

@spec new(map(), module(), map()) :: t()

config: hostname, listener (:mx | :submission | :submissions), tls_available, auth (:disabled | :tls_only), limits, and the opt-in extensions dsn and requiretls. Both default to off. Advertising DSN obliges Karta to honour the requests: it does for mail submitted to be delivered (the submission listeners enable it, and write the delivery status notifications), and not yet for mail it receives and passes on (the mx listener does not). Advertising REQUIRETLS (after STARTTLS only) obliges it to deliver such messages only over verified TLS to servers that support REQUIRETLS (RFC 8689), which outbound delivery does: the submission listeners may enable it, the mx listener not before mail it receives can be passed on. scram_nonce (a function returning a nonce; default Karta.SASL.SCRAM.nonce/0) exists for test vectors.

put_tls_info(s, info)

@spec put_tls_info(t(), %{version: String.t(), cipher: String.t()}) :: t()

Records what the TLS handshake negotiated, %{version: "TLSv1.3", cipher: "TLS_AES_256_GCM_SHA384"}, for the Received: field. The connection calls it once the handshake is done.

tls_started(s)

@spec tls_started(t()) :: t()