WHERE MAIL FOR THIS HOST LIVES, AND THE ONLY THING THAT READS IT BACK.
StreamSec.Mail.SMTPDelivery puts a message in a mailbox; this unit is the
other half - enumerate, read, flag, delete, and the per-message identity POP3
and IMAP have to be able to promise is stable.
MAILDIR++, because the commit protocol is the one this codebase already
trusts (write into tmp, flush, rename into new) and because the layout is what
Dovecot and Courier read. That second reason is the deciding one: an operator
can point another server at the same tree, or migrate off this one, without a
converter. Flags live in the file name, folders are sibling '.Name'
directories, and the hierarchy delimiter is '.'.
THE ONE INVARIANT EVERYTHING ELSE RESTS ON:
THE DELIVERY PATH NEVER TAKES A MAILBOX LOCK.
Deliver writes tmp -> flush -> rename into new/. It reads no index and takes
no cross-process maildrop lock. A short publication lock serializes fatal
failure with writes. It waits for no lock held for a client session.
A POP3 session held open for twenty minutes cannot delay or fail an inbound
delivery through its long-lived maildrop lock. The index is only ever
written by a reader that already holds the store lock.
UIDVALIDITY IS MINTED ONCE AND NEVER REUSED. If the index is lost or will not
parse, a NEW UIDVALIDITY is minted rather than the old one recovered. RFC 3501
sec.2.3.1.1 makes a changed UIDVALIDITY mean "discard your cache", which costs
a client one resync; reusing a UID for a different message means the client
shows the wrong mail and never finds out. On any doubt, bump it.
ONE ALLOWLIST FOR CLIENT PATH SEGMENTS. A mailbox is looked up by address
and named by configuration, and an IMAP folder name is checked segment by
segment against MailSafeFolderSegment before a directory is built from it.
MailSafeSegment is the canonical client path allowlist; SMTPDelivery
delegates to it, and MailSafeFolderSegment widens it only by an interior
space and canonical modified UTF-7 (RFC 3501 sec.5.1.3), the spelling the
folder directory then carries, as Dovecot and Courier name theirs.
Comparisons accept filesystem folder names independently of the allowlist
used to CREATE them.Feature notes: Mail subsystem notes (SMTP / POP3 / IMAP)
function Enter(aShutdown: Boolean): Boolean;
procedure Leave(aShutdown: Boolean);
procedure Cancel;
Log, Shutdown: tMailFatalErrorEvent;
Lease: iMailFatalLease;
inherits from Exception
(no public members)
inherits from eMailStoreError
A new keyword for a folder whose keyword table is full (cMailMaxKeywords).
(no public members)
inherits from eMailStoreError
Retained views and access references are invalid after shutdown.
(no public members)
inherits from eMailStoreUnavailable
(no public members)
inherits from eMailStoreError
Every failed rollback pair is reported as current -> original path.
(no public members)
inherits from eMailStoreError
Physical removals remain true even if saving their index failed.
constructor Create(const aMessage: string; const aRemoved: TArray<Integer>);
property Removed: TArray<Integer> read fRemoved;
ONE MESSAGE, as a view reports it. The body is NOT here - a mailbox of ten thousand messages costs ten thousand of these rather than the mail itself.
Uid: Cardinal;
Name: string;
Octets: Int64;
Flags: tMailFlags;
Arrived: TDateTime;
Recent: Boolean;
Keywords: TArray<string>;
(no public members)
WHAT EVERY RETRIEVAL PROTOCOL NEEDS OF A MAILBOX TREE.
function Available: Boolean;
function SameFolderName(const aFolder1, aFolder2: string): Boolean;
function MailboxExists(const aAddress: string): Boolean;
function OpenFolder(const aAddress, aFolder: string; aMode: tMailboxOpenMode; out aView: iMailboxView; out aWhy: string; out aMissing: Boolean): Boolean;
function ListFolders(const aAddress, aPattern: string): TArray<string>;
function CreateFolder(const aAddress, aFolder: string): Boolean;
function CreateNewFolder(const aAddress, aFolder: string; out aExisted: Boolean): Boolean;
function DeleteFolder(const aAddress, aFolder: string): Boolean;
function RenameFolder(const aAddress, aFrom, aTo: string): Boolean;
function ReadSubscriptions(const aAddress: string): TArray<string>;
function WriteSubscriptions(const aAddress: string; const aNames: TArray<string>): Boolean;
ONE OPEN FOLDER, for the length of a session.
IT IS A SNAPSHOT. Message numbers are fixed when it opens and only Refresh may change them - POP3 forbids renumbering inside a session outright, and IMAP forbids it anywhere an untagged EXISTS would not be legal.
function GetCount: Integer;
function GetTotalOctets: Int64;
function GetUidValidity: Cardinal;
function GetUidNext: Cardinal;
function GetItem(aIndex: Integer): tMailItem;
property Count: Integer read GetCount;
property TotalOctets: Int64 read GetTotalOctets;
property UidValidity: Cardinal read GetUidValidity;
property UidNext: Cardinal read GetUidNext;
property Items[aIndex: Integer]: tMailItem read GetItem;
function ReadMessage(aIndex: Integer; out aData: tBytes): Boolean;
function ReadHead(aIndex, aBodyLines: Integer; out aData: tBytes): Boolean;
function SetFlags(aIndex: Integer; aFlags: tMailFlags; aMode: tMailFlagMode; aExternalChanged: PBoolean = nil): tMailFlagResult;
procedure MarkDeleted(aIndex: Integer; aDeleted: Boolean);
function Expunge: TArray<Integer>;
function Refresh(out aAdded: Integer): Boolean;
function Append(const aData: tBytes; aFlags: tMailFlags; const aArrived: TDateTime; out aUid: Cardinal): Boolean;
function RemoveAppended(const aNames: TArray<string>): Boolean;
function MoveTo(const aIndices: TArray<Integer>; const aTarget: iMailboxView; out aSrcUids, aDstUids: TArray<Cardinal>; out aRemoved: TArray<Integer>): Boolean;
function Keywords: TArray<string>;
function SetFlagsKeywords(aIndex: Integer; aFlags: tMailFlags; const aKeywords: TArray<string>; aMode: tMailFlagMode; aExternalChanged: PBoolean = nil): tMailFlagResult;
function AppendKeywords(const aData: tBytes; aFlags: tMailFlags; const aKeywords: TArray<string>; const aArrived: TDateTime; out aUid: Cardinal): Boolean;
procedure Close;
(no public members)
inherits from TInterfacedObject
implements iMailboxView
ONE OPEN FOLDER. Created only by tMaildir.
constructor Create(aOwner: tMaildir; const aBase: string; aMode: tMailboxOpenMode; aLock: TFileStream);
destructor Destroy; override;
function ReadMessage(aIndex: Integer; out aData: tBytes): Boolean;
function ReadHead(aIndex, aBodyLines: Integer; out aData: tBytes): Boolean;
function SetFlags(aIndex: Integer; aFlags: tMailFlags; aMode: tMailFlagMode; aExternalChanged: PBoolean = nil): tMailFlagResult;
procedure MarkDeleted(aIndex: Integer; aDeleted: Boolean);
function Expunge: TArray<Integer>;
function Refresh(out aAdded: Integer): Boolean;
function Append(const aData: tBytes; aFlags: tMailFlags; const aArrived: TDateTime; out aUid: Cardinal): Boolean;
function RemoveAppended(const aNames: TArray<string>): Boolean;
function MoveTo(const aIndices: TArray<Integer>; const aTarget: iMailboxView; out aSrcUids, aDstUids: TArray<Cardinal>; out aRemoved: TArray<Integer>): Boolean;
function Keywords: TArray<string>;
function SetFlagsKeywords(aIndex: Integer; aFlags: tMailFlags; const aKeywords: TArray<string>; aMode: tMailFlagMode; aExternalChanged: PBoolean = nil): tMailFlagResult;
function AppendKeywords(const aData: tBytes; aFlags: tMailFlags; const aKeywords: TArray<string>; const aArrived: TDateTime; out aUid: Cardinal): Boolean;
procedure Close;
inherits from TInterfacedObject
implements iMailboxAccess
THE TREE. One directory per mailbox under a root, Maildir++ inside each.
constructor Create(const aRoot: string);
destructor Destroy; override;
procedure AddMailbox(const aAddress, aDirectory: string);
procedure ClearMailboxes;
procedure ReplaceMailboxes(aList: TStrings);
procedure EnsureMailbox(const aAddress: string);
function ResolveMailbox(const aAddress: string; out aMailbox: string): Boolean;
procedure Deliver(const aMailbox: string; const aData: tBytes);
function DeliverToFolder(const aMailbox, aFolder: string; const aData: tBytes): Boolean;
procedure BeginCommit;
procedure EndCommit;
function Available: Boolean;
function SameFolderName(const aFolder1, aFolder2: string): Boolean;
function MailboxExists(const aAddress: string): Boolean;
function OpenFolder(const aAddress, aFolder: string; aMode: tMailboxOpenMode; out aView: iMailboxView; out aWhy: string; out aMissing: Boolean): Boolean;
function ListFolders(const aAddress, aPattern: string): TArray<string>;
function CreateFolder(const aAddress, aFolder: string): Boolean;
function CreateNewFolder(const aAddress, aFolder: string; out aExisted: Boolean): Boolean;
function DeleteFolder(const aAddress, aFolder: string): Boolean;
function RenameFolder(const aAddress, aFrom, aTo: string): Boolean;
function ReadSubscriptions(const aAddress: string): TArray<string>;
function WriteSubscriptions(const aAddress: string; const aNames: TArray<string>): Boolean;
function Generation: Integer;
function GenerationSlot(const aAddress, aFolder: string): Integer;
function MailboxGeneration(aSlot: Integer): Int64;
function MailboxQuota(const aMailbox: string; out aUsed, aLimit: Int64): Boolean;
function AccountQuota(const aAddress: string; out aUsed, aLimit: Int64): Boolean;
procedure RecountUsage(const aMailbox: string);
property Quota: Int64 read GetQuota write SetQuota;
property OnMailboxQuota: tMailQuotaEvent read fOnMailboxQuota write fOnMailboxQuota;
property Root: string read fRoot;
property HostName: string read fHostName write fHostName;
property AutoCreate: Boolean read fAutoCreate write fAutoCreate;
property FlushToDiskProperty: Boolean read fFlushToDisk write fFlushToDisk;
inherits from TComponent
THE MAILBOX TREE AS ONE COMPONENT, so the SMTP server that writes it and the POP3 and IMAP servers that read it are pointed at THE SAME OBJECT rather than three configured with the same directory.
One shared configuration keeps mailbox lists and AutoCreate policy from disagreeing. Separate stores share the process operation lock, index and maildrop exclusion, and the fatal latch for the same canonical root.
ONE LIST, NOT TWO. With Users assigned the accepted addresses are the ACCOUNTS: an account is a mailbox is a maildir, which is the 1:1:1 model StreamSec.Mail.Users already claims, made structural. Without it, fill in Mailboxes by hand. Configuring both is legitimate - the explicit list wins for an address the account store does not know - but it is the arrangement in which the two can drift.
constructor Create(AOwner: TComponent); override;
procedure BeforeDestruction; override;
procedure ReportFatalError(E: Exception);
destructor Destroy; override;
property Store: tMaildir read fStore;
property Access: iMailboxAccess read fStoreRef;
property Failed: Boolean read GetFailed;
property FailurePending: Boolean read GetFailurePending;
procedure AddFatalErrorHandler(const aLog, aShutdown: tMailFatalErrorEvent);
procedure RemoveFatalErrorHandler(const aLog, aShutdown: tMailFatalErrorEvent);
procedure Refresh;
property Root: string read fRoot write SetRoot;
property Mailboxes: TStringList read fMailboxes write SetMailboxes;
property Users: TsmMailUsers read fUsers write SetUsers;
property HostName: string read GetHostName write SetHostName;
property AutoCreate: Boolean read GetAutoCreate write SetAutoCreate default False;
property FlushToDisk: Boolean read GetFlushToDisk write SetFlushToDisk default True;
property Quota: Int64 read GetQuota write SetQuota default 0;
property OnMailboxQuota: tMailQuotaEvent read fOnMailboxQuota write fOnMailboxQuota;
property Active: Boolean read fActive write SetActive default False;
property OnFatalError: tMailFatalErrorEvent read fOnFatalError write fOnFatalError;
property OnCompareFolderName: tMailCompareFolderNameEvent read fOnCompareFolderName write SetOnCompareFolderName;
aSame starts with the store's case-insensitive default answer.
aMailbox is the mailbox's directory name. aLimit starts with the store's Quota: octets, 0 for no limit.
The Maildir++ flag set. The letters are the on-disk spelling and the order of this enumeration IS their ASCII order, which is what the convention requires of the info suffix.