procmail2notmuch: add sql rule loader
This commit is contained in:
parent
1c4f27902e
commit
7de36bbc3d
3
Cargo.lock
generated
3
Cargo.lock
generated
@ -3014,6 +3014,9 @@ version = "0.15.9"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"anyhow",
|
"anyhow",
|
||||||
"clap",
|
"clap",
|
||||||
|
"serde",
|
||||||
|
"sqlx",
|
||||||
|
"tokio 1.44.2",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
|||||||
@ -12,4 +12,7 @@ version.workspace = true
|
|||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
anyhow = "1.0.69"
|
anyhow = "1.0.69"
|
||||||
clap = { version = "4.5.37", features = ["derive"] }
|
clap = { version = "4.5.37", features = ["derive", "env"] }
|
||||||
|
serde = { version = "1.0.219", features = ["derive"] }
|
||||||
|
sqlx = { version = "0.8.5", features = ["postgres", "runtime-tokio"] }
|
||||||
|
tokio = { version = "1.44.2", features = ["rt", "macros", "rt-multi-thread"] }
|
||||||
|
|||||||
@ -1,8 +1,12 @@
|
|||||||
use std::{collections::HashMap, convert::Infallible, io::Write, str::FromStr};
|
use std::{collections::HashMap, convert::Infallible, io::Write, str::FromStr};
|
||||||
|
|
||||||
use clap::{Parser, ValueEnum};
|
use clap::{Parser, Subcommand};
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use sqlx::{types::Json, PgPool};
|
||||||
|
|
||||||
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, Hash, Ord, PartialOrd)]
|
#[derive(
|
||||||
|
Copy, Clone, Debug, Default, PartialEq, Eq, Hash, Ord, PartialOrd, Serialize, Deserialize,
|
||||||
|
)]
|
||||||
enum MatchType {
|
enum MatchType {
|
||||||
From,
|
From,
|
||||||
Sender,
|
Sender,
|
||||||
@ -19,16 +23,16 @@ enum MatchType {
|
|||||||
#[default]
|
#[default]
|
||||||
Unknown,
|
Unknown,
|
||||||
}
|
}
|
||||||
#[derive(Debug, Default)]
|
#[derive(Debug, Default, Serialize, Deserialize)]
|
||||||
struct Match {
|
struct Match {
|
||||||
match_type: MatchType,
|
match_type: MatchType,
|
||||||
needle: String,
|
needle: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Default)]
|
#[derive(Debug, Default, Serialize, Deserialize)]
|
||||||
struct Rule {
|
struct Rule {
|
||||||
matches: Vec<Match>,
|
matches: Vec<Match>,
|
||||||
tags: Option<String>,
|
tag: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn unescape(s: &str) -> String {
|
fn unescape(s: &str) -> String {
|
||||||
@ -155,13 +159,109 @@ impl FromStr for Match {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Subcommand)]
|
||||||
|
enum Mode {
|
||||||
|
Debug,
|
||||||
|
Notmuchrc,
|
||||||
|
LoadSql {
|
||||||
|
#[arg(short, long, default_value = env!("DATABASE_URL"))]
|
||||||
|
dsn: String,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Simple program to greet a person
|
||||||
|
#[derive(Parser, Debug)]
|
||||||
|
#[command(version, about, long_about = None)]
|
||||||
|
struct Args {
|
||||||
|
#[arg(short, long, default_value = "/home/wathiede/dotfiles/procmailrc")]
|
||||||
|
input: String,
|
||||||
|
|
||||||
|
#[command(subcommand)]
|
||||||
|
mode: Mode,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::main]
|
||||||
|
async fn main() -> anyhow::Result<()> {
|
||||||
|
let args = Args::parse();
|
||||||
|
let mut rules = Vec::new();
|
||||||
|
let mut cur_rule = Rule::default();
|
||||||
|
for l in std::fs::read_to_string(args.input)?.lines() {
|
||||||
|
let l = if let Some(idx) = l.find('#') {
|
||||||
|
&l[..idx]
|
||||||
|
} else {
|
||||||
|
l
|
||||||
|
}
|
||||||
|
.trim();
|
||||||
|
if l.is_empty() {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if l.find('=').is_some() {
|
||||||
|
// Probably a variable assignment, skip line
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
let first = l.chars().nth(0).unwrap_or(' ');
|
||||||
|
match first {
|
||||||
|
':' => {
|
||||||
|
// start of rule
|
||||||
|
}
|
||||||
|
'*' => {
|
||||||
|
// add to current rule
|
||||||
|
let m: Match = l.parse()?;
|
||||||
|
cur_rule.matches.push(m);
|
||||||
|
}
|
||||||
|
'.' => {
|
||||||
|
// delivery to folder
|
||||||
|
cur_rule.tag = Some(cleanup_match(
|
||||||
|
"",
|
||||||
|
&l.replace('.', "/")
|
||||||
|
.replace(' ', "")
|
||||||
|
.trim_matches('/')
|
||||||
|
.to_string(),
|
||||||
|
));
|
||||||
|
rules.push(cur_rule);
|
||||||
|
cur_rule = Rule::default();
|
||||||
|
}
|
||||||
|
'/' => cur_rule = Rule::default(), // Ex. /dev/null
|
||||||
|
'|' => cur_rule = Rule::default(), // external command
|
||||||
|
'$' => {
|
||||||
|
// TODO(wathiede): tag messages with no other tag as 'inbox'
|
||||||
|
cur_rule.tag = Some(cleanup_match("", "inbox"));
|
||||||
|
rules.push(cur_rule);
|
||||||
|
cur_rule = Rule::default();
|
||||||
|
} // variable, should only be $DEFAULT in my config
|
||||||
|
_ => panic!("Unhandled first character '{}'\nLine: {}", first, l),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
match args.mode {
|
||||||
|
Mode::Debug => print_rules(&rules),
|
||||||
|
Mode::Notmuchrc => notmuch_from_rules(std::io::stdout(), &rules)?,
|
||||||
|
Mode::LoadSql { dsn } => load_sql(&dsn, &rules).await?,
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn print_rules(rules: &[Rule]) {
|
||||||
|
let mut tally = HashMap::new();
|
||||||
|
for r in rules {
|
||||||
|
for m in &r.matches {
|
||||||
|
*tally.entry(m.match_type).or_insert(0) += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let mut sorted: Vec<_> = tally.iter().map(|(k, v)| (v, k)).collect();
|
||||||
|
sorted.sort();
|
||||||
|
sorted.reverse();
|
||||||
|
for (v, k) in sorted {
|
||||||
|
println!("{k:?}: {v}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn notmuch_from_rules<W: Write>(mut w: W, rules: &[Rule]) -> anyhow::Result<()> {
|
fn notmuch_from_rules<W: Write>(mut w: W, rules: &[Rule]) -> anyhow::Result<()> {
|
||||||
// TODO(wathiede): if reindexing this many tags is too slow, see if combining rules per tag is
|
// TODO(wathiede): if reindexing this many tags is too slow, see if combining rules per tag is
|
||||||
// faster.
|
// faster.
|
||||||
let mut lines = Vec::new();
|
let mut lines = Vec::new();
|
||||||
for r in rules {
|
for r in rules {
|
||||||
for m in &r.matches {
|
for m in &r.matches {
|
||||||
if let Some(t) = &r.tags {
|
if let Some(t) = &r.tag {
|
||||||
if let MatchType::Unknown = m.match_type {
|
if let MatchType::Unknown = m.match_type {
|
||||||
eprintln!("rule has unknown match {:?}", r);
|
eprintln!("rule has unknown match {:?}", r);
|
||||||
continue;
|
continue;
|
||||||
@ -206,92 +306,26 @@ fn notmuch_from_rules<W: Write>(mut w: W, rules: &[Rule]) -> anyhow::Result<()>
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ValueEnum)]
|
async fn load_sql(dsn: &str, rules: &[Rule]) -> anyhow::Result<()> {
|
||||||
enum Mode {
|
let pool = PgPool::connect(dsn).await?;
|
||||||
Debug,
|
sqlx::migrate!("../server/migrations").run(&pool).await?;
|
||||||
Notmuchrc,
|
println!("clearing email_rule table");
|
||||||
}
|
sqlx::query!("DELETE FROM email_rule")
|
||||||
|
.execute(&pool)
|
||||||
|
.await?;
|
||||||
|
|
||||||
/// Simple program to greet a person
|
for (order, rule) in rules.iter().enumerate() {
|
||||||
#[derive(Parser, Debug)]
|
println!("inserting {order}: {rule:?}");
|
||||||
#[command(version, about, long_about = None)]
|
sqlx::query!(
|
||||||
struct Args {
|
r#"
|
||||||
#[arg(short, long, default_value = "/home/wathiede/dotfiles/procmailrc")]
|
INSERT INTO email_rule (sort_order, rule)
|
||||||
input: String,
|
VALUES ($1, $2)
|
||||||
|
"#,
|
||||||
#[arg(value_enum)]
|
order as i32,
|
||||||
mode: Mode,
|
Json(rule) as _
|
||||||
}
|
)
|
||||||
|
.execute(&pool)
|
||||||
fn main() -> anyhow::Result<()> {
|
.await?;
|
||||||
let args = Args::parse();
|
|
||||||
let mut rules = Vec::new();
|
|
||||||
let mut cur_rule = Rule::default();
|
|
||||||
for l in std::fs::read_to_string(args.input)?.lines() {
|
|
||||||
let l = if let Some(idx) = l.find('#') {
|
|
||||||
&l[..idx]
|
|
||||||
} else {
|
|
||||||
l
|
|
||||||
}
|
|
||||||
.trim();
|
|
||||||
if l.is_empty() {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if l.find('=').is_some() {
|
|
||||||
// Probably a variable assignment, skip line
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
let first = l.chars().nth(0).unwrap_or(' ');
|
|
||||||
match first {
|
|
||||||
':' => {
|
|
||||||
// start of rule
|
|
||||||
}
|
|
||||||
'*' => {
|
|
||||||
// add to current rule
|
|
||||||
let m: Match = l.parse()?;
|
|
||||||
cur_rule.matches.push(m);
|
|
||||||
}
|
|
||||||
'.' => {
|
|
||||||
// delivery to folder
|
|
||||||
cur_rule.tags = Some(cleanup_match(
|
|
||||||
"",
|
|
||||||
&l.replace('.', "/")
|
|
||||||
.replace(' ', "")
|
|
||||||
.trim_matches('/')
|
|
||||||
.to_string(),
|
|
||||||
));
|
|
||||||
rules.push(cur_rule);
|
|
||||||
cur_rule = Rule::default();
|
|
||||||
}
|
|
||||||
'/' => cur_rule = Rule::default(), // Ex. /dev/null
|
|
||||||
'|' => cur_rule = Rule::default(), // external command
|
|
||||||
'$' => {
|
|
||||||
// TODO(wathiede): tag messages with no other tag as 'inbox'
|
|
||||||
cur_rule.tags = Some(cleanup_match("", "inbox"));
|
|
||||||
rules.push(cur_rule);
|
|
||||||
cur_rule = Rule::default();
|
|
||||||
} // variable, should only be $DEFAULT in my config
|
|
||||||
_ => panic!("Unhandled first character '{}'\nLine: {}", first, l),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
match args.mode {
|
|
||||||
Mode::Debug => print_rules(&rules),
|
|
||||||
Mode::Notmuchrc => notmuch_from_rules(std::io::stdout(), &rules)?,
|
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn print_rules(rules: &[Rule]) {
|
|
||||||
let mut tally = HashMap::new();
|
|
||||||
for r in rules {
|
|
||||||
for m in &r.matches {
|
|
||||||
*tally.entry(m.match_type).or_insert(0) += 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
let mut sorted: Vec<_> = tally.iter().map(|(k, v)| (v, k)).collect();
|
|
||||||
sorted.sort();
|
|
||||||
sorted.reverse();
|
|
||||||
for (v, k) in sorted {
|
|
||||||
println!("{k:?}: {v}");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
3
server/migrations/20250419202131_email-rules.down.sql
Normal file
3
server/migrations/20250419202131_email-rules.down.sql
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
DROP TABLE IF NOT EXISTS email_rule;
|
||||||
|
|
||||||
|
-- Add down migration script here
|
||||||
5
server/migrations/20250419202131_email-rules.up.sql
Normal file
5
server/migrations/20250419202131_email-rules.up.sql
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
CREATE TABLE IF NOT EXISTS email_rule (
|
||||||
|
id integer NOT NULL GENERATED ALWAYS AS IDENTITY,
|
||||||
|
sort_order integer NOT NULL,
|
||||||
|
rule jsonb NOT NULL
|
||||||
|
);
|
||||||
Loading…
x
Reference in New Issue
Block a user