Example #1
0
void
relay_config_change_network_allowed_ips (void *data,
                                         struct t_config_option *option)
{
    const char *allowed_ips;

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

    if (relay_config_regex_allowed_ips)
    {
        regfree (relay_config_regex_allowed_ips);
        free (relay_config_regex_allowed_ips);
        relay_config_regex_allowed_ips = NULL;
    }

    allowed_ips = weechat_config_string (relay_config_network_allowed_ips);
    if (allowed_ips && allowed_ips[0])
    {
        relay_config_regex_allowed_ips = malloc (sizeof (*relay_config_regex_allowed_ips));
        if (relay_config_regex_allowed_ips)
        {
            if (weechat_string_regcomp (relay_config_regex_allowed_ips,
                                        allowed_ips,
                                        REG_EXTENDED | REG_ICASE) != 0)
            {
                free (relay_config_regex_allowed_ips);
                relay_config_regex_allowed_ips = NULL;
            }
        }
    }
}
Example #2
0
char *
irc_color_decode_ansi (const char *string, int keep_colors)
{
    struct t_irc_color_ansi_state ansi_state;

    /* allocate/compile regex if needed (first call) */
    if (!irc_color_regex_ansi)
    {
        irc_color_regex_ansi = malloc (sizeof (*irc_color_regex_ansi));
        if (!irc_color_regex_ansi)
            return NULL;
        if (weechat_string_regcomp (irc_color_regex_ansi,
                                    weechat_info_get ("color_ansi_regex", NULL),
                                    REG_EXTENDED) != 0)
        {
            free (irc_color_regex_ansi);
            irc_color_regex_ansi = NULL;
            return NULL;
        }
    }

    ansi_state.keep_colors = keep_colors;
    ansi_state.bold = 0;
    ansi_state.underline = 0;
    ansi_state.italic = 0;

    return weechat_string_replace_regex (string, irc_color_regex_ansi,
                                         "$0", '$',
                                         &irc_color_decode_ansi_cb,
                                         &ansi_state);
}
Example #3
0
struct t_irc_ignore *
irc_ignore_new (const char *mask, const char *server, const char *channel)
{
    struct t_irc_ignore *new_ignore;
    regex_t *regex;

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

    regex = malloc (sizeof (*regex));
    if (!regex)
        return NULL;

    if (weechat_string_regcomp (regex, mask,
                                REG_EXTENDED | REG_ICASE | REG_NOSUB) != 0)
    {
        free (regex);
        return NULL;
    }

    new_ignore = malloc (sizeof (*new_ignore));
    if (new_ignore)
    {
        new_ignore->number = (last_irc_ignore) ? last_irc_ignore->number + 1 : 1;
        new_ignore->mask = strdup (mask);
        new_ignore->regex_mask = regex;
        new_ignore->server = (server) ? strdup (server) : strdup ("*");
        new_ignore->channel = (channel) ? strdup (channel) : strdup ("*");

        /* add ignore to ignore list */
        new_ignore->prev_ignore = last_irc_ignore;
        if (irc_ignore_list)
            last_irc_ignore->next_ignore = new_ignore;
        else
            irc_ignore_list = new_ignore;
        last_irc_ignore = new_ignore;
        new_ignore->next_ignore = NULL;
    }

    return new_ignore;
}
Example #4
0
int
trigger_regex_split (const char *str_regex,
                     int *regex_count, struct t_trigger_regex **regex)
{
    const char *ptr_regex, *pos, *pos_replace, *pos_replace_end;
    const char *pos_next_regex;
    char *delimiter, *str_regex_escaped;
    int rc, index, length_delimiter;
    struct t_trigger_regex *new_regex;

    rc = 0;
    delimiter = NULL;
    str_regex_escaped = NULL;

    if (!regex_count || !regex)
        goto end;

    /* remove any existing regex */
    trigger_regex_free (regex_count, regex);

    if (!str_regex || !str_regex[0])
        goto end;

    /* min 3 chars, for example: "/a/" */
    if (strlen (str_regex) < 3)
        goto format_error;

    /* parse regular expressions in the option */
    ptr_regex = str_regex;
    while (ptr_regex && ptr_regex[0])
    {
        if (delimiter)
        {
            free (delimiter);
            delimiter = NULL;
        }

        /* search the delimiter (which can be more than one char) */
        pos = weechat_utf8_next_char (ptr_regex);
        while (pos[0] && (weechat_utf8_charcmp (ptr_regex, pos) == 0))
        {
            pos = weechat_utf8_next_char (pos);
        }
        if (!pos[0])
            goto format_error;
        delimiter = weechat_strndup (ptr_regex, pos - ptr_regex);
        if (!delimiter)
            goto memory_error;

        length_delimiter = strlen (delimiter);

        ptr_regex = pos;
        if (!ptr_regex[0])
            goto format_error;

        /* search the start of replacement string */
        pos_replace = strstr (ptr_regex, delimiter);
        if (!pos_replace)
            goto format_error;

        /* search the end of replacement string */
        pos_replace_end = strstr (pos_replace + length_delimiter, delimiter);

        new_regex = realloc (*regex,
                             (*regex_count + 1) * sizeof ((*regex)[0]));
        if (!new_regex)
            goto memory_error;

        *regex = new_regex;
        (*regex_count)++;
        index = *regex_count - 1;

        /* initialize new regex */
        (*regex)[index].variable = NULL;
        (*regex)[index].str_regex = NULL;
        (*regex)[index].regex = NULL;
        (*regex)[index].replace = NULL;
        (*regex)[index].replace_escaped = NULL;

        /* set string with regex */
        (*regex)[index].str_regex = weechat_strndup (ptr_regex,
                                                     pos_replace - ptr_regex);
        if (!(*regex)[index].str_regex)
            goto memory_error;
        str_regex_escaped = weechat_string_convert_escaped_chars ((*regex)[index].str_regex);
        if (!str_regex_escaped)
            goto memory_error;

        /* set regex */
        (*regex)[index].regex = malloc (sizeof (*(*regex)[index].regex));
        if (!(*regex)[index].regex)
            goto memory_error;
        if (weechat_string_regcomp ((*regex)[index].regex,
                                    str_regex_escaped,
                                    REG_EXTENDED | REG_ICASE) != 0)
        {
            free ((*regex)[index].regex);
            (*regex)[index].regex = NULL;
            goto compile_error;
        }

        /* set replace and replace_eval */
        (*regex)[index].replace = (pos_replace_end) ?
            weechat_strndup (pos_replace + length_delimiter,
                             pos_replace_end - pos_replace - length_delimiter) :
            strdup (pos_replace + length_delimiter);
        if (!(*regex)[index].replace)
            goto memory_error;
        (*regex)[index].replace_escaped =
            weechat_string_convert_escaped_chars ((*regex)[index].replace);
        if (!(*regex)[index].replace_escaped)
            goto memory_error;

        if (!pos_replace_end)
            break;

        /* set variable (optional) */
        ptr_regex = pos_replace_end + length_delimiter;
        if (!ptr_regex[0])
            break;
        if (ptr_regex[0] == ' ')
            pos_next_regex = ptr_regex;
        else
        {
            pos_next_regex = strchr (ptr_regex, ' ');
            (*regex)[index].variable = (pos_next_regex) ?
                weechat_strndup (ptr_regex, pos_next_regex - ptr_regex) :
                strdup (ptr_regex);
            if (!(*regex)[index].variable)
                goto memory_error;
        }
        if (!pos_next_regex)
            break;

        /* skip spaces before next regex */
        ptr_regex = pos_next_regex + 1;
        while (ptr_regex[0] == ' ')
        {
            ptr_regex++;
        }
    }

    goto end;

format_error:
    rc = -1;
    goto end;

compile_error:
    rc = -2;
    goto end;

memory_error:
    rc = -3;
    goto end;

end:
    if (delimiter)
        free (delimiter);
    if (str_regex_escaped)
        free (str_regex_escaped);
    if (rc < 0)
        trigger_regex_free (regex_count, regex);

    return rc;
}
Example #5
0
struct t_rmodifier *
rmodifier_new (const char *name, const char *modifiers, const char *str_regex,
               const char *groups)
{
    struct t_rmodifier *new_rmodifier, *ptr_rmodifier;
    regex_t *regex;

    if (!name || !name[0] || !modifiers || !modifiers[0]
        || !str_regex || !str_regex[0])
    {
        return NULL;
    }

    regex = malloc (sizeof (*regex));
    if (!regex)
        return NULL;

    if (weechat_string_regcomp (regex, str_regex,
                                REG_EXTENDED | REG_ICASE) != 0)
    {
        weechat_printf (NULL,
                        _("%s%s: error compiling regular expression \"%s\""),
                        weechat_prefix ("error"), RMODIFIER_PLUGIN_NAME,
                        str_regex);
        free (regex);
        return NULL;
    }

    ptr_rmodifier = rmodifier_search (name);
    if (ptr_rmodifier)
        rmodifier_free (ptr_rmodifier);

    new_rmodifier = malloc (sizeof (*new_rmodifier));
    if (new_rmodifier)
    {
        new_rmodifier->name = strdup (name);
        new_rmodifier->hooks = NULL;
        new_rmodifier->modifiers = strdup (modifiers);
        new_rmodifier->str_regex = strdup (str_regex);
        new_rmodifier->regex = regex;
        new_rmodifier->groups = strdup ((groups) ? groups : "");

        /* create modifiers */
        rmodifier_hook_modifiers (new_rmodifier);

        if (rmodifier_list)
        {
            /* add rmodifier to end of list */
            new_rmodifier->prev_rmodifier = last_rmodifier;
            new_rmodifier->next_rmodifier = NULL;
            last_rmodifier->next_rmodifier = new_rmodifier;
            last_rmodifier = new_rmodifier;
        }
        else
        {
            new_rmodifier->prev_rmodifier = NULL;
            new_rmodifier->next_rmodifier = NULL;
            rmodifier_list = new_rmodifier;
            last_rmodifier = new_rmodifier;
        }

        rmodifier_count++;
    }

    return new_rmodifier;
}