From d95daff90c8a6690f43b9f2ec91b63001fc54878 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rodolphe=20Br=C3=A9ard?= Date: Sat, 25 Mar 2023 15:42:46 +0100 Subject: [PATCH] Parse the message --- Cargo.toml | 1 + src/message.rs | 34 +++++++++++++++++++++++++++++++++- 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index b0ead3d..ce75883 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,4 +10,5 @@ publish = false [dependencies] env_logger = "^0.10" log = "^0.4" +mailparse = "^0.14" nom = "^7.1" diff --git a/src/message.rs b/src/message.rs index e226630..4cb78e1 100644 --- a/src/message.rs +++ b/src/message.rs @@ -1,4 +1,5 @@ use crate::entry::Entry; +use mailparse::parse_mail; use std::io::{BufWriter, Write}; pub const RETURN_SEP: &[u8] = b"|"; @@ -50,7 +51,38 @@ impl Message { } pub fn sign_and_return(&self) { - // TODO: sign the message using DKIM + log::trace!("content:\n{}", crate::display_bytes!(&self.content)); + match parse_mail(&self.content) { + Ok(parsed_msg) => { + log::trace!("mail parsed"); + for h in parsed_msg.get_headers() { + log::trace!("{:?}", h); + } + match self.get_body() { + Some(body) => { + log::trace!("MailBody:\n{}", crate::display_bytes!(body)); + // TODO: sign the message using DKIM + } + None => { + log::error!("{}: unable to find the body", self.session_id); + } + } + } + Err(e) => { + log::error!("{}: unable to parse message: {e}", self.session_id); + } + }; + self.print_msg(); + } + + fn get_body(&self) -> Option<&[u8]> { + match self.content.windows(4).position(|w| w == b"\r\n\r\n") { + Some(body_index) => Some(&self.content[body_index + 4..]), + None => None, + } + } + + fn print_msg(&self) { let i = self.content.len() - 1; for line in self.content[0..i].split(|&b| b == b'\n') { self.print_line(line);