-
Notifications
You must be signed in to change notification settings - Fork 87
/
Copy pathidle.rs
82 lines (68 loc) · 2.4 KB
/
idle.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
use structopt::StructOpt;
#[derive(StructOpt, Debug)]
#[structopt(name = "idle")]
struct Opt {
// The server name to connect to
#[structopt(short, long)]
server: String,
// The port to use
#[structopt(short, long, default_value = "993")]
port: u16,
// The account username
#[structopt(short, long)]
username: String,
// The account password. In a production system passwords
// would normally be in a config or fetched at runtime from
// a password manager or user prompt and not passed on the
// command line.
#[structopt(short = "w", long)]
password: String,
// The mailbox to IDLE on
#[structopt(short, long, default_value = "INBOX")]
mailbox: String,
#[structopt(
short = "x",
long,
help = "The number of responses to receive before exiting",
default_value = "5"
)]
max_responses: usize,
}
fn main() {
let opt = Opt::from_args();
let client = imap::ClientBuilder::new(opt.server.clone(), opt.port)
.connect()
.expect("Could not connect to imap server");
let mut imap = client
.login(opt.username, opt.password)
.expect("Could not authenticate");
// Turn on debug output so we can see the actual traffic coming
// from the server and how it is handled in our callback.
// This wouldn't be turned on in a production build, but is helpful
// in examples and for debugging.
imap.debug = true;
imap.select(opt.mailbox).expect("Could not select mailbox");
// Implement a trivial counter that causes the IDLE callback to end the IDLE
// after a fixed number of responses.
//
// A threaded client could use channels or shared data to interact with the
// rest of the program and update mailbox state, decide to exit the IDLE, etc.
let mut num_responses = 0;
let max_responses = opt.max_responses;
let idle_result = imap.idle().wait_while(|response| {
num_responses += 1;
println!("IDLE response #{}: {:?}", num_responses, response);
if num_responses >= max_responses {
// Stop IDLE
false
} else {
// Continue IDLE
true
}
});
match idle_result {
Ok(reason) => println!("IDLE finished normally {:?}", reason),
Err(e) => println!("IDLE finished with error {:?}", e),
}
imap.logout().expect("Could not log out");
}