From 24c0253f882e3ee4d5dd0f21a74a166031b0983b Mon Sep 17 00:00:00 2001 From: Rodolphe Breard Date: Tue, 17 Sep 2019 19:38:41 +0200 Subject: [PATCH] Implement the register_events method --- opensmtpd/src/handler.rs | 6 ++--- opensmtpd/src/lib.rs | 52 +++++++++++++++++++++++++++++++++++++--- 2 files changed, 52 insertions(+), 6 deletions(-) diff --git a/opensmtpd/src/handler.rs b/opensmtpd/src/handler.rs index cb41b81..10a2d51 100644 --- a/opensmtpd/src/handler.rs +++ b/opensmtpd/src/handler.rs @@ -29,9 +29,9 @@ type Callback = fn(&mut dyn FilterOutput, &Entry) -> Result<(), String>; #[derive(Clone)] pub struct Handler { version: Version, - kind: Kind, - subsystem: Subsystem, - events: HashSet, + pub(crate) kind: Kind, + pub(crate) subsystem: Subsystem, + pub(crate) events: HashSet, action: Callback, } diff --git a/opensmtpd/src/lib.rs b/opensmtpd/src/lib.rs index d204577..a836cd5 100644 --- a/opensmtpd/src/lib.rs +++ b/opensmtpd/src/lib.rs @@ -14,7 +14,9 @@ pub mod entry; pub mod input; pub mod output; +use crate::entry::{Kind, Subsystem}; use log; +use std::collections::HashSet; use std::default::Default; pub use crate::errors::Error; @@ -43,6 +45,32 @@ macro_rules! simple_filter_log_level { }; } +macro_rules! fatal_error { + ($error: ident) => { + log::error!("Error: {}", $error); + std::process::exit(1); + }; +} + +macro_rules! insert_events { + ($handler: ident, $set: ident) => {{ + for e in $handler.events.iter() { + $set.insert(e); + } + }}; +} + +macro_rules! register_events { + ($output: expr, $set: ident, $kind: expr, $subsystem: expr) => { + for e in $set.iter() { + let msg = format!("register|{}|{}|{}", $kind, $subsystem, e.to_string()); + if let Err(e) = $output.send(&msg) { + fatal_error!(e); + }; + } + }; +} + pub struct Filter where I: crate::input::FilterInput + Default, @@ -78,7 +106,26 @@ where } pub fn register_events(&mut self) -> &mut Self { - // TODO: use self.output to register events + let mut report_smtp_in = HashSet::new(); + let mut report_smtp_out = HashSet::new(); + let mut filter_smtp_in = HashSet::new(); + let mut filter_smtp_out = HashSet::new(); + for h in self.handlers.iter() { + match h.kind { + Kind::Report => match h.subsystem { + Subsystem::SmtpIn => insert_events!(h, report_smtp_in), + Subsystem::SmtpOut => insert_events!(h, report_smtp_out), + }, + Kind::Filter => match h.subsystem { + Subsystem::SmtpIn => insert_events!(h, filter_smtp_in), + Subsystem::SmtpOut => insert_events!(h, filter_smtp_out), + }, + }; + } + register_events!(self.output, report_smtp_in, "report", "smtp-in"); + register_events!(self.output, report_smtp_out, "report", "smtp-out"); + register_events!(self.output, filter_smtp_in, "filter", "smtp-in"); + register_events!(self.output, filter_smtp_out, "filter", "smtp-out"); self } @@ -97,8 +144,7 @@ where } } Err(e) => { - log::error!("Error: {}", e); - std::process::exit(1); + fatal_error!(e); } }; }