Skip to content

Commit

Permalink
feat: add a loading bar and an option to randomize the message-id.
Browse files Browse the repository at this point in the history
  • Loading branch information
maeln committed Jan 21, 2021
1 parent a910cd5 commit 9066ace
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 29 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/target
/eml
*.eml
3 changes: 2 additions & 1 deletion Cargo.lock

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

5 changes: 3 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.0"
version = "0.1.1"
authors = ["Maël Naccache Tüfekçi <[email protected]>"]
edition = "2018"
license = "CECILL-2.1"
Expand All @@ -16,4 +16,5 @@ imap = "2.4"
native-tls = "0.2"
clap = "2.33"
walkdir = "2"
indicatif = "0.15.0"
indicatif = "0.15"
rand = "0.8"
15 changes: 6 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,20 @@ This tool read all the EML (RFC822 / RFC2822) in a directory and copy them in a
Usage:

```
eml-replicator 1.0
eml-replicator 1.1
Maël Naccache Tüfekçi
A tool that read EML files and copy them to a IMAP mailbox.
USAGE:
eml-replicator [FLAGS] [OPTIONS] <IMAP_SERVER> <DIR>
eml-replicator.exe [FLAGS] [OPTIONS] <IMAP_SERVER> <DIR>
FLAGS:
-h, --help Prints help information
-r, --recursive Goes through the directory recursively to find EML files.
-s, --follow-symlink Follow symlink when crawling the directory recursively.
-V, --version Prints version information
-h, --help Prints help information
-r, --recursive Goes through the directory recursively to find EML files.
-s, --follow-symlink Follow symlink when crawling the directory recursively.
-V, --version Prints version information
OPTIONS:
-f, --folder <FOLDER> IMAP Folder in which to put the EMLs. [default: INBOX]
-l, --login <LOGIN> login of the mailbox. [default: ]
-p, --password <PASSWORD> password of the mailbox. [default: ]
--port <IMAP_SERVER_PORT> Port to connect to the imap server. [default: 993]
ARGS:
Expand Down
72 changes: 55 additions & 17 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,10 @@ use std::io::{Error, ErrorKind};
use std::path::{Path, PathBuf};

use clap::{App, Arg, ArgMatches};
use rand::distributions::Alphanumeric;
use rand::Rng;
use walkdir::WalkDir;

#[derive(Clone, Debug)]
struct Config {
server: String,
port: u16,
login: String,
password: String,
folder: String,
directory: String,
recursive: bool,
symlink: bool,
}

fn push_ext(list: &mut Vec<PathBuf>, entry: &Path, ext_used: &str) {
let path = entry;
if path.is_file() {
Expand Down Expand Up @@ -54,6 +44,41 @@ fn list_eml_file(
));
}

fn randomize_message_id(eml: &String) -> Result<String, String> {
let mut new_eml = String::new();

let header_pos = eml.find("Message-ID:");
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());
new_eml.push_str(fpart);

let (_mid, rest) = lpart.split_at(lpart.find('\n').expect("Malformed Message-ID."));
new_eml.push_str("Message-ID: ");
let rand_string: String = rand::thread_rng()
.sample_iter(&Alphanumeric)
.take(30)
.map(char::from)
.collect();
new_eml.push_str(&rand_string);
new_eml.push_str(rest);
return Ok(new_eml);
}

#[derive(Clone, Debug)]
struct Config {
server: String,
port: u16,
login: String,
password: String,
folder: String,
directory: String,
recursive: bool,
symlink: bool,
random_id: bool,
}

impl Config {
pub fn new(matches: ArgMatches) -> Config {
let server = String::from(matches.value_of("server").unwrap());
Expand All @@ -69,7 +94,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 All @@ -79,13 +104,14 @@ impl Config {
directory,
recursive,
symlink,
random_id,
}
}
}

fn main() {
let matches = App::new("eml-replicator")
.version("1.0")
.version("1.1")
.author("Maël Naccache Tüfekçi")
.about("A tool that read EML files and copy them to a IMAP mailbox.")
.arg(
Expand Down Expand Up @@ -143,6 +169,11 @@ fn main() {
.long("follow-symlink")
.help("Follow symlink when crawling the directory recursively."),
)
.arg(
Arg::with_name("random-message-id")
.long("random-message-id")
.help("Randomize the Message-ID in the emls before sending them."),
)
.arg(
Arg::with_name("DIR")
.help("Directory in which to get the EML files.")
Expand Down Expand Up @@ -172,9 +203,16 @@ fn main() {
bar.set_message("EML Copied");
for eml in &emls_files {
let rfc822 = fs::read_to_string(eml).expect("Failed to read eml file.");
session
.append(&conf.folder, &rfc822)
.expect("Could not copy eml file to inbox.");
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.");
} else {
session
.append(&conf.folder, &rfc822)
.expect("Could not copy eml file to inbox.");
}
bar.inc(1);
}
bar.finish();
Expand Down

0 comments on commit 9066ace

Please sign in to comment.