summaryrefslogtreecommitdiff
path: root/config_load.c
blob: df29cb7c10e94857bbf361c6b0a3cfbc29c3897a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include "config_load.h"

int config_load_ini(char *ini_fname, config_ini **config)
{
	int i;
	config_ini *res_conf=NULL;

	if (!ini_fname)
	{
		PERM();
		return -1;
	}

	if (!config)
	{
		PERM();
		return -1;
	}
	
	res_conf = calloc(1,sizeof(config_ini));

	res_conf->ini = iniparser_load(ini_fname);
	if (!res_conf->ini)
	{
		ERROR("Cant parse ini file %s\n",ini_fname);
		free(res_conf);
		return -1;
	}

	*config = res_conf;

	return 0;
}


int config_server_num(config_ini *config)
{
	int ret=-1;
	if (!config)
	{
		PERM();
		return -1;
	}

	ret = iniparser_getnsec(config->ini);

	return ret;
}


int config_get_irc_config(config_ini *config, int n, irc_server_conf *server_conf)
{

	int i;
	size_t buf_sz=128;
	char buf[buf_sz];
	char *section_name=NULL;
	dictionary *ini;
	char *ini_str;
	int ini_int;
	int count;
	sds params;
	sds *tokens=NULL;

	if (!config)
	{
		PERM();
		return -1;
	}

	if (!server_conf)
	{
		PERM();
		return -1;
	}

	if (n<0)
	{
		PERM();
		return -1;
	}

	ini = config->ini;
	section_name = (char *)iniparser_getsecname(ini, n);
	if (!section_name)
	{
		PERM();
		return -1;
	}

	//section name
	server_conf->name = (char *)alloc_new_str(section_name);

	//get server name
	snprintf(buf, buf_sz, "%s:server", section_name);
	ini_str = (char *)iniparser_getstring(ini, buf, NULL);
	server_conf->server = alloc_new_str(ini_str);

	//get user name
	snprintf(buf, buf_sz, "%s:user", section_name);
	ini_str = (char *)iniparser_getstring(ini, buf, NULL);
	server_conf->user = alloc_new_str(ini_str);

	//get port number
	snprintf(buf, buf_sz, "%s:port", section_name);
	ini_str = (char *)iniparser_getstring(ini, buf, NULL);
	server_conf->port = (char *)alloc_new_str(ini_str);

	//get enabled
	snprintf(buf, buf_sz, "%s:enabled", section_name);
	ini_int = (int)iniparser_getint(ini, buf, -1);
	server_conf->enabled = ini_int;

	//get ssl
	snprintf(buf, buf_sz, "%s:ssl", section_name);
	ini_int = (int)iniparser_getint(ini, buf, -1);
	server_conf->ssl = ini_int;

	//get channel list
	snprintf(buf, buf_sz, "%s:channels", section_name);
	ini_str = (char *)iniparser_getstring(ini, buf, NULL);
	params = sdsnew(ini_str);
	tokens = sdssplitlen(params,sdslen(params),",",1,&count);
	for (i=0;(i<count)&&(i<15);i++)//woop woop dirty hack, max chan nums is 16 not constanteniuated
	{
		server_conf->channels[i] = alloc_new_str(tokens[i]);
	}
	server_conf->channels[i]=NULL;
	sdsfree(params);
	sdsfreesplitres(tokens,count);

	return 0;
}


void config_free(config_ini *config)
{
	if (!config)
	{
		PERM();
		return;
	}

	iniparser_freedict(config->ini);
	config->ini = NULL;

	free(config);
	config = NULL;

}