#include "module.h"
#include "signals.h"
#include "commands.h"
+#include "levels.h"
#include "misc.h"
#include "lib-config/iconfig.h"
+#include "recode.h"
#include "settings.h"
#include "default-config.h"
#include <signal.h>
+#define SETTINGS_AUTOSAVE_TIMEOUT (1000*60*60) /* 1 hour */
+
CONFIG_REC *mainconfig;
static GString *last_errors;
-static char *last_config_error_msg;
static GSList *last_invalid_modules;
static int fe_initialized;
static int config_changed; /* FIXME: remove after .98 (unless needed again) */
return rec;
}
-const char *settings_get_str(const char *key)
+static SETTINGS_REC *settings_get(const char *key, SettingType type)
{
SETTINGS_REC *rec;
- CONFIG_NODE *setnode, *node;
+
+ g_return_val_if_fail(key != NULL, NULL);
rec = settings_find(key);
- g_return_val_if_fail(rec != NULL, NULL);
+ if (rec == NULL) {
+ g_warning("settings_get(%s) : not found", key);
+ return NULL;
+ }
+ if (type != -1 && rec->type != type) {
+ g_warning("settings_get(%s) : invalid type", key);
+ return NULL;
+ }
- setnode = iconfig_node_traverse("settings", FALSE);
- if (setnode == NULL)
- return rec->def;
+ return rec;
+}
+
+static const char *
+settings_get_str_type(const char *key, SettingType type)
+{
+ SETTINGS_REC *rec;
+ CONFIG_NODE *node;
- node = config_node_section(setnode, rec->module, -1);
- return node == NULL ? rec->def :
- config_node_get_str(node, key, rec->def);
+ rec = settings_get(key, type);
+ if (rec == NULL) return NULL;
+
+ node = iconfig_node_traverse("settings", FALSE);
+ node = node == NULL ? NULL : config_node_section(node, rec->module, -1);
+
+ return node == NULL ? rec->default_value.v_string :
+ config_node_get_str(node, key, rec->default_value.v_string);
+}
+
+const char *settings_get_str(const char *key)
+{
+ return settings_get_str_type(key, -1);
}
int settings_get_int(const char *key)
{
SETTINGS_REC *rec;
- CONFIG_NODE *setnode, *node;
- int def;
+ CONFIG_NODE *node;
- rec = settings_find(key);
- g_return_val_if_fail(rec != NULL, 0);
- def = GPOINTER_TO_INT(rec->def);
+ rec = settings_get(key, SETTING_TYPE_INT);
+ if (rec == NULL) return 0;
- setnode = iconfig_node_traverse("settings", FALSE);
- if (setnode == NULL)
- return def;
+ node = iconfig_node_traverse("settings", FALSE);
+ node = node == NULL ? NULL : config_node_section(node, rec->module, -1);
- node = config_node_section(setnode, rec->module, -1);
- return node == NULL ? def :
- config_node_get_int(node, key, def);
+ return node == NULL ? rec->default_value.v_int :
+ config_node_get_int(node, key, rec->default_value.v_int);
}
int settings_get_bool(const char *key)
{
SETTINGS_REC *rec;
- CONFIG_NODE *setnode, *node;
- int def;
+ CONFIG_NODE *node;
- rec = settings_find(key);
- g_return_val_if_fail(rec != NULL, 0);
- def = GPOINTER_TO_INT(rec->def);
+ rec = settings_get(key, SETTING_TYPE_BOOLEAN);
+ if (rec == NULL) return FALSE;
- setnode = iconfig_node_traverse("settings", FALSE);
- if (setnode == NULL)
- return def;
+ node = iconfig_node_traverse("settings", FALSE);
+ node = node == NULL ? NULL : config_node_section(node, rec->module, -1);
- node = config_node_section(setnode, rec->module, -1);
- return node == NULL ? def :
- config_node_get_bool(node, key, def);
+ return node == NULL ? rec->default_value.v_bool :
+ config_node_get_bool(node, key, rec->default_value.v_bool);
}
-void settings_add_str_module(const char *module, const char *section,
- const char *key, const char *def)
+int settings_get_time(const char *key)
{
- SETTINGS_REC *rec;
+ const char *str;
+ int msecs;
- g_return_if_fail(key != NULL);
- g_return_if_fail(section != NULL);
+ str = settings_get_str_type(key, SETTING_TYPE_TIME);
+ if (str != NULL && !parse_time_interval(str, &msecs))
+ g_warning("settings_get_time(%s) : Invalid time '%s'", key, str);
+ return str == NULL ? 0 : msecs;
+}
- rec = g_hash_table_lookup(settings, key);
- g_return_if_fail(rec == NULL);
+int settings_get_level(const char *key)
+{
+ const char *str;
- 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);
+ str = settings_get_str_type(key, SETTING_TYPE_LEVEL);
+ return str == NULL ? 0 : level2bits(str);
+}
- g_hash_table_insert(settings, rec->key, rec);
+int settings_get_size(const char *key)
+{
+ const char *str;
+ int bytes;
+
+ str = settings_get_str_type(key, SETTING_TYPE_SIZE);
+ if (str != NULL && !parse_size(str, &bytes))
+ g_warning("settings_get_size(%s) : Invalid size '%s'", key, str);
+ return str == NULL ? 0 : bytes;
}
-void settings_add_int_module(const char *module, const char *section,
- const char *key, int def)
+static void settings_add(const char *module, const char *section,
+ const char *key, SettingType type,
+ const SettingValue *default_value)
{
SETTINGS_REC *rec;
g_return_if_fail(section != NULL);
rec = g_hash_table_lookup(settings, key);
- g_return_if_fail(rec == NULL);
+ if (rec != NULL) {
+ /* Already exists, make sure it's correct type */
+ if (rec->type != type) {
+ g_warning("Trying to add already existing "
+ "setting '%s' with different type.", key);
+ return;
+ }
+ } else {
+ rec = g_new(SETTINGS_REC, 1);
+ rec->module = g_strdup(module);
+ rec->key = g_strdup(key);
+ rec->section = g_strdup(section);
+ rec->type = type;
+
+ rec->default_value = *default_value;
+ g_hash_table_insert(settings, rec->key, rec);
+ }
+
+ rec->refcount++;
+}
- 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);
+void settings_add_str_module(const char *module, const char *section,
+ const char *key, const char *def)
+{
+ SettingValue default_value;
- g_hash_table_insert(settings, rec->key, rec);
+ memset(&default_value, 0, sizeof(default_value));
+ default_value.v_string = g_strdup(def);
+ settings_add(module, section, key, SETTING_TYPE_STRING, &default_value);
+}
+
+void settings_add_int_module(const char *module, const char *section,
+ const char *key, int def)
+{
+ SettingValue default_value;
+
+ memset(&default_value, 0, sizeof(default_value));
+ default_value.v_int = def;
+ settings_add(module, section, key, SETTING_TYPE_INT, &default_value);
}
void settings_add_bool_module(const char *module, const char *section,
const char *key, int def)
{
- SETTINGS_REC *rec;
+ SettingValue default_value;
- g_return_if_fail(key != NULL);
- g_return_if_fail(section != NULL);
+ memset(&default_value, 0, sizeof(default_value));
+ default_value.v_bool = def;
+ settings_add(module, section, key, SETTING_TYPE_BOOLEAN,
+ &default_value);
+}
- rec = g_hash_table_lookup(settings, key);
- g_return_if_fail(rec == NULL);
+void settings_add_time_module(const char *module, const char *section,
+ const char *key, const char *def)
+{
+ SettingValue default_value;
+
+ memset(&default_value, 0, sizeof(default_value));
+ default_value.v_string = g_strdup(def);
+ settings_add(module, section, key, SETTING_TYPE_TIME, &default_value);
+}
- 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);
+void settings_add_level_module(const char *module, const char *section,
+ const char *key, const char *def)
+{
+ SettingValue default_value;
- g_hash_table_insert(settings, rec->key, rec);
+ memset(&default_value, 0, sizeof(default_value));
+ default_value.v_string = g_strdup(def);
+ settings_add(module, section, key, SETTING_TYPE_LEVEL, &default_value);
+}
+
+void settings_add_size_module(const char *module, const char *section,
+ const char *key, const char *def)
+{
+ SettingValue default_value;
+
+ memset(&default_value, 0, sizeof(default_value));
+ default_value.v_string = g_strdup(def);
+ settings_add(module, section, key, SETTING_TYPE_SIZE, &default_value);
}
static void settings_destroy(SETTINGS_REC *rec)
{
- if (rec->type == SETTING_TYPE_STRING)
- g_free_not_null(rec->def);
+ if (rec->type != SETTING_TYPE_INT &&
+ rec->type != SETTING_TYPE_BOOLEAN)
+ g_free(rec->default_value.v_string);
g_free(rec->module);
g_free(rec->section);
g_free(rec->key);
g_free(rec);
}
+static void settings_unref(SETTINGS_REC *rec, int remove_hash)
+{
+ if (--rec->refcount == 0) {
+ if (remove_hash)
+ g_hash_table_remove(settings, rec->key);
+ settings_destroy(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);
+ if (rec != NULL)
+ settings_unref(rec, TRUE);
}
static int settings_remove_hash(const char *key, SETTINGS_REC *rec,
const char *module)
{
if (strcmp(rec->module, module) == 0) {
- settings_destroy(rec);
+ settings_unref(rec, FALSE);
return TRUE;
}
g_return_val_if_fail(key != NULL, NULL);
rec = g_hash_table_lookup(settings, key);
- g_return_val_if_fail(rec != NULL, NULL);
+ if (rec == NULL) {
+ g_warning("Changing unknown setting '%s'", key);
+ return NULL;
+ }
node = iconfig_node_traverse("settings", TRUE);
return config_node_section(node, rec->module, NODE_TYPE_BLOCK);
iconfig_node_set_bool(settings_get_node(key), key, value);
}
-int settings_get_type(const char *key)
+int settings_set_time(const char *key, const char *value)
+{
+ int msecs;
+
+ if (!parse_time_interval(value, &msecs))
+ return FALSE;
+
+ iconfig_node_set_str(settings_get_node(key), key, value);
+ return TRUE;
+}
+
+int settings_set_level(const char *key, const char *value)
+{
+ iconfig_node_set_str(settings_get_node(key), key, value);
+ return TRUE;
+}
+
+int settings_set_size(const char *key, const char *value)
+{
+ int size;
+
+ if (!parse_size(value, &size))
+ return FALSE;
+
+ iconfig_node_set_str(settings_get_node(key), key, value);
+ return TRUE;
+}
+
+SettingType settings_get_type(const char *key)
{
SETTINGS_REC *rec;
g_string_free(last_errors, TRUE);
}
- if (last_config_error_msg != NULL) {
- signal_emit("gui dialog", 2, "error", last_config_error_msg);
- g_free_and_null(last_config_error_msg);
- }
-
if (config_changed) {
/* some backwards compatibility changes were made to
config file, reload it */
+ g_warning("Some settings were automatically "
+ "updated, please /SAVE");
signal_emit("setup changed", 0);
}
}
-/* FIXME: remove after 0.7.98 - only for backward compatibility */
-static void settings_move(SETTINGS_REC *rec, char *value)
-{
- CONFIG_NODE *setnode, *node;
-
- setnode = iconfig_node_traverse("settings", TRUE);
- node = config_node_section(setnode, rec->module, NODE_TYPE_BLOCK);
-
- iconfig_node_set_str(node, rec->key, value);
- iconfig_node_set_str(setnode, rec->key, NULL);
-
- config_changed = TRUE;
-}
-
static void settings_clean_invalid_module(const char *module)
{
CONFIG_NODE *node;
node = config_node_section(node, module, -1);
if (node == NULL) return;
- for (tmp = node->value; tmp != NULL; tmp = next) {
+ for (tmp = config_node_first(node->value); tmp != NULL; tmp = next) {
CONFIG_NODE *subnode = tmp->data;
- next = tmp->next;
+ next = config_node_next(tmp);
set = g_hash_table_lookup(settings, subnode->key);
if (set == NULL || strcmp(set->module, module) != 0)
settings_clean_invalid_module(module);
- g_free(module);
last_invalid_modules =
g_slist_remove(last_invalid_modules, module);
+ g_free(module);
+ }
+}
+
+static int backwards_compatibility(const char *module, CONFIG_NODE *node,
+ CONFIG_NODE *parent)
+{
+ const char *new_key, *new_module;
+ CONFIG_NODE *new_node;
+ char *new_value;
+ int old_value;
+
+ new_value = NULL; new_key = NULL; new_module = NULL;
+
+ /* fe-text term_type -> fe-common/core term_charset - for 0.8.10-> */
+ if (strcmp(module, "fe-text") == 0) {
+ if (strcasecmp(node->key, "term_type") == 0 ||
+ /* kludge for cvs-version where term_charset was in fe-text */
+ strcasecmp(node->key, "term_charset") == 0) {
+ new_module = "fe-common/core";
+ new_key = "term_charset";
+ new_value = !is_valid_charset(node->value) ? NULL :
+ g_strdup(node->value);
+ new_node = iconfig_node_traverse("settings", FALSE);
+ new_node = new_node == NULL ? NULL :
+ config_node_section(new_node, new_module, -1);
+
+ config_node_set_str(mainconfig, new_node,
+ new_key, new_value);
+ /* remove old */
+ config_node_set_str(mainconfig, parent,
+ node->key, NULL);
+ g_free(new_value);
+ config_changed = TRUE;
+ return new_key != NULL;
+ }
+ }
+ new_value = NULL, new_key = NULL;
+ /* FIXME: remove later - for 0.8.6 -> */
+ if (node->value == NULL || !is_numeric(node->value, '\0'))
+ return FALSE;
+
+ old_value = atoi(node->value);
+
+ if (strcmp(module, "fe-text") == 0) {
+ if (strcasecmp(node->key, "lag_min_show") == 0)
+ new_value = g_strdup_printf("%dms", old_value*10);
+ else if (strcasecmp(node->key, "scrollback_hours") == 0) {
+ new_value = g_strdup_printf("%dh", old_value);
+ new_key = "scrollback_time";
+ }
+ } else if (strcmp(module, "irc/core") == 0) {
+ if (strcasecmp(node->key, "cmd_queue_speed") == 0)
+ new_value = g_strdup_printf("%dms", old_value);
+ } else if (strcmp(module, "irc/dcc") == 0) {
+ if (strcasecmp(node->key, "dcc_autoget_max_size") == 0)
+ new_value = g_strdup_printf("%dk", old_value);
+ } else if (strcmp(module, "irc/notify") == 0) {
+ if (strcasecmp(node->key, "notify_idle_time") == 0)
+ new_value = g_strdup_printf("%dmin", old_value);
+ } else if (strcmp(module, "core") == 0) {
+ if (strcasecmp(node->key, "write_buffer_mins") == 0) {
+ new_value = g_strdup_printf("%dmin", old_value);
+ new_key = "write_buffer_timeout";
+ } else if (strcasecmp(node->key, "write_buffer_kb") == 0) {
+ new_value = g_strdup_printf("%dk", old_value);
+ new_key = "write_buffer_size";
+ }
}
+
+ if (new_key != NULL || new_value != NULL) {
+ config_node_set_str(mainconfig, parent,
+ new_key != NULL ? new_key : node->key,
+ new_value != NULL ?
+ new_value : node->value);
+ if (new_key != NULL) {
+ /* remove old */
+ config_node_set_str(mainconfig, parent,
+ node->key, NULL);
+ }
+ config_changed = TRUE;
+ g_free(new_value);
+ }
+ return new_key != NULL;
}
/* verify that all settings in config file for `module' are actually found
void settings_check_module(const char *module)
{
SETTINGS_REC *set;
- CONFIG_NODE *node;
+ CONFIG_NODE *node, *parent;
GString *errors;
GSList *tmp, *next;
int count;
g_return_if_fail(module != NULL);
node = iconfig_node_traverse("settings", FALSE);
- if (node != NULL) {
- /* FIXME: remove after 0.7.98 */
- for (tmp = node->value; tmp != NULL; tmp = next) {
- CONFIG_NODE *node = tmp->data;
-
- next = tmp->next;
- if (node->type != NODE_TYPE_KEY)
- continue;
- set = g_hash_table_lookup(settings, node->key);
- if (set != NULL)
- settings_move(set, node->value);
- }
- }
node = node == NULL ? NULL : config_node_section(node, module, -1);
if (node == NULL) return;
"file for module %s:", module);
count = 0;
- for (tmp = node->value; tmp != NULL; tmp = tmp->next) {
+ parent = node;
+ tmp = config_node_first(node->value);
+ for (; tmp != NULL; tmp = next) {
node = tmp->data;
+ next = config_node_next(tmp);
set = g_hash_table_lookup(settings, node->key);
+ if (backwards_compatibility(module, node, parent))
+ continue;
+
if (set == NULL || strcmp(set->module, module) != 0) {
g_string_sprintfa(errors, " %s", node->key);
count++;
CONFIG_REC *config;
struct stat statbuf;
const char *path;
- char *real_fname;
+ char *str;
- real_fname = fname != NULL ? g_strdup(fname) :
- g_strdup_printf("%s"G_DIR_SEPARATOR_S".irssi"
- G_DIR_SEPARATOR_S"config", g_get_home_dir());
+ if (fname == NULL)
+ fname = get_irssi_config();
- if (stat(real_fname, &statbuf) == 0)
- path = real_fname;
+ if (stat(fname, &statbuf) == 0)
+ path = fname;
else {
/* user configuration file not found, use the default one
from sysconfdir */
- path = SYSCONFDIR"/irssi/config";
+ path = SYSCONFDIR"/"IRSSI_GLOBAL_CONFIG;
if (stat(path, &statbuf) != 0) {
/* no configuration file in sysconfdir ..
use the build-in configuration */
config = config_open(path, -1);
if (config == NULL) {
- last_config_error_msg =
- g_strdup_printf("Error opening configuration file %s: %s",
- path, g_strerror(errno));
+ 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)
+ if (config->fname != NULL)
config_parse(config);
else
config_parse_data(config, default_config, "internal");
- config_change_file_name(config, real_fname, 0660);
- irssi_config_save_state(real_fname);
- g_free(real_fname);
+ config_change_file_name(config, fname, 0660);
+ irssi_config_save_state(fname);
return config;
}
struct stat statbuf;
char *str;
- str = g_strdup_printf("%s"G_DIR_SEPARATOR_S".irssi", g_get_home_dir());
- if (stat(str, &statbuf) != 0) {
+ if (stat(get_irssi_dir(), &statbuf) != 0) {
/* ~/.irssi not found, create it. */
- if (mkpath(str, 0700) != 0) {
- g_error("Couldn't create %s directory", str);
+ 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 ~/.irssi", str);
+ "You should remove it with command: rm %s",
+ get_irssi_dir(), get_irssi_dir());
}
- g_free(str);
mainconfig = parse_configfile(NULL);
config_last_modifycounter = mainconfig->modifycounter;
/* any errors? */
if (config_last_error(mainconfig) != NULL) {
- last_config_error_msg =
- g_strdup_printf("Ignored errors in configuration "
- "file:\n%s",
- config_last_error(mainconfig));
+ 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);
CONFIG_REC *tempconfig;
char *str;
- if (fname == NULL) fname = "~/.irssi/config";
-
- str = convert_home(fname);
+ str = fname == NULL ? NULL : convert_home(fname);
tempconfig = parse_configfile(str);
- g_free(str);
+ g_free_not_null(str);
if (tempconfig == NULL) {
signal_emit("gui dialog", 2, "error", g_strerror(errno));
config_last_modifycounter = mainconfig->modifycounter;
signal_emit("setup changed", 0);
- signal_emit("setup reread", 0);
+ signal_emit("setup reread", 1, mainconfig->fname);
return TRUE;
}
-int settings_save(const char *fname)
+int settings_save(const char *fname, int autosave)
{
char *str;
int error;
signal_emit("gui dialog", 2, "error", str);
g_free(str);
}
+ signal_emit("setup saved", 2, fname, GINT_TO_POINTER(autosave));
return !error;
}
-static void sig_autosave(void)
+static int sig_autosave(void)
{
char *fname, *str;
if (!settings_get_bool("settings_autosave") ||
config_last_modifycounter == mainconfig->modifycounter)
- return;
+ return 1;
if (!irssi_config_is_changed(NULL))
- settings_save(NULL);
+ settings_save(NULL, TRUE);
else {
fname = g_strconcat(mainconfig->fname, ".autosave", NULL);
str = g_strdup_printf("Configuration file was modified "
signal_emit("gui dialog", 2, "warning", str);
g_free(str);
- settings_save(fname);
+ settings_save(fname, TRUE);
g_free(fname);
}
+
+ return 1;
}
void settings_init(void)
{
- settings = g_hash_table_new((GHashFunc) g_str_hash,
- (GCompareFunc) g_str_equal);
+ settings = g_hash_table_new((GHashFunc) g_istr_hash,
+ (GCompareFunc) g_istr_equal);
last_errors = NULL;
- last_config_error_msg = NULL;
last_invalid_modules = NULL;
fe_initialized = FALSE;
config_changed = FALSE;
init_configfile();
settings_add_bool("misc", "settings_autosave", TRUE);
- timeout_tag = g_timeout_add(1000*60*60, (GSourceFunc) sig_autosave, NULL);
+ timeout_tag = g_timeout_add(SETTINGS_AUTOSAVE_TIMEOUT,
+ (GSourceFunc) sig_autosave, NULL);
signal_add("irssi init finished", (SIGNAL_FUNC) sig_init_finished);
signal_add("gui exit", (SIGNAL_FUNC) sig_autosave);
}