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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie <jeff@ocjtech.us>// SPDX-License-Identifier: MIT
//! Which mailboxes to read, and where to put what is in them.//!//! A file rather than flags, because the interesting part of configuring this//! is not the path — `--file` takes one of those — but the four things that//! cannot be read out of an mbox and have to be written down beside it: what//! the mailbox is called, which variant it is, who the mail was delivered to,//! and what else to tag it with. Those belong somewhere they can be got right//! once.//!//! It is JSON for the same reason allthemail's is: there is a parser in the//! standard library and the file is read once at startup.//!//! ## Nothing in here is a secret//!//! The archive's API token is named by path rather than written down, which is//! the whole reason the extra file is worth it: a configuration with a token//! in it cannot go in the Nix store, cannot be committed, and has to be mode//! 0600 wherever it lands. One that only names a path can be generated,//! world-readable and checked in, with the secret arriving separately — from//! agenix, or from a file somebody wrote by hand.//!//! The permission check moves with it: it is the token file that is refused if//! anyone but its owner can read it, and this file is not checked at all,//! because there is nothing in it to protect.
const Config = @This();
const std = @import("std");const Io = std.Io;const Allocator = std.mem.Allocator;const known_folders = @import("known-folders");const mbox = @import("mbox");
const AllTheMail = @import("AllTheMail.zig");const properties = @import("properties.zig");const tags = @import("tags.zig");
/// The directory everything this program owns goes under, inside whichever/// base directory is right for the kind of thing it is.const dir_name = "allthemail-mbox";
/// Where the ledger is.////// Absent, which is the default, puts it at `allthemail-mbox/state.db` under/// the data directory — `$XDG_DATA_HOME`, or its equivalent elsewhere. The/// directory is created; see `databasePath`.database: ?[]const u8 = null,
/// The archive's base URL, without a trailing slash:/// `http://127.0.0.1:8080`.allthemail: []const u8,
/// File holding the API token to present to the archive, and nothing else.////// Absent, which is the default, sends no credential at all — right for an/// archive with `auth` off, which is allthemail's default too, and reached/// over loopback or through something in front of it that authenticates.////// Mint it from the archive's reading UI with `read` and `write`. `write` is/// what `POST /archive` needs and is the one that matters; `read` is for the/// `HEAD /message/{hash}` pre-check, which `Archiver` only makes when the/// submission would carry no tags and no properties.allthemail_token_path: ?[]const u8 = null,
/// What every mailbox's name becomes a tag under: a mailbox called/// `lists/zig` becomes `mbox/lists/zig`.////// Null sends no tags at all, which is what an archive fed by one mailbox and/// uninterested in where it came from wants. That is a JSON `null` and not the/// same as leaving the setting out, which takes the default.tag_prefix: ?[]const u8 = "mbox/",
/// What the postmark's facts are recorded under as notmuch properties:/// `mbox.envelope-from`, `mbox.delivered` and `mbox.source`.////// Null records none of them. That throws away the envelope sender and the/// delivery time, which are the two things an mbox carries that the message/// itself does not — so it is worth being sure before turning it off. See/// `properties.zig`.////// It is conventionally a prefix ending in `.`, which is how notmuch/// properties are namespaced, but nothing requires that and nothing adds one.property_prefix: ?[]const u8 = "mbox.",
/// How many messages to archive before stopping to write down what happened/// to them.////// The ledger is written once per chunk rather than once per message, so this/// is how much work an interrupted run costs: a re-read of part of a local/// file, and an upload of whatever the archive was already told about and/// will recognise as a duplicate.////// It is not a limit on memory. One message is held at a time whatever this/// says — a mailbox is where holding more is least affordable — and what a/// chunk accumulates is a hash and an outcome per message.chunk_size: usize = 100,
/// The largest single message this will take out of a mailbox.////// There has to be one, and it is not really about large messages. A mailbox/// whose postmarks have been damaged — a truncated download, a transfer that/// ate the blank lines — is not a mailbox with a problem, it is one enormous/// message, and without a cap reading it is an attempt to put the whole file/// in memory at once.max_message_bytes: usize = 64 << 20,
/// How many times a message may fail before it stops being retried every run./// Without a cap, one message the archive will never accept makes every future/// run busy and noisy. `atm-mbox retry` clears the count.max_attempts: i64 = 5,
/// The mailboxes to read.////// May be empty, which is a perfectly good configuration for somebody who only/// ever names a file on the command line: what a run needs from this file/// then is the archive's URL and its token, and the mailbox comes from/// `--file`. A run that ends up with no mailboxes at all is refused by the/// command rather than here, because only the command knows whether one was/// named.mailboxes: []const Mailbox = &.{},
pub const Mailbox = struct { /// Where the file is. Absolute, or relative to wherever the command is /// run, which is why an absolute one is what a configuration should say. path: []const u8,
/// What this mailbox is called: the ledger's key, the tag under /// `tag_prefix`, and the `source` property. /// /// Absent takes the file's basename without its extension — see /// `tags.nameFromPath`. Writing one down is what makes the name survive /// the file being moved or renamed, and it is the only way to get a /// name with a `/` in it. name: ?[]const u8 = null,
/// Which of the four mboxes this is. /// /// **Nothing in the file says**, and guessing wrong corrupts every body it /// touches — a `>From ` that should have lost a `>` keeps it, or one that /// should have kept it loses it — so this is a parameter and never a /// detection. `mboxrd` is the default because it is the only variant whose /// quoting is reversible, and it is what almost everything modern writes. /// /// The cost of getting it wrong is not an error, it is a message whose /// bytes differ by one character from the message somebody sent, archived /// for ever under the hash of the wrong thing. If you do not know, look: /// `zig-mbox check --variant …` says which variants the file is /// self-consistent under. variant: Variant = .mboxrd,
/// Envelope recipients, which the archive turns into tags under its /// `recipient_tag_prefix` — `to/jeff@example.com`. /// /// Written down because an mbox does not know. The postmark carries the /// envelope *sender*, and the `To:` header is what the author wrote rather /// than where the message was actually delivered; the one true statement /// available is whose spool this was, and only the person configuring it /// can make it. recipients: []const []const u8 = &.{},
/// Tags to apply on top of the one made from `name`. /// /// Sent exactly as written, under no prefix, because a tag written here is /// somebody saying precisely what they want: `mailing-list`, /// `imported/2019`. They are still sanitized — a `400` over a tag costs /// the message — but nothing is added to them. tags: []const []const u8 = &.{},
/// Take a shared `flock` while reading. /// /// On by default, which is right for a mailbox something else might touch. /// Worth turning off for one on a filesystem whose locks do not work, or /// one being read straight off read-only media. /// /// It is worth knowing what the lock is and is not. It keeps this program /// and anything else using `flock` — mutt, another `atm-mbox` — from /// reading a mailbox while it is being rewritten. It does nothing about a /// delivery agent appending to a live spool, which is why a live spool is /// not what this should be pointed at: copy it aside first. lock: bool = true,
/// Whether a `Content-Length` header may say where a body ends, for the /// two variants that write one. /// /// On by default because `mboxcl2` is unreadable without it. Turning it /// off makes the scanner search for the next postmark instead, which can /// split a message but can never hide one — the trade worth making for a /// mailbox whose lengths are known to be wrong. trust_content_length: bool = true,
/// The name this mailbox is known by: `name`, or worked out from `path`. pub fn resolvedName(self: Mailbox) []const u8 { return self.name orelse tags.nameFromPath(self.path); }};
/// The four mboxes, as the configuration spells them.////// A copy of `mbox.Variant` rather than that enum directly, because this is a/// wire format: the names in somebody's configuration file must not change/// because a dependency renamed a tag, and `toMbox` is where the two are/// pinned together.pub const Variant = enum { mboxo, mboxrd, mboxcl, mboxcl2,
pub fn toMbox(self: Variant) mbox.Variant { return switch (self) { .mboxo => .mboxo, .mboxrd => .mboxrd, .mboxcl => .mboxcl, .mboxcl2 => .mboxcl2, }; }};
pub const CredentialError = error{ NotFound, Unreadable, /// Readable by somebody other than its owner. TooOpen, /// Empty, or not one opaque token: a file with spaces or line breaks in /// the middle of it is a file somebody pointed at by mistake. Malformed,} || Allocator.Error;
/// Which file a `CredentialError` was about, so that the message can name it.pub const CredentialDiagnostics = struct { path: ?[]const u8 = null,};
/// Largest credential file this will read. A file near this size is the wrong/// file.pub const max_credential_bytes = 4096;
/// One credential file: its contents, with surrounding whitespace removed.////// The trailing newline is the point of the trimming — every editor adds one,/// and a header value carrying one is a request no server would answer.fn readCredential( io: Io, gpa: Allocator, dir: Io.Dir, path: []const u8,) CredentialError![]const u8 { var file = dir.openFile(io, path, .{}) catch |err| switch (err) { error.FileNotFound => return error.NotFound, else => return error.Unreadable, }; defer file.close(io);
if (statIsTooOpen(io, file)) return error.TooOpen;
var buffer: [512]u8 = undefined; var reader = file.reader(io, &buffer); const source = reader.interface.allocRemaining(gpa, .limited(max_credential_bytes)) catch return error.Unreadable; defer gpa.free(source);
const trimmed = std.mem.trim(u8, source, " \t\r\n"); if (trimmed.len == 0) return error.Malformed;
// A token is one opaque run of characters. Anything with a space or a // control character inside it is a key file, or a note, or a // pretty-printed document -- something pointed at by mistake, and worth // saying so about rather than presenting to the archive to be refused // with an error that describes none of this. for (trimmed) |c| { if (std.ascii.isWhitespace(c) or std.ascii.isControl(c)) return error.Malformed; }
// The likeliest mistake the check above misses: a JSON document, written // compactly so it has no whitespace to catch it by. if (trimmed[0] == '{' or trimmed[0] == '[') return error.Malformed;
return try gpa.dupe(u8, trimmed);}
/// The API token for the archive, or null when none is configured. Allocated.pub fn archiveToken( io: Io, gpa: Allocator, dir: Io.Dir, self: Config, diagnostics: ?*CredentialDiagnostics,) CredentialError!?[]const u8 { const path = self.allthemail_token_path orelse return null;
if (diagnostics) |d| d.path = path; const token = try readCredential(io, gpa, dir, path); errdefer gpa.free(token);
// Refused here rather than at the first request, so that the answer is one // line at startup naming the file instead of a failed run. if (token.len > AllTheMail.max_token_bytes) return error.Malformed;
if (diagnostics) |d| d.path = null; return token;}
/// The parsed file and the arena its strings live in.pub const Parsed = struct { value: Config, arena: std.heap.ArenaAllocator,
pub fn deinit(self: *Parsed) void { self.arena.deinit(); self.* = undefined; }};
pub const ParseError = error{ /// The file is not JSON, or not the shape this expects. Malformed, /// The file parsed but says something that cannot be acted on. Invalid,} || Allocator.Error;
pub const LoadError = ParseError || error{ NotFound, Unreadable,};
/// Largest config this will read. A file bigger than this is a mistake, not a/// configuration.pub const max_bytes = 1 << 20;
/// Reads and validates the file at `path`.pub fn load(io: Io, gpa: Allocator, dir: Io.Dir, path: []const u8) LoadError!Parsed { var file = dir.openFile(io, path, .{}) catch |err| switch (err) { error.FileNotFound => return error.NotFound, else => return error.Unreadable, }; defer file.close(io);
// Deliberately not checked for permissions: there is nothing in it to // protect. The token file it names is the one that is checked.
var buffer: [4096]u8 = undefined; var reader = file.reader(io, &buffer); const source = reader.interface.allocRemaining(gpa, .limited(max_bytes)) catch return error.Unreadable; defer gpa.free(source);
return parse(gpa, source);}
/// Whether the file is readable or writable by its group or by anyone else.////// `Io.File.Permissions` is `std.Options.FilePermissions` on POSIX and a/// platform default elsewhere, and only the POSIX one has a mode to look at,/// so this asks rather than assuming — on a platform with no mode there is/// nothing to check and nothing to refuse.fn statIsTooOpen(io: Io, file: Io.File) bool { const stat = file.stat(io) catch return false; const Permissions = @TypeOf(stat.permissions); if (!@hasDecl(Permissions, "toMode")) return false; return stat.permissions.toMode() & 0o077 != 0;}
pub fn parse(gpa: Allocator, source: []const u8) ParseError!Parsed { var arena: std.heap.ArenaAllocator = .init(gpa); errdefer arena.deinit();
const parsed = std.json.parseFromSliceLeaky(Config, arena.allocator(), source, .{ .allocate = .alloc_always, }) catch return error.Malformed;
try validate(parsed); return .{ .value = parsed, .arena = arena };}
/// An absolute HTTP URL with no trailing slash.////// The trailing slash is refused where it can be seen rather than trimmed/// where it cannot: every URL built from one would contain `//`, which works/// everywhere and looks like a bug in every log line.fn validateUrl(url: []const u8) ParseError!void { if (std.mem.endsWith(u8, url, "/")) return error.Invalid; const uri = std.Uri.parse(url) catch return error.Invalid; if (!std.mem.eql(u8, uri.scheme, "http") and !std.mem.eql(u8, uri.scheme, "https")) return error.Invalid; if (uri.host == null) return error.Invalid;}
/// The rules `Archive.validateTag` applies on the far side, checked here so/// that a bad tag is one error at startup rather than a `400` on every message/// in the mailbox.fn validateTagText(tag: []const u8) ParseError!void { if (tag.len == 0 or tag.len > tags.tag_max) return error.Invalid; if (!std.unicode.utf8ValidateSlice(tag)) return error.Invalid; for (tag) |c| if (std.ascii.isControl(c)) return error.Invalid; // The archive reserves its `recipient_tag_prefix` -- `to/` unless the // operator changed it -- for its own statement about delivery. Only the // default can be checked from here; a configured one comes back as // `reserved_tag_prefix` on the first message. if (std.mem.startsWith(u8, tag, "to/")) return error.Invalid;}
fn validate(self: Config) ParseError!void { if (self.database) |path| { if (path.len == 0) return error.Invalid; } if (self.chunk_size == 0) return error.Invalid; if (self.max_message_bytes == 0) return error.Invalid; if (self.max_attempts <= 0) return error.Invalid;
try validateUrl(self.allthemail);
// Named but empty is a configuration that meant to point somewhere. The // way to send no token is to leave the field out. if (self.allthemail_token_path) |path| { if (path.len == 0) return error.Invalid; }
if (self.tag_prefix) |prefix| { if (!std.unicode.utf8ValidateSlice(prefix)) return error.Invalid; for (prefix) |c| if (std.ascii.isControl(c)) return error.Invalid; if (std.mem.startsWith(u8, prefix, "to/")) return error.Invalid; }
if (self.property_prefix) |prefix| { if (!std.unicode.utf8ValidateSlice(prefix)) return error.Invalid; for (prefix) |c| if (std.ascii.isControl(c)) return error.Invalid; // Both of these would otherwise be found by `properties.build` once // per message rather than once at startup, and the second would be // found as a `400` from the archive naming a key nobody wrote. if (std.mem.startsWith(u8, prefix, properties.reserved_prefix)) return error.Invalid; if (std.mem.findScalar(u8, prefix, '=') != null) return error.Invalid; }
for (self.mailboxes, 0..) |m, i| { if (m.path.len == 0) return error.Invalid; // Every mailbox has to be callable something, because the name is the // ledger's key: two mailboxes with no name would be one row. A path // with no basename -- `/`, or a directory -- has no name in it, and a // basename that is not UTF-8 is one that cannot be a notmuch tag. // Both are a `name` away from being fine, so both are refused here // rather than worked around. const resolved = m.resolvedName(); if (resolved.len == 0) return error.Invalid; if (!std.unicode.utf8ValidateSlice(resolved)) return error.Invalid;
for (m.recipients) |recipient| { if (recipient.len == 0) return error.Invalid; // It goes in an HTTP header, where `std.http.Client` asserts there // is no CRLF -- and an assertion is a panic where this wants an // error somebody can read. for (recipient) |c| { if (c < 0x21 or c > 0x7e) return error.Invalid; } }
for (m.tags) |tag| try validateTagText(tag);
// One ledger keyed by name cannot hold the same name twice, and two // mailboxes sharing one is a typo either way: the second would look to // every run like the first one having been rewritten. for (self.mailboxes[0..i]) |earlier| { if (std.mem.eql(u8, earlier.resolvedName(), resolved)) return error.Invalid; } }}
/// The mailbox with this name, or null.pub fn mailbox(self: Config, name: []const u8) ?Mailbox { for (self.mailboxes) |m| { if (std.mem.eql(u8, m.resolvedName(), name)) return m; } return null;}
/// Errors from working a default path out. `NoKnownFolder` is what it means/// when the platform cannot say where a data directory is — no `HOME`, most/// likely — and the answer is to say so rather than to invent somewhere.pub const PathError = error{NoKnownFolder} || Allocator.Error || Io.Cancelable;
/// `<base>/allthemail-mbox/<rest>`, for whichever base directory `folder`/// names.fn underKnownFolder( io: Io, gpa: Allocator, env: *const std.process.Environ.Map, folder: known_folders.KnownFolder, rest: []const u8,) PathError![]const u8 { const base = try known_folders.getPath(io, gpa, env, folder) orelse return error.NoKnownFolder; defer gpa.free(base); return std.fmt.allocPrint(gpa, "{s}/{s}/{s}", .{ base, dir_name, rest });}
/// Where the ledger lives: what the configuration says, or/// `allthemail-mbox/state.db` under the data directory. Allocated.pub fn databasePath( io: Io, gpa: Allocator, env: *const std.process.Environ.Map, self: Config,) PathError![]const u8 { if (self.database) |path| return try gpa.dupe(u8, path); // Data rather than cache: losing it does not lose any mail -- the archive // holds that, and the pre-check would find it again -- but it costs a // re-read and a re-hash of every byte of every configured mailbox, and // one `HEAD` per message to discover that none of it was needed. return underKnownFolder(io, gpa, env, .data, "state.db");}
/// Where to look for the config when `--config` was not given:/// `$ALLTHEMAIL_MBOX_CONFIG`, then `allthemail-mbox/config.json` under the/// configuration directory.pub fn defaultPath( io: Io, gpa: Allocator, env: *const std.process.Environ.Map,) PathError![]const u8 { if (env.get("ALLTHEMAIL_MBOX_CONFIG")) |path| return try gpa.dupe(u8, path); // `local_configuration` rather than `roaming_configuration`: this names // files on this machine, which is nothing that would be true on another. return underKnownFolder(io, gpa, env, .local_configuration, "config.json");}
const testing = std.testing;
const minimal = \\{ \\ "database": "/tmp/state.db", \\ "allthemail": "http://127.0.0.1:8080", \\ "mailboxes": [{"path": "/home/jeff/mail/lists/zig.mbox"}] \\};
test "a minimal config parses and takes the defaults" { var parsed = try parse(testing.allocator, minimal); defer parsed.deinit(); const config = parsed.value;
try testing.expectEqualStrings("/tmp/state.db", config.database.?); try testing.expectEqualStrings("mbox/", config.tag_prefix.?); try testing.expectEqualStrings("mbox.", config.property_prefix.?); try testing.expectEqual(@as(usize, 100), config.chunk_size); try testing.expectEqual(@as(i64, 5), config.max_attempts); try testing.expectEqual(@as(usize, 1), config.mailboxes.len);
const m = config.mailboxes[0]; // Nothing in a file says which variant it is, so the default is the only // reversible one. try testing.expectEqual(Variant.mboxrd, m.variant); try testing.expect(m.lock); try testing.expectEqual(@as(usize, 0), m.recipients.len); // Named after the file, since nothing said otherwise. try testing.expectEqualStrings("zig", m.resolvedName());
// No token named is no `Authorization` header sent, which is what an // archive with `auth` off wants. try testing.expectEqual(@as(?[]const u8, null), config.allthemail_token_path);}
test "a mailbox keeps the name it was given" { var parsed = try parse(testing.allocator, \\{ \\ "allthemail": "http://127.0.0.1:8080", \\ "mailboxes": [ \\ {"path": "/srv/mail/zig.mbox", "name": "lists/zig", \\ "variant": "mboxcl2", "recipients": ["jeff@example.com"], \\ "tags": ["mailing-list"], "lock": false} \\ ] \\} ); defer parsed.deinit();
const m = parsed.value.mailboxes[0]; try testing.expectEqualStrings("lists/zig", m.resolvedName()); try testing.expectEqual(Variant.mboxcl2, m.variant); try testing.expectEqual(mbox.Variant.mboxcl2, m.variant.toMbox()); try testing.expect(!m.lock); try testing.expectEqualStrings("jeff@example.com", m.recipients[0]); try testing.expectEqualStrings("mailing-list", m.tags[0]); try testing.expect(parsed.value.mailbox("lists/zig") != null); try testing.expect(parsed.value.mailbox("zig") == null);}
test "a configuration with no mailboxes is one that expects --file" { // Refusing this here would mean somebody who only ever imports a file // named on the command line had to write a mailbox they never use. var parsed = try parse(testing.allocator, \\{"allthemail": "http://127.0.0.1:8080", "mailboxes": []} ); defer parsed.deinit(); try testing.expectEqual(@as(usize, 0), parsed.value.mailboxes.len);}
test "null means no tags and no properties, which is not the same as absent" { var parsed = try parse(testing.allocator, \\{ \\ "allthemail": "http://127.0.0.1:8080", \\ "tag_prefix": null, \\ "property_prefix": null, \\ "mailboxes": [{"path": "a.mbox"}] \\} ); defer parsed.deinit(); try testing.expectEqual(@as(?[]const u8, null), parsed.value.tag_prefix); try testing.expectEqual(@as(?[]const u8, null), parsed.value.property_prefix);}
test "a configuration that cannot be acted on is refused rather than run" { const gpa = testing.allocator; for ([_][]const u8{ // A trailing slash, which would put `//` in every URL built from it. \\{"allthemail": "http://127.0.0.1:8080/", "mailboxes": [{"path": "a"}]} , \\{"allthemail": "ftp://127.0.0.1", "mailboxes": [{"path": "a"}]} , // An empty path is not a mailbox. \\{"allthemail": "http://127.0.0.1:8080", "mailboxes": [{"path": ""}]} , // A path with no basename. There is no name in it, and the name is // the ledger's key -- so this wants a `name` rather than a guess. \\{"allthemail": "http://127.0.0.1:8080", "mailboxes": [{"path": "/"}]} , \\{"allthemail": "http://127.0.0.1:8080", "mailboxes": [{"path": "a", "name": ""}]} , // Two mailboxes resolving to one name: the second would look to every // run like the first having been replaced. \\{"allthemail": "http://127.0.0.1:8080", \\ "mailboxes": [{"path": "/a/zig.mbox"}, {"path": "/b/zig.mbox"}]} , // The archive's own tag prefix, and its own property prefix. \\{"allthemail": "http://127.0.0.1:8080", "tag_prefix": "to/", \\ "mailboxes": [{"path": "a"}]} , \\{"allthemail": "http://127.0.0.1:8080", "property_prefix": "allthemail.x", \\ "mailboxes": [{"path": "a"}]} , // A property prefix with an `=` in it: notmuch would take the key as // everything before it and put the value inside the key. \\{"allthemail": "http://127.0.0.1:8080", "property_prefix": "mbox=", \\ "mailboxes": [{"path": "a"}]} , // A configured tag the archive would refuse, which costs the message // and not the tag. \\{"allthemail": "http://127.0.0.1:8080", \\ "mailboxes": [{"path": "a", "tags": ["with\na newline"]}]} , \\{"allthemail": "http://127.0.0.1:8080", \\ "mailboxes": [{"path": "a", "tags": [""]}]} , // A recipient with a space in it cannot go in a header, and // `std.http.Client` asserts rather than erring on one. \\{"allthemail": "http://127.0.0.1:8080", \\ "mailboxes": [{"path": "a", "recipients": ["two words"]}]} , \\{"allthemail": "http://127.0.0.1:8080", "max_attempts": 0, \\ "mailboxes": [{"path": "a"}]} , \\{"allthemail": "http://127.0.0.1:8080", "chunk_size": 0, \\ "mailboxes": [{"path": "a"}]} , \\{"allthemail": "http://127.0.0.1:8080", "max_message_bytes": 0, \\ "mailboxes": [{"path": "a"}]} , }) |source| { try testing.expectError(error.Invalid, parse(gpa, source)); }}
test "something that is not this shape at all is malformed rather than invalid" { const gpa = testing.allocator; try testing.expectError(error.Malformed, parse(gpa, "not json")); try testing.expectError(error.Malformed, parse(gpa, "{}")); // An unknown variant. Better a refusal naming the file than a silent // fallback to `mboxrd` over a mailbox that is not one. try testing.expectError(error.Malformed, parse(gpa, \\{"allthemail": "http://127.0.0.1:8080", \\ "mailboxes": [{"path": "a", "variant": "maildir"}]} ));}
test "every configuration variant maps to the library's" { // Two enums that have to keep agreeing: one is a wire format and the other // is a dependency's. `std.enums.values` means a variant added to either // without the other fails to compile or fails here. for (std.enums.values(Variant)) |v| { try testing.expectEqualStrings(@tagName(v), @tagName(v.toMbox())); }}