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
//
// imag - the personal information management suite for the commandline
// Copyright (C) 2015-2020 Matthias Beyer <mail@beyermatthias.de> and contributors
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; version
// 2.1 of the License.
//
// This library 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
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
//

use std::path::Path;
use std::fs::OpenOptions;
use std::io::Read;

use failure::Error;
use failure::Fallible as Result;
use failure::ResultExt;

/// Get the message header at a specific key
///
/// # WARNING
///
/// Key must be all-lowercase
///
/// # WARNING
///
/// Expensive, as mailfile gets read from disk internally.
/// TODO: Optimize
///
pub(crate) fn get_message_header_at_key<P: AsRef<Path>, K: AsRef<str>>(p: P, k: K) -> Result<String> {
    let buffer = {
        let mut buffer = Vec::with_capacity(4096); // allocate new buffer with 4 KB space
        OpenOptions::new()
            .read(true)
            .open(p.as_ref())?
            .read_to_end(&mut buffer)?;
        buffer
    };

    ::mailparse::parse_mail(&buffer)
        .context(format_err!("Cannot parse Email {}", p.as_ref().display()))?
        .headers
        .into_iter()
        .filter_map(|hdr| match hdr.get_key().context("Cannot get key from mail header").map_err(Error::from) {
            Ok(key) => {
                let lower_key = key.to_lowercase();
                trace!("Test: {} == {}", lower_key, k.as_ref());
                if lower_key == k.as_ref() {
                    Some(Ok(hdr))
                } else {
                    None
                }
            },
            Err(e) => Some(Err(e))
        })
        .next()
        .ok_or_else(|| format_err!("'{}' not found in {}", k.as_ref(), p.as_ref().display()))?
        .and_then(|hdr| hdr.get_value().context("Cannot get value from mail header").map_err(Error::from))
}

pub(crate) fn get_message_id_for_mailfile<P: AsRef<Path>>(p: P) -> Result<String> {
    get_message_header_at_key(p, "message-id").map(strip_message_delimiters)
}

/// Strips message delimiters ('<' and '>') from a Message-ID field.
pub(crate) fn strip_message_delimiters<ID: AsRef<str>>(id: ID) -> String {
    let len  = id.as_ref().len();
    // We have to strip the '<' and '>' if there are any, because they do not belong to the
    // Message-Id at all
    id.as_ref()
        .chars()
        .enumerate()
        .filter(|(idx, chr)| !(*idx == 0 && *chr == '<' || *idx == len - 1 && *chr == '>'))
        .map(|tpl| tpl.1)
        .collect()
}

pub fn get_mail_text_content<P: AsRef<Path>>(p: P) -> Result<String> {
    ::mailparse::parse_mail(::std::fs::read_to_string(p.as_ref())?.as_bytes())
        .context(format_err!("Cannot parse Email {}", p.as_ref().display()))?
        .get_body()
        .context("Cannot get body of mail")
        .map_err(Error::from)
}