Skip to content

Commit 770f3e0

Browse files
committed
feature: use a better and more reliable way to change the message-id.
1 parent 2c4f332 commit 770f3e0

File tree

4 files changed

+34
-10
lines changed

4 files changed

+34
-10
lines changed

Cargo.lock

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "eml-replicator"
3-
version = "0.1.2"
3+
version = "0.1.3"
44
authors = ["Maël Naccache Tüfekçi <contact@maeln.com>"]
55
edition = "2018"
66
license = "CECILL-2.1"
@@ -17,4 +17,6 @@ native-tls = "0.2"
1717
clap = "2.33"
1818
walkdir = "2"
1919
indicatif = "0.15"
20-
rand = "0.8"
20+
rand = "0.8"
21+
regex = "1"
22+
lazy_static = "1.4"

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ This tool read all the EML (RFC822 / RFC2822) in a directory and copy them in a
55
Usage:
66

77
```
8-
eml-replicator 0.1.2
8+
eml-replicator 0.1.3
99
Maël Naccache Tüfekçi
1010
A tool that read EML files and copy them to a IMAP mailbox.
1111

src/main.rs

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
#[macro_use]
2+
extern crate lazy_static;
13
use indicatif::ProgressStyle;
4+
use regex::Regex;
25
use std::fs::{self};
36
use std::io::{Error, ErrorKind};
47
use std::path::{Path, PathBuf};
@@ -45,13 +48,18 @@ fn list_eml_file(
4548
}
4649

4750
fn randomize_message_id(eml: &String) -> Result<String, String> {
51+
lazy_static! {
52+
static ref MID_RE: Regex = Regex::new(r"(?imu)^message-id:.+$").unwrap();
53+
}
54+
4855
let mut new_eml = String::new();
4956

50-
let header_pos = eml.find("Message-ID:");
57+
let header_pos = MID_RE.find(eml);
5158
if header_pos.is_none() {
5259
return Err("Could not find Message-ID in the EML.".to_string());
5360
}
54-
let (fpart, lpart) = eml.split_at(header_pos.unwrap());
61+
62+
let (fpart, lpart) = eml.split_at(header_pos.unwrap().start());
5563
new_eml.push_str(fpart);
5664

5765
let (_mid, rest) = lpart.split_at(lpart.find('\n').expect("Malformed Message-ID."));
@@ -95,6 +103,7 @@ impl Config {
95103
let recursive = matches.is_present("recursive");
96104
let symlink = matches.is_present("symlink");
97105
let random_id = matches.is_present("random-message-id");
106+
98107
Config {
99108
server,
100109
port,
@@ -192,6 +201,10 @@ fn main() {
192201
println!("- {}", path.to_str().unwrap_or(""));
193202
}
194203

204+
if conf.random_id {
205+
println!("Randomizing Message-IDs.")
206+
}
207+
195208
let tls = native_tls::TlsConnector::builder().build().unwrap();
196209
let client = imap::connect((conf.server.clone(), conf.port), conf.server, &tls).unwrap();
197210
let mut session = client.login(conf.login, conf.password).unwrap();
@@ -204,10 +217,17 @@ fn main() {
204217
for eml in &emls_files {
205218
let rfc822 = fs::read_to_string(eml).expect("Failed to read eml file.");
206219
if conf.random_id {
207-
let randomize_id = randomize_message_id(&rfc822).unwrap();
208-
session
209-
.append(&conf.folder, &randomize_id)
210-
.expect("Could not copy eml file to inbox.");
220+
let randomize_id = randomize_message_id(&rfc822);
221+
if randomize_id.is_err() {
222+
println!(
223+
"Could not find Message-ID for file {}, skipping.",
224+
eml.to_string_lossy()
225+
);
226+
} else {
227+
session
228+
.append(&conf.folder, &randomize_id.unwrap())
229+
.expect("Could not copy eml file to inbox.");
230+
}
211231
} else {
212232
session
213233
.append(&conf.folder, &rfc822)

0 commit comments

Comments
 (0)