4 Copyright (C) 1999 Timo Sirainen
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "module-formats.h"
27 #include "lib-config/iconfig.h"
31 #include "printtext.h"
34 static void set_print(SETTINGS_REC *rec)
37 char value_int[MAX_INT_STRLEN];
40 case SETTING_TYPE_BOOLEAN:
41 value = settings_get_bool(rec->key) ? "ON" : "OFF";
43 case SETTING_TYPE_INT:
44 ltoa(value_int, settings_get_int(rec->key));
47 case SETTING_TYPE_STRING:
48 value = settings_get_str(rec->key);
53 printtext(NULL, NULL, MSGLEVEL_CLIENTCRAP, "%s = %s", rec->key, value);
56 static void set_boolean(const char *key, const char *value)
58 if (g_strcasecmp(value, "ON") == 0)
59 settings_set_bool(key, TRUE);
60 else if (g_strcasecmp(value, "OFF") == 0)
61 settings_set_bool(key, FALSE);
62 else if (g_strcasecmp(value, "TOGGLE") == 0)
63 settings_set_bool(key, !settings_get_bool(key));
65 printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP, TXT_NOT_TOGGLE);
68 /* SYNTAX: SET [-clear] [<key> [<value>]] */
69 static void cmd_set(char *data)
73 const char *last_section;
78 if (!cmd_get_params(data, &free_arg, 2 | PARAM_FLAG_GETREST | PARAM_FLAG_OPTIONS,
79 "set", &optlist, &key, &value))
82 clear = g_hash_table_lookup(optlist, "clear") != NULL;
84 last_section = ""; found = 0;
85 sets = settings_get_sorted();
86 for (tmp = sets; tmp != NULL; tmp = tmp->next) {
87 SETTINGS_REC *rec = tmp->data;
89 if (((clear || *value != '\0') && g_strcasecmp(rec->key, key) != 0) ||
90 (*value == '\0' && *key != '\0' && stristr(rec->key, key) == NULL))
93 if (strcmp(last_section, rec->section) != 0) {
95 printtext(NULL, NULL, MSGLEVEL_CLIENTCRAP, "%_[ %s ]", rec->section);
96 last_section = rec->section;
99 if (clear || *value != '\0') {
100 /* change the setting */
102 case SETTING_TYPE_BOOLEAN:
104 settings_set_bool(key, FALSE);
106 set_boolean(key, value);
108 case SETTING_TYPE_INT:
109 settings_set_int(key, clear ? 0 : atoi(value));
111 case SETTING_TYPE_STRING:
112 settings_set_str(key, clear ? "" : value);
115 signal_emit("setup changed", 0);
121 if (clear || *value != '\0')
127 printtext(NULL, NULL, MSGLEVEL_CLIENTERROR, "Unknown setting %s", key);
129 cmd_params_free(free_arg);
132 /* SYNTAX: TOGGLE <key> [on|off|toggle] */
133 static void cmd_toggle(const char *data)
139 if (!cmd_get_params(data, &free_arg, 2 | PARAM_FLAG_GETREST, &key, &value))
142 if (*key == '\0') cmd_param_error(CMDERR_NOT_ENOUGH_PARAMS);
144 type = settings_get_type(key);
146 printtext(NULL, NULL, MSGLEVEL_CLIENTERROR, "Unknown setting %_%s", key);
147 else if (type != SETTING_TYPE_BOOLEAN)
148 printtext(NULL, NULL, MSGLEVEL_CLIENTERROR, "Setting %_%s%_ isn't boolean, use /SET", key);
150 set_boolean(key, *value != '\0' ? value : "TOGGLE");
151 set_print(settings_get_record(key));
154 cmd_params_free(free_arg);
157 static int config_key_compare(CONFIG_NODE *node1, CONFIG_NODE *node2)
159 return g_strcasecmp(node1->key, node2->key);
162 static void show_aliases(const char *alias)
168 printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP, TXT_ALIASLIST_HEADER);
170 node = iconfig_node_traverse("aliases", FALSE);
171 tmp = node == NULL ? NULL : node->value;
173 /* first get the list of aliases sorted */
175 aliaslen = strlen(alias);
176 for (; tmp != NULL; tmp = tmp->next) {
177 CONFIG_NODE *node = tmp->data;
179 if (node->type != NODE_TYPE_KEY)
182 if (aliaslen != 0 && g_strncasecmp(node->key, alias, aliaslen) != 0)
185 list = g_slist_insert_sorted(list, node, (GCompareFunc) config_key_compare);
188 /* print the aliases */
189 for (tmp = list; tmp != NULL; tmp = tmp->next) {
190 CONFIG_NODE *node = tmp->data;
192 printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP, TXT_ALIASLIST_LINE,
193 node->key, node->value);
197 printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP, TXT_ALIASLIST_FOOTER);
200 static void alias_remove(const char *alias)
202 if (iconfig_get_str("aliases", alias, NULL) == NULL)
203 printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE, TXT_ALIAS_NOT_FOUND, alias);
205 printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE, TXT_ALIAS_REMOVED, alias);
206 iconfig_set_str("aliases", alias, NULL);
210 /* SYNTAX: ALIAS [[-]<alias> [<command>]] */
211 static void cmd_alias(const char *data)
216 g_return_if_fail(data != NULL);
218 if (!cmd_get_params(data, &free_arg, 2 | PARAM_FLAG_GETREST, &alias, &value))
222 if (alias[1] != '\0') alias_remove(alias+1);
223 } else if (*alias == '\0' || *value == '\0')
226 printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE, TXT_ALIAS_ADDED, alias);
227 iconfig_set_str("aliases", alias, value);
229 cmd_params_free(free_arg);
232 /* SYNTAX: UNALIAS <alias> */
233 static void cmd_unalias(const char *data)
235 g_return_if_fail(data != NULL);
236 if (*data == '\0') cmd_return_error(CMDERR_NOT_ENOUGH_PARAMS);
241 /* SYNTAX: RELOAD [<file>] */
242 static void cmd_reload(const char *data)
246 fname = *data != '\0' ? g_strdup(data) :
247 g_strdup_printf("%s/.irssi/config", g_get_home_dir());
248 if (settings_reread(fname)) {
249 printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE,
250 TXT_CONFIG_RELOADED, fname);
255 static void settings_save_fe(const char *fname)
257 if (settings_save(fname)) {
258 printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE,
259 TXT_CONFIG_SAVED, fname);
263 static void settings_save_confirm(const char *line, char *fname)
266 settings_save_fe(fname);
270 /* SYNTAX: SAVE [<file>] */
271 static void cmd_save(const char *data)
276 data = mainconfig->fname;
278 if (!irssi_config_is_changed(data)) {
279 settings_save_fe(data);
283 printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE,
284 TXT_CONFIG_MODIFIED, data);
286 format = format_get_text(MODULE_NAME, NULL, NULL, NULL,
287 TXT_OVERWRITE_CONFIG);
288 keyboard_entry_redirect((SIGNAL_FUNC) settings_save_confirm,
289 format, 0, g_strdup(data));
293 static void settings_clean_confirm(const char *line)
296 settings_clean_invalid();
299 static void sig_settings_errors(const char *msg)
301 printtext(NULL, NULL, MSGLEVEL_CLIENTERROR, "%s", msg);
302 keyboard_entry_redirect((SIGNAL_FUNC) settings_clean_confirm,
303 "Remove unknown settings from config file (Y/n)?",
307 void fe_settings_init(void)
309 command_bind("set", NULL, (SIGNAL_FUNC) cmd_set);
310 command_bind("toggle", NULL, (SIGNAL_FUNC) cmd_toggle);
311 command_bind("alias", NULL, (SIGNAL_FUNC) cmd_alias);
312 command_bind("unalias", NULL, (SIGNAL_FUNC) cmd_unalias);
313 command_bind("reload", NULL, (SIGNAL_FUNC) cmd_reload);
314 command_bind("save", NULL, (SIGNAL_FUNC) cmd_save);
315 command_set_options("set", "clear");
317 signal_add("settings errors", (SIGNAL_FUNC) sig_settings_errors);
320 void fe_settings_deinit(void)
322 command_unbind("set", (SIGNAL_FUNC) cmd_set);
323 command_unbind("toggle", (SIGNAL_FUNC) cmd_toggle);
324 command_unbind("alias", (SIGNAL_FUNC) cmd_alias);
325 command_unbind("unalias", (SIGNAL_FUNC) cmd_unalias);
326 command_unbind("reload", (SIGNAL_FUNC) cmd_reload);
327 command_unbind("save", (SIGNAL_FUNC) cmd_save);
329 signal_remove("settings errors", (SIGNAL_FUNC) sig_settings_errors);