This repository has been archived on 2023-09-20. You can view files and clone it, but cannot push or open issues or pull requests.
rust-opensmtpd/opensmtpd/examples/counter.rs

56 lines
1.1 KiB
Rust
Raw Permalink Normal View History

use log;
2020-12-20 17:24:08 +01:00
use opensmtpd::{run_filter, Address, Filter, ReportEntry};
use opensmtpd_derive::register;
2020-12-20 14:31:09 +01:00
use simplelog::{Config, LevelFilter, WriteLogger};
use std::fs::File;
pub const DEFAULT_LOG_FILE: &str = "/tmp/counter.log";
#[derive(Default)]
struct MyCounter {
2021-02-28 13:56:40 +01:00
nb_connected: u64,
nb_total: u64,
}
impl Filter for MyCounter {
2021-02-28 13:56:40 +01:00
#[register]
fn on_report_link_connect(
&mut self,
_entry: &ReportEntry,
_rdns: &str,
_fcrdns: &str,
_src: &Address,
_dest: &Address,
) {
self.nb_connected += 1;
self.nb_total += 1;
log::info!(
"New client (connected: {}, total: {})",
self.nb_connected,
self.nb_total
);
}
2021-02-28 13:56:40 +01:00
#[register]
fn on_report_link_disconnect(&mut self, _entry: &ReportEntry) {
self.nb_connected -= 1;
log::info!(
"Client left (connected: {}, total: {})",
self.nb_connected,
self.nb_total
);
}
}
fn main() {
2021-02-28 13:56:40 +01:00
let log_file = std::env::var("LOG_FILE").unwrap_or(String::from(DEFAULT_LOG_FILE));
WriteLogger::init(
LevelFilter::Info,
Config::default(),
File::create(&log_file).unwrap(),
)
.unwrap();
let mut my_counter: MyCounter = Default::default();
run_filter(&mut my_counter);
}