summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNic Gaffney <gaffney_nic@protonmail.com>2026-10-20 18:57:33 -0500
committerNic Gaffney <gaffney_nic@protonmail.com>2026-10-20 18:57:33 -0500
commitad821a3e2cfb535c62ac2041c951433b6e99db58 (patch)
treec48f939a28be8ffe4f1ab3f291c7510dfe606ab4
parent735cb3fedd5f8adf89cfc869103e4e7992a61139 (diff)
downloadnvlope-ad821a3e2cfb535c62ac2041c951433b6e99db58.tar.gz
Spool in progress
-rw-r--r--src/bin/app.zig19
-rw-r--r--src/bin/store.zig57
-rw-r--r--src/bin/util.zig15
3 files changed, 89 insertions, 2 deletions
diff --git a/src/bin/app.zig b/src/bin/app.zig
index 512c2f7..46ba178 100644
--- a/src/bin/app.zig
+++ b/src/bin/app.zig
@@ -1,10 +1,12 @@
const std = @import("std");
const nv = @import("nvlope");
const Allocator = std.mem.Allocator;
+const normalizeUser = @import("util.zig").normalizeUser;
+const Store = @import("store.zig");
pub const App = @This();
-mailbox: []const u8 = "./Mail",
+comptime maildir: []const u8 = "./Mail",
gpa: Allocator,
domain: []const u8 = "localhost",
sender: ?[]const u8 = null,
@@ -17,6 +19,7 @@ state: enum(u8) {
mail, // MAIL
rcpt, // RCPT
} = .init,
+store: *Store,
const max_rcpt = 100;
const max_size = 512;
@@ -84,9 +87,21 @@ pub fn rcpt(self: *App, arena: Allocator, r: nv.RcptTo) !nv.Reply {
return nv.Reply.single(.too_many, "Too many recipients");
var it = r.paramIterator();
- if (it.next() != null) // NOTIFY/ORCPT not supported
+ if (it.next() != null)
return nv.Reply.single(.param_unrecognized, "Unrecognized parameter");
+ const at = std.mem.lastIndexOfScalar(u8, r.path, '@') orelse
+ return self.acceptLocal(std.mem.trim(u8, r.path, " "));
+ const local = r.path[0..at];
+ const domain = r.path[at + 1 ..];
+ if (!std.ascii.eqlIgnoreCase(domain, self.domain))
+ return nv.Reply.single(.relay_denied, "Relay access denied"); // 554
+ var buf: [64]u8 = undefined;
+ const user = normalizeUser(&buf, local) catch
+ return nv.Reply.single(.mailbox_unavailable, "No such user"); // 550
+ if (!self.store.exists(user))
+ return nv.Reply.single(.mailbox_unavailable, "No such user"); // 550
+
const owned = try self.gpa.dupe(u8, r.path);
errdefer self.gpa.free(owned);
try self.recipients.append(self.gpa, owned);
diff --git a/src/bin/store.zig b/src/bin/store.zig
new file mode 100644
index 0000000..ad720ae
--- /dev/null
+++ b/src/bin/store.zig
@@ -0,0 +1,57 @@
+const std = @import("std");
+const App = @import("app.zig");
+const normalizeUser = @import("util.zig").normalizeUser;
+
+const Self = @This();
+
+maildir: std.fs.Dir,
+
+pub fn init(app: App) !Self {
+ const maildir = std.fs.openDirAbsolute(app.maildir, .{}) catch |e| switch (e) {
+ error.FileNotFound => blk: { // First time startup, create the maildir
+ try std.fs.makeDirAbsolute(app.maildir);
+ break :blk try std.fs.openDirAbsolute(app.maildir, .{});
+ },
+ else => return e,
+ };
+ maildir.makePath("spool/queue") catch |e| switch (e) {
+ error.PathAlreadyExists => void,
+ else => return e,
+ };
+ maildir.makePath("spool/active") catch |e| switch (e) {
+ error.PathAlreadyExists => void,
+ else => return e,
+ };
+ maildir.makePath("spool/tmp") catch |e| switch (e) {
+ error.PathAlreadyExists => void,
+ else => return e,
+ };
+ maildir.makePath("mailboxes") catch |e| switch (e) {
+ error.PathAlreadyExists => void,
+ else => return e,
+ };
+ return .{ .maildir = maildir };
+}
+
+pub fn register(self: *Self, raw: []const u8) !void {
+ var buf: [64]u8 = undefined;
+ const user = try normalizeUser(&buf, raw);
+ var mailbox = try self.maildir.openDir("mailboxes", .{});
+ defer mailbox.close();
+ try mailbox.makeDir(user);
+ var userbox = try mailbox.openDir(user, .{});
+ defer userbox.close();
+ try userbox.makeDir("tmp");
+ try userbox.makeDir("new");
+ try userbox.makeDir("cur");
+}
+
+pub fn exists(self: Self, raw: []const u8) !bool {
+ const mailbox = try self.maildir.openDir("mailboxes", .{});
+ defer mailbox.close();
+ _ = mailbox.statFile(raw) catch |e| switch (e) {
+ error.FileNotFound => return false,
+ else => return e,
+ };
+ return true;
+}
diff --git a/src/bin/util.zig b/src/bin/util.zig
new file mode 100644
index 0000000..756ed74
--- /dev/null
+++ b/src/bin/util.zig
@@ -0,0 +1,15 @@
+const std = @import("std");
+pub fn validateUser(user: []const u8) !void {
+ if (user.len == 0 or user.len > 64) return error.InvalidUser;
+ if (user[0] == '.') return error.InvalidUser;
+ for (user) |c| switch (c) {
+ 'a'...'z', '0'...'9', '.', '_', '-' => {},
+ else => return error.InvalidUser,
+ };
+}
+
+pub fn normalizeUser(user: []const u8, buf: *[64]u8) ![]const u8 {
+ const out = std.ascii.lowerString(buf, user);
+ try validateUser(out);
+ return out;
+}