コード例 #1
0
ファイル: irc-buffer.c プロジェクト: anders/weechat
struct t_gui_buffer *
irc_buffer_search_server_lowest_number ()
{
    struct t_gui_buffer *ptr_buffer;
    struct t_irc_server *ptr_server;
    int number, number_found;

    ptr_buffer = NULL;
    number_found = INT_MAX;

    for (ptr_server = irc_servers; ptr_server;
         ptr_server = ptr_server->next_server)
    {
        if (ptr_server->buffer)
        {
            number = weechat_buffer_get_integer (ptr_server->buffer, "number");
            if (number < number_found)
            {
                number_found = number;
                ptr_buffer = ptr_server->buffer;
            }
        }
    }
    return ptr_buffer;
}
コード例 #2
0
ファイル: irc-buffer.c プロジェクト: anders/weechat
struct t_gui_buffer *
irc_buffer_search_private_lowest_number (struct t_irc_server *server)
{
    struct t_gui_buffer *ptr_buffer;
    struct t_irc_server *ptr_server;
    struct t_irc_channel *ptr_channel;
    int number, number_found;

    ptr_buffer = NULL;
    number_found = INT_MAX;

    for (ptr_server = (server) ? server : irc_servers; ptr_server;
         ptr_server = ptr_server->next_server)
    {
        for (ptr_channel = ptr_server->channels; ptr_channel;
             ptr_channel = ptr_channel->next_channel)
        {
            if ((ptr_channel->type == IRC_CHANNEL_TYPE_PRIVATE)
                && ptr_channel->buffer)
            {
                number = weechat_buffer_get_integer (ptr_channel->buffer,
                                                     "number");
                if (number < number_found)
                {
                    number_found = number;
                    ptr_buffer = ptr_channel->buffer;
                }
            }
        }
        if (server)
            break;
    }
    return ptr_buffer;
}
コード例 #3
0
ファイル: xfer-chat.c プロジェクト: Petzku/weechat
void
xfer_chat_open_buffer (struct t_xfer *xfer)
{
    char *name;
    int length, buffer_created;

    buffer_created = 0;

    length = strlen (xfer->plugin_name) + 8 + strlen (xfer->plugin_id) + 1
        + strlen (xfer->remote_nick) + 1;
    name = malloc (length);
    if (name)
    {
        snprintf (name, length, "%s_dcc.%s.%s",
                  xfer->plugin_name, xfer->plugin_id, xfer->remote_nick);
        xfer->buffer = weechat_buffer_search (XFER_PLUGIN_NAME, name);
        if (!xfer->buffer)
        {
            xfer->buffer = weechat_buffer_new (name,
                                               &xfer_chat_buffer_input_cb, NULL,
                                               &xfer_chat_buffer_close_cb, NULL);
            buffer_created = 1;

            /* failed to create buffer ? then return */
            if (!xfer->buffer)
                return;
        }

        if (buffer_created)
        {
            weechat_buffer_set (xfer->buffer, "title", _("xfer chat"));
            if (!weechat_buffer_get_integer (xfer->buffer, "short_name_is_set"))
            {
                weechat_buffer_set (xfer->buffer, "short_name",
                                    xfer->remote_nick);
            }
            weechat_buffer_set (xfer->buffer, "localvar_set_type", "private");
            weechat_buffer_set (xfer->buffer, "localvar_set_nick", xfer->local_nick);
            weechat_buffer_set (xfer->buffer, "localvar_set_channel", xfer->remote_nick);
            weechat_buffer_set (xfer->buffer, "highlight_words_add", "$nick");
        }

        weechat_printf (xfer->buffer,
                        _("%s%s: connected to %s (%s) via xfer chat"),
                        weechat_prefix ("network"),
                        XFER_PLUGIN_NAME,
                        xfer->remote_nick,
                        xfer->remote_address_str);

        free (name);
    }
}
コード例 #4
0
ファイル: relay-weechat-protocol.c プロジェクト: lp0/weechat
void
relay_weechat_protocol_nicklist_map_cb (void *data,
                                        struct t_hashtable *hashtable,
                                        const void *key,
                                        const void *value)
{
    struct t_relay_client *ptr_client;
    struct t_gui_buffer *ptr_buffer;
    struct t_relay_weechat_nicklist *ptr_nicklist;
    struct t_hdata *ptr_hdata;
    struct t_relay_weechat_msg *msg;

    /* make C compiler happy */
    (void) hashtable;

    ptr_client = (struct t_relay_client *)data;
    ptr_buffer = (struct t_gui_buffer *)key;
    ptr_nicklist = (struct t_relay_weechat_nicklist *)value;

    ptr_hdata = weechat_hdata_get ("buffer");
    if (ptr_hdata)
    {
        if (weechat_hdata_check_pointer (ptr_hdata,
                                         weechat_hdata_get_list (ptr_hdata, "gui_buffers"),
                                         ptr_buffer))
        {
            /*
             * if no diff at all, or if diffs are bigger than nicklist:
             * send whole nicklist
             */
            if (ptr_nicklist
                && ((ptr_nicklist->items_count == 0)
                    || (ptr_nicklist->items_count >= weechat_buffer_get_integer (ptr_buffer, "nicklist_count") + 1)))
            {
                ptr_nicklist = NULL;
            }

            /* send nicklist diffs or full nicklist */
            msg = relay_weechat_msg_new ((ptr_nicklist) ? "_nicklist_diff" : "_nicklist");
            if (msg)
            {
                relay_weechat_msg_add_nicklist (msg, ptr_buffer, ptr_nicklist);
                relay_weechat_msg_send (ptr_client, msg);
                relay_weechat_msg_free (msg);
            }
        }
    }
}
コード例 #5
0
ファイル: exec-buffer.c プロジェクト: Evalle/weechat
struct t_gui_buffer *
exec_buffer_new (const char *name, int free_content, int clear_buffer,
                 int switch_to_buffer)
{
    struct t_gui_buffer *new_buffer;
    int buffer_type;

    new_buffer = weechat_buffer_search (EXEC_PLUGIN_NAME, name);
    if (new_buffer)
    {
        buffer_type = weechat_buffer_get_integer (new_buffer, "type");
        if (((buffer_type == 0) && free_content)
            || ((buffer_type == 1) && !free_content))
        {
            /* change the type of buffer */
            weechat_buffer_set (new_buffer,
                                "type",
                                (free_content) ? "free" : "formatted");
        }
        goto end;
    }

    new_buffer = weechat_buffer_new (name,
                                     &exec_buffer_input_cb, NULL, NULL,
                                     &exec_buffer_close_cb, NULL, NULL);

    /* failed to create buffer ? then return */
    if (!new_buffer)
        return NULL;

    if (free_content)
        weechat_buffer_set (new_buffer, "type", "free");
    weechat_buffer_set (new_buffer, "clear", "1");
    weechat_buffer_set (new_buffer, "title", _("Executed commands"));
    weechat_buffer_set (new_buffer, "localvar_set_type", "exec");
    weechat_buffer_set (new_buffer, "localvar_set_no_log", "1");
    weechat_buffer_set (new_buffer, "time_for_each_line", "0");
    weechat_buffer_set (new_buffer, "input_get_unknown_commands", "0");

end:
    if (clear_buffer)
        weechat_buffer_clear (new_buffer);
    if (switch_to_buffer)
        weechat_buffer_set (new_buffer, "display", "1");

    return new_buffer;
}
コード例 #6
0
ファイル: relay-raw.c プロジェクト: jameslord/weechat
void
relay_raw_open (int switch_to_buffer)
{
    struct t_relay_raw_message *ptr_raw_message;

    if (!relay_raw_buffer)
    {
        relay_raw_buffer = weechat_buffer_search (RELAY_PLUGIN_NAME,
                                                  RELAY_RAW_BUFFER_NAME);
        if (!relay_raw_buffer)
        {
            relay_raw_buffer = weechat_buffer_new (RELAY_RAW_BUFFER_NAME,
                                                   &relay_buffer_input_cb, NULL,
                                                   &relay_buffer_close_cb, NULL);

            /* failed to create buffer ? then return */
            if (!relay_raw_buffer)
                return;

            weechat_buffer_set (relay_raw_buffer,
                                "title", _("Relay raw messages"));

            if (!weechat_buffer_get_integer (relay_raw_buffer, "short_name_is_set"))
            {
                weechat_buffer_set (relay_raw_buffer, "short_name",
                                    RELAY_RAW_BUFFER_NAME);
            }
            weechat_buffer_set (relay_raw_buffer, "localvar_set_type", "debug");
            weechat_buffer_set (relay_raw_buffer, "localvar_set_server", RELAY_RAW_BUFFER_NAME);
            weechat_buffer_set (relay_raw_buffer, "localvar_set_channel", RELAY_RAW_BUFFER_NAME);
            weechat_buffer_set (relay_raw_buffer, "localvar_set_no_log", "1");

            /* disable all highlights on this buffer */
            weechat_buffer_set (relay_raw_buffer, "highlight_words", "-");

            /* print messages in list */
            for (ptr_raw_message = relay_raw_messages; ptr_raw_message;
                 ptr_raw_message = ptr_raw_message->next_message)
            {
                relay_raw_message_print (ptr_raw_message);
            }
        }
    }

    if (relay_raw_buffer && switch_to_buffer)
        weechat_buffer_set (relay_raw_buffer, "display", "1");
}
コード例 #7
0
ファイル: relay-weechat-protocol.c プロジェクト: lp0/weechat
int
relay_weechat_protocol_hsignal_nicklist_cb (const void *pointer, void *data,
                                            const char *signal,
                                            struct t_hashtable *hashtable)
{
    struct t_relay_client *ptr_client;
    struct t_gui_nick_group *parent_group, *group;
    struct t_gui_nick *nick;
    struct t_gui_buffer *ptr_buffer;
    struct t_relay_weechat_nicklist *ptr_nicklist;
    char diff;

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

    ptr_client = (struct t_relay_client *)pointer;
    if (!ptr_client || !relay_client_valid (ptr_client))
        return WEECHAT_RC_OK;

    /* check if buffer is synchronized with flag "nicklist" */
    ptr_buffer = weechat_hashtable_get (hashtable, "buffer");
    if (!relay_weechat_protocol_is_sync (ptr_client, ptr_buffer,
                                         RELAY_WEECHAT_PROTOCOL_SYNC_NICKLIST))
        return WEECHAT_RC_OK;

    parent_group = weechat_hashtable_get (hashtable, "parent_group");
    group = weechat_hashtable_get (hashtable, "group");
    nick = weechat_hashtable_get (hashtable, "nick");

    /* if there is no parent group (for example "root" group), ignore the signal */
    if (!parent_group)
        return WEECHAT_RC_OK;

    ptr_nicklist = weechat_hashtable_get (RELAY_WEECHAT_DATA(ptr_client,
                                                             buffers_nicklist),
                                          ptr_buffer);
    if (!ptr_nicklist)
    {
        ptr_nicklist = relay_weechat_nicklist_new ();
        if (!ptr_nicklist)
            return WEECHAT_RC_OK;
        ptr_nicklist->nicklist_count = weechat_buffer_get_integer (ptr_buffer,
                                                                   "nicklist_count");
        weechat_hashtable_set (RELAY_WEECHAT_DATA(ptr_client, buffers_nicklist),
                               ptr_buffer,
                               ptr_nicklist);
    }

    /* set diff type */
    diff = RELAY_WEECHAT_NICKLIST_DIFF_UNKNOWN;
    if ((strcmp (signal, "nicklist_group_added") == 0)
        || (strcmp (signal, "nicklist_nick_added") == 0))
    {
        diff = RELAY_WEECHAT_NICKLIST_DIFF_ADDED;
    }
    else if ((strcmp (signal, "nicklist_group_removing") == 0)
        || (strcmp (signal, "nicklist_nick_removing") == 0))
    {
        diff = RELAY_WEECHAT_NICKLIST_DIFF_REMOVED;
    }
    else if ((strcmp (signal, "nicklist_group_changed") == 0)
        || (strcmp (signal, "nicklist_nick_changed") == 0))
    {
        diff = RELAY_WEECHAT_NICKLIST_DIFF_CHANGED;
    }

    if (diff != RELAY_WEECHAT_NICKLIST_DIFF_UNKNOWN)
    {
        /*
         * add items if nicklist was not empty or very small (otherwise we will
         * send full nicklist)
         */
        if (ptr_nicklist->nicklist_count > 1)
        {
            /* add nicklist item for parent group and group/nick */
            relay_weechat_nicklist_add_item (ptr_nicklist,
                                             RELAY_WEECHAT_NICKLIST_DIFF_PARENT,
                                             parent_group, NULL);
            relay_weechat_nicklist_add_item (ptr_nicklist, diff, group, nick);
        }

        /* add timer to send nicklist */
        if (RELAY_WEECHAT_DATA(ptr_client, hook_timer_nicklist))
        {
            weechat_unhook (RELAY_WEECHAT_DATA(ptr_client, hook_timer_nicklist));
            RELAY_WEECHAT_DATA(ptr_client, hook_timer_nicklist) = NULL;
        }
        relay_weechat_hook_timer_nicklist (ptr_client);
    }

    return WEECHAT_RC_OK;
}
コード例 #8
0
ファイル: weechat-aspell.c プロジェクト: FauxFaux/weechat_old
char *
weechat_aspell_modifier_cb (void *data, const char *modifier,
                            const char *modifier_data, const char *string)
{
    long unsigned int value;
    struct t_gui_buffer *buffer;
    struct t_aspell_speller_buffer *ptr_speller_buffer;
    char *result, *ptr_string, *pos_space, *ptr_end, *ptr_end_valid, save_end;
    char *word_for_suggestions, *old_suggestions, *suggestions;
    char *word_and_suggestions;
    const char *color_normal, *color_error, *ptr_suggestions;
    int utf8_char_int, char_size;
    int length, index_result, length_word, word_ok;
    int length_color_normal, length_color_error, rc;
    int input_pos, current_pos, word_start_pos, word_end_pos, word_end_pos_valid;

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

    if (!aspell_enabled)
        return NULL;

    if (!string)
        return NULL;

    rc = sscanf (modifier_data, "%lx", &value);
    if ((rc == EOF) || (rc == 0))
        return NULL;

    buffer = (struct t_gui_buffer *)value;

    /* check text during search only if option is enabled */
    if (weechat_buffer_get_integer (buffer, "text_search")
        && !weechat_config_boolean (weechat_aspell_config_check_during_search))
        return NULL;

    /* get structure with speller info for buffer */
    ptr_speller_buffer = weechat_hashtable_get (weechat_aspell_speller_buffer,
                                                buffer);
    if (!ptr_speller_buffer)
    {
        ptr_speller_buffer = weechat_aspell_speller_buffer_new (buffer);
        if (!ptr_speller_buffer)
            return NULL;
    }
    if (!ptr_speller_buffer->spellers)
        return NULL;

    /*
     * for performance: return last string built if input string is the
     * same (and cursor position is the same, if suggestions are enabled)
     */
    input_pos = weechat_buffer_get_integer (buffer, "input_pos");
    if (ptr_speller_buffer->modifier_string
        && (strcmp (string, ptr_speller_buffer->modifier_string) == 0)
        && ((weechat_config_integer (weechat_aspell_config_check_suggestions) < 0)
            || (input_pos == ptr_speller_buffer->input_pos)))
    {
        return (ptr_speller_buffer->modifier_result) ?
            strdup (ptr_speller_buffer->modifier_result) : NULL;
    }

    /* free last modifier string and result */
    if (ptr_speller_buffer->modifier_string)
    {
        free (ptr_speller_buffer->modifier_string);
        ptr_speller_buffer->modifier_string = NULL;
    }
    if (ptr_speller_buffer->modifier_result)
    {
        free (ptr_speller_buffer->modifier_result);
        ptr_speller_buffer->modifier_result = NULL;
    }

    word_for_suggestions = NULL;

    /* save last modifier string received */
    ptr_speller_buffer->modifier_string = strdup (string);
    ptr_speller_buffer->input_pos = input_pos;

    color_normal = weechat_color ("bar_fg");
    length_color_normal = strlen (color_normal);
    color_error = weechat_color (weechat_config_string (weechat_aspell_config_color_misspelled));
    length_color_error = strlen (color_error);

    length = strlen (string);
    result = malloc (length + (length * length_color_error) + 1);

    if (result)
    {
        result[0] = '\0';

        ptr_string = ptr_speller_buffer->modifier_string;
        index_result = 0;

        /* check if string is a command */
        if (!weechat_string_input_for_buffer (ptr_string))
        {
            char_size = weechat_utf8_char_size (ptr_string);
            ptr_string += char_size;
            pos_space = ptr_string;
            while (pos_space && pos_space[0] && (pos_space[0] != ' '))
            {
                pos_space = weechat_utf8_next_char (pos_space);
            }
            if (!pos_space || !pos_space[0])
            {
                free (result);
                return NULL;
            }

            pos_space[0] = '\0';

            /* exit if command is not authorized for spell checking */
            if (!weechat_aspell_command_authorized (ptr_string))
            {
                free (result);
                return NULL;
            }
            memcpy (result + index_result,
                    ptr_speller_buffer->modifier_string,
                    char_size);
            index_result += char_size;
            strcpy (result + index_result, ptr_string);
            index_result += strlen (ptr_string);

            pos_space[0] = ' ';
            ptr_string = pos_space;
        }

        current_pos = 0;
        while (ptr_string[0])
        {
            /* find start of word: it must start with an alphanumeric char */
            utf8_char_int = weechat_utf8_char_int (ptr_string);
            while ((!iswalnum (utf8_char_int)) || iswspace (utf8_char_int))
            {
                char_size = weechat_utf8_char_size (ptr_string);
                memcpy (result + index_result, ptr_string, char_size);
                index_result += char_size;
                ptr_string += char_size;
                current_pos++;
                if (!ptr_string[0])
                    break;
                utf8_char_int = weechat_utf8_char_int (ptr_string);
            }
            if (!ptr_string[0])
                break;

            word_start_pos = current_pos;
            word_end_pos = current_pos;
            word_end_pos_valid = current_pos;

            /* find end of word: ' and - allowed in word, but not at the end */
            ptr_end_valid = ptr_string;
            ptr_end = weechat_utf8_next_char (ptr_string);
            utf8_char_int = weechat_utf8_char_int (ptr_end);
            while (iswalnum (utf8_char_int) || (utf8_char_int == '\'')
                   || (utf8_char_int == '-'))
            {
                word_end_pos++;
                if (iswalnum (utf8_char_int))
                {
                    /* pointer to last alphanumeric char in the word */
                    ptr_end_valid = ptr_end;
                    word_end_pos_valid = word_end_pos;
                }
                ptr_end = weechat_utf8_next_char (ptr_end);
                if (!ptr_end[0])
                    break;
                utf8_char_int = weechat_utf8_char_int (ptr_end);
            }
            ptr_end = weechat_utf8_next_char (ptr_end_valid);
            word_end_pos = word_end_pos_valid;
            word_ok = 0;
            if (weechat_aspell_string_is_url (ptr_string))
            {
                /*
                 * word is an URL, then it is OK, and search for next space
                 * (will be end of word)
                 */
                word_ok = 1;
                if (ptr_end[0])
                {
                    utf8_char_int = weechat_utf8_char_int (ptr_end);
                    while (!iswspace (utf8_char_int))
                    {
                        ptr_end = weechat_utf8_next_char (ptr_end);
                        if (!ptr_end[0])
                            break;
                        utf8_char_int = weechat_utf8_char_int (ptr_end);
                    }
                }
            }
            save_end = ptr_end[0];
            ptr_end[0] = '\0';
            length_word = ptr_end - ptr_string;

            if (!word_ok)
            {
                if ((save_end != '\0')
                    || (weechat_config_integer (weechat_aspell_config_check_real_time)))
                {
                    word_ok = weechat_aspell_check_word (buffer,
                                                         ptr_speller_buffer,
                                                         ptr_string);
                    if (!word_ok && (input_pos >= word_start_pos))
                    {
                        /*
                         * if word is misspelled and that cursor is after
                         * the beginning of this word, save the word (we will
                         * look for suggestions after this loop)
                         */
                        if (word_for_suggestions)
                            free (word_for_suggestions);
                        word_for_suggestions = strdup (ptr_string);
                    }
                }
                else
                    word_ok = 1;
            }

            /* add error color */
            if (!word_ok)
            {
                strcpy (result + index_result, color_error);
                index_result += length_color_error;
            }

            /* add word */
            strcpy (result + index_result, ptr_string);
            index_result += length_word;

            /* add normal color (after misspelled word) */
            if (!word_ok)
            {
                strcpy (result + index_result, color_normal);
                index_result += length_color_normal;
            }

            if (save_end == '\0')
                break;

            ptr_end[0] = save_end;
            ptr_string = ptr_end;
            current_pos = word_end_pos + 1;
        }
        result[index_result] = '\0';
    }

    /* save old suggestions in buffer */
    ptr_suggestions = weechat_buffer_get_string (buffer,
                                                 "localvar_aspell_suggest");
    old_suggestions = (ptr_suggestions) ? strdup (ptr_suggestions) : NULL;

    /* if there is a misspelled word, get suggestions and set them in buffer */
    if (word_for_suggestions)
    {
        suggestions = weechat_aspell_get_suggestions (ptr_speller_buffer,
                                                      word_for_suggestions);
        if (suggestions)
        {
            length = strlen (word_for_suggestions) + 1 /* ":" */
                + strlen (suggestions) + 1;
            word_and_suggestions = malloc (length);
            if (word_and_suggestions)
            {
                snprintf (word_and_suggestions, length, "%s:%s",
                          word_for_suggestions, suggestions);
                weechat_buffer_set (buffer, "localvar_set_aspell_suggest",
                                    word_and_suggestions);
                free (word_and_suggestions);
            }
            else
            {
                weechat_buffer_set (buffer, "localvar_del_aspell_suggest", "");
            }
            free (suggestions);
        }
        else
        {
            weechat_buffer_set (buffer, "localvar_del_aspell_suggest", "");
        }
        free (word_for_suggestions);
    }
    else
    {
        weechat_buffer_set (buffer, "localvar_del_aspell_suggest", "");
    }

    /*
     * if suggestions have changed, update the bar item
     * and send signal "aspell_suggest"
     */
    ptr_suggestions = weechat_buffer_get_string (buffer,
                                                 "localvar_aspell_suggest");
    if ((old_suggestions && !ptr_suggestions)
        || (!old_suggestions && ptr_suggestions)
        || (old_suggestions && ptr_suggestions
            && (strcmp (old_suggestions, ptr_suggestions) != 0)))
    {
        weechat_bar_item_update ("aspell_suggest");
        weechat_hook_signal_send ("aspell_suggest",
                                  WEECHAT_HOOK_SIGNAL_POINTER, buffer);
    }
    if (old_suggestions)
        free (old_suggestions);

    if (!result)
        return NULL;

    ptr_speller_buffer->modifier_result = strdup (result);

    return result;
}
コード例 #9
0
ファイル: exec-command.c プロジェクト: bobbertson/weechat
int
exec_command_parse_options (struct t_exec_cmd_options *cmd_options,
                            int argc, char **argv, int start_arg,
                            int set_command_index)
{
    int i, j, end, length, length_total;
    char *error;

    for (i = start_arg; i < argc; i++)
    {
        if (weechat_strcasecmp (argv[i], "-sh") == 0)
        {
            cmd_options->use_shell = 1;
        }
        else if (weechat_strcasecmp (argv[i], "-nosh") == 0)
        {
            cmd_options->use_shell = 0;
        }
        else if (weechat_strcasecmp (argv[i], "-bg") == 0)
        {
            cmd_options->detached = 1;
        }
        else if (weechat_strcasecmp (argv[i], "-nobg") == 0)
        {
            cmd_options->detached = 0;
        }
        else if (weechat_strcasecmp (argv[i], "-stdin") == 0)
        {
            cmd_options->pipe_stdin = 1;
        }
        else if (weechat_strcasecmp (argv[i], "-nostdin") == 0)
        {
            cmd_options->pipe_stdin = 0;
        }
        else if (weechat_strcasecmp (argv[i], "-buffer") == 0)
        {
            if (i + 1 >= argc)
                return 0;
            i++;
            cmd_options->ptr_buffer_name = argv[i];
            cmd_options->ptr_buffer = weechat_buffer_search ("==", argv[i]);
            if (cmd_options->ptr_buffer
                && (weechat_buffer_get_integer (cmd_options->ptr_buffer, "type") != 0))
            {
                /* only a buffer with formatted content is allowed */
                return 0;
            }
            if (!cmd_options->ptr_buffer)
                cmd_options->new_buffer = 1;
        }
        else if (weechat_strcasecmp (argv[i], "-l") == 0)
        {
            cmd_options->output_to_buffer = 0;
            cmd_options->new_buffer = 0;
        }
        else if (weechat_strcasecmp (argv[i], "-o") == 0)
        {
            cmd_options->output_to_buffer = 1;
            cmd_options->new_buffer = 0;
        }
        else if (weechat_strcasecmp (argv[i], "-n") == 0)
        {
            cmd_options->output_to_buffer = 0;
            cmd_options->new_buffer = 1;
        }
        else if (weechat_strcasecmp (argv[i], "-nf") == 0)
        {
            cmd_options->output_to_buffer = 0;
            cmd_options->new_buffer = 2;
        }
        else if (weechat_strcasecmp (argv[i], "-cl") == 0)
        {
            cmd_options->new_buffer_clear = 1;
        }
        else if (weechat_strcasecmp (argv[i], "-nocl") == 0)
        {
            cmd_options->new_buffer_clear = 0;
        }
        else if (weechat_strcasecmp (argv[i], "-sw") == 0)
        {
            cmd_options->switch_to_buffer = 1;
        }
        else if (weechat_strcasecmp (argv[i], "-nosw") == 0)
        {
            cmd_options->switch_to_buffer = 0;
        }
        else if (weechat_strcasecmp (argv[i], "-ln") == 0)
        {
            cmd_options->line_numbers = 1;
        }
        else if (weechat_strcasecmp (argv[i], "-noln") == 0)
        {
            cmd_options->line_numbers = 0;
        }
        else if (weechat_strcasecmp (argv[i], "-flush") == 0)
        {
            cmd_options->flush = 1;
        }
        else if (weechat_strcasecmp (argv[i], "-noflush") == 0)
        {
            cmd_options->flush = 0;
        }
        else if (weechat_strcasecmp (argv[i], "-color") == 0)
        {
            if (i + 1 >= argc)
                return 0;
            i++;
            cmd_options->color = exec_search_color (argv[i]);
            if (cmd_options->color < 0)
                return 0;
        }
        else if (weechat_strcasecmp (argv[i], "-rc") == 0)
        {
            cmd_options->display_rc = 1;
        }
        else if (weechat_strcasecmp (argv[i], "-norc") == 0)
        {
            cmd_options->display_rc = 0;
        }
        else if (weechat_strcasecmp (argv[i], "-timeout") == 0)
        {
            if (i + 1 >= argc)
                return 0;
            i++;
            error = NULL;
            cmd_options->timeout = strtol (argv[i], &error, 10);
            if (!error || error[0])
                return 0;
        }
        else if (weechat_strcasecmp (argv[i], "-name") == 0)
        {
            if (i + 1 >= argc)
                return 0;
            i++;
            cmd_options->ptr_command_name = argv[i];
        }
        else if (weechat_strcasecmp (argv[i], "-pipe") == 0)
        {
            if (i + 1 >= argc)
                return 0;
            i++;
            if (cmd_options->pipe_command)
            {
                free (cmd_options->pipe_command);
                cmd_options->pipe_command = NULL;
            }
            if (argv[i][0] == '"')
            {
                /* search the ending double quote */
                length_total = 1;
                end = i;
                while (end < argc)
                {
                    length = strlen (argv[end]);
                    length_total += length + 1;
                    if ((length > 0) && (argv[end][length - 1] == '"'))
                        break;
                    end++;
                }
                if (end == argc)
                    return 0;
                cmd_options->pipe_command = malloc (length_total);
                if (!cmd_options->pipe_command)
                    return 0;
                cmd_options->pipe_command[0] = '\0';
                for (j = i; j <= end; j++)
                {
                    if (cmd_options->pipe_command[0])
                        strcat (cmd_options->pipe_command, " ");
                    strcat (cmd_options->pipe_command,
                            (j == i) ? argv[j] + 1 : argv[j]);
                }
                length = strlen (cmd_options->pipe_command);
                if (length > 0)
                    cmd_options->pipe_command[length - 1] = '\0';
                i = end;
            }
            else
                cmd_options->pipe_command = strdup (argv[i]);
        }
        else if (weechat_strcasecmp (argv[i], "-hsignal") == 0)
        {
            if (i + 1 >= argc)
                return 0;
            i++;
            if (cmd_options->hsignal)
            {
                free (cmd_options->hsignal);
                cmd_options->hsignal = NULL;
            }
            cmd_options->hsignal = strdup (argv[i]);
        }
        else
        {
            if (set_command_index)
            {
                cmd_options->command_index = i;
                break;
            }
            else
                return 0;
        }
    }

    return 1;
}
コード例 #10
0
ファイル: exec.c プロジェクト: weechat/weechat
void
exec_end_command (struct t_exec_cmd *exec_cmd, int return_code)
{
    struct t_gui_buffer *ptr_buffer;
    struct t_hashtable *hashtable;
    char str_number[32], *output;
    int i, buffer_type;

    if (exec_cmd->hsignal)
    {
        hashtable = weechat_hashtable_new (32,
                                           WEECHAT_HASHTABLE_STRING,
                                           WEECHAT_HASHTABLE_STRING,
                                           NULL, NULL);
        if (hashtable)
        {
            weechat_hashtable_set (hashtable, "command", exec_cmd->command);
            snprintf (str_number, sizeof (str_number), "%d", exec_cmd->number);
            weechat_hashtable_set (hashtable, "number", str_number);
            weechat_hashtable_set (hashtable, "name", exec_cmd->name);
            output = exec_decode_color (exec_cmd, exec_cmd->output[EXEC_STDOUT]);
            weechat_hashtable_set (hashtable, "out", output);
            if (output)
                free (output);
            output = exec_decode_color (exec_cmd, exec_cmd->output[EXEC_STDERR]);
            weechat_hashtable_set (hashtable, "err", output);
            if (output)
                free (output);
            snprintf (str_number, sizeof (str_number), "%d", return_code);
            weechat_hashtable_set (hashtable, "rc", str_number);
            weechat_hook_hsignal_send (exec_cmd->hsignal, hashtable);
            weechat_hashtable_free (hashtable);
        }
    }
    else
    {
        ptr_buffer = weechat_buffer_search ("==", exec_cmd->buffer_full_name);

        /* display the last line of output (if not ending with '\n') */
        exec_display_line (exec_cmd, ptr_buffer, EXEC_STDOUT,
                           exec_cmd->output[EXEC_STDOUT]);
        exec_display_line (exec_cmd, ptr_buffer, EXEC_STDERR,
                           exec_cmd->output[EXEC_STDERR]);

        /*
         * display return code (only if command is not detached, if output is
         * NOT sent to buffer, and if command is not piped)
         */
        if (exec_cmd->display_rc
            && !exec_cmd->detached && !exec_cmd->output_to_buffer
            && !exec_cmd->pipe_command)
        {
            buffer_type = weechat_buffer_get_integer (ptr_buffer, "type");
            if (return_code >= 0)
            {
                if (buffer_type == 1)
                {
                    weechat_printf_y (ptr_buffer, -1,
                                      ("%s: end of command %d (\"%s\"), "
                                       "return code: %d"),
                                      EXEC_PLUGIN_NAME, exec_cmd->number,
                                      exec_cmd->command, return_code);
                }
                else
                {
                    weechat_printf_date_tags (
                        ptr_buffer, 0, "exec_rc",
                        _("%s: end of command %d (\"%s\"), "
                          "return code: %d"),
                        EXEC_PLUGIN_NAME, exec_cmd->number,
                        exec_cmd->command, return_code);
                }
            }
            else
            {
                if (buffer_type == 1)
                {
                    weechat_printf_y (ptr_buffer, -1,
                                      _("%s: unexpected end of command %d "
                                        "(\"%s\")"),
                                      EXEC_PLUGIN_NAME, exec_cmd->number,
                                      exec_cmd->command);
                }
                else
                {
                    weechat_printf_date_tags (
                        ptr_buffer, 0, "exec_rc",
                        _("%s: unexpected end of command %d "
                          "(\"%s\")"),
                        EXEC_PLUGIN_NAME, exec_cmd->number,
                        exec_cmd->command);
                }
            }
        }
    }

    /* (re)set some variables after the end of command */
    exec_cmd->hook = NULL;
    exec_cmd->pid = 0;
    exec_cmd->end_time = time (NULL);
    exec_cmd->return_code = return_code;
    for (i = 0; i < 2; i++)
    {
        if (exec_cmd->output[i])
        {
            free (exec_cmd->output[i]);
            exec_cmd->output[i] = NULL;
        }
        exec_cmd->output_size[i] = 0;
    }

    /* schedule a timer to remove the executed command */
    if (weechat_config_integer (exec_config_command_purge_delay) >= 0)
    {
        weechat_hook_timer (1 + (1000 * weechat_config_integer (exec_config_command_purge_delay)),
                            0, 1,
                            &exec_timer_delete_cb, exec_cmd, NULL);
    }
}
コード例 #11
0
ファイル: exec.c プロジェクト: weechat/weechat
void
exec_display_line (struct t_exec_cmd *exec_cmd, struct t_gui_buffer *buffer,
                   int out, const char *line)
{
    char *line_color, *line_color2, *line2, str_number[32], str_tags[1024];
    const char *ptr_line_color;
    int length;

    if (!exec_cmd || !line)
        return;

    /*
     * if output is sent to the buffer, the buffer must exist
     * (we don't send output by default to core buffer)
     */
    if (exec_cmd->output_to_buffer && !exec_cmd->pipe_command && !buffer)
        return;

    /* decode colors */
    line_color = exec_decode_color (exec_cmd, line);
    if (!line_color)
        return;

    exec_cmd->output_line_nb++;

    if (exec_cmd->pipe_command)
    {
        if (strstr (exec_cmd->pipe_command, "$line"))
        {
            /* replace $line by line content */
            line2 = weechat_string_replace (exec_cmd->pipe_command,
                                            "$line", line_color);
            if (line2)
            {
                weechat_command (buffer, line2);
                free (line2);
            }
        }
        else
        {
            /* add line at the end of command, after a space */
            length = strlen (exec_cmd->pipe_command) + 1 + strlen (line_color) + 1;
            line2 = malloc (length);
            if (line2)
            {
                snprintf (line2, length,
                          "%s %s", exec_cmd->pipe_command, line_color);
                weechat_command (buffer, line2);
                free (line2);
            }
        }
    }
    else if (exec_cmd->output_to_buffer)
    {
        if (exec_cmd->line_numbers)
        {
            length = 32 + strlen (line_color) + 1;
            line2 = malloc (length);
            if (line2)
            {
                snprintf (line2, length,
                          "%d. %s", exec_cmd->output_line_nb, line_color);
                weechat_command (buffer, line2);
                free (line2);
            }
        }
        else
        {
            if (exec_cmd->output_to_buffer_exec_cmd)
                ptr_line_color = line_color;
            else
                ptr_line_color = weechat_string_input_for_buffer (line_color);

            if (ptr_line_color)
            {
                weechat_command (buffer,
                                 (ptr_line_color[0]) ? ptr_line_color : " ");
            }
            else
            {
                length = 1 + strlen (line_color) + 1;
                line_color2 = malloc (length);
                if (line_color2)
                {
                    snprintf (line_color2, length, "%c%s",
                              line_color[0], line_color);
                    weechat_command (buffer,
                                     (line_color2[0]) ? line_color2 : " ");
                    free (line_color2);
                }
            }
        }
    }
    else
    {
        snprintf (str_number, sizeof (str_number), "%d", exec_cmd->number);
        snprintf (str_tags, sizeof (str_tags),
                  "exec_%s,exec_cmd_%s",
                  (out == EXEC_STDOUT) ? "stdout" : "stderr",
                  (exec_cmd->name) ? exec_cmd->name : str_number);
        if (weechat_buffer_get_integer (buffer, "type") == 1)
        {
            snprintf (str_number, sizeof (str_number),
                      "%d. ", exec_cmd->output_line_nb);
            weechat_printf_y (buffer, -1,
                              "%s%s",
                              (exec_cmd->line_numbers) ? str_number : " ",
                              line_color);
        }
        else
        {
            snprintf (str_number, sizeof (str_number),
                      "%d\t", exec_cmd->output_line_nb);
            weechat_printf_date_tags (
                buffer, 0, str_tags,
                "%s%s",
                (exec_cmd->line_numbers) ? str_number : " \t",
                line_color);
        }
    }

    free (line_color);
}
コード例 #12
0
ファイル: weechat-aspell.c プロジェクト: jameslord/weechat
char *
weechat_aspell_modifier_cb (void *data, const char *modifier,
                            const char *modifier_data, const char *string)
{
    long unsigned int value;
    struct t_gui_buffer *buffer;
    char *result, *ptr_string, *pos_space, *ptr_end, save_end;
    const char *color_normal, *color_error;
    int buffer_has_changed, utf8_char_int, char_size;
    int length, index_result, length_word, word_ok;
    int length_color_normal, length_color_error, rc;

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

    if (!aspell_enabled)
        return NULL;

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

    rc = sscanf (modifier_data, "%lx", &value);
    if ((rc == EOF) || (rc == 0))
        return NULL;

    buffer = (struct t_gui_buffer *)value;

    buffer_has_changed = 0;
    if (buffer != aspell_buffer_spellers)
    {
        weechat_aspell_create_spellers (buffer);
        aspell_buffer_spellers = buffer;
        buffer_has_changed = 1;
    }

    if (!weechat_aspell_spellers)
        return NULL;

    /* check text search only if option is enabled */
    if (weechat_buffer_get_integer (buffer, "text_search")
        && !weechat_config_boolean (weechat_aspell_config_check_during_search))
        return NULL;

    /*
     * for performance: return last string built if input string is the
     * same (for example user just change cursor position, or input text is
     * refreshed with same content)
     */
    if (!buffer_has_changed
        && aspell_last_modifier_string
        && (strcmp (string, aspell_last_modifier_string) == 0))
    {
        return (aspell_last_modifier_result) ?
            strdup (aspell_last_modifier_result) : NULL;
    }

    /* free last modifier string and result */
    if (aspell_last_modifier_string)
    {
        free (aspell_last_modifier_string);
        aspell_last_modifier_string = NULL;
    }
    if (aspell_last_modifier_result)
    {
        free (aspell_last_modifier_result);
        aspell_last_modifier_result = NULL;
    }

    /* save last modifier string received */
    aspell_last_modifier_string = strdup (string);

    color_normal = weechat_color ("bar_fg");
    length_color_normal = strlen (color_normal);
    color_error = weechat_color (weechat_config_string (weechat_aspell_config_look_color));
    length_color_error = strlen (color_error);

    length = strlen (string);
    result = malloc (length + (length * length_color_error) + 1);

    if (result)
    {
        result[0] = '\0';

        ptr_string = aspell_last_modifier_string;
        index_result = 0;

        /* check if string is a command */
        if (!weechat_string_input_for_buffer (ptr_string))
        {
            char_size = weechat_utf8_char_size (ptr_string);
            ptr_string += char_size;
            pos_space = ptr_string;
            while (pos_space && pos_space[0] && (pos_space[0] != ' '))
            {
                pos_space = weechat_utf8_next_char (pos_space);
            }
            if (!pos_space || !pos_space[0])
            {
                free (result);
                return NULL;
            }

            pos_space[0] = '\0';

            /* exit if command is not authorized for spell checking */
            if (!weechat_aspell_command_authorized (ptr_string))
            {
                free (result);
                return NULL;
            }
            memcpy (result + index_result, aspell_last_modifier_string, char_size);
            index_result += char_size;
            strcpy (result + index_result, ptr_string);
            index_result += strlen (ptr_string);

            pos_space[0] = ' ';
            ptr_string = pos_space;
        }

        while (ptr_string[0])
        {
            /* find start of word */
            utf8_char_int = weechat_utf8_char_int (ptr_string);
            while ((!iswalnum (utf8_char_int) && (utf8_char_int != '\'')
                    && (utf8_char_int != '-'))
                   || iswspace (utf8_char_int))
            {
                char_size = weechat_utf8_char_size (ptr_string);
                memcpy (result + index_result, ptr_string, char_size);
                index_result += char_size;
                ptr_string += char_size;
                if (!ptr_string[0])
                    break;
                utf8_char_int = weechat_utf8_char_int (ptr_string);
            }
            if (!ptr_string[0])
                break;

            ptr_end = weechat_utf8_next_char (ptr_string);
            utf8_char_int = weechat_utf8_char_int (ptr_end);
            while (iswalnum (utf8_char_int) || (utf8_char_int == '\'')
                   || (utf8_char_int == '-'))
            {
                ptr_end = weechat_utf8_next_char (ptr_end);
                if (!ptr_end[0])
                    break;
                utf8_char_int = weechat_utf8_char_int (ptr_end);
            }
            word_ok = 0;
            if (weechat_aspell_string_is_url (ptr_string))
            {
                /*
                 * word is an URL, then it is ok, and search for next space
                 * (will be end of word)
                 */
                word_ok = 1;
                if (ptr_end[0])
                {
                    utf8_char_int = weechat_utf8_char_int (ptr_end);
                    while (!iswspace (utf8_char_int))
                    {
                        ptr_end = weechat_utf8_next_char (ptr_end);
                        if (!ptr_end[0])
                            break;
                        utf8_char_int = weechat_utf8_char_int (ptr_end);
                    }
                }
            }
            save_end = ptr_end[0];
            ptr_end[0] = '\0';
            length_word = ptr_end - ptr_string;

            if (!word_ok)
            {
                if ((save_end != '\0')
                    || (weechat_config_integer (weechat_aspell_config_check_real_time)))
                    word_ok = weechat_aspell_check_word (buffer, ptr_string);
                else
                    word_ok = 1;
            }

            /* add error color */
            if (!word_ok)
            {
                strcpy (result + index_result, color_error);
                index_result += length_color_error;
            }

            /* add word */
            strcpy (result + index_result, ptr_string);
            index_result += length_word;

            /* add normal color (after misspelled word) */
            if (!word_ok)
            {
                strcpy (result + index_result, color_normal);
                index_result += length_color_normal;
            }

            if (save_end == '\0')
                break;

            ptr_end[0] = save_end;
            ptr_string = ptr_end;
        }

        result[index_result] = '\0';
    }

    if (!result)
        return NULL;

    aspell_last_modifier_result = strdup (result);
    return result;
}