diff --git a/CHANGELOG.md b/CHANGELOG.md index 6c6d209..95c5718 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Nix run issue [#272] - Range not displayed when fetch fails [#276] - Blank lines and spaces in `text/plain` parts [#280] +- Watch command [#271] ### Removed @@ -373,4 +374,5 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 [#272]: https://github.com/soywod/himalaya/issues/272 [#273]: https://github.com/soywod/himalaya/issues/273 [#276]: https://github.com/soywod/himalaya/issues/276 +[#271]: https://github.com/soywod/himalaya/issues/271 [#280]: https://github.com/soywod/himalaya/issues/280 diff --git a/src/config/config_entity.rs b/src/config/config_entity.rs index 44ffb34..dd57821 100644 --- a/src/config/config_entity.rs +++ b/src/config/config_entity.rs @@ -1,7 +1,7 @@ use anyhow::{Context, Error, Result}; use log::{debug, trace}; use serde::Deserialize; -use std::{collections::HashMap, convert::TryFrom, env, fs, path::PathBuf, thread}; +use std::{collections::HashMap, convert::TryFrom, env, fs, path::PathBuf}; use toml; use crate::output::run_cmd; @@ -135,28 +135,8 @@ impl Config { .map(|cmd| format!(r#"{} {:?} {:?}"#, cmd, subject, sender)) .unwrap_or(default_cmd); + debug!("run command: {}", cmd); run_cmd(&cmd).context("cannot run notify cmd")?; - - Ok(()) - } - - pub fn _exec_watch_cmds(&self, account: &ConfigAccountEntry) -> Result<()> { - let cmds = account - .watch_cmds - .as_ref() - .or_else(|| self.watch_cmds.as_ref()) - .map(|cmds| cmds.to_owned()) - .unwrap_or_default(); - - thread::spawn(move || { - debug!("batch execution of {} cmd(s)", cmds.len()); - cmds.iter().for_each(|cmd| { - debug!("running command {:?}…", cmd); - let res = run_cmd(cmd); - debug!("{:?}", res); - }) - }); - Ok(()) } } diff --git a/src/domain/imap/imap_handler.rs b/src/domain/imap/imap_handler.rs index 6f7b132..276c952 100644 --- a/src/domain/imap/imap_handler.rs +++ b/src/domain/imap/imap_handler.rs @@ -4,7 +4,10 @@ use anyhow::Result; -use crate::{config::Config, domain::imap::ImapServiceInterface}; +use crate::{ + config::{Account, Config}, + domain::imap::ImapServiceInterface, +}; /// Notify handler. pub fn notify<'a, ImapService: ImapServiceInterface<'a>>( @@ -18,7 +21,8 @@ pub fn notify<'a, ImapService: ImapServiceInterface<'a>>( /// Watch handler. pub fn watch<'a, ImapService: ImapServiceInterface<'a>>( keepalive: u64, + account: &Account, imap: &mut ImapService, ) -> Result<()> { - imap.watch(keepalive) + imap.watch(account, keepalive) } diff --git a/src/domain/imap/imap_service.rs b/src/domain/imap/imap_service.rs index e89f87b..0587b51 100644 --- a/src/domain/imap/imap_service.rs +++ b/src/domain/imap/imap_service.rs @@ -9,18 +9,20 @@ use std::{ collections::HashSet, convert::{TryFrom, TryInto}, net::TcpStream, + thread, }; use crate::{ config::{Account, Config}, domain::{Envelope, Envelopes, Flags, Mbox, Mboxes, Msg, RawEnvelopes, RawMboxes}, + output::run_cmd, }; type ImapSession = imap::Session>; pub trait ImapServiceInterface<'a> { fn notify(&mut self, config: &Config, keepalive: u64) -> Result<()>; - fn watch(&mut self, keepalive: u64) -> Result<()>; + fn watch(&mut self, account: &Account, keepalive: u64) -> Result<()>; fn fetch_mboxes(&'a mut self) -> Result; fn fetch_envelopes(&mut self, page_size: &usize, page: &usize) -> Result; fn fetch_envelopes_with( @@ -247,12 +249,14 @@ impl<'a> ImapServiceInterface<'a> for ImapService<'a> { } fn notify(&mut self, config: &Config, keepalive: u64) -> Result<()> { + debug!("notify"); + let mbox = self.mbox.to_owned(); - debug!("examine mailbox: {}", mbox.name); + debug!("examine mailbox {:?}", mbox); self.sess()? .examine(&mbox.name) - .context(format!("cannot examine mailbox `{}`", &self.mbox.name))?; + .context(format!("cannot examine mailbox {}", self.mbox.name))?; debug!("init messages hashset"); let mut msgs_set: HashSet = self @@ -317,7 +321,7 @@ impl<'a> ImapServiceInterface<'a> for ImapService<'a> { } } - fn watch(&mut self, keepalive: u64) -> Result<()> { + fn watch(&mut self, account: &Account, keepalive: u64) -> Result<()> { debug!("examine mailbox: {}", &self.mbox.name); let mbox = self.mbox.to_owned(); @@ -338,8 +342,17 @@ impl<'a> ImapServiceInterface<'a> for ImapService<'a> { }) }) .context("cannot start the idle mode")?; - // FIXME - // ctx.config.exec_watch_cmds(&ctx.account)?; + + let cmds = account.watch_cmds.clone(); + thread::spawn(move || { + debug!("batch execution of {} cmd(s)", cmds.len()); + cmds.iter().for_each(|cmd| { + debug!("running command {:?}…", cmd); + let res = run_cmd(cmd); + debug!("{:?}", res); + }) + }); + debug!("end loop"); } } diff --git a/src/domain/mbox/mbox_handler.rs b/src/domain/mbox/mbox_handler.rs index ceb6db9..314227b 100644 --- a/src/domain/mbox/mbox_handler.rs +++ b/src/domain/mbox/mbox_handler.rs @@ -28,7 +28,7 @@ mod tests { use termcolor::ColorSpec; use crate::{ - config::Config, + config::{Account, Config}, domain::{AttrRemote, Attrs, Envelopes, Flags, Mbox, Mboxes, Msg}, output::{Print, PrintTable, WriteColor}, }; @@ -117,7 +117,7 @@ mod tests { fn notify(&mut self, _: &Config, _: u64) -> Result<()> { unimplemented!() } - fn watch(&mut self, _: u64) -> Result<()> { + fn watch(&mut self, _: &Account, _: u64) -> Result<()> { unimplemented!() } fn fetch_envelopes(&mut self, _: &usize, _: &usize) -> Result { diff --git a/src/main.rs b/src/main.rs index 4d0d279..78738a5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -80,7 +80,7 @@ fn main() -> Result<()> { return imap_handler::notify(keepalive, &config, &mut imap); } Some(imap_arg::Command::Watch(keepalive)) => { - return imap_handler::watch(keepalive, &mut imap); + return imap_handler::watch(keepalive, &account, &mut imap); } _ => (), }