added autoload and created config.c

This commit is contained in:
Aaron Blakely 2015-04-07 13:18:24 -05:00
parent 1e54da29f3
commit 0f4468a4c2
10 changed files with 104 additions and 53 deletions

View File

@ -11,6 +11,7 @@ EXEC=xbot
main:
@rm -rf build
@mkdir build
$(CC) $(CFLAGS) $(SRC)/config.c -o $(OBJ)/config.o
$(CC) $(CFLAGS) $(SRC)/main.c -o $(OBJ)/main.o
$(CC) $(CFLAGS) $(SRC)/irc.c -o $(OBJ)/irc.o
$(CC) $(CFLAGS) $(SRC)/util.c -o $(OBJ)/util.o

8
lib/config.h Normal file
View File

@ -0,0 +1,8 @@
#ifndef CONFIG_H
#define CONFIG_H
#include "irc.h"
struct irc_conn read_config(struct irc_conn bot, char *file);
#endif

View File

@ -15,9 +15,9 @@ struct irc_conn
char nick[32];
char *admin;
char *host;
char *port;
char *real_name;
char host[256];
char port[5];
char real_name[512];
// I/O Buffers
char out[4096];

View File

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

Binary file not shown.

View File

@ -17,7 +17,7 @@ void hello(struct irc_conn *bot, char *user, char *chan, char *text)
free(buf);
}
void mod_init(void *handle, void (*export)())
void mod_init()
{
export(handle, PRIVMSG_CHAN, "hello");
add_handler(PRIVMSG_CHAN, hello);
}

57
src/config.c Normal file
View File

@ -0,0 +1,57 @@
#include <stdio.h>
#include <stdlib.h>
#include <libconfig.h>
#include "irc.h"
#include "util.h"
#include "module.h"
struct irc_conn read_config(struct irc_conn bot, char *file)
{
int count, n;
config_t cfg, *cf;
const config_setting_t *autoload;
const char *base = (const char*)malloc(sizeof(char *) * 1024);
const char *mod = NULL;
char *modpath = (char *)malloc(sizeof(char *) * 500);
cf = &cfg;
config_init(cf);
if (!config_read_file(cf, file))
{
printf("[xbot.cfg:%d] Configuration error: %s\n",
config_error_line(cf),
config_error_text(cf)
);
config_destroy(cf);
exit(-1);
}
if (config_lookup_string(cf, "bot.nick", &base))
strlcpy(bot.nick, base, sizeof bot.nick);
if (config_lookup_string(cf, "server.host", &base))
strlcpy(bot.host, base, sizeof bot.host);
if (config_lookup_string(cf, "server.port", &base))
strlcpy(bot.port, base, sizeof bot.port);
if (config_lookup_string(cf, "bot.admin", &base))
bot.admin = (char *)base;
autoload = config_lookup(cf, "mods.autoload");
count = config_setting_length(autoload);
for (n = 0; n < count; n++)
{
mod = config_setting_get_string_elem(autoload, n);
sprintf(modpath, "./mods/%s.so", mod);
load_module(&bot, "main", 3, modpath);
}
config_destroy(cf);
return bot;
}

View File

@ -6,58 +6,30 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/select.h>
#include <libconfig.h>
#include "config.h"
#include "irc.h"
#include "util.h"
#include "events.h"
#include "module.h"
int main()
{
fd_set rd;
config_t cfg, *cf;
const config_setting_t *retries;
const char *base = NULL;
struct irc_conn bot;
init_events();
// Init the config parser
cf = &cfg;
config_init(cf);
if (!config_read_file(cf, "xbot.cfg"))
{
printf("xbot.cfg:%d - %s\n",
config_error_line(cf),
config_error_text(cf));
config_destroy(cf);
return -1;
}
// Fill our bot struct with values from the config
if (config_lookup_string(cf, "bot.nick", &base))
strlcpy(bot.nick, base, sizeof bot.nick);
if (config_lookup_string(cf, "server.host", &base))
bot.host = (char *)base;
if (config_lookup_string(cf, "server.port", &base))
bot.port = (char *)base;
if (config_lookup_string(cf, "bot.admin", &base))
bot.admin = (char *)base;
// Read the config
bot = read_config(bot, "xbot.cfg");
// Connect to the server
printf("Connecting to %s...\n", bot.host);
irc_connect(&bot);
irc_auth(&bot);
// Free the config before entering the main loop
config_destroy(cf);
for (;;)
{

View File

@ -5,18 +5,10 @@
#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)(void *handle, void (*export)());
void (*mod_init)();
char *error = (char *)malloc(sizeof(char *)*1024);
@ -25,7 +17,12 @@ void load_module(struct irc_conn *bot, char *where, int stype, char *file)
{
sprintf(error, "Error: %s", dlerror());
if (stype == PRIVMSG_CHAN)
if (stype == 3)
{
eprint("%s\n", error);
return;
}
else if (stype == PRIVMSG_CHAN)
{
irc_privmsg(bot, where, error);
}
@ -45,7 +42,12 @@ void load_module(struct irc_conn *bot, char *where, int stype, char *file)
{
//sprintf(error, "Error: %s", error);
eprint("Error: %s\n", error);
if (stype == PRIVMSG_CHAN)
if (stype == 3)
{
return;
}
else if (stype == PRIVMSG_CHAN)
{
irc_privmsg(bot, where, error);
}
@ -55,11 +57,17 @@ void load_module(struct irc_conn *bot, char *where, int stype, char *file)
}
}
(*mod_init)(handle, export_handler);
(*mod_init)();
dlclose(handle);
irc_privmsg(bot, where, "Module '%s' loaded.", file);
if (stype != 3)
{
irc_privmsg(bot, where, "Module '%s' loaded.", file);
}
else
{
printf("Module '%s' loaded.\n", file);
}
free(error);
}

View File

@ -11,4 +11,10 @@ server:
{
host = "irc.alphachat.net";
port = "6667";
};
mods:
{
autoload = ("test");
blacklist = ();
};