summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile4
-rw-r--r--README6
-rw-r--r--agni.c92
-rw-r--r--config/servers.ini2
-rw-r--r--config_all_cmds.h3
-rw-r--r--config_cmds.h1
-rw-r--r--config_servers.c26
-rw-r--r--config_servers.h20
-rw-r--r--version.h4
9 files changed, 128 insertions, 30 deletions
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;i<SIZEOF_SERVER_LIST;i++)
+ cnt_servers = config_server_num(config);
+ if (cnt_servers == 0)
{
- printf("Load config for server %s\n",server_list[i].server);
+ printf("No configured servers to load\n");
+ return 0;
}
cfg_list = malloc(sizeof(server_cfg)*cnt_servers);
@@ -859,31 +898,50 @@ int main(int argc, char **argv)
{
ENL();
}
+ memset(cfg_list, 0, sizeof(server_cfg)*cnt_servers);
+
mq_array = malloc(sizeof(mq_ntf_mdt)*cnt_servers);
if (mq_array == NULL)
{
ENL();
}
+
/* For each configuration create listener */
- for (i=0;i<SIZEOF_SERVER_LIST;i++)
+ for (i=0;i<cnt_servers;i++)
{
/*initialise server config*/
- server_cfg *srvc = cfg_list+i;
- irc_server_conf *isrvc = &server_list[i];
+ server_cfg *srvc = &cfg_list[i];
+ //irc_server_conf *isrvc = &server_list[i];
+ irc_server_conf isrvc;
+
+ memset(&isrvc, 0, sizeof(irc_server_conf));
+ config_get_irc_config(config, i, &isrvc); //well there is no free structure so you gona get memleaks here =P in isrvc
+
srvc->tid = 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;j<SERV_CHAN_MAX_NUM;j++)
+ {
+ if (isrvc.channels[j])
+ {
+ srvc->channels[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 <stdio.h>
+#include <stdlib.h>
#include <stdint.h>
-
+#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