diff --git a/CHANGELOG.md b/CHANGELOG.md index f695ff6..bc27b11 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Fixed + +- Fixed bug when listing envelopes with `backend = "imap"`, `sync.enable = true` and `envelope.watch.backend = "imap"` led to unwanted IMAP connection creation (which slowed down the listing). + ## [1.0.0-beta] - 2024-01-01 Few major concepts changed: diff --git a/Cargo.lock b/Cargo.lock index 7c5c34d..6c1001e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1830,7 +1830,7 @@ dependencies = [ [[package]] name = "himalaya" -version = "1.0.0-beta" +version = "1.0.0-beta.2" dependencies = [ "anyhow", "async-trait", diff --git a/Cargo.toml b/Cargo.toml index b23d75b..7d1e644 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "himalaya" description = "CLI to manage emails" -version = "1.0.0-beta" +version = "1.0.0-beta.2" authors = ["soywod "] edition = "2021" license = "MIT" diff --git a/README.md b/README.md index 4731091..317cc9e 100644 --- a/README.md +++ b/README.md @@ -73,7 +73,7 @@ Please read the [documentation](https://pimalaya.org/himalaya/cli/latest/configu If you want to **report a bug** that [does not exist yet](https://todo.sr.ht/~soywod/pimalaya), please send an email at [~soywod/pimalaya@todo.sr.ht](mailto:~soywod/pimalaya@todo.sr.ht). -If you want to **propose a feature** or **fix a bug**, please send a patch at [~soywod/pimalaya@lists.sr.ht](mailto:~soywod/pimalaya@lists.sr.ht) using [git send-email](https://git-scm.com/docs/git-send-email). Follow [this guide](https://git-send-email.io/) to configure git properly. +If you want to **propose a feature** or **fix a bug**, please open a [pull request](https://github.com/soywod/himalaya/pulls). If you just want to **discuss** about the project, feel free to join the [Matrix](https://matrix.org/) workspace [#pimalaya.general](https://matrix.to/#/#pimalaya.general:matrix.org) or contact me directly [@soywod](https://matrix.to/#/@soywod:matrix.org). You can also use the mailing list [[send an email](mailto:~soywod/pimalaya@lists.sr.ht)|[subscribe](mailto:~soywod/pimalaya+subscribe@lists.sr.ht)|[unsubscribe](mailto:~soywod/pimalaya+unsubscribe@lists.sr.ht)]. diff --git a/src/backend/mod.rs b/src/backend/mod.rs index 85ca851..a25c865 100644 --- a/src/backend/mod.rs +++ b/src/backend/mod.rs @@ -84,13 +84,13 @@ impl ToString for BackendKind { #[derive(Clone, Default)] pub struct BackendContextBuilder { - maildir: Option, - maildir_for_sync: Option, + pub maildir: Option, + pub maildir_for_sync: Option, #[cfg(feature = "imap")] - imap: Option, + pub imap: Option, #[cfg(feature = "smtp")] - smtp: Option, - sendmail: Option, + pub smtp: Option, + pub sendmail: Option, } #[async_trait] @@ -699,6 +699,16 @@ impl Backend { .await } + pub async fn new_v2( + toml_account_config: TomlAccountConfig, + builder: email::backend::BackendBuilder, + ) -> Result { + Ok(Self { + toml_account_config, + backend: builder.build().await?, + }) + } + fn build_id_mapper( &self, folder: &str, diff --git a/src/email/envelope/command/list.rs b/src/email/envelope/command/list.rs index fc47a49..a45e7b3 100644 --- a/src/email/envelope/command/list.rs +++ b/src/email/envelope/command/list.rs @@ -1,10 +1,18 @@ use anyhow::Result; use clap::Parser; +use email::backend::BackendBuilder; +#[cfg(feature = "imap")] +use email::{envelope::list::imap::ListEnvelopesImap, imap::ImapSessionBuilder}; +#[cfg(feature = "maildir")] +use email::{ + envelope::list::maildir::ListEnvelopesMaildir, + maildir::{config::MaildirConfig, MaildirSessionBuilder}, +}; use log::info; use crate::{ account::arg::name::AccountNameFlag, - backend::Backend, + backend::{Backend, BackendContextBuilder, BackendKind}, cache::arg::disable::CacheDisableFlag, config::TomlConfig, folder::arg::name::FolderNameOptionalArg, @@ -54,7 +62,77 @@ impl ListEnvelopesCommand { let (toml_account_config, account_config) = config .clone() .into_account_configs(some_account_name, self.cache.disable)?; - let backend = Backend::new(toml_account_config, account_config.clone(), false).await?; + + let backend_kind = toml_account_config.list_envelopes_kind(); + let backend_ctx_builder = BackendContextBuilder { + maildir: toml_account_config + .maildir + .as_ref() + .filter(|_| matches!(backend_kind, Some(BackendKind::Maildir))) + .map(|mdir_config| { + MaildirSessionBuilder::new(account_config.clone(), mdir_config.clone()) + }), + maildir_for_sync: Some(MaildirConfig { + root_dir: account_config.get_sync_dir()?, + }) + .filter(|_| matches!(backend_kind, Some(BackendKind::MaildirForSync))) + .map(|mdir_config| MaildirSessionBuilder::new(account_config.clone(), mdir_config)), + #[cfg(feature = "imap")] + imap: { + let ctx_builder = toml_account_config + .imap + .as_ref() + .filter(|_| matches!(backend_kind, Some(BackendKind::Imap))) + .map(|imap_config| { + ImapSessionBuilder::new(account_config.clone(), imap_config.clone()) + .with_prebuilt_credentials() + }); + match ctx_builder { + Some(ctx_builder) => Some(ctx_builder.await?), + None => None, + } + }, + #[cfg(feature = "notmuch")] + notmuch: toml_account_config + .notmuch + .as_ref() + .filter(|_| matches!(backend_kind, Some(BackendKind::Notmuch))) + .map(|notmuch_config| { + NotmuchSessionBuilder::new(account_config.clone(), notmuch_config.clone()) + }), + ..Default::default() + }; + + let mut backend_builder = BackendBuilder::new(account_config.clone(), backend_ctx_builder); + + match toml_account_config.list_envelopes_kind() { + Some(BackendKind::Maildir) => { + backend_builder = backend_builder.with_list_envelopes(|ctx| { + ctx.maildir.as_ref().and_then(ListEnvelopesMaildir::new) + }); + } + Some(BackendKind::MaildirForSync) => { + backend_builder = backend_builder.with_list_envelopes(|ctx| { + ctx.maildir_for_sync + .as_ref() + .and_then(ListEnvelopesMaildir::new) + }); + } + #[cfg(feature = "imap")] + Some(BackendKind::Imap) => { + backend_builder = backend_builder + .with_list_envelopes(|ctx| ctx.imap.as_ref().and_then(ListEnvelopesImap::new)); + } + #[cfg(feature = "notmuch")] + Some(BackendKind::Notmuch) => { + backend_builder = backend_builder.with_list_envelopes(|ctx| { + ctx.notmuch.as_ref().and_then(ListEnvelopesNotmuch::new) + }); + } + _ => (), + } + + let backend = Backend::new_v2(toml_account_config.clone(), backend_builder).await?; let page_size = self .page_size