Fixed the handler/module system

This commit is contained in:
Aaron Blakely 2015-04-07 01:56:50 -05:00
parent 56f6159e19
commit a718957b7f
8 changed files with 41 additions and 21 deletions

View File

@ -4,5 +4,6 @@
#include "irc.h" #include "irc.h"
void load_module(struct irc_conn *bot, char *where, int stype, char *file); void load_module(struct irc_conn *bot, char *where, int stype, char *file);
void export_handler(int type, char *hname);
#endif #endif

Binary file not shown.

View File

@ -1,4 +1,7 @@
gcc -c -fPIC -I../../lib test.c -o test.o CC=gcc
gcc test.o -shared test.so CFLAGS=-fPIC -I../../lib
mv test.so .. OBJ=../test.so
rm test.o
main:
$(CC) -shared -o $(OBJ) $(CFLAGS) ./test.c
@echo "All Done!"

View File

View File

@ -1,17 +1,14 @@
#include "irc.h" #include "irc.h"
#include "events.h" #include "events.h"
#include "module.h"
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
void hello(struct irc_conn *bot, char *user, char *chan, char *text) void hello(struct irc_conn *bot, char *user, char *chan, char *text)
{ {
printf("hi\n");
char *buf = (char *)malloc(sizeof(char *) * 500); char *buf = (char *)malloc(sizeof(char *) * 500);
sprintf(buf, "hi %s", bot->nick); sprintf(buf, "hi %s", bot->nick);
printf("trigger: %s\n", buf);
if (!strcmp(text, buf)) if (!strcmp(text, buf))
{ {
irc_privmsg(bot, chan, "hi %s", user); 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); 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");
} }

View File

@ -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++) for (int i = 0; i < privmsg_chan.count; i++)
{ {
handler = privmsg_chan.handlers[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 handle_self_privmsg(struct irc_conn *bot, char *user, char *text)
{ {
void (*handler)(); void (*handler)();
int i;
char *cmd, *arg, *modpath; char *cmd, *arg, *modpath;
cmd = text; cmd = text;
arg = skip(cmd, ' '); arg = skip(cmd, ' ');
modpath = (char *)malloc(sizeof(char)*500); 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]; handler = privmsg_self.handlers[i];
((void(*)())handler)(bot, user, text); ((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("LOADMOD", cmd))
{ {
if (strcmp(bot->admin, user)) if (strcmp(bot->admin, user))

View File

@ -13,12 +13,6 @@
#include "events.h" #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() int main()
{ {
fd_set rd; fd_set rd;

View File

@ -5,14 +5,22 @@
#include <stdlib.h> #include <stdlib.h>
#include <dlfcn.h> #include <dlfcn.h>
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 load_module(struct irc_conn *bot, char *where, int stype, char *file)
{ {
void *handle; 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); 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) if (!handle)
{ {
sprintf(error, "Error: %s", dlerror()); 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); dlclose(handle);