From fdc915cd663fb1c2f2f5978c46aa18ae3cb69cf2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rodolphe=20Br=C3=A9ard?= Date: Sun, 19 Mar 2023 21:42:36 +0100 Subject: [PATCH] Write the messages to stdout --- src/main.rs | 3 +-- src/message.rs | 41 +++++++++++++++++++++++++++++++++-------- 2 files changed, 34 insertions(+), 10 deletions(-) diff --git a/src/main.rs b/src/main.rs index 7cf1d02..1fa9d4f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -29,11 +29,10 @@ fn main() { } } None => { + let msg = Message::from_entry(&entry); if !entry.is_end_of_message() { - let msg = Message::from_line(entry.get_data()); messages.insert(msg_id, msg); } else { - let msg = Message::new(); msg.sign_and_return(); } } diff --git a/src/message.rs b/src/message.rs index f1514e1..2571915 100644 --- a/src/message.rs +++ b/src/message.rs @@ -1,17 +1,27 @@ +use crate::entry::Entry; +use std::io::{BufWriter, Write}; + +pub const RETURN_SEP: &[u8] = b"|"; +pub const RETURN_START: &[u8] = b"filter-dataline|"; + #[derive(Debug)] pub struct Message { + session_id: String, + token: String, lines: Vec>, } impl Message { - pub fn new() -> Self { - Self { lines: Vec::new() } - } - - pub fn from_line(line: &[u8]) -> Self { - Self { - lines: vec![line.to_vec()], + pub fn from_entry(entry: &Entry) -> Self { + let mut ret = Self { + session_id: entry.get_session_id().to_string(), + token: entry.get_token().to_string(), + lines: Vec::new(), + }; + if !entry.is_end_of_message() { + ret.append_line(entry.get_data()); } + ret } pub fn append_line(&mut self, line: &[u8]) { @@ -19,6 +29,21 @@ impl Message { } pub fn sign_and_return(&self) { - // TODO: sign the message using DKIM and send it to stdout + // TODO: sign the message using DKIM + for line in &self.lines { + self.print_line(line); + } + self.print_line(b"."); + } + + fn print_line(&self, line: &[u8]) { + let mut stdout = BufWriter::new(std::io::stdout()); + stdout.write_all(RETURN_START).unwrap(); + stdout.write_all(self.session_id.as_bytes()).unwrap(); + stdout.write_all(RETURN_SEP).unwrap(); + stdout.write_all(self.token.as_bytes()).unwrap(); + stdout.write_all(RETURN_SEP).unwrap(); + stdout.write_all(line).unwrap(); + stdout.write_all(b"\n").unwrap(); } }