summaryrefslogtreecommitdiffstats
path: root/src/addrparse.rs
blob: 3ef4d950a5328ff9f048ea9e869481ca09fc1766 (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
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
/// A representation of a single mailbox. Each mailbox has
/// a routing address `addr` and an optional display name.
#[derive(Clone, Debug, PartialEq)]
pub struct SingleInfo {
    pub display_name: Option<String>,
    pub addr: String,
}

impl SingleInfo {
    fn new(name: Option<String>, addr: String) -> Self {
        SingleInfo {
            display_name: name,
            addr: addr,
        }
    }
}

/// A representation of a group address. It has a name and
/// a list of mailboxes.
#[derive(Clone, Debug, PartialEq)]
pub struct GroupInfo {
    pub group_name: String,
    pub addrs: Vec<SingleInfo>,
}

impl GroupInfo {
    fn new(name: String, addrs: Vec<SingleInfo>) -> Self {
        GroupInfo {
            group_name: name,
            addrs: addrs,
        }
    }
}

/// An abstraction over the two different kinds of top-level addresses allowed
/// in email headers. Group addresses have a name and a list of mailboxes. Single
/// addresses are just a mailbox. Each mailbox consists of what you would consider
/// an email address (e.g. foo@bar.com) and optionally a display name ("Foo Bar").
/// Groups are represented in email headers with colons and semicolons, e.g.
///    To: my-peeps: foo@peeps.org, bar@peeps.org;
#[derive(Clone, Debug, PartialEq)]
pub enum MailAddr {
    Group(GroupInfo),
    Single(SingleInfo),
}

#[derive(Debug)]
enum AddrParseState {
    Initial,
    QuotedName,
    EscapedChar,
    AfterQuotedName,
    BracketedAddr,
    AfterBracketedAddr,
    Unquoted,
    TrailerComment,
}

/// Convert an address field from an email header into a structured type.
/// This function handles the most common formatting of to/from/cc/bcc fields
/// found in email headers.
///
/// # Examples
/// ```
///     use mailparse::{addrparse, MailAddr, SingleInfo};
///     match &addrparse("John Doe <john@doe.com>").unwrap()[0] {
///         MailAddr::Single(info) => {
///             assert_eq!(info.display_name, Some("John Doe".to_string()));
///             assert_eq!(info.addr, "john@doe.com".to_string());
///         }
///         _ => panic!()
///     };
/// ```
pub fn addrparse(addrs: &str) -> Result<Vec<MailAddr>, &'static str> {
    let mut it = addrs.chars();
    addrparse_inner(&mut it, false)
}

fn addrparse_inner(it: &mut std::str::Chars, in_group: bool) -> Result<Vec<MailAddr>, &'static str> {
    let mut result = vec![];
    let mut state = AddrParseState::Initial;

    let mut c = match it.next() {
        None => return Ok(vec![]),
        Some(v) => v,
    };

    let mut name = None;
    let mut addr = None;
    let mut post_quote_ws = None;

    loop {
        match state {
            AddrParseState::Initial => {
                if c.is_whitespace() {
                    // continue in same state
                } else if c == '"' {
                    state = AddrParseState::QuotedName;
                    name = Some(String::new());
                } else if c == '<' {
                    state = AddrParseState::BracketedAddr;
                    addr = Some(String::new());
                } else if c == ';' {
                    if !in_group {
                        return Err("Unexpected group terminator found in initial list");
                    }
                    return Ok(result);
                } else {
                    state = AddrParseState::Unquoted;
                    addr = Some(String::new());
                    addr.as_mut().unwrap().push(c);
                }
            }
            AddrParseState::QuotedName => {
                if c == '\\' {
                    state = AddrParseState::EscapedChar;
                } else if c == '"' {
                    state = AddrParseState::AfterQuotedName;
                } else {
                    name.as_mut().unwrap().push(c);
                }
            }
            AddrParseState::EscapedChar => {
                state = AddrParseState::QuotedName;
                name.as_mut().unwrap().push(c);
            }
            AddrParseState::AfterQuotedName => {
                if c.is_whitespace() {
                    if post_quote_ws.is_none() {
                        post_quote_ws = Some(String::new());
                    }
                    post_quote_ws.as_mut().unwrap().push(c);
                } else if c == '<' {
                    state = AddrParseState::BracketedAddr;
                    addr = Some(String::new());
                } else if c == ':' {
                    if in_group {
                        return Err("Found unexpected nested group");
                    }
                    let group_addrs = try!(addrparse_inner(it, true));
                    state = AddrParseState::Initial;
                    result.push(MailAddr::Group(GroupInfo::new(
                        name.unwrap(),
                        group_addrs.into_iter().map(|addr| {
                            match addr {
                                MailAddr::Single(s) => s,
                                MailAddr::Group(_) => panic!("Unexpected nested group encountered"),
                            }
                        }).collect()
                    )));
                    name = None;
                } else {
                    // I think technically not valid, but this occurs in real-world corpus, so
                    // handle gracefully
                    if c == '"' {
                        post_quote_ws.map(|ws| name.as_mut().unwrap().push_str(&ws));
                        state = AddrParseState::QuotedName;
                    } else {
                        post_quote_ws.map(|ws| name.as_mut().unwrap().push_str(&ws));
                        name.as_mut().unwrap().push(c);
                    }
                    post_quote_ws = None;
                }
            }
            AddrParseState::BracketedAddr => {
                if c == '>' {
                    state = AddrParseState::AfterBracketedAddr;
                    result.push(MailAddr::Single(SingleInfo::new(name, addr.unwrap())));
                    name = None;
                    addr = None;
                } else {
                    addr.as_mut().unwrap().push(c);
                }
            }
            AddrParseState::AfterBracketedAddr => {
                if c.is_whitespace() {
                    // continue in same state
                } else if c == ',' {
                    state = AddrParseState::Initial;
                } else if c == ';' {
                    if in_group {
                        return Ok(result);
                    }
                    // Technically not valid, but a similar case occurs in real-world corpus, so handle it gracefully
                    state = AddrParseState::Initial;
                } else if c == '(' {
                    state = AddrParseState::TrailerComment;
                } else {
                    return Err("Unexpected char found after bracketed address");
                }
            }
            AddrParseState::Unquoted => {
                if c == '<' {
                    state = AddrParseState::BracketedAddr;
                    name = addr.map(|s| s.trim_end().to_owned());
                    addr = Some(String::new());
                } else if c == ',' {
                    state = AddrParseState::Initial;
                    result.push(MailAddr::Single(SingleInfo::new(None, addr.unwrap().trim_end().to_owned())));
                    addr = None;
                } else if c == ';' {
                    result.push(MailAddr::Single(SingleInfo::new(None, addr.unwrap().trim_end().to_owned())));
                    if in_group {
                        return Ok(result);
                    }
                    // Technically not valid, but occurs in real-world corpus, so handle it gracefully
                    state = AddrParseState::Initial;
                    addr = None;
                } else if c == ':' {
                    if in_group {
                        return Err("Found unexpected nested group");
                    }
                    let group_addrs = try!(addrparse_inner(it, true));
                    state = AddrParseState::Initial;
                    result.push(MailAddr::Group(GroupInfo::new(
                        addr.unwrap().trim_end().to_owned(),
                        group_addrs.into_iter().map(|addr| {
                            match addr {
                                MailAddr::Single(s) => s,
                                MailAddr::Group(_) => panic!("Unexpected nested group encountered"),
                            }
                        }).collect()
                    )));
                    addr = None;
                } else {
                    addr.as_mut().unwrap().push(c);
                }
            }
            AddrParseState::TrailerComment => {
                if c == ')' {
                    state = AddrParseState::AfterBracketedAddr;
                }
            }
        }

        c = match it.next() {
            None => break,
            Some(v) => v,
        };
    }

    if in_group {
        return Err("Found unterminated group address");
    }

    match state {
        AddrParseState::QuotedName |
        AddrParseState::EscapedChar |
        AddrParseState::AfterQuotedName |
        AddrParseState::BracketedAddr |
        AddrParseState::TrailerComment => {
            Err("Address string unexpected terminated")
        }
        AddrParseState::Unquoted => {
            result.push(MailAddr::Single(SingleInfo::new(None, addr.unwrap().trim_end().to_owned())));
            Ok(result)
        }
        _ => {
            Ok(result)
        }
    }
}

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

    #[test]
    fn parse_basic() {
        assert_eq!(
            addrparse("foo bar <foo@bar.com>").unwrap(),
            vec![MailAddr::Single(SingleInfo::new(Some("foo bar".to_string()), "foo@bar.com".to_string()))]
        );
        assert_eq!(
            addrparse("\"foo bar\" <foo@bar.com>").unwrap(),
            vec![MailAddr::Single(SingleInfo::new(Some("foo bar".to_string()), "foo@bar.com".to_string()))]
        );
        assert_eq!(
            addrparse("foo@bar.com ").unwrap(),
            vec![MailAddr::Single(SingleInfo::new(None, "foo@bar.com".to_string()))]
        );
        assert_eq!(
            addrparse("foo <bar>").unwrap(),
            vec![MailAddr::Single(SingleInfo::new(Some("foo".to_string()), "bar".to_string()))]
        );
        assert_eq!(
            addrparse("\"foo\" <bar>").unwrap(),
            vec![MailAddr::Single(SingleInfo::new(Some("foo".to_string()), "bar".to_string()))]
        );
        assert_eq!(
            addrparse("\"foo \" <bar>").unwrap(),
            vec![MailAddr::Single(SingleInfo::new(Some("foo ".to_string()), "bar".to_string()))]
        );
    }

    #[test]
    fn parse_backslashes() {
        assert_eq!(
            addrparse(r#" "First \"nick\" Last" <user@host.tld> "#).unwrap(),
            vec![MailAddr::Single(SingleInfo::new(Some("First \"nick\" Last".to_string()), "user@host.tld".to_string()))]
        );
        assert_eq!(
            addrparse(r#" First \"nick\" Last <user@host.tld> "#).unwrap(),
            vec![MailAddr::Single(SingleInfo::new(Some("First \\\"nick\\\" Last".to_string()), "user@host.tld".to_string()))]
        );
    }

    #[test]
    fn parse_multi() {
        assert_eq!(
            addrparse("foo <bar>, joe, baz <quux>").unwrap(),
            vec![
                MailAddr::Single(SingleInfo::new(Some("foo".to_string()), "bar".to_string())),
                MailAddr::Single(SingleInfo::new(None, "joe".to_string())),
                MailAddr::Single(SingleInfo::new(Some("baz".to_string()), "quux".to_string())),
            ]
        );
    }

    #[test]
    fn parse_empty_group() {
        assert_eq!(
            addrparse("empty-group:;").unwrap(),
            vec![MailAddr::Group(GroupInfo::new("empty-group".to_string(), vec![]))]
        );
        assert_eq!(
            addrparse(" empty-group : ; ").unwrap(),
            vec![MailAddr::Group(GroupInfo::new("empty-group".to_string(), vec![]))]
        );
    }

    #[test]
    fn parse_simple_group() {
        assert_eq!(
            addrparse("bar-group: foo <foo@bar.com>;").unwrap(),
            vec![MailAddr::Group(GroupInfo::new("bar-group".to_string(), vec![
                SingleInfo::new(Some("foo".to_string()), "foo@bar.com".to_string()),
            ]))]
        );
        assert_eq!(
            addrparse("bar-group: foo <foo@bar.com>, baz@bar.com;").unwrap(),
            vec![MailAddr::Group(GroupInfo::new("bar-group".to_string(), vec![
                SingleInfo::new(Some("foo".to_string()), "foo@bar.com".to_string()),
                SingleInfo::new(None, "baz@bar.com".to_string()),
            ]))]
        );
    }

    #[test]
    fn parse_mixed() {
        assert_eq!(
            addrparse("joe@bloe.com, bar-group: foo <foo@bar.com>;").unwrap(),
            vec![
                MailAddr::Single(SingleInfo::new(None, "joe@bloe.com".to_string())),
                MailAddr::Group(GroupInfo::new("bar-group".to_string(), vec![
                    SingleInfo::new(Some("foo".to_string()), "foo@bar.com".to_string()),
                ])),
            ]
        );
        assert_eq!(
            addrparse("bar-group: foo <foo@bar.com>; joe@bloe.com").unwrap(),
            vec![
                MailAddr::Group(GroupInfo::new("bar-group".to_string(), vec![
                    SingleInfo::new(Some("foo".to_string()), "foo@bar.com".to_string()),
                ])),
                MailAddr::Single(SingleInfo::new(None, "joe@bloe.com".to_string())),
            ]
        );
        assert_eq!(
            addrparse("flim@flam.com, bar-group: foo <foo@bar.com>; joe@bloe.com").unwrap(),
            vec![
                MailAddr::Single(SingleInfo::new(None, "flim@flam.com".to_string())),
                MailAddr::Group(GroupInfo::new("bar-group".to_string(), vec![
                    SingleInfo::new(Some("foo".to_string()), "foo@bar.com".to_string()),
                ])),
                MailAddr::Single(SingleInfo::new(None, "joe@bloe.com".to_string())),
            ]
        );
        assert_eq!(
            addrparse("first-group:; flim@flam.com, bar-group: foo <foo@bar.com>; joe@bloe.com, final-group: zip, zap, \"Zaphod\" <zaphod@beeblebrox>;").unwrap(),
            vec![
                MailAddr::Group(GroupInfo::new("first-group".to_string(), vec![])),
                MailAddr::Single(SingleInfo::new(None, "flim@flam.com".to_string())),
                MailAddr::Group(GroupInfo::new("bar-group".to_string(), vec![
                    SingleInfo::new(Some("foo".to_string()), "foo@bar.com".to_string()),
                ])),
                MailAddr::Single(SingleInfo::new(None, "joe@bloe.com".to_string())),
                MailAddr::Group(GroupInfo::new("final-group".to_string(), vec![
                    SingleInfo::new(None, "zip".to_string()),
                    SingleInfo::new(None, "zap".to_string()),
                    SingleInfo::new(Some("Zaphod".to_string()), "zaphod@beeblebrox".to_string()),
                ])),
            ]
        );
    }

    #[test]
    fn real_world_examples() {
        // taken from a real "From" header. This might not be valid according to the RFC
        // but obviously made it through the internet so we should at least not crash.
        assert_eq!(
            addrparse("\"The Foo of Bar\" Course Staff <foo-no-reply@bar.edx.org>").unwrap(),
            vec![MailAddr::Single(SingleInfo::new(Some("The Foo of Bar Course Staff".to_string()), "foo-no-reply@bar.edx.org".to_string()))]
        );

        // This one has a comment tacked on to the end. Adding proper support for comments seems
        // complicated so I just added trailer comment support.
        assert_eq!(
            addrparse("John Doe <support@github.com> (GitHub Staff)").unwrap(),
            vec![MailAddr::Single(SingleInfo::new(Some("John Doe".to_string()), "support@github.com".to_string()))]
        );

        // Taken from a real world "To" header. It was spam, but still...
        assert_eq!(
            addrparse("foo@bar.com;").unwrap(),
            vec![MailAddr::Single(SingleInfo::new(None, "foo@bar.com".to_string()))]
        );
    }
}