Add option to disallow text messages.
[umurmur.git] / src / conf.c
index d85ff7c0eb20aef5613d3fc1661e8ed016fd264a..cf1677bffdd852bf3f16e6309a9e9bebb691fe1c 100644 (file)
@@ -1,5 +1,5 @@
-/* Copyright (C) 2009-2010, Martin Johansson <martin@fatbob.nu>
-   Copyright (C) 2005-2010, Thorvald Natvig <thorvald@natvig.com>
+/* Copyright (C) 2009-2011, Martin Johansson <martin@fatbob.nu>
+   Copyright (C) 2005-2011, Thorvald Natvig <thorvald@natvig.com>
 
    All rights reserved.
 
 
 static config_t configuration;
 
-#define DEFAULT_CONFIG "/etc/umurmur.conf"
 #define DEFAULT_WELCOME "Welcome to uMurmur!"
 #define DEFAULT_MAX_CLIENTS 10
 #define DEFAULT_MAX_BANDWIDTH 48000
 #define DEFAULT_BINDPORT 64738
+#define DEFAULT_BAN_LENGTH (60*60)
 
 const char defaultconfig[] = DEFAULT_CONFIG;
 
-int Conf_init(const char *conffile)
+void Conf_init(const char *conffile)
 {
-       const char *conf;
-       
        config_init(&configuration);
        if (conffile == NULL)
-               conf = defaultconfig;
-       else
-               conf = conffile;
-       if (config_read_file(&configuration, conf) != CONFIG_TRUE) {
-               fprintf(stderr, "Error in config file %s: %s at line %d\n", conffile,
-                               config_error_text(&configuration), config_error_line(&configuration));
-               exit(1);
+               conffile = defaultconfig;
+       if (config_read_file(&configuration, conffile) != CONFIG_TRUE) {
+               Log_fatal("Error in config file %s line %d: %s", conffile,
+                                 config_error_line(&configuration), config_error_text(&configuration));
        }
-       return 0;
+}
+
+bool_t Conf_ok(const char *conffile)
+{
+       bool_t rc = true;
+       config_init(&configuration);
+       if (conffile == NULL)
+               conffile = defaultconfig;
+       if (config_read_file(&configuration, conffile) != CONFIG_TRUE) {
+               fprintf(stderr, "Error in config file %s line %d: %s\n", conffile,
+                       config_error_line(&configuration), config_error_text(&configuration));
+               rc = false;
+       }
+       config_destroy(&configuration);
+       return rc;
 }
 
 void Conf_deinit()
@@ -109,6 +118,17 @@ const char *getStrConf(param_t param)
                                return "";
                }
                break;
+       case ADMIN_PASSPHRASE:
+               setting = config_lookup(&configuration, "admin_password");
+               if (!setting)
+                       return "";
+               else {
+                       if ((strsetting = config_setting_get_string(setting)) != NULL)
+                               return strsetting;
+                       else
+                               return "";
+               }
+               break;
        case BINDADDR:
                setting = config_lookup(&configuration, "bindaddr");
                if (!setting)
@@ -142,6 +162,39 @@ const char *getStrConf(param_t param)
                        return "";
                }
                break;
+       case USERNAME:
+               setting = config_lookup(&configuration, "username");
+               if (!setting)
+                       return "";
+               else {
+                       if ((strsetting = config_setting_get_string(setting)) != NULL)
+                               return strsetting;
+                       else
+                       return "";
+               }
+               break;
+       case GROUPNAME:
+               setting = config_lookup(&configuration, "groupname");
+               if (!setting)
+                       return "";
+               else {
+                       if ((strsetting = config_setting_get_string(setting)) != NULL)
+                               return strsetting;
+                       else
+                       return "";
+               }
+               break;
+       case LOGFILE:
+               setting = config_lookup(&configuration, "logfile");
+               if (!setting)
+                       return NULL;
+               else {
+                       if ((strsetting = config_setting_get_string(setting)) != NULL)
+                               return strsetting;
+                       else
+                       return NULL;
+               }
+               break;
        default:
                doAssert(false);
                break;
@@ -162,6 +215,14 @@ int getIntConf(param_t param)
                        return config_setting_get_int(setting);
                }
                break;
+       case BAN_LENGTH:
+               setting = config_lookup(&configuration, "ban_length");
+               if (!setting)
+                       return DEFAULT_BAN_LENGTH;
+               else {
+                       return config_setting_get_int(setting);
+               }
+               break;
        case MAX_BANDWIDTH:
                setting = config_lookup(&configuration, "max_bandwidth");
                if (!setting)
@@ -183,6 +244,23 @@ int getIntConf(param_t param)
        }
 }
 
+bool_t getBoolConf(param_t param)
+{
+       config_setting_t *setting = NULL;
+       
+       switch (param) {
+       case ALLOW_TEXTMESSAGE:
+               setting = config_lookup(&configuration, "allow_textmessage");
+               if (!setting)
+                       return true;
+               else
+                       return config_setting_get_bool(setting);
+               break;
+       default:
+               doAssert(false);
+       }
+}
+
 int Conf_getNextChannel(conf_channel_t *chdesc, int index)
 {
        config_setting_t *setting = NULL;
@@ -208,6 +286,13 @@ int Conf_getNextChannel(conf_channel_t *chdesc, int index)
        else
                chdesc->description = config_setting_get_string(setting);
        
+       ret = snprintf(configstr, maxconfig, "channels.[%d].password", index);
+       setting = config_lookup(&configuration, configstr);
+       if (ret >= maxconfig || ret < 0 || setting == NULL) /* Optional */
+               chdesc->password = NULL;
+       else
+               chdesc->password = config_setting_get_string(setting);
+       
        ret = snprintf(configstr, maxconfig, "channels.[%d].noenter", index);
        setting = config_lookup(&configuration, configstr);
        if (ret >= maxconfig || ret < 0 || setting == NULL) /* Optional */