diff options
Diffstat (limited to 'src/bin/app.zig')
| -rw-r--r-- | src/bin/app.zig | 36 |
1 files changed, 22 insertions, 14 deletions
diff --git a/src/bin/app.zig b/src/bin/app.zig index 474592e..801a7bd 100644 --- a/src/bin/app.zig +++ b/src/bin/app.zig @@ -47,7 +47,9 @@ 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) }); 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", .{}); defer maildir.close(self.io); var mailbox = try maildir.openDir(self.io, local, .{}); @@ -83,7 +85,9 @@ pub fn queue(self: *App) !void { file.close(self.io); const new = try mailbox.openDir(self.io, "new", .{}); + // errdefer maildir.close(self.io); try tmp.rename(id, new, id, self.io); + // std.posix.sync(); } else { // Relay / user not defined } @@ -98,7 +102,10 @@ pub fn appendBodyLine(self: *App, line: []const u8) !enum { ok, too_large } { } pub fn resetTransaction(self: *App) void { - if (@backingInt(self.state) <= 2) self.state = .ready else self.state = .start; + if (self.helo_ident) |h| { + self.gpa.free(h); + self.state = .ready; + } if (self.sender) |s| self.gpa.free(s); self.sender = null; for (self.recipients.items) |r| self.gpa.free(r); @@ -106,6 +113,15 @@ pub fn resetTransaction(self: *App) void { self.body.clearRetainingCapacity(); } +fn setHelo(self: *App, who: []const u8) !void { + var buf: [255]u8 = undefined; + const n = @min(who.len, buf.len); + for (who[0..n], buf[0..n]) |c, *o| o.* = if (c > 0x20 and c < 0x7f) c else '?'; + const owned = try self.gpa.dupe(u8, buf[0..n]); + if (self.helo_ident) |old| self.gpa.free(old); + self.helo_ident = owned; +} + pub fn ehlo(self: *App, arena: Allocator, id: nv.Identity) !nv.Reply { self.resetTransaction(); var tmp: [255]u8 = undefined; @@ -115,12 +131,7 @@ pub fn ehlo(self: *App, arena: Allocator, id: nv.Identity) !nv.Reply { lines[1] = "8BITMIME"; lines[2] = "SIZE 35882577"; self.state = .ready; - - var buf: [255]u8 = undefined; - const n = @min(who.len, buf.len); - for (who[0..n], buf[0..n]) |c, *o| o.* = if (c > 0x20 and c < 0x7f) c else '?'; - self.helo_ident = try arena.dupe(u8, buf[0..n]); - + try self.setHelo(who); self.esmtp = true; return .{ .code = .ok, .lines = lines }; } @@ -131,15 +142,12 @@ pub fn helo(self: *App, arena: Allocator, id: nv.Identity) !nv.Reply { var tmp: [255]u8 = undefined; const who = try id.toString(&tmp); const line = try std.fmt.allocPrint(arena, "{s}", .{self.domain}); - - var buf: [255]u8 = undefined; - const n = @min(who.len, buf.len); - for (who[0..n], buf[0..n]) |c, *o| o.* = if (c > 0x20 and c < 0x7f) c else '?'; - self.helo_ident = try arena.dupe(u8, buf[0..n]); + try self.setHelo(who); return .{ .code = .ok, .lines = try arena.dupe([]const u8, &.{line}) }; } pub fn mail(self: *App, arena: Allocator, m: nv.MailFrom) !nv.Reply { + if (self.sender) |_| return nv.Reply.single(.bad_sequence, "MAIL already recieved, need RSET to reset MAIL"); _ = arena; if (@backingInt(self.state) < 2) return nv.Reply.single(.bad_sequence, "Need EHLO before MAIL"); var it = m.paramIterator(); @@ -153,7 +161,6 @@ pub fn mail(self: *App, arena: Allocator, m: nv.MailFrom) !nv.Reply { } } self.state = .mail; - if (self.sender) |old| self.gpa.free(old); self.sender = try self.gpa.dupe(u8, m.path); return nv.Reply.single(.ok, "Sender OK"); } @@ -180,7 +187,7 @@ pub fn rcpt(self: *App, arena: Allocator, r: nv.RcptTo) !nv.Reply { 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); + const owned = try std.mem.concat(self.gpa, u8, &[_][]const u8{ user, "@", domain }); errdefer self.gpa.free(owned); try self.recipients.append(self.gpa, owned); return nv.Reply.single(.ok, "OK"); @@ -199,6 +206,7 @@ pub fn noop(self: *App, arena: Allocator, _: void) !nv.Reply { } pub fn data(self: *App, arena: Allocator, _: void) !nv.Reply { + if (self.sender == null) return nv.Reply.single(.bad_sequence, "Need MAIL before DATA"); if (self.recipients.items.len == 0) return nv.Reply.single(.bad_sequence, "Need RCPT before DATA"); const line = "Start mail input; end with <CRLF>.<CRLF>"; return .{ .code = .start_data, .lines = try arena.dupe([]const u8, &.{line}), .action = .data }; |
