Skip to content

Commit

Permalink
feature: try to reconnect if the session return an error.
Browse files Browse the repository at this point in the history
  • Loading branch information
maeln committed Feb 16, 2021
1 parent 770f3e0 commit 04c41d8
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "eml-replicator"
version = "0.1.3"
version = "0.1.4"
authors = ["Maël Naccache Tüfekçi <contact@maeln.com>"]
edition = "2018"
license = "CECILL-2.1"
Expand Down
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.3
eml-replicator 0.1.4
Maël Naccache Tüfekçi
A tool that read EML files and copy them to a IMAP mailbox.
Expand Down
27 changes: 18 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
extern crate lazy_static;
use indicatif::ProgressStyle;
use regex::Regex;
use std::fs::{self};
use std::fs;
use std::io::{Error, ErrorKind};
use std::path::{Path, PathBuf};

Expand Down Expand Up @@ -206,16 +206,17 @@ fn main() {
}

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();
let client =
imap::connect((conf.server.clone(), conf.port.clone()), &conf.server, &tls).unwrap();
let mut session = client.login(&conf.login, &conf.password).unwrap();
let bar = indicatif::ProgressBar::new(emls_files.len() as u64);
bar.set_style(
ProgressStyle::default_bar()
.template("[{elapsed_precise}] {msg} {pos:>7}/{len:7} {bar:40.cyan/blue}"),
);
bar.set_message("EML Copied");
for eml in &emls_files {
let rfc822 = fs::read_to_string(eml).expect("Failed to read eml file.");
let mut rfc822 = fs::read_to_string(eml).expect("Failed to read eml file.");
if conf.random_id {
let randomize_id = randomize_message_id(&rfc822);
if randomize_id.is_err() {
Expand All @@ -224,15 +225,23 @@ fn main() {
eml.to_string_lossy()
);
} else {
session
.append(&conf.folder, &randomize_id.unwrap())
.expect("Could not copy eml file to inbox.");
rfc822 = randomize_id.unwrap();
}
} else {
}

let send_res = session.append(&conf.folder, &rfc822);
if send_res.is_err() {
// we might have been disconnected so we retry.
let _ = session.close();
let new_client =
imap::connect((conf.server.clone(), conf.port.clone()), &conf.server, &tls)
.unwrap();
session = new_client.login(&conf.login, &conf.password).unwrap();
session
.append(&conf.folder, &rfc822)
.expect("Could not copy eml file to inbox.");
.expect("Could not copy email.");
}

bar.inc(1);
}
bar.finish();
Expand Down

0 comments on commit 04c41d8

Please sign in to comment.