Aggregate entries in messages

This commit is contained in:
Rodolphe Bréard 2023-03-19 20:32:11 +01:00
parent 420a23ad35
commit 1945abfc2a
3 changed files with 51 additions and 4 deletions

View file

@ -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
}

View file

@ -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<String, Message> = 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) => {

24
src/message.rs Normal file
View file

@ -0,0 +1,24 @@
#[derive(Debug)]
pub struct Message {
lines: Vec<Vec<u8>>,
}
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
}
}