summaryrefslogtreecommitdiff
path: root/src/bin/trace.zig
blob: a2fb3944fa4fe998cacaa1e3515fd5e307829f08 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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(),
    });
}