source: filter.c @ 799b60e

debianrelease-1.10release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since 799b60e was cdc6ff1, checked in by Nelson Elhage <nelhage@mit.edu>, 16 years ago
Implement a getfilter command to get the text of a filter.
  • Property mode set to 100644
File size: 8.6 KB
RevLine 
[7d4fbcd]1#include <string.h>
2#include "owl.h"
3
[1aee7d9]4static const char fileIdent[] = "$Id$";
5
[40458b9]6#define OWL_FILTER_MAXRECURSE 20
7
[e187445]8int owl_filter_init_fromstring(owl_filter *f, char *name, char *string)
9{
[7d4fbcd]10  char **argv;
11  int argc, out;
12
13  argv=owl_parseline(string, &argc);
14  out=owl_filter_init(f, name, argc, argv);
[1716fed]15  owl_parsefree(argv, argc);
[7d4fbcd]16  return(out);
17}
18
[e187445]19int owl_filter_init(owl_filter *f, char *name, int argc, char **argv)
20{
[7d4fbcd]21  f->name=owl_strdup(name);
22  f->polarity=0;
[8fa9562]23  f->fgcolor=OWL_COLOR_DEFAULT;
24  f->bgcolor=OWL_COLOR_DEFAULT;
[59cf91c]25  f->cachedmsgid=-1;
[cb769bb]26
[7d4fbcd]27  /* first take arguments that have to come first */
28  /* set the color */
[8fa9562]29  while ( argc>=2 && ( !strcmp(argv[0], "-c") ||
30                       !strcmp(argv[0], "-b") ) ) {
[601733d]31    if (owl_util_string_to_color(argv[1])==OWL_COLOR_INVALID) {
[12c35df]32      owl_function_error("The color '%s' is not available, using default.", argv[1]);
33    } else {
[8fa9562]34      switch (argv[0][1]) {
35      case 'c':
36        f->fgcolor=owl_util_string_to_color(argv[1]);
37        break;
38      case 'b':
39        f->bgcolor=owl_util_string_to_color(argv[1]);
40        break;
41      }
[12c35df]42    }
[7d4fbcd]43    argc-=2;
44    argv+=2;
45  }
46
[cb769bb]47  if(!(f->root = owl_filter_parse_expression(argc, argv, NULL)))
48    return(-1);
[7d4fbcd]49
[cb769bb]50  /* Now check for recursion. */
[40458b9]51  if (owl_filter_is_toodeep(f)) {
52    owl_function_error("Filter loop or exceeds recursion depth");
53    owl_filter_free(f);
54    return(-1);
[7d4fbcd]55  }
[cb769bb]56
[7d4fbcd]57  return(0);
58}
59
[cb769bb]60
61/* A primitive expression is one without any toplevel ``and'' or ``or''s*/
62
63static owl_filterelement * owl_filter_parse_primitive_expression(int argc, char **argv, int *next)
64{
[ad15610]65  owl_filterelement *fe, *op;
66  int i = 0, skip;
[cb769bb]67
[ad15610]68  if(!argc) return NULL;
[cb769bb]69
[ad15610]70  fe = owl_malloc(sizeof(owl_filterelement));
[cb769bb]71  owl_filterelement_create(fe);
72
73  if(!strcasecmp(argv[i], "(")) {
74    i++;
75    op = owl_filter_parse_expression(argc-i, argv+i, &skip);
76    if(!op) goto err;
77    i += skip;
[d791cdb]78    if(i >= argc) goto err;
[cb769bb]79    if(strcasecmp(argv[i++], ")")) goto err;
80    owl_filterelement_create_group(fe, op);
81  } else if(!strcasecmp(argv[i], "not")) {
82    i++;
83    op = owl_filter_parse_primitive_expression(argc-i, argv+i, &skip);
84    if(!op) goto err;
85    i += skip;
86    owl_filterelement_create_not(fe, op);
87  } else if(!strcasecmp(argv[i], "true")) {
88    i++;
89    owl_filterelement_create_true(fe);
90  } else if(!strcasecmp(argv[i], "false")) {
91    i++;
92    owl_filterelement_create_false(fe);
93  } else {
94    if(argc == 1) goto err;
95    if(!strcasecmp(*argv, "filter")) {
96      owl_filterelement_create_filter(fe, *(argv+1));
97    } else if(!strcasecmp(*argv, "perl")) {
98      owl_filterelement_create_perl(fe, *(argv+1));
99    } else {
100      owl_filterelement_create_re(fe, *argv, *(argv+1));
101    }
102    i += 2;
103  }
104
105  if(next) {
106    *next = i;
107  } else if(i != argc) {
108    goto err;
109  }
110  return fe;
111err:
112  owl_filterelement_free(fe);
113  owl_free(fe);
114  return NULL;
115}
116
117owl_filterelement * owl_filter_parse_expression(int argc, char **argv, int *next)
118{
119  int i = 0, skip;
[ad15610]120  owl_filterelement * op1 = NULL, * op2 = NULL, *tmp;
[cb769bb]121
122  op1 = owl_filter_parse_primitive_expression(argc-i, argv+i, &skip);
123  i += skip;
124  if(!op1) goto err;
125
126  while(i < argc) {
127    if(strcasecmp(argv[i], "and") &&
128       strcasecmp(argv[i], "or")) break;
129    op2 = owl_filter_parse_primitive_expression(argc-i-1, argv+i+1, &skip);
130    if(!op2) goto err;
[ad15610]131    tmp = owl_malloc(sizeof(owl_filterelement));
[cb769bb]132    if(!strcasecmp(argv[i], "and")) {
133      owl_filterelement_create_and(tmp, op1, op2);
134    } else {
135      owl_filterelement_create_or(tmp, op1, op2);
136    }
137    op1 = tmp;
138    op2 = NULL;
139    i += skip+1;
140  }
141
142  if(next) {
143    *next = i;
144  } else if(i != argc) {
145    goto err;
146  }
147  return op1;
148err:
149  if(op1) {
150    owl_filterelement_free(op1);
151    owl_free(op1);
152  }
153  return NULL;
154}
155
[e187445]156char *owl_filter_get_name(owl_filter *f)
157{
[7d4fbcd]158  return(f->name);
159}
160
[e187445]161void owl_filter_set_polarity_match(owl_filter *f)
162{
[7d4fbcd]163  f->polarity=0;
164}
165
[e187445]166void owl_filter_set_polarity_unmatch(owl_filter *f)
167{
[7d4fbcd]168  f->polarity=1;
169}
170
[8fa9562]171void owl_filter_set_fgcolor(owl_filter *f, int color)
[e187445]172{
[8fa9562]173  f->fgcolor=color;
[7d4fbcd]174}
175
[8fa9562]176int owl_filter_get_fgcolor(owl_filter *f)
[e187445]177{
[8fa9562]178  return(f->fgcolor);
179}
180
181void owl_filter_set_bgcolor(owl_filter *f, int color)
182{
183  f->bgcolor=color;
184}
185
186int owl_filter_get_bgcolor(owl_filter *f)
187{
188  return(f->bgcolor);
[7d4fbcd]189}
190
[e187445]191void owl_filter_set_cachedmsgid(owl_filter *f, int cachedmsgid)
192{
[59cf91c]193  f->cachedmsgid=cachedmsgid;
194}
195
[e187445]196int owl_filter_get_cachedmsgid(owl_filter *f)
197{
[59cf91c]198  return(f->cachedmsgid);
199}
200
[15b34fd]201/* return 1 if the message matches the given filter, otherwise
202 * return 0.
203 */
[e187445]204int owl_filter_message_match(owl_filter *f, owl_message *m)
205{
[ad15610]206  int ret;
[cb769bb]207  if(!f->root) return 0;
[ad15610]208  ret = owl_filterelement_match(f->root, m);
[cb769bb]209  if(f->polarity) ret = !ret;
210  return ret;
[7d4fbcd]211}
212
213
[0504f63]214char* owl_filter_print(owl_filter *f)
[e187445]215{
[cdc6ff1]216  GString *out = g_string_new("");
[446aa2b]217
[8fa9562]218  if (f->fgcolor!=OWL_COLOR_DEFAULT) {
[0504f63]219    g_string_append(out, "-c ");
[c2c5c77]220    if (f->fgcolor < 8) {
[0504f63]221      g_string_append(out, owl_util_color_to_string(f->fgcolor));
[c2c5c77]222    }
223    else {
[0504f63]224      g_string_append_printf(out, "%i",f->fgcolor);
[c2c5c77]225    }
[0504f63]226    g_string_append(out, " ");
[8fa9562]227  }
228  if (f->bgcolor!=OWL_COLOR_DEFAULT) {
[0504f63]229    g_string_append(out, "-b ");
[c2c5c77]230    if (f->bgcolor < 8) {
[0504f63]231      g_string_append(out, owl_util_color_to_string(f->bgcolor));
[c2c5c77]232    }
233    else {
[0504f63]234      g_string_append_printf(out, "%i",f->fgcolor);
[c2c5c77]235    }
[0504f63]236    g_string_append(out, " ");
237  }
238  if(f->root) {
239    owl_filterelement_print(f->root, out);
240    g_string_append(out, "\n");
[446aa2b]241  }
[0504f63]242
243  return g_string_free(out, 0);
[7d4fbcd]244}
245
[40458b9]246/* Return 1 if the filters 'a' and 'b' are equivalent, 0 otherwise */
[e187445]247int owl_filter_equiv(owl_filter *a, owl_filter *b)
248{
[0504f63]249  char *buffa, *buffb;
250  int ret;
[7d4fbcd]251
[0504f63]252  buffa = owl_filter_print(a);
253  buffb = owl_filter_print(b);
[7d4fbcd]254
[0504f63]255  ret = !strcmp(buffa, buffb);
[cdc6ff1]256  ret = ret && !strcmp(owl_filter_get_name(a),
257                       owl_filter_get_name(b));
[0504f63]258
259  owl_free(buffa);
260  owl_free(buffb);
261
262  return ret;
[7d4fbcd]263}
264
[cb769bb]265
266int owl_filter_is_toodeep(owl_filter *f)
[40458b9]267{
[cb769bb]268  return owl_filterelement_is_toodeep(f, f->root);
[40458b9]269}
270
[cb769bb]271void owl_filter_free(owl_filter *f)
[40458b9]272{
[cb769bb]273  if(f->root) {
274    owl_filterelement_free(f->root);
275    owl_free(f->root);
[40458b9]276  }
[cb769bb]277  if (f->name) owl_free(f->name);
278}
[40458b9]279
[cb769bb]280/**************************************************************************/
281/************************* REGRESSION TESTS *******************************/
282/**************************************************************************/
[40458b9]283
[cb769bb]284#ifdef OWL_INCLUDE_REG_TESTS
[40458b9]285
[cb769bb]286int owl_filter_test_string(char * filt, owl_message *m, int shouldmatch) /* noproto */ {
287  owl_filter f;
288  int ok;
289  int failed = 0;
290  if(owl_filter_init_fromstring(&f, "test-filter", filt)) {
[535d68b]291    printf("not ok can't parse %s\n", filt);
[cb769bb]292    failed = 1;
293    goto out;
294  }
295  ok = owl_filter_message_match(&f, m);
296  if((shouldmatch && !ok) || (!shouldmatch && ok)) {
[535d68b]297    printf("not ok match %s (got %d, expected %d)\n", filt, ok, shouldmatch);
[cb769bb]298    failed = 1;
299  }
300 out:
301  owl_filter_free(&f);
302  if(!failed) {
[535d68b]303    printf("ok %s %s\n", shouldmatch ? "matches" : "doesn't match", filt);
[cb769bb]304  }
305  return failed;
306}
[40458b9]307
308
[cb769bb]309#include "test.h"
[40458b9]310
311
[cb769bb]312int owl_filter_regtest(void) {
313  int numfailed=0;
314  owl_message m;
[ad15610]315  owl_filter f1, f2, f3, f4;
316
317  owl_list_create(&(g.filterlist));
[cb769bb]318  owl_message_init(&m);
319  owl_message_set_type_zephyr(&m);
320  owl_message_set_direction_in(&m);
321  owl_message_set_class(&m, "owl");
322  owl_message_set_instance(&m, "tester");
323  owl_message_set_sender(&m, "owl-user");
324  owl_message_set_recipient(&m, "joe");
325  owl_message_set_attribute(&m, "foo", "bar");
[40458b9]326
[cb769bb]327#define TEST_FILTER(f, e) numfailed += owl_filter_test_string(f, &m, e)
[40458b9]328
[7d4fbcd]329
[cb769bb]330  TEST_FILTER("true", 1);
331  TEST_FILTER("false", 0);
332  TEST_FILTER("( true )", 1);
333  TEST_FILTER("not false", 1);
334  TEST_FILTER("( true ) or ( false )", 1);
335  TEST_FILTER("true and false", 0);
336  TEST_FILTER("( true or true ) or ( ( false ) )", 1);
337
338  TEST_FILTER("class owl", 1);
339  TEST_FILTER("class ^owl$", 1);
340  TEST_FILTER("instance test", 1);
341  TEST_FILTER("instance ^test$", 0);
342  TEST_FILTER("instance ^tester$", 1);
343
344  TEST_FILTER("foo bar", 1);
345  TEST_FILTER("class owl and instance tester", 1);
346  TEST_FILTER("type ^zephyr$ and direction ^in$ and ( class ^owl$ or instance ^owl$ )", 1);
347
[af1920fd]348  /* Order of operations and precedence */
[cb769bb]349  TEST_FILTER("not true or false", 0);
350  TEST_FILTER("true or true and false", 0);
351  TEST_FILTER("true and true and false or true", 1);
352  TEST_FILTER("false and false or true", 1);
353  TEST_FILTER("true and false or false", 0);
354
355  owl_filter_init_fromstring(&f1, "f1", "class owl");
356  owl_global_add_filter(&g, &f1);
357  TEST_FILTER("filter f1", 1);
358
[af1920fd]359  /* Test recursion prevention */
[cb769bb]360  FAIL_UNLESS("self reference", owl_filter_init_fromstring(&f2, "test", "filter test"));
361
[af1920fd]362  /* mutual recursion */
[cb769bb]363  owl_filter_init_fromstring(&f3, "f3", "filter f4");
364  owl_global_add_filter(&g, &f3);
365  FAIL_UNLESS("mutual recursion",   owl_filter_init_fromstring(&f4, "f4", "filter f3"));
366
367  return 0;
[7d4fbcd]368}
[cb769bb]369
370
371#endif /* OWL_INCLUDE_REG_TESTS */
Note: See TracBrowser for help on using the repository browser.