imported.
[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         printtext(NULL, NULL, MSGLEVEL_CLIENTCRAP, "%s = %s", rec->key, value);
54 }
55
56 static void set_boolean(const char *key, const char *value)
57 {
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));
64         else
65                 printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP, TXT_NOT_TOGGLE);
66 }
67
68 /* SYNTAX: SET [-clear] [<key> [<value>]] */
69 static void cmd_set(char *data)
70 {
71         GHashTable *optlist;
72         GSList *sets, *tmp;
73         const char *last_section;
74         char *key, *value;
75         void *free_arg;
76         int found, clear;
77
78         if (!cmd_get_params(data, &free_arg, 2 | PARAM_FLAG_GETREST | PARAM_FLAG_OPTIONS,
79                             "set", &optlist, &key, &value))
80                 return;
81
82         clear = g_hash_table_lookup(optlist, "clear") != NULL;
83
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;
88
89                 if (((clear || *value != '\0') && g_strcasecmp(rec->key, key) != 0) ||
90                     (*value == '\0' && *key != '\0' && stristr(rec->key, key) == NULL))
91                         continue;
92
93                 if (strcmp(last_section, rec->section) != 0) {
94                         /* print section */
95                         printtext(NULL, NULL, MSGLEVEL_CLIENTCRAP, "%_[ %s ]", rec->section);
96                         last_section = rec->section;
97                 }
98
99                 if (clear || *value != '\0') {
100                         /* change the setting */
101                         switch (rec->type) {
102                         case SETTING_TYPE_BOOLEAN:
103                                 if (clear)
104                                         settings_set_bool(key, FALSE);
105                                 else
106                                         set_boolean(key, value);
107                                 break;
108                         case SETTING_TYPE_INT:
109                                 settings_set_int(key, clear ? 0 : atoi(value));
110                                 break;
111                         case SETTING_TYPE_STRING:
112                                 settings_set_str(key, clear ? "" : value);
113                                 break;
114                         }
115                         signal_emit("setup changed", 0);
116                 }
117
118                 set_print(rec);
119                 found = TRUE;
120
121                 if (clear || *value != '\0')
122                         break;
123         }
124         g_slist_free(sets);
125
126         if (!found)
127                 printtext(NULL, NULL, MSGLEVEL_CLIENTERROR, "Unknown setting %s", key);
128
129         cmd_params_free(free_arg);
130 }
131
132 /* SYNTAX: TOGGLE <key> [on|off|toggle] */
133 static void cmd_toggle(const char *data)
134 {
135         char *key, *value;
136         void *free_arg;
137         int type;
138
139         if (!cmd_get_params(data, &free_arg, 2 | PARAM_FLAG_GETREST, &key, &value))
140                 return;
141
142         if (*key == '\0') cmd_param_error(CMDERR_NOT_ENOUGH_PARAMS);
143
144         type = settings_get_type(key);
145         if (type == -1)
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);
149         else {
150                 set_boolean(key, *value != '\0' ? value : "TOGGLE");
151                 set_print(settings_get_record(key));
152         }
153
154         cmd_params_free(free_arg);
155 }
156
157 static int config_key_compare(CONFIG_NODE *node1, CONFIG_NODE *node2)
158 {
159         return g_strcasecmp(node1->key, node2->key);
160 }
161
162 static void show_aliases(const char *alias)
163 {
164         CONFIG_NODE *node;
165         GSList *tmp, *list;
166         int aliaslen;
167
168         printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP, TXT_ALIASLIST_HEADER);
169
170         node = iconfig_node_traverse("aliases", FALSE);
171         tmp = node == NULL ? NULL : node->value;
172
173         /* first get the list of aliases sorted */
174         list = NULL;
175         aliaslen = strlen(alias);
176         for (; tmp != NULL; tmp = tmp->next) {
177                 CONFIG_NODE *node = tmp->data;
178
179                 if (node->type != NODE_TYPE_KEY)
180                         continue;
181
182                 if (aliaslen != 0 && g_strncasecmp(node->key, alias, aliaslen) != 0)
183                         continue;
184
185                 list = g_slist_insert_sorted(list, node, (GCompareFunc) config_key_compare);
186         }
187
188         /* print the aliases */
189         for (tmp = list; tmp != NULL; tmp = tmp->next) {
190                 CONFIG_NODE *node = tmp->data;
191
192                 printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP, TXT_ALIASLIST_LINE,
193                             node->key, node->value);
194         }
195         g_slist_free(list);
196
197         printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP, TXT_ALIASLIST_FOOTER);
198 }
199
200 static void alias_remove(const char *alias)
201 {
202         if (iconfig_get_str("aliases", alias, NULL) == NULL)
203                 printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE, TXT_ALIAS_NOT_FOUND, alias);
204         else {
205                 printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE, TXT_ALIAS_REMOVED, alias);
206                 iconfig_set_str("aliases", alias, NULL);
207         }
208 }
209
210 /* SYNTAX: ALIAS [[-]<alias> [<command>]] */
211 static void cmd_alias(const char *data)
212 {
213         char *alias, *value;
214         void *free_arg;
215
216         g_return_if_fail(data != NULL);
217
218         if (!cmd_get_params(data, &free_arg, 2 | PARAM_FLAG_GETREST, &alias, &value))
219                 return;
220
221         if (*alias == '-') {
222                 if (alias[1] != '\0') alias_remove(alias+1);
223         } else if (*alias == '\0' || *value == '\0')
224                 show_aliases(alias);
225         else {
226                 printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE, TXT_ALIAS_ADDED, alias);
227                 iconfig_set_str("aliases", alias, value);
228         }
229         cmd_params_free(free_arg);
230 }
231
232 /* SYNTAX: UNALIAS <alias> */
233 static void cmd_unalias(const char *data)
234 {
235         g_return_if_fail(data != NULL);
236         if (*data == '\0') cmd_return_error(CMDERR_NOT_ENOUGH_PARAMS);
237
238         alias_remove(data);
239 }
240
241 /* SYNTAX: RELOAD [<file>] */
242 static void cmd_reload(const char *data)
243 {
244         char *fname;
245
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);
251         }
252         g_free(fname);
253 }
254
255 static void settings_save_fe(const char *fname)
256 {
257         if (settings_save(fname)) {
258                 printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE,
259                             TXT_CONFIG_SAVED, fname);
260         }
261 }
262
263 static void settings_save_confirm(const char *line, char *fname)
264 {
265         if (line[0] == 'Y')
266                 settings_save_fe(fname);
267         g_free(fname);
268 }
269
270 /* SYNTAX: SAVE [<file>] */
271 static void cmd_save(const char *data)
272 {
273         char *format;
274
275         if (*data == '\0')
276                 data = mainconfig->fname;
277
278         if (!irssi_config_is_changed(data)) {
279                 settings_save_fe(data);
280                 return;
281         }
282
283         printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE,
284                     TXT_CONFIG_MODIFIED, data);
285
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));
290         g_free(format);
291 }
292
293 static void settings_clean_confirm(const char *line)
294 {
295         if (line[0] == 'Y')
296                 settings_clean_invalid();
297 }
298
299 static void sig_settings_errors(const char *msg)
300 {
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)?",
304                                 0, NULL);
305 }
306
307 void fe_settings_init(void)
308 {
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");
316
317         signal_add("settings errors", (SIGNAL_FUNC) sig_settings_errors);
318 }
319
320 void fe_settings_deinit(void)
321 {
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);
328
329         signal_remove("settings errors", (SIGNAL_FUNC) sig_settings_errors);
330 }