// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie // SPDX-License-Identifier: MIT //! Removing messages from a mailbox, which in this format means writing a new //! one without them. //! //! There is no other way. An mbox has no index and no free list: a message is //! the bytes between two postmarks, so taking one out means moving everything //! after it, and moving everything after it is the same work as writing the //! file again. Every mbox tool does this, and the ones that do it in place are //! the ones that lose a mailbox when the power goes. //! //! This is the whole of that logic, as a function from a reader to a writer, //! so that it can be tested and fuzzed with no filesystem in sight. //! `Mailbox.filter` wraps it in the locking and the atomic replace; //! `Mailbox.append` is the same function with a predicate that keeps //! everything. const std = @import("std"); const Io = std.Io; const Allocator = std.mem.Allocator; const testing = std.testing; const From = @import("From.zig"); const Reader = @import("Reader.zig"); const Writer = @import("Writer.zig"); const variant = @import("variant.zig"); /// What a pass over a mailbox did. pub const Stats = struct { kept: usize = 0, removed: usize = 0, /// The size of the messages kept, as they were handed to the predicate, /// which is not the size of the file they were written into: the framing /// and the quoting are not counted. bytes_kept: u64 = 0, }; pub const Error = Reader.Error || Io.Writer.Error; /// Copies `in` to `out`, keeping the messages `keep` returns true for. /// /// `keep` may fail only by running out of memory. That is less of a /// restriction than it looks: `Entry.parse` hands the bytes to zig-mime, whose /// parse has no other way to fail, so a predicate that reads the message it is /// deciding about fits exactly. A predicate that needs to fail some other way /// should record it in its context and be asked afterwards, which keeps the /// error out of the middle of a rewrite. /// /// Nothing is written until the predicate has been asked, and `out` is written /// from beginning to end in one pass, so the caller can point it at a /// temporary file and rename it over the original. pub fn copy( gpa: Allocator, in: *Io.Reader, out: *Io.Writer, options: Reader.Options, context: anytype, comptime keep: fn (@TypeOf(context), Reader.Entry) Allocator.Error!bool, ) Error!Stats { var reader: Reader = .init(gpa, in, options); defer reader.deinit(); var writer: Writer = .init(out, .{ .variant = options.variant }); var stats: Stats = .{}; while (try reader.next()) |entry| { if (try keep(context, entry)) { try writer.append(entry.from, entry.bytes); stats.kept += 1; stats.bytes_kept += entry.bytes.len; } else { stats.removed += 1; } } return stats; } /// The predicate that keeps everything, which is what an append is. pub fn keepAll(_: void, _: Reader.Entry) Allocator.Error!bool { return true; } // -- tests ------------------------------------------------------------------- const two_messages = "From alice Mon Jan 1 00:00:00 2024\n" ++ "Subject: one\n" ++ "\n" ++ "first\n" ++ "\n" ++ "From bob Tue Jan 2 00:00:00 2024\n" ++ "Subject: two\n" ++ "\n" ++ "second\n"; fn run( buffer: []u8, source: []const u8, options: Reader.Options, context: anytype, comptime keep: fn (@TypeOf(context), Reader.Entry) Allocator.Error!bool, ) !struct { out: []u8, stats: Stats } { var in: Io.Reader = .fixed(source); var out: Io.Writer = .fixed(buffer); const stats = try copy(testing.allocator, &in, &out, options, context, keep); return .{ .out = out.buffered(), .stats = stats }; } fn bySender(want: []const u8, entry: Reader.Entry) Allocator.Error!bool { return std.mem.eql(u8, entry.from.address, want); } test "keeping everything reproduces the mailbox byte for byte" { var buffer: [1024]u8 = undefined; // The source ends without the trailing empty line that a writer always // puts there, so the copy is the mailbox in the form this library writes. const got = try run(&buffer, two_messages ++ "\n", .{}, {}, keepAll); try testing.expectEqualStrings(two_messages ++ "\n", got.out); try testing.expectEqual(@as(usize, 2), got.stats.kept); try testing.expectEqual(@as(usize, 0), got.stats.removed); } test "removing a message leaves the others untouched" { var buffer: [1024]u8 = undefined; const got = try run(&buffer, two_messages, .{}, @as([]const u8, "bob"), bySender); try testing.expectEqualStrings( "From bob Tue Jan 2 00:00:00 2024\nSubject: two\n\nsecond\n\n", got.out, ); try testing.expectEqual(@as(usize, 1), got.stats.kept); try testing.expectEqual(@as(usize, 1), got.stats.removed); } test "keeping nothing leaves an empty mailbox, which is a valid one" { var buffer: [1024]u8 = undefined; const got = try run(&buffer, two_messages, .{}, @as([]const u8, "nobody"), bySender); try testing.expectEqualStrings("", got.out); try testing.expectEqual(@as(usize, 2), got.stats.removed); // And reading it back finds nothing rather than failing. var in: Io.Reader = .fixed(got.out); var reader: Reader = .init(testing.allocator, &in, .{}); defer reader.deinit(); try testing.expect((try reader.next()) == null); } test "a body that could have split its message survives the round trip" { const source = "From alice Mon Jan 1 00:00:00 2024\n" ++ "\n" ++ "here is a quoted header:\n" ++ ">From bob Tue Jan 2 00:00:00 2024\n" ++ ">>From deeper still\n" ++ "\n"; var buffer: [1024]u8 = undefined; const got = try run(&buffer, source, .{}, {}, keepAll); try testing.expectEqualStrings(source, got.out); try testing.expectEqual(@as(usize, 1), got.stats.kept); } test "a CRLF mailbox comes back a CRLF mailbox" { const source = "From alice Mon Jan 1 00:00:00 2024\r\n" ++ "Subject: one\r\n" ++ "\r\n" ++ "first\r\n" ++ "\r\n"; var buffer: [1024]u8 = undefined; const got = try run(&buffer, source, .{}, {}, keepAll); try testing.expectEqualStrings(source, got.out); } test "a predicate may read the message it is deciding about" { const Spam = struct { fn keep(gpa: Allocator, entry: Reader.Entry) Allocator.Error!bool { var headers = try entry.headers(gpa); defer headers.deinit(); const flag = headers.get("X-Spam-Flag") orelse return true; return !std.mem.eql(u8, std.mem.trim(u8, flag, " \t"), "YES"); } }; const source = "From alice Mon Jan 1 00:00:00 2024\n" ++ "X-Spam-Flag: YES\n" ++ "\n" ++ "buy things\n" ++ "\n" ++ "From bob Tue Jan 2 00:00:00 2024\n" ++ "Subject: real mail\n" ++ "\n" ++ "hello\n" ++ "\n"; var buffer: [1024]u8 = undefined; const got = try run(&buffer, source, .{}, testing.allocator, Spam.keep); try testing.expectEqual(@as(usize, 1), got.stats.kept); try testing.expectEqual(@as(usize, 1), got.stats.removed); try testing.expect(std.mem.indexOf(u8, got.out, "real mail") != null); try testing.expect(std.mem.indexOf(u8, got.out, "buy things") == null); } test "every reversible variant round trips what it wrote" { for ([_]variant.Variant{ .mboxrd, .mboxcl2 }) |v| { // Written by this library, so that the framing is the one it produces. var written: [1024]u8 = undefined; var w: Io.Writer = .fixed(&written); var mbox: Writer = .init(&w, .{ .variant = v }); try mbox.append(try From.parse("From alice Mon Jan 1 00:00:00 2024"), "\nFrom the top\n"); try mbox.append(try From.parse("From bob Tue Jan 2 00:00:00 2024"), "Subject: two\n\nsecond\n"); const source = w.buffered(); var buffer: [1024]u8 = undefined; const got = try run(&buffer, source, .{ .variant = v }, {}, keepAll); try testing.expectEqualStrings(source, got.out); try testing.expectEqual(@as(usize, 2), got.stats.kept); } }