mirror of
https://github.com/stalwartlabs/mail-parser.git
synced 2026-11-28 08:01:24 +00:00
Thread name parsing.
This commit is contained in:
@@ -1,3 +1,8 @@
|
||||
mail-parser 0.4.2
|
||||
================================
|
||||
- Added `Message::get_thread_name()` to obtain the base subject of a message as defined in [RFC 5957 - Internet Message Access Protocol - SORT and THREAD Extensions (Section 2.1)](https://datatracker.ietf.org/doc/html/rfc5256#section-2.1).
|
||||
- Added `MimeHeader::get_attachment_name` for simplified access to a MIME attachment file name.
|
||||
|
||||
mail-parser 0.4.1
|
||||
================================
|
||||
- Lazy parsing of nested e-mail messages.
|
||||
|
||||
Generated
+1
-1
@@ -59,7 +59,7 @@ checksum = "7fb9b38af92608140b86b693604b9ffcc5824240a484d1ecd4795bacb2fe88f3"
|
||||
|
||||
[[package]]
|
||||
name = "mail-parser"
|
||||
version = "0.4.1"
|
||||
version = "0.4.2"
|
||||
dependencies = [
|
||||
"encoding_rs",
|
||||
"serde",
|
||||
|
||||
+1
-1
@@ -1,7 +1,7 @@
|
||||
[package]
|
||||
name = "mail-parser"
|
||||
description = "Fast and robust e-mail parsing library for Rust"
|
||||
version = "0.4.1"
|
||||
version = "0.4.2"
|
||||
edition = "2018"
|
||||
authors = [ "Stalwart Labs <hello@stalw.art>"]
|
||||
license = "Apache-2.0 OR MIT"
|
||||
|
||||
@@ -224,6 +224,7 @@ and, to run the benchmarks:
|
||||
- [RFC 2919 - List-Id: A Structured Field and Namespace for the Identification of Mailing Lists](https://datatracker.ietf.org/doc/html/rfc2919)
|
||||
- [RFC 3339 - Date and Time on the Internet: Timestamps](https://datatracker.ietf.org/doc/html/rfc3339)
|
||||
- [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)
|
||||
- [RFC 5957 - Internet Message Access Protocol - SORT and THREAD Extensions (Section 2.1)](https://datatracker.ietf.org/doc/html/rfc5256#section-2.1)
|
||||
|
||||
## Supported Character Sets
|
||||
|
||||
|
||||
Generated
+1
-1
@@ -42,7 +42,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "mail-parser"
|
||||
version = "0.2.1"
|
||||
version = "0.4.2"
|
||||
dependencies = [
|
||||
"encoding_rs",
|
||||
"serde",
|
||||
|
||||
@@ -22,11 +22,12 @@ use mail_parser::{
|
||||
id::parse_id,
|
||||
list::parse_comma_separared,
|
||||
raw::{parse_and_ignore, parse_raw},
|
||||
thread::{thread_name, trim_trailing_fwd},
|
||||
unstructured::parse_unstructured,
|
||||
},
|
||||
mime::*,
|
||||
header::parse_header_name,
|
||||
message::MessageStream,
|
||||
mime::*,
|
||||
},
|
||||
Message,
|
||||
};
|
||||
@@ -62,7 +63,7 @@ fuzz_target!(|data: &[u8]| {
|
||||
parse_content_type(&mut stream);
|
||||
}
|
||||
9 => {
|
||||
parse_header_name(&mut stream);
|
||||
parse_header_name(&data);
|
||||
}
|
||||
10 => {
|
||||
decode_rfc2047(&mut stream, 0);
|
||||
@@ -113,26 +114,27 @@ fuzz_target!(|data: &[u8]| {
|
||||
}
|
||||
}
|
||||
|
||||
// Fuzz HTML functions
|
||||
// Fuzz text functions
|
||||
let mut html_str = String::with_capacity(data.len());
|
||||
let str_data = String::from_utf8_lossy(data);
|
||||
add_html_token(&mut html_str, str_data.as_ref().as_bytes(), false);
|
||||
html_to_text(&str_data);
|
||||
text_to_html(&str_data);
|
||||
thread_name(&str_data);
|
||||
trim_trailing_fwd(&str_data);
|
||||
|
||||
// Fuzz decoding functions
|
||||
decode_hex(data);
|
||||
get_charset_decoder(data);
|
||||
|
||||
let decoders: &[for<'x> fn(&'x [u8]) -> String] = &[
|
||||
for decoder in &[
|
||||
decoder_utf7,
|
||||
decoder_utf16_le,
|
||||
decoder_utf16_be,
|
||||
decoder_utf16,
|
||||
decoder_iso_8859_1,
|
||||
];
|
||||
|
||||
for decoder in decoders {
|
||||
] as &[for<'x> fn(&'x [u8]) -> String]
|
||||
{
|
||||
decoder(data);
|
||||
}
|
||||
|
||||
|
||||
+2
-1
@@ -59,6 +59,7 @@
|
||||
//! - [RFC 2919 - List-Id: A Structured Field and Namespace for the Identification of Mailing Lists](https://datatracker.ietf.org/doc/html/rfc2919)
|
||||
//! - [RFC 3339 - Date and Time on the Internet: Timestamps](https://datatracker.ietf.org/doc/html/rfc3339)
|
||||
//! - [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)
|
||||
//! - [RFC 5957 - Internet Message Access Protocol - SORT and THREAD Extensions (Section 2.1)](https://datatracker.ietf.org/doc/html/rfc5256#section-2.1)
|
||||
//!
|
||||
//! ## Supported Character Sets
|
||||
//!
|
||||
@@ -261,7 +262,7 @@ pub mod parsers;
|
||||
use std::{borrow::Cow, collections::HashMap, fmt};
|
||||
|
||||
use decoders::html::{html_to_text, text_to_html};
|
||||
use parsers::fields::unstructured::thread_name;
|
||||
use parsers::fields::thread::thread_name;
|
||||
#[cfg(feature = "serde_support")]
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
|
||||
@@ -15,4 +15,5 @@ pub mod date;
|
||||
pub mod id;
|
||||
pub mod list;
|
||||
pub mod raw;
|
||||
pub mod thread;
|
||||
pub mod unstructured;
|
||||
|
||||
@@ -0,0 +1,308 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
static RE_PREFIXES: &[&str] = &[
|
||||
"re", "res", "sv", "antw", "ref", "aw", "απ", "השב", "vá", "r", "rif", "bls", "odp", "ynt",
|
||||
"atb", "رد", "回复", "转发",
|
||||
];
|
||||
|
||||
static FWD_PREFIXES: &[&str] = &[
|
||||
"fwd",
|
||||
"fw",
|
||||
"rv",
|
||||
"enc",
|
||||
"vs",
|
||||
"doorst",
|
||||
"vl",
|
||||
"tr",
|
||||
"wg",
|
||||
"πρθ",
|
||||
"הועבר",
|
||||
"továbbítás",
|
||||
"i",
|
||||
"fs",
|
||||
"trs",
|
||||
"vb",
|
||||
"pd",
|
||||
"i̇lt",
|
||||
"yml",
|
||||
"إعادة توجيه",
|
||||
"回覆",
|
||||
"轉寄",
|
||||
];
|
||||
|
||||
pub fn thread_name(text: &str) -> &str {
|
||||
let mut token_start = 0;
|
||||
let mut token_end = 0;
|
||||
|
||||
let mut thread_name_start = 0;
|
||||
let mut fwd_start = 0;
|
||||
let mut fwd_end = 0;
|
||||
let mut last_blob_end = 0;
|
||||
|
||||
let mut in_blob = false;
|
||||
let mut in_blob_ignore = false;
|
||||
let mut seen_header = false;
|
||||
let mut seen_blob_header = false;
|
||||
let mut token_found = false;
|
||||
|
||||
for (pos, ch) in text.char_indices() {
|
||||
match ch {
|
||||
'[' => {
|
||||
if !in_blob {
|
||||
if token_found {
|
||||
if token_end == 0 {
|
||||
token_end = pos;
|
||||
}
|
||||
let prefix = text[token_start..token_end].to_lowercase();
|
||||
if RE_PREFIXES.contains(&prefix.as_ref())
|
||||
|| FWD_PREFIXES.contains(&prefix.as_ref())
|
||||
{
|
||||
seen_header = true;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
token_found = false;
|
||||
in_blob = true;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
']' if in_blob => {
|
||||
if seen_blob_header && token_found {
|
||||
fwd_start = token_start;
|
||||
fwd_end = pos;
|
||||
}
|
||||
if !seen_header {
|
||||
last_blob_end = pos + 1;
|
||||
}
|
||||
in_blob = false;
|
||||
token_found = false;
|
||||
seen_blob_header = false;
|
||||
in_blob_ignore = false;
|
||||
}
|
||||
':' if !in_blob => {
|
||||
if (seen_header && token_found) || (!seen_header && !token_found) {
|
||||
break;
|
||||
} else if !seen_header {
|
||||
if token_end == 0 {
|
||||
token_end = pos;
|
||||
}
|
||||
let prefix = text[token_start..token_end].to_lowercase();
|
||||
if !RE_PREFIXES.contains(&prefix.as_ref())
|
||||
&& !FWD_PREFIXES.contains(&prefix.as_ref())
|
||||
{
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
seen_header = false;
|
||||
}
|
||||
thread_name_start = pos + 1;
|
||||
token_found = false;
|
||||
}
|
||||
':' if in_blob && !in_blob_ignore => {
|
||||
if token_end == 0 {
|
||||
token_end = pos;
|
||||
}
|
||||
|
||||
let prefix = text[token_start..token_end].to_lowercase();
|
||||
if FWD_PREFIXES.contains(&prefix.as_ref()) {
|
||||
token_found = false;
|
||||
seen_blob_header = true;
|
||||
} else if seen_blob_header && RE_PREFIXES.contains(&prefix.as_ref()) {
|
||||
token_found = false;
|
||||
} else {
|
||||
in_blob_ignore = true;
|
||||
}
|
||||
}
|
||||
_ if ch.is_whitespace() => {
|
||||
if token_end == 0 {
|
||||
token_end = pos;
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
if !token_found {
|
||||
token_start = pos;
|
||||
token_end = 0;
|
||||
token_found = true;
|
||||
} else if !in_blob && pos - token_start > 21 {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if last_blob_end > thread_name_start
|
||||
|| (fwd_start > 0 && last_blob_end > fwd_start && fwd_start > thread_name_start)
|
||||
{
|
||||
let result = trim_trailing_fwd(&text[last_blob_end..]);
|
||||
if !result.is_empty() {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
if fwd_start > 0 && thread_name_start < fwd_start {
|
||||
let result = trim_trailing_fwd(&text[fwd_start..fwd_end]);
|
||||
if !result.is_empty() {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
trim_trailing_fwd(&text[thread_name_start..])
|
||||
}
|
||||
|
||||
pub fn trim_trailing_fwd(text: &str) -> &str {
|
||||
let mut in_parentheses = false;
|
||||
let mut trim_end = true;
|
||||
let mut end_found = false;
|
||||
|
||||
let mut text_start = 0;
|
||||
let mut text_end = text.len();
|
||||
let mut fwd_end = 0;
|
||||
|
||||
for (pos, ch) in text.char_indices().rev() {
|
||||
match ch {
|
||||
'(' if !end_found => {
|
||||
if in_parentheses {
|
||||
in_parentheses = false;
|
||||
if fwd_end - pos > 2
|
||||
&& FWD_PREFIXES.contains(&text[pos + 1..fwd_end].to_lowercase().as_ref())
|
||||
{
|
||||
text_end = pos;
|
||||
trim_end = true;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
end_found = true;
|
||||
}
|
||||
')' if !end_found => {
|
||||
if !in_parentheses {
|
||||
in_parentheses = true;
|
||||
fwd_end = pos;
|
||||
} else {
|
||||
end_found = true;
|
||||
}
|
||||
}
|
||||
_ if ch.is_whitespace() => {
|
||||
if trim_end {
|
||||
text_end = pos;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
_ => {
|
||||
if !in_parentheses && !end_found {
|
||||
end_found = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if trim_end {
|
||||
trim_end = false;
|
||||
}
|
||||
text_start = pos;
|
||||
}
|
||||
|
||||
if text_end >= text_start {
|
||||
&text[text_start..text_end]
|
||||
} else {
|
||||
""
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::parsers::fields::thread::{thread_name, trim_trailing_fwd};
|
||||
|
||||
#[test]
|
||||
fn parse_thread_name() {
|
||||
let tests = [
|
||||
("re: hello", "hello"),
|
||||
("re:re: hello", "hello"),
|
||||
("re:fwd: hello", "hello"),
|
||||
("fwd[5]:re[5]: hello", "hello"),
|
||||
("fwd[99]: re[40]: hello", "hello"),
|
||||
(": hello", ": hello"),
|
||||
("z: hello", "z: hello"),
|
||||
("re:: hello", ": hello"),
|
||||
("[10] hello", "hello"),
|
||||
("fwd[a]: hello", "hello"),
|
||||
("re:", ""),
|
||||
("re::", ":"),
|
||||
("", ""),
|
||||
(" ", ""),
|
||||
("回复: 轉寄: 轉寄", "轉寄"),
|
||||
("aw[50]: wg: aw[1]: hallo", "hallo"),
|
||||
("res: rv: enc: továbbítás: ", ""),
|
||||
("[fwd: hello world]", "hello world"),
|
||||
("re: enc: re[5]: [fwd: hello world]", "hello world"),
|
||||
("[fwd: re: fw: hello world]", "hello world"),
|
||||
("[fwd: hello world]: another text", ": another text"),
|
||||
("[fwd: re: fwd:] another text", "another text"),
|
||||
("[hello world]", "[hello world]"),
|
||||
("re: fwd[9]: [hello world]", "[hello world]"),
|
||||
("[mailing-list] hello world", "hello world"),
|
||||
("[mailing-list] re: hello world", "hello world"),
|
||||
("[mailing-list] wg[8]:re: hello world", "hello world"),
|
||||
("hello [world]", "hello [world]"),
|
||||
(" [hello] [world] ", "[hello] [world]"),
|
||||
("[mailing-list] hello [world]", "hello [world]"),
|
||||
("[hello [world]", "[hello [world]"),
|
||||
("[]hello [world]", "hello [world]"),
|
||||
("[fwd: re: re:] fwd[6]:re: fw:", ""),
|
||||
("[fwd hello] world hello", "world hello"),
|
||||
("[fwd: مرحبا بالعالم]", "مرحبا بالعالم"),
|
||||
("[fwd: hello world] مرحبا بالعالم", "مرحبا بالعالم"),
|
||||
(" hello world ", "hello world"),
|
||||
(
|
||||
"[mailing-list] wg[8]:re: hello world (fwd)(fwd)",
|
||||
"hello world",
|
||||
),
|
||||
("[fwd: re: fw: hello world (fwd)]", "hello world"),
|
||||
(
|
||||
"res: rv: enc: továbbítás: hello world (doorst)",
|
||||
"hello world",
|
||||
),
|
||||
("[fwd: re: re: (fwd)] fwd[6]:re: fw: (fwd)", ""),
|
||||
];
|
||||
|
||||
for (input, expected) in tests {
|
||||
assert_eq!(thread_name(input), expected, "{:?}", input);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_trail_fwd() {
|
||||
let tests = [
|
||||
("hello (fwd)", "hello"),
|
||||
(" hello (fwd)(fwd)", "hello"),
|
||||
("hello (wg) (fwd) (fwd)", "hello"),
|
||||
("(fwd)(fwd)", ""),
|
||||
("(fwd)hello(fwd)", "(fwd)hello"),
|
||||
(" hello ", "hello"),
|
||||
(" hello world ", "hello world"),
|
||||
("", ""),
|
||||
(" ", ""),
|
||||
("hello ()(fwd)", "hello ()"),
|
||||
("(hello)", "(hello)"),
|
||||
("hello () (fwd) ()(fwd)", "hello () (fwd) ()"),
|
||||
(")(", ")("),
|
||||
(" 你好世界(fwd) ", "你好世界"),
|
||||
("你好世界 (回覆)", "你好世界"),
|
||||
("hello(fwd", "hello(fwd"),
|
||||
("hello(fwd))", "hello(fwd))"),
|
||||
];
|
||||
|
||||
for (input, expected) in tests {
|
||||
assert_eq!(trim_trailing_fwd(input), expected, "{:?}", input);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -104,141 +104,8 @@ pub fn parse_unstructured<'x>(stream: &mut MessageStream<'x>) -> HeaderValue<'x>
|
||||
HeaderValue::Empty
|
||||
}
|
||||
|
||||
pub fn thread_name(text: &str) -> &str {
|
||||
static THREAD_PREFIXES: &[&str] = &[
|
||||
"re",
|
||||
"fwd",
|
||||
"fw",
|
||||
"sv",
|
||||
"vs",
|
||||
"antw",
|
||||
"doorst",
|
||||
"vl",
|
||||
"ref",
|
||||
"tr",
|
||||
"aw",
|
||||
"wg",
|
||||
"απ",
|
||||
"πρθ",
|
||||
"השב",
|
||||
"הועבר",
|
||||
"vá",
|
||||
"továbbítás",
|
||||
"r",
|
||||
"rif",
|
||||
"i",
|
||||
"fs",
|
||||
"bls",
|
||||
"trs",
|
||||
"vb",
|
||||
"rv",
|
||||
"res",
|
||||
"enc",
|
||||
"odp",
|
||||
"pd",
|
||||
"ynt",
|
||||
"i̇lt",
|
||||
"atb",
|
||||
"yml",
|
||||
"رد",
|
||||
"إعادة توجيه",
|
||||
"回复",
|
||||
"回覆",
|
||||
"转发",
|
||||
"轉寄",
|
||||
];
|
||||
|
||||
let mut token_start = 0;
|
||||
let mut token_end = 0;
|
||||
|
||||
let mut in_brackets = false;
|
||||
let mut token_found = false;
|
||||
|
||||
for (pos, ch) in text.char_indices() {
|
||||
match ch {
|
||||
':' => {
|
||||
if in_brackets || !token_found {
|
||||
break;
|
||||
}
|
||||
if token_end == 0 {
|
||||
token_end = pos;
|
||||
}
|
||||
if THREAD_PREFIXES.contains(&text[token_start..token_end].to_lowercase().as_ref()) {
|
||||
token_found = false;
|
||||
token_start = pos + 1;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
'[' => {
|
||||
if !in_brackets && token_found {
|
||||
token_end = pos;
|
||||
in_brackets = true;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
']' => {
|
||||
if in_brackets {
|
||||
in_brackets = false;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
_ if ch.is_ascii_digit() => {
|
||||
if !in_brackets {
|
||||
break;
|
||||
}
|
||||
}
|
||||
_ if ch.is_whitespace() => {
|
||||
if token_found || in_brackets {
|
||||
break;
|
||||
}
|
||||
}
|
||||
_ if ch.is_alphabetic() => {
|
||||
if !token_found {
|
||||
token_start = pos;
|
||||
token_end = 0;
|
||||
token_found = true;
|
||||
} else if in_brackets {
|
||||
break;
|
||||
}
|
||||
}
|
||||
_ => break,
|
||||
}
|
||||
}
|
||||
&text[token_start..]
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::parsers::fields::unstructured::thread_name;
|
||||
|
||||
#[test]
|
||||
fn parse_thread_name() {
|
||||
let tests = [
|
||||
("re: hello", "hello"),
|
||||
("re:re: hello", "hello"),
|
||||
("re:fwd: hello", "hello"),
|
||||
("fwd[5]:re[5]: hello", "hello"),
|
||||
("fwd[99]: re[40]: hello", "hello"),
|
||||
(": hello", ": hello"),
|
||||
("z: hello", "z: hello"),
|
||||
("re:: hello", ": hello"),
|
||||
("[10] hello", "[10] hello"),
|
||||
("fwd[a]: hello", "fwd[a]: hello"),
|
||||
("re:", ""),
|
||||
("re::", ":"),
|
||||
("回复: 轉寄: 轉寄", "轉寄"),
|
||||
("aw[50]: wg: aw[1]: hallo", "hallo"),
|
||||
("res: rv: enc: továbbítás: ", " "),
|
||||
];
|
||||
|
||||
for (input, expected) in tests {
|
||||
assert_eq!(thread_name(input), expected, "{:?}", input);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_unstructured_text() {
|
||||
use crate::parsers::fields::unstructured::parse_unstructured;
|
||||
|
||||
Reference in New Issue
Block a user