1 /* Copyright (C) 2009-2012, Martin Johansson <martin@fatbob.nu>
2 Copyright (C) 2005-2012, Thorvald Natvig <thorvald@natvig.com>
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions
10 - Redistributions of source code must retain the above copyright notice,
11 this list of conditions and the following disclaimer.
12 - Redistributions in binary form must reproduce the above copyright notice,
13 this list of conditions and the following disclaimer in the documentation
14 and/or other materials provided with the distribution.
15 - Neither the name of the Developers nor the names of its contributors may
16 be used to endorse or promote products derived from this software without
17 specific prior written permission.
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
23 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 #include <libconfig.h>
41 static config_t configuration;
43 #define DEFAULT_WELCOME "Welcome to uMurmur!"
44 #define DEFAULT_MAX_CLIENTS 10
45 #define DEFAULT_MAX_BANDWIDTH 48000
46 #define DEFAULT_BINDPORT 64738
47 #define DEFAULT_BAN_LENGTH (60*60)
48 #define DEFAULT_OPUS_THRESHOLD 100
50 const char defaultconfig[] = DEFAULT_CONFIG;
52 void Conf_init(const char *conffile)
54 config_init(&configuration);
56 conffile = defaultconfig;
57 if (config_read_file(&configuration, conffile) != CONFIG_TRUE) {
58 Log_fatal("Error in config file %s line %d: %s", conffile,
59 config_error_line(&configuration), config_error_text(&configuration));
63 bool_t Conf_ok(const char *conffile)
66 config_init(&configuration);
68 conffile = defaultconfig;
69 if (config_read_file(&configuration, conffile) != CONFIG_TRUE) {
70 fprintf(stderr, "Error in config file %s line %d: %s\n", conffile,
71 config_error_line(&configuration), config_error_text(&configuration));
74 config_destroy(&configuration);
80 config_destroy(&configuration);
83 const char *getStrConf(param_t param)
85 config_setting_t *setting = NULL;
86 const char *strsetting = NULL;
90 setting = config_lookup(&configuration, "certificate");
92 return "/etc/umurmur/certificate.crt";
94 if ((strsetting = config_setting_get_string(setting)) != NULL)
97 return "/etc/umurmur/certificate.crt";
101 setting = config_lookup(&configuration, "private_key");
103 return "/etc/umurmur/private_key.key";
105 if ((strsetting = config_setting_get_string(setting)) != NULL)
108 return "/etc/umurmur/private_key.key";
112 setting = config_lookup(&configuration, "password");
116 if ((strsetting = config_setting_get_string(setting)) != NULL)
122 case ADMIN_PASSPHRASE:
123 setting = config_lookup(&configuration, "admin_password");
127 if ((strsetting = config_setting_get_string(setting)) != NULL)
134 setting = config_lookup(&configuration, "bindaddr");
138 if ((strsetting = config_setting_get_string(setting)) != NULL)
145 setting = config_lookup(&configuration, "welcometext");
147 return DEFAULT_WELCOME;
149 if ((strsetting = config_setting_get_string(setting)) != NULL)
152 return DEFAULT_WELCOME;
155 case DEFAULT_CHANNEL:
156 setting = config_lookup(&configuration, "default_channel");
160 if ((strsetting = config_setting_get_string(setting)) != NULL)
167 setting = config_lookup(&configuration, "username");
171 if ((strsetting = config_setting_get_string(setting)) != NULL)
178 setting = config_lookup(&configuration, "groupname");
182 if ((strsetting = config_setting_get_string(setting)) != NULL)
189 setting = config_lookup(&configuration, "logfile");
193 if ((strsetting = config_setting_get_string(setting)) != NULL)
200 setting = config_lookup(&configuration, "banfile");
204 if ((strsetting = config_setting_get_string(setting)) != NULL)
217 int getIntConf(param_t param)
219 config_setting_t *setting = NULL;
223 setting = config_lookup(&configuration, "bindport");
225 return DEFAULT_BINDPORT;
227 return config_setting_get_int(setting);
231 setting = config_lookup(&configuration, "ban_length");
233 return DEFAULT_BAN_LENGTH;
235 return config_setting_get_int(setting);
239 setting = config_lookup(&configuration, "max_bandwidth");
241 return DEFAULT_MAX_BANDWIDTH;
243 return config_setting_get_int(setting);
247 setting = config_lookup(&configuration, "max_users");
249 return DEFAULT_MAX_CLIENTS;
251 return config_setting_get_int(setting);
255 setting = config_lookup(&configuration, "opus_threshold");
257 return DEFAULT_OPUS_THRESHOLD;
259 return config_setting_get_int(setting);
267 bool_t getBoolConf(param_t param)
269 config_setting_t *setting = NULL;
272 case ALLOW_TEXTMESSAGE:
273 setting = config_lookup(&configuration, "allow_textmessage");
277 return config_setting_get_bool(setting);
280 setting = config_lookup(&configuration, "enable_ban");
284 return config_setting_get_bool(setting);
287 setting = config_lookup(&configuration, "sync_banfile");
291 return config_setting_get_bool(setting);
298 int Conf_getNextChannel(conf_channel_t *chdesc, int index)
300 config_setting_t *setting = NULL;
301 int maxconfig = 64, ret = 0;
302 char configstr[maxconfig];
304 ret = snprintf(configstr, maxconfig, "channels.[%d].name", index);
305 setting = config_lookup(&configuration, configstr);
306 if (ret >= maxconfig || ret < 0 || setting == NULL)
307 return -1; /* Required */
308 chdesc->name = config_setting_get_string(setting);
310 ret = snprintf(configstr, maxconfig, "channels.[%d].parent", index);
311 setting = config_lookup(&configuration, configstr);
312 if (ret >= maxconfig || ret < 0 || setting == NULL)
313 return -1; /* Required */
314 chdesc->parent = config_setting_get_string(setting);
316 ret = snprintf(configstr, maxconfig, "channels.[%d].description", index);
317 setting = config_lookup(&configuration, configstr);
318 if (ret >= maxconfig || ret < 0 || setting == NULL) /* Optional */
319 chdesc->description = NULL;
321 chdesc->description = config_setting_get_string(setting);
323 ret = snprintf(configstr, maxconfig, "channels.[%d].password", index);
324 setting = config_lookup(&configuration, configstr);
325 if (ret >= maxconfig || ret < 0 || setting == NULL) /* Optional */
326 chdesc->password = NULL;
328 chdesc->password = config_setting_get_string(setting);
330 ret = snprintf(configstr, maxconfig, "channels.[%d].noenter", index);
331 setting = config_lookup(&configuration, configstr);
332 if (ret >= maxconfig || ret < 0 || setting == NULL) /* Optional */
333 chdesc->noenter = false;
335 chdesc->noenter = config_setting_get_bool(setting);
340 int Conf_getNextChannelLink(conf_channel_link_t *chlink, int index)
342 config_setting_t *setting = NULL;
343 int maxconfig = 64, ret = 0;
344 char configstr[maxconfig];
346 ret = snprintf(configstr, maxconfig, "channel_links.[%d].source", index);
347 setting = config_lookup(&configuration, configstr);
348 if (ret >= maxconfig || ret < 0 || setting == NULL)
350 chlink->source = config_setting_get_string(setting);
352 ret = snprintf(configstr, maxconfig, "channel_links.[%d].destination", index);
353 setting = config_lookup(&configuration, configstr);
354 if (ret >= maxconfig || ret < 0 || setting == NULL)
356 chlink->destination = config_setting_get_string(setting);