diff --git a/Cargo.toml b/Cargo.toml index b63c17a..dbc20f0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,9 +21,9 @@ serde_yaml = ">=0.8.21" serde_json = ">=1.0.59" [features] -default = ["multibytedecode", "serialize"] -multibytedecode = ["encoding_rs"] -serialize = ["serde", "serde_bytes"] +default = ["serde_support", "full_encoding"] +full_encoding = ["encoding_rs"] +serde_support = ["serde", "serde_bytes"] [profile.bench] debug = true diff --git a/README.md b/README.md index 18d9994..0369a17 100644 --- a/README.md +++ b/README.md @@ -4,8 +4,8 @@ _mail-parser_ is an **e-mail parsing library** written in Rust that fully confor Multipurpose Internet Mail Extensions (MIME; _RFC 2045 - 2049_) as well as other [internet messaging RFCs](#conformed-rfcs). It also supports decoding messages in [41 different character sets](#supported-character-sets) including obsolete formats such as UTF-7. -All Unicode (UTF-*) and single-byte character sets are handled internally by the library while support for Asian multi-byte encodings -such as BIG5 or ISO-2022-JP is provided by the optional dependency [encoding_rs](https://crates.io/crates/encoding_rs). +All Unicode (UTF-*) and single-byte character sets are handled internally by the library while support for legacy multi-byte encodings of Chinese +and Japanse languages such as BIG5 or ISO-2022-JP is provided by the optional dependency [encoding_rs](https://crates.io/crates/encoding_rs). In general, this library abides by the Postel's law or [Robustness Principle](https://en.wikipedia.org/wiki/Robustness_principle) which states that an implementation must be conservative in its sending behavior and liberal in its receiving behavior. This means that @@ -24,7 +24,7 @@ Performance and memory safety were two important factors while designing _mail-p place re-using the input buffer. - [Perfect hashing](https://en.wikipedia.org/wiki/Perfect_hash_function) is used for fast look-up of message header fields, character set names and aliases, HTML entities as well as month names while parsing _Date_ fields. -- Fully battle-tested with millions of real-world e-mail messages created from 1995 until today. +- Fully battle-tested with millions of real-world e-mail messages dating from 1995 until today. ## Usage Example @@ -183,19 +183,19 @@ Performance and memory safety were two important factors while designing _mail-p To run the testsuite: ```bash - $ cargo test + $ cargo test --all-features ``` or, to run the testsuite using MIRI: ```bash - $ cargo +nightly miri test + $ cargo +nightly miri test --all-features ``` and to run the benchmarks: ```bash - $ cargo +nightly bench + $ cargo +nightly bench --all-features ``` ## Conformed RFCs diff --git a/src/decoders/charsets/mod.rs b/src/decoders/charsets/mod.rs index 0f10ea2..706acd0 100644 --- a/src/decoders/charsets/mod.rs +++ b/src/decoders/charsets/mod.rs @@ -12,7 +12,6 @@ use std::borrow::Cow; pub mod map; -#[cfg(feature = "multibytedecode")] pub mod multi_byte; pub mod single_byte; pub mod utf; @@ -51,19 +50,19 @@ mod tests { ("utf-16", b"\xff\xfe\xe1\x00\xe9\x00\xed\x00\xf3\x00\xfa\x00".to_vec(),"áéíóú"), // Little endian ("utf-16", b"\xfe\xff\x00\xe1\x00\xe9\x00\xed\x00\xf3\x00\xfa".to_vec(),"áéíóú"), // Big endian - #[cfg(feature = "multibytedecode")] + #[cfg(feature = "full_encoding")] ("shift_jis", b"\x83n\x83\x8D\x81[\x81E\x83\x8F\x81[\x83\x8B\x83h".to_vec(),"ハロー・ワールド"), - #[cfg(feature = "multibytedecode")] + #[cfg(feature = "full_encoding")] ("big5", b"\xa7A\xa6n\xa1A\xa5@\xac\xc9".to_vec(),"你好,世界"), - #[cfg(feature = "multibytedecode")] + #[cfg(feature = "full_encoding")] ("euc-jp", b"\xa5\xcf\xa5\xed\xa1\xbc\xa1\xa6\xa5\xef\xa1\xbc\xa5\xeb\xa5\xc9".to_vec(),"ハロー・ワールド"), - #[cfg(feature = "multibytedecode")] + #[cfg(feature = "full_encoding")] ("euc-kr", b"\xbe\xc8\xb3\xe7\xc7\xcf\xbc\xbc\xbf\xe4 \xbc\xbc\xb0\xe8".to_vec(),"안녕하세요 세계"), - #[cfg(feature = "multibytedecode")] + #[cfg(feature = "full_encoding")] ("iso-2022-jp", b"\x1b$B%O%m! Cow { let mut result = String::with_capacity(bytes.len() * 3); if let (CoderResult::OutputFull, _, _) = decoder.decode_to_string(bytes, &mut result, true) { - debug_assert!(false, "String full while decoding") + debug_assert!(false, "String full while decoding.") } result.shrink_to_fit(); @@ -25,37 +30,109 @@ fn multi_byte_decoder(mut decoder: Decoder, bytes: &[u8]) -> Cow { } pub fn decoder_shift_jis(bytes: &[u8]) -> Cow { - multi_byte_decoder(SHIFT_JIS.new_decoder(), bytes) + #[cfg(feature = "full_encoding")] + { + multi_byte_decoder(SHIFT_JIS.new_decoder(), bytes) + } + + #[cfg(not(feature = "full_encoding"))] + { + decoder_default(bytes) + } } pub fn decoder_big5(bytes: &[u8]) -> Cow { - multi_byte_decoder(BIG5.new_decoder(), bytes) + #[cfg(feature = "full_encoding")] + { + multi_byte_decoder(BIG5.new_decoder(), bytes) + } + + #[cfg(not(feature = "full_encoding"))] + { + decoder_default(bytes) + } } pub fn decoder_euc_jp(bytes: &[u8]) -> Cow { - multi_byte_decoder(EUC_JP.new_decoder(), bytes) + #[cfg(feature = "full_encoding")] + { + multi_byte_decoder(EUC_JP.new_decoder(), bytes) + } + + #[cfg(not(feature = "full_encoding"))] + { + decoder_default(bytes) + } } pub fn decoder_euc_kr(bytes: &[u8]) -> Cow { - multi_byte_decoder(EUC_KR.new_decoder(), bytes) + #[cfg(feature = "full_encoding")] + { + multi_byte_decoder(EUC_KR.new_decoder(), bytes) + } + + #[cfg(not(feature = "full_encoding"))] + { + decoder_default(bytes) + } } pub fn decoder_gb18030(bytes: &[u8]) -> Cow { - multi_byte_decoder(GB18030.new_decoder(), bytes) + #[cfg(feature = "full_encoding")] + { + multi_byte_decoder(GB18030.new_decoder(), bytes) + } + + #[cfg(not(feature = "full_encoding"))] + { + decoder_default(bytes) + } } pub fn decoder_gbk(bytes: &[u8]) -> Cow { - multi_byte_decoder(GBK.new_decoder(), bytes) + #[cfg(feature = "full_encoding")] + { + multi_byte_decoder(GBK.new_decoder(), bytes) + } + + #[cfg(not(feature = "full_encoding"))] + { + decoder_default(bytes) + } } pub fn decoder_iso2022_jp(bytes: &[u8]) -> Cow { - multi_byte_decoder(ISO_2022_JP.new_decoder(), bytes) + #[cfg(feature = "full_encoding")] + { + multi_byte_decoder(ISO_2022_JP.new_decoder(), bytes) + } + + #[cfg(not(feature = "full_encoding"))] + { + decoder_default(bytes) + } } pub fn decoder_windows874(bytes: &[u8]) -> Cow { - multi_byte_decoder(WINDOWS_874.new_decoder(), bytes) + #[cfg(feature = "full_encoding")] + { + multi_byte_decoder(WINDOWS_874.new_decoder(), bytes) + } + + #[cfg(not(feature = "full_encoding"))] + { + decoder_default(bytes) + } } pub fn decoder_ibm866(bytes: &[u8]) -> Cow { - multi_byte_decoder(IBM866.new_decoder(), bytes) + #[cfg(feature = "full_encoding")] + { + multi_byte_decoder(IBM866.new_decoder(), bytes) + } + + #[cfg(not(feature = "full_encoding"))] + { + decoder_default(bytes) + } } diff --git a/src/decoders/encoded_word.rs b/src/decoders/encoded_word.rs index 09080d8..f7657c9 100644 --- a/src/decoders/encoded_word.rs +++ b/src/decoders/encoded_word.rs @@ -150,13 +150,13 @@ mod tests { "مرحبا بالعالم", true, ), - #[cfg(feature = "multibytedecode")] + #[cfg(feature = "full_encoding")] ( "?shift_jis?B?g26DjYFbgUWDj4Fbg4uDaA==?=".to_string(), "ハロー・ワールド", true, ), - #[cfg(feature = "multibytedecode")] + #[cfg(feature = "full_encoding")] ( "?iso-2022-jp?q?=1B$B%O%m!` or `Cow<[u8]>` references to the input raw message. +//! - Memory allocations are always avoided unless they are really necessary. In fact, all Base64 and Quoted-Printable parts are decoded in +//! place re-using the input buffer. +//! - [Perfect hashing](https://en.wikipedia.org/wiki/Perfect_hash_function) is used for fast look-up of message header fields, character +//! set names and aliases, HTML entities as well as month names while parsing _Date_ fields. +//! - Fully battle-tested with millions of real-world e-mail messages dating from 1995 until today. +//! +//! Jump to the [example](#usage-example). +//! +//! ## Conformed RFCs +//! +//! - [RFC 822 - Standard for ARPA Internet Text Messages](https://datatracker.ietf.org/doc/html/rfc822) +//! - [RFC 5322 - Internet Message Format](https://datatracker.ietf.org/doc/html/rfc5322) +//! - [RFC 2045 - Multipurpose Internet Mail Extensions (MIME) Part One: Format of Internet Message Bodies](https://datatracker.ietf.org/doc/html/rfc2045) +//! - [RFC 2046 - Multipurpose Internet Mail Extensions (MIME) Part Two: Media Types](https://datatracker.ietf.org/doc/html/rfc2046) +//! - [RFC 2047 - MIME (Multipurpose Internet Mail Extensions) Part Three: Message Header Extensions for Non-ASCII Text](https://datatracker.ietf.org/doc/html/rfc2047) +//! - [RFC 2048 - Multipurpose Internet Mail Extensions (MIME) Part Four: Registration Procedures](https://datatracker.ietf.org/doc/html/rfc2048) +//! - [RFC 2049 - Multipurpose Internet Mail Extensions (MIME) Part Five: Conformance Criteria and Examples](https://datatracker.ietf.org/doc/html/rfc2049) +//! - [RFC 2231 - MIME Parameter Value and Encoded Word Extensions: Character Sets, Languages, and Continuations](https://datatracker.ietf.org/doc/html/rfc2231) +//! - [RFC 2183 - Communicating Presentation Information in Internet Messages: The Content-Disposition Header Field](https://datatracker.ietf.org/doc/html/rfc2183) +//! - [RFC 6532 - Internationalized Email Headers](https://datatracker.ietf.org/doc/html/rfc6532) +//! - [RFC 2152 - UTF-7 - A Mail-Safe Transformation Format of Unicode](https://datatracker.ietf.org/doc/html/rfc2152) +//! - [RFC 2369 - The Use of URLs as Meta-Syntax for Core Mail List Commands and their Transport through Message Header Fields](https://datatracker.ietf.org/doc/html/rfc2369) +//! - [RFC 2919 - List-Id: A Structured Field and Namespace for the Identification of Mailing Lists](https://datatracker.ietf.org/doc/html/rfc2919) +//! - [RFC 8621 - The JSON Meta Application Protocol (JMAP) for Mail (Section 4.1.4)](https://datatracker.ietf.org/doc/html/rfc8621#section-4.1.4) +//! +//! ## Supported Character Sets +//! +//! - UTF-8 +//! - UTF-16, UTF-16BE, UTF-16LE +//! - UTF-7 +//! - US-ASCII +//! - ISO-8859-1 +//! - ISO-8859-2 +//! - ISO-8859-3 +//! - ISO-8859-4 +//! - ISO-8859-5 +//! - ISO-8859-6 +//! - ISO-8859-7 +//! - ISO-8859-8 +//! - ISO-8859-9 +//! - ISO-8859-10 +//! - ISO-8859-13 +//! - ISO-8859-14 +//! - ISO-8859-15 +//! - ISO-8859-16 +//! - CP1250 +//! - CP1251 +//! - CP1252 +//! - CP1253 +//! - CP1254 +//! - CP1255 +//! - CP1256 +//! - CP1257 +//! - CP1258 +//! - KOI8-R +//! - KOI8_U +//! - MACINTOSH +//! - IBM850 +//! - TIS-620 +//! +//! Supported character sets via the optional dependency [encoding_rs](https://crates.io/crates/encoding_rs): +//! +//! - SHIFT_JIS +//! - BIG5 +//! - EUC-JP +//! - EUC-KR +//! - GB18030 +//! - GBK +//! - ISO-2022-JP +//! - WINDOWS-874 +//! - IBM-866 +//! +//! ## Usage Example +//! +//! ``` +//! use mail_parser::*; +//! +//! let mut input = concat!( +//! "From: Art Vandelay (Vandelay Industries)\n", +//! "To: \"Colleagues\": \"James Smythe\" ; Friends:\n", +//! " jane@example.com, =?UTF-8?Q?John_Sm=C3=AEth?= ;\n", +//! "Date: Sat, 20 Nov 2021 14:22:01 -0800\n", +//! "Subject: Why not both importing AND exporting? =?utf-8?b?4pi6?=\n", +//! "Content-Type: multipart/mixed; boundary=\"festivus\";\n\n", +//! "--festivus\n", +//! "Content-Type: text/html; charset=\"us-ascii\"\n", +//! "Content-Transfer-Encoding: base64\n\n", +//! "PGh0bWw+PHA+SSB3YXMgdGhpbmtpbmcgYWJvdXQgcXVpdHRpbmcgdGhlICZsZHF1bztle\n", +//! "HBvcnRpbmcmcmRxdW87IHRvIGZvY3VzIGp1c3Qgb24gdGhlICZsZHF1bztpbXBvcnRpbm\n", +//! "cmcmRxdW87LDwvcD48cD5idXQgdGhlbiBJIHRob3VnaHQsIHdoeSBub3QgZG8gYm90aD8\n", +//! "gJiN4MjYzQTs8L3A+PC9odG1sPg==\n", +//! "--festivus\n", +//! "Content-Type: message/rfc822\n\n", +//! "From: \"Cosmo Kramer\" \n", +//! "Subject: Exporting my book about coffee tables\n", +//! "Content-Type: multipart/mixed; boundary=\"giddyup\";\n\n", +//! "--giddyup\n", +//! "Content-Type: text/plain; charset=\"utf-16\"\n", +//! "Content-Transfer-Encoding: quoted-printable\n\n", +//! "=FF=FE=0C!5=D8\"=DD5=D8)=DD5=D8-=DD =005=D8*=DD5=D8\"=DD =005=D8\"=\n", +//! "=DD5=D85=DD5=D8-=DD5=D8,=DD5=D8/=DD5=D81=DD =005=D8*=DD5=D86=DD =\n", +//! "=005=D8=1F=DD5=D8,=DD5=D8,=DD5=D8(=DD =005=D8-=DD5=D8)=DD5=D8\"=\n", +//! "=DD5=D8=1E=DD5=D80=DD5=D8\"=DD!=00\n", +//! "--giddyup\n", +//! "Content-Type: image/gif; name*1=\"about \"; name*0=\"Book \";\n", +//! " name*2*=utf-8''%e2%98%95 tables.gif\n", +//! "Content-Transfer-Encoding: Base64\n", +//! "Content-Disposition: attachment\n\n", +//! "R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7\n", +//! "--giddyup--\n", +//! "--festivus--\n", +//! ) +//! .as_bytes() +//! .to_vec(); +//! +//! let message = Message::parse(&mut input[..]); +//! +//! // Parses addresses (including comments), lists and groups +//! assert_eq!( +//! message.get_from(), +//! &Address::Address(Addr { +//! name: Some("Art Vandelay (Vandelay Industries)".into()), +//! address: Some("art@vandelay.com".into()) +//! }) +//! ); +//! assert_eq!( +//! message.get_to(), +//! &Address::GroupList(vec![ +//! Group { +//! name: Some("Colleagues".into()), +//! addresses: vec![Addr { +//! name: Some("James Smythe".into()), +//! address: Some("james@vandelay.com".into()) +//! }] +//! }, +//! Group { +//! name: Some("Friends".into()), +//! addresses: vec![ +//! Addr { +//! name: None, +//! address: Some("jane@example.com".into()) +//! }, +//! Addr { +//! name: Some("John Smîth".into()), +//! address: Some("john@example.com".into()) +//! } +//! ] +//! } +//! ]) +//! ); +//! +//! assert_eq!( +//! message.get_date().unwrap().to_iso8601(), +//! "2021-11-20T14:22:01-08:00" +//! ); +//! +//! // RFC2047 support for encoded text in message readers +//! assert_eq!( +//! message.get_subject().unwrap(), +//! "Why not both importing AND exporting? ☺" +//! ); +//! +//! // HTML and text body parts are returned conforming to RFC8621, Section 4.1.4 +//! assert_eq!( +//! message.get_html_body(0).unwrap().to_string(), +//! concat!( +//! "

I was thinking about quitting the “exporting” to ", +//! "focus just on the “importing”,

but then I thought,", +//! " why not do both? ☺

" +//! ) +//! ); +//! +//! // HTML parts are converted to plain text (and viceversa) when missing +//! assert_eq!( +//! message.get_text_body(0).unwrap().to_string(), +//! concat!( +//! "I was thinking about quitting the “exporting” to focus just on the", +//! " “importing”,\nbut then I thought, why not do both? ☺\n" +//! ) +//! ); +//! +//! // Supports nested messages as well as multipart/digest +//! let nested_message = match message.get_attachment(0).unwrap() { +//! MessagePart::Message(v) => v, +//! _ => unreachable!(), +//! }; +//! +//! assert_eq!( +//! nested_message.get_subject().unwrap(), +//! "Exporting my book about coffee tables" +//! ); +//! +//! // Handles UTF-* as well as many legacy encodings +//! assert_eq!( +//! nested_message.get_text_body(0).unwrap().to_string(), +//! "ℌ𝔢𝔩𝔭 𝔪𝔢 𝔢𝔵𝔭𝔬𝔯𝔱 𝔪𝔶 𝔟𝔬𝔬𝔨 𝔭𝔩𝔢𝔞𝔰𝔢!" +//! ); +//! assert_eq!( +//! nested_message.get_html_body(0).unwrap().to_string(), +//! "ℌ𝔢𝔩𝔭 𝔪𝔢 𝔢𝔵𝔭𝔬𝔯𝔱 𝔪𝔶 𝔟𝔬𝔬𝔨 𝔭𝔩𝔢𝔞𝔰𝔢!" +//! ); +//! +//! let nested_attachment = match nested_message.get_attachment(0).unwrap() { +//! MessagePart::Binary(v) => v, +//! _ => unreachable!(), +//! }; +//! +//! assert_eq!(nested_attachment.len(), 42); +//! +//! // Full RFC2231 support for continuations and character sets +//! assert_eq!( +//! nested_attachment +//! .get_header() +//! .unwrap() +//! .get_content_type() +//! .unwrap() +//! .get_attribute("name") +//! .unwrap(), +//! "Book about ☕ tables.gif" +//! ); +//! +//! // Integrates with Serde +//! println!("{}", serde_json::to_string_pretty(&message).unwrap()); +//! println!("{}", serde_yaml::to_string(&message).unwrap()); +//!``` + + mod decoders; mod parsers; use std::{borrow::Cow, collections::HashMap, fmt, slice::Iter}; +#[cfg(feature = "serde_support")] use serde::{Deserialize, Serialize}; -#[derive(Debug, Default, Serialize, Deserialize, PartialEq)] +/// An RFC5322/RFC822 message. +#[derive(Debug, Default, PartialEq)] +#[cfg_attr(feature = "serde_support", derive(Serialize, Deserialize))] pub struct Message<'x> { - #[serde(borrow)] + #[cfg_attr(feature = "serde_support", serde(borrow))] header: Box>, - #[serde(skip_serializing_if = "Vec::is_empty")] - #[serde(default)] + #[cfg_attr(feature = "serde_support", serde(skip_serializing_if = "Vec::is_empty"))] + #[cfg_attr(feature = "serde_support", serde(default))] html_body: Vec>, - #[serde(skip_serializing_if = "Vec::is_empty")] - #[serde(default)] + #[cfg_attr(feature = "serde_support", serde(skip_serializing_if = "Vec::is_empty"))] + #[cfg_attr(feature = "serde_support", serde(default))] text_body: Vec>, - #[serde(skip_serializing_if = "Vec::is_empty")] - #[serde(default)] + #[cfg_attr(feature = "serde_support", serde(skip_serializing_if = "Vec::is_empty"))] + #[cfg_attr(feature = "serde_support", serde(default))] attachments: Vec>, } -#[derive(Debug, Default, Serialize, Deserialize, PartialEq)] + +/// A text message part. +#[derive(Debug, Default, PartialEq)] +#[cfg_attr(feature = "serde_support", derive(Serialize, Deserialize))] pub struct TextPart<'x> { - #[serde(skip_serializing_if = "Option::is_none")] - #[serde(default)] + #[cfg_attr(feature = "serde_support", serde(skip_serializing_if = "Option::is_none"))] + #[cfg_attr(feature = "serde_support", serde(default))] header: Option>, contents: Cow<'x, str>, } -#[derive(Debug, Default, Serialize, Deserialize, PartialEq)] +/// A binary (`[u8]`) message part. +#[derive(Debug, Default, PartialEq)] +#[cfg_attr(feature = "serde_support", derive(Serialize, Deserialize))] pub struct BinaryPart<'x> { - #[serde(skip_serializing_if = "Option::is_none")] - #[serde(default)] + #[cfg_attr(feature = "serde_support", serde(skip_serializing_if = "Option::is_none"))] + #[cfg_attr(feature = "serde_support", serde(default))] header: Option>, - #[serde(with = "serde_bytes")] - #[serde(borrow)] + #[cfg_attr(feature = "serde_support", serde(with = "serde_bytes"))] + #[cfg_attr(feature = "serde_support", serde(borrow))] contents: Cow<'x, [u8]>, } -#[derive(Debug, Serialize, Deserialize, PartialEq)] -#[serde(untagged)] +#[doc(hidden)] +#[derive(Debug, PartialEq)] +#[cfg_attr(feature = "serde_support", derive(Serialize, Deserialize))] +#[cfg_attr(feature = "serde_support", serde(untagged))] pub enum InlinePart<'x> { Text(TextPart<'x>), InlineBinary(u32), } -#[derive(Debug, Serialize, Deserialize, PartialEq)] +/// A text, binary or nested e-mail MIME message part. +/// +/// - Text: Any text/* part +/// - Binary: Any other part type that is not text, usually attachments. +/// - InlineBinary: Same as the Binary variant but an inline part according to RFC 8621, Section 4.1.4 +/// - Message: A nested RFC5322 message. +/// +#[derive(Debug, PartialEq)] +#[cfg_attr(feature = "serde_support", derive(Serialize, Deserialize))] pub enum MessagePart<'x> { + /// Any text/* part Text(TextPart<'x>), - #[serde(borrow)] + + /// Any other part type that is not text, usually attachments. + #[cfg_attr(feature = "serde_support", serde(borrow))] Binary(BinaryPart<'x>), - #[serde(borrow)] + + /// Same as the Binary variant but an inline part according to RFC 8621, Section 4.1.4 + #[cfg_attr(feature = "serde_support", serde(borrow))] InlineBinary(BinaryPart<'x>), + + /// A nested RFC5322 message. Message(Message<'x>), } -#[derive(PartialEq, Debug, Default, Serialize, Deserialize)] +/// An RFC5322 message header. +#[derive(PartialEq, Debug, Default)] +#[cfg_attr(feature = "serde_support", derive(Serialize, Deserialize))] pub struct MessageHeader<'x> { - #[serde(skip_serializing_if = "Address::is_empty")] - #[serde(default)] + #[cfg_attr(feature = "serde_support", serde(skip_serializing_if = "Address::is_empty"))] + #[cfg_attr(feature = "serde_support", serde(default))] pub bcc: Address<'x>, - #[serde(skip_serializing_if = "Address::is_empty")] - #[serde(default)] + #[cfg_attr(feature = "serde_support", serde(skip_serializing_if = "Address::is_empty"))] + #[cfg_attr(feature = "serde_support", serde(default))] pub cc: Address<'x>, - #[serde(skip_serializing_if = "Option::is_none")] - #[serde(default)] + #[cfg_attr(feature = "serde_support", serde(skip_serializing_if = "Option::is_none"))] + #[cfg_attr(feature = "serde_support", serde(default))] pub comments: Option>>, - #[serde(skip_serializing_if = "Option::is_none")] - #[serde(default)] + #[cfg_attr(feature = "serde_support", serde(skip_serializing_if = "Option::is_none"))] + #[cfg_attr(feature = "serde_support", serde(default))] pub date: Option, - #[serde(skip_serializing_if = "Address::is_empty")] - #[serde(default)] + #[cfg_attr(feature = "serde_support", serde(skip_serializing_if = "Address::is_empty"))] + #[cfg_attr(feature = "serde_support", serde(default))] pub from: Address<'x>, - #[serde(skip_serializing_if = "Option::is_none")] - #[serde(default)] + #[cfg_attr(feature = "serde_support", serde(skip_serializing_if = "Option::is_none"))] + #[cfg_attr(feature = "serde_support", serde(default))] pub in_reply_to: Option>>, - #[serde(skip_serializing_if = "Option::is_none")] - #[serde(default)] + #[cfg_attr(feature = "serde_support", serde(skip_serializing_if = "Option::is_none"))] + #[cfg_attr(feature = "serde_support", serde(default))] pub keywords: Option>>, - #[serde(skip_serializing_if = "Address::is_empty")] - #[serde(default)] + #[cfg_attr(feature = "serde_support", serde(skip_serializing_if = "Address::is_empty"))] + #[cfg_attr(feature = "serde_support", serde(default))] pub list_archive: Address<'x>, - #[serde(skip_serializing_if = "Address::is_empty")] - #[serde(default)] + #[cfg_attr(feature = "serde_support", serde(skip_serializing_if = "Address::is_empty"))] + #[cfg_attr(feature = "serde_support", serde(default))] pub list_help: Address<'x>, - #[serde(skip_serializing_if = "Address::is_empty")] - #[serde(default)] + #[cfg_attr(feature = "serde_support", serde(skip_serializing_if = "Address::is_empty"))] + #[cfg_attr(feature = "serde_support", serde(default))] pub list_id: Address<'x>, - #[serde(skip_serializing_if = "Address::is_empty")] - #[serde(default)] + #[cfg_attr(feature = "serde_support", serde(skip_serializing_if = "Address::is_empty"))] + #[cfg_attr(feature = "serde_support", serde(default))] pub list_owner: Address<'x>, - #[serde(skip_serializing_if = "Address::is_empty")] - #[serde(default)] + #[cfg_attr(feature = "serde_support", serde(skip_serializing_if = "Address::is_empty"))] + #[cfg_attr(feature = "serde_support", serde(default))] pub list_post: Address<'x>, - #[serde(skip_serializing_if = "Address::is_empty")] - #[serde(default)] + #[cfg_attr(feature = "serde_support", serde(skip_serializing_if = "Address::is_empty"))] + #[cfg_attr(feature = "serde_support", serde(default))] pub list_subscribe: Address<'x>, - #[serde(skip_serializing_if = "Address::is_empty")] - #[serde(default)] + #[cfg_attr(feature = "serde_support", serde(skip_serializing_if = "Address::is_empty"))] + #[cfg_attr(feature = "serde_support", serde(default))] pub list_unsubscribe: Address<'x>, - #[serde(skip_serializing_if = "Option::is_none")] - #[serde(default)] + #[cfg_attr(feature = "serde_support", serde(skip_serializing_if = "Option::is_none"))] + #[cfg_attr(feature = "serde_support", serde(default))] pub message_id: Option>, - #[serde(skip_serializing_if = "Option::is_none")] - #[serde(default)] + #[cfg_attr(feature = "serde_support", serde(skip_serializing_if = "Option::is_none"))] + #[cfg_attr(feature = "serde_support", serde(default))] pub mime_version: Option>, - #[serde(skip_serializing_if = "Option::is_none")] - #[serde(default)] + #[cfg_attr(feature = "serde_support", serde(skip_serializing_if = "Option::is_none"))] + #[cfg_attr(feature = "serde_support", serde(default))] pub received: Option>>, - #[serde(skip_serializing_if = "Option::is_none")] - #[serde(default)] + #[cfg_attr(feature = "serde_support", serde(skip_serializing_if = "Option::is_none"))] + #[cfg_attr(feature = "serde_support", serde(default))] pub references: Option>>, - #[serde(skip_serializing_if = "Address::is_empty")] - #[serde(default)] + #[cfg_attr(feature = "serde_support", serde(skip_serializing_if = "Address::is_empty"))] + #[cfg_attr(feature = "serde_support", serde(default))] pub reply_to: Address<'x>, - #[serde(skip_serializing_if = "Address::is_empty")] - #[serde(default)] + #[cfg_attr(feature = "serde_support", serde(skip_serializing_if = "Address::is_empty"))] + #[cfg_attr(feature = "serde_support", serde(default))] pub resent_bcc: Address<'x>, - #[serde(skip_serializing_if = "Address::is_empty")] - #[serde(default)] + #[cfg_attr(feature = "serde_support", serde(skip_serializing_if = "Address::is_empty"))] + #[cfg_attr(feature = "serde_support", serde(default))] pub resent_cc: Address<'x>, - #[serde(skip_serializing_if = "Option::is_none")] - #[serde(default)] + #[cfg_attr(feature = "serde_support", serde(skip_serializing_if = "Option::is_none"))] + #[cfg_attr(feature = "serde_support", serde(default))] pub resent_date: Option>, - #[serde(skip_serializing_if = "Address::is_empty")] - #[serde(default)] + #[cfg_attr(feature = "serde_support", serde(skip_serializing_if = "Address::is_empty"))] + #[cfg_attr(feature = "serde_support", serde(default))] pub resent_from: Address<'x>, - #[serde(skip_serializing_if = "Option::is_none")] - #[serde(default)] + #[cfg_attr(feature = "serde_support", serde(skip_serializing_if = "Option::is_none"))] + #[cfg_attr(feature = "serde_support", serde(default))] pub resent_message_id: Option>>, - #[serde(skip_serializing_if = "Address::is_empty")] - #[serde(default)] + #[cfg_attr(feature = "serde_support", serde(skip_serializing_if = "Address::is_empty"))] + #[cfg_attr(feature = "serde_support", serde(default))] pub resent_sender: Address<'x>, - #[serde(skip_serializing_if = "Address::is_empty")] - #[serde(default)] + #[cfg_attr(feature = "serde_support", serde(skip_serializing_if = "Address::is_empty"))] + #[cfg_attr(feature = "serde_support", serde(default))] pub resent_to: Address<'x>, - #[serde(skip_serializing_if = "Option::is_none")] - #[serde(default)] + #[cfg_attr(feature = "serde_support", serde(skip_serializing_if = "Option::is_none"))] + #[cfg_attr(feature = "serde_support", serde(default))] pub return_path: Option>>, - #[serde(skip_serializing_if = "Address::is_empty")] - #[serde(default)] + #[cfg_attr(feature = "serde_support", serde(skip_serializing_if = "Address::is_empty"))] + #[cfg_attr(feature = "serde_support", serde(default))] pub sender: Address<'x>, - #[serde(skip_serializing_if = "Option::is_none")] - #[serde(default)] + #[cfg_attr(feature = "serde_support", serde(skip_serializing_if = "Option::is_none"))] + #[cfg_attr(feature = "serde_support", serde(default))] pub subject: Option>, - #[serde(skip_serializing_if = "Address::is_empty")] - #[serde(default)] + #[cfg_attr(feature = "serde_support", serde(skip_serializing_if = "Address::is_empty"))] + #[cfg_attr(feature = "serde_support", serde(default))] pub to: Address<'x>, - #[serde(skip_serializing_if = "Option::is_none")] - #[serde(default)] + #[cfg_attr(feature = "serde_support", serde(skip_serializing_if = "Option::is_none"))] + #[cfg_attr(feature = "serde_support", serde(default))] pub content_description: Option>, - #[serde(skip_serializing_if = "Option::is_none")] - #[serde(default)] + #[cfg_attr(feature = "serde_support", serde(skip_serializing_if = "Option::is_none"))] + #[cfg_attr(feature = "serde_support", serde(default))] pub content_disposition: Option>, - #[serde(skip_serializing_if = "Option::is_none")] - #[serde(default)] + #[cfg_attr(feature = "serde_support", serde(skip_serializing_if = "Option::is_none"))] + #[cfg_attr(feature = "serde_support", serde(default))] pub content_id: Option>, - #[serde(skip_serializing_if = "Option::is_none")] - #[serde(default)] + #[cfg_attr(feature = "serde_support", serde(skip_serializing_if = "Option::is_none"))] + #[cfg_attr(feature = "serde_support", serde(default))] pub content_transfer_encoding: Option>, - #[serde(skip_serializing_if = "Option::is_none")] - #[serde(default)] + #[cfg_attr(feature = "serde_support", serde(skip_serializing_if = "Option::is_none"))] + #[cfg_attr(feature = "serde_support", serde(default))] pub content_type: Option>, - #[serde(borrow)] - #[serde(skip_serializing_if = "HashMap::is_empty")] - #[serde(default)] + #[cfg_attr(feature = "serde_support", serde(borrow))] + #[cfg_attr(feature = "serde_support", serde(skip_serializing_if = "HashMap::is_empty"))] + #[cfg_attr(feature = "serde_support", serde(default))] pub others: HashMap<&'x str, Vec>>, } -#[derive(PartialEq, Debug, Default, Serialize, Deserialize)] +/// A MIME header. +#[derive(PartialEq, Debug, Default)] +#[cfg_attr(feature = "serde_support", derive(Serialize, Deserialize))] pub struct MimeHeader<'x> { - #[serde(skip_serializing_if = "Option::is_none")] - #[serde(default)] + #[cfg_attr(feature = "serde_support", serde(skip_serializing_if = "Option::is_none"))] + #[cfg_attr(feature = "serde_support", serde(default))] pub content_description: Option>, - #[serde(skip_serializing_if = "Option::is_none")] - #[serde(default)] + #[cfg_attr(feature = "serde_support", serde(skip_serializing_if = "Option::is_none"))] + #[cfg_attr(feature = "serde_support", serde(default))] pub content_disposition: Option>, - #[serde(skip_serializing_if = "Option::is_none")] - #[serde(default)] + #[cfg_attr(feature = "serde_support", serde(skip_serializing_if = "Option::is_none"))] + #[cfg_attr(feature = "serde_support", serde(default))] pub content_id: Option>, - #[serde(skip_serializing_if = "Option::is_none")] - #[serde(default)] + #[cfg_attr(feature = "serde_support", serde(skip_serializing_if = "Option::is_none"))] + #[cfg_attr(feature = "serde_support", serde(default))] pub content_transfer_encoding: Option>, - #[serde(skip_serializing_if = "Option::is_none")] - #[serde(default)] + #[cfg_attr(feature = "serde_support", serde(skip_serializing_if = "Option::is_none"))] + #[cfg_attr(feature = "serde_support", serde(default))] pub content_type: Option>, } -#[derive(Debug, PartialEq, Serialize, Deserialize)] +/// An RFC5322 or RFC2369 internet address. +#[derive(Debug, PartialEq)] +#[cfg_attr(feature = "serde_support", derive(Serialize, Deserialize))] pub struct Addr<'x> { - #[serde(skip_serializing_if = "Option::is_none")] - #[serde(default)] + /// The address name including comments + #[cfg_attr(feature = "serde_support", serde(skip_serializing_if = "Option::is_none"))] + #[cfg_attr(feature = "serde_support", serde(default))] pub name: Option>, - #[serde(skip_serializing_if = "Option::is_none")] - #[serde(default)] + + /// An e-mail address (RFC5322/RFC2369) or URL (RFC2369) + #[cfg_attr(feature = "serde_support", serde(skip_serializing_if = "Option::is_none"))] + #[cfg_attr(feature = "serde_support", serde(default))] pub address: Option>, } -#[derive(Debug, PartialEq, Serialize, Deserialize)] +/// An RFC5322 address group. +#[derive(Debug, PartialEq)] +#[cfg_attr(feature = "serde_support", derive(Serialize, Deserialize))] pub struct Group<'x> { - #[serde(skip_serializing_if = "Option::is_none")] - #[serde(default)] + /// Group name + #[cfg_attr(feature = "serde_support", serde(skip_serializing_if = "Option::is_none"))] + #[cfg_attr(feature = "serde_support", serde(default))] pub name: Option>, - #[serde(skip_serializing_if = "Vec::is_empty")] - #[serde(default)] + + /// Addressess member of the group + #[cfg_attr(feature = "serde_support", serde(skip_serializing_if = "Vec::is_empty"))] + #[cfg_attr(feature = "serde_support", serde(default))] pub addresses: Vec>, } -#[derive(Debug, PartialEq, Serialize, Deserialize)] +/// An RFC5322 or RFC2369 address field. +#[derive(Debug, PartialEq)] +#[cfg_attr(feature = "serde_support", derive(Serialize, Deserialize))] pub enum Address<'x> { + /// A single address Address(Addr<'x>), + + /// An address list AddressList(Vec>), + + /// A group of addresses Group(Group<'x>), + + /// A list containing two or more groups GroupList(Vec>), + + /// A collection of address fields, used for header fields + /// that might be present more than once in a message, + /// for example Resent-To Collection(Vec>), Empty, } -#[derive(Debug, PartialEq, Serialize, Deserialize)] +/// An RFC2047 Content-Type or RFC2183 Content-Disposition MIME header field. +#[derive(Debug, PartialEq)] +#[cfg_attr(feature = "serde_support", derive(Serialize, Deserialize))] pub struct ContentType<'x> { c_type: Cow<'x, str>, - #[serde(skip_serializing_if = "Option::is_none")] - #[serde(default)] + #[cfg_attr(feature = "serde_support", serde(skip_serializing_if = "Option::is_none"))] + #[cfg_attr(feature = "serde_support", serde(default))] c_subtype: Option>, - #[serde(skip_serializing_if = "Option::is_none")] - #[serde(default)] + #[cfg_attr(feature = "serde_support", serde(skip_serializing_if = "Option::is_none"))] + #[cfg_attr(feature = "serde_support", serde(default))] attributes: Option, Cow<'x, str>>>, } -#[derive(Debug, PartialEq, Serialize, Deserialize)] +/// An RFC5322 datetime. +#[derive(Debug, PartialEq)] +#[cfg_attr(feature = "serde_support", derive(Serialize, Deserialize))] pub struct DateTime { year: u32, month: u32, @@ -251,135 +556,172 @@ pub struct DateTime { tz_minute: u32, } +/// MIME header fields. pub trait MimeFieldGet<'x> { + /// Content-Description MIME header fn get_content_description(&self) -> Option<&Cow<'x, str>>; + /// Content-Disposition MIME header fn get_content_disposition(&self) -> Option<&ContentType<'x>>; + /// Content-ID MIME header fn get_content_id(&self) -> Option<&Cow<'x, str>>; + /// Content-Transfer-Encoding MIME header fn get_content_transfer_encoding(&self) -> Option<&Cow<'x, str>>; + /// Content-Type MIME header fn get_content_type(&self) -> Option<&ContentType<'x>>; } impl<'x> Message<'x> { + /// Returns the BCC header field pub fn get_bcc(&self) -> &Address<'x> { &self.header.bcc } + /// Returns the CC header field pub fn get_cc(&self) -> &Address<'x> { &self.header.cc } + /// Returns all Comments header fields pub fn get_comments(&self) -> Option<&Vec>> { self.header.comments.as_ref() } + /// Returns the Date header field pub fn get_date(&self) -> Option<&DateTime> { self.header.date.as_ref() } + /// Returns the From header field pub fn get_from(&self) -> &Address<'x> { &self.header.from } + /// Returns all In-Reply-To header fields pub fn get_in_reply_to(&self) -> Option<&Vec>> { self.header.in_reply_to.as_ref() } + /// Returns all Keywords header fields pub fn get_keywords(&self) -> Option<&Vec>> { self.header.keywords.as_ref() } + /// Returns the List-Archive header field pub fn get_list_archive(&self) -> &Address<'x> { &self.header.list_archive } + /// Returns the List-Help header field pub fn get_list_help(&self) -> &Address<'x> { &self.header.list_help } + /// Returns the List-ID header field pub fn get_list_id(&self) -> &Address<'x> { &self.header.list_id } + /// Returns the List-Owner header field pub fn get_list_owner(&self) -> &Address<'x> { &self.header.list_owner } + /// Returns the List-Port header field pub fn get_list_post(&self) -> &Address<'x> { &self.header.list_post } + /// Returns the List-Subscribe header field pub fn get_list_subscribe(&self) -> &Address<'x> { &self.header.list_subscribe } + /// Returns the List-Unsubscribe header field pub fn get_list_unsubscribe(&self) -> &Address<'x> { &self.header.list_unsubscribe } + /// Returns the Message-ID header field pub fn get_message_id(&self) -> Option<&Cow<'x, str>> { self.header.message_id.as_ref() } + /// Returns the MIME-Version header field pub fn get_mime_version(&self) -> Option<&Cow<'x, str>> { self.header.mime_version.as_ref() } + /// Returns all Received header fields pub fn get_received(&self) -> Option<&Vec>> { self.header.received.as_ref() } + /// Returns all References header fields pub fn get_references(&self) -> Option<&Vec>> { self.header.references.as_ref() } + /// Returns the Reply-To header field pub fn get_reply_to(&self) -> &Address<'x> { &self.header.reply_to } + /// Returns the Resent-BCC header field pub fn get_resent_bcc(&self) -> &Address<'x> { &self.header.bcc } + /// Returns the Resent-CC header field pub fn get_resent_cc(&self) -> &Address<'x> { &self.header.resent_to } + /// Returns all Resent-Date header fields pub fn get_resent_date(&self) -> Option<&Vec> { self.header.resent_date.as_ref() } + /// Returns the Resent-From header field pub fn get_resent_from(&self) -> &Address<'x> { &self.header.resent_from } + /// Returns all Resent-Message-ID header fields pub fn get_resent_message_id(&self) -> Option<&Vec>> { self.header.resent_message_id.as_ref() } + /// Returns the Sender header field pub fn get_resent_sender(&self) -> &Address<'x> { &self.header.resent_sender } + /// Returns the Resent-To header field pub fn get_resent_to(&self) -> &Address<'x> { &self.header.resent_to } + /// Returns all Return-Path header fields pub fn get_return_path(&self) -> Option<&Vec>> { self.header.return_path.as_ref() } + /// Returns the Sender header field pub fn get_sender(&self) -> &Address<'x> { &self.header.sender } + /// Returns the Subject header field pub fn get_subject(&self) -> Option<&Cow<'x, str>> { self.header.subject.as_ref() } + /// Returns the To header field pub fn get_to(&self) -> &Address<'x> { &self.header.to } + /// Returns any other message header not specified in RFCs 5322, 2045-2049, 2183, 2369 or 2919. pub fn get_header(&self, name: &str) -> Option<&Vec>> { self.header.others.get(name) } @@ -396,38 +738,47 @@ impl<'x> Message<'x> { } } + /// Returns an inline HTML body part by position pub fn get_html_body(&self, pos: usize) -> Option<&dyn BodyPart> { self.get_body_part(&self.html_body, pos) } + /// Returns an inline text body part by position pub fn get_text_body(&self, pos: usize) -> Option<&dyn BodyPart> { self.get_body_part(&self.text_body, pos) } + /// Returns an attacment by position pub fn get_attachment(&self, pos: usize) -> Option<&MessagePart<'x>> { self.attachments.get(pos) } + /// Returns the number of plain text body parts pub fn get_text_body_count(&self) -> usize { self.text_body.len() } + /// Returns the number of HTML body parts pub fn get_html_body_count(&self) -> usize { self.html_body.len() } + /// Returns the number of attachments pub fn get_attachment_count(&self) -> usize { self.attachments.len() } + /// Returns an Interator over the text body parts pub fn get_text_bodies(&'x self) -> BodyPartIterator<'x> { BodyPartIterator::new(self, &self.text_body) } + /// Returns an Interator over the HTML body parts pub fn get_html_bodies(&'x self) -> BodyPartIterator<'x> { BodyPartIterator::new(self, &self.html_body) } + /// Returns an Interator over the attachments pub fn get_attachments(&self) -> Iter> { self.attachments.iter() } @@ -499,13 +850,30 @@ impl<'x> MimeFieldGet<'x> for MessageHeader<'x> { } } +/// An inline Text or Binary body part. pub trait BodyPart<'x>: fmt::Display { + /// Returns the MIME Header of the part fn get_header(&self) -> Option<&MimeHeader<'x>>; + + /// Returns the body part's contents as a `u8` slice fn get_contents(&'x self) -> &'x [u8]; + + /// Returns the body part's contents as a `str` fn get_text_contents(&'x self) -> &'x str; + + /// Returns the body part's length fn len(&self) -> usize; + + /// Returns `true` when the body part MIME type is text/* fn is_text(&self) -> bool; + + /// Returns `true` when the part is not text fn is_binary(&self) -> bool; + + /// Returns `true` when the body part is empty + fn is_empty(&self) -> bool { + self.len() == 0 + } } impl<'x> BodyPart<'x> for TextPart<'x> { @@ -572,6 +940,7 @@ impl<'x> fmt::Display for BinaryPart<'x> { } } +#[doc(hidden)] pub struct BodyPartIterator<'x> { message: &'x Message<'x>, list: &'x [InlinePart<'x>], @@ -597,29 +966,36 @@ impl<'x> Iterator for BodyPartIterator<'x> { } } +/// An RFC2047 Content-Type or RFC2183 Content-Disposition MIME header field. impl<'x> ContentType<'x> { + /// Returns the type pub fn get_type(&'x self) -> &'x str { &self.c_type } + /// Returns the sub-type pub fn get_subtype(&'x self) -> Option<&'x str> { self.c_subtype.as_ref()?.as_ref().into() } + /// Returns an attribute by name pub fn get_attribute(&'x self, name: &str) -> Option<&'x str> { self.attributes.as_ref()?.get(name)?.as_ref().into() } + /// Returns `true` when the provided attribute name is present pub fn has_attribute(&'x self, name: &str) -> bool { self.attributes .as_ref() .map_or_else(|| false, |attr| attr.contains_key(name)) } + /// Returns ```true``` if the Content-Disposition type is "attachment" pub fn is_attachment(&'x self) -> bool { self.c_type.eq_ignore_ascii_case("attachment") } + /// Returns ```true``` if the Content-Disposition type is "inline" pub fn is_inline(&'x self) -> bool { self.c_type.eq_ignore_ascii_case("inline") } diff --git a/src/parsers/fields/date.rs b/src/parsers/fields/date.rs index d5db46a..4eda04a 100644 --- a/src/parsers/fields/date.rs +++ b/src/parsers/fields/date.rs @@ -14,6 +14,7 @@ use std::fmt; use crate::{parsers::message_stream::MessageStream, DateTime}; impl DateTime { + /// Returns an ISO-8601 representation of the parsed RFC5322 datetime field pub fn to_iso8601(&self) -> String { format!( "{:04}-{:02}-{:02}T{:02}:{:02}:{:02}{}{:02}:{:02}", diff --git a/src/parsers/message.rs b/src/parsers/message.rs index 58883db..61eced3 100644 --- a/src/parsers/message.rs +++ b/src/parsers/message.rs @@ -130,6 +130,15 @@ impl<'x> Message<'x> { } } + /// Parses a byte slice containing the RFC5322 raw message and returns a + /// `Message` struct. + /// + /// This function never panics, a best-effort is made to parse the message and + /// if no headers are found and empty `Message` struct is returned. + /// + /// Note: The `parse()` function requires a mutable `[u8]` as input in order + /// to perform BASE64/Quoted-Printable decoding in-place. + /// pub fn parse(bytes: &'x mut [u8]) -> Message<'x> { let stream = MessageStream::new(bytes);