summaryrefslogtreecommitdiff
path: root/src/bin/app.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/app.zig
parentc979431b49259eb90e92a96a427906242f5e3802 (diff)
downloadnvlope-f9ad26b8a4eb91506b48c3f8275226a8983cd694.tar.gz
Functioning local mailserver
Diffstat (limited to 'src/bin/app.zig')
-rw-r--r--src/bin/app.zig82
1 files changed, 70 insertions, 12 deletions
diff --git a/src/bin/app.zig b/src/bin/app.zig
index e3b76fd..474592e 100644
--- a/src/bin/app.zig
+++ b/src/bin/app.zig
@@ -3,6 +3,7 @@ const nv = @import("nvlope");
const Allocator = std.mem.Allocator;
const normalizeUser = @import("util.zig").normalizeUser;
const Store = @import("store.zig");
+const Trace = @import("trace.zig");
pub const App = @This();
@@ -20,27 +21,71 @@ state: enum(u8) {
rcpt, // RCPT
} = .init,
store: *Store,
+peer: []const u8,
+helo_ident: ?[]u8 = null,
+esmtp: bool = false,
const max_rcpt = 100;
const max_size = 512;
const max_body = 10 * 1024 * 1024;
+var next_id: std.atomic.Value(u64) = .init(0);
+
+fn makeId(buf: []u8, nanos: i96, host: []const u8) ![]const u8 {
+ const n = next_id.fetchAdd(1, .monotonic);
+ return std.fmt.bufPrint(buf, "{d}.{d}.{s}", .{ nanos, n, host });
+}
+
fn acceptLocal(self: *App, raw: []const u8) !nv.Reply {
_ = self;
_ = raw;
return error.NotImplemented;
}
-pub fn queue(self: *App) void {
+pub fn queue(self: *App) !void {
for (self.recipients.items) |rc| {
- const at = std.mem.lastIndexOfScalar(u8, rc, '@') orelse rc.len;
- const local = rc.path[0..at];
- const domain = rc.path[at + 1 ..];
- if (std.ascii.eqlIgnoreCase(domain, self.domain) and self.store.exists(self.io, local)) {
- var mailbox = try self.store.maildir.openDir(self.io, local, .{});
+ const at = std.mem.lastIndexOfScalar(u8, rc, '@') orelse continue;
+ const local = rc[0..at];
+ const domain = rc[at + 1 ..];
+ if (std.ascii.eqlIgnoreCase(domain, self.domain) and try self.store.exists(self.io, local)) {
+ var maildir = try self.store.maildir.openDir(self.io, "mailboxes", .{});
+ defer maildir.close(self.io);
+ var mailbox = try maildir.openDir(self.io, local, .{});
defer mailbox.close(self.io);
- var tmp = try mailbox.openDir(self.io, "tmp");
- tmp.createFile(self.io, "MAAAIL", .{.lock});
+ var tmp = try mailbox.openDir(self.io, "tmp", .{});
+ defer tmp.close(self.io);
+
+ const nano = std.Io.Timestamp.now(self.io, .real).nanoseconds;
+ var buf: [64]u8 = undefined;
+ const id = try makeId(&buf, nano, domain);
+
+ const trace: Trace = .{
+ .sender = self.sender.?,
+ .helo = self.helo_ident orelse "unknown",
+ .peer = self.peer,
+ .by = self.domain,
+ .esmtp = self.esmtp,
+ .id = id,
+ .rcpt = rc,
+ .secs = @divFloor(nano, std.time.ns_per_s),
+ };
+
+ var file = try tmp.createFile(self.io, id, .{ .exclusive = true });
+ errdefer tmp.deleteFile(self.io, id) catch {};
+
+ var fbuf: [512]u8 = undefined;
+ var fwriter = file.writer(self.io, &fbuf);
+ const writer = &fwriter.interface;
+ try trace.writeTrace(writer);
+ try writer.writeAll(self.body.items);
+ try writer.flush();
+ try file.sync(self.io);
+ file.close(self.io);
+
+ const new = try mailbox.openDir(self.io, "new", .{});
+ try tmp.rename(id, new, id, self.io);
+ } else {
+ // Relay / user not defined
}
}
}
@@ -63,21 +108,34 @@ pub fn resetTransaction(self: *App) void {
pub fn ehlo(self: *App, arena: Allocator, id: nv.Identity) !nv.Reply {
self.resetTransaction();
- var tmp: [64]u8 = undefined;
+ var tmp: [255]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;
+
+ var buf: [255]u8 = undefined;
+ 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 '?';
+ self.helo_ident = try arena.dupe(u8, buf[0..n]);
+
+ self.esmtp = true;
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;
+ var tmp: [255]u8 = undefined;
+ const who = try id.toString(&tmp);
+ const line = try std.fmt.allocPrint(arena, "{s}", .{self.domain});
+
+ var buf: [255]u8 = undefined;
+ 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 '?';
+ self.helo_ident = try arena.dupe(u8, buf[0..n]);
return .{ .code = .ok, .lines = try arena.dupe([]const u8, &.{line}) };
}
@@ -141,7 +199,7 @@ pub fn noop(self: *App, arena: Allocator, _: void) !nv.Reply {
}
pub fn data(self: *App, arena: Allocator, _: void) !nv.Reply {
- _ = self;
+ if (self.recipients.items.len == 0) return nv.Reply.single(.bad_sequence, "Need RCPT before DATA");
const line = "Start mail input; end with <CRLF>.<CRLF>";
return .{ .code = .start_data, .lines = try arena.dupe([]const u8, &.{line}), .action = .data };
}