Access headers as key/values #24
12 changed files with 423 additions and 339 deletions
102
src/header.rs
102
src/header.rs
|
@ -1,55 +1,62 @@
|
||||||
|
|
||||||
use crate::text::misc_token::{unstructured, Unstructured};
|
|
||||||
use crate::text::whitespace::{foldable_line, obs_crlf};
|
|
||||||
use nom::{
|
use nom::{
|
||||||
branch::alt,
|
branch::alt,
|
||||||
bytes::complete::{tag, tag_no_case, take_while1},
|
bytes::complete::{tag, take_while1},
|
||||||
character::complete::space0,
|
character::complete::space0,
|
||||||
combinator::map,
|
combinator::{into, recognize},
|
||||||
multi::{fold_many0},
|
multi::many0,
|
||||||
sequence::{pair, terminated, tuple},
|
sequence::{pair, terminated, tuple},
|
||||||
IResult,
|
IResult,
|
||||||
};
|
};
|
||||||
|
use std::fmt;
|
||||||
|
|
||||||
#[derive(Debug, PartialEq)]
|
use crate::text::misc_token::unstructured;
|
||||||
pub enum CompField<'a, T> {
|
use crate::text::whitespace::{foldable_line, obs_crlf};
|
||||||
Known(T),
|
|
||||||
Unknown(Kv<'a>),
|
#[derive(PartialEq, Clone)]
|
||||||
Bad(&'a [u8]),
|
pub struct Kv2<'a>(pub &'a [u8], pub &'a [u8]);
|
||||||
|
impl<'a> From<(&'a [u8], &'a [u8])> for Kv2<'a> {
|
||||||
|
fn from(pair: (&'a [u8], &'a [u8])) -> Self {
|
||||||
|
Self(pair.0, pair.1)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
impl<'a> fmt::Debug for Kv2<'a> {
|
||||||
#[derive(Debug, PartialEq, Clone)]
|
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
pub struct Kv<'a>(pub &'a [u8], pub Unstructured<'a>);
|
fmt.debug_tuple("header::Kv2")
|
||||||
|
.field(&String::from_utf8_lossy(self.0))
|
||||||
|
.field(&String::from_utf8_lossy(self.1))
|
||||||
pub fn header<'a, T>(
|
.finish()
|
||||||
fx: impl Fn(&'a [u8]) -> IResult<&'a [u8], T> + Copy,
|
|
||||||
) -> impl Fn(&'a [u8]) -> IResult<&'a [u8], (Vec::<T>, Vec::<Kv>, Vec<&'a [u8]>)> {
|
|
||||||
move |input| {
|
|
||||||
terminated(
|
|
||||||
fold_many0(
|
|
||||||
alt((
|
|
||||||
map(fx, CompField::Known),
|
|
||||||
map(opt_field, |(k, v)| CompField::Unknown(Kv(k, v))),
|
|
||||||
map(foldable_line, CompField::Bad),
|
|
||||||
)),
|
|
||||||
|| (Vec::<T>::new(), Vec::<Kv>::new(), Vec::<&'a [u8]>::new()),
|
|
||||||
|(mut known, mut unknown, mut bad), item| {
|
|
||||||
match item {
|
|
||||||
CompField::Known(v) => known.push(v),
|
|
||||||
CompField::Unknown(v) => unknown.push(v),
|
|
||||||
CompField::Bad(v) => bad.push(v),
|
|
||||||
};
|
|
||||||
(known, unknown, bad)
|
|
||||||
}
|
|
||||||
),
|
|
||||||
obs_crlf,
|
|
||||||
)(input)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn field_name<'a>(name: &'static [u8]) -> impl Fn(&'a [u8]) -> IResult<&'a [u8], &'a [u8]> {
|
#[derive(Debug, PartialEq, Clone)]
|
||||||
move |input| terminated(tag_no_case(name), tuple((space0, tag(b":"), space0)))(input)
|
pub enum Field<'a> {
|
||||||
|
Good(Kv2<'a>),
|
||||||
|
Bad(&'a [u8]),
|
||||||
|
}
|
||||||
|
impl<'a> From<Kv2<'a>> for Field<'a> {
|
||||||
|
fn from(kv: Kv2<'a>) -> Self {
|
||||||
|
Self::Good(kv)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl<'a> From<&'a [u8]> for Field<'a> {
|
||||||
|
fn from(bad: &'a [u8]) -> Self {
|
||||||
|
Self::Bad(bad)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Parse headers as key/values
|
||||||
|
pub fn header_kv(input: &[u8]) -> IResult<&[u8], Vec<Field>> {
|
||||||
|
terminated(
|
||||||
|
many0(alt((into(correct_field), into(foldable_line)))),
|
||||||
|
obs_crlf,
|
||||||
|
)(input)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn field_any(input: &[u8]) -> IResult<&[u8], &[u8]> {
|
||||||
|
terminated(
|
||||||
|
take_while1(|c| (0x21..=0x7E).contains(&c) && c != 0x3A),
|
||||||
|
tuple((space0, tag(b":"), space0)),
|
||||||
|
)(input)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Optional field
|
/// Optional field
|
||||||
|
@ -61,15 +68,6 @@ pub fn field_name<'a>(name: &'static [u8]) -> impl Fn(&'a [u8]) -> IResult<&'a [
|
||||||
/// %d59-126 ; characters not including
|
/// %d59-126 ; characters not including
|
||||||
/// ; ":".
|
/// ; ":".
|
||||||
/// ```
|
/// ```
|
||||||
pub fn opt_field(input: &[u8]) -> IResult<&[u8], (&[u8], Unstructured)> {
|
pub fn correct_field(input: &[u8]) -> IResult<&[u8], Kv2> {
|
||||||
terminated(
|
terminated(into(pair(field_any, recognize(unstructured))), obs_crlf)(input)
|
||||||
pair(
|
|
||||||
terminated(
|
|
||||||
take_while1(|c| (0x21..=0x7E).contains(&c) && c != 0x3A),
|
|
||||||
tuple((space0, tag(b":"), space0)),
|
|
||||||
),
|
|
||||||
unstructured,
|
|
||||||
),
|
|
||||||
obs_crlf,
|
|
||||||
)(input)
|
|
||||||
}
|
}
|
||||||
|
|
127
src/imf/field.rs
127
src/imf/field.rs
|
@ -1,21 +1,14 @@
|
||||||
use chrono::{DateTime, FixedOffset};
|
use chrono::{DateTime, FixedOffset};
|
||||||
use nom::{
|
use nom::combinator::map;
|
||||||
branch::alt,
|
|
||||||
combinator::map,
|
|
||||||
sequence::{preceded, terminated},
|
|
||||||
IResult,
|
|
||||||
};
|
|
||||||
|
|
||||||
use crate::header::{field_name, header};
|
use crate::header;
|
||||||
use crate::imf::address::{address_list, mailbox_list, nullable_address_list, AddressList};
|
use crate::imf::address::{address_list, mailbox_list, nullable_address_list, AddressList};
|
||||||
use crate::imf::datetime::section as date;
|
use crate::imf::datetime::section as date;
|
||||||
use crate::imf::identification::{msg_id, msg_list, MessageID, MessageIDList};
|
use crate::imf::identification::{msg_id, msg_list, MessageID, MessageIDList};
|
||||||
use crate::imf::mailbox::{mailbox, AddrSpec, MailboxList, MailboxRef};
|
use crate::imf::mailbox::{mailbox, AddrSpec, MailboxList, MailboxRef};
|
||||||
use crate::imf::mime::{version, Version};
|
use crate::imf::mime::{version, Version};
|
||||||
use crate::imf::trace::{received_log, return_path, ReceivedLog};
|
use crate::imf::trace::{received_log, return_path, ReceivedLog};
|
||||||
use crate::imf::Imf;
|
|
||||||
use crate::text::misc_token::{phrase_list, unstructured, PhraseList, Unstructured};
|
use crate::text::misc_token::{phrase_list, unstructured, PhraseList, Unstructured};
|
||||||
use crate::text::whitespace::obs_crlf;
|
|
||||||
|
|
||||||
#[derive(Debug, PartialEq)]
|
#[derive(Debug, PartialEq)]
|
||||||
pub enum Field<'a> {
|
pub enum Field<'a> {
|
||||||
|
@ -49,94 +42,34 @@ pub enum Field<'a> {
|
||||||
|
|
||||||
MIMEVersion(Version),
|
MIMEVersion(Version),
|
||||||
}
|
}
|
||||||
|
impl<'a> TryFrom<&header::Field<'a>> for Field<'a> {
|
||||||
pub fn field(input: &[u8]) -> IResult<&[u8], Field> {
|
type Error = ();
|
||||||
terminated(
|
fn try_from(f: &header::Field<'a>) -> Result<Self, Self::Error> {
|
||||||
alt((
|
let content = match f {
|
||||||
preceded(field_name(b"date"), map(date, Field::Date)),
|
header::Field::Good(header::Kv2(key, value)) => {
|
||||||
preceded(field_name(b"from"), map(mailbox_list, Field::From)),
|
match key.to_ascii_lowercase().as_slice() {
|
||||||
preceded(field_name(b"sender"), map(mailbox, Field::Sender)),
|
b"date" => map(date, Field::Date)(value),
|
||||||
preceded(field_name(b"reply-to"), map(address_list, Field::ReplyTo)),
|
b"from" => map(mailbox_list, Field::From)(value),
|
||||||
preceded(field_name(b"to"), map(address_list, Field::To)),
|
b"sender" => map(mailbox, Field::Sender)(value),
|
||||||
preceded(field_name(b"cc"), map(address_list, Field::Cc)),
|
b"reply-to" => map(address_list, Field::ReplyTo)(value),
|
||||||
preceded(field_name(b"bcc"), map(nullable_address_list, Field::Bcc)),
|
b"to" => map(address_list, Field::To)(value),
|
||||||
preceded(field_name(b"message-id"), map(msg_id, Field::MessageID)),
|
b"cc" => map(address_list, Field::Cc)(value),
|
||||||
preceded(field_name(b"in-reply-to"), map(msg_list, Field::InReplyTo)),
|
b"bcc" => map(nullable_address_list, Field::Bcc)(value),
|
||||||
preceded(field_name(b"references"), map(msg_list, Field::References)),
|
b"message-id" => map(msg_id, Field::MessageID)(value),
|
||||||
preceded(field_name(b"subject"), map(unstructured, Field::Subject)),
|
b"in-reply-to" => map(msg_list, Field::InReplyTo)(value),
|
||||||
preceded(field_name(b"comments"), map(unstructured, Field::Comments)),
|
b"references" => map(msg_list, Field::References)(value),
|
||||||
preceded(field_name(b"keywords"), map(phrase_list, Field::Keywords)),
|
b"subject" => map(unstructured, Field::Subject)(value),
|
||||||
preceded(
|
b"comments" => map(unstructured, Field::Comments)(value),
|
||||||
field_name(b"return-path"),
|
b"keywords" => map(phrase_list, Field::Keywords)(value),
|
||||||
map(return_path, Field::ReturnPath),
|
b"return-path" => map(return_path, Field::ReturnPath)(value),
|
||||||
),
|
b"received" => map(received_log, Field::Received)(value),
|
||||||
preceded(field_name(b"received"), map(received_log, Field::Received)),
|
b"mime-version" => map(version, Field::MIMEVersion)(value),
|
||||||
preceded(
|
_ => return Err(()),
|
||||||
field_name(b"mime-version"),
|
|
||||||
map(version, Field::MIMEVersion),
|
|
||||||
),
|
|
||||||
)),
|
|
||||||
obs_crlf,
|
|
||||||
)(input)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn imf(input: &[u8]) -> IResult<&[u8], Imf> {
|
|
||||||
map(header(field), |(known, unknown, bad)| {
|
|
||||||
let mut imf = Imf::from_iter(known);
|
|
||||||
imf.header_ext = unknown;
|
|
||||||
imf.header_bad = bad;
|
|
||||||
imf
|
|
||||||
})(input)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod tests {
|
|
||||||
use super::*;
|
|
||||||
use crate::imf::address::*;
|
|
||||||
use crate::imf::mailbox::*;
|
|
||||||
use crate::text::misc_token::*;
|
|
||||||
use chrono::{FixedOffset, TimeZone};
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_header() {
|
|
||||||
let fullmail = b"Date: 7 Mar 2023 08:00:00 +0200
|
|
||||||
From: someone@example.com
|
|
||||||
To: someone_else@example.com
|
|
||||||
Subject: An RFC 822 formatted message
|
|
||||||
|
|
||||||
This is the plain text body of the message. Note the blank line
|
|
||||||
between the header information and the body of the message.";
|
|
||||||
|
|
||||||
assert_eq!(
|
|
||||||
imf(fullmail),
|
|
||||||
Ok((
|
|
||||||
&b"This is the plain text body of the message. Note the blank line\nbetween the header information and the body of the message."[..],
|
|
||||||
Imf {
|
|
||||||
date: Some(FixedOffset::east_opt(2 * 3600).unwrap().with_ymd_and_hms(2023, 3, 7, 8, 0, 0).unwrap()),
|
|
||||||
from: vec![MailboxRef {
|
|
||||||
name: None,
|
|
||||||
addrspec: AddrSpec {
|
|
||||||
local_part: LocalPart(vec![LocalPartToken::Word(Word::Atom(&b"someone"[..]))]),
|
|
||||||
domain: Domain::Atoms(vec![&b"example"[..], &b"com"[..]]),
|
|
||||||
}
|
|
||||||
}],
|
|
||||||
to: vec![AddressRef::Single(MailboxRef {
|
|
||||||
name: None,
|
|
||||||
addrspec: AddrSpec {
|
|
||||||
local_part: LocalPart(vec![LocalPartToken::Word(Word::Atom(&b"someone_else"[..]))]),
|
|
||||||
domain: Domain::Atoms(vec![&b"example"[..], &b"com"[..]]),
|
|
||||||
}
|
|
||||||
})],
|
|
||||||
subject: Some(Unstructured(vec![
|
|
||||||
UnstrToken::Plain(&b"An"[..]),
|
|
||||||
UnstrToken::Plain(&b"RFC"[..]),
|
|
||||||
UnstrToken::Plain(&b"822"[..]),
|
|
||||||
UnstrToken::Plain(&b"formatted"[..]),
|
|
||||||
UnstrToken::Plain(&b"message"[..]),
|
|
||||||
])),
|
|
||||||
..Imf::default()
|
|
||||||
}
|
}
|
||||||
)),
|
}
|
||||||
)
|
_ => return Err(()),
|
||||||
|
};
|
||||||
|
|
||||||
|
content.map(|(_, content)| content).or(Err(()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
/// Parse and represent IMF (Internet Message Format) headers (RFC822, RFC5322)
|
/// Parse and represent IMF (Internet Message Format) headers (RFC822, RFC5322)
|
||||||
|
|
||||||
pub mod address;
|
pub mod address;
|
||||||
pub mod datetime;
|
pub mod datetime;
|
||||||
pub mod field;
|
pub mod field;
|
||||||
|
@ -8,13 +7,15 @@ pub mod mailbox;
|
||||||
pub mod mime;
|
pub mod mime;
|
||||||
pub mod trace;
|
pub mod trace;
|
||||||
|
|
||||||
|
use nom::{combinator::map, IResult};
|
||||||
|
|
||||||
|
use crate::header;
|
||||||
use crate::imf::address::AddressRef;
|
use crate::imf::address::AddressRef;
|
||||||
use crate::imf::field::Field;
|
use crate::imf::field::Field;
|
||||||
use crate::imf::identification::MessageID;
|
use crate::imf::identification::MessageID;
|
||||||
use crate::imf::mailbox::{AddrSpec, MailboxRef};
|
use crate::imf::mailbox::{AddrSpec, MailboxRef};
|
||||||
use crate::imf::mime::Version;
|
use crate::imf::mime::Version;
|
||||||
use crate::imf::trace::ReceivedLog;
|
use crate::imf::trace::ReceivedLog;
|
||||||
use crate::header;
|
|
||||||
use crate::text::misc_token::{PhraseList, Unstructured};
|
use crate::text::misc_token::{PhraseList, Unstructured};
|
||||||
use chrono::{DateTime, FixedOffset};
|
use chrono::{DateTime, FixedOffset};
|
||||||
|
|
||||||
|
@ -51,17 +52,13 @@ pub struct Imf<'a> {
|
||||||
// MIME
|
// MIME
|
||||||
pub mime_version: Option<Version>,
|
pub mime_version: Option<Version>,
|
||||||
|
|
||||||
// Junk
|
// Raw fields
|
||||||
pub header_ext: Vec<header::Kv<'a>>,
|
pub kv: Vec<header::Field<'a>>,
|
||||||
pub header_bad: Vec<&'a [u8]>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Imf<'a> {
|
impl<'a> Imf<'a> {
|
||||||
pub fn with_opt(mut self, opt: Vec<header::Kv<'a>>) -> Self {
|
pub fn with_kv(mut self, v: Vec<header::Field<'a>>) -> Self {
|
||||||
self.header_ext = opt; self
|
self.kv = v;
|
||||||
}
|
self
|
||||||
pub fn with_bad(mut self, bad: Vec<&'a [u8]>) -> Self {
|
|
||||||
self.header_bad = bad; self
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -92,3 +89,65 @@ impl<'a> FromIterator<Field<'a>> for Imf<'a> {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn imf(input: &[u8]) -> IResult<&[u8], Imf> {
|
||||||
|
map(header::header_kv, |fields| {
|
||||||
|
fields
|
||||||
|
.iter()
|
||||||
|
.flat_map(Field::try_from)
|
||||||
|
.into_iter()
|
||||||
|
.collect::<Imf>()
|
||||||
|
})(input)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
use crate::imf::address::*;
|
||||||
|
use crate::imf::mailbox::*;
|
||||||
|
use crate::text::misc_token::*;
|
||||||
|
use chrono::{FixedOffset, TimeZone};
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_header() {
|
||||||
|
let fullmail = b"Date: 7 Mar 2023 08:00:00 +0200
|
||||||
|
From: someone@example.com
|
||||||
|
To: someone_else@example.com
|
||||||
|
Subject: An RFC 822 formatted message
|
||||||
|
|
||||||
|
This is the plain text body of the message. Note the blank line
|
||||||
|
between the header information and the body of the message.";
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
imf(fullmail),
|
||||||
|
Ok((
|
||||||
|
&b"This is the plain text body of the message. Note the blank line\nbetween the header information and the body of the message."[..],
|
||||||
|
Imf {
|
||||||
|
date: Some(FixedOffset::east_opt(2 * 3600).unwrap().with_ymd_and_hms(2023, 3, 7, 8, 0, 0).unwrap()),
|
||||||
|
from: vec![MailboxRef {
|
||||||
|
name: None,
|
||||||
|
addrspec: AddrSpec {
|
||||||
|
local_part: LocalPart(vec![LocalPartToken::Word(Word::Atom(&b"someone"[..]))]),
|
||||||
|
domain: Domain::Atoms(vec![&b"example"[..], &b"com"[..]]),
|
||||||
|
}
|
||||||
|
}],
|
||||||
|
to: vec![AddressRef::Single(MailboxRef {
|
||||||
|
name: None,
|
||||||
|
addrspec: AddrSpec {
|
||||||
|
local_part: LocalPart(vec![LocalPartToken::Word(Word::Atom(&b"someone_else"[..]))]),
|
||||||
|
domain: Domain::Atoms(vec![&b"example"[..], &b"com"[..]]),
|
||||||
|
}
|
||||||
|
})],
|
||||||
|
subject: Some(Unstructured(vec![
|
||||||
|
UnstrToken::Plain(&b"An"[..]),
|
||||||
|
UnstrToken::Plain(&b"RFC"[..]),
|
||||||
|
UnstrToken::Plain(&b"822"[..]),
|
||||||
|
UnstrToken::Plain(&b"formatted"[..]),
|
||||||
|
UnstrToken::Plain(&b"message"[..]),
|
||||||
|
])),
|
||||||
|
..Imf::default()
|
||||||
|
}
|
||||||
|
)),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -18,7 +18,7 @@ pub mod text;
|
||||||
/// Manipulate buffer of bytes
|
/// Manipulate buffer of bytes
|
||||||
mod pointers;
|
mod pointers;
|
||||||
|
|
||||||
use nom::{IResult, combinator::into};
|
use nom::{combinator::into, IResult};
|
||||||
|
|
||||||
/// Parse a whole email including its (MIME) body
|
/// Parse a whole email including its (MIME) body
|
||||||
///
|
///
|
||||||
|
@ -57,7 +57,9 @@ use nom::{IResult, combinator::into};
|
||||||
/// );
|
/// );
|
||||||
/// ```
|
/// ```
|
||||||
pub fn parse_message(input: &[u8]) -> IResult<&[u8], part::composite::Message> {
|
pub fn parse_message(input: &[u8]) -> IResult<&[u8], part::composite::Message> {
|
||||||
into(part::composite::message(mime::MIME::<mime::r#type::DeductibleMessage>::default()))(input)
|
into(part::composite::message(mime::MIME::<
|
||||||
|
mime::r#type::DeductibleMessage,
|
||||||
|
>::default()))(input)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Only extract the headers of the email that are part of the Internet Message Format spec
|
/// Only extract the headers of the email that are part of the Internet Message Format spec
|
||||||
|
@ -98,5 +100,5 @@ pub fn parse_message(input: &[u8]) -> IResult<&[u8], part::composite::Message> {
|
||||||
/// );
|
/// );
|
||||||
/// ```
|
/// ```
|
||||||
pub fn parse_imf(input: &[u8]) -> IResult<&[u8], imf::Imf> {
|
pub fn parse_imf(input: &[u8]) -> IResult<&[u8], imf::Imf> {
|
||||||
imf::field::imf(input)
|
imf::imf(input)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,16 +1,10 @@
|
||||||
use nom::{
|
use nom::combinator::map;
|
||||||
branch::alt,
|
|
||||||
combinator::map,
|
|
||||||
sequence::{preceded, terminated},
|
|
||||||
IResult,
|
|
||||||
};
|
|
||||||
|
|
||||||
use crate::header::{field_name};
|
use crate::header;
|
||||||
use crate::imf::identification::{msg_id, MessageID};
|
use crate::imf::identification::{msg_id, MessageID};
|
||||||
use crate::mime::mechanism::{mechanism, Mechanism};
|
use crate::mime::mechanism::{mechanism, Mechanism};
|
||||||
use crate::mime::r#type::{naive_type, NaiveType};
|
use crate::mime::r#type::{naive_type, NaiveType};
|
||||||
use crate::text::misc_token::{unstructured, Unstructured};
|
use crate::text::misc_token::{unstructured, Unstructured};
|
||||||
use crate::text::whitespace::obs_crlf;
|
|
||||||
|
|
||||||
#[derive(Debug, PartialEq)]
|
#[derive(Debug, PartialEq)]
|
||||||
pub enum Content<'a> {
|
pub enum Content<'a> {
|
||||||
|
@ -47,38 +41,38 @@ impl<'a> Content<'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
impl<'a> TryFrom<&header::Field<'a>> for Content<'a> {
|
||||||
pub fn to_mime<'a, T: WithDefaultType>(list: Vec<Content<'a>>) -> AnyMIMEWithDefault<'a, T> {
|
type Error = ();
|
||||||
list.into_iter().collect::<AnyMIMEWithDefault<T>>()
|
fn try_from(f: &header::Field<'a>) -> Result<Self, Self::Error> {
|
||||||
}*/
|
let content = match f {
|
||||||
|
header::Field::Good(header::Kv2(key, value)) => match key
|
||||||
|
.to_ascii_lowercase()
|
||||||
|
.as_slice()
|
||||||
|
{
|
||||||
|
b"content-type" => map(naive_type, Content::Type)(value),
|
||||||
|
b"content-transfer-encoding" => map(mechanism, Content::TransferEncoding)(value),
|
||||||
|
b"content-id" => map(msg_id, Content::ID)(value),
|
||||||
|
b"content-description" => map(unstructured, Content::Description)(value),
|
||||||
|
_ => return Err(()),
|
||||||
|
},
|
||||||
|
_ => return Err(()),
|
||||||
|
};
|
||||||
|
|
||||||
pub fn content(input: &[u8]) -> IResult<&[u8], Content> {
|
//@TODO check that the full value is parsed, otherwise maybe log an error ?!
|
||||||
terminated(
|
content.map(|(_, content)| content).or(Err(()))
|
||||||
alt((
|
}
|
||||||
preceded(field_name(b"content-type"), map(naive_type, Content::Type)),
|
|
||||||
preceded(
|
|
||||||
field_name(b"content-transfer-encoding"),
|
|
||||||
map(mechanism, Content::TransferEncoding),
|
|
||||||
),
|
|
||||||
preceded(field_name(b"content-id"), map(msg_id, Content::ID)),
|
|
||||||
preceded(
|
|
||||||
field_name(b"content-description"),
|
|
||||||
map(unstructured, Content::Description),
|
|
||||||
),
|
|
||||||
)),
|
|
||||||
obs_crlf,
|
|
||||||
)(input)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
use crate::header::{header};
|
use crate::header;
|
||||||
use crate::mime::charset::EmailCharset;
|
//use crate::mime::charset::EmailCharset;
|
||||||
use crate::mime::r#type::*;
|
use crate::mime::r#type::*;
|
||||||
use crate::text::misc_token::MIMEWord;
|
use crate::text::misc_token::MIMEWord;
|
||||||
use crate::text::quoted::QuotedString;
|
use crate::text::quoted::QuotedString;
|
||||||
|
|
||||||
|
/*
|
||||||
#[test]
|
#[test]
|
||||||
fn test_content_type() {
|
fn test_content_type() {
|
||||||
let (rest, content) =
|
let (rest, content) =
|
||||||
|
@ -96,7 +90,7 @@ mod tests {
|
||||||
} else {
|
} else {
|
||||||
panic!("Expected Content::Type, got {:?}", content);
|
panic!("Expected Content::Type, got {:?}", content);
|
||||||
}
|
}
|
||||||
}
|
}*/
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_header() {
|
fn test_header() {
|
||||||
|
@ -116,7 +110,10 @@ This is a multipart message.
|
||||||
.as_bytes();
|
.as_bytes();
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
map(header(content), |(k, _, _)| k)(fullmail),
|
map(header::header_kv, |k| k
|
||||||
|
.iter()
|
||||||
|
.flat_map(Content::try_from)
|
||||||
|
.collect())(fullmail),
|
||||||
Ok((
|
Ok((
|
||||||
&b"This is a multipart message.\n\n"[..],
|
&b"This is a multipart message.\n\n"[..],
|
||||||
vec![
|
vec![
|
||||||
|
|
|
@ -10,19 +10,20 @@ pub mod mechanism;
|
||||||
/// Content-Type representation
|
/// Content-Type representation
|
||||||
pub mod r#type;
|
pub mod r#type;
|
||||||
|
|
||||||
|
use std::fmt;
|
||||||
use std::marker::PhantomData;
|
use std::marker::PhantomData;
|
||||||
|
|
||||||
|
use crate::header;
|
||||||
use crate::imf::identification::MessageID;
|
use crate::imf::identification::MessageID;
|
||||||
use crate::mime::field::Content;
|
use crate::mime::field::Content;
|
||||||
use crate::mime::mechanism::Mechanism;
|
use crate::mime::mechanism::Mechanism;
|
||||||
use crate::mime::r#type::{AnyType, NaiveType};
|
use crate::mime::r#type::{AnyType, NaiveType};
|
||||||
use crate::header;
|
|
||||||
use crate::text::misc_token::Unstructured; //Multipart, Message, Text, Binary};
|
use crate::text::misc_token::Unstructured; //Multipart, Message, Text, Binary};
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Clone)]
|
#[derive(Debug, PartialEq, Clone)]
|
||||||
pub struct MIME<'a, T> {
|
pub struct MIME<'a, T> {
|
||||||
pub interpreted_type: T,
|
pub interpreted_type: T,
|
||||||
pub fields: NaiveMIME<'a>
|
pub fields: NaiveMIME<'a>,
|
||||||
}
|
}
|
||||||
impl<'a> Default for MIME<'a, r#type::DeductibleText> {
|
impl<'a> Default for MIME<'a, r#type::DeductibleText> {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
|
@ -55,22 +56,32 @@ impl<'a, T: WithDefaultType> From<AnyMIMEWithDefault<'a, T>> for AnyMIME<'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Default, Clone)]
|
#[derive(PartialEq, Default, Clone)]
|
||||||
pub struct NaiveMIME<'a> {
|
pub struct NaiveMIME<'a> {
|
||||||
pub ctype: Option<NaiveType<'a>>,
|
pub ctype: Option<NaiveType<'a>>,
|
||||||
pub transfer_encoding: Mechanism<'a>,
|
pub transfer_encoding: Mechanism<'a>,
|
||||||
pub id: Option<MessageID<'a>>,
|
pub id: Option<MessageID<'a>>,
|
||||||
pub description: Option<Unstructured<'a>>,
|
pub description: Option<Unstructured<'a>>,
|
||||||
pub header_ext: Vec<header::Kv<'a>>,
|
pub kv: Vec<header::Field<'a>>,
|
||||||
pub header_bad: Vec<&'a [u8]>,
|
|
||||||
pub raw: &'a [u8],
|
pub raw: &'a [u8],
|
||||||
}
|
}
|
||||||
|
impl<'a> fmt::Debug for NaiveMIME<'a> {
|
||||||
|
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
fmt.debug_struct("NaiveMime")
|
||||||
|
.field("ctype", &self.ctype)
|
||||||
|
.field("transfer_encoding", &self.transfer_encoding)
|
||||||
|
.field("id", &self.id)
|
||||||
|
.field("description", &self.description)
|
||||||
|
.field("kv", &self.kv)
|
||||||
|
.field("raw", &String::from_utf8_lossy(self.raw))
|
||||||
|
.finish()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<'a> FromIterator<Content<'a>> for NaiveMIME<'a> {
|
impl<'a> FromIterator<Content<'a>> for NaiveMIME<'a> {
|
||||||
fn from_iter<I: IntoIterator<Item = Content<'a>>>(it: I) -> Self {
|
fn from_iter<I: IntoIterator<Item = Content<'a>>>(it: I) -> Self {
|
||||||
it.into_iter().fold(
|
it.into_iter()
|
||||||
NaiveMIME::default(),
|
.fold(NaiveMIME::default(), |mut section, field| {
|
||||||
|mut section, field| {
|
|
||||||
match field {
|
match field {
|
||||||
Content::Type(v) => section.ctype = Some(v),
|
Content::Type(v) => section.ctype = Some(v),
|
||||||
Content::TransferEncoding(v) => section.transfer_encoding = v,
|
Content::TransferEncoding(v) => section.transfer_encoding = v,
|
||||||
|
@ -78,28 +89,29 @@ impl<'a> FromIterator<Content<'a>> for NaiveMIME<'a> {
|
||||||
Content::Description(v) => section.description = Some(v),
|
Content::Description(v) => section.description = Some(v),
|
||||||
};
|
};
|
||||||
section
|
section
|
||||||
},
|
})
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> NaiveMIME<'a> {
|
impl<'a> NaiveMIME<'a> {
|
||||||
pub fn with_opt(mut self, opt: Vec<header::Kv<'a>>) -> Self {
|
pub fn with_kv(mut self, fields: Vec<header::Field<'a>>) -> Self {
|
||||||
self.header_ext = opt; self
|
self.kv = fields;
|
||||||
}
|
self
|
||||||
pub fn with_bad(mut self, bad: Vec<&'a [u8]>) -> Self {
|
|
||||||
self.header_bad = bad; self
|
|
||||||
}
|
}
|
||||||
pub fn with_raw(mut self, raw: &'a [u8]) -> Self {
|
pub fn with_raw(mut self, raw: &'a [u8]) -> Self {
|
||||||
self.raw = raw; self
|
self.raw = raw;
|
||||||
|
self
|
||||||
}
|
}
|
||||||
pub fn to_interpreted<T: WithDefaultType>(self) -> AnyMIME<'a> {
|
pub fn to_interpreted<T: WithDefaultType>(self) -> AnyMIME<'a> {
|
||||||
self.ctype.as_ref().map(|c| c.to_type()).unwrap_or(T::default_type()).to_mime(self).into()
|
self.ctype
|
||||||
|
.as_ref()
|
||||||
|
.map(|c| c.to_type())
|
||||||
|
.unwrap_or(T::default_type())
|
||||||
|
.to_mime(self)
|
||||||
|
.into()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
pub trait WithDefaultType {
|
pub trait WithDefaultType {
|
||||||
fn default_type() -> AnyType;
|
fn default_type() -> AnyType;
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,19 +5,29 @@ use nom::{
|
||||||
sequence::{preceded, terminated, tuple},
|
sequence::{preceded, terminated, tuple},
|
||||||
IResult,
|
IResult,
|
||||||
};
|
};
|
||||||
|
use std::fmt;
|
||||||
|
|
||||||
use crate::mime::charset::EmailCharset;
|
use crate::mime::charset::EmailCharset;
|
||||||
|
use crate::mime::{AnyMIME, NaiveMIME, MIME};
|
||||||
use crate::text::misc_token::{mime_word, MIMEWord};
|
use crate::text::misc_token::{mime_word, MIMEWord};
|
||||||
use crate::text::words::mime_atom;
|
use crate::text::words::mime_atom;
|
||||||
use crate::mime::{AnyMIME, MIME, NaiveMIME};
|
|
||||||
|
|
||||||
// --------- NAIVE TYPE
|
// --------- NAIVE TYPE
|
||||||
#[derive(Debug, PartialEq, Clone)]
|
#[derive(PartialEq, Clone)]
|
||||||
pub struct NaiveType<'a> {
|
pub struct NaiveType<'a> {
|
||||||
pub main: &'a [u8],
|
pub main: &'a [u8],
|
||||||
pub sub: &'a [u8],
|
pub sub: &'a [u8],
|
||||||
pub params: Vec<Parameter<'a>>,
|
pub params: Vec<Parameter<'a>>,
|
||||||
}
|
}
|
||||||
|
impl<'a> fmt::Debug for NaiveType<'a> {
|
||||||
|
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
fmt.debug_struct("mime::NaiveType")
|
||||||
|
.field("main", &String::from_utf8_lossy(self.main))
|
||||||
|
.field("sub", &String::from_utf8_lossy(self.sub))
|
||||||
|
.field("params", &self.params)
|
||||||
|
.finish()
|
||||||
|
}
|
||||||
|
}
|
||||||
impl<'a> NaiveType<'a> {
|
impl<'a> NaiveType<'a> {
|
||||||
pub fn to_type(&self) -> AnyType {
|
pub fn to_type(&self) -> AnyType {
|
||||||
self.into()
|
self.into()
|
||||||
|
@ -30,11 +40,20 @@ pub fn naive_type(input: &[u8]) -> IResult<&[u8], NaiveType> {
|
||||||
)(input)
|
)(input)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Clone)]
|
#[derive(PartialEq, Clone)]
|
||||||
pub struct Parameter<'a> {
|
pub struct Parameter<'a> {
|
||||||
pub name: &'a [u8],
|
pub name: &'a [u8],
|
||||||
pub value: MIMEWord<'a>,
|
pub value: MIMEWord<'a>,
|
||||||
}
|
}
|
||||||
|
impl<'a> fmt::Debug for Parameter<'a> {
|
||||||
|
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
fmt.debug_struct("mime::Parameter")
|
||||||
|
.field("name", &String::from_utf8_lossy(self.name))
|
||||||
|
.field("value", &self.value)
|
||||||
|
.finish()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn parameter(input: &[u8]) -> IResult<&[u8], Parameter> {
|
pub fn parameter(input: &[u8]) -> IResult<&[u8], Parameter> {
|
||||||
map(
|
map(
|
||||||
tuple((mime_atom, tag(b"="), mime_word)),
|
tuple((mime_atom, tag(b"="), mime_word)),
|
||||||
|
@ -73,16 +92,27 @@ impl<'a> From<&'a NaiveType<'a>> for AnyType {
|
||||||
|
|
||||||
impl<'a> AnyType {
|
impl<'a> AnyType {
|
||||||
pub fn to_mime(self, fields: NaiveMIME<'a>) -> AnyMIME<'a> {
|
pub fn to_mime(self, fields: NaiveMIME<'a>) -> AnyMIME<'a> {
|
||||||
match self {
|
match self {
|
||||||
Self::Multipart(interpreted_type) => AnyMIME::Mult(MIME::<Multipart> { interpreted_type, fields }),
|
Self::Multipart(interpreted_type) => AnyMIME::Mult(MIME::<Multipart> {
|
||||||
Self::Message(interpreted_type) => AnyMIME::Msg(MIME::<DeductibleMessage> { interpreted_type, fields }),
|
interpreted_type,
|
||||||
Self::Text(interpreted_type) => AnyMIME::Txt(MIME::<DeductibleText> { interpreted_type, fields }),
|
fields,
|
||||||
Self::Binary(interpreted_type) => AnyMIME::Bin(MIME::<Binary> { interpreted_type, fields }),
|
}),
|
||||||
|
Self::Message(interpreted_type) => AnyMIME::Msg(MIME::<DeductibleMessage> {
|
||||||
|
interpreted_type,
|
||||||
|
fields,
|
||||||
|
}),
|
||||||
|
Self::Text(interpreted_type) => AnyMIME::Txt(MIME::<DeductibleText> {
|
||||||
|
interpreted_type,
|
||||||
|
fields,
|
||||||
|
}),
|
||||||
|
Self::Binary(interpreted_type) => AnyMIME::Bin(MIME::<Binary> {
|
||||||
|
interpreted_type,
|
||||||
|
fields,
|
||||||
|
}),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Clone)]
|
#[derive(Debug, PartialEq, Clone)]
|
||||||
pub enum Deductible<T: Default> {
|
pub enum Deductible<T: Default> {
|
||||||
Inferred(T),
|
Inferred(T),
|
||||||
|
@ -139,7 +169,8 @@ impl ToString for MultipartSubtype {
|
||||||
Self::Parallel => "parallel",
|
Self::Parallel => "parallel",
|
||||||
Self::Report => "report",
|
Self::Report => "report",
|
||||||
Self::Unknown => "mixed",
|
Self::Unknown => "mixed",
|
||||||
}.into()
|
}
|
||||||
|
.into()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl<'a> From<&NaiveType<'a>> for MultipartSubtype {
|
impl<'a> From<&NaiveType<'a>> for MultipartSubtype {
|
||||||
|
@ -155,8 +186,6 @@ impl<'a> From<&NaiveType<'a>> for MultipartSubtype {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Default, Clone)]
|
#[derive(Debug, PartialEq, Default, Clone)]
|
||||||
pub enum MessageSubtype {
|
pub enum MessageSubtype {
|
||||||
#[default]
|
#[default]
|
||||||
|
@ -172,7 +201,8 @@ impl ToString for MessageSubtype {
|
||||||
Self::Partial => "partial",
|
Self::Partial => "partial",
|
||||||
Self::External => "external",
|
Self::External => "external",
|
||||||
Self::Unknown => "rfc822",
|
Self::Unknown => "rfc822",
|
||||||
}.into()
|
}
|
||||||
|
.into()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -184,17 +214,25 @@ pub struct Message {
|
||||||
impl<'a> From<&NaiveType<'a>> for Message {
|
impl<'a> From<&NaiveType<'a>> for Message {
|
||||||
fn from(nt: &NaiveType<'a>) -> Self {
|
fn from(nt: &NaiveType<'a>) -> Self {
|
||||||
match nt.sub.to_ascii_lowercase().as_slice() {
|
match nt.sub.to_ascii_lowercase().as_slice() {
|
||||||
b"rfc822" => Self { subtype: MessageSubtype::RFC822 },
|
b"rfc822" => Self {
|
||||||
b"partial" => Self { subtype: MessageSubtype::Partial },
|
subtype: MessageSubtype::RFC822,
|
||||||
b"external" => Self { subtype: MessageSubtype::External },
|
},
|
||||||
_ => Self { subtype: MessageSubtype::Unknown },
|
b"partial" => Self {
|
||||||
|
subtype: MessageSubtype::Partial,
|
||||||
|
},
|
||||||
|
b"external" => Self {
|
||||||
|
subtype: MessageSubtype::External,
|
||||||
|
},
|
||||||
|
_ => Self {
|
||||||
|
subtype: MessageSubtype::Unknown,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl From<Deductible<Message>> for Message {
|
impl From<Deductible<Message>> for Message {
|
||||||
fn from(d: Deductible<Message>) -> Self {
|
fn from(d: Deductible<Message>) -> Self {
|
||||||
match d {
|
match d {
|
||||||
Deductible::Inferred(t) | Deductible::Explicit(t) => t
|
Deductible::Inferred(t) | Deductible::Explicit(t) => t,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -221,7 +259,7 @@ impl<'a> From<&NaiveType<'a>> for Text {
|
||||||
impl From<Deductible<Text>> for Text {
|
impl From<Deductible<Text>> for Text {
|
||||||
fn from(d: Deductible<Text>) -> Self {
|
fn from(d: Deductible<Text>) -> Self {
|
||||||
match d {
|
match d {
|
||||||
Deductible::Inferred(t) | Deductible::Explicit(t) => t
|
Deductible::Inferred(t) | Deductible::Explicit(t) => t,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -238,7 +276,8 @@ impl ToString for TextSubtype {
|
||||||
match self {
|
match self {
|
||||||
Self::Plain | Self::Unknown => "plain",
|
Self::Plain | Self::Unknown => "plain",
|
||||||
Self::Html => "html",
|
Self::Html => "html",
|
||||||
}.into()
|
}
|
||||||
|
.into()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl<'a> From<&NaiveType<'a>> for TextSubtype {
|
impl<'a> From<&NaiveType<'a>> for TextSubtype {
|
||||||
|
@ -258,8 +297,8 @@ pub struct Binary {}
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
use crate::mime::charset::EmailCharset;
|
use crate::mime::charset::EmailCharset;
|
||||||
use crate::text::quoted::QuotedString;
|
|
||||||
use crate::mime::r#type::Deductible;
|
use crate::mime::r#type::Deductible;
|
||||||
|
use crate::text::quoted::QuotedString;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_parameter() {
|
fn test_parameter() {
|
||||||
|
@ -317,7 +356,12 @@ mod tests {
|
||||||
let (rest, nt) = naive_type(b"message/rfc822").unwrap();
|
let (rest, nt) = naive_type(b"message/rfc822").unwrap();
|
||||||
assert_eq!(rest, &[]);
|
assert_eq!(rest, &[]);
|
||||||
|
|
||||||
assert_eq!(nt.to_type(), AnyType::Message(Deductible::Explicit(Message { subtype: MessageSubtype::RFC822 })));
|
assert_eq!(
|
||||||
|
nt.to_type(),
|
||||||
|
AnyType::Message(Deductible::Explicit(Message {
|
||||||
|
subtype: MessageSubtype::RFC822
|
||||||
|
}))
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
|
@ -1,20 +1,37 @@
|
||||||
use nom::IResult;
|
use nom::IResult;
|
||||||
|
use std::fmt;
|
||||||
|
|
||||||
use crate::header::{header, self};
|
use crate::header;
|
||||||
use crate::imf;
|
use crate::imf;
|
||||||
use crate::mime;
|
use crate::mime;
|
||||||
use crate::part::{self, AnyPart, field::MixedField};
|
use crate::part::{self, AnyPart};
|
||||||
use crate::text::boundary::{boundary, Delimiter};
|
|
||||||
use crate::pointers;
|
use crate::pointers;
|
||||||
|
use crate::text::boundary::{boundary, Delimiter};
|
||||||
|
|
||||||
//--- Multipart
|
//--- Multipart
|
||||||
#[derive(Debug, PartialEq)]
|
#[derive(PartialEq)]
|
||||||
pub struct Multipart<'a> {
|
pub struct Multipart<'a> {
|
||||||
pub mime: mime::MIME<'a, mime::r#type::Multipart>,
|
pub mime: mime::MIME<'a, mime::r#type::Multipart>,
|
||||||
pub children: Vec<AnyPart<'a>>,
|
pub children: Vec<AnyPart<'a>>,
|
||||||
pub raw_part_inner: &'a [u8],
|
pub raw_part_inner: &'a [u8],
|
||||||
pub raw_part_outer: &'a [u8],
|
pub raw_part_outer: &'a [u8],
|
||||||
}
|
}
|
||||||
|
impl<'a> fmt::Debug for Multipart<'a> {
|
||||||
|
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
fmt.debug_struct("part::Multipart")
|
||||||
|
.field("mime", &self.mime)
|
||||||
|
.field("children", &self.children)
|
||||||
|
.field(
|
||||||
|
"raw_part_inner",
|
||||||
|
&String::from_utf8_lossy(self.raw_part_inner),
|
||||||
|
)
|
||||||
|
.field(
|
||||||
|
"raw_part_outer",
|
||||||
|
&String::from_utf8_lossy(self.raw_part_outer),
|
||||||
|
)
|
||||||
|
.finish()
|
||||||
|
}
|
||||||
|
}
|
||||||
impl<'a> Multipart<'a> {
|
impl<'a> Multipart<'a> {
|
||||||
pub fn preamble(&self) -> &'a [u8] {
|
pub fn preamble(&self) -> &'a [u8] {
|
||||||
pointers::parsed(self.raw_part_outer, self.raw_part_inner)
|
pointers::parsed(self.raw_part_outer, self.raw_part_inner)
|
||||||
|
@ -65,7 +82,10 @@ pub fn multipart<'a>(
|
||||||
mime: m.clone(),
|
mime: m.clone(),
|
||||||
children: mparts,
|
children: mparts,
|
||||||
raw_part_inner: pointers::parsed(inner_orig, inp),
|
raw_part_inner: pointers::parsed(inner_orig, inp),
|
||||||
raw_part_outer: pointers::parsed(outer_orig, &outer_orig[outer_orig.len()..]),
|
raw_part_outer: pointers::parsed(
|
||||||
|
outer_orig,
|
||||||
|
&outer_orig[outer_orig.len()..],
|
||||||
|
),
|
||||||
},
|
},
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
@ -73,24 +93,30 @@ pub fn multipart<'a>(
|
||||||
};
|
};
|
||||||
|
|
||||||
// parse mime headers, otherwise pick default mime
|
// parse mime headers, otherwise pick default mime
|
||||||
let (input, naive_mime) = match header(mime::field::content)(input) {
|
let (input, naive_mime) = match header::header_kv(input) {
|
||||||
Ok((input_eom, (known, unknown, bad))) => {
|
Ok((input_eom, fields)) => {
|
||||||
let raw_hdrs = pointers::parsed(input, input_eom);
|
let raw_hdrs = pointers::parsed(input, input_eom);
|
||||||
let mime = known
|
let mime = fields
|
||||||
|
.iter()
|
||||||
|
.flat_map(mime::field::Content::try_from)
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.collect::<mime::NaiveMIME>()
|
.collect::<mime::NaiveMIME>();
|
||||||
.with_opt(unknown)
|
|
||||||
.with_bad(bad)
|
let mime = mime.with_kv(fields).with_raw(raw_hdrs);
|
||||||
.with_raw(raw_hdrs);
|
|
||||||
(input_eom, mime)
|
(input_eom, mime)
|
||||||
},
|
}
|
||||||
Err(_) => (input, mime::NaiveMIME::default()),
|
Err(_) => (input, mime::NaiveMIME::default()),
|
||||||
};
|
};
|
||||||
|
|
||||||
// interpret mime according to context
|
// interpret mime according to context
|
||||||
let mime = match m.interpreted_type.subtype {
|
let mime = match m.interpreted_type.subtype {
|
||||||
mime::r#type::MultipartSubtype::Digest => naive_mime.to_interpreted::<mime::WithDigestDefault>().into(),
|
mime::r#type::MultipartSubtype::Digest => naive_mime
|
||||||
_ => naive_mime.to_interpreted::<mime::WithGenericDefault>().into(),
|
.to_interpreted::<mime::WithDigestDefault>()
|
||||||
|
.into(),
|
||||||
|
_ => naive_mime
|
||||||
|
.to_interpreted::<mime::WithGenericDefault>()
|
||||||
|
.into(),
|
||||||
};
|
};
|
||||||
|
|
||||||
// parse raw part
|
// parse raw part
|
||||||
|
@ -109,7 +135,7 @@ pub fn multipart<'a>(
|
||||||
|
|
||||||
//--- Message
|
//--- Message
|
||||||
|
|
||||||
#[derive(Debug, PartialEq)]
|
#[derive(PartialEq)]
|
||||||
pub struct Message<'a> {
|
pub struct Message<'a> {
|
||||||
pub mime: mime::MIME<'a, mime::r#type::DeductibleMessage>,
|
pub mime: mime::MIME<'a, mime::r#type::DeductibleMessage>,
|
||||||
pub imf: imf::Imf<'a>,
|
pub imf: imf::Imf<'a>,
|
||||||
|
@ -119,6 +145,18 @@ pub struct Message<'a> {
|
||||||
pub raw_headers: &'a [u8],
|
pub raw_headers: &'a [u8],
|
||||||
pub raw_body: &'a [u8],
|
pub raw_body: &'a [u8],
|
||||||
}
|
}
|
||||||
|
impl<'a> fmt::Debug for Message<'a> {
|
||||||
|
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
fmt.debug_struct("part::Message")
|
||||||
|
.field("mime", &self.mime)
|
||||||
|
.field("imf", &self.imf)
|
||||||
|
.field("child", &self.child)
|
||||||
|
.field("raw_part", &String::from_utf8_lossy(self.raw_part))
|
||||||
|
.field("raw_headers", &String::from_utf8_lossy(self.raw_headers))
|
||||||
|
.field("raw_body", &String::from_utf8_lossy(self.raw_body))
|
||||||
|
.finish()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn message<'a>(
|
pub fn message<'a>(
|
||||||
m: mime::MIME<'a, mime::r#type::DeductibleMessage>,
|
m: mime::MIME<'a, mime::r#type::DeductibleMessage>,
|
||||||
|
@ -127,23 +165,27 @@ pub fn message<'a>(
|
||||||
let orig = input;
|
let orig = input;
|
||||||
|
|
||||||
// parse header fields
|
// parse header fields
|
||||||
let (input, (known, unknown, bad)): (_, (Vec::<MixedField>, Vec<header::Kv>, Vec<&[u8]>)) =
|
let (input, headers) = header::header_kv(input)?;
|
||||||
header(part::field::mixed_field)(input)?;
|
|
||||||
|
|
||||||
// extract raw parts 1/2
|
// extract raw parts 1/2
|
||||||
let raw_headers = pointers::parsed(orig, input);
|
let raw_headers = pointers::parsed(orig, input);
|
||||||
let body_orig = input;
|
let body_orig = input;
|
||||||
|
|
||||||
|
//---------------
|
||||||
// aggregate header fields
|
// aggregate header fields
|
||||||
let (naive_mime, imf) = part::field::sections(known);
|
let (naive_mime, imf) = part::field::split_and_build(&headers);
|
||||||
|
|
||||||
// attach bad headers to imf
|
// Bind headers to the IMF object
|
||||||
let imf = imf.with_opt(unknown).with_bad(bad);
|
let imf = imf.with_kv(headers);
|
||||||
|
|
||||||
// interpret headers to choose a mime type
|
// interpret headers to choose a mime type
|
||||||
let in_mime = naive_mime.with_raw(raw_headers).to_interpreted::<mime::WithGenericDefault>().into();
|
let in_mime = naive_mime
|
||||||
|
.with_raw(raw_headers)
|
||||||
|
.to_interpreted::<mime::WithGenericDefault>()
|
||||||
|
.into();
|
||||||
|
//---------------
|
||||||
|
|
||||||
// parse this mimetype
|
// parse a part following this mime specification
|
||||||
let (input, part) = part::anypart(in_mime)(input)?;
|
let (input, part) = part::anypart(in_mime)(input)?;
|
||||||
|
|
||||||
// extract raw parts 2/2
|
// extract raw parts 2/2
|
||||||
|
@ -155,7 +197,9 @@ pub fn message<'a>(
|
||||||
Message {
|
Message {
|
||||||
mime: m.clone(),
|
mime: m.clone(),
|
||||||
imf,
|
imf,
|
||||||
raw_part, raw_headers, raw_body,
|
raw_part,
|
||||||
|
raw_headers,
|
||||||
|
raw_body,
|
||||||
child: Box::new(part),
|
child: Box::new(part),
|
||||||
},
|
},
|
||||||
))
|
))
|
||||||
|
@ -168,7 +212,7 @@ mod tests {
|
||||||
use crate::part::discrete::Text;
|
use crate::part::discrete::Text;
|
||||||
use crate::part::AnyPart;
|
use crate::part::AnyPart;
|
||||||
use crate::text::encoding::{Base64Word, EncodedWord, QuotedChunk, QuotedWord};
|
use crate::text::encoding::{Base64Word, EncodedWord, QuotedChunk, QuotedWord};
|
||||||
use crate::text::misc_token::{Phrase, UnstrToken, Unstructured, Word, MIMEWord};
|
use crate::text::misc_token::{MIMEWord, Phrase, UnstrToken, Unstructured, Word};
|
||||||
use crate::text::quoted::QuotedString;
|
use crate::text::quoted::QuotedString;
|
||||||
use chrono::{FixedOffset, TimeZone};
|
use chrono::{FixedOffset, TimeZone};
|
||||||
|
|
||||||
|
@ -229,7 +273,10 @@ It DOES end with a linebreak.
|
||||||
subtype: mime::r#type::TextSubtype::Plain,
|
subtype: mime::r#type::TextSubtype::Plain,
|
||||||
charset: mime::r#type::Deductible::Inferred(mime::charset::EmailCharset::US_ASCII),
|
charset: mime::r#type::Deductible::Inferred(mime::charset::EmailCharset::US_ASCII),
|
||||||
}),
|
}),
|
||||||
fields: mime::NaiveMIME::default(),
|
fields: mime::NaiveMIME {
|
||||||
|
raw: &b"\n"[..],
|
||||||
|
..mime::NaiveMIME::default()
|
||||||
|
},
|
||||||
},
|
},
|
||||||
body: &b"This is implicitly typed plain US-ASCII text.\nIt does NOT end with a linebreak."[..],
|
body: &b"This is implicitly typed plain US-ASCII text.\nIt does NOT end with a linebreak."[..],
|
||||||
}),
|
}),
|
||||||
|
@ -250,6 +297,10 @@ It DOES end with a linebreak.
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}),
|
}),
|
||||||
|
raw: &b"Content-type: text/plain; charset=us-ascii\n\n"[..],
|
||||||
|
kv: vec![
|
||||||
|
header::Field::Good(header::Kv2(&b"Content-type"[..], &b"text/plain; charset=us-ascii"[..]))
|
||||||
|
],
|
||||||
..mime::NaiveMIME::default()
|
..mime::NaiveMIME::default()
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -455,14 +506,18 @@ OoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoO<br />
|
||||||
right: &b"www.grrrndzero.org"[..],
|
right: &b"www.grrrndzero.org"[..],
|
||||||
}),
|
}),
|
||||||
mime_version: Some(imf::mime::Version { major: 1, minor: 0}),
|
mime_version: Some(imf::mime::Version { major: 1, minor: 0}),
|
||||||
header_ext: vec![
|
kv: vec![
|
||||||
header::Kv(&b"X-Unknown"[..], Unstructured(vec![
|
header::Field::Good(header::Kv2(&b"Date"[..], &b"Sat, 8 Jul 2023 07:14:29 +0200"[..])),
|
||||||
UnstrToken::Plain(&b"something"[..]),
|
header::Field::Good(header::Kv2(&b"From"[..], &b"Grrrnd Zero <grrrndzero@example.org>"[..])),
|
||||||
UnstrToken::Plain(&b"something"[..]),
|
header::Field::Good(header::Kv2(&b"To"[..], &b"John Doe <jdoe@machine.example>"[..])),
|
||||||
]))
|
header::Field::Good(header::Kv2(&b"CC"[..], &b"=?ISO-8859-1?Q?Andr=E9?= Pirard <PIRARD@vm1.ulg.ac.be>"[..])),
|
||||||
],
|
header::Field::Good(header::Kv2(&b"Subject"[..], &b"=?ISO-8859-1?B?SWYgeW91IGNhbiByZWFkIHRoaXMgeW8=?=\n =?ISO-8859-2?B?dSB1bmRlcnN0YW5kIHRoZSBleGFtcGxlLg==?="[..])),
|
||||||
header_bad: vec![
|
header::Field::Good(header::Kv2(&b"X-Unknown"[..], &b"something something"[..])),
|
||||||
&b"Bad entry\n on multiple lines\n"[..],
|
header::Field::Bad(&b"Bad entry\n on multiple lines\n"[..]),
|
||||||
|
header::Field::Good(header::Kv2(&b"Message-ID"[..], &b"<NTAxNzA2AC47634Y366BAMTY4ODc5MzQyODY0ODY5@www.grrrndzero.org>"[..])),
|
||||||
|
header::Field::Good(header::Kv2(&b"MIME-Version"[..], &b"1.0"[..])),
|
||||||
|
header::Field::Good(header::Kv2(&b"Content-Type"[..], &b"multipart/alternative;\n boundary=\"b1_e376dc71bafc953c0b0fdeb9983a9956\""[..])),
|
||||||
|
header::Field::Good(header::Kv2(&b"Content-Transfer-Encoding"[..], &b"7bit"[..])),
|
||||||
],
|
],
|
||||||
..imf::Imf::default()
|
..imf::Imf::default()
|
||||||
},
|
},
|
||||||
|
@ -483,6 +538,7 @@ OoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoO<br />
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}),
|
}),
|
||||||
|
raw: hdrs,
|
||||||
..mime::NaiveMIME::default()
|
..mime::NaiveMIME::default()
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -507,6 +563,11 @@ OoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoO<br />
|
||||||
]
|
]
|
||||||
}),
|
}),
|
||||||
transfer_encoding: mime::mechanism::Mechanism::QuotedPrintable,
|
transfer_encoding: mime::mechanism::Mechanism::QuotedPrintable,
|
||||||
|
kv: vec![
|
||||||
|
header::Field::Good(header::Kv2(&b"Content-Type"[..], &b"text/plain; charset=utf-8"[..])),
|
||||||
|
header::Field::Good(header::Kv2(&b"Content-Transfer-Encoding"[..], &b"quoted-printable"[..])),
|
||||||
|
],
|
||||||
|
raw: &b"Content-Type: text/plain; charset=utf-8\nContent-Transfer-Encoding: quoted-printable\n\n"[..],
|
||||||
..mime::NaiveMIME::default()
|
..mime::NaiveMIME::default()
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
@ -530,6 +591,10 @@ OoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoO<br />
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}),
|
}),
|
||||||
|
kv: vec![
|
||||||
|
header::Field::Good(header::Kv2(&b"Content-Type"[..], &b"text/html; charset=us-ascii"[..])),
|
||||||
|
],
|
||||||
|
raw: &b"Content-Type: text/html; charset=us-ascii\n\n"[..],
|
||||||
..mime::NaiveMIME::default()
|
..mime::NaiveMIME::default()
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
|
@ -12,10 +12,7 @@ impl<'a> fmt::Debug for Text<'a> {
|
||||||
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
|
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
fmt.debug_struct("part::Text")
|
fmt.debug_struct("part::Text")
|
||||||
.field("mime", &self.mime)
|
.field("mime", &self.mime)
|
||||||
.field(
|
.field("body", &String::from_utf8_lossy(self.body))
|
||||||
"body",
|
|
||||||
&format_args!("\"{}\"", String::from_utf8_lossy(self.body)),
|
|
||||||
)
|
|
||||||
.finish()
|
.finish()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -30,10 +27,7 @@ impl<'a> fmt::Debug for Binary<'a> {
|
||||||
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
|
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
fmt.debug_struct("part::Binary")
|
fmt.debug_struct("part::Binary")
|
||||||
.field("mime", &self.mime)
|
.field("mime", &self.mime)
|
||||||
.field(
|
.field("body", &String::from_utf8_lossy(self.body))
|
||||||
"body",
|
|
||||||
&format_args!("\"{}\"", String::from_utf8_lossy(self.body)),
|
|
||||||
)
|
|
||||||
.finish()
|
.finish()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,50 +1,24 @@
|
||||||
use nom::{branch::alt, combinator::map, IResult};
|
use crate::header;
|
||||||
|
|
||||||
use crate::imf;
|
use crate::imf;
|
||||||
use crate::mime;
|
use crate::mime;
|
||||||
|
|
||||||
pub enum MixedField<'a> {
|
pub fn split_and_build<'a>(v: &Vec<header::Field<'a>>) -> (mime::NaiveMIME<'a>, imf::Imf<'a>) {
|
||||||
MIME(mime::field::Content<'a>),
|
let (mimev, imfv) = v.iter().fold(
|
||||||
IMF(imf::field::Field<'a>),
|
(
|
||||||
}
|
Vec::<mime::field::Content>::new(),
|
||||||
#[allow(dead_code)]
|
Vec::<imf::field::Field>::new(),
|
||||||
impl<'a> MixedField<'a> {
|
),
|
||||||
pub fn mime(&self) -> Option<&mime::field::Content<'a>> {
|
|(mut mime, mut imf), f| {
|
||||||
match self {
|
if let Ok(m) = mime::field::Content::try_from(f) {
|
||||||
Self::MIME(v) => Some(v),
|
mime.push(m);
|
||||||
_ => None,
|
} else if let Ok(i) = imf::field::Field::try_from(f) {
|
||||||
}
|
imf.push(i);
|
||||||
}
|
}
|
||||||
pub fn to_mime(self) -> Option<mime::field::Content<'a>> {
|
(mime, imf)
|
||||||
match self {
|
},
|
||||||
Self::MIME(v) => Some(v),
|
);
|
||||||
_ => None,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
pub fn imf(&self) -> Option<&imf::field::Field<'a>> {
|
|
||||||
match self {
|
|
||||||
Self::IMF(v) => Some(v),
|
|
||||||
_ => None,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
pub fn to_imf(self) -> Option<imf::field::Field<'a>> {
|
|
||||||
match self {
|
|
||||||
Self::IMF(v) => Some(v),
|
|
||||||
_ => None,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn sections<'a>(list: Vec<MixedField<'a>>) -> (mime::NaiveMIME<'a>, imf::Imf<'a>) {
|
let fmime = mimev.into_iter().collect::<mime::NaiveMIME>();
|
||||||
let (v1, v2): (Vec<MixedField>, Vec<_>) = list.into_iter().partition(|v| v.mime().is_some());
|
let fimf = imfv.into_iter().collect::<imf::Imf>();
|
||||||
let mime = v1.into_iter().flat_map(MixedField::to_mime).collect::<mime::NaiveMIME>();
|
(fmime, fimf)
|
||||||
let imf = v2.into_iter().flat_map(MixedField::to_imf).collect::<imf::Imf>();
|
|
||||||
(mime, imf)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn mixed_field(input: &[u8]) -> IResult<&[u8], MixedField> {
|
|
||||||
alt((
|
|
||||||
map(mime::field::content, MixedField::MIME),
|
|
||||||
map(imf::field::field, MixedField::IMF),
|
|
||||||
))(input)
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -80,18 +80,19 @@ pub fn anypart<'a>(m: AnyMIME<'a>) -> impl FnOnce(&'a [u8]) -> IResult<&'a [u8],
|
||||||
move |input| {
|
move |input| {
|
||||||
let part = match m {
|
let part = match m {
|
||||||
AnyMIME::Mult(a) => multipart(a)(input)
|
AnyMIME::Mult(a) => multipart(a)(input)
|
||||||
.map(|(_, multi)|
|
.map(|(_, multi)| multi.into())
|
||||||
multi.into())
|
|
||||||
.unwrap_or(AnyPart::Txt(Text {
|
|
||||||
mime: mime::MIME::<mime::r#type::DeductibleText>::default(),
|
|
||||||
body: input,
|
|
||||||
})),
|
|
||||||
AnyMIME::Msg(a) => message(a)(input)
|
|
||||||
.map(|(_, msg)| msg.into())
|
|
||||||
.unwrap_or(AnyPart::Txt(Text {
|
.unwrap_or(AnyPart::Txt(Text {
|
||||||
mime: mime::MIME::<mime::r#type::DeductibleText>::default(),
|
mime: mime::MIME::<mime::r#type::DeductibleText>::default(),
|
||||||
body: input,
|
body: input,
|
||||||
})),
|
})),
|
||||||
|
AnyMIME::Msg(a) => {
|
||||||
|
message(a)(input)
|
||||||
|
.map(|(_, msg)| msg.into())
|
||||||
|
.unwrap_or(AnyPart::Txt(Text {
|
||||||
|
mime: mime::MIME::<mime::r#type::DeductibleText>::default(),
|
||||||
|
body: input,
|
||||||
|
}))
|
||||||
|
}
|
||||||
AnyMIME::Txt(a) => AnyPart::Txt(Text {
|
AnyMIME::Txt(a) => AnyPart::Txt(Text {
|
||||||
mime: a,
|
mime: a,
|
||||||
body: input,
|
body: input,
|
||||||
|
|
|
@ -22,7 +22,12 @@ use nom::{
|
||||||
/// \r or \n is allowed nowhere else, so we also add this support.
|
/// \r or \n is allowed nowhere else, so we also add this support.
|
||||||
|
|
||||||
pub fn obs_crlf(input: &[u8]) -> IResult<&[u8], &[u8]> {
|
pub fn obs_crlf(input: &[u8]) -> IResult<&[u8], &[u8]> {
|
||||||
alt((tag(ascii::CRLF), tag(ascii::CRCRLF), tag(&[ascii::CR]), tag(&[ascii::LF])))(input)
|
alt((
|
||||||
|
tag(ascii::CRLF),
|
||||||
|
tag(ascii::CRCRLF),
|
||||||
|
tag(&[ascii::CR]),
|
||||||
|
tag(&[ascii::LF]),
|
||||||
|
))(input)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// ```abnf
|
/// ```abnf
|
||||||
|
|
Loading…
Reference in a new issue