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]
-
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]
-
Stages that support macro (symbol) lists, for
NegotiatemessagesEach stage corresponds to an event
Message, although not every event is a stage.The values of the stages do not correspond to
Message.identvalues. It is left to implementors creatingNegotiatemessages 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.IntFlagBit-field values for the
Negotiate.action_flagsfield of theNegotiatemessageThe 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.IntFlagBit-field values for the
Negotiate.protocol_flagsfield of theNegotiatemessageThe 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:
objectA base class for all messages which also handles packing and unpacking messages
Unpacking messages from a
FixedSizeBufferuses theMessage.unpackclass 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.packmethod, 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.releasemethod 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’sreleasemethod 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]
- 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 copiesUsers 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.MessageMessage 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.
- action_flags: kilter.protocol.messages.ActionFlags
- protocol_flags: kilter.protocol.messages.ProtocolFlags
- 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.
- class Macro(stage: int, macros: collections.abc.Mapping[str, str])[source]
Bases:
kilter.protocol.messages.MessageA message type for transferring symbol mappings prior to a stage event
- 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.
- class Connect(hostname: str, address: ipaddress.IPv4Address | ipaddress.IPv6Address | pathlib.Path | None = None, port: int = 0)[source]
Bases:
kilter.protocol.messages.MessageAn 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:
IPv4AddressorIPv6Addressfor INET and INET6 addresses,Pathfor UNIX addresses, orNonefor connections for which there is no known address (e.g. stdin).- address: ipaddress.IPv4Address | ipaddress.IPv6Address | pathlib.Path | None = None
- 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.
- class Helo(hostname: str)[source]
Bases:
kilter.protocol.messages.MessageAn event message reporting a client sent an SMTP HELO/EHLO command
- 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.
- class EnvelopeFrom(sender: bytes | memoryview, arguments: list[bytes | memoryview] = <factory>)[source]
Bases:
kilter.protocol.messages.MessageAn 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 copiesUsers 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 EnvelopeRecipient(recipient: bytes | memoryview, arguments: list[bytes | memoryview] = <factory>)[source]
Bases:
kilter.protocol.messages.MessageAn 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 copiesUsers 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 Data[source]
Bases:
kilter.protocol.messages.NoDataMessageAn 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.
- class Unknown(content: bytes | memoryview)[source]
Bases:
kilter.protocol.messages.BytesMessageAn event message reporting a client sent an unknown SMTP command
The raw command and arguments are sent as the message content.
- class Header(name: str, value: bytes | memoryview)[source]
Bases:
kilter.protocol.messages.MessageTransfers a header name and value from an email to a filter
- 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 copiesUsers 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 EndOfHeaders[source]
Bases:
kilter.protocol.messages.NoDataMessageA message that indicates the end of an email’s headers
- class Body(content: bytes | memoryview)[source]
Bases:
kilter.protocol.messages.BytesMessageTransfers a chunk of an email body to a filter
- class EndOfMessage(content: bytes | memoryview)[source]
Bases:
kilter.protocol.messages.BytesMessageA message that indicates the end of an email’s body
- class Abort[source]
Bases:
kilter.protocol.messages.NoDataMessageA notification that either the client or MTA decided to end a session before completion
- class Close[source]
Bases:
kilter.protocol.messages.NoDataMessageA notification of a session end, either after completion or an abort
- class Continue[source]
Bases:
kilter.protocol.messages.NoDataMessageA filter response instructing an MTA to continue without a specific decision
- class Reject[source]
Bases:
kilter.protocol.messages.NoDataMessageA filter response instructing an MTA to reject a message
- class Discard[source]
Bases:
kilter.protocol.messages.NoDataMessageA filter response instructing an MTA to reject a message without informing the client
- class Accept[source]
Bases:
kilter.protocol.messages.NoDataMessageA filter response instructing an MTA to accept a message
- class TemporaryFailure[source]
Bases:
kilter.protocol.messages.NoDataMessageA filter response instructing an MTA to reject a message temporarily (i.e. with a 4xx)
- class Skip[source]
Bases:
kilter.protocol.messages.NoDataMessageA 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.
- class ReplyCode(content: bytes | memoryview)[source]
Bases:
kilter.protocol.messages.BytesMessageA filter response instructing an MTA to reject a message with a specific code and reason
- class ChangeHeader(index: int, name: str, value: bytes | memoryview)[source]
Bases:
kilter.protocol.messages.MessageMessage from a filter to request a header is modified or removed at a given index
- 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 copiesUsers 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 Quarantine(reason: str)[source]
Bases:
kilter.protocol.messages.MessageRequest that a message is quarantined (blocked, but kept for review)
- 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.
- class AddHeader(name: str, value: bytes | memoryview)[source]
Bases:
kilter.protocol.messages.HeaderMessage from a filter to request a header is appended/prepended to the message
TODO: Append or prepend?
- class InsertHeader(index: int, name: str, value: bytes | memoryview)[source]
Bases:
kilter.protocol.messages.ChangeHeaderMessage from a filter to request a header is inserted into the message
- class ChangeSender(address: str, args: str | None = None)[source]
Bases:
kilter.protocol.messages._AddrParCmdMessage from a filter to request the message sender is modified
- class AddRecipient(address: str)[source]
Bases:
kilter.protocol.messages._AddrCmdMessage from a filter to request a recipient is inserted into the envelope
- class AddRecipientPar(address: str, args: str | None = None)[source]
Bases:
kilter.protocol.messages._AddrParCmdMessage from a filter to request a recipient is inserted into the envelope, with parameters
- class RemoveRecipient(address: str)[source]
Bases:
kilter.protocol.messages._AddrCmdMessage from a filter to request a recipient is removed from the envelope
- class ReplaceBody(content: bytes | memoryview)[source]
Bases:
kilter.protocol.messages.BytesMessageMessage from a filter to request the wholesale replacement of the message body