From 8596cc7b309343131a379e4f1d481073fe49acd6 Mon Sep 17 00:00:00 2001 From: Tim Culverhouse Date: Wed, 19 Mar 2025 21:51:07 -0500 Subject: [PATCH] config: don't require user or realname --- docs/comlink.3.scd | 4 ++-- docs/comlink.lua | 4 ++-- src/lua.zig | 36 +++++++++++++++++++++++++++--------- 3 files changed, 31 insertions(+), 13 deletions(-) diff --git a/docs/comlink.3.scd b/docs/comlink.3.scd index f02504e..604a244 100644 --- a/docs/comlink.3.scd +++ b/docs/comlink.3.scd @@ -39,16 +39,16 @@ This module provides application level API calls. configuration. The table has the following required fields: - *server*: string, the server URL - - *user*: string, username used in SASL - *nick*: string, nickname to identify as - *password*: string, password to use in SASL - - *real_name*: string, user's real name The following optional fields are available: - *port*: uint16 (default=6697 (tls) or 6667), port to use for connection + - *real_name*: string, user's real name - *tls*: boolean (default=true), when true, use an encrypted connection + - *user*: string, username used in SASL *comlink.log* Accepts a string and inserts a log statement into the comlink diff --git a/docs/comlink.lua b/docs/comlink.lua index cd20cc1..b4982b8 100644 --- a/docs/comlink.lua +++ b/docs/comlink.lua @@ -18,10 +18,10 @@ function comlink.setup(cfg) end ---@class ConnectionConfiguration --- ---@field server string The server to connect to, eg "chat.sr.ht" ----@field user string Username for server connection +---@field user? string Username for server connection ---@field nick string Nick to use when connecting via SASL to IRC ---@field password string Password for server ----@field real_name string Real name of user +---@field real_name? string Real name of user ---@field tls? boolean Whether to encrypt connections ---@field port? number Optional port to use for server connection. Defaults to 6697 for TLS connections and 6667 for plaintext connections diff --git a/src/lua.zig b/src/lua.zig index d5bb9c7..49f2ffd 100644 --- a/src/lua.zig +++ b/src/lua.zig @@ -315,25 +315,43 @@ const Comlink = struct { lua.argCheck(lua.isTable(1), 1, "expected a table"); // [table] - var lua_type = lua.getField(1, "user"); // [table,string] - lua.argCheck(lua_type == .string, 1, "expected a string for field 'user'"); - const user = lua.toString(-1) catch unreachable; - lua.pop(1); // [table] - - lua_type = lua.getField(1, "nick"); // [table,string] + var lua_type = lua.getField(1, "nick"); // [table,string] lua.argCheck(lua_type == .string, 1, "expected a string for field 'nick'"); const nick = lua.toString(-1) catch unreachable; lua.pop(1); // [table] + lua_type = lua.getField(1, "user"); // [table,string] + const user: []const u8 = switch (lua_type) { + .nil => blk: { + lua.pop(1); // [table] + break :blk nick; + }, + .string => blk: { + const val = lua.toString(-1) catch ""; + lua.pop(1); // [table] + break :blk val; + }, + else => lua.raiseErrorStr("expected a string for field 'user'", .{}), + }; + lua_type = lua.getField(1, "password"); // [table, string] lua.argCheck(lua_type == .string, 1, "expected a string for field 'password'"); const password = lua.toString(-1) catch unreachable; lua.pop(1); // [table] lua_type = lua.getField(1, "real_name"); // [table, string] - lua.argCheck(lua_type == .string, 1, "expected a string for field 'real_name'"); - const real_name = lua.toString(-1) catch unreachable; - lua.pop(1); // [table] + const real_name: []const u8 = switch (lua_type) { + .nil => blk: { + lua.pop(1); // [table] + break :blk nick; + }, + .string => blk: { + const val = lua.toString(-1) catch ""; + lua.pop(1); // [table] + break :blk val; + }, + else => lua.raiseErrorStr("expected a string for field 'real_name'", .{}), + }; lua_type = lua.getField(1, "server"); // [table, string] lua.argCheck(lua_type == .string, 1, "expected a string for field 'server'"); -- 2.51.2