Something went wrong. Try again.
Archives a classic Unix mailbox into a local allthemail archive, tagged with the mailbox name, with a SQLite ledger so reruns send nothing.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie <jeff@ocjtech.us>// SPDX-License-Identifier: MIT
//! The postmark, on its way into the archive as notmuch properties.//!//! # Why this exists at all//!//! Every message in an mbox has a line in front of it that is not part of the//! message://!//! ```text//! From jeff@example.com Mon Jan 1 00:00:00 2024//! ```//!//! That is the *postmark*, and what it carries is the envelope sender and the//! time of delivery. Neither is in the RFC 5322 bytes underneath it, and//! neither need agree with anything that is: the `From:` header says who wrote//! the message and the postmark says who handed it over, which is the address//! a bounce goes back to and the one a mailing list rewrites. The moment a//! message is lifted out of the file, both are gone.//!//! So they are recorded, as notmuch properties. A property is what allthemail//! offers for exactly this — something the archive knows *about* a message//! rather than something the message says about itself — and unlike a header//! it survives into the index, where `notmuch search property:mbox.source=…`//! can find it.//!//! The third one is the mailbox the message came out of. That is not in the//! message either, and it is not reliably in the tags: `tag_prefix` may be//! null, a name may have been truncated to fit notmuch's two hundred bytes,//! and a tag says nothing about *which* of several runs put it there. A//! message found in two mailboxes collects a `source` for each, because//! notmuch lets one property key carry several values and allthemail adds//! rather than replaces on every delivery.//!//! # What is deliberately not recorded//!//! **The offset.** Where a message sits in a file is true until the next time//! anything rewrites that file, which for an mbox is every deletion — so it is//! a fact with a shorter life than the archive and not worth storing there.//! The ledger keeps it, where it can be wrong without lying to anybody.//!//! **A normalized date.** `delivered` is the postmark's date exactly as//! written, undecoded. A great many mailboxes carry a postmark date that no//! parser can read, and a good many more carry one that parses into the wrong//! day because the deliverer's clock or timezone was wrong; rewriting either//! into an ISO 8601 string would be inventing a fact rather than recording//! one. What the file says is what goes in. Anything wanting the date decoded//! has `From.timestamp`, and anything wanting a date it can trust has the//! message's own `Date:` and `Received:` headers, which notmuch already//! indexes.//!//! # The namespace//!//! `notmuch-properties(7)` asks extensions that use properties to tell the//! notmuch project which keys they use, so that the namespace does not//! collide. The three here are `envelope-from`, `delivered` and `source`//! under whatever `property_prefix` says, which is `mbox.` unless somebody//! changed it — and `mbox.` is exactly the kind of name two programs could//! reach for, so a second one reading a mailbox into an archive is the//! collision to watch for. The prefix is configurable for that reason as much//! as for taste.//!//! # Why sanitizing is not optional//!//! `Archive.validateProperty` refuses a property with no `=`, with an empty//! key, longer than 1024 bytes, carrying a control character, not valid UTF-8,//! or beginning with `allthemail.` — and a refusal is a `400` for the **whole//! request**, so one strange postmark would cost the message rather than the//! property. An envelope sender is arbitrary bytes written by whatever//! delivered the message years ago, which is about as untrusted as input gets.
const std = @import("std");
/// allthemail's `Archive.property_max`: the longest `key=value` it will take./// Hardcoded rather than read from there, for the same reason `tags.tag_max`/// is: this program does not link the archive, and the number is part of its/// published contract.pub const property_max = 1024;
/// The prefix allthemail keeps for its own account of a message. A property/// starting with it is refused, because letting a submitter write there would/// let it forge the archive's record of how and when mail actually arrived.pub const reserved_prefix = "allthemail.";
/// A property, built into storage the caller owns.pub const Buffer = [property_max]u8;
/// The keys this program writes, under whatever prefix the configuration/// gives. Names rather than a stringly-typed call site, so that the set is/// enumerable and the fuzz target can walk it.pub const Key = enum { /// The envelope sender from the postmark. Absent rather than empty when /// the postmark had none, which is what a bounce looks like. @"envelope-from", /// The postmark's date, exactly as written. delivered, /// The configured name of the mailbox this came out of. source,
pub fn text(self: Key) []const u8 { return @tagName(self); }};
pub const Error = error{ /// The value was empty, or nothing survived sanitizing it. Recording a /// property that says nothing is worse than recording none: it makes a /// message look as though the question was asked and answered. Empty, /// The key and value together did not fit `property_max`. /// /// Not truncated, the way a tag is. A tag is a label and half of one is /// still a label; a property is a value somebody will compare against, and /// half an address that still looks like an address is worse than no /// address at all. TooLong, /// The prefix or the value was not valid UTF-8. NotUtf8, /// The configured prefix begins with `reserved_prefix`, which the archive /// keeps for its own account of a message. ReservedPrefix, /// The configured prefix has an `=` in it. notmuch takes the key as /// everything before the first one, so it would move the key's end into /// the middle of the prefix and put the value inside the key. There is no /// sanitizing that fixes that, so it is refused. PrefixHasEquals, /// The configured prefix has a control character in it. /// /// Refused rather than dropped, which is the opposite of what happens to /// one in a *value*. A value is a fact read out of a file and losing a /// byte of it is better than losing the message it describes; a prefix is /// a setting somebody typed, and quietly recording every property under a /// key they did not write is worse than telling them. PrefixHasControlCharacter,};
/// Writes `prefix ++ key ++ "=" ++ value` into `buffer` as a property/// allthemail will accept.////// Control characters are dropped, which is the same latitude `tags.build`/// takes and for the same reason: a postmark written by something with a bug/// in it should cost the archive a byte, not a message. Everything else is/// either passed through or refused.pub fn build( buffer: *Buffer, prefix: []const u8, key: Key, value: []const u8,) Error![]const u8 { if (!std.unicode.utf8ValidateSlice(prefix)) return error.NotUtf8; if (!std.unicode.utf8ValidateSlice(value)) return error.NotUtf8; // Caught here rather than left to the archive, because the answer is a // configuration to correct and the archive's answer would be a `400` on // every message in the mailbox. if (std.mem.startsWith(u8, prefix, reserved_prefix)) return error.ReservedPrefix; for (prefix) |c| { if (std.ascii.isControl(c)) return error.PrefixHasControlCharacter; if (c == '=') return error.PrefixHasEquals; }
var end: usize = 0; for ([_][]const u8{ prefix, key.text(), "=" }) |part| { if (end + part.len > property_max) return error.TooLong; @memcpy(buffer[end..][0..part.len], part); end += part.len; } const value_start = end; for (value) |c| { if (std.ascii.isControl(c)) continue; if (end == property_max) return error.TooLong; buffer[end] = c; end += 1; }
if (end == value_start) return error.Empty; // Dropping bytes out of the middle of a multi-byte sequence cannot happen // -- every byte of one has the high bit set and no control character does // -- but the value is arriving from a file and this is the one place that // claim is worth checking rather than asserting. if (!std.unicode.utf8ValidateSlice(buffer[value_start..end])) return error.NotUtf8;
return buffer[0..end];}
const testing = std.testing;
test "the three postmark properties are built under the prefix" { var buffer: Buffer = undefined; try testing.expectEqualStrings( "mbox.envelope-from=jeff@example.com", try build(&buffer, "mbox.", .@"envelope-from", "jeff@example.com"), ); try testing.expectEqualStrings( "mbox.delivered=Mon Jan 1 00:00:00 2024", try build(&buffer, "mbox.", .delivered, "Mon Jan 1 00:00:00 2024"), ); try testing.expectEqualStrings( "mbox.source=lists/zig", try build(&buffer, "mbox.", .source, "lists/zig"), );}
test "an empty value is no property rather than an empty one" { var buffer: Buffer = undefined; // What a bounce's postmark looks like: `From Mon Jan 1 ...`, with // nothing between the two spaces. There is nothing true to record. try testing.expectError(error.Empty, build(&buffer, "mbox.", .@"envelope-from", "")); try testing.expectError(error.Empty, build(&buffer, "mbox.", .delivered, "\n\r\x00"));}
test "control characters are dropped and the rest is kept" { var buffer: Buffer = undefined; try testing.expectEqualStrings( "mbox.envelope-from=ab", try build(&buffer, "mbox.", .@"envelope-from", "a\x00b\n"), );}
test "a value too long to fit is refused rather than truncated" { var buffer: Buffer = undefined; // Half an address that still looks like an address is worse than none. const long = &@as([property_max]u8, @splat('a')); try testing.expectError(error.TooLong, build(&buffer, "mbox.", .source, long));
// And the boundary is exact: one byte less than the limit fits. const key = "mbox." ++ "source="; const fits = &@as([property_max - key.len]u8, @splat('a')); const built = try build(&buffer, "mbox.", .source, fits); try testing.expectEqual(@as(usize, property_max), built.len);}
test "a prefix that would make a key nobody wrote is refused" { var buffer: Buffer = undefined; // notmuch takes the key as everything before the first `=`, so this would // record a property called `mbox` whose value began `source=`. try testing.expectError(error.PrefixHasEquals, build(&buffer, "mbox=", .source, "inbox")); // And a control character in the key is a property notmuch will store and // allthemail will refuse -- so it costs the message, not the property. try testing.expectError( error.PrefixHasControlCharacter, build(&buffer, "mbox\n", .source, "inbox"), );}
test "the archive's own prefix is refused here rather than on every message" { var buffer: Buffer = undefined; try testing.expectError( error.ReservedPrefix, build(&buffer, reserved_prefix, .source, "inbox"), );}
test "every built property satisfies what allthemail checks" { var buffer: Buffer = undefined;
// The rules from `Archive.validateProperty`, restated so that this fails // if the sanitizing ever stops producing something the archive will take. for ([_][]const u8{ "jeff@example.com", "MAILER-DAEMON", "Mon Jan 1 00:00:00 2024", "a\nb", "ünïcödé", "lists/zig", " spaces ", }) |value| { for (std.enums.values(Key)) |key| { const property = build(&buffer, "mbox.", key, value) catch continue; try testing.expect(property.len <= property_max); try testing.expect(std.unicode.utf8ValidateSlice(property)); for (property) |c| try testing.expect(!std.ascii.isControl(c)); try testing.expect(!std.mem.startsWith(u8, property, reserved_prefix));
const equals = std.mem.findScalar(u8, property, '=').?; try testing.expect(equals > 0); // Exactly the split notmuch will make: key, then everything else. try testing.expectEqualStrings("mbox.", property[0..5]); try testing.expectEqualStrings(key.text(), property[5..equals]); } }}
test "a value that is not UTF-8 is refused rather than stored for ever" { var buffer: Buffer = undefined; // An envelope sender is bytes written by something years ago, so this is // reachable from an ordinary mailbox rather than only from a fuzzer. try testing.expectError( error.NotUtf8, build(&buffer, "mbox.", .@"envelope-from", "\xff\xfe@example.com"), );}