Refactor the library
Threads are a bad idea because for now the filter API is not guaranteed to be state-less. The interface is now synchronous, which should be enough for most filters. The refactoring brought other changes, the most important being the concept of modular input sources and output destination and the complete rewrite of the procedural macro.
This commit is contained in:
parent
988f028c23
commit
45639f18c0
18 changed files with 486 additions and 400 deletions
|
@ -1,6 +1,6 @@
|
|||
[package]
|
||||
name = "opensmtpd_derive"
|
||||
version = "0.1.0"
|
||||
version = "0.2.0"
|
||||
authors = ["Rodolphe Bréard <rodolphe@what.tf>"]
|
||||
edition = "2018"
|
||||
description = "Interface for OpenSMTPD filters"
|
||||
|
@ -15,5 +15,5 @@ include = ["src/**/*", "Cargo.toml", "LICENSE-*.txt"]
|
|||
proc-macro = true
|
||||
|
||||
[dependencies]
|
||||
quote = "0.6"
|
||||
syn = { version = "0.15", features = ["full"] }
|
||||
quote = "1.0"
|
||||
syn = { version = "1.0", features = ["full", "extra-traits"] }
|
||||
|
|
|
@ -10,70 +10,152 @@ extern crate proc_macro;
|
|||
|
||||
use proc_macro::TokenStream;
|
||||
use quote::quote;
|
||||
use syn::{parse_macro_input, ItemFn};
|
||||
use syn::parse::{Parse, ParseStream};
|
||||
use syn::punctuated::Punctuated;
|
||||
use syn::{parenthesized, parse_macro_input, ExprArray, Ident, ItemFn, Result, Token, TypePath};
|
||||
|
||||
fn get_type(
|
||||
params: &syn::punctuated::Punctuated<syn::FnArg, syn::token::Comma>,
|
||||
) -> Result<(Box<syn::Type>, syn::Type), ()> {
|
||||
match params.iter().count() {
|
||||
1 => {
|
||||
let ctx = Box::new(syn::Type::Verbatim(syn::TypeVerbatim {
|
||||
tts: quote! {
|
||||
opensmtpd::NoContext
|
||||
},
|
||||
}));
|
||||
let cb = syn::Type::Verbatim(syn::TypeVerbatim {
|
||||
tts: quote! { opensmtpd::Callback::NoCtx },
|
||||
});
|
||||
Ok((ctx, cb))
|
||||
}
|
||||
2 => match params.iter().next().unwrap() {
|
||||
syn::FnArg::Captured(ref a) => match &a.ty {
|
||||
syn::Type::Reference(r) => {
|
||||
let cb = match r.mutability {
|
||||
Some(_) => syn::Type::Verbatim(syn::TypeVerbatim {
|
||||
tts: quote! { opensmtpd::Callback::CtxMut },
|
||||
}),
|
||||
None => syn::Type::Verbatim(syn::TypeVerbatim {
|
||||
tts: quote! { opensmtpd::Callback::Ctx },
|
||||
}),
|
||||
};
|
||||
Ok((r.elem.clone(), cb))
|
||||
}
|
||||
_ => Err(()),
|
||||
},
|
||||
_ => Err(()),
|
||||
},
|
||||
_ => Err(()),
|
||||
#[derive(Debug)]
|
||||
struct OpenSmtpdAttributes {
|
||||
version: Ident,
|
||||
subsystem: Ident,
|
||||
events: Punctuated<Ident, Token![,]>,
|
||||
}
|
||||
|
||||
impl Parse for OpenSmtpdAttributes {
|
||||
fn parse(input: ParseStream) -> Result<Self> {
|
||||
let version = input.parse()?;
|
||||
let _: Token![,] = input.parse()?;
|
||||
let subsystem = input.parse()?;
|
||||
let _: Token![,] = input.parse()?;
|
||||
let _match: Token![match] = input.parse()?;
|
||||
let content;
|
||||
let _ = parenthesized!(content in input);
|
||||
let events = content.parse_terminated(Ident::parse)?;
|
||||
Ok(OpenSmtpdAttributes {
|
||||
version,
|
||||
subsystem,
|
||||
events,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[proc_macro_attribute]
|
||||
pub fn event(attr: TokenStream, input: TokenStream) -> TokenStream {
|
||||
let attr = attr.to_string();
|
||||
let item = parse_macro_input!(input as ItemFn);
|
||||
let fn_name = &item.ident;
|
||||
let fn_params = &item.decl.inputs;
|
||||
let (ctx_type, callback_type) = match get_type(fn_params) {
|
||||
Ok(t) => t,
|
||||
Err(_e) => {
|
||||
panic!();
|
||||
impl OpenSmtpdAttributes {
|
||||
fn get_version(&self) -> String {
|
||||
format!(
|
||||
"opensmtpd::entry::Version::{}",
|
||||
self.version.to_string().to_uppercase()
|
||||
)
|
||||
}
|
||||
|
||||
fn get_subsystem(&self) -> String {
|
||||
let subsystem = match self.subsystem.to_string().as_str() {
|
||||
"smtp_in" => "SmtpIn",
|
||||
"smtp_out" => "SmtpOut",
|
||||
_ => "",
|
||||
};
|
||||
format!("opensmtpd::entry::Subsystem::{}", subsystem)
|
||||
}
|
||||
|
||||
fn get_events(&self) -> String {
|
||||
let events = if self
|
||||
.events
|
||||
.iter()
|
||||
.find(|&e| e.to_string().to_lowercase().as_str() == "all")
|
||||
.is_some()
|
||||
{
|
||||
let lst = [
|
||||
"LinkAuth",
|
||||
"LinkConnect",
|
||||
"LinkDisconnect",
|
||||
"LinkIdentify",
|
||||
"LinkReset",
|
||||
"LinkTls",
|
||||
"TxBegin",
|
||||
"TxMail",
|
||||
"TxRcpt",
|
||||
"TxEnvelope",
|
||||
"TxData",
|
||||
"TxCommit",
|
||||
"TxRollback",
|
||||
"ProtocolClient",
|
||||
"ProtocolServer",
|
||||
"FilterResponse",
|
||||
"Timeout",
|
||||
];
|
||||
lst.iter()
|
||||
.map(|e| format!("opensmtpd::entry::Event::{}", e))
|
||||
.collect::<Vec<String>>()
|
||||
} else {
|
||||
self.events
|
||||
.iter()
|
||||
.map(|e| {
|
||||
let name = match e.to_string().as_str() {
|
||||
"link_auth" => "LinkAuth",
|
||||
"link_connect" => "LinkConnect",
|
||||
"link_disconnect" => "LinkDisconnect",
|
||||
"link_identify" => "LinkIdentify",
|
||||
"link_reset" => "LinkReset",
|
||||
"link_tls" => "LinkTls",
|
||||
"tx_begin" => "TxBegin",
|
||||
"tx_mail" => "TxMail",
|
||||
"tx_rcpt" => "TxRcpt",
|
||||
"tx_envelope" => "TxEnvelope",
|
||||
"tx_data" => "TxData",
|
||||
"tx_commit" => "TxCommit",
|
||||
"tx_rollback" => "TxRollback",
|
||||
"protocol_client" => "ProtocolClient",
|
||||
"protocol_server" => "ProtocolServer",
|
||||
"filter_response" => "FilterResponse",
|
||||
"timeout" => "Timeout",
|
||||
_ => "",
|
||||
};
|
||||
format!("opensmtpd::entry::Event::{}", name)
|
||||
})
|
||||
.collect::<Vec<String>>()
|
||||
};
|
||||
format!("[{}]", events.join(", "))
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! parse_item {
|
||||
($item: expr, $type: ty) => {
|
||||
match syn::parse_str::<$type>($item) {
|
||||
Ok(i) => i,
|
||||
Err(e) => {
|
||||
return TokenStream::from(e.to_compile_error());
|
||||
}
|
||||
}
|
||||
};
|
||||
let fn_body = &item.block;
|
||||
let fn_output = &item.decl.output;
|
||||
let output = quote! {
|
||||
fn #fn_name() -> opensmtpd::EventHandler<#ctx_type> {
|
||||
opensmtpd::EventHandler::new(
|
||||
#attr,
|
||||
#callback_type(|#fn_params| #fn_output #fn_body)
|
||||
)
|
||||
}
|
||||
};
|
||||
output.into()
|
||||
}
|
||||
|
||||
#[proc_macro_attribute]
|
||||
pub fn report(attr: TokenStream, input: TokenStream) -> TokenStream {
|
||||
event(attr, input)
|
||||
let attr = parse_macro_input!(attr as OpenSmtpdAttributes);
|
||||
let version = parse_item!(&attr.get_version(), TypePath);
|
||||
let subsystem = parse_item!(&attr.get_subsystem(), TypePath);
|
||||
let events = parse_item!(&attr.get_events(), ExprArray);
|
||||
let item = parse_macro_input!(input as ItemFn);
|
||||
let fn_name = &item.sig.ident;
|
||||
let fn_params = &item.sig.inputs;
|
||||
let fn_body = &item.block;
|
||||
let output = quote! {
|
||||
fn #fn_name() -> opensmtpd::Handler {
|
||||
opensmtpd::Handler::new(
|
||||
#version,
|
||||
opensmtpd::entry::Kind::Report,
|
||||
#subsystem,
|
||||
&#events,
|
||||
|_output: &mut dyn opensmtpd::output::FilterOutput, entry: &opensmtpd::entry::Entry,| {
|
||||
// TODO: look at `item.sig.output` and adapt the calling scheme.
|
||||
// example: if no return, add `Ok(())`.
|
||||
// https://docs.rs/syn/1.0.5/syn/struct.Signature.html
|
||||
let inner_fn = |#fn_params| -> Result<(), String> {
|
||||
#fn_body
|
||||
};
|
||||
inner_fn(entry)
|
||||
},
|
||||
)
|
||||
}
|
||||
};
|
||||
output.into()
|
||||
}
|
||||
|
|
Reference in a new issue