summaryrefslogtreecommitdiff
path: root/src/core/wee-input.c
blob: 7757739844c76214ed78eef3d944d64fabfbb4e9 (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
/*
 * wee-input.c - default input callback for buffers
 *
 * Copyright (C) 2003-2019 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-input.h"
#include "wee-config.h"
#include "wee-hook.h"
#include "wee-string.h"
#include "wee-utf8.h"
#include "../gui/gui-buffer.h"
#include "../gui/gui-chat.h"
#include "../gui/gui-filter.h"
#include "../gui/gui-window.h"
#include "../plugins/plugin.h"


char **input_commands_allowed = NULL;


/*
 * Sends data to buffer input callback.
 */

void
input_exec_data (struct t_gui_buffer *buffer, const char *data)
{
    if (buffer->input_callback)
    {
        (void)(buffer->input_callback) (buffer->input_callback_pointer,
                                        buffer->input_callback_data,
                                        buffer,
                                        data);
    }
    else
    {
        gui_chat_printf (buffer,
                         _("%sYou can not write text in this "
                           "buffer"),
                         gui_chat_prefix[GUI_CHAT_PREFIX_ERROR]);
    }
}

/*
 * Executes a command.
 *
 * Returns:
 *   WEECHAT_RC_OK: command executed
 *   WEECHAT_RC_ERROR: error, command not executed
 */

int
input_exec_command (struct t_gui_buffer *buffer,
                    int any_plugin,
                    struct t_weechat_plugin *plugin,
                    const char *string,
                    const char *commands_allowed)
{
    char *command, *command_name, *pos;
    char **old_commands_allowed, **new_commands_allowed;
    int rc;

    if ((!string) || (!string[0]))
        return WEECHAT_RC_ERROR;

    rc = WEECHAT_RC_OK;

    command = NULL;
    command_name = NULL;

    old_commands_allowed = input_commands_allowed;
    new_commands_allowed = NULL;

    command = strdup (string);
    if (!command)
    {
        rc = WEECHAT_RC_ERROR;
        goto end;
    }

    if (commands_allowed)
    {
        new_commands_allowed = string_split (
            commands_allowed,
            ",",
            NULL,
            WEECHAT_STRING_SPLIT_STRIP_LEFT
            | WEECHAT_STRING_SPLIT_STRIP_RIGHT
            | WEECHAT_STRING_SPLIT_COLLAPSE_SEPS,
            0,
            NULL);
        input_commands_allowed = new_commands_allowed;
    }

    /* ignore spaces at the end of command */
    pos = &command[strlen (command) - 1];
    if (pos[0] == ' ')
    {
        while ((pos > command) && (pos[0] == ' '))
            pos--;
        pos[1] = '\0';
    }

    /* extract command name */
    pos = strchr (command, ' ');
    command_name = (pos) ?
        string_strndup (command, pos - command) : strdup (command);
    if (!command_name)
    {
        rc = WEECHAT_RC_ERROR;
        goto end;
    }

    /* check if command is allowed */
    if (input_commands_allowed
        && !string_match_list (command_name + 1,
                               (const char **)input_commands_allowed, 0))
    {
        if (weechat_debug_core >= 1)
        {
            gui_chat_printf_date_tags (
                NULL, 0, "command_forbidden," GUI_FILTER_TAG_NO_FILTER,
                _("warning: the command \"%s\" is not currently allowed "
                  "(command: \"%s\", buffer: \"%s\")"),
                command_name,
                command,
                buffer->full_name);
        }
        rc = WEECHAT_RC_ERROR;
        goto end;
    }

    /* execute command */
    switch (hook_command_exec (buffer, any_plugin, plugin, command))
    {
        case HOOK_COMMAND_EXEC_OK:
            /* command hooked, OK (executed) */
            break;
        case HOOK_COMMAND_EXEC_ERROR:
            /* command hooked, error */
            rc = WEECHAT_RC_ERROR;
            break;
        case HOOK_COMMAND_EXEC_NOT_FOUND:
            /*
             * command not found: if unknown commands are accepted by this
             * buffer, just send input text as data to buffer,
             * otherwise display error
             */
            if (buffer->input_get_unknown_commands)
            {
                input_exec_data (buffer, string);
            }
            else
            {
                gui_chat_printf_date_tags (NULL, 0, GUI_FILTER_TAG_NO_FILTER,
                                           _("%sError: unknown command \"%s\" "
                                             "(type /help for help)"),
                                           gui_chat_prefix[GUI_CHAT_PREFIX_ERROR],
                                           command_name);
                rc = WEECHAT_RC_ERROR;
            }
            break;
        case HOOK_COMMAND_EXEC_AMBIGUOUS_PLUGINS:
            /* command is ambiguous (exists for other plugins) */
            gui_chat_printf_date_tags (NULL, 0, GUI_FILTER_TAG_NO_FILTER,
                                       _("%sError: ambiguous command \"%s\": "
                                         "it exists in many plugins and not in "
                                         "\"%s\" plugin"),
                                       gui_chat_prefix[GUI_CHAT_PREFIX_ERROR],
                                       command_name,
                                       plugin_get_name (plugin));
            rc = WEECHAT_RC_ERROR;
            break;
        case HOOK_COMMAND_EXEC_AMBIGUOUS_INCOMPLETE:
            /*
             * command is ambiguous (incomplete command and multiple commands
             * start with this name)
             */
            gui_chat_printf_date_tags (NULL, 0, GUI_FILTER_TAG_NO_FILTER,
                                       _("%sError: incomplete command \"%s\" "
                                         "and multiple commands start with "
                                         "this name"),
                                       gui_chat_prefix[GUI_CHAT_PREFIX_ERROR],
                                       command_name);
            rc = WEECHAT_RC_ERROR;
            break;
        case HOOK_COMMAND_EXEC_RUNNING:
            /* command is running */
            gui_chat_printf_date_tags (NULL, 0, GUI_FILTER_TAG_NO_FILTER,
                                       _("%sError: too many calls to command "
                                         "\"%s\" (looping)"),
                                       gui_chat_prefix[GUI_CHAT_PREFIX_ERROR],
                                       command_name);
            rc = WEECHAT_RC_ERROR;
            break;
        default:
            break;
    }

end:
    if (command)
        free (command);
    if (command_name)
        free (command_name);

    if (new_commands_allowed)
        string_free_split (new_commands_allowed);
    input_commands_allowed = old_commands_allowed;

    return rc;
}

/*
 * Sends data to a buffer's callback.
 *
 * Returns:
 *   WEECHAT_RC_OK: data properly sent (or command executed successfully)
 *   WEECHAT_RC_ERROR: error
 */

int
input_data (struct t_gui_buffer *buffer, const char *data,
            const char *commands_allowed)
{
    char *pos, *buf, str_buffer[128], *new_data, *buffer_full_name;
    const char *ptr_data, *ptr_data_for_buffer;
    int length, char_size, first_command, rc;

    if (!buffer || !gui_buffer_valid (buffer) || !data)
        return WEECHAT_RC_ERROR;

    rc = WEECHAT_RC_OK;

    buffer_full_name = NULL;
    new_data = NULL;

    buffer_full_name = strdup (buffer->full_name);
    if (!buffer_full_name)
    {
        rc = WEECHAT_RC_ERROR;
        goto end;
    }

    /* execute modifier "input_text_for_buffer" */
    snprintf (str_buffer, sizeof (str_buffer),
              "0x%lx", (unsigned long)buffer);
    new_data = hook_modifier_exec (NULL,
                                   "input_text_for_buffer",
                                   str_buffer,
                                   data);

    /* data was dropped? */
    if (data[0] && new_data && !new_data[0])
        goto end;

    first_command = 1;
    ptr_data = (new_data) ? new_data : data;
    while (ptr_data)
    {
        /*
         * if the buffer pointer is not valid any more (or if it's another
         * buffer), use the current buffer for the next command
         */
        if (!first_command
            && (!gui_buffer_valid (buffer)
                || (strcmp (buffer->full_name, buffer_full_name) != 0)))
        {
            if (!gui_current_window || !gui_current_window->buffer)
                break;
            buffer = gui_current_window->buffer;
            free (buffer_full_name);
            buffer_full_name = strdup (buffer->full_name);
            if (!buffer_full_name)
                break;
        }

        pos = strchr (ptr_data, '\n');
        if (pos)
            pos[0] = '\0';

        ptr_data_for_buffer = string_input_for_buffer (ptr_data);
        if (ptr_data_for_buffer)
        {
            /*
             * input string is NOT a command, send it to buffer input
             * callback
             */
            if (string_is_command_char (ptr_data_for_buffer))
            {
                char_size = utf8_char_size (ptr_data_for_buffer);
                length = strlen (ptr_data_for_buffer) + char_size + 1;
                buf = malloc (length);
                if (buf)
                {
                    memcpy (buf, ptr_data_for_buffer, char_size);
                    snprintf (buf + char_size, length - char_size,
                              "%s", ptr_data_for_buffer);
                    input_exec_data (buffer, buf);
                    free (buf);
                }
            }
            else
                input_exec_data (buffer, ptr_data_for_buffer);
        }
        else
        {
            /* input string is a command */
            rc = input_exec_command (buffer, 1, buffer->plugin, ptr_data,
                                     commands_allowed);
        }

        if (pos)
        {
            pos[0] = '\n';
            ptr_data = pos + 1;
        }
        else
            ptr_data = NULL;

        first_command = 0;
    }

end:
    if (buffer_full_name)
        free (buffer_full_name);
    if (new_data)
        free (new_data);

    return rc;
}

/*
 * Callback for timer set by input_data_delayed.
 */

int
input_data_timer_cb (const void *pointer, void *data, int remaining_calls)
{
    char **timer_args;
    int i;
    struct t_gui_buffer *ptr_buffer;

    /* make C compiler happy */
    (void) data;
    (void) remaining_calls;

    timer_args = (char **)pointer;

    if (!timer_args)
        return WEECHAT_RC_ERROR;

    if (timer_args[0] && timer_args[1])
    {
        ptr_buffer = gui_buffer_search_by_full_name (timer_args[0]);
        if (ptr_buffer)
            (void) input_data (ptr_buffer, timer_args[1], timer_args[2]);
    }

    for (i = 0; i < 3; i++)
    {
        if (timer_args[i])
            free (timer_args[i]);
    }
    free (timer_args);

    return WEECHAT_RC_OK;
}

/*
 * Sends data to a buffer's callback with an optional delay (in milliseconds).
 *
 * If delay < 1, the command is executed immediately.
 * If delay >= 1, the command is scheduled for execution in this number of ms.
 *
 * Returns:
 *   WEECHAT_RC_OK: data properly sent or scheduled for execution
 *   WEECHAT_RC_ERROR: error
 */

int
input_data_delayed (struct t_gui_buffer *buffer, const char *data,
                    const char *commands_allowed, long delay)
{
    char **timer_args, *new_commands_allowed;

    if (delay < 1)
        return input_data (buffer, data, commands_allowed);

    timer_args = malloc (3 * sizeof (*timer_args));
    if (!timer_args)
    {
        gui_chat_printf (NULL,
                         _("%sNot enough memory (%s)"),
                         gui_chat_prefix[GUI_CHAT_PREFIX_ERROR],
                         "/wait");
        return WEECHAT_RC_ERROR;
    }

    if (commands_allowed)
    {
        new_commands_allowed = strdup (commands_allowed);
    }
    else if (input_commands_allowed)
    {
        new_commands_allowed = string_build_with_split_string (
            (const char **)input_commands_allowed, ",");
    }
    else
    {
        new_commands_allowed = NULL;
    }

    timer_args[0] = strdup (buffer->full_name);
    timer_args[1] = strdup (data);
    timer_args[2] = new_commands_allowed;

    /* schedule command, execute it after "delay" milliseconds */
    hook_timer (NULL,
                (delay >= 1) ? delay : 1,
                0,
                1,
                &input_data_timer_cb,
                timer_args, NULL);

    return WEECHAT_RC_OK;
}