From 17ba6ecf840e743db2a2a305df73070d0eb69217 Mon Sep 17 00:00:00 2001 From: Nic Gaffney Date: Tue, 20 Oct 2026 04:12:48 -0500 Subject: reorganized --- src/bin/app.zig | 124 +++++++++++++++++++++++++++++++++++++++++++++++++ src/bin/main.zig | 11 +++-- src/bin/response.zig | 129 --------------------------------------------------- 3 files changed, 130 insertions(+), 134 deletions(-) create mode 100644 src/bin/app.zig delete mode 100644 src/bin/response.zig (limited to 'src/bin') diff --git a/src/bin/app.zig b/src/bin/app.zig new file mode 100644 index 0000000..512c2f7 --- /dev/null +++ b/src/bin/app.zig @@ -0,0 +1,124 @@ +const std = @import("std"); +const nv = @import("nvlope"); +const Allocator = std.mem.Allocator; + +pub const App = @This(); + +mailbox: []const u8 = "./Mail", +gpa: Allocator, +domain: []const u8 = "localhost", +sender: ?[]const u8 = null, +recipients: std.ArrayList([]const u8) = .empty, +body: std.ArrayList(u8) = .empty, +state: enum(u8) { + init = 0, + start, // post 220 send + ready = 2, // EHLO / RSET + mail, // MAIL + rcpt, // RCPT +} = .init, + +const max_rcpt = 100; +const max_size = 512; +const max_body = 10 * 1024 * 1024; + +pub fn appendBodyLine(self: *App, line: []const u8) !enum { ok, too_large } { + const l = if (std.mem.startsWith(u8, line, "..")) line[1..] else line; + if (self.body.items.len + l.len > max_body) return .too_large; + try self.body.appendSlice(self.gpa, l); + return .ok; +} + +pub fn resetTransaction(self: *App) void { + if (@intFromEnum(self.state) <= 2) self.state = .ready else self.state = .start; + if (self.sender) |s| self.gpa.free(s); + self.sender = null; + for (self.recipients.items) |r| self.gpa.free(r); + self.recipients.clearRetainingCapacity(); + self.body.clearRetainingCapacity(); +} + +pub fn ehlo(self: *App, arena: Allocator, id: nv.Identity) !nv.Reply { + self.resetTransaction(); + var tmp: [64]u8 = undefined; + const who = try id.toString(&tmp); + const lines = try arena.alloc([]const u8, 3); + lines[0] = try std.fmt.allocPrint(arena, "{s} greets {s}", .{ self.domain, who }); + lines[1] = "8BITMIME"; + lines[2] = "SIZE 35882577"; + self.state = .ready; + return .{ .code = .ok, .lines = lines }; +} + +pub fn helo(self: *App, arena: Allocator, id: nv.Identity) !nv.Reply { + self.resetTransaction(); + _ = id; + const line = try std.fmt.allocPrint(arena, "{s}", .{self.domain}); + self.state = .ready; + return .{ .code = .ok, .lines = try arena.dupe([]const u8, &.{line}) }; +} + +pub fn mail(self: *App, arena: Allocator, m: nv.MailFrom) !nv.Reply { + _ = arena; + if (@intFromEnum(self.state) < 2) return nv.Reply.single(.bad_sequence, "Need EHLO before MAIL"); + var it = m.paramIterator(); + while (it.next()) |p| { + if (std.ascii.eqlIgnoreCase(p.key, "SIZE")) { + const n = std.fmt.parseInt(usize, p.value orelse "", 10) catch + return nv.Reply.single(.bad_param, "Bad SIZE parameter"); + if (n > max_size) return nv.Reply.single(.exceeded, "Message too large"); + } else if (!std.ascii.eqlIgnoreCase(p.key, "BODY")) { + return nv.Reply.single(.param_not_impl, "Unrecognized parameter"); + } + } + self.state = .mail; + if (self.sender) |old| self.gpa.free(old); + self.sender = try self.gpa.dupe(u8, m.path); + return nv.Reply.single(.ok, "Sender OK"); +} + +pub fn rcpt(self: *App, arena: Allocator, r: nv.RcptTo) !nv.Reply { + _ = arena; + if (self.sender == null) return nv.Reply.single(.bad_sequence, "Need MAIL before RCPT"); + if (self.recipients.items.len >= max_rcpt) + return nv.Reply.single(.too_many, "Too many recipients"); + + var it = r.paramIterator(); + if (it.next() != null) // NOTIFY/ORCPT not supported + return nv.Reply.single(.param_unrecognized, "Unrecognized parameter"); + + const owned = try self.gpa.dupe(u8, r.path); + errdefer self.gpa.free(owned); + try self.recipients.append(self.gpa, owned); + return nv.Reply.single(.ok, "OK"); +} + +pub fn rset(self: *App, arena: Allocator, _: void) !nv.Reply { + _ = arena; + self.resetTransaction(); + return nv.Reply.single(.ok, "OK"); +} + +pub fn noop(self: *App, arena: Allocator, _: void) !nv.Reply { + _ = self; + _ = arena; + return nv.Reply.single(.ok, "OK"); +} + +pub fn data(self: *App, arena: Allocator, _: void) !nv.Reply { + _ = self; + const line = "Start mail input; end with ."; + return .{ .code = .start_data, .lines = try arena.dupe([]const u8, &.{line}), .action = .data }; +} + +pub fn vrfy(self: *App, arena: Allocator, who: []const u8) !nv.Reply { + _ = self; + _ = arena; + _ = who; + return nv.Reply.single(.cannot_verify, "Cannot VRFY user, but will accept message and attempt delivery"); +} + +pub fn quit(self: *App, arena: Allocator, _: void) !nv.Reply { + const line = try std.fmt.allocPrint(arena, "{s} Service closing transmission channel", .{self.domain}); + return .{ .code = .closing, .lines = try arena.dupe([]const u8, &.{line}), .action = .close }; +} diff --git a/src/bin/main.zig b/src/bin/main.zig index 634676d..6784263 100644 --- a/src/bin/main.zig +++ b/src/bin/main.zig @@ -1,7 +1,6 @@ const std = @import("std"); const Io = std.Io; -const Response = @import("response.zig"); -const App = Response.App; +const App = @import("app.zig"); const nv = @import("nvlope"); @@ -10,7 +9,7 @@ pub fn main(init: std.process.Init) !void { const alloc = std.heap.smp_allocator; // Initialize server on localhost at the specified port - const port = 8080; + const port = 2525; const addr = try Io.net.IpAddress.parse("127.0.0.1", port); var server = try addr.listen(io, .{ .mode = .stream, @@ -22,8 +21,9 @@ pub fn main(init: std.process.Init) !void { var group: Io.Group = .init; defer group.cancel(io); + // Async delivery worker here + while (true) { - std.debug.print("Waiting for conn...\n", .{}); const stream = try server.accept(io); group.async(io, serve, .{ alloc, io, stream }); } @@ -52,7 +52,7 @@ fn session(alloc: std.mem.Allocator, io: Io, stream: *Io.net.Stream) !void { var app: App = .{ .gpa = alloc }; // Greeting: the server speaks first - try nv.Reply.single(.ready, "localhost ESMTP ready").send(w); + try nv.Reply.single(.ready, "nvlope ready").send(w); try w.flush(); var in_data = false; @@ -74,6 +74,7 @@ fn session(alloc: std.mem.Allocator, io: Io, stream: *Io.net.Stream) !void { in_data = false; try nv.Reply.single(.ok, "Queued").send(w); try w.flush(); + std.debug.print("App {{\n\t body: {s}\n\tsender: {s}\n\trecipients: {any}\n}}\n", .{ app.body.items, app.sender.?, app.recipients.items }); } else { const status = try app.appendBodyLine(line); switch (status) { diff --git a/src/bin/response.zig b/src/bin/response.zig deleted file mode 100644 index 72b8cbd..0000000 --- a/src/bin/response.zig +++ /dev/null @@ -1,129 +0,0 @@ -const std = @import("std"); -const nv = @import("nvlope"); -const Allocator = std.mem.Allocator; - -pub const App = struct { - mailbox: std.fs.path, - gpa: Allocator, - domain: []const u8 = "localhost", - sender: ?[]const u8 = null, - recipients: std.ArrayList([]const u8) = .empty, - body: std.ArrayList(u8) = .empty, - state: enum(u8) { - init = 0, - start, // post 220 send - ready = 2, // EHLO / RSET - mail, // MAIL - rcpt, // RCPT - } = .init, - - const max_rcpt = 100; - const max_size = 512; - const max_body = 10 * 1024 * 1024; - - pub fn appendBodyLine(self: *App, line: []const u8) !enum { ok, too_large } { - const l = if (std.mem.startsWith(u8, line, "..")) line[1..] else line; - if (self.body.items.len + l.len > max_body) return .too_large; - try self.body.appendSlice(self.gpa, l); - return .ok; - } - - pub fn resetTransaction(self: *App) void { - if (@intFromEnum(self.state) <= 2) self.state = .ready else self.state = .start; - self.sender = null; - for (self.recipients.items) |r| self.gpa.free(r); - self.recipients.clearRetainingCapacity(); - self.body.clearRetainingCapacity(); - } - - pub fn init(self: *App, arena: Allocator, id: nv.Identity) !nv.Reply { - _ = id; - self.state = .start; - const line = "nvlope ready"; - return .{ .code = .init, .lines = try arena.dupe([]const u8, &.{line}) }; - } - - pub fn ehlo(self: *App, arena: Allocator, id: nv.Identity) !nv.Reply { - self.resetTransaction(); - var tmp: [64]u8 = undefined; - const who = try id.toString(&tmp); - const lines = try arena.alloc([]const u8, 3); - lines[0] = try std.fmt.allocPrint(arena, "{s} greets {s}", .{ self.domain, who }); - lines[1] = "8BITMIME"; - lines[2] = "SIZE 35882577"; - self.state = .ready; - return .{ .code = .ok, .lines = lines }; - } - - pub fn helo(self: *App, arena: Allocator, id: nv.Identity) !nv.Reply { - self.resetTransaction(); - _ = id; - const line = try std.fmt.allocPrint(arena, "{s}", .{self.domain}); - self.state = .ready; - return .{ .code = .ok, .lines = try arena.dupe([]const u8, &.{line}) }; - } - - pub fn mail(self: *App, arena: Allocator, m: nv.MailFrom) !nv.Reply { - _ = arena; - if (@intFromEnum(self.state) < 2) return nv.Reply.single(.bad_sequence, "Need EHLO before MAIL"); - var it = m.paramIterator(); - while (it.next()) |p| { - if (std.ascii.eqlIgnoreCase(p.key, "SIZE")) { - const n = std.fmt.parseInt(usize, p.value orelse "", 10) catch - return nv.Reply.single(.bad_param, "Bad SIZE parameter"); - if (n > max_size) return nv.Reply.single(.exceeded, "Message too large"); - } else if (!std.ascii.eqlIgnoreCase(p.key, "BODY")) { - return nv.Reply.single(.param_not_impl, "Unrecognized parameter"); - } - } - self.sender = m.path; // dupe it if it must outlive this line - self.state = .mail; - return nv.Reply.single(.ok, "OK"); - } - - pub fn rcpt(self: *App, arena: Allocator, r: nv.RcptTo) !nv.Reply { - _ = arena; - if (self.sender == null) return nv.Reply.single(.bad_sequence, "Need MAIL before RCPT"); - if (self.recipients.items.len >= max_rcpt) - return nv.Reply.single(.too_many, "Too many recipients"); - - var it = r.paramIterator(); - if (it.next() != null) // NOTIFY/ORCPT not supported - return nv.Reply.single(.param_unrecognized, "Unrecognized parameter"); - - const owned = try self.gpa.dupe(u8, r.path); - errdefer self.gpa.free(owned); - try self.recipients.append(self.gpa, owned); - return nv.Reply.single(.ok, "OK"); - } - - pub fn rset(self: *App, arena: Allocator, _: void) !nv.Reply { - _ = arena; - self.resetTransaction(); - return nv.Reply.single(.ok, "OK"); - } - - pub fn noop(self: *App, arena: Allocator, _: void) !nv.Reply { - _ = self; - _ = arena; - return nv.Reply.single(.ok, "OK"); - } - - pub fn data(self: *App, arena: Allocator, _: void) !nv.Reply { - _ = self; - const line = "Start mail input; end with ."; - return .{ .code = .start_data, .lines = try arena.dupe([]const u8, &.{line}), .action = .data }; - } - - pub fn vrfy(self: *App, arena: Allocator, who: []const u8) !nv.Reply { - _ = self; - _ = arena; - _ = who; - return nv.Reply.single(.cannot_verify, "Cannot VRFY user, but will accept message and attempt delivery"); - } - - pub fn quit(self: *App, arena: Allocator, _: void) !nv.Reply { - const line = try std.fmt.allocPrint(arena, "{s} Service closing transmission channel", .{self.domain}); - return .{ .code = .closing, .lines = try arena.dupe([]const u8, &.{line}), .action = .close }; - } -}; -- cgit v1.2.3