// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie // SPDX-License-Identifier: MIT //! `message/feedback-report` — RFC 5965 — the Abuse Reporting Format, which is //! what arrives when somebody presses "this is spam". //! //! A mailbox provider that lets its users report mail sends the report back to //! whoever sent the mail, through a *feedback loop*. It is the same //! `multipart/report` envelope as a bounce and a read receipt, with //! `report-type=feedback-report`, and the same header syntax in the //! machine-readable part: //! //! ```text //! Feedback-Type: abuse //! User-Agent: SomeGenerator/1.0 //! Version: 1 //! Original-Mail-From: //! Arrival-Date: Tue, 1 Jul 2003 10:52:37 +0200 //! Source-IP: 192.0.2.1 //! Reported-Domain: example.com //! ``` //! //! What a sender does with one is unsubscribe the complainant, immediately and //! without asking, which is why the identity of that complainant is usually //! *not* in the report: RFC 6590 has providers redact it, and the third part of //! the report — the message that was complained about — is where a sender finds //! its own tracking identifier instead. A report with no recipient in it is //! therefore the normal case rather than a defect. const std = @import("std"); const Allocator = std.mem.Allocator; const Io = std.Io; const testing = std.testing; const Headers = @import("Headers.zig"); const date = @import("header/date.zig"); const reports = @import("report.zig"); pub const TypedValue = reports.TypedValue; /// What is being complained about. The registry is open; these are the values /// RFC 5965 §7.3 registered. pub const Type = enum { /// Unsolicited mail. Almost every report is this one. abuse, /// Phishing or another deception. fraud, /// The message carried malware. virus, /// A report that something was marked as spam and should not have been, /// which is a correction rather than a complaint. not_spam, /// Something else, or something registered since. other, pub fn parse(text: []const u8) Type { const trimmed = std.mem.trim(u8, text, " \t\r\n"); const table = .{ .{ "abuse", Type.abuse }, .{ "fraud", Type.fraud }, .{ "virus", Type.virus }, .{ "not-spam", Type.not_spam }, }; 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) { .abuse => "abuse", .fraud => "fraud", .virus => "virus", .not_spam => "not-spam", .other => "other", }; } }; /// A parsed `message/feedback-report` body. pub const FeedbackReport = struct { fields: Headers, pub fn deinit(self: *FeedbackReport) void { self.fields.deinit(); self.* = undefined; } pub fn parse(gpa: Allocator, body: []const u8) Allocator.Error!FeedbackReport { return .{ .fields = try reports.firstGroup(gpa, body) }; } /// What is being complained about. RFC 5965 requires the field. pub fn feedbackType(self: *FeedbackReport) Allocator.Error!Type { const value = try self.fields.getUnfolded("feedback-type") orelse return .other; return Type.parse(value); } /// The software that generated the report. pub fn userAgent(self: *FeedbackReport) Allocator.Error!?[]const u8 { return self.fields.getUnfolded("user-agent"); } /// The format version, which is `1` and has been since 2010. pub fn version(self: *FeedbackReport) Allocator.Error!?u32 { const value = try self.fields.getUnfolded("version") orelse return null; return std.fmt.parseInt(u32, std.mem.trim(u8, value, " \t"), 10) catch null; } /// The envelope sender of the message complained about — the address a /// bounce would have gone to, and the one a sender matches against its own /// records. pub fn originalMailFrom(self: *FeedbackReport) Allocator.Error!?[]const u8 { return self.fields.getUnfolded("original-mail-from"); } /// The complainant, where the provider chose to say. Usually absent: RFC /// 6590 has providers redact it, and a report without one is the normal /// case rather than a broken one. pub fn originalRcptTo(self: *FeedbackReport) Allocator.Error!?[]const u8 { return self.fields.getUnfolded("original-rcpt-to"); } /// The address the sender should stop mailing, where the provider named /// one explicitly. pub fn removalRecipient(self: *FeedbackReport) Allocator.Error!?[]const u8 { return self.fields.getUnfolded("removal-recipient"); } /// The address the message was received from. pub fn sourceIp(self: *FeedbackReport) Allocator.Error!?[]const u8 { return self.fields.getUnfolded("source-ip"); } /// The domain the complaint is against. pub fn reportedDomain(self: *FeedbackReport) Allocator.Error!?[]const u8 { return self.fields.getUnfolded("reported-domain"); } /// When the message arrived at the provider. pub fn arrivalDate(self: *FeedbackReport) Allocator.Error!?date.Parsed { const value = try self.fields.getUnfolded("arrival-date") orelse return null; return date.parse(value) catch null; } /// How many complaints this report stands for, where a provider batches /// them. Absent means one. pub fn incidents(self: *FeedbackReport) Allocator.Error!u32 { const value = try self.fields.getUnfolded("incidents") orelse return 1; return std.fmt.parseInt(u32, std.mem.trim(u8, value, " \t"), 10) catch 1; } pub fn write(self: FeedbackReport, w: *Io.Writer) Io.Writer.Error!void { return self.fields.writeFields(w); } }; const example = "Feedback-Type: abuse\r\n" ++ "User-Agent: SomeGenerator/1.0\r\n" ++ "Version: 1\r\n" ++ "Original-Mail-From: \r\n" ++ "Arrival-Date: Tue, 1 Jul 2003 10:52:37 +0200\r\n" ++ "Source-IP: 192.0.2.1\r\n" ++ "Reported-Domain: example.com\r\n" ++ "Incidents: 4\r\n"; test "a complaint" { var complaint: FeedbackReport = try .parse(testing.allocator, example); defer complaint.deinit(); try testing.expectEqual(Type.abuse, try complaint.feedbackType()); try testing.expectEqualStrings("SomeGenerator/1.0", (try complaint.userAgent()).?); try testing.expectEqual(1, (try complaint.version()).?); try testing.expectEqualStrings("", (try complaint.originalMailFrom()).?); try testing.expectEqualStrings("192.0.2.1", (try complaint.sourceIp()).?); try testing.expectEqualStrings("example.com", (try complaint.reportedDomain()).?); try testing.expectEqual(2003, (try complaint.arrivalDate()).?.time.year); try testing.expectEqual(4, try complaint.incidents()); // The complainant is not in it, which is what RFC 6590 asks of providers. try testing.expectEqual(null, try complaint.originalRcptTo()); } test "the defaults are the common cases" { var bare: FeedbackReport = try .parse(testing.allocator, "Feedback-Type: not-spam\r\n"); defer bare.deinit(); try testing.expectEqual(Type.not_spam, try bare.feedbackType()); // A report that does not say stands for one complaint. try testing.expectEqual(1, try bare.incidents()); try testing.expectEqual(null, try bare.version()); try testing.expectEqual(null, try bare.userAgent()); } test "an unregistered type is not guessed at" { var odd: FeedbackReport = try .parse(testing.allocator, "Feedback-Type: opt-out\r\n"); defer odd.deinit(); try testing.expectEqual(Type.other, try odd.feedbackType()); // And the value is still there for a caller that knows what it means. try testing.expectEqualStrings("opt-out", (try odd.fields.getUnfolded("feedback-type")).?); } test "a complaint written back out" { var complaint: FeedbackReport = try .parse(testing.allocator, example); defer complaint.deinit(); var out: Io.Writer.Allocating = .init(testing.allocator); defer out.deinit(); try complaint.write(&out.writer); try testing.expectEqualStrings(example, out.written()); }