2021-02-15 10:36:06 +00:00
|
|
|
//! Mail
|
|
|
|
|
|
|
|
mod imap;
|
2021-02-15 11:07:48 +00:00
|
|
|
mod imap2;
|
2021-02-15 10:36:06 +00:00
|
|
|
|
|
|
|
use anyhow::Result;
|
|
|
|
use futures::stream::StreamExt;
|
2021-02-20 05:24:46 +00:00
|
|
|
use panorama_imap::{client::ClientBuilder, command::Command as ImapCommand};
|
2021-02-15 10:36:06 +00:00
|
|
|
use tokio::{sync::mpsc::UnboundedReceiver, task::JoinHandle};
|
|
|
|
use tokio_stream::wrappers::WatchStream;
|
|
|
|
|
2021-02-20 05:24:46 +00:00
|
|
|
use crate::config::{Config, ConfigWatcher, MailAccountConfig, TlsMethod};
|
2021-02-15 10:36:06 +00:00
|
|
|
|
2021-02-20 01:44:04 +00:00
|
|
|
use self::imap2::open_imap_connection;
|
2021-02-15 10:36:06 +00:00
|
|
|
|
|
|
|
/// Command sent to the mail thread by something else (i.e. UI)
|
|
|
|
pub enum MailCommand {
|
|
|
|
/// Refresh the list
|
|
|
|
Refresh,
|
|
|
|
|
|
|
|
/// Send a raw command
|
|
|
|
Raw(ImapCommand),
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Main entrypoint for the mail listener.
|
|
|
|
pub async fn run_mail(
|
2021-02-15 11:07:48 +00:00
|
|
|
mut config_watcher: ConfigWatcher,
|
2021-02-15 10:36:06 +00:00
|
|
|
_cmd_in: UnboundedReceiver<MailCommand>,
|
|
|
|
) -> Result<()> {
|
|
|
|
let mut curr_conn: Option<JoinHandle<_>> = None;
|
|
|
|
|
2021-02-15 11:07:48 +00:00
|
|
|
// let mut config_watcher = WatchStream::new(config_watcher);
|
2021-02-15 10:36:06 +00:00
|
|
|
loop {
|
|
|
|
debug!("listening for configs");
|
2021-02-15 11:07:48 +00:00
|
|
|
let config: Config = match config_watcher.changed().await {
|
2021-02-16 10:45:41 +00:00
|
|
|
Ok(_) => config_watcher.borrow().clone(),
|
2021-02-15 10:36:06 +00:00
|
|
|
_ => break,
|
|
|
|
};
|
2021-02-16 10:45:41 +00:00
|
|
|
debug!("got");
|
2021-02-15 10:36:06 +00:00
|
|
|
|
|
|
|
// TODO: gracefully shut down connection
|
|
|
|
// just gonna drop the connection for now
|
|
|
|
if let Some(curr_conn) = curr_conn.take() {
|
|
|
|
debug!("dropping connection...");
|
|
|
|
curr_conn.abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
let handle = tokio::spawn(async {
|
|
|
|
for acct in config.mail_accounts.into_iter() {
|
2021-02-20 01:44:04 +00:00
|
|
|
debug!("opening imap connection for {:?}", acct);
|
2021-02-20 05:24:46 +00:00
|
|
|
osu(acct).await;
|
|
|
|
// open_imap_connection(acct.imap).await.unwrap();
|
2021-02-15 10:36:06 +00:00
|
|
|
}
|
|
|
|
});
|
2021-02-16 10:45:41 +00:00
|
|
|
|
2021-02-15 10:36:06 +00:00
|
|
|
curr_conn = Some(handle);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2021-02-20 05:24:46 +00:00
|
|
|
|
|
|
|
async fn osu(acct: MailAccountConfig) -> Result<()> {
|
|
|
|
let builder = ClientBuilder::default()
|
|
|
|
.hostname(acct.imap.server.clone())
|
|
|
|
.port(acct.imap.port)
|
|
|
|
.tls(matches!(acct.imap.tls, TlsMethod::On))
|
|
|
|
.build()
|
|
|
|
.map_err(|err| anyhow!("err: {}", err))?;
|
|
|
|
|
|
|
|
debug!("connecting to {}:{}", &acct.imap.server, acct.imap.port);
|
|
|
|
let unauth = builder.connect().await;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|