// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie // SPDX-License-Identifier: MIT //! What the machine-readable part of a `multipart/report` is made of, whatever //! kind of report it is. //! //! RFC 6522 wraps three quite different things in the same envelope — a bounce //! (RFC 3464), a read receipt (RFC 8098), a spam complaint (RFC 5965) — and //! the reason it can is that all three write their contents the same way: //! groups of `Name: value` in header syntax, separated by blank lines, folded //! by the same rules. So the same code reads all three, and what differs //! between them is only which fields they carry. //! //! A bounce is the one with more than one group, because it is about a message //! and then about each recipient of it in turn. A receipt and a complaint are //! about one thing each and have one group. const std = @import("std"); const Allocator = std.mem.Allocator; const Io = std.Io; const testing = std.testing; const Headers = @import("Headers.zig"); /// `type; value` — the shape nearly every field of a report takes. /// /// The type says how to read the value: `rfc822` for an address, `dns` for a /// host name, `smtp` for a diagnostic. It exists because a report may be about /// mail that was never internet mail, and the field has to say which world the /// name belongs to. In practice it is `rfc822`, `dns` and `smtp`, and a field /// with no `;` at all is common enough to be worth accepting — the whole value /// is then the value, and the type is empty. pub const TypedValue = struct { type: []const u8, value: []const u8, pub fn parse(text: []const u8) TypedValue { const semicolon = std.mem.findScalar(u8, text, ';') orelse return .{ .type = "", .value = std.mem.trim(u8, text, " \t\r\n"), }; return .{ .type = std.mem.trim(u8, text[0..semicolon], " \t\r\n"), .value = std.mem.trim(u8, text[semicolon + 1 ..], " \t\r\n"), }; } pub fn write(self: TypedValue, w: *Io.Writer) Io.Writer.Error!void { // A value that holds a `;` and has no type has to keep the empty type // in front of it, or reading the field back splits it at the wrong // semicolon: `Final-Recipient: a;b@example.com` written without the // separator comes back as type `a` and recipient `b@example.com`, // which is a bounce reporting on an address nobody sent to. The // leading `; ` is what a type-less value with a semicolon in it looks // like, and it survives the trip. if (self.type.len != 0 or std.mem.findScalar(u8, self.value, ';') != null) { try w.writeAll(self.type); try w.writeAll("; "); } try w.writeAll(self.value); } pub fn format(self: TypedValue, w: *Io.Writer) Io.Writer.Error!void { return self.write(w); } }; /// Splits a report body into the groups that blank lines separate. /// /// Safe because a continuation line begins with whitespace and so is never /// blank: the only empty line in a well-formed group is the one that ends it. pub const GroupIterator = struct { source: []const u8, index: usize = 0, pub fn init(source: []const u8) GroupIterator { return .{ .source = source }; } pub fn next(self: *GroupIterator) ?[]const u8 { // Skip the blank lines between groups, and any at the start. while (self.index < self.source.len) { const line = self.lineAt(self.index); if (line.text_end != self.index) break; self.index = line.next; } if (self.index >= self.source.len) return null; const start = self.index; while (self.index < self.source.len) { const line = self.lineAt(self.index); if (line.text_end == self.index) break; // A blank line ends the group. self.index = line.next; } return self.source[start..self.index]; } const Line = struct { text_end: usize, next: usize }; fn lineAt(self: *const GroupIterator, start: usize) Line { const nl = std.mem.findScalarPos(u8, self.source, start, '\n') orelse return .{ .text_end = self.source.len, .next = self.source.len }; const text_end = if (nl > start and self.source[nl - 1] == '\r') nl - 1 else nl; return .{ .text_end = text_end, .next = nl + 1 }; } }; /// The fields of a report that has only one group: a read receipt or a spam /// complaint, as against a bounce, which has one group and then one per /// recipient. /// /// Anything after the first blank line is ignored. Some reporting systems put /// a signature or a copy of something there, and none of it is part of the /// report. pub fn firstGroup(gpa: Allocator, body: []const u8) Allocator.Error!Headers { var groups: GroupIterator = .init(body); return Headers.parse(gpa, groups.next() orelse ""); } test TypedValue { const typed = TypedValue.parse(" rfc822 ; nobody@example.com "); try testing.expectEqualStrings("rfc822", typed.type); try testing.expectEqualStrings("nobody@example.com", typed.value); // No type at all, which is malformed and is written all the same. const bare = TypedValue.parse("nobody@example.com"); try testing.expectEqualStrings("", bare.type); try testing.expectEqualStrings("nobody@example.com", bare.value); // And a type-less value that itself holds a semicolon keeps the empty // type, so that reading it back does not split it at that semicolon. const awkward = TypedValue.parse("; a;b@example.com"); try testing.expectEqualStrings("", awkward.type); try testing.expectEqualStrings("a;b@example.com", awkward.value); var round: [64]u8 = undefined; var rw: Io.Writer = .fixed(&round); try awkward.write(&rw); try testing.expectEqualStrings("; a;b@example.com", rw.buffered()); const back = TypedValue.parse(rw.buffered()); try testing.expectEqualStrings(awkward.type, back.type); try testing.expectEqualStrings(awkward.value, back.value); var buffer: [64]u8 = undefined; var w: Io.Writer = .fixed(&buffer); try w.print("{f}", .{typed}); try testing.expectEqualStrings("rfc822; nobody@example.com", w.buffered()); } test GroupIterator { var groups: GroupIterator = .init("a: 1\r\n\r\nb: 2\r\nc: 3\r\n\r\n\r\nd: 4"); try testing.expectEqualStrings("a: 1\r\n", groups.next().?); try testing.expectEqualStrings("b: 2\r\nc: 3\r\n", groups.next().?); try testing.expectEqualStrings("d: 4", groups.next().?); try testing.expectEqual(null, groups.next()); var empty: GroupIterator = .init(""); try testing.expectEqual(null, empty.next()); } test firstGroup { var headers = try firstGroup(testing.allocator, "A: 1\r\nB: 2\r\n\r\nC: ignored\r\n"); defer headers.deinit(); try testing.expectEqual(2, headers.fields.items.len); try testing.expectEqualStrings("1", headers.get("a").?); try testing.expectEqual(null, headers.get("c")); }