Skip to content

Commit

Permalink
make imap connection work
Browse files Browse the repository at this point in the history
  • Loading branch information
joshka committed Dec 10, 2024
1 parent cdd2f66 commit cf3efd0
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 15 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand All @@ -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"
Expand Down
4 changes: 2 additions & 2 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<LevelFilter>,

Expand Down Expand Up @@ -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,
}
Expand Down
21 changes: 13 additions & 8 deletions src/logging.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use tracing::debug;
use tracing_appender::non_blocking::WorkerGuard;
use std::fs::File;

use tracing::info;

use crate::config;

Expand All @@ -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<WorkerGuard> {
// pub fn init() -> color_eyre::Result<WorkerGuard> {
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();

Expand All @@ -36,6 +40,7 @@ pub fn init() -> color_eyre::Result<WorkerGuard> {
.with(tui_logger::tracing_subscriber_layer())
.try_init()?;

debug!("logging initialized");
Ok(worker_guard)
info!("logging initialized");
// Ok(worker_guard)
Ok(())
}
21 changes: 17 additions & 4 deletions src/mail_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ pub struct MailClient {
}

impl MailClient {
#[instrument]
pub async fn init() -> color_eyre::Result<Self> {
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);
Expand All @@ -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")?;
Expand All @@ -60,25 +67,31 @@ impl MailClient {
self.folder.as_ref().map_or(INBOX, String::as_ref)
}

#[instrument(skip(self))]
pub async fn load_folder(&self) -> color_eyre::Result<Envelopes> {
let folder = self.folder_or_default();
info!("loading folder {folder}");
let options = ListEnvelopesOptions {
page_size: DEFAULT_PAGE_SIZE,
page: 0,
query: None,
};
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<Messages> {
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)
Expand Down

0 comments on commit cf3efd0

Please sign in to comment.