milter-callback 0.1.5

Attribute macros for milter callback generation
Documentation

Procedural macros to generate C callback functions for use in milter implementation.

The attribute macros in this crate facilitate the creation of FFI callback functions that are required for the configuration of a Milter. The attribute macros are used to annotate ordinary Rust functions as milter callbacks. A C function is then generated that delegates to the Rust callback, safely, and taking care of conversion between Rust/C types.

Callback functions serve the purpose of event handlers (hence the nomenclature on_*) for the various ‘events’ that happen during an SMTP conversation. For each of the stages in the milter protocol there is a corresponding attribute macro.

Usage

This crate is a dependency of the milter crate, which re-exports all macros under its namespace. Thus you should not need to use this crate directly.

Note that with Rust 2018 there are two ways of importing procedural macros. Nowadays, macros can be imported like other symbols:

use milter::{on_connect, on_close, Milter, Status};

That does require you to list all macros in the use statement, though. The older syntax with extern crate lets you import all macros without listing them explicitly:

#[macro_use] extern crate milter;

use milter::{Milter, Status};

Both notations are acceptable.

Raw string inputs

By default, callbacks receive string inputs of type &str, that is, UTF-8 strings. Where UTF-8 encoding is not desired, it is possible to substitute the byte slice type &[u8] for &str in your handler function signature in order to receive the raw bytes instead.

Contrast the following example with the one shown at on_header.

# #[macro_use] extern crate milter_callback;
# use milter::{Context, Status};
#[on_header(header_callback)]
fn handle_header(context: Context<()>, name: &[u8], value: &[u8]) -> Status {
//                                       ^^^^^         ^^^^^
Status::Continue
}

This feature is supported wherever &str appears in callback function arguments.

Callback results

The return type of a callback function may be wrapped in a milter::Result where desired. This is a convenience: as most Context methods return milter::Results these can then be unwrapped with the ? operator.

Compare the following example with the one shown at on_eom. This code fragment also demonstrates the use of the ? operator enabled by choosing this return type.

# #[macro_use] extern crate milter_callback;
# use milter::{ActionContext, Status};
#[on_eom(eom_callback)]
fn handle_eom(context: ActionContext<()>) -> milter::Result<Status> {
//                                       ^^^^^^^^^^^^^^^^^^^^^^
if let Some(version) = context.macro_value("v")? {
println!("{}", version);
}

Ok(Status::Continue)
}

This feature is supported on all callback functions.

Failure modes

An Err result returned from a callback leads to a temporary failure (Status::Tempfail) response being returned to the MTA. The milter then continues to handle requests normally.

Panicking, on the other hand, leads to immediate shutdown of the milter. All stages switch to returning a failure response and no longer execute the handler functions (however, currently executing callback handlers are allowed to finish). The milter library worker processes are terminated and the currently blocked invocation of Milter::run returns. Cleanup logic in the close or other stages is not executed.

The principle behind the panicking behaviour is, as elsewhere, exit as quickly as possible, within the constraints posed by the milter library and the FFI interface.

The above failure modes are provided as a convenience. Use explicit error handling if they don’t satisfy your requirements.