summaryrefslogtreecommitdiff
path: root/src/lib/commands.zig
blob: 719e93f5fa8229491d0094feb6a9d057c7e2b427 (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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
const IpAddress = @import("std").Io.net.IpAddress;
const std = @import("std");

pub const Identity = union(enum) {
    domain: []const u8,
    addr: IpAddress,

    pub fn toString(self: Identity, buf: []u8) ![]const u8 {
        return switch (self) {
            .domain => self.domain,
            .addr => blk: {
                var fw: std.Io.Writer = .fixed(buf);
                try self.addr.format(&fw);
                const full = fw.buffered();
                const host: []const u8 = if (std.mem.lastIndexOfScalar(u8, full, ':')) |i| full[0..i] else full;
                break :blk host;
            },
        };
    }
};

pub const Path = struct {
    path: []const u8,
    params: []const u8,

    pub const Param = struct { key: []const u8, value: ?[]const u8 };

    pub const ParamIterator = struct {
        it: std.mem.TokenIterator(u8, .scalar),

        pub fn next(self: *ParamIterator) ?Param {
            const tok = self.it.next() orelse return null;
            if (std.mem.indexOfScalar(u8, tok, '=')) |i|
                return .{ .key = tok[0..i], .value = tok[i + 1 ..] };
            return .{ .key = tok, .value = null };
        }
    };

    pub fn paramIterator(self: Path) ParamIterator {
        return .{ .it = std.mem.tokenizeScalar(u8, self.params, ' ') };
    }
};

pub const MailFrom = Path;
pub const RcptTo = Path;

pub const User = struct {
    name: []const u8, // local part; a quoted string keeps its quotes
    host: ?Identity, // null only for <postmaster>
};

pub const Verb = enum { ehlo, helo, mail, rcpt, data, rset, noop, quit, vrfy };

pub const Command = union(Verb) {
    ehlo: Identity,
    helo: Identity,
    mail: MailFrom,
    rcpt: RcptTo,
    data: void,
    rset: void,
    noop: void,
    quit: void,
    vrfy: []const u8,
};