From 06f628c33bc072abc30633b2bb7e014cf05a5a3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20DOUIN?= Date: Sun, 25 Apr 2021 16:20:24 +0200 Subject: [PATCH] fix idle mode after network interruption (#123) --- CHANGELOG.md | 2 ++ src/imap/model.rs | 63 +++++++++++++++++++++++------------------------ wiki | 2 +- 3 files changed, 34 insertions(+), 33 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6a2e5b9..6bdbc3a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,6 +24,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Improve config compatibility on Windows [#111](https://github.com/soywod/himalaya/pull/111) - Vim table containing emoji [#122] +- IDLE mode after network interruption [#123] ## [0.2.6] - 2021-04-17 @@ -203,5 +204,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 [#96]: https://github.com/soywod/himalaya/issues/96 [#100]: https://github.com/soywod/himalaya/issues/100 [#122]: https://github.com/soywod/himalaya/issues/122 +[#123]: https://github.com/soywod/himalaya/issues/123 [#125]: https://github.com/soywod/himalaya/issues/125 [#126]: https://github.com/soywod/himalaya/issues/126 diff --git a/src/imap/model.rs b/src/imap/model.rs index 189bf35..1841434 100644 --- a/src/imap/model.rs +++ b/src/imap/model.rs @@ -95,71 +95,70 @@ impl<'ic> ImapConnector<'ic> { } fn search_new_msgs(&mut self) -> Result> { - debug!("[imap::model::search_new_msgs] begin"); - - let seqs: Vec = self + let uids: Vec = self .sess - .search("NEW") + .uid_search("NEW") .chain_err(|| "Could not search new messages")? .into_iter() .collect(); - debug!( - "[imap::model::search_new_msgs] found {} new messages", - seqs.len() - ); - trace!("[imap::model::search_new_msgs] {:?}", seqs); + debug!("found {} new messages", uids.len()); + trace!("uids: {:?}", uids); - Ok(seqs) + Ok(uids) } pub fn idle(&mut self, config: &Config, mbox: &str) -> Result<()> { - debug!("begin"); - - debug!("examine mailbox {}", mbox); + debug!("examine mailbox: {}", mbox); self.sess .examine(mbox) .chain_err(|| format!("Could not examine mailbox `{}`", mbox))?; - debug!("init message hashset"); - let mut msg_set: HashSet = HashSet::from_iter(self.search_new_msgs()?.iter().cloned()); - trace!("{:?}", msg_set); + debug!("init messages hashset"); + let mut msgs_set: HashSet = + HashSet::from_iter(self.search_new_msgs()?.iter().cloned()); + trace!("messages hashset: {:?}", msgs_set); loop { debug!("begin loop"); - self.sess .idle() - .and_then(|idle| idle.wait_keepalive()) - .chain_err(|| "Could not enter in idle mode")?; + .and_then(|mut idle| { + idle.set_keepalive(std::time::Duration::new(300, 0)); + idle.wait_keepalive() + }) + .chain_err(|| "Could not start the idle mode")?; - let new_msgs: Vec = self + let uids: Vec = self .search_new_msgs()? .into_iter() - .filter(|seq| msg_set.get(&seq).is_none()) + .filter(|uid| msgs_set.get(&uid).is_none()) .collect(); - debug!("found {} new messages not in hashset", new_msgs.len()); - trace!("messages: {:?}", new_msgs); + debug!("found {} new messages not in hashset", uids.len()); + trace!("messages hashet: {:?}", msgs_set); - if !new_msgs.is_empty() { - let new_msgs = new_msgs + if !uids.is_empty() { + let uids = uids .iter() - .map(|seq| seq.to_string()) + .map(|uid| uid.to_string()) .collect::>() .join(","); let fetches = self .sess - .fetch(new_msgs, "(ENVELOPE)") - .chain_err(|| "Cannot fetch new messages enveloppe")?; + .uid_fetch(uids, "(ENVELOPE)") + .chain_err(|| "Could not fetch new messages enveloppe")?; for fetch in fetches.iter() { let msg = Msg::from(fetch); + let uid = fetch.uid.ok_or_else(|| { + format!("Could not retrieve message {}'s UID", fetch.message) + })?; config.run_notify_cmd(&msg.subject, &msg.sender)?; - debug!("notify message {}", fetch.message); + debug!("notify message: {}", uid); trace!("message: {:?}", msg); - debug!("insert msg {} to hashset", fetch.message); - msg_set.insert(fetch.message); - trace!("messages: {:?}", msg_set); + debug!("insert message {} in hashset", uid); + msgs_set.insert(uid); + trace!("messages hashset: {:?}", msgs_set); } } diff --git a/wiki b/wiki index a022c95..0befc18 160000 --- a/wiki +++ b/wiki @@ -1 +1 @@ -Subproject commit a022c9580de7f4837cd1e6b028f8da7be96787b8 +Subproject commit 0befc18a2857a97d3adf2b11f5c1d6233c9cf8c6