vsmtp-mail-parser 2.2.1

Next-gen MTA. Secured, Faster and Greener
Documentation
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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
/*
 * vSMTP mail transfer agent
 * Copyright (C) 2022 viridIT SAS
 *
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see https://www.gnu.org/licenses/.
 *
*/
use crate::helpers::get_mime_type;
use crate::helpers::read_header;
use crate::message::mail::{BodyType, Mail, MailHeaders};
use crate::message::mime_type::{Mime, MimeBodyType, MimeHeader, MimeMultipart};
use crate::{MailParser, RawBody};
use crate::{ParserError, ParserResult};

/// a boundary serves as a delimiter between mime parts in a multipart section.
enum BoundaryType {
    Delimiter,
    End,
    OutOfScope,
}

/// Instance parsing a message body
#[derive(Default)]
pub struct MailMimeParser {
    boundary_stack: Vec<String>,
}

impl MailParser for MailMimeParser {
    fn parse_sync(&mut self, raw: Vec<Vec<u8>>) -> ParserResult<either::Either<RawBody, Mail>> {
        let ref_raw = raw
            .iter()
            .map(|l| std::str::from_utf8(l).unwrap())
            .collect::<Vec<&str>>();
        self.parse_inner(&mut &ref_raw[..]).map(either::Right)
    }
}

impl MailMimeParser {
    #[allow(clippy::cognitive_complexity)]
    #[tracing::instrument(name = "parsing email", skip_all)]
    fn parse_inner(&mut self, content: &mut &[&str]) -> ParserResult<Mail> {
        let mut headers = MailHeaders(Vec::with_capacity(10));
        let mut mime_headers = Vec::with_capacity(10);

        while !content.is_empty() {
            match read_header(content) {
                Some((name, value)) if is_mime_header(&name) => {
                    // FIXME: should header content be traced ?
                    tracing::trace!("new mime header found: '{name}' => '{value}'",);
                    mime_headers.push(get_mime_header(&name, &value));
                }

                Some((name, value)) => {
                    tracing::trace!("new header found: '{name}' => '{value}'",);
                    headers.0.push((name, value));
                }

                None => {
                    // there is an empty lines after headers
                    *content = &content[1..];

                    if content.is_empty() {
                        return Ok(Mail {
                            headers,
                            body: BodyType::Undefined,
                        });
                    }

                    tracing::trace!("finished parsing headers, body found.");

                    check_mandatory_headers(&headers.0)?;
                    let has_mime_version = headers.0.iter().any(|(name, _)| name == "mime-version");
                    tracing::trace!("mime-version header found?: {has_mime_version}",);

                    return Ok(Mail {
                        headers,
                        body: if has_mime_version {
                            BodyType::Mime(Box::new(self.as_mime_body(
                                content,
                                mime_headers,
                                None,
                            )?))
                        } else {
                            BodyType::Regular(self.as_regular_body(content)?)
                        },
                    });
                }
            };

            *content = &content[1..];
        }

        Ok(Mail {
            headers,
            body: BodyType::Undefined,
        })
    }

    fn check_boundary(&self, line: &str) -> Option<BoundaryType> {
        // we start by checking if the stack as any boundary.
        self.boundary_stack.last().and_then(|b| {
            get_boundary_type(line, b).map_or_else(
                || {
                    if self.boundary_stack[..self.boundary_stack.len() - 1]
                        .iter()
                        .any(|b| get_boundary_type(line, b).is_some())
                    {
                        Some(BoundaryType::OutOfScope)
                    } else {
                        None
                    }
                },
                Some,
            )
        })
    }

    fn as_regular_body(&self, content: &mut &[&str]) -> ParserResult<Vec<String>> {
        let mut body = Vec::with_capacity(100);
        tracing::trace!("storing body of regular message.");

        while !content.is_empty() {
            match self.check_boundary(content[0]) {
                // the current mail ils probably embedded.
                // we can stop parsing the mail and return it.
                Some(BoundaryType::Delimiter | BoundaryType::End) => {
                    tracing::trace!("boundary found in regular message.");
                    *content = &content[1..];
                    return Ok(body);
                }

                Some(BoundaryType::OutOfScope) => {
                    return Err(ParserError::MisplacedBoundary(format!(
                        "'{}' boundary is out of scope.",
                        &content[0],
                    )));
                }

                // we just skip the line & push the content in the body.
                None => body.push(content[0].to_string()),
            };
            *content = &content[1..];
        }

        // EOF reached.
        tracing::trace!("EOF reached while storing body of regular message.");
        Ok(body)
    }

    // TODO: merge with @as_regular_body
    fn parse_regular_mime_body(&self, content: &mut &[&str]) -> ParserResult<Vec<String>> {
        let mut body = Vec::new();

        while !content.is_empty() {
            match self.check_boundary(content[0]) {
                Some(BoundaryType::Delimiter | BoundaryType::End) => {
                    return Ok(body);
                }

                Some(BoundaryType::OutOfScope) => {
                    return Err(ParserError::MisplacedBoundary(format!(
                        "'{}' boundary is out of scope.",
                        &content[0],
                    )));
                }

                None => {
                    // we skip the header & body separation line.
                    if !(body.is_empty() && content[0].is_empty()) {
                        body.push(content[0].to_string());
                    }
                }
            };
            *content = &content[1..];
        }

        Ok(body)
    }

    fn as_mime_body(
        &mut self,
        content: &mut &[&str],
        headers: Vec<MimeHeader>,
        parent: Option<&[MimeHeader]>,
    ) -> ParserResult<Mime> {
        match get_mime_type(&headers, parent)? {
            ("message", sub_type) => {
                tracing::trace!("'message' content type found (message/{})", sub_type);
                *content = &content[1..];
                Ok(Mime {
                    headers,
                    content: MimeBodyType::Embedded(self.parse_inner(content)?),
                })
            }
            ("multipart", _) => {
                tracing::trace!("parsing multipart.");
                Ok(Mime {
                    headers: headers.clone(),
                    content: MimeBodyType::Multipart(self.parse_multipart(&headers, content)?),
                })
            }
            (body_type, sub_type) => {
                tracing::trace!(
                    "parsing regular mime section of type '{}' and subtype '{}'",
                    body_type,
                    sub_type
                );
                Ok(Mime {
                    headers,
                    content: MimeBodyType::Regular(self.parse_regular_mime_body(content)?),
                })
            }
        }
    }

    fn parse_mime(
        &mut self,
        content: &mut &[&str],
        parent: Option<&[MimeHeader]>,
    ) -> ParserResult<Mime> {
        let mut headers = Vec::new();

        tracing::trace!("parsing a mime section.");

        while content.len() > 1 {
            if let Some((name, value)) = read_header(content) {
                tracing::trace!("mime-header found: '{}' => '{}'.", name, value);
                headers.push(get_mime_header(&name, &value));
            } else {
                tracing::trace!("finished reading mime headers, body found.");
                break;
            };
            *content = &content[1..];
        }

        self.as_mime_body(content, headers, parent)
    }

    fn parse_preamble<'a>(&self, content: &'a mut &[&str]) -> ParserResult<Vec<&'a str>> {
        tracing::trace!("storing preamble for a multipart mime section.");
        let mut preamble = Vec::new();

        while content.len() > 1 {
            match self.check_boundary(content[0]) {
                Some(BoundaryType::Delimiter) => {
                    tracing::trace!(
                        "delimiter boundary found for multipart, finished storing preamble."
                    );
                    return Ok(preamble);
                }
                Some(BoundaryType::End) => {
                    return Err(ParserError::MisplacedBoundary(
                        "their should not be a end boundary in the preamble".to_string(),
                    ));
                }
                Some(BoundaryType::OutOfScope) => {
                    return Err(ParserError::MisplacedBoundary(format!(
                        "'{}' boundary is out of scope.",
                        &content[0],
                    )));
                }
                None => preamble.push(content[0]),
            };

            *content = &content[1..];
        }

        Err(ParserError::BoundaryNotFound(
            "boundary not found after mime part preamble".to_string(),
        ))
    }

    fn parse_epilogue<'a>(&self, content: &'a mut &[&str]) -> ParserResult<Vec<&'a str>> {
        tracing::trace!("storing epilogue for a multipart mime section.");
        let mut epilogue = Vec::new();

        while content.len() > 1 {
            match self.check_boundary(content[0]) {
                // there could be an ending or delimiting boundary,
                // meaning that the next lines will be part of another mime part.
                Some(BoundaryType::Delimiter | BoundaryType::End) => {
                    tracing::trace!("boundary found for multipart, finished storing epilogue.");
                    break;
                }
                Some(BoundaryType::OutOfScope) => {
                    return Err(ParserError::MisplacedBoundary(format!(
                        "'{}' boundary is out of scope.",
                        &content[0],
                    )));
                }
                None => epilogue.push(content[0]),
            };
            *content = &content[1..];
        }

        Ok(epilogue)
    }

    #[allow(clippy::cognitive_complexity)]
    fn parse_multipart(
        &mut self,
        headers: &[MimeHeader],
        content: &mut &[&str],
    ) -> ParserResult<MimeMultipart> {
        let content_type = headers.iter().find(|h| h.name == "content-type").unwrap();

        match content_type.args.get("boundary") {
            Some(b) => {
                tracing::trace!("boundary found in parameters: '{}'.", b);
                self.boundary_stack.push(b.to_string());
            }
            None => {
                return Err(ParserError::BoundaryNotFound(
                    "boundary parameter not found in Content-Type header for a multipart."
                        .to_string(),
                ))
            }
        };

        let mut multi_parts = MimeMultipart {
            preamble: self
                .parse_preamble(content)?
                .iter()
                .map(ToString::to_string)
                .collect::<Vec<_>>()
                .join("\r\n"),
            parts: Vec::new(),
            epilogue: String::new(),
        };

        while content.len() > 1 {
            match self.check_boundary(content[0]) {
                Some(BoundaryType::Delimiter) => {
                    tracing::trace!(
                        "delimiter boundary found while parsing multipart: '{}', calling parse_mime.",
                        &content[0]
                    );
                    *content = &content[1..];

                    multi_parts
                        .parts
                        .push(self.parse_mime(content, Some(headers))?);
                }

                Some(BoundaryType::End) => {
                    tracing::trace!(
                        "end boundary found while parsing multipart: '{}', stopping multipart parsing.",
                        &content[0]
                    );
                    self.boundary_stack.pop();
                    *content = &content[1..];
                    multi_parts.epilogue = self
                        .parse_epilogue(content)?
                        .iter()
                        .map(ToString::to_string)
                        .collect::<Vec<_>>()
                        .join("\r\n");
                    return Ok(multi_parts);
                }

                Some(BoundaryType::OutOfScope) => {
                    return Err(ParserError::MisplacedBoundary(format!(
                        "'{}' boundary is out of scope.",
                        &content[0],
                    )));
                }

                None => {
                    tracing::trace!("EOF reached while parsing multipart.",);
                    return Ok(multi_parts);
                }
            };
        }

        Ok(multi_parts)
    }
}

fn check_mandatory_headers(headers: &[(String, String)]) -> ParserResult<()> {
    /// rfc822 headers that requires to be specified.
    /// ? does they require ONLY to be at the root message ? (in case of embedded messages)
    const MANDATORY_HEADERS: [&str; 2] = ["from", "date"];

    for mh in MANDATORY_HEADERS {
        if !headers.iter().any(|h| h.0.as_str() == mh) {
            return Err(ParserError::MandatoryHeadersNotFound(mh.to_string()));
        }
    }

    Ok(())
}

/// take the name and value of a header and parses those to create
/// a `MimeHeader` struct.
///
/// # Arguments
///
/// * `name` - the name of the header.
/// * `value` - the value of the header (with all params, folded included if any).
#[must_use]
pub fn get_mime_header(name: &str, value: &str) -> MimeHeader {
    // cut the current line using the ";" separator into a vector of "arg=value" strings.
    let args = value.split(';').collect::<Vec<&str>>();
    let mut args_iter = args.iter();

    MimeHeader {
        name: name.to_string(),
        value: args_iter.next().unwrap_or(&"").trim().to_lowercase(),

        // split every element of args by the "=" token (if there are any parameters).
        // inserts all resulting key / value pair into new_args.
        args: args_iter
            .filter_map(|arg| {
                let mut split = arg.splitn(2, '=');
                match (split.next(), split.next()) {
                    (Some(key), Some(value)) => Some((key, value)),
                    // no error here, bad arguments are just omitted.
                    _ => None,
                }
            })
            .map(|(key, value)| {
                (
                    key.trim().to_lowercase(),
                    match (value.find('"'), value.rfind('"')) {
                        (Some(first), Some(last)) if first < last => &value[first + 1..last],
                        _ => value,
                    }
                    // TODO: replace all characters specified in rfc.
                    .replace(&['\"', '\\'][..], ""),
                )
            })
            .collect::<std::collections::HashMap<String, String>>(),
    }
}

// check rfc2045 p.9. Additional MIME Header Fields.
#[inline]
fn is_mime_header(name: &str) -> bool {
    name.starts_with("content-")
}

// is used to deduce the boundary type.
// ! this method is called too many times, causing slow downs.
#[inline]
fn get_boundary_type(line: &str, boundary: &str) -> Option<BoundaryType> {
    match (
        // TODO: can be optimized.
        line.starts_with("--") && !line.starts_with(boundary),
        line.ends_with("--") && !line.ends_with(boundary),
        line.contains(boundary),
    ) {
        (true, false, true) => Some(BoundaryType::Delimiter),
        (true, true, true) => Some(BoundaryType::End),
        _ => None,
    }
}

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

    // NOTE: things to consider:
    //       - header folding (does letter does it automatically ?)
    //       - comments (do we need to keep them ?)
    //       - boundaries
    //       -

    /// FIXME: a \n is added between the headers and the body
    #[test]
    #[ignore]
    fn test_to_raw() {
        let content = vec![
"x-mozilla-status: 0001",
"x-mozilla-status2: 01000000",
"x-mozilla-keys:                                                                                 ",
"fcc: imap://john%40localhost.com@localhost.com/sent",
"x-identity-key: id3",
"x-account-key: account4",
"from: john doe <john@localhost>",
"subject: text content",
"to: john@localhost, green@example.com, foo@example.com, x@x.com",
"message-id: <51734671-2e09-946e-7e3f-ec59b83e82d0@localhost.com>",
"date: tue, 30 nov 2021 20:54:27 +0100",
"x-mozilla-draft-info: internal/draft; vcard=0; receipt=0; dsn=0; uuencode=0;",
" attachmentreminder=0; deliveryformat=1",
"user-agent: mozilla/5.0 (x11; linux x86_64; rv:78.0) gecko/20100101",
" thunderbird/78.14.0",
"mime-version: 1.0",
"content-type: text/plain; charset=utf-8; format=flowed",
"content-language: en-us",
"content-transfer-encoding: 7bit",
"",
"je ne suis qu'un contenu de texte."];

        let parsed = MailMimeParser::default()
            .parse(content.join("\n").as_bytes())
            .expect("parsing failed");

        assert_eq!(
            {
                let (headers, body) = parsed.to_raw();
                [headers, body].join("\n")
            },
            r#"x-mozilla-status: 0001
x-mozilla-status2: 01000000
x-mozilla-keys:
fcc: imap://john%40localhost.com@localhost.com/sent
x-identity-key: id3
x-account-key: account4
from: john doe <john@localhost>
subject: text content
to: john@localhost, green@example.com, foo@example.com, x@x.com
message-id: <51734671-2e09-946e-7e3f-ec59b83e82d0@localhost.com>
date: tue, 30 nov 2021 20:54:27 +0100
x-mozilla-draft-info: internal/draft; vcard=0; receipt=0; dsn=0; uuencode=0; attachmentreminder=0; deliveryformat=1
user-agent: mozilla/5.0 gecko/20100101 thunderbird/78.14.0
mime-version: 1.0
content-type: text/plain; charset="utf-8"; format="flowed"
content-language: en-us
content-transfer-encoding: 7bit

je ne suis qu'un contenu de texte."#
        );
    }
}
*/