// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie // SPDX-License-Identifier: MIT //! `message/disposition-notification` — RFC 8098 — the machine-readable half //! of a read receipt. //! //! A receipt says what became of a message after it was delivered, which is a //! different question from whether it was delivered: that is a bounce, and it //! is `delivery_status`. It arrives in the same `multipart/report` envelope, //! with `report-type=disposition-notification`, and its body is the same //! header syntax — but with one group rather than one per recipient, because a //! receipt is about one recipient by definition: the one whose mail program //! sent it. //! //! ```text //! Reporting-UA: mail.example.net; ExampleMail 4.2 //! Final-Recipient: rfc822; alice@example.net //! Original-Message-ID: //! Disposition: manual-action/MDN-sent-manually; displayed //! ``` //! //! **A receipt is a claim, not evidence.** It is generated by the recipient's //! software, sent only if that software chooses to, and says whatever it //! chooses to say. `displayed` means a mail program put the message on a //! screen; it does not mean a person read it, and nothing stops a program //! sending it without doing either. The `Disposition` field says so itself: //! `manual-action` means a person agreed to send the receipt and //! `automatic-action` means nobody was asked, which is the closest the format //! comes to admitting how little it can promise. const std = @import("std"); const Allocator = std.mem.Allocator; const Io = std.Io; const testing = std.testing; const Headers = @import("Headers.zig"); const reports = @import("report.zig"); pub const TypedValue = reports.TypedValue; /// What was done with the message. RFC 8098 §3.2.6. pub const Type = enum { /// Put in front of a person. It does not mean anybody read it. displayed, /// Removed without being displayed. deleted, /// Sent on somewhere else — printed, faxed, forwarded to a system that /// does not report back. dispatched, /// Handled by something that is not a person, in some way the report does /// not describe. processed, /// Something else was written, which RFC 8098 does not allow. other, pub fn parse(text: []const u8) Type { const trimmed = std.mem.trim(u8, text, " \t\r\n"); const table = .{ .{ "displayed", Type.displayed }, .{ "deleted", Type.deleted }, .{ "dispatched", Type.dispatched }, .{ "processed", Type.processed }, }; inline for (table) |entry| { if (std.ascii.eqlIgnoreCase(trimmed, entry[0])) return entry[1]; } return .other; } pub fn name(self: Type) []const u8 { return switch (self) { .displayed => "displayed", .deleted => "deleted", .dispatched => "dispatched", .processed => "processed", .other => "processed", }; } }; /// Whether a person was involved, which is the only thing a receipt can /// honestly report about itself. pub const Mode = enum { /// A person did it, or agreed to it. manual, /// Nobody was asked. automatic, other, fn parse(text: []const u8, comptime manual_name: []const u8, comptime automatic_name: []const u8) Mode { const trimmed = std.mem.trim(u8, text, " \t\r\n"); if (std.ascii.eqlIgnoreCase(trimmed, manual_name)) return .manual; if (std.ascii.eqlIgnoreCase(trimmed, automatic_name)) return .automatic; return .other; } }; /// The `Disposition` field, taken apart: /// `action-mode/sending-mode; disposition-type[/modifier,...]`. pub const Disposition = struct { /// Whether a person chose what became of the message. action: Mode, /// Whether a person chose to send the receipt saying so. sending: Mode, type: Type, /// Anything after a `/` on the type: `error` and whatever else a /// reporting system invented. Kept as written. modifiers: []const u8, pub fn parse(text: []const u8) Disposition { // With no `;` there are no modes, which RFC 8098 does not allow — and // the whole value is then the disposition type, which is the part // worth reading and the part such a sender meant. const semicolon = std.mem.findScalar(u8, text, ';'); const modes = if (semicolon) |at| std.mem.trim(u8, text[0..at], " \t\r\n") else ""; var rest = std.mem.trim(u8, if (semicolon) |at| text[at + 1 ..] else text, " \t\r\n"); const slash = std.mem.findScalar(u8, modes, '/'); const action = modes[0 .. slash orelse modes.len]; const sending = if (slash) |at| modes[at + 1 ..] else ""; var modifiers: []const u8 = ""; if (std.mem.findScalar(u8, rest, '/')) |at| { modifiers = std.mem.trim(u8, rest[at + 1 ..], " \t\r\n"); rest = std.mem.trim(u8, rest[0..at], " \t\r\n"); } return .{ .action = Mode.parse(action, "manual-action", "automatic-action"), .sending = Mode.parse(sending, "MDN-sent-manually", "MDN-sent-automatically"), .type = Type.parse(rest), .modifiers = modifiers, }; } }; /// A parsed `message/disposition-notification` body. pub const DispositionNotification = struct { fields: Headers, pub fn deinit(self: *DispositionNotification) void { self.fields.deinit(); self.* = undefined; } /// Never fails except on allocation: a receipt about a message that has /// already gone somewhere is not worth refusing to read over a malformed /// field, and every accessor reports what is missing. pub fn parse(gpa: Allocator, body: []const u8) Allocator.Error!DispositionNotification { return .{ .fields = try reports.firstGroup(gpa, body) }; } /// What became of the message. RFC 8098 requires the field; a receipt /// without one says nothing, so this reports its absence. pub fn disposition(self: *DispositionNotification) Allocator.Error!?Disposition { const value = try self.fields.getUnfolded("disposition") orelse return null; return Disposition.parse(value); } /// Who the receipt is about, which is whoever's software sent it. pub fn finalRecipient(self: *DispositionNotification) Allocator.Error!?TypedValue { return self.typed("final-recipient"); } /// The address as the sender wrote it, before any alias expansion. pub fn originalRecipient(self: *DispositionNotification) Allocator.Error!?TypedValue { return self.typed("original-recipient"); } /// The `Message-ID` of the message this is about, which is how a sender /// matches a receipt to what it sent. Without angle brackets. pub fn originalMessageId(self: *DispositionNotification) Allocator.Error!?[]const u8 { const value = try self.fields.getUnfolded("original-message-id") orelse return null; return @import("header/message_id.zig").first(value); } /// The software that sent the receipt, and the host it ran on. pub fn reportingUa(self: *DispositionNotification) Allocator.Error!?TypedValue { return self.typed("reporting-ua"); } /// Why the disposition was what it was, where the reporting system chose /// to say. Free text and not worth deciding anything on. pub fn failure(self: *DispositionNotification) Allocator.Error!?[]const u8 { return self.fields.getUnfolded("failure"); } pub fn @"error"(self: *DispositionNotification) Allocator.Error!?[]const u8 { return self.fields.getUnfolded("error"); } pub fn write(self: DispositionNotification, w: *Io.Writer) Io.Writer.Error!void { return self.fields.writeFields(w); } fn typed(self: *DispositionNotification, name: []const u8) Allocator.Error!?TypedValue { const value = try self.fields.getUnfolded(name) orelse return null; return TypedValue.parse(value); } }; const example = "Reporting-UA: mail.example.net; ExampleMail 4.2\r\n" ++ "Original-Recipient: rfc822; everyone@example.net\r\n" ++ "Final-Recipient: rfc822; alice@example.net\r\n" ++ "Original-Message-ID: \r\n" ++ "Disposition: manual-action/MDN-sent-manually; displayed\r\n"; test "a receipt somebody chose to send" { var receipt: DispositionNotification = try .parse(testing.allocator, example); defer receipt.deinit(); const disposition = (try receipt.disposition()).?; try testing.expectEqual(Type.displayed, disposition.type); try testing.expectEqual(Mode.manual, disposition.action); try testing.expectEqual(Mode.manual, disposition.sending); try testing.expectEqualStrings("", disposition.modifiers); try testing.expectEqualStrings("alice@example.net", (try receipt.finalRecipient()).?.value); try testing.expectEqualStrings("everyone@example.net", (try receipt.originalRecipient()).?.value); try testing.expectEqualStrings("lunch@example.com", (try receipt.originalMessageId()).?); try testing.expectEqualStrings("ExampleMail 4.2", (try receipt.reportingUa()).?.value); try testing.expectEqualStrings("mail.example.net", (try receipt.reportingUa()).?.type); } test "a receipt nobody was asked about" { var receipt: DispositionNotification = try .parse( testing.allocator, "Final-Recipient: rfc822; alice@example.net\r\n" ++ "Disposition: automatic-action/MDN-sent-automatically; deleted/error\r\n" ++ "Error: mailbox over quota\r\n", ); defer receipt.deinit(); const disposition = (try receipt.disposition()).?; try testing.expectEqual(Type.deleted, disposition.type); try testing.expectEqual(Mode.automatic, disposition.action); try testing.expectEqual(Mode.automatic, disposition.sending); // The message was deleted unread, and the modifier says the deletion was // not something anybody chose. try testing.expectEqualStrings("error", disposition.modifiers); try testing.expectEqualStrings("mailbox over quota", (try receipt.@"error"()).?); } test "the shapes that are malformed" { var bare: DispositionNotification = try .parse(testing.allocator, ""); defer bare.deinit(); try testing.expectEqual(null, try bare.disposition()); try testing.expectEqual(null, try bare.finalRecipient()); try testing.expectEqual(null, try bare.originalMessageId()); // A `Disposition` with no modes at all, which RFC 8098 does not allow. var terse: DispositionNotification = try .parse(testing.allocator, "Disposition: displayed\r\n"); defer terse.deinit(); const disposition = (try terse.disposition()).?; try testing.expectEqual(Mode.other, disposition.action); try testing.expectEqual(Mode.other, disposition.sending); // And the type is still read, which is the part worth having. try testing.expectEqual(Type.displayed, disposition.type); } test "anything after the first blank line is not the report" { var receipt: DispositionNotification = try .parse( testing.allocator, example ++ "\r\nDisposition: automatic-action/MDN-sent-automatically; deleted\r\n", ); defer receipt.deinit(); try testing.expectEqual(Type.displayed, (try receipt.disposition()).?.type); } test "a receipt written back out" { var receipt: DispositionNotification = try .parse(testing.allocator, example); defer receipt.deinit(); var out: Io.Writer.Allocating = .init(testing.allocator); defer out.deinit(); try receipt.write(&out.writer); try testing.expectEqualStrings(example, out.written()); }