|
| 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 | +} |
0 commit comments