summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorNic Gaffney <gaffney_nic@protonmail.com>2026-10-13 16:18:01 -0500
committerNic Gaffney <gaffney_nic@protonmail.com>2026-10-13 16:18:01 -0500
commitbbe688ac4b05d9e10a68bffe9ece18da1a0b8d61 (patch)
treefd3a0bdddf29c43dbc3b044deeb9730bfe7dcb87 /src
parentcdcd11f471fd225d5fe36e0d71da58e41f00aa5f (diff)
downloadnvlope-bbe688ac4b05d9e10a68bffe9ece18da1a0b8d61.tar.gz
ehlo and helo
Diffstat (limited to 'src')
-rw-r--r--src/lib/parser.zig39
-rw-r--r--src/lib/response.zig7
-rw-r--r--src/lib/root.zig6
-rw-r--r--src/lib/smtp_parse.zig153
-rw-r--r--src/lib/types.zig8
5 files changed, 204 insertions, 9 deletions
diff --git a/src/lib/parser.zig b/src/lib/parser.zig
new file mode 100644
index 0000000..02474f5
--- /dev/null
+++ b/src/lib/parser.zig
@@ -0,0 +1,39 @@
+const std = @import("std");
+
+const ParserError = error{ InvalidCharacter, EndOfString };
+
+str: []const u8,
+pos: u32 = 0,
+
+const Self = @This();
+
+pub fn peek(self: Self) ?u8 {
+ if (self.pos > self.str.len) return null;
+ return self.str[self.pos];
+}
+
+pub fn consume(self: *Self, c: u8) ParserError!void {
+ if (self.peek()) |pk| {
+ if (pk == c) {
+ self.pos += 1;
+ return c;
+ }
+ return .InvalidCharacter;
+ }
+ return .EndOfString;
+}
+
+pub fn skip(self: *Self) void {
+ self.pos += 1;
+}
+
+pub fn takeUntil(self: *Self, c: u8) ParserError![]const u8 {
+ const i = self.pos;
+ while (if (self.peek()) |pk| pk != c else {
+ self.pos = i;
+ return .EndOfString;
+ }) {
+ self.skip();
+ }
+ return self.str[i..self.pos];
+}
diff --git a/src/lib/response.zig b/src/lib/response.zig
new file mode 100644
index 0000000..7aacfd1
--- /dev/null
+++ b/src/lib/response.zig
@@ -0,0 +1,7 @@
+const std = @import("std");
+
+const Response = enum(u32) {
+ OK = 250,
+};
+
+pub fn ehlo_rsp() Response {}
diff --git a/src/lib/root.zig b/src/lib/root.zig
index 4538e98..674c5bb 100644
--- a/src/lib/root.zig
+++ b/src/lib/root.zig
@@ -1 +1,5 @@
-const SMTP_message = @import("types.zig").SMTP_message;
+pub const SMTP = @import("smtp_parse.zig");
+
+test "all" {
+ _ = SMTP;
+}
diff --git a/src/lib/smtp_parse.zig b/src/lib/smtp_parse.zig
new file mode 100644
index 0000000..fd9c9d2
--- /dev/null
+++ b/src/lib/smtp_parse.zig
@@ -0,0 +1,153 @@
+const std = @import("std");
+const Parser = @import("parser.zig");
+
+const Identity = union(enum) {
+ domain: []const u8,
+ addr: std.Io.net.IpAddress,
+};
+
+const Command = enum {
+ EHLO,
+ HELO,
+ MAIL,
+ RCPT,
+ DATA,
+ RSET,
+ NOOP,
+ QUIT,
+ VRFY,
+
+ pub fn fromStr(msg: []const u8) Command {
+ var MSG: [4]u8 = undefined;
+ MSG = std.ascii.upperString(&MSG, msg);
+ if (std.mem.eql(u8, MSG, "EHLO")) return .EHLO;
+ if (std.mem.eql(u8, MSG, "HELO")) return .HELO;
+ if (std.mem.eql(u8, MSG, "MAIL")) return .MAIL;
+ if (std.mem.eql(u8, MSG, "RCPT")) return .RCPT;
+ if (std.mem.eql(u8, MSG, "DATA")) return .DATA;
+ if (std.mem.eql(u8, MSG, "RSET")) return .RSET;
+ if (std.mem.eql(u8, MSG, "NOOP")) return .NOOP;
+ if (std.mem.eql(u8, MSG, "QUIT")) return .QUIT;
+ if (std.mem.eql(u8, MSG, "VRFY")) return .VRFY;
+ }
+};
+
+fn parse_crlf(parser: *Parser) ![]const u8 {
+ var result = try parser.takeUntil('\r');
+ try parser.consume('\n') catch {
+ result = try parser.parse_crlf();
+ };
+ return result;
+}
+
+pub fn parse(msg: []const u8) ?i32 {
+ if (msg.len < 4) return null;
+ const cmd: Command = .fromStr(msg[0..4]);
+ switch (cmd) {
+ .ehlo => ehlo(msg[4..]),
+ .helo => helo(msg[4..]),
+ }
+}
+
+fn ehlo(msg: []const u8) !Identity {
+ const parser: Parser = .{ .str = msg };
+ try parser.consume(' ');
+ const ipaddr: std.Io.net.IpAddress = try .parse(msg, 0) catch {
+ return .{ .domain = parse_crlf(parser) };
+ };
+ _ = try parse_crlf(parser);
+ return .{ .addr = ipaddr };
+}
+
+fn helo(msg: []const u8) !Identity {
+ const parser: Parser = .{ .str = msg };
+ try parser.consume(' ');
+ return .{ .domain = parse_crlf(parser) };
+}
+
+const t = std.testing;
+
+test "Command.fromStr: known verbs, any case" {
+ try t.expectEqual(Command.EHLO, Command.fromStr("EHLO").?);
+ try t.expectEqual(Command.EHLO, Command.fromStr("ehlo").?);
+ try t.expectEqual(Command.MAIL, Command.fromStr("MaIl").?);
+ try t.expectEqual(Command.QUIT, Command.fromStr("quit").?);
+}
+
+test "Command.fromStr: unknown or wrong length" {
+ try t.expectEqual(@as(?Command, null), Command.fromStr("XXXX"));
+ try t.expectEqual(@as(?Command, null), Command.fromStr("EHL"));
+ try t.expectEqual(@as(?Command, null), Command.fromStr("EHLOO"));
+ try t.expectEqual(@as(?Command, null), Command.fromStr(""));
+}
+
+test "parse_crlf: strips terminator" {
+ var p: Parser = .{ .rest = "hello\r\n" };
+ try t.expectEqualStrings("hello", try parse_crlf(&p));
+ try t.expectEqual(@as(usize, 0), p.rest.len);
+}
+
+test "parse_crlf: rejects bare LF, missing terminator, trailing data" {
+ var a: Parser = .{ .rest = "hello\n" };
+ try t.expectError(error.Unexpected, parse_crlf(&a));
+ var b: Parser = .{ .rest = "hello" };
+ try t.expectError(error.Unexpected, parse_crlf(&b));
+ var c: Parser = .{ .rest = "hello\r\nextra" };
+ try t.expectError(error.Unexpected, parse_crlf(&c));
+}
+
+test "parse_crlf: lone CR inside the line is not a terminator" {
+ var p: Parser = .{ .rest = "a\rb\r\n" };
+ try t.expectEqualStrings("a\rb", try parse_crlf(&p));
+}
+
+test "ehlo: domain" {
+ const id = try ehlo(" mail.example.com\r\n");
+ try t.expectEqualStrings("mail.example.com", id.domain);
+}
+
+test "ehlo: IPv4 address literal" {
+ const id = try ehlo(" [192.0.2.1]\r\n");
+ try t.expect(id == .addr);
+}
+
+test "ehlo: IPv6 address literal" {
+ const id = try ehlo(" [IPv6:2001:db8::1]\r\n");
+ try t.expect(id == .addr);
+}
+
+test "ehlo: errors" {
+ try t.expectError(error.Unexpected, ehlo("mail.example.com\r\n")); // no space
+ try t.expectError(error.Unexpected, ehlo(" mail.example.com")); // no CRLF
+ try t.expectError(error.InvalidDomain, ehlo(" \r\n")); // empty
+ try t.expectError(error.InvalidDomain, ehlo(" bad_domain!\r\n"));
+ try t.expectError(error.Unexpected, ehlo(" [1.2.3.4\r\n")); // unclosed
+ try t.expectError(error.InvalidAddressLiteral, ehlo(" [not.an.ip]\r\n"));
+}
+
+test "helo: domain and errors" {
+ const id = try helo(" example.org\r\n");
+ try t.expectEqualStrings("example.org", id.domain);
+ try t.expectError(error.InvalidDomain, helo(" [1.2.3.4]\r\n"));
+ try t.expectError(error.Unexpected, helo("example.org\r\n"));
+}
+
+test "parse: dispatches EHLO/HELO case-insensitively" {
+ const a = try parse("ehlo client.example\r\n");
+ try t.expectEqualStrings("client.example", a.ehlo.domain);
+ const b = try parse("HELO client.example\r\n");
+ try t.expectEqualStrings("client.example", b.helo.domain);
+}
+
+test "parse: argument-less commands" {
+ try t.expectEqual(Command.QUIT, (try parse("QUIT\r\n")).simple);
+ try t.expectEqual(Command.NOOP, (try parse("noop\r\n")).simple);
+ try t.expectError(error.Unexpected, parse("QUIT now\r\n"));
+ try t.expectError(error.Unexpected, parse("QUIT\n"));
+}
+
+test "parse: bad input" {
+ try t.expectError(error.Unexpected, parse("EH"));
+ try t.expectError(error.UnknownCommand, parse("FOOO\r\n"));
+ try t.expectError(error.NotImplemented, parse("MAIL FROM:<a@b.c>\r\n"));
+}
diff --git a/src/lib/types.zig b/src/lib/types.zig
deleted file mode 100644
index ef7b12a..0000000
--- a/src/lib/types.zig
+++ /dev/null
@@ -1,8 +0,0 @@
-const std = @import("std");
-
-pub const SMTP_message = enum {
- MAIL,
- RCPT,
- DATA,
- QUIT,
-};