JFLib

netpkt/pop3.rs#

Source: lib/netpkt/pop3.rs

pop3 -- the Post Office Protocol version 3 (RFC 1939) with its optional commands UIDL, TOP
and USER / PASS, and CAPA of RFC 2449: command lines, the status line +OK / -ERR, multi-line
responses with their dot-stuffing

  Command { verb, args }    Command::parse(line) (the verb in capitals), Command::new(verb,
                            args), to_line() ("RETR 1\r\n"; CR, LF and NUL of the fields left
                            out)
  Response { ok, text, body }   Response::ok(text) / err(text), with_body(bytes) (a multi-
                            line response: the lines of the bytes follow the status line,
                            each with a leading dot doubled, then the line with the dot);
                            to_bytes()
  multi_line(&Command)      a +OK to it carries a body: LIST and UIDL without an argument,
                            RETR, TOP, CAPA
  Reader                    the responses of a server from its bytes in any pieces; the
                            client says for each command it sent whether a body may follow
                            (expect(multi)); Reader::new() waits for the greeting first
  PORT_POP3 (110), verb_known

A body travels with CRLF line ends; Response::body holds it with the dots undone and CRLF
after every line (the message as the server stored it).  The text after +OK and -ERR is the
server's own words; RFC 2449's codes in brackets ("-ERR [AUTH] ...") stay part of it.
PORT_POP3: u16 = 110
  • fn verb_known(v: &str) -> bool the commands of RFC 1939 and 2449 (and those of later RFCs a dissector names)

  • struct Command a command line of a client

    • verb: String -- in capitals
    • args: Vec<String> -- the words after it (PASS keeps the rest of the line as one argument: a password may hold spaces)

impl Command

  • fn new(verb: &str, args: &[&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 arguments are left out (they would end the line and start another command)

  • fn num(&self, k: usize) -> Option<u32> argument k as a number (a message number)

  • fn multi_line(c: &Command) -> bool a +OK to this command is followed by lines and the line with the dot (RFC 1939 3)

  • struct Response a response of a server

    • ok: bool -- +OK (else -ERR)
    • text: String -- the text after the status indicator
    • body: Option<Vec<u8>> -- the lines of a multi-line response, CRLF after each, dots undone

impl Response

  • fn ok(text: &str) -> Response

  • fn err(text: &str) -> Response

  • fn with_body(mut self, body: &[u8]) -> Response the same response with lines after it

  • fn status_line(&self) -> String the status line ("+OK text") without the line end

  • fn to_bytes(&self) -> Vec<u8> the bytes on the wire: the status line, the body lines with dot-stuffing and ".\r\n"

  • fn stuffed(body: &[u8]) -> Vec<u8> a body as it travels: CRLF line ends, a line that starts with a dot gets another one, then ".\r\n"

  • struct Reader the responses of a server, as its bytes arrive

impl Reader

  • fn new() -> Reader a reader that waits for the greeting first (a single line)
  • fn expect(&mut self, multi: bool) the next response (after the greeting, which is single-line) answers a command whose +OK carries a body when multi
  • fn push(&mut self, b: &[u8]) -> Vec<Response> bytes of the server: the responses complete now

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