xbot/mods/lua/lua.c

98 lines
2.0 KiB
C
Raw Normal View History

2024-02-25 09:09:24 -08:00
#define MY_DLL_EXPORTS 1
#include "util.h"
#include "irc.h"
#include "events.h"
#include "module.h"
#include "timers.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "lua.h"
struct lua_interp lua;
struct irc_conn *instance;
2024-02-26 23:16:58 -08:00
2024-02-25 09:09:24 -08:00
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));
}
}
}
2024-02-26 23:16:58 -08:00
2024-02-25 09:09:24 -08:00
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);
2024-02-26 23:16:58 -08:00
strlcpy(lua.scripts[lua.script_count].fname, buf, 150);
2024-02-25 09:09:24 -08:00
if (luaL_loadfile(lua.L, buf))
{
irc_privmsg(bot, chan, "Error: %s", lua_tostring(lua.L, -1));
return;
}
sprintf(buf, "Loaded %s", name);
2024-02-26 23:16:58 -08:00
lua.script_count++;
2024-02-25 09:09:24 -08:00
irc_privmsg(bot, chan, buf);
}
free(buf);
}
2024-02-26 23:16:58 -08:00
void lua_setvar(char *name, char *value)
2024-02-25 09:09:24 -08:00
{
2024-02-26 23:16:58 -08:00
lua_pushstring(lua.L, value);
lua_setglobal(lua.L, name);
2024-02-25 09:09:24 -08:00
}
MY_API void mod_init()
{
instance = get_bot();
lua.scripts = calloc(512, sizeof(struct lua_script));
2024-02-26 23:16:58 -08:00
lua.events = calloc(1024, sizeof(struct lua_event));
lua.script_count = 0;
lua.event_count = 0;
2024-02-25 09:09:24 -08:00
lua.L = luaL_newstate();
luaL_openlibs(lua.L);
2024-02-26 23:16:58 -08:00
lua_init_wrappers();
lua_init_events();
2024-02-25 09:09:24 -08:00
register_module("lua", "Aaron Blakely", "v0.1", "Lua module");
2024-02-26 23:16:58 -08:00
lua_init_handlers();
2024-02-25 09:09:24 -08:00
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");
}