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