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; }