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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
|
/*
* wee-hook-command.c - WeeChat command hook
*
* Copyright (C) 2003-2018 Sébastien Helleu <flashcode@flashtux.org>
*
* This file is part of WeeChat, the extensible chat client.
*
* WeeChat 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 3 of the License, or
* (at your option) any later version.
*
* WeeChat 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 WeeChat. If not, see <https://www.gnu.org/licenses/>.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdlib.h>
#include <string.h>
#include "../weechat.h"
#include "../wee-config.h"
#include "../wee-hook.h"
#include "../wee-infolist.h"
#include "../wee-list.h"
#include "../wee-log.h"
#include "../wee-string.h"
#include "../wee-utf8.h"
#include "../../gui/gui-chat.h"
#include "../../plugins/plugin.h"
/*
* Searches for a command hook in list.
*
* Returns pointer to hook found, NULL if not found.
*/
struct t_hook *
hook_command_search (struct t_weechat_plugin *plugin, const char *command)
{
struct t_hook *ptr_hook;
for (ptr_hook = weechat_hooks[HOOK_TYPE_COMMAND]; ptr_hook;
ptr_hook = ptr_hook->next_hook)
{
if (!ptr_hook->deleted
&& (ptr_hook->plugin == plugin)
&& (string_strcasecmp (HOOK_COMMAND(ptr_hook, command), command) == 0))
return ptr_hook;
}
/* command hook not found for plugin */
return NULL;
}
/*
* Builds variables/arrays that will be used for completion of commands
* arguments.
*/
void
hook_command_build_completion (struct t_hook_command *hook_command)
{
int i, j, k, length, num_items;
struct t_weelist *list;
char *pos_completion, *pos_double_pipe, *pos_start, *pos_end;
char **items;
const char *last_space, *ptr_template;
/* split templates using "||" as separator */
hook_command->cplt_num_templates = 1;
pos_completion = hook_command->completion;
while ((pos_double_pipe = strstr (pos_completion, "||")) != NULL)
{
hook_command->cplt_num_templates++;
pos_completion = pos_double_pipe + 2;
}
hook_command->cplt_templates = malloc (hook_command->cplt_num_templates *
sizeof (*hook_command->cplt_templates));
for (i = 0; i < hook_command->cplt_num_templates; i++)
{
hook_command->cplt_templates[i] = NULL;
}
pos_completion = hook_command->completion;
i = 0;
while (pos_completion)
{
pos_double_pipe = strstr (pos_completion, "||");
if (!pos_double_pipe)
pos_double_pipe = pos_completion + strlen (pos_completion);
pos_start = pos_completion;
pos_end = pos_double_pipe - 1;
if (pos_end < pos_start)
{
hook_command->cplt_templates[i] = strdup ("");
}
else
{
while (pos_start[0] == ' ')
{
pos_start++;
}
pos_end = pos_double_pipe - 1;
while ((pos_end > pos_start) && (pos_end[0] == ' '))
{
pos_end--;
}
hook_command->cplt_templates[i] = string_strndup (pos_start,
pos_end - pos_start + 1);
}
i++;
if (!pos_double_pipe[0])
pos_completion = NULL;
else
pos_completion = pos_double_pipe + 2;
}
/* for each template, split/count args */
hook_command->cplt_templates_static = malloc (hook_command->cplt_num_templates *
sizeof (*hook_command->cplt_templates_static));
hook_command->cplt_template_num_args = malloc (hook_command->cplt_num_templates *
sizeof (*hook_command->cplt_template_num_args));
hook_command->cplt_template_args = malloc (hook_command->cplt_num_templates *
sizeof (*hook_command->cplt_template_args));
hook_command->cplt_template_num_args_concat = 0;
for (i = 0; i < hook_command->cplt_num_templates; i++)
{
/*
* build static part of template: it's first argument(s) which does not
* contain "%"
*/
last_space = NULL;
ptr_template = hook_command->cplt_templates[i];
while (ptr_template && ptr_template[0])
{
if (ptr_template[0] == ' ')
{
last_space = ptr_template;
break;
}
else if (ptr_template[0] == '%')
break;
ptr_template = utf8_next_char (ptr_template);
}
if (last_space)
{
last_space--;
while (last_space > hook_command->cplt_templates[i])
{
if (last_space[0] != ' ')
break;
}
if (last_space < hook_command->cplt_templates[i])
last_space = NULL;
else
last_space++;
}
if (last_space)
hook_command->cplt_templates_static[i] = string_strndup (hook_command->cplt_templates[i],
last_space - hook_command->cplt_templates[i]);
else
hook_command->cplt_templates_static[i] = strdup (hook_command->cplt_templates[i]);
/* build arguments for each template */
hook_command->cplt_template_args[i] = string_split (hook_command->cplt_templates[i],
" ", 0, 0,
&(hook_command->cplt_template_num_args[i]));
if (hook_command->cplt_template_num_args[i] > hook_command->cplt_template_num_args_concat)
hook_command->cplt_template_num_args_concat = hook_command->cplt_template_num_args[i];
}
/*
* build strings with concatenation of items from different templates
* for each argument: these strings will be used when completing argument
* if we can't find which template to use (for example for first argument)
*/
if (hook_command->cplt_template_num_args_concat == 0)
hook_command->cplt_template_args_concat = NULL;
else
{
hook_command->cplt_template_args_concat = malloc (hook_command->cplt_template_num_args_concat *
sizeof (*hook_command->cplt_template_args_concat));
list = weelist_new ();
for (i = 0; i < hook_command->cplt_template_num_args_concat; i++)
{
/* first compute length */
length = 1;
for (j = 0; j < hook_command->cplt_num_templates; j++)
{
if (i < hook_command->cplt_template_num_args[j])
length += strlen (hook_command->cplt_template_args[j][i]) + 1;
}
/* alloc memory */
hook_command->cplt_template_args_concat[i] = malloc (length);
if (hook_command->cplt_template_args_concat[i])
{
/* concatenate items with "|" as separator */
weelist_remove_all (list);
hook_command->cplt_template_args_concat[i][0] = '\0';
for (j = 0; j < hook_command->cplt_num_templates; j++)
{
if (i < hook_command->cplt_template_num_args[j])
{
items = string_split (hook_command->cplt_template_args[j][i],
"|", 0, 0, &num_items);
for (k = 0; k < num_items; k++)
{
if (!weelist_search (list, items[k]))
{
if (hook_command->cplt_template_args_concat[i][0])
strcat (hook_command->cplt_template_args_concat[i], "|");
strcat (hook_command->cplt_template_args_concat[i],
items[k]);
weelist_add (list, items[k], WEECHAT_LIST_POS_END,
NULL);
}
}
string_free_split (items);
}
}
}
}
weelist_free (list);
}
}
/*
* Hooks a command.
*
* Returns pointer to new hook, NULL if error.
*/
struct t_hook *
hook_command (struct t_weechat_plugin *plugin, const char *command,
const char *description,
const char *args, const char *args_description,
const char *completion,
t_hook_callback_command *callback,
const void *callback_pointer,
void *callback_data)
{
struct t_hook *new_hook;
struct t_hook_command *new_hook_command;
int priority;
const char *ptr_command;
if (!callback)
return NULL;
if (hook_command_search (plugin, command))
{
gui_chat_printf (NULL,
_("%sError: another command \"%s\" already exists "
"for plugin \"%s\""),
gui_chat_prefix[GUI_CHAT_PREFIX_ERROR],
command,
plugin_get_name (plugin));
return NULL;
}
new_hook = malloc (sizeof (*new_hook));
if (!new_hook)
return NULL;
new_hook_command = malloc (sizeof (*new_hook_command));
if (!new_hook_command)
{
free (new_hook);
return NULL;
}
hook_get_priority_and_name (command, &priority, &ptr_command);
hook_init_data (new_hook, plugin, HOOK_TYPE_COMMAND, priority,
callback_pointer, callback_data);
new_hook->hook_data = new_hook_command;
new_hook_command->callback = callback;
new_hook_command->command = strdup ((ptr_command) ? ptr_command :
((command) ? command : ""));
new_hook_command->description = strdup ((description) ? description : "");
new_hook_command->args = strdup ((args) ? args : "");
new_hook_command->args_description = strdup ((args_description) ?
args_description : "");
new_hook_command->completion = strdup ((completion) ? completion : "");
/* build completion variables for command */
new_hook_command->cplt_num_templates = 0;
new_hook_command->cplt_templates = NULL;
new_hook_command->cplt_templates_static = NULL;
new_hook_command->cplt_template_num_args = NULL;
new_hook_command->cplt_template_args = NULL;
new_hook_command->cplt_template_num_args_concat = 0;
new_hook_command->cplt_template_args_concat = NULL;
hook_command_build_completion (new_hook_command);
hook_add_to_list (new_hook);
return new_hook;
}
/*
* Executes a command hook.
*
* Returns:
* HOOK_COMMAND_EXEC_OK: command executed successfully
* HOOK_COMMAND_EXEC_ERROR: command executed and failed
* HOOK_COMMAND_EXEC_NOT_FOUND: command not found
* HOOK_COMMAND_EXEC_AMBIGUOUS_PLUGINS: command is ambiguous (same command
* exists for another plugin, and we don't know which one to run)
* HOOK_COMMAND_EXEC_AMBIGUOUS_INCOMPLETE: command is ambiguous (incomplete
* command and multiple commands start with this name)
* HOOK_COMMAND_EXEC_RUNNING: command is already running
*/
int
hook_command_exec (struct t_gui_buffer *buffer, int any_plugin,
struct t_weechat_plugin *plugin, const char *string)
{
struct t_hook *ptr_hook, *next_hook;
struct t_hook *hook_plugin, *hook_other_plugin, *hook_other_plugin2;
struct t_hook *hook_incomplete_command;
char **argv, **argv_eol;
const char *ptr_command_name;
int argc, rc, length_command_name, allow_incomplete_commands;
int count_other_plugin, count_incomplete_commands;
if (!buffer || !string || !string[0])
return HOOK_COMMAND_EXEC_NOT_FOUND;
if (hook_command_run_exec (buffer, string) == WEECHAT_RC_OK_EAT)
return HOOK_COMMAND_EXEC_OK;
argv = string_split (string, " ", 0, 0, &argc);
if (argc == 0)
{
string_free_split (argv);
return HOOK_COMMAND_EXEC_NOT_FOUND;
}
argv_eol = string_split (string, " ", 1, 0, NULL);
ptr_command_name = utf8_next_char (argv[0]);
length_command_name = strlen (ptr_command_name);
hook_exec_start ();
hook_plugin = NULL;
hook_other_plugin = NULL;
hook_other_plugin2 = NULL;
hook_incomplete_command = NULL;
count_other_plugin = 0;
allow_incomplete_commands = CONFIG_BOOLEAN(config_look_command_incomplete);
count_incomplete_commands = 0;
ptr_hook = weechat_hooks[HOOK_TYPE_COMMAND];
while (ptr_hook)
{
next_hook = ptr_hook->next_hook;
if (!ptr_hook->deleted)
{
if (string_strcasecmp (ptr_command_name,
HOOK_COMMAND(ptr_hook, command)) == 0)
{
if (ptr_hook->plugin == plugin)
{
if (!hook_plugin)
hook_plugin = ptr_hook;
}
else
{
if (any_plugin)
{
if (!hook_other_plugin)
hook_other_plugin = ptr_hook;
else if (!hook_other_plugin2)
hook_other_plugin2 = ptr_hook;
count_other_plugin++;
}
}
}
else if (allow_incomplete_commands
&& (string_strncasecmp (ptr_command_name,
HOOK_COMMAND(ptr_hook, command),
length_command_name) == 0))
{
hook_incomplete_command = ptr_hook;
count_incomplete_commands++;
}
}
ptr_hook = next_hook;
}
rc = HOOK_COMMAND_EXEC_NOT_FOUND;
ptr_hook = NULL;
if (hook_plugin || hook_other_plugin)
{
if (!hook_plugin && (count_other_plugin > 1)
&& (hook_other_plugin->priority == hook_other_plugin2->priority))
{
/*
* ambiguous: no command for current plugin, but more than one
* command was found for other plugins with the same priority
* => we don't know which one to run!
*/
rc = HOOK_COMMAND_EXEC_AMBIGUOUS_PLUGINS;
}
else
{
if (hook_plugin && hook_other_plugin)
{
/*
* if we have a command in current plugin and another plugin,
* choose the command with the higher priority (if priority
* is the same, always choose the command for the current
* plugin)
*/
ptr_hook = (hook_other_plugin->priority > hook_plugin->priority) ?
hook_other_plugin : hook_plugin;
}
else
{
/*
* choose the command for current plugin, if found, otherwise
* use command found in another plugin
*/
ptr_hook = (hook_plugin) ? hook_plugin : hook_other_plugin;
}
}
}
else if (hook_incomplete_command)
{
if (count_incomplete_commands == 1)
ptr_hook = hook_incomplete_command;
else
rc = HOOK_COMMAND_EXEC_AMBIGUOUS_INCOMPLETE;
}
/* execute the command for the hook found */
if (ptr_hook)
{
if (ptr_hook->running >= HOOK_COMMAND_MAX_CALLS)
{
/* loop in execution of command => do NOT execute again */
rc = HOOK_COMMAND_EXEC_RUNNING;
}
else
{
/* execute the command! */
ptr_hook->running++;
rc = (int) (HOOK_COMMAND(ptr_hook, callback))
(ptr_hook->callback_pointer,
ptr_hook->callback_data,
buffer,
argc,
argv,
argv_eol);
ptr_hook->running--;
if (rc == WEECHAT_RC_ERROR)
rc = HOOK_COMMAND_EXEC_ERROR;
else
rc = HOOK_COMMAND_EXEC_OK;
}
}
string_free_split (argv);
string_free_split (argv_eol);
hook_exec_end ();
return rc;
}
/*
* Frees data in a command hook.
*/
void
hook_command_free_data (struct t_hook *hook)
{
int i;
if (!hook || !hook->hook_data)
return;
if (HOOK_COMMAND(hook, command))
{
free (HOOK_COMMAND(hook, command));
HOOK_COMMAND(hook, command) = NULL;
}
if (HOOK_COMMAND(hook, description))
{
free (HOOK_COMMAND(hook, description));
HOOK_COMMAND(hook, description) = NULL;
}
if (HOOK_COMMAND(hook, args))
{
free (HOOK_COMMAND(hook, args));
HOOK_COMMAND(hook, args) = NULL;
}
if (HOOK_COMMAND(hook, args_description))
{
free (HOOK_COMMAND(hook, args_description));
HOOK_COMMAND(hook, args_description) = NULL;
}
if (HOOK_COMMAND(hook, completion))
{
free (HOOK_COMMAND(hook, completion));
HOOK_COMMAND(hook, completion) = NULL;
}
if (HOOK_COMMAND(hook, cplt_templates))
{
for (i = 0; i < HOOK_COMMAND(hook, cplt_num_templates); i++)
{
if (HOOK_COMMAND(hook, cplt_templates)[i])
free (HOOK_COMMAND(hook, cplt_templates)[i]);
if (HOOK_COMMAND(hook, cplt_templates_static)[i])
free (HOOK_COMMAND(hook, cplt_templates_static)[i]);
string_free_split (HOOK_COMMAND(hook, cplt_template_args)[i]);
}
free (HOOK_COMMAND(hook, cplt_templates));
}
if (HOOK_COMMAND(hook, cplt_templates_static))
{
free (HOOK_COMMAND(hook, cplt_templates_static));
HOOK_COMMAND(hook, cplt_templates_static) = NULL;
}
if (HOOK_COMMAND(hook, cplt_template_num_args))
{
free (HOOK_COMMAND(hook, cplt_template_num_args));
HOOK_COMMAND(hook, cplt_template_num_args) = NULL;
}
if (HOOK_COMMAND(hook, cplt_template_args))
{
free (HOOK_COMMAND(hook, cplt_template_args));
HOOK_COMMAND(hook, cplt_template_args) = NULL;
}
if (HOOK_COMMAND(hook, cplt_template_args_concat))
{
for (i = 0;
i < HOOK_COMMAND(hook, cplt_template_num_args_concat);
i++)
{
free (HOOK_COMMAND(hook, cplt_template_args_concat[i]));
}
free (HOOK_COMMAND(hook, cplt_template_args_concat));
HOOK_COMMAND(hook, cplt_template_args_concat) = NULL;
}
free (hook->hook_data);
hook->hook_data = NULL;
}
/*
* Adds command hook data in the infolist item.
*
* Returns:
* 1: OK
* 0: error
*/
int
hook_command_add_to_infolist (struct t_infolist_item *item,
struct t_hook *hook)
{
if (!item || !hook || !hook->hook_data)
return 0;
if (!infolist_new_var_pointer (item, "callback", HOOK_COMMAND(hook, callback)))
return 0;
if (!infolist_new_var_string (item, "command", HOOK_COMMAND(hook, command)))
return 0;
if (!infolist_new_var_string (item, "description",
HOOK_COMMAND(hook, description)))
return 0;
if (!infolist_new_var_string (item, "description_nls",
(HOOK_COMMAND(hook, description)
&& HOOK_COMMAND(hook, description)[0]) ?
_(HOOK_COMMAND(hook, description)) : ""))
return 0;
if (!infolist_new_var_string (item, "args",
HOOK_COMMAND(hook, args)))
return 0;
if (!infolist_new_var_string (item, "args_nls",
(HOOK_COMMAND(hook, args)
&& HOOK_COMMAND(hook, args)[0]) ?
_(HOOK_COMMAND(hook, args)) : ""))
return 0;
if (!infolist_new_var_string (item, "args_description",
HOOK_COMMAND(hook, args_description)))
return 0;
if (!infolist_new_var_string (item, "args_description_nls",
(HOOK_COMMAND(hook, args_description)
&& HOOK_COMMAND(hook, args_description)[0]) ?
_(HOOK_COMMAND(hook, args_description)) : ""))
return 0;
if (!infolist_new_var_string (item, "completion", HOOK_COMMAND(hook, completion)))
return 0;
return 1;
}
/*
* Prints command hook data in WeeChat log file (usually for crash dump).
*/
void
hook_command_print_log (struct t_hook *hook)
{
int i, j;
if (!hook || !hook->hook_data)
return;
log_printf (" command data:");
log_printf (" callback. . . . . . . : 0x%lx", HOOK_COMMAND(hook, callback));
log_printf (" command . . . . . . . : '%s'", HOOK_COMMAND(hook, command));
log_printf (" description . . . . . : '%s'", HOOK_COMMAND(hook, description));
log_printf (" args. . . . . . . . . : '%s'", HOOK_COMMAND(hook, args));
log_printf (" args_description. . . : '%s'", HOOK_COMMAND(hook, args_description));
log_printf (" completion. . . . . . : '%s'", HOOK_COMMAND(hook, completion));
log_printf (" cplt_num_templates. . : %d", HOOK_COMMAND(hook, cplt_num_templates));
for (i = 0; i < HOOK_COMMAND(hook, cplt_num_templates); i++)
{
log_printf (" cplt_templates[%04d] . . . : '%s'",
i, HOOK_COMMAND(hook, cplt_templates)[i]);
log_printf (" cplt_templates_static[%04d]: '%s'",
i, HOOK_COMMAND(hook, cplt_templates_static)[i]);
log_printf (" num_args. . . . . . : %d",
HOOK_COMMAND(hook, cplt_template_num_args)[i]);
for (j = 0; j < HOOK_COMMAND(hook, cplt_template_num_args)[i]; j++)
{
log_printf (" args[%04d]. . . . . : '%s'",
j, HOOK_COMMAND(hook, cplt_template_args)[i][j]);
}
}
log_printf (" num_args_concat . . . : %d", HOOK_COMMAND(hook, cplt_template_num_args_concat));
for (i = 0; i < HOOK_COMMAND(hook, cplt_template_num_args_concat); i++)
{
log_printf (" args_concat[%04d] . . : '%s'",
i, HOOK_COMMAND(hook, cplt_template_args_concat)[i]);
}
}
|