1 /* Copyright (C) 2009-2010, Martin Johansson <martin@fatbob.nu>
2 Copyright (C) 2005-2010, 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_CONFIG "/etc/umurmur.conf"
44 #define DEFAULT_WELCOME "Welcome to uMurmur!"
45 #define DEFAULT_MAX_CLIENTS 10
46 #define DEFAULT_MAX_BANDWIDTH 48000
47 #define DEFAULT_BINDPORT 64738
49 const char defaultconfig[] = DEFAULT_CONFIG;
51 void Conf_init(const char *conffile)
53 config_init(&configuration);
55 conffile = defaultconfig;
56 if (config_read_file(&configuration, conffile) != CONFIG_TRUE) {
57 Log_fatal("Error in config file %s line %d: %s", conffile,
58 config_error_line(&configuration), config_error_text(&configuration));
64 config_destroy(&configuration);
67 const char *getStrConf(param_t param)
69 config_setting_t *setting = NULL;
70 const char *strsetting = NULL;
74 setting = config_lookup(&configuration, "certificate");
76 return "/etc/umurmur/certificate.crt";
78 if ((strsetting = config_setting_get_string(setting)) != NULL)
81 return "/etc/umurmur/certificate.crt";
85 setting = config_lookup(&configuration, "private_key");
87 return "/etc/umurmur/private_key.key";
89 if ((strsetting = config_setting_get_string(setting)) != NULL)
92 return "/etc/umurmur/private_key.key";
96 setting = config_lookup(&configuration, "password");
100 if ((strsetting = config_setting_get_string(setting)) != NULL)
107 setting = config_lookup(&configuration, "bindaddr");
111 if ((strsetting = config_setting_get_string(setting)) != NULL)
118 setting = config_lookup(&configuration, "welcometext");
120 return DEFAULT_WELCOME;
122 if ((strsetting = config_setting_get_string(setting)) != NULL)
125 return DEFAULT_WELCOME;
128 case DEFAULT_CHANNEL:
129 setting = config_lookup(&configuration, "default_channel");
133 if ((strsetting = config_setting_get_string(setting)) != NULL)
146 int getIntConf(param_t param)
148 config_setting_t *setting = NULL;
152 setting = config_lookup(&configuration, "bindport");
154 return DEFAULT_BINDPORT;
156 return config_setting_get_int(setting);
160 setting = config_lookup(&configuration, "max_bandwidth");
162 return DEFAULT_MAX_BANDWIDTH;
164 return config_setting_get_int(setting);
168 setting = config_lookup(&configuration, "max_users");
170 return DEFAULT_MAX_CLIENTS;
172 return config_setting_get_int(setting);
180 int Conf_getNextChannel(conf_channel_t *chdesc, int index)
182 config_setting_t *setting = NULL;
183 int maxconfig = 64, ret = 0;
184 char configstr[maxconfig];
186 ret = snprintf(configstr, maxconfig, "channels.[%d].name", index);
187 setting = config_lookup(&configuration, configstr);
188 if (ret >= maxconfig || ret < 0 || setting == NULL)
189 return -1; /* Required */
190 chdesc->name = config_setting_get_string(setting);
192 ret = snprintf(configstr, maxconfig, "channels.[%d].parent", index);
193 setting = config_lookup(&configuration, configstr);
194 if (ret >= maxconfig || ret < 0 || setting == NULL)
195 return -1; /* Required */
196 chdesc->parent = config_setting_get_string(setting);
198 ret = snprintf(configstr, maxconfig, "channels.[%d].description", index);
199 setting = config_lookup(&configuration, configstr);
200 if (ret >= maxconfig || ret < 0 || setting == NULL) /* Optional */
201 chdesc->description = NULL;
203 chdesc->description = config_setting_get_string(setting);
205 ret = snprintf(configstr, maxconfig, "channels.[%d].noenter", index);
206 setting = config_lookup(&configuration, configstr);
207 if (ret >= maxconfig || ret < 0 || setting == NULL) /* Optional */
208 chdesc->noenter = false;
210 chdesc->noenter = config_setting_get_bool(setting);
215 int Conf_getNextChannelLink(conf_channel_link_t *chlink, int index)
217 config_setting_t *setting = NULL;
218 int maxconfig = 64, ret = 0;
219 char configstr[maxconfig];
221 ret = snprintf(configstr, maxconfig, "channel_links.[%d].source", index);
222 setting = config_lookup(&configuration, configstr);
223 if (ret >= maxconfig || ret < 0 || setting == NULL)
225 chlink->source = config_setting_get_string(setting);
227 ret = snprintf(configstr, maxconfig, "channel_links.[%d].destination", index);
228 setting = config_lookup(&configuration, configstr);
229 if (ret >= maxconfig || ret < 0 || setting == NULL)
231 chlink->destination = config_setting_get_string(setting);