improving logging of the bot

This commit is contained in:
Aaron Blakely 2024-03-09 03:38:58 -06:00
parent 5ec7b93a7a
commit 015c20e061
14 changed files with 83 additions and 72 deletions

View File

@ -7,6 +7,8 @@
#ifndef IRC_H
#define IRC_H
#define VERSION "0.1.0"
#include <stdio.h>
#include "util.h"

View File

@ -25,7 +25,7 @@ int lua_add_handler(lua_State *L)
if (lua_gettop(L) < 2)
{
printf("Error: add_handler requires 2 arguments\n");
xlog("[lua] Error: add_handler requires 2 arguments\n");
return 0;
}
@ -36,12 +36,12 @@ int lua_add_handler(lua_State *L)
if (lreg == -1)
{
const char *err = lua_tostring(L, -1);
printf("Error: %s\n", err);
xlog("[lua/events/lua_add_handler] Error: %s\n", err);
}
event = (char *)lua_tostring(L, 1);
printf("Installing handler for event: %s : %d\n", event, lreg);
xlog("[lua] Installing handler for event: %s : %d\n", event, lreg);
strlcpy(lua.events[lua.event_count].event, event, 25);
lua.events[lua.event_count].lreg = lreg;
@ -61,7 +61,7 @@ void lua_del_handler(lua_State *L)
if (lua_gettop(L) < 2)
{
printf("Error: del_handler requires 2 arguments\n");
xlog("[lua] Error: del_handler requires 2 arguments\n");
return;
}
@ -77,7 +77,7 @@ void lua_del_handler(lua_State *L)
{
if (lua.events[i].lreg == lreg)
{
printf("Removing handler for event: %s : %d\n", event, lreg);
xlog("[lua] Removing handler for event: %s : %d\n", event, lreg);
luaL_unref(L, LUA_REGISTRYINDEX, lua.events[i].lreg);
while (i < lua.event_count)
@ -111,7 +111,7 @@ void lua_callfunc(int lreg, int argc, ...)
if (lua_pcall(lua.L, argc, 0, 0) != LUA_OK)
{
const char *err = lua_tostring(L, -1);
printf("Error: %s\n", err);
xlog("[lua/events/lua_callfunc] Error: %s\n", err);
lua_pop(L, 1);
}
@ -126,8 +126,6 @@ void lua_fire_handlers(char *event, ...)
va_list args;
char *user, *host, *chan, *text;
printf("lua_fire_handlers: %s\n", event);
for (i = 0; i < lua.event_count; i++)
{
if (!lua.events[i].lreg)
@ -144,9 +142,6 @@ void lua_fire_handlers(char *event, ...)
chan = va_arg(args, char *);
text = va_arg(args, char *);
printf("dbug: %s %s %s %s\n", user, host, chan, text);
printf("dbug: %s %d %d\n", lua.events[i].event, lua.events[i].lreg, lua.event_count);
lua_callfunc(lua.events[i].lreg, 4, user, host, chan, text);
}
else if (!strcmp(event, PRIVMSG_SELF))

View File

@ -34,5 +34,5 @@ function del_handler(type, func)
end
end
print("Handler not found")
xlog("[lua/init.lua] del_handler: Handler not found")
end

View File

@ -125,12 +125,9 @@ void lua_eval(struct irc_conn *bot, char *user, char *host, char *chan, const ch
return;
block = 1;
printf("lua eval called with %s\n", text);
if (strstr(text, "!lua") != NULL)
{
text = skip(text, ' ');
printf("lua: %s\n", text);
res = luaL_loadstring(lua.L, text);
if (res == LUA_OK)
@ -174,11 +171,12 @@ void lua_load_script(struct irc_conn *bot, char *user, char *host, char *chan, c
{
if (!strcmp(chan, "-stdio-"))
{
printf("Error loading lua script: %s\n", lua_tostring(lua.L, -1));
xlog("[lua] Error loading lua script: %s\n", lua_tostring(lua.L, -1));
}
else
{
irc_privmsg(bot, chan, "Error loading lua script: %s", lua_tostring(lua.L, -1));
irc_privmsg(bot, chan, "[lua] Error loading lua script: %s", lua_tostring(lua.L, -1));
xlog("[lua] Error loading lua script: %s\n", lua_tostring(lua.L, -1));
}
return;
@ -193,11 +191,12 @@ void lua_load_script(struct irc_conn *bot, char *user, char *host, char *chan, c
{
if (!strcmp(chan, "-stdio-"))
{
printf("Error executing lua script: %s\n", lua_tostring(lua.L, -1));
xlog("[lua] Error executing lua script: %s\n", lua_tostring(lua.L, -1));
}
else
{
irc_privmsg(bot, chan, "Error executing lua script: %s", lua_tostring(lua.L, -1));
xlog("[lua] Error executing lua script: %s\n", lua_tostring(lua.L, -1));
}
return;
@ -209,7 +208,6 @@ void lua_load_script(struct irc_conn *bot, char *user, char *host, char *chan, c
if (lua_isfunction(lua.L, -1))
{
lua.scripts[lua.script_count].unload = luaL_ref(lua.L, LUA_REGISTRYINDEX);
printf("dbg: unload ref: %d\n", lua.scripts[lua.script_count].unload);
}
else
{
@ -217,11 +215,12 @@ void lua_load_script(struct irc_conn *bot, char *user, char *host, char *chan, c
if (!strcmp(chan, "-stdio-"))
{
printf("No unload() function in %s\n", name);
xlog("[lua] No unload() function in %s\n", name);
}
else
{
irc_privmsg(bot, chan, "No unload() function in %s", name);
xlog("[lua] No unload() function in %s [issued by %s!%s@%s]\n", name, user, host, chan);
}
}
@ -234,11 +233,12 @@ void lua_load_script(struct irc_conn *bot, char *user, char *host, char *chan, c
{
if (!strcmp(chan, "-stdio-"))
{
printf("Error calling load() in %s: %s\n", buf, lua_tostring(lua.L, -1));
xlog("[lua] Error calling load() in %s: %s\n", buf, lua_tostring(lua.L, -1));
}
else
{
irc_privmsg(bot, chan, "Error calling load() in %s: %s", buf, lua_tostring(lua.L, -1));
xlog("[lua] Error calling load() in %s: %s\n", buf, lua_tostring(lua.L, -1));
}
return;
@ -248,22 +248,24 @@ void lua_load_script(struct irc_conn *bot, char *user, char *host, char *chan, c
if (!strcmp(chan, "-stdio-"))
{
printf("Loaded %s\n", name);
xlog("[lua] Loaded %s\n", name);
}
else
{
irc_privmsg(bot, chan, "Loaded %s", name);
xlog("[lua] Loaded %s [issued by %s!%s@%s]\n", name, user, host, chan);
}
}
else
{
if (!strcmp(chan, "-stdio-"))
{
printf("Error: No load() function in %s\n", buf);
xlog("[lua] Error: No load() function in %s\n", buf);
}
else
{
irc_privmsg(bot, chan, "Error: No load() function in %s", buf);
xlog("[lua] Error: No load() function in %s [issued by %s!%s@%s]\n", buf, user, host, chan);
}
}
}
@ -299,6 +301,9 @@ void lua_unload_script(struct irc_conn *bot, char *user, char *host, char *chan,
if (lua_pcall(lua.L, 0, 0, 0) != LUA_OK)
{
irc_privmsg(bot, chan, "Error calling unload() in %s: %s", buf, lua_tostring(lua.L, -1));
xlog("[lua] Error calling unload() in %s: %s\n", buf, lua_tostring(lua.L, -1));
free(buf);
return;
}
@ -307,8 +312,8 @@ void lua_unload_script(struct irc_conn *bot, char *user, char *host, char *chan,
remove_script(text);
sprintf(buf, "Unloaded %s", text);
irc_privmsg(bot, chan, buf);
irc_privmsg(bot, chan, "Unloaded %s", text);
xlog("[lua] Unloaded %s [issued by %s!%s@%s]\n", text, user, host, chan);
while (i < lua.script_count)
{
@ -324,6 +329,7 @@ void lua_unload_script(struct irc_conn *bot, char *user, char *host, char *chan,
}
irc_privmsg(bot, chan, "Error: %s not loaded", text);
xlog("[lua] Error: %s not loaded [issued by %s!%s@%s]\n", text, user, host, chan);
}
@ -362,33 +368,37 @@ void mod_init()
// load init.lua
if (luaL_loadfile(lua.L, "./mods/lua/init.lua") != LUA_OK)
{
printf("Error loading init.lua: %s\n", lua_tostring(lua.L, -1));
xlog("[lua] Error loading init.lua: %s\n", lua_tostring(lua.L, -1));
free(buf);
return;
}
if (lua_pcall(lua.L, 0, 0, 0) != LUA_OK)
{
printf("Error executing init.lua: %s\n", lua_tostring(lua.L, -1));
xlog("[lua] Error executing init.lua: %s\n", lua_tostring(lua.L, -1));
free(buf);
return;
}
for (i = 0; i < list.count; i++)
{
printf("Loading %s\n", list.scripts[i]);
xlog("[lua] Loading %s\n", list.scripts[i]);
sprintf(buf, "./scripts/%s", list.scripts[i]);
strlcpy(lua.scripts[lua.script_count].fname, buf, 150);
if (luaL_loadfile(lua.L, buf) != LUA_OK)
{
printf("Error loading lua script: %s\n", lua_tostring(lua.L, -1));
xlog("[lua] Error loading lua script: %s\n", lua_tostring(lua.L, -1));
continue;
}
// execute the script
if (lua_pcall(lua.L, 0, 0, 0) != LUA_OK)
{
printf("Error executing lua script: %s\n", lua_tostring(lua.L, -1));
xlog("[lua] Error executing lua script: %s\n", lua_tostring(lua.L, -1));
continue;
}
@ -398,12 +408,11 @@ void mod_init()
if (lua_isfunction(lua.L, -1))
{
lua.scripts[lua.script_count].unload = luaL_ref(lua.L, LUA_REGISTRYINDEX);
printf("dbg: unload ref: %d\n", lua.scripts[lua.script_count].unload);
}
else
{
lua.scripts[lua.script_count].unload = -1;
printf("No unload() function in %s\n", list.scripts[i]);
xlog("[lua] No unload() function in %s\n", list.scripts[i]);
}
// call the load function if it exists
@ -413,20 +422,19 @@ void mod_init()
{
if (lua_pcall(lua.L, 0, 0, 0) != LUA_OK)
{
printf("Error calling load() in %s: %s\n", buf, lua_tostring(lua.L, -1));
xlog("[lua] Error calling load() in %s: %s\n", buf, lua_tostring(lua.L, -1));
continue;
}
lua.script_count++;
printf("Loaded %s\n", list.scripts[i]);
xlog("[lua] Loaded %s\n", list.scripts[i]);
}
else
{
printf("Error: No load() function in %s\n", buf);
xlog("[lua] Error: No load() function in %s\n", buf);
}
}
printf("Lua module loaded\n");
free(buf);
}
@ -439,5 +447,4 @@ void mod_unload()
unregister_module("lua");
del_handler(PRIVMSG_CHAN, lua_eval);
printf("Lua module unloaded\n");
}

View File

@ -61,6 +61,7 @@ void lua_fire_handlers(char *event, ...);
// wrappers.c
void lua_init_wrappers();
void xlog_wrapper(lua_State *L);
void raw_wrapper(lua_State *L);
void privmsg_wrapper(lua_State *L);
void notice_wrapper(lua_State *L);

View File

@ -1,2 +0,0 @@
test.lua
hello.lua

View File

@ -11,6 +11,14 @@ void lua_init_wrappers()
lua_register(lua.L, "kick", kick_wrapper);
lua_register(lua.L, "mode", mode_wrapper);
lua_register(lua.L, "ctcp", ctcp_wrapper);
lua_register(lua.L, "xlog", xlog_wrapper);
}
void xlog_wrapper(lua_State *L)
{
char *msg = (char *)lua_tostring(L, 1);
xlog("%s", msg);
}
void raw_wrapper(lua_State *L)

View File

@ -16,7 +16,7 @@ void add_channel(char *name)
if (channel_exists(name) == 1)
return;
xlog("Adding channel %s\n", name);
xlog("[channel] Adding channel %s\n", name);
channels[chan_count] = calloc(1, sizeof(struct channel));
strlcpy(channels[chan_count]->name, name, 32);
@ -35,7 +35,7 @@ void remove_channel(char *name)
{
if (!strcmp(channels[i]->name, name))
{
xlog("Removing channel %s\n", name);
xlog("[channel] Removing channel %s\n", name);
free(channels[i]->users);
free(channels[i]);
@ -122,7 +122,7 @@ void add_user_to_channel(char *user, char *host, char *chan)
if (user_exists(chan, user) == 1)
return;
xlog("Adding user %s!%s to channel %s\n", user, host, chan);
xlog("[channel] Adding user %s!%s to channel %s\n", user, host, chan);
for (i = 0; i < chan_count; i++)
{
@ -181,7 +181,7 @@ void remove_user_from_channel(char *user, char *chan)
{
if (!strcmp(channels[i]->users[j].nick, user))
{
xlog("Removing user %s from channel %s\n", user, chan);
xlog("[channel] Removing user %s from channel %s\n", user, chan);
for (j = j; j < channels[i]->user_count; j++)
{
@ -286,7 +286,7 @@ void user_quit(char *nick)
{
if (!strcmp(channels[i]->users[j].nick, nick))
{
xlog("Removing user %s from channel %s\n", nick, channels[i]->name);
xlog("[channel] Removing user %s from channel %s\n", nick, channels[i]->name);
for (j = j; j < channels[i]->user_count; j++)
{

View File

@ -43,11 +43,10 @@ void init_events()
MY_API int add_handler(char *type, void *handler)
{
int i;
xlog("Installing handler @ %p [type: %s]\n", handler, type);
xlog("[events] Installing handler @ %p [type: %s]\n", handler, type);
for (i = 0; i < handlers_count; i++)
{
printf("comparing %s to %s\n", handlers[i]->type, type);
if (!strcmp(handlers[i]->type, type))
{
if (handlers[i]->count < 128)
@ -57,12 +56,11 @@ MY_API int add_handler(char *type, void *handler)
handlers[i]->count++;
printf("type %s count: %d\n", type, handlers[i]->count);
return handlers[i]->count - 1;
}
else
{
xlog("Handler array is full, cannot add more handlers.\n");
xlog("[events] Handler array is full, cannot add more handlers.\n");
return -1;
}
}

View File

@ -123,10 +123,10 @@ void irc_connect(struct irc_conn *bot)
freeaddrinfo(res);
if (!r)
{
eprint("Error: Cannot connect to host '%s'\n", bot->host);
eprint("[IRC] Error: Cannot connect to host '%s'\n", bot->host);
}
xlog("Connected!\n");
xlog("[IRC] Connected!\n");
bot->srv_fd = FDOPEN(srv_fd, "r+");
#endif
}
@ -134,7 +134,7 @@ void irc_connect(struct irc_conn *bot)
void irc_auth(struct irc_conn *bot)
{
irc_raw(bot, "NICK %s", bot->nick);
irc_raw(bot, "USER %s \" %s :xbot (v0.5) - developed by ab3800", bot->nick, bot->host);
irc_raw(bot, "USER %s \" %s :xbot v%s (https://github.com/ablakely/xbot)", bot->nick, bot->host, VERSION);
#ifndef _WIN32
fflush(bot->srv_fd);
@ -291,9 +291,9 @@ void irc_parse_raw(struct irc_conn *bot, char *raw)
if (!strcmp("VERSION", ctcp))
{
#ifdef _WIN32
irc_notice(bot, user, "VERSION xbot: v0.5 (Windows) - Developed by ab3800");
irc_notice(bot, user, "VERSION xbot: v%s (Windows)", VERSION);
#else
irc_notice(bot, user, "VERSION xbot: v0.5 (Linux) - Developed by ab3800");
irc_notice(bot, user, "VERSION xbot: v%s (Linux)", VERSION);
#endif
}
else

View File

@ -25,12 +25,12 @@ void xlog(char *fmt, ...)
va_list args;
time_t tv;
char *buf = calloc(8128, sizeof(char));
char *buf = calloc(4165, sizeof(char));
char *msg = calloc(4096, sizeof(char));
char *tbuf = calloc(64, sizeof(char));
va_start(args, fmt);
vsnprintf(msg, 4096, fmt, args);
vsnprintf(msg, 4095, fmt, args);
time(&tv);

View File

@ -4,7 +4,6 @@
* Written by Aaron Blakely <aaron@ephasic.org>
**/
#define VERSION "0.1.0"
#include <stdio.h>
#include <string.h>
@ -109,7 +108,7 @@ int main(int argc, char **argv)
if (access(bot.db_file, F_OK) == -1)
#endif
{
xlog("Creating database file: %s\n", bot.db_file);
xlog("[DB] Creating database file: %s\n", bot.db_file);
bot.db = (struct db_table *)malloc(sizeof(struct db_table));
memset(bot.db, 0, sizeof(struct db_table));
set_bot_db(bot.db);
@ -121,7 +120,7 @@ int main(int argc, char **argv)
}
else
{
xlog("Reading database file: %s\n", bot.db_file);
xlog("[DB] Reading database file: %s\n", bot.db_file);
bot.db = db_read(bot.db_file);
set_bot_db(bot.db);
}
@ -130,7 +129,7 @@ int main(int argc, char **argv)
run_autoload(&bot);
// Connect to the server
xlog("Connecting to %s...\n", bot.host);
xlog("[IRC] Connecting to %s...\n", bot.host);
irc_connect(&bot);
trespond = time(NULL);

View File

@ -108,10 +108,8 @@ void load_module(struct irc_conn *bot, char *where, char *stype, char *file)
{
irc_notice(bot, where, "Module '%s' loaded.", file);
}
else
{
xlog("Module '%s' loaded.\n", file);
}
xlog("[module] Module '%s' loaded.\n", file);
free(error);
#else
void (*mod_init)();
@ -120,7 +118,7 @@ void load_module(struct irc_conn *bot, char *where, char *stype, char *file)
mods->modules[mods->count].handle = dlopen(file, RTLD_NOW | RTLD_GLOBAL | RTLD_NODELETE);
if (!mods->modules[mods->count].handle)
{
sprintf(error, "Error: %s", dlerror());
sprintf(error, "[module] Error loading %s: %s", file, dlerror());
if (strcmp("runtime", stype))
{
@ -136,6 +134,7 @@ void load_module(struct irc_conn *bot, char *where, char *stype, char *file)
irc_notice(bot, where, error);
}
xlog("[module] %s", error);
return;
}
@ -145,11 +144,9 @@ void load_module(struct irc_conn *bot, char *where, char *stype, char *file)
if ((error = dlerror()) != NULL)
{
//sprintf(error, "Error: %s", error);
eprint("Error: %s\n", error);
if (strcmp("runtime", stype))
{
eprint("Error: %s\n", error);
return;
}
else if (strcmp(PRIVMSG_CHAN, stype))
@ -160,6 +157,8 @@ void load_module(struct irc_conn *bot, char *where, char *stype, char *file)
{
irc_notice(bot, where, error);
}
xlog("[module] Error: %s", error);
}
(*mods->modules[mods->count].init)();
@ -210,7 +209,7 @@ void load_module(struct irc_conn *bot, char *where, char *stype, char *file)
}
else
{
xlog("Module '%s' loaded.\n", file);
xlog("[module] Module '%s' loaded.\n", file);
}
free(error);
#endif
@ -239,7 +238,7 @@ void unload_module(struct irc_conn *bot, char *where, char *file)
}
else
{
xlog("Module '%s' unloaded.\n", file);
xlog("[module] Module '%s' unloaded.\n", file);
}
while (i < mods->count)

View File

@ -15,18 +15,22 @@
void eprint(char *fmt, ...)
{
char msg[4096];
char bufout[4096];
va_list ap;
va_start(ap, fmt);
vsnprintf(bufout, sizeof bufout, fmt, ap);
vsnprintf(msg, sizeof msg, fmt, ap);
va_end(ap);
fprintf(stderr, "%s", bufout);
sprintf(bufout, "%s", msg);
if (fmt[0] && fmt[strlen(fmt) - 1] == ':')
{
fprintf(stderr, "%s\n", strerror(errno));
sprintf(bufout, "%s\n", strerror(errno));
}
fprintf(stderr, "%s", bufout);
xlog("%s", bufout);
}
#if defined(__GLIBC__) && (__GLIBC__ < 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ < 38)) || defined(_WIN32)