2023-06-13 20:50:29 +00:00
|
|
|
use std::collections::HashMap;
|
2023-06-13 15:47:57 +00:00
|
|
|
use nom::{
|
|
|
|
IResult,
|
2023-06-13 20:50:29 +00:00
|
|
|
branch::alt,
|
|
|
|
bytes::complete::tag,
|
|
|
|
character::complete::space0,
|
2023-06-16 07:58:07 +00:00
|
|
|
combinator::{map, opt, recognize},
|
|
|
|
multi::many0,
|
|
|
|
sequence::{delimited, pair, tuple},
|
2023-06-13 17:35:41 +00:00
|
|
|
};
|
2023-06-16 07:58:07 +00:00
|
|
|
use crate::{datetime, mailbox, model, misc_token, whitespace};
|
|
|
|
|
|
|
|
pub fn received_body(input: &str) -> IResult<&str, &str> {
|
|
|
|
map(
|
|
|
|
tuple((
|
|
|
|
recognize(many0(received_tokens)),
|
|
|
|
tag(";"),
|
|
|
|
datetime::section,
|
|
|
|
)),
|
|
|
|
|(tokens, _, _)| tokens,
|
2023-06-13 20:50:29 +00:00
|
|
|
)(input)
|
|
|
|
}
|
|
|
|
|
2023-06-16 07:58:07 +00:00
|
|
|
pub fn return_path_body(input: &str) -> IResult<&str, Option<model::MailboxRef>> {
|
2023-06-13 20:50:29 +00:00
|
|
|
alt((
|
|
|
|
map(mailbox::angle_addr, |a| Some(a)),
|
|
|
|
empty_path
|
|
|
|
))(input)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn empty_path(input: &str) -> IResult<&str, Option<model::MailboxRef>> {
|
|
|
|
let (input, _) = tuple((
|
|
|
|
opt(whitespace::cfws),
|
|
|
|
tag("<"),
|
|
|
|
opt(whitespace::cfws),
|
|
|
|
tag(">"),
|
|
|
|
opt(whitespace::cfws),
|
|
|
|
))(input)?;
|
|
|
|
Ok((input, None))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn received_tokens(input: &str) -> IResult<&str, &str> {
|
|
|
|
alt((
|
|
|
|
recognize(mailbox::angle_addr),
|
|
|
|
recognize(mailbox::addr_spec),
|
|
|
|
recognize(mailbox::domain_part),
|
|
|
|
recognize(misc_token::word),
|
|
|
|
))(input)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
use chrono::{FixedOffset, TimeZone};
|
|
|
|
|
|
|
|
#[test]
|
2023-06-16 08:19:28 +00:00
|
|
|
fn test_received_body() {
|
2023-06-16 07:58:07 +00:00
|
|
|
let hdrs = r#"from smtp.example.com ([10.83.2.2])
|
2023-06-13 20:50:29 +00:00
|
|
|
by server with LMTP
|
|
|
|
id xxxxxxxxx
|
|
|
|
(envelope-from <gitlab@example.com>)
|
2023-06-16 07:58:07 +00:00
|
|
|
for <me@example.com>; Tue, 13 Jun 2023 19:01:08 +0000"#;
|
|
|
|
|
2023-06-13 20:50:29 +00:00
|
|
|
assert_eq!(
|
2023-06-16 07:58:07 +00:00
|
|
|
received_body(hdrs),
|
|
|
|
Ok(("", r#"from smtp.example.com ([10.83.2.2])
|
2023-06-13 20:50:29 +00:00
|
|
|
by server with LMTP
|
|
|
|
id xxxxxxxxx
|
|
|
|
(envelope-from <gitlab@example.com>)
|
2023-06-16 07:58:07 +00:00
|
|
|
for <me@example.com>"#))
|
2023-06-13 20:50:29 +00:00
|
|
|
);
|
|
|
|
}
|
2023-06-13 15:47:57 +00:00
|
|
|
}
|