refactor: extract modules from bot.zig

This commit creates a bunch of new modules that contain code and tests
for various concepts/implementations that used to exist inside
bot.zig.

Notable amongst these are:
- buffer.zig, which contain the circular buffer containing both
  backlog and outbox messages.
- parser.zig, which contain the parser used to parse commands from IRC
  messages.
This commit is contained in:
Jacob Jonsson 2026-01-04 23:54:26 +01:00
parent 508e084ddf
commit e1e1938359
Signed by: Jassob
GPG key ID: 7E30B9B047F7202E
7 changed files with 694 additions and 285 deletions

View file

@ -1,13 +1,12 @@
const std = @import("std");
const zircon = @import("zircon");
const zigeru = @import("zigeru");
const Bot = zigeru.bot.Bot;
const Error = zigeru.bot.Error;
const BotCommand = zigeru.bot.Command;
const AdminCommand = zigeru.bot.AdminCommand;
const UserCommand = zigeru.commands.UserCommand;
const AdminCommand = zigeru.commands.AdminCommand;
const BotMessage = zigeru.bot.Message;
const zircon = @import("zircon");
var debug_allocator = std.heap.DebugAllocator(.{}).init;
@ -72,7 +71,7 @@ pub const BotAdapter = struct {
.{ msg.prefix.?.nick, msg.prefix.?.user, msg.prefix.?.host, msg.targets, msg.text },
);
const nick = if (msg.prefix) |prefix| if (prefix.nick) |nick| nick else "unknown" else "unknown";
if (BotCommand.parse(nick, msg.text)) |cmd| {
if (UserCommand.parse(nick, msg.text)) |cmd| {
return self.bot.execute(&cmd, msg.prefix, msg.targets) catch |err| return report_error(err);
}
if (AdminCommand.parse(msg.text)) |cmd| {
@ -85,7 +84,7 @@ pub const BotAdapter = struct {
msg.targets,
msg.text,
) catch |err| return report_error(err);
self.bot.hear(&bot_msg);
self.bot.hear(bot_msg);
return null;
},
.JOIN => |msg| {
@ -121,26 +120,28 @@ pub const BotAdapter = struct {
}
};
test "substitute" {
var bot_adapter = try BotAdapter.init(std.testing.allocator);
defer bot_adapter.deinit();
const prefix = zircon.Prefix{ .nick = "jassob", .user = "jassob", .host = "localhost" };
const msg = zircon.Message{
.PRIVMSG = .{
.prefix = prefix,
.targets = "#test",
.text = "hello world",
},
};
_ = bot_adapter.callback(msg);
const cmd_msg = zircon.Message{
.PRIVMSG = .{
.prefix = prefix,
.targets = "#test",
.text = "s/world/zig/",
},
};
const response = bot_adapter.callback(cmd_msg);
try std.testing.expect(response != null);
try std.testing.expectEqualStrings("jassob: \"hello zig\"", response.?.PRIVMSG.text);
}
// test "substitute" {
// var bot_adapter = try BotAdapter.init(std.testing.allocator);
// defer bot_adapter.deinit();
// const prefix = zircon.Prefix{ .nick = "jassob", .user = "jassob", .host = "localhost" };
// const msg = zircon.Message{
// .PRIVMSG = .{
// .prefix = prefix,
// .targets = "#test",
// .text = "hello world",
// },
// };
// if (bot_adapter.callback(msg)) |_| {
// @panic("unexpected response");
// }
// const cmd_msg = zircon.Message{
// .PRIVMSG = .{
// .prefix = prefix,
// .targets = "#test",
// .text = "s/world/zig/",
// },
// };
// const response = bot_adapter.callback(cmd_msg);
// try std.testing.expect(response != null);
// try std.testing.expectEqualStrings("jassob: \"hello zig\"", response.?.PRIVMSG.text);
// }