Initial commit

This commit is contained in:
Joe Bellus 2024-03-03 01:42:25 -05:00
commit dc7239f9c2
10 changed files with 1962 additions and 0 deletions

1
.direnv/flake-profile Symbolic link
View File

@ -0,0 +1 @@
flake-profile-2-link

View File

@ -0,0 +1 @@
/nix/store/2accvk3hns9h9zhmv1l8jjskrgxqpa5c-nix-shell-env

View File

@ -0,0 +1 @@
/nix/store/hfppccc1m8j06yxqql77lg6s8cb8i51z-nix-shell-env

1
.envrc Normal file
View File

@ -0,0 +1 @@
use flake

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/target

1811
Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

20
Cargo.toml Normal file
View File

@ -0,0 +1,20 @@
[package]
name = "mbdirnotify"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
anyhow = "1.0.80"
clap = {version = "4.5.1", features = ["derive"] }
mailparse = "0.14.1"
notify = "6.1.1"
notify-rust = "4.10.0"
serde = {version = "1.0.197", features = ["derive"] }
[profile.release]
strip = true
opt-level = "z"
lto = true
codegen-units = 1

27
flake.lock Normal file
View File

@ -0,0 +1,27 @@
{
"nodes": {
"nixpkgs": {
"locked": {
"lastModified": 1709237383,
"narHash": "sha256-cy6ArO4k5qTx+l5o+0mL9f5fa86tYUX3ozE1S+Txlds=",
"owner": "nixos",
"repo": "nixpkgs",
"rev": "1536926ef5621b09bba54035ae2bb6d806d72ac8",
"type": "github"
},
"original": {
"owner": "nixos",
"ref": "nixos-unstable",
"repo": "nixpkgs",
"type": "github"
}
},
"root": {
"inputs": {
"nixpkgs": "nixpkgs"
}
}
},
"root": "root",
"version": 7
}

19
flake.nix Normal file
View File

@ -0,0 +1,19 @@
{
description = "Sends notifications for emails stored in maildir";
inputs = { nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable"; };
outputs = { self, nixpkgs }:
let
system = "x86_64-linux";
pkgs = import nixpkgs { inherit system; };
in {
devShells.${system}.default = pkgs.mkShell {
packages = [ pkgs.rustc pkgs.cargo pkgs.rust-analyzer pkgs.rustfmt ];
};
packages.${system}.default = pkgs.rustPlatform.buildRustPackage {
pname = "mbdirnotify";
version = "0.4.3";
cargoLock.lockFile = ./Cargo.lock;
src = pkgs.lib.cleanSource ./.;
};
};
}

80
src/main.rs Normal file
View File

@ -0,0 +1,80 @@
use std::{fs::File, io::Read, path::PathBuf};
use anyhow::Result;
use clap::{
builder::{
styling::{AnsiColor, Effects},
Styles,
},
Parser,
};
use mailparse::MailHeaderMap;
use notify::{Event, EventKind, RecursiveMode, Watcher};
use notify_rust::Notification;
fn styles() -> Styles {
Styles::styled()
.header(AnsiColor::Red.on_default() | Effects::BOLD)
.usage(AnsiColor::Red.on_default() | Effects::BOLD)
.literal(AnsiColor::Blue.on_default() | Effects::BOLD)
.placeholder(AnsiColor::Green.on_default())
}
#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None, styles=styles())]
pub struct Args {
maildir: String,
}
fn main() {
let args = Args::parse();
let mut watcher = notify::recommended_watcher(watch).unwrap();
watcher
.watch(&PathBuf::from(args.maildir), RecursiveMode::NonRecursive)
.unwrap();
loop {
std::thread::sleep(std::time::Duration::from_millis(500));
}
}
fn watch(event: notify::Result<Event>) {
if let Err(e) = parse_event(event) {
eprintln!("{}", e);
}
}
fn parse_event(event: notify::Result<Event>) -> Result<()> {
let event = event?;
if let EventKind::Create(_kind) = event.kind {
if let Some(p) = event.paths.first() {
let mut f = File::open(&p)?;
let mut buf = vec![];
f.read_to_end(&mut buf)?;
let mail_data = mailparse::parse_mail(&buf)?;
let subject = mail_data
.headers
.get_first_value("Subject")
.unwrap_or_default();
let from_raw = mail_data
.headers
.get_first_value("From")
.unwrap_or_default();
let from = parse_name(&from_raw);
Notification::new()
.summary(&from)
.body(&subject)
.icon("email")
.show()
.unwrap();
}
}
Ok(())
}
fn parse_name(s: &str) -> &str {
s.split("\"").nth(1).unwrap_or(s)
}