summaryrefslogtreecommitdiff
path: root/src/bin/store.zig
blob: ad720ae9d62d19ac4fa7c1fe2e2b547fa73c8574 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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;
}