Rename to imap-watch and handle errors

This commit is contained in:
2020-04-22 16:23:18 -06:00
parent 974e51f686
commit 09bf1d7d72
3 changed files with 41 additions and 21 deletions

2
Cargo.lock generated
View File

@@ -242,7 +242,7 @@ dependencies = [
] ]
[[package]] [[package]]
name = "imap-run" name = "imap-watch"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"fondant", "fondant",

View File

@@ -1,5 +1,5 @@
[package] [package]
name = "imap-run" name = "imap-watch"
version = "0.1.0" version = "0.1.0"
authors = ["Dusan Maliarik <dusan@struna.me>"] authors = ["Dusan Maliarik <dusan@struna.me>"]
edition = "2018" edition = "2018"

View File

@@ -3,6 +3,7 @@ extern crate native_tls;
extern crate serde; extern crate serde;
extern crate fondant; extern crate fondant;
use std::{thread, time};
use fondant::Configure; use fondant::Configure;
use serde::{Serialize, Deserialize}; use serde::{Serialize, Deserialize};
@@ -21,14 +22,15 @@ type Session = imap::Session<native_tls::TlsStream<std::net::TcpStream>>;
fn connect(host: &str, user: &str, pass: &str) fn connect(host: &str, user: &str, pass: &str)
-> Result<Session, imap::error::Error> -> Result<Session, imap::error::Error>
{ {
let tls = native_tls::TlsConnector::builder().build().unwrap(); let tls = native_tls::TlsConnector::builder().build()?;
let client = imap::connect((host, 993), host, &tls).unwrap(); let client = imap::connect((host, 993), host, &tls)?;
let mut imap_session = client let mut imap_session = client
.login(user, pass) .login(user, pass)
.map_err(|e| e.0)?; .map_err(|e| e.0)?;
imap_session.select("INBOX")?; imap_session.select("INBOX")?;
println!("connected to {}", host);
Ok(imap_session) Ok(imap_session)
} }
@@ -38,9 +40,32 @@ fn get_pass(eval: &str) -> String {
.args(&["-c", eval]) .args(&["-c", eval])
.output().unwrap().stdout.as_ref() .output().unwrap().stdout.as_ref()
).unwrap()).split("\n").next().unwrap().to_string() ).unwrap()).split("\n").next().unwrap().to_string()
} }
fn loop_session<F>(user: &str, pass: &str, host: &str, hook: F)
where F: Fn() -> ()
{
match connect(host, user, pass) {
Ok(mut sess) => {
loop {
match sess.idle() {
Ok(idle) => {
match idle.wait_keepalive() {
Ok(()) => {
hook();
},
_ => {
eprintln!("failed-idle-wait");
break;
}}},
_ => {
eprintln!("failed-idle");
break;
}}}},
_ => {
eprintln!("connect-failed");
}}}
fn main() { fn main() {
let conf = Config::load().unwrap(); let conf = Config::load().unwrap();
let pass = match (conf.pass.len(), conf.pass_eval.len()) { let pass = match (conf.pass.len(), conf.pass_eval.len()) {
@@ -52,21 +77,16 @@ fn main() {
(_p, 0) => conf.pass, (_p, 0) => conf.pass,
(_p, _e) => get_pass(&conf.pass_eval), (_p, _e) => get_pass(&conf.pass_eval),
}; };
let mut sess = connect(&conf.host, &conf.user, &pass).unwrap(); let hook = &conf.hook;
println!("connected to {}", &conf.host);
loop { loop {
match sess.idle().unwrap().wait_keepalive() { loop_session(&conf.user, &pass, &conf.host, || {
Ok(()) => {
println!("got message!");
std::process::Command::new("sh") std::process::Command::new("sh")
.args(&["-c", &conf.hook]) .args(&["-c", hook])
.stdout(std::process::Stdio::null())
.stderr(std::process::Stdio::null())
.status() .status()
.expect("failed to execute process"); .expect("failed-to-execute-process");
}, });
_ => { thread::sleep(time::Duration::from_millis(1000));
eprintln!("Failed to wait on PUSH");
break;
}
}
} }
} }