const std = @import("std"); const Code = enum(u16) { OK = 250, UNKNOWN = 404, }; const Response = struct { code: Code, pass: bool, msg: []const u8, }; const Message = struct { name: []const u8, pass: []const Code, fail: []const Code, handle: fn (*std.Io.Writer, *std.Io.net.Server, args: anytype) anyerror!Response, }; const Messages = union(enum(u8)) { HELO = Message{ .name = "HELO", .pass = .{.OK}, .fail = .{.UNKNOWN}, .handle = blk: { const f = struct { pub fn handle(w: *std.Io.Writer, s: *std.Io.net.Server, args: anytype) !Response { var resp: Response = .{ .code = .OK, .pass = true, .contents = undefined }; if (args.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; var fw: std.Io.Writer = .fixed(&buf); try s.socket.address.format(&fw); const full = fw.buffered(); const host = if (std.mem.lastIndexOfScalar(u8, full, ':')) |i| full[0..i] else full; try w.print("[{s}]\r\n", .{host}); resp.contents = host; return resp; } }; break :blk f.handle; }, }, };