From ad821a3e2cfb535c62ac2041c951433b6e99db58 Mon Sep 17 00:00:00 2001 From: Nic Gaffney Date: Tue, 20 Oct 2026 18:57:33 -0500 Subject: Spool in progress --- src/bin/store.zig | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 src/bin/store.zig (limited to 'src/bin/store.zig') 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; +} -- cgit v1.2.3