diff --git a/Cargo.lock b/Cargo.lock index 0c6f770..2de6a01 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -138,13 +138,15 @@ checksum = "ea221b5284a47e40033bf9b66f35f984ec0ea2931eb03505246cd27a963f981b" [[package]] name = "eml-replicator" -version = "0.1.2" +version = "0.1.3" dependencies = [ "clap", "imap", "indicatif", + "lazy_static", "native-tls", "rand", + "regex", "walkdir", ] diff --git a/Cargo.toml b/Cargo.toml index 9245a58..6364021 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "eml-replicator" -version = "0.1.2" +version = "0.1.3" authors = ["Maël Naccache Tüfekçi "] edition = "2018" license = "CECILL-2.1" @@ -17,4 +17,6 @@ native-tls = "0.2" clap = "2.33" walkdir = "2" indicatif = "0.15" -rand = "0.8" \ No newline at end of file +rand = "0.8" +regex = "1" +lazy_static = "1.4" \ No newline at end of file diff --git a/README.md b/README.md index f4b914b..bbbc4fe 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ This tool read all the EML (RFC822 / RFC2822) in a directory and copy them in a Usage: ``` -eml-replicator 0.1.2 +eml-replicator 0.1.3 Maël Naccache Tüfekçi A tool that read EML files and copy them to a IMAP mailbox. diff --git a/src/main.rs b/src/main.rs index b6a4e6f..67d1484 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,7 @@ +#[macro_use] +extern crate lazy_static; use indicatif::ProgressStyle; +use regex::Regex; use std::fs::{self}; use std::io::{Error, ErrorKind}; use std::path::{Path, PathBuf}; @@ -45,13 +48,18 @@ fn list_eml_file( } fn randomize_message_id(eml: &String) -> Result { + lazy_static! { + static ref MID_RE: Regex = Regex::new(r"(?imu)^message-id:.+$").unwrap(); + } + let mut new_eml = String::new(); - let header_pos = eml.find("Message-ID:"); + let header_pos = MID_RE.find(eml); if header_pos.is_none() { return Err("Could not find Message-ID in the EML.".to_string()); } - let (fpart, lpart) = eml.split_at(header_pos.unwrap()); + + let (fpart, lpart) = eml.split_at(header_pos.unwrap().start()); new_eml.push_str(fpart); let (_mid, rest) = lpart.split_at(lpart.find('\n').expect("Malformed Message-ID.")); @@ -95,6 +103,7 @@ impl Config { let recursive = matches.is_present("recursive"); let symlink = matches.is_present("symlink"); let random_id = matches.is_present("random-message-id"); + Config { server, port, @@ -192,6 +201,10 @@ fn main() { println!("- {}", path.to_str().unwrap_or("")); } + if conf.random_id { + println!("Randomizing Message-IDs.") + } + let tls = native_tls::TlsConnector::builder().build().unwrap(); let client = imap::connect((conf.server.clone(), conf.port), conf.server, &tls).unwrap(); let mut session = client.login(conf.login, conf.password).unwrap(); @@ -204,10 +217,17 @@ fn main() { for eml in &emls_files { let rfc822 = fs::read_to_string(eml).expect("Failed to read eml file."); if conf.random_id { - let randomize_id = randomize_message_id(&rfc822).unwrap(); - session - .append(&conf.folder, &randomize_id) - .expect("Could not copy eml file to inbox."); + let randomize_id = randomize_message_id(&rfc822); + if randomize_id.is_err() { + println!( + "Could not find Message-ID for file {}, skipping.", + eml.to_string_lossy() + ); + } else { + session + .append(&conf.folder, &randomize_id.unwrap()) + .expect("Could not copy eml file to inbox."); + } } else { session .append(&conf.folder, &rfc822)