diff --git a/daemon/src/lib.rs b/daemon/src/lib.rs index be75f68..3c2e158 100644 --- a/daemon/src/lib.rs +++ b/daemon/src/lib.rs @@ -37,32 +37,21 @@ use crate::mail::{mail_main, MailStore}; /// The panorama daemon runs in the background and communicates with other /// panorama components over Unix sockets. #[derive(Debug, Parser)] -struct Options { +pub struct Options { /// Config file path (defaults to XDG) #[clap(long = "config", short = 'c')] - config_file: Option, + pub config_file: Option, /// Verbose mode (-v, -vv, -vvv, etc) #[clap(short = 'v', long = "verbose", parse(from_occurrences))] - verbose: usize, + pub verbose: usize, } -/// Primary entrypoint; this is the function that is called by main with no -/// arguments. +/// Primary entrypoint; this is the function that is called by main. /// /// The purpose of this function is to parse command line arguments /// and set up config watching, then call [`run_with_config`] with the config. -pub async fn run() -> Result<()> { - let opt = Options::parse(); - - stderrlog::new() - .module(module_path!()) - .module("panorama_daemon") - .module("panorama_imap") - .verbosity(opt.verbose) - .init() - .unwrap(); - +pub async fn run(opt: Options) -> Result<()> { // if we're using a config-watcher, then start the watcher system #[cfg(feature = "config-watch")] { diff --git a/daemon/src/mail/mod.rs b/daemon/src/mail/mod.rs index f2e054a..4fe0202 100644 --- a/daemon/src/mail/mod.rs +++ b/daemon/src/mail/mod.rs @@ -68,15 +68,15 @@ pub async fn mail_main( async fn run_sync_loop(mut conn: ClientAuthenticated) -> Result<()> { // get the list of folders first - debug!(target: "run_sync_loop", "Retrieving folder list..."); + debug!("Retrieving folder list..."); let folder_list = conn.list().await?; + debug!("Mailbox list: {:?}", folder_list); // let _ = mail2ui_tx.send(MailEvent::FolderList( // acct_name.clone(), // folder_list.clone(), // )); - debug!("mailbox list: {:?}", folder_list); for folder in folder_list.iter() { debug!("folder: {:?}", folder); let select = conn.select("INBOX").await?; diff --git a/daemon/src/main.rs b/daemon/src/main.rs index b19a5c8..e0ea5ef 100644 --- a/daemon/src/main.rs +++ b/daemon/src/main.rs @@ -1,4 +1,18 @@ use anyhow::Result; +use clap::Parser; +use panorama_daemon::Options; #[tokio::main] -async fn main() -> Result<()> { panorama_daemon::run().await } +async fn main() -> Result<()> { + let opt = Options::parse(); + + stderrlog::new() + .module(module_path!()) + .module("panorama_daemon") + .module("panorama_imap") + .verbosity(opt.verbose) + .init() + .unwrap(); + + panorama_daemon::run(opt).await +} diff --git a/imap/src/client/client.rs b/imap/src/client/client.rs index 31f3cdf..be72ab1 100644 --- a/imap/src/client/client.rs +++ b/imap/src/client/client.rs @@ -57,7 +57,12 @@ impl ConfigBuilder { let hostname = config.hostname.as_ref(); let port = config.port; - trace!("connecting to {}:{}...", hostname, port); + trace!( + "connecting to {}:{} {}...", + hostname, + port, + if config.tls { "(encrypted)" } else { "" } + ); let conn = TcpStream::connect((hostname, port)).await?; trace!("connected."); diff --git a/imap/src/pool/mod.rs b/imap/src/pool/mod.rs index ca81e31..4e457b0 100644 --- a/imap/src/pool/mod.rs +++ b/imap/src/pool/mod.rs @@ -6,8 +6,9 @@ use tokio::sync::Semaphore; use crate::{interface::ImapClient, proto::response::Envelope}; -use super::client::{ClientAuthenticated, Config}; +use super::client::{ClientAuthenticated, Config, ConfigBuilder}; +#[derive(Debug)] pub struct PoolConfig { pub max_connections: usize, } @@ -33,6 +34,7 @@ impl ImapClient for ImapPool { impl ImapPool { pub fn new(config: Config, pool_config: PoolConfig) -> Self { + debug!("Created new IMAP pool with config {:?}", pool_config); let inner = InnerPool::init(config, pool_config); ImapPool(Arc::new(inner)) } @@ -54,12 +56,23 @@ impl InnerPool { } pub async fn acquire(&self) -> Result { + debug!("Trying to acquire a connection from the pool..."); let guard = match self.connections.pop() { // we can reuse - Some(conn) => {} + Some(conn) => { + return Ok(conn); + } // no existing connection, time to make a new one - None => {} + None => { + let client = ConfigBuilder::default() + .hostname(self.config.hostname.clone()) + .port(self.config.port) + .tls(self.config.tls) + .open() + .await?; + debug!("Client connected to {}", self.config.hostname); + } }; todo!()