WHERE MAIL FOR THIS HOST GOES. StreamSec.Mail.SMTPSpool queues what has to be
handed onward; this unit is the other half - what an inbound mail exchanger
does with a message that has arrived at its final destination.
MAILDIR, because its commit protocol is the one this codebase already trusts:
write into tmp, flush to the platter, rename into new. That is the same single
atomic rename tSmtpSpool commits with, and it means a crash can leave a stray
file in tmp but never a half-written message in new. mbox was rejected: it
needs From_-line escaping, i.e. rewriting the peer's octets, and the whole
reason BareNewlinePolicy defaults to sbnReject is that this library does not
rewrite what it was given.
TWO HEADERS ARE PREPENDED and nothing else is touched:
Return-Path: <reverse path from the ENVELOPE>
Delivered-To: <the recipient this copy is for>
which is what every local delivery agent does, and what makes a stored message
traceable. Prepending is signature-safe - DKIM covers named headers and these
two are excluded by convention - and every octet below them is byte-exact, so
S/MIME and DKIM still verify.
WHAT THIS IS NOT: a retrieval protocol. There is no POP3 or IMAP server here.
What it produces is a maildir that something else serves.
AND THE MAILDIR ITSELF IS NO LONGER HERE. StreamSec.Mail.Maildir owns the
layout, the naming, the commit protocol, the flags and the UID index, because
the retrieval side has to read back exactly what this writes - and two
implementations of one commit protocol is how a mailbox ends up holding a
message only one of them can see. tSmtpMaildirStore is the SMTP face of it:
the envelope becomes two prepended headers, failures become
eSmtpDeliveryError, and everything below that is delegated. Reach the store
itself through its Store property when a consumer also has to read.
NO FILE JAIL, AND THAT IS DELIBERATE. stFileJail is this library's confinement
boundary, but its safe entry points hand back an OPEN STREAM and it has no
rename primitive - and the rename IS the commit here, so a jail could guard
the write and not the thing that makes the write durable. Using it for half
the operation would look like confinement without being it. Instead the
property that matters is arranged structurally: IN THE DEFAULT CONFIGURATION
NO PEER-SUPPLIED OCTET REACHES A PATH AT ALL. A mailbox directory comes from
the configured list or from the application's own resolver; the peer's address
is a lookup KEY and never a path component, and the file name is generated.
AutoCreate is the single mode that derives a directory from the local part,
it is off by default, and what it derives must first pass
SmtpSafeMailboxSegment - a strict allowlist, not a denylist.Feature notes: Mail subsystem notes (SMTP / POP3 / IMAP)
inherits from Exception
(no public members)
RESOLVING A RECIPIENT TO A MAILBOX, AND PUTTING A MESSAGE IN IT.
ResolveMailbox runs at RCPT TO, so it must be quick and must not block: it is what lets an MX refuse an unknown user before the message is transferred rather than accepting and bouncing, which is the difference between a mail exchanger and a backscatter source.
Deliver RAISES on failure. A normal return is the promise the 250 is built on, so a store that cannot be sure the message is durable must throw.
function ResolveMailbox(const aPath: string; out aMailbox: string): Boolean;
procedure Deliver(const aMailbox: string; const aEnvelope: iSmtpEnvelope; const aData: tBytes);
THE QUOTA OF A MAILBOX, for a store that has one: an iSmtpMailboxStore that also implements this is asked at RCPT TO and before delivery at the end of DATA. aMailbox is what ResolveMailbox gave. aUsed and aLimit are octets; True when aLimit is a limit. Quick, like ResolveMailbox.
function MailboxQuota(const aMailbox: string; out aUsed, aLimit: Int64): Boolean;
THE SIZE OF A COPY AS IT WILL BE STORED, for a store that adds to what it is given: StoredCopySize answers how many octets Deliver, or DeliverToFolder with aFolder, would write into aMailbox for aEnvelope and aData, whatever the store puts in front of the message included. A quota is charged what is stored, so that is what a quota is checked against: a store with a quota (iSmtpMailboxQuota) that prepends anything implements this too, from the code that composes the copy. A store that does not implement it is taken to store aData as it is.
function StoredCopySize(const aMailbox, aFolder: string; const aEnvelope: iSmtpEnvelope; const aData: tBytes): Int64;
FILING INTO A FOLDER, for a store that has folders: an iSmtpMailboxStore that also implements this is asked to put a message that came with a field forged in this host's name into the mailbox's Junk folder (TsmSMTPServer.JunkFolder) - that one alone. DeliverToFolder writes aData into aFolder of aMailbox, what ResolveMailbox gave, as Deliver writes it into the mailbox, and raises as Deliver does. It answers False, having written nothing, when the mailbox has no such folder: it never creates one. THAT IS THE PER-ACCOUNT SWITCH. A mailbox read over IMAP has the folder - the client, the user or the application made it - and one collected over POP3, which shows no folder, has none and keeps getting its mail in the INBOX.
function DeliverToFolder(const aMailbox, aFolder: string; const aEnvelope: iSmtpEnvelope; const aData: tBytes): Boolean;
THE SMTP FACE OF StreamSec.Mail.Maildir, and nothing more. The layout, the naming, the commit protocol, the flags and the index all live in that unit now, because the retrieval side has to read back exactly what this writes - and two implementations of one commit protocol is how a mailbox ends up with a message only one of them can see.
WHAT IS LEFT HERE IS WHAT IS GENUINELY SMTP: turning an ENVELOPE into the two prepended headers, and answering with eSmtpDeliveryError so the relay's 4xx path and the spool's transient/permanent split keep working unchanged.
constructor Create(const aRoot: string); overload;
constructor Create(aStore: tMaildir); overload;
destructor Destroy; override;
property Store: tMaildir read fStore;
procedure AddMailbox(const aAddress, aDirectory: string);
procedure ClearMailboxes;
function ResolveMailbox(const aPath: string; out aMailbox: string): Boolean;
procedure Deliver(const aMailbox: string; const aEnvelope: iSmtpEnvelope; const aData: tBytes);
function MailboxQuota(const aMailbox: string; out aUsed, aLimit: Int64): Boolean;
function DeliverToFolder(const aMailbox, aFolder: string; const aEnvelope: iSmtpEnvelope; const aData: tBytes): Boolean;
function StoredCopySize(const aMailbox, aFolder: string; const aEnvelope: iSmtpEnvelope; const aData: tBytes): Int64;
property Root: string read GetRoot;
property HostName: string read GetHostName write SetHostName;
property AutoCreate: Boolean read GetAutoCreate write SetAutoCreate;
property FlushToDisk: Boolean read GetFlushToDisk write SetFlushToDisk;
property PrependHeaders: Boolean read fPrependHeaders write fPrependHeaders;
inherits from TInterfacedObject
implements iSmtpDeliveryAgent
AN ENTIRE SPOOLED MESSAGE INTO THE STORE. For an MX with no smarthost - the queue still exists, because a message may need retrying if a mailbox write fails - and for a bounce addressed to a local postmaster.
constructor Create(const aStore: iSmtpMailboxStore);
function Deliver(const aEnvelope: iSmtpEnvelope; const aData: tBytes; var aError: string): tSmtpDeliveryOutcome;
inherits from TInterfacedObject
implements iSmtpDeliveryAgent
LOCAL RECIPIENTS TO THE STORE, THE REST TO SOMEBODY ELSE - normally tSmtpSmarthostAgent. ONE OUTCOME PER MESSAGE, which is the honest limit of iSmtpDeliveryAgent and of the queue behind it: it returns the WORST of the two halves, so a message that was delivered locally and deferred onward is reported deferred and will be retried - and the local copy will be written twice. A queue that could do better would have to track recipients individually, which StreamSec.Mail.SMTPSpool states plainly that it does not. The relay avoids this for mail it accepts itself by splitting the transaction BEFORE it is queued; this class exists for messages that reach the queue already mixed.
constructor Create(const aStore: iSmtpMailboxStore; const aOnward: iSmtpDeliveryAgent);
function Deliver(const aEnvelope: iSmtpEnvelope; const aData: tBytes; var aError: string): tSmtpDeliveryOutcome;