Browse Source

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.
tags/alpha-0.6.0
Manos Pitsidianakis 8 months ago
parent
commit
7bbfd188ef
Signed by: epilys GPG Key ID: 73627C2F690DF710
3 changed files with 17 additions and 14 deletions
  1. +3
    -2
      melib/src/backends/imap.rs
  2. +13
    -10
      melib/src/backends/imap/connection.rs
  3. +1
    -2
      melib/src/backends/imap/untagged.rs

+ 3
- 2
melib/src/backends/imap.rs View File

@ -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 */

+ 13
- 10
melib/src/backends/imap/connection.rs View File

@ -46,9 +46,10 @@ pub enum ImapProtocol {
#[derive(Debug)]
pub struct ImapStream {
cmd_id: usize,
stream: AsyncWrapper<Connection>,
protocol: ImapProtocol,
pub cmd_id: usize,
pub stream: AsyncWrapper<Connection>,
pub protocol: ImapProtocol,
pub current_mailbox: MailboxSelection,
}
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
@ -73,7 +74,6 @@ pub struct ImapConnection {
pub stream: Result<ImapStream>,
pub server_conf: ImapServerConf,
pub uid_store: Arc<UIDStore>,
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);

+ 1
- 2
melib/src/backends/imap/untagged.rs View File

@ -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),
};

Loading…
Cancel
Save