2022-06-12 19:42:18 +00:00
|
|
|
mod message_reader;
|
|
|
|
|
2022-06-07 20:27:28 +00:00
|
|
|
use std::{
|
2022-06-12 19:42:18 +00:00
|
|
|
fs::OpenOptions,
|
2022-06-07 20:27:28 +00:00
|
|
|
io::Write,
|
|
|
|
path::{Path, PathBuf},
|
2022-06-10 11:46:07 +00:00
|
|
|
};
|
2022-06-07 20:27:28 +00:00
|
|
|
|
2022-07-20 21:03:23 +00:00
|
|
|
use mail_parser::Message as MpMessage;
|
2022-06-07 20:27:28 +00:00
|
|
|
|
2022-06-10 11:46:07 +00:00
|
|
|
use sha2::{Digest, Sha256};
|
|
|
|
|
2022-06-07 20:27:28 +00:00
|
|
|
extern crate imap;
|
|
|
|
extern crate mail_parser;
|
|
|
|
extern crate rustls_connector;
|
2022-06-10 11:46:07 +00:00
|
|
|
extern crate sha2;
|
2022-06-07 20:27:28 +00:00
|
|
|
|
2022-06-12 19:42:18 +00:00
|
|
|
use message_reader::{EmailReader, TestMessagesReader};
|
|
|
|
|
2022-07-20 21:03:23 +00:00
|
|
|
pub struct Message {
|
|
|
|
uid: String,
|
|
|
|
data: Vec<u8>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Message {
|
|
|
|
pub fn new(uid: String, data: Vec<u8>) -> Message {
|
2022-08-02 21:07:01 +00:00
|
|
|
Message { uid, data }
|
2022-07-20 21:03:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn get_parsed(&self) -> Option<MpMessage> {
|
|
|
|
MpMessage::parse(&self.data)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_uid(&self) -> &String {
|
|
|
|
&self.uid
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-07 20:27:28 +00:00
|
|
|
fn main() {
|
|
|
|
let dir = Path::new("data");
|
|
|
|
if !dir.exists() {
|
|
|
|
std::fs::create_dir(&dir).expect("Could not create directory");
|
|
|
|
}
|
|
|
|
|
2022-06-12 19:39:19 +00:00
|
|
|
let mut reader = TestMessagesReader::new((&Path::new("tests/data")).to_path_buf());
|
|
|
|
|
2022-07-20 21:03:23 +00:00
|
|
|
for msg in reader.read_rfc822_messages() {
|
|
|
|
println!("Processing message {}", msg.get_uid());
|
2022-06-07 20:27:28 +00:00
|
|
|
|
2022-07-20 21:03:23 +00:00
|
|
|
let parsed = msg.get_parsed().expect("A parsed messsage.");
|
2022-06-07 20:27:28 +00:00
|
|
|
|
2022-06-12 18:39:35 +00:00
|
|
|
let html_body = parsed.get_html_body(0).expect("Could not read html body");
|
2022-06-10 22:28:28 +00:00
|
|
|
let processed_html = process_html(&html_body).expect("Could not process the HTML");
|
|
|
|
let html_bytes = processed_html.as_bytes();
|
2022-06-10 11:46:07 +00:00
|
|
|
|
2022-08-02 21:07:01 +00:00
|
|
|
let html_path: PathBuf = [dir, Path::new(&get_path(&parsed, &msg))].iter().collect();
|
|
|
|
println!("Storing to {}", &html_path.display());
|
2022-06-12 18:39:35 +00:00
|
|
|
|
|
|
|
OpenOptions::new()
|
2022-06-07 20:27:28 +00:00
|
|
|
.write(true)
|
|
|
|
.create(true)
|
2022-06-12 18:39:35 +00:00
|
|
|
.open(&html_path)
|
|
|
|
.expect(format!("Could not open file '{}' for writing", &html_path.display()).as_str())
|
|
|
|
.write_all(&html_bytes)
|
|
|
|
.expect(format!("Could not write html to file '{}'.", &html_path.display()).as_str());
|
2022-06-10 22:28:28 +00:00
|
|
|
|
|
|
|
println!();
|
2022-06-07 20:27:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-02 21:07:01 +00:00
|
|
|
fn get_path(parsed: &MpMessage, msg: &Message) -> String {
|
|
|
|
let date = parsed.get_date().expect("Could not extract date");
|
|
|
|
let date_str = format!(
|
|
|
|
"{:04}{:02}{:02}{:02}{:02}{:02}",
|
|
|
|
&date.year, &date.month, &date.day, &date.hour, &date.minute, &date.second
|
|
|
|
);
|
|
|
|
|
|
|
|
let hash = base16ct::lower::encode_string(&Sha256::digest(
|
|
|
|
&parsed.get_html_body(0).expect("Expected a body").as_bytes(),
|
|
|
|
));
|
|
|
|
|
|
|
|
let uid: i32 = msg
|
|
|
|
.get_uid()
|
|
|
|
.parse()
|
|
|
|
.expect("Could not convert message uid to an i32.");
|
|
|
|
//format!("{}_{}_{}.html", &date_str, &file_name, msg.get_uid()).to_owned()
|
|
|
|
format!("{:05}_{}_{}.html", uid, date_str, &hash).to_owned()
|
|
|
|
}
|
|
|
|
|
2022-08-02 21:11:55 +00:00
|
|
|
fn process_html(input: &str) -> Result<String, ()> {
|
|
|
|
Ok(input.replace("src", "data-source"))
|
2022-06-10 22:28:28 +00:00
|
|
|
}
|
|
|
|
|
2022-08-02 21:17:59 +00:00
|
|
|
fn write_to_test_path(msg: &Message) {
|
|
|
|
let test_path: PathBuf = [
|
|
|
|
Path::new("tests/data"),
|
|
|
|
Path::new(&format!("{}.eml", &msg.get_uid())),
|
|
|
|
]
|
|
|
|
.iter()
|
|
|
|
.collect();
|
2022-06-10 22:28:28 +00:00
|
|
|
|
|
|
|
let _ = OpenOptions::new()
|
|
|
|
.write(true)
|
|
|
|
.create(true)
|
|
|
|
.open(test_path)
|
|
|
|
.expect("Could not open file fir writing")
|
2022-08-02 21:17:59 +00:00
|
|
|
.write_all(&msg.data);
|
2022-06-10 22:28:28 +00:00
|
|
|
}
|