summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/bin/app.zig34
-rw-r--r--src/bin/main.zig27
-rw-r--r--src/bin/store.zig58
-rw-r--r--src/lib/reply.zig3
4 files changed, 81 insertions, 41 deletions
diff --git a/src/bin/app.zig b/src/bin/app.zig
index 46ba178..e3b76fd 100644
--- a/src/bin/app.zig
+++ b/src/bin/app.zig
@@ -6,8 +6,8 @@ const Store = @import("store.zig");
pub const App = @This();
-comptime maildir: []const u8 = "./Mail",
gpa: Allocator,
+io: std.Io,
domain: []const u8 = "localhost",
sender: ?[]const u8 = null,
recipients: std.ArrayList([]const u8) = .empty,
@@ -25,6 +25,26 @@ const max_rcpt = 100;
const max_size = 512;
const max_body = 10 * 1024 * 1024;
+fn acceptLocal(self: *App, raw: []const u8) !nv.Reply {
+ _ = self;
+ _ = raw;
+ return error.NotImplemented;
+}
+
+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, .{});
+ defer mailbox.close(self.io);
+ var tmp = try mailbox.openDir(self.io, "tmp");
+ tmp.createFile(self.io, "MAAAIL", .{.lock});
+ }
+ }
+}
+
pub fn appendBodyLine(self: *App, line: []const u8) !enum { ok, too_large } {
const l = if (std.mem.startsWith(u8, line, "..")) line[1..] else line;
if (self.body.items.len + l.len > max_body) return .too_large;
@@ -33,7 +53,7 @@ pub fn appendBodyLine(self: *App, line: []const u8) !enum { ok, too_large } {
}
pub fn resetTransaction(self: *App) void {
- if (@intFromEnum(self.state) <= 2) self.state = .ready else self.state = .start;
+ if (@backingInt(self.state) <= 2) self.state = .ready else self.state = .start;
if (self.sender) |s| self.gpa.free(s);
self.sender = null;
for (self.recipients.items) |r| self.gpa.free(r);
@@ -63,7 +83,7 @@ pub fn helo(self: *App, arena: Allocator, id: nv.Identity) !nv.Reply {
pub fn mail(self: *App, arena: Allocator, m: nv.MailFrom) !nv.Reply {
_ = arena;
- if (@intFromEnum(self.state) < 2) return nv.Reply.single(.bad_sequence, "Need EHLO before MAIL");
+ if (@backingInt(self.state) < 2) return nv.Reply.single(.bad_sequence, "Need EHLO before MAIL");
var it = m.paramIterator();
while (it.next()) |p| {
if (std.ascii.eqlIgnoreCase(p.key, "SIZE")) {
@@ -97,10 +117,10 @@ pub fn rcpt(self: *App, arena: Allocator, r: nv.RcptTo) !nv.Reply {
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 user = normalizeUser(local, &buf) catch
+ return nv.Reply.single(.rejected, "No such user"); // 550
+ if (!try self.store.exists(self.io, user))
+ return nv.Reply.single(.rejected, "No such user"); // 550
const owned = try self.gpa.dupe(u8, r.path);
errdefer self.gpa.free(owned);
diff --git a/src/bin/main.zig b/src/bin/main.zig
index 6784263..1a9243b 100644
--- a/src/bin/main.zig
+++ b/src/bin/main.zig
@@ -1,16 +1,30 @@
const std = @import("std");
const Io = std.Io;
const App = @import("app.zig");
-
+const Store = @import("store.zig");
+const util = @import("util.zig");
const nv = @import("nvlope");
+const MAILDIR: []const u8 = "/var/mail/";
+const PORT = 2525;
+
pub fn main(init: std.process.Init) !void {
const io = init.io;
const alloc = std.heap.smp_allocator;
+ var argiter = init.minimal.args.iterate();
+ _ = argiter.skip();
+ if (argiter.next()) |arg| {
+ if (std.ascii.eqlIgnoreCase(arg[0..arg.len], "--register")) {
+ const raw = argiter.next().?;
+ var store = try Store.init(io, MAILDIR);
+ defer store.maildir.close(io);
+ try store.register(io, raw);
+ return;
+ }
+ }
// Initialize server on localhost at the specified port
- const port = 2525;
- const addr = try Io.net.IpAddress.parse("127.0.0.1", port);
+ const addr = try Io.net.IpAddress.parse("127.0.0.1", PORT);
var server = try addr.listen(io, .{
.mode = .stream,
.protocol = .tcp,
@@ -21,6 +35,9 @@ pub fn main(init: std.process.Init) !void {
var group: Io.Group = .init;
defer group.cancel(io);
+ // First time startup check
+ (try Store.init(io, MAILDIR)).maildir.close(io);
+
// Async delivery worker here
while (true) {
@@ -49,7 +66,8 @@ fn session(alloc: std.mem.Allocator, io: Io, stream: *Io.net.Stream) !void {
// Per-connection state
var arena: std.heap.ArenaAllocator = .init(alloc);
defer arena.deinit();
- var app: App = .{ .gpa = alloc };
+ var store: Store = try .init(io, MAILDIR);
+ var app: App = .{ .gpa = alloc, .store = &store, .io = io };
// Greeting: the server speaks first
try nv.Reply.single(.ready, "nvlope ready").send(w);
@@ -72,6 +90,7 @@ fn session(alloc: std.mem.Allocator, io: Io, stream: *Io.net.Stream) !void {
if (in_data) {
if (std.mem.eql(u8, line, ".\r\n")) {
in_data = false;
+ app.queue();
try nv.Reply.single(.ok, "Queued").send(w);
try w.flush();
std.debug.print("App {{\n\t body: {s}\n\tsender: {s}\n\trecipients: {any}\n}}\n", .{ app.body.items, app.sender.?, app.recipients.items });
diff --git a/src/bin/store.zig b/src/bin/store.zig
index ad720ae..8d93fec 100644
--- a/src/bin/store.zig
+++ b/src/bin/store.zig
@@ -1,55 +1,55 @@
const std = @import("std");
-const App = @import("app.zig");
const normalizeUser = @import("util.zig").normalizeUser;
const Self = @This();
-maildir: std.fs.Dir,
+maildir: std.Io.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, .{});
+pub fn init(io: std.Io, dir: []const u8) !Self {
+ const maildir: std.Io.Dir = std.Io.Dir.openDirAbsolute(io, dir, .{}) catch |e| switch (e) {
+ // First time startup, create the maildir
+ error.FileNotFound => blk: {
+ try std.Io.Dir.createDirAbsolute(io, dir, @fromBackingInt(@intCast(0o700)));
+ break :blk try std.Io.Dir.openDirAbsolute(io, dir, .{});
},
else => return e,
};
- maildir.makePath("spool/queue") catch |e| switch (e) {
- error.PathAlreadyExists => void,
+ maildir.createDirPath(io, "spool/queue") catch |e| switch (e) {
+ error.PathAlreadyExists => {},
else => return e,
};
- maildir.makePath("spool/active") catch |e| switch (e) {
- error.PathAlreadyExists => void,
+ maildir.createDirPath(io, "spool/active") catch |e| switch (e) {
+ error.PathAlreadyExists => {},
else => return e,
};
- maildir.makePath("spool/tmp") catch |e| switch (e) {
- error.PathAlreadyExists => void,
+ maildir.createDirPath(io, "spool/tmp") catch |e| switch (e) {
+ error.PathAlreadyExists => {},
else => return e,
};
- maildir.makePath("mailboxes") catch |e| switch (e) {
- error.PathAlreadyExists => void,
+ maildir.createDirPath(io, "mailboxes") catch |e| switch (e) {
+ error.PathAlreadyExists => {},
else => return e,
};
return .{ .maildir = maildir };
}
-pub fn register(self: *Self, raw: []const u8) !void {
+pub fn register(self: *Self, io: std.Io, 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");
+ const user = try normalizeUser(raw, &buf);
+ var mailbox = try self.maildir.openDir(io, "mailboxes", .{});
+ defer mailbox.close(io);
+ try mailbox.createDir(io, user, @fromBackingInt(@intCast(0o700)));
+ var userbox = try mailbox.openDir(io, user, .{});
+ defer userbox.close(io);
+ try userbox.createDir(io, "tmp", @fromBackingInt(@intCast(0o700)));
+ try userbox.createDir(io, "new", @fromBackingInt(@intCast(0o700)));
+ try userbox.createDir(io, "cur", @fromBackingInt(@intCast(0o700)));
}
-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) {
+pub fn exists(self: Self, io: std.Io, raw: []const u8) !bool {
+ const mailbox = try self.maildir.openDir(io, "mailboxes", .{});
+ defer mailbox.close(io);
+ _ = mailbox.statFile(io, raw, .{}) catch |e| switch (e) {
error.FileNotFound => return false,
else => return e,
};
diff --git a/src/lib/reply.zig b/src/lib/reply.zig
index 61df971..5bb5c94 100644
--- a/src/lib/reply.zig
+++ b/src/lib/reply.zig
@@ -17,6 +17,7 @@ pub const Code = enum(u16) {
too_many = 452,
exceeded = 552,
param_unrecognized = 555,
+ relay_denied = 554,
_, //@as(Code, @enumFromInt(user defined code))
};
@@ -32,7 +33,7 @@ pub const Reply = struct {
}
pub fn send(self: Reply, w: *Io.Writer) !void {
- const code = @intFromEnum(self.code);
+ const code = @backingInt(self.code);
for (self.lines, 0..) |line, i| {
const sep: u8 = if (i == self.lines.len - 1) ' ' else '-';
try w.print("{d}{c}{s}\r\n", .{ code, sep, line });