summary refs log tree commit diff stats
path: root/src/common/cfgfiles.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/common/cfgfiles.c')
-rw-r--r--src/common/cfgfiles.c140
1 files changed, 82 insertions, 58 deletions
diff --git a/src/common/cfgfiles.c b/src/common/cfgfiles.c
index c3cad2aa..20e48ffd 100644
--- a/src/common/cfgfiles.c
+++ b/src/common/cfgfiles.c
@@ -337,21 +337,10 @@ get_xdir (void)
 	return xdir;
 }
 
-static void
-check_prefs_dir (void)
+int
+check_config_dir (void)
 {
-	char *dir = get_xdir ();
-	char *msg;
-
-	if (g_access (dir, F_OK) != 0)
-	{
-		if (g_mkdir (dir, 0700) != 0)
-		{
-			msg = g_strdup_printf ("Cannot create %s", get_xdir ());
-			fe_message (msg, FE_MSG_ERROR);
-			g_free (msg);
-		}
-	}
+	return g_access (get_xdir (), F_OK);
 }
 
 static char *
@@ -617,16 +606,14 @@ convert_with_fallback (const char *str, const char *fallback)
 }
 
 void
-load_config (void)
+load_default_config(void)
 {
-	char *cfg, *sp, *buf;
 	const char *username, *realname;
-	int res, val, i;
+	char *sp;
 #ifdef WIN32
 	char out[256];
 #endif
 
-	check_prefs_dir ();
 	username = g_get_user_name ();
 	if (!username)
 		username = "root";
@@ -800,55 +787,89 @@ load_config (void)
 	/* private variables */
 	prefs.local_ip = 0xffffffff;
 
+	sp = strchr (prefs.hex_irc_user_name, ' ');
+	if (sp)
+		sp[0] = 0;	/* spaces in username would break the login */
+
 	g_free ((char *)username);
 	g_free ((char *)realname);
+}
 
-	if (g_file_get_contents (default_file (), &cfg, NULL, NULL))
+int
+make_config_dirs (void)
+{
+	char *buf;
+
+	if (g_mkdir (get_xdir (), 0700) != 0)
+		return -1;
+	
+	buf = g_build_filename (get_xdir (), "addons", NULL);
+	if (g_mkdir (buf, 0700) != 0)
 	{
-		i = 0;
-		do
-		{
-			switch (vars[i].type)
-			{
-			case TYPE_STR:
-				cfg_get_str (cfg, vars[i].name, (char *) &prefs + vars[i].offset,
-								 vars[i].len);
-				break;
-			case TYPE_BOOL:
-			case TYPE_INT:
-				val = cfg_get_int_with_result (cfg, vars[i].name, &res);
-				if (res)
-					*((int *) &prefs + vars[i].offset) = val;
-				break;
-			}
-			i++;
-		}
-		while (vars[i].name);
+		g_free (buf);
+		return -1;
+	}
+	g_free (buf);
+	
+	buf = g_build_filename (get_xdir (), HEXCHAT_SOUND_DIR, NULL);
+	if (g_mkdir (buf, 0700) != 0)
+	{
+		g_free (buf);
+		return -1;
+	}
+	g_free (buf);
 
-		g_free (cfg);
+	return 0;
+}
 
-	} else
-	{
-#ifndef WIN32
-#ifndef __EMX__
-		/* OS/2 uses UID 0 all the time */
-		if (getuid () == 0)
-			fe_message (_("* Running IRC as root is stupid! You should\n"
-							"  create a User Account and use that to login.\n"), FE_MSG_WARN|FE_MSG_WAIT);
-#endif
-#endif /* !WIN32 */
+int
+make_dcc_dirs (void)
+{
+	if (g_mkdir (prefs.hex_dcc_dir, 0700) != 0)
+		return -1;
 
-		g_mkdir (prefs.hex_dcc_dir, 0700);
-		g_mkdir (prefs.hex_dcc_completed_dir, 0700);
+	if (g_mkdir (prefs.hex_dcc_completed_dir, 0700) != 0)
+		return -1;
 
-		buf = g_build_filename (get_xdir (), "addons", NULL);
-		g_mkdir (buf, 0700);
-		g_free (buf);
+	return 0;
+}
 
-		buf = g_build_filename (get_xdir (), HEXCHAT_SOUND_DIR, NULL);
-		g_mkdir (buf, 0700);
-		g_free (buf);
+int
+load_config (void)
+{
+	char *cfg, *sp;
+	int res, val, i;
+
+	g_assert(check_config_dir () == 0);
+
+	if (!g_file_get_contents (default_file (), &cfg, NULL, NULL))
+		return -1;
+
+	/* If the config is incomplete we have the default values loaded */
+	load_default_config();
+
+	i = 0;
+	do
+	{
+		switch (vars[i].type)
+		{
+		case TYPE_STR:
+			cfg_get_str (cfg, vars[i].name, (char *) &prefs + vars[i].offset,
+				     vars[i].len);
+			break;
+		case TYPE_BOOL:
+		case TYPE_INT:
+			val = cfg_get_int_with_result (cfg, vars[i].name, &res);
+			if (res)
+				*((int *) &prefs + vars[i].offset) = val;
+			break;
+		}
+		i++;
 	}
+	while (vars[i].name);
+	
+	g_free (cfg);
+
 	if (prefs.hex_gui_win_height < 138)
 		prefs.hex_gui_win_height = 138;
 	if (prefs.hex_gui_win_width < 106)
@@ -857,6 +878,8 @@ load_config (void)
 	sp = strchr (prefs.hex_irc_user_name, ' ');
 	if (sp)
 		sp[0] = 0;	/* spaces in username would break the login */
+
+	return 0;
 }
 
 int
@@ -865,7 +888,8 @@ save_config (void)
 	int fh, i;
 	char *config, *new_config;
 
-	check_prefs_dir ();
+	if (check_config_dir () != 0)
+		make_config_dirs ();
 
 	config = default_file ();
 	new_config = g_strconcat (config, ".new", NULL);