diff options
Diffstat (limited to 'src/lib/commands.zig')
| -rw-r--r-- | src/lib/commands.zig | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/src/lib/commands.zig b/src/lib/commands.zig new file mode 100644 index 0000000..719e93f --- /dev/null +++ b/src/lib/commands.zig @@ -0,0 +1,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, +}; |
