From 04c41d82443df7c9f197142b89491af8fb90f0a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABl=20Naccache=20T=C3=BCfek=C3=A7i?= Date: Tue, 16 Feb 2021 15:03:40 +0100 Subject: [PATCH] feature: try to reconnect if the session return an error. --- Cargo.lock | 2 +- Cargo.toml | 2 +- README.md | 2 +- src/main.rs | 27 ++++++++++++++++++--------- 4 files changed, 21 insertions(+), 12 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2de6a01..2ae87e0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -138,7 +138,7 @@ checksum = "ea221b5284a47e40033bf9b66f35f984ec0ea2931eb03505246cd27a963f981b" [[package]] name = "eml-replicator" -version = "0.1.3" +version = "0.1.4" dependencies = [ "clap", "imap", diff --git a/Cargo.toml b/Cargo.toml index 6364021..e96b784 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "eml-replicator" -version = "0.1.3" +version = "0.1.4" authors = ["Maël Naccache Tüfekçi "] edition = "2018" license = "CECILL-2.1" diff --git a/README.md b/README.md index bbbc4fe..98399f1 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.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. diff --git a/src/main.rs b/src/main.rs index 67d1484..047ef40 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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}; @@ -206,8 +206,9 @@ 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() @@ -215,7 +216,7 @@ 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."); + 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() { @@ -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();