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
use charset::{decode_ascii, Charset};
use {MailParseError, ParsedContentType};
pub enum Body<'a> {
Base64(EncodedBody<'a>),
QuotedPrintable(EncodedBody<'a>),
SevenBit(TextBody<'a>),
EightBit(TextBody<'a>),
Binary(BinaryBody<'a>),
}
impl<'a> Body<'a> {
pub fn new(
body: &'a [u8],
ctype: &'a ParsedContentType,
transfer_encoding: &Option<String>,
) -> Body<'a> {
transfer_encoding
.as_ref()
.map(|encoding| match encoding.as_ref() {
"base64" => Body::Base64(EncodedBody {
decoder: decode_base64,
body,
ctype,
}),
"quoted-printable" => Body::QuotedPrintable(EncodedBody {
decoder: decode_quoted_printable,
body,
ctype,
}),
"7bit" => Body::SevenBit(TextBody { body, ctype }),
"8bit" => Body::EightBit(TextBody { body, ctype }),
"binary" => Body::Binary(BinaryBody { body, ctype }),
_ => Body::get_default(body, ctype),
})
.unwrap_or_else(|| Body::get_default(body, ctype))
}
fn get_default(body: &'a [u8], ctype: &'a ParsedContentType) -> Body<'a> {
Body::SevenBit(TextBody { body, ctype })
}
}
pub struct EncodedBody<'a> {
decoder: fn(&[u8]) -> Result<Vec<u8>, MailParseError>,
ctype: &'a ParsedContentType,
body: &'a [u8],
}
impl<'a> EncodedBody<'a> {
pub fn get_content_type(&self) -> &'a ParsedContentType {
self.ctype
}
pub fn get_raw(&self) -> &'a [u8] {
self.body
}
pub fn get_decoded(&self) -> Result<Vec<u8>, MailParseError> {
(self.decoder)(self.body)
}
pub fn get_decoded_as_string(&self) -> Result<String, MailParseError> {
get_body_as_string(&self.get_decoded()?, &self.ctype)
}
}
pub struct TextBody<'a> {
ctype: &'a ParsedContentType,
body: &'a [u8],
}
impl<'a> TextBody<'a> {
pub fn get_content_type(&self) -> &'a ParsedContentType {
self.ctype
}
pub fn get_raw(&self) -> &'a [u8] {
self.body
}
pub fn get_as_string(&self) -> Result<String, MailParseError> {
get_body_as_string(self.body, &self.ctype)
}
}
pub struct BinaryBody<'a> {
ctype: &'a ParsedContentType,
body: &'a [u8],
}
impl<'a> BinaryBody<'a> {
pub fn get_content_type(&self) -> &'a ParsedContentType {
self.ctype
}
pub fn get_raw(&self) -> &'a [u8] {
self.body
}
}
fn decode_base64(body: &[u8]) -> Result<Vec<u8>, MailParseError> {
let cleaned = body
.iter()
.filter(|c| !c.is_ascii_whitespace())
.cloned()
.collect::<Vec<u8>>();
Ok(base64::decode(&cleaned)?)
}
fn decode_quoted_printable(body: &[u8]) -> Result<Vec<u8>, MailParseError> {
Ok(quoted_printable::decode(
body,
quoted_printable::ParseMode::Robust,
)?)
}
fn get_body_as_string(body: &[u8], ctype: &ParsedContentType) -> Result<String, MailParseError> {
let cow = if let Some(charset) = Charset::for_label(ctype.charset.as_bytes()) {
let (cow, _, _) = charset.decode(body);
cow
} else {
decode_ascii(body)
};
Ok(cow.into_owned())
}