mirror of
https://github.com/stalwartlabs/mail-parser.git
synced 2026-12-01 08:02:03 +00:00
Integration tests.
This commit is contained in:
@@ -1,6 +1,162 @@
|
||||
# mail_parser
|
||||
|
||||
|
||||
## Highlighs
|
||||
blah
|
||||
## Usage
|
||||
|
||||
```rust
|
||||
let mut input = concat!(
|
||||
"From: Art Vandelay <art@vandelay.com> (Vandelay Industries)\n",
|
||||
"To: \"Colleagues\": \"James Smythe\" <james@vandelay.com>; Friends:\n",
|
||||
" jane@example.com, =?UTF-8?Q?John_Sm=C3=AEth?= <john@example.com>;\n",
|
||||
"Date: Sat, 20 Nov 2021 14:22:01 -0800\n",
|
||||
"Subject: Why not both importing AND exporting? =?utf-8?b?4pi6?=\n",
|
||||
"Content-Type: multipart/mixed; boundary=\"festivus\";\n\n",
|
||||
"--festivus\n",
|
||||
"Content-Type: text/html; charset=\"us-ascii\"\n",
|
||||
"Content-Transfer-Encoding: base64\n\n",
|
||||
"PGh0bWw+PHA+SSB3YXMgdGhpbmtpbmcgYWJvdXQgcXVpdHRpbmcgdGhlICZsZHF1bztle\n",
|
||||
"HBvcnRpbmcmcmRxdW87IHRvIGZvY3VzIGp1c3Qgb24gdGhlICZsZHF1bztpbXBvcnRpbm\n",
|
||||
"cmcmRxdW87LDwvcD48cD5idXQgdGhlbiBJIHRob3VnaHQsIHdoeSBub3QgZG8gYm90aD8\n",
|
||||
"gJiN4MjYzQTs8L3A+PC9odG1sPg==\n",
|
||||
"--festivus\n",
|
||||
"Content-Type: message/rfc822\n\n",
|
||||
"From: \"Cosmo Kramer\" <kramer@kramerica.com>\n",
|
||||
"Subject: Exporting my book about coffee tables\n",
|
||||
"Content-Type: multipart/mixed; boundary=\"giddyup\";\n\n",
|
||||
"--giddyup\n",
|
||||
"Content-Type: text/plain; charset=\"utf-16\"\n",
|
||||
"Content-Transfer-Encoding: quoted-printable\n\n",
|
||||
"=FF=FE=0C!5=D8\"=DD5=D8)=DD5=D8-=DD =005=D8*=DD5=D8\"=DD =005=D8\"=\n",
|
||||
"=DD5=D85=DD5=D8-=DD5=D8,=DD5=D8/=DD5=D81=DD =005=D8*=DD5=D86=DD =\n",
|
||||
"=005=D8=1F=DD5=D8,=DD5=D8,=DD5=D8(=DD =005=D8-=DD5=D8)=DD5=D8\"=\n",
|
||||
"=DD5=D8=1E=DD5=D80=DD5=D8\"=DD!=00\n",
|
||||
"--giddyup\n",
|
||||
"Content-Type: image/gif; name*1=\"about \"; name*0=\"Book \";\n",
|
||||
" name*2*=utf-8''%e2%98%95tables.gif\n",
|
||||
"Content-Transfer-Encoding: Base64\n",
|
||||
"Content-Disposition: attachment\n\n",
|
||||
"R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7\n",
|
||||
"--giddyup--\n",
|
||||
"--festivus--\n",
|
||||
)
|
||||
.as_bytes()
|
||||
.to_vec();
|
||||
|
||||
let message = Message::parse(&mut input[..]);
|
||||
|
||||
// Parses addresses (including comments), lists and groups
|
||||
assert_eq!(
|
||||
message.get_from(),
|
||||
&Address::Address(Addr {
|
||||
name: Some("Art Vandelay (Vandelay Industries)".into()),
|
||||
address: Some("art@vandelay.com".into())
|
||||
})
|
||||
);
|
||||
assert_eq!(
|
||||
message.get_to(),
|
||||
&Address::GroupList(vec![
|
||||
Group {
|
||||
name: Some("Colleagues".into()),
|
||||
addresses: vec![Addr {
|
||||
name: Some("James Smythe".into()),
|
||||
address: Some("james@vandelay.com".into())
|
||||
}]
|
||||
},
|
||||
Group {
|
||||
name: Some("Friends".into()),
|
||||
addresses: vec![
|
||||
Addr {
|
||||
name: None,
|
||||
address: Some("jane@example.com".into())
|
||||
},
|
||||
Addr {
|
||||
name: Some("John Smîth".into()),
|
||||
address: Some("john@example.com".into())
|
||||
}
|
||||
]
|
||||
}
|
||||
])
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
message.get_date().unwrap().to_iso8601(),
|
||||
"2021-11-20T14:22:01-08:00"
|
||||
);
|
||||
|
||||
// RFC2047 support for encoded text in message readers
|
||||
assert_eq!(
|
||||
message.get_subject().unwrap(),
|
||||
"Why not both importing AND exporting? ☺"
|
||||
);
|
||||
|
||||
// HTML and text body parts are returned conforming to RFC8621, Section 4.1.4
|
||||
assert_eq!(
|
||||
message.get_html_body(0).unwrap().to_string(),
|
||||
concat!(
|
||||
"<html><p>I was thinking about quitting the “exporting” to ",
|
||||
"focus just on the “importing”,</p><p>but then I thought,",
|
||||
" why not do both? ☺</p></html>"
|
||||
)
|
||||
);
|
||||
|
||||
// HTML parts are converted to plain text (and viceversa) when missing
|
||||
assert_eq!(
|
||||
message.get_text_body(0).unwrap().to_string(),
|
||||
concat!(
|
||||
"I was thinking about quitting the “exporting” to focus just on the",
|
||||
" “importing”,\nbut then I thought, why not do both? ☺\n"
|
||||
)
|
||||
);
|
||||
|
||||
// Supports multipart/digest and nested messages
|
||||
let nested_message = match message.get_attachment(0).unwrap() {
|
||||
MessagePart::Message(v) => v,
|
||||
_ => unreachable!(),
|
||||
};
|
||||
|
||||
assert_eq!(
|
||||
nested_message.get_subject().unwrap(),
|
||||
"Exporting my book about coffee tables"
|
||||
);
|
||||
|
||||
// Handles UTF-* as well as many other legacy encodings
|
||||
assert_eq!(
|
||||
nested_message.get_text_body(0).unwrap().to_string(),
|
||||
"ℌ𝔢𝔩𝔭 𝔪𝔢 𝔢𝔵𝔭𝔬𝔯𝔱 𝔪𝔶 𝔟𝔬𝔬𝔨 𝔭𝔩𝔢𝔞𝔰𝔢!"
|
||||
);
|
||||
assert_eq!(
|
||||
nested_message.get_html_body(0).unwrap().to_string(),
|
||||
"<html><body>ℌ𝔢𝔩𝔭 𝔪𝔢 𝔢𝔵𝔭𝔬𝔯𝔱 𝔪𝔶 𝔟𝔬𝔬𝔨 𝔭𝔩𝔢𝔞𝔰𝔢!</body></html>"
|
||||
);
|
||||
|
||||
let nested_attachment = match nested_message.get_attachment(0).unwrap() {
|
||||
MessagePart::Binary(v) => v,
|
||||
_ => unreachable!(),
|
||||
};
|
||||
|
||||
assert_eq!(nested_attachment.len(), 42);
|
||||
|
||||
// Full RFC2231 support for continuations and character sets
|
||||
assert_eq!(
|
||||
nested_attachment
|
||||
.get_header()
|
||||
.unwrap()
|
||||
.get_content_type()
|
||||
.unwrap()
|
||||
.get_attribute("name")
|
||||
.unwrap(),
|
||||
"Book about ☕tables.gif"
|
||||
);
|
||||
|
||||
// Integrates with Serde
|
||||
println!("{}", serde_json::to_string_pretty(&message).unwrap());
|
||||
println!("{}", serde_yaml::to_string(&message).unwrap());
|
||||
```
|
||||
|
||||
## Conformed RFCs
|
||||
-------------
|
||||
|
||||
- [RFC 822 - Standard for ARPA Internet Text Messages](https://datatracker.ietf.org/doc/html/rfc822)
|
||||
- [RFC 5322 - Internet Message Format](https://datatracker.ietf.org/doc/html/rfc5322)
|
||||
- [RFC 2045 - Multipurpose Internet Mail Extensions (MIME) Part One: Format of Internet Message Bodies](https://datatracker.ietf.org/doc/html/rfc2045)
|
||||
@@ -17,7 +173,7 @@
|
||||
- [RFC 8621 - The JSON Meta Application Protocol (JMAP) for Mail (Section 4.1.4)](https://datatracker.ietf.org/doc/html/rfc8621#section-4.1.4)
|
||||
|
||||
## Supported Character Sets
|
||||
-------------
|
||||
|
||||
- UTF-8
|
||||
- UTF-16, UTF-16BE, UTF-16LE
|
||||
- UTF-7
|
||||
|
||||
@@ -1,3 +1,14 @@
|
||||
/*
|
||||
* Copyright Stalwart Labs, Minter Ltd. See the COPYING
|
||||
* file at the top-level directory of this distribution.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
* https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
* <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
|
||||
* option. This file may not be copied, modified, or distributed
|
||||
* except according to those terms.
|
||||
*/
|
||||
|
||||
#![feature(test)]
|
||||
|
||||
extern crate test;
|
||||
|
||||
+184
-10
@@ -12,7 +12,7 @@
|
||||
mod decoders;
|
||||
mod parsers;
|
||||
|
||||
use std::{borrow::Cow, collections::HashMap};
|
||||
use std::{borrow::Cow, collections::HashMap, fmt, slice::Iter};
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
@@ -22,13 +22,13 @@ pub struct Message<'x> {
|
||||
header: Box<MessageHeader<'x>>,
|
||||
#[serde(skip_serializing_if = "Vec::is_empty")]
|
||||
#[serde(default)]
|
||||
html_body: Vec<BodyPart<'x>>,
|
||||
html_body: Vec<InlinePart<'x>>,
|
||||
#[serde(skip_serializing_if = "Vec::is_empty")]
|
||||
#[serde(default)]
|
||||
text_body: Vec<BodyPart<'x>>,
|
||||
text_body: Vec<InlinePart<'x>>,
|
||||
#[serde(skip_serializing_if = "Vec::is_empty")]
|
||||
#[serde(default)]
|
||||
attachments: Vec<AttachmentPart<'x>>,
|
||||
attachments: Vec<MessagePart<'x>>,
|
||||
}
|
||||
#[derive(Debug, Default, Serialize, Deserialize, PartialEq)]
|
||||
pub struct TextPart<'x> {
|
||||
@@ -50,13 +50,13 @@ pub struct BinaryPart<'x> {
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize, PartialEq)]
|
||||
#[serde(untagged)]
|
||||
pub enum BodyPart<'x> {
|
||||
pub enum InlinePart<'x> {
|
||||
Text(TextPart<'x>),
|
||||
InlineBinary(u32),
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize, PartialEq)]
|
||||
pub enum AttachmentPart<'x> {
|
||||
pub enum MessagePart<'x> {
|
||||
Text(TextPart<'x>),
|
||||
#[serde(borrow)]
|
||||
Binary(BinaryPart<'x>),
|
||||
@@ -201,20 +201,20 @@ pub struct MimeHeader<'x> {
|
||||
pub struct Addr<'x> {
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
#[serde(default)]
|
||||
name: Option<Cow<'x, str>>,
|
||||
pub name: Option<Cow<'x, str>>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
#[serde(default)]
|
||||
address: Option<Cow<'x, str>>,
|
||||
pub address: Option<Cow<'x, str>>,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
||||
pub struct Group<'x> {
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
#[serde(default)]
|
||||
name: Option<Cow<'x, str>>,
|
||||
pub name: Option<Cow<'x, str>>,
|
||||
#[serde(skip_serializing_if = "Vec::is_empty")]
|
||||
#[serde(default)]
|
||||
addresses: Vec<Addr<'x>>,
|
||||
pub addresses: Vec<Addr<'x>>,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
||||
@@ -383,6 +383,54 @@ impl<'x> Message<'x> {
|
||||
pub fn get_header(&self, name: &str) -> Option<&Vec<Cow<'x, str>>> {
|
||||
self.header.others.get(name)
|
||||
}
|
||||
|
||||
fn get_body_part(&self, list: &'x [InlinePart<'x>], pos: usize) -> Option<&'x dyn BodyPart> {
|
||||
match list.get(pos) {
|
||||
Some(InlinePart::Text(v)) => Some(v),
|
||||
Some(InlinePart::InlineBinary(v)) => match self.attachments.get(*v as usize)? {
|
||||
MessagePart::Text(v) => Some(v),
|
||||
MessagePart::Binary(v) | MessagePart::InlineBinary(v) => Some(v),
|
||||
_ => None,
|
||||
},
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_html_body(&self, pos: usize) -> Option<&dyn BodyPart> {
|
||||
self.get_body_part(&self.html_body, pos)
|
||||
}
|
||||
|
||||
pub fn get_text_body(&self, pos: usize) -> Option<&dyn BodyPart> {
|
||||
self.get_body_part(&self.text_body, pos)
|
||||
}
|
||||
|
||||
pub fn get_attachment(&self, pos: usize) -> Option<&MessagePart<'x>> {
|
||||
self.attachments.get(pos)
|
||||
}
|
||||
|
||||
pub fn get_text_body_count(&self) -> usize {
|
||||
self.text_body.len()
|
||||
}
|
||||
|
||||
pub fn get_html_body_count(&self) -> usize {
|
||||
self.html_body.len()
|
||||
}
|
||||
|
||||
pub fn get_attachment_count(&self) -> usize {
|
||||
self.attachments.len()
|
||||
}
|
||||
|
||||
pub fn get_text_bodies(&'x self) -> BodyPartIterator<'x> {
|
||||
BodyPartIterator::new(self, &self.text_body)
|
||||
}
|
||||
|
||||
pub fn get_html_bodies(&'x self) -> BodyPartIterator<'x> {
|
||||
BodyPartIterator::new(self, &self.html_body)
|
||||
}
|
||||
|
||||
pub fn get_attachments(&self) -> Iter<MessagePart<'_>> {
|
||||
self.attachments.iter()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'x> MimeFieldGet<'x> for Message<'x> {
|
||||
@@ -450,3 +498,129 @@ impl<'x> MimeFieldGet<'x> for MessageHeader<'x> {
|
||||
self.content_type.as_ref()
|
||||
}
|
||||
}
|
||||
|
||||
pub trait BodyPart<'x>: fmt::Display {
|
||||
fn get_header(&self) -> Option<&MimeHeader<'x>>;
|
||||
fn get_contents(&'x self) -> &'x [u8];
|
||||
fn get_text_contents(&'x self) -> &'x str;
|
||||
fn len(&self) -> usize;
|
||||
fn is_text(&self) -> bool;
|
||||
fn is_binary(&self) -> bool;
|
||||
}
|
||||
|
||||
impl<'x> BodyPart<'x> for TextPart<'x> {
|
||||
fn get_header(&self) -> Option<&MimeHeader<'x>> {
|
||||
self.header.as_ref()
|
||||
}
|
||||
|
||||
fn get_contents(&'x self) -> &'x [u8] {
|
||||
self.contents.as_bytes()
|
||||
}
|
||||
|
||||
fn is_text(&self) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
fn is_binary(&self) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
fn get_text_contents(&'x self) -> &'x str {
|
||||
self.contents.as_ref()
|
||||
}
|
||||
|
||||
fn len(&self) -> usize {
|
||||
self.contents.len()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'x> fmt::Display for TextPart<'x> {
|
||||
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
||||
fmt.write_str(self.contents.as_ref())
|
||||
}
|
||||
}
|
||||
|
||||
impl<'x> BodyPart<'x> for BinaryPart<'x> {
|
||||
fn get_header(&self) -> Option<&MimeHeader<'x>> {
|
||||
self.header.as_ref()
|
||||
}
|
||||
|
||||
fn get_contents(&'x self) -> &'x [u8] {
|
||||
self.contents.as_ref()
|
||||
}
|
||||
|
||||
fn get_text_contents(&'x self) -> &'x str {
|
||||
std::str::from_utf8(self.contents.as_ref()).unwrap_or("")
|
||||
}
|
||||
|
||||
fn is_text(&self) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
fn is_binary(&self) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
fn len(&self) -> usize {
|
||||
self.contents.len()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'x> fmt::Display for BinaryPart<'x> {
|
||||
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
||||
fmt.write_str("[binary contents]")
|
||||
}
|
||||
}
|
||||
|
||||
pub struct BodyPartIterator<'x> {
|
||||
message: &'x Message<'x>,
|
||||
list: &'x [InlinePart<'x>],
|
||||
pos: isize,
|
||||
}
|
||||
|
||||
impl<'x> BodyPartIterator<'x> {
|
||||
fn new(message: &'x Message<'x>, list: &'x [InlinePart<'x>]) -> BodyPartIterator<'x> {
|
||||
BodyPartIterator {
|
||||
message,
|
||||
list,
|
||||
pos: -1,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'x> Iterator for BodyPartIterator<'x> {
|
||||
type Item = &'x dyn BodyPart<'x>;
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
self.pos += 1;
|
||||
self.message.get_body_part(self.list, self.pos as usize)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'x> ContentType<'x> {
|
||||
pub fn get_type(&'x self) -> &'x str {
|
||||
&self.c_type
|
||||
}
|
||||
|
||||
pub fn get_subtype(&'x self) -> Option<&'x str> {
|
||||
self.c_subtype.as_ref()?.as_ref().into()
|
||||
}
|
||||
|
||||
pub fn get_attribute(&'x self, name: &str) -> Option<&'x str> {
|
||||
self.attributes.as_ref()?.get(name)?.as_ref().into()
|
||||
}
|
||||
|
||||
pub fn has_attribute(&'x self, name: &str) -> bool {
|
||||
self.attributes
|
||||
.as_ref()
|
||||
.map_or_else(|| false, |attr| attr.contains_key(name))
|
||||
}
|
||||
|
||||
pub fn is_attachment(&'x self) -> bool {
|
||||
self.c_type.eq_ignore_ascii_case("attachment")
|
||||
}
|
||||
|
||||
pub fn is_inline(&'x self) -> bool {
|
||||
self.c_type.eq_ignore_ascii_case("inline")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,34 +24,6 @@ use crate::{
|
||||
ContentType,
|
||||
};
|
||||
|
||||
impl<'x> ContentType<'x> {
|
||||
pub fn get_type(&'x self) -> &'x str {
|
||||
&self.c_type
|
||||
}
|
||||
|
||||
pub fn get_subtype(&'x self) -> Option<&'x str> {
|
||||
self.c_subtype.as_ref()?.as_ref().into()
|
||||
}
|
||||
|
||||
pub fn get_attribute(&'x self, name: &str) -> Option<&'x str> {
|
||||
self.attributes.as_ref()?.get(name)?.as_ref().into()
|
||||
}
|
||||
|
||||
pub fn has_attribute(&'x self, name: &str) -> bool {
|
||||
self.attributes
|
||||
.as_ref()
|
||||
.map_or_else(|| false, |attr| attr.contains_key(name))
|
||||
}
|
||||
|
||||
pub fn is_attachment(&'x self) -> bool {
|
||||
self.c_type.eq_ignore_ascii_case("attachment")
|
||||
}
|
||||
|
||||
pub fn is_inline(&'x self) -> bool {
|
||||
self.c_type.eq_ignore_ascii_case("inline")
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, PartialEq, Debug)]
|
||||
enum ContentState {
|
||||
Type,
|
||||
|
||||
+18
-18
@@ -18,7 +18,7 @@ use crate::{
|
||||
html::{html_to_text, text_to_html},
|
||||
quoted_printable::QuotedPrintableDecoder,
|
||||
},
|
||||
AttachmentPart, BinaryPart, BodyPart, ContentType, Message, MessageHeader, MimeHeader,
|
||||
MessagePart, BinaryPart, InlinePart, ContentType, Message, MessageHeader, MimeHeader,
|
||||
TextPart,
|
||||
};
|
||||
|
||||
@@ -324,23 +324,23 @@ impl<'x> Message<'x> {
|
||||
let is_html = mime_type == MimeType::TextHtml;
|
||||
|
||||
if add_to_html && !is_html {
|
||||
message.html_body.push(BodyPart::Text(TextPart {
|
||||
message.html_body.push(InlinePart::Text(TextPart {
|
||||
header: None,
|
||||
contents: text_to_html(&text_part.contents).into(),
|
||||
}));
|
||||
} else if add_to_text && is_html {
|
||||
message.text_body.push(BodyPart::Text(TextPart {
|
||||
message.text_body.push(InlinePart::Text(TextPart {
|
||||
header: None,
|
||||
contents: html_to_text(&text_part.contents).into(),
|
||||
}));
|
||||
}
|
||||
|
||||
if add_to_html && is_html {
|
||||
message.html_body.push(BodyPart::Text(text_part));
|
||||
message.html_body.push(InlinePart::Text(text_part));
|
||||
} else if add_to_text && !is_html {
|
||||
message.text_body.push(BodyPart::Text(text_part));
|
||||
message.text_body.push(InlinePart::Text(text_part));
|
||||
} else {
|
||||
message.attachments.push(AttachmentPart::Text(text_part));
|
||||
message.attachments.push(MessagePart::Text(text_part));
|
||||
}
|
||||
} else {
|
||||
let binary_part = BinaryPart {
|
||||
@@ -355,18 +355,18 @@ impl<'x> Message<'x> {
|
||||
if add_to_html {
|
||||
message
|
||||
.html_body
|
||||
.push(BodyPart::InlineBinary(message.attachments.len() as u32));
|
||||
.push(InlinePart::InlineBinary(message.attachments.len() as u32));
|
||||
}
|
||||
if add_to_text {
|
||||
message
|
||||
.text_body
|
||||
.push(BodyPart::InlineBinary(message.attachments.len() as u32));
|
||||
.push(InlinePart::InlineBinary(message.attachments.len() as u32));
|
||||
}
|
||||
|
||||
message.attachments.push(if !is_inline {
|
||||
AttachmentPart::Binary(binary_part)
|
||||
MessagePart::Binary(binary_part)
|
||||
} else {
|
||||
AttachmentPart::InlineBinary(binary_part)
|
||||
MessagePart::InlineBinary(binary_part)
|
||||
});
|
||||
};
|
||||
|
||||
@@ -381,7 +381,7 @@ impl<'x> Message<'x> {
|
||||
{
|
||||
prev_message
|
||||
.attachments
|
||||
.push(AttachmentPart::Message(message));
|
||||
.push(MessagePart::Message(message));
|
||||
message = prev_message;
|
||||
prev_state.mime_boundary = state.mime_boundary;
|
||||
state = prev_state;
|
||||
@@ -404,12 +404,12 @@ impl<'x> Message<'x> {
|
||||
{
|
||||
for part in message.html_body[state.html_parts..].iter() {
|
||||
message.text_body.push(match part {
|
||||
BodyPart::Text(part) => BodyPart::Text(TextPart {
|
||||
InlinePart::Text(part) => InlinePart::Text(TextPart {
|
||||
header: None,
|
||||
contents: html_to_text(&part.contents).into(),
|
||||
}),
|
||||
BodyPart::InlineBinary(index) => {
|
||||
BodyPart::InlineBinary(*index)
|
||||
InlinePart::InlineBinary(index) => {
|
||||
InlinePart::InlineBinary(*index)
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -421,12 +421,12 @@ impl<'x> Message<'x> {
|
||||
{
|
||||
for part in message.text_body[state.text_parts..].iter() {
|
||||
message.html_body.push(match part {
|
||||
BodyPart::Text(part) => BodyPart::Text(TextPart {
|
||||
InlinePart::Text(part) => InlinePart::Text(TextPart {
|
||||
header: None,
|
||||
contents: text_to_html(&part.contents).into(),
|
||||
}),
|
||||
BodyPart::InlineBinary(index) => {
|
||||
BodyPart::InlineBinary(*index)
|
||||
InlinePart::InlineBinary(index) => {
|
||||
InlinePart::InlineBinary(*index)
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -459,7 +459,7 @@ impl<'x> Message<'x> {
|
||||
while let Some(mut prev_message) = message_stack.pop() {
|
||||
prev_message
|
||||
.attachments
|
||||
.push(AttachmentPart::Message(message));
|
||||
.push(MessagePart::Message(message));
|
||||
message = prev_message;
|
||||
}
|
||||
|
||||
|
||||
+142
-6
@@ -1,11 +1,147 @@
|
||||
use mail_parser::{Message, MimeFieldGet};
|
||||
/*
|
||||
* Copyright Stalwart Labs, Minter Ltd. See the COPYING
|
||||
* file at the top-level directory of this distribution.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
* https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
* <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
|
||||
* option. This file may not be copied, modified, or distributed
|
||||
* except according to those terms.
|
||||
*/
|
||||
|
||||
use mail_parser::*;
|
||||
|
||||
#[test]
|
||||
fn test_mail_parser() {
|
||||
let mut input = "test".to_string();
|
||||
let message = Message::parse(unsafe{input.as_bytes_mut()});
|
||||
fn test_api() {
|
||||
let mut input = concat!(
|
||||
"From: Art Vandelay <art@vandelay.com> (Vandelay Industries)\n",
|
||||
"To: \"Colleagues\": \"James Smythe\" <james@vandelay.com>; Friends:\n",
|
||||
" jane@example.com, =?UTF-8?Q?John_Sm=C3=AEth?= <john@example.com>;\n",
|
||||
"Date: Sat, 20 Nov 2021 14:22:01 -0800\n",
|
||||
"Subject: Why not both importing AND exporting? =?utf-8?b?4pi6?=\n",
|
||||
"Content-Type: multipart/mixed; boundary=\"festivus\";\n\n",
|
||||
"--festivus\n",
|
||||
"Content-Type: text/html; charset=\"us-ascii\"\n",
|
||||
"Content-Transfer-Encoding: base64\n\n",
|
||||
"PGh0bWw+PHA+SSB3YXMgdGhpbmtpbmcgYWJvdXQgcXVpdHRpbmcgdGhlICZsZHF1bztle\n",
|
||||
"HBvcnRpbmcmcmRxdW87IHRvIGZvY3VzIGp1c3Qgb24gdGhlICZsZHF1bztpbXBvcnRpbm\n",
|
||||
"cmcmRxdW87LDwvcD48cD5idXQgdGhlbiBJIHRob3VnaHQsIHdoeSBub3QgZG8gYm90aD8\n",
|
||||
"gJiN4MjYzQTs8L3A+PC9odG1sPg==\n",
|
||||
"--festivus\n",
|
||||
"Content-Type: message/rfc822\n\n",
|
||||
"From: \"Cosmo Kramer\" <kramer@kramerica.com>\n",
|
||||
"Subject: Exporting my book about coffee tables\n",
|
||||
"Content-Type: multipart/mixed; boundary=\"giddyup\";\n\n",
|
||||
"--giddyup\n",
|
||||
"Content-Type: text/plain; charset=\"utf-16\"\n",
|
||||
"Content-Transfer-Encoding: quoted-printable\n\n",
|
||||
"=FF=FE=0C!5=D8\"=DD5=D8)=DD5=D8-=DD =005=D8*=DD5=D8\"=DD =005=D8\"=\n",
|
||||
"=DD5=D85=DD5=D8-=DD5=D8,=DD5=D8/=DD5=D81=DD =005=D8*=DD5=D86=DD =\n",
|
||||
"=005=D8=1F=DD5=D8,=DD5=D8,=DD5=D8(=DD =005=D8-=DD5=D8)=DD5=D8\"=\n",
|
||||
"=DD5=D8=1E=DD5=D80=DD5=D8\"=DD!=00\n",
|
||||
"--giddyup\n",
|
||||
"Content-Type: image/gif; name*1=\"about \"; name*0=\"Book \";\n",
|
||||
" name*2*=utf-8''%e2%98%95tables.gif\n",
|
||||
"Content-Transfer-Encoding: Base64\n",
|
||||
"Content-Disposition: attachment\n\n",
|
||||
"R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7\n",
|
||||
"--giddyup--\n",
|
||||
"--festivus--\n",
|
||||
)
|
||||
.as_bytes()
|
||||
.to_vec();
|
||||
|
||||
message.get_content_id();
|
||||
message.get_subject();
|
||||
let message = Message::parse(&mut input[..]);
|
||||
|
||||
assert_eq!(
|
||||
message.get_from(),
|
||||
&Address::Address(Addr {
|
||||
name: Some("Art Vandelay (Vandelay Industries)".into()),
|
||||
address: Some("art@vandelay.com".into())
|
||||
})
|
||||
);
|
||||
assert_eq!(
|
||||
message.get_to(),
|
||||
&Address::GroupList(vec![
|
||||
Group {
|
||||
name: Some("Colleagues".into()),
|
||||
addresses: vec![Addr {
|
||||
name: Some("James Smythe".into()),
|
||||
address: Some("james@vandelay.com".into())
|
||||
}]
|
||||
},
|
||||
Group {
|
||||
name: Some("Friends".into()),
|
||||
addresses: vec![
|
||||
Addr {
|
||||
name: None,
|
||||
address: Some("jane@example.com".into())
|
||||
},
|
||||
Addr {
|
||||
name: Some("John Smîth".into()),
|
||||
address: Some("john@example.com".into())
|
||||
}
|
||||
]
|
||||
}
|
||||
])
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
message.get_date().unwrap().to_iso8601(),
|
||||
"2021-11-20T14:22:01-08:00"
|
||||
);
|
||||
assert_eq!(
|
||||
message.get_subject().unwrap(),
|
||||
"Why not both importing AND exporting? ☺"
|
||||
);
|
||||
assert_eq!(
|
||||
message.get_html_body(0).unwrap().to_string(),
|
||||
concat!(
|
||||
"<html><p>I was thinking about quitting the “exporting” to ",
|
||||
"focus just on the “importing”,</p><p>but then I thought,",
|
||||
" why not do both? ☺</p></html>"
|
||||
)
|
||||
);
|
||||
assert_eq!(
|
||||
message.get_text_body(0).unwrap().to_string(),
|
||||
concat!(
|
||||
"I was thinking about quitting the “exporting” to focus just on the",
|
||||
" “importing”,\nbut then I thought, why not do both? ☺\n"
|
||||
)
|
||||
);
|
||||
let nested_message = match message.get_attachment(0).unwrap() {
|
||||
MessagePart::Message(v) => v,
|
||||
_ => unreachable!(),
|
||||
};
|
||||
assert_eq!(
|
||||
nested_message.get_subject().unwrap(),
|
||||
"Exporting my book about coffee tables"
|
||||
);
|
||||
assert_eq!(
|
||||
nested_message.get_text_body(0).unwrap().to_string(),
|
||||
"ℌ𝔢𝔩𝔭 𝔪𝔢 𝔢𝔵𝔭𝔬𝔯𝔱 𝔪𝔶 𝔟𝔬𝔬𝔨 𝔭𝔩𝔢𝔞𝔰𝔢!"
|
||||
);
|
||||
assert_eq!(
|
||||
nested_message.get_html_body(0).unwrap().to_string(),
|
||||
"<html><body>ℌ𝔢𝔩𝔭 𝔪𝔢 𝔢𝔵𝔭𝔬𝔯𝔱 𝔪𝔶 𝔟𝔬𝔬𝔨 𝔭𝔩𝔢𝔞𝔰𝔢!</body></html>"
|
||||
);
|
||||
let nested_attachment = match nested_message.get_attachment(0).unwrap() {
|
||||
MessagePart::Binary(v) => v,
|
||||
_ => unreachable!(),
|
||||
};
|
||||
assert_eq!(nested_attachment.len(), 42);
|
||||
assert_eq!(
|
||||
nested_attachment
|
||||
.get_header()
|
||||
.unwrap()
|
||||
.get_content_type()
|
||||
.unwrap()
|
||||
.get_attribute("name")
|
||||
.unwrap(),
|
||||
"Book about ☕tables.gif"
|
||||
);
|
||||
|
||||
//println!("{}", serde_json::to_string_pretty(&message).unwrap());
|
||||
//println!("{}", serde_yaml::to_string(&message).unwrap());
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user