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.
memfd
Manos Pitsidianakis 2020-07-27 12:31:50 +03:00
parent 2db983ae1f
commit 7bbfd188ef
Signed by: Manos Pitsidianakis
GPG Key ID: 73627C2F690DF710
3 changed files with 17 additions and 14 deletions

View File

@ -752,9 +752,10 @@ impl MailBackend for ImapType {
let mut response = String::with_capacity(8 * 1024); let mut response = String::with_capacity(8 * 1024);
{ {
let mut conn_lck = connection.lock().await; let mut conn_lck = connection.lock().await;
let current_mailbox = conn_lck.stream.as_ref()?.current_mailbox;
if !no_select if !no_select
&& (conn_lck.current_mailbox == MailboxSelection::Examine(mailbox_hash) && (current_mailbox == MailboxSelection::Examine(mailbox_hash)
|| conn_lck.current_mailbox == MailboxSelection::Select(mailbox_hash)) || current_mailbox == MailboxSelection::Select(mailbox_hash))
{ {
/* make sure mailbox is not selected before it gets deleted, otherwise /* make sure mailbox is not selected before it gets deleted, otherwise
* connection gets dropped by server */ * connection gets dropped by server */

View File

@ -46,9 +46,10 @@ pub enum ImapProtocol {
#[derive(Debug)] #[derive(Debug)]
pub struct ImapStream { pub struct ImapStream {
cmd_id: usize, pub cmd_id: usize,
stream: AsyncWrapper<Connection>, pub stream: AsyncWrapper<Connection>,
protocol: ImapProtocol, pub protocol: ImapProtocol,
pub current_mailbox: MailboxSelection,
} }
#[derive(Debug, Copy, Clone, Eq, PartialEq)] #[derive(Debug, Copy, Clone, Eq, PartialEq)]
@ -73,7 +74,6 @@ pub struct ImapConnection {
pub stream: Result<ImapStream>, pub stream: Result<ImapStream>,
pub server_conf: ImapServerConf, pub server_conf: ImapServerConf,
pub uid_store: Arc<UIDStore>, pub uid_store: Arc<UIDStore>,
pub current_mailbox: MailboxSelection,
} }
impl Drop for ImapStream { impl Drop for ImapStream {
@ -220,6 +220,7 @@ impl ImapStream {
cmd_id, cmd_id,
stream, stream,
protocol: server_conf.protocol, protocol: server_conf.protocol,
current_mailbox: MailboxSelection::None,
}; };
if let ImapProtocol::ManageSieve = server_conf.protocol { if let ImapProtocol::ManageSieve = server_conf.protocol {
use data_encoding::BASE64; use data_encoding::BASE64;
@ -476,7 +477,6 @@ impl ImapConnection {
stream: Err(MeliError::new("Offline".to_string())), stream: Err(MeliError::new("Offline".to_string())),
server_conf: server_conf.clone(), server_conf: server_conf.clone(),
uid_store, uid_store,
current_mailbox: MailboxSelection::None,
} }
} }
@ -615,7 +615,8 @@ impl ImapConnection {
ret: &mut String, ret: &mut String,
force: bool, force: bool,
) -> Result<()> { ) -> Result<()> {
if !force && self.current_mailbox == MailboxSelection::Select(mailbox_hash) { if !force && self.stream.as_ref()?.current_mailbox == MailboxSelection::Select(mailbox_hash)
{
return Ok(()); return Ok(());
} }
self.send_command( self.send_command(
@ -629,7 +630,7 @@ impl ImapConnection {
self.read_response(ret, RequiredResponses::SELECT_REQUIRED) self.read_response(ret, RequiredResponses::SELECT_REQUIRED)
.await?; .await?;
debug!("select response {}", ret); debug!("select response {}", ret);
self.current_mailbox = MailboxSelection::Select(mailbox_hash); self.stream.as_mut()?.current_mailbox = MailboxSelection::Select(mailbox_hash);
Ok(()) Ok(())
} }
@ -639,7 +640,9 @@ impl ImapConnection {
ret: &mut String, ret: &mut String,
force: bool, force: bool,
) -> Result<()> { ) -> Result<()> {
if !force && self.current_mailbox == MailboxSelection::Examine(mailbox_hash) { if !force
&& self.stream.as_ref()?.current_mailbox == MailboxSelection::Examine(mailbox_hash)
{
return Ok(()); return Ok(());
} }
self.send_command( self.send_command(
@ -653,12 +656,12 @@ impl ImapConnection {
self.read_response(ret, RequiredResponses::EXAMINE_REQUIRED) self.read_response(ret, RequiredResponses::EXAMINE_REQUIRED)
.await?; .await?;
debug!("examine response {}", ret); debug!("examine response {}", ret);
self.current_mailbox = MailboxSelection::Examine(mailbox_hash); self.stream.as_mut()?.current_mailbox = MailboxSelection::Examine(mailbox_hash);
Ok(()) Ok(())
} }
pub async fn unselect(&mut self) -> Result<()> { 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::Examine(mailbox_hash) |
MailboxSelection::Select(mailbox_hash) =>{ MailboxSelection::Select(mailbox_hash) =>{
let mut response = String::with_capacity(8 * 1024); let mut response = String::with_capacity(8 * 1024);

View File

@ -51,8 +51,7 @@ impl ImapConnection {
} else { Ok(()) }?;)+ } else { Ok(()) }?;)+
}; };
} }
//FIXME let mailbox_hash = match self.stream.as_ref()?.current_mailbox {
let mailbox_hash = match self.current_mailbox {
MailboxSelection::Select(h) | MailboxSelection::Examine(h) => h, MailboxSelection::Select(h) | MailboxSelection::Examine(h) => h,
MailboxSelection::None => return Ok(false), MailboxSelection::None => return Ok(false),
}; };