mirror of
https://github.com/lelgenio/maildir-notify-daemon.git
synced 2025-01-18 05:56:25 -03:00
better error handling and add README.md
This commit is contained in:
parent
2bd4b95bf4
commit
b067ba8689
13
README.md
Normal file
13
README.md
Normal 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/
|
||||
```
|
55
src/main.rs
55
src/main.rs
|
@ -15,55 +15,39 @@ fn main() {
|
|||
Hotwatch::new_with_custom_delay(std::time::Duration::from_secs(0))
|
||||
.expect("hotwatch failed to initialize!");
|
||||
|
||||
std::env::args().for_each(|arg| {
|
||||
hotwatch.watch(arg, handle_event).ok();
|
||||
});
|
||||
for arg in std::env::args() {
|
||||
if let Err(e) = hotwatch.watch(&arg, handle_event) {
|
||||
eprintln!("Failed to watch arg {} with error: {}", &arg, e);
|
||||
}
|
||||
}
|
||||
|
||||
hotwatch.run();
|
||||
}
|
||||
|
||||
fn handle_event(event: hotwatch::Event) -> hotwatch::blocking::Flow {
|
||||
if let hotwatch::Event::Create(newfile) = event {
|
||||
_handle_event(newfile)
|
||||
if let Err(e) = _handle_event(newfile) {
|
||||
eprintln!("{}", e);
|
||||
};
|
||||
};
|
||||
|
||||
Flow::Continue
|
||||
}
|
||||
|
||||
fn _handle_event(newfile: PathBuf) {
|
||||
let raw_content = match std::fs::read(&newfile) {
|
||||
Ok(c) => c,
|
||||
Err(e) => {
|
||||
eprintln!(
|
||||
"Cannot read file '{}': {:?}",
|
||||
newfile.to_string_lossy(),
|
||||
e
|
||||
);
|
||||
return;
|
||||
}
|
||||
};
|
||||
fn _handle_event(newfile: PathBuf) -> Result<(), String> {
|
||||
let raw_content = std::fs::read(&newfile).map_err(|e| {
|
||||
format!("Cannot read file '{}': {:?}", newfile.to_string_lossy(), e)
|
||||
})?;
|
||||
|
||||
let mail_content = match mailparse::parse_mail(&raw_content) {
|
||||
Ok(c) => c,
|
||||
Err(e) => {
|
||||
eprintln!(
|
||||
"Cannot parse file '{}': {:?}",
|
||||
newfile.to_string_lossy(),
|
||||
e
|
||||
);
|
||||
return;
|
||||
}
|
||||
};
|
||||
let mail_content = mailparse::parse_mail(&raw_content).map_err(|e| {
|
||||
format!("Cannot parse file '{}': {:?}", newfile.to_string_lossy(), e)
|
||||
})?;
|
||||
|
||||
let headers = mail_content.get_headers();
|
||||
|
||||
let from = match headers.get_first_value("From") {
|
||||
Some(f) => f,
|
||||
None => {
|
||||
eprintln!("Cannot parse file '{}'", newfile.to_string_lossy(),);
|
||||
return;
|
||||
}
|
||||
};
|
||||
let from = headers.get_first_value("From").ok_or_else(|| {
|
||||
format!("Cannot parse file '{}'", newfile.to_string_lossy())
|
||||
})?;
|
||||
|
||||
let subject = headers.get_first_value("Subject").unwrap_or_else(|| {
|
||||
mail_content
|
||||
|
@ -81,5 +65,6 @@ fn _handle_event(newfile: PathBuf) {
|
|||
.body(&format!("Subject: {}", subject))
|
||||
.icon("mail-unread-symbolic")
|
||||
.show()
|
||||
.ok();
|
||||
.map(|_| ())
|
||||
.map_err(|e| format!("Could not send notification: {}", e))
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue