Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Retry connect, and connect in parallel
  • Loading branch information
jonhoo committed Mar 3, 2017
1 parent d4e3e91 commit 90b1602
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 13 deletions.
31 changes: 31 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Expand Up @@ -23,3 +23,4 @@ mailparse = "0.5.1"
toml = "0.3.1"
xdg = "2.0.0"
notify-rust = "3.2.1"
rayon = "0.6.0"
55 changes: 42 additions & 13 deletions src/main.rs
@@ -1,17 +1,20 @@
extern crate xdg;
extern crate toml;
extern crate imap;
extern crate rayon;
extern crate openssl;
extern crate systray;
extern crate mailparse;
extern crate notify_rust;

use openssl::ssl::{SslContext, SslMethod};
use imap::client::Client;
use rayon::prelude::*;

use std::collections::HashSet;
use std::process::Command;
use std::io::prelude::*;
use std::time::Duration;
use std::sync::mpsc;
use std::fs::File;
use std::thread;
Expand Down Expand Up @@ -122,23 +125,49 @@ fn main() {
// TODO: w.set_tooltip(&"Whatever".to_string());
// TODO: app.wait_for_message();

let accounts: Vec<_> = accounts.into_iter()
let accounts: Vec<_> = accounts.par_iter()
.filter_map(|a| {
let name = a.name;
Client::secure_connect(a.server, SslContext::new(SslMethod::Sslv23).unwrap())
.map(move |mut c| {
c.login(a.username, &a.password).unwrap();
assert!(c.capability().unwrap().iter().any(|c| c == "IDLE"));
c.select("INBOX").unwrap();
(String::from(name), c)
})
.map_err(move |e| {
println!("Failed to connect account {}: {}", name, e);
})
.ok()
let mut wait = 1;
for _ in 0..5 {
let c = Client::secure_connect(a.server,
SslContext::new(SslMethod::Sslv23).unwrap())
.and_then(|mut c| {
try!(c.login(a.username, &a.password));
let cap = try!(c.capability());
if !cap.iter().any(|c| c == "IDLE") {
return Err(imap::error::Error::BadResponse(cap));
}
try!(c.select("INBOX"));
Ok((String::from(a.name), c))
});

match c {
Ok(c) => return Some(c),
Err(imap::error::Error::Io(e)) => {
println!("Failed to connect account {}: {}; retrying in {}s",
a.name,
e,
wait);
thread::sleep(Duration::from_secs(wait));
}
Err(e) => {
println!("{} host produced bad IMAP tunnel: {}", a.name, e);
break;
}
}

wait *= 2;
}

None
})
.collect();

if accounts.is_empty() {
println!("No accounts in config worked; exiting...");
return;
}

// We have now connected
app.set_icon_from_file(&"/usr/share/icons/Faenza/stock/24/stock_connect.png".to_string()).ok();

Expand Down

0 comments on commit 90b1602

Please sign in to comment.