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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
|
const std = @import("std");
pub const ResponseError = error{Unimplemented};
pub const Identity = union(enum) {
domain: []const u8,
addr: std.Io.net.IpAddress,
};
pub const Message = struct {
str: []const u8,
cmd: Command,
client: Identity,
};
pub const Command = enum {
EHLO,
HELO,
MAIL,
RCPT,
DATA,
RSET,
NOOP,
QUIT,
VRFY,
pub fn fromStr(msg: []const u8) ?Command {
var MSG: [4]u8 = undefined;
if (msg.len != 4) return null;
_ = std.ascii.upperString(&MSG, msg);
if (std.mem.eql(u8, &MSG, "EHLO")) return .EHLO;
if (std.mem.eql(u8, &MSG, "HELO")) return .HELO;
if (std.mem.eql(u8, &MSG, "MAIL")) return .MAIL;
if (std.mem.eql(u8, &MSG, "RCPT")) return .RCPT;
if (std.mem.eql(u8, &MSG, "DATA")) return .DATA;
if (std.mem.eql(u8, &MSG, "RSET")) return .RSET;
if (std.mem.eql(u8, &MSG, "NOOP")) return .NOOP;
if (std.mem.eql(u8, &MSG, "QUIT")) return .QUIT;
if (std.mem.eql(u8, &MSG, "VRFY")) return .VRFY;
return null;
}
};
const Code = enum(u32) {
INIT = 220,
OK = 250,
};
pub const Response = struct {
code: Code,
contents: []const u8,
};
pub fn init(w: *std.Io.Writer, server: *std.Io.net.Server) !Response {
const resp: Response = .{ .code = .INIT, .contents = "nvlope ready" };
try w.print("{d} ", .{resp.code});
var buf: [64]u8 = undefined;
const l = try strip_port(server.socket.address, &buf);
const host = buf[0..l];
try w.print("[{s}]", .{host});
try w.print(" {s}\r\n", .{resp.contents});
try w.flush();
return resp;
}
pub fn respond(w: *std.Io.Writer, msg: Message, server: *std.Io.net.Server) !Response {
return switch (msg.cmd) {
.EHLO => try ehlo(w, msg, null, server),
.HELO => try helo(w, msg, null, server),
else => ResponseError.Unimplemented,
};
}
pub fn helo(w: *std.Io.Writer, msg: Message, domain: ?[]const u8, server: *std.Io.net.Server) !Response {
_ = msg;
var resp: Response = .{ .code = .OK, .contents = undefined };
if (domain) |d| {
resp.contents = d;
try w.print("{d} {s}\r\n", .{ resp.code, resp.contents });
try w.flush();
return resp;
}
try w.print("{d} ", .{resp.code});
var buf: [64]u8 = undefined;
const l = try strip_port(server.socket.address, &buf);
const host: []const u8 = buf[0..l];
try w.print("[{s}]\r\n", .{host});
resp.contents = host;
return resp;
}
pub fn ehlo(w: *std.Io.Writer, msg: Message, domain: ?[]const u8, server: *std.Io.net.Server) !Response {
_ = msg;
var resp: Response = .{ .code = .OK, .contents = undefined };
if (domain) |d| {
resp.contents = d;
try w.print("{d} {s}\r\n", .{ resp.code, resp.contents });
try w.flush();
return resp;
}
try w.print("{d} ", .{resp.code});
var buf: [64]u8 = undefined;
const l = try strip_port(server.socket.address, &buf);
const host: []const u8 = buf[0..l];
try w.print("[{s}]\r\n", .{host});
resp.contents = host;
return resp;
}
fn strip_port(addr: std.Io.net.IpAddress, buf: []u8) !usize {
var fw: std.Io.Writer = .fixed(buf);
try addr.format(&fw);
const full = fw.buffered();
const host = if (std.mem.lastIndexOfScalar(u8, full, ':')) |i| full[0..i] else full;
std.mem.copyForwards(u8, buf, host);
return host.len;
}
|