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 = "";
|
|
|
|
}
|
|
|
|
printtext(NULL, NULL, MSGLEVEL_CLIENTCRAP, "%s = %s", rec->key, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2000-07-23 19:19:22 -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 */
|
|
|
|
printtext(NULL, NULL, MSGLEVEL_CLIENTCRAP, "%_[ %s ]", rec->section);
|
|
|
|
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:
|
2000-07-04 15:36:36 -04:00
|
|
|
set_boolean(key, clear ? FALSE : 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);
|
|
|
|
|
|
|
|
if (!found)
|
|
|
|
printtext(NULL, NULL, MSGLEVEL_CLIENTERROR, "Unknown setting %s", key);
|
|
|
|
|
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)
|
|
|
|
printtext(NULL, NULL, MSGLEVEL_CLIENTERROR, "Unknown setting %_%s", key);
|
|
|
|
else if (type != SETTING_TYPE_BOOLEAN)
|
|
|
|
printtext(NULL, NULL, MSGLEVEL_CLIENTERROR, "Setting %_%s%_ isn't boolean, use /SET", key);
|
|
|
|
else {
|
|
|
|
set_boolean(key, *value != '\0' ? value : "TOGGLE");
|
|
|
|
set_print(settings_get_record(key));
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
char *fname;
|
|
|
|
|
|
|
|
fname = *data != '\0' ? g_strdup(data) :
|
|
|
|
g_strdup_printf("%s/.irssi/config", g_get_home_dir());
|
|
|
|
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
|
|
|
}
|
|
|
|
g_free(fname);
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
2000-12-03 01:39:28 -05:00
|
|
|
if (g_strncasecmp(line, _("Y"), 1) == 0)
|
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)
|
|
|
|
{
|
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-01-04 13:01:51 -05:00
|
|
|
keyboard_entry_redirect((SIGNAL_FUNC) settings_save_confirm,
|
|
|
|
_("Overwrite config (y/N)?"),
|
|
|
|
0, g_strdup(data));
|
2000-07-22 18:05:29 -04:00
|
|
|
}
|
|
|
|
|
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");
|
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);
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|