Fixed the handler/module system
This commit is contained in:
parent
56f6159e19
commit
a718957b7f
@ -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
|
BIN
mods/test.so
BIN
mods/test.so
Binary file not shown.
@ -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
|
||||
CC=gcc
|
||||
CFLAGS=-fPIC -I../../lib
|
||||
OBJ=../test.so
|
||||
|
||||
main:
|
||||
$(CC) -shared -o $(OBJ) $(CFLAGS) ./test.c
|
||||
@echo "All Done!"
|
@ -1,17 +1,14 @@
|
||||
#include "irc.h"
|
||||
#include "events.h"
|
||||
#include "module.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
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");
|
||||
}
|
21
src/events.c
21
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))
|
||||
|
@ -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;
|
||||
|
14
src/module.c
14
src/module.c
@ -5,14 +5,22 @@
|
||||
#include <stdlib.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 *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);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user