summaryrefslogtreecommitdiff
path: root/src/core/commands.c
blob: 8f02db898052fbd8b34ba750d344c896679e7cd5 (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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
/*
 commands.c : irssi

    Copyright (C) 1999-2000 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#include "module.h"
#include "modules.h"
#include "signals.h"
#include "commands.h"
#include "misc.h"
#include "server.h"
#include "server-redirect.h"
#include "special-vars.h"

#include "lib-config/iconfig.h"
#include "settings.h"

#define alias_find(alias) \
	iconfig_get_str("aliases", alias, NULL)

GSList *commands;
char *current_command;

static GSList *cmdget_funcs;
static int signal_default_command;

void command_bind(const char *cmd, const char *category, SIGNAL_FUNC func)
{
	COMMAND_REC *rec;
	char *str;

	g_return_if_fail(cmd != NULL);

	rec = g_new0(COMMAND_REC, 1);
	rec->cmd = g_strdup(cmd);
	rec->category = category == NULL ? NULL : g_strdup(category);
	commands = g_slist_append(commands, rec);

	if (func != NULL) {
		str = g_strconcat("command ", cmd, NULL);
		signal_add(str, func);
		g_free(str);
	}

	signal_emit("commandlist new", 1, rec);
}

void command_free(COMMAND_REC *rec)
{
	commands = g_slist_remove(commands, rec);
	signal_emit("commandlist remove", 1, rec);

	g_free_not_null(rec->category);
	g_free(rec->cmd);
	g_free(rec);
}

void command_unbind(const char *cmd, SIGNAL_FUNC func)
{
	GSList *tmp;
	char *str;

	g_return_if_fail(cmd != NULL);

	for (tmp = commands; tmp != NULL; tmp = tmp->next) {
		COMMAND_REC *rec = tmp->data;

		if (g_strcasecmp(rec->cmd, cmd) == 0) {
			command_free(rec);
			break;
		}
	}

	if (func != NULL) {
		str = g_strconcat("command ", cmd, NULL);
		signal_remove(str, func);
		g_free(str);
	}
}

void command_runsub(const char *cmd, const char *data, void *p1, void *p2)
{
	char *subcmd, *defcmd, *args;

	g_return_if_fail(data != NULL);

	/* get command.. */
	subcmd = g_strdup_printf("command %s %s", cmd, data);
	args = strchr(subcmd+9 + strlen(cmd), ' ');
	if (args != NULL) *args++ = '\0'; else args = "";
	while (*args == ' ') args++;

	g_strdown(subcmd);
	if (!signal_emit(subcmd, 3, args, p1, p2)) {
		defcmd = g_strdup_printf("default command %s", cmd);
		if (!signal_emit(defcmd, 3, data, p1, p2))
			signal_emit("unknown command", 3, strchr(subcmd, ' ')+1, p1, p2);
                g_free(defcmd);
	}
	g_free(subcmd);
}

char *cmd_get_param(char **data)
{
	char *pos;

	g_return_val_if_fail(data != NULL, NULL);
	g_return_val_if_fail(*data != NULL, NULL);

	while (**data == ' ') (*data)++;
	pos = *data;

	while (**data != '\0' && **data != ' ') (*data)++;
	if (**data == ' ') *(*data)++ = '\0';

	return pos;
}

char *cmd_get_quoted_param(char **data)
{
	char *pos, quote;

	g_return_val_if_fail(data != NULL, NULL);
	g_return_val_if_fail(*data != NULL, NULL);

	while (**data == ' ') (*data)++;
	if (**data != '\'' && **data != '"')
		return cmd_get_param(data);

	quote = **data; (*data)++;

	pos = *data;
	while (**data != '\0' && **data != quote) {
		if (**data == '\\' && (*data)[1] != '\0')
                        g_memmove(*data, (*data)+1, strlen(*data));
		(*data)++;
	}

	if (**data != '\0') *(*data)++ = '\0';

	return pos;
}

static char *get_opt_args(char **data)
{
	/* -cmd1 -cmd2 -cmd3 ... */
	char *p, *ret;

	g_return_val_if_fail(data != NULL, NULL);
	g_return_val_if_fail(*data != NULL, NULL);

	ret = NULL;
	for (p = *data;;) {
		if (*p != '-') {
			if (p == *data) return "";

			while (isspace(p[-1]) && p > *data) p--;
			if (*p != '\0') *p++ = '\0';
			ret = *data;
			*data = p;
			return ret;
		}

		while (!isspace(*p) && *p != '\0') p++;
		while (isspace(*p)) p++;
	}
}

static void cmd_params_pack(char ***subargs, char *end, char *start, char *newstart)
{
	char ***tmp;
	char *data;
	int bufsize, datalen, len;

	bufsize = (int) (end-newstart)+1;

	data = g_malloc(bufsize); datalen = 0;
	for (tmp = subargs; *tmp != NULL; tmp++) {
		if (**tmp < start || **tmp > end)
			continue;

                len = strlen(**tmp)+1;
		if (datalen+len > bufsize)
			g_error("cmd_params_pack() : buffer overflow!");

		memcpy(data+datalen, **tmp, len);
		**tmp = newstart+datalen;
		datalen += len;
	}

	g_memmove(newstart, data, datalen);
	g_free(data);
}

int arg_find(char **array, const char *item)
{
	char **tmp;
	int index;

	g_return_val_if_fail(array != NULL, 0);
	g_return_val_if_fail(item != NULL, 0);

	index = 0;
	for (tmp = array; *tmp != NULL; tmp++, index++) {
		if (g_strcasecmp(*tmp + (**tmp == '@'), item) == 0)
			return index;
	}

	return -1;
}

static int get_multi_args(char **data, va_list va)
{
	/* -cmd1 arg1 -cmd2 "argument two" -cmd3 */
        GString *returnargs;
	char **args, **arglist, *arg, *origdata;
	char **nextarg, ***subargs;
	int eat, pos;

	eat = 0;
	args = (char **) va_arg(va, char **);
	g_return_val_if_fail(args != NULL && *args != NULL && **args != '\0', 0);

	arglist = g_strsplit(*args, " ", -1);
	eat = strarray_length(arglist);

	subargs = g_new(char **, eat+1);
	for (pos = 0; pos < eat; pos++) {
		subargs[pos] = (char **) va_arg(va, char **);
		if (subargs[pos] == NULL) {
			g_free(subargs);
			g_warning("get_multi_args() : subargument == NULL");
			return eat;
		}
		*subargs[pos] = "";
	}
	subargs[eat] = NULL;

        origdata = *data;
	returnargs = g_string_new(NULL);
	nextarg = NULL;
	for (;;) {
		if (**data == '-') {
			(*data)++; arg = cmd_get_param(data);
			g_string_sprintfa(returnargs, "-%s ", arg);

			/* check if this argument can have parameter */
			pos = arg_find(arglist, arg);
			nextarg = pos == -1 ? NULL : subargs[pos];

			while (isspace(**data)) (*data)++;
			continue;
		}

		if (nextarg == NULL)
			break;

		if (*arglist[pos] == '@' && !isdigit(**data))
			break; /* expected a numeric argument */

		/* save the sub-argument to `nextarg' */
		arg = cmd_get_quoted_param(data);
                *nextarg = arg; nextarg = NULL;

		while (isspace(**data)) (*data)++;
	}

	/* ok, this is a bit stupid. this will pack the arguments in `data'
	   like "-arg1 subarg -arg2 sub2\0" -> "-arg1 -arg2\0subarg\0sub2\0"
	   this is because it's easier to free only _one_ string instead of
	   two (`args') when using PARAM_FLAG_MULTIARGS. */
	if (origdata == *data)
		*args = "";
	else {
		cmd_params_pack(subargs, **data == '\0' ? *data : (*data)-1,
				origdata, origdata+returnargs->len);

		g_string_truncate(returnargs, returnargs->len-1);
		strcpy(origdata, returnargs->str);
		*args = origdata;
	}

	g_string_free(returnargs, TRUE);
	g_strfreev(arglist);
	g_free(subargs);

	return eat;
}

char *cmd_get_callfuncs(const char *data, int *count, va_list *args)
{
	CMD_GET_FUNC func;
	GSList *tmp;
	char *ret, *old;

	ret = g_strdup(data);
	for (tmp = cmdget_funcs; tmp != NULL; tmp = tmp->next) {
		func = (CMD_GET_FUNC) tmp->data;

		old = ret;
		ret = func(ret, count, args);
                g_free(old);
	}

	return ret;
}

char *cmd_get_params(const char *data, int count, ...)
{
	char **str, *arg, *ret, *datad;
	va_list args;
	int cnt, eat;

	g_return_val_if_fail(data != NULL, NULL);

	va_start(args, count);
	ret = datad = cmd_get_callfuncs(data, &count, &args);

	cnt = PARAM_WITHOUT_FLAGS(count);
	while (cnt-- > 0) {
		if (count & PARAM_FLAG_OPTARGS) {
			arg = get_opt_args(&datad);
			count &= ~PARAM_FLAG_OPTARGS;
		} else if (count & PARAM_FLAG_MULTIARGS) {
			eat = get_multi_args(&datad, args)+1;
			count &= ~PARAM_FLAG_MULTIARGS;

			cnt -= eat-1;
			while (eat-- > 0)
				str = (char **) va_arg(args, char **);
			continue;
		} else if (cnt == 0 && count & PARAM_FLAG_GETREST) {
			/* get rest */
			arg = datad;
		} else {
			arg = cmd_get_quoted_param(&datad);
		}

		str = (char **) va_arg(args, char **);
		if (str != NULL) *str = arg;
	}
	va_end(args);

	return ret;
}

void cmd_get_add_func(CMD_GET_FUNC func)
{
        cmdget_funcs = g_slist_prepend(cmdget_funcs, (void *) func);
}

void cmd_get_remove_func(CMD_GET_FUNC func)
{
        cmdget_funcs = g_slist_prepend(cmdget_funcs, (void *) func);
}

static void parse_outgoing(const char *line, SERVER_REC *server, void *item)
{
	const char *cmdchars, *alias;
	char *cmd, *str, *args, *oldcmd;
	int use_alias = TRUE;

	g_return_if_fail(line != NULL);

	if (*line == '\0') {
		/* empty line, forget it. */
                signal_stop();
		return;
	}

	cmdchars = settings_get_str("cmdchars");
	if (strchr(cmdchars, *line) == NULL)
		return; /* handle only /commands here */
	line++;

	/* //command ignores aliases */
	if (strchr(cmdchars, *line) != NULL) {
		line++;
		use_alias = FALSE;
	}

	cmd = str = g_strconcat("command ", line, NULL);
	args = strchr(cmd+8, ' ');
	if (args != NULL) *args++ = '\0'; else args = "";

	/* check if there's an alias for command */
	alias = use_alias ? alias_find(cmd+8) : NULL;
	if (alias != NULL)
		eval_special_string(alias, args, server, item);
	else {
		if (server != NULL)
			server_redirect_default((SERVER_REC *) server, cmd);

		g_strdown(cmd);
		oldcmd = current_command;
		current_command = cmd+8;
		if (!signal_emit(cmd, 3, args, server, item))
			signal_emit_id(signal_default_command, 3, line, server, item);
                current_command = oldcmd;
	}

	g_free(str);
}

static void cmd_eval(const char *data, SERVER_REC *server, void *item)
{
	g_return_if_fail(data != NULL);

	eval_special_string(data, "", server, item);
}

static void cmd_cd(const char *data)
{
	char *str;

	g_return_if_fail(data != NULL);
	if (*data == '\0') return;

	str = convert_home(data);
	chdir(str);
	g_free(str);
}

void commands_init(void)
{
	cmdget_funcs = NULL;
	current_command = NULL;

	signal_default_command = module_get_uniq_id_str("signals", "default command");

	settings_add_str("misc", "cmdchars", "/");
	signal_add("send command", (SIGNAL_FUNC) parse_outgoing);

	command_bind("eval", NULL, (SIGNAL_FUNC) cmd_eval);
	command_bind("cd", NULL, (SIGNAL_FUNC) cmd_cd);
}

void commands_deinit(void)
{
	g_free_not_null(current_command);
	g_slist_free(cmdget_funcs);

	signal_remove("send command", (SIGNAL_FUNC) parse_outgoing);

	command_unbind("eval", (SIGNAL_FUNC) cmd_eval);
	command_unbind("cd", (SIGNAL_FUNC) cmd_cd);
}