Beispiel #1
0
struct t_irc_raw_message *
irc_raw_message_add (struct t_irc_server *server, int send, int modified,
                     const char *message)
{
    char *buf, *buf2, prefix[256];
    const unsigned char *ptr_buf;
    const char *hexa = "0123456789ABCDEF";
    int pos_buf, pos_buf2, char_size, i;
    struct t_irc_raw_message *new_raw_message;
    
    buf = weechat_iconv_to_internal (NULL, message);
    buf2 = malloc ((strlen (buf) * 3) + 1);
    if (buf2)
    {
        ptr_buf = (buf) ? (unsigned char *)buf : (unsigned char *)message;
        pos_buf = 0;
        pos_buf2 = 0;
        while (ptr_buf[pos_buf])
        {
            if (ptr_buf[pos_buf] < 32)
            {
                buf2[pos_buf2++] = '\\';
                buf2[pos_buf2++] = hexa[ptr_buf[pos_buf] / 16];
                buf2[pos_buf2++] = hexa[ptr_buf[pos_buf] % 16];
                pos_buf++;
            }
            else
            {
                char_size = weechat_utf8_char_size ((const char *)(ptr_buf + pos_buf));
                for (i = 0; i < char_size; i++)
                {
                    buf2[pos_buf2++] = ptr_buf[pos_buf++];
                }
            }
        }
        buf2[pos_buf2] = '\0';
    }
    snprintf (prefix, sizeof (prefix), "%s%s%s%s%s",
              (server) ? weechat_color ("chat_server") : "",
              (server) ? server->name : "",
              (server) ? " " : "",
              (send) ?
              weechat_color ("chat_prefix_quit") :
              weechat_color ("chat_prefix_join"),
              (send) ?
              ((modified) ? IRC_RAW_PREFIX_SEND_MOD : IRC_RAW_PREFIX_SEND) :
              ((modified) ? IRC_RAW_PREFIX_RECV_MOD : IRC_RAW_PREFIX_RECV));
    
    new_raw_message = irc_raw_message_add_to_list (time (NULL),
                                                   prefix,
                                                   (buf2) ? buf2 : ((buf) ? buf : message));
    
    if (buf)
        free (buf);
    if (buf2)
        free (buf2);
    
    return new_raw_message;
}
Beispiel #2
0
void
script_buffer_display_line_script (int line, struct t_script_repo *script)
{
    char str_line[16384], str_item[1024], str_color_name[256], str_color[32];
    char str_format[256], str_date[64], str_key[2], utf_char[16], *tags;
    const char *columns, *ptr_col;
    int char_size, *ptr_max_length, max_length, num_spaces, unknown;
    struct tm *tm;

    snprintf (str_color_name, sizeof (str_color_name),
              "%s,%s",
              (line == script_buffer_selected_line) ?
              weechat_config_string (script_config_color_text_selected) :
              weechat_config_string (script_config_color_text),
              (line == script_buffer_selected_line) ?
              weechat_config_string (script_config_color_text_bg_selected) :
              weechat_config_string (script_config_color_text_bg));
    snprintf (str_color, sizeof (str_color),
              "%s", weechat_color (str_color_name));

    columns = weechat_config_string (script_config_look_columns);
    ptr_col = columns;

    str_line[0] = '\0';
    while (ptr_col[0])
    {
        unknown = 0;
        str_item[0] = '\0';
        num_spaces = 0;
        char_size = weechat_utf8_char_size (ptr_col);
        memcpy (utf_char, ptr_col, char_size);
        utf_char[char_size] = '\0';
        if (utf_char[0] == '%')
        {
            ptr_col += char_size;
            char_size = weechat_utf8_char_size (ptr_col);
            memcpy (utf_char, ptr_col, char_size);
            utf_char[char_size] = '\0';

            str_key[0] = ptr_col[0];
            str_key[1] = '\0';
            ptr_max_length = weechat_hashtable_get (script_repo_max_length_field,
                                                    str_key);
            max_length = (ptr_max_length) ? *ptr_max_length : 0;
            num_spaces = max_length;

            switch (utf_char[0])
            {
                case 'a': /* author */
                    if (script->author)
                    {
                        num_spaces = max_length - weechat_utf8_strlen_screen (script->author);
                        snprintf (str_item, sizeof (str_item),
                                  "%s", script->author);
                    }
                    break;
                case 'd': /* description */
                    if (script->description)
                    {
                        num_spaces = max_length - weechat_utf8_strlen_screen (script->description);
                        snprintf (str_item, sizeof (str_item),
                                  "%s%s",
                                  weechat_color (
                                      weechat_config_string (
                                          (line == script_buffer_selected_line) ?
                                          script_config_color_text_description_selected :
                                          script_config_color_text_description)),
                                  script->description);
                    }
                    break;
                case 'D': /* date added */
                    if (script->date_added > 0)
                    {
                        tm = localtime (&script->date_added);
                        strftime (str_date, sizeof (str_date),
                                  "%Y-%m-%d", tm);
                        snprintf (str_item, sizeof (str_item),
                                  "%s%s",
                                  weechat_color (
                                      weechat_config_string (
                                          (line == script_buffer_selected_line) ?
                                          script_config_color_text_date_selected :
                                          script_config_color_text_date)),
                                  str_date);
                    }
                    else
                        num_spaces = 10;
                    break;
                case 'e': /* file extension */
                    if (script->language >= 0)
                    {
                        num_spaces = max_length - weechat_utf8_strlen_screen (script_extension[script->language]);
                        snprintf (str_item, sizeof (str_item),
                                  "%s%s",
                                  weechat_color (
                                      weechat_config_string (
                                          (line == script_buffer_selected_line) ?
                                          script_config_color_text_extension_selected :
                                          script_config_color_text_extension)),
                                  script_extension[script->language]);
                    }
                    break;
                case 'l': /* language */
                    if (script->language >= 0)
                    {
                        num_spaces = max_length - weechat_utf8_strlen_screen (script_language[script->language]);
                        snprintf (str_item, sizeof (str_item),
                                  "%s", script_language[script->language]);
                    }
                    break;
                case 'L': /* license */
                    if (script->license)
                    {
                        num_spaces = max_length - weechat_utf8_strlen_screen (script->license);
                        snprintf (str_item, sizeof (str_item),
                                  "%s", script->license);
                    }
                    break;
                case 'n': /* name + extension */
                    if (script->name_with_extension)
                    {
                        num_spaces = max_length - weechat_utf8_strlen_screen (script->name_with_extension);
                        snprintf (str_item, sizeof (str_item),
                                  "%s%s%s.%s",
                                  weechat_color (
                                      weechat_config_string (
                                          (line == script_buffer_selected_line) ?
                                          script_config_color_text_name_selected :
                                          script_config_color_text_name)),
                                  script->name,
                                  weechat_color (
                                      weechat_config_string (
                                          (line == script_buffer_selected_line) ?
                                          script_config_color_text_extension_selected :
                                          script_config_color_text_extension)),
                                  script_extension[script->language]);
                    }
                    break;
                case 'N': /* name (without extension) */
                    if (script->name)
                    {
                        num_spaces = max_length - weechat_utf8_strlen_screen (script->name);
                        snprintf (str_item, sizeof (str_item),
                                  "%s%s",
                                  weechat_color (
                                      weechat_config_string (
                                          (line == script_buffer_selected_line) ?
                                          script_config_color_text_name_selected :
                                          script_config_color_text_name)),
                                  script->name);
                    }
                    break;
                case 'r': /* requirements */
                    if (script->requirements)
                    {
                        num_spaces = max_length - weechat_utf8_strlen_screen (script->requirements);
                        snprintf (str_item, sizeof (str_item),
                                  "%s", script->requirements);
                    }
                    break;
                case 's': /* status */
                    snprintf (str_item, sizeof (str_item),
                              "%s",
                              script_repo_get_status_for_display (script,
                                                                  "*iaHrN", 0));
                    break;
                case 't': /* tags */
                    if (script->tags)
                    {
                        num_spaces = max_length - weechat_utf8_strlen_screen (script->tags);
                        tags = weechat_string_replace (script->tags, ",", " ");
                        if (tags)
                        {
                            snprintf (str_item, sizeof (str_item),
                                      "%s%s",
                                      weechat_color (
                                          weechat_config_string (
                                              (line == script_buffer_selected_line) ?
                                              script_config_color_text_tags_selected :
                                              script_config_color_text_tags)),
                                      tags);
                            free (tags);
                        }
                    }
                    break;
                case 'u': /* date updated */
                    if (script->date_updated > 0)
                    {
                        tm = localtime (&script->date_updated);
                        strftime (str_date, sizeof (str_date),
                                  "%Y-%m-%d", tm);
                        snprintf (str_item, sizeof (str_item),
                                  "%s%s",
                                  weechat_color (
                                      weechat_config_string (
                                          (line == script_buffer_selected_line) ?
                                          script_config_color_text_date_selected :
                                          script_config_color_text_date)),
                                  str_date);
                    }
                    else
                        num_spaces = 10;
                    break;
                case 'v': /* version */
                    if (script->version)
                    {
                        num_spaces = max_length - weechat_utf8_strlen_screen (script->version);
                        snprintf (str_item, sizeof (str_item),
                                  "%s%s",
                                  weechat_color (
                                      weechat_config_string (
                                          (line == script_buffer_selected_line) ?
                                          script_config_color_text_version_selected :
                                          script_config_color_text_version)),
                                  script->version);
                    }
                    break;
                case 'V': /* version loaded */
                    if (script->version_loaded)
                    {
                        num_spaces = max_length - weechat_utf8_strlen_screen (script->version_loaded);
                        snprintf (str_item, sizeof (str_item),
                                  "%s%s",
                                  weechat_color (
                                      weechat_config_string (
                                          (line == script_buffer_selected_line) ?
                                          script_config_color_text_version_loaded_selected :
                                          script_config_color_text_version_loaded)),
                                  script->version_loaded);
                    }
                    break;
                case 'w': /* min_weechat */
                    if (script->min_weechat)
                    {
                        num_spaces = max_length - weechat_utf8_strlen_screen (script->min_weechat);
                        snprintf (str_item, sizeof (str_item),
                                  "%s", script->min_weechat);
                    }
                    break;
                case 'W': /* max_weechat */
                    if (script->max_weechat)
                    {
                        num_spaces = max_length - weechat_utf8_strlen_screen (script->max_weechat);
                        snprintf (str_item, sizeof (str_item),
                                  "%s", script->max_weechat);
                    }
                    break;
                case '%': /* "%%" will display a single "%" */
                    snprintf (str_item, sizeof (str_item),
                              "%s%%",
                              weechat_color (weechat_config_string (script_config_color_text_delimiters)));
                    break;
                default:
                    unknown = 1;
                    break;
            }
        }
        else
        {
            snprintf (str_item, sizeof (str_item),
                      "%s%s",
                      weechat_color (weechat_config_string (script_config_color_text_delimiters)),
                      utf_char);
        }
        if (!unknown)
        {
            if (str_item[0])
            {
                strcat (str_line, str_color);
                strcat (str_line, str_item);
            }
            if (num_spaces > 0)
            {
                snprintf (str_format, sizeof (str_format),
                          "%%-%ds",
                          num_spaces);
                snprintf (str_item, sizeof (str_item),
                          str_format, " ");
                strcat (str_line, str_item);
            }
        }
        ptr_col += char_size;
    }

    weechat_printf_y (script_buffer, line, "%s", str_line);
}
Beispiel #3
0
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;
}
Beispiel #4
0
struct t_relay_raw_message *
relay_raw_message_add (struct t_relay_client *client, int flags,
                       const char *message)
{
    char *buf, *buf2, prefix[256], prefix_arrow[16];
    const unsigned char *ptr_buf;
    const char *hexa = "0123456789ABCDEF";
    int pos_buf, pos_buf2, char_size, i;
    struct t_relay_raw_message *new_raw_message;

    buf = weechat_iconv_to_internal (NULL, message);
    buf2 = malloc ((strlen (buf) * 3) + 1);
    if (buf2)
    {
        ptr_buf = (buf) ? (unsigned char *)buf : (unsigned char *)message;
        pos_buf = 0;
        pos_buf2 = 0;
        while (ptr_buf[pos_buf])
        {
            if (ptr_buf[pos_buf] < 32)
            {
                buf2[pos_buf2++] = '\\';
                buf2[pos_buf2++] = hexa[ptr_buf[pos_buf] / 16];
                buf2[pos_buf2++] = hexa[ptr_buf[pos_buf] % 16];
                pos_buf++;
            }
            else
            {
                char_size = weechat_utf8_char_size ((const char *)(ptr_buf + pos_buf));
                for (i = 0; i < char_size; i++)
                {
                    buf2[pos_buf2++] = ptr_buf[pos_buf++];
                }
            }
        }
        buf2[pos_buf2] = '\0';
    }

    /* build prefix with arrow */
    prefix_arrow[0] = '\0';
    switch (flags & (RELAY_RAW_FLAG_RECV | RELAY_RAW_FLAG_SEND))
    {
        case RELAY_RAW_FLAG_RECV:
            strcpy (prefix_arrow, RELAY_RAW_PREFIX_RECV);
            break;
        case RELAY_RAW_FLAG_SEND:
            strcpy (prefix_arrow, RELAY_RAW_PREFIX_SEND);
            break;
        default:
            if (flags & RELAY_RAW_FLAG_RECV)
                strcpy (prefix_arrow, RELAY_RAW_PREFIX_RECV);
            else
                strcpy (prefix_arrow, RELAY_RAW_PREFIX_SEND);
            break;
    }

    if (client)
    {
        snprintf (prefix, sizeof (prefix), "%s[%s%d%s] %s%s%s%s %s%s",
                  weechat_color ("chat_delimiters"),
                  weechat_color ("chat"),
                  client->id,
                  weechat_color ("chat_delimiters"),
                  weechat_color ("chat_server"),
                  relay_protocol_string[client->protocol],
                  (client->protocol_args) ? "." : "",
                  (client->protocol_args) ? client->protocol_args : "",
                  (flags & RELAY_RAW_FLAG_SEND) ?
                  weechat_color ("chat_prefix_quit") :
                  weechat_color ("chat_prefix_join"),
                  prefix_arrow);
    }
    else
    {
        snprintf (prefix, sizeof (prefix), "%s%s",
                  (flags & RELAY_RAW_FLAG_SEND) ?
                  weechat_color ("chat_prefix_quit") :
                  weechat_color ("chat_prefix_join"),
                  prefix_arrow);
    }

    new_raw_message = relay_raw_message_add_to_list (time (NULL),
                                                     prefix,
                                                     (buf2) ? buf2 : ((buf) ? buf : message));

    if (buf)
        free (buf);
    if (buf2)
        free (buf2);

    return new_raw_message;
}
Beispiel #5
0
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;
}
Beispiel #6
0
struct t_irc_raw_message *
irc_raw_message_add (struct t_irc_server *server, int flags,
                     const char *message)
{
    char *buf, *buf2, prefix[256], prefix_arrow[16];
    const unsigned char *ptr_buf;
    const char *hexa = "0123456789ABCDEF";
    int pos_buf, pos_buf2, char_size, i;
    struct t_irc_raw_message *new_raw_message;

    buf = weechat_iconv_to_internal (NULL, message);
    buf2 = malloc ((strlen (buf) * 3) + 1);
    if (buf2)
    {
        ptr_buf = (buf) ? (unsigned char *)buf : (unsigned char *)message;
        pos_buf = 0;
        pos_buf2 = 0;
        while (ptr_buf[pos_buf])
        {
            if (ptr_buf[pos_buf] < 32)
            {
                buf2[pos_buf2++] = '\\';
                buf2[pos_buf2++] = hexa[ptr_buf[pos_buf] / 16];
                buf2[pos_buf2++] = hexa[ptr_buf[pos_buf] % 16];
                pos_buf++;
            }
            else
            {
                char_size = weechat_utf8_char_size ((const char *)(ptr_buf + pos_buf));
                for (i = 0; i < char_size; i++)
                {
                    buf2[pos_buf2++] = ptr_buf[pos_buf++];
                }
            }
        }
        buf2[pos_buf2] = '\0';
    }

    /* build prefix with arrow */
    prefix_arrow[0] = '\0';
    switch (flags & (IRC_RAW_FLAG_RECV | IRC_RAW_FLAG_SEND
                     | IRC_RAW_FLAG_MODIFIED | IRC_RAW_FLAG_REDIRECT))
    {
        case IRC_RAW_FLAG_RECV:
            strcpy (prefix_arrow, IRC_RAW_PREFIX_RECV);
            break;
        case IRC_RAW_FLAG_RECV | IRC_RAW_FLAG_MODIFIED:
            strcpy (prefix_arrow, IRC_RAW_PREFIX_RECV_MODIFIED);
            break;
        case IRC_RAW_FLAG_RECV | IRC_RAW_FLAG_REDIRECT:
            strcpy (prefix_arrow, IRC_RAW_PREFIX_RECV_REDIRECT);
            break;
        case IRC_RAW_FLAG_SEND:
            strcpy (prefix_arrow, IRC_RAW_PREFIX_SEND);
            break;
        case IRC_RAW_FLAG_SEND | IRC_RAW_FLAG_MODIFIED:
            strcpy (prefix_arrow, IRC_RAW_PREFIX_SEND_MODIFIED);
            break;
        default:
            if (flags & IRC_RAW_FLAG_RECV)
                strcpy (prefix_arrow, IRC_RAW_PREFIX_RECV);
            else
                strcpy (prefix_arrow, IRC_RAW_PREFIX_SEND);
            break;
    }

    snprintf (prefix, sizeof (prefix), "%s%s%s%s%s",
              (server) ? weechat_color ("chat_server") : "",
              (server) ? server->name : "",
              (server) ? " " : "",
              (flags & IRC_RAW_FLAG_SEND) ?
              weechat_color ("chat_prefix_quit") :
              weechat_color ("chat_prefix_join"),
              prefix_arrow);

    new_raw_message = irc_raw_message_add_to_list (time (NULL),
                                                   prefix,
                                                   (buf2) ? buf2 : ((buf) ? buf : message));

    if (buf)
        free (buf);
    if (buf2)
        free (buf2);

    return new_raw_message;
}
Beispiel #7
0
char *
irc_color_decode (const char *string, int keep_colors)
{
    unsigned char *out, *out2, *ptr_string;
    int out_length, length, out_pos, length_to_add;
    char str_fg[3], str_bg[3], str_color[128], str_key[128], str_to_add[128];
    const char *remapped_color;
    int fg, bg, bold, reverse, italic, underline, rc;

    if (!string)
        return NULL;

    /*
     * create output string with size of length*2 (with min 128 bytes),
     * this string will be realloc() later with a larger size if needed
     */
    out_length = (strlen (string) * 2) + 1;
    if (out_length < 128)
        out_length = 128;
    out = malloc (out_length);
    if (!out)
        return NULL;

    /* initialize attributes */
    bold = 0;
    reverse = 0;
    italic = 0;
    underline = 0;

    ptr_string = (unsigned char *)string;
    out[0] = '\0';
    out_pos = 0;
    while (ptr_string && ptr_string[0])
    {
        str_to_add[0] = '\0';
        switch (ptr_string[0])
        {
            case IRC_COLOR_BOLD_CHAR:
                if (keep_colors)
                {
                    snprintf (str_to_add, sizeof (str_to_add), "%s",
                              weechat_color ((bold) ? "-bold" : "bold"));
                }
                bold ^= 1;
                ptr_string++;
                break;
            case IRC_COLOR_RESET_CHAR:
                if (keep_colors)
                {
                    snprintf (str_to_add, sizeof (str_to_add), "%s",
                              weechat_color ("reset"));
                }
                bold = 0;
                reverse = 0;
                italic = 0;
                underline = 0;
                ptr_string++;
                break;
            case IRC_COLOR_FIXED_CHAR:
                ptr_string++;
                break;
            case IRC_COLOR_REVERSE_CHAR:
                if (keep_colors)
                {
                    snprintf (str_to_add, sizeof (str_to_add), "%s",
                              weechat_color ((reverse) ? "-reverse" : "reverse"));
                }
                reverse ^= 1;
                ptr_string++;
                break;
            case IRC_COLOR_ITALIC_CHAR:
                if (keep_colors)
                {
                    snprintf (str_to_add, sizeof (str_to_add), "%s",
                              weechat_color ((italic) ? "-italic" : "italic"));
                }
                italic ^= 1;
                ptr_string++;
                break;
            case IRC_COLOR_UNDERLINE_CHAR:
                if (keep_colors)
                {
                    snprintf (str_to_add, sizeof (str_to_add), "%s",
                              weechat_color ((underline) ? "-underline" : "underline"));
                }
                underline ^= 1;
                ptr_string++;
                break;
            case IRC_COLOR_COLOR_CHAR:
                ptr_string++;
                str_fg[0] = '\0';
                str_bg[0] = '\0';
                if (isdigit (ptr_string[0]))
                {
                    str_fg[0] = ptr_string[0];
                    str_fg[1] = '\0';
                    ptr_string++;
                    if (isdigit (ptr_string[0]))
                    {
                        str_fg[1] = ptr_string[0];
                        str_fg[2] = '\0';
                        ptr_string++;
                    }
                }
                if ((ptr_string[0] == ',') && (isdigit (ptr_string[1])))
                {
                    ptr_string++;
                    str_bg[0] = ptr_string[0];
                    str_bg[1] = '\0';
                    ptr_string++;
                    if (isdigit (ptr_string[0]))
                    {
                        str_bg[1] = ptr_string[0];
                        str_bg[2] = '\0';
                        ptr_string++;
                    }
                }
                if (keep_colors)
                {
                    if (str_fg[0] || str_bg[0])
                    {
                        fg = -1;
                        bg = -1;
                        if (str_fg[0])
                        {
                            rc = sscanf (str_fg, "%d", &fg);
                            if ((rc != EOF) && (rc >= 1))
                            {
                                fg %= IRC_NUM_COLORS;
                            }
                        }
                        if (str_bg[0])
                        {
                            rc = sscanf (str_bg, "%d", &bg);
                            if ((rc != EOF) && (rc >= 1))
                            {
                                bg %= IRC_NUM_COLORS;
                            }
                        }
                        /* search "fg,bg" in hashtable of remapped colors */
                        snprintf (str_key, sizeof (str_key), "%d,%d", fg, bg);
                        remapped_color = weechat_hashtable_get (
                            irc_config_hashtable_color_mirc_remap,
                            str_key);
                        if (remapped_color)
                        {
                            snprintf (str_color, sizeof (str_color),
                                      "|%s", remapped_color);
                        }
                        else
                        {
                            snprintf (str_color, sizeof (str_color),
                                      "|%s%s%s",
                                      (fg >= 0) ? irc_color_to_weechat[fg] : "",
                                      (bg >= 0) ? "," : "",
                                      (bg >= 0) ? irc_color_to_weechat[bg] : "");
                        }
                        snprintf (str_to_add, sizeof (str_to_add), "%s",
                                  weechat_color (str_color));
                    }
                    else
                    {
                        snprintf (str_to_add, sizeof (str_to_add), "%s",
                                  weechat_color ("resetcolor"));
                    }
                }
                break;
            default:
                /*
                 * we are not on an IRC color code, just copy the UTF-8 char
                 * into "str_to_add"
                 */
                length = weechat_utf8_char_size ((char *)ptr_string);
                if (length == 0)
                    length = 1;
                memcpy (str_to_add, ptr_string, length);
                str_to_add[length] = '\0';
                ptr_string += length;
                break;
        }
        /* add "str_to_add" (if not empty) to "out" */
        if (str_to_add[0])
        {
            /* if "out" is too small for adding "str_to_add", do a realloc() */
            length_to_add = strlen (str_to_add);
            if (out_pos + length_to_add + 1 > out_length)
            {
                /* try to double the size of "out" */
                out_length *= 2;
                out2 = realloc (out, out_length);
                if (!out2)
                    return (char *)out;
                out = out2;
            }
            /* add "str_to_add" to "out" */
            memcpy (out + out_pos, str_to_add, length_to_add + 1);
            out_pos += length_to_add;
        }
    }

    return (char *)out;
}
Beispiel #8
0
char *
irc_color_encode (const char *string, int keep_colors)
{
    unsigned char *out, *ptr_string;
    int out_length, out_pos, length;

    if (!string)
        return NULL;

    out_length = (strlen (string) * 2) + 1;
    out = malloc (out_length);
    if (!out)
        return NULL;

    ptr_string = (unsigned char *)string;
    out_pos = 0;
    while (ptr_string && ptr_string[0] && (out_pos < out_length - 1))
    {
        switch (ptr_string[0])
        {
            case 0x02: /* ^B */
                if (keep_colors)
                    out[out_pos++] = IRC_COLOR_BOLD_CHAR;
                ptr_string++;
                break;
            case 0x03: /* ^C */
                if (keep_colors)
                    out[out_pos++] = IRC_COLOR_COLOR_CHAR;
                ptr_string++;
                if (isdigit (ptr_string[0]))
                {
                    if (keep_colors)
                        out[out_pos++] = ptr_string[0];
                    ptr_string++;
                    if (isdigit (ptr_string[0]))
                    {
                        if (keep_colors)
                            out[out_pos++] = ptr_string[0];
                        ptr_string++;
                    }
                }
                if (ptr_string[0] == ',')
                {
                    if (keep_colors)
                        out[out_pos++] = ',';
                    ptr_string++;
                    if (isdigit (ptr_string[0]))
                    {
                        if (keep_colors)
                            out[out_pos++] = ptr_string[0];
                        ptr_string++;
                        if (isdigit (ptr_string[0]))
                        {
                            if (keep_colors)
                                out[out_pos++] = ptr_string[0];
                            ptr_string++;
                        }
                    }
                }
                break;
            case 0x0F: /* ^O */
                if (keep_colors)
                    out[out_pos++] = IRC_COLOR_RESET_CHAR;
                ptr_string++;
                break;
            case 0x16: /* ^V */
                if (keep_colors)
                    out[out_pos++] = IRC_COLOR_REVERSE_CHAR;
                ptr_string++;
                break;
            case 0x1F: /* ^_ */
                if (keep_colors)
                    out[out_pos++] = IRC_COLOR_UNDERLINE_CHAR;
                ptr_string++;
                break;
            default:
                length = weechat_utf8_char_size ((char *)ptr_string);
                if (length == 0)
                    length = 1;
                memcpy (out + out_pos, ptr_string, length);
                out_pos += length;
                ptr_string += length;
        }
    }

    out[out_pos] = '\0';

    return (char *)out;
}
Beispiel #9
0
void
relay_raw_message_add (struct t_relay_client *client, int flags,
                       const char *data, int data_size)
{
    char *buf, *buf2, prefix[256], prefix_arrow[16];
    const unsigned char *ptr_buf;
    const char *hexa = "0123456789ABCDEF";
    int pos_buf, pos_buf2, char_size, i;
    struct t_relay_raw_message *new_raw_message;

    buf = NULL;
    buf2 = NULL;

    if (flags & RELAY_RAW_FLAG_BINARY)
    {
        /* binary message */
        buf = weechat_string_hex_dump (data, data_size, 16, "  > ", NULL);
        snprintf (prefix, sizeof (prefix), " ");
    }
    else
    {
        /* text message */
        buf = weechat_iconv_to_internal (NULL, data);
        buf2 = weechat_string_replace (buf, "\r", "");
        if (buf2)
        {
            free (buf);
            buf = buf2;
            buf2 = NULL;
        }
        buf2 = malloc ((strlen (buf) * 4) + 1);
        if (buf2)
        {
            ptr_buf = (buf) ? (unsigned char *)buf : (unsigned char *)data;
            pos_buf = 0;
            pos_buf2 = 0;
            while (ptr_buf[pos_buf])
            {
                if ((ptr_buf[pos_buf] < 32) && (ptr_buf[pos_buf] != '\n'))
                {
                    buf2[pos_buf2++] = '\\';
                    buf2[pos_buf2++] = 'x';
                    buf2[pos_buf2++] = hexa[ptr_buf[pos_buf] / 16];
                    buf2[pos_buf2++] = hexa[ptr_buf[pos_buf] % 16];
                    pos_buf++;
                }
                else
                {
                    char_size = weechat_utf8_char_size ((const char *)(ptr_buf + pos_buf));
                    for (i = 0; i < char_size; i++)
                    {
                        buf2[pos_buf2++] = ptr_buf[pos_buf++];
                    }
                }
            }
            buf2[pos_buf2] = '\0';
        }

        /* build prefix with arrow */
        prefix_arrow[0] = '\0';
        switch (flags & (RELAY_RAW_FLAG_RECV | RELAY_RAW_FLAG_SEND))
        {
            case RELAY_RAW_FLAG_RECV:
                strcpy (prefix_arrow, RELAY_RAW_PREFIX_RECV);
                break;
            case RELAY_RAW_FLAG_SEND:
                strcpy (prefix_arrow, RELAY_RAW_PREFIX_SEND);
                break;
            default:
                if (flags & RELAY_RAW_FLAG_RECV)
                    strcpy (prefix_arrow, RELAY_RAW_PREFIX_RECV);
                else
                    strcpy (prefix_arrow, RELAY_RAW_PREFIX_SEND);
                break;
        }

        if (client)
        {
            snprintf (prefix, sizeof (prefix), "%s%s %s[%s%d%s] %s%s%s%s",
                      (flags & RELAY_RAW_FLAG_SEND) ?
                      weechat_color ("chat_prefix_quit") :
                      weechat_color ("chat_prefix_join"),
                      prefix_arrow,
                      weechat_color ("chat_delimiters"),
                      weechat_color ("chat"),
                      client->id,
                      weechat_color ("chat_delimiters"),
                      weechat_color ("chat_server"),
                      relay_protocol_string[client->protocol],
                      (client->protocol_args) ? "." : "",
                      (client->protocol_args) ? client->protocol_args : "");
        }
        else
        {
            snprintf (prefix, sizeof (prefix), "%s%s",
                      (flags & RELAY_RAW_FLAG_SEND) ?
                      weechat_color ("chat_prefix_quit") :
                      weechat_color ("chat_prefix_join"),
                      prefix_arrow);
        }
    }

    new_raw_message = relay_raw_message_add_to_list (
        time (NULL),
        prefix,
        (buf2) ? buf2 : ((buf) ? buf : data));

    if (new_raw_message)
    {
        if (relay_raw_buffer)
            relay_raw_message_print (new_raw_message);
        if (weechat_config_integer (relay_config_look_raw_messages) == 0)
            relay_raw_message_free (new_raw_message);
    }

    if (buf)
        free (buf);
    if (buf2)
        free (buf2);
}