High level, asynchronous framework for writing mail filters
Project description
Kilter Service
Kilter is a framework for writing mail filters (known as "milters")
compatible with Sendmail and Postfix MTAs. Unlike many previous milter implementations in
Python it is not simply bindings to the libmilter
library (originally from the Sendmail
project). The framework aims to provide Pythonic interfaces for implementing filters,
including leveraging coroutines instead of libmilter
's callback-style interface.
The kilter.service
package contains the higher-level, asynchronous framework for writing
mail filters.
Sendmail Filters
The Sendmail filter (milter) API facilitates communication between a Mail Transfer Agent (MTA) and arbitrary filters running as external services. These filters can perform a number of operations on received and outgoing mail, such as: virus scanning; checking senders' reputations; signing outgoing mail; and verifying signatures of incoming mail.
While the protocol was originally for filtering mail through a Sendmail MTA, Postfix has also reverse engineered the protocol and supports most filters made for Sendmail.
libmilter
Historically filters used the libmilter
library supplied by the Sendmail project to handle
all aspects of communication with an MTA. Filters simply registered callbacks for various
events then started the library's main loop. This approach makes implementing simple filters
in C easy for users, but makes writing "Pythonic" filters difficult, especially when a user
wishes to make use of async/await features.
Use of libmilter
to implement filters is almost universal as it is a black-box; the
on-the-wire protocol used is undocumented and subject to change between versions, which
makes writing a third-party parser difficult.
Alternatives in the Python Space
-
purepythonmilter: A modern and robust implementation for
asyncio
, written purely in statically typed Python without the need forlibmilter
. -
python-libmilter: Another pure-Python module using threading. Lacks static type annotations and is no longer actively developed, although still minimally maintained.
Usage
To write filters, create a coroutine function that conforms to Filter
. This function
takes a Session
object as its only argument.
Session
instances have several awaitable methods corresponding to SMTP commands
(i.e. MAIL
, RCPT
) and instances of HeadersAccessor
and BodyAccessor
which have
awaitable methods and are themselves asynchronous iterators. The various methods await
particular messages from an MTA and may return appropriate values from them. The
asynchronous iterators yield repeating messages like kilter.protocol.Header
and
kilter.protocol.Body
.
Examples
The following is a contrived example showing a filter that rejects messages sent by a particular user:
from kilter.service import Session
from kilter.protocol import Reject, Accept
# This corncob doesn't know when to stop; block him
BLOCK = b"the.black.knight@example.com"
async def reject_black_knight(session: Session) -> Reject|Accept:
if (await session.envelope_from()) == BLOCK:
return Reject()
async with session.headers as headers:
async for header in headers:
if header.name == "From" and header.value == BLOCK:
return Reject()
return Accept()
The following two examples show two implementations of a filter that strips headers starting with "X-". They demonstrate the two methods of collecting headers, then later modifying them during the post phase.
from kilter.service import Session
from kilter.protocol import Accept
async def strip_x_headers(session: Session) -> Accept:
remove = []
# iterate over headers as they arrive and select ones for later removal
async with session.headers as headers:
async for header in headers:
if header.name.startswith("X-"):
remove.append(header)
# remove the selected headers during the post phase
for header in remove:
await session.headers.delete(header)
return Accept()
from kilter.service import Session
from kilter.protocol import Accept
async def strip_x_headers(session: Session) -> Accept:
# collect the headers first
await session.headers.collect()
# iterate over collected headers during the post phase, removing the unwanted ones
async with session.headers as headers:
async for header in headers:
if header.name.startswith("X-"):
await session.headers.delete(header)
return Accept()
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file kilter_service-0.3.0.tar.gz
.
File metadata
- Download URL: kilter_service-0.3.0.tar.gz
- Upload date:
- Size: 22.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.10.14
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7671714b39ebc742ffa14a48f4fb58894f04c1970455aa981fee395a1706776a |
|
MD5 | 7f6562a5b3977badc71202dfbdc049cd |
|
BLAKE2b-256 | f1aec2d34819dd70513b55cb3c2885d8dd1a022fb94e6e66c2049ef78e7ed753 |
File details
Details for the file kilter_service-0.3.0-py3-none-any.whl
.
File metadata
- Download URL: kilter_service-0.3.0-py3-none-any.whl
- Upload date:
- Size: 17.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.10.14
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 253abf7b22faf4dedcb364b3d843ec736d8fc2b248d168a19c53e64a5ab7c93d |
|
MD5 | e397404f503c469cb573cc9510d59105 |
|
BLAKE2b-256 | 6e1c3e9170e833e094913b61f9f6ba7b084d60f34bb3eda76debef4cb6f8b02d |