Added SILC Thread Queue API
[crypto.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 "levels.h"
25 #include "misc.h"
26
27 #include "lib-config/iconfig.h"
28 #include "recode.h"
29 #include "settings.h"
30 #include "default-config.h"
31
32 #include <signal.h>
33
34 #define SETTINGS_AUTOSAVE_TIMEOUT (1000*60*60) /* 1 hour */
35
36 CONFIG_REC *mainconfig;
37
38 static GString *last_errors;
39 static GSList *last_invalid_modules;
40 static int fe_initialized;
41 static int config_changed; /* FIXME: remove after .98 (unless needed again) */
42
43 static GHashTable *settings;
44 static int timeout_tag;
45
46 static int config_last_modifycounter;
47 static time_t config_last_mtime;
48 static long config_last_size;
49 static unsigned int config_last_checksum;
50
51 static SETTINGS_REC *settings_find(const char *key)
52 {
53         SETTINGS_REC *rec;
54
55         g_return_val_if_fail(key != NULL, NULL);
56
57         rec = g_hash_table_lookup(settings, key);
58         if (rec == NULL) {
59                 g_warning("settings_get_default_str(%s) : "
60                           "unknown setting", key);
61                 return NULL;
62         }
63
64         return rec;
65 }
66
67 static SETTINGS_REC *settings_get(const char *key, SettingType type)
68 {
69         SETTINGS_REC *rec;
70
71         g_return_val_if_fail(key != NULL, NULL);
72
73         rec = settings_find(key);
74         if (rec == NULL) {
75                 g_warning("settings_get(%s) : not found", key);
76                 return NULL;
77         }
78         if (type != -1 && rec->type != type) {
79                 g_warning("settings_get(%s) : invalid type", key);
80                 return NULL;
81         }
82
83         return rec;
84 }
85
86 static const char *
87 settings_get_str_type(const char *key, SettingType type)
88 {
89         SETTINGS_REC *rec;
90         CONFIG_NODE *node;
91
92         rec = settings_get(key, type);
93         if (rec == NULL) return NULL;
94
95         node = iconfig_node_traverse("settings", FALSE);
96         node = node == NULL ? NULL : config_node_section(node, rec->module, -1);
97
98         return node == NULL ? rec->default_value.v_string :
99                 config_node_get_str(node, key, rec->default_value.v_string);
100 }
101
102 const char *settings_get_str(const char *key)
103 {
104         return settings_get_str_type(key, -1);
105 }
106
107 int settings_get_int(const char *key)
108 {
109         SETTINGS_REC *rec;
110         CONFIG_NODE *node;
111
112         rec = settings_get(key, SETTING_TYPE_INT);
113         if (rec == NULL) return 0;
114
115         node = iconfig_node_traverse("settings", FALSE);
116         node = node == NULL ? NULL : config_node_section(node, rec->module, -1);
117
118         return node == NULL ? rec->default_value.v_int :
119                 config_node_get_int(node, key, rec->default_value.v_int);
120 }
121
122 int settings_get_bool(const char *key)
123 {
124         SETTINGS_REC *rec;
125         CONFIG_NODE *node;
126
127         rec = settings_get(key, SETTING_TYPE_BOOLEAN);
128         if (rec == NULL) return FALSE;
129
130         node = iconfig_node_traverse("settings", FALSE);
131         node = node == NULL ? NULL : config_node_section(node, rec->module, -1);
132
133         return node == NULL ? rec->default_value.v_bool :
134                 config_node_get_bool(node, key, rec->default_value.v_bool);
135 }
136
137 int settings_get_time(const char *key)
138 {
139         const char *str;
140         int msecs;
141
142         str = settings_get_str_type(key, SETTING_TYPE_TIME);
143         if (str != NULL && !parse_time_interval(str, &msecs))
144                 g_warning("settings_get_time(%s) : Invalid time '%s'", key, str);
145         return str == NULL ? 0 : msecs;
146 }
147
148 int settings_get_level(const char *key)
149 {
150         const char *str;
151
152         str = settings_get_str_type(key, SETTING_TYPE_LEVEL);
153         return str == NULL ? 0 : level2bits(str);
154 }
155
156 int settings_get_size(const char *key)
157 {
158         const char *str;
159         int bytes;
160
161         str = settings_get_str_type(key, SETTING_TYPE_SIZE);
162         if (str != NULL && !parse_size(str, &bytes))
163                 g_warning("settings_get_size(%s) : Invalid size '%s'", key, str);
164         return str == NULL ? 0 : bytes;
165 }
166
167 static void settings_add(const char *module, const char *section,
168                          const char *key, SettingType type,
169                          const SettingValue *default_value)
170 {
171         SETTINGS_REC *rec;
172
173         g_return_if_fail(key != NULL);
174         g_return_if_fail(section != NULL);
175
176         rec = g_hash_table_lookup(settings, key);
177         if (rec != NULL) {
178                 /* Already exists, make sure it's correct type */
179                 if (rec->type != type) {
180                         g_warning("Trying to add already existing "
181                                   "setting '%s' with different type.", key);
182                         return;
183                 }
184         } else {
185                 rec = g_new(SETTINGS_REC, 1);
186                 rec->module = g_strdup(module);
187                 rec->key = g_strdup(key);
188                 rec->section = g_strdup(section);
189                 rec->type = type;
190
191                 rec->default_value = *default_value;
192                 g_hash_table_insert(settings, rec->key, rec);
193         }
194
195         rec->refcount++;
196 }
197
198 void settings_add_str_module(const char *module, const char *section,
199                              const char *key, const char *def)
200 {
201         SettingValue default_value;
202
203         memset(&default_value, 0, sizeof(default_value));
204         default_value.v_string = g_strdup(def);
205         settings_add(module, section, key, SETTING_TYPE_STRING, &default_value);
206 }
207
208 void settings_add_int_module(const char *module, const char *section,
209                              const char *key, int def)
210 {
211         SettingValue default_value;
212
213         memset(&default_value, 0, sizeof(default_value));
214         default_value.v_int = def;
215         settings_add(module, section, key, SETTING_TYPE_INT, &default_value);
216 }
217
218 void settings_add_bool_module(const char *module, const char *section,
219                               const char *key, int def)
220 {
221         SettingValue default_value;
222
223         memset(&default_value, 0, sizeof(default_value));
224         default_value.v_bool = def;
225         settings_add(module, section, key, SETTING_TYPE_BOOLEAN,
226                      &default_value);
227 }
228
229 void settings_add_time_module(const char *module, const char *section,
230                               const char *key, const char *def)
231 {
232         SettingValue default_value;
233
234         memset(&default_value, 0, sizeof(default_value));
235         default_value.v_string = g_strdup(def);
236         settings_add(module, section, key, SETTING_TYPE_TIME, &default_value);
237 }
238
239 void settings_add_level_module(const char *module, const char *section,
240                                const char *key, const char *def)
241 {
242         SettingValue default_value;
243
244         memset(&default_value, 0, sizeof(default_value));
245         default_value.v_string = g_strdup(def);
246         settings_add(module, section, key, SETTING_TYPE_LEVEL, &default_value);
247 }
248
249 void settings_add_size_module(const char *module, const char *section,
250                               const char *key, const char *def)
251 {
252         SettingValue default_value;
253
254         memset(&default_value, 0, sizeof(default_value));
255         default_value.v_string = g_strdup(def);
256         settings_add(module, section, key, SETTING_TYPE_SIZE, &default_value);
257 }
258
259 static void settings_destroy(SETTINGS_REC *rec)
260 {
261         if (rec->type != SETTING_TYPE_INT &&
262             rec->type != SETTING_TYPE_BOOLEAN)
263                 g_free(rec->default_value.v_string);
264         g_free(rec->module);
265         g_free(rec->section);
266         g_free(rec->key);
267         g_free(rec);
268 }
269
270 static void settings_unref(SETTINGS_REC *rec, int remove_hash)
271 {
272         if (--rec->refcount == 0) {
273                 if (remove_hash)
274                         g_hash_table_remove(settings, rec->key);
275                 settings_destroy(rec);
276         }
277 }
278
279 void settings_remove(const char *key)
280 {
281         SETTINGS_REC *rec;
282
283         g_return_if_fail(key != NULL);
284
285         rec = g_hash_table_lookup(settings, key);
286         if (rec != NULL)
287                 settings_unref(rec, TRUE);
288 }
289
290 static int settings_remove_hash(const char *key, SETTINGS_REC *rec,
291                                 const char *module)
292 {
293         if (strcmp(rec->module, module) == 0) {
294                 settings_unref(rec, FALSE);
295                 return TRUE;
296         }
297
298         return FALSE;
299 }
300
301 void settings_remove_module(const char *module)
302 {
303         g_hash_table_foreach_remove(settings,
304                                     (GHRFunc) settings_remove_hash,
305                                     (void *) module);
306 }
307
308 static CONFIG_NODE *settings_get_node(const char *key)
309 {
310         SETTINGS_REC *rec;
311         CONFIG_NODE *node;
312
313         g_return_val_if_fail(key != NULL, NULL);
314
315         rec = g_hash_table_lookup(settings, key);
316         if (rec == NULL) {
317                 g_warning("Changing unknown setting '%s'", key);
318                 return NULL;
319         }
320
321         node = iconfig_node_traverse("settings", TRUE);
322         return config_node_section(node, rec->module, NODE_TYPE_BLOCK);
323 }
324
325 void settings_set_str(const char *key, const char *value)
326 {
327         iconfig_node_set_str(settings_get_node(key), key, value);
328 }
329
330 void settings_set_int(const char *key, int value)
331 {
332         iconfig_node_set_int(settings_get_node(key), key, value);
333 }
334
335 void settings_set_bool(const char *key, int value)
336 {
337         iconfig_node_set_bool(settings_get_node(key), key, value);
338 }
339
340 int settings_set_time(const char *key, const char *value)
341 {
342         int msecs;
343
344         if (!parse_time_interval(value, &msecs))
345                 return FALSE;
346
347         iconfig_node_set_str(settings_get_node(key), key, value);
348         return TRUE;
349 }
350
351 int settings_set_level(const char *key, const char *value)
352 {
353         iconfig_node_set_str(settings_get_node(key), key, value);
354         return TRUE;
355 }
356
357 int settings_set_size(const char *key, const char *value)
358 {
359         int size;
360
361         if (!parse_size(value, &size))
362                 return FALSE;
363
364         iconfig_node_set_str(settings_get_node(key), key, value);
365         return TRUE;
366 }
367
368 SettingType settings_get_type(const char *key)
369 {
370         SETTINGS_REC *rec;
371
372         g_return_val_if_fail(key != NULL, -1);
373
374         rec = g_hash_table_lookup(settings, key);
375         return rec == NULL ? -1 : rec->type;
376 }
377
378 /* Get the record of the setting */
379 SETTINGS_REC *settings_get_record(const char *key)
380 {
381         g_return_val_if_fail(key != NULL, NULL);
382
383         return g_hash_table_lookup(settings, key);
384 }
385
386 static void sig_init_finished(void)
387 {
388         fe_initialized = TRUE;
389         if (last_errors != NULL) {
390                 signal_emit("settings errors", 1, last_errors->str);
391                 g_string_free(last_errors, TRUE);
392         }
393
394         if (config_changed) {
395                 /* some backwards compatibility changes were made to
396                    config file, reload it */
397                 g_warning("Some settings were automatically "
398                           "updated, please /SAVE");
399                 signal_emit("setup changed", 0);
400         }
401 }
402
403 static void settings_clean_invalid_module(const char *module)
404 {
405         CONFIG_NODE *node;
406         SETTINGS_REC *set;
407         GSList *tmp, *next;
408
409         node = iconfig_node_traverse("settings", FALSE);
410         if (node == NULL) return;
411
412         node = config_node_section(node, module, -1);
413         if (node == NULL) return;
414
415         for (tmp = config_node_first(node->value); tmp != NULL; tmp = next) {
416                 CONFIG_NODE *subnode = tmp->data;
417                 next = config_node_next(tmp);
418
419                 set = g_hash_table_lookup(settings, subnode->key);
420                 if (set == NULL || strcmp(set->module, module) != 0)
421                         iconfig_node_remove(node, subnode);
422         }
423 }
424
425 /* remove all invalid settings from config file. works only with the
426    modules that have already called settings_check() */
427 void settings_clean_invalid(void)
428 {
429         while (last_invalid_modules != NULL) {
430                 char *module = last_invalid_modules->data;
431
432                 settings_clean_invalid_module(module);
433
434                 last_invalid_modules =
435                         g_slist_remove(last_invalid_modules, module);
436                 g_free(module);
437         }
438 }
439
440 static int backwards_compatibility(const char *module, CONFIG_NODE *node,
441                                    CONFIG_NODE *parent)
442 {
443         const char *new_key, *new_module;
444         CONFIG_NODE *new_node;
445         char *new_value;
446         int old_value;
447
448         new_value = NULL; new_key = NULL; new_module = NULL;
449
450         /* fe-text term_type -> fe-common/core term_charset - for 0.8.10-> */
451         if (strcmp(module, "fe-text") == 0) {
452                 if (strcasecmp(node->key, "term_type") == 0 ||
453                     /* kludge for cvs-version where term_charset was in fe-text */
454                     strcasecmp(node->key, "term_charset") == 0) {
455                         new_module = "fe-common/core";
456                         new_key = "term_charset";
457                         new_value = !is_valid_charset(node->value) ? NULL :
458                                 g_strdup(node->value);
459                         new_node = iconfig_node_traverse("settings", FALSE);
460                         new_node = new_node == NULL ? NULL :
461                                 config_node_section(new_node, new_module, -1);
462
463                         config_node_set_str(mainconfig, new_node,
464                                             new_key, new_value);
465                         /* remove old */
466                         config_node_set_str(mainconfig, parent,
467                                             node->key, NULL);
468                         g_free(new_value);
469                         config_changed = TRUE;
470                         return new_key != NULL;
471                 }
472         }
473         new_value = NULL, new_key = NULL;
474         /* FIXME: remove later - for 0.8.6 -> */
475         if (node->value == NULL || !is_numeric(node->value, '\0'))
476                 return FALSE;
477
478         old_value = atoi(node->value);
479
480         if (strcmp(module, "fe-text") == 0) {
481                 if (strcasecmp(node->key, "lag_min_show") == 0)
482                         new_value = g_strdup_printf("%dms", old_value*10);
483                 else if (strcasecmp(node->key, "scrollback_hours") == 0) {
484                         new_value = g_strdup_printf("%dh", old_value);
485                         new_key = "scrollback_time";
486                 }
487         } else if (strcmp(module, "irc/core") == 0) {
488                 if (strcasecmp(node->key, "cmd_queue_speed") == 0)
489                         new_value = g_strdup_printf("%dms", old_value);
490         } else if (strcmp(module, "irc/dcc") == 0) {
491                 if (strcasecmp(node->key, "dcc_autoget_max_size") == 0)
492                         new_value = g_strdup_printf("%dk", old_value);
493         } else if (strcmp(module, "irc/notify") == 0) {
494                 if (strcasecmp(node->key, "notify_idle_time") == 0)
495                         new_value = g_strdup_printf("%dmin", old_value);
496         } else if (strcmp(module, "core") == 0) {
497                 if (strcasecmp(node->key, "write_buffer_mins") == 0) {
498                         new_value = g_strdup_printf("%dmin", old_value);
499                         new_key = "write_buffer_timeout";
500                 } else if (strcasecmp(node->key, "write_buffer_kb") == 0) {
501                         new_value = g_strdup_printf("%dk", old_value);
502                         new_key = "write_buffer_size";
503                 }
504         }
505
506         if (new_key != NULL || new_value != NULL) {
507                 config_node_set_str(mainconfig, parent,
508                                     new_key != NULL ? new_key : node->key,
509                                     new_value != NULL ?
510                                     new_value : node->value);
511                 if (new_key != NULL) {
512                         /* remove old */
513                         config_node_set_str(mainconfig, parent,
514                                             node->key, NULL);
515                 }
516                 config_changed = TRUE;
517                 g_free(new_value);
518         }
519         return new_key != NULL;
520 }
521
522 /* verify that all settings in config file for `module' are actually found
523    from /SET list */
524 void settings_check_module(const char *module)
525 {
526         SETTINGS_REC *set;
527         CONFIG_NODE *node, *parent;
528         GString *errors;
529         GSList *tmp, *next;
530         int count;
531
532         g_return_if_fail(module != NULL);
533
534         node = iconfig_node_traverse("settings", FALSE);
535         node = node == NULL ? NULL : config_node_section(node, module, -1);
536         if (node == NULL) return;
537
538         errors = g_string_new(NULL);
539         g_string_sprintf(errors, "Unknown settings in configuration "
540                          "file for module %s:", module);
541
542         count = 0;
543         parent = node;
544         tmp = config_node_first(node->value);
545         for (; tmp != NULL; tmp = next) {
546                 node = tmp->data;
547                 next = config_node_next(tmp);
548
549                 set = g_hash_table_lookup(settings, node->key);
550                 if (backwards_compatibility(module, node, parent))
551                         continue;
552
553                 if (set == NULL || strcmp(set->module, module) != 0) {
554                         g_string_sprintfa(errors, " %s", node->key);
555                         count++;
556                 }
557         }
558         if (count > 0) {
559                 if (gslist_find_icase_string(last_invalid_modules,
560                                              module) == NULL) {
561                         /* mark this module having invalid settings */
562                         last_invalid_modules =
563                                 g_slist_append(last_invalid_modules,
564                                                g_strdup(module));
565                 }
566                 if (fe_initialized)
567                         signal_emit("settings errors", 1, errors->str);
568                 else {
569                         if (last_errors == NULL)
570                                 last_errors = g_string_new(NULL);
571                         else
572                                 g_string_append_c(last_errors, '\n');
573                         g_string_append(last_errors, errors->str);
574                 }
575         }
576         g_string_free(errors, TRUE);
577 }
578
579 static int settings_compare(SETTINGS_REC *v1, SETTINGS_REC *v2)
580 {
581         return strcmp(v1->section, v2->section);
582 }
583
584 static void settings_hash_get(const char *key, SETTINGS_REC *rec,
585                               GSList **list)
586 {
587         *list = g_slist_insert_sorted(*list, rec,
588                                       (GCompareFunc) settings_compare);
589 }
590
591 GSList *settings_get_sorted(void)
592 {
593         GSList *list;
594
595         list = NULL;
596         g_hash_table_foreach(settings, (GHFunc) settings_hash_get, &list);
597         return list;
598 }
599
600 void sig_term(int n)
601 {
602         /* if we get SIGTERM after this, just die instead of coming back here. */
603         signal(SIGTERM, SIG_DFL);
604
605         /* quit from all servers too.. */
606         signal_emit("command quit", 1, "");
607
608         /* and die */
609         raise(SIGTERM);
610 }
611
612 /* Yes, this is my own stupid checksum generator, some "real" algorithm
613    would be nice but would just take more space without much real benefit */
614 static unsigned int file_checksum(const char *fname)
615 {
616         char buf[512];
617         int f, ret, n;
618         unsigned int checksum = 0;
619
620         f = open(fname, O_RDONLY);
621         if (f == -1) return 0;
622
623         n = 0;
624         while ((ret = read(f, buf, sizeof(buf))) > 0) {
625                 while (ret-- > 0)
626                         checksum += buf[ret] << ((n++ & 3)*8);
627         }
628         close(f);
629         return checksum;
630 }
631
632 static void irssi_config_save_state(const char *fname)
633 {
634         struct stat statbuf;
635
636         g_return_if_fail(fname != NULL);
637
638         if (stat(fname, &statbuf) != 0)
639                 return;
640
641         /* save modify time, file size and checksum */
642         config_last_mtime = statbuf.st_mtime;
643         config_last_size = statbuf.st_size;
644         config_last_checksum = file_checksum(fname);
645 }
646
647 int irssi_config_is_changed(const char *fname)
648 {
649         struct stat statbuf;
650
651         if (fname == NULL)
652                 fname = mainconfig->fname;
653
654         if (stat(fname, &statbuf) != 0)
655                 return FALSE;
656
657         return config_last_mtime != statbuf.st_mtime &&
658                 (config_last_size != statbuf.st_size ||
659                  config_last_checksum != file_checksum(fname));
660 }
661
662 static CONFIG_REC *parse_configfile(const char *fname)
663 {
664         CONFIG_REC *config;
665         struct stat statbuf;
666         const char *path;
667         char *str;
668
669         if (fname == NULL)
670                 fname = get_irssi_config();
671
672         if (stat(fname, &statbuf) == 0)
673                 path = fname;
674         else {
675                 /* user configuration file not found, use the default one
676                    from sysconfdir */
677                 path = SYSCONFDIR"/"IRSSI_GLOBAL_CONFIG;
678                 if (stat(path, &statbuf) != 0) {
679                         /* no configuration file in sysconfdir ..
680                            use the build-in configuration */
681                         path = NULL;
682                 }
683         }
684
685         config = config_open(path, -1);
686         if (config == NULL) {
687                 str = g_strdup_printf("Error opening configuration file %s: %s",
688                                       path, g_strerror(errno));
689                 signal_emit("gui dialog", 2, "error", str);
690                 g_free(str);
691
692                 config = config_open(NULL, -1);
693         }
694
695         if (config->fname != NULL)
696                 config_parse(config);
697         else
698                 config_parse_data(config, default_config, "internal");
699
700         config_change_file_name(config, fname, 0660);
701         irssi_config_save_state(fname);
702         return config;
703 }
704
705 static void init_configfile(void)
706 {
707         struct stat statbuf;
708         char *str;
709
710         if (stat(get_irssi_dir(), &statbuf) != 0) {
711                 /* ~/.irssi not found, create it. */
712                 if (mkpath(get_irssi_dir(), 0700) != 0) {
713                         g_error("Couldn't create %s directory", get_irssi_dir());
714                 }
715         } else if (!S_ISDIR(statbuf.st_mode)) {
716                 g_error("%s is not a directory.\n"
717                         "You should remove it with command: rm %s",
718                         get_irssi_dir(), get_irssi_dir());
719         }
720
721         mainconfig = parse_configfile(NULL);
722         config_last_modifycounter = mainconfig->modifycounter;
723
724         /* any errors? */
725         if (config_last_error(mainconfig) != NULL) {
726                 str = g_strdup_printf("Ignored errors in configuration file:\n%s",
727                                       config_last_error(mainconfig));
728                 signal_emit("gui dialog", 2, "error", str);
729                 g_free(str);
730         }
731
732         signal(SIGTERM, sig_term);
733 }
734
735 int settings_reread(const char *fname)
736 {
737         CONFIG_REC *tempconfig;
738         char *str;
739
740         str = fname == NULL ? NULL : convert_home(fname);
741         tempconfig = parse_configfile(str);
742         g_free_not_null(str);
743
744         if (tempconfig == NULL) {
745                 signal_emit("gui dialog", 2, "error", g_strerror(errno));
746                 return FALSE;
747         }
748
749         if (config_last_error(tempconfig) != NULL) {
750                 str = g_strdup_printf("Errors in configuration file:\n%s",
751                                       config_last_error(tempconfig));
752                 signal_emit("gui dialog", 2, "error", str);
753                 g_free(str);
754
755                 config_close(tempconfig);
756                 return FALSE;
757         }
758
759         config_close(mainconfig);
760         mainconfig = tempconfig;
761         config_last_modifycounter = mainconfig->modifycounter;
762
763         signal_emit("setup changed", 0);
764         signal_emit("setup reread", 1, mainconfig->fname);
765         return TRUE;
766 }
767
768 int settings_save(const char *fname, int autosave)
769 {
770         char *str;
771         int error;
772
773         if (fname == NULL)
774                 fname = mainconfig->fname;
775
776         error = config_write(mainconfig, fname, 0660) != 0;
777         irssi_config_save_state(fname);
778         config_last_modifycounter = mainconfig->modifycounter;
779         if (error) {
780                 str = g_strdup_printf("Couldn't save configuration file: %s",
781                                       config_last_error(mainconfig));
782                 signal_emit("gui dialog", 2, "error", str);
783                 g_free(str);
784         }
785         signal_emit("setup saved", 2, fname, GINT_TO_POINTER(autosave));
786         return !error;
787 }
788
789 static int sig_autosave(void)
790 {
791         char *fname, *str;
792
793         if (!settings_get_bool("settings_autosave") ||
794             config_last_modifycounter == mainconfig->modifycounter)
795                 return 1;
796
797         if (!irssi_config_is_changed(NULL))
798                 settings_save(NULL, TRUE);
799         else {
800                 fname = g_strconcat(mainconfig->fname, ".autosave", NULL);
801                 str = g_strdup_printf("Configuration file was modified "
802                                       "while irssi was running. Saving "
803                                       "configuration to file '%s' instead. "
804                                       "Use /SAVE or /RELOAD to get rid of "
805                                       "this message.", fname);
806                 signal_emit("gui dialog", 2, "warning", str);
807                 g_free(str);
808
809                 settings_save(fname, TRUE);
810                 g_free(fname);
811         }
812
813         return 1;
814 }
815
816 void settings_init(void)
817 {
818         settings = g_hash_table_new((GHashFunc) g_istr_hash,
819                                     (GCompareFunc) g_istr_equal);
820
821         last_errors = NULL;
822         last_invalid_modules = NULL;
823         fe_initialized = FALSE;
824         config_changed = FALSE;
825
826         config_last_mtime = 0;
827         config_last_modifycounter = 0;
828         init_configfile();
829
830         settings_add_bool("misc", "settings_autosave", TRUE);
831         timeout_tag = g_timeout_add(SETTINGS_AUTOSAVE_TIMEOUT,
832                                     (GSourceFunc) sig_autosave, NULL);
833         signal_add("irssi init finished", (SIGNAL_FUNC) sig_init_finished);
834         signal_add("gui exit", (SIGNAL_FUNC) sig_autosave);
835 }
836
837 static void settings_hash_free(const char *key, SETTINGS_REC *rec)
838 {
839         settings_destroy(rec);
840 }
841
842 void settings_deinit(void)
843 {
844         g_source_remove(timeout_tag);
845         signal_remove("irssi init finished", (SIGNAL_FUNC) sig_init_finished);
846         signal_remove("gui exit", (SIGNAL_FUNC) sig_autosave);
847
848         g_slist_foreach(last_invalid_modules, (GFunc) g_free, NULL);
849         g_slist_free(last_invalid_modules);
850
851         g_hash_table_foreach(settings, (GHFunc) settings_hash_free, NULL);
852         g_hash_table_destroy(settings);
853
854         if (mainconfig != NULL) config_close(mainconfig);
855 }