Commit
Author: Kevin Schoon [me@kevinschoon.com]
Hash: 637b2390098c75f77980a33156b942c6df4fdfb4
Timestamp: Mon, 02 Sep 2024 08:20:33 +0000 (3 months ago)

+48 -0 +/-2 browse
add simple rewrite module
1diff --git a/maitred/src/lib.rs b/maitred/src/lib.rs
2index 189649b..18658ff 100644
3--- a/maitred/src/lib.rs
4+++ b/maitred/src/lib.rs
5 @@ -58,6 +58,7 @@ pub use mail_parser;
6 pub use smtp_proto;
7
8 mod error;
9+ mod rewrite;
10 mod server;
11 mod session;
12 mod transport;
13 @@ -85,4 +86,5 @@ macro_rules! smtp_response {
14 Response::General(SmtpResponse::new($code, $e1, $e2, $e3, $name.to_string()))
15 };
16 }
17+
18 pub(crate) use smtp_response;
19 diff --git a/maitred/src/rewrite.rs b/maitred/src/rewrite.rs
20new file mode 100644
21index 0000000..4b76dac
22--- /dev/null
23+++ b/maitred/src/rewrite.rs
24 @@ -0,0 +1,46 @@
25+ use mail_parser::{Message, MessageParser};
26+
27+ /// Wrapper around raw bytes that allows for easy modification of an email.
28+ pub struct Rewrite<'a>(pub &'a mut Vec<u8>);
29+
30+ impl<'a> Rewrite<'a> {
31+ /// Prepend a header to the message body
32+ pub fn with_header(&mut self, key: &str, value: &str) {
33+ let header: Vec<u8> = format!("{}: {}", key, value).bytes().collect();
34+ self.0.splice(0..0, header);
35+ }
36+
37+ /// Return a parsed Message
38+ pub fn message(&'a self) -> Option<Message<'a>> {
39+ let message = MessageParser::default().parse(self.0.as_slice());
40+ message
41+ }
42+ }
43+
44+ #[cfg(test)]
45+ mod test {
46+ use super::*;
47+
48+ const TEST_EMAIL: &str = r#"
49+ Date: Mon, 2 Sep 2024 00:17:18 +0200
50+ From: kevin <kevin@ayllu-dev.local>
51+ To: hello@example.org
52+ Subject: Fuu
53+ Message-ID: <ewo47gsen3mimmdzlg5v4otplgiwwyogq7avzbs26lxnir3rem@xgg5xxzon6f7>
54+ MIME-Version: 1.0
55+ Content-Type: text/plain; charset=us-ascii
56+ Content-Disposition: inline
57+
58+ Hello World
59+ "#;
60+
61+ #[test]
62+ fn test_apppend_header() {
63+ let mut email_bytes = TEST_EMAIL.as_bytes().to_vec();
64+ let mut rewrite = Rewrite(&mut email_bytes);
65+ rewrite.with_header("a", "b");
66+ let message = rewrite.message().unwrap();
67+ let value = message.header("a").unwrap();
68+ assert!(value.as_text().unwrap() == "b");
69+ }
70+ }