diff --git a/lib/module.h b/lib/module.h index 3b130c0..0f2e301 100644 --- a/lib/module.h +++ b/lib/module.h @@ -4,5 +4,6 @@ #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 75064ab..8fa03f1 100755 Binary files a/mods/test.so and b/mods/test.so differ diff --git a/mods/test/Makefile b/mods/test/Makefile index c83dea2..3df7c81 100644 --- a/mods/test/Makefile +++ b/mods/test/Makefile @@ -1,4 +1,7 @@ -gcc -c -fPIC -I../../lib test.c -o test.o -gcc test.o -shared test.so -mv test.so .. -rm test.o \ No newline at end of file +CC=gcc +CFLAGS=-fPIC -I../../lib +OBJ=../test.so + +main: + $(CC) -shared -o $(OBJ) $(CFLAGS) ./test.c + @echo "All Done!" \ No newline at end of file diff --git a/mods/test/build.sh b/mods/test/build.sh deleted file mode 100755 index e69de29..0000000 diff --git a/mods/test/test.c b/mods/test/test.c index 1184ad1..7199325 100644 --- a/mods/test/test.c +++ b/mods/test/test.c @@ -1,17 +1,14 @@ #include "irc.h" #include "events.h" +#include "module.h" #include #include void hello(struct irc_conn *bot, char *user, char *chan, char *text) { - printf("hi\n"); - char *buf = (char *)malloc(sizeof(char *) * 500); sprintf(buf, "hi %s", bot->nick); - printf("trigger: %s\n", buf); - if (!strcmp(text, buf)) { irc_privmsg(bot, chan, "hi %s", user); @@ -20,7 +17,7 @@ void hello(struct irc_conn *bot, char *user, char *chan, char *text) free(buf); } -void mod_init(struct irc_conn *b, char *where) +void mod_init(void *handle, void (*export)()) { - add_handler(PRIVMSG_CHAN, hello); + export(handle, PRIVMSG_CHAN, "hello"); } \ No newline at end of file diff --git a/src/events.c b/src/events.c index 2547201..5bf7054 100644 --- a/src/events.c +++ b/src/events.c @@ -58,20 +58,21 @@ void handle_chan_privmsg(struct irc_conn *bot, char *user, char *chan, char *tex for (int i = 0; i < privmsg_chan.count; i++) { handler = privmsg_chan.handlers[i]; - ((void(*)())handler)(bot, user, chan, text); + (*handler)(bot, user, chan, text); } } void handle_self_privmsg(struct irc_conn *bot, char *user, char *text) { void (*handler)(); + int i; char *cmd, *arg, *modpath; cmd = text; arg = skip(cmd, ' '); modpath = (char *)malloc(sizeof(char)*500); - for (int i = 0; i < privmsg_self.count; i++) + for (i = 0; i < privmsg_self.count; i++) { handler = privmsg_self.handlers[i]; ((void(*)())handler)(bot, user, text); @@ -89,6 +90,22 @@ void handle_self_privmsg(struct irc_conn *bot, char *user, char *text) } } + if (!strcmp("PRINT_HANDLERS", cmd)) + { + if (strcmp(bot->admin, user)) + { + for (i = 0; i < privmsg_chan.count; i++) + { + irc_notice(bot, user, "handler[%i:%i]: %p", i, privmsg_chan.type, privmsg_chan.handlers[i]); + } + + for (i = 0; i < privmsg_self.count; i++) + { + irc_notice(bot, user, "handler[%i:%i]: %p", i, privmsg_self.type, privmsg_self.handlers[i]); + } + } + } + if (!strcmp("LOADMOD", cmd)) { if (strcmp(bot->admin, user)) diff --git a/src/main.c b/src/main.c index c1a73c6..c05a2b1 100644 --- a/src/main.c +++ b/src/main.c @@ -13,12 +13,6 @@ #include "events.h" -void hello_cmd(struct irc_conn *bot, char *user, char *text) -{ - printf("cmd exec\n"); - irc_notice(bot, user, "Hello"); -} - int main() { fd_set rd; diff --git a/src/module.c b/src/module.c index 99dd6e2..8dd1421 100644 --- a/src/module.c +++ b/src/module.c @@ -5,14 +5,22 @@ #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)(struct irc_conn *bot, char *where, void (*ah)()); + void (*mod_init)(void *handle, void (*export)()); char *error = (char *)malloc(sizeof(char *)*1024); - handle = dlopen(file, RTLD_NOW | RTLD_GLOBAL); + handle = dlopen(file, RTLD_NOW | RTLD_GLOBAL | RTLD_NODELETE); if (!handle) { sprintf(error, "Error: %s", dlerror()); @@ -47,7 +55,7 @@ void load_module(struct irc_conn *bot, char *where, int stype, char *file) } } - (*mod_init)(bot, where); + (*mod_init)(handle, export_handler); dlclose(handle);