From 1ff5fed390400fb1742b991ffaa7ec26dc28d24c Mon Sep 17 00:00:00 2001 From: FreeArtMan Date: Thu, 6 Jul 2017 14:30:28 +0100 Subject: Support for config files, ofc we use ini --- Makefile | 4 +-- README | 6 ++++ agni.c | 92 ++++++++++++++++++++++++++++++++++++++++++++---------- config/servers.ini | 2 +- config_all_cmds.h | 3 +- config_cmds.h | 1 + config_servers.c | 26 +++++++++++++++ config_servers.h | 20 +++++++----- version.h | 4 +-- 9 files changed, 128 insertions(+), 30 deletions(-) create mode 100644 config_servers.c diff --git a/Makefile b/Makefile index 5e8b052..56687e7 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ PROJECT=agni CC=gcc -CFLAGS= +CFLAGS=-g3 SOURCES=$(wildcard *.c) OBJECTS=$(SOURCES:.c=.o) LDFLAGS=-lrt -lm @@ -57,7 +57,7 @@ createextlib: $(EXTLIB_OBJECTS) #asd leak: - valgrind --leak-check=full --track-origins=yes --log-file=log.txt ./$(PROJECT) + valgrind --leak-check=full --track-origins=yes --log-file=log.txt ./$(PROJECT) -c config/test.ini clean: rm -f agni *.o cmd/*.o \ No newline at end of file diff --git a/README b/README index 0c3d728..8b4c56f 100644 --- a/README +++ b/README @@ -9,6 +9,12 @@ No external dependencies except libc. * Multi-server * Multi-channel * IRC protocol, non-SSL yet +* Support INI config files + +#Run + +./agni +./agni -c [CONFIG_FILENAME] #Compile diff --git a/agni.c b/agni.c index a5b0014..1ff1c1a 100644 --- a/agni.c +++ b/agni.c @@ -20,6 +20,7 @@ #include "config_servers.h" #include "config_cmds.h" +#include "config_load.h" #include "darray.h" #include "buf.h" @@ -31,11 +32,38 @@ #include "irc_parse.h" #include "mmm.h" +#include "arg.h" + /* no defence programming, no error checking, no argument checking just PoC nothing else */ +/* +Define command line arguments +*/ + +s_arg_val arg_config_filename = +{ + .ptr = "config/servers.ini", + .default_ptr = "config/servers.ini", + .result = NULL +}; + +def_arg agni_cmd_args[] = +{ + ARG_ENTRY("-c",VAL,&arg_config_filename,"ini config file"), + {NULL,0,NULL} +}; + +arg_t *cfg = NULL; + +/* +configuration variables +*/ + +config_ini *config=NULL; + #define MQ_MSG_SIZE 8192 //HACK @@ -138,7 +166,7 @@ typedef struct server_cfg char *user; char *password; char *server; //should be changed to hostname? - char **channels; + char *channels[SERV_CHAN_MAX_NUM]; char *port; int ssl; } server_cfg; @@ -469,6 +497,7 @@ int th_start_client(void *data) while (cfg->channels[iter]!=NULL) { chan_name = cfg->channels[iter]; + printf("%s\n",chan_name); if ((strncmp(chan_name,sep_chan,strlen(chan_name))==0) && (strlen(sep_chan)==strlen(chan_name))) { @@ -641,6 +670,8 @@ int th_event_manager(void *data) PERM(); } + + //load predefined/precompiled command list { single_cmd_def *single_cmd = confgi_cmd_list; @@ -837,7 +868,7 @@ as a watch dog. *******************************************************************************/ int main(int argc, char **argv) { - int i; + int i,j; int cnt_servers,cnt_running; server_cfg *cfg_list; @@ -847,11 +878,19 @@ int main(int argc, char **argv) /*set atomic variables to init value*/ atomic_store(&_glbl_id, 0); + + /*Load configs*/ + cfg = arg_load(argc, argv, agni_cmd_args); + + config_load_ini(arg_config_filename.result->ptr, &config); + printf("%s\n",arg_config_filename.result->ptr); + /* Load configuration */ - cnt_servers = SIZEOF_SERVER_LIST; - for (i=0;itid = i; srvc->stack = malloc(STACK_SIZE); //NULL will brake everything ;) if (srvc->stack == NULL) { ERROR("BLow"); } - srvc->user = isrvc->user; - srvc->password = isrvc->password; - srvc->server = isrvc->server; - srvc->channels = isrvc->channels; - srvc->port = isrvc->port; - srvc->ssl = isrvc->ssl; + srvc->user = alloc_new_str(isrvc.user); + //srvc->password = alloc_new_str(isrvc.password); + srvc->server = alloc_new_str(isrvc.server); + srvc->port = alloc_new_str(isrvc.port); + srvc->ssl = isrvc.ssl; + //srvc->channels = isrvc.channels; + for (j=0;jchannels[j] = alloc_new_str(isrvc.channels[j]); + } else + { + srvc->channels[j] = NULL; + } + } + free_irc_server_conf(&isrvc); //atomic_init( &srvc->running, 1); atomic_store(&srvc->running, 0); @@ -904,11 +962,11 @@ int main(int argc, char **argv) } /* event handler thread */ - evhnd_cfg = malloc(sizeof(event_handler_cfg)); + evhnd_cfg = malloc(sizeof(event_handler_cfg)); memset(evhnd_cfg, 0, sizeof(event_handler_cfg)); - evhnd_cfg->stack = malloc(EVENT_HND_STACK_SIZE); + evhnd_cfg->stack = malloc(EVENT_HND_STACK_SIZE); atomic_store(&evhnd_cfg->running, 0); - evhnd_cfg->mq_num = cnt_servers; + evhnd_cfg->mq_num = cnt_servers; evhnd_cfg->mq_listen = mq_array; clone(th_event_manager, evhnd_cfg->stack+EVENT_HND_STACK_SIZE, CLONE_VM|CLONE_FILES, (void *)evhnd_cfg); diff --git a/config/servers.ini b/config/servers.ini index c7be510..d5cf034 100644 --- a/config/servers.ini +++ b/config/servers.ini @@ -1,4 +1,4 @@ -[irc.freenode] +[irc.freenode.net] user=cbot_git server=irc.freenode.net port=6667 diff --git a/config_all_cmds.h b/config_all_cmds.h index 8847d7d..c2cdabf 100644 --- a/config_all_cmds.h +++ b/config_all_cmds.h @@ -10,9 +10,10 @@ #include "cmd/cmd_loadavg.h" #include "cmd/cmd_uptime.h" #include "cmd/cmd_rand_libc.h" -#include "cmd/cmd_botu.h" #include "cmd/cmd_ping.h" +#include "cmd/cmd_botu.h" #include "cmd/cmd_fir.h" #include "cmd/cmd_sum.h" +#include "cmd/cmd_help.h" #include "cmd/cmd_date.h" #endif diff --git a/config_cmds.h b/config_cmds.h index 9adfe7a..76c721a 100644 --- a/config_cmds.h +++ b/config_cmds.h @@ -29,6 +29,7 @@ single_cmd_def confgi_cmd_list[] = {"RAND",cmd_rand}, {"RANDC",cmd_rand_libc}, {"FIR",cmd_fir}, + {"HELP",cmd_help}, {NULL,NULL} }; diff --git a/config_servers.c b/config_servers.c new file mode 100644 index 0000000..5177a72 --- /dev/null +++ b/config_servers.c @@ -0,0 +1,26 @@ +#include "config_servers.h" + +void free_irc_server_conf(irc_server_conf *isc) +{ + int i; + + if (isc == NULL) + return; + + if (isc->name) + free(isc->name); + if (isc->user) + free(isc->user); + if (isc->server) + free(isc->server); + if (isc->port) + free(isc->port); + + for (i=0;i<16;i++) //em 16 is hardcoded mate + { + if (isc->channels[i]) + free(isc->channels[i]); + } + //free(isc); + +} \ No newline at end of file diff --git a/config_servers.h b/config_servers.h index 30a1105..005b274 100644 --- a/config_servers.h +++ b/config_servers.h @@ -1,40 +1,45 @@ #ifndef __CONFIG_SERVERS_H #define __CONFIG_SERVERS_H +#include +#include #include - +#define SERV_CHAN_MAX_NUM 16 typedef struct irc_server_conf { + char *name; char *user; char *password; char *server; char *port; - int ssl; - char *channels[16]; + int ssl; + int enabled; + char *channels[SERV_CHAN_MAX_NUM]; } irc_server_conf; +void free_irc_server_conf(irc_server_conf *isc); + static irc_server_conf server_list[] = { /* { - .user = "cbot_git2",#meeseekeria + .user = "cbot_git", .password = "asdsada", .server = "irc.freenode.net", .channels = {"#mainlv","#developerslv","#meeseekeria",NULL}, .port = "6667", .ssl = 0, }, - */ - /* + + { .user = "cbot_git", .password = "asdaasda", .server = "localhost", .channels = {"#default","#bots",NULL}, .port = "9000", - //.port = "6667", .ssl = 0 }, */ @@ -47,6 +52,7 @@ static irc_server_conf server_list[] = .port = "6667", .ssl = 0 } + }; #define SIZEOF_SERVER_LIST (sizeof(server_list)/sizeof(irc_server_conf)) diff --git a/version.h b/version.h index 9379667..d1961f2 100644 --- a/version.h +++ b/version.h @@ -1,5 +1,5 @@ #ifndef __VERSION_H #define __VERSION_H -#define VERSION_DATE "Sun May 7 18:06:02 BST 2017" -#define VERSION_COMMIT "910c194 Forgot add Makefile changes" +#define VERSION_DATE "Thu Jul 6 14:22:25 BST 2017" +#define VERSION_COMMIT "803d146 Added config loading library" #endif -- cgit v1.2.3