Add channel position.
[umurmur.git] / src / conf.c
1 /* Copyright (C) 2009-2013, Martin Johansson <martin@fatbob.nu>
2    Copyright (C) 2005-2013, Thorvald Natvig <thorvald@natvig.com>
3
4    All rights reserved.
5
6    Redistribution and use in source and binary forms, with or without
7    modification, are permitted provided that the following conditions
8    are met:
9
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.
18
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.
30 */
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34
35 #include <libconfig.h>
36
37 #include "types.h"
38 #include "conf.h"
39 #include "log.h"
40
41 static config_t configuration;
42
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
49
50 const char defaultconfig[] = DEFAULT_CONFIG;
51
52 void Conf_init(const char *conffile)
53 {
54         config_init(&configuration);
55         if (conffile == NULL)
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));
60         }
61 }
62
63 bool_t Conf_ok(const char *conffile)
64 {
65         bool_t rc = true;
66         config_init(&configuration);
67         if (conffile == NULL)
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));
72                 rc = false;
73         }
74         config_destroy(&configuration);
75         return rc;
76 }
77
78 void Conf_deinit()
79 {
80         config_destroy(&configuration);
81 }
82
83 const char *getStrConf(param_t param)
84 {
85         config_setting_t *setting = NULL;
86         const char *strsetting = NULL;
87         
88         switch (param) {
89         case CERTIFICATE:
90                 setting = config_lookup(&configuration, "certificate");
91                 if (!setting)
92                         return "/etc/umurmur/certificate.crt";
93                 else {
94                         if ((strsetting = config_setting_get_string(setting)) != NULL)
95                                 return strsetting;
96                         else
97                                 return "/etc/umurmur/certificate.crt";
98                 }
99                 break;
100         case KEY:
101                 setting = config_lookup(&configuration, "private_key");
102                 if (!setting)
103                         return "/etc/umurmur/private_key.key";
104                 else {
105                         if ((strsetting = config_setting_get_string(setting)) != NULL)
106                                 return strsetting;
107                         else
108                                 return "/etc/umurmur/private_key.key";
109                 }
110                 break;
111         case PASSPHRASE:
112                 setting = config_lookup(&configuration, "password");
113                 if (!setting)
114                         return "";
115                 else {
116                         if ((strsetting = config_setting_get_string(setting)) != NULL)
117                                 return strsetting;
118                         else
119                                 return "";
120                 }
121                 break;
122         case ADMIN_PASSPHRASE:
123                 setting = config_lookup(&configuration, "admin_password");
124                 if (!setting)
125                         return "";
126                 else {
127                         if ((strsetting = config_setting_get_string(setting)) != NULL)
128                                 return strsetting;
129                         else
130                                 return "";
131                 }
132                 break;
133         case BINDADDR:
134                 setting = config_lookup(&configuration, "bindaddr");
135                 if (!setting)
136                         return "";
137                 else {
138                         if ((strsetting = config_setting_get_string(setting)) != NULL)
139                                 return strsetting;
140                         else
141                                 return "";
142                 }
143                 break;
144         case WELCOMETEXT:
145                 setting = config_lookup(&configuration, "welcometext");
146                 if (!setting)
147                         return DEFAULT_WELCOME;
148                 else {
149                         if ((strsetting = config_setting_get_string(setting)) != NULL)
150                                 return strsetting;
151                         else
152                         return DEFAULT_WELCOME;
153                 }
154                 break;
155         case DEFAULT_CHANNEL:
156                 setting = config_lookup(&configuration, "default_channel");
157                 if (!setting)
158                         return "";
159                 else {
160                         if ((strsetting = config_setting_get_string(setting)) != NULL)
161                                 return strsetting;
162                         else
163                         return "";
164                 }
165                 break;
166         case USERNAME:
167                 setting = config_lookup(&configuration, "username");
168                 if (!setting)
169                         return "";
170                 else {
171                         if ((strsetting = config_setting_get_string(setting)) != NULL)
172                                 return strsetting;
173                         else
174                         return "";
175                 }
176                 break;
177         case GROUPNAME:
178                 setting = config_lookup(&configuration, "groupname");
179                 if (!setting)
180                         return "";
181                 else {
182                         if ((strsetting = config_setting_get_string(setting)) != NULL)
183                                 return strsetting;
184                         else
185                         return "";
186                 }
187                 break;
188         case LOGFILE:
189                 setting = config_lookup(&configuration, "logfile");
190                 if (!setting)
191                         return NULL;
192                 else {
193                         if ((strsetting = config_setting_get_string(setting)) != NULL)
194                                 return strsetting;
195                         else
196                         return NULL;
197                 }
198                 break;
199         case BANFILE:
200                 setting = config_lookup(&configuration, "banfile");
201                 if (!setting)
202                         return NULL;
203                 else {
204                         if ((strsetting = config_setting_get_string(setting)) != NULL)
205                                 return strsetting;
206                         else
207                         return NULL;
208                 }
209                 break;
210         default:
211                 doAssert(false);
212                 break;
213         }
214         return NULL;
215 }
216
217 int getIntConf(param_t param)
218 {
219         config_setting_t *setting = NULL;
220         
221         switch (param) {
222         case BINDPORT:
223                 setting = config_lookup(&configuration, "bindport");
224                 if (!setting)
225                         return DEFAULT_BINDPORT;
226                 else {
227                         return config_setting_get_int(setting);
228                 }
229                 break;
230         case BAN_LENGTH:
231                 setting = config_lookup(&configuration, "ban_length");
232                 if (!setting)
233                         return DEFAULT_BAN_LENGTH;
234                 else {
235                         return config_setting_get_int(setting);
236                 }
237                 break;
238         case MAX_BANDWIDTH:
239                 setting = config_lookup(&configuration, "max_bandwidth");
240                 if (!setting)
241                         return DEFAULT_MAX_BANDWIDTH;
242                 else {
243                         return config_setting_get_int(setting);
244                 }
245                 break;
246         case MAX_CLIENTS:
247                 setting = config_lookup(&configuration, "max_users");
248                 if (!setting)
249                         return DEFAULT_MAX_CLIENTS;
250                 else {
251                         return config_setting_get_int(setting);
252                 }
253                 break;
254         case OPUS_THRESHOLD:
255                 setting = config_lookup(&configuration, "opus_threshold");
256                 if (!setting)
257                         return DEFAULT_OPUS_THRESHOLD;
258                 else {
259                         return config_setting_get_int(setting);
260                 }
261                 break;
262         default:
263                 doAssert(false);
264         }
265 }
266
267 bool_t getBoolConf(param_t param)
268 {
269         config_setting_t *setting = NULL;
270         
271         switch (param) {
272         case ALLOW_TEXTMESSAGE:
273                 setting = config_lookup(&configuration, "allow_textmessage");
274                 if (!setting)
275                         return true;
276                 else
277                         return config_setting_get_bool(setting);
278                 break;
279         case ENABLE_BAN:
280                 setting = config_lookup(&configuration, "enable_ban");
281                 if (!setting)
282                         return false;
283                 else
284                         return config_setting_get_bool(setting);
285                 break;
286         case SYNC_BANFILE:
287                 setting = config_lookup(&configuration, "sync_banfile");
288                 if (!setting)
289                         return false;
290                 else
291                         return config_setting_get_bool(setting);
292                 break;
293         default:
294                 doAssert(false);
295         }
296 }
297
298 int Conf_getNextChannel(conf_channel_t *chdesc, int index)
299 {
300         config_setting_t *setting = NULL;
301         int maxconfig = 64, ret = 0;
302         char configstr[maxconfig];
303         
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);
309         
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);
315         
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;
320         else
321                 chdesc->description = config_setting_get_string(setting);
322         
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;
327         else
328                 chdesc->password = config_setting_get_string(setting);
329         
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;
334         else
335                 chdesc->noenter = config_setting_get_bool(setting);
336         
337         ret = snprintf(configstr, maxconfig, "channels.[%d].silent", index);
338         setting = config_lookup(&configuration, configstr);
339         if (ret >= maxconfig || ret < 0 || setting == NULL) /* Optional */
340                 chdesc->silent = false;
341         else
342                 chdesc->silent = config_setting_get_bool(setting);
343         
344         ret = snprintf(configstr, maxconfig, "channels.[%d].position", index);
345         setting = config_lookup(&configuration, configstr);
346         if (ret >= maxconfig || ret < 0 || setting == NULL) /* Optional */
347                 chdesc->position = 0;
348         else
349                 chdesc->position = config_setting_get_int(setting);
350
351         return 0;
352 }
353
354 int Conf_getNextChannelLink(conf_channel_link_t *chlink, int index)
355 {
356         config_setting_t *setting = NULL;
357         int maxconfig = 64, ret = 0;
358         char configstr[maxconfig];
359         
360         ret = snprintf(configstr, maxconfig, "channel_links.[%d].source", index);
361         setting = config_lookup(&configuration, configstr);
362         if (ret >= maxconfig || ret < 0 || setting == NULL)
363                 return -1;
364         chlink->source = config_setting_get_string(setting);
365
366         ret = snprintf(configstr, maxconfig, "channel_links.[%d].destination", index);
367         setting = config_lookup(&configuration, configstr);
368         if (ret >= maxconfig || ret < 0 || setting == NULL)
369                 return -1;
370         chlink->destination = config_setting_get_string(setting);
371
372         return 0;
373 }