diff --git a/Makefile b/Makefile index 019e36a..c950972 100644 --- a/Makefile +++ b/Makefile @@ -11,6 +11,7 @@ EXEC=xbot main: @rm -rf build @mkdir build + $(CC) $(CFLAGS) $(SRC)/config.c -o $(OBJ)/config.o $(CC) $(CFLAGS) $(SRC)/main.c -o $(OBJ)/main.o $(CC) $(CFLAGS) $(SRC)/irc.c -o $(OBJ)/irc.o $(CC) $(CFLAGS) $(SRC)/util.c -o $(OBJ)/util.o diff --git a/lib/config.h b/lib/config.h new file mode 100644 index 0000000..5b682a1 --- /dev/null +++ b/lib/config.h @@ -0,0 +1,8 @@ +#ifndef CONFIG_H +#define CONFIG_H + +#include "irc.h" + +struct irc_conn read_config(struct irc_conn bot, char *file); + +#endif \ No newline at end of file diff --git a/lib/irc.h b/lib/irc.h index 28f2d36..9c40825 100644 --- a/lib/irc.h +++ b/lib/irc.h @@ -15,9 +15,9 @@ struct irc_conn char nick[32]; char *admin; - char *host; - char *port; - char *real_name; + char host[256]; + char port[5]; + char real_name[512]; // I/O Buffers char out[4096]; diff --git a/lib/module.h b/lib/module.h index 0f2e301..3b130c0 100644 --- a/lib/module.h +++ b/lib/module.h @@ -4,6 +4,5 @@ #include "irc.h" void load_module(struct irc_conn *bot, char *where, int stype, char *file); -void export_handler(int type, char *hname); #endif \ No newline at end of file diff --git a/mods/test.so b/mods/test.so index 8fa03f1..dc996a1 100755 Binary files a/mods/test.so and b/mods/test.so differ diff --git a/mods/test/test.c b/mods/test/test.c index 7199325..cf64871 100644 --- a/mods/test/test.c +++ b/mods/test/test.c @@ -17,7 +17,7 @@ void hello(struct irc_conn *bot, char *user, char *chan, char *text) free(buf); } -void mod_init(void *handle, void (*export)()) +void mod_init() { - export(handle, PRIVMSG_CHAN, "hello"); + add_handler(PRIVMSG_CHAN, hello); } \ No newline at end of file diff --git a/src/config.c b/src/config.c new file mode 100644 index 0000000..52054e5 --- /dev/null +++ b/src/config.c @@ -0,0 +1,57 @@ +#include +#include +#include +#include "irc.h" +#include "util.h" +#include "module.h" + +struct irc_conn read_config(struct irc_conn bot, char *file) +{ + int count, n; + config_t cfg, *cf; + const config_setting_t *autoload; + const char *base = (const char*)malloc(sizeof(char *) * 1024); + const char *mod = NULL; + char *modpath = (char *)malloc(sizeof(char *) * 500); + + + cf = &cfg; + config_init(cf); + + if (!config_read_file(cf, file)) + { + printf("[xbot.cfg:%d] Configuration error: %s\n", + config_error_line(cf), + config_error_text(cf) + ); + + config_destroy(cf); + exit(-1); + } + + if (config_lookup_string(cf, "bot.nick", &base)) + strlcpy(bot.nick, base, sizeof bot.nick); + + if (config_lookup_string(cf, "server.host", &base)) + strlcpy(bot.host, base, sizeof bot.host); + + if (config_lookup_string(cf, "server.port", &base)) + strlcpy(bot.port, base, sizeof bot.port); + + if (config_lookup_string(cf, "bot.admin", &base)) + bot.admin = (char *)base; + + autoload = config_lookup(cf, "mods.autoload"); + count = config_setting_length(autoload); + + for (n = 0; n < count; n++) + { + mod = config_setting_get_string_elem(autoload, n); + sprintf(modpath, "./mods/%s.so", mod); + load_module(&bot, "main", 3, modpath); + } + + config_destroy(cf); + + return bot; +} \ No newline at end of file diff --git a/src/main.c b/src/main.c index c05a2b1..8474fd9 100644 --- a/src/main.c +++ b/src/main.c @@ -6,58 +6,30 @@ #include #include +#include #include -#include +#include "config.h" #include "irc.h" #include "util.h" #include "events.h" +#include "module.h" int main() { fd_set rd; - config_t cfg, *cf; - const config_setting_t *retries; - const char *base = NULL; struct irc_conn bot; init_events(); - // Init the config parser - cf = &cfg; - config_init(cf); - - if (!config_read_file(cf, "xbot.cfg")) - { - printf("xbot.cfg:%d - %s\n", - config_error_line(cf), - config_error_text(cf)); - - config_destroy(cf); - return -1; - } - - // Fill our bot struct with values from the config - if (config_lookup_string(cf, "bot.nick", &base)) - strlcpy(bot.nick, base, sizeof bot.nick); - - if (config_lookup_string(cf, "server.host", &base)) - bot.host = (char *)base; - - if (config_lookup_string(cf, "server.port", &base)) - bot.port = (char *)base; - - if (config_lookup_string(cf, "bot.admin", &base)) - bot.admin = (char *)base; + // Read the config + bot = read_config(bot, "xbot.cfg"); // Connect to the server printf("Connecting to %s...\n", bot.host); irc_connect(&bot); irc_auth(&bot); - // Free the config before entering the main loop - config_destroy(cf); - for (;;) { diff --git a/src/module.c b/src/module.c index 8dd1421..50ec8f7 100644 --- a/src/module.c +++ b/src/module.c @@ -5,18 +5,10 @@ #include #include -void export_handler(void *handle, int stype, char *hname) -{ - void (*handler)(struct irc_conn *bot, char *user, char *chan, char *text); - - *(void **)(&handler) = dlsym(handle, hname); - add_handler(stype, handler); -} - void load_module(struct irc_conn *bot, char *where, int stype, char *file) { void *handle; - void (*mod_init)(void *handle, void (*export)()); + void (*mod_init)(); char *error = (char *)malloc(sizeof(char *)*1024); @@ -25,7 +17,12 @@ void load_module(struct irc_conn *bot, char *where, int stype, char *file) { sprintf(error, "Error: %s", dlerror()); - if (stype == PRIVMSG_CHAN) + if (stype == 3) + { + eprint("%s\n", error); + return; + } + else if (stype == PRIVMSG_CHAN) { irc_privmsg(bot, where, error); } @@ -45,7 +42,12 @@ void load_module(struct irc_conn *bot, char *where, int stype, char *file) { //sprintf(error, "Error: %s", error); eprint("Error: %s\n", error); - if (stype == PRIVMSG_CHAN) + + if (stype == 3) + { + return; + } + else if (stype == PRIVMSG_CHAN) { irc_privmsg(bot, where, error); } @@ -55,11 +57,17 @@ void load_module(struct irc_conn *bot, char *where, int stype, char *file) } } - (*mod_init)(handle, export_handler); + (*mod_init)(); dlclose(handle); - irc_privmsg(bot, where, "Module '%s' loaded.", file); - + if (stype != 3) + { + irc_privmsg(bot, where, "Module '%s' loaded.", file); + } + else + { + printf("Module '%s' loaded.\n", file); + } free(error); } \ No newline at end of file diff --git a/xbot.cfg b/xbot.cfg index 3b080b3..1e7208d 100644 --- a/xbot.cfg +++ b/xbot.cfg @@ -11,4 +11,10 @@ server: { host = "irc.alphachat.net"; port = "6667"; +}; + +mods: +{ + autoload = ("test"); + blacklist = (); }; \ No newline at end of file