[svn-commits] file: trunk r363 - /trunk/res_config_mysql.c

svn-commits at lists.digium.com svn-commits at lists.digium.com
Wed Apr 11 07:34:08 MST 2007


Author: file
Date: Wed Apr 11 09:34:07 2007
New Revision: 363

URL: http://svn.digium.com/view/asterisk-addons?view=rev&rev=363
Log:
Make the configuration loader a bit smarter... and use defaults when we can't find settings. (issue #9518 reported by cstadlmann)

Modified:
    trunk/res_config_mysql.c

Modified: trunk/res_config_mysql.c
URL: http://svn.digium.com/view/asterisk-addons/trunk/res_config_mysql.c?view=diff&rev=363&r1=362&r2=363
==============================================================================
--- trunk/res_config_mysql.c (original)
+++ trunk/res_config_mysql.c Wed Apr 11 09:34:07 2007
@@ -696,36 +696,63 @@
 	return 0;
 }
 
+static int set_defaults(struct mysql_conn *conn)
+{
+	ast_copy_string(conn->user, "asterisk", sizeof(conn->user));
+	ast_copy_string(conn->pass, "asterisk", sizeof(conn->pass));
+	ast_copy_string(conn->host, "", sizeof(conn->host));
+	ast_copy_string(conn->name, "asterisk", sizeof(conn->name));
+	conn->port = 3306;
+	ast_copy_string(conn->sock, "/tmp/mysql.sock", sizeof(conn->sock));
+
+	if (!ast_strlen_zero(conn->host)) {
+		ast_log(LOG_DEBUG, "MySQL RealTime host: %s\n", conn->host);
+		ast_log(LOG_DEBUG, "MySQL RealTime port: %i\n", conn->port);
+	} else
+		ast_log(LOG_DEBUG, "MySQL RealTime socket: %s\n", conn->sock);
+	ast_log(LOG_DEBUG, "MySQL RealTime database name: %s\n", conn->name);
+	ast_log(LOG_DEBUG, "MySQL RealTime user: %s\n", conn->user);
+	ast_log(LOG_DEBUG, "MySQL RealTime password: %s\n", conn->pass);
+
+	return 0;
+}
+
 static int parse_config(void)
 {
-	struct ast_config *config;
-
-	config = ast_config_load(RES_CONFIG_MYSQL_CONF);
-
-	if (config) {
-		const char *catg = "write";
-		int haswriteconfig = 0;
-		if (!ast_category_exist(config, catg))
-			catg = "general";
-		if (!ast_category_exist(config, catg))
-			ast_log(LOG_WARNING, "No configuration set for writing to the MySQL database.\n");
-		else
-			haswriteconfig = load_mysql_config(config, catg, &dbwrite);
-
-		if (!ast_category_exist(config, "read")) {
-			if (haswriteconfig) {
-				/* Copy from write config */
-				ast_copy_string(dbread.host, dbwrite.host, sizeof(dbread.host));
-				ast_copy_string(dbread.sock, dbwrite.sock, sizeof(dbread.sock));
-				ast_copy_string(dbread.name, dbwrite.name, sizeof(dbread.name));
-				ast_copy_string(dbread.user, dbwrite.user, sizeof(dbread.user));
-				ast_copy_string(dbread.pass, dbwrite.pass, sizeof(dbread.pass));
-				dbread.port = dbwrite.port;
-			} else
-				ast_log(LOG_WARNING, "It's kind of silly to be loading res_config_mysql.so if there's no configuration settings.\n");
-		} else
-			load_mysql_config(config, "read", &dbread);
-	}
+	struct ast_config *config = NULL;
+	const char *catg = "write";
+	int haswriteconfig = 0;
+
+	if (!(config = ast_config_load(RES_CONFIG_MYSQL_CONF))) {
+		set_defaults(&dbwrite);
+		set_defaults(&dbread);
+		return 0;
+	}
+
+	if (!ast_category_exist(config, catg))
+		catg = "general";
+	if (!ast_category_exist(config, catg)) {
+		ast_log(LOG_WARNING, "No configuration set for writing to the MySQL database. Using defaults.\n");
+		set_defaults(&dbwrite);
+	} else
+		haswriteconfig = load_mysql_config(config, catg, &dbwrite);
+	
+	if (!ast_category_exist(config, "read")) {
+		if (haswriteconfig) {
+			/* Copy from write config */
+			ast_copy_string(dbread.host, dbwrite.host, sizeof(dbread.host));
+			ast_copy_string(dbread.sock, dbwrite.sock, sizeof(dbread.sock));
+			ast_copy_string(dbread.name, dbwrite.name, sizeof(dbread.name));
+			ast_copy_string(dbread.user, dbwrite.user, sizeof(dbread.user));
+			ast_copy_string(dbread.pass, dbwrite.pass, sizeof(dbread.pass));
+			dbread.port = dbwrite.port;
+		} else {
+			ast_log(LOG_WARNING, "It's kind of silly to be loading res_config_mysql.so if there's no configuration settings. Let's use the defaults!\n");
+			set_defaults(&dbread);
+		}
+	} else
+		load_mysql_config(config, "read", &dbread);
+
 	ast_config_destroy(config);
 
 	return 0;



More information about the svn-commits mailing list