source: filter.c @ 04166e9

barnowl_perlaimdebianrelease-1.10release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since 04166e9 was ad15610, checked in by Nelson Elhage <nelhage@mit.edu>, 17 years ago
Applying ctl's variable shuffling patch for better ANSI C-ness. closes #18
  • Property mode set to 100644
File size: 8.5 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") ) ) {
[12c35df]31    if (owl_util_string_to_color(argv[1])==-1) {
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
[e187445]214void owl_filter_print(owl_filter *f, char *out)
215{
[446aa2b]216  strcpy(out, owl_filter_get_name(f));
217  strcat(out, ": ");
218
[8fa9562]219  if (f->fgcolor!=OWL_COLOR_DEFAULT) {
[446aa2b]220    strcat(out, "-c ");
[c2c5c77]221    if (f->fgcolor < 8) {
222      strcat(out, owl_util_color_to_string(f->fgcolor));
223    }
224    else {
225      char* c = owl_sprintf("%i",f->fgcolor);
226      strcat(out, c);
227      owl_free(c);
228    }
[8fa9562]229    strcat(out, " ");
230  }
231  if (f->bgcolor!=OWL_COLOR_DEFAULT) {
232    strcat(out, "-b ");
[c2c5c77]233    if (f->bgcolor < 8) {
234      strcat(out, owl_util_color_to_string(f->bgcolor));
235    }
236    else {
237      char* c = owl_sprintf("%i",f->bgcolor);
238      strcat(out, c);
239      owl_free(c);
240    }
[446aa2b]241    strcat(out, " ");
242  }
[cb769bb]243  if(!f->root) return;
244  owl_filterelement_print(f->root, out);
[7d4fbcd]245  strcat(out, "\n");
246}
247
[40458b9]248/* Return 1 if the filters 'a' and 'b' are equivalent, 0 otherwise */
[e187445]249int owl_filter_equiv(owl_filter *a, owl_filter *b)
250{
[7d4fbcd]251  char buff[LINE], buff2[LINE];
252
253  owl_filter_print(a, buff);
254  owl_filter_print(b, buff2);
255
256  if (!strcmp(buff, buff2)) return(1);
257  return(0);
258}
259
[cb769bb]260
261int owl_filter_is_toodeep(owl_filter *f)
[40458b9]262{
[cb769bb]263  return owl_filterelement_is_toodeep(f, f->root);
[40458b9]264}
265
[cb769bb]266void owl_filter_free(owl_filter *f)
[40458b9]267{
[cb769bb]268  if(f->root) {
269    owl_filterelement_free(f->root);
270    owl_free(f->root);
[40458b9]271  }
[cb769bb]272  if (f->name) owl_free(f->name);
273}
[40458b9]274
[cb769bb]275/**************************************************************************/
276/************************* REGRESSION TESTS *******************************/
277/**************************************************************************/
[40458b9]278
[cb769bb]279#ifdef OWL_INCLUDE_REG_TESTS
[40458b9]280
[cb769bb]281int owl_filter_test_string(char * filt, owl_message *m, int shouldmatch) /* noproto */ {
282  owl_filter f;
283  int ok;
284  int failed = 0;
285  if(owl_filter_init_fromstring(&f, "test-filter", filt)) {
[535d68b]286    printf("not ok can't parse %s\n", filt);
[cb769bb]287    failed = 1;
288    goto out;
289  }
290  ok = owl_filter_message_match(&f, m);
291  if((shouldmatch && !ok) || (!shouldmatch && ok)) {
[535d68b]292    printf("not ok match %s (got %d, expected %d)\n", filt, ok, shouldmatch);
[cb769bb]293    failed = 1;
294  }
295 out:
296  owl_filter_free(&f);
297  if(!failed) {
[535d68b]298    printf("ok %s %s\n", shouldmatch ? "matches" : "doesn't match", filt);
[cb769bb]299  }
300  return failed;
301}
[40458b9]302
303
[cb769bb]304#include "test.h"
[40458b9]305
306
[cb769bb]307int owl_filter_regtest(void) {
308  int numfailed=0;
309  owl_message m;
[ad15610]310  owl_filter f1, f2, f3, f4;
311
312  owl_list_create(&(g.filterlist));
[cb769bb]313  owl_message_init(&m);
314  owl_message_set_type_zephyr(&m);
315  owl_message_set_direction_in(&m);
316  owl_message_set_class(&m, "owl");
317  owl_message_set_instance(&m, "tester");
318  owl_message_set_sender(&m, "owl-user");
319  owl_message_set_recipient(&m, "joe");
320  owl_message_set_attribute(&m, "foo", "bar");
[40458b9]321
[cb769bb]322#define TEST_FILTER(f, e) numfailed += owl_filter_test_string(f, &m, e)
[40458b9]323
[7d4fbcd]324
[cb769bb]325  TEST_FILTER("true", 1);
326  TEST_FILTER("false", 0);
327  TEST_FILTER("( true )", 1);
328  TEST_FILTER("not false", 1);
329  TEST_FILTER("( true ) or ( false )", 1);
330  TEST_FILTER("true and false", 0);
331  TEST_FILTER("( true or true ) or ( ( false ) )", 1);
332
333  TEST_FILTER("class owl", 1);
334  TEST_FILTER("class ^owl$", 1);
335  TEST_FILTER("instance test", 1);
336  TEST_FILTER("instance ^test$", 0);
337  TEST_FILTER("instance ^tester$", 1);
338
339  TEST_FILTER("foo bar", 1);
340  TEST_FILTER("class owl and instance tester", 1);
341  TEST_FILTER("type ^zephyr$ and direction ^in$ and ( class ^owl$ or instance ^owl$ )", 1);
342
343  // Order of operations and precedence
344  TEST_FILTER("not true or false", 0);
345  TEST_FILTER("true or true and false", 0);
346  TEST_FILTER("true and true and false or true", 1);
347  TEST_FILTER("false and false or true", 1);
348  TEST_FILTER("true and false or false", 0);
349
350  owl_filter_init_fromstring(&f1, "f1", "class owl");
351  owl_global_add_filter(&g, &f1);
352  TEST_FILTER("filter f1", 1);
353
354  // Test recursion prevention
355  FAIL_UNLESS("self reference", owl_filter_init_fromstring(&f2, "test", "filter test"));
356
357  // mutual recursion
358  owl_filter_init_fromstring(&f3, "f3", "filter f4");
359  owl_global_add_filter(&g, &f3);
360  FAIL_UNLESS("mutual recursion",   owl_filter_init_fromstring(&f4, "f4", "filter f3"));
361
362  return 0;
[7d4fbcd]363}
[cb769bb]364
365
366#endif /* OWL_INCLUDE_REG_TESTS */
Note: See TracBrowser for help on using the repository browser.