~asimpson/mailpreview-cli

ref: cd3871fda618611ecef0f5cf453211c656431ba9 mailpreview-cli/src/main.rs -rw-r--r-- 3.2 KiB
cd3871fd — Adam Simpson fix: use children of multipart/related that are in right format 1 year, 7 months ago
                                                                                
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
use mailparse::*;
use std::error::Error;
use std::fs::File;
use std::io::prelude::*;
use std::io::Read;

fn return_body_from_alternative(mail: &ParsedMail, format: &str) -> Result<String, MailParseError> {
    let mut body = String::new();

    for m in mail.subparts.iter() {
        if m.ctype.mimetype == format {
            body = m.get_body()?;
        }
    }

    Ok(body)
}

fn return_body(mail: ParsedMail, format: String) -> Result<String, MailParseError> {
    let mut body = String::new();

    if !mail.subparts.is_empty() {
        for m in mail.subparts.iter() {
            if m.ctype.mimetype == "multipart/related" {
                // TODO account for mixed
                for i in m.subparts.iter() {
                    if i.ctype.mimetype == "multipart/alternative" {
                        body = return_body_from_alternative(i, &format)?;
                    }

                    if i.ctype.mimetype == format {
                        body = i.get_body()?;
                    }
                }
            }
            if m.ctype.mimetype == "multipart/alternative" {
                body = return_body_from_alternative(m, &format)?;
            }
            if m.ctype.mimetype == format {
                body = m.get_body()?;
            }
        }
    } else {
        body = mail.get_body()?;
    }

    if body.is_empty() {
        println!("No processable body in email");
        std::process::exit(1);
    }

    Ok(body)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn multipart_mixed() {
        let body = "Content-Type: multipart/mixed;
	boundary=\"mixed\"

--mixed
Content-Type: multipart/related;
	boundary=\"related\"

--related
Content-Type: text/html

hello!
--related--

--mixed
Content-Type: text/plain

bye!
--mixed--
";
        let mail = parse_mail(body.as_bytes()).unwrap();
        let body = return_body(mail, "text/html".to_string()).unwrap();
        assert_eq!(body.trim(), "hello!");
    }
}

fn return_path_from_cli() -> Result<String, String> {
    let file = std::env::args().nth(1);

    match file {
        Some(f) => Ok(f),
        None => Err("No message file was provided.".to_string()),
    }
}

fn return_format_from_cli() -> String {
    let format = std::env::args().nth(2);

    if format == Some("text/html".to_string()) {
        "text/html".to_string()
    } else {
        "text/plain".to_string()
    }
}

fn main() -> Result<(), Box<dyn Error>> {
    let file = return_path_from_cli()?;
    let format = return_format_from_cli();
    let mut contents = String::new();
    let mut file_name = String::new();
    File::open(file.trim())?.read_to_string(&mut contents)?;

    let mail = parse_mail(contents.as_bytes())?;
    let message_id = mail
        .headers
        .get_first_value("Message-ID")
        .expect("Message-ID");

    let id_for_file_name = message_id.replace("/", "-");

    if format == "text/html" {
        file_name = format!("/tmp/{}.html", id_for_file_name)
    }

    if format == "text/plain" {
        file_name = format!("/tmp/{}", id_for_file_name)
    }

    let body = return_body(mail, format).expect("parsed body");

    File::create(&file_name)?.write_all(body.as_bytes())?;
    println!("{}", &file_name);

    Ok(())
}