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
use failure::Fallible as Result;
use failure::ResultExt;
use failure::Error;
use libimagstore::store::Entry;
use libimagentryutil::isa::Is;
use libimagentryutil::isa::IsKindHeaderPathProvider;
use libimagentryref::reference::Config as RefConfig;
use libimagentryref::reference::{Ref, RefFassade};
provide_kindflag_path!(pub IsMail, "mail.is_mail");
pub trait Mail : RefFassade {
fn is_mail(&self) -> Result<bool>;
fn get_field(&self, refconfig: &RefConfig, field: &str) -> Result<Option<String>>;
fn get_from(&self, refconfig: &RefConfig) -> Result<Option<String>>;
fn get_to(&self, refconfig: &RefConfig) -> Result<Option<String>>;
fn get_subject(&self, refconfig: &RefConfig) -> Result<Option<String>>;
fn get_message_id(&self, refconfig: &RefConfig) -> Result<Option<String>>;
fn get_in_reply_to(&self, refconfig: &RefConfig) -> Result<Option<String>>;
}
impl Mail for Entry {
fn is_mail(&self) -> Result<bool> {
self.is::<IsMail>()
}
fn get_field(&self, refconfig: &RefConfig, field: &str) -> Result<Option<String>> {
use std::fs::read_to_string;
use crate::hasher::MailHasher;
debug!("Getting field in mail: {:?}", field);
let mail_file_location = self.as_ref_with_hasher::<MailHasher>().get_path(refconfig)?;
match ::mailparse::parse_mail(read_to_string(mail_file_location.as_path())?.as_bytes())
.context(format_err!("Cannot parse Email {}", mail_file_location.display()))?
.headers
.into_iter()
.filter_map(|hdr| {
match hdr.get_key()
.context(format_err!("Cannot fetch key '{}' from Email {}", field, mail_file_location.display()))
.map_err(Error::from)
{
Ok(k) => if k == field {
Some(Ok(hdr))
} else {
None
},
Err(e) => Some(Err(e)),
}
})
.next()
{
None => Ok(None),
Some(Err(e)) => Err(e),
Some(Ok(hdr)) => Ok(Some(hdr.get_value()?))
}
}
fn get_from(&self, refconfig: &RefConfig) -> Result<Option<String>> {
self.get_field(refconfig, "From")
}
fn get_to(&self, refconfig: &RefConfig) -> Result<Option<String>> {
self.get_field(refconfig, "To")
}
fn get_subject(&self, refconfig: &RefConfig) -> Result<Option<String>> {
self.get_field(refconfig, "Subject")
}
fn get_message_id(&self, refconfig: &RefConfig) -> Result<Option<String>> {
self.get_field(refconfig, "Message-ID")
.map(|o| o.map(crate::util::strip_message_delimiters))
}
fn get_in_reply_to(&self, refconfig: &RefConfig) -> Result<Option<String>> {
self.get_field(refconfig, "In-Reply-To")
}
}
#[derive(Debug)]
pub struct MailHeader<'a>(Vec<::mailparse::MailHeader<'a>>);
impl<'a> From<Vec<::mailparse::MailHeader<'a>>> for MailHeader<'a> {
fn from(mh: Vec<::mailparse::MailHeader<'a>>) -> Self {
MailHeader(mh)
}
}
impl<'a> MailHeader<'a> {
pub fn get_field(&self, field: &str) -> Result<Option<String>> {
match self.0
.iter()
.filter_map(|hdr| {
match hdr.get_key()
.context(format_err!("Cannot get field {}", field))
.map_err(Error::from)
{
Ok(key) => if key == field {
Some(Ok(hdr))
} else {
None
},
Err(e) => Some(Err(e))
}
})
.next()
{
None => Ok(None),
Some(Err(e)) => Err(e),
Some(Ok(hdr)) => Ok(Some(hdr.get_value()?))
}
}
pub fn get_from(&self) -> Result<Option<String>> {
self.get_field("From")
}
pub fn get_to(&self) -> Result<Option<String>> {
self.get_field("To")
}
pub fn get_subject(&self) -> Result<Option<String>> {
self.get_field("Subject")
}
pub fn get_message_id(&self) -> Result<Option<String>> {
self.get_field("Message-ID")
}
pub fn get_in_reply_to(&self) -> Result<Option<String>> {
self.get_field("In-Reply-To")
}
}