addition of silc.css
[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 char *last_config_error_msg;
36 static GSList *last_invalid_modules;
37 static int fe_initialized;
38 static int config_changed; /* FIXME: remove after .98 (unless needed again) */
39
40 static GHashTable *settings;
41 static int timeout_tag;
42
43 static int config_last_modifycounter;
44 static time_t config_last_mtime;
45 static long config_last_size;
46 static unsigned int config_last_checksum;
47
48 static SETTINGS_REC *settings_find(const char *key)
49 {
50         SETTINGS_REC *rec;
51
52         g_return_val_if_fail(key != NULL, NULL);
53
54         rec = g_hash_table_lookup(settings, key);
55         if (rec == NULL) {
56                 g_warning("settings_get_default_str(%s) : "
57                           "unknown setting", key);
58                 return NULL;
59         }
60
61         return rec;
62 }
63
64 const char *settings_get_str(const char *key)
65 {
66         SETTINGS_REC *rec;
67         CONFIG_NODE *setnode, *node;
68
69         rec = settings_find(key);
70         g_return_val_if_fail(rec != NULL, NULL);
71
72         setnode = iconfig_node_traverse("settings", FALSE);
73         if (setnode == NULL)
74                 return rec->def;
75
76         node = config_node_section(setnode, rec->module, -1);
77         return node == NULL ? rec->def :
78                 config_node_get_str(node, key, rec->def);
79 }
80
81 int settings_get_int(const char *key)
82 {
83         SETTINGS_REC *rec;
84         CONFIG_NODE *setnode, *node;
85         int def;
86
87         rec = settings_find(key);
88         g_return_val_if_fail(rec != NULL, 0);
89         def = GPOINTER_TO_INT(rec->def);
90
91         setnode = iconfig_node_traverse("settings", FALSE);
92         if (setnode == NULL)
93                 return def;
94
95         node = config_node_section(setnode, rec->module, -1);
96         return node == NULL ? def :
97                 config_node_get_int(node, key, def);
98 }
99
100 int settings_get_bool(const char *key)
101 {
102         SETTINGS_REC *rec;
103         CONFIG_NODE *setnode, *node;
104         int def;
105
106         rec = settings_find(key);
107         g_return_val_if_fail(rec != NULL, 0);
108         def = GPOINTER_TO_INT(rec->def);
109
110         setnode = iconfig_node_traverse("settings", FALSE);
111         if (setnode == NULL)
112                 return def;
113
114         node = config_node_section(setnode, rec->module, -1);
115         return node == NULL ? def :
116                 config_node_get_bool(node, key, def);
117 }
118
119 void settings_add_str_module(const char *module, const char *section,
120                              const char *key, const char *def)
121 {
122         SETTINGS_REC *rec;
123
124         g_return_if_fail(key != NULL);
125         g_return_if_fail(section != NULL);
126
127         rec = g_hash_table_lookup(settings, key);
128         g_return_if_fail(rec == NULL);
129
130         rec = g_new0(SETTINGS_REC, 1);
131         rec->module = g_strdup(module);
132         rec->key = g_strdup(key);
133         rec->section = g_strdup(section);
134         rec->def = def == NULL ? NULL : g_strdup(def);
135
136         g_hash_table_insert(settings, rec->key, rec);
137 }
138
139 void settings_add_int_module(const char *module, const char *section,
140                              const char *key, int def)
141 {
142         SETTINGS_REC *rec;
143
144         g_return_if_fail(key != NULL);
145         g_return_if_fail(section != NULL);
146
147         rec = g_hash_table_lookup(settings, key);
148         g_return_if_fail(rec == NULL);
149
150         rec = g_new0(SETTINGS_REC, 1);
151         rec->module = g_strdup(module);
152         rec->type = SETTING_TYPE_INT;
153         rec->key = g_strdup(key);
154         rec->section = g_strdup(section);
155         rec->def = GINT_TO_POINTER(def);
156
157         g_hash_table_insert(settings, rec->key, rec);
158 }
159
160 void settings_add_bool_module(const char *module, const char *section,
161                               const char *key, int def)
162 {
163         SETTINGS_REC *rec;
164
165         g_return_if_fail(key != NULL);
166         g_return_if_fail(section != NULL);
167
168         rec = g_hash_table_lookup(settings, key);
169         g_return_if_fail(rec == NULL);
170
171         rec = g_new0(SETTINGS_REC, 1);
172         rec->module = g_strdup(module);
173         rec->type = SETTING_TYPE_BOOLEAN;
174         rec->key = g_strdup(key);
175         rec->section = g_strdup(section);
176         rec->def = GINT_TO_POINTER(def);
177
178         g_hash_table_insert(settings, rec->key, rec);
179 }
180
181 static void settings_destroy(SETTINGS_REC *rec)
182 {
183         if (rec->type == SETTING_TYPE_STRING)
184                 g_free_not_null(rec->def);
185         g_free(rec->module);
186         g_free(rec->section);
187         g_free(rec->key);
188         g_free(rec);
189 }
190
191 void settings_remove(const char *key)
192 {
193         SETTINGS_REC *rec;
194
195         g_return_if_fail(key != NULL);
196
197         rec = g_hash_table_lookup(settings, key);
198         if (rec == NULL) return;
199
200         g_hash_table_remove(settings, key);
201         settings_destroy(rec);
202 }
203
204 static int settings_remove_hash(const char *key, SETTINGS_REC *rec,
205                                 const char *module)
206 {
207         if (strcmp(rec->module, module) == 0) {
208                 settings_destroy(rec);
209                 return TRUE;
210         }
211
212         return FALSE;
213 }
214
215 void settings_remove_module(const char *module)
216 {
217         g_hash_table_foreach_remove(settings,
218                                     (GHRFunc) settings_remove_hash,
219                                     (void *) module);
220 }
221
222 static CONFIG_NODE *settings_get_node(const char *key)
223 {
224         SETTINGS_REC *rec;
225         CONFIG_NODE *node;
226
227         g_return_val_if_fail(key != NULL, NULL);
228
229         rec = g_hash_table_lookup(settings, key);
230         g_return_val_if_fail(rec != NULL, NULL);
231
232         node = iconfig_node_traverse("settings", TRUE);
233         return config_node_section(node, rec->module, NODE_TYPE_BLOCK);
234 }
235
236 void settings_set_str(const char *key, const char *value)
237 {
238         iconfig_node_set_str(settings_get_node(key), key, value);
239 }
240
241 void settings_set_int(const char *key, int value)
242 {
243         iconfig_node_set_int(settings_get_node(key), key, value);
244 }
245
246 void settings_set_bool(const char *key, int value)
247 {
248         iconfig_node_set_bool(settings_get_node(key), key, value);
249 }
250
251 int settings_get_type(const char *key)
252 {
253         SETTINGS_REC *rec;
254
255         g_return_val_if_fail(key != NULL, -1);
256
257         rec = g_hash_table_lookup(settings, key);
258         return rec == NULL ? -1 : rec->type;
259 }
260
261 /* Get the record of the setting */
262 SETTINGS_REC *settings_get_record(const char *key)
263 {
264         g_return_val_if_fail(key != NULL, NULL);
265
266         return g_hash_table_lookup(settings, key);
267 }
268
269 static void sig_init_finished(void)
270 {
271         fe_initialized = TRUE;
272         if (last_errors != NULL) {
273                 signal_emit("settings errors", 1, last_errors->str);
274                 g_string_free(last_errors, TRUE);
275         }
276
277         if (last_config_error_msg != NULL) {
278                 signal_emit("gui dialog", 2, "error", last_config_error_msg);
279                 g_free_and_null(last_config_error_msg);
280         }
281
282         if (config_changed) {
283                 /* some backwards compatibility changes were made to
284                    config file, reload it */
285                 signal_emit("setup changed", 0);
286         }
287 }
288
289 /* FIXME: remove after 0.7.98 - only for backward compatibility */
290 static void settings_move(SETTINGS_REC *rec, char *value)
291 {
292         CONFIG_NODE *setnode, *node;
293
294         setnode = iconfig_node_traverse("settings", TRUE);
295         node = config_node_section(setnode, rec->module, NODE_TYPE_BLOCK);
296
297         iconfig_node_set_str(node, rec->key, value);
298         iconfig_node_set_str(setnode, rec->key, NULL);
299
300         config_changed = TRUE;
301 }
302
303 static void settings_clean_invalid_module(const char *module)
304 {
305         CONFIG_NODE *node;
306         SETTINGS_REC *set;
307         GSList *tmp, *next;
308
309         node = iconfig_node_traverse("settings", FALSE);
310         if (node == NULL) return;
311
312         node = config_node_section(node, module, -1);
313         if (node == NULL) return;
314
315         for (tmp = node->value; tmp != NULL; tmp = next) {
316                 CONFIG_NODE *subnode = tmp->data;
317                 next = tmp->next;
318
319                 set = g_hash_table_lookup(settings, subnode->key);
320                 if (set == NULL || strcmp(set->module, module) != 0)
321                         iconfig_node_remove(node, subnode);
322         }
323 }
324
325 /* remove all invalid settings from config file. works only with the
326    modules that have already called settings_check() */
327 void settings_clean_invalid(void)
328 {
329         while (last_invalid_modules != NULL) {
330                 char *module = last_invalid_modules->data;
331
332                 settings_clean_invalid_module(module);
333
334                 g_free(module);
335                 last_invalid_modules =
336                         g_slist_remove(last_invalid_modules, module);
337         }
338 }
339
340 /* verify that all settings in config file for `module' are actually found
341    from /SET list */
342 void settings_check_module(const char *module)
343 {
344         SETTINGS_REC *set;
345         CONFIG_NODE *node;
346         GString *errors;
347         GSList *tmp, *next;
348         int count;
349
350         g_return_if_fail(module != NULL);
351
352         node = iconfig_node_traverse("settings", FALSE);
353         if (node != NULL) {
354                 /* FIXME: remove after 0.7.98 */
355                 for (tmp = node->value; tmp != NULL; tmp = next) {
356                         CONFIG_NODE *node = tmp->data;
357
358                         next = tmp->next;
359                         if (node->type != NODE_TYPE_KEY)
360                                 continue;
361                         set = g_hash_table_lookup(settings, node->key);
362                         if (set != NULL)
363                                 settings_move(set, node->value);
364                 }
365         }
366         node = node == NULL ? NULL : config_node_section(node, module, -1);
367         if (node == NULL) return;
368
369         errors = g_string_new(NULL);
370         g_string_sprintf(errors, "Unknown settings in configuration "
371                          "file for module %s:", module);
372
373         count = 0;
374         for (tmp = node->value; tmp != NULL; tmp = tmp->next) {
375                 node = tmp->data;
376
377                 set = g_hash_table_lookup(settings, node->key);
378                 if (set == NULL || strcmp(set->module, module) != 0) {
379                         g_string_sprintfa(errors, " %s", node->key);
380                         count++;
381                 }
382         }
383         if (count > 0) {
384                 if (gslist_find_icase_string(last_invalid_modules,
385                                              module) == NULL) {
386                         /* mark this module having invalid settings */
387                         last_invalid_modules =
388                                 g_slist_append(last_invalid_modules,
389                                                g_strdup(module));
390                 }
391                 if (fe_initialized)
392                         signal_emit("settings errors", 1, errors->str);
393                 else {
394                         if (last_errors == NULL)
395                                 last_errors = g_string_new(NULL);
396                         else
397                                 g_string_append_c(last_errors, '\n');
398                         g_string_append(last_errors, errors->str);
399                 }
400         }
401         g_string_free(errors, TRUE);
402 }
403
404 static int settings_compare(SETTINGS_REC *v1, SETTINGS_REC *v2)
405 {
406         return strcmp(v1->section, v2->section);
407 }
408
409 static void settings_hash_get(const char *key, SETTINGS_REC *rec,
410                               GSList **list)
411 {
412         *list = g_slist_insert_sorted(*list, rec,
413                                       (GCompareFunc) settings_compare);
414 }
415
416 GSList *settings_get_sorted(void)
417 {
418         GSList *list;
419
420         list = NULL;
421         g_hash_table_foreach(settings, (GHFunc) settings_hash_get, &list);
422         return list;
423 }
424
425 void sig_term(int n)
426 {
427         /* if we get SIGTERM after this, just die instead of coming back here. */
428         signal(SIGTERM, SIG_DFL);
429
430         /* quit from all servers too.. */
431         signal_emit("command quit", 1, "");
432
433         /* and die */
434         raise(SIGTERM);
435 }
436
437 /* Yes, this is my own stupid checksum generator, some "real" algorithm
438    would be nice but would just take more space without much real benefit */
439 static unsigned int file_checksum(const char *fname)
440 {
441         char buf[512];
442         int f, ret, n;
443         unsigned int checksum = 0;
444
445         f = open(fname, O_RDONLY);
446         if (f == -1) return 0;
447
448         n = 0;
449         while ((ret = read(f, buf, sizeof(buf))) > 0) {
450                 while (ret-- > 0)
451                         checksum += buf[ret] << ((n++ & 3)*8);
452         }
453         close(f);
454         return checksum;
455 }
456
457 static void irssi_config_save_state(const char *fname)
458 {
459         struct stat statbuf;
460
461         g_return_if_fail(fname != NULL);
462
463         if (stat(fname, &statbuf) != 0)
464                 return;
465
466         /* save modify time, file size and checksum */
467         config_last_mtime = statbuf.st_mtime;
468         config_last_size = statbuf.st_size;
469         config_last_checksum = file_checksum(fname);
470 }
471
472 int irssi_config_is_changed(const char *fname)
473 {
474         struct stat statbuf;
475
476         if (fname == NULL)
477                 fname = mainconfig->fname;
478
479         if (stat(fname, &statbuf) != 0)
480                 return FALSE;
481
482         return config_last_mtime != statbuf.st_mtime &&
483                 (config_last_size != statbuf.st_size ||
484                  config_last_checksum != file_checksum(fname));
485 }
486
487 static CONFIG_REC *parse_configfile(const char *fname)
488 {
489         CONFIG_REC *config;
490         struct stat statbuf;
491         const char *path;
492         char *real_fname;
493
494         real_fname = fname != NULL ? g_strdup(fname) :
495                 g_strdup_printf("%s"G_DIR_SEPARATOR_S".silc"
496                                 G_DIR_SEPARATOR_S"config", g_get_home_dir());
497
498         if (stat(real_fname, &statbuf) == 0)
499                 path = real_fname;
500         else {
501                 /* user configuration file not found, use the default one
502                    from sysconfdir */
503                 path = SYSCONFDIR"/irssi/config";
504                 if (stat(path, &statbuf) != 0) {
505                         /* no configuration file in sysconfdir ..
506                            use the build-in configuration */
507                         path = NULL;
508                 }
509         }
510
511         config = config_open(path, -1);
512         if (config == NULL) {
513                 last_config_error_msg =
514                         g_strdup_printf("Error opening configuration file %s: %s",
515                                         path, g_strerror(errno));
516                 config = config_open(NULL, -1);
517         }
518
519         if (path != NULL)
520                 config_parse(config);
521         else
522                 config_parse_data(config, default_config, "internal");
523
524         config_change_file_name(config, real_fname, 0660);
525         irssi_config_save_state(real_fname);
526         g_free(real_fname);
527         return config;
528 }
529
530 static void init_configfile(void)
531 {
532         struct stat statbuf;
533         char *str;
534
535         str = g_strdup_printf("%s"G_DIR_SEPARATOR_S".silc", g_get_home_dir());
536         if (stat(str, &statbuf) != 0) {
537                 /* ~/.irssi not found, create it. */
538                 if (mkpath(str, 0700) != 0) {
539                         g_error("Couldn't create %s directory", str);
540                 }
541         } else if (!S_ISDIR(statbuf.st_mode)) {
542                 g_error("%s is not a directory.\n"
543                         "You should remove it with command: rm ~/.irssi", str);
544         }
545         g_free(str);
546
547         mainconfig = parse_configfile(NULL);
548         config_last_modifycounter = mainconfig->modifycounter;
549
550         /* any errors? */
551         if (config_last_error(mainconfig) != NULL) {
552                 last_config_error_msg =
553                         g_strdup_printf("Ignored errors in configuration "
554                                         "file:\n%s",
555                                         config_last_error(mainconfig));
556         }
557
558         signal(SIGTERM, sig_term);
559 }
560
561 int settings_reread(const char *fname)
562 {
563         CONFIG_REC *tempconfig;
564         char *str;
565
566         if (fname == NULL) fname = "~/.silc/config";
567
568         str = convert_home(fname);
569         tempconfig = parse_configfile(str);
570         g_free(str);
571
572         if (tempconfig == NULL) {
573                 signal_emit("gui dialog", 2, "error", g_strerror(errno));
574                 return FALSE;
575         }
576
577         if (config_last_error(tempconfig) != NULL) {
578                 str = g_strdup_printf("Errors in configuration file:\n%s",
579                                       config_last_error(tempconfig));
580                 signal_emit("gui dialog", 2, "error", str);
581                 g_free(str);
582
583                 config_close(tempconfig);
584                 return FALSE;
585         }
586
587         config_close(mainconfig);
588         mainconfig = tempconfig;
589         config_last_modifycounter = mainconfig->modifycounter;
590
591         signal_emit("setup changed", 0);
592         signal_emit("setup reread", 0);
593         return TRUE;
594 }
595
596 int settings_save(const char *fname)
597 {
598         char *str;
599         int error;
600
601         if (fname == NULL)
602                 fname = mainconfig->fname;
603
604         error = config_write(mainconfig, fname, 0660) != 0;
605         irssi_config_save_state(fname);
606         config_last_modifycounter = mainconfig->modifycounter;
607         if (error) {
608                 str = g_strdup_printf("Couldn't save configuration file: %s",
609                                       config_last_error(mainconfig));
610                 signal_emit("gui dialog", 2, "error", str);
611                 g_free(str);
612         }
613         return !error;
614 }
615
616 static void sig_autosave(void)
617 {
618         char *fname, *str;
619
620         if (!settings_get_bool("settings_autosave") ||
621             config_last_modifycounter == mainconfig->modifycounter)
622                 return;
623
624         if (!irssi_config_is_changed(NULL))
625                 settings_save(NULL);
626         else {
627                 fname = g_strconcat(mainconfig->fname, ".autosave", NULL);
628                 str = g_strdup_printf("Configuration file was modified "
629                                       "while irssi was running. Saving "
630                                       "configuration to file '%s' instead. "
631                                       "Use /SAVE or /RELOAD to get rid of "
632                                       "this message.", fname);
633                 signal_emit("gui dialog", 2, "warning", str);
634                 g_free(str);
635
636                 settings_save(fname);
637                 g_free(fname);
638         }
639 }
640
641 void settings_init(void)
642 {
643         settings = g_hash_table_new((GHashFunc) g_str_hash,
644                                     (GCompareFunc) g_str_equal);
645
646         last_errors = NULL;
647         last_config_error_msg = NULL;
648         last_invalid_modules = NULL;
649         fe_initialized = FALSE;
650         config_changed = FALSE;
651
652         config_last_mtime = 0;
653         config_last_modifycounter = 0;
654         init_configfile();
655
656         settings_add_bool("misc", "settings_autosave", TRUE);
657         timeout_tag = g_timeout_add(1000*60*60, (GSourceFunc) sig_autosave, NULL);
658         signal_add("irssi init finished", (SIGNAL_FUNC) sig_init_finished);
659         signal_add("gui exit", (SIGNAL_FUNC) sig_autosave);
660 }
661
662 static void settings_hash_free(const char *key, SETTINGS_REC *rec)
663 {
664         settings_destroy(rec);
665 }
666
667 void settings_deinit(void)
668 {
669         g_source_remove(timeout_tag);
670         signal_remove("irssi init finished", (SIGNAL_FUNC) sig_init_finished);
671         signal_remove("gui exit", (SIGNAL_FUNC) sig_autosave);
672
673         g_slist_foreach(last_invalid_modules, (GFunc) g_free, NULL);
674         g_slist_free(last_invalid_modules);
675
676         g_hash_table_foreach(settings, (GHFunc) settings_hash_free, NULL);
677         g_hash_table_destroy(settings);
678
679         if (mainconfig != NULL) config_close(mainconfig);
680 }