From a718957b7f01eb8e82849dd8881646b7424bd53d Mon Sep 17 00:00:00 2001 From: Aaron Blakely Date: Tue, 7 Apr 2015 01:56:50 -0500 Subject: [PATCH] Fixed the handler/module system --- lib/module.h | 1 + mods/test.so | Bin 8204 -> 8102 bytes mods/test/Makefile | 11 +++++++---- mods/test/build.sh | 0 mods/test/test.c | 9 +++------ src/events.c | 21 +++++++++++++++++++-- src/main.c | 6 ------ src/module.c | 14 +++++++++++--- 8 files changed, 41 insertions(+), 21 deletions(-) delete mode 100755 mods/test/build.sh 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 75064ab19ca90cf3933ec94d87736977ab41b31b..8fa03f11d97fa6d35b1eb022a8a0831012e40f62 100755 GIT binary patch delta 2408 zcmZ`)Z){XW5Z}E&y?e*~d)K?ZyKC#cYptHfcn4ALiZtyD0UwFzA@+koXiKEF5e2nG zFp{>ZVw-D|Ud9iEgnu6}#1sSJ1EPtxDSwKIcxWW`8;!wSBPQj8gqp}XySH200+YO* zo!`#R%$wbv8E*Nac{Hr*6@*hunUQS%skAZXEK8CTqH}=a$n+ClE+W_YL~_5SjO+Om znwk|#S)r88Uu{!u!tw%#M%osCl)Y)@5NR4N&{*TDokxGG|NXpQO&NAk?7y2Bez zvQ;a7F8pM4!{fS@*jf~Zx$*OYYH3yv*>SpvI-tX%(U;i>#6qNFZKxr zg4PF``fwv@MK?WBRJSs9jK{mp9BJxwK%e7r;{oyocPV5 z1BmXO<92hK=FE{$JMR3BWm;}8UB5bsd170(h&*;xVB7=uN<9%w$yOA6F_iywVp?-n zwg9J{{^wp{ZrO2nb?XfukglD(cCm{{tF15S$l2+Q{aY%nMP5YJH<9m$dFPY%E=2c0 zgHj*=7;%144X^*8Wu2$OGVFFZ38_I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h(?fzafH{?2TDTw6p7doiZvB?0(i@@PFjW!9YN^=Tycb?G~^t?s=d^R zDAaEx8Iy*XhRXnKED54-8{Q`%UNQ*1l2uY+$>+wADYl&&7v*IgyLEg+$Fn+~)A6E? zU+EZ~i8%9;bPFP-A=fgk3W~rBa01y0y(EE8!gOiKmeyH(jM93SC|lqFMBE{lcsPM+ zIN=W2#oBl+Fy-Ft3hR{!9ET;GD$X*2%Md9G*~Ny6%XzG9vuj>oC4uEIT^6$admiP= z>g~8aYhr_^J)4Wg@y3rSJPh10o^b*(l^Z^|qC>4>p$97mMBX z7Jg+6QWU2=EbWEq@{mpJz&H;*#i60n2F_zg+CMp?qyQLed}3RQj?+vO#{2VV61kw4}47-i&7 z`KhO;@vguxe%UA1RD^B7frQ6j6kGw>m(kz4u #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);