updates.
[silc.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] [<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;
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
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;
89
90                 if (((clear || *value != '\0') && g_strcasecmp(rec->key, key) != 0) ||
91                     (*value == '\0' && *key != '\0' && stristr(rec->key, key) == NULL))
92                         continue;
93
94                 if (strcmp(last_section, rec->section) != 0) {
95                         /* print section */
96                         printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP,
97                                     TXT_SET_TITLE, rec->section);
98                         last_section = rec->section;
99                 }
100
101                 if (clear || *value != '\0') {
102                         /* change the setting */
103                         switch (rec->type) {
104                         case SETTING_TYPE_BOOLEAN:
105                                 if (clear)
106                                         settings_set_bool(key, FALSE);
107                                 else
108                                         set_boolean(key, value);
109                                 break;
110                         case SETTING_TYPE_INT:
111                                 settings_set_int(key, clear ? 0 : atoi(value));
112                                 break;
113                         case SETTING_TYPE_STRING:
114                                 settings_set_str(key, clear ? "" : value);
115                                 break;
116                         }
117                         signal_emit("setup changed", 0);
118                 }
119
120                 set_print(rec);
121                 found = TRUE;
122
123                 if (clear || *value != '\0')
124                         break;
125         }
126         g_slist_free(sets);
127
128         if (!found) {
129                 printformat(NULL, NULL, MSGLEVEL_CLIENTERROR,
130                             TXT_SET_UNKNOWN, key);
131          }
132
133         cmd_params_free(free_arg);
134 }
135
136 /* SYNTAX: TOGGLE <key> [on|off|toggle] */
137 static void cmd_toggle(const char *data)
138 {
139         char *key, *value;
140         void *free_arg;
141         int type;
142
143         if (!cmd_get_params(data, &free_arg, 2 | PARAM_FLAG_GETREST, &key, &value))
144                 return;
145
146         if (*key == '\0') cmd_param_error(CMDERR_NOT_ENOUGH_PARAMS);
147
148         type = settings_get_type(key);
149         if (type == -1)
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);
153         else {
154                 set_boolean(key, *value != '\0' ? value : "TOGGLE");
155                 set_print(settings_get_record(key));
156                 signal_emit("setup changed", 0);
157         }
158
159         cmd_params_free(free_arg);
160 }
161
162 static int config_key_compare(CONFIG_NODE *node1, CONFIG_NODE *node2)
163 {
164         return g_strcasecmp(node1->key, node2->key);
165 }
166
167 static void show_aliases(const char *alias)
168 {
169         CONFIG_NODE *node;
170         GSList *tmp, *list;
171         int aliaslen;
172
173         printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP, TXT_ALIASLIST_HEADER);
174
175         node = iconfig_node_traverse("aliases", FALSE);
176         tmp = node == NULL ? NULL : config_node_first(node->value);
177
178         /* first get the list of aliases sorted */
179         list = NULL;
180         aliaslen = strlen(alias);
181         for (; tmp != NULL; tmp = config_node_next(tmp)) {
182                 CONFIG_NODE *node = tmp->data;
183
184                 if (node->type != NODE_TYPE_KEY)
185                         continue;
186
187                 if (aliaslen != 0 && g_strncasecmp(node->key, alias, aliaslen) != 0)
188                         continue;
189
190                 list = g_slist_insert_sorted(list, node, (GCompareFunc) config_key_compare);
191         }
192
193         /* print the aliases */
194         for (tmp = list; tmp != NULL; tmp = tmp->next) {
195                 CONFIG_NODE *node = tmp->data;
196
197                 printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP, TXT_ALIASLIST_LINE,
198                             node->key, node->value);
199         }
200         g_slist_free(list);
201
202         printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP, TXT_ALIASLIST_FOOTER);
203 }
204
205 static void alias_remove(const char *alias)
206 {
207         if (iconfig_get_str("aliases", alias, NULL) == NULL)
208                 printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE, TXT_ALIAS_NOT_FOUND, alias);
209         else {
210                 printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE, TXT_ALIAS_REMOVED, alias);
211                 iconfig_set_str("aliases", alias, NULL);
212
213                 signal_emit("alias removed", 1, alias);
214         }
215 }
216
217 /* SYNTAX: ALIAS [[-]<alias> [<command>]] */
218 static void cmd_alias(const char *data)
219 {
220         char *alias, *value;
221         void *free_arg;
222
223         g_return_if_fail(data != NULL);
224
225         if (!cmd_get_params(data, &free_arg, 2 | PARAM_FLAG_GETREST, &alias, &value))
226                 return;
227
228         if (*alias == '-') {
229                 if (alias[1] != '\0') alias_remove(alias+1);
230         } else if (*alias == '\0' || *value == '\0')
231                 show_aliases(alias);
232         else {
233                 printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE, TXT_ALIAS_ADDED, alias);
234                 iconfig_set_str("aliases", alias, value);
235                 signal_emit("alias added", 2, alias, value);
236         }
237         cmd_params_free(free_arg);
238 }
239
240 /* SYNTAX: UNALIAS <alias> */
241 static void cmd_unalias(const char *data)
242 {
243         g_return_if_fail(data != NULL);
244         if (*data == '\0') cmd_return_error(CMDERR_NOT_ENOUGH_PARAMS);
245
246         alias_remove(data);
247 }
248
249 /* SYNTAX: RELOAD [<file>] */
250 static void cmd_reload(const char *data)
251 {
252         const char *fname;
253
254         fname = *data == '\0' ? get_irssi_config() : data;
255
256         if (settings_reread(fname)) {
257                 printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE,
258                             TXT_CONFIG_RELOADED, fname);
259         }
260 }
261
262 static void settings_save_fe(const char *fname)
263 {
264         if (settings_save(fname, FALSE /* not autosaved */)) {
265                 printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE,
266                             TXT_CONFIG_SAVED, fname);
267         }
268 }
269
270 static void settings_save_confirm(const char *line, char *fname)
271 {
272         if (i_toupper(line[0]) == 'Y')
273                 settings_save_fe(fname);
274         g_free(fname);
275 }
276
277 /* SYNTAX: SAVE [<file>] */
278 static void cmd_save(const char *data)
279 {
280         GHashTable *optlist;
281         char *format, *fname;
282         void *free_arg;
283
284         if (!cmd_get_params(data, &free_arg, 1 | PARAM_FLAG_OPTIONS,
285                             "save", &optlist, &fname))
286                 return;
287
288         if (*fname == '\0')
289                 fname = mainconfig->fname;
290
291         if (!irssi_config_is_changed(fname))
292                 settings_save_fe(fname);
293         else {
294                 /* config file modified outside irssi */
295                 printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE,
296                             TXT_CONFIG_MODIFIED, fname);
297
298                 format = format_get_text(MODULE_NAME, NULL, NULL, NULL,
299                                          TXT_OVERWRITE_CONFIG);
300                 keyboard_entry_redirect((SIGNAL_FUNC) settings_save_confirm,
301                                         format, 0, g_strdup(fname));
302                 g_free(format);
303         }
304
305         cmd_params_free(free_arg);
306 }
307
308 static void settings_clean_confirm(const char *line)
309 {
310         if (i_toupper(line[0]) == 'Y')
311                 settings_clean_invalid();
312 }
313
314 static void sig_settings_errors(const char *msg)
315 {
316         printtext(NULL, NULL, MSGLEVEL_CLIENTERROR, "%s", msg);
317         keyboard_entry_redirect((SIGNAL_FUNC) settings_clean_confirm,
318                                 "Remove unknown settings from config file (Y/n)?",
319                                 0, NULL);
320 }
321
322 void fe_settings_init(void)
323 {
324         command_bind("set", NULL, (SIGNAL_FUNC) cmd_set);
325         command_bind("toggle", NULL, (SIGNAL_FUNC) cmd_toggle);
326         command_bind("alias", NULL, (SIGNAL_FUNC) cmd_alias);
327         command_bind("unalias", NULL, (SIGNAL_FUNC) cmd_unalias);
328         command_bind("reload", NULL, (SIGNAL_FUNC) cmd_reload);
329         command_bind("save", NULL, (SIGNAL_FUNC) cmd_save);
330         command_set_options("set", "clear");
331
332         signal_add("settings errors", (SIGNAL_FUNC) sig_settings_errors);
333 }
334
335 void fe_settings_deinit(void)
336 {
337         command_unbind("set", (SIGNAL_FUNC) cmd_set);
338         command_unbind("toggle", (SIGNAL_FUNC) cmd_toggle);
339         command_unbind("alias", (SIGNAL_FUNC) cmd_alias);
340         command_unbind("unalias", (SIGNAL_FUNC) cmd_unalias);
341         command_unbind("reload", (SIGNAL_FUNC) cmd_reload);
342         command_unbind("save", (SIGNAL_FUNC) cmd_save);
343
344         signal_remove("settings errors", (SIGNAL_FUNC) sig_settings_errors);
345 }