crabmail

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

commit a9cf33b102523bc8fb2e264add74b30690ecf5be
parent d17486a5e404bea07bc8a31a144e14ee10de8229
Author: alex wennerberg <alex@alexwennerberg.com>
Date:   Sat, 18 Dec 2021 11:57:51 -0800

WIP atom feeds

Diffstat:
Msrc/main.rs | 34++++++++++++++++++++++++++++++++--
Msrc/utils.rs | 19+++++++++++++------
2 files changed, 45 insertions(+), 8 deletions(-)

diff --git a/src/main.rs b/src/main.rs @@ -21,6 +21,7 @@ use std::io::prelude::*; use urlencoding; use config::{Config, INSTANCE}; +mod atom; mod config; mod mbox; mod utils; @@ -132,8 +133,36 @@ impl<'a> MailThread<'a> { return self.messages[self.messages.len() - 1].date; } - fn build_atom_feed() -> String { - String::new() + fn build_atom_feed(&self) -> String { + let mut entries: String = String::new(); + for message in &self.messages { + let tmpl = format!( + r#"<title>{title}</title> +<link href="{item_link}"/> +<id>{entry_id}</id> +<updated>{updated_at}</updated> +<summary>{summary}</summary> +<author> + <name>{author_name}</name> + <email>{author_email}</email> +</author> +<content> +{content} +</content> +</feed> +"#, + title = "123", + item_link = "tbd", + entry_id = "tbd", + updated_at = "tbd", + summary = "tbd", + author_name = "tbd", + author_email = "tbd", + content = "tbd", + ); + entries.push_str(&tmpl); + } + entries } fn write_to_file(&self, out_dir: &Path) -> Result<()> { @@ -404,6 +433,7 @@ fn main() -> Result<()> { for leftover in curr_threads { let file_to_remove = out_dir.join("threads").join(format!("{}.html", leftover)); + println!("{:?}", file_to_remove); std::fs::remove_file(&file_to_remove)?; } diff --git a/src/utils.rs b/src/utils.rs @@ -24,20 +24,20 @@ pub fn email_body(body: &str) -> String { match span.kind() { Some(LinkKind::Url) => { bytes.extend_from_slice(b"<a href=\""); - escape(span.as_str(), &mut bytes); + xml_escape(span.as_str(), &mut bytes); bytes.extend_from_slice(b"\">"); - escape(span.as_str(), &mut bytes); + xml_escape(span.as_str(), &mut bytes); bytes.extend_from_slice(b"</a>"); } Some(LinkKind::Email) => { bytes.extend_from_slice(b"<a href=\"mailto:"); - escape(span.as_str(), &mut bytes); + xml_escape(span.as_str(), &mut bytes); bytes.extend_from_slice(b"\">"); - escape(span.as_str(), &mut bytes); + xml_escape(span.as_str(), &mut bytes); bytes.extend_from_slice(b"</a>"); } _ => { - escape(span.as_str(), &mut bytes); + xml_escape(span.as_str(), &mut bytes); } } } @@ -50,7 +50,14 @@ pub fn email_body(body: &str) -> String { String::from_utf8(bytes).unwrap() } -fn escape(text: &str, dest: &mut Vec<u8>) { +// less efficient, easier api +fn xml_safe(text: &str) -> String { + let mut dest = Vec::new(); + xml_escape(text, &mut dest); + std::str::from_utf8(&dest).unwrap().to_owned() +} + +fn xml_escape(text: &str, dest: &mut Vec<u8>) { for c in text.bytes() { match c { b'&' => dest.extend_from_slice(b"&amp;"),