Mercurial > ~mikael > mcabber > hg
comparison mcabber/mcabber/settings.c @ 2204:ccd4ffa41a1b
Use XDG configuration directory if a config file exists
If $XDG_CONFIG_HOME (or $HOME/.config) contains a file "mcabber/mcabberrc"
it is used and $XDG_CONFIG_HOME/mcabber becomes the main mcabber
configuration directory.
(The ~/.mcabber/mcabberrc is still checked first.)
author | Mikael Berthe <mikael@lilotux.net> |
---|---|
date | Sat, 17 Oct 2015 20:17:30 +0200 |
parents | 038c4d601011 |
children | 5a107c907e71 |
comparison
equal
deleted
inserted
replaced
2203:170597f5365b | 2204:ccd4ffa41a1b |
---|---|
1 /* | 1 /* |
2 * settings.c -- Configuration stuff | 2 * settings.c -- Configuration stuff |
3 * | 3 * |
4 * Copyright (C) 2005-2010 Mikael Berthe <mikael@lilotux.net> | 4 * Copyright (C) 2005-2015 Mikael Berthe <mikael@lilotux.net> |
5 * | 5 * |
6 * This program is free software; you can redistribute it and/or modify | 6 * This program is free software; you can redistribute it and/or modify |
7 * it under the terms of the GNU General Public License as published by | 7 * it under the terms of the GNU General Public License as published by |
8 * the Free Software Foundation; either version 2 of the License, or (at | 8 * the Free Software Foundation; either version 2 of the License, or (at |
9 * your option) any later version. | 9 * your option) any later version. |
20 */ | 20 */ |
21 | 21 |
22 #include <stdio.h> | 22 #include <stdio.h> |
23 #include <stdlib.h> | 23 #include <stdlib.h> |
24 #include <string.h> | 24 #include <string.h> |
25 #include <sys/stat.h> | |
25 | 26 |
26 #include "config.h" | 27 #include "config.h" |
27 #include "settings.h" | 28 #include "settings.h" |
28 #include "commands.h" | 29 #include "commands.h" |
29 #include "logprint.h" | 30 #include "logprint.h" |
85 #ifdef HAVE_LIBOTR | 86 #ifdef HAVE_LIBOTR |
86 otrpolicy = g_hash_table_new(&g_str_hash, &g_str_equal); | 87 otrpolicy = g_hash_table_new(&g_str_hash, &g_str_equal); |
87 #endif | 88 #endif |
88 } | 89 } |
89 | 90 |
91 // settings_get_mcabber_config_dir() | |
92 // Returns the mcabber configuration directory. | |
93 // The directory is looked up for only once (and the string is never freed, | |
94 // but we might change this later). | |
95 const gchar *settings_get_mcabber_config_dir(void) | |
96 { | |
97 static char *config_dir; | |
98 char *home_dir; | |
99 GString *sfilename; | |
100 FILE *fp; | |
101 | |
102 if (config_dir) | |
103 return config_dir; | |
104 | |
105 home_dir = getenv("HOME"); | |
106 if (!home_dir) { | |
107 scr_log_print(LPRINT_LOG, "Can't find home dir!"); | |
108 fprintf(stderr, "Can't find home dir!\n"); | |
109 return NULL; | |
110 } | |
111 | |
112 sfilename = g_string_new(""); | |
113 | |
114 // First try: $HOME/.mcabber/mcabberrc | |
115 g_string_printf(sfilename, "%s/.mcabber/mcabberrc", home_dir); | |
116 if ((fp = fopen(sfilename->str, "r")) != NULL) { | |
117 fclose(fp); | |
118 config_dir = g_strdup_printf("%s/.mcabber", home_dir); | |
119 } | |
120 | |
121 // Second guess: Try to use the XDG standard configuration directory | |
122 if (!config_dir) { | |
123 char *xdg_config_dir = g_strdup(getenv("XDG_CONFIG_HOME")); | |
124 if (!xdg_config_dir || !*xdg_config_dir) { | |
125 // Free the string if it is non-null but empty | |
126 if (xdg_config_dir) g_free(xdg_config_dir); | |
127 xdg_config_dir = g_strdup_printf("%s/.config", home_dir); | |
128 } | |
129 | |
130 if (xdg_config_dir) { | |
131 int fd = -1; | |
132 struct stat statbuf; | |
133 | |
134 if (*xdg_config_dir) | |
135 fd = stat(xdg_config_dir, &statbuf); | |
136 | |
137 // If the XDG configuration directory exists, see if we can find a | |
138 // configuration file for mcabber inside. | |
139 if (fd != -1) { | |
140 char *xdg_mcabber_dir = g_strdup_printf("%s/mcabber", xdg_config_dir); | |
141 g_string_printf(sfilename, "%s/mcabberrc", xdg_mcabber_dir); | |
142 if ((fp = fopen(sfilename->str, "r")) != NULL) { | |
143 fclose(fp); | |
144 config_dir = xdg_mcabber_dir; | |
145 } else { | |
146 g_free(xdg_mcabber_dir); | |
147 } | |
148 } | |
149 g_free(xdg_config_dir); | |
150 } | |
151 } | |
152 | |
153 // Last guess: home directory itself... | |
154 if (!config_dir) { | |
155 g_string_printf(sfilename, "%s/.mcabberrc", home_dir); | |
156 if ((fp = fopen(sfilename->str, "r")) != NULL) { | |
157 fclose(fp); | |
158 config_dir = g_strdup(home_dir); | |
159 } else { | |
160 scr_log_print(LPRINT_NORMAL, "Cannot find/open any configuration file!\n"); | |
161 } | |
162 } | |
163 | |
164 g_string_free(sfilename, TRUE); | |
165 return config_dir; | |
166 } | |
167 | |
90 // cfg_read_file(filename, mainfile) | 168 // cfg_read_file(filename, mainfile) |
91 // Read and parse config file "filename". If filename is NULL, | 169 // Read and parse config file "filename". If filename is NULL, |
92 // try to open the configuration file at the default locations. | 170 // try to open the configuration file at the default locations. |
93 // mainfile must be set to TRUE for the startup config file. | 171 // mainfile must be set to TRUE for the startup config file. |
94 // If mainfile is TRUE, the permissions of the configuration file will | 172 // If mainfile is TRUE, the permissions of the configuration file will |
102 char *line, *eol; | 180 char *line, *eol; |
103 unsigned int ln = 0; | 181 unsigned int ln = 0; |
104 int err = 0; | 182 int err = 0; |
105 | 183 |
106 if (!filename) { | 184 if (!filename) { |
107 // Use default config file locations | 185 const gchar *mcabber_conf_dir; |
108 char *home; | 186 gchar *def_filename; |
109 GString *sfilename; | |
110 | 187 |
111 if (!mainfile) { | 188 if (!mainfile) { |
112 scr_LogPrint(LPRINT_LOGNORM, "No file name provided"); | 189 scr_LogPrint(LPRINT_LOGNORM, "No file name provided"); |
113 return -1; | 190 return -1; |
114 } | 191 } |
115 | 192 |
116 home = getenv("HOME"); | 193 mcabber_conf_dir = settings_get_mcabber_config_dir(); |
117 if (!home) { | 194 if (!mcabber_conf_dir) |
118 scr_LogPrint(LPRINT_LOG, "Can't find home dir!"); | 195 return -1; |
119 fprintf(stderr, "Can't find home dir!\n"); | 196 |
197 def_filename = g_strdup_printf("%s/mcabberrc", mcabber_conf_dir); | |
198 if ((fp = fopen(def_filename, "r")) == NULL) { | |
199 fprintf(stderr, "Cannot open config file!\n"); | |
200 g_free(def_filename); | |
120 err = -1; | 201 err = -1; |
121 goto cfg_read_file_return; | 202 goto cfg_read_file_return; |
122 } | 203 } |
123 sfilename = g_string_new(""); | |
124 g_string_printf(sfilename, "%s/.mcabber/mcabberrc", home); | |
125 if ((fp = fopen(sfilename->str, "r")) == NULL) { | |
126 // 2nd try... | |
127 g_string_printf(sfilename, "%s/.mcabberrc", home); | |
128 if ((fp = fopen(sfilename->str, "r")) == NULL) { | |
129 fprintf(stderr, "Cannot open config file!\n"); | |
130 g_string_free(sfilename, TRUE); | |
131 err = -1; | |
132 goto cfg_read_file_return; | |
133 } | |
134 } | |
135 // Check configuration file permissions | 204 // Check configuration file permissions |
136 // As it could contain sensitive data, we make it user-readable only. | 205 // As it could contain sensitive data, we make it user-readable only. |
137 checkset_perm(sfilename->str, TRUE); | 206 checkset_perm(def_filename, TRUE); |
138 scr_LogPrint(LPRINT_LOGNORM, "Reading %s", sfilename->str); | 207 scr_log_print(LPRINT_LOGNORM, "Reading %s", def_filename); |
139 // Check mcabber dir. Here we just warn, we don't change the modes. | 208 // Check mcabber dir. Here we just warn, we don't change the modes. |
140 g_string_printf(sfilename, "%s/.mcabber/", home); | 209 checkset_perm(mcabber_conf_dir, FALSE); |
141 checkset_perm(sfilename->str, FALSE); | 210 g_free(def_filename); |
142 g_string_free(sfilename, TRUE); | |
143 } else { | 211 } else { |
144 // filename was specified | 212 // filename was specified |
145 if ((fp = fopen(filename, "r")) == NULL) { | 213 if ((fp = fopen(filename, "r")) == NULL) { |
146 const char *msg = "Cannot open configuration file"; | 214 const char *msg = "Cannot open configuration file"; |
147 if (mainfile) | 215 if (mainfile) |