|
|
|
@ -175,227 +175,113 @@ pub fn idle(kit: ImapWatchKit) -> Result<()> {
|
|
|
|
|
let _26_mins = std::time::Duration::from_secs(26 * 60);
|
|
|
|
|
/* duration interval to check other folders for changes */
|
|
|
|
|
let _5_mins = std::time::Duration::from_secs(5 * 60);
|
|
|
|
|
loop {
|
|
|
|
|
while let Some(line) = iter.next() {
|
|
|
|
|
let now = std::time::Instant::now();
|
|
|
|
|
if now.duration_since(beat) >= _26_mins {
|
|
|
|
|
exit_on_error!(
|
|
|
|
|
sender,
|
|
|
|
|
folder_hash,
|
|
|
|
|
work_context,
|
|
|
|
|
thread_id,
|
|
|
|
|
iter.conn.set_nonblocking(true)
|
|
|
|
|
iter.conn.send_raw(b"DONE")
|
|
|
|
|
iter.conn.read_response(&mut response)
|
|
|
|
|
iter.conn.send_command(b"IDLE")
|
|
|
|
|
iter.conn.set_nonblocking(false)
|
|
|
|
|
main_conn.lock().unwrap().send_command(b"NOOP")
|
|
|
|
|
main_conn.lock().unwrap().read_response(&mut response)
|
|
|
|
|
while let Some(line) = iter.next() {
|
|
|
|
|
let now = std::time::Instant::now();
|
|
|
|
|
if now.duration_since(beat) >= _26_mins {
|
|
|
|
|
exit_on_error!(
|
|
|
|
|
sender,
|
|
|
|
|
folder_hash,
|
|
|
|
|
work_context,
|
|
|
|
|
thread_id,
|
|
|
|
|
iter.conn.set_nonblocking(true)
|
|
|
|
|
iter.conn.send_raw(b"DONE")
|
|
|
|
|
iter.conn.read_response(&mut response)
|
|
|
|
|
iter.conn.send_command(b"IDLE")
|
|
|
|
|
iter.conn.set_nonblocking(false)
|
|
|
|
|
main_conn.lock().unwrap().send_command(b"NOOP")
|
|
|
|
|
main_conn.lock().unwrap().read_response(&mut response)
|
|
|
|
|
);
|
|
|
|
|
beat = now;
|
|
|
|
|
}
|
|
|
|
|
if now.duration_since(watch) >= _5_mins {
|
|
|
|
|
/* Time to poll the other inboxes */
|
|
|
|
|
exit_on_error!(
|
|
|
|
|
sender,
|
|
|
|
|
folder_hash,
|
|
|
|
|
work_context,
|
|
|
|
|
thread_id,
|
|
|
|
|
iter.conn.set_nonblocking(true)
|
|
|
|
|
iter.conn.send_raw(b"DONE")
|
|
|
|
|
iter.conn.read_response(&mut response)
|
|
|
|
|
);
|
|
|
|
|
for (hash, folder) in folders.lock().unwrap().iter() {
|
|
|
|
|
if *hash == folder_hash {
|
|
|
|
|
/* Skip INBOX */
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
work_context
|
|
|
|
|
.set_status
|
|
|
|
|
.send((
|
|
|
|
|
thread_id,
|
|
|
|
|
format!("examining `{}` for updates...", folder.path()),
|
|
|
|
|
))
|
|
|
|
|
.unwrap();
|
|
|
|
|
examine_updates(
|
|
|
|
|
folder,
|
|
|
|
|
&sender,
|
|
|
|
|
&mut iter.conn,
|
|
|
|
|
&hash_index,
|
|
|
|
|
&uid_index,
|
|
|
|
|
&work_context,
|
|
|
|
|
);
|
|
|
|
|
beat = now;
|
|
|
|
|
}
|
|
|
|
|
if now.duration_since(watch) >= _5_mins {
|
|
|
|
|
/* Time to poll the other inboxes */
|
|
|
|
|
exit_on_error!(
|
|
|
|
|
sender,
|
|
|
|
|
folder_hash,
|
|
|
|
|
work_context,
|
|
|
|
|
thread_id,
|
|
|
|
|
iter.conn.set_nonblocking(true)
|
|
|
|
|
iter.conn.send_raw(b"DONE")
|
|
|
|
|
iter.conn.read_response(&mut response)
|
|
|
|
|
);
|
|
|
|
|
for (hash, folder) in folders.lock().unwrap().iter() {
|
|
|
|
|
if *hash == folder_hash {
|
|
|
|
|
/* Skip INBOX */
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
work_context
|
|
|
|
|
.set_status
|
|
|
|
|
.send((
|
|
|
|
|
thread_id,
|
|
|
|
|
format!("examining `{}` for updates...", folder.path()),
|
|
|
|
|
))
|
|
|
|
|
.unwrap();
|
|
|
|
|
examine_updates(
|
|
|
|
|
folder,
|
|
|
|
|
&sender,
|
|
|
|
|
&mut iter.conn,
|
|
|
|
|
&hash_index,
|
|
|
|
|
&uid_index,
|
|
|
|
|
&work_context,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
work_context
|
|
|
|
|
.set_status
|
|
|
|
|
.send((thread_id, "done examining mailboxes.".to_string()))
|
|
|
|
|
.unwrap();
|
|
|
|
|
exit_on_error!(
|
|
|
|
|
sender,
|
|
|
|
|
folder_hash,
|
|
|
|
|
work_context,
|
|
|
|
|
thread_id,
|
|
|
|
|
iter.conn.send_command(b"IDLE")
|
|
|
|
|
iter.conn.set_nonblocking(false)
|
|
|
|
|
main_conn.lock().unwrap().send_command(b"NOOP")
|
|
|
|
|
main_conn.lock().unwrap().read_response(&mut response)
|
|
|
|
|
);
|
|
|
|
|
watch = now;
|
|
|
|
|
}
|
|
|
|
|
match protocol_parser::untagged_responses(line.as_slice())
|
|
|
|
|
.to_full_result()
|
|
|
|
|
.map_err(MeliError::from)
|
|
|
|
|
{
|
|
|
|
|
Ok(Some(Recent(r))) => {
|
|
|
|
|
work_context
|
|
|
|
|
.set_status
|
|
|
|
|
.send((thread_id, "done examining mailboxes.".to_string()))
|
|
|
|
|
.send((thread_id, format!("got `{} RECENT` notification", r)))
|
|
|
|
|
.unwrap();
|
|
|
|
|
/* UID SEARCH RECENT */
|
|
|
|
|
exit_on_error!(
|
|
|
|
|
sender,
|
|
|
|
|
folder_hash,
|
|
|
|
|
work_context,
|
|
|
|
|
thread_id,
|
|
|
|
|
iter.conn.send_command(b"IDLE")
|
|
|
|
|
iter.conn.set_nonblocking(false)
|
|
|
|
|
main_conn.lock().unwrap().send_command(b"NOOP")
|
|
|
|
|
main_conn.lock().unwrap().read_response(&mut response)
|
|
|
|
|
iter.conn.set_nonblocking(true)
|
|
|
|
|
iter.conn.send_raw(b"DONE")
|
|
|
|
|
iter.conn.read_response(&mut response)
|
|
|
|
|
iter.conn.send_command(b"UID SEARCH RECENT")
|
|
|
|
|
iter.conn.read_response(&mut response)
|
|
|
|
|
);
|
|
|
|
|
watch = now;
|
|
|
|
|
}
|
|
|
|
|
match protocol_parser::untagged_responses(line.as_slice())
|
|
|
|
|
.to_full_result()
|
|
|
|
|
.map_err(MeliError::from)
|
|
|
|
|
{
|
|
|
|
|
Ok(Some(Recent(r))) => {
|
|
|
|
|
work_context
|
|
|
|
|
.set_status
|
|
|
|
|
.send((thread_id, format!("got `{} RECENT` notification", r)))
|
|
|
|
|
.unwrap();
|
|
|
|
|
/* UID SEARCH RECENT */
|
|
|
|
|
exit_on_error!(
|
|
|
|
|
sender,
|
|
|
|
|
folder_hash,
|
|
|
|
|
work_context,
|
|
|
|
|
thread_id,
|
|
|
|
|
iter.conn.set_nonblocking(true)
|
|
|
|
|
iter.conn.send_raw(b"DONE")
|
|
|
|
|
iter.conn.read_response(&mut response)
|
|
|
|
|
iter.conn.send_command(b"UID SEARCH RECENT")
|
|
|
|
|
iter.conn.read_response(&mut response)
|
|
|
|
|
);
|
|
|
|
|
match protocol_parser::search_results_raw(response.as_bytes())
|
|
|
|
|
.to_full_result()
|
|
|
|
|
.map_err(MeliError::from)
|
|
|
|
|
{
|
|
|
|
|
Ok(&[]) => {
|
|
|
|
|
debug!("UID SEARCH RECENT returned no results");
|
|
|
|
|
}
|
|
|
|
|
Ok(v) => {
|
|
|
|
|
exit_on_error!(
|
|
|
|
|
sender,
|
|
|
|
|
folder_hash,
|
|
|
|
|
work_context,
|
|
|
|
|
thread_id,
|
|
|
|
|
iter.conn.send_command(
|
|
|
|
|
&[b"UID FETCH", v, b"(FLAGS RFC822.HEADER)"]
|
|
|
|
|
.join(&b' '),
|
|
|
|
|
)
|
|
|
|
|
iter.conn.read_response(&mut response)
|
|
|
|
|
);
|
|
|
|
|
debug!(&response);
|
|
|
|
|
match protocol_parser::uid_fetch_response(response.as_bytes())
|
|
|
|
|
.to_full_result()
|
|
|
|
|
.map_err(MeliError::from)
|
|
|
|
|
{
|
|
|
|
|
Ok(v) => {
|
|
|
|
|
let len = v.len();
|
|
|
|
|
let mut ctr = 0;
|
|
|
|
|
for (uid, flags, b) in v {
|
|
|
|
|
work_context
|
|
|
|
|
.set_status
|
|
|
|
|
.send((
|
|
|
|
|
thread_id,
|
|
|
|
|
format!("parsing {}/{} envelopes..", ctr, len),
|
|
|
|
|
))
|
|
|
|
|
.unwrap();
|
|
|
|
|
if let Ok(env) = Envelope::from_bytes(&b, flags) {
|
|
|
|
|
ctr += 1;
|
|
|
|
|
hash_index
|
|
|
|
|
.lock()
|
|
|
|
|
.unwrap()
|
|
|
|
|
.insert(env.hash(), (uid, folder_hash));
|
|
|
|
|
uid_index.lock().unwrap().insert(uid, env.hash());
|
|
|
|
|
debug!(
|
|
|
|
|
"Create event {} {} {}",
|
|
|
|
|
env.hash(),
|
|
|
|
|
env.subject(),
|
|
|
|
|
folder.path(),
|
|
|
|
|
);
|
|
|
|
|
sender.send(RefreshEvent {
|
|
|
|
|
hash: folder_hash,
|
|
|
|
|
kind: Create(Box::new(env)),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
work_context
|
|
|
|
|
.set_status
|
|
|
|
|
.send((
|
|
|
|
|
thread_id,
|
|
|
|
|
format!("parsed {}/{} envelopes.", ctr, len),
|
|
|
|
|
))
|
|
|
|
|
.unwrap();
|
|
|
|
|
}
|
|
|
|
|
Err(e) => {
|
|
|
|
|
debug!(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Err(e) => {
|
|
|
|
|
debug!(
|
|
|
|
|
"UID SEARCH RECENT err: {}\nresp: {}",
|
|
|
|
|
e.to_string(),
|
|
|
|
|
&response
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
match protocol_parser::search_results_raw(response.as_bytes())
|
|
|
|
|
.to_full_result()
|
|
|
|
|
.map_err(MeliError::from)
|
|
|
|
|
{
|
|
|
|
|
Ok(&[]) => {
|
|
|
|
|
debug!("UID SEARCH RECENT returned no results");
|
|
|
|
|
}
|
|
|
|
|
exit_on_error!(
|
|
|
|
|
sender,
|
|
|
|
|
folder_hash,
|
|
|
|
|
work_context,
|
|
|
|
|
thread_id,
|
|
|
|
|
iter.conn.send_command(b"IDLE")
|
|
|
|
|
iter.conn.set_nonblocking(false)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
Ok(Some(Expunge(n))) => {
|
|
|
|
|
work_context
|
|
|
|
|
.set_status
|
|
|
|
|
.send((thread_id, format!("got `{} EXPUNGED` notification", n)))
|
|
|
|
|
.unwrap();
|
|
|
|
|
debug!("expunge {}", n);
|
|
|
|
|
}
|
|
|
|
|
Ok(Some(Exists(n))) => {
|
|
|
|
|
exit_on_error!(
|
|
|
|
|
sender,
|
|
|
|
|
folder_hash,
|
|
|
|
|
work_context,
|
|
|
|
|
thread_id,
|
|
|
|
|
iter.conn.set_nonblocking(true)
|
|
|
|
|
iter.conn.send_raw(b"DONE")
|
|
|
|
|
iter.conn.read_response(&mut response)
|
|
|
|
|
);
|
|
|
|
|
/* UID FETCH ALL UID, cross-ref, then FETCH difference headers
|
|
|
|
|
* */
|
|
|
|
|
let mut prev_exists = folder.exists.lock().unwrap();
|
|
|
|
|
debug!("exists {}", n);
|
|
|
|
|
work_context
|
|
|
|
|
.set_status
|
|
|
|
|
.send((
|
|
|
|
|
thread_id,
|
|
|
|
|
format!(
|
|
|
|
|
"got `{} EXISTS` notification (EXISTS was previously {} for {}",
|
|
|
|
|
n,
|
|
|
|
|
*prev_exists,
|
|
|
|
|
folder.path()
|
|
|
|
|
),
|
|
|
|
|
))
|
|
|
|
|
.unwrap();
|
|
|
|
|
if n > *prev_exists {
|
|
|
|
|
Ok(v) => {
|
|
|
|
|
exit_on_error!(
|
|
|
|
|
sender,
|
|
|
|
|
folder_hash,
|
|
|
|
|
work_context,
|
|
|
|
|
thread_id,
|
|
|
|
|
iter.conn.send_command(
|
|
|
|
|
&[
|
|
|
|
|
b"FETCH",
|
|
|
|
|
format!("{}:{}", *prev_exists + 1, n).as_bytes(),
|
|
|
|
|
b"(UID FLAGS RFC822.HEADER)",
|
|
|
|
|
]
|
|
|
|
|
&[b"UID FETCH", v, b"(FLAGS RFC822.HEADER)"]
|
|
|
|
|
.join(&b' '),
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
iter.conn.read_response(&mut response)
|
|
|
|
|
);
|
|
|
|
|
debug!(&response);
|
|
|
|
|
match protocol_parser::uid_fetch_response(response.as_bytes())
|
|
|
|
|
.to_full_result()
|
|
|
|
|
.map_err(MeliError::from)
|
|
|
|
@ -411,10 +297,6 @@ pub fn idle(kit: ImapWatchKit) -> Result<()> {
|
|
|
|
|
format!("parsing {}/{} envelopes..", ctr, len),
|
|
|
|
|
))
|
|
|
|
|
.unwrap();
|
|
|
|
|
if uid_index.lock().unwrap().contains_key(&uid) {
|
|
|
|
|
ctr += 1;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if let Ok(env) = Envelope::from_bytes(&b, flags) {
|
|
|
|
|
ctr += 1;
|
|
|
|
|
hash_index
|
|
|
|
@ -443,29 +325,151 @@ pub fn idle(kit: ImapWatchKit) -> Result<()> {
|
|
|
|
|
debug!(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
*prev_exists = n;
|
|
|
|
|
} else if n < *prev_exists {
|
|
|
|
|
*prev_exists = n;
|
|
|
|
|
}
|
|
|
|
|
Err(e) => {
|
|
|
|
|
debug!(
|
|
|
|
|
"UID SEARCH RECENT err: {}\nresp: {}",
|
|
|
|
|
e.to_string(),
|
|
|
|
|
&response
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
exit_on_error!(
|
|
|
|
|
sender,
|
|
|
|
|
folder_hash,
|
|
|
|
|
work_context,
|
|
|
|
|
thread_id,
|
|
|
|
|
iter.conn.send_command(b"IDLE")
|
|
|
|
|
iter.conn.set_nonblocking(false)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
Ok(Some(Expunge(n))) => {
|
|
|
|
|
work_context
|
|
|
|
|
.set_status
|
|
|
|
|
.send((thread_id, format!("got `{} EXPUNGED` notification", n)))
|
|
|
|
|
.unwrap();
|
|
|
|
|
debug!("expunge {}", n);
|
|
|
|
|
}
|
|
|
|
|
Ok(Some(Exists(n))) => {
|
|
|
|
|
exit_on_error!(
|
|
|
|
|
sender,
|
|
|
|
|
folder_hash,
|
|
|
|
|
work_context,
|
|
|
|
|
thread_id,
|
|
|
|
|
iter.conn.set_nonblocking(true)
|
|
|
|
|
iter.conn.send_raw(b"DONE")
|
|
|
|
|
iter.conn.read_response(&mut response)
|
|
|
|
|
);
|
|
|
|
|
/* UID FETCH ALL UID, cross-ref, then FETCH difference headers
|
|
|
|
|
* */
|
|
|
|
|
let mut prev_exists = folder.exists.lock().unwrap();
|
|
|
|
|
debug!("exists {}", n);
|
|
|
|
|
work_context
|
|
|
|
|
.set_status
|
|
|
|
|
.send((
|
|
|
|
|
thread_id,
|
|
|
|
|
format!(
|
|
|
|
|
"got `{} EXISTS` notification (EXISTS was previously {} for {}",
|
|
|
|
|
n,
|
|
|
|
|
*prev_exists,
|
|
|
|
|
folder.path()
|
|
|
|
|
),
|
|
|
|
|
))
|
|
|
|
|
.unwrap();
|
|
|
|
|
if n > *prev_exists {
|
|
|
|
|
exit_on_error!(
|
|
|
|
|
sender,
|
|
|
|
|
folder_hash,
|
|
|
|
|
work_context,
|
|
|
|
|
thread_id,
|
|
|
|
|
iter.conn.send_command(b"IDLE")
|
|
|
|
|
iter.conn.set_nonblocking(false)
|
|
|
|
|
iter.conn.send_command(
|
|
|
|
|
&[
|
|
|
|
|
b"FETCH",
|
|
|
|
|
format!("{}:{}", *prev_exists + 1, n).as_bytes(),
|
|
|
|
|
b"(UID FLAGS RFC822.HEADER)",
|
|
|
|
|
]
|
|
|
|
|
.join(&b' '),
|
|
|
|
|
)
|
|
|
|
|
iter.conn.read_response(&mut response)
|
|
|
|
|
);
|
|
|
|
|
match protocol_parser::uid_fetch_response(response.as_bytes())
|
|
|
|
|
.to_full_result()
|
|
|
|
|
.map_err(MeliError::from)
|
|
|
|
|
{
|
|
|
|
|
Ok(v) => {
|
|
|
|
|
let len = v.len();
|
|
|
|
|
let mut ctr = 0;
|
|
|
|
|
for (uid, flags, b) in v {
|
|
|
|
|
work_context
|
|
|
|
|
.set_status
|
|
|
|
|
.send((
|
|
|
|
|
thread_id,
|
|
|
|
|
format!("parsing {}/{} envelopes..", ctr, len),
|
|
|
|
|
))
|
|
|
|
|
.unwrap();
|
|
|
|
|
if uid_index.lock().unwrap().contains_key(&uid) {
|
|
|
|
|
ctr += 1;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if let Ok(env) = Envelope::from_bytes(&b, flags) {
|
|
|
|
|
ctr += 1;
|
|
|
|
|
hash_index
|
|
|
|
|
.lock()
|
|
|
|
|
.unwrap()
|
|
|
|
|
.insert(env.hash(), (uid, folder_hash));
|
|
|
|
|
uid_index.lock().unwrap().insert(uid, env.hash());
|
|
|
|
|
debug!(
|
|
|
|
|
"Create event {} {} {}",
|
|
|
|
|
env.hash(),
|
|
|
|
|
env.subject(),
|
|
|
|
|
folder.path(),
|
|
|
|
|
);
|
|
|
|
|
sender.send(RefreshEvent {
|
|
|
|
|
hash: folder_hash,
|
|
|
|
|
kind: Create(Box::new(env)),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
work_context
|
|
|
|
|
.set_status
|
|
|
|
|
.send((thread_id, format!("parsed {}/{} envelopes.", ctr, len)))
|
|
|
|
|
.unwrap();
|
|
|
|
|
}
|
|
|
|
|
Err(e) => {
|
|
|
|
|
debug!(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
*prev_exists = n;
|
|
|
|
|
} else if n < *prev_exists {
|
|
|
|
|
*prev_exists = n;
|
|
|
|
|
}
|
|
|
|
|
Ok(None) | Err(_) => {}
|
|
|
|
|
exit_on_error!(
|
|
|
|
|
sender,
|
|
|
|
|
folder_hash,
|
|
|
|
|
work_context,
|
|
|
|
|
thread_id,
|
|
|
|
|
iter.conn.send_command(b"IDLE")
|
|
|
|
|
iter.conn.set_nonblocking(false)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
work_context
|
|
|
|
|
.set_status
|
|
|
|
|
.send((thread_id, "IDLEing".to_string()))
|
|
|
|
|
.unwrap();
|
|
|
|
|
Ok(None) | Err(_) => {}
|
|
|
|
|
}
|
|
|
|
|
work_context
|
|
|
|
|
.set_status
|
|
|
|
|
.send((thread_id, "IDLEing".to_string()))
|
|
|
|
|
.unwrap();
|
|
|
|
|
}
|
|
|
|
|
Ok(())
|
|
|
|
|
debug!("IDLE exited");
|
|
|
|
|
work_context
|
|
|
|
|
.set_status
|
|
|
|
|
.send((thread_id, "IDLE exited".to_string()))
|
|
|
|
|
.unwrap();
|
|
|
|
|
sender.send(RefreshEvent {
|
|
|
|
|
hash: folder_hash,
|
|
|
|
|
kind: RefreshEventKind::Failure(MeliError::new("IDLE exited".to_string())),
|
|
|
|
|
});
|
|
|
|
|
Err(MeliError::new("IDLE exited".to_string()))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn examine_updates(
|
|
|
|
|