From cf3efd0afb8034b1f7b7bab4d8395ab4d6bbf00e Mon Sep 17 00:00:00 2001 From: Josh McKinney Date: Tue, 10 Dec 2024 01:57:54 -0800 Subject: [PATCH] make imap connection work --- Cargo.lock | 1 + Cargo.toml | 3 ++- src/config.rs | 4 ++-- src/logging.rs | 21 +++++++++++++-------- src/mail_client.rs | 21 +++++++++++++++++---- 5 files changed, 35 insertions(+), 15 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 631c100..4d49d1f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2715,6 +2715,7 @@ dependencies = [ "himalaya", "pimalaya-tui", "ratatui", + "secret-lib", "serde", "serde_with", "tokio", diff --git a/Cargo.toml b/Cargo.toml index ddeeeeb..6a6af92 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,7 +13,7 @@ clap = { version = "4.5.23", features = ["derive"] } color-eyre = { version = "0.6.3", features = ["issue-url"] } crossterm = "0.28.1" directories = "5.0.1" -email-lib = "0.26.2" +email-lib = { version = "0.26.2", features = ["imap", "keyring"] } figment = { version = "0.10.19", features = ["env", "toml"] } himalaya = { version = "1.0.0" } pimalaya-tui = { version = "0.2.1", features = ["keyring", "config"] } @@ -28,6 +28,7 @@ tracing-subscriber = { version = "0.3.19", features = ["env-filter"] } tracing-error = "0.2.1" tracing-log = "0.2.0" tui-logger = { version = "0.14.0", default-features = false, features = ["tracing-support"] } +secret-lib = "1.0.0" # email-lib = { git = "https://git.sr.ht/~soywod/pimalaya", default-features = false } # mail-builder = "0.3.1" diff --git a/src/config.rs b/src/config.rs index 3fcd747..603f3c9 100644 --- a/src/config.rs +++ b/src/config.rs @@ -40,7 +40,7 @@ pub struct Cli { /// The log level to use. /// /// Valid values are: error, warn, info, debug, trace, off. The default is info. - #[arg(long, value_name = "LEVEL", default_value = "info", alias = "log")] + #[arg(long, value_name = "LEVEL", default_value = "debug", alias = "log")] #[serde_as(as = "NoneAsEmptyString")] log_level: Option, @@ -104,7 +104,7 @@ impl Default for AppConfig { Self { data_dir, himalaya_config, - log_level: LevelFilter::INFO, + log_level: LevelFilter::DEBUG, account_name: None, folder: None, } diff --git a/src/logging.rs b/src/logging.rs index a69d4c8..30d5946 100644 --- a/src/logging.rs +++ b/src/logging.rs @@ -1,5 +1,6 @@ -use tracing::debug; -use tracing_appender::non_blocking::WorkerGuard; +use std::fs::File; + +use tracing::info; use crate::config; @@ -15,17 +16,20 @@ const LOG_FILE_NAME: &str = "nitidus.log"; /// /// Returns a [`WorkerGuard`][guard] which is returned by [`tracing_appender::non_blocking`][non_blocking] /// to ensure buffered logs are flushed to their output in the case of abrupt terminations of a process. -pub fn init() -> color_eyre::Result { +// pub fn init() -> color_eyre::Result { +pub fn init() -> color_eyre::Result<()> { use tracing_error::ErrorLayer; use tracing_subscriber::{fmt, prelude::*, EnvFilter}; let app_config = config::get(); // log to a file that is rotated hourly and stored in the app's data directory - let log_file = tracing_appender::rolling::hourly(app_config.data_dir.clone(), LOG_FILE_NAME); - let (log_file_writer, worker_guard) = tracing_appender::non_blocking(log_file); + // let log_file = tracing_appender::rolling::hourly(app_config.data_dir.clone(), LOG_FILE_NAME); + // let (log_file_writer, worker_guard) = tracing_appender::non_blocking(log_file); + let file = File::create(LOG_FILE_NAME)?; let log_file_layer = fmt::layer() - .with_writer(log_file_writer) + // .with_writer(log_file_writer) + .with_writer(file) .with_ansi(false) .compact(); @@ -36,6 +40,7 @@ pub fn init() -> color_eyre::Result { .with(tui_logger::tracing_subscriber_layer()) .try_init()?; - debug!("logging initialized"); - Ok(worker_guard) + info!("logging initialized"); + // Ok(worker_guard) + Ok(()) } diff --git a/src/mail_client.rs b/src/mail_client.rs index a73e71a..79667a3 100644 --- a/src/mail_client.rs +++ b/src/mail_client.rs @@ -26,7 +26,11 @@ pub struct MailClient { } impl MailClient { + #[instrument] pub async fn init() -> color_eyre::Result { + info!("initializing mail client"); + secret::keyring::set_global_service_name("himalaya-cli"); + let app_config = config::get(); let himalaya_config = load_config(app_config.himalaya_config.clone()).await?; let account_name = app_config.account_name.as_ref().map(String::as_ref); @@ -39,13 +43,16 @@ impl MailClient { ) })?; - let account_config = Arc::new(account_config.clone()); let Some(imap_config) = toml_account_config.imap_config() else { bail!("missing backend config") }; + + info!(?account_config, ?imap_config, "creating imap backend"); + + let account_config = Arc::new(account_config.clone()); let imap_config = Arc::new(imap_config.clone()); let imap_ctx = ImapContextBuilder::new(account_config.clone(), imap_config); - let backend = BackendBuilder::new(account_config.clone(), imap_ctx.clone()) + let backend = BackendBuilder::new(account_config, imap_ctx.clone()) .build() .await .wrap_err("cannot create imap backend")?; @@ -60,7 +67,10 @@ impl MailClient { self.folder.as_ref().map_or(INBOX, String::as_ref) } + #[instrument(skip(self))] pub async fn load_folder(&self) -> color_eyre::Result { + let folder = self.folder_or_default(); + info!("loading folder {folder}"); let options = ListEnvelopesOptions { page_size: DEFAULT_PAGE_SIZE, page: 0, @@ -68,17 +78,20 @@ impl MailClient { }; let envelopes = self .backend - .list_envelopes(self.folder_or_default(), options) + .list_envelopes(folder, options) .await .wrap_err("cannot list envelopes")?; Ok(envelopes) } + #[instrument(skip(self))] pub async fn load_messages(&self, id: &str) -> color_eyre::Result { let id = Id::single(id); + let folder = self.folder_or_default(); + info!("loading messages for {id} in folder {folder}"); let emails = self .backend - .get_messages(self.folder_or_default(), &id) + .get_messages(folder, &id) .await .wrap_err("cannot load messages")?; Ok(emails)