aboutsummaryrefslogtreecommitdiffstats
path: root/src/main.rs
blob: 8b9c93a433a00759325139bbf084be0857ecb6ac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
//! alias-auto-add
//!
//! Adds unique aliases to a Mutt alias file.
//!
//! Reads an email from STDIN and tries to add an alias for the from address
//! listed. If the given alias already exists, a new unique alias is generated
//! and used instead. This allows us to always capture an alias even if a
//! person has multiple email addresses.

use std::env;
use std::io::{self, BufRead};

mod alias;

#[cfg(test)]
mod tests;

use alias::*;

fn print_usage(program: &str) {
    println!("Usage: {} FILE", program);
}

fn main() {
    let args: Vec<String> = env::args().collect();
    let program = args[0].clone();

    let file = if args.len() > 1 {
        &args[1]
    } else {
        print_usage(&program);
        return;
    };

    let stdin = io::stdin();

    for line in stdin.lock().lines() {
        let line = line.expect("Error reading from STDIN");

        // Write the message to STDOUT so that it can be properly read by Mutt
        println!("{}", line);

        if line.starts_with("From: ") {
            let mut alias = Alias::new(&line);
            alias.write_to_file(&file).ok();
        }
    }
}