2000-04-26 04:03:38 -04:00
|
|
|
/*
|
|
|
|
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"
|
2000-05-15 04:25:45 -04:00
|
|
|
#include "misc.h"
|
2000-04-26 04:03:38 -04:00
|
|
|
|
|
|
|
#include "lib-config/iconfig.h"
|
|
|
|
#include "settings.h"
|
|
|
|
#include "default-config.h"
|
|
|
|
|
|
|
|
#include <signal.h>
|
|
|
|
|
|
|
|
CONFIG_REC *mainconfig;
|
|
|
|
|
2000-12-17 00:44:45 -05:00
|
|
|
static GString *last_errors;
|
2001-03-16 18:57:24 -05:00
|
|
|
static GSList *last_invalid_modules;
|
2000-12-17 00:44:45 -05:00
|
|
|
static int fe_initialized;
|
2000-12-19 12:42:56 -05:00
|
|
|
static int config_changed; /* FIXME: remove after .98 (unless needed again) */
|
2000-12-17 00:44:45 -05:00
|
|
|
|
2000-04-26 04:03:38 -04:00
|
|
|
static GHashTable *settings;
|
2000-07-23 08:29:50 -04:00
|
|
|
static int timeout_tag;
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2000-11-25 21:17:14 -05:00
|
|
|
static int config_last_modifycounter;
|
2000-10-15 15:21:21 -04:00
|
|
|
static time_t config_last_mtime;
|
|
|
|
static long config_last_size;
|
|
|
|
static unsigned int config_last_checksum;
|
|
|
|
|
2000-12-17 00:44:45 -05:00
|
|
|
static SETTINGS_REC *settings_find(const char *key)
|
2000-04-26 04:03:38 -04:00
|
|
|
{
|
|
|
|
SETTINGS_REC *rec;
|
|
|
|
|
|
|
|
g_return_val_if_fail(key != NULL, NULL);
|
|
|
|
|
|
|
|
rec = g_hash_table_lookup(settings, key);
|
|
|
|
if (rec == NULL) {
|
2000-07-16 16:18:05 -04:00
|
|
|
g_warning("settings_get_default_str(%s) : "
|
|
|
|
"unknown setting", key);
|
2000-04-26 04:03:38 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2000-12-17 00:44:45 -05:00
|
|
|
return rec;
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
2000-12-17 00:44:45 -05:00
|
|
|
const char *settings_get_str(const char *key)
|
2000-04-26 04:03:38 -04:00
|
|
|
{
|
|
|
|
SETTINGS_REC *rec;
|
2000-12-17 00:44:45 -05:00
|
|
|
CONFIG_NODE *setnode, *node;
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2000-12-17 00:44:45 -05:00
|
|
|
rec = settings_find(key);
|
|
|
|
g_return_val_if_fail(rec != NULL, NULL);
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2000-12-17 00:44:45 -05:00
|
|
|
setnode = iconfig_node_traverse("settings", FALSE);
|
|
|
|
if (setnode == NULL)
|
|
|
|
return rec->def;
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2000-12-17 00:44:45 -05:00
|
|
|
node = config_node_section(setnode, rec->module, -1);
|
|
|
|
return node == NULL ? rec->def :
|
|
|
|
config_node_get_str(node, key, rec->def);
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
2000-11-23 16:40:07 -05:00
|
|
|
int settings_get_int(const char *key)
|
2000-04-26 04:03:38 -04:00
|
|
|
{
|
2000-12-17 00:44:45 -05:00
|
|
|
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);
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
2000-11-23 16:40:07 -05:00
|
|
|
int settings_get_bool(const char *key)
|
2000-04-26 04:03:38 -04:00
|
|
|
{
|
2000-12-17 00:44:45 -05:00
|
|
|
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);
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
2000-12-16 23:14:47 -05:00
|
|
|
void settings_add_str_module(const char *module, const char *section,
|
|
|
|
const char *key, const char *def)
|
2000-04-26 04:03:38 -04:00
|
|
|
{
|
|
|
|
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);
|
2000-12-16 23:14:47 -05:00
|
|
|
rec->module = g_strdup(module);
|
2000-04-26 04:03:38 -04:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2000-12-16 23:14:47 -05:00
|
|
|
void settings_add_int_module(const char *module, const char *section,
|
|
|
|
const char *key, int def)
|
2000-04-26 04:03:38 -04:00
|
|
|
{
|
|
|
|
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);
|
2000-12-16 23:14:47 -05:00
|
|
|
rec->module = g_strdup(module);
|
2000-04-26 04:03:38 -04:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2000-12-16 23:14:47 -05:00
|
|
|
void settings_add_bool_module(const char *module, const char *section,
|
|
|
|
const char *key, int def)
|
2000-04-26 04:03:38 -04:00
|
|
|
{
|
|
|
|
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);
|
2000-12-16 23:14:47 -05:00
|
|
|
rec->module = g_strdup(module);
|
2000-04-26 04:03:38 -04:00
|
|
|
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);
|
2000-12-16 23:14:47 -05:00
|
|
|
g_free(rec->module);
|
2000-04-26 04:03:38 -04:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2000-12-16 23:14:47 -05:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2000-12-17 00:44:45 -05:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2000-04-26 04:03:38 -04:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2000-12-17 00:44:45 -05:00
|
|
|
static void sig_init_finished(void)
|
|
|
|
{
|
|
|
|
fe_initialized = TRUE;
|
|
|
|
if (last_errors != NULL) {
|
2001-03-16 18:57:24 -05:00
|
|
|
signal_emit("settings errors", 1, last_errors->str);
|
2000-12-17 00:44:45 -05:00
|
|
|
g_string_free(last_errors, TRUE);
|
|
|
|
}
|
|
|
|
|
2000-12-19 12:42:56 -05:00
|
|
|
if (config_changed) {
|
|
|
|
/* some backwards compatibility changes were made to
|
|
|
|
config file, reload it */
|
|
|
|
signal_emit("setup changed", 0);
|
|
|
|
}
|
2000-12-17 00:44:45 -05:00
|
|
|
}
|
|
|
|
|
2001-03-16 18:57:24 -05:00
|
|
|
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;
|
|
|
|
|
2002-02-02 12:37:44 -05:00
|
|
|
for (tmp = config_node_first(node->value); tmp != NULL; tmp = next) {
|
2001-03-16 18:57:24 -05:00
|
|
|
CONFIG_NODE *subnode = tmp->data;
|
2002-02-02 12:37:44 -05:00
|
|
|
next = config_node_next(tmp);
|
2001-03-16 18:57:24 -05:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-12-17 00:44:45 -05:00
|
|
|
/* 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;
|
2002-02-02 12:37:44 -05:00
|
|
|
GSList *tmp;
|
2000-12-17 00:44:45 -05:00
|
|
|
int count;
|
|
|
|
|
|
|
|
g_return_if_fail(module != NULL);
|
|
|
|
|
2001-03-16 18:57:24 -05:00
|
|
|
node = iconfig_node_traverse("settings", FALSE);
|
2000-12-17 00:44:45 -05:00
|
|
|
node = node == NULL ? NULL : config_node_section(node, module, -1);
|
|
|
|
if (node == NULL) return;
|
|
|
|
|
|
|
|
errors = g_string_new(NULL);
|
2001-05-17 16:13:59 -04:00
|
|
|
g_string_sprintf(errors, "Unknown settings in configuration "
|
|
|
|
"file for module %s:", module);
|
2000-12-17 00:44:45 -05:00
|
|
|
|
|
|
|
count = 0;
|
2002-02-02 12:37:44 -05:00
|
|
|
tmp = config_node_first(node->value);
|
|
|
|
for (; tmp != NULL; tmp = config_node_next(tmp)) {
|
2000-12-17 00:44:45 -05:00
|
|
|
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) {
|
2001-03-16 18:57:24 -05:00
|
|
|
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));
|
|
|
|
}
|
2000-12-17 00:44:45 -05:00
|
|
|
if (fe_initialized)
|
2001-03-16 18:57:24 -05:00
|
|
|
signal_emit("settings errors", 1, errors->str);
|
2000-12-17 00:44:45 -05:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2000-04-26 04:03:38 -04:00
|
|
|
static int settings_compare(SETTINGS_REC *v1, SETTINGS_REC *v2)
|
|
|
|
{
|
|
|
|
return strcmp(v1->section, v2->section);
|
|
|
|
}
|
|
|
|
|
2000-07-16 16:18:05 -04:00
|
|
|
static void settings_hash_get(const char *key, SETTINGS_REC *rec,
|
|
|
|
GSList **list)
|
2000-04-26 04:03:38 -04:00
|
|
|
{
|
2000-07-16 16:18:05 -04:00
|
|
|
*list = g_slist_insert_sorted(*list, rec,
|
|
|
|
(GCompareFunc) settings_compare);
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2000-10-15 15:21:21 -04:00
|
|
|
/* 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)
|
|
|
|
{
|
2001-02-19 20:54:14 -05:00
|
|
|
char buf[512];
|
|
|
|
int f, ret, n;
|
2000-10-15 15:21:21 -04:00
|
|
|
unsigned int checksum = 0;
|
|
|
|
|
2001-02-19 20:54:14 -05:00
|
|
|
f = open(fname, O_RDONLY);
|
|
|
|
if (f == -1) return 0;
|
2000-11-29 11:05:15 -05:00
|
|
|
|
2001-02-19 20:54:14 -05:00
|
|
|
n = 0;
|
|
|
|
while ((ret = read(f, buf, sizeof(buf))) > 0) {
|
|
|
|
while (ret-- > 0)
|
|
|
|
checksum += buf[ret] << ((n++ & 3)*8);
|
|
|
|
}
|
|
|
|
close(f);
|
2000-10-15 15:21:21 -04:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
2000-04-26 04:03:38 -04:00
|
|
|
static CONFIG_REC *parse_configfile(const char *fname)
|
|
|
|
{
|
|
|
|
CONFIG_REC *config;
|
2001-02-19 20:54:14 -05:00
|
|
|
struct stat statbuf;
|
|
|
|
const char *path;
|
2001-07-14 20:39:48 -04:00
|
|
|
char *str;
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2001-07-14 20:39:48 -04:00
|
|
|
if (fname == NULL)
|
|
|
|
fname = get_irssi_config();
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2001-07-14 20:39:48 -04:00
|
|
|
if (stat(fname, &statbuf) == 0)
|
|
|
|
path = fname;
|
2001-02-19 20:54:14 -05:00
|
|
|
else {
|
2000-04-26 04:03:38 -04:00
|
|
|
/* user configuration file not found, use the default one
|
|
|
|
from sysconfdir */
|
2002-02-10 09:35:21 -05:00
|
|
|
path = SYSCONFDIR"/"IRSSI_GLOBAL_CONFIG;
|
2001-02-19 20:54:14 -05:00
|
|
|
if (stat(path, &statbuf) != 0) {
|
2000-04-26 04:03:38 -04:00
|
|
|
/* no configuration file in sysconfdir ..
|
|
|
|
use the build-in configuration */
|
2001-02-19 20:54:14 -05:00
|
|
|
path = NULL;
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
2001-02-19 20:54:14 -05:00
|
|
|
}
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2001-02-19 20:54:14 -05:00
|
|
|
config = config_open(path, -1);
|
|
|
|
if (config == NULL) {
|
2001-07-14 15:10:17 -04:00
|
|
|
str = g_strdup_printf("Error opening configuration file %s: %s",
|
|
|
|
path, g_strerror(errno));
|
|
|
|
signal_emit("gui dialog", 2, "error", str);
|
|
|
|
g_free(str);
|
|
|
|
|
2001-02-19 20:54:14 -05:00
|
|
|
config = config_open(NULL, -1);
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
2001-02-19 20:54:14 -05:00
|
|
|
if (path != NULL)
|
|
|
|
config_parse(config);
|
|
|
|
else
|
|
|
|
config_parse_data(config, default_config, "internal");
|
|
|
|
|
2001-07-14 20:39:48 -04:00
|
|
|
config_change_file_name(config, fname, 0660);
|
|
|
|
irssi_config_save_state(fname);
|
2000-04-26 04:03:38 -04:00
|
|
|
return config;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void init_configfile(void)
|
|
|
|
{
|
|
|
|
struct stat statbuf;
|
|
|
|
char *str;
|
|
|
|
|
2001-07-14 20:39:48 -04:00
|
|
|
if (stat(get_irssi_dir(), &statbuf) != 0) {
|
2000-04-26 04:03:38 -04:00
|
|
|
/* ~/.irssi not found, create it. */
|
2001-07-14 20:39:48 -04:00
|
|
|
if (mkpath(get_irssi_dir(), 0700) != 0) {
|
|
|
|
g_error("Couldn't create %s directory", get_irssi_dir());
|
2000-05-04 06:32:42 -04:00
|
|
|
}
|
2000-07-16 16:18:05 -04:00
|
|
|
} else if (!S_ISDIR(statbuf.st_mode)) {
|
2001-05-17 16:13:59 -04:00
|
|
|
g_error("%s is not a directory.\n"
|
2001-07-14 20:39:48 -04:00
|
|
|
"You should remove it with command: rm %s",
|
|
|
|
get_irssi_dir(), get_irssi_dir());
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
mainconfig = parse_configfile(NULL);
|
2000-11-25 21:26:13 -05:00
|
|
|
config_last_modifycounter = mainconfig->modifycounter;
|
2000-04-26 04:03:38 -04:00
|
|
|
|
|
|
|
/* any errors? */
|
|
|
|
if (config_last_error(mainconfig) != NULL) {
|
2001-07-14 15:10:17 -04:00
|
|
|
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);
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
signal(SIGTERM, sig_term);
|
|
|
|
}
|
|
|
|
|
2000-07-22 18:05:29 -04:00
|
|
|
int settings_reread(const char *fname)
|
2000-04-26 04:03:38 -04:00
|
|
|
{
|
|
|
|
CONFIG_REC *tempconfig;
|
2000-05-15 04:25:45 -04:00
|
|
|
char *str;
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2001-07-28 20:27:23 -04:00
|
|
|
str = fname == NULL ? NULL : convert_home(fname);
|
2000-05-15 04:25:45 -04:00
|
|
|
tempconfig = parse_configfile(str);
|
2001-07-28 20:27:23 -04:00
|
|
|
g_free_not_null(str);
|
2000-04-26 04:03:38 -04:00
|
|
|
|
|
|
|
if (tempconfig == NULL) {
|
|
|
|
signal_emit("gui dialog", 2, "error", g_strerror(errno));
|
2000-07-22 18:05:29 -04:00
|
|
|
return FALSE;
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (config_last_error(tempconfig) != NULL) {
|
2001-05-17 16:13:59 -04:00
|
|
|
str = g_strdup_printf("Errors in configuration file:\n%s",
|
2000-04-26 04:03:38 -04:00
|
|
|
config_last_error(tempconfig));
|
|
|
|
signal_emit("gui dialog", 2, "error", str);
|
|
|
|
g_free(str);
|
2000-07-16 16:18:05 -04:00
|
|
|
|
2000-04-26 04:03:38 -04:00
|
|
|
config_close(tempconfig);
|
2000-07-22 18:05:29 -04:00
|
|
|
return FALSE;
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
config_close(mainconfig);
|
|
|
|
mainconfig = tempconfig;
|
2000-11-25 21:26:13 -05:00
|
|
|
config_last_modifycounter = mainconfig->modifycounter;
|
2000-04-26 04:03:38 -04:00
|
|
|
|
|
|
|
signal_emit("setup changed", 0);
|
2001-11-16 06:50:14 -05:00
|
|
|
signal_emit("setup reread", 1, mainconfig->fname);
|
2000-07-22 18:05:29 -04:00
|
|
|
return TRUE;
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
2001-11-14 11:28:56 -05:00
|
|
|
int settings_save(const char *fname, int autosave)
|
2000-04-26 04:03:38 -04:00
|
|
|
{
|
2000-05-04 06:32:42 -04:00
|
|
|
char *str;
|
2000-10-15 15:21:21 -04:00
|
|
|
int error;
|
2000-05-04 06:32:42 -04:00
|
|
|
|
2000-10-15 15:21:21 -04:00
|
|
|
if (fname == NULL)
|
|
|
|
fname = mainconfig->fname;
|
2000-05-04 06:32:42 -04:00
|
|
|
|
2000-10-15 15:21:21 -04:00
|
|
|
error = config_write(mainconfig, fname, 0660) != 0;
|
2000-11-25 21:17:14 -05:00
|
|
|
irssi_config_save_state(fname);
|
|
|
|
config_last_modifycounter = mainconfig->modifycounter;
|
2000-10-15 15:21:21 -04:00
|
|
|
if (error) {
|
2001-05-17 16:13:59 -04:00
|
|
|
str = g_strdup_printf("Couldn't save configuration file: %s",
|
2000-10-15 15:21:21 -04:00
|
|
|
config_last_error(mainconfig));
|
|
|
|
signal_emit("gui dialog", 2, "error", str);
|
|
|
|
g_free(str);
|
|
|
|
}
|
2001-11-14 11:31:09 -05:00
|
|
|
signal_emit("setup saved", 2, fname, GINT_TO_POINTER(autosave));
|
2000-10-15 15:21:21 -04:00
|
|
|
return !error;
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
2002-01-19 22:30:28 -05:00
|
|
|
static int sig_autosave(void)
|
2000-07-23 08:29:50 -04:00
|
|
|
{
|
2000-10-15 15:21:21 -04:00
|
|
|
char *fname, *str;
|
|
|
|
|
2000-11-25 21:17:14 -05:00
|
|
|
if (!settings_get_bool("settings_autosave") ||
|
|
|
|
config_last_modifycounter == mainconfig->modifycounter)
|
2002-01-19 22:30:28 -05:00
|
|
|
return 1;
|
2000-10-15 15:21:21 -04:00
|
|
|
|
|
|
|
if (!irssi_config_is_changed(NULL))
|
2001-11-14 11:28:56 -05:00
|
|
|
settings_save(NULL, TRUE);
|
2000-10-15 15:21:21 -04:00
|
|
|
else {
|
|
|
|
fname = g_strconcat(mainconfig->fname, ".autosave", NULL);
|
2001-05-17 16:13:59 -04:00
|
|
|
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);
|
2000-10-15 15:21:21 -04:00
|
|
|
signal_emit("gui dialog", 2, "warning", str);
|
|
|
|
g_free(str);
|
|
|
|
|
2001-11-14 11:28:56 -05:00
|
|
|
settings_save(fname, TRUE);
|
2000-10-15 15:21:21 -04:00
|
|
|
g_free(fname);
|
|
|
|
}
|
2002-01-19 22:30:28 -05:00
|
|
|
|
|
|
|
return 1;
|
2000-07-23 08:29:50 -04:00
|
|
|
}
|
|
|
|
|
2000-04-26 04:03:38 -04:00
|
|
|
void settings_init(void)
|
|
|
|
{
|
2002-01-12 09:32:28 -05:00
|
|
|
settings = g_hash_table_new((GHashFunc) g_istr_hash,
|
|
|
|
(GCompareFunc) g_istr_equal);
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2000-12-17 00:44:45 -05:00
|
|
|
last_errors = NULL;
|
2001-03-16 18:57:24 -05:00
|
|
|
last_invalid_modules = NULL;
|
2000-12-17 00:44:45 -05:00
|
|
|
fe_initialized = FALSE;
|
2000-12-19 12:42:56 -05:00
|
|
|
config_changed = FALSE;
|
2000-12-17 00:44:45 -05:00
|
|
|
|
2000-10-15 15:21:21 -04:00
|
|
|
config_last_mtime = 0;
|
2000-11-25 21:17:14 -05:00
|
|
|
config_last_modifycounter = 0;
|
2000-04-26 04:03:38 -04:00
|
|
|
init_configfile();
|
2000-07-23 08:29:50 -04:00
|
|
|
|
|
|
|
settings_add_bool("misc", "settings_autosave", TRUE);
|
|
|
|
timeout_tag = g_timeout_add(1000*60*60, (GSourceFunc) sig_autosave, NULL);
|
2000-12-17 00:44:45 -05:00
|
|
|
signal_add("irssi init finished", (SIGNAL_FUNC) sig_init_finished);
|
2000-07-23 08:29:50 -04:00
|
|
|
signal_add("gui exit", (SIGNAL_FUNC) sig_autosave);
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void settings_hash_free(const char *key, SETTINGS_REC *rec)
|
|
|
|
{
|
|
|
|
settings_destroy(rec);
|
|
|
|
}
|
|
|
|
|
|
|
|
void settings_deinit(void)
|
|
|
|
{
|
2000-07-23 08:29:50 -04:00
|
|
|
g_source_remove(timeout_tag);
|
2000-12-17 00:44:45 -05:00
|
|
|
signal_remove("irssi init finished", (SIGNAL_FUNC) sig_init_finished);
|
2000-07-23 08:29:50 -04:00
|
|
|
signal_remove("gui exit", (SIGNAL_FUNC) sig_autosave);
|
|
|
|
|
2001-03-16 18:57:24 -05:00
|
|
|
g_slist_foreach(last_invalid_modules, (GFunc) g_free, NULL);
|
|
|
|
g_slist_free(last_invalid_modules);
|
|
|
|
|
2000-04-26 04:03:38 -04:00
|
|
|
g_hash_table_foreach(settings, (GHFunc) settings_hash_free, NULL);
|
|
|
|
g_hash_table_destroy(settings);
|
|
|
|
|
|
|
|
if (mainconfig != NULL) config_close(mainconfig);
|
|
|
|
}
|