diff options
Diffstat (limited to 'src/bin/trace.zig')
| -rw-r--r-- | src/bin/trace.zig | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/src/bin/trace.zig b/src/bin/trace.zig new file mode 100644 index 0000000..a2fb394 --- /dev/null +++ b/src/bin/trace.zig @@ -0,0 +1,46 @@ +const std = @import("std"); +const Io = std.Io; + +sender: []const u8, +helo: []const u8, +peer: []const u8, +by: []const u8, +esmtp: bool, +id: []const u8, +rcpt: []const u8, +secs: i96, + +const Self = @This(); +const Trace = @This(); + +pub fn writeTrace(t: Trace, w: *Io.Writer) !void { + try w.print("Return-Path: <{s}>\r\n", .{t.sender}); + try w.print( + "Received: from {s} ({s})\r\n\tby {s} with {s} id {s}\r\n\tfor <{s}>; ", + .{ t.helo, t.peer, t.by, if (t.esmtp) "ESMTP" else "SMTP", t.id, t.rcpt }, + ); + try writeDate(w, t.secs); + try w.writeAll("\r\n"); +} + +fn writeDate(w: *Io.Writer, secs: i96) !void { + const days = [_][]const u8{ "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }; + const mons = [_][]const u8{ "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; + + const es: std.time.epoch.EpochSeconds = .{ .secs = @intCast(secs) }; + const ed = es.getEpochDay(); + const yd = ed.calculateYearDay(); + const md = yd.calculateMonthDay(); + const ds = es.getDaySeconds(); + const dow = (ed.day + 4) % 7; + + try w.print("{s}, {d} {s} {d} {d:0>2}:{d:0>2}:{d:0>2} +0000", .{ + days[dow], + md.day_index + 1, + mons[md.month.numeric() - 1], + yd.year, + ds.getHoursIntoDay(), + ds.getMinutesIntoHour(), + ds.getSecondsIntoMinute(), + }); +} |
