eml-codec/src/imf/trace.rs

127 lines
4 KiB
Rust
Raw Normal View History

2023-07-23 16:37:47 +02:00
use chrono::{DateTime, FixedOffset};
2023-06-13 17:47:57 +02:00
use nom::{
2023-06-13 22:50:29 +02:00
branch::alt,
2023-07-19 17:40:00 +02:00
bytes::complete::{is_a, tag},
2023-07-23 16:37:47 +02:00
combinator::{map, not, opt},
2023-06-16 09:58:07 +02:00
multi::many0,
2023-07-23 16:37:47 +02:00
sequence::{terminated, tuple},
2023-06-22 15:08:50 +02:00
IResult,
2023-06-13 19:35:41 +02:00
};
2023-07-19 17:40:00 +02:00
2023-07-24 11:02:49 +02:00
use crate::imf::{datetime, mailbox};
2023-07-23 16:37:47 +02:00
use crate::text::{ascii, misc_token, whitespace};
2023-06-20 17:03:18 +02:00
#[derive(Debug, PartialEq)]
2023-07-19 17:40:00 +02:00
pub enum ReceivedLogToken<'a> {
Addr(mailbox::AddrSpec<'a>),
Domain(mailbox::Domain<'a>),
2023-07-23 16:37:47 +02:00
Word(misc_token::Word<'a>),
2023-07-19 17:40:00 +02:00
}
#[derive(Debug, PartialEq)]
pub struct ReceivedLog<'a> {
pub log: Vec<ReceivedLogToken<'a>>,
pub date: Option<DateTime<FixedOffset>>,
}
2023-06-20 17:03:18 +02:00
2023-07-19 17:40:00 +02:00
/*
2023-06-22 10:48:07 +02:00
impl<'a> TryFrom<&'a lazy::ReceivedLog<'a>> for ReceivedLog<'a> {
2023-06-20 17:03:18 +02:00
type Error = IMFError<'a>;
2023-06-22 10:48:07 +02:00
fn try_from(input: &'a lazy::ReceivedLog<'a>) -> Result<Self, Self::Error> {
2023-06-20 17:03:18 +02:00
received_body(input.0)
.map_err(|e| IMFError::ReceivedLog(e))
.map(|(_, v)| ReceivedLog(v))
}
2023-07-19 17:40:00 +02:00
}*/
2023-06-16 09:58:07 +02:00
2023-07-19 22:27:59 +02:00
pub fn received_log(input: &[u8]) -> IResult<&[u8], ReceivedLog> {
2023-06-16 09:58:07 +02:00
map(
2023-07-23 16:37:47 +02:00
tuple((many0(received_tokens), tag(";"), datetime::section)),
|(tokens, _, dt)| ReceivedLog {
log: tokens,
date: dt,
},
2023-06-13 22:50:29 +02:00
)(input)
}
2023-07-19 22:27:59 +02:00
pub fn return_path(input: &[u8]) -> IResult<&[u8], Option<mailbox::AddrSpec>> {
2023-07-24 09:24:38 +02:00
alt((map(mailbox::angle_addr, Some), empty_path))(input)
2023-06-13 22:50:29 +02:00
}
2023-07-19 17:40:00 +02:00
fn empty_path(input: &[u8]) -> IResult<&[u8], Option<mailbox::AddrSpec>> {
2023-06-13 22:50:29 +02:00
let (input, _) = tuple((
opt(whitespace::cfws),
2023-07-19 17:40:00 +02:00
tag(&[ascii::LT]),
2023-06-13 22:50:29 +02:00
opt(whitespace::cfws),
2023-07-19 17:40:00 +02:00
tag(&[ascii::GT]),
2023-06-13 22:50:29 +02:00
opt(whitespace::cfws),
))(input)?;
Ok((input, None))
}
2023-07-19 17:40:00 +02:00
fn received_tokens(input: &[u8]) -> IResult<&[u8], ReceivedLogToken> {
2023-06-13 22:50:29 +02:00
alt((
2023-07-23 16:37:47 +02:00
terminated(
2023-07-24 09:24:38 +02:00
map(misc_token::word, ReceivedLogToken::Word),
2023-07-23 16:37:47 +02:00
not(is_a([ascii::PERIOD, ascii::AT])),
),
2023-07-24 09:24:38 +02:00
map(mailbox::angle_addr, ReceivedLogToken::Addr),
map(mailbox::addr_spec, ReceivedLogToken::Addr),
map(mailbox::obs_domain, ReceivedLogToken::Domain),
2023-06-13 22:50:29 +02:00
))(input)
}
#[cfg(test)]
mod tests {
use super::*;
2023-07-24 11:02:49 +02:00
use crate::imf::trace::misc_token::Word;
2023-07-23 16:37:47 +02:00
use chrono::TimeZone;
2023-06-13 22:50:29 +02:00
#[test]
2023-06-16 10:19:28 +02:00
fn test_received_body() {
2023-06-16 09:58:07 +02:00
let hdrs = r#"from smtp.example.com ([10.83.2.2])
2023-06-13 22:50:29 +02:00
by server with LMTP
id xxxxxxxxx
(envelope-from <gitlab@example.com>)
2023-07-23 16:37:47 +02:00
for <me@example.com>; Tue, 13 Jun 2023 19:01:08 +0000"#
.as_bytes();
2023-06-16 09:58:07 +02:00
2023-06-13 22:50:29 +02:00
assert_eq!(
2023-07-20 09:41:10 +02:00
received_log(hdrs),
2023-06-22 15:08:50 +02:00
Ok((
2023-07-19 17:40:00 +02:00
&b""[..],
ReceivedLog {
2023-07-23 16:37:47 +02:00
date: Some(
FixedOffset::east_opt(0)
.unwrap()
.with_ymd_and_hms(2023, 06, 13, 19, 1, 8)
.unwrap()
),
2023-07-19 17:40:00 +02:00
log: vec![
ReceivedLogToken::Word(Word::Atom(&b"from"[..])),
2023-07-23 16:37:47 +02:00
ReceivedLogToken::Domain(mailbox::Domain::Atoms(vec![
&b"smtp"[..],
&b"example"[..],
&b"com"[..]
])),
2023-07-19 17:40:00 +02:00
ReceivedLogToken::Word(Word::Atom(&b"by"[..])),
ReceivedLogToken::Word(Word::Atom(&b"server"[..])),
ReceivedLogToken::Word(Word::Atom(&b"with"[..])),
ReceivedLogToken::Word(Word::Atom(&b"LMTP"[..])),
ReceivedLogToken::Word(Word::Atom(&b"id"[..])),
ReceivedLogToken::Word(Word::Atom(&b"xxxxxxxxx"[..])),
ReceivedLogToken::Word(Word::Atom(&b"for"[..])),
ReceivedLogToken::Addr(mailbox::AddrSpec {
2023-07-23 16:37:47 +02:00
local_part: mailbox::LocalPart(vec![mailbox::LocalPartToken::Word(
Word::Atom(&b"me"[..])
)]),
domain: mailbox::Domain::Atoms(vec![&b"example"[..], &b"com"[..]]),
2023-07-19 17:40:00 +02:00
})
],
2023-07-23 16:37:47 +02:00
}
2023-06-22 15:08:50 +02:00
))
2023-06-13 22:50:29 +02:00
);
}
2023-06-13 17:47:57 +02:00
}