Remove the Response from the events callback

Events, also known as reports, do not generate responses. Responses must
therefore be limited to filters, which are not implemented yet.
This commit is contained in:
Rodolphe Breard 2019-01-12 12:58:01 +01:00
parent 82fe5e24de
commit dd7f4d1a86
2 changed files with 26 additions and 15 deletions

View file

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

View file

@ -1,5 +1,4 @@
use crate::entry::{Entry, Event}; use crate::entry::{Entry, Event};
use crate::Response;
use std::str::FromStr; use std::str::FromStr;
#[derive(Clone, Debug, PartialEq)] #[derive(Clone, Debug, PartialEq)]
@ -10,9 +9,9 @@ pub enum MatchEvent {
#[derive(Clone)] #[derive(Clone)]
pub enum Callback<T> { pub enum Callback<T> {
NoCtx(fn(&Entry) -> Response), NoCtx(fn(&Entry)),
Ctx(fn(&T, &Entry) -> Response), Ctx(fn(&T, &Entry)),
CtxMut(fn(&mut T, &Entry) -> Response), CtxMut(fn(&mut T, &Entry)),
} }
#[derive(Clone)] #[derive(Clone)]
@ -52,14 +51,11 @@ impl<T: Clone + Default> EventHandler<T> {
} }
pub fn call(&self, entry: &Entry, context: &mut T) { pub fn call(&self, entry: &Entry, context: &mut T) {
let ret = match self.callback { match self.callback {
Callback::NoCtx(f) => f(entry), Callback::NoCtx(f) => f(entry),
Callback::Ctx(f) => f(context, entry), Callback::Ctx(f) => f(context, entry),
Callback::CtxMut(f) => f(context, entry), Callback::CtxMut(f) => f(context, entry),
}; };
match ret {
Response::None => {}
}
} }
} }
@ -68,10 +64,27 @@ mod test {
use crate::*; use crate::*;
#[test] #[test]
fn test_eventhandler_build() { fn test_eventhandler_build_noctx() {
// TODO: Remove the ::<NoContext>
EventHandler::new( EventHandler::new(
"Any", "Any",
|_context: &mut NoContext, _entry: &Entry| -> Response { Response::None }, Callback::NoCtx::<NoContext>(|_entry: &Entry| {}),
);
}
#[test]
fn test_eventhandler_build_ctx() {
EventHandler::new(
"Any",
Callback::Ctx(|_context: &NoContext, _entry: &Entry| {}),
);
}
#[test]
fn test_eventhandler_build_ctxmut() {
EventHandler::new(
"Any",
Callback::CtxMut(|_context: &mut NoContext, _entry: &Entry| {}),
); );
} }
} }