From 4ed46092723f03cf840897df02ba75a9f3a8fee8 Mon Sep 17 00:00:00 2001 From: Rodolphe Breard Date: Sun, 6 Jan 2019 16:03:49 +0100 Subject: [PATCH] Add a Response object This object will abstract the filter response. For now, it only allow not to respond. This will change in a future version. --- opensmtpd/examples/dummy.rs | 10 +++++----- opensmtpd/src/event_handlers.rs | 11 +++++++---- opensmtpd/src/lib.rs | 4 ++++ 3 files changed, 16 insertions(+), 9 deletions(-) diff --git a/opensmtpd/examples/dummy.rs b/opensmtpd/examples/dummy.rs index e8a4908..593b455 100644 --- a/opensmtpd/examples/dummy.rs +++ b/opensmtpd/examples/dummy.rs @@ -1,17 +1,17 @@ use env_logger::{Builder, Env}; use log::{debug, info}; -use opensmtpd::{event, handlers, Entry, SmtpIn}; +use opensmtpd::{event, handlers, Entry, Response, SmtpIn}; #[event(Any)] -fn on_event(entry: &Entry) -> bool { +fn on_event(entry: &Entry) -> Response { debug!("Event received: {:?}", entry); - true + Response::None } #[event(LinkConnect)] -fn on_connect(entry: &Entry) -> bool { +fn on_connect(entry: &Entry) -> Response { info!("New client on session {:x}.", entry.session_id); - true + Response::None } fn main() { diff --git a/opensmtpd/src/event_handlers.rs b/opensmtpd/src/event_handlers.rs index 152401d..8e54d5f 100644 --- a/opensmtpd/src/event_handlers.rs +++ b/opensmtpd/src/event_handlers.rs @@ -1,4 +1,5 @@ use crate::entry::{Entry, Event}; +use crate::Response; #[derive(Clone, Debug, PartialEq)] pub enum MatchEvent { @@ -9,7 +10,7 @@ pub enum MatchEvent { #[derive(Clone)] pub struct EventHandler { event: MatchEvent, - callback: (fn(&Entry) -> bool), + callback: (fn(&Entry) -> Response), } impl EventHandler { @@ -31,7 +32,7 @@ impl EventHandler { MatchEvent::Evt(events) } - pub fn new(event_str: String, callback: (fn(&Entry) -> bool)) -> Self { + pub fn new(event_str: String, callback: (fn(&Entry) -> Response)) -> Self { EventHandler { event: EventHandler::get_events_from_string(&event_str), callback, @@ -45,7 +46,9 @@ impl EventHandler { } } - pub fn call(&self, entry: &Entry) -> bool { - (self.callback)(entry) + pub fn call(&self, entry: &Entry) { + match (self.callback)(entry) { + Response::None => {} + }; } } diff --git a/opensmtpd/src/lib.rs b/opensmtpd/src/lib.rs index 7a85e91..9d59d7d 100644 --- a/opensmtpd/src/lib.rs +++ b/opensmtpd/src/lib.rs @@ -26,6 +26,10 @@ macro_rules! handlers { }; } +pub enum Response { + None, +} + struct SessionHandler { entry_rx: mpsc::Receiver, event_handlers: Vec,