7fb512e8446172150bad00929e60478e0b626497
[crypto.git] / apps / irssi / src / fe-common / core / fe-settings.c
1 /*
2  fe-settings.c : irssi
3
4     Copyright (C) 1999 Timo Sirainen
5
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.
10
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.
15
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
19 */
20
21 #include "module.h"
22 #include "module-formats.h"
23 #include "signals.h"
24 #include "commands.h"
25 #include "servers.h"
26 #include "misc.h"
27 #include "lib-config/iconfig.h"
28 #include "settings.h"
29
30 #include "levels.h"
31 #include "printtext.h"
32 #include "keyboard.h"
33
34 static void set_print(SETTINGS_REC *rec)
35 {
36         const char *value;
37         char value_int[MAX_INT_STRLEN];
38
39         switch (rec->type) {
40         case SETTING_TYPE_BOOLEAN:
41                 value = settings_get_bool(rec->key) ? "ON" : "OFF";
42                 break;
43         case SETTING_TYPE_INT:
44                 ltoa(value_int, settings_get_int(rec->key));
45                 value = value_int;
46                 break;
47         case SETTING_TYPE_STRING:
48                 value = settings_get_str(rec->key);
49                 break;
50         default:
51                 value = "";
52         }
53         printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP, TXT_SET_ITEM,
54                     rec->key, value);
55 }
56
57 static void set_boolean(const char *key, const char *value)
58 {
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));
65         else
66                 printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP, TXT_NOT_TOGGLE);
67 }
68
69 /* SYNTAX: SET [-clear | -default] [<key> [<value>]] */
70 static void cmd_set(char *data)
71 {
72         GHashTable *optlist;
73         GSList *sets, *tmp;
74         const char *last_section;
75         char *key, *value;
76         void *free_arg;
77         int found, clear, set_default;
78
79         if (!cmd_get_params(data, &free_arg, 2 | PARAM_FLAG_GETREST | PARAM_FLAG_OPTIONS,
80                             "set", &optlist, &key, &value))
81                 return;
82
83         clear = g_hash_table_lookup(optlist, "clear") != NULL;
84         set_default = g_hash_table_lookup(optlist, "default") != NULL;
85
86         last_section = ""; found = 0;
87         sets = settings_get_sorted();
88         for (tmp = sets; tmp != NULL; tmp = tmp->next) {
89                 SETTINGS_REC *rec = tmp->data;
90
91                 if (((clear || *value != '\0') && g_strcasecmp(rec->key, key) != 0) ||
92                     (*value == '\0' && *key != '\0' && stristr(rec->key, key) == NULL))
93                         continue;
94
95                 if (strcmp(last_section, rec->section) != 0) {
96                         /* print section */
97                         printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP,
98                                     TXT_SET_TITLE, rec->section);
99                         last_section = rec->section;
100                 }
101
102                 if (clear || set_default || *value != '\0') {
103                         /* change the setting */
104                         switch (rec->type) {
105                         case SETTING_TYPE_BOOLEAN:
106                                 if (clear)
107                                         settings_set_bool(key, FALSE);
108                                 else if (set_default)
109                                         settings_set_bool(key, GPOINTER_TO_INT(rec->def));
110                                 else
111                                         set_boolean(key, value);
112                                 break;
113                         case SETTING_TYPE_INT:
114                                 settings_set_int(key, clear ? 0 :
115                                                  set_default ? GPOINTER_TO_INT(rec->def) :
116                                                  atoi(value));
117                                 break;
118                         case SETTING_TYPE_STRING:
119                                 settings_set_str(key, clear ? "" :
120                                                  set_default ? rec->def :
121                                                  value);
122                                 break;
123                         }
124                         signal_emit("setup changed", 0);
125                 }
126
127                 set_print(rec);
128                 found = TRUE;
129
130                 if (clear || *value != '\0')
131                         break;
132         }
133         g_slist_free(sets);
134
135         if (!found) {
136                 printformat(NULL, NULL, MSGLEVEL_CLIENTERROR,
137                             TXT_SET_UNKNOWN, key);
138          }
139
140         cmd_params_free(free_arg);
141 }
142
143 /* SYNTAX: TOGGLE <key> [on|off|toggle] */
144 static void cmd_toggle(const char *data)
145 {
146         char *key, *value;
147         void *free_arg;
148         int type;
149
150         if (!cmd_get_params(data, &free_arg, 2 | PARAM_FLAG_GETREST, &key, &value))
151                 return;
152
153         if (*key == '\0') cmd_param_error(CMDERR_NOT_ENOUGH_PARAMS);
154
155         type = settings_get_type(key);
156         if (type == -1)
157                 printformat(NULL, NULL, MSGLEVEL_CLIENTERROR, TXT_SET_UNKNOWN, key);
158         else if (type != SETTING_TYPE_BOOLEAN)
159                 printformat(NULL, NULL, MSGLEVEL_CLIENTERROR, TXT_SET_NOT_BOOLEAN, key);
160         else {
161                 set_boolean(key, *value != '\0' ? value : "TOGGLE");
162                 set_print(settings_get_record(key));
163                 signal_emit("setup changed", 0);
164         }
165
166         cmd_params_free(free_arg);
167 }
168
169 static int config_key_compare(CONFIG_NODE *node1, CONFIG_NODE *node2)
170 {
171         return g_strcasecmp(node1->key, node2->key);
172 }
173
174 static void show_aliases(const char *alias)
175 {
176         CONFIG_NODE *node;
177         GSList *tmp, *list;
178         int aliaslen;
179
180         printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP, TXT_ALIASLIST_HEADER);
181
182         node = iconfig_node_traverse("aliases", FALSE);
183         tmp = node == NULL ? NULL : config_node_first(node->value);
184
185         /* first get the list of aliases sorted */
186         list = NULL;
187         aliaslen = strlen(alias);
188         for (; tmp != NULL; tmp = config_node_next(tmp)) {
189                 CONFIG_NODE *node = tmp->data;
190
191                 if (node->type != NODE_TYPE_KEY)
192                         continue;
193
194                 if (aliaslen != 0 && g_strncasecmp(node->key, alias, aliaslen) != 0)
195                         continue;
196
197                 list = g_slist_insert_sorted(list, node, (GCompareFunc) config_key_compare);
198         }
199
200         /* print the aliases */
201         for (tmp = list; tmp != NULL; tmp = tmp->next) {
202                 CONFIG_NODE *node = tmp->data;
203
204                 printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP, TXT_ALIASLIST_LINE,
205                             node->key, node->value);
206         }
207         g_slist_free(list);
208
209         printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP, TXT_ALIASLIST_FOOTER);
210 }
211
212 static void alias_remove(const char *alias)
213 {
214         if (iconfig_get_str("aliases", alias, NULL) == NULL)
215                 printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE, TXT_ALIAS_NOT_FOUND, alias);
216         else {
217                 printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE, TXT_ALIAS_REMOVED, alias);
218                 iconfig_set_str("aliases", alias, NULL);
219
220                 signal_emit("alias removed", 1, alias);
221         }
222 }
223
224 /* SYNTAX: ALIAS [[-]<alias> [<command>]] */
225 static void cmd_alias(const char *data)
226 {
227         char *alias, *value;
228         void *free_arg;
229
230         g_return_if_fail(data != NULL);
231
232         if (!cmd_get_params(data, &free_arg, 2 | PARAM_FLAG_GETREST, &alias, &value))
233                 return;
234
235         if (*alias == '-') {
236                 if (alias[1] != '\0') alias_remove(alias+1);
237         } else if (*alias == '\0' || *value == '\0')
238                 show_aliases(alias);
239         else {
240                 printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE, TXT_ALIAS_ADDED, alias);
241                 iconfig_set_str("aliases", alias, value);
242                 signal_emit("alias added", 2, alias, value);
243         }
244         cmd_params_free(free_arg);
245 }
246
247 /* SYNTAX: UNALIAS <alias> */
248 static void cmd_unalias(const char *data)
249 {
250         char *alias;
251         void *free_arg;
252
253         g_return_if_fail(data != NULL);
254
255         if (!cmd_get_params(data, &free_arg, 1, &alias))
256                 return;
257         if (*alias == '\0') cmd_param_error(CMDERR_NOT_ENOUGH_PARAMS);
258
259         alias_remove(alias);
260         cmd_params_free(free_arg);
261 }
262
263 /* SYNTAX: RELOAD [<file>] */
264 static void cmd_reload(const char *data)
265 {
266         const char *fname;
267
268         fname = *data == '\0' ? get_irssi_config() : data;
269
270         if (settings_reread(fname)) {
271                 printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE,
272                             TXT_CONFIG_RELOADED, fname);
273         }
274 }
275
276 static void settings_save_fe(const char *fname)
277 {
278         if (settings_save(fname, FALSE /* not autosaved */)) {
279                 printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE,
280                             TXT_CONFIG_SAVED, fname);
281         }
282 }
283
284 static void settings_save_confirm(const char *line, char *fname)
285 {
286         if (i_toupper(line[0]) == 'Y')
287                 settings_save_fe(fname);
288         g_free(fname);
289 }
290
291 /* SYNTAX: SAVE [<file>] */
292 static void cmd_save(const char *data)
293 {
294         GHashTable *optlist;
295         char *format, *fname;
296         void *free_arg;
297
298         if (!cmd_get_params(data, &free_arg, 1 | PARAM_FLAG_OPTIONS,
299                             "save", &optlist, &fname))
300                 return;
301
302         if (*fname == '\0')
303                 fname = mainconfig->fname;
304
305         if (!irssi_config_is_changed(fname))
306                 settings_save_fe(fname);
307         else {
308                 /* config file modified outside irssi */
309                 printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE,
310                             TXT_CONFIG_MODIFIED, fname);
311
312                 format = format_get_text(MODULE_NAME, NULL, NULL, NULL,
313                                          TXT_OVERWRITE_CONFIG);
314                 keyboard_entry_redirect((SIGNAL_FUNC) settings_save_confirm,
315                                         format, 0, g_strdup(fname));
316                 g_free(format);
317         }
318
319         cmd_params_free(free_arg);
320 }
321
322 static void settings_clean_confirm(const char *line)
323 {
324         if (i_toupper(line[0]) == 'Y')
325                 settings_clean_invalid();
326 }
327
328 static void sig_settings_errors(const char *msg)
329 {
330         printtext(NULL, NULL, MSGLEVEL_CLIENTERROR, "%s", msg);
331         keyboard_entry_redirect((SIGNAL_FUNC) settings_clean_confirm,
332                                 "Remove unknown settings from config file (Y/n)?",
333                                 0, NULL);
334 }
335
336 void fe_settings_init(void)
337 {
338         command_bind("set", NULL, (SIGNAL_FUNC) cmd_set);
339         command_bind("toggle", NULL, (SIGNAL_FUNC) cmd_toggle);
340         command_bind("alias", NULL, (SIGNAL_FUNC) cmd_alias);
341         command_bind("unalias", NULL, (SIGNAL_FUNC) cmd_unalias);
342         command_bind("reload", NULL, (SIGNAL_FUNC) cmd_reload);
343         command_bind("save", NULL, (SIGNAL_FUNC) cmd_save);
344         command_set_options("set", "clear default");
345
346         signal_add("settings errors", (SIGNAL_FUNC) sig_settings_errors);
347 }
348
349 void fe_settings_deinit(void)
350 {
351         command_unbind("set", (SIGNAL_FUNC) cmd_set);
352         command_unbind("toggle", (SIGNAL_FUNC) cmd_toggle);
353         command_unbind("alias", (SIGNAL_FUNC) cmd_alias);
354         command_unbind("unalias", (SIGNAL_FUNC) cmd_unalias);
355         command_unbind("reload", (SIGNAL_FUNC) cmd_reload);
356         command_unbind("save", (SIGNAL_FUNC) cmd_save);
357
358         signal_remove("settings errors", (SIGNAL_FUNC) sig_settings_errors);
359 }