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
use crate::address::*;
use crate::prelude::*;
use std::borrow::Cow;

/// A struct representing a valid RFC 5322 message.
///
/// # Example
///
/// ```
/// # use email_parser::prelude::*;
/// let email = Email::parse(
///     b"\
///     From: Mubelotix <mubelotix@mubelotix.dev>\r\n\
///     Subject:Example Email\r\n\
///     To: Someone <example@example.com>\r\n\
///     Message-id: <6546518945@mubelotix.dev>\r\n\
///     Date: 5 May 2003 18:58:34 +0000\r\n\
///     \r\n\
///     Hey!\r\n",
/// )
/// .unwrap();
///
/// assert_eq!(email.subject.unwrap(), "Example Email");
/// assert_eq!(email.sender.name.unwrap(), vec!["Mubelotix"]);
/// assert_eq!(email.sender.address.local_part, "mubelotix");
/// assert_eq!(email.sender.address.domain, "mubelotix.dev");
/// ```
#[derive(Debug)]
pub struct Email<'a> {
    /// The ASCII text of the body.
    #[cfg(not(feature = "mime"))]
    pub body: Option<Cow<'a, str>>,

    #[cfg(feature = "from")]
    /// The list of authors of the message.\
    /// It's **not** the identity of the sender. See the [sender field](#structfield.sender).
    pub from: Vec<Mailbox<'a>>,

    #[cfg(feature = "sender")]
    /// The mailbox of the agent responsible for the actual transmission of the message.\
    /// Do not mix up with the [from field](#structfield.from) that contains the list of authors.\
    /// When there is only one author, this field can be omitted, and its value is inferred. Otherwise, an explicit value is required.
    pub sender: Mailbox<'a>,

    #[cfg(feature = "subject")]
    /// A short optional string identifying the topic of the message.
    pub subject: Option<Cow<'a, str>>,

    #[cfg(feature = "date")]
    /// The date and time at which the [sender](#structfield.sender) of the message indicated that the message was complete and ready to enter the mail delivery system.
    /// For instance, this might be the time that a user pushes the "send" or "submit" button in an application program.
    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>>,
    )>,

    #[cfg(feature = "mime")]
    pub mime_entity: RawEntity<'a>,

    /// The list of unrecognized fields.\
    /// Each field is stored as a `(name, value)` tuple.
    pub unknown_fields: Vec<(&'a str, Cow<'a, str>)>,
}

impl<'a> Email<'a> {
    /// Parse an email.
    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();
        #[cfg(feature = "mime")]
        let mut mime_version = None;
        #[cfg(feature = "mime")]
        let mut content_type = None;
        #[cfg(feature = "mime")]
        let mut content_transfer_encoding = None;
        #[cfg(feature = "mime")]
        let mut content_id = None;
        #[cfg(feature = "mime")]
        let mut content_description = None;
        #[cfg(feature = "content-disposition")]
        let mut content_disposition = None;

        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::DuplicateHeader("From"));
                    }
                }
                #[cfg(feature = "sender")]
                Field::Sender(mailbox) => {
                    if sender.is_none() {
                        sender = Some(mailbox)
                    } else {
                        return Err(Error::DuplicateHeader("Sender"));
                    }
                }
                #[cfg(feature = "subject")]
                Field::Subject(data) => {
                    if subject.is_none() {
                        subject = Some(data)
                    } else {
                        return Err(Error::DuplicateHeader("Subject"));
                    }
                }
                #[cfg(feature = "date")]
                Field::Date(data) => {
                    if date.is_none() {
                        date = Some(data)
                    } else {
                        return Err(Error::DuplicateHeader("Date"));
                    }
                }
                #[cfg(feature = "to")]
                Field::To(addresses) => {
                    if to.is_none() {
                        to = Some(addresses)
                    } else {
                        return Err(Error::DuplicateHeader("To"));
                    }
                }
                #[cfg(feature = "cc")]
                Field::Cc(addresses) => {
                    if cc.is_none() {
                        cc = Some(addresses)
                    } else {
                        return Err(Error::DuplicateHeader("Cc"));
                    }
                }
                #[cfg(feature = "bcc")]
                Field::Bcc(addresses) => {
                    if bcc.is_none() {
                        bcc = Some(addresses)
                    } else {
                        return Err(Error::DuplicateHeader("Bcc"));
                    }
                }
                #[cfg(feature = "message-id")]
                Field::MessageId(id) => {
                    if message_id.is_none() {
                        message_id = Some(id)
                    } else {
                        return Err(Error::DuplicateHeader("Message-ID"));
                    }
                }
                #[cfg(feature = "in-reply-to")]
                Field::InReplyTo(ids) => {
                    if in_reply_to.is_none() {
                        in_reply_to = Some(ids)
                    } else {
                        return Err(Error::DuplicateHeader("In-Reply-To"));
                    }
                }
                #[cfg(feature = "references")]
                Field::References(ids) => {
                    if references.is_none() {
                        references = Some(ids)
                    } else {
                        return Err(Error::DuplicateHeader("References"));
                    }
                }
                #[cfg(feature = "reply-to")]
                Field::ReplyTo(mailboxes) => {
                    if reply_to.is_none() {
                        reply_to = Some(mailboxes)
                    } else {
                        return Err(Error::DuplicateHeader("Reply-To"));
                    }
                }
                #[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));
                }
                #[cfg(feature = "mime")]
                Field::MimeVersion(major, minor) => {
                    if mime_version.is_none() {
                        mime_version = Some((major, minor))
                    } else {
                        return Err(Error::DuplicateHeader("Mime-Version"));
                    }
                }
                #[cfg(feature = "mime")]
                Field::ContentType {
                    mime_type,
                    subtype,
                    parameters,
                } => {
                    if content_type.is_none() {
                        content_type = Some((mime_type, subtype, parameters))
                    } else {
                        return Err(Error::DuplicateHeader("Content-Type"));
                    }
                }
                #[cfg(feature = "mime")]
                Field::ContentTransferEncoding(encoding) => {
                    if content_transfer_encoding.is_none() {
                        content_transfer_encoding = Some(encoding)
                    } else {
                        return Err(Error::DuplicateHeader("Content-Transfer-Encoding"));
                    }
                }
                #[cfg(feature = "mime")]
                Field::ContentId(id) => {
                    if content_id.is_none() {
                        content_id = Some(id)
                    } else {
                        return Err(Error::DuplicateHeader("Content-Id"));
                    }
                }
                #[cfg(feature = "mime")]
                Field::ContentDescription(description) => {
                    if content_description.is_none() {
                        content_description = Some(description)
                    } else {
                        return Err(Error::DuplicateHeader("Content-Description"));
                    }
                }
                #[cfg(feature = "content-disposition")]
                Field::ContentDisposition(disposition) => {
                    if content_disposition.is_none() {
                        content_disposition = Some(disposition)
                    } else {
                        return Err(Error::DuplicateHeader("Content-Disposition"));
                    }
                }
                Field::Unknown { name, value } => {
                    unknown_fields.push((name, value));
                }
            }
        }

        #[cfg(feature = "from")]
        let from = from.ok_or(Error::MissingHeader("From"))?;
        #[cfg(feature = "date")]
        let date = date.ok_or(Error::MissingHeader("Date"))?;

        #[cfg(feature = "sender")]
        let sender = match sender {
            Some(sender) => sender,
            None => {
                if from.len() == 1 {
                    from[0].clone()
                } else {
                    return Err(Error::MissingHeader("Sender"));
                }
            }
        };

        #[cfg(feature = "mime")]
        let (content_type, body) = (
            content_type.unwrap_or((
                ContentType::Text,
                Cow::Borrowed("plain"),
                vec![(Cow::Borrowed("charset"), Cow::Borrowed("us-ascii"))]
                    .into_iter()
                    .collect(),
            )),
            if let Some(body) = body {
                Some(crate::parsing::mime::entity::decode_value(
                    Cow::Borrowed(body),
                    content_transfer_encoding.unwrap_or(ContentTransferEncoding::SevenBit),
                )?)
            } else {
                None
            },
        );

        Ok(Email {
            #[cfg(not(feature = "mime"))]
            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,
            #[cfg(feature = "mime")]
            mime_entity: RawEntity {
                mime_type: content_type.0,
                subtype: content_type.1,
                description: content_description,
                id: content_id,
                parameters: content_type.2,
                #[cfg(feature = "content-disposition")]
                disposition: content_disposition,
                value: body.unwrap_or(Cow::Borrowed(b"")),
                additional_headers: Vec::new(),
            },
            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_full_email() {
        /*let multipart = Email::parse(include_bytes!("../mail.txt")).unwrap().mime_entity.parse().unwrap();
        println!("{:?}", multipart);
        if let Entity::Multipart{content, subtype: _} = multipart {
            for entity in content {
                println!("{:?}", entity.parse().unwrap())
            }
        } else {
            panic!("Failed to parse multipart");
        }*/
    }

    #[test]
    fn test_field_number() {
        assert!(Email::parse(
            // missing date
            b"\
            From: Mubelotix <mubelotix@mubelotix.dev>\r\n\
            \r\n\
            Hey!\r\n",
        )
        .is_err());

        assert!(Email::parse(
            // 2 date fields
            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(
            // missing from
            b"\
            Date: 5 May 2003 18:58:34 +0000\r\n\
            \r\n\
            Hey!\r\n",
        )
        .is_err());

        assert!(Email::parse(
            // 2 from fields
            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());
    }
}