kilter.protocol.messages module

Classes for representing protocol messages, and packing and unpacking them

Most users of classes in this module will only be interested in their use as data-classes; leaving packing and unpacking to a separate module. See Message for details of packing and unpacking.

class Family(value, names=<not given>, *values, module=None, qualname=None, type=None, start=1, boundary=None)[source]

Bases: bytes, enum.Enum

BSD socket address families (of MTA clients) supported by the protocol

UNKNOWN = b'U'
UNIX = b'L'
INET = b'4'
INET6 = b'6'
class Stage(value, names=<not given>, *values, module=None, qualname=None, type=None, start=1, boundary=None)[source]

Bases: int, enum.Enum

Stages that support macro (symbol) lists, for Negotiate messages

Each stage corresponds to an event Message, although not every event is a stage.

The values of the stages do not correspond to Message.ident values. It is left to implementors creating Negotiate messages to implement the mapping, if needed.

CONNECT = 0
HELO = 1
ENVELOPE_FROM = 2
ENVELOPE_RECIPIENT = 3
DATA = 4
END_MESSAGE = 5
END_HEADERS = 6
class ActionFlags(value, names=<not given>, *values, module=None, qualname=None, type=None, start=1, boundary=None)[source]

Bases: enum.IntFlag

Bit-field values for the Negotiate.action_flags field of the Negotiate message

The values correspond to the SPFIF_* codes as described in https://pythonhosted.org/pymilter/milter_api/smfi_register.html#flags

NONE = 0
ALL = 511
ADD_HEADERS = 1
ADDHDRS = 1
CHANGE_HEADERS = 16
CHGHDRS = 16
CHANGE_BODY = 2
CHGBODY = 2
ADD_RECIPIENT = 4
ADDRCPT = 4
ADD_RECIPIENT_PAR = 128
ADDRCPT_PAR = 128
DELETE_RECIPIENT = 8
DELRCPT = 8
QUARANTINE = 32
CHANGE_FROM = 64
CHGFROM = 64
SETSYMLIST = 256
class ProtocolFlags(value, names=<not given>, *values, module=None, qualname=None, type=None, start=1, boundary=None)[source]

Bases: enum.IntFlag

Bit-field values for the Negotiate.protocol_flags field of the Negotiate message

The values correspond to the SMFIP_* codes described in https://pythonhosted.org/pymilter/milter_api/xxfi_negotiate.html

NONE = 0
NO_CONNECT = 1
NO_HELO = 2
NO_SENDER = 4
NO_MAIL = 4
NO_RECIPIENT = 8
NO_RCPT = 8
NO_BODY = 16
NO_HEADERS = 32
NO_HDRS = 32
NO_END_OF_HEADERS = 64
NO_EOH = 64
NO_UNKNOWN = 256
NO_DATA = 512
SKIP = 1024
REJECTED_RECIPIENT = 2048
RCPT_REJ = 2048
NR_CONNECT = 4096
NR_CONN = 4096
NR_HELO = 8192
NR_SENDER = 16384
NR_MAIL = 16384
NR_RECIPIENT = 32768
NR_RCPT = 32768
NR_DATA = 65536
NR_UNKNOWN = 131072
NR_UNKN = 131072
NR_END_OF_HEADERS = 262144
NR_EOH = 262144
NR_BODY = 524288
NR_HEADER = 128
NR_HDR = 128
HEADER_LEADING_SPACE = 1048576
HDR_LEADSPC = 1048576
MAX_DATA_SIZE_256K = 268435456
MDS_256K = 268435456
MAX_DATA_SIZE_1M = 536870912
MDS_1M = 536870912
class Message[source]

Bases: object

A base class for all messages which also handles packing and unpacking messages

Unpacking messages from a FixedSizeBuffer uses the Message.unpack class method:

>>> from kilter.protocol.buffer import SimpleBuffer
>>> from kilter.protocol.messages import Message
>>> buf = SimpleBuffer(1024)
>>> buf[:] = b"\x00\x00\x00\x0dHexample.com\x00"
>>> Message.unpack(buf)
(Helo(hostname='example.com'), 17)

Packing messages into a buffer is done with the Message.pack method, available to all instances:

>>> from ipaddress import IPv4Address
>>> from kilter.protocol.messages import Connect
>>> buf = SimpleBuffer(1024)
>>> message = Connect("example.com", IPv4Address("10.0.0.1"), 25025)
>>> message.pack(buf)
>>> buf[:].tobytes()
b'\x00\x00\x00\x19Cexample.com\x004a\xc110.0.0.1\x00'

Note that messages unpacked from a buffer may hold memoryviews of the buffer. This means that (a) if the buffer contents are overwritten the message’s data may be affected, and (b) the buffer cannot be resized without calling the message’s Message.release method first. For these reasons users should ensure that before updating the buffer with new data, each message is processed, any needed data is copied, and the message’s release method is called.

>>> buf[0:] = b"\x00\x00\x00\x12Mtest@example.com\x00"
>>> message, _ = Message.unpack(buf)
>>> del buf[:10]
Traceback (most recent call last):
        ...
BufferError: Existing exports of data: object cannot be re-sized
>>> message.release()
>>> del buf[:10]
ident: ClassVar[int]
classmethod unpack(buf: FixedSizeBuffer) → tuple[Message, int][source]

Unpack a message in buff and return a subclass instance and the number of bytes read

abstract classmethod from_buffer(buf: memoryview) → Self[source]

Construct an instance with values unpacked from a buffer

Concrete classes must implement this method to unpack the values each class needs. The buffer view contains the message contents, from after the five byte header, up to the length indicated by the header.

pack(buf: FixedSizeBuffer) → None[source]

Pack a message onto the end of a buffer

abstract to_buffer(buf: FixedSizeBuffer) → None[source]

Pack a message’s contents into a buffer

Concrete classes must implement this method to pack the values each class needs. Implementations must not write the message header.

release() → None[source]

Release any memoryviews a message holds of a buffer from which it was created

Concrete classes MUST implement this if they store references to memoryviews, or any value that could be a memoryview.

freeze() → None[source]

Similar to release(), but memoryviews are replaced with byte object copies

Users may call this to copy data from a buffer for later reading, for instance if they intend to store messages and process them at a later stage.

Concrete classes MUST implement this if they store references to memoryviews, or any value that could be a memoryview.

class Negotiate(version: int, action_flags: kilter.protocol.messages.ActionFlags, protocol_flags: kilter.protocol.messages.ProtocolFlags, macros: collections.abc.Mapping[kilter.protocol.messages.Stage, collections.abc.Collection[str]] = <factory>)[source]

Bases: kilter.protocol.messages.Message

Message type for MTA and filters to negotiate the features they provide and need

A session starts with an MTA connecting to a filter and sending a negotiate message with the action and protocol flags it supports. The filter then returns a negotiate message with the subset of the flags it wants to use, and optionally the macros (symbols) it wants before each stage.

version: int
action_flags: kilter.protocol.messages.ActionFlags
protocol_flags: kilter.protocol.messages.ProtocolFlags
macros: collections.abc.Mapping[kilter.protocol.messages.Stage, collections.abc.Collection[str]]
classmethod from_buffer(buf: memoryview) → Self[source]

Construct an instance with values unpacked from a buffer

Concrete classes must implement this method to unpack the values each class needs. The buffer view contains the message contents, from after the five byte header, up to the length indicated by the header.

to_buffer(buf: FixedSizeBuffer) → None[source]

Pack a message’s contents into a buffer

Concrete classes must implement this method to pack the values each class needs. Implementations must not write the message header.

ident: ClassVar[int] = 79
class Macro(stage: int, macros: collections.abc.Mapping[str, str])[source]

Bases: kilter.protocol.messages.Message

A message type for transferring symbol mappings prior to a stage event

stage: int
macros: collections.abc.Mapping[str, str]
classmethod from_buffer(buf: memoryview) → Self[source]

Construct an instance with values unpacked from a buffer

Concrete classes must implement this method to unpack the values each class needs. The buffer view contains the message contents, from after the five byte header, up to the length indicated by the header.

to_buffer(buf: FixedSizeBuffer) → None[source]

Pack a message’s contents into a buffer

Concrete classes must implement this method to pack the values each class needs. Implementations must not write the message header.

ident: ClassVar[int] = 68
class Connect(hostname: str, address: ipaddress.IPv4Address | ipaddress.IPv6Address | pathlib.Path | None = None, port: int = 0)[source]

Bases: kilter.protocol.messages.Message

An Event message reporting a client connecting to the MTA

The message contains a hostname from doing a reverse DNS lookup, or “[<address>]” if that fails; an address for known network families; and a port for stream types which have ports.

The type of an address value indicates the address family: IPv4Address or IPv6Address for INET and INET6 addresses, Path for UNIX addresses, or None for connections for which there is no known address (e.g. stdin).

hostname: str
address: ipaddress.IPv4Address | ipaddress.IPv6Address | pathlib.Path | None = None
port: int = 0
classmethod from_buffer(buf: memoryview) → Self[source]

Construct an instance with values unpacked from a buffer

Concrete classes must implement this method to unpack the values each class needs. The buffer view contains the message contents, from after the five byte header, up to the length indicated by the header.

to_buffer(buf: FixedSizeBuffer, offset: int = 0) → None[source]

Pack a message’s contents into a buffer

Concrete classes must implement this method to pack the values each class needs. Implementations must not write the message header.

ident: ClassVar[int] = 67
class Helo(hostname: str)[source]

Bases: kilter.protocol.messages.Message

An event message reporting a client sent an SMTP HELO/EHLO command

hostname: str
classmethod from_buffer(buf: memoryview) → Self[source]

Construct an instance with values unpacked from a buffer

Concrete classes must implement this method to unpack the values each class needs. The buffer view contains the message contents, from after the five byte header, up to the length indicated by the header.

to_buffer(buf: FixedSizeBuffer) → None[source]

Pack a message’s contents into a buffer

Concrete classes must implement this method to pack the values each class needs. Implementations must not write the message header.

ident: ClassVar[int] = 72
class EnvelopeFrom(sender: bytes | memoryview, arguments: list[bytes | memoryview] = <factory>)[source]

Bases: kilter.protocol.messages.Message

An event message reporting a client sent an SMTP “MAIL FROM” command

sender: bytes | memoryview
arguments: list[bytes | memoryview]
classmethod from_buffer(buf: memoryview) → Self[source]

Construct an instance with values unpacked from a buffer

Concrete classes must implement this method to unpack the values each class needs. The buffer view contains the message contents, from after the five byte header, up to the length indicated by the header.

to_buffer(buf: FixedSizeBuffer) → None[source]

Pack a message’s contents into a buffer

Concrete classes must implement this method to pack the values each class needs. Implementations must not write the message header.

release() → None[source]

Release any memoryviews a message holds of a buffer from which it was created

Concrete classes MUST implement this if they store references to memoryviews, or any value that could be a memoryview.

freeze() → None[source]

Similar to release(), but memoryviews are replaced with byte object copies

Users may call this to copy data from a buffer for later reading, for instance if they intend to store messages and process them at a later stage.

Concrete classes MUST implement this if they store references to memoryviews, or any value that could be a memoryview.

ident: ClassVar[int] = 77
class EnvelopeRecipient(recipient: bytes | memoryview, arguments: list[bytes | memoryview] = <factory>)[source]

Bases: kilter.protocol.messages.Message

An event message reporting a client sent an SMTP “RCPT TO” command

A client must send at least one “RCPT TO” command, and can send multiple.

recipient: bytes | memoryview
arguments: list[bytes | memoryview]
classmethod from_buffer(buf: memoryview) → Self[source]

Construct an instance with values unpacked from a buffer

Concrete classes must implement this method to unpack the values each class needs. The buffer view contains the message contents, from after the five byte header, up to the length indicated by the header.

to_buffer(buf: FixedSizeBuffer) → None[source]

Pack a message’s contents into a buffer

Concrete classes must implement this method to pack the values each class needs. Implementations must not write the message header.

release() → None[source]

Release any memoryviews a message holds of a buffer from which it was created

Concrete classes MUST implement this if they store references to memoryviews, or any value that could be a memoryview.

freeze() → None[source]

Similar to release(), but memoryviews are replaced with byte object copies

Users may call this to copy data from a buffer for later reading, for instance if they intend to store messages and process them at a later stage.

Concrete classes MUST implement this if they store references to memoryviews, or any value that could be a memoryview.

ident: ClassVar[int] = 82
class Data[source]

Bases: kilter.protocol.messages.NoDataMessage

An event message reporting a client sent an SMTP “DATA” command

This event mainly indicates to a filter that there will be no further SMTP commands sent.

ident: ClassVar[int] = 84
class Unknown(content: bytes | memoryview)[source]

Bases: kilter.protocol.messages.BytesMessage

An event message reporting a client sent an unknown SMTP command

The raw command and arguments are sent as the message content.

ident: ClassVar[int] = 85
class Header(name: str, value: bytes | memoryview)[source]

Bases: kilter.protocol.messages.Message

Transfers a header name and value from an email to a filter

name: str
value: bytes | memoryview
classmethod from_buffer(buf: memoryview) → Self[source]

Construct an instance with values unpacked from a buffer

Concrete classes must implement this method to unpack the values each class needs. The buffer view contains the message contents, from after the five byte header, up to the length indicated by the header.

to_buffer(buf: FixedSizeBuffer) → None[source]

Pack a message’s contents into a buffer

Concrete classes must implement this method to pack the values each class needs. Implementations must not write the message header.

release() → None[source]

Release any memoryviews a message holds of a buffer from which it was created

Concrete classes MUST implement this if they store references to memoryviews, or any value that could be a memoryview.

freeze() → None[source]

Similar to release(), but memoryviews are replaced with byte object copies

Users may call this to copy data from a buffer for later reading, for instance if they intend to store messages and process them at a later stage.

Concrete classes MUST implement this if they store references to memoryviews, or any value that could be a memoryview.

ident: ClassVar[int] = 76
class EndOfHeaders[source]

Bases: kilter.protocol.messages.NoDataMessage

A message that indicates the end of an email’s headers

ident: ClassVar[int] = 78
class Body(content: bytes | memoryview)[source]

Bases: kilter.protocol.messages.BytesMessage

Transfers a chunk of an email body to a filter

ident: ClassVar[int] = 66
class EndOfMessage(content: bytes | memoryview)[source]

Bases: kilter.protocol.messages.BytesMessage

A message that indicates the end of an email’s body

ident: ClassVar[int] = 69
class Abort[source]

Bases: kilter.protocol.messages.NoDataMessage

A notification that either the client or MTA decided to end a session before completion

ident: ClassVar[int] = 65
class Close[source]

Bases: kilter.protocol.messages.NoDataMessage

A notification of a session end, either after completion or an abort

ident: ClassVar[int] = 81
class Continue[source]

Bases: kilter.protocol.messages.NoDataMessage

A filter response instructing an MTA to continue without a specific decision

ident: ClassVar[int] = 99
class Reject[source]

Bases: kilter.protocol.messages.NoDataMessage

A filter response instructing an MTA to reject a message

ident: ClassVar[int] = 114
class Discard[source]

Bases: kilter.protocol.messages.NoDataMessage

A filter response instructing an MTA to reject a message without informing the client

ident: ClassVar[int] = 100
class Accept[source]

Bases: kilter.protocol.messages.NoDataMessage

A filter response instructing an MTA to accept a message

ident: ClassVar[int] = 97
class TemporaryFailure[source]

Bases: kilter.protocol.messages.NoDataMessage

A filter response instructing an MTA to reject a message temporarily (i.e. with a 4xx)

ident: ClassVar[int] = 116
class Skip[source]

Bases: kilter.protocol.messages.NoDataMessage

A filter response instructing an MTA to skip the rest of a message’s content

This may only be sent in response to a Body message.

ident: ClassVar[int] = 115
class ReplyCode(content: bytes | memoryview)[source]

Bases: kilter.protocol.messages.BytesMessage

A filter response instructing an MTA to reject a message with a specific code and reason

ident: ClassVar[int] = 121
class ChangeHeader(index: int, name: str, value: bytes | memoryview)[source]

Bases: kilter.protocol.messages.Message

Message from a filter to request a header is modified or removed at a given index

index: int
name: str
value: bytes | memoryview
classmethod from_buffer(buf: memoryview) → Self[source]

Construct an instance with values unpacked from a buffer

Concrete classes must implement this method to unpack the values each class needs. The buffer view contains the message contents, from after the five byte header, up to the length indicated by the header.

to_buffer(buf: FixedSizeBuffer) → None[source]

Pack a message’s contents into a buffer

Concrete classes must implement this method to pack the values each class needs. Implementations must not write the message header.

release() → None[source]

Release any memoryviews a message holds of a buffer from which it was created

Concrete classes MUST implement this if they store references to memoryviews, or any value that could be a memoryview.

freeze() → None[source]

Similar to release(), but memoryviews are replaced with byte object copies

Users may call this to copy data from a buffer for later reading, for instance if they intend to store messages and process them at a later stage.

Concrete classes MUST implement this if they store references to memoryviews, or any value that could be a memoryview.

ident: ClassVar[int] = 109
class Quarantine(reason: str)[source]

Bases: kilter.protocol.messages.Message

Request that a message is quarantined (blocked, but kept for review)

reason: str
classmethod from_buffer(buf: memoryview) → Self[source]

Construct an instance with values unpacked from a buffer

Concrete classes must implement this method to unpack the values each class needs. The buffer view contains the message contents, from after the five byte header, up to the length indicated by the header.

ident: ClassVar[int] = 113
to_buffer(buf: FixedSizeBuffer) → None[source]

Pack a message’s contents into a buffer

Concrete classes must implement this method to pack the values each class needs. Implementations must not write the message header.

class AddHeader(name: str, value: bytes | memoryview)[source]

Bases: kilter.protocol.messages.Header

Message from a filter to request a header is appended/prepended to the message

TODO: Append or prepend?

ident: ClassVar[int] = 104
class InsertHeader(index: int, name: str, value: bytes | memoryview)[source]

Bases: kilter.protocol.messages.ChangeHeader

Message from a filter to request a header is inserted into the message

ident: ClassVar[int] = 105
class ChangeSender(address: str, args: str | None = None)[source]

Bases: kilter.protocol.messages._AddrParCmd

Message from a filter to request the message sender is modified

ident: ClassVar[int] = 101
class AddRecipient(address: str)[source]

Bases: kilter.protocol.messages._AddrCmd

Message from a filter to request a recipient is inserted into the envelope

ident: ClassVar[int] = 43
class AddRecipientPar(address: str, args: str | None = None)[source]

Bases: kilter.protocol.messages._AddrParCmd

Message from a filter to request a recipient is inserted into the envelope, with parameters

ident: ClassVar[int] = 50
class RemoveRecipient(address: str)[source]

Bases: kilter.protocol.messages._AddrCmd

Message from a filter to request a recipient is removed from the envelope

ident: ClassVar[int] = 45
class ReplaceBody(content: bytes | memoryview)[source]

Bases: kilter.protocol.messages.BytesMessage

Message from a filter to request the wholesale replacement of the message body

ident: ClassVar[int] = 98
class Progress[source]

Bases: kilter.protocol.messages.NoDataMessage

Message from a filter to inform an MTA that it is still operational, despite delays

ident: ClassVar[int] = 112