2018-12-15 01:44:32 +00:00
|
|
|
//
|
|
|
|
// imag - the personal information management suite for the commandline
|
|
|
|
// Copyright (C) 2015-2019 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 failure::Error;
|
|
|
|
use failure::Fallible as Result;
|
|
|
|
use failure::ResultExt;
|
|
|
|
|
2019-02-22 10:31:49 +00:00
|
|
|
/// 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> {
|
2018-12-15 01:44:32 +00:00
|
|
|
::mailparse::parse_mail(::std::fs::read_to_string(p.as_ref())?.as_bytes())
|
|
|
|
.context(format_err!("Cannot parse Email {}", p.as_ref().display()))?
|
|
|
|
.headers
|
|
|
|
.into_iter()
|
2019-05-17 22:14:34 +00:00
|
|
|
.filter_map(|hdr| match hdr.get_key().context("Cannot get key from mail header").map_err(Error::from) {
|
2019-02-24 21:33:41 +00:00
|
|
|
Ok(key) => {
|
2019-02-28 17:07:25 +00:00
|
|
|
let lower_key = key.to_lowercase();
|
|
|
|
trace!("Test: {} == {}", lower_key, k.as_ref());
|
|
|
|
if lower_key == k.as_ref() {
|
2019-02-24 21:33:41 +00:00
|
|
|
Some(Ok(hdr))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
2019-05-17 22:14:34 +00:00
|
|
|
},
|
|
|
|
Err(e) => Some(Err(e))
|
2018-12-15 01:44:32 +00:00
|
|
|
})
|
|
|
|
.next()
|
2019-02-22 10:31:49 +00:00
|
|
|
.ok_or_else(|| format_err!("'{}' not found in {}", k.as_ref(), p.as_ref().display()))?
|
2019-05-17 22:14:34 +00:00
|
|
|
.and_then(|hdr| hdr.get_value().context("Cannot get value from mail header").map_err(Error::from))
|
2019-02-22 10:31:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2018-12-15 01:44:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// 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()
|
2019-05-17 22:14:34 +00:00
|
|
|
.context("Cannot get body of mail")
|
2018-12-15 01:44:32 +00:00
|
|
|
.map_err(Error::from)
|
|
|
|
}
|
|
|
|
|