better error handling and add README.md

This commit is contained in:
lelgenio 2022-03-04 09:46:14 -03:00
parent 2bd4b95bf4
commit b067ba8689
2 changed files with 33 additions and 35 deletions

13
README.md Normal file
View file

@ -0,0 +1,13 @@
# Maildir Notification Daemon
Waits for new files on provided dirs, parse them as email and send a notification with author and subject.
```sh
maildir-notify-daemon ~/.local/share/mail/*/*/new/
```
## Try it without sending emails
```sh
cp ~/.local/share/mail/personal/INBOX/cur/[ANY_MAIL_FILE] ~/.local/share/mail/personal/INBOX/new/
```

View file

@ -15,55 +15,39 @@ fn main() {
Hotwatch::new_with_custom_delay(std::time::Duration::from_secs(0)) Hotwatch::new_with_custom_delay(std::time::Duration::from_secs(0))
.expect("hotwatch failed to initialize!"); .expect("hotwatch failed to initialize!");
std::env::args().for_each(|arg| { for arg in std::env::args() {
hotwatch.watch(arg, handle_event).ok(); if let Err(e) = hotwatch.watch(&arg, handle_event) {
}); eprintln!("Failed to watch arg {} with error: {}", &arg, e);
}
}
hotwatch.run(); hotwatch.run();
} }
fn handle_event(event: hotwatch::Event) -> hotwatch::blocking::Flow { fn handle_event(event: hotwatch::Event) -> hotwatch::blocking::Flow {
if let hotwatch::Event::Create(newfile) = event { if let hotwatch::Event::Create(newfile) = event {
_handle_event(newfile) if let Err(e) = _handle_event(newfile) {
eprintln!("{}", e);
};
}; };
Flow::Continue Flow::Continue
} }
fn _handle_event(newfile: PathBuf) { fn _handle_event(newfile: PathBuf) -> Result<(), String> {
let raw_content = match std::fs::read(&newfile) { let raw_content = std::fs::read(&newfile).map_err(|e| {
Ok(c) => c, format!("Cannot read file '{}': {:?}", newfile.to_string_lossy(), e)
Err(e) => { })?;
eprintln!(
"Cannot read file '{}': {:?}",
newfile.to_string_lossy(),
e
);
return;
}
};
let mail_content = match mailparse::parse_mail(&raw_content) { let mail_content = mailparse::parse_mail(&raw_content).map_err(|e| {
Ok(c) => c, format!("Cannot parse file '{}': {:?}", newfile.to_string_lossy(), e)
Err(e) => { })?;
eprintln!(
"Cannot parse file '{}': {:?}",
newfile.to_string_lossy(),
e
);
return;
}
};
let headers = mail_content.get_headers(); let headers = mail_content.get_headers();
let from = match headers.get_first_value("From") { let from = headers.get_first_value("From").ok_or_else(|| {
Some(f) => f, format!("Cannot parse file '{}'", newfile.to_string_lossy())
None => { })?;
eprintln!("Cannot parse file '{}'", newfile.to_string_lossy(),);
return;
}
};
let subject = headers.get_first_value("Subject").unwrap_or_else(|| { let subject = headers.get_first_value("Subject").unwrap_or_else(|| {
mail_content mail_content
@ -81,5 +65,6 @@ fn _handle_event(newfile: PathBuf) {
.body(&format!("Subject: {}", subject)) .body(&format!("Subject: {}", subject))
.icon("mail-unread-symbolic") .icon("mail-unread-symbolic")
.show() .show()
.ok(); .map(|_| ())
.map_err(|e| format!("Could not send notification: {}", e))
} }