Clean up 'help'.
[umurmur.git] / src / conf.c
1 /* Copyright (C) 2009-2011, Martin Johansson <martin@fatbob.nu>
2    Copyright (C) 2005-2011, 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
48 const char defaultconfig[] = DEFAULT_CONFIG;
49
50 void Conf_init(const char *conffile)
51 {
52         config_init(&configuration);
53         if (conffile == NULL)
54                 conffile = defaultconfig;
55         if (config_read_file(&configuration, conffile) != CONFIG_TRUE) {
56                 Log_fatal("Error in config file %s line %d: %s", conffile,
57                                   config_error_line(&configuration), config_error_text(&configuration));
58         }
59 }
60
61 void Conf_deinit()
62 {
63         config_destroy(&configuration);
64 }
65
66 const char *getStrConf(param_t param)
67 {
68         config_setting_t *setting = NULL;
69         const char *strsetting = NULL;
70         
71         switch (param) {
72         case CERTIFICATE:
73                 setting = config_lookup(&configuration, "certificate");
74                 if (!setting)
75                         return "/etc/umurmur/certificate.crt";
76                 else {
77                         if ((strsetting = config_setting_get_string(setting)) != NULL)
78                                 return strsetting;
79                         else
80                                 return "/etc/umurmur/certificate.crt";
81                 }
82                 break;
83         case KEY:
84                 setting = config_lookup(&configuration, "private_key");
85                 if (!setting)
86                         return "/etc/umurmur/private_key.key";
87                 else {
88                         if ((strsetting = config_setting_get_string(setting)) != NULL)
89                                 return strsetting;
90                         else
91                                 return "/etc/umurmur/private_key.key";
92                 }
93                 break;
94         case PASSPHRASE:
95                 setting = config_lookup(&configuration, "password");
96                 if (!setting)
97                         return "";
98                 else {
99                         if ((strsetting = config_setting_get_string(setting)) != NULL)
100                                 return strsetting;
101                         else
102                                 return "";
103                 }
104                 break;
105         case BINDADDR:
106                 setting = config_lookup(&configuration, "bindaddr");
107                 if (!setting)
108                         return "";
109                 else {
110                         if ((strsetting = config_setting_get_string(setting)) != NULL)
111                                 return strsetting;
112                         else
113                                 return "";
114                 }
115                 break;
116         case WELCOMETEXT:
117                 setting = config_lookup(&configuration, "welcometext");
118                 if (!setting)
119                         return DEFAULT_WELCOME;
120                 else {
121                         if ((strsetting = config_setting_get_string(setting)) != NULL)
122                                 return strsetting;
123                         else
124                         return DEFAULT_WELCOME;
125                 }
126                 break;
127         case DEFAULT_CHANNEL:
128                 setting = config_lookup(&configuration, "default_channel");
129                 if (!setting)
130                         return "";
131                 else {
132                         if ((strsetting = config_setting_get_string(setting)) != NULL)
133                                 return strsetting;
134                         else
135                         return "";
136                 }
137                 break;
138         case USERNAME:
139                 setting = config_lookup(&configuration, "username");
140                 if (!setting)
141                         return "";
142                 else {
143                         if ((strsetting = config_setting_get_string(setting)) != NULL)
144                                 return strsetting;
145                         else
146                         return "";
147                 }
148                 break;
149         case GROUPNAME:
150                 setting = config_lookup(&configuration, "groupname");
151                 if (!setting)
152                         return "";
153                 else {
154                         if ((strsetting = config_setting_get_string(setting)) != NULL)
155                                 return strsetting;
156                         else
157                         return "";
158                 }
159                 break;
160         case LOGFILE:
161                 setting = config_lookup(&configuration, "logfile");
162                 if (!setting)
163                         return NULL;
164                 else {
165                         if ((strsetting = config_setting_get_string(setting)) != NULL)
166                                 return strsetting;
167                         else
168                         return NULL;
169                 }
170                 break;
171         default:
172                 doAssert(false);
173                 break;
174         }
175         return NULL;
176 }
177
178 int getIntConf(param_t param)
179 {
180         config_setting_t *setting = NULL;
181         
182         switch (param) {
183         case BINDPORT:
184                 setting = config_lookup(&configuration, "bindport");
185                 if (!setting)
186                         return DEFAULT_BINDPORT;
187                 else {
188                         return config_setting_get_int(setting);
189                 }
190                 break;
191         case MAX_BANDWIDTH:
192                 setting = config_lookup(&configuration, "max_bandwidth");
193                 if (!setting)
194                         return DEFAULT_MAX_BANDWIDTH;
195                 else {
196                         return config_setting_get_int(setting);
197                 }
198                 break;
199         case MAX_CLIENTS:
200                 setting = config_lookup(&configuration, "max_users");
201                 if (!setting)
202                         return DEFAULT_MAX_CLIENTS;
203                 else {
204                         return config_setting_get_int(setting);
205                 }
206                 break;
207         default:
208                 doAssert(false);
209         }
210 }
211
212 int Conf_getNextChannel(conf_channel_t *chdesc, int index)
213 {
214         config_setting_t *setting = NULL;
215         int maxconfig = 64, ret = 0;
216         char configstr[maxconfig];
217         
218         ret = snprintf(configstr, maxconfig, "channels.[%d].name", index);
219         setting = config_lookup(&configuration, configstr);
220         if (ret >= maxconfig || ret < 0 || setting == NULL)
221                 return -1; /* Required */
222         chdesc->name =  config_setting_get_string(setting);
223         
224         ret = snprintf(configstr, maxconfig, "channels.[%d].parent", index);
225         setting = config_lookup(&configuration, configstr);
226         if (ret >= maxconfig || ret < 0 || setting == NULL)
227                 return -1; /* Required */
228         chdesc->parent = config_setting_get_string(setting);
229         
230         ret = snprintf(configstr, maxconfig, "channels.[%d].description", index);
231         setting = config_lookup(&configuration, configstr);
232         if (ret >= maxconfig || ret < 0 || setting == NULL) /* Optional */
233                 chdesc->description = NULL;
234         else
235                 chdesc->description = config_setting_get_string(setting);
236         
237         ret = snprintf(configstr, maxconfig, "channels.[%d].noenter", index);
238         setting = config_lookup(&configuration, configstr);
239         if (ret >= maxconfig || ret < 0 || setting == NULL) /* Optional */
240                 chdesc->noenter = false;
241         else
242                 chdesc->noenter = config_setting_get_bool(setting);
243
244         return 0;
245 }
246
247 int Conf_getNextChannelLink(conf_channel_link_t *chlink, int index)
248 {
249         config_setting_t *setting = NULL;
250         int maxconfig = 64, ret = 0;
251         char configstr[maxconfig];
252         
253         ret = snprintf(configstr, maxconfig, "channel_links.[%d].source", index);
254         setting = config_lookup(&configuration, configstr);
255         if (ret >= maxconfig || ret < 0 || setting == NULL)
256                 return -1;
257         chlink->source = config_setting_get_string(setting);
258
259         ret = snprintf(configstr, maxconfig, "channel_links.[%d].destination", index);
260         setting = config_lookup(&configuration, configstr);
261         if (ret >= maxconfig || ret < 0 || setting == NULL)
262                 return -1;
263         chlink->destination = config_setting_get_string(setting);
264
265         return 0;
266 }