d99757e0a6f0c69ede91359e87b040b0fff1a110
[silc.git] / apps / irssi / src / core / settings.c
1 /*
2  settings.c : Irssi settings
3
4     Copyright (C) 1999 Timo Sirainen
5
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.
10
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.
15
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
19 */
20
21 #include "module.h"
22 #include "signals.h"
23 #include "commands.h"
24 #include "misc.h"
25
26 #include "lib-config/iconfig.h"
27 #include "settings.h"
28 #include "default-config.h"
29
30 #include <signal.h>
31
32 CONFIG_REC *mainconfig;
33
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) */
38
39 static GHashTable *settings;
40 static int timeout_tag;
41
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;
46
47 static SETTINGS_REC *settings_find(const char *key)
48 {
49         SETTINGS_REC *rec;
50
51         g_return_val_if_fail(key != NULL, NULL);
52
53         rec = g_hash_table_lookup(settings, key);
54         if (rec == NULL) {
55                 g_warning("settings_get_default_str(%s) : "
56                           "unknown setting", key);
57                 return NULL;
58         }
59
60         return rec;
61 }
62
63 const char *settings_get_str(const char *key)
64 {
65         SETTINGS_REC *rec;
66         CONFIG_NODE *setnode, *node;
67
68         rec = settings_find(key);
69         g_return_val_if_fail(rec != NULL, NULL);
70
71         setnode = iconfig_node_traverse("settings", FALSE);
72         if (setnode == NULL)
73                 return rec->def;
74
75         node = config_node_section(setnode, rec->module, -1);
76         return node == NULL ? rec->def :
77                 config_node_get_str(node, key, rec->def);
78 }
79
80 int settings_get_int(const char *key)
81 {
82         SETTINGS_REC *rec;
83         CONFIG_NODE *setnode, *node;
84         int def;
85
86         rec = settings_find(key);
87         g_return_val_if_fail(rec != NULL, 0);
88         def = GPOINTER_TO_INT(rec->def);
89
90         setnode = iconfig_node_traverse("settings", FALSE);
91         if (setnode == NULL)
92                 return def;
93
94         node = config_node_section(setnode, rec->module, -1);
95         return node == NULL ? def :
96                 config_node_get_int(node, key, def);
97 }
98
99 int settings_get_bool(const char *key)
100 {
101         SETTINGS_REC *rec;
102         CONFIG_NODE *setnode, *node;
103         int def;
104
105         rec = settings_find(key);
106         g_return_val_if_fail(rec != NULL, 0);
107         def = GPOINTER_TO_INT(rec->def);
108
109         setnode = iconfig_node_traverse("settings", FALSE);
110         if (setnode == NULL)
111                 return def;
112
113         node = config_node_section(setnode, rec->module, -1);
114         return node == NULL ? def :
115                 config_node_get_bool(node, key, def);
116 }
117
118 void settings_add_str_module(const char *module, const char *section,
119                              const char *key, const char *def)
120 {
121         SETTINGS_REC *rec;
122
123         g_return_if_fail(key != NULL);
124         g_return_if_fail(section != NULL);
125
126         rec = g_hash_table_lookup(settings, key);
127         g_return_if_fail(rec == NULL);
128
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);
134
135         g_hash_table_insert(settings, rec->key, rec);
136 }
137
138 void settings_add_int_module(const char *module, const char *section,
139                              const char *key, int def)
140 {
141         SETTINGS_REC *rec;
142
143         g_return_if_fail(key != NULL);
144         g_return_if_fail(section != NULL);
145
146         rec = g_hash_table_lookup(settings, key);
147         g_return_if_fail(rec == NULL);
148
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);
155
156         g_hash_table_insert(settings, rec->key, rec);
157 }
158
159 void settings_add_bool_module(const char *module, const char *section,
160                               const char *key, int def)
161 {
162         SETTINGS_REC *rec;
163
164         g_return_if_fail(key != NULL);
165         g_return_if_fail(section != NULL);
166
167         rec = g_hash_table_lookup(settings, key);
168         g_return_if_fail(rec == NULL);
169
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);
176
177         g_hash_table_insert(settings, rec->key, rec);
178 }
179
180 static void settings_destroy(SETTINGS_REC *rec)
181 {
182         if (rec->type == SETTING_TYPE_STRING)
183                 g_free_not_null(rec->def);
184         g_free(rec->module);
185         g_free(rec->section);
186         g_free(rec->key);
187         g_free(rec);
188 }
189
190 void settings_remove(const char *key)
191 {
192         SETTINGS_REC *rec;
193
194         g_return_if_fail(key != NULL);
195
196         rec = g_hash_table_lookup(settings, key);
197         if (rec == NULL) return;
198
199         g_hash_table_remove(settings, key);
200         settings_destroy(rec);
201 }
202
203 static int settings_remove_hash(const char *key, SETTINGS_REC *rec,
204                                 const char *module)
205 {
206         if (strcmp(rec->module, module) == 0) {
207                 settings_destroy(rec);
208                 return TRUE;
209         }
210
211         return FALSE;
212 }
213
214 void settings_remove_module(const char *module)
215 {
216         g_hash_table_foreach_remove(settings,
217                                     (GHRFunc) settings_remove_hash,
218                                     (void *) module);
219 }
220
221 static CONFIG_NODE *settings_get_node(const char *key)
222 {
223         SETTINGS_REC *rec;
224         CONFIG_NODE *node;
225
226         g_return_val_if_fail(key != NULL, NULL);
227
228         rec = g_hash_table_lookup(settings, key);
229         g_return_val_if_fail(rec != NULL, NULL);
230
231         node = iconfig_node_traverse("settings", TRUE);
232         return config_node_section(node, rec->module, NODE_TYPE_BLOCK);
233 }
234
235 void settings_set_str(const char *key, const char *value)
236 {
237         iconfig_node_set_str(settings_get_node(key), key, value);
238 }
239
240 void settings_set_int(const char *key, int value)
241 {
242         iconfig_node_set_int(settings_get_node(key), key, value);
243 }
244
245 void settings_set_bool(const char *key, int value)
246 {
247         iconfig_node_set_bool(settings_get_node(key), key, value);
248 }
249
250 int settings_get_type(const char *key)
251 {
252         SETTINGS_REC *rec;
253
254         g_return_val_if_fail(key != NULL, -1);
255
256         rec = g_hash_table_lookup(settings, key);
257         return rec == NULL ? -1 : rec->type;
258 }
259
260 /* Get the record of the setting */
261 SETTINGS_REC *settings_get_record(const char *key)
262 {
263         g_return_val_if_fail(key != NULL, NULL);
264
265         return g_hash_table_lookup(settings, key);
266 }
267
268 static void sig_init_finished(void)
269 {
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);
274         }
275
276         if (config_changed) {
277                 /* some backwards compatibility changes were made to
278                    config file, reload it */
279                 signal_emit("setup changed", 0);
280         }
281 }
282
283 static void settings_clean_invalid_module(const char *module)
284 {
285         CONFIG_NODE *node;
286         SETTINGS_REC *set;
287         GSList *tmp, *next;
288
289         node = iconfig_node_traverse("settings", FALSE);
290         if (node == NULL) return;
291
292         node = config_node_section(node, module, -1);
293         if (node == NULL) return;
294
295         for (tmp = config_node_first(node->value); tmp != NULL; tmp = next) {
296                 CONFIG_NODE *subnode = tmp->data;
297                 next = config_node_next(tmp);
298
299                 set = g_hash_table_lookup(settings, subnode->key);
300                 if (set == NULL || strcmp(set->module, module) != 0)
301                         iconfig_node_remove(node, subnode);
302         }
303 }
304
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)
308 {
309         while (last_invalid_modules != NULL) {
310                 char *module = last_invalid_modules->data;
311
312                 settings_clean_invalid_module(module);
313
314                 g_free(module);
315                 last_invalid_modules =
316                         g_slist_remove(last_invalid_modules, module);
317         }
318 }
319
320 /* verify that all settings in config file for `module' are actually found
321    from /SET list */
322 void settings_check_module(const char *module)
323 {
324         SETTINGS_REC *set;
325         CONFIG_NODE *node;
326         GString *errors;
327         GSList *tmp;
328         int count;
329
330         g_return_if_fail(module != NULL);
331
332         node = iconfig_node_traverse("settings", FALSE);
333         node = node == NULL ? NULL : config_node_section(node, module, -1);
334         if (node == NULL) return;
335
336         errors = g_string_new(NULL);
337         g_string_sprintf(errors, "Unknown settings in configuration "
338                          "file for module %s:", module);
339
340         count = 0;
341         tmp = config_node_first(node->value);
342         for (; tmp != NULL; tmp = config_node_next(tmp)) {
343                 node = tmp->data;
344
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);
348                         count++;
349                 }
350         }
351         if (count > 0) {
352                 if (gslist_find_icase_string(last_invalid_modules,
353                                              module) == NULL) {
354                         /* mark this module having invalid settings */
355                         last_invalid_modules =
356                                 g_slist_append(last_invalid_modules,
357                                                g_strdup(module));
358                 }
359                 if (fe_initialized)
360                         signal_emit("settings errors", 1, errors->str);
361                 else {
362                         if (last_errors == NULL)
363                                 last_errors = g_string_new(NULL);
364                         else
365                                 g_string_append_c(last_errors, '\n');
366                         g_string_append(last_errors, errors->str);
367                 }
368         }
369         g_string_free(errors, TRUE);
370 }
371
372 static int settings_compare(SETTINGS_REC *v1, SETTINGS_REC *v2)
373 {
374         return strcmp(v1->section, v2->section);
375 }
376
377 static void settings_hash_get(const char *key, SETTINGS_REC *rec,
378                               GSList **list)
379 {
380         *list = g_slist_insert_sorted(*list, rec,
381                                       (GCompareFunc) settings_compare);
382 }
383
384 GSList *settings_get_sorted(void)
385 {
386         GSList *list;
387
388         list = NULL;
389         g_hash_table_foreach(settings, (GHFunc) settings_hash_get, &list);
390         return list;
391 }
392
393 void sig_term(int n)
394 {
395         /* if we get SIGTERM after this, just die instead of coming back here. */
396         signal(SIGTERM, SIG_DFL);
397
398         /* quit from all servers too.. */
399         signal_emit("command quit", 1, "");
400
401         /* and die */
402         raise(SIGTERM);
403 }
404
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)
408 {
409         char buf[512];
410         int f, ret, n;
411         unsigned int checksum = 0;
412
413         f = open(fname, O_RDONLY);
414         if (f == -1) return 0;
415
416         n = 0;
417         while ((ret = read(f, buf, sizeof(buf))) > 0) {
418                 while (ret-- > 0)
419                         checksum += buf[ret] << ((n++ & 3)*8);
420         }
421         close(f);
422         return checksum;
423 }
424
425 static void irssi_config_save_state(const char *fname)
426 {
427         struct stat statbuf;
428
429         g_return_if_fail(fname != NULL);
430
431         if (stat(fname, &statbuf) != 0)
432                 return;
433
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);
438 }
439
440 int irssi_config_is_changed(const char *fname)
441 {
442         struct stat statbuf;
443
444         if (fname == NULL)
445                 fname = mainconfig->fname;
446
447         if (stat(fname, &statbuf) != 0)
448                 return FALSE;
449
450         return config_last_mtime != statbuf.st_mtime &&
451                 (config_last_size != statbuf.st_size ||
452                  config_last_checksum != file_checksum(fname));
453 }
454
455 static CONFIG_REC *parse_configfile(const char *fname)
456 {
457         CONFIG_REC *config;
458         struct stat statbuf;
459         const char *path;
460         char *str;
461
462         if (fname == NULL)
463                 fname = get_irssi_config();
464
465         if (stat(fname, &statbuf) == 0)
466                 path = fname;
467         else {
468                 /* user configuration file not found, use the default one
469                    from sysconfdir */
470                 path = SYSCONFDIR"/"IRSSI_GLOBAL_CONFIG;
471                 if (stat(path, &statbuf) != 0) {
472                         /* no configuration file in sysconfdir ..
473                            use the build-in configuration */
474                         path = NULL;
475                 }
476         }
477
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);
483                 g_free(str);
484
485                 config = config_open(NULL, -1);
486         }
487
488         if (path != NULL)
489                 config_parse(config);
490         else
491                 config_parse_data(config, default_config, "internal");
492
493         config_change_file_name(config, fname, 0660);
494         irssi_config_save_state(fname);
495         return config;
496 }
497
498 static void init_configfile(void)
499 {
500         struct stat statbuf;
501         char *str;
502
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());
507                 }
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());
512         }
513
514         mainconfig = parse_configfile(NULL);
515         config_last_modifycounter = mainconfig->modifycounter;
516
517         /* any errors? */
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);
522                 g_free(str);
523         }
524
525         signal(SIGTERM, sig_term);
526 }
527
528 int settings_reread(const char *fname)
529 {
530         CONFIG_REC *tempconfig;
531         char *str;
532
533         str = fname == NULL ? NULL : convert_home(fname);
534         tempconfig = parse_configfile(str);
535         g_free_not_null(str);
536
537         if (tempconfig == NULL) {
538                 signal_emit("gui dialog", 2, "error", g_strerror(errno));
539                 return FALSE;
540         }
541
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);
546                 g_free(str);
547
548                 config_close(tempconfig);
549                 return FALSE;
550         }
551
552         config_close(mainconfig);
553         mainconfig = tempconfig;
554         config_last_modifycounter = mainconfig->modifycounter;
555
556         signal_emit("setup changed", 0);
557         signal_emit("setup reread", 1, mainconfig->fname);
558         return TRUE;
559 }
560
561 int settings_save(const char *fname, int autosave)
562 {
563         char *str;
564         int error;
565
566         if (fname == NULL)
567                 fname = mainconfig->fname;
568
569         error = config_write(mainconfig, fname, 0660) != 0;
570         irssi_config_save_state(fname);
571         config_last_modifycounter = mainconfig->modifycounter;
572         if (error) {
573                 str = g_strdup_printf("Couldn't save configuration file: %s",
574                                       config_last_error(mainconfig));
575                 signal_emit("gui dialog", 2, "error", str);
576                 g_free(str);
577         }
578         signal_emit("setup saved", 2, fname, GINT_TO_POINTER(autosave));
579         return !error;
580 }
581
582 static int sig_autosave(void)
583 {
584         char *fname, *str;
585
586         if (!settings_get_bool("settings_autosave") ||
587             config_last_modifycounter == mainconfig->modifycounter)
588                 return 1;
589
590         if (!irssi_config_is_changed(NULL))
591                 settings_save(NULL, TRUE);
592         else {
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);
600                 g_free(str);
601
602                 settings_save(fname, TRUE);
603                 g_free(fname);
604         }
605
606         return 1;
607 }
608
609 void settings_init(void)
610 {
611         settings = g_hash_table_new((GHashFunc) g_istr_hash,
612                                     (GCompareFunc) g_istr_equal);
613
614         last_errors = NULL;
615         last_invalid_modules = NULL;
616         fe_initialized = FALSE;
617         config_changed = FALSE;
618
619         config_last_mtime = 0;
620         config_last_modifycounter = 0;
621         init_configfile();
622
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);
627 }
628
629 static void settings_hash_free(const char *key, SETTINGS_REC *rec)
630 {
631         settings_destroy(rec);
632 }
633
634 void settings_deinit(void)
635 {
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);
639
640         g_slist_foreach(last_invalid_modules, (GFunc) g_free, NULL);
641         g_slist_free(last_invalid_modules);
642
643         g_hash_table_foreach(settings, (GHFunc) settings_hash_free, NULL);
644         g_hash_table_destroy(settings);
645
646         if (mainconfig != NULL) config_close(mainconfig);
647 }