Skip to content

Commit 46a8db9

Browse files
committed
MBox mailbox parsing support.
1 parent a0448c3 commit 46a8db9

File tree

3 files changed

+145
-0
lines changed

3 files changed

+145
-0
lines changed

examples/parse_mbox.rs

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* Copyright Stalwart Labs, Minter Ltd. See the COPYING
3+
* file at the top-level directory of this distribution.
4+
*
5+
* Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
* https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
* <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
8+
* option. This file may not be copied, modified, or distributed
9+
* except according to those terms.
10+
*/
11+
12+
use mail_parser::{parsers::mbox::MBoxParser, Message};
13+
14+
fn main() {
15+
// Read an MBox mailbox from stdin and prints each message as YAML.
16+
17+
for raw_message in MBoxParser::new(std::io::stdin()) {
18+
let message = Message::parse(&raw_message).unwrap();
19+
println!("{}", serde_yaml::to_string(&message).unwrap());
20+
}
21+
}

src/parsers/mbox.rs

+123
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
/*
2+
* Copyright Stalwart Labs, Minter Ltd. See the COPYING
3+
* file at the top-level directory of this distribution.
4+
*
5+
* Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
* https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
* <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
8+
* option. This file may not be copied, modified, or distributed
9+
* except according to those terms.
10+
*/
11+
12+
use std::io::{BufRead, BufReader, Read};
13+
14+
pub struct MBoxParser<T: Read> {
15+
reader: BufReader<T>,
16+
found_from: bool,
17+
}
18+
19+
impl<T> MBoxParser<T>
20+
where
21+
T: Read,
22+
{
23+
pub fn new(reader: T) -> MBoxParser<T> {
24+
MBoxParser {
25+
reader: BufReader::new(reader),
26+
found_from: false,
27+
}
28+
}
29+
}
30+
31+
impl<T> Iterator for MBoxParser<T>
32+
where
33+
T: Read,
34+
{
35+
type Item = Vec<u8>;
36+
37+
fn next(&mut self) -> Option<Vec<u8>> {
38+
let mut message = Vec::with_capacity(1024);
39+
let mut message_line = Vec::with_capacity(80);
40+
41+
while self
42+
.reader
43+
.read_until(b'\n', &mut message_line)
44+
.expect("Error while reading Mbox")
45+
> 0
46+
{
47+
let is_from = message_line
48+
.get(..5)
49+
.map(|line| line == b"From ")
50+
.unwrap_or(false);
51+
52+
if self.found_from {
53+
if !is_from {
54+
if message_line[0] != b'>' {
55+
message.append(&mut message_line);
56+
} else if message_line
57+
.iter()
58+
.skip_while(|&&ch| ch == b'>')
59+
.take(5)
60+
.copied()
61+
.collect::<Vec<u8>>()
62+
== b"From "
63+
{
64+
message.extend_from_slice(&message_line[1..]);
65+
message_line.clear();
66+
} else {
67+
message.append(&mut message_line);
68+
}
69+
} else {
70+
break;
71+
}
72+
} else {
73+
if is_from {
74+
self.found_from = true;
75+
}
76+
message_line.clear();
77+
}
78+
}
79+
80+
if !message.is_empty() {
81+
Some(message)
82+
} else {
83+
None
84+
}
85+
}
86+
}
87+
88+
#[cfg(test)]
89+
mod tests {
90+
use super::MBoxParser;
91+
92+
#[test]
93+
fn parse_mbox() {
94+
let message = br#"From Mon, 15 Jan 2018 15:30:00 +0100
95+
Message 1
96+
97+
From Mon, 15 Jan 2018 15:30:00 +0100
98+
Message 2
99+
100+
From Mon, 15 Jan 2018 15:30:00 +0100
101+
Message 3
102+
>From hello
103+
>>From world
104+
>>>From test
105+
106+
From Mon, 15 Jan 2018 15:30:00 +0100
107+
Message 4
108+
> From
109+
>F
110+
"#;
111+
112+
let mut parser = MBoxParser::new(&message[..]);
113+
114+
assert_eq!(parser.next().unwrap(), b"Message 1\n\n");
115+
assert_eq!(parser.next().unwrap(), b"Message 2\n\n");
116+
assert_eq!(
117+
parser.next().unwrap(),
118+
b"Message 3\nFrom hello\n>From world\n>>From test\n\n"
119+
);
120+
assert_eq!(parser.next().unwrap(), b"Message 4\n> From\n>F\n");
121+
assert!(parser.next().is_none());
122+
}
123+
}

src/parsers/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@
1111

1212
pub mod fields;
1313
pub mod header;
14+
pub mod mbox;
1415
pub mod message;
1516
pub mod mime;

0 commit comments

Comments
 (0)