summaryrefslogtreecommitdiff
path: root/src/bin/main.zig
diff options
context:
space:
mode:
authorNic Gaffney <gaffney_nic@protonmail.com>2026-10-21 04:00:42 -0500
committerNic Gaffney <gaffney_nic@protonmail.com>2026-10-21 04:00:42 -0500
commitf9ad26b8a4eb91506b48c3f8275226a8983cd694 (patch)
tree553973488b668136c4d9cd1768a4897e3553602d /src/bin/main.zig
parentc979431b49259eb90e92a96a427906242f5e3802 (diff)
downloadnvlope-f9ad26b8a4eb91506b48c3f8275226a8983cd694.tar.gz
Functioning local mailserver
Diffstat (limited to 'src/bin/main.zig')
-rw-r--r--src/bin/main.zig18
1 files changed, 16 insertions, 2 deletions
diff --git a/src/bin/main.zig b/src/bin/main.zig
index 1a9243b..db41f36 100644
--- a/src/bin/main.zig
+++ b/src/bin/main.zig
@@ -63,11 +63,21 @@ fn session(alloc: std.mem.Allocator, io: Io, stream: *Io.net.Stream) !void {
var writer = stream.writer(io, &wbuf);
const w = &writer.interface;
+ var tmp: [255]u8 = undefined;
+ var buf: [255]u8 = undefined;
+ var fbuf: [255]u8 = undefined;
+ const id = nv.Identity{ .addr = stream.socket.address };
+ const who = try id.toString(&tmp);
+ const n = @min(who.len, buf.len);
+ for (who[0..n], buf[0..n]) |c, *o| o.* = if (c > 0x20 and c < 0x7f) c else '?';
+ var fw: std.Io.Writer = .fixed(&fbuf);
+ try util.literal(&fw, who);
+
// Per-connection state
var arena: std.heap.ArenaAllocator = .init(alloc);
defer arena.deinit();
var store: Store = try .init(io, MAILDIR);
- var app: App = .{ .gpa = alloc, .store = &store, .io = io };
+ var app: App = .{ .gpa = alloc, .store = &store, .io = io, .peer = fw.buffered() };
// Greeting: the server speaks first
try nv.Reply.single(.ready, "nvlope ready").send(w);
@@ -90,7 +100,11 @@ fn session(alloc: std.mem.Allocator, io: Io, stream: *Io.net.Stream) !void {
if (in_data) {
if (std.mem.eql(u8, line, ".\r\n")) {
in_data = false;
- app.queue();
+ app.queue() catch {
+ try nv.Reply.single(.storage_error, "Internal storage error").send(w);
+ try w.flush();
+ continue;
+ };
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 });