2000-04-26 04:03:38 -04:00
|
|
|
/*
|
|
|
|
fe-settings.c : irssi
|
|
|
|
|
|
|
|
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 "module-formats.h"
|
|
|
|
#include "signals.h"
|
|
|
|
#include "commands.h"
|
2000-08-26 11:39:44 -04:00
|
|
|
#include "servers.h"
|
2000-05-04 06:32:42 -04:00
|
|
|
#include "misc.h"
|
2000-04-26 04:03:38 -04:00
|
|
|
#include "lib-config/iconfig.h"
|
|
|
|
#include "settings.h"
|
|
|
|
|
|
|
|
#include "levels.h"
|
2000-10-28 16:14:19 -04:00
|
|
|
#include "printtext.h"
|
2001-01-04 13:01:51 -05:00
|
|
|
#include "keyboard.h"
|
2000-04-26 04:03:38 -04:00
|
|
|
|
|
|
|
static void set_print(SETTINGS_REC *rec)
|
|
|
|
{
|
|
|
|
const char *value;
|
|
|
|
char value_int[MAX_INT_STRLEN];
|
|
|
|
|
|
|
|
switch (rec->type) {
|
|
|
|
case SETTING_TYPE_BOOLEAN:
|
|
|
|
value = settings_get_bool(rec->key) ? "ON" : "OFF";
|
|
|
|
break;
|
|
|
|
case SETTING_TYPE_INT:
|
2000-07-04 15:36:36 -04:00
|
|
|
ltoa(value_int, settings_get_int(rec->key));
|
2000-04-26 04:03:38 -04:00
|
|
|
value = value_int;
|
|
|
|
break;
|
|
|
|
case SETTING_TYPE_STRING:
|
|
|
|
value = settings_get_str(rec->key);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
value = "";
|
|
|
|
}
|
2001-05-26 15:20:45 -04:00
|
|
|
printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP, TXT_SET_ITEM,
|
|
|
|
rec->key, value);
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void set_boolean(const char *key, const char *value)
|
|
|
|
{
|
|
|
|
if (g_strcasecmp(value, "ON") == 0)
|
2000-12-17 00:44:45 -05:00
|
|
|
settings_set_bool(key, TRUE);
|
2000-04-26 04:03:38 -04:00
|
|
|
else if (g_strcasecmp(value, "OFF") == 0)
|
2000-12-17 00:44:45 -05:00
|
|
|
settings_set_bool(key, FALSE);
|
2000-04-26 04:03:38 -04:00
|
|
|
else if (g_strcasecmp(value, "TOGGLE") == 0)
|
2000-12-17 00:44:45 -05:00
|
|
|
settings_set_bool(key, !settings_get_bool(key));
|
2000-04-26 04:03:38 -04:00
|
|
|
else
|
2001-01-07 14:42:59 -05:00
|
|
|
printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP, TXT_NOT_TOGGLE);
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
2001-04-09 12:16:09 -04:00
|
|
|
/* SYNTAX: SET [-clear] [<key> [<value>]] */
|
2000-04-26 04:03:38 -04:00
|
|
|
static void cmd_set(char *data)
|
|
|
|
{
|
2000-07-04 15:36:36 -04:00
|
|
|
GHashTable *optlist;
|
2000-04-26 04:03:38 -04:00
|
|
|
GSList *sets, *tmp;
|
2000-07-10 19:00:56 -04:00
|
|
|
const char *last_section;
|
|
|
|
char *key, *value;
|
2000-06-17 21:18:12 -04:00
|
|
|
void *free_arg;
|
2000-07-04 15:36:36 -04:00
|
|
|
int found, clear;
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2000-07-04 15:36:36 -04:00
|
|
|
if (!cmd_get_params(data, &free_arg, 2 | PARAM_FLAG_GETREST | PARAM_FLAG_OPTIONS,
|
|
|
|
"set", &optlist, &key, &value))
|
2000-06-17 21:18:12 -04:00
|
|
|
return;
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2000-07-04 15:36:36 -04:00
|
|
|
clear = g_hash_table_lookup(optlist, "clear") != NULL;
|
|
|
|
|
2000-04-26 04:03:38 -04:00
|
|
|
last_section = ""; found = 0;
|
|
|
|
sets = settings_get_sorted();
|
|
|
|
for (tmp = sets; tmp != NULL; tmp = tmp->next) {
|
|
|
|
SETTINGS_REC *rec = tmp->data;
|
|
|
|
|
2000-07-04 15:36:36 -04:00
|
|
|
if (((clear || *value != '\0') && g_strcasecmp(rec->key, key) != 0) ||
|
2000-05-04 06:32:42 -04:00
|
|
|
(*value == '\0' && *key != '\0' && stristr(rec->key, key) == NULL))
|
2000-04-26 04:03:38 -04:00
|
|
|
continue;
|
|
|
|
|
|
|
|
if (strcmp(last_section, rec->section) != 0) {
|
|
|
|
/* print section */
|
2001-05-26 15:20:45 -04:00
|
|
|
printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP,
|
|
|
|
TXT_SET_TITLE, rec->section);
|
2000-04-26 04:03:38 -04:00
|
|
|
last_section = rec->section;
|
|
|
|
}
|
|
|
|
|
2000-07-04 15:36:36 -04:00
|
|
|
if (clear || *value != '\0') {
|
2000-04-26 04:03:38 -04:00
|
|
|
/* change the setting */
|
|
|
|
switch (rec->type) {
|
|
|
|
case SETTING_TYPE_BOOLEAN:
|
2001-03-03 11:14:28 -05:00
|
|
|
if (clear)
|
|
|
|
settings_set_bool(key, FALSE);
|
|
|
|
else
|
|
|
|
set_boolean(key, value);
|
2000-04-26 04:03:38 -04:00
|
|
|
break;
|
|
|
|
case SETTING_TYPE_INT:
|
2000-12-17 00:44:45 -05:00
|
|
|
settings_set_int(key, clear ? 0 : atoi(value));
|
2000-04-26 04:03:38 -04:00
|
|
|
break;
|
|
|
|
case SETTING_TYPE_STRING:
|
2000-12-17 00:44:45 -05:00
|
|
|
settings_set_str(key, clear ? "" : value);
|
2000-04-26 04:03:38 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
signal_emit("setup changed", 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
set_print(rec);
|
|
|
|
found = TRUE;
|
2000-07-04 15:36:36 -04:00
|
|
|
|
|
|
|
if (clear || *value != '\0')
|
|
|
|
break;
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
g_slist_free(sets);
|
|
|
|
|
2001-05-26 15:20:45 -04:00
|
|
|
if (!found) {
|
|
|
|
printformat(NULL, NULL, MSGLEVEL_CLIENTERROR,
|
|
|
|
TXT_SET_UNKNOWN, key);
|
|
|
|
}
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2000-06-17 21:18:12 -04:00
|
|
|
cmd_params_free(free_arg);
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
2000-07-23 19:19:22 -04:00
|
|
|
/* SYNTAX: TOGGLE <key> [on|off|toggle] */
|
2000-04-26 04:03:38 -04:00
|
|
|
static void cmd_toggle(const char *data)
|
|
|
|
{
|
2000-06-17 21:18:12 -04:00
|
|
|
char *key, *value;
|
|
|
|
void *free_arg;
|
2000-04-26 04:03:38 -04:00
|
|
|
int type;
|
|
|
|
|
2000-06-17 21:18:12 -04:00
|
|
|
if (!cmd_get_params(data, &free_arg, 2 | PARAM_FLAG_GETREST, &key, &value))
|
|
|
|
return;
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2001-02-19 02:16:32 -05:00
|
|
|
if (*key == '\0') cmd_param_error(CMDERR_NOT_ENOUGH_PARAMS);
|
|
|
|
|
2000-04-26 04:03:38 -04:00
|
|
|
type = settings_get_type(key);
|
|
|
|
if (type == -1)
|
2001-05-26 15:20:45 -04:00
|
|
|
printformat(NULL, NULL, MSGLEVEL_CLIENTERROR, TXT_SET_UNKNOWN, key);
|
2000-04-26 04:03:38 -04:00
|
|
|
else if (type != SETTING_TYPE_BOOLEAN)
|
2001-05-26 15:20:45 -04:00
|
|
|
printformat(NULL, NULL, MSGLEVEL_CLIENTERROR, TXT_SET_NOT_BOOLEAN, key);
|
2000-04-26 04:03:38 -04:00
|
|
|
else {
|
|
|
|
set_boolean(key, *value != '\0' ? value : "TOGGLE");
|
|
|
|
set_print(settings_get_record(key));
|
2001-07-30 08:24:24 -04:00
|
|
|
signal_emit("setup changed", 0);
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
2000-06-17 21:18:12 -04:00
|
|
|
cmd_params_free(free_arg);
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
2000-06-18 06:25:39 -04:00
|
|
|
static int config_key_compare(CONFIG_NODE *node1, CONFIG_NODE *node2)
|
|
|
|
{
|
|
|
|
return g_strcasecmp(node1->key, node2->key);
|
|
|
|
}
|
|
|
|
|
2000-04-26 04:03:38 -04:00
|
|
|
static void show_aliases(const char *alias)
|
|
|
|
{
|
|
|
|
CONFIG_NODE *node;
|
2000-06-18 06:25:39 -04:00
|
|
|
GSList *tmp, *list;
|
2000-04-26 04:03:38 -04:00
|
|
|
int aliaslen;
|
|
|
|
|
2001-01-07 14:42:59 -05:00
|
|
|
printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP, TXT_ALIASLIST_HEADER);
|
2000-04-26 04:03:38 -04:00
|
|
|
|
|
|
|
node = iconfig_node_traverse("aliases", FALSE);
|
|
|
|
tmp = node == NULL ? NULL : node->value;
|
|
|
|
|
2000-06-18 06:25:39 -04:00
|
|
|
/* first get the list of aliases sorted */
|
|
|
|
list = NULL;
|
2000-04-26 04:03:38 -04:00
|
|
|
aliaslen = strlen(alias);
|
|
|
|
for (; tmp != NULL; tmp = tmp->next) {
|
|
|
|
CONFIG_NODE *node = tmp->data;
|
|
|
|
|
|
|
|
if (node->type != NODE_TYPE_KEY)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (aliaslen != 0 && g_strncasecmp(node->key, alias, aliaslen) != 0)
|
|
|
|
continue;
|
|
|
|
|
2000-06-18 06:25:39 -04:00
|
|
|
list = g_slist_insert_sorted(list, node, (GCompareFunc) config_key_compare);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* print the aliases */
|
|
|
|
for (tmp = list; tmp != NULL; tmp = tmp->next) {
|
|
|
|
CONFIG_NODE *node = tmp->data;
|
|
|
|
|
2001-01-07 14:42:59 -05:00
|
|
|
printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP, TXT_ALIASLIST_LINE,
|
2000-04-26 04:03:38 -04:00
|
|
|
node->key, node->value);
|
|
|
|
}
|
2000-06-18 06:25:39 -04:00
|
|
|
g_slist_free(list);
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2001-01-07 14:42:59 -05:00
|
|
|
printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP, TXT_ALIASLIST_FOOTER);
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void alias_remove(const char *alias)
|
|
|
|
{
|
|
|
|
if (iconfig_get_str("aliases", alias, NULL) == NULL)
|
2001-01-07 14:42:59 -05:00
|
|
|
printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE, TXT_ALIAS_NOT_FOUND, alias);
|
2000-04-26 04:03:38 -04:00
|
|
|
else {
|
2001-01-07 14:42:59 -05:00
|
|
|
printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE, TXT_ALIAS_REMOVED, alias);
|
2000-04-26 04:03:38 -04:00
|
|
|
iconfig_set_str("aliases", alias, NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-07-23 19:19:22 -04:00
|
|
|
/* SYNTAX: ALIAS [[-]<alias> [<command>]] */
|
2000-04-26 04:03:38 -04:00
|
|
|
static void cmd_alias(const char *data)
|
|
|
|
{
|
2000-06-17 21:18:12 -04:00
|
|
|
char *alias, *value;
|
|
|
|
void *free_arg;
|
2000-04-26 04:03:38 -04:00
|
|
|
|
|
|
|
g_return_if_fail(data != NULL);
|
|
|
|
|
2000-06-17 21:18:12 -04:00
|
|
|
if (!cmd_get_params(data, &free_arg, 2 | PARAM_FLAG_GETREST, &alias, &value))
|
|
|
|
return;
|
|
|
|
|
2000-04-26 04:03:38 -04:00
|
|
|
if (*alias == '-') {
|
|
|
|
if (alias[1] != '\0') alias_remove(alias+1);
|
|
|
|
} else if (*alias == '\0' || *value == '\0')
|
|
|
|
show_aliases(alias);
|
|
|
|
else {
|
2001-01-07 14:42:59 -05:00
|
|
|
printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE, TXT_ALIAS_ADDED, alias);
|
2000-04-26 04:03:38 -04:00
|
|
|
iconfig_set_str("aliases", alias, value);
|
|
|
|
}
|
2000-06-17 21:18:12 -04:00
|
|
|
cmd_params_free(free_arg);
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
2000-07-23 19:19:22 -04:00
|
|
|
/* SYNTAX: UNALIAS <alias> */
|
2000-04-26 04:03:38 -04:00
|
|
|
static void cmd_unalias(const char *data)
|
|
|
|
{
|
|
|
|
g_return_if_fail(data != NULL);
|
|
|
|
if (*data == '\0') cmd_return_error(CMDERR_NOT_ENOUGH_PARAMS);
|
|
|
|
|
|
|
|
alias_remove(data);
|
|
|
|
}
|
|
|
|
|
2000-07-23 19:19:22 -04:00
|
|
|
/* SYNTAX: RELOAD [<file>] */
|
2000-07-22 18:05:29 -04:00
|
|
|
static void cmd_reload(const char *data)
|
|
|
|
{
|
2001-07-28 20:27:23 -04:00
|
|
|
const char *fname;
|
|
|
|
|
|
|
|
fname = *data == '\0' ? get_irssi_config() : data;
|
2000-07-22 18:05:29 -04:00
|
|
|
|
|
|
|
if (settings_reread(fname)) {
|
|
|
|
printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE,
|
2001-01-07 14:42:59 -05:00
|
|
|
TXT_CONFIG_RELOADED, fname);
|
2000-07-22 18:05:29 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-10-15 15:21:21 -04:00
|
|
|
static void settings_save_fe(const char *fname)
|
|
|
|
{
|
|
|
|
if (settings_save(fname)) {
|
|
|
|
printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE,
|
2001-01-07 14:42:59 -05:00
|
|
|
TXT_CONFIG_SAVED, fname);
|
2000-10-15 15:21:21 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void settings_save_confirm(const char *line, char *fname)
|
|
|
|
{
|
2001-06-01 16:20:22 -04:00
|
|
|
if (toupper(line[0]) == 'Y')
|
2000-10-15 15:21:21 -04:00
|
|
|
settings_save_fe(fname);
|
|
|
|
g_free(fname);
|
|
|
|
}
|
|
|
|
|
2000-07-23 19:19:22 -04:00
|
|
|
/* SYNTAX: SAVE [<file>] */
|
2000-07-22 18:05:29 -04:00
|
|
|
static void cmd_save(const char *data)
|
|
|
|
{
|
2001-05-17 16:13:59 -04:00
|
|
|
char *format;
|
|
|
|
|
2000-10-15 15:21:21 -04:00
|
|
|
if (*data == '\0')
|
|
|
|
data = mainconfig->fname;
|
|
|
|
|
|
|
|
if (!irssi_config_is_changed(data)) {
|
|
|
|
settings_save_fe(data);
|
|
|
|
return;
|
2000-07-22 18:05:29 -04:00
|
|
|
}
|
2000-10-15 15:21:21 -04:00
|
|
|
|
|
|
|
printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE,
|
2001-01-07 14:42:59 -05:00
|
|
|
TXT_CONFIG_MODIFIED, data);
|
2001-05-17 16:13:59 -04:00
|
|
|
|
|
|
|
format = format_get_text(MODULE_NAME, NULL, NULL, NULL,
|
|
|
|
TXT_OVERWRITE_CONFIG);
|
2001-01-04 13:01:51 -05:00
|
|
|
keyboard_entry_redirect((SIGNAL_FUNC) settings_save_confirm,
|
2001-05-17 16:13:59 -04:00
|
|
|
format, 0, g_strdup(data));
|
|
|
|
g_free(format);
|
2000-07-22 18:05:29 -04:00
|
|
|
}
|
|
|
|
|
2001-03-16 18:57:24 -05:00
|
|
|
static void settings_clean_confirm(const char *line)
|
|
|
|
{
|
2001-06-01 16:20:22 -04:00
|
|
|
if (toupper(line[0]) == 'Y')
|
2001-03-16 18:57:24 -05:00
|
|
|
settings_clean_invalid();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void sig_settings_errors(const char *msg)
|
|
|
|
{
|
|
|
|
printtext(NULL, NULL, MSGLEVEL_CLIENTERROR, "%s", msg);
|
|
|
|
keyboard_entry_redirect((SIGNAL_FUNC) settings_clean_confirm,
|
2001-05-17 16:13:59 -04:00
|
|
|
"Remove unknown settings from config file (Y/n)?",
|
2001-03-16 18:57:24 -05:00
|
|
|
0, NULL);
|
|
|
|
}
|
|
|
|
|
2000-04-26 04:03:38 -04:00
|
|
|
void fe_settings_init(void)
|
|
|
|
{
|
|
|
|
command_bind("set", NULL, (SIGNAL_FUNC) cmd_set);
|
|
|
|
command_bind("toggle", NULL, (SIGNAL_FUNC) cmd_toggle);
|
|
|
|
command_bind("alias", NULL, (SIGNAL_FUNC) cmd_alias);
|
|
|
|
command_bind("unalias", NULL, (SIGNAL_FUNC) cmd_unalias);
|
2000-07-22 18:05:29 -04:00
|
|
|
command_bind("reload", NULL, (SIGNAL_FUNC) cmd_reload);
|
|
|
|
command_bind("save", NULL, (SIGNAL_FUNC) cmd_save);
|
2000-07-04 15:36:36 -04:00
|
|
|
command_set_options("set", "clear");
|
2001-03-16 18:57:24 -05:00
|
|
|
|
|
|
|
signal_add("settings errors", (SIGNAL_FUNC) sig_settings_errors);
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void fe_settings_deinit(void)
|
|
|
|
{
|
|
|
|
command_unbind("set", (SIGNAL_FUNC) cmd_set);
|
|
|
|
command_unbind("toggle", (SIGNAL_FUNC) cmd_toggle);
|
|
|
|
command_unbind("alias", (SIGNAL_FUNC) cmd_alias);
|
|
|
|
command_unbind("unalias", (SIGNAL_FUNC) cmd_unalias);
|
2000-07-22 18:05:29 -04:00
|
|
|
command_unbind("reload", (SIGNAL_FUNC) cmd_reload);
|
|
|
|
command_unbind("save", (SIGNAL_FUNC) cmd_save);
|
2001-03-16 18:57:24 -05:00
|
|
|
|
|
|
|
signal_remove("settings errors", (SIGNAL_FUNC) sig_settings_errors);
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|