summaryrefslogtreecommitdiff
path: root/src/fe-common/core/fe-settings.c
blob: 9c370838c08681c1dfe8f4ee4be03a8ad8a4b0f5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
/*
 fe-settings.c : irssi

    Copyright (C) 1999 Timo Sirainen

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License along
    with this program; if not, write to the Free Software Foundation, Inc.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#include "module.h"
#include "module-formats.h"
#include "signals.h"
#include "commands.h"
#include "servers.h"
#include "misc.h"
#include "lib-config/iconfig.h"
#include "settings.h"

#include "levels.h"
#include "printtext.h"
#include "keyboard.h"

static void set_print(SETTINGS_REC *rec)
{
	char *value;

	value = settings_get_print(rec);
	printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP, TXT_SET_ITEM,
		    rec->key, value);
	g_free(value);
}

static void set_print_pattern(const char *pattern)
{
	GSList *sets, *tmp;
	const char *last_section;

	last_section = "";
	sets = settings_get_sorted();
	for (tmp = sets; tmp != NULL; tmp = tmp->next) {
		SETTINGS_REC *rec = tmp->data;

		if (stristr(rec->key, pattern) == NULL)
			continue;
		if (g_strcmp0(last_section, rec->section) != 0) {
			/* print section */
			printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP,
				    TXT_SET_TITLE, rec->section);
			last_section = rec->section;
		}
		set_print(rec);
	}
	g_slist_free(sets);
}

static void set_boolean(const char *key, const char *value)
{
	char *stripped_value;
	stripped_value = g_strdup(value);
	g_strstrip(stripped_value);

	if (g_ascii_strcasecmp(stripped_value, "ON") == 0)
		settings_set_bool(key, TRUE);
	else if (g_ascii_strcasecmp(stripped_value, "OFF") == 0)
		settings_set_bool(key, FALSE);
	else if (g_ascii_strcasecmp(stripped_value, "TOGGLE") == 0)
		settings_set_bool(key, !settings_get_bool(key));
	else
		printformat(NULL, NULL, MSGLEVEL_CLIENTERROR, TXT_NOT_TOGGLE);

    g_free(stripped_value);
}

static void set_int(const char *key, const char *value)
{
	char *endp;
	long longval;
	int error;

	errno = 0;
	longval = strtol(value, &endp, 10);
	error = errno;
	while (i_isspace(*endp))
		endp++;
	if (error != 0 || *endp != '\0' || longval < INT_MIN || longval > INT_MAX)
		printformat(NULL, NULL, MSGLEVEL_CLIENTERROR, TXT_INVALID_NUMBER);
	else
		settings_set_int(key, (int)longval);
}

/* SYNTAX: SET [-clear | -default] [<key> [<value>]] */
static void cmd_set(char *data)
{
        GHashTable *optlist;
	char *key, *value;
	void *free_arg;
	int clear, set_default;
	SETTINGS_REC *rec;

	if (!cmd_get_params(data, &free_arg, 2 | PARAM_FLAG_GETREST | PARAM_FLAG_OPTIONS,
			    "set", &optlist, &key, &value))
		return;

	clear = g_hash_table_lookup(optlist, "clear") != NULL;
	set_default = g_hash_table_lookup(optlist, "default") != NULL;

	if (*key == '\0')
		clear = set_default = FALSE;

	if (!(clear || set_default || *value != '\0'))
		set_print_pattern(key);
	else {
		rec = settings_get_record(key);
		if (rec != NULL) {
			/* change the setting */
			switch (rec->type) {
			case SETTING_TYPE_BOOLEAN:
                                if (clear)
					settings_set_bool(key, FALSE);
				else if (set_default)
					settings_set_bool(key, rec->default_value.v_bool);
				else
					set_boolean(key, value);
				break;
			case SETTING_TYPE_INT:
				if (clear)
					settings_set_int(key, 0);
				else if (set_default)
					settings_set_int(key, rec->default_value.v_int);
				else
					set_int(key, value);
				break;
			case SETTING_TYPE_STRING:
				settings_set_str(key, clear ? "" :
						 set_default ? rec->default_value.v_string :
						 value);
				break;
			case SETTING_TYPE_TIME:
				if (!settings_set_time(key, clear ? "0" :
						       set_default ? rec->default_value.v_string : value))
					printformat(NULL, NULL, MSGLEVEL_CLIENTERROR,
						    TXT_INVALID_TIME);
				break;
			case SETTING_TYPE_LEVEL:
				if (!settings_set_level(key, clear ? "" :
							set_default ? rec->default_value.v_string : value))
					printformat(NULL, NULL, MSGLEVEL_CLIENTERROR,
						    TXT_INVALID_LEVEL);
				break;
			case SETTING_TYPE_SIZE:
				if (!settings_set_size(key, clear ? "0" :
						       set_default ? rec->default_value.v_string : value))
					printformat(NULL, NULL, MSGLEVEL_CLIENTERROR,
						    TXT_INVALID_SIZE);
				break;
			}
			signal_emit("setup changed", 0);
			printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP,
				    TXT_SET_TITLE, rec->section);
			set_print(rec);
		} else
			printformat(NULL, NULL, MSGLEVEL_CLIENTERROR,
				    TXT_SET_UNKNOWN, key);
	}

        cmd_params_free(free_arg);
}

/* SYNTAX: TOGGLE <key> [on|off|toggle] */
static void cmd_toggle(const char *data)
{
	char *key, *value;
	void *free_arg;
	int type;

	if (!cmd_get_params(data, &free_arg, 2 | PARAM_FLAG_GETREST, &key, &value))
		return;

        if (*key == '\0') cmd_param_error(CMDERR_NOT_ENOUGH_PARAMS);

	type = settings_get_type(key);
        if (type == -1)
		printformat(NULL, NULL, MSGLEVEL_CLIENTERROR, TXT_SET_UNKNOWN, key);
	else if (type != SETTING_TYPE_BOOLEAN)
		printformat(NULL, NULL, MSGLEVEL_CLIENTERROR, TXT_SET_NOT_BOOLEAN, key);
	else {
		set_boolean(key, *value != '\0' ? value : "TOGGLE");
                set_print(settings_get_record(key));
		signal_emit("setup changed", 0);
	}

        cmd_params_free(free_arg);
}

static int config_key_compare(CONFIG_NODE *node1, CONFIG_NODE *node2)
{
	return g_ascii_strcasecmp(node1->key, node2->key);
}

static void show_aliases(const char *alias)
{
	CONFIG_NODE *node;
	GSList *tmp, *list;
	int aliaslen;

	printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP, TXT_ALIASLIST_HEADER);

	node = iconfig_node_traverse("aliases", FALSE);
	tmp = node == NULL ? NULL : config_node_first(node->value);

	/* first get the list of aliases sorted */
	list = NULL;
	aliaslen = strlen(alias);
	for (; tmp != NULL; tmp = config_node_next(tmp)) {
		CONFIG_NODE *node = tmp->data;

		if (node->type != NODE_TYPE_KEY)
			continue;

		if (aliaslen != 0 && g_ascii_strncasecmp(node->key, alias, aliaslen) != 0)
			continue;

		list = g_slist_insert_sorted(list, node, (GCompareFunc) config_key_compare);
	}

	/* print the aliases */
	for (tmp = list; tmp != NULL; tmp = tmp->next) {
		CONFIG_NODE *node = tmp->data;

		printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP, TXT_ALIASLIST_LINE,
			    node->key, node->value);
	}
	g_slist_free(list);

	printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP, TXT_ALIASLIST_FOOTER);
}

static void alias_remove(const char *alias)
{
	if (iconfig_get_str("aliases", alias, NULL) == NULL)
		printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE, TXT_ALIAS_NOT_FOUND, alias);
	else {
		printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE, TXT_ALIAS_REMOVED, alias);
		iconfig_set_str("aliases", alias, NULL);

		signal_emit("alias removed", 1, alias);
	}
}

/* SYNTAX: ALIAS [[-]<alias> [<command>]] */
static void cmd_alias(const char *data)
{
	char *alias, *value;
	void *free_arg;

	g_return_if_fail(data != NULL);

	if (!cmd_get_params(data, &free_arg, 2 | PARAM_FLAG_GETREST, &alias, &value))
		return;

	if (*alias == '-') {
		if (alias[1] != '\0') alias_remove(alias+1);
	} else if (*alias == '\0' || *value == '\0')
		show_aliases(alias);
	else {
		printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE, TXT_ALIAS_ADDED, alias);
		iconfig_set_str("aliases", alias, value);
		signal_emit("alias added", 2, alias, value);
	}
        cmd_params_free(free_arg);
}

/* SYNTAX: UNALIAS <alias> */
static void cmd_unalias(const char *data)
{
	char *alias;
	void *free_arg;

	g_return_if_fail(data != NULL);

	if (!cmd_get_params(data, &free_arg, 1, &alias))
		return;
	if (*alias == '\0') cmd_param_error(CMDERR_NOT_ENOUGH_PARAMS);

	alias_remove(alias);
        cmd_params_free(free_arg);
}

/* SYNTAX: RELOAD [<file>] */
static void cmd_reload(const char *data)
{
	const char *fname;

	fname = *data == '\0' ? get_irssi_config() : data;

	if (settings_reread(fname)) {
		printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE,
			    TXT_CONFIG_RELOADED, fname);
	}
}

static void settings_save_fe(const char *fname)
{
	if (settings_save(fname, FALSE /* not autosaved */)) {
		printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE,
			    TXT_CONFIG_SAVED, fname);
	}
}

static void settings_save_confirm(const char *line, char *fname)
{
	if (i_toupper(line[0]) == 'Y')
		settings_save_fe(fname);
	g_free(fname);
}

/* SYNTAX: SAVE [<file>] */
static void cmd_save(const char *data)
{
        GHashTable *optlist;
	char *format, *fname;
        void *free_arg;

	if (!cmd_get_params(data, &free_arg, 1 | PARAM_FLAG_OPTIONS,
			    "save", &optlist, &fname))
		return;

	if (*fname == '\0')
		fname = mainconfig->fname;

	if (!irssi_config_is_changed(fname))
		settings_save_fe(fname);
	else {
                /* config file modified outside irssi */
		printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE,
			    TXT_CONFIG_MODIFIED, fname);

		format = format_get_text(MODULE_NAME, NULL, NULL, NULL,
					 TXT_OVERWRITE_CONFIG);
		keyboard_entry_redirect((SIGNAL_FUNC) settings_save_confirm,
					format, 0, g_strdup(fname));
		g_free(format);
	}

	cmd_params_free(free_arg);
}

static void settings_clean_confirm(const char *line)
{
	if (i_toupper(line[0]) == 'Y')
                settings_clean_invalid();
}

static void sig_settings_errors(const char *msg)
{
        printtext(NULL, NULL, MSGLEVEL_CLIENTERROR, "%s", msg);
	keyboard_entry_redirect((SIGNAL_FUNC) settings_clean_confirm,
				"Remove unknown settings from config file (Y/n)?",
				0, NULL);
}

void fe_settings_init(void)
{
	command_bind("set", NULL, (SIGNAL_FUNC) cmd_set);
	command_bind("toggle", NULL, (SIGNAL_FUNC) cmd_toggle);
	command_bind("alias", NULL, (SIGNAL_FUNC) cmd_alias);
	command_bind("unalias", NULL, (SIGNAL_FUNC) cmd_unalias);
	command_bind("reload", NULL, (SIGNAL_FUNC) cmd_reload);
	command_bind("save", NULL, (SIGNAL_FUNC) cmd_save);
	command_set_options("set", "clear default");

        signal_add("settings errors", (SIGNAL_FUNC) sig_settings_errors);
}

void fe_settings_deinit(void)
{
	command_unbind("set", (SIGNAL_FUNC) cmd_set);
	command_unbind("toggle", (SIGNAL_FUNC) cmd_toggle);
	command_unbind("alias", (SIGNAL_FUNC) cmd_alias);
	command_unbind("unalias", (SIGNAL_FUNC) cmd_unalias);
	command_unbind("reload", (SIGNAL_FUNC) cmd_reload);
	command_unbind("save", (SIGNAL_FUNC) cmd_save);

	signal_remove("settings errors", (SIGNAL_FUNC) sig_settings_errors);
}