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.
This commit is contained in:
Rodolphe Breard 2019-01-06 16:03:49 +01:00
parent 21efb88331
commit 4ed4609272
3 changed files with 16 additions and 9 deletions

View file

@ -1,17 +1,17 @@
use env_logger::{Builder, Env}; use env_logger::{Builder, Env};
use log::{debug, info}; use log::{debug, info};
use opensmtpd::{event, handlers, Entry, SmtpIn}; use opensmtpd::{event, handlers, Entry, Response, SmtpIn};
#[event(Any)] #[event(Any)]
fn on_event(entry: &Entry) -> bool { fn on_event(entry: &Entry) -> Response {
debug!("Event received: {:?}", entry); debug!("Event received: {:?}", entry);
true Response::None
} }
#[event(LinkConnect)] #[event(LinkConnect)]
fn on_connect(entry: &Entry) -> bool { fn on_connect(entry: &Entry) -> Response {
info!("New client on session {:x}.", entry.session_id); info!("New client on session {:x}.", entry.session_id);
true Response::None
} }
fn main() { fn main() {

View file

@ -1,4 +1,5 @@
use crate::entry::{Entry, Event}; use crate::entry::{Entry, Event};
use crate::Response;
#[derive(Clone, Debug, PartialEq)] #[derive(Clone, Debug, PartialEq)]
pub enum MatchEvent { pub enum MatchEvent {
@ -9,7 +10,7 @@ pub enum MatchEvent {
#[derive(Clone)] #[derive(Clone)]
pub struct EventHandler { pub struct EventHandler {
event: MatchEvent, event: MatchEvent,
callback: (fn(&Entry) -> bool), callback: (fn(&Entry) -> Response),
} }
impl EventHandler { impl EventHandler {
@ -31,7 +32,7 @@ impl EventHandler {
MatchEvent::Evt(events) 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 { EventHandler {
event: EventHandler::get_events_from_string(&event_str), event: EventHandler::get_events_from_string(&event_str),
callback, callback,
@ -45,7 +46,9 @@ impl EventHandler {
} }
} }
pub fn call(&self, entry: &Entry) -> bool { pub fn call(&self, entry: &Entry) {
(self.callback)(entry) match (self.callback)(entry) {
Response::None => {}
};
} }
} }

View file

@ -26,6 +26,10 @@ macro_rules! handlers {
}; };
} }
pub enum Response {
None,
}
struct SessionHandler { struct SessionHandler {
entry_rx: mpsc::Receiver<Entry>, entry_rx: mpsc::Receiver<Entry>,
event_handlers: Vec<EventHandler>, event_handlers: Vec<EventHandler>,