this repo has no description
3
fork

Configure Feed

Select the types of activity you want to include in your feed.

at 8596cc7b309343131a379e4f1d481073fe49acd6 601 lines 23 kB view raw
1const std = @import("std"); 2const comlink = @import("comlink.zig"); 3const vaxis = @import("vaxis"); 4const ziglua = @import("ziglua"); 5 6const irc = comlink.irc; 7const App = comlink.App; 8const Lua = ziglua.Lua; 9 10const assert = std.debug.assert; 11 12/// lua constant for the REGISTRYINDEX table 13const registry_index = ziglua.registry_index; 14 15/// global key for the app userdata pointer in the registry 16const app_key = "comlink.app"; 17 18/// active client key. This gets replaced with the client context during callbacks 19const client_key = "comlink.client"; 20 21pub fn init(app: *App) !void { 22 const lua = app.lua; 23 // load standard libraries 24 lua.openLibs(); 25 26 _ = try lua.getGlobal("package"); // [package] 27 _ = lua.getField(1, "preload"); // [package, preload] 28 lua.pushFunction(ziglua.wrap(Comlink.preloader)); // [package, preload, function] 29 lua.setField(2, "comlink"); // [package, preload] 30 lua.pop(1); // [package] 31 _ = lua.getField(1, "path"); // [package, string] 32 const package_path = try lua.toString(2); 33 lua.pop(1); // [package] 34 35 // set package.path 36 { 37 var buf: [std.posix.PATH_MAX]u8 = undefined; 38 var fba = std.heap.FixedBufferAllocator.init(&buf); 39 const alloc = fba.allocator(); 40 const prefix = blk: { 41 if (app.env.get("XDG_CONFIG_HOME")) |cfg| 42 break :blk try std.fs.path.join(alloc, &.{ cfg, "comlink" }); 43 if (app.env.get("HOME")) |home| 44 break :blk try std.fs.path.join(alloc, &.{ home, ".config/comlink" }); 45 return error.NoConfigFile; 46 }; 47 const base = try std.fs.path.join(app.alloc, &.{ prefix, "?.lua" }); 48 defer app.alloc.free(base); 49 const one = try std.fs.path.join(app.alloc, &.{ prefix, "lua/?.lua" }); 50 defer app.alloc.free(one); 51 const two = try std.fs.path.join(app.alloc, &.{ prefix, "lua/?/init.lua" }); 52 defer app.alloc.free(two); 53 const new_pkg_path = try std.mem.join(app.alloc, ";", &.{ package_path, base, one, two }); 54 _ = lua.pushString(new_pkg_path); // [package, string] 55 lua.setField(1, "path"); // [package]; 56 defer app.alloc.free(new_pkg_path); 57 } 58 59 // empty the stack 60 lua.pop(1); // [] 61 62 // keep a reference to our app in the lua state 63 lua.pushLightUserdata(app); // [userdata] 64 lua.setField(registry_index, app_key); // [] 65 66 // load config 67 var buf: [std.posix.PATH_MAX]u8 = undefined; 68 var fba = std.heap.FixedBufferAllocator.init(&buf); 69 const alloc = fba.allocator(); 70 const path = blk: { 71 if (app.env.get("XDG_CONFIG_HOME")) |cfg| 72 break :blk try std.fs.path.joinZ(alloc, &.{ cfg, "comlink/init.lua" }); 73 if (app.env.get("HOME")) |home| 74 break :blk try std.fs.path.joinZ(alloc, &.{ home, ".config/comlink/init.lua" }); 75 unreachable; 76 }; 77 78 switch (ziglua.lang) { 79 .luajit, .lua51 => lua.loadFile(path) catch return error.LuaError, 80 else => lua.loadFile(path, .binary_text) catch return error.LuaError, 81 } 82 lua.protectedCall(.{ 83 .args = 0, 84 .results = ziglua.mult_return, 85 .msg_handler = 0, 86 }) catch return error.LuaError; 87} 88 89/// retrieves the *App lightuserdata from the registry index 90fn getApp(lua: *Lua) *App { 91 const lua_type = lua.getField(registry_index, app_key); // [userdata] 92 assert(lua_type == .light_userdata); // set by comlink as a lightuserdata 93 const app = lua.toUserdata(App, -1) catch unreachable; // already asserted 94 lua.pop(1); // [] 95 // as lightuserdata 96 return app; 97} 98 99fn getClient(lua: *Lua) *irc.Client { 100 const lua_type = lua.getField(registry_index, client_key); // [userdata] 101 assert(lua_type == .light_userdata); // set by comlink as a lightuserdata 102 const client = lua.toUserdata(irc.Client, -1) catch unreachable; // already asserted 103 // as lightuserdata 104 return client; 105} 106 107/// The on_connect event is emitted when we complete registration and receive a RPL_WELCOME message 108pub fn onConnect(lua: *Lua, client: *irc.Client) !void { 109 defer lua.setTop(0); // [] 110 lua.pushLightUserdata(client); // [light_userdata] 111 lua.setField(registry_index, client_key); // [] 112 113 Client.getTable(lua, client.config.lua_table); // [table] 114 const lua_type = lua.getField(1, "on_connect"); // [table, type] 115 switch (lua_type) { 116 .function => { 117 // Push the table to the top since it is our argument to the function 118 lua.pushValue(1); // [table, function, table] 119 lua.protectedCall(.{ 120 .args = 1, 121 .results = 0, 122 .msg_handler = 0, 123 }) catch return error.LuaError; // [table] 124 // clear the stack 125 lua.pop(1); // [] 126 }, 127 else => {}, 128 } 129} 130 131pub fn onMessage(lua: *Lua, client: *irc.Client, channel: []const u8, sender: []const u8, msg: []const u8) !void { 132 defer lua.setTop(0); // [] 133 Client.getTable(lua, client.config.lua_table); // [table] 134 const lua_type = lua.getField(1, "on_message"); // [table, type] 135 switch (lua_type) { 136 .function => { 137 // Push the table to the top since it is our argument to the function 138 _ = lua.pushString(channel); // [function,string] 139 _ = lua.pushString(sender); // [function,string,string] 140 _ = lua.pushString(msg); // [function,string,string,string] 141 lua.protectedCall(.{ 142 .args = 3, 143 .results = 0, 144 .msg_handler = 0, 145 }) catch return error.LuaError; // [function,string,string,string] 146 }, 147 else => {}, 148 } 149} 150 151pub fn execFn(lua: *Lua, func: i32) !void { 152 const lua_type = lua.rawGetIndex(registry_index, func); // [function] 153 switch (lua_type) { 154 .function => lua.protectedCall(.{ 155 .args = 0, 156 .results = 0, 157 .msg_handler = 0, 158 }) catch return error.LuaError, 159 else => lua.raiseErrorStr("not a function", .{}), 160 } 161} 162 163pub fn execUserCommand(lua: *Lua, cmdline: []const u8, func: i32) !void { 164 defer lua.setTop(0); // [] 165 const lua_type = lua.rawGetIndex(registry_index, func); // [function] 166 _ = lua.pushString(cmdline); // [function, string] 167 168 switch (lua_type) { 169 .function => lua.protectedCall(.{ 170 .args = 1, 171 .results = 0, 172 .msg_handler = 0, 173 }) catch |err| { 174 const msg = lua.toString(-1) catch { 175 std.log.err("{}", .{err}); 176 return error.LuaError; 177 }; 178 std.log.err("{s}", .{msg}); 179 }, 180 else => lua.raiseErrorStr("not a function", .{}), 181 } 182} 183 184/// Comlink function namespace 185const Comlink = struct { 186 /// loads our "comlink" library 187 pub fn preloader(lua: *Lua) i32 { 188 const fns = [_]ziglua.FnReg{ 189 .{ .name = "bind", .func = ziglua.wrap(bind) }, 190 .{ .name = "setup", .func = ziglua.wrap(setup) }, 191 .{ .name = "connect", .func = ziglua.wrap(connect) }, 192 .{ .name = "log", .func = ziglua.wrap(log) }, 193 .{ .name = "notify", .func = ziglua.wrap(notify) }, 194 .{ .name = "add_command", .func = ziglua.wrap(addCommand) }, 195 .{ .name = "selected_channel", .func = ziglua.wrap(Comlink.selectedChannel) }, 196 }; 197 lua.newLibTable(&fns); // [table] 198 lua.setFuncs(&fns, 0); // [table] 199 return 1; 200 } 201 202 /// Sets global configuration 203 fn setup(lua: *Lua) i32 { 204 defer lua.pop(1); // [] 205 lua.argCheck(lua.isTable(1), 1, "expected a table"); 206 // [table] 207 const app = getApp(lua); 208 const fields = std.meta.fieldNames(comlink.Config); 209 for (fields) |field| { 210 defer lua.pop(1); // [table] 211 const lua_type = lua.getField(1, field); // [table,type] 212 if (lua_type == .nil) { 213 // The field wasn't present 214 continue; 215 } 216 const expected_type = comlink.Config.fieldToLuaType(field); 217 if (lua_type != expected_type) { 218 std.log.warn("unexpected type: {}, expected {}", .{ lua_type, expected_type }); 219 continue; 220 } 221 222 const field_enum = std.meta.stringToEnum(comlink.Config.Fields(), field) orelse continue; 223 switch (field_enum) { 224 .markread_on_focus => app.config.markread_on_focus = lua.toBoolean(-1), 225 } 226 } 227 return 0; 228 } 229 230 /// creates a keybind. Accepts one or two string. 231 /// 232 /// The first string is the key binding. The second string is the optional 233 /// action. If nil, the key is unbound (if a binding exists). Otherwise, the 234 /// provided key is bound to the provided action. 235 fn bind(lua: *Lua) i32 { 236 const app = getApp(lua); 237 lua.argCheck(lua.isString(1), 1, "expected a string"); 238 lua.argCheck(lua.isString(2) or lua.isNil(2) or lua.isFunction(2), 2, "expected a string, a function, or nil"); 239 240 // [string {string,function,nil}] 241 const key_str = lua.toString(1) catch unreachable; 242 243 var codepoint: ?u21 = null; 244 var mods: vaxis.Key.Modifiers = .{}; 245 246 var iter = std.mem.splitScalar(u8, key_str, '+'); 247 while (iter.next()) |key_txt| { 248 const last = iter.peek() == null; 249 if (last) { 250 codepoint = vaxis.Key.name_map.get(key_txt) orelse 251 std.unicode.utf8Decode(key_txt) catch { 252 lua.raiseErrorStr("invalid utf8 or more than one codepoint", .{}); 253 }; 254 } 255 if (std.mem.eql(u8, "shift", key_txt)) 256 mods.shift = true 257 else if (std.mem.eql(u8, "alt", key_txt)) 258 mods.alt = true 259 else if (std.mem.eql(u8, "ctrl", key_txt)) 260 mods.ctrl = true 261 else if (std.mem.eql(u8, "super", key_txt)) 262 mods.super = true 263 else if (std.mem.eql(u8, "hyper", key_txt)) 264 mods.hyper = true 265 else if (std.mem.eql(u8, "meta", key_txt)) 266 mods.meta = true; 267 } 268 269 const cp = codepoint orelse lua.raiseErrorStr("invalid keybind", .{}); 270 271 const cmd: comlink.Command = switch (lua.typeOf(2)) { 272 .string => blk: { 273 const cmd_str = lua.toString(2) catch unreachable; 274 const cmd = comlink.Command.fromString(cmd_str) orelse 275 lua.raiseErrorStr("unknown command", .{}); 276 break :blk cmd; 277 }, 278 .function => blk: { 279 const ref = lua.ref(registry_index) catch 280 lua.raiseErrorStr("couldn't ref keybind function", .{}); 281 const cmd: comlink.Command = .{ .lua_function = ref }; 282 break :blk cmd; 283 }, 284 .nil => { 285 // remove the keybind 286 for (app.binds.items, 0..) |item, i| { 287 if (item.key.matches(cp, mods)) { 288 _ = app.binds.swapRemove(i); 289 break; 290 } 291 } 292 return 0; 293 }, 294 else => unreachable, 295 }; 296 297 // replace an existing bind if we have one 298 for (app.binds.items) |*item| { 299 if (item.key.matches(cp, mods)) { 300 item.command = cmd; 301 break; 302 } 303 } else { 304 // otherwise add a new bind 305 app.binds.append(.{ 306 .key = .{ .codepoint = cp, .mods = mods }, 307 .command = cmd, 308 }) catch lua.raiseErrorStr("out of memory", .{}); 309 } 310 return 0; 311 } 312 313 /// connects to a client. Accepts a table 314 fn connect(lua: *Lua) i32 { 315 lua.argCheck(lua.isTable(1), 1, "expected a table"); 316 317 // [table] 318 var lua_type = lua.getField(1, "nick"); // [table,string] 319 lua.argCheck(lua_type == .string, 1, "expected a string for field 'nick'"); 320 const nick = lua.toString(-1) catch unreachable; 321 lua.pop(1); // [table] 322 323 lua_type = lua.getField(1, "user"); // [table,string] 324 const user: []const u8 = switch (lua_type) { 325 .nil => blk: { 326 lua.pop(1); // [table] 327 break :blk nick; 328 }, 329 .string => blk: { 330 const val = lua.toString(-1) catch ""; 331 lua.pop(1); // [table] 332 break :blk val; 333 }, 334 else => lua.raiseErrorStr("expected a string for field 'user'", .{}), 335 }; 336 337 lua_type = lua.getField(1, "password"); // [table, string] 338 lua.argCheck(lua_type == .string, 1, "expected a string for field 'password'"); 339 const password = lua.toString(-1) catch unreachable; 340 lua.pop(1); // [table] 341 342 lua_type = lua.getField(1, "real_name"); // [table, string] 343 const real_name: []const u8 = switch (lua_type) { 344 .nil => blk: { 345 lua.pop(1); // [table] 346 break :blk nick; 347 }, 348 .string => blk: { 349 const val = lua.toString(-1) catch ""; 350 lua.pop(1); // [table] 351 break :blk val; 352 }, 353 else => lua.raiseErrorStr("expected a string for field 'real_name'", .{}), 354 }; 355 356 lua_type = lua.getField(1, "server"); // [table, string] 357 lua.argCheck(lua_type == .string, 1, "expected a string for field 'server'"); 358 const server = lua.toString(-1) catch unreachable; // [table] 359 lua.pop(1); // [table] 360 361 lua_type = lua.getField(1, "tls"); // [table, boolean|nil] 362 const tls: bool = switch (lua_type) { 363 .nil => blk: { 364 lua.pop(1); // [table] 365 break :blk true; 366 }, 367 .boolean => blk: { 368 const val = lua.toBoolean(-1); 369 lua.pop(1); // [table] 370 break :blk val; 371 }, 372 else => lua.raiseErrorStr("expected a boolean for field 'tls'", .{}), 373 }; 374 375 lua_type = lua.getField(1, "port"); // [table, int|nil] 376 lua.argCheck(lua_type == .nil or lua_type == .number, 1, "expected a number or nil"); 377 const port: ?u16 = switch (lua_type) { 378 .nil => blk: { 379 lua.pop(1); // [table] 380 break :blk null; 381 }, 382 .number => blk: { 383 const val = lua.toNumber(-1) catch unreachable; 384 lua.pop(1); // [table] 385 break :blk @intFromFloat(val); 386 }, 387 else => lua.raiseErrorStr("expected a boolean for field 'tls'", .{}), 388 }; 389 390 // Ref the config table so it doesn't get garbage collected 391 _ = lua.ref(registry_index) catch lua.raiseErrorStr("couldn't ref config table", .{}); // [] 392 393 Client.initTable(lua); // [table] 394 const table_ref = lua.ref(registry_index) catch { 395 lua.raiseErrorStr("couldn't ref client table", .{}); 396 }; 397 398 const cfg: irc.Client.Config = .{ 399 .server = server, 400 .user = user, 401 .nick = nick, 402 .password = password, 403 .real_name = real_name, 404 .tls = tls, 405 .lua_table = table_ref, 406 .port = port, 407 }; 408 409 const app = getApp(lua); 410 app.connect(cfg) catch { 411 lua.raiseErrorStr("couldn't connect", .{}); 412 }; 413 414 // put the table back on the stack 415 Client.getTable(lua, table_ref); // [table] 416 return 1; // [] 417 } 418 419 fn log(lua: *Lua) i32 { 420 lua.argCheck(lua.isString(1), 1, "expected a string"); // [string] 421 const msg = lua.toString(1) catch unreachable; // [] 422 std.log.scoped(.lua).info("{s}", .{msg}); 423 return 0; 424 } 425 426 /// System notification. Takes two strings: title, body 427 fn notify(lua: *Lua) i32 { 428 lua.argCheck(lua.isString(1), 1, "expected a string"); // [string, string] 429 lua.argCheck(lua.isString(2), 2, "expected a string"); // [string, string] 430 const app = getApp(lua); 431 const title = lua.toString(1) catch { // [string, string] 432 lua.raiseErrorStr("couldn't write notification", .{}); 433 }; 434 const body = lua.toString(2) catch { // [string, string] 435 lua.raiseErrorStr("couldn't write notification", .{}); 436 }; 437 lua.pop(2); // [] 438 if (app.ctx) |ctx| { 439 ctx.sendNotification(title, body) catch { 440 lua.raiseErrorStr("couldn't write notification", .{}); 441 }; 442 } 443 return 0; 444 } 445 446 /// Add a user command to the command list 447 fn addCommand(lua: *Lua) i32 { 448 assert(lua.getTop() == 2); 449 lua.argCheck(lua.isString(1), 1, "expected a string"); // [string, function] 450 lua.argCheck(lua.isFunction(2), 2, "expected a function"); // [string, function] 451 const ref = lua.ref(registry_index) catch lua.raiseErrorStr("couldn't ref function", .{}); // [string] 452 const cmd = lua.toString(1) catch unreachable; 453 454 // ref the string so we don't garbage collect it 455 _ = lua.ref(registry_index) catch lua.raiseErrorStr("couldn't ref command name", .{}); // [] 456 comlink.Command.user_commands.put(cmd, ref) catch lua.raiseErrorStr("out of memory", .{}); 457 return 0; 458 } 459 460 fn selectedChannel(lua: *Lua) i32 { 461 const app = getApp(lua); 462 if (app.selectedBuffer()) |buf| { 463 switch (buf) { 464 .client => {}, 465 .channel => |chan| { 466 Channel.initTable(lua, chan); // [table] 467 return 1; 468 }, 469 } 470 } 471 lua.pushNil(); // [nil] 472 return 1; 473 } 474}; 475 476const Channel = struct { 477 fn initTable(lua: *Lua, channel: *irc.Channel) void { 478 const fns = [_]ziglua.FnReg{ 479 .{ .name = "send_msg", .func = ziglua.wrap(Channel.sendMsg) }, 480 .{ .name = "insert_text", .func = ziglua.wrap(Channel.insertText) }, 481 .{ .name = "name", .func = ziglua.wrap(Channel.name) }, 482 .{ .name = "mark_read", .func = ziglua.wrap(Channel.markRead) }, 483 }; 484 lua.newLibTable(&fns); // [table] 485 lua.setFuncs(&fns, 0); // [table] 486 487 lua.pushLightUserdata(channel); // [table, lightuserdata] 488 lua.setField(1, "_ptr"); // [table] 489 } 490 491 fn sendMsg(lua: *Lua) i32 { 492 lua.argCheck(lua.isTable(1), 1, "expected a table"); // [table] 493 lua.argCheck(lua.isString(2), 2, "expected a string"); // [table,string] 494 const msg = lua.toString(2) catch unreachable; 495 lua.pop(1); // [table] 496 const lua_type = lua.getField(1, "_ptr"); // [table, lightuserdata] 497 lua.argCheck(lua_type == .light_userdata, 2, "expected lightuserdata"); 498 const channel = lua.toUserdata(irc.Channel, 2) catch unreachable; 499 lua.pop(1); // [table] 500 501 if (msg.len > 0 and msg[0] == '/') { 502 const app = getApp(lua); 503 app.handleCommand(.{ .channel = channel }, msg) catch 504 lua.raiseErrorStr("couldn't handle command", .{}); 505 return 0; 506 } 507 508 var buf: [1024]u8 = undefined; 509 const msg_final = std.fmt.bufPrint( 510 &buf, 511 "PRIVMSG {s} :{s}\r\n", 512 .{ channel.name, msg }, 513 ) catch lua.raiseErrorStr("out of memory", .{}); 514 channel.client.queueWrite(msg_final) catch lua.raiseErrorStr("out of memory", .{}); 515 return 0; 516 } 517 518 fn insertText(lua: *Lua) i32 { 519 lua.argCheck(lua.isTable(1), 1, "expected a table"); // [table] 520 lua.argCheck(lua.isString(2), 2, "expected a string"); // [table,string] 521 const msg = lua.toString(2) catch unreachable; 522 lua.pop(1); // [table] 523 const lua_type = lua.getField(1, "_ptr"); // [table, lightuserdata] 524 lua.argCheck(lua_type == .light_userdata, 2, "expected lightuserdata"); 525 const channel = lua.toUserdata(irc.Channel, 2) catch unreachable; 526 lua.pop(1); // [] 527 528 channel.text_field.insertSliceAtCursor(msg) catch { 529 lua.raiseErrorStr("couldn't insert text", .{}); 530 }; 531 return 0; 532 } 533 534 fn name(lua: *Lua) i32 { 535 lua.argCheck(lua.isTable(1), 1, "expected a table"); // [table] 536 const lua_type = lua.getField(1, "_ptr"); // [table, lightuserdata] 537 lua.argCheck(lua_type == .light_userdata, 2, "expected lightuserdata"); 538 const channel = lua.toUserdata(irc.Channel, 2) catch unreachable; 539 lua.pop(2); // [] 540 _ = lua.pushString(channel.name); // [string] 541 return 1; 542 } 543 544 fn markRead(lua: *Lua) i32 { 545 lua.argCheck(lua.isTable(1), 1, "expected a table"); // [table] 546 const lua_type = lua.getField(1, "_ptr"); // [table, lightuserdata] 547 lua.argCheck(lua_type == .light_userdata, 2, "expected lightuserdata"); 548 const channel = lua.toUserdata(irc.Channel, 2) catch unreachable; 549 channel.last_read_indicator = channel.last_read; 550 lua.pop(2); // [] 551 return 0; 552 } 553}; 554 555/// Client function namespace 556const Client = struct { 557 /// initialize a table for a client and pushes it on the stack 558 fn initTable(lua: *Lua) void { 559 const fns = [_]ziglua.FnReg{ 560 .{ .name = "join", .func = ziglua.wrap(Client.join) }, 561 .{ .name = "name", .func = ziglua.wrap(Client.name) }, 562 }; 563 lua.newLibTable(&fns); // [table] 564 lua.setFuncs(&fns, 0); // [table] 565 566 lua.pushNil(); // [table, nil] 567 lua.setField(1, "on_connect"); // [table] 568 } 569 570 /// retrieve a client table and push it on the stack 571 fn getTable(lua: *Lua, i: i32) void { 572 const lua_type = lua.rawGetIndex(registry_index, i); // [table] 573 if (lua_type != .table) 574 lua.raiseErrorStr("couldn't get client table", .{}); 575 } 576 577 /// exectute a join command 578 fn join(lua: *Lua) i32 { 579 const client = getClient(lua); 580 lua.argCheck(lua.isString(1), 1, "expected a string"); // [string] 581 const channel = lua.toString(1) catch unreachable; // [] 582 assert(channel.len < 120); // channel name too long 583 var buf: [128]u8 = undefined; 584 585 const msg = std.fmt.bufPrint( 586 &buf, 587 "JOIN {s}\r\n", 588 .{channel}, 589 ) catch lua.raiseErrorStr("channel name too long", .{}); 590 591 client.queueWrite(msg) catch lua.raiseErrorStr("couldn't queue write", .{}); 592 593 return 0; 594 } 595 596 fn name(lua: *Lua) i32 { 597 const client = getClient(lua); // [] 598 _ = lua.pushString(client.config.name orelse ""); // [string] 599 return 1; // [] 600 } 601};