JFLib

netpkt/smtp.rs#

Source: lib/netpkt/smtp.rs

smtp -- the Simple Mail Transfer Protocol (RFC 5321) with the service extensions of EHLO that
JFNetworker's servers speak: SIZE (RFC 1870), 8BITMIME (RFC 6152), PIPELINING (RFC 2920),
ENHANCEDSTATUSCODES (RFC 2034 with the codes of RFC 3463) and AUTH with PLAIN and LOGIN
(RFC 4954, 4616); no STARTTLS

  Command { verb, arg }     Command::parse(line) (the verb in capitals, the argument as
                            typed), Command::new(verb, arg), to_line() ("EHLO pc1\r\n"; CR, LF
                            and NUL of the fields left out)
  Path { addr, params }     parse_path(arg, "FROM" | "TO"): the address of MAIL FROM:<..> or
                            RCPT TO:<..> (empty for the null reverse path "<>") and the
                            parameters after it (SIZE=1234, BODY=8BITMIME); fmt_path (without
                            CR, LF, NUL, angle brackets in the address and blanks in parameters)
  Reply { code, lines }     Reply::new(code, text), Reply::enh(code, "2.1.0", text) (the
                            enhanced code before the text), to_bytes(): "250 text" or the
                            multi-line form "250-first" ... "250 last" of 4.2.1; text(),
                            status() (the enhanced code), is_positive / is_intermediate /
                            is_transient / is_permanent
  ReplyReader               the replies of a server from its bytes in any pieces
  Ehlo { domain, keywords } of_reply(&Reply): what a 250 to EHLO offers; has("PIPELINING"),
                            param("SIZE") -> "10240000", size_limit()
  Stream                    the server's side of a connection: push(bytes), line() while
                            commands come, begin_data(max) after 354, data() until the line
                            with the dot (dot-stuffing undone; beyond max the bytes are
                            counted but not kept)
  stuff(message)            the bytes after DATA: CRLF line ends, a dot doubled at the start
                            of a line (4.5.2), ".\r\n" at the end
  auth_plain(user, pass) / plain_of(b64)   the initial response of AUTH PLAIN and back
  LOGIN_USER / LOGIN_PASS   the base64 challenges of AUTH LOGIN ("Username:", "Password:")
  received(...)             the value of a Received field (4.4)
  PORT_SMTP (25), PORT_SUBMISSION (587, RFC 6409), verb_known

Lines end in CRLF; a server here also takes a lone LF.  Reply texts are the server's own
words: clients go by the codes (and the enhanced codes when ENHANCEDSTATUSCODES was offered).
PORT_SMTP: u16 = 25
/// message submission (RFC 6409): clients hand in their mail here, after AUTH
PORT_SUBMISSION: u16 = 587
/// the base64 challenges of AUTH LOGIN: "Username:" and "Password:"
LOGIN_USER: &str = "VXNlcm5hbWU6"
LOGIN_PASS: &str = "UGFzc3dvcmQ6"
  • fn verb_known(v: &str) -> bool the commands of RFC 5321 and the extensions a dissector names

  • struct Command a command line of a client

    • verb: String -- in capitals
    • arg: String -- everything after the first space

impl Command

  • fn new(verb: &str, arg: &str) -> Command

  • fn parse(line: &str) -> Option<Command> a line without its line end; None for an empty one

  • fn to_line(&self) -> String the line on the wire, CRLF included; CR, LF and NUL of verb and argument are left out (they would end the line and start another command)

  • struct Path the address and parameters of MAIL FROM or RCPT TO

    • addr: String -- without the angle brackets; empty: the null reverse path "<>"
    • params: Vec<(String, String)> -- "SIZE=1234" -> ("SIZE", "1234"), "BODY=8BITMIME"; keywords in capitals

impl Path

  • fn param(&self, k: &str) -> Option<&str> a parameter's value

  • fn size(&self) -> Option<u64> SIZE=n

  • fn parse_path(arg: &str, key: &str) -> Option<Path> "FROM:<anna@a.lab> SIZE=345" (the argument of MAIL; key "FROM", for RCPT "TO"): None when the keyword, the colon or the brackets are missing (a source route "<@a,@b:c@d>" keeps its mailbox, 4.1.2)

  • fn fmt_path(key: &str, addr: &str, params: &[(&str, &str)]) -> String "FROM:<addr> SIZE=n" with the parameters given; the address loses CR, LF, NUL and angle brackets, the parameters those and blanks, so that neither ends the path, adds a parameter or starts another command

  • struct Reply a reply of a server

    • code: u16
    • lines: Vec<String> -- the text of each line (without the code)

impl Reply

  • fn new(code: u16, text: &str) -> Reply

  • fn enh(code: u16, status: &str, text: &str) -> Reply a reply whose lines start with an enhanced status code ("2.1.0 Sender fine")

  • fn text(&self) -> &str the text of the first line (with its enhanced code)

  • fn joined(&self) -> String all lines as one text ("a b c")

  • fn status(&self) -> Option<&str> the enhanced status code at the start of the first line ("5.1.1")

  • fn is_positive(&self) -> bool 2yz: done

  • fn is_intermediate(&self) -> bool 3yz: go on (354 after DATA, 334 in AUTH)

  • fn is_transient(&self) -> bool 4yz: try again later

  • fn is_permanent(&self) -> bool 5yz: will not work

  • fn to_bytes(&self) -> Vec<u8> "code text" lines as they travel (4.2.1)

  • fn line(&self) -> String "550 5.1.1 text" as one line (logs, bounces)

  • struct ReplyReader the replies of a server, as its bytes arrive

impl ReplyReader

  • fn new() -> ReplyReader

  • fn push(&mut self, b: &[u8]) -> Vec<Reply> the replies that are complete now; a line that is no reply line is skipped

  • struct Ehlo what a server offers in its answer to EHLO (4.1.1.1)

    • domain: String -- the name it gives for itself
    • keywords: Vec<String> -- "SIZE 10240000", "8BITMIME", "AUTH PLAIN LOGIN" ...

impl Ehlo

  • fn of_reply(r: &Reply) -> Ehlo the 250 reply to EHLO: its first line names the server, every further line is a keyword with its parameters

  • fn has(&self, kw: &str) -> bool the server offers the keyword

  • fn param(&self, kw: &str) -> Option<&str> the parameters after a keyword ("SIZE" -> "10240000", "AUTH" -> "PLAIN LOGIN")

  • fn size_limit(&self) -> Option<u64> the size limit SIZE names (0: none given)

  • fn auth(&self, mech: &str) -> bool AUTH offers this mechanism

  • fn to_reply(&self, greeting: &str) -> Reply the reply a server sends: its name and greeting, then the keywords

  • struct Data a message after DATA

    • body: Vec<u8> -- the message with CRLF line ends, dot-stuffing undone (empty when it was too big)
    • size: u64 -- its size in bytes, also when it was too big
    • over: bool -- more than the limit came
  • struct Stream the server's side of a connection: command lines, then the message after 354

impl Stream

  • fn new() -> Stream

  • fn push(&mut self, b: &[u8])

  • fn pending(&self) -> usize bytes not taken yet

  • fn in_data(&self) -> bool a message is being read

  • fn line(&mut self) -> Option<String> the next command line (its line end removed); None until a line is complete or while a message is read

  • fn begin_data(&mut self, max: u64) the message starts after the 354; more than max bytes are counted, not kept (0: no limit)

  • fn data(&mut self) -> Option<Data> the message once its line with the dot came (the bytes after it stay for the next commands); None while it goes on

  • fn stuff(message: &[u8]) -> Vec<u8> the bytes after DATA: CRLF line ends, a dot doubled at the start of a line, the line with the dot at the end (4.5.2)

  • fn auth_plain(user: &str, pass: &str) -> String the initial response of AUTH PLAIN (RFC 4616): base64 of "\0user\0password"

  • fn plain_of(b64: &str) -> Option<(String, String)> the user and password of an AUTH PLAIN response (the authorization identity ignored)

  • fn received(helo: &str, peer: &str, by: &str, with: &str, id: &str, rcpt: Option<&str>, date: &str) -> String the value of a Received field (4.4): "from HELO (NAME [ADDR]) by HOST with PROTO id ID for <RCPT>; DATE"

Source: docs/API.md in the JFLib repository.