Skip to content

Commit

Permalink
feature: use a better and more reliable way to change the message-id.
Browse files Browse the repository at this point in the history
  • Loading branch information
maeln committed Feb 16, 2021
1 parent 2c4f332 commit 770f3e0
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 10 deletions.
4 changes: 3 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "eml-replicator"
version = "0.1.2"
version = "0.1.3"
authors = ["Maël Naccache Tüfekçi <[email protected]>"]
edition = "2018"
license = "CECILL-2.1"
Expand All @@ -17,4 +17,6 @@ native-tls = "0.2"
clap = "2.33"
walkdir = "2"
indicatif = "0.15"
rand = "0.8"
rand = "0.8"
regex = "1"
lazy_static = "1.4"
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
32 changes: 26 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -45,13 +48,18 @@ fn list_eml_file(
}

fn randomize_message_id(eml: &String) -> Result<String, String> {
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."));
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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();
Expand All @@ -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)
Expand Down

0 comments on commit 770f3e0

Please sign in to comment.