From 1945abfc2a339a9b9dc919a4cf71f80505e6283f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rodolphe=20Br=C3=A9ard?= Date: Sun, 19 Mar 2023 20:32:11 +0100 Subject: [PATCH] Aggregate entries in messages --- src/entry.rs | 4 ++++ src/main.rs | 27 +++++++++++++++++++++++---- src/message.rs | 24 ++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 4 deletions(-) create mode 100644 src/message.rs diff --git a/src/entry.rs b/src/entry.rs index 81ca0fc..72dee66 100644 --- a/src/entry.rs +++ b/src/entry.rs @@ -9,6 +9,10 @@ pub struct Entry { } impl Entry { + pub fn get_msg_id(&self) -> String { + format!("{}.{}", self.session_id, self.token) + } + pub fn get_session_id(&self) -> &str { &self.session_id } diff --git a/src/main.rs b/src/main.rs index 90be7d6..7cf1d02 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,23 +1,42 @@ mod entry; mod handshake; +mod message; mod stdin_reader; use entry::Entry; +use message::Message; +use std::collections::HashMap; use stdin_reader::StdinReader; const DEFAULT_BUFF_SIZE: usize = 1024; fn main() { let mut reader = StdinReader::new(); + let mut messages: HashMap = HashMap::new(); handshake::read_config(&mut reader); handshake::register_filter(); loop { match Entry::from_bytes(&reader.read_line()) { Ok(entry) => { - if !entry.is_end_of_message() { - println!("Debug: {entry:?}"); - } else { - println!("Debug: end of message: {entry:?}"); + let msg_id = entry.get_msg_id(); + match messages.get_mut(&msg_id) { + Some(msg) => { + if !entry.is_end_of_message() { + msg.append_line(entry.get_data()); + } else { + msg.sign_and_return(); + messages.remove(&msg_id); + } + } + None => { + 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(); + } + } } } Err(err) => { diff --git a/src/message.rs b/src/message.rs new file mode 100644 index 0000000..f1514e1 --- /dev/null +++ b/src/message.rs @@ -0,0 +1,24 @@ +#[derive(Debug)] +pub struct Message { + 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 append_line(&mut self, line: &[u8]) { + self.lines.push(line.to_vec()) + } + + pub fn sign_and_return(&self) { + // TODO: sign the message using DKIM and send it to stdout + } +}