Permalink
Browse files

Retry connect, and connect in parallel

  • Loading branch information...
jonhoo committed Mar 3, 2017
1 parent d4e3e91 commit 90b1602ad1c2d6a1c3836efb2dfe11d8157c2255
Showing with 74 additions and 13 deletions.
  1. +31 −0 Cargo.lock
  2. +1 −0 Cargo.toml
  3. +42 −13 src/main.rs

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
@@ -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"
@@ -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;
@@ -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();

0 comments on commit 90b1602

Please sign in to comment.