This commit is contained in:
Mauro D
2022-08-10 12:51:30 +00:00
parent e922562b1c
commit 402ee4c4db
18 changed files with 391 additions and 28 deletions
+8
View File
@@ -1,3 +1,11 @@
mail-parser 0.6.0
================================
- Maildir parsing support.
- Headers and attributes are now stored in a `Vec` instead of a `HashMap` for tiny performance enhancement.
- Support for Content-Type attributes spanning multiple lines.
- Support for malformed Thunderbird messages (#27).
- Fixed raw offset range for body parts.
mail-parser 0.5.0
================================
- `Message` headers are now stored as a `MessagePart` with index 0.
Generated
+1 -1
View File
@@ -81,7 +81,7 @@ checksum = "7fb9b38af92608140b86b693604b9ffcc5824240a484d1ecd4795bacb2fe88f3"
[[package]]
name = "mail-parser"
version = "0.5.0"
version = "0.6.0"
dependencies = [
"bincode",
"chrono",
+1 -1
View File
@@ -1,7 +1,7 @@
[package]
name = "mail-parser"
description = "Fast and robust e-mail parsing library for Rust"
version = "0.5.0"
version = "0.6.0"
edition = "2018"
authors = [ "Stalwart Labs <hello@stalw.art>"]
license = "Apache-2.0 OR MIT"
+36
View File
@@ -0,0 +1,36 @@
/*
* Copyright Stalwart Labs 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 std::path::PathBuf;
use mail_parser::mailbox::maildir::FolderIterator;
fn main() {
// Iterates a Maildir++ structure printing the results to stdout.
let mut test_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
test_dir.push("tests");
test_dir.push("maildir");
for folder in FolderIterator::new(test_dir).unwrap() {
let folder = folder.unwrap();
println!("------\nMailbox: {:?}", folder.name().unwrap_or("INBOX"));
for message in folder {
let message = message.unwrap();
println!(
"Message with internal date {}, flags {:?} and content {:?}.",
message.internal_date(),
message.flags(),
String::from_utf8_lossy(message.contents())
);
}
}
}
@@ -9,13 +9,14 @@
* except according to those terms.
*/
use mail_parser::{parsers::mbox::MBoxParser, Message};
use mail_parser::{mailbox::mbox::MboxIterator, Message};
fn main() {
// Read an MBox mailbox from stdin and prints each message as YAML.
for raw_message in MBoxParser::new(std::io::stdin()) {
// Reads an MBox mailbox from stdin and prints each message as YAML.
for raw_message in MboxIterator::new(std::io::stdin()) {
let raw_message = raw_message.unwrap();
let message = Message::parse(&raw_message).unwrap();
println!("{}", serde_yaml::to_string(&message).unwrap());
}
}
+1
View File
@@ -250,6 +250,7 @@
//!```
#[forbid(unsafe_code)]
pub mod decoders;
pub mod mailbox;
pub mod parsers;
use std::{
+301
View File
@@ -0,0 +1,301 @@
/*
* Copyright Stalwart Labs 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 std::{
fs, io,
path::{Path, PathBuf},
};
/// Maildir folder iterator
pub struct FolderIterator {
inbox: Option<MessageIterator>,
it: fs::ReadDir,
}
/// Maildir message iterator
pub struct MessageIterator {
name: Option<String>,
cur_it: fs::ReadDir,
new_it: fs::ReadDir,
}
/// Maildir message contents and metadata
#[derive(Debug, PartialEq, Eq, Clone, PartialOrd, Ord)]
pub struct Message {
internal_date: u64,
flags: Vec<Flag>,
contents: Vec<u8>,
}
/// Flags of Maildir message
#[derive(Debug, PartialEq, Eq, Clone, Copy, PartialOrd, Ord)]
pub enum Flag {
Passed,
Replied,
Seen,
Trashed,
Draft,
Flagged,
}
impl FolderIterator {
/// Creates a new Maildir folder iterator
pub fn new(path: impl Into<PathBuf>) -> io::Result<FolderIterator> {
let path = path.into();
Ok(FolderIterator {
it: fs::read_dir(&path)?,
inbox: MessageIterator::new_(&path, None)?.into(),
})
}
}
impl MessageIterator {
/// Creates a new Maildir message iterator
pub fn new(path: impl Into<PathBuf>) -> io::Result<MessageIterator> {
MessageIterator::new_(&path.into(), None)
}
fn new_(path: &Path, name: Option<String>) -> io::Result<MessageIterator> {
let mut cur_path = path.to_path_buf();
cur_path.push("cur");
if !cur_path.exists() {
return Err(io::Error::new(
io::ErrorKind::NotFound,
"Invalid Maildir format, 'cur' directory not found.",
));
}
let mut new_path = path.to_path_buf();
new_path.push("new");
if !new_path.exists() {
return Err(io::Error::new(
io::ErrorKind::NotFound,
"Invalid Maildir format, 'new' directory not found.",
));
}
Ok(MessageIterator {
name,
cur_it: fs::read_dir(cur_path)?,
new_it: fs::read_dir(new_path)?,
})
}
/// Returns the mailbox name of None for 'INBOX'.
pub fn name(&self) -> Option<&str> {
self.name.as_deref()
}
}
impl Iterator for FolderIterator {
type Item = io::Result<MessageIterator>;
fn next(&mut self) -> Option<Self::Item> {
if let Some(inbox) = self.inbox.take() {
return Some(Ok(inbox));
}
loop {
let entry = match self.it.next() {
Some(Ok(entry)) => entry,
Some(Err(err)) => return Some(Err(err)),
None => return None,
};
let path = entry.path();
if path.is_dir() {
if let Some(name) = path
.file_name()
.and_then(|name| name.to_str())
.and_then(|name| name.strip_prefix('.'))
{
match MessageIterator::new_(&path, Some(name.to_string())) {
Ok(folder) => return Some(Ok(folder)),
Err(err) => {
if err.kind() != io::ErrorKind::NotFound {
return Some(Err(err));
}
}
}
}
}
}
}
}
impl Iterator for MessageIterator {
type Item = io::Result<Message>;
fn next(&mut self) -> Option<Self::Item> {
loop {
let entry = match self.cur_it.next().or_else(|| self.new_it.next()) {
Some(Ok(entry)) => entry,
Some(Err(err)) => return Some(Err(err)),
None => return None,
};
let path = entry.path();
if path.is_file() {
if let Some(name) = path.file_name().and_then(|name| name.to_str()) {
if !name.starts_with('.') {
let internal_date =
match fs::metadata(&path).and_then(|m| m.created()).and_then(|d| {
d.duration_since(std::time::UNIX_EPOCH)
.map(|d| d.as_secs())
.map_err(|e| {
io::Error::new(io::ErrorKind::InvalidData, e.to_string())
})
}) {
Ok(metadata) => metadata,
Err(err) => return Some(Err(err)),
};
let contents = match fs::read(&path) {
Ok(contents) => contents,
Err(err) => return Some(Err(err)),
};
let mut flags = Vec::new();
if let Some((_, part)) = name.rsplit_once("2,") {
for &ch in part.as_bytes() {
match ch {
b'P' => flags.push(Flag::Passed),
b'R' => flags.push(Flag::Replied),
b'S' => flags.push(Flag::Seen),
b'T' => flags.push(Flag::Trashed),
b'D' => flags.push(Flag::Draft),
b'F' => flags.push(Flag::Flagged),
_ => {
if !ch.is_ascii_alphanumeric() {
break;
}
}
}
}
}
return Some(Ok(Message {
contents,
internal_date,
flags,
}));
}
}
}
}
}
}
impl Message {
pub fn new(internal_date: u64, flags: Vec<Flag>, contents: Vec<u8>) -> Message {
Message {
internal_date,
flags,
contents,
}
}
/// Returns the message creation date in seconds since UNIX epoch
pub fn internal_date(&self) -> u64 {
self.internal_date
}
/// Returns the message flags
pub fn flags(&self) -> &[Flag] {
&self.flags
}
/// Returns the message contents
pub fn contents(&self) -> &[u8] {
&self.contents
}
/// Unwraps the message contents
pub fn unwrap_contents(self) -> Vec<u8> {
self.contents
}
}
#[cfg(test)]
mod tests {
use std::path::PathBuf;
use crate::mailbox::maildir::{Flag, Message};
use super::FolderIterator;
#[test]
fn parse_maildir() {
let mut test_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
test_dir.push("tests");
test_dir.push("maildir");
let mut messages = Vec::new();
let expected_messages = vec![
(
"INBOX".to_string(),
Message {
internal_date: 1660133582,
flags: vec![Flag::Seen, Flag::Trashed],
contents: vec![97, 10],
},
),
(
"INBOX".to_string(),
Message {
internal_date: 1660133743,
flags: vec![Flag::Seen],
contents: vec![98, 10],
},
),
(
"My Folder".to_string(),
Message {
internal_date: 1660133871,
flags: vec![Flag::Trashed, Flag::Draft, Flag::Replied],
contents: vec![99, 10],
},
),
(
"My Folder".to_string(),
Message {
internal_date: 1660133903,
flags: vec![],
contents: vec![100, 10],
},
),
(
"My Folder.Nested Folder".to_string(),
Message {
internal_date: 1660133935,
flags: vec![Flag::Flagged, Flag::Passed],
contents: vec![101, 10],
},
),
(
"My Folder.Nested Folder".to_string(),
Message {
internal_date: 1660133987,
flags: vec![Flag::Replied, Flag::Draft, Flag::Flagged],
contents: vec![102, 10],
},
),
];
for folder in FolderIterator::new(test_dir).unwrap() {
let folder = folder.unwrap();
let name = folder.name().unwrap_or("INBOX").to_string();
for message in folder {
messages.push((name.clone(), message.unwrap()));
}
}
messages.sort_unstable();
assert_eq!(messages, expected_messages);
}
}
+30 -21
View File
@@ -11,42 +11,51 @@
use std::io::{BufRead, BufReader, Read};
/// Parses an MBox mailbox from a `Read` stream, returning each message as a
/// Parses an Mbox mailbox from a `Read` stream, returning each message as a
/// `Vec<u8>`.
/// supports >From quoting as defined in the [QMail mbox specification](http://qmail.org/qmail-manual-html/man5/mbox.html).
pub struct MBoxParser<T: Read> {
pub struct MboxIterator<T: Read> {
reader: BufReader<T>,
found_from: bool,
}
impl<T> MBoxParser<T>
#[derive(Debug)]
pub struct ParseError {}
impl<T> MboxIterator<T>
where
T: Read,
{
pub fn new(reader: T) -> MBoxParser<T> {
MBoxParser {
pub fn new(reader: T) -> MboxIterator<T> {
MboxIterator {
reader: BufReader::new(reader),
found_from: false,
}
}
}
impl<T> Iterator for MBoxParser<T>
impl<T> Iterator for MboxIterator<T>
where
T: Read,
{
type Item = Vec<u8>;
type Item = Result<Vec<u8>, ParseError>;
fn next(&mut self) -> Option<Vec<u8>> {
fn next(&mut self) -> Option<Self::Item> {
let mut message = Vec::with_capacity(1024);
let mut message_line = Vec::with_capacity(80);
while self
.reader
.read_until(b'\n', &mut message_line)
.expect("Error while reading Mbox")
> 0
{
loop {
match self.reader.read_until(b'\n', &mut message_line) {
Ok(bytes_read) => {
if bytes_read == 0 {
break;
}
}
Err(_) => {
return Some(Err(ParseError {}));
}
}
let is_from = message_line
.get(..5)
.map(|line| line == b"From ")
@@ -81,7 +90,7 @@ where
}
if !message.is_empty() {
Some(message)
Some(Ok(message))
} else {
None
}
@@ -90,7 +99,7 @@ where
#[cfg(test)]
mod tests {
use super::MBoxParser;
use super::MboxIterator;
#[test]
fn parse_mbox() {
@@ -112,15 +121,15 @@ Message 4
>F
"#;
let mut parser = MBoxParser::new(&message[..]);
let mut parser = MboxIterator::new(&message[..]);
assert_eq!(parser.next().unwrap(), b"Message 1\n\n");
assert_eq!(parser.next().unwrap(), b"Message 2\n\n");
assert_eq!(parser.next().unwrap().unwrap(), b"Message 1\n\n");
assert_eq!(parser.next().unwrap().unwrap(), b"Message 2\n\n");
assert_eq!(
parser.next().unwrap(),
parser.next().unwrap().unwrap(),
b"Message 3\nFrom hello\n>From world\n>>From test\n\n"
);
assert_eq!(parser.next().unwrap(), b"Message 4\n> From\n>F\n");
assert_eq!(parser.next().unwrap().unwrap(), b"Message 4\n> From\n>F\n");
assert!(parser.next().is_none());
}
}
+2
View File
@@ -0,0 +1,2 @@
pub mod maildir;
pub mod mbox;
-1
View File
@@ -11,7 +11,6 @@
pub mod fields;
pub mod header;
pub mod mbox;
pub mod message;
pub mod mime;
pub mod preview;