From 7bbfd188efaba578c047a4bb3423a9a858b1e13c Mon Sep 17 00:00:00 2001 From: Manos Pitsidianakis Date: Mon, 27 Jul 2020 12:31:50 +0300 Subject: [PATCH] melib/imap: move current_mailbox to ImapStream ImapStream holds the connection state (current command id), so it makes sense to move current_mailbox state there. That way, when a connection drops for whatever reason the old current_mailbox is dropped and not carried over to new connections. --- melib/src/backends/imap.rs | 5 +++-- melib/src/backends/imap/connection.rs | 23 +++++++++++++---------- melib/src/backends/imap/untagged.rs | 3 +-- 3 files changed, 17 insertions(+), 14 deletions(-) diff --git a/melib/src/backends/imap.rs b/melib/src/backends/imap.rs index aec553b4..5b125868 100644 --- a/melib/src/backends/imap.rs +++ b/melib/src/backends/imap.rs @@ -752,9 +752,10 @@ impl MailBackend for ImapType { let mut response = String::with_capacity(8 * 1024); { let mut conn_lck = connection.lock().await; + let current_mailbox = conn_lck.stream.as_ref()?.current_mailbox; if !no_select - && (conn_lck.current_mailbox == MailboxSelection::Examine(mailbox_hash) - || conn_lck.current_mailbox == MailboxSelection::Select(mailbox_hash)) + && (current_mailbox == MailboxSelection::Examine(mailbox_hash) + || current_mailbox == MailboxSelection::Select(mailbox_hash)) { /* make sure mailbox is not selected before it gets deleted, otherwise * connection gets dropped by server */ diff --git a/melib/src/backends/imap/connection.rs b/melib/src/backends/imap/connection.rs index 2c736a66..aa268562 100644 --- a/melib/src/backends/imap/connection.rs +++ b/melib/src/backends/imap/connection.rs @@ -46,9 +46,10 @@ pub enum ImapProtocol { #[derive(Debug)] pub struct ImapStream { - cmd_id: usize, - stream: AsyncWrapper, - protocol: ImapProtocol, + pub cmd_id: usize, + pub stream: AsyncWrapper, + pub protocol: ImapProtocol, + pub current_mailbox: MailboxSelection, } #[derive(Debug, Copy, Clone, Eq, PartialEq)] @@ -73,7 +74,6 @@ pub struct ImapConnection { pub stream: Result, pub server_conf: ImapServerConf, pub uid_store: Arc, - pub current_mailbox: MailboxSelection, } impl Drop for ImapStream { @@ -220,6 +220,7 @@ impl ImapStream { cmd_id, stream, protocol: server_conf.protocol, + current_mailbox: MailboxSelection::None, }; if let ImapProtocol::ManageSieve = server_conf.protocol { use data_encoding::BASE64; @@ -476,7 +477,6 @@ impl ImapConnection { stream: Err(MeliError::new("Offline".to_string())), server_conf: server_conf.clone(), uid_store, - current_mailbox: MailboxSelection::None, } } @@ -615,7 +615,8 @@ impl ImapConnection { ret: &mut String, force: bool, ) -> Result<()> { - if !force && self.current_mailbox == MailboxSelection::Select(mailbox_hash) { + if !force && self.stream.as_ref()?.current_mailbox == MailboxSelection::Select(mailbox_hash) + { return Ok(()); } self.send_command( @@ -629,7 +630,7 @@ impl ImapConnection { self.read_response(ret, RequiredResponses::SELECT_REQUIRED) .await?; debug!("select response {}", ret); - self.current_mailbox = MailboxSelection::Select(mailbox_hash); + self.stream.as_mut()?.current_mailbox = MailboxSelection::Select(mailbox_hash); Ok(()) } @@ -639,7 +640,9 @@ impl ImapConnection { ret: &mut String, force: bool, ) -> Result<()> { - if !force && self.current_mailbox == MailboxSelection::Examine(mailbox_hash) { + if !force + && self.stream.as_ref()?.current_mailbox == MailboxSelection::Examine(mailbox_hash) + { return Ok(()); } self.send_command( @@ -653,12 +656,12 @@ impl ImapConnection { self.read_response(ret, RequiredResponses::EXAMINE_REQUIRED) .await?; debug!("examine response {}", ret); - self.current_mailbox = MailboxSelection::Examine(mailbox_hash); + self.stream.as_mut()?.current_mailbox = MailboxSelection::Examine(mailbox_hash); Ok(()) } pub async fn unselect(&mut self) -> Result<()> { - match self.current_mailbox.take() { + match self.stream.as_mut()?.current_mailbox.take() { MailboxSelection::Examine(mailbox_hash) | MailboxSelection::Select(mailbox_hash) =>{ let mut response = String::with_capacity(8 * 1024); diff --git a/melib/src/backends/imap/untagged.rs b/melib/src/backends/imap/untagged.rs index 6887701a..43ae2b20 100644 --- a/melib/src/backends/imap/untagged.rs +++ b/melib/src/backends/imap/untagged.rs @@ -51,8 +51,7 @@ impl ImapConnection { } else { Ok(()) }?;)+ }; } - //FIXME - let mailbox_hash = match self.current_mailbox { + let mailbox_hash = match self.stream.as_ref()?.current_mailbox { MailboxSelection::Select(h) | MailboxSelection::Examine(h) => h, MailboxSelection::None => return Ok(false), };