crabmail

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

commit cedcdefae1523225ae7c3de1ab075ed50c62eea5
parent 666fc960e09625be2f74163de4d1c8fd409e40ac
Author: alex wennerberg <alex@alexwennerberg.com>
Date:   Mon, 14 Mar 2022 20:51:12 -0700

add some headers

Diffstat:
Msrc/models.rs | 27+++++++++++++++------------
Msrc/templates/html.rs | 35+++++++++++++++++++++++++++++------
2 files changed, 44 insertions(+), 18 deletions(-)

diff --git a/src/models.rs b/src/models.rs @@ -1,5 +1,6 @@ use crate::config::{Config, Subsection}; use crate::threading::{Msg, ThreadIdx}; +use mail_parser::MimeHeaders; use mail_parser::{Addr, HeaderValue, Message}; use std::borrow::Cow; use std::path::PathBuf; @@ -78,6 +79,9 @@ pub struct StrMessage { pub date: String, // TODO better dates pub body: String, pub in_reply_to: Option<String>, + pub to: Vec<MailAddress>, + pub cc: Vec<MailAddress>, + pub content_type: String, // url: Cow<'a, str>, // download_path: PathBuf, // TODO } @@ -94,24 +98,16 @@ impl StrMessage { // i suck at Cow and strings pub struct MailAddress { - pub name: String, + pub name: Option<String>, pub address: String, } impl MailAddress { fn from_addr(addr: &Addr) -> Self { // todo wtf - let address = addr - .address - .clone() - .unwrap_or(Cow::Borrowed("invalid-email")) - .to_string(); + let address = addr.address.to_owned(); MailAddress { - name: addr - .name - .clone() - .unwrap_or(Cow::Owned(address.clone())) - .to_string(), - address: address.to_string(), + name: addr.name.to_owned().and_then(|a| Some(a.to_string())), + address: address.unwrap().to_string(), } } } @@ -128,6 +124,7 @@ impl StrMessage { }; let from = MailAddress::from_addr(from); let date = msg.get_date().unwrap().to_iso8601(); + let to = "Tbd"; let in_reply_to = msg .get_in_reply_to() .as_text_ref() @@ -141,7 +138,13 @@ impl StrMessage { StrMessage { id: id.to_owned(), subject: subject.to_owned(), + content_type: msg + .get_content_type() + .map(|x| x.c_type.to_string()) + .unwrap_or(String::new()), from: from, + to: vec![], + cc: vec![], date: date.to_owned(), body: body.to_string(), in_reply_to: in_reply_to, diff --git a/src/templates/html.rs b/src/templates/html.rs @@ -55,6 +55,21 @@ impl List { } } +impl MailAddress { + fn to_html(&self) -> String { + let mut out = String::new(); + if let Some(n) = &self.name { + out.push('"'); + out.push_str(&x(n)); + out.push_str("\" "); + } + out.push_str(&format!( + "<<a href='mailto:{0}'>{0}</a>>", + &x(&self.address) + )); + out + } +} impl Thread { pub fn to_html(&self) -> String { let root = &self.messages[0]; @@ -81,6 +96,17 @@ impl Thread { // TODO converted from html // fix from header parsing // TODO in reply to + let mut extra_headers = format!("Message-Id: {}<br>\n", &x(&msg.id)); + if let Some(irt) = &msg.in_reply_to { + extra_headers.push_str(&format!("In-Reply-To: {}<br>\n", x(irt))); + } + extra_headers.push_str("To: "); + // extra_headers.push_str(msg.to.iter().map(|x| x.to_html()).collect().join(",")); + extra_headers.push_str("Cc: \n"); + // extra_headers.push_str(msg.cc.iter().map(|x| x.to_html()).collect().join(",")); + + extra_headers.push_str(&format!("Content-Type: {}<br>\n", &x(&msg.content_type))); + // Content-Type: tbd <br> let ms = r#"<div id="{msg_id}" class="message"> <div class="message-meta"> <span class="bold"> @@ -92,11 +118,7 @@ impl Thread { Date: <span class="light">{date}</span> <details> <summary>More headers</summary> - Message-Id: {msg_id} <br> - Content-Type: tbd <br> - To: tbd <br> - In-Reply-To: tbd <br> - Cc: tbd + {extra_headers} </details> <a class="bold" href="tbd">Reply</a> [<a href="tbd.eml">Download</a>] @@ -113,8 +135,9 @@ impl Thread { ("msg_id", &x(&msg.id)), ("subject", &x(&msg.subject)), ("from_addr", &x(&msg.from.address)), - ("from_name", &x(&msg.from.name)), + ("from_name", &msg.from.to_html()), ("date", &x(&msg.date)), + ("extra_headers", &extra_headers), ("body", &email_body(&msg.body)), ], )