src/modules/logwatch_filter.c
author Tero Marttila <terom@fixme.fi>
Fri, 24 Apr 2009 23:01:34 +0300
changeset 153 d35e7cb3a489
parent 138 a716c621cb90
permissions -rw-r--r--
implement irc_net reconnect, requires testing
121
4682ebbc5644 implement logwatch_conf_filter such that it compiles and loads, but not yet tested
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     1
#include "logwatch.h"
128
6c5a5fdfd1d5 update logwatch_filter_apply
Tero Marttila <terom@fixme.fi>
parents: 127
diff changeset
     2
#include "../str.h"
130
ffefb6d85ea6 implement logwatch_filter::format using str_format
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
     3
#include "../log.h"
121
4682ebbc5644 implement logwatch_conf_filter such that it compiles and loads, but not yet tested
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     4
4682ebbc5644 implement logwatch_conf_filter such that it compiles and loads, but not yet tested
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     5
#include <stdlib.h>
4682ebbc5644 implement logwatch_conf_filter such that it compiles and loads, but not yet tested
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     6
#include <string.h>
4682ebbc5644 implement logwatch_conf_filter such that it compiles and loads, but not yet tested
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     7
130
ffefb6d85ea6 implement logwatch_filter::format using str_format
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
     8
#include <assert.h>
ffefb6d85ea6 implement logwatch_filter::format using str_format
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
     9
135
9159bd51525f have logwatch_conf_filter replace any old filter with the same name, and move irc_log.sql to modules/
Tero Marttila <terom@fixme.fi>
parents: 133
diff changeset
    10
struct logwatch_filter* logwatch_filter_lookup (struct logwatch *ctx, const char *name)
9159bd51525f have logwatch_conf_filter replace any old filter with the same name, and move irc_log.sql to modules/
Tero Marttila <terom@fixme.fi>
parents: 133
diff changeset
    11
{
9159bd51525f have logwatch_conf_filter replace any old filter with the same name, and move irc_log.sql to modules/
Tero Marttila <terom@fixme.fi>
parents: 133
diff changeset
    12
    struct logwatch_filter *filter;
9159bd51525f have logwatch_conf_filter replace any old filter with the same name, and move irc_log.sql to modules/
Tero Marttila <terom@fixme.fi>
parents: 133
diff changeset
    13
    
9159bd51525f have logwatch_conf_filter replace any old filter with the same name, and move irc_log.sql to modules/
Tero Marttila <terom@fixme.fi>
parents: 133
diff changeset
    14
    // inspect each filter
9159bd51525f have logwatch_conf_filter replace any old filter with the same name, and move irc_log.sql to modules/
Tero Marttila <terom@fixme.fi>
parents: 133
diff changeset
    15
    TAILQ_FOREACH(filter, &ctx->filters, logwatch_filters)
9159bd51525f have logwatch_conf_filter replace any old filter with the same name, and move irc_log.sql to modules/
Tero Marttila <terom@fixme.fi>
parents: 133
diff changeset
    16
        if (strcmp(filter->name, name) == 0)
9159bd51525f have logwatch_conf_filter replace any old filter with the same name, and move irc_log.sql to modules/
Tero Marttila <terom@fixme.fi>
parents: 133
diff changeset
    17
            return filter;
9159bd51525f have logwatch_conf_filter replace any old filter with the same name, and move irc_log.sql to modules/
Tero Marttila <terom@fixme.fi>
parents: 133
diff changeset
    18
    
9159bd51525f have logwatch_conf_filter replace any old filter with the same name, and move irc_log.sql to modules/
Tero Marttila <terom@fixme.fi>
parents: 133
diff changeset
    19
    // not found
9159bd51525f have logwatch_conf_filter replace any old filter with the same name, and move irc_log.sql to modules/
Tero Marttila <terom@fixme.fi>
parents: 133
diff changeset
    20
    return NULL;
9159bd51525f have logwatch_conf_filter replace any old filter with the same name, and move irc_log.sql to modules/
Tero Marttila <terom@fixme.fi>
parents: 133
diff changeset
    21
}
9159bd51525f have logwatch_conf_filter replace any old filter with the same name, and move irc_log.sql to modules/
Tero Marttila <terom@fixme.fi>
parents: 133
diff changeset
    22
121
4682ebbc5644 implement logwatch_conf_filter such that it compiles and loads, but not yet tested
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    23
struct logwatch_filter* logwatch_filter (struct logwatch *ctx, const char *name, const struct logwatch_source *source,
132
f2ece471fb07 implement logwatch_source names, and logwatch_chan
Tero Marttila <terom@fixme.fi>
parents: 130
diff changeset
    24
        const char *pattern, const char *format, struct logwatch_chan *chan, struct error_info *err)
121
4682ebbc5644 implement logwatch_conf_filter such that it compiles and loads, but not yet tested
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    25
{
4682ebbc5644 implement logwatch_conf_filter such that it compiles and loads, but not yet tested
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    26
    struct logwatch_filter *filter;
4682ebbc5644 implement logwatch_conf_filter such that it compiles and loads, but not yet tested
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    27
    const char *err_msg;
4682ebbc5644 implement logwatch_conf_filter such that it compiles and loads, but not yet tested
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    28
    int err_offset;
4682ebbc5644 implement logwatch_conf_filter such that it compiles and loads, but not yet tested
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    29
4682ebbc5644 implement logwatch_conf_filter such that it compiles and loads, but not yet tested
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    30
    // alloc
4682ebbc5644 implement logwatch_conf_filter such that it compiles and loads, but not yet tested
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    31
    if ((filter = calloc(1, sizeof(*filter))) == NULL)
4682ebbc5644 implement logwatch_conf_filter such that it compiles and loads, but not yet tested
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    32
        JUMP_SET_ERROR(err, ERR_CALLOC);
4682ebbc5644 implement logwatch_conf_filter such that it compiles and loads, but not yet tested
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    33
4682ebbc5644 implement logwatch_conf_filter such that it compiles and loads, but not yet tested
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    34
    // optional source
4682ebbc5644 implement logwatch_conf_filter such that it compiles and loads, but not yet tested
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    35
    filter->source = source;
4682ebbc5644 implement logwatch_conf_filter such that it compiles and loads, but not yet tested
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    36
4682ebbc5644 implement logwatch_conf_filter such that it compiles and loads, but not yet tested
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    37
    // name?
4682ebbc5644 implement logwatch_conf_filter such that it compiles and loads, but not yet tested
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    38
    if (name && (filter->name = strdup(name)) == NULL)
4682ebbc5644 implement logwatch_conf_filter such that it compiles and loads, but not yet tested
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    39
        JUMP_SET_ERROR(err, ERR_STRDUP);
4682ebbc5644 implement logwatch_conf_filter such that it compiles and loads, but not yet tested
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    40
    
4682ebbc5644 implement logwatch_conf_filter such that it compiles and loads, but not yet tested
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    41
    // compile the regexp
4682ebbc5644 implement logwatch_conf_filter such that it compiles and loads, but not yet tested
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    42
    // XXX: any flags?
124
f18d69425c4f fix up lua_module_conf/config_* enough so that logwatch_conf_filter works
Tero Marttila <terom@fixme.fi>
parents: 121
diff changeset
    43
    if (pattern && (filter->regexp = pcre_compile(pattern, 0, &err_msg, &err_offset, NULL)) == NULL)
121
4682ebbc5644 implement logwatch_conf_filter such that it compiles and loads, but not yet tested
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    44
        JUMP_SET_ERROR_STR(err, ERR_PCRE_COMPILE, err_msg);
4682ebbc5644 implement logwatch_conf_filter such that it compiles and loads, but not yet tested
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    45
4682ebbc5644 implement logwatch_conf_filter such that it compiles and loads, but not yet tested
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    46
    // format?
4682ebbc5644 implement logwatch_conf_filter such that it compiles and loads, but not yet tested
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    47
    if (format && (filter->format = strdup(format)) == NULL)
4682ebbc5644 implement logwatch_conf_filter such that it compiles and loads, but not yet tested
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    48
        JUMP_SET_ERROR(err, ERR_STRDUP);
4682ebbc5644 implement logwatch_conf_filter such that it compiles and loads, but not yet tested
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    49
   
4682ebbc5644 implement logwatch_conf_filter such that it compiles and loads, but not yet tested
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    50
    // output channel
4682ebbc5644 implement logwatch_conf_filter such that it compiles and loads, but not yet tested
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    51
    filter->chan = chan;
4682ebbc5644 implement logwatch_conf_filter such that it compiles and loads, but not yet tested
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    52
 
4682ebbc5644 implement logwatch_conf_filter such that it compiles and loads, but not yet tested
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    53
    // add it
4682ebbc5644 implement logwatch_conf_filter such that it compiles and loads, but not yet tested
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    54
    TAILQ_INSERT_TAIL(&ctx->filters, filter, logwatch_filters);
132
f2ece471fb07 implement logwatch_source names, and logwatch_chan
Tero Marttila <terom@fixme.fi>
parents: 130
diff changeset
    55
f2ece471fb07 implement logwatch_source names, and logwatch_chan
Tero Marttila <terom@fixme.fi>
parents: 130
diff changeset
    56
    // mark it as registered
121
4682ebbc5644 implement logwatch_conf_filter such that it compiles and loads, but not yet tested
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    57
    filter->ctx = ctx;
4682ebbc5644 implement logwatch_conf_filter such that it compiles and loads, but not yet tested
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    58
4682ebbc5644 implement logwatch_conf_filter such that it compiles and loads, but not yet tested
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    59
    // ok
4682ebbc5644 implement logwatch_conf_filter such that it compiles and loads, but not yet tested
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    60
    return filter;
4682ebbc5644 implement logwatch_conf_filter such that it compiles and loads, but not yet tested
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    61
4682ebbc5644 implement logwatch_conf_filter such that it compiles and loads, but not yet tested
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    62
error:
4682ebbc5644 implement logwatch_conf_filter such that it compiles and loads, but not yet tested
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    63
    // cleanup
4682ebbc5644 implement logwatch_conf_filter such that it compiles and loads, but not yet tested
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    64
    if (filter)
4682ebbc5644 implement logwatch_conf_filter such that it compiles and loads, but not yet tested
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    65
        logwatch_filter_destroy(filter);
4682ebbc5644 implement logwatch_conf_filter such that it compiles and loads, but not yet tested
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    66
    
4682ebbc5644 implement logwatch_conf_filter such that it compiles and loads, but not yet tested
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    67
    return NULL;
4682ebbc5644 implement logwatch_conf_filter such that it compiles and loads, but not yet tested
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    68
}
4682ebbc5644 implement logwatch_conf_filter such that it compiles and loads, but not yet tested
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    69
4682ebbc5644 implement logwatch_conf_filter such that it compiles and loads, but not yet tested
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    70
void logwatch_filter_destroy (struct logwatch_filter *filter)
4682ebbc5644 implement logwatch_conf_filter such that it compiles and loads, but not yet tested
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    71
{
4682ebbc5644 implement logwatch_conf_filter such that it compiles and loads, but not yet tested
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    72
    if (filter->ctx)
138
a716c621cb90 implement blackhole filters for logwatch, and stop applying filters after the first hit
Tero Marttila <terom@fixme.fi>
parents: 135
diff changeset
    73
        // unreigster
121
4682ebbc5644 implement logwatch_conf_filter such that it compiles and loads, but not yet tested
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    74
        TAILQ_REMOVE(&filter->ctx->filters, filter, logwatch_filters);
132
f2ece471fb07 implement logwatch_source names, and logwatch_chan
Tero Marttila <terom@fixme.fi>
parents: 130
diff changeset
    75
    
f2ece471fb07 implement logwatch_source names, and logwatch_chan
Tero Marttila <terom@fixme.fi>
parents: 130
diff changeset
    76
    // this if is probably not needed
f2ece471fb07 implement logwatch_source names, and logwatch_chan
Tero Marttila <terom@fixme.fi>
parents: 130
diff changeset
    77
    if (filter->regexp) 
f2ece471fb07 implement logwatch_source names, and logwatch_chan
Tero Marttila <terom@fixme.fi>
parents: 130
diff changeset
    78
        pcre_free(filter->regexp);
f2ece471fb07 implement logwatch_source names, and logwatch_chan
Tero Marttila <terom@fixme.fi>
parents: 130
diff changeset
    79
    
138
a716c621cb90 implement blackhole filters for logwatch, and stop applying filters after the first hit
Tero Marttila <terom@fixme.fi>
parents: 135
diff changeset
    80
    if (filter->chan)    
a716c621cb90 implement blackhole filters for logwatch, and stop applying filters after the first hit
Tero Marttila <terom@fixme.fi>
parents: 135
diff changeset
    81
        // release the logwatch_chan ref
a716c621cb90 implement blackhole filters for logwatch, and stop applying filters after the first hit
Tero Marttila <terom@fixme.fi>
parents: 135
diff changeset
    82
        logwatch_chan_put(filter->chan);
121
4682ebbc5644 implement logwatch_conf_filter such that it compiles and loads, but not yet tested
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    83
132
f2ece471fb07 implement logwatch_source names, and logwatch_chan
Tero Marttila <terom@fixme.fi>
parents: 130
diff changeset
    84
    // free misc
121
4682ebbc5644 implement logwatch_conf_filter such that it compiles and loads, but not yet tested
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    85
    free(filter->format);
4682ebbc5644 implement logwatch_conf_filter such that it compiles and loads, but not yet tested
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    86
    free(filter->name);
4682ebbc5644 implement logwatch_conf_filter such that it compiles and loads, but not yet tested
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    87
    free(filter);
4682ebbc5644 implement logwatch_conf_filter such that it compiles and loads, but not yet tested
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    88
}
127
94e6c3b4230f implement logwatch_on_line/logwatch_filter_apply, along with irc_*_NOTICE
Tero Marttila <terom@fixme.fi>
parents: 124
diff changeset
    89
132
f2ece471fb07 implement logwatch_source names, and logwatch_chan
Tero Marttila <terom@fixme.fi>
parents: 130
diff changeset
    90
void logwatch_filter_remove (struct logwatch *ctx, const struct logwatch_source *source)
f2ece471fb07 implement logwatch_source names, and logwatch_chan
Tero Marttila <terom@fixme.fi>
parents: 130
diff changeset
    91
{
f2ece471fb07 implement logwatch_source names, and logwatch_chan
Tero Marttila <terom@fixme.fi>
parents: 130
diff changeset
    92
    struct logwatch_filter *filter, *next;
f2ece471fb07 implement logwatch_source names, and logwatch_chan
Tero Marttila <terom@fixme.fi>
parents: 130
diff changeset
    93
    
f2ece471fb07 implement logwatch_source names, and logwatch_chan
Tero Marttila <terom@fixme.fi>
parents: 130
diff changeset
    94
    // inspect each filter
f2ece471fb07 implement logwatch_source names, and logwatch_chan
Tero Marttila <terom@fixme.fi>
parents: 130
diff changeset
    95
    for (filter = TAILQ_FIRST(&ctx->filters); filter; filter = next) {
f2ece471fb07 implement logwatch_source names, and logwatch_chan
Tero Marttila <terom@fixme.fi>
parents: 130
diff changeset
    96
        next = TAILQ_NEXT(filter, logwatch_filters);
f2ece471fb07 implement logwatch_source names, and logwatch_chan
Tero Marttila <terom@fixme.fi>
parents: 130
diff changeset
    97
    
f2ece471fb07 implement logwatch_source names, and logwatch_chan
Tero Marttila <terom@fixme.fi>
parents: 130
diff changeset
    98
        // destroy matching
f2ece471fb07 implement logwatch_source names, and logwatch_chan
Tero Marttila <terom@fixme.fi>
parents: 130
diff changeset
    99
        if (filter->source == source)
f2ece471fb07 implement logwatch_source names, and logwatch_chan
Tero Marttila <terom@fixme.fi>
parents: 130
diff changeset
   100
            logwatch_filter_destroy(filter);
f2ece471fb07 implement logwatch_source names, and logwatch_chan
Tero Marttila <terom@fixme.fi>
parents: 130
diff changeset
   101
    }
f2ece471fb07 implement logwatch_source names, and logwatch_chan
Tero Marttila <terom@fixme.fi>
parents: 130
diff changeset
   102
}
f2ece471fb07 implement logwatch_source names, and logwatch_chan
Tero Marttila <terom@fixme.fi>
parents: 130
diff changeset
   103
130
ffefb6d85ea6 implement logwatch_filter::format using str_format
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   104
/**
ffefb6d85ea6 implement logwatch_filter::format using str_format
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   105
 * str_format context for logwatch_filter_apply operation
ffefb6d85ea6 implement logwatch_filter::format using str_format
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   106
 */
ffefb6d85ea6 implement logwatch_filter::format using str_format
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   107
struct logwatch_filter_str_format_ctx {
ffefb6d85ea6 implement logwatch_filter::format using str_format
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   108
    /** The regexp we are processing */
ffefb6d85ea6 implement logwatch_filter::format using str_format
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   109
    pcre *regexp;
ffefb6d85ea6 implement logwatch_filter::format using str_format
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   110
ffefb6d85ea6 implement logwatch_filter::format using str_format
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   111
    /** The subject string */
ffefb6d85ea6 implement logwatch_filter::format using str_format
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   112
    const char *subject;
ffefb6d85ea6 implement logwatch_filter::format using str_format
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   113
ffefb6d85ea6 implement logwatch_filter::format using str_format
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   114
    /** The match group vector */
ffefb6d85ea6 implement logwatch_filter::format using str_format
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   115
    int *groups;
ffefb6d85ea6 implement logwatch_filter::format using str_format
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   116
    
ffefb6d85ea6 implement logwatch_filter::format using str_format
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   117
    /** Number of match items in groups */
ffefb6d85ea6 implement logwatch_filter::format using str_format
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   118
    size_t group_count;
ffefb6d85ea6 implement logwatch_filter::format using str_format
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   119
};
ffefb6d85ea6 implement logwatch_filter::format using str_format
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   120
132
f2ece471fb07 implement logwatch_source names, and logwatch_chan
Tero Marttila <terom@fixme.fi>
parents: 130
diff changeset
   121
/**
f2ece471fb07 implement logwatch_source names, and logwatch_chan
Tero Marttila <terom@fixme.fi>
parents: 130
diff changeset
   122
 * Our callback for str_format params used by logwatch_filter_apply()
f2ece471fb07 implement logwatch_source names, and logwatch_chan
Tero Marttila <terom@fixme.fi>
parents: 130
diff changeset
   123
 */
130
ffefb6d85ea6 implement logwatch_filter::format using str_format
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   124
static err_t logwatch_filter_str_format_cb (const char *name, const char **value, ssize_t *value_len, void *arg)
ffefb6d85ea6 implement logwatch_filter::format using str_format
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   125
{
ffefb6d85ea6 implement logwatch_filter::format using str_format
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   126
    struct logwatch_filter_str_format_ctx *ctx = arg;
ffefb6d85ea6 implement logwatch_filter::format using str_format
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   127
    int num;
ffefb6d85ea6 implement logwatch_filter::format using str_format
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   128
ffefb6d85ea6 implement logwatch_filter::format using str_format
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   129
    // look it up
ffefb6d85ea6 implement logwatch_filter::format using str_format
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   130
    if (!ctx->regexp || (num = pcre_get_stringnumber(ctx->regexp, name)) < 0)
ffefb6d85ea6 implement logwatch_filter::format using str_format
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   131
        // no such capture group
ffefb6d85ea6 implement logwatch_filter::format using str_format
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   132
        return ERR_STR_FMT_NAME;
ffefb6d85ea6 implement logwatch_filter::format using str_format
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   133
ffefb6d85ea6 implement logwatch_filter::format using str_format
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   134
    // sanity check
ffefb6d85ea6 implement logwatch_filter::format using str_format
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   135
    assert((size_t) num < ctx->group_count);
ffefb6d85ea6 implement logwatch_filter::format using str_format
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   136
ffefb6d85ea6 implement logwatch_filter::format using str_format
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   137
    // look up the start/end offsets
ffefb6d85ea6 implement logwatch_filter::format using str_format
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   138
    int start = ctx->groups[num * 2 + 0];
ffefb6d85ea6 implement logwatch_filter::format using str_format
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   139
    int end = ctx->groups[num * 2 + 1];
ffefb6d85ea6 implement logwatch_filter::format using str_format
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   140
ffefb6d85ea6 implement logwatch_filter::format using str_format
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   141
    // return a pointer to the subject
ffefb6d85ea6 implement logwatch_filter::format using str_format
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   142
    *value = ctx->subject + start;
ffefb6d85ea6 implement logwatch_filter::format using str_format
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   143
    *value_len = end - start;
ffefb6d85ea6 implement logwatch_filter::format using str_format
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   144
ffefb6d85ea6 implement logwatch_filter::format using str_format
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   145
    // ok
ffefb6d85ea6 implement logwatch_filter::format using str_format
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   146
    return SUCCESS;
ffefb6d85ea6 implement logwatch_filter::format using str_format
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   147
}
ffefb6d85ea6 implement logwatch_filter::format using str_format
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   148
127
94e6c3b4230f implement logwatch_on_line/logwatch_filter_apply, along with irc_*_NOTICE
Tero Marttila <terom@fixme.fi>
parents: 124
diff changeset
   149
// number of pcre ovec elemnents required
94e6c3b4230f implement logwatch_on_line/logwatch_filter_apply, along with irc_*_NOTICE
Tero Marttila <terom@fixme.fi>
parents: 124
diff changeset
   150
#define LOGWATCH_FILTER_OVEC_ITEMS ((LOGWATCH_FILTER_GROUPS_MAX + 1) * 3)
94e6c3b4230f implement logwatch_on_line/logwatch_filter_apply, along with irc_*_NOTICE
Tero Marttila <terom@fixme.fi>
parents: 124
diff changeset
   151
128
6c5a5fdfd1d5 update logwatch_filter_apply
Tero Marttila <terom@fixme.fi>
parents: 127
diff changeset
   152
// length of output line prefix
6c5a5fdfd1d5 update logwatch_filter_apply
Tero Marttila <terom@fixme.fi>
parents: 127
diff changeset
   153
#define LOGWATCH_FILTER_OUT_PREFIX_LEN 16
6c5a5fdfd1d5 update logwatch_filter_apply
Tero Marttila <terom@fixme.fi>
parents: 127
diff changeset
   154
138
a716c621cb90 implement blackhole filters for logwatch, and stop applying filters after the first hit
Tero Marttila <terom@fixme.fi>
parents: 135
diff changeset
   155
int logwatch_filter_apply (const struct logwatch_filter *filter, const struct logwatch_source *source, const char *line, struct error_info *err)
127
94e6c3b4230f implement logwatch_on_line/logwatch_filter_apply, along with irc_*_NOTICE
Tero Marttila <terom@fixme.fi>
parents: 124
diff changeset
   156
{
94e6c3b4230f implement logwatch_on_line/logwatch_filter_apply, along with irc_*_NOTICE
Tero Marttila <terom@fixme.fi>
parents: 124
diff changeset
   157
    int ovec[LOGWATCH_FILTER_OVEC_ITEMS], ret;
133
e2d0c0c23b39 implement logwatch_chan.c, logwatch_chan_msg, logwatch_on_error
Tero Marttila <terom@fixme.fi>
parents: 132
diff changeset
   158
    char buf[LOGWATCH_FILTER_OUT_MAX + 1], *buf_ptr = buf;
e2d0c0c23b39 implement logwatch_chan.c, logwatch_chan_msg, logwatch_on_error
Tero Marttila <terom@fixme.fi>
parents: 132
diff changeset
   159
    size_t buf_size = sizeof(buf);
127
94e6c3b4230f implement logwatch_on_line/logwatch_filter_apply, along with irc_*_NOTICE
Tero Marttila <terom@fixme.fi>
parents: 124
diff changeset
   160
94e6c3b4230f implement logwatch_on_line/logwatch_filter_apply, along with irc_*_NOTICE
Tero Marttila <terom@fixme.fi>
parents: 124
diff changeset
   161
    // XXX: what to do for truncated data?
94e6c3b4230f implement logwatch_on_line/logwatch_filter_apply, along with irc_*_NOTICE
Tero Marttila <terom@fixme.fi>
parents: 124
diff changeset
   162
94e6c3b4230f implement logwatch_on_line/logwatch_filter_apply, along with irc_*_NOTICE
Tero Marttila <terom@fixme.fi>
parents: 124
diff changeset
   163
    // discard based on source?
94e6c3b4230f implement logwatch_on_line/logwatch_filter_apply, along with irc_*_NOTICE
Tero Marttila <terom@fixme.fi>
parents: 124
diff changeset
   164
    if (filter->source && source != filter->source)
138
a716c621cb90 implement blackhole filters for logwatch, and stop applying filters after the first hit
Tero Marttila <terom@fixme.fi>
parents: 135
diff changeset
   165
        return 0;
127
94e6c3b4230f implement logwatch_on_line/logwatch_filter_apply, along with irc_*_NOTICE
Tero Marttila <terom@fixme.fi>
parents: 124
diff changeset
   166
    
128
6c5a5fdfd1d5 update logwatch_filter_apply
Tero Marttila <terom@fixme.fi>
parents: 127
diff changeset
   167
    // match against the regexp?
6c5a5fdfd1d5 update logwatch_filter_apply
Tero Marttila <terom@fixme.fi>
parents: 127
diff changeset
   168
    if (filter->regexp && (ret = pcre_exec(filter->regexp, NULL, line, strlen(line), 0, 0, ovec, LOGWATCH_FILTER_OVEC_ITEMS)) <= 0) {
127
94e6c3b4230f implement logwatch_on_line/logwatch_filter_apply, along with irc_*_NOTICE
Tero Marttila <terom@fixme.fi>
parents: 124
diff changeset
   169
        if (ret == PCRE_ERROR_NOMATCH)
94e6c3b4230f implement logwatch_on_line/logwatch_filter_apply, along with irc_*_NOTICE
Tero Marttila <terom@fixme.fi>
parents: 124
diff changeset
   170
            // no match, ignore
138
a716c621cb90 implement blackhole filters for logwatch, and stop applying filters after the first hit
Tero Marttila <terom@fixme.fi>
parents: 135
diff changeset
   171
            return 0;
127
94e6c3b4230f implement logwatch_on_line/logwatch_filter_apply, along with irc_*_NOTICE
Tero Marttila <terom@fixme.fi>
parents: 124
diff changeset
   172
94e6c3b4230f implement logwatch_on_line/logwatch_filter_apply, along with irc_*_NOTICE
Tero Marttila <terom@fixme.fi>
parents: 124
diff changeset
   173
        else
94e6c3b4230f implement logwatch_on_line/logwatch_filter_apply, along with irc_*_NOTICE
Tero Marttila <terom@fixme.fi>
parents: 124
diff changeset
   174
            // misc. error
138
a716c621cb90 implement blackhole filters for logwatch, and stop applying filters after the first hit
Tero Marttila <terom@fixme.fi>
parents: 135
diff changeset
   175
            // XXX: might be 0 for too many groups
a716c621cb90 implement blackhole filters for logwatch, and stop applying filters after the first hit
Tero Marttila <terom@fixme.fi>
parents: 135
diff changeset
   176
            JUMP_SET_ERROR_EXTRA(err, ERR_PCRE_EXEC, ret);
127
94e6c3b4230f implement logwatch_on_line/logwatch_filter_apply, along with irc_*_NOTICE
Tero Marttila <terom@fixme.fi>
parents: 124
diff changeset
   177
    }
94e6c3b4230f implement logwatch_on_line/logwatch_filter_apply, along with irc_*_NOTICE
Tero Marttila <terom@fixme.fi>
parents: 124
diff changeset
   178
138
a716c621cb90 implement blackhole filters for logwatch, and stop applying filters after the first hit
Tero Marttila <terom@fixme.fi>
parents: 135
diff changeset
   179
    // blackhole?
a716c621cb90 implement blackhole filters for logwatch, and stop applying filters after the first hit
Tero Marttila <terom@fixme.fi>
parents: 135
diff changeset
   180
    if (!filter->chan)
a716c621cb90 implement blackhole filters for logwatch, and stop applying filters after the first hit
Tero Marttila <terom@fixme.fi>
parents: 135
diff changeset
   181
        return 1;
a716c621cb90 implement blackhole filters for logwatch, and stop applying filters after the first hit
Tero Marttila <terom@fixme.fi>
parents: 135
diff changeset
   182
127
94e6c3b4230f implement logwatch_on_line/logwatch_filter_apply, along with irc_*_NOTICE
Tero Marttila <terom@fixme.fi>
parents: 124
diff changeset
   183
    // format?
94e6c3b4230f implement logwatch_on_line/logwatch_filter_apply, along with irc_*_NOTICE
Tero Marttila <terom@fixme.fi>
parents: 124
diff changeset
   184
    if (filter->format) {
130
ffefb6d85ea6 implement logwatch_filter::format using str_format
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   185
        // setup the ctx
ffefb6d85ea6 implement logwatch_filter::format using str_format
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   186
        struct logwatch_filter_str_format_ctx ctx = {
ffefb6d85ea6 implement logwatch_filter::format using str_format
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   187
            .regexp         = filter->regexp, 
ffefb6d85ea6 implement logwatch_filter::format using str_format
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   188
            .subject        = line,
ffefb6d85ea6 implement logwatch_filter::format using str_format
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   189
            .groups         = ovec,
ffefb6d85ea6 implement logwatch_filter::format using str_format
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   190
            .group_count    = ret 
ffefb6d85ea6 implement logwatch_filter::format using str_format
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   191
        };
ffefb6d85ea6 implement logwatch_filter::format using str_format
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   192
ffefb6d85ea6 implement logwatch_filter::format using str_format
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   193
        // operate
ffefb6d85ea6 implement logwatch_filter::format using str_format
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   194
        if ((ERROR_CODE(err) = str_format(buf_ptr, buf_size, filter->format, logwatch_filter_str_format_cb, &ctx)))
ffefb6d85ea6 implement logwatch_filter::format using str_format
Tero Marttila <terom@fixme.fi>
parents: 128
diff changeset
   195
            goto error;
127
94e6c3b4230f implement logwatch_on_line/logwatch_filter_apply, along with irc_*_NOTICE
Tero Marttila <terom@fixme.fi>
parents: 124
diff changeset
   196
    
94e6c3b4230f implement logwatch_on_line/logwatch_filter_apply, along with irc_*_NOTICE
Tero Marttila <terom@fixme.fi>
parents: 124
diff changeset
   197
    } else {
128
6c5a5fdfd1d5 update logwatch_filter_apply
Tero Marttila <terom@fixme.fi>
parents: 127
diff changeset
   198
        // quote the entire line
133
e2d0c0c23b39 implement logwatch_chan.c, logwatch_chan_msg, logwatch_on_error
Tero Marttila <terom@fixme.fi>
parents: 132
diff changeset
   199
        // XXX: overflow
128
6c5a5fdfd1d5 update logwatch_filter_apply
Tero Marttila <terom@fixme.fi>
parents: 127
diff changeset
   200
        buf_ptr += str_advance(NULL, &buf_size, str_quote(buf_ptr, buf_size, line, -1));
133
e2d0c0c23b39 implement logwatch_chan.c, logwatch_chan_msg, logwatch_on_error
Tero Marttila <terom@fixme.fi>
parents: 132
diff changeset
   201
127
94e6c3b4230f implement logwatch_on_line/logwatch_filter_apply, along with irc_*_NOTICE
Tero Marttila <terom@fixme.fi>
parents: 124
diff changeset
   202
    }
94e6c3b4230f implement logwatch_on_line/logwatch_filter_apply, along with irc_*_NOTICE
Tero Marttila <terom@fixme.fi>
parents: 124
diff changeset
   203
133
e2d0c0c23b39 implement logwatch_chan.c, logwatch_chan_msg, logwatch_on_error
Tero Marttila <terom@fixme.fi>
parents: 132
diff changeset
   204
    // log
132
f2ece471fb07 implement logwatch_source names, and logwatch_chan
Tero Marttila <terom@fixme.fi>
parents: 130
diff changeset
   205
    log_info("<%s>:%s -> %s:%s", source->name, line, irc_chan_name(filter->chan->irc_chan), buf);
133
e2d0c0c23b39 implement logwatch_chan.c, logwatch_chan_msg, logwatch_on_error
Tero Marttila <terom@fixme.fi>
parents: 132
diff changeset
   206
    
e2d0c0c23b39 implement logwatch_chan.c, logwatch_chan_msg, logwatch_on_error
Tero Marttila <terom@fixme.fi>
parents: 132
diff changeset
   207
    // send it
e2d0c0c23b39 implement logwatch_chan.c, logwatch_chan_msg, logwatch_on_error
Tero Marttila <terom@fixme.fi>
parents: 132
diff changeset
   208
    if ((ERROR_CODE(err) = logwatch_chan_msg(filter->chan, "[%s] %s", filter->name, buf)))
127
94e6c3b4230f implement logwatch_on_line/logwatch_filter_apply, along with irc_*_NOTICE
Tero Marttila <terom@fixme.fi>
parents: 124
diff changeset
   209
        goto error;
94e6c3b4230f implement logwatch_on_line/logwatch_filter_apply, along with irc_*_NOTICE
Tero Marttila <terom@fixme.fi>
parents: 124
diff changeset
   210
138
a716c621cb90 implement blackhole filters for logwatch, and stop applying filters after the first hit
Tero Marttila <terom@fixme.fi>
parents: 135
diff changeset
   211
    // ok, stop here
a716c621cb90 implement blackhole filters for logwatch, and stop applying filters after the first hit
Tero Marttila <terom@fixme.fi>
parents: 135
diff changeset
   212
    return 1;
127
94e6c3b4230f implement logwatch_on_line/logwatch_filter_apply, along with irc_*_NOTICE
Tero Marttila <terom@fixme.fi>
parents: 124
diff changeset
   213
94e6c3b4230f implement logwatch_on_line/logwatch_filter_apply, along with irc_*_NOTICE
Tero Marttila <terom@fixme.fi>
parents: 124
diff changeset
   214
error:
138
a716c621cb90 implement blackhole filters for logwatch, and stop applying filters after the first hit
Tero Marttila <terom@fixme.fi>
parents: 135
diff changeset
   215
    return -ERROR_CODE(err);    
127
94e6c3b4230f implement logwatch_on_line/logwatch_filter_apply, along with irc_*_NOTICE
Tero Marttila <terom@fixme.fi>
parents: 124
diff changeset
   216
}
94e6c3b4230f implement logwatch_on_line/logwatch_filter_apply, along with irc_*_NOTICE
Tero Marttila <terom@fixme.fi>
parents: 124
diff changeset
   217