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
use crate::address::*;
use crate::prelude::*;
use std::borrow::Cow;
#[derive(Debug)]
pub struct Email<'a> {
pub body: Option<Cow<'a, str>>,
#[cfg(feature = "from")]
pub from: Vec<Mailbox<'a>>,
#[cfg(feature = "sender")]
pub sender: Mailbox<'a>,
#[cfg(feature = "subject")]
pub subject: Option<Cow<'a, str>>,
#[cfg(feature = "date")]
pub date: DateTime,
#[cfg(feature = "to")]
pub to: Option<Vec<Address<'a>>>,
#[cfg(feature = "cc")]
pub cc: Option<Vec<Address<'a>>>,
#[cfg(feature = "bcc")]
pub bcc: Option<Vec<Address<'a>>>,
#[cfg(feature = "message-id")]
pub message_id: Option<(Cow<'a, str>, Cow<'a, str>)>,
#[cfg(feature = "in-reply-to")]
pub in_reply_to: Option<Vec<(Cow<'a, str>, Cow<'a, str>)>>,
#[cfg(feature = "references")]
pub references: Option<Vec<(Cow<'a, str>, Cow<'a, str>)>>,
#[cfg(feature = "reply-to")]
pub reply_to: Option<Vec<Address<'a>>>,
#[cfg(feature = "comments")]
pub comments: Vec<Cow<'a, str>>,
#[cfg(feature = "keywords")]
pub keywords: Vec<Vec<Cow<'a, str>>>,
#[cfg(feature = "trace")]
pub trace: Vec<(
Option<Option<EmailAddress<'a>>>,
Vec<(Vec<crate::parsing::fields::ReceivedToken<'a>>, DateTime)>,
Vec<crate::parsing::fields::TraceField<'a>>,
)>,
pub unknown_fields: Vec<(Cow<'a, str>, Cow<'a, str>)>,
}
impl<'a> Email<'a> {
pub fn parse(data: &'a [u8]) -> Result<Email<'a>, Error> {
let (fields, body) = crate::parse_message(data)?;
#[cfg(feature = "from")]
let mut from = None;
#[cfg(feature = "sender")]
let mut sender = None;
#[cfg(feature = "subject")]
let mut subject = None;
#[cfg(feature = "date")]
let mut date = None;
#[cfg(feature = "to")]
let mut to = None;
#[cfg(feature = "cc")]
let mut cc = None;
#[cfg(feature = "bcc")]
let mut bcc = None;
#[cfg(feature = "message-id")]
let mut message_id = None;
#[cfg(feature = "in-reply-to")]
let mut in_reply_to = None;
#[cfg(feature = "references")]
let mut references = None;
#[cfg(feature = "reply-to")]
let mut reply_to = None;
#[cfg(feature = "comments")]
let mut comments = Vec::new();
#[cfg(feature = "keywords")]
let mut keywords = Vec::new();
#[cfg(feature = "trace")]
let mut trace = Vec::new();
let mut unknown_fields = Vec::new();
for field in fields {
match field {
#[cfg(feature = "from")]
Field::From(mailboxes) => {
if from.is_none() {
from = Some(mailboxes)
} else {
return Err(Error::Known("Multiple from fields"));
}
}
#[cfg(feature = "sender")]
Field::Sender(mailbox) => {
if sender.is_none() {
sender = Some(mailbox)
} else {
return Err(Error::Known("Multiple sender fields"));
}
}
#[cfg(feature = "subject")]
Field::Subject(data) => {
if subject.is_none() {
subject = Some(data)
} else {
return Err(Error::Known("Multiple subject fields"));
}
}
#[cfg(feature = "date")]
Field::Date(data) => {
if date.is_none() {
date = Some(data)
} else {
return Err(Error::Known("Multiple date fields"));
}
}
#[cfg(feature = "to")]
Field::To(addresses) => {
if to.is_none() {
to = Some(addresses)
} else {
return Err(Error::Known("Multiple to fields"));
}
}
#[cfg(feature = "cc")]
Field::Cc(addresses) => {
if cc.is_none() {
cc = Some(addresses)
} else {
return Err(Error::Known("Multiple cc fields"));
}
}
#[cfg(feature = "bcc")]
Field::Bcc(addresses) => {
if bcc.is_none() {
bcc = Some(addresses)
} else {
return Err(Error::Known("Multiple bcc fields"));
}
}
#[cfg(feature = "message-id")]
Field::MessageId(id) => {
if message_id.is_none() {
message_id = Some(id)
} else {
return Err(Error::Known("Multiple message-id fields"));
}
}
#[cfg(feature = "in-reply-to")]
Field::InReplyTo(ids) => {
if in_reply_to.is_none() {
in_reply_to = Some(ids)
} else {
return Err(Error::Known("Multiple in-reply-to fields"));
}
}
#[cfg(feature = "references")]
Field::References(ids) => {
if references.is_none() {
references = Some(ids)
} else {
return Err(Error::Known("Multiple references fields"));
}
}
#[cfg(feature = "reply-to")]
Field::ReplyTo(mailboxes) => {
if reply_to.is_none() {
reply_to = Some(mailboxes)
} else {
return Err(Error::Known("Multiple reply-to fields"));
}
}
#[cfg(feature = "comments")]
Field::Comments(data) => comments.push(data),
#[cfg(feature = "keywords")]
Field::Keywords(mut data) => {
keywords.append(&mut data);
}
#[cfg(feature = "trace")]
Field::Trace {
return_path,
received,
fields,
} => {
trace.push((return_path, received, fields));
}
Field::Unknown { name, value } => {
unknown_fields.push((name, value));
}
}
}
#[cfg(feature = "from")]
let from = from.ok_or(Error::Known("Expected at least one from field"))?;
#[cfg(feature = "date")]
let date = date.ok_or(Error::Known("Expected at least one date field"))?;
#[cfg(feature = "sender")]
let sender = match sender {
Some(sender) => sender,
None => {
if from.len() == 1 {
from[0].clone()
} else {
return Err(Error::Known("Sender field required but missing"));
}
}
};
Ok(Email {
body,
#[cfg(feature = "from")]
from,
#[cfg(feature = "sender")]
sender,
#[cfg(feature = "subject")]
subject,
#[cfg(feature = "date")]
date,
#[cfg(feature = "to")]
to,
#[cfg(feature = "cc")]
cc,
#[cfg(feature = "bcc")]
bcc,
#[cfg(feature = "message-id")]
message_id,
#[cfg(feature = "in-reply-to")]
in_reply_to,
#[cfg(feature = "references")]
references,
#[cfg(feature = "reply-to")]
reply_to,
#[cfg(feature = "trace")]
trace,
#[cfg(feature = "comments")]
comments,
#[cfg(feature = "keywords")]
keywords,
unknown_fields,
})
}
}
impl<'a> std::convert::TryFrom<&'a [u8]> for Email<'a> {
type Error = crate::error::Error;
fn try_from(value: &'a [u8]) -> Result<Self, Self::Error> {
Self::parse(value)
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_field_number() {
assert!(Email::parse(
b"\
From: Mubelotix <mubelotix@mubelotix.dev>\r\n\
\r\n\
Hey!\r\n",
)
.is_err());
assert!(Email::parse(
b"\
From: Mubelotix <mubelotix@mubelotix.dev>\r\n\
Date: 5 May 2003 18:58:34 +0000\r\n\
Date: 6 May 2003 18:58:34 +0000\r\n\
\r\n\
Hey!\r\n",
)
.is_err());
assert!(Email::parse(
b"\
Date: 5 May 2003 18:58:34 +0000\r\n\
\r\n\
Hey!\r\n",
)
.is_err());
assert!(Email::parse(
b"\
From: Mubelotix <mubelotix@mubelotix.dev>\r\n\
From: Someone <jack@gmail.com>\r\n\
Date: 5 May 2003 18:58:34 +0000\r\n\
\r\n\
Hey!\r\n",
)
.is_err());
}
}