summaryrefslogtreecommitdiff
path: root/src/plugins/trigger/trigger-completion.c
blob: 90c95059718e556a02c41d5e3d71c1b15eb29de1 (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
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
/*
 * trigger-completion.c - completion for trigger commands
 *
 * Copyright (C) 2014-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 <http://www.gnu.org/licenses/>.
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "../weechat-plugin.h"
#include "trigger.h"
#include "trigger-config.h"


/*
 * Adds triggers to completion list.
 */

int
trigger_completion_triggers_cb (const void *pointer, void *data,
                                const char *completion_item,
                                struct t_gui_buffer *buffer,
                                struct t_gui_completion *completion)
{
    struct t_trigger *ptr_trigger;

    /* make C compiler happy */
    (void) pointer;
    (void) data;
    (void) completion_item;
    (void) buffer;

    for (ptr_trigger = triggers; ptr_trigger;
         ptr_trigger = ptr_trigger->next_trigger)
    {
        weechat_hook_completion_list_add (completion, ptr_trigger->name,
                                          0, WEECHAT_LIST_POS_SORT);
    }

    return WEECHAT_RC_OK;
}

/*
 * Adds default triggers to completion list.
 */

int
trigger_completion_triggers_default_cb (const void *pointer, void *data,

                                        const char *completion_item,
                                        struct t_gui_buffer *buffer,
                                        struct t_gui_completion *completion)
{
    int i;

    /* make C compiler happy */
    (void) pointer;
    (void) data;
    (void) completion_item;
    (void) buffer;

    for (i = 0; trigger_config_default_list[i][0]; i++)
    {
        weechat_hook_completion_list_add (completion,
                                          trigger_config_default_list[i][0],
                                          0, WEECHAT_LIST_POS_SORT);
    }

    return WEECHAT_RC_OK;
}

/*
 * Adds options for triggers to completion list.
 */

int
trigger_completion_options_cb (const void *pointer, void *data,
                               const char *completion_item,
                               struct t_gui_buffer *buffer,
                               struct t_gui_completion *completion)
{
    int i;

    /* make C compiler happy */
    (void) pointer;
    (void) data;
    (void) completion_item;
    (void) buffer;

    for (i = 0; i < TRIGGER_NUM_OPTIONS; i++)
    {
        weechat_hook_completion_list_add (completion,
                                          trigger_option_string[i],
                                          0, WEECHAT_LIST_POS_SORT);
    }

    return WEECHAT_RC_OK;
}

/*
 * Adds value of a trigger option to completion list.
 */

int
trigger_completion_option_value_cb (const void *pointer, void *data,
                                    const char *completion_item,
                                    struct t_gui_buffer *buffer,
                                    struct t_gui_completion *completion)
{
    const char *args;
    char **argv;
    int argc, index_option;
    struct t_trigger *ptr_trigger;

    /* make C compiler happy */
    (void) pointer;
    (void) data;
    (void) completion_item;
    (void) buffer;

    args = weechat_hook_completion_get_string (completion, "args");
    if (!args)
        return WEECHAT_RC_OK;

    argv = weechat_string_split (args, " ", 0, 0, &argc);
    if (!argv)
        return WEECHAT_RC_OK;

    if (argc >= 3)
    {
        ptr_trigger = trigger_search (argv[1]);
        if (ptr_trigger)
        {
            if (weechat_strcasecmp (argv[2], "name") == 0)
            {
                weechat_hook_completion_list_add (completion,
                                                  ptr_trigger->name,
                                                  0,
                                                  WEECHAT_LIST_POS_BEGINNING);
            }
            else
            {
                index_option = trigger_search_option (argv[2]);
                if (index_option >= 0)
                {
                    weechat_hook_completion_list_add (completion,
                                                      weechat_config_string (ptr_trigger->options[index_option]),
                                                      0,
                                                      WEECHAT_LIST_POS_BEGINNING);
                }
            }
        }
    }

    weechat_string_free_split (argv);

    return WEECHAT_RC_OK;
}

/*
 * Adds hooks for triggers to completion list.
 */

int
trigger_completion_hooks_cb (const void *pointer, void *data,
                             const char *completion_item,
                             struct t_gui_buffer *buffer,
                             struct t_gui_completion *completion)
{
    int i;

    /* make C compiler happy */
    (void) pointer;
    (void) data;
    (void) completion_item;
    (void) buffer;

    for (i = 0; i < TRIGGER_NUM_HOOK_TYPES; i++)
    {
        weechat_hook_completion_list_add (completion,
                                          trigger_hook_type_string[i],
                                          0, WEECHAT_LIST_POS_END);
    }

    return WEECHAT_RC_OK;
}

/*
 * Adds hooks for filtering triggers to completion list.
 */

int
trigger_completion_hooks_filter_cb (const void *pointer, void *data,
                                    const char *completion_item,
                                    struct t_gui_buffer *buffer,
                                    struct t_gui_completion *completion)
{
    int i;
    char str_hook[128];

    /* make C compiler happy */
    (void) pointer;
    (void) data;
    (void) completion_item;
    (void) buffer;

    for (i = 0; i < TRIGGER_NUM_HOOK_TYPES; i++)
    {
        snprintf (str_hook, sizeof (str_hook),
                  "@%s", trigger_hook_type_string[i]);
        weechat_hook_completion_list_add (completion, str_hook,
                                          0, WEECHAT_LIST_POS_END);
    }

    return WEECHAT_RC_OK;
}

/*
 * Adds a word with quotes around to completion list.
 */

void
trigger_completion_add_quoted_word (struct t_gui_completion *completion,
                                    const char *word)
{
    char *temp;
    int length;

    length = 1 + strlen (word) + 1 + 1;
    temp = malloc (length);
    if (!temp)
        return;

    snprintf (temp, length, "\"%s\"", word);
    weechat_hook_completion_list_add (completion, temp, 0,
                                      WEECHAT_LIST_POS_END);

    free (temp);
}

/*
 * Adds a default string to completion list, depending on hook type.
 *
 * If split is not NULL, the default string found is split using this separator,
 * and therefore many words can be added to completion list.
 */

void
trigger_completion_add_default_for_hook (struct t_gui_completion *completion,
                                         char *default_strings[], char *split)
{
    const char *args;
    char **argv, **items;
    int argc, num_items, type, i;

    args = weechat_hook_completion_get_string (completion, "args");
    if (!args)
        return;

    argv = weechat_string_split (args, " ", 0, 0, &argc);
    if (!argv)
        return;

    if (argc >= 3)
    {
        type = trigger_search_hook_type (argv[2]);
        if (type >= 0)
        {
            if (default_strings[type][0] && split && split[0])
            {
                items = weechat_string_split (default_strings[type], split,
                                              0, 0, &num_items);
                if (items)
                {
                    for (i = 0; i < num_items; i++)
                    {
                        trigger_completion_add_quoted_word (completion,
                                                            items[i]);
                    }
                    weechat_string_free_split (items);
                }
            }
            else
            {
                trigger_completion_add_quoted_word (completion,
                                                    default_strings[type]);
            }
        }
    }

    weechat_string_free_split (argv);
}

/*
 * Adds default arguments for hook to completion list.
 */

int
trigger_completion_hook_arguments_cb (const void *pointer, void *data,
                                      const char *completion_item,
                                      struct t_gui_buffer *buffer,
                                      struct t_gui_completion *completion)
{
    /* make C compiler happy */
    (void) pointer;
    (void) data;
    (void) completion_item;
    (void) buffer;

    trigger_completion_add_default_for_hook (completion,
                                             trigger_hook_default_arguments,
                                             NULL);
    weechat_hook_completion_list_add (completion, "\"\"", 0,
                                      WEECHAT_LIST_POS_END);

    return WEECHAT_RC_OK;
}

/*
 * Adds default conditions for hook to completion list.
 */

int
trigger_completion_hook_conditions_cb (const void *pointer, void *data,
                                       const char *completion_item,
                                       struct t_gui_buffer *buffer,
                                       struct t_gui_completion *completion)
{
    /* make C compiler happy */
    (void) pointer;
    (void) data;
    (void) completion_item;
    (void) buffer;

    weechat_hook_completion_list_add (completion,
                                      "\"" TRIGGER_HOOK_DEFAULT_CONDITIONS "\"",
                                      0,
                                      WEECHAT_LIST_POS_END);
    weechat_hook_completion_list_add (completion, "\"\"", 0,
                                      WEECHAT_LIST_POS_END);

    return WEECHAT_RC_OK;
}

/*
 * Adds default regular expression for hook to completion list.
 */

int
trigger_completion_hook_regex_cb (const void *pointer, void *data,
                                  const char *completion_item,
                                  struct t_gui_buffer *buffer,
                                  struct t_gui_completion *completion)
{
    /* make C compiler happy */
    (void) pointer;
    (void) data;
    (void) completion_item;
    (void) buffer;

    weechat_hook_completion_list_add (completion,
                                      "\"" TRIGGER_HOOK_DEFAULT_REGEX "\"",
                                      0,
                                      WEECHAT_LIST_POS_END);
    weechat_hook_completion_list_add (completion, "\"\"", 0,
                                      WEECHAT_LIST_POS_END);

    return WEECHAT_RC_OK;
}

/*
 * Adds default command for hook to completion list.
 */

int
trigger_completion_hook_command_cb (const void *pointer, void *data,
                                    const char *completion_item,
                                    struct t_gui_buffer *buffer,
                                    struct t_gui_completion *completion)
{
    /* make C compiler happy */
    (void) pointer;
    (void) data;
    (void) completion_item;
    (void) buffer;

    weechat_hook_completion_list_add (completion,
                                      "\"" TRIGGER_HOOK_DEFAULT_COMMAND "\"",
                                      0,
                                      WEECHAT_LIST_POS_END);
    weechat_hook_completion_list_add (completion, "\"\"", 0,
                                      WEECHAT_LIST_POS_END);

    return WEECHAT_RC_OK;
}

/*
 * Adds default return code(s) for hook to completion list.
 */

int
trigger_completion_hook_rc_cb (const void *pointer, void *data,
                               const char *completion_item,
                               struct t_gui_buffer *buffer,
                               struct t_gui_completion *completion)
{
    /* make C compiler happy */
    (void) pointer;
    (void) data;
    (void) completion_item;
    (void) buffer;

    trigger_completion_add_default_for_hook (completion,
                                             trigger_hook_default_rc,
                                             ",");

    return WEECHAT_RC_OK;
}

/*
 * Adds default post actions to completion list.
 */

int
trigger_completion_post_action_cb (const void *pointer, void *data,
                                   const char *completion_item,
                                   struct t_gui_buffer *buffer,
                                   struct t_gui_completion *completion)
{
    int i;

    /* make C compiler happy */
    (void) pointer;
    (void) data;
    (void) completion_item;
    (void) buffer;

    for (i = 0; i < TRIGGER_NUM_POST_ACTIONS; i++)
    {
        trigger_completion_add_quoted_word (completion,
                                            trigger_post_action_string[i]);
    }

    return WEECHAT_RC_OK;
}


/*
 * Hooks completions.
 */

void
trigger_completion_init ()
{
    weechat_hook_completion ("trigger_names",
                             N_("triggers"),
                             &trigger_completion_triggers_cb, NULL, NULL);
    weechat_hook_completion ("trigger_names_default",
                             N_("default triggers"),
                             &trigger_completion_triggers_default_cb, NULL, NULL);
    weechat_hook_completion ("trigger_options",
                             N_("options for triggers"),
                             &trigger_completion_options_cb, NULL, NULL);
    weechat_hook_completion ("trigger_option_value",
                             N_("value of a trigger option"),
                             &trigger_completion_option_value_cb, NULL, NULL);
    weechat_hook_completion ("trigger_hooks",
                             N_("hooks for triggers"),
                             &trigger_completion_hooks_cb, NULL, NULL);
    weechat_hook_completion ("trigger_hooks_filter",
                             N_("hooks for triggers (for filter in monitor buffer)"),
                             &trigger_completion_hooks_filter_cb, NULL, NULL);
    weechat_hook_completion ("trigger_hook_arguments",
                             N_("default arguments for a hook"),
                             &trigger_completion_hook_arguments_cb, NULL, NULL);
    weechat_hook_completion ("trigger_hook_conditions",
                             N_("default conditions for a hook"),
                             &trigger_completion_hook_conditions_cb, NULL, NULL);
    weechat_hook_completion ("trigger_hook_regex",
                             N_("default regular expression for a hook"),
                             &trigger_completion_hook_regex_cb, NULL, NULL);
    weechat_hook_completion ("trigger_hook_command",
                             N_("default command for a hook"),
                             &trigger_completion_hook_command_cb, NULL, NULL);
    weechat_hook_completion ("trigger_hook_rc",
                             N_("default return codes for hook callback"),
                             &trigger_completion_hook_rc_cb, NULL, NULL);
    weechat_hook_completion ("trigger_post_action",
                             N_("trigger post actions"),
                             &trigger_completion_post_action_cb, NULL, NULL);
}