summaryrefslogtreecommitdiff
path: root/src/lib/response.zig
diff options
context:
space:
mode:
authorNic Gaffney <gaffney_nic@protonmail.com>2026-10-19 20:25:09 -0500
committerNic Gaffney <gaffney_nic@protonmail.com>2026-10-19 20:25:09 -0500
commit73e4e4490696a7f680cc0ef5e3b6d2d8ebc9c690 (patch)
tree5360ab60fa5e67a7ce356253d3eab875c007be98 /src/lib/response.zig
parentdbf5e0958a6e13c9e36a0b1a29771a708f407c20 (diff)
downloadnvlope-73e4e4490696a7f680cc0ef5e3b6d2d8ebc9c690.tar.gz
working on HELO/EHLO response
Diffstat (limited to 'src/lib/response.zig')
-rw-r--r--src/lib/response.zig75
1 files changed, 73 insertions, 2 deletions
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;
+}