~asimpson/mailpreview-cli

ref: 3c7fad06a86001071d7ce7d204fcb38c37d9a5b0 mailpreview-cli/src/main.rs -rw-r--r-- 4.8 KiB
3c7fad06 — Adam Simpson test: add unit test for every logic branch 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
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" {
                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_alt() {
        let body = "Content-Type: multipart/mixed;
	boundary=MIXED

--MIXED
Content-Type: multipart/related;
	boundary=RELATED

--RELATED
Content-Type: multipart/alternative;
	boundary=ALT

--ALT
Content-Type: text/plain

plain!
--ALT
Content-Type: text/html
Content-Transfer-Encoding: quoted-printable

html!
--ALT--

--RELATED--

--MIXED--
";
        let mail = parse_mail(body.as_bytes()).unwrap();
        let body = return_body(mail, "text/plain".to_string()).unwrap();
        assert_eq!(body.trim(), "plain!");
    }

    #[test]
    fn related_format() {
        let body = "Content-Type: multipart/alternative; boundary=ALT

--ALT
Content-Type: text/plain;

plain!
--ALT
Content-Type: text/html

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

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

--MIXED
Content-Type: multipart/alternative; boundary=ALT

--ALT
Content-Type: text/plain;

plain!
--ALT
Content-Type: text/html

html!
--ALT
Content-Type: text/calendar

calendar!
--ALT--
--MIXED
Content-Type: application/ics

ics!
--MIXED--
";
        let mail = parse_mail(body.as_bytes()).unwrap();
        let body = return_body(mail, "text/plain".to_string()).unwrap();
        assert_eq!(body.trim(), "plain!");
    }

    #[test]
    fn simple() {
        let body = "Content-Type: text/plain

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

    #[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(())
}