diff --git a/opensmtpd/Cargo.toml b/opensmtpd/Cargo.toml index 17cc838..c30ee80 100644 --- a/opensmtpd/Cargo.toml +++ b/opensmtpd/Cargo.toml @@ -20,6 +20,6 @@ opensmtpd_derive = { path = "../opensmtpd-derive", version = "0.2" } name = "echo" path = "examples/echo.rs" -#[[example]] -#name = "counter" -#path = "examples/session_event_counter.rs" +[[example]] +name = "counter" +path = "examples/session_event_counter.rs" diff --git a/opensmtpd/examples/echo.rs b/opensmtpd/examples/echo.rs index 14a0ff8..27fcf48 100644 --- a/opensmtpd/examples/echo.rs +++ b/opensmtpd/examples/echo.rs @@ -1,6 +1,11 @@ use opensmtpd::entry::Entry; use opensmtpd::{report, simple_filter}; +#[derive(Clone, Default)] +struct MyContext { + nb: usize, +} + #[report(v1, smtp_in, match(all))] fn echo_handler(entry: &Entry) -> Result<(), String> { log::info!("TEST ENTRY: {:?}", entry); @@ -14,5 +19,5 @@ fn test(entry: &Entry) { } fn main() { - simple_filter!(echo_handler, test); + simple_filter!(MyContext, [echo_handler, test]); } diff --git a/opensmtpd/examples/session_event_counter.rs b/opensmtpd/examples/session_event_counter.rs index 3dd93cb..b062bc1 100644 --- a/opensmtpd/examples/session_event_counter.rs +++ b/opensmtpd/examples/session_event_counter.rs @@ -1,3 +1,5 @@ +use log; +use opensmtpd::entry::Entry; use opensmtpd::{report, simple_filter}; #[derive(Clone, Default)] @@ -5,12 +7,13 @@ struct MyContext { nb: usize, } -#[report(Any)] +#[report(v1, smtp_in, match(all))] fn on_report(ctx: &mut MyContext, entry: &Entry) { ctx.nb += 1; - info!("Event received: {}, {}", entry.get_session_id(), ctx.nb); + log::info!("Event received: {}, {}", entry.get_session_id(), ctx.nb); + Ok(()) } fn main() { - simple_filter!(vec![on_report]); + simple_filter!(MyContext, [on_report]); } diff --git a/opensmtpd/src/lib.rs b/opensmtpd/src/lib.rs index a836cd5..972458d 100644 --- a/opensmtpd/src/lib.rs +++ b/opensmtpd/src/lib.rs @@ -26,22 +26,35 @@ pub use opensmtpd_derive::report; #[macro_export] macro_rules! simple_filter { - ($( $x:expr ),*) => { - opensmtpd::simple_filter_log_level!(log::Level::Info $(,$x)*); + ($handlers: expr) => { + opensmtpd::simple_filter!( + log::Level::Info, + opensmtpd::NoContext, + opensmtpd::NoContext, + $handlers + ); }; -} - -#[macro_export] -macro_rules! simple_filter_log_level { - ($log_level: expr, $( $x:expr ),*) => { - let mut handlers = Vec::new(); - $( - handlers.push(($x)()); - )*; - let _ = opensmtpd::SmtpdLogger::new() - .set_level($log_level) - .init(); - opensmtpd::Filter::::default().set_handlers(&handlers).register_events().run(); + ($filter_ctx: ty, $handlers: expr) => { + opensmtpd::simple_filter!( + log::Level::Info, + opensmtpd::NoContext, + $filter_ctx, + $handlers + ); + }; + ($sesion_ctx: ty, $filter_ctx: ty, $handlers: expr) => { + opensmtpd::simple_filter!(log::Level::Info, $sesion_ctx, $filter_ctx, $handlers); + }; + ($log_level: path, $sesion_ctx: ty, $filter_ctx: ty, $handlers: expr) => { + let handlers = ($handlers) + .iter() + .map(|f| f()) + .collect::>(); + let _ = opensmtpd::SmtpdLogger::new().set_level($log_level).init(); + opensmtpd::Filter::::default() + .set_handlers(handlers.as_slice()) + .register_events() + .run(); }; } @@ -71,6 +84,9 @@ macro_rules! register_events { }; } +#[derive(Default)] +pub struct NoContext; + pub struct Filter where I: crate::input::FilterInput + Default,