JFLib

netpkt/imap.rs#

Source: lib/netpkt/imap.rs

imap -- IMAP without I/O (a subset of RFC 9051 and RFC 3501): command and response lines with
tags, quoted strings, literals and parenthesized lists, the readers of both directions, sequence
sets, flags, FETCH items, SEARCH keys and the ENVELOPE of a message

  PORT_IMAP (143), verb_known
  Item                      Atom | Quoted | Literal | List | Nil: one value of a line; text()
  Line { text, lits }       a whole logical line: the bytes with its literals apart
  LineReader                bytes in any pieces: push(b) then next() -> Some(Read::Line(line)) or
                            Some(Read::NeedLiteral(n)), a client that waits for "+" (a server
                            answers it)
  tokens(&Line) -> Vec<Item>   the values of a line
  Command { tag, verb, args }  Command::parse(&line) (the verb in capitals); line(tag, verb,
                            args) the bytes of a simple command (CR, LF, NUL left out but for
                            whole literals)
  Status, Kind, Response    Response::parse(&line): a tagged status, an untagged one, untagged
                            data, a continuation; response code, text, the values
  ok / no / bad / untagged / cont   server lines ("a1 OK done\r\n")
  astring(s) / quoted(s)    an atom when it can be, else quoted, a literal for other bytes
  SeqSet                    "1:3,5,*": parse, contains(n, max), to_text
  FetchItem, Section        parse_fetch(item): what FETCH asks (ALL, FAST, FULL too)
  SearchKey                 parse_search(args), matches through a closure of facts
  envelope(&Message)        the ENVELOPE of RFC 9051 7.5.2 as text
  internal_date(unix)       "01-Jan-2026 00:00:00 +0000"
  header_of(data) / text_of(data) / header_fields(data, names, not)   the parts FETCH names
  SEEN, ANSWERED, FLAGGED, DELETED, DRAFT, RECENT    the flags

A line ends with CRLF.  A literal {n} (or {n+}) at its end means n bytes follow the CRLF and the
line goes on after them; Line keeps those bytes in `lits` and marks their place in `text` with a
NUL, which IMAP text never has.  Words are not checked here beyond their shape: what a verb takes
is the server's business.  Strings a server sends in the body of its messages are literals.
PORT_IMAP: u16 = 143
SEEN: &str = "\\Seen"
ANSWERED: &str = "\\Answered"
FLAGGED: &str = "\\Flagged"
DELETED: &str = "\\Deleted"
DRAFT: &str = "\\Draft"
RECENT: &str = "\\Recent"
  • fn verb_known(v: &str) -> bool the commands of RFC 9051 and 3501 and of the extensions a dissector meets

  • enum Item one value of a line

    • Atom(String)
    • Quoted(String)
    • Literal(Vec<u8>)
    • List(Vec<Item>)
    • Nil

impl Item

  • fn text(&self) -> Option<String> the value as text (an atom, a string, a literal; a list or NIL has none)

  • fn atom(s: &str) -> Item

  • struct Line a whole logical line: its bytes (CRLF gone, NUL where a literal stands) and its literals

    • text: Vec<u8>
    • lits: Vec<Vec<u8>>

impl Line

  • fn plain(s: &str) -> Line a line without literals (tests, tools)

  • enum Read what a reader found

    • Line(Line)
    • NeedLiteral(usize) -- a literal of n bytes without "+" in its braces was announced: a server says "+ ..." now
  • struct LineReader splits bytes into logical lines

impl LineReader

  • fn new() -> LineReader

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

  • fn pending(&self) -> usize bytes it holds that make up no line yet

  • fn next(&mut self) -> Option<Read> the next thing that is complete

  • fn tokens_from(l: &Line, from: usize) -> Vec<Item> the values of a line, from from on

  • fn tokens(l: &Line) -> Vec<Item> the values of a whole line

  • struct Command a command: its tag, the verb in capitals, its values

    • tag: String
    • verb: String
    • args: Vec<Item>

impl Command

  • fn parse(l: &Line) -> Option<Command> a command line; None without a tag and a verb

  • fn arg(&self, k: usize) -> Option<String> the text of argument k

  • fn line(tag: &str, verb: &str, args: &str) -> Vec<u8> a simple command line of a client; CR, LF and NUL of tag, verb and arguments are left out (they would end the line and start another command), but for the line end after a literal's "{n}" ("{n+}") in the arguments and the n bytes of the literal after it (astring)

  • enum Status the status of a response

    • Ok
    • No
    • Bad
    • Bye
    • PreAuth

impl Status

  • fn word(self) -> &'static str

  • enum Kind what sort of line a response is

    • Tagged(String) -- "a1 OK ...": the end of a command
    • Untagged -- "* ..."
    • Cont -- "+ ...": the server waits for more
  • struct Response a response line

    • kind: Kind
    • status: Option<Status>
    • code: Option<String> -- the response code of a status line, without its brackets ("UIDNEXT 7")
    • text: String -- the text of a status line or a continuation
    • items: Vec<Item> -- the values of untagged data ("1", "FETCH", (...))

impl Response

  • fn parse(l: &Line) -> Option<Response>

  • fn name(&self) -> Option<&str> the data's name: the first atom after an optional number ("FETCH", "LIST", "EXISTS")

  • fn number(&self) -> Option<u32> the number before the name

  • fn ok(tag: &str, code: Option<&str>, text: &str) -> Vec<u8> a status line OK / NO / BAD of a command

  • fn no(tag: &str, code: Option<&str>, text: &str) -> Vec<u8>

  • fn bad(tag: &str, text: &str) -> Vec<u8>

  • fn untagged(text: &str) -> Vec<u8> "* text"

  • fn cont(text: &str) -> Vec<u8> "+ text"

  • fn quoted(s: &str) -> String a string in quotes (without CR, LF and NUL, which a quoted string cannot hold)

  • fn astring(s: &str) -> String an atom when it can be one, else a quoted string, else a literal (for other bytes)

  • struct SeqSet a set of message numbers or UIDs: "1:3,5,*"

impl SeqSet

  • fn parse(s: &str) -> Option<SeqSet>

  • fn contains(&self, n: u32, max: u32) -> bool n is in the set; max is what "*" stands for

  • fn to_text(&self) -> String

  • enum Section the part of a message BODY[...] names

    • Full
    • Header
    • Fields(Vec<String>, bool) -- HEADER.FIELDS (or .NOT, not)
    • Text
  • enum FetchItem what FETCH asks for

    • Flags
    • Uid
    • Rfc822Size
    • InternalDate
    • Envelope
    • Body { peek: bool, section: Section, name: String } -- BODY[...] or BODY.PEEK[...] (peek: \Seen stays as it is); the text shows it in the answer
    • Rfc822
    • Rfc822Header
    • Rfc822Text
  • fn parse_fetch(arg: &Item) -> Option<Vec<FetchItem>> the items of FETCH's argument: one atom or a list; None for one this does not know

  • enum SearchKey a SEARCH key

    • All
    • Seen
    • Unseen
    • Answered
    • Unanswered
    • Flagged
    • Unflagged
    • Deleted
    • Undeleted
    • Recent
    • From(String)
    • To(String)
    • Subject(String)
    • Body(String)
    • Text(String)
    • Larger(u64)
    • Smaller(u64)
    • Uid(SeqSet)
    • Seq(SeqSet)
    • Not(alloc::boxed::Box<SearchKey>)
    • Or(alloc::boxed::Box<SearchKey>, alloc::boxed::Box<SearchKey>)
    • And(Vec<SearchKey>)
  • fn envelope(m: &Message) -> String the ENVELOPE of a message as its text (RFC 9051 7.5.2): date, subject, from, sender, reply-to, to, cc, bcc, in-reply-to, message-id

  • fn internal_date(unix: i64) -> String "01-Jan-2026 00:00:00 +0000" of seconds since 1970 (UTC)

  • fn header_of(data: &[u8]) -> &[u8] the header of a message with its empty line

  • fn text_of(data: &[u8]) -> &[u8] the text after the header

  • fn header_fields(data: &[u8], names: &[String], not: bool) -> Vec<u8> the fields of the header with these names (not: all others), each with its continuation lines, and the empty line

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