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 printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP, TXT_SET_ITEM,
57 static void set_boolean(const char *key, const char *value)
59 if (g_strcasecmp(value, "ON") == 0)
60 settings_set_bool(key, TRUE);
61 else if (g_strcasecmp(value, "OFF") == 0)
62 settings_set_bool(key, FALSE);
63 else if (g_strcasecmp(value, "TOGGLE") == 0)
64 settings_set_bool(key, !settings_get_bool(key));
66 printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP, TXT_NOT_TOGGLE);
69 /* SYNTAX: SET [-clear] [<key> [<value>]] */
70 static void cmd_set(char *data)
74 const char *last_section;
79 if (!cmd_get_params(data, &free_arg, 2 | PARAM_FLAG_GETREST | PARAM_FLAG_OPTIONS,
80 "set", &optlist, &key, &value))
83 clear = g_hash_table_lookup(optlist, "clear") != NULL;
85 last_section = ""; found = 0;
86 sets = settings_get_sorted();
87 for (tmp = sets; tmp != NULL; tmp = tmp->next) {
88 SETTINGS_REC *rec = tmp->data;
90 if (((clear || *value != '\0') && g_strcasecmp(rec->key, key) != 0) ||
91 (*value == '\0' && *key != '\0' && stristr(rec->key, key) == NULL))
94 if (strcmp(last_section, rec->section) != 0) {
96 printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP,
97 TXT_SET_TITLE, rec->section);
98 last_section = rec->section;
101 if (clear || *value != '\0') {
102 /* change the setting */
104 case SETTING_TYPE_BOOLEAN:
106 settings_set_bool(key, FALSE);
108 set_boolean(key, value);
110 case SETTING_TYPE_INT:
111 settings_set_int(key, clear ? 0 : atoi(value));
113 case SETTING_TYPE_STRING:
114 settings_set_str(key, clear ? "" : value);
117 signal_emit("setup changed", 0);
123 if (clear || *value != '\0')
129 printformat(NULL, NULL, MSGLEVEL_CLIENTERROR,
130 TXT_SET_UNKNOWN, key);
133 cmd_params_free(free_arg);
136 /* SYNTAX: TOGGLE <key> [on|off|toggle] */
137 static void cmd_toggle(const char *data)
143 if (!cmd_get_params(data, &free_arg, 2 | PARAM_FLAG_GETREST, &key, &value))
146 if (*key == '\0') cmd_param_error(CMDERR_NOT_ENOUGH_PARAMS);
148 type = settings_get_type(key);
150 printformat(NULL, NULL, MSGLEVEL_CLIENTERROR, TXT_SET_UNKNOWN, key);
151 else if (type != SETTING_TYPE_BOOLEAN)
152 printformat(NULL, NULL, MSGLEVEL_CLIENTERROR, TXT_SET_NOT_BOOLEAN, key);
154 set_boolean(key, *value != '\0' ? value : "TOGGLE");
155 set_print(settings_get_record(key));
156 signal_emit("setup changed", 0);
159 cmd_params_free(free_arg);
162 static int config_key_compare(CONFIG_NODE *node1, CONFIG_NODE *node2)
164 return g_strcasecmp(node1->key, node2->key);
167 static void show_aliases(const char *alias)
173 printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP, TXT_ALIASLIST_HEADER);
175 node = iconfig_node_traverse("aliases", FALSE);
176 tmp = node == NULL ? NULL : config_node_first(node->value);
178 /* first get the list of aliases sorted */
180 aliaslen = strlen(alias);
181 for (; tmp != NULL; tmp = config_node_next(tmp)) {
182 CONFIG_NODE *node = tmp->data;
184 if (node->type != NODE_TYPE_KEY)
187 if (aliaslen != 0 && g_strncasecmp(node->key, alias, aliaslen) != 0)
190 list = g_slist_insert_sorted(list, node, (GCompareFunc) config_key_compare);
193 /* print the aliases */
194 for (tmp = list; tmp != NULL; tmp = tmp->next) {
195 CONFIG_NODE *node = tmp->data;
197 printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP, TXT_ALIASLIST_LINE,
198 node->key, node->value);
202 printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP, TXT_ALIASLIST_FOOTER);
205 static void alias_remove(const char *alias)
207 if (iconfig_get_str("aliases", alias, NULL) == NULL)
208 printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE, TXT_ALIAS_NOT_FOUND, alias);
210 printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE, TXT_ALIAS_REMOVED, alias);
211 iconfig_set_str("aliases", alias, NULL);
215 /* SYNTAX: ALIAS [[-]<alias> [<command>]] */
216 static void cmd_alias(const char *data)
221 g_return_if_fail(data != NULL);
223 if (!cmd_get_params(data, &free_arg, 2 | PARAM_FLAG_GETREST, &alias, &value))
227 if (alias[1] != '\0') alias_remove(alias+1);
228 } else if (*alias == '\0' || *value == '\0')
231 printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE, TXT_ALIAS_ADDED, alias);
232 iconfig_set_str("aliases", alias, value);
234 cmd_params_free(free_arg);
237 /* SYNTAX: UNALIAS <alias> */
238 static void cmd_unalias(const char *data)
240 g_return_if_fail(data != NULL);
241 if (*data == '\0') cmd_return_error(CMDERR_NOT_ENOUGH_PARAMS);
246 /* SYNTAX: RELOAD [<file>] */
247 static void cmd_reload(const char *data)
251 fname = *data == '\0' ? get_irssi_config() : data;
253 if (settings_reread(fname)) {
254 printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE,
255 TXT_CONFIG_RELOADED, fname);
259 static void settings_save_fe(const char *fname)
261 if (settings_save(fname, FALSE /* not autosaved */)) {
262 printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE,
263 TXT_CONFIG_SAVED, fname);
267 static void settings_save_confirm(const char *line, char *fname)
269 if (i_toupper(line[0]) == 'Y')
270 settings_save_fe(fname);
274 /* SYNTAX: SAVE [<file>] */
275 static void cmd_save(const char *data)
278 char *format, *fname;
281 if (!cmd_get_params(data, &free_arg, 1 | PARAM_FLAG_OPTIONS,
282 "save", &optlist, &fname))
286 fname = mainconfig->fname;
288 if (!irssi_config_is_changed(fname))
289 settings_save_fe(fname);
291 /* config file modified outside irssi */
292 printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE,
293 TXT_CONFIG_MODIFIED, fname);
295 format = format_get_text(MODULE_NAME, NULL, NULL, NULL,
296 TXT_OVERWRITE_CONFIG);
297 keyboard_entry_redirect((SIGNAL_FUNC) settings_save_confirm,
298 format, 0, g_strdup(fname));
302 cmd_params_free(free_arg);
305 static void settings_clean_confirm(const char *line)
307 if (i_toupper(line[0]) == 'Y')
308 settings_clean_invalid();
311 static void sig_settings_errors(const char *msg)
313 printtext(NULL, NULL, MSGLEVEL_CLIENTERROR, "%s", msg);
314 keyboard_entry_redirect((SIGNAL_FUNC) settings_clean_confirm,
315 "Remove unknown settings from config file (Y/n)?",
319 void fe_settings_init(void)
321 command_bind("set", NULL, (SIGNAL_FUNC) cmd_set);
322 command_bind("toggle", NULL, (SIGNAL_FUNC) cmd_toggle);
323 command_bind("alias", NULL, (SIGNAL_FUNC) cmd_alias);
324 command_bind("unalias", NULL, (SIGNAL_FUNC) cmd_unalias);
325 command_bind("reload", NULL, (SIGNAL_FUNC) cmd_reload);
326 command_bind("save", NULL, (SIGNAL_FUNC) cmd_save);
327 command_set_options("set", "clear");
329 signal_add("settings errors", (SIGNAL_FUNC) sig_settings_errors);
332 void fe_settings_deinit(void)
334 command_unbind("set", (SIGNAL_FUNC) cmd_set);
335 command_unbind("toggle", (SIGNAL_FUNC) cmd_toggle);
336 command_unbind("alias", (SIGNAL_FUNC) cmd_alias);
337 command_unbind("unalias", (SIGNAL_FUNC) cmd_unalias);
338 command_unbind("reload", (SIGNAL_FUNC) cmd_reload);
339 command_unbind("save", (SIGNAL_FUNC) cmd_save);
341 signal_remove("settings errors", (SIGNAL_FUNC) sig_settings_errors);