commit 5a4560910821d60caeba0e33eaeaa2ae4d67d11d
parent bec7324fa76016d46778b561593c7896d84dfa17
Author: alex wennerberg <alex@alexwennerberg.com>
Date: Thu, 13 May 2021 13:58:50 -0700
Get started on email parsing
Diffstat:
2 files changed, 34 insertions(+), 2 deletions(-)
diff --git a/README.md b/README.md
@@ -1,5 +1,4 @@
# crabmail
-
An html mail archive, written in Rust. Similar to [Hypermail](https://github.com/hypermail-project/hypermail).
Notes:
diff --git a/src/main.rs b/src/main.rs
@@ -1,6 +1,6 @@
use anyhow::Result;
use mailparse::{parse_mail, MailHeaderMap, ParsedMail};
-use std::fs::OpenOptions;
+use std::fs::{File, OpenOptions};
use std::io::prelude::*;
use std::path::Path;
@@ -10,6 +10,7 @@ Usage: crabmail
FLAGS:
-h, --help Prints this help information and exits.
-v, --version Prints the version and exits
+ -t, --threads Group messages into threads
OPTIONS:
-d, --dir Directory to save the HTML files in
@@ -33,6 +34,18 @@ fn main() -> Result<()> {
println!("Please provide one or more input files with the -m flag");
std::process::exit(1);
}
+ for file in in_mboxes {
+ // assuming one email per file for now
+ let mut buffer = Vec::new();
+ let mut f = File::open(&file)?;
+ f.read_to_end(&mut buffer)?;
+ let email = parse_mail(&buffer)?;
+ println!("{}", email_to_html(email));
+ }
+ if pargs.contains(["-t", "--threads"]) {
+ // TODO
+ }
+
std::fs::create_dir(&out_dir).ok();
write_index(&out_dir)?;
Ok(())
@@ -42,6 +55,26 @@ fn parse_path(s: &std::ffi::OsStr) -> Result<std::path::PathBuf, &'static str> {
Ok(s.into())
}
+fn email_to_html(email: ParsedMail) -> String {
+ // TODO use format strings here i think
+ let mut result = String::new();
+ result.push_str("Subject: ");
+ result.push_str(
+ &email
+ .headers
+ .get_first_value("Subject")
+ .unwrap_or(String::from("<No Subject>")),
+ );
+ result.push_str("\nFrom: ");
+ result.push_str(
+ &email
+ .headers
+ .get_first_value("From")
+ .unwrap_or(String::from("<No From?>")),
+ );
+ result
+}
+
// TODO set lang, title, etc
const HEADER: &[u8] = br#"<!DOCTYPE html>
<html>