From 97713e6c299bec38757b26c4d13b40615b3f3674 Mon Sep 17 00:00:00 2001 From: Aaron Blakely Date: Sun, 25 Feb 2024 11:09:24 -0600 Subject: [PATCH] working on lua module --- lib/module.h | 2 + mods/lua/Makefile | 7 ++++ mods/lua/lua.c | 99 +++++++++++++++++++++++++++++++++++++++++++++++ mods/lua/lua.h | 28 ++++++++++++++ scripts/hello.lua | 17 ++++++++ src/main.c | 1 + src/module.c | 11 ++++++ xbot.cfg | 2 +- 8 files changed, 166 insertions(+), 1 deletion(-) create mode 100755 mods/lua/Makefile create mode 100755 mods/lua/lua.c create mode 100755 mods/lua/lua.h create mode 100755 scripts/hello.lua diff --git a/lib/module.h b/lib/module.h index e3a114a..cf6efce 100755 --- a/lib/module.h +++ b/lib/module.h @@ -38,8 +38,10 @@ void init_mods(); void load_module(struct irc_conn *bot, char *where, char *stype, char *file); void unload_module(struct irc_conn *bot, char *where, char *file); void list_modules(struct irc_conn *bot, char *where); +void set_bot(struct irc_conn *b); MY_API void register_module(char *name, char *author, char *version, char *description); MY_API void unregister_module(char *name); MY_API struct mods *get_mods(); +MY_API struct irc_conn *get_bot(); #endif diff --git a/mods/lua/Makefile b/mods/lua/Makefile new file mode 100755 index 0000000..f519f3a --- /dev/null +++ b/mods/lua/Makefile @@ -0,0 +1,7 @@ +CC=gcc +CFLAGS=-fPIC -I../../lib -llua +OBJ=../lua.so + +main: + $(CC) -shared -o $(OBJ) $(CFLAGS) ./lua.c + @echo "All Done!" diff --git a/mods/lua/lua.c b/mods/lua/lua.c new file mode 100755 index 0000000..70af434 --- /dev/null +++ b/mods/lua/lua.c @@ -0,0 +1,99 @@ +#define MY_DLL_EXPORTS 1 + +#include "util.h" +#include "irc.h" +#include "events.h" +#include "module.h" +#include "timers.h" +#include +#include +#include + +#include "lua.h" + +struct lua_interp lua; +struct irc_conn *instance; + +MY_API void lua_eval(struct irc_conn *bot, char *user, char *host, char *chan, const char *text) +{ + printf("lua eval called with %s\n", text); + if (strstr(text, "!lua") != NULL) + { + // skip the command + text = skip(text, ' '); + printf("lua: %s\n", text); + + if (luaL_dostring(lua.L, text)) + { + irc_privmsg(bot, chan, "Error: %s", lua_tostring(lua.L, -1)); + } + } +} + +MY_API void lua_load_script(struct irc_conn *bot, char *user, char *host, char *chan, const char *text) +{ + char *name; + char *script; + char *buf = (char *)malloc(sizeof(char *) * 500); + + if (strstr(text, "!load") != NULL) + { + text = skip((char *)text, ' '); + sprintf(buf, "../scripts/%s", text); + + strlcpy(lua.scripts[lua.count].fname, buf, 150); + + if (luaL_loadfile(lua.L, buf)) + { + irc_privmsg(bot, chan, "Error: %s", lua_tostring(lua.L, -1)); + return; + } + + sprintf(buf, "Loaded %s", name); + irc_privmsg(bot, chan, buf); + } + + free(buf); +} + +void raw_wrapper(lua_State *L) +{ + char *text = (char *)lua_tostring(L, 1); + + irc_raw(instance, text); +} + +void privmsg_wrapper(lua_State *L) +{ + char *where = (char *)lua_tostring(L, 1); + char *text = (char *)lua_tostring(L, 2); + + irc_privmsg(instance, where, text); +} + +MY_API void mod_init() +{ + instance = get_bot(); + + lua.scripts = calloc(512, sizeof(struct lua_script)); + lua.count = 0; + + lua.L = luaL_newstate(); + luaL_openlibs(lua.L); + + lua_register(lua.L, "privmsg", privmsg_wrapper); + lua_register(lua.L, "raw", raw_wrapper); + + register_module("lua", "Aaron Blakely", "v0.1", "Lua module"); + add_handler(PRIVMSG_CHAN, lua_eval); + printf("Lua module loaded\n"); +} + +MY_API void mod_unload() +{ + lua_close(lua.L); + + unregister_module("lua"); + del_handler(PRIVMSG_CHAN, lua_eval); + printf("Lua module unloaded\n"); +} diff --git a/mods/lua/lua.h b/mods/lua/lua.h new file mode 100755 index 0000000..ae2e568 --- /dev/null +++ b/mods/lua/lua.h @@ -0,0 +1,28 @@ +#ifndef LUA_H +#define LUA_H + +#include +#include +#include +#include "irc.h" + +struct lua_script +{ + char name[25]; + char author[50]; + char version[10]; + char description[256]; + + char fname[256]; +}; + +struct lua_interp +{ + lua_State *L; + int count; + struct lua_script *scripts; +}; + +extern struct lua_interp lua; + +#endif diff --git a/scripts/hello.lua b/scripts/hello.lua new file mode 100755 index 0000000..ea7ed44 --- /dev/null +++ b/scripts/hello.lua @@ -0,0 +1,17 @@ +NAME = "hello" +VERSION = "v0.5" +AUTHOR = "Aaron Blakely" +DESCRIPTION = "A simple hello world script for xbot" + + +function hi(nick, host, chan, text) + irc_privmsg(chan, "Hello, " .. nick .. "!") +end + +function load() + add_lua_handler("PRIVMSG_CHAN", hi) +end + +function unload() + remove_lua_handler("PRIVMSG_CHAN", hi) +end diff --git a/src/main.c b/src/main.c index d20fc16..041a4ab 100755 --- a/src/main.c +++ b/src/main.c @@ -42,6 +42,7 @@ int main() last_ping.tv_sec = time(NULL); + set_bot(&bot); init_events(); init_timers(); init_mods(); diff --git a/src/module.c b/src/module.c index c1fdae7..2e1f970 100755 --- a/src/module.c +++ b/src/module.c @@ -13,6 +13,7 @@ #endif struct mods *mods; +struct irc_conn *instance; void init_mods() { @@ -304,3 +305,13 @@ MY_API struct mods *get_mods() { return mods; } + +MY_API struct irc_conn *get_bot() +{ + return instance; +} + +MY_API void set_bot(struct irc_conn *b) +{ + instance = b; +} diff --git a/xbot.cfg b/xbot.cfg index c4c9945..958ad5a 100755 --- a/xbot.cfg +++ b/xbot.cfg @@ -16,7 +16,7 @@ server: mods: { - autoload = ("chanop", "autojoin", "hello", "uptime"); + autoload = ("chanop", "lua", "autojoin", "hello", "uptime"); blacklist = (); # config option for mods/autojoin.so