This commit is contained in:
lelgenio 2022-02-07 11:24:52 -03:00
parent 044a9b16fe
commit a4e4cdd851
4 changed files with 1421 additions and 0 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
/target

1306
Cargo.lock generated Normal file

File diff suppressed because it is too large Load diff

13
Cargo.toml Normal file
View file

@ -0,0 +1,13 @@
[package]
name = "maildir-notify-daemon"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
hotwatch = "0.4.6"
mailparse = "0.13.8"
notify-rust = "4.5.6"
[features]

101
src/main.rs Normal file
View file

@ -0,0 +1,101 @@
use hotwatch::blocking::{
Flow,
Hotwatch,
};
use mailparse::{
self,
MailHeaderMap,
};
use notify_rust::Notification;
fn main() {
let args = std::env::args(); // .collect::<Vec<_>>();
// let path = args.get(1).expect("No path provided, exiting");
let mut hotwatch =
Hotwatch::new_with_custom_delay(std::time::Duration::from_secs(0))
.expect("hotwatch failed to initialize!");
println!("starting");
for arg in args {
hotwatch
.watch(arg, handle_event)
.expect("hotwatch failed to initialize!");
}
hotwatch.run();
println!("exiting");
}
fn handle_event(event: hotwatch::Event) -> hotwatch::blocking::Flow {
_handle_event(event);
Flow::Continue
}
fn _handle_event(event: hotwatch::Event) {
let newfile = match dbg! {event} {
hotwatch::Event::Create(newfile) => newfile,
_ => return,
};
let raw_content = match std::fs::read_to_string(&newfile) {
Ok(c) => c,
Err(e) => {
eprintln!(
"Cannot read file '{}': {:?}",
newfile.to_string_lossy(),
e
);
return;
}
};
let mail_content = match mailparse::parse_mail(raw_content.as_bytes()) {
Ok(c) => c,
Err(e) => {
eprintln!(
"Cannot parse file '{}': {:?}",
newfile.to_string_lossy(),
e
);
return;
}
};
let headers = mail_content.get_headers();
eprintln!("getting from field");
let from = match headers.get_first_value("From") {
Some(f) => f,
None => {
eprintln!("Cannot parse file '{}'", newfile.to_string_lossy(),);
return;
}
};
eprintln!("getting subject field");
let subject = match headers.get_first_value("Subject") {
Some(sub) => sub,
None => mail_content
.get_body()
.unwrap_or("".to_string())
.lines()
.filter(|line| line.len() != 0)
.next()
.unwrap_or("")
.to_string(),
};
eprintln!("setting subject field to {}", &subject);
eprintln!("showing notification");
Notification::new()
.summary(&format!("From: {}", from))
.body(&format!("Subject: {}", subject))
.icon("mail-unread-symbolic")
.show()
.ok();
}