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