an experimental irc client

config: accept an optional server port number

Signed-off-by: Tim Culverhouse <tim@timculverhouse.com>

+24 -2
+2
docs/comlink.3.scd
··· 37 37 38 38 The following optional fields are available: 39 39 40 + - *port*: uint16 (default=6697 (tls) or 6667), port to use for 41 + connection 40 42 - *tls*: boolean (default=true), when true, use an encrypted connection 41 43 42 44 *comlink.log*
+1
docs/comlink.lua
··· 13 13 ---@field password string Password for server 14 14 ---@field real_name string Real name of user 15 15 ---@field tls? boolean Whether to encrypt connections 16 + ---@field port? number Optional port to use for server connection. Defaults to 6697 for TLS connections and 6667 for plaintext connections 16 17 17 18 ---A connection to a server 18 19 ---
+5 -2
src/irc.zig
··· 444 444 password: []const u8, 445 445 real_name: []const u8, 446 446 server: []const u8, 447 + port: ?u16, 447 448 network_id: ?[]const u8 = null, 448 449 name: ?[]const u8 = null, 449 450 tls: bool = true, ··· 676 677 677 678 pub fn connect(self: *Client) !void { 678 679 if (self.config.tls) { 679 - self.stream = try std.net.tcpConnectToHost(self.alloc, self.config.server, 6697); 680 + const port: u16 = self.config.port orelse 6697; 681 + self.stream = try std.net.tcpConnectToHost(self.alloc, self.config.server, port); 680 682 self.client = try tls.client(self.stream, .{ 681 683 .host = self.config.server, 682 684 .root_ca = self.app.bundle, 683 685 }); 684 686 } else { 685 - self.stream = try std.net.tcpConnectToHost(self.alloc, self.config.server, 6667); 687 + const port: u16 = self.config.port orelse 6667; 688 + self.stream = try std.net.tcpConnectToHost(self.alloc, self.config.server, port); 686 689 } 687 690 688 691 var buf: [4096]u8 = undefined;
+16
src/lua.zig
··· 315 315 else => lua.raiseErrorStr("expected a boolean for field 'tls'", .{}), 316 316 }; 317 317 318 + lua_type = lua.getField(1, "port"); // [table, int|nil] 319 + lua.argCheck(lua_type == .nil or lua_type == .number, 1, "expected a number or nil"); 320 + const port: ?u16 = switch (lua_type) { 321 + .nil => blk: { 322 + lua.pop(1); // [table] 323 + break :blk null; 324 + }, 325 + .number => blk: { 326 + const val = lua.toNumber(-1) catch unreachable; 327 + lua.pop(1); // [table] 328 + break :blk @intFromFloat(val); 329 + }, 330 + else => lua.raiseErrorStr("expected a boolean for field 'tls'", .{}), 331 + }; 332 + 318 333 // Ref the config table so it doesn't get garbage collected 319 334 _ = lua.ref(registry_index) catch lua.raiseErrorStr("couldn't ref config table", .{}); // [] 320 335 ··· 331 346 .real_name = real_name, 332 347 .tls = tls, 333 348 .lua_table = table_ref, 349 + .port = port, 334 350 }; 335 351 336 352 const loop = getLoop(lua); // []