JFLib

netpkt/imf.rs#

Source: lib/netpkt/imf.rs

imf -- Internet messages (RFC 5322) with the part of MIME (RFC 2045, 2046, 2047) that plain
text mail needs: header fields with folding, addresses with display names, the Date form,
Message-ID, a text/plain body in UTF-8 as 7bit, 8bit or quoted-printable, multipart bodies
(a bounce's multipart/report of RFC 3464 and 6522) and base64

  Field { name, value }        one header field; the value unfolded (fold() writes it again)
  Message { fields, body }     parse(bytes) (CRLF or LF; the header ends at the first empty
                               line), get(name) / get_all / set / add / prepend / remove,
                               to_bytes() (CRLF, every field folded at 78 columns), text()
                               (the body as text: transfer encoding, charset and multipart
                               undone), subject() / from() / to() / cc() / date()
  Mailbox { name, addr }       parse_mailbox("Anna Muster <anna@a.lab>"), parse_list("a@x, B
                               <b@y>"), to_header() (a display name quoted or as an encoded
                               word); addr_ok(addr), split_addr(addr) -> (local, domain),
                               split_list(text): the parts between the commas, as typed
  params(value)                "text/plain; charset=UTF-8" -> ("text/plain", [("charset",
                               "UTF-8")]); param(value, name)
  date(unix) / parse_date(text) the Date form of 3.3 ("Thu, 01 Jan 2026 00:00:05 +0000"),
                               read back to seconds since 1970 (UTC, any zone it names)
  message_id(left, domain)     "<left@domain>"
  encode_word / decode_words   RFC 2047 encoded words (UTF-8, Q and B; ISO-8859-1 read too)
  qp_encode / qp_decode        quoted-printable of RFC 2045 6.7 (soft line breaks at 76)
  b64_encode / b64_lines / b64_decode   base64 of RFC 4648 (MIME lines of 76, padding)
  Draft { from, to, cc, subject, date, id, in_reply_to, references, text }
                               to_bytes(eight_bit): the message a mail program sends
                               (MIME-Version 1.0, text/plain in UTF-8; 7bit for ASCII, 8bit
                               when the server takes 8BITMIME, else quoted-printable)
  fold(name, value)            "Name: value\r\n" with continuation lines that start with a
                               space (2.2.3); CR and LF of a value become spaces, so no value
                               adds a field or ends the header

Lines of a header field are at most 78 characters where there is whitespace to break at
(2.1.1); a word longer than that stays whole.  Display names and subjects with characters
outside ASCII travel as encoded words, so a header stays 7-bit.  Addresses are not
normalised: the local part keeps its case, the domain is compared without it by the callers.
This is no full MIME implementation: nested multiparts are searched for their first text
part, other parts (attachments) are named but not decoded.
  • struct Field a header field

    • name: String
    • value: String -- unfolded, without the space after the colon
  • struct Message a message: its header fields in order and the body bytes

    • fields: Vec<Field>
    • body: Vec<u8>

impl Message

  • fn new() -> Message

  • fn parse(b: &[u8]) -> Message a message as it travels or lies in a mailbox: header lines (folded lines unfolded), the empty line, the body; a line without a colon in the header is left out

  • fn get(&self, name: &str) -> Option<&str> the first field of a name (any case)

  • fn get_all<'a>(&'a self, name: &'a str) -> impl Iterator<Item = &'a str> + 'a every field of a name, in order

  • fn set(&mut self, name: &str, value: &str) replaces the first field of a name (or adds it at the end)

  • fn add(&mut self, name: &str, value: &str)

  • fn prepend(&mut self, name: &str, value: &str) a field at the top (trace fields: Received, Return-Path)

  • fn remove(&mut self, name: &str)

  • fn header_bytes(&self) -> Vec<u8> the header as it travels (CRLF, folded), without the empty line; a field whose name is empty or holds other than printable characters but the colon is left out

  • fn to_bytes(&self) -> Vec<u8> header, empty line, body (the body as it is)

  • fn subject(&self) -> String the subject with its encoded words decoded

  • fn from(&self) -> Option<Mailbox> the first mailbox of From

  • fn to(&self) -> Vec<Mailbox>

  • fn cc(&self) -> Vec<Mailbox>

  • fn date(&self) -> Option<i64> the Date field in seconds since 1970

  • fn text(&self) -> String the body as text (line ends "\n"): the first text/plain part of a multipart, its transfer encoding and charset undone

  • fn split_multipart(text: &[u8], boundary: &str) -> Vec<Vec<u8>> the parts of a multipart body between its boundary lines (RFC 2046 5.1.1)

  • fn fold(name: &str, value: &str) -> String "Name: value\r\n" folded at whitespace so that no line is longer than 78 characters where that is possible. No text starts a field or the body of its own: the name keeps only its printable characters but the colon, CR and LF in the value become spaces, NUL is left out

  • fn unfold(s: &str) -> String removes the line ends of folded lines (CRLF or LF before whitespace)

  • fn params(value: &str) -> (String, Vec<(String, String)>) the value and parameters of a field like Content-Type: ("text/plain", [("charset", "UTF-8")]); names in lower case, quotes removed

  • fn param(value: &str, name: &str) -> Option<String> one parameter of such a field

  • fn split_list(s: &str) -> Vec<&str> the parts of an address list between its commas (not those inside quotes, angle brackets or comments), as typed

  • struct Mailbox an address with its display name (empty when there is none)

    • name: String
    • addr: String

impl Mailbox

  • fn new(name: &str, addr: &str) -> Mailbox

  • fn to_header(&self) -> String as a header writes it: "addr", "Name <addr>", "\"Muster, Anna\" <addr>" or an encoded word for a name outside ASCII

  • fn shown(&self) -> &str the name when there is one, else the address

  • fn parse_mailbox(s: &str) -> Option<Mailbox> a mailbox: "Anna <anna@a.lab>", "\"Muster, Anna\" <anna@a.lab>", "anna@a.lab", "anna@a.lab (Anna)"; None when no address can be read

  • fn parse_list(s: &str) -> Vec<Mailbox> the mailboxes of an address list ("a@x, B <b@y>"; a group "Team: a@x, b@y;" gives its members, "undisclosed-recipients:;" none)

  • fn addr_ok(a: &str) -> bool the address has the form local@domain: a dot-atom or a quoted local part, a domain of labels (letters, digits, hyphens) or an address literal in brackets

  • fn split_addr(a: &str) -> Option<(&str, &str)> (local part, domain) at the last '@'

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

  • fn parse_date(s: &str) -> Option<i64> the seconds since 1970 of a Date field: the day of the week is optional, the zone may be +hhmm / -hhmm or one of the obsolete names (UT, GMT, Z, EST ...)

  • fn message_id(left: &str, domain: &str) -> String a Message-ID: "<left@domain>"

  • fn encode_word(text: &str) -> String text for a header: as it is in ASCII, else encoded words "=?UTF-8?Q?...?=" of at most 75 characters each (separated by a space, a character never split)

  • fn decode_words(s: &str) -> String a header value with its encoded words decoded (whitespace between two encoded words goes)

  • fn qp_encode(text: &[u8]) -> Vec<u8> quoted-printable (RFC 2045 6.7): lines end in CRLF, a line longer than 76 breaks softly ("=" at its end), "=", control and 8-bit bytes as "=XX", spaces at a line end too

  • fn qp_decode(b: &[u8]) -> Vec<u8> undoes quoted-printable: "=XX" bytes, soft line breaks, whitespace at line ends

  • fn b64_encode(data: &[u8]) -> String base64 with padding, one line

  • fn b64_lines(data: &[u8]) -> String base64 in MIME lines of 76 characters with CRLF

  • fn b64_decode(s: &[u8]) -> Option<Vec<u8>> base64 back to bytes; whitespace and line ends are skipped, None for other characters

  • struct Draft what a mail program puts into a new message

    • from: Mailbox
    • to: Vec<Mailbox>
    • cc: Vec<Mailbox>
    • subject: String
    • date: i64 -- seconds since 1970
    • id: String -- the Message-ID with its angle brackets
    • in_reply_to: String -- the Message-ID of the message answered (empty: none)
    • references: String -- the References of a reply: the ids before, separated by spaces
    • text: String -- the text with "\n" line ends

impl Draft

  • fn to_bytes(&self, eight_bit: bool) -> Vec<u8> the message (CRLF): 7bit when the text is ASCII, else 8bit when the server takes 8BITMIME, else quoted-printable; lines over 998 bytes always go quoted-printable

  • fn crlf(b: &[u8]) -> Vec<u8> text with "\n" (or CRLF) line ends as CRLF lines

  • fn lf(b: &[u8]) -> Vec<u8> CRLF line ends as "\n" (a mailbox file keeps its messages that way)

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