crabmail

Static HTML email archive viewer in Rust
git clone git://git.alexwennerberg.com/crabmail
Log | Files | Refs | README | LICENSE

commit 2619feb05fe8b61e595c8acbbdd1cd3cebc9cdc5
parent 516b2c49026bfecab86c5116a9b41b086783d9d0
Author: alex wennerberg <alex@alexwennerberg.com>
Date:   Wed, 22 Dec 2021 16:47:17 -0800

nested parse email body

Diffstat:
Msrc/main.rs | 37+++++++++++++++++++++++++++----------
1 file changed, 27 insertions(+), 10 deletions(-)

diff --git a/src/main.rs b/src/main.rs @@ -43,6 +43,16 @@ struct Email { date_string: String, body: String, mime: String, + attachments: Vec<Attachment>, +} + +#[derive(Debug, Clone)] +struct Attachment { + filename: String, +} + +impl Attachment { + fn local_path(&self) {} } #[derive(Debug, Clone)] @@ -370,19 +380,25 @@ fn local_parse_email(data: &[u8]) -> Result<Email> { let parsed_mail = parse_mail(data)?; let mut body: String = "[Message has no body]".to_owned(); let mut mime: String = "".to_owned(); + let attachments = vec![]; let nobody = "[No body found]"; - if parsed_mail.subparts.len() == 0 { - body = parsed_mail.get_body().unwrap_or(nobody.to_owned()); - } else { - for sub in &parsed_mail.subparts { - if sub.ctype.mimetype == "text/plain" { - body = sub.get_body().unwrap_or(nobody.to_owned()); - mime = sub.ctype.mimetype.clone(); + // nested lookup + let mut queue = vec![&parsed_mail]; + while queue.len() > 0 { + let top = queue.pop().unwrap(); + for sub in &top.subparts { + queue.push(sub); + } + let content_disposition = top.get_content_disposition(); + if content_disposition.disposition == mailparse::DispositionType::Attachment { + // attachment handler + } else { + if top.ctype.mimetype == "text/plain" { + body = top.get_body().unwrap_or(nobody.to_owned()); + mime = top.ctype.mimetype.clone(); break; } - } - #[cfg(feature = "html")] - for sub in &parsed_mail.subparts { + #[cfg(feature = "html")] if sub.ctype.mimetype == "text/html" { mime = sub.ctype.mimetype.clone(); break; @@ -421,6 +437,7 @@ fn local_parse_email(data: &[u8]) -> Result<Email> { date_string, body, mime, + attachments, }); }