From 73e4e4490696a7f680cc0ef5e3b6d2d8ebc9c690 Mon Sep 17 00:00:00 2001 From: Nic Gaffney Date: Mon, 19 Oct 2026 20:25:09 -0500 Subject: working on HELO/EHLO response --- src/lib/response.zig | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 73 insertions(+), 2 deletions(-) (limited to 'src/lib/response.zig') diff --git a/src/lib/response.zig b/src/lib/response.zig index 56c327a..06458ff 100644 --- a/src/lib/response.zig +++ b/src/lib/response.zig @@ -1,5 +1,7 @@ const std = @import("std"); +pub const ResponseError = error{Unimplemented}; + pub const Identity = union(enum) { domain: []const u8, addr: std.Io.net.IpAddress, @@ -39,8 +41,77 @@ pub const Command = enum { } }; -const Response = enum(u32) { +const Code = enum(u32) { + INIT = 220, OK = 250, }; -pub fn ehlo_rsp() Response {} +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; +} -- cgit v1.2.3