diff options
| author | Nic Gaffney <gaffney_nic@protonmail.com> | 2026-10-20 03:35:56 -0500 |
|---|---|---|
| committer | Nic Gaffney <gaffney_nic@protonmail.com> | 2026-10-20 03:35:56 -0500 |
| commit | afac4c8c7780cc40bb41d8bae97a4ae83a79db9f (patch) | |
| tree | 63b7efe0b995630002a662738013d3fcb885df29 /src/lib/response.zig | |
| parent | 73e4e4490696a7f680cc0ef5e3b6d2d8ebc9c690 (diff) | |
| download | nvlope-afac4c8c7780cc40bb41d8bae97a4ae83a79db9f.tar.gz | |
Finished minimal message impl
Diffstat (limited to 'src/lib/response.zig')
| -rw-r--r-- | src/lib/response.zig | 117 |
1 files changed, 0 insertions, 117 deletions
diff --git a/src/lib/response.zig b/src/lib/response.zig deleted file mode 100644 index 06458ff..0000000 --- a/src/lib/response.zig +++ /dev/null @@ -1,117 +0,0 @@ -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; -} |
