Add filter-level and session-level contexts
This commit is contained in:
parent
995c0c35c1
commit
fdc8bd3dc4
10 changed files with 233 additions and 84 deletions
|
@ -1,23 +1,13 @@
|
|||
use opensmtpd::entry::Entry;
|
||||
use opensmtpd::{report, simple_filter};
|
||||
use opensmtpd::{register_no_context, report, simple_filter};
|
||||
|
||||
#[derive(Clone, Default)]
|
||||
struct MyContext {
|
||||
nb: usize,
|
||||
}
|
||||
register_no_context!();
|
||||
|
||||
#[report(v1, smtp_in, match(all))]
|
||||
fn echo_handler(entry: &Entry) -> Result<(), String> {
|
||||
log::info!("TEST ENTRY: {:?}", entry);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[report(v1, smtp_in, match(link_disconnect))]
|
||||
fn test(entry: &Entry) {
|
||||
log::info!("HAZ LINK DISCONNECT: {:?}", entry);
|
||||
Ok(()) // TODO: REMOVE ME!
|
||||
fn echo(entry: &Entry) {
|
||||
log::info!("New entry: {:?}", entry);
|
||||
}
|
||||
|
||||
fn main() {
|
||||
simple_filter!(MyContext, [echo_handler, test]);
|
||||
simple_filter!([echo]);
|
||||
}
|
||||
|
|
13
opensmtpd/examples/hello.rs
Normal file
13
opensmtpd/examples/hello.rs
Normal file
|
@ -0,0 +1,13 @@
|
|||
use opensmtpd::entry::Entry;
|
||||
use opensmtpd::{register_no_context, report, simple_filter};
|
||||
|
||||
register_no_context!();
|
||||
|
||||
#[report(v1, smtp_in, match(link_connect))]
|
||||
fn hello(entry: &Entry) {
|
||||
log::info!("Hello {}!", entry.get_session_id());
|
||||
}
|
||||
|
||||
fn main() {
|
||||
simple_filter!([hello]);
|
||||
}
|
26
opensmtpd/examples/report_counter.rs
Normal file
26
opensmtpd/examples/report_counter.rs
Normal file
|
@ -0,0 +1,26 @@
|
|||
use log;
|
||||
use opensmtpd::entry::Entry;
|
||||
use opensmtpd::{register_contexts, report, simple_filter};
|
||||
|
||||
#[derive(Clone, Default)]
|
||||
struct MyCounter {
|
||||
nb: usize,
|
||||
}
|
||||
|
||||
register_contexts!(MyCounter, MyCounter);
|
||||
|
||||
#[report(v1, smtp_in, match(all))]
|
||||
fn on_report(entry: &Entry, total: &mut MyCounter, session: &mut MyCounter) {
|
||||
total.nb += 1;
|
||||
session.nb += 1;
|
||||
log::info!(
|
||||
"Event received for session {}: {} (total: {})",
|
||||
entry.get_session_id(),
|
||||
session.nb,
|
||||
total.nb
|
||||
);
|
||||
}
|
||||
|
||||
fn main() {
|
||||
simple_filter!(MyCounter, MyCounter, [on_report]);
|
||||
}
|
|
@ -1,19 +0,0 @@
|
|||
use log;
|
||||
use opensmtpd::entry::Entry;
|
||||
use opensmtpd::{report, simple_filter};
|
||||
|
||||
#[derive(Clone, Default)]
|
||||
struct MyContext {
|
||||
nb: usize,
|
||||
}
|
||||
|
||||
#[report(v1, smtp_in, match(all))]
|
||||
fn on_report(ctx: &mut MyContext, entry: &Entry) {
|
||||
ctx.nb += 1;
|
||||
log::info!("Event received: {}, {}", entry.get_session_id(), ctx.nb);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn main() {
|
||||
simple_filter!(MyContext, [on_report]);
|
||||
}
|
Reference in a new issue