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; }