fix watch command (#271)

This commit is contained in:
Clément DOUIN 2022-02-01 17:00:16 +01:00
parent 1dbd7a4893
commit c1841a2932
No known key found for this signature in database
GPG key ID: 353E4A18EE0FAB72
6 changed files with 32 additions and 33 deletions

View file

@ -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

View file

@ -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(())
}
}

View file

@ -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)
}

View file

@ -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<TlsStream<TcpStream>>;
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<Mboxes>;
fn fetch_envelopes(&mut self, page_size: &usize, page: &usize) -> Result<Envelopes>;
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<u32> = 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");
}
}

View file

@ -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<Envelopes> {

View file

@ -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);
}
_ => (),
}