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;
|
|
|
|
use panorama_imap::builders::command::Command as ImapCommand;
|
|
|
|
use tokio::{sync::mpsc::UnboundedReceiver, task::JoinHandle};
|
|
|
|
use tokio_stream::wrappers::WatchStream;
|
|
|
|
|
|
|
|
use crate::config::{Config, ConfigWatcher};
|
|
|
|
|
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-15 10:36:06 +00:00
|
|
|
open_imap_connection(acct.imap).await.unwrap();
|
|
|
|
}
|
|
|
|
});
|
2021-02-16 10:45:41 +00:00
|
|
|
|
2021-02-15 10:36:06 +00:00
|
|
|
curr_conn = Some(handle);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|