Started on module interface. Currently producing segfaults.

This commit is contained in:
Aaron Blakely 2015-03-27 10:44:40 -05:00
parent dbd669011a
commit ae4d758aea
15 changed files with 129 additions and 8 deletions

View File

@ -1,6 +1,6 @@
CC=gcc CC=gcc
CFLAGS=-g -std=gnu99 -c -lconfig -I./lib CFLAGS=-g -std=gnu99 -c -lconfig -ldl -I./lib
BINFLAGS=-g -lconfig BINFLAGS=-g -rdynamic -ldl -lconfig
SRC=./src SRC=./src
OBJ=./build OBJ=./build
OBJECTS=$(OBJ)/*.o OBJECTS=$(OBJ)/*.o
@ -13,6 +13,7 @@ main:
$(CC) $(CFLAGS) $(SRC)/irc.c -o $(OBJ)/irc.o $(CC) $(CFLAGS) $(SRC)/irc.c -o $(OBJ)/irc.o
$(CC) $(CFLAGS) $(SRC)/util.c -o $(OBJ)/util.o $(CC) $(CFLAGS) $(SRC)/util.c -o $(OBJ)/util.o
$(CC) $(CFLAGS) $(SRC)/events.c -o $(OBJ)/events.o $(CC) $(CFLAGS) $(SRC)/events.c -o $(OBJ)/events.o
$(CC) $(CFLAGS) $(SRC)/module.c -o $(OBJ)/module.o
$(CC) -o $(EXEC) $(OBJECTS) $(BINFLAGS) $(CC) -o $(EXEC) $(OBJECTS) $(BINFLAGS)
@echo "All Done!" @echo "All Done!"

8
exported.txt Normal file
View File

@ -0,0 +1,8 @@
{
extern "C"
{
add_handler;
irc_privmsg;
irc_notice;
};
}

View File

@ -28,6 +28,7 @@ struct irc_conn
void irc_connect(struct irc_conn *bot); void irc_connect(struct irc_conn *bot);
void irc_auth(struct irc_conn *bot); void irc_auth(struct irc_conn *bot);
void irc_notice(struct irc_conn *bot, char *to, char *fmt, ...); void irc_notice(struct irc_conn *bot, char *to, char *fmt, ...);
void irc_privmsg(struct irc_conn *bot, char *to, char *fmt, ...);
void irc_raw(struct irc_conn *bot, char *fmt, ...); void irc_raw(struct irc_conn *bot, char *fmt, ...);
void irc_parse_raw(struct irc_conn *bot, char *raw); void irc_parse_raw(struct irc_conn *bot, char *raw);

View File

@ -0,0 +1,8 @@
#ifndef MODULE_H
#define MODULE_H
#include "irc.h"
void load_module(struct irc_conn *bot, char *where, int stype, char *file);
#endif

BIN
mods/test.so Executable file

Binary file not shown.

4
mods/test/Makefile Normal file
View File

@ -0,0 +1,4 @@
gcc -c -fPIC -I../../lib test.c -o test.o
gcc test.o -shared test.so
mv test.so ..
rm test.o

0
mods/test/build.sh Executable file
View File

26
mods/test/test.c Normal file
View File

@ -0,0 +1,26 @@
#include "irc.h"
#include "events.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);
}
free(buf);
}
void mod_init(struct irc_conn *b, char *where)
{
add_handler(PRIVMSG_CHAN, hello);
}

BIN
mods/test/test.o Normal file

Binary file not shown.

BIN
mods/test/test.so Executable file

Binary file not shown.

View File

@ -1,6 +1,7 @@
#include "irc.h" #include "irc.h"
#include "util.h" #include "util.h"
#include "events.h" #include "events.h"
#include "module.h"
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -29,6 +30,7 @@ void init_events()
void add_handler(int type, void *handler) void add_handler(int type, void *handler)
{ {
printf("Installing handler @ %p [type: %i]\n", handler, type);
int handler_count; int handler_count;
if (type == PRIVMSG_SELF) if (type == PRIVMSG_SELF)
@ -63,10 +65,12 @@ void handle_chan_privmsg(struct irc_conn *bot, char *user, char *chan, char *tex
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)();
char *cmd, *arg; char *cmd, *arg, *modpath;
cmd = text; cmd = text;
arg = skip(cmd, ' '); arg = skip(cmd, ' ');
modpath = (char *)malloc(sizeof(char)*500);
for (int i = 0; i < privmsg_self.count; i++) for (int i = 0; i < privmsg_self.count; i++)
{ {
handler = privmsg_self.handlers[i]; handler = privmsg_self.handlers[i];
@ -90,12 +94,16 @@ void handle_self_privmsg(struct irc_conn *bot, char *user, char *text)
if (strcmp(bot->admin, user)) if (strcmp(bot->admin, user))
{ {
irc_notice(bot, user, "Loading module: mods/%s.so", arg); irc_notice(bot, user, "Loading module: mods/%s.so", arg);
sprintf(modpath, "./mods/%s.so", arg);
load_module(bot, user, PRIVMSG_SELF, modpath);
} }
else else
{ {
irc_notice(bot, user, "You are unauthorized to use this command."); irc_notice(bot, user, "You are unauthorized to use this command.");
} }
} }
free(modpath);
} }
void handle_join(struct irc_conn *bot, char *user, char *chan) void handle_join(struct irc_conn *bot, char *user, char *chan)

View File

@ -75,6 +75,18 @@ void irc_notice(struct irc_conn *bot, char *to, char *fmt, ...)
irc_raw(bot, "NOTICE %s :%s", to, msg_); irc_raw(bot, "NOTICE %s :%s", to, msg_);
} }
void irc_privmsg(struct irc_conn *bot, char *to, char *fmt, ...)
{
char msg_[4096];
va_list ap;
va_start(ap, fmt);
vsnprintf(msg_, sizeof msg_, fmt, ap);
va_end(ap);
irc_raw(bot, "PRIVMSG %s :%s", to, msg_);
}
void irc_raw(struct irc_conn *bot, char *fmt, ...) void irc_raw(struct irc_conn *bot, char *fmt, ...)
{ {
va_list ap; va_list ap;
@ -122,7 +134,7 @@ void irc_parse_raw(struct irc_conn *bot, char *raw)
if (!strcmp("PRIVMSG", raw)) if (!strcmp("PRIVMSG", raw))
{ {
if (strcmp(user, bot->nick)) if (!strcmp(par, bot->nick))
{ {
handle_self_privmsg(bot, user, text); handle_self_privmsg(bot, user, text);
} }

View File

@ -64,8 +64,6 @@ int main()
// Free the config before entering the main loop // Free the config before entering the main loop
config_destroy(cf); config_destroy(cf);
add_handler(PRIVMSG_SELF, hello_cmd);
add_handler(PRIVMSG_SELF, hello_cmd);
for (;;) for (;;)
{ {

View File

@ -0,0 +1,57 @@
#include "irc.h"
#include "util.h"
#include "events.h"
#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>
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)());
char *error = (char *)malloc(sizeof(char *)*1024);
handle = dlopen(file, RTLD_NOW | RTLD_GLOBAL);
if (!handle)
{
sprintf(error, "Error: %s", dlerror());
if (stype == PRIVMSG_CHAN)
{
irc_privmsg(bot, where, error);
}
else
{
irc_notice(bot, where, error);
}
return;
}
dlerror();
*(void **)(&mod_init) = dlsym(handle, "mod_init");
if ((error = dlerror()) != NULL)
{
//sprintf(error, "Error: %s", error);
eprint("Error: %s\n", error);
if (stype == PRIVMSG_CHAN)
{
irc_privmsg(bot, where, error);
}
else
{
irc_notice(bot, where, error);
}
}
(*mod_init)(bot, where);
dlclose(handle);
irc_privmsg(bot, where, "Module '%s' loaded.", file);
free(error);
}

View File

@ -25,8 +25,6 @@ void eprint(char *fmt, ...)
{ {
fprintf(stderr, "%s\n", strerror(errno)); fprintf(stderr, "%s\n", strerror(errno));
} }
exit(1);
} }
void strlcpy(char *to, const char *from, int len) void strlcpy(char *to, const char *from, int len)