Allow the use of custom context

The main goal of events/reports is to update a context object, which
will be used in filters to generate a response. It is now possible to
use any object implementing both Clone and Default as a context object.
It is also possible to define no context at all.
This commit is contained in:
Rodolphe Breard 2019-01-12 23:43:02 +01:00
parent dd7f4d1a86
commit 4b1f99db7e
6 changed files with 75 additions and 18 deletions

View file

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

View file

@ -0,0 +1,19 @@
use env_logger::{Builder, Env};
use log::info;
use opensmtpd::{event, handlers, Entry, SmtpIn};
#[derive(Clone, Default)]
struct MyContext {
nb: usize,
}
#[event(Any)]
fn on_event(ctx: &mut MyContext, entry: &Entry) {
ctx.nb += 1;
info!("Event received: {}, {}", entry.session_id, ctx.nb);
}
fn main() {
Builder::from_env(Env::default().default_filter_or("debug")).init();
SmtpIn::new().event_handlers(handlers!(on_event)).run();
}