2 settings.c : Irssi settings
4 Copyright (C) 1999 Timo Sirainen
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
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #include "lib-config/iconfig.h"
28 #include "default-config.h"
32 CONFIG_REC *mainconfig;
34 static GString *last_errors;
35 static GSList *last_invalid_modules;
36 static int fe_initialized;
37 static int config_changed; /* FIXME: remove after .98 (unless needed again) */
39 static GHashTable *settings;
40 static int timeout_tag;
42 static int config_last_modifycounter;
43 static time_t config_last_mtime;
44 static long config_last_size;
45 static unsigned int config_last_checksum;
47 static SETTINGS_REC *settings_find(const char *key)
51 g_return_val_if_fail(key != NULL, NULL);
53 rec = g_hash_table_lookup(settings, key);
55 g_warning("settings_get_default_str(%s) : "
56 "unknown setting", key);
63 const char *settings_get_str(const char *key)
66 CONFIG_NODE *setnode, *node;
68 rec = settings_find(key);
69 g_return_val_if_fail(rec != NULL, NULL);
71 setnode = iconfig_node_traverse("settings", FALSE);
75 node = config_node_section(setnode, rec->module, -1);
76 return node == NULL ? rec->def :
77 config_node_get_str(node, key, rec->def);
80 int settings_get_int(const char *key)
83 CONFIG_NODE *setnode, *node;
86 rec = settings_find(key);
87 g_return_val_if_fail(rec != NULL, 0);
88 def = GPOINTER_TO_INT(rec->def);
90 setnode = iconfig_node_traverse("settings", FALSE);
94 node = config_node_section(setnode, rec->module, -1);
95 return node == NULL ? def :
96 config_node_get_int(node, key, def);
99 int settings_get_bool(const char *key)
102 CONFIG_NODE *setnode, *node;
105 rec = settings_find(key);
106 g_return_val_if_fail(rec != NULL, 0);
107 def = GPOINTER_TO_INT(rec->def);
109 setnode = iconfig_node_traverse("settings", FALSE);
113 node = config_node_section(setnode, rec->module, -1);
114 return node == NULL ? def :
115 config_node_get_bool(node, key, def);
118 void settings_add_str_module(const char *module, const char *section,
119 const char *key, const char *def)
123 g_return_if_fail(key != NULL);
124 g_return_if_fail(section != NULL);
126 rec = g_hash_table_lookup(settings, key);
127 g_return_if_fail(rec == NULL);
129 rec = g_new0(SETTINGS_REC, 1);
130 rec->module = g_strdup(module);
131 rec->key = g_strdup(key);
132 rec->section = g_strdup(section);
133 rec->def = def == NULL ? NULL : g_strdup(def);
135 g_hash_table_insert(settings, rec->key, rec);
138 void settings_add_int_module(const char *module, const char *section,
139 const char *key, int def)
143 g_return_if_fail(key != NULL);
144 g_return_if_fail(section != NULL);
146 rec = g_hash_table_lookup(settings, key);
147 g_return_if_fail(rec == NULL);
149 rec = g_new0(SETTINGS_REC, 1);
150 rec->module = g_strdup(module);
151 rec->type = SETTING_TYPE_INT;
152 rec->key = g_strdup(key);
153 rec->section = g_strdup(section);
154 rec->def = GINT_TO_POINTER(def);
156 g_hash_table_insert(settings, rec->key, rec);
159 void settings_add_bool_module(const char *module, const char *section,
160 const char *key, int def)
164 g_return_if_fail(key != NULL);
165 g_return_if_fail(section != NULL);
167 rec = g_hash_table_lookup(settings, key);
168 g_return_if_fail(rec == NULL);
170 rec = g_new0(SETTINGS_REC, 1);
171 rec->module = g_strdup(module);
172 rec->type = SETTING_TYPE_BOOLEAN;
173 rec->key = g_strdup(key);
174 rec->section = g_strdup(section);
175 rec->def = GINT_TO_POINTER(def);
177 g_hash_table_insert(settings, rec->key, rec);
180 static void settings_destroy(SETTINGS_REC *rec)
182 if (rec->type == SETTING_TYPE_STRING)
183 g_free_not_null(rec->def);
185 g_free(rec->section);
190 void settings_remove(const char *key)
194 g_return_if_fail(key != NULL);
196 rec = g_hash_table_lookup(settings, key);
197 if (rec == NULL) return;
199 g_hash_table_remove(settings, key);
200 settings_destroy(rec);
203 static int settings_remove_hash(const char *key, SETTINGS_REC *rec,
206 if (strcmp(rec->module, module) == 0) {
207 settings_destroy(rec);
214 void settings_remove_module(const char *module)
216 g_hash_table_foreach_remove(settings,
217 (GHRFunc) settings_remove_hash,
221 static CONFIG_NODE *settings_get_node(const char *key)
226 g_return_val_if_fail(key != NULL, NULL);
228 rec = g_hash_table_lookup(settings, key);
229 g_return_val_if_fail(rec != NULL, NULL);
231 node = iconfig_node_traverse("settings", TRUE);
232 return config_node_section(node, rec->module, NODE_TYPE_BLOCK);
235 void settings_set_str(const char *key, const char *value)
237 iconfig_node_set_str(settings_get_node(key), key, value);
240 void settings_set_int(const char *key, int value)
242 iconfig_node_set_int(settings_get_node(key), key, value);
245 void settings_set_bool(const char *key, int value)
247 iconfig_node_set_bool(settings_get_node(key), key, value);
250 int settings_get_type(const char *key)
254 g_return_val_if_fail(key != NULL, -1);
256 rec = g_hash_table_lookup(settings, key);
257 return rec == NULL ? -1 : rec->type;
260 /* Get the record of the setting */
261 SETTINGS_REC *settings_get_record(const char *key)
263 g_return_val_if_fail(key != NULL, NULL);
265 return g_hash_table_lookup(settings, key);
268 static void sig_init_finished(void)
270 fe_initialized = TRUE;
271 if (last_errors != NULL) {
272 signal_emit("settings errors", 1, last_errors->str);
273 g_string_free(last_errors, TRUE);
276 if (config_changed) {
277 /* some backwards compatibility changes were made to
278 config file, reload it */
279 signal_emit("setup changed", 0);
283 static void settings_clean_invalid_module(const char *module)
289 node = iconfig_node_traverse("settings", FALSE);
290 if (node == NULL) return;
292 node = config_node_section(node, module, -1);
293 if (node == NULL) return;
295 for (tmp = config_node_first(node->value); tmp != NULL; tmp = next) {
296 CONFIG_NODE *subnode = tmp->data;
297 next = config_node_next(tmp);
299 set = g_hash_table_lookup(settings, subnode->key);
300 if (set == NULL || strcmp(set->module, module) != 0)
301 iconfig_node_remove(node, subnode);
305 /* remove all invalid settings from config file. works only with the
306 modules that have already called settings_check() */
307 void settings_clean_invalid(void)
309 while (last_invalid_modules != NULL) {
310 char *module = last_invalid_modules->data;
312 settings_clean_invalid_module(module);
315 last_invalid_modules =
316 g_slist_remove(last_invalid_modules, module);
320 /* verify that all settings in config file for `module' are actually found
322 void settings_check_module(const char *module)
330 g_return_if_fail(module != NULL);
332 node = iconfig_node_traverse("settings", FALSE);
333 node = node == NULL ? NULL : config_node_section(node, module, -1);
334 if (node == NULL) return;
336 errors = g_string_new(NULL);
337 g_string_sprintf(errors, "Unknown settings in configuration "
338 "file for module %s:", module);
341 tmp = config_node_first(node->value);
342 for (; tmp != NULL; tmp = config_node_next(tmp)) {
345 set = g_hash_table_lookup(settings, node->key);
346 if (set == NULL || strcmp(set->module, module) != 0) {
347 g_string_sprintfa(errors, " %s", node->key);
352 if (gslist_find_icase_string(last_invalid_modules,
354 /* mark this module having invalid settings */
355 last_invalid_modules =
356 g_slist_append(last_invalid_modules,
360 signal_emit("settings errors", 1, errors->str);
362 if (last_errors == NULL)
363 last_errors = g_string_new(NULL);
365 g_string_append_c(last_errors, '\n');
366 g_string_append(last_errors, errors->str);
369 g_string_free(errors, TRUE);
372 static int settings_compare(SETTINGS_REC *v1, SETTINGS_REC *v2)
374 return strcmp(v1->section, v2->section);
377 static void settings_hash_get(const char *key, SETTINGS_REC *rec,
380 *list = g_slist_insert_sorted(*list, rec,
381 (GCompareFunc) settings_compare);
384 GSList *settings_get_sorted(void)
389 g_hash_table_foreach(settings, (GHFunc) settings_hash_get, &list);
395 /* if we get SIGTERM after this, just die instead of coming back here. */
396 signal(SIGTERM, SIG_DFL);
398 /* quit from all servers too.. */
399 signal_emit("command quit", 1, "");
405 /* Yes, this is my own stupid checksum generator, some "real" algorithm
406 would be nice but would just take more space without much real benefit */
407 static unsigned int file_checksum(const char *fname)
411 unsigned int checksum = 0;
413 f = open(fname, O_RDONLY);
414 if (f == -1) return 0;
417 while ((ret = read(f, buf, sizeof(buf))) > 0) {
419 checksum += buf[ret] << ((n++ & 3)*8);
425 static void irssi_config_save_state(const char *fname)
429 g_return_if_fail(fname != NULL);
431 if (stat(fname, &statbuf) != 0)
434 /* save modify time, file size and checksum */
435 config_last_mtime = statbuf.st_mtime;
436 config_last_size = statbuf.st_size;
437 config_last_checksum = file_checksum(fname);
440 int irssi_config_is_changed(const char *fname)
445 fname = mainconfig->fname;
447 if (stat(fname, &statbuf) != 0)
450 return config_last_mtime != statbuf.st_mtime &&
451 (config_last_size != statbuf.st_size ||
452 config_last_checksum != file_checksum(fname));
455 static CONFIG_REC *parse_configfile(const char *fname)
463 fname = get_irssi_config();
465 if (stat(fname, &statbuf) == 0)
468 /* user configuration file not found, use the default one
470 path = SYSCONFDIR"/irssi.conf";
471 if (stat(path, &statbuf) != 0) {
472 /* no configuration file in sysconfdir ..
473 use the build-in configuration */
478 config = config_open(path, -1);
479 if (config == NULL) {
480 str = g_strdup_printf("Error opening configuration file %s: %s",
481 path, g_strerror(errno));
482 signal_emit("gui dialog", 2, "error", str);
485 config = config_open(NULL, -1);
489 config_parse(config);
491 config_parse_data(config, default_config, "internal");
493 config_change_file_name(config, fname, 0660);
494 irssi_config_save_state(fname);
498 static void init_configfile(void)
503 if (stat(get_irssi_dir(), &statbuf) != 0) {
504 /* ~/.irssi not found, create it. */
505 if (mkpath(get_irssi_dir(), 0700) != 0) {
506 g_error("Couldn't create %s directory", get_irssi_dir());
508 } else if (!S_ISDIR(statbuf.st_mode)) {
509 g_error("%s is not a directory.\n"
510 "You should remove it with command: rm %s",
511 get_irssi_dir(), get_irssi_dir());
514 mainconfig = parse_configfile(NULL);
515 config_last_modifycounter = mainconfig->modifycounter;
518 if (config_last_error(mainconfig) != NULL) {
519 str = g_strdup_printf("Ignored errors in configuration file:\n%s",
520 config_last_error(mainconfig));
521 signal_emit("gui dialog", 2, "error", str);
525 signal(SIGTERM, sig_term);
528 int settings_reread(const char *fname)
530 CONFIG_REC *tempconfig;
533 str = fname == NULL ? NULL : convert_home(fname);
534 tempconfig = parse_configfile(str);
535 g_free_not_null(str);
537 if (tempconfig == NULL) {
538 signal_emit("gui dialog", 2, "error", g_strerror(errno));
542 if (config_last_error(tempconfig) != NULL) {
543 str = g_strdup_printf("Errors in configuration file:\n%s",
544 config_last_error(tempconfig));
545 signal_emit("gui dialog", 2, "error", str);
548 config_close(tempconfig);
552 config_close(mainconfig);
553 mainconfig = tempconfig;
554 config_last_modifycounter = mainconfig->modifycounter;
556 signal_emit("setup changed", 0);
557 signal_emit("setup reread", 1, mainconfig->fname);
561 int settings_save(const char *fname, int autosave)
567 fname = mainconfig->fname;
569 error = config_write(mainconfig, fname, 0660) != 0;
570 irssi_config_save_state(fname);
571 config_last_modifycounter = mainconfig->modifycounter;
573 str = g_strdup_printf("Couldn't save configuration file: %s",
574 config_last_error(mainconfig));
575 signal_emit("gui dialog", 2, "error", str);
578 signal_emit("setup saved", 2, fname, GINT_TO_POINTER(autosave));
582 static int sig_autosave(void)
586 if (!settings_get_bool("settings_autosave") ||
587 config_last_modifycounter == mainconfig->modifycounter)
590 if (!irssi_config_is_changed(NULL))
591 settings_save(NULL, TRUE);
593 fname = g_strconcat(mainconfig->fname, ".autosave", NULL);
594 str = g_strdup_printf("Configuration file was modified "
595 "while irssi was running. Saving "
596 "configuration to file '%s' instead. "
597 "Use /SAVE or /RELOAD to get rid of "
598 "this message.", fname);
599 signal_emit("gui dialog", 2, "warning", str);
602 settings_save(fname, TRUE);
609 void settings_init(void)
611 settings = g_hash_table_new((GHashFunc) g_istr_hash,
612 (GCompareFunc) g_istr_equal);
615 last_invalid_modules = NULL;
616 fe_initialized = FALSE;
617 config_changed = FALSE;
619 config_last_mtime = 0;
620 config_last_modifycounter = 0;
623 settings_add_bool("misc", "settings_autosave", TRUE);
624 timeout_tag = g_timeout_add(1000*60*60, (GSourceFunc) sig_autosave, NULL);
625 signal_add("irssi init finished", (SIGNAL_FUNC) sig_init_finished);
626 signal_add("gui exit", (SIGNAL_FUNC) sig_autosave);
629 static void settings_hash_free(const char *key, SETTINGS_REC *rec)
631 settings_destroy(rec);
634 void settings_deinit(void)
636 g_source_remove(timeout_tag);
637 signal_remove("irssi init finished", (SIGNAL_FUNC) sig_init_finished);
638 signal_remove("gui exit", (SIGNAL_FUNC) sig_autosave);
640 g_slist_foreach(last_invalid_modules, (GFunc) g_free, NULL);
641 g_slist_free(last_invalid_modules);
643 g_hash_table_foreach(settings, (GHFunc) settings_hash_free, NULL);
644 g_hash_table_destroy(settings);
646 if (mainconfig != NULL) config_close(mainconfig);