summaryrefslogtreecommitdiff
path: root/src/bin
diff options
context:
space:
mode:
Diffstat (limited to 'src/bin')
-rw-r--r--src/bin/app.zig27
-rw-r--r--src/bin/main.zig1
2 files changed, 21 insertions, 7 deletions
diff --git a/src/bin/app.zig b/src/bin/app.zig
index 305bbca..d448734 100644
--- a/src/bin/app.zig
+++ b/src/bin/app.zig
@@ -26,7 +26,6 @@ helo_ident: ?[]u8 = null,
esmtp: bool = false,
const max_rcpt = 100;
-const max_size = 512;
const max_body = 10 * 1024 * 1024;
var next_id: std.atomic.Value(u64) = .init(0);
@@ -47,7 +46,7 @@ pub fn queue(self: *App) !void {
const at = std.mem.lastIndexOfScalar(u8, rc, '@') orelse continue;
const local = rc[0..at];
const domain = rc[at + 1 ..];
- std.debug.print("{s} {any}\n", .{ rc, try self.store.exists(self.io, local) });
+ // std.debug.print("{s} {any}\n", .{ rc, try self.store.exists(self.io, local) });
if (std.ascii.eqlIgnoreCase(domain, self.domain) and try self.store.exists(self.io, local)) {
std.debug.print("sending {s}\n", .{rc});
var maildir = try self.store.maildir.openDir(self.io, "mailboxes", .{});
@@ -73,6 +72,7 @@ pub fn queue(self: *App) !void {
};
var file = try tmp.createFile(self.io, id, .{ .exclusive = true });
+ errdefer file.close(self.io);
errdefer tmp.deleteFile(self.io, id) catch {};
var fbuf: [512]u8 = undefined;
@@ -85,6 +85,7 @@ pub fn queue(self: *App) !void {
file.close(self.io);
const new = try mailbox.openDir(self.io, "new", .{});
+ defer new.close(self.io);
// errdefer maildir.close(self.io);
try tmp.rename(id, new, id, self.io);
// std.posix.sync();
@@ -102,10 +103,7 @@ pub fn appendBodyLine(self: *App, line: []const u8) !enum { ok, too_large } {
}
pub fn resetTransaction(self: *App) void {
- if (self.helo_ident) |h| {
- self.gpa.free(h);
- self.state = .ready;
- }
+ if (self.state != .init and self.state != .start) self.state = .ready;
if (self.sender) |s| self.gpa.free(s);
self.sender = null;
for (self.recipients.items) |r| self.gpa.free(r);
@@ -113,6 +111,14 @@ pub fn resetTransaction(self: *App) void {
self.body.clearRetainingCapacity();
}
+pub fn deinit(self: *App) void {
+ self.resetTransaction();
+ self.recipients.deinit(self.gpa);
+ self.body.deinit(self.gpa);
+ if (self.helo_ident) |h| self.gpa.free(h);
+ self.helo_ident = null;
+}
+
fn setHelo(self: *App, who: []const u8) !void {
var buf: [255]u8 = undefined;
const n = @min(who.len, buf.len);
@@ -155,7 +161,7 @@ pub fn mail(self: *App, arena: Allocator, m: nv.MailFrom) !nv.Reply {
if (std.ascii.eqlIgnoreCase(p.key, "SIZE")) {
const n = std.fmt.parseInt(usize, p.value orelse "", 10) catch
return nv.Reply.single(.bad_param, "Bad SIZE parameter");
- if (n > max_size) return nv.Reply.single(.exceeded, "Message too large");
+ if (n > max_body) return nv.Reply.single(.exceeded, "Message too large");
} else if (!std.ascii.eqlIgnoreCase(p.key, "BODY")) {
return nv.Reply.single(.param_not_impl, "Unrecognized parameter");
}
@@ -189,6 +195,13 @@ pub fn rcpt(self: *App, arena: Allocator, r: nv.RcptTo) !nv.Reply {
return nv.Reply.single(.rejected, "No such user"); // 550
var slices = [_][]const u8{ user, r.path[at..] };
const owned = try std.mem.concat(self.gpa, u8, &slices);
+ for (self.recipients.items) |existing| {
+ if (std.ascii.eqlIgnoreCase(existing, owned)) {
+ self.gpa.free(owned);
+ return nv.Reply.single(.ok, "OK"); // already accepted; one copy only
+ }
+ }
+ errdefer self.gpa.free(owned);
try self.recipients.append(self.gpa, owned);
return nv.Reply.single(.ok, "OK");
}
diff --git a/src/bin/main.zig b/src/bin/main.zig
index b93556a..cdc9d3d 100644
--- a/src/bin/main.zig
+++ b/src/bin/main.zig
@@ -94,6 +94,7 @@ fn session(alloc: std.mem.Allocator, io: Io, stream: *Io.net.Stream) !void {
defer arena.deinit();
var store: Store = try .init(io, MAILDIR);
var app: App = .{ .gpa = alloc, .store = &store, .io = io, .peer = fw.buffered() };
+ defer app.deinit();
// Greeting: the server speaks first
try nv.Reply.single(.ready, "nvlope ready").send(w);