mirror of
https://github.com/stalwartlabs/mail-parser.git
synced 2026-11-29 00:01:49 +00:00
Moved definitions to lib.rs
This commit is contained in:
@@ -861,6 +861,7 @@ static CH_MAP: &[&[u8]] = &[
|
||||
b"csgb18030",
|
||||
];
|
||||
|
||||
#[allow(clippy::type_complexity)]
|
||||
static FNC_MAP: &[for<'x> fn(&'x [u8]) -> Cow<'x, str>] = &[
|
||||
decoder_iso_8859_14,
|
||||
decoder_default,
|
||||
|
||||
@@ -115,6 +115,7 @@ pub fn decoder_utf7(bytes: &[u8]) -> Cow<str> {
|
||||
result.into()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::decoders::charsets::utf7::decoder_utf7;
|
||||
|
||||
|
||||
@@ -240,8 +240,8 @@ pub fn text_to_html(input: &str) -> String {
|
||||
unsafe { String::from_utf8_unchecked(result) }
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::fs;
|
||||
|
||||
use crate::decoders::html::{add_html_token, html_to_text, text_to_html};
|
||||
|
||||
|
||||
+235
-23
@@ -12,29 +12,241 @@
|
||||
mod decoders;
|
||||
mod parsers;
|
||||
|
||||
fn main() {
|
||||
let mail = concat!(
|
||||
"Subject: This is a test email\n",
|
||||
"Content-Type: multipart/alternative; boundary=foobar\n",
|
||||
"Date: Sun, 02 Oct 2016 07:06:22 -0700 (PDT)\n",
|
||||
"\n",
|
||||
"--foobar\n",
|
||||
"Content-Type: text/plain; charset=utf-8\n",
|
||||
"Content-Transfer-Encoding: quoted-printable\n",
|
||||
"\n",
|
||||
"This is the plaintext version, in utf-8. Proof by Euro: =E2=82=AC\n",
|
||||
"--foobar\n",
|
||||
"Content-Type: text/html\n",
|
||||
"Content-Transfer-Encoding: base64\n",
|
||||
"\n",
|
||||
"PGh0bWw+PGJvZHk+VGhpcyBpcyB0aGUgPGI+SFRNTDwvYj4gdmVyc2lvbiwgaW4g \n",
|
||||
"dXMtYXNjaWkuIFByb29mIGJ5IEV1cm86ICZldXJvOzwvYm9keT48L2h0bWw+Cg== \n",
|
||||
"--foobar--\n",
|
||||
"After the final boundary stuff gets ignored.\n"
|
||||
)
|
||||
.to_string();
|
||||
use std::{borrow::Cow, collections::HashMap};
|
||||
|
||||
//println!("{}", vec![0u8; 100].as_mut_slice().len());
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
//let mut parser = MessageStream::new(mail.as_bytes());
|
||||
#[derive(Debug, Default, Serialize, Deserialize, PartialEq)]
|
||||
pub struct Message<'x> {
|
||||
#[serde(borrow)]
|
||||
header: Box<MessageHeader<'x>>,
|
||||
#[serde(skip_serializing_if = "Vec::is_empty")]
|
||||
#[serde(default)]
|
||||
html_body: Vec<BodyPart<'x>>,
|
||||
#[serde(skip_serializing_if = "Vec::is_empty")]
|
||||
#[serde(default)]
|
||||
text_body: Vec<BodyPart<'x>>,
|
||||
#[serde(skip_serializing_if = "Vec::is_empty")]
|
||||
#[serde(default)]
|
||||
attachments: Vec<AttachmentPart<'x>>,
|
||||
}
|
||||
#[derive(Debug, Default, Serialize, Deserialize, PartialEq)]
|
||||
pub struct TextPart<'x> {
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
#[serde(default)]
|
||||
header: Option<MimeHeader<'x>>,
|
||||
contents: Cow<'x, str>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Default, Serialize, Deserialize, PartialEq)]
|
||||
pub struct BinaryPart<'x> {
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
#[serde(default)]
|
||||
header: Option<MimeHeader<'x>>,
|
||||
#[serde(with = "serde_bytes")]
|
||||
#[serde(borrow)]
|
||||
contents: Cow<'x, [u8]>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize, PartialEq)]
|
||||
#[serde(untagged)]
|
||||
pub enum BodyPart<'x> {
|
||||
Text(TextPart<'x>),
|
||||
InlineBinary(u32),
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize, PartialEq)]
|
||||
pub enum AttachmentPart<'x> {
|
||||
Text(TextPart<'x>),
|
||||
#[serde(borrow)]
|
||||
Binary(BinaryPart<'x>),
|
||||
#[serde(borrow)]
|
||||
InlineBinary(BinaryPart<'x>),
|
||||
Message(Message<'x>),
|
||||
}
|
||||
|
||||
#[derive(PartialEq, Debug, Default, Serialize, Deserialize)]
|
||||
pub struct MessageHeader<'x> {
|
||||
#[serde(skip_serializing_if = "Address::is_empty")]
|
||||
#[serde(default)]
|
||||
pub bcc: Address<'x>,
|
||||
#[serde(skip_serializing_if = "Address::is_empty")]
|
||||
#[serde(default)]
|
||||
pub cc: Address<'x>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
#[serde(default)]
|
||||
pub comments: Option<Vec<Cow<'x, str>>>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
#[serde(default)]
|
||||
pub date: Option<DateTime>,
|
||||
#[serde(skip_serializing_if = "Address::is_empty")]
|
||||
#[serde(default)]
|
||||
pub from: Address<'x>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
#[serde(default)]
|
||||
pub in_reply_to: Option<Vec<Cow<'x, str>>>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
#[serde(default)]
|
||||
pub keywords: Option<Vec<Cow<'x, str>>>,
|
||||
#[serde(skip_serializing_if = "Address::is_empty")]
|
||||
#[serde(default)]
|
||||
pub list_archive: Address<'x>,
|
||||
#[serde(skip_serializing_if = "Address::is_empty")]
|
||||
#[serde(default)]
|
||||
pub list_help: Address<'x>,
|
||||
#[serde(skip_serializing_if = "Address::is_empty")]
|
||||
#[serde(default)]
|
||||
pub list_id: Address<'x>,
|
||||
#[serde(skip_serializing_if = "Address::is_empty")]
|
||||
#[serde(default)]
|
||||
pub list_owner: Address<'x>,
|
||||
#[serde(skip_serializing_if = "Address::is_empty")]
|
||||
#[serde(default)]
|
||||
pub list_post: Address<'x>,
|
||||
#[serde(skip_serializing_if = "Address::is_empty")]
|
||||
#[serde(default)]
|
||||
pub list_subscribe: Address<'x>,
|
||||
#[serde(skip_serializing_if = "Address::is_empty")]
|
||||
#[serde(default)]
|
||||
pub list_unsubscribe: Address<'x>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
#[serde(default)]
|
||||
pub message_id: Option<Cow<'x, str>>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
#[serde(default)]
|
||||
pub mime_version: Option<Cow<'x, str>>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
#[serde(default)]
|
||||
pub received: Option<Vec<Cow<'x, str>>>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
#[serde(default)]
|
||||
pub references: Option<Vec<Cow<'x, str>>>,
|
||||
#[serde(skip_serializing_if = "Address::is_empty")]
|
||||
#[serde(default)]
|
||||
pub reply_to: Address<'x>,
|
||||
#[serde(skip_serializing_if = "Address::is_empty")]
|
||||
#[serde(default)]
|
||||
pub resent_bcc: Address<'x>,
|
||||
#[serde(skip_serializing_if = "Address::is_empty")]
|
||||
#[serde(default)]
|
||||
pub resent_cc: Address<'x>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
#[serde(default)]
|
||||
pub resent_date: Option<Vec<DateTime>>,
|
||||
#[serde(skip_serializing_if = "Address::is_empty")]
|
||||
#[serde(default)]
|
||||
pub resent_from: Address<'x>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
#[serde(default)]
|
||||
pub resent_message_id: Option<Vec<Cow<'x, str>>>,
|
||||
#[serde(skip_serializing_if = "Address::is_empty")]
|
||||
#[serde(default)]
|
||||
pub resent_sender: Address<'x>,
|
||||
#[serde(skip_serializing_if = "Address::is_empty")]
|
||||
#[serde(default)]
|
||||
pub resent_to: Address<'x>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
#[serde(default)]
|
||||
pub return_path: Option<Vec<Cow<'x, str>>>,
|
||||
#[serde(skip_serializing_if = "Address::is_empty")]
|
||||
#[serde(default)]
|
||||
pub sender: Address<'x>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
#[serde(default)]
|
||||
pub subject: Option<Cow<'x, str>>,
|
||||
#[serde(skip_serializing_if = "Address::is_empty")]
|
||||
#[serde(default)]
|
||||
pub to: Address<'x>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
#[serde(default)]
|
||||
pub content_description: Option<Cow<'x, str>>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
#[serde(default)]
|
||||
pub content_disposition: Option<ContentType<'x>>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
#[serde(default)]
|
||||
pub content_id: Option<Cow<'x, str>>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
#[serde(default)]
|
||||
pub content_transfer_encoding: Option<Cow<'x, str>>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
#[serde(default)]
|
||||
pub content_type: Option<ContentType<'x>>,
|
||||
#[serde(borrow)]
|
||||
#[serde(skip_serializing_if = "HashMap::is_empty")]
|
||||
#[serde(default)]
|
||||
pub others: HashMap<&'x str, Vec<Cow<'x, str>>>,
|
||||
}
|
||||
|
||||
#[derive(PartialEq, Debug, Default, Serialize, Deserialize)]
|
||||
pub struct MimeHeader<'x> {
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
#[serde(default)]
|
||||
pub content_description: Option<Cow<'x, str>>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
#[serde(default)]
|
||||
pub content_disposition: Option<ContentType<'x>>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
#[serde(default)]
|
||||
pub content_id: Option<Cow<'x, str>>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
#[serde(default)]
|
||||
pub content_transfer_encoding: Option<Cow<'x, str>>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
#[serde(default)]
|
||||
pub content_type: Option<ContentType<'x>>,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
||||
pub struct Addr<'x> {
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
#[serde(default)]
|
||||
name: Option<Cow<'x, str>>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
#[serde(default)]
|
||||
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>>,
|
||||
#[serde(skip_serializing_if = "Vec::is_empty")]
|
||||
#[serde(default)]
|
||||
addresses: Vec<Addr<'x>>,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
||||
pub enum Address<'x> {
|
||||
Address(Addr<'x>),
|
||||
AddressList(Vec<Addr<'x>>),
|
||||
Group(Group<'x>),
|
||||
GroupList(Vec<Group<'x>>),
|
||||
Collection(Vec<Address<'x>>),
|
||||
Empty,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
||||
pub struct ContentType<'x> {
|
||||
c_type: Cow<'x, str>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
#[serde(default)]
|
||||
c_subtype: Option<Cow<'x, str>>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
#[serde(default)]
|
||||
attributes: Option<HashMap<Cow<'x, str>, Cow<'x, str>>>,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
||||
pub struct DateTime {
|
||||
year: u32,
|
||||
month: u32,
|
||||
day: u32,
|
||||
hour: u32,
|
||||
minute: u32,
|
||||
second: u32,
|
||||
tz_before_gmt: bool,
|
||||
tz_hour: u32,
|
||||
tz_minute: u32,
|
||||
}
|
||||
|
||||
@@ -9,40 +9,9 @@
|
||||
* except according to those terms.
|
||||
*/
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::borrow::Cow;
|
||||
|
||||
use crate::{decoders::encoded_word::parse_encoded_word, parsers::message_stream::MessageStream};
|
||||
|
||||
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
||||
pub struct Addr<'x> {
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
#[serde(default)]
|
||||
name: Option<Cow<'x, str>>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
#[serde(default)]
|
||||
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>>,
|
||||
#[serde(skip_serializing_if = "Vec::is_empty")]
|
||||
#[serde(default)]
|
||||
addresses: Vec<Addr<'x>>,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
||||
pub enum Address<'x> {
|
||||
Address(Addr<'x>),
|
||||
AddressList(Vec<Addr<'x>>),
|
||||
Group(Group<'x>),
|
||||
GroupList(Vec<Group<'x>>),
|
||||
Collection(Vec<Address<'x>>),
|
||||
Empty,
|
||||
}
|
||||
use crate::{Addr, Address, Group, decoders::encoded_word::parse_encoded_word, parsers::message_stream::MessageStream};
|
||||
|
||||
impl<'x> Default for Address<'x> {
|
||||
fn default() -> Self {
|
||||
|
||||
@@ -9,11 +9,9 @@
|
||||
* except according to those terms.
|
||||
*/
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::{
|
||||
borrow::Cow,
|
||||
collections::{hash_map::Entry, HashMap},
|
||||
ops::Add,
|
||||
};
|
||||
|
||||
use crate::{
|
||||
@@ -23,19 +21,9 @@ use crate::{
|
||||
hex::decode_hex,
|
||||
},
|
||||
parsers::message_stream::MessageStream,
|
||||
ContentType,
|
||||
};
|
||||
|
||||
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
||||
pub struct ContentType<'x> {
|
||||
c_type: Cow<'x, str>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
#[serde(default)]
|
||||
c_subtype: Option<Cow<'x, str>>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
#[serde(default)]
|
||||
attributes: Option<HashMap<Cow<'x, str>, Cow<'x, str>>>,
|
||||
}
|
||||
|
||||
impl<'x> ContentType<'x> {
|
||||
pub fn get_type(&'x self) -> &'x str {
|
||||
&self.c_type
|
||||
|
||||
@@ -11,21 +11,7 @@
|
||||
|
||||
use std::fmt;
|
||||
|
||||
use crate::parsers::message_stream::MessageStream;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
||||
pub struct DateTime {
|
||||
year: u32,
|
||||
month: u32,
|
||||
day: u32,
|
||||
hour: u32,
|
||||
minute: u32,
|
||||
second: u32,
|
||||
tz_before_gmt: bool,
|
||||
tz_hour: u32,
|
||||
tz_minute: u32,
|
||||
}
|
||||
use crate::{DateTime, parsers::message_stream::MessageStream};
|
||||
|
||||
impl DateTime {
|
||||
pub fn to_iso8601(&self) -> String {
|
||||
@@ -50,7 +36,7 @@ impl fmt::Display for DateTime {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn parse_date<'x>(stream: &'x MessageStream, abort_on_invalid: bool) -> Option<DateTime> {
|
||||
pub fn parse_date(stream: &MessageStream, abort_on_invalid: bool) -> Option<DateTime> {
|
||||
let mut pos = 0;
|
||||
let mut parts = [0u32; 7];
|
||||
let mut parts_sizes = [
|
||||
@@ -205,6 +191,7 @@ pub static MONTH_MAP: &[u8] = &[
|
||||
5, 0, 0, 0, 10, 3, 0, 0, 0, 7, 1, 0, 0, 0, 12, 6, 0, 0, 0, 8, 4, 0, 0, 0, 2, 9, 0, 0, 0, 0, 11,
|
||||
];
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::parsers::{fields::date::parse_date, message_stream::MessageStream};
|
||||
|
||||
@@ -281,7 +268,6 @@ mod tests {
|
||||
//println!("{} -> None", input.0.escape_debug());
|
||||
assert!(input.1.is_empty());
|
||||
}
|
||||
_ => unreachable!(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -65,9 +65,8 @@ pub fn parse_id<'x>(stream: &MessageStream<'x>) -> Option<Vec<Cow<'x, str>>> {
|
||||
None
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::borrow::Cow;
|
||||
|
||||
use crate::parsers::{fields::id::parse_id, message_stream::MessageStream};
|
||||
|
||||
#[test]
|
||||
|
||||
+20
-19
@@ -47,7 +47,7 @@ fn add_token<'x>(parser: &mut ListParser<'x>, stream: &MessageStream<'x>, add_sp
|
||||
}
|
||||
}
|
||||
|
||||
fn add_tokens_to_list<'x>(parser: &mut ListParser<'x>) {
|
||||
fn add_tokens_to_list(parser: &mut ListParser) {
|
||||
if !parser.tokens.is_empty() {
|
||||
parser.list.push(if parser.tokens.len() == 1 {
|
||||
parser.tokens.pop().unwrap()
|
||||
@@ -101,7 +101,7 @@ pub fn parse_comma_separared<'x>(stream: &MessageStream<'x>) -> Option<Vec<Cow<'
|
||||
b'=' if parser.is_token_start => {
|
||||
if let Some(token) = parse_encoded_word(stream) {
|
||||
add_token(&mut parser, stream, true);
|
||||
parser.tokens.push(token.into());
|
||||
parser.tokens.push(token);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
@@ -133,6 +133,7 @@ pub fn parse_comma_separared<'x>(stream: &MessageStream<'x>) -> Option<Vec<Cow<'
|
||||
None
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::parsers::{fields::list::parse_comma_separared, message_stream::MessageStream};
|
||||
|
||||
@@ -140,18 +141,18 @@ mod tests {
|
||||
fn parse_comma_separated_text() {
|
||||
let inputs = [
|
||||
(" one item \n", vec!["one item"]),
|
||||
("simple, list\n", vec!["simple".into(), "list".into()]),
|
||||
("simple, list\n", vec!["simple", "list"]),
|
||||
(
|
||||
"multi \r\n list, \r\n with, cr lf \r\n",
|
||||
vec!["multi list".into(), "with".into(), "cr lf".into()],
|
||||
vec!["multi list", "with", "cr lf"],
|
||||
),
|
||||
(
|
||||
"=?iso-8859-1?q?this is some text?=, in, a, list, \n",
|
||||
vec![
|
||||
"this is some text".into(),
|
||||
"in".into(),
|
||||
"a".into(),
|
||||
"list".into(),
|
||||
"this is some text",
|
||||
"in",
|
||||
"a",
|
||||
"list",
|
||||
],
|
||||
),
|
||||
(
|
||||
@@ -161,25 +162,25 @@ mod tests {
|
||||
" , but, in a list, which, is, more, fun!\n"
|
||||
),
|
||||
vec![
|
||||
"If you can read this you understand the example.".into(),
|
||||
"but".into(),
|
||||
"in a list".into(),
|
||||
"which".into(),
|
||||
"is".into(),
|
||||
"more".into(),
|
||||
"fun!".into(),
|
||||
"If you can read this you understand the example.",
|
||||
"but",
|
||||
"in a list",
|
||||
"which",
|
||||
"is",
|
||||
"more",
|
||||
"fun!",
|
||||
],
|
||||
),
|
||||
(
|
||||
"=?ISO-8859-1?Q?a?= =?ISO-8859-1?Q?b?=\n , listed\n",
|
||||
vec!["ab".into(), "listed".into()],
|
||||
vec!["ab", "listed"],
|
||||
),
|
||||
(
|
||||
"ハロー・ワールド, and also, ascii terms\n",
|
||||
vec![
|
||||
"ハロー・ワールド".into(),
|
||||
"and also".into(),
|
||||
"ascii terms".into(),
|
||||
"ハロー・ワールド",
|
||||
"and also",
|
||||
"ascii terms",
|
||||
],
|
||||
),
|
||||
];
|
||||
|
||||
@@ -11,18 +11,11 @@
|
||||
|
||||
use std::borrow::Cow;
|
||||
|
||||
use self::{
|
||||
address::{parse_address, Address},
|
||||
content_type::ContentType,
|
||||
date::DateTime,
|
||||
id::parse_id,
|
||||
list::parse_comma_separared,
|
||||
raw::{parse_and_ignore, parse_raw},
|
||||
unstructured::parse_unstructured,
|
||||
};
|
||||
use crate::{Address, ContentType, DateTime, MessageHeader, MimeHeader};
|
||||
|
||||
use self::{address::parse_address, id::parse_id, list::parse_comma_separared, raw::{parse_and_ignore, parse_raw}, unstructured::parse_unstructured};
|
||||
|
||||
use super::{
|
||||
header::{MessageHeader, MimeHeader},
|
||||
message_stream::MessageStream,
|
||||
};
|
||||
|
||||
@@ -581,7 +574,7 @@ fn append_address_line<'x>(value: &mut Address<'x>, new_value: Address<'x>) {
|
||||
}
|
||||
}
|
||||
|
||||
fn append_date_line<'x>(value: &mut Option<Vec<DateTime>>, new_value: Option<DateTime>) {
|
||||
fn append_date_line(value: &mut Option<Vec<DateTime>>, new_value: Option<DateTime>) {
|
||||
if let Some(new_value) = new_value {
|
||||
if let Some(value) = value {
|
||||
value.push(new_value);
|
||||
@@ -614,7 +607,7 @@ fn append_line_many<'x>(
|
||||
}
|
||||
}
|
||||
|
||||
pub fn parse_no_op<'x>(header: &mut dyn MessageField<'x>, stream: &MessageStream<'x>) {}
|
||||
pub fn parse_no_op<'x>(_header: &mut dyn MessageField<'x>, _stream: &MessageStream<'x>) {}
|
||||
|
||||
pub fn parse_unsupported<'x>(
|
||||
header: &mut dyn MessageField<'x>,
|
||||
|
||||
@@ -55,25 +55,21 @@ pub fn parse_raw<'x>(stream: &MessageStream<'x>) -> Option<Cow<'x, str>> {
|
||||
None
|
||||
}
|
||||
|
||||
pub fn parse_and_ignore<'x>(stream: &MessageStream<'x>) {
|
||||
let mut token_start: usize = 0;
|
||||
let mut token_end: usize = 0;
|
||||
let mut is_token_safe = true;
|
||||
|
||||
pub fn parse_and_ignore(stream: &MessageStream) {
|
||||
while let Some(ch) = stream.next() {
|
||||
match ch {
|
||||
b'\n' => match stream.peek() {
|
||||
if ch == &b'\n' {
|
||||
match stream.peek() {
|
||||
Some(b' ' | b'\t') => {
|
||||
stream.advance(1);
|
||||
continue;
|
||||
}
|
||||
_ => return,
|
||||
},
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::parsers::{fields::raw::parse_raw, message_stream::MessageStream};
|
||||
|
||||
|
||||
@@ -85,7 +85,7 @@ pub fn parse_unstructured<'x>(stream: &MessageStream<'x>) -> Option<Cow<'x, str>
|
||||
b'=' if parser.is_token_start => {
|
||||
if let Some(token) = parse_encoded_word(stream) {
|
||||
add_token(&mut parser, stream, true);
|
||||
parser.tokens.push(token.into());
|
||||
parser.tokens.push(token);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
@@ -112,6 +112,7 @@ pub fn parse_unstructured<'x>(stream: &MessageStream<'x>) -> Option<Cow<'x, str>
|
||||
None
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
|
||||
#[test]
|
||||
@@ -119,7 +120,6 @@ mod tests {
|
||||
use crate::parsers::{
|
||||
fields::unstructured::parse_unstructured, message_stream::MessageStream,
|
||||
};
|
||||
use std::borrow::Cow;
|
||||
|
||||
let inputs = [
|
||||
("Saying Hello\n", "Saying Hello", true),
|
||||
|
||||
+2
-138
@@ -9,128 +9,12 @@
|
||||
* except according to those terms.
|
||||
*/
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::{borrow::Cow, collections::HashMap};
|
||||
|
||||
use crate::parsers::message_stream::MessageStream;
|
||||
use crate::{MessageHeader, MimeHeader, parsers::message_stream::MessageStream};
|
||||
|
||||
use super::fields::{
|
||||
address::Address, content_type::ContentType, date::DateTime, parse_unsupported, MessageField,
|
||||
parse_unsupported, MessageField,
|
||||
};
|
||||
|
||||
#[derive(PartialEq, Debug, Default, Serialize, Deserialize)]
|
||||
pub struct MessageHeader<'x> {
|
||||
#[serde(skip_serializing_if = "Address::is_empty")]
|
||||
#[serde(default)]
|
||||
pub bcc: Address<'x>,
|
||||
#[serde(skip_serializing_if = "Address::is_empty")]
|
||||
#[serde(default)]
|
||||
pub cc: Address<'x>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
#[serde(default)]
|
||||
pub comments: Option<Vec<Cow<'x, str>>>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
#[serde(default)]
|
||||
pub date: Option<DateTime>,
|
||||
#[serde(skip_serializing_if = "Address::is_empty")]
|
||||
#[serde(default)]
|
||||
pub from: Address<'x>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
#[serde(default)]
|
||||
pub in_reply_to: Option<Vec<Cow<'x, str>>>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
#[serde(default)]
|
||||
pub keywords: Option<Vec<Cow<'x, str>>>,
|
||||
#[serde(skip_serializing_if = "Address::is_empty")]
|
||||
#[serde(default)]
|
||||
pub list_archive: Address<'x>,
|
||||
#[serde(skip_serializing_if = "Address::is_empty")]
|
||||
#[serde(default)]
|
||||
pub list_help: Address<'x>,
|
||||
#[serde(skip_serializing_if = "Address::is_empty")]
|
||||
#[serde(default)]
|
||||
pub list_id: Address<'x>,
|
||||
#[serde(skip_serializing_if = "Address::is_empty")]
|
||||
#[serde(default)]
|
||||
pub list_owner: Address<'x>,
|
||||
#[serde(skip_serializing_if = "Address::is_empty")]
|
||||
#[serde(default)]
|
||||
pub list_post: Address<'x>,
|
||||
#[serde(skip_serializing_if = "Address::is_empty")]
|
||||
#[serde(default)]
|
||||
pub list_subscribe: Address<'x>,
|
||||
#[serde(skip_serializing_if = "Address::is_empty")]
|
||||
#[serde(default)]
|
||||
pub list_unsubscribe: Address<'x>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
#[serde(default)]
|
||||
pub message_id: Option<Cow<'x, str>>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
#[serde(default)]
|
||||
pub mime_version: Option<Cow<'x, str>>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
#[serde(default)]
|
||||
pub received: Option<Vec<Cow<'x, str>>>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
#[serde(default)]
|
||||
pub references: Option<Vec<Cow<'x, str>>>,
|
||||
#[serde(skip_serializing_if = "Address::is_empty")]
|
||||
#[serde(default)]
|
||||
pub reply_to: Address<'x>,
|
||||
#[serde(skip_serializing_if = "Address::is_empty")]
|
||||
#[serde(default)]
|
||||
pub resent_bcc: Address<'x>,
|
||||
#[serde(skip_serializing_if = "Address::is_empty")]
|
||||
#[serde(default)]
|
||||
pub resent_cc: Address<'x>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
#[serde(default)]
|
||||
pub resent_date: Option<Vec<DateTime>>,
|
||||
#[serde(skip_serializing_if = "Address::is_empty")]
|
||||
#[serde(default)]
|
||||
pub resent_from: Address<'x>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
#[serde(default)]
|
||||
pub resent_message_id: Option<Vec<Cow<'x, str>>>,
|
||||
#[serde(skip_serializing_if = "Address::is_empty")]
|
||||
#[serde(default)]
|
||||
pub resent_sender: Address<'x>,
|
||||
#[serde(skip_serializing_if = "Address::is_empty")]
|
||||
#[serde(default)]
|
||||
pub resent_to: Address<'x>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
#[serde(default)]
|
||||
pub return_path: Option<Vec<Cow<'x, str>>>,
|
||||
#[serde(skip_serializing_if = "Address::is_empty")]
|
||||
#[serde(default)]
|
||||
pub sender: Address<'x>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
#[serde(default)]
|
||||
pub subject: Option<Cow<'x, str>>,
|
||||
#[serde(skip_serializing_if = "Address::is_empty")]
|
||||
#[serde(default)]
|
||||
pub to: Address<'x>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
#[serde(default)]
|
||||
pub content_description: Option<Cow<'x, str>>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
#[serde(default)]
|
||||
pub content_disposition: Option<ContentType<'x>>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
#[serde(default)]
|
||||
pub content_id: Option<Cow<'x, str>>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
#[serde(default)]
|
||||
pub content_transfer_encoding: Option<Cow<'x, str>>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
#[serde(default)]
|
||||
pub content_type: Option<ContentType<'x>>,
|
||||
#[serde(borrow)]
|
||||
#[serde(skip_serializing_if = "HashMap::is_empty")]
|
||||
#[serde(default)]
|
||||
pub others: HashMap<&'x str, Vec<Cow<'x, str>>>,
|
||||
}
|
||||
|
||||
impl<'x> MessageHeader<'x> {
|
||||
pub fn new() -> MessageHeader<'x> {
|
||||
MessageHeader {
|
||||
@@ -139,25 +23,6 @@ impl<'x> MessageHeader<'x> {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(PartialEq, Debug, Default, Serialize, Deserialize)]
|
||||
pub struct MimeHeader<'x> {
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
#[serde(default)]
|
||||
pub content_description: Option<Cow<'x, str>>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
#[serde(default)]
|
||||
pub content_disposition: Option<ContentType<'x>>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
#[serde(default)]
|
||||
pub content_id: Option<Cow<'x, str>>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
#[serde(default)]
|
||||
pub content_transfer_encoding: Option<Cow<'x, str>>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
#[serde(default)]
|
||||
pub content_type: Option<ContentType<'x>>,
|
||||
}
|
||||
|
||||
impl<'x> MimeHeader<'x> {
|
||||
pub fn new() -> MimeHeader<'x> {
|
||||
MimeHeader {
|
||||
@@ -196,7 +61,6 @@ pub fn parse_headers<'x>(header: &mut dyn MessageField<'x>, stream: &MessageStre
|
||||
HeaderParserResult::Unsupported(name) => parse_unsupported(header, stream, name),
|
||||
HeaderParserResult::Lf => return true,
|
||||
HeaderParserResult::Eof => return false,
|
||||
_ => return false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+14
-69
@@ -9,87 +9,27 @@
|
||||
* except according to those terms.
|
||||
*/
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::borrow::{Borrow, Cow};
|
||||
use std::borrow::{Cow};
|
||||
|
||||
use crate::decoders::{
|
||||
use crate::{AttachmentPart, BinaryPart, BodyPart, ContentType, Message, MessageHeader, MimeHeader, TextPart, decoders::{
|
||||
base64::Base64Decoder,
|
||||
charsets::map::{decoder_default, get_charset_decoder},
|
||||
html::{html_to_text, text_to_html},
|
||||
quoted_printable::QuotedPrintableDecoder,
|
||||
};
|
||||
}};
|
||||
|
||||
use super::{
|
||||
fields::{content_type::ContentType, MessageField},
|
||||
header::{parse_headers, MessageHeader, MimeHeader},
|
||||
fields::{MessageField},
|
||||
header::{parse_headers},
|
||||
message_stream::MessageStream,
|
||||
};
|
||||
|
||||
#[derive(Debug, Default, Serialize, Deserialize, PartialEq)]
|
||||
pub struct Message<'x> {
|
||||
#[serde(borrow)]
|
||||
header: Box<MessageHeader<'x>>,
|
||||
#[serde(skip_serializing_if = "Vec::is_empty")]
|
||||
#[serde(default)]
|
||||
html_body: Vec<BodyPart<'x>>,
|
||||
#[serde(skip_serializing_if = "Vec::is_empty")]
|
||||
#[serde(default)]
|
||||
text_body: Vec<BodyPart<'x>>,
|
||||
#[serde(skip_serializing_if = "Vec::is_empty")]
|
||||
#[serde(default)]
|
||||
attachments: Vec<AttachmentPart<'x>>,
|
||||
}
|
||||
#[derive(Debug, Default, Serialize, Deserialize, PartialEq)]
|
||||
pub struct TextPart<'x> {
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
#[serde(default)]
|
||||
header: Option<MimeHeader<'x>>,
|
||||
contents: Cow<'x, str>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Default, Serialize, Deserialize, PartialEq)]
|
||||
pub struct BinaryPart<'x> {
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
#[serde(default)]
|
||||
header: Option<MimeHeader<'x>>,
|
||||
#[serde(with = "serde_bytes")]
|
||||
#[serde(borrow)]
|
||||
contents: Cow<'x, [u8]>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize, PartialEq)]
|
||||
#[serde(untagged)]
|
||||
pub enum BodyPart<'x> {
|
||||
Text(TextPart<'x>),
|
||||
InlineBinary(u32),
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize, PartialEq)]
|
||||
pub enum AttachmentPart<'x> {
|
||||
Text(TextPart<'x>),
|
||||
#[serde(borrow)]
|
||||
Binary(BinaryPart<'x>),
|
||||
#[serde(borrow)]
|
||||
InlineBinary(BinaryPart<'x>),
|
||||
Message(Message<'x>),
|
||||
}
|
||||
|
||||
impl<'x> Message<'x> {
|
||||
fn new() -> Message<'x> {
|
||||
Message {
|
||||
header: Box::new(MessageHeader::new()),
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
enum MimeType {
|
||||
MultipartMixed,
|
||||
MultipartAlernative,
|
||||
MultipartRelated,
|
||||
MultipartDigest,
|
||||
MultipartOther,
|
||||
TextPlain,
|
||||
TextHtml,
|
||||
TextOther,
|
||||
@@ -183,6 +123,13 @@ impl MessageParserState {
|
||||
}
|
||||
|
||||
impl<'x> Message<'x> {
|
||||
fn new() -> Message<'x> {
|
||||
Message {
|
||||
header: Box::new(MessageHeader::new()),
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn parse(bytes: &'x mut [u8]) -> Message<'x> {
|
||||
let stream = MessageStream::new(bytes);
|
||||
|
||||
@@ -468,7 +415,7 @@ impl<'x> Message<'x> {
|
||||
}
|
||||
}
|
||||
|
||||
// Found HTML part only
|
||||
// Found text part only
|
||||
if state.html_parts == message.html_body.len()
|
||||
&& state.text_parts != message.text_body.len()
|
||||
{
|
||||
@@ -521,9 +468,7 @@ impl<'x> Message<'x> {
|
||||
}
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::{ffi::OsStr, fmt::format, fs, path::PathBuf};
|
||||
|
||||
use serde_json::to_string;
|
||||
use std::{fs, path::PathBuf};
|
||||
|
||||
use crate::parsers::message::Message;
|
||||
|
||||
|
||||
@@ -87,15 +87,6 @@ impl<'x> MessageStream<'x> {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn match_bytes(&self, start_pos: usize, bytes: &[u8]) -> bool {
|
||||
unsafe {
|
||||
(*self.data.get())
|
||||
.get(start_pos..start_pos + bytes.len())
|
||||
.unwrap_or(&[])
|
||||
== bytes
|
||||
}
|
||||
}
|
||||
|
||||
pub fn seek_next_part(&self, boundary: &[u8]) -> bool {
|
||||
if !boundary.is_empty() {
|
||||
unsafe {
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
use mail_parser::Message;
|
||||
|
||||
#[test]
|
||||
fn test_mail_parser() {
|
||||
let mut input = "pepe".to_string();
|
||||
let result = Message::parse(unsafe{input.as_bytes_mut()});
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user