This commit is contained in:
Jacob Jonsson 2025-11-28 23:04:53 +01:00
parent 6efdd9e027
commit 59293d1690
Signed by: Jassob
GPG key ID: 7E30B9B047F7202E
5 changed files with 232 additions and 58 deletions

View file

@ -1,28 +1,227 @@
const std = @import("std");
const zigeru = @import("zigeru");
const zircon = @import("zircon");
const Bot = zigeru.bot.Bot;
var debug_allocator = std.heap.DebugAllocator(.{}).init;
pub fn main() !void {
// Prints to stderr, ignoring potential errors.
const message = zigeru.bot.Message{
.author = "Jassob",
.timestamp = 12345,
.content = "All your base are belong to us.\n",
};
const allocator = debug_allocator.allocator();
defer _ = debug_allocator.deinit();
var gpa = std.heap.DebugAllocator(.{}){};
var bot = zigeru.bot.Bot.new(gpa.allocator());
bot.hear(message);
const command = zigeru.bot.Command{
.substitute = .{
.author = "Jassob",
.needle = "base",
.replacement = "codebase",
},
var channels: [0][]const u8 = .{};
// Create a zircon.Client with a given configuration.
var client = try zircon.Client.init(allocator, .{
.user = "eru",
.nick = "eru",
.real_name = "Eru (zigeru) bot",
.server = "irc.dtek.se",
.port = 6697,
.tls = true,
.channels = &channels,
});
defer client.deinit();
var bot_adapter = BotAdapter.init(allocator);
var adapter = bot_adapter.adapter();
// Connect to the IRC server and perform registration.
try client.connect();
try client.register();
try client.join("#eru-tests");
const callback = adapter.callbackFn;
// Enter the main loop that keeps reading incoming IRC messages forever.
// The client loop accepts a LoopConfig struct with two optional fields.
//
// These two fields, .msg_callback and .spawn_thread are callback pointers.
// You set them to custom functions you define to customize the main loop.
//
// .msg_callback lets you answer any received IRC messages with another one.
//
// .spawn_thread lets you tweak if you spawn a thread to run .msg_callback.
client.loop(.{ .msg_callback = callback }) catch |err| {
std.debug.print("eru exited with error: {}", .{err});
};
const result = try bot.execute(&command);
switch (result) {
.post_message => |msg| {
std.debug.print("{s}", .{msg.content});
}
const Adapter = struct {
ptr: *anyopaque,
callbackFn: *const fn (zircon.Message) ?zircon.Message,
};
const BotAdapter = struct {
bot: Bot,
allocator: std.mem.Allocator,
pub fn init(allocator: std.mem.Allocator) BotAdapter {
return BotAdapter{
.bot = Bot.init(allocator),
.allocator = allocator,
};
}
pub fn callback(self: *BotAdapter, message: zircon.Message) ?zircon.Message {
std.debug.print("{} {}", .{ self, message });
return null;
}
pub fn adapter(self: *BotAdapter) Adapter {
return .{
.ptr = self,
.callbackFn = self.callback,
};
}
};
/// msgCallback is called by zircon.Client.loop when a new IRC message arrives.
/// The message parameter holds the IRC message that arrived from the server.
/// You can switch on the message tagged union to reply based on its kind.
/// On this example we only care about messages of type JOIN, PRIVMSG or PART.
/// To reply to each message we finally return another message to the loop.
fn msgCallback(message: zircon.Message) ?zircon.Message {
std.debug.print("received message: {}\n", .{message});
switch (message) {
.JOIN => |msg| {
return zircon.Message{
.PRIVMSG = .{
.targets = msg.channels,
.text = "Welcome to the channel!",
},
};
},
.PRIVMSG => |msg| {
if (Command.parse(msg.prefix, msg.targets, msg.text)) |command| {
return command.handle();
}
return null;
},
.PART => |msg| {
if (msg.reason) |msg_reason| {
if (std.mem.containsAtLeast(u8, msg_reason, 1, "goodbye")) {
return zircon.Message{
.PRIVMSG = .{
.targets = msg.channels,
.text = "Goodbye for you too!",
},
};
}
}
},
.NICK => |msg| {
return zircon.Message{ .PRIVMSG = .{
.targets = "#geeks",
.text = msg.nickname,
} };
},
else => return null,
}
return null;
}
/// spawnThread is called by zircon.Client.loop to decide when to spawn a thread.
/// The message parameter holds the IRC message that arrived from the server.
/// You can switch on the message tagged union to decide based on its kind.
/// On this example we only care about messages of type PRIVMSG or PART.
/// To spawn a thread we return true to the loop or false otherwise.
/// We should spawn a thread for long running tasks like for instance a bot command.
/// Otherwise we might block the main thread where zircon.Client.loop is running.
fn spawnThread(message: zircon.Message) bool {
switch (message) {
.PRIVMSG => return true,
.PART => return true,
else => return false,
}
}
/// Command encapsulates each command that our IRC bot supports.
pub const Command = struct {
name: CommandName,
prefix: ?zircon.Prefix,
params: []const u8,
targets: []const u8,
pub const CommandName = enum {
echo,
substitute,
help,
quit,
};
pub const Operators = enum {
substitute,
};
const map = std.StaticStringMap(Command.CommandName).initComptime(.{
.{ "echo", CommandName.echo },
.{ "help", CommandName.help },
.{ "quit", CommandName.quit },
});
const operators = std.StaticStringMap(Command.CommandName).initComptime(.{
.{ "s", CommandName.substitute },
});
pub fn parse(prefix: ?zircon.Prefix, targets: []const u8, text: []const u8) ?Command {
var iter = std.mem.tokenizeAny(u8, text, &std.ascii.whitespace);
const name = iter.next() orelse return null;
const command_name = map.get(name);
if (command_name == null) {
var parts = std.mem.splitScalar(u8, name, '/');
const operator = parts.next() orelse return null;
return .{
.name = operators.get(operator) orelse return null,
.prefix = prefix,
.params = parts.rest(),
.targets = targets,
};
}
if (name.len < 2) return null;
return .{
.name = map.get(name) orelse return null,
.prefix = prefix,
.params = iter.rest(),
.targets = targets,
};
}
pub fn handle(self: Command) ?zircon.Message {
switch (self.name) {
.echo => return echo(self.targets, self.params),
.help => return help(self.prefix, self.targets),
.substitute => unreachable,
.quit => return quit(self.params),
}
}
fn echo(targets: []const u8, params: []const u8) ?zircon.Message {
return zircon.Message{
.PRIVMSG = .{
.targets = targets,
.text = params,
},
};
}
fn help(prefix: ?zircon.Prefix, targets: []const u8) ?zircon.Message {
return zircon.Message{
.PRIVMSG = .{
.targets = if (prefix) |p| p.nick orelse targets else targets,
.text = "This is the help message!",
},
};
}
fn quit(params: []const u8) ?zircon.Message {
return zircon.Message{
.QUIT = .{
.reason = params,
},
};
}
};