-
Notifications
You must be signed in to change notification settings - Fork 0
/
settings.c
204 lines (172 loc) · 4.16 KB
/
settings.c
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/******************************************************************************
* eiwic - Extensible Ircbot Written In C
* Copyright (C) Hannes Gräuler <[email protected]>
*
* settings.c: Implementation of functions to load and validate the config
* file.
****************************************************************************/
#include "eiwic.h"
#include "settings.h"
#include "plugins.h"
#include "servers.h"
static int conf_set(char *parameter, char *value);
static int conf_add(char *parameter, char *value);
static int conf_echo(char *parameter, char *value);
static int conf_load(char *parameter, char *value);
static int conf_load_module(char *filename);
int conf_loadfile()
{
FILE* conf;
char buf[512], *action, param[128];
int line = 0, success;
if (!(conf = fopen(set_get_string("config_file"), "r")))
{
log(LOG_ERROR, "Unable to open config file \"%s\" for reading",
set_get_string("config_file"));
return -1;
}
while (fgets(buf, sizeof(buf), conf) > 0 && ++line)
{
if (!*buf || *buf=='#')
continue;
action = strtok(buf, " \t\r\n");
if (action != NULL)
{
if (strcasecmp(action, "set") == 0)
{
sprintf(param, "%s", strtok(NULL, " =\r\n"));
success = conf_set(param, strtok(NULL, "\r\n"));
}
else if (strcasecmp(action, "add") == 0)
{
sprintf(param, "%s", strtok(NULL, " \r\n"));
success = conf_add(param, strtok(NULL, "\r\n"));
}
else if (strcasecmp(action, "echo") == 0)
{
success = conf_echo(strtok(NULL, "\r\n"), NULL);
}
else if (strcasecmp(action, "load") == 0)
{
sprintf(param, "%s", strtok(NULL, " =\r\n"));
success = conf_load(param, strtok(NULL, "\r\n"));
}
else
{
log(LOG_ERROR, "configuration file: Unknown command \"%s\"",
action);
success = -1;
}
if (success == -1)
{
log(LOG_WARNING, "Loading of configuration file failed "
"(error in %s, line %i)", set_get_string("config_file"),
line);
fclose(conf);
return -1;
}
}
}
fclose(conf);
return 0;
}
int conf_set(char *parameter, char *value)
{
SETTING *s;
if ((s = set_find(parameter)) == NULL)
{ /* can only set existing settings */
log(LOG_WARNING, "No such configuration symbol: %s", parameter);
log(LOG_WARNING, "Assuming that %s is a setting of type STRING",parameter);
set_add(SET_STRING, parameter, "");
s = set_find(parameter);
}
switch (s->type)
{
case SET_BOOL:
set_set(s, (strcmp(value, "yes") == 0 && atoi(value) == 1)?1:0);
break;
case SET_FLOAT:
set_set(s, atof(value));
break;
case SET_INT:
set_set(s, atol(value));
break;
case SET_STRING:
case SET_LONGSTRING:
set_set(s, value);
break;
}
return 0;
}
int conf_echo(char *parameter, char *value)
{
log(LOG_STATUS, "config: %s\n", parameter);
return 0;
}
int conf_add(char *parameter, char *value)
{
if (strcasecmp(parameter, "channel") == 0)
{
channel_add(value);
}
else
if (strcasecmp(parameter, "server") == 0)
{
char host[128], port[6], *p, ipv6=0;
if ((p = strtok(value, " \r\n")) == NULL)
return 0;
strncpy(host, p, sizeof(host));
if ((p = strtok(NULL, " \r\n")) == NULL)
return 0;
strncpy(port, p, sizeof(port));
p = strtok(NULL, " \r\n");
if (p)
if (strcasecmp(p, "ipv6") == 0)
ipv6 = 1;
#ifndef IPV6
if (ipv6 == 1)
{
log(LOG_WARNING, "Unable to add IPv6 server '%s'! "
"You don't have IPv6 support enabled.", host);
return 0;
}
#endif
server_add(host, (u_short)atoi(port), ipv6);
}
else
{
log(LOG_ERROR, "add: Unknown parameter \"%s\"", parameter);
return -1;
}
return 0;
}
int conf_load(char *parameter, char *value)
{
if (strcasecmp(parameter, "module") == 0)
{
/*char fn[1024];
sprintf(fn, "%s%s", set_get_string("module_path"), value);
*/
log(LOG_INIT_DEBUG, "Loading module %s...", value);
return conf_load_module(value);
}
else
{
log(LOG_ERROR, "load: Unknown parameter \"%s\"", parameter);
return -1;
}
}
int conf_load_module(char *filename)
{
struct eiwic_plugin *ep;
ep = plug_load(filename, e_global->log_output);
if (ep != NULL)
{
return 0;
}
else
{
log(LOG_ERROR, "Loading of module %s failed: %s", filename, dlerror());
return -1;
}
}