source: filter.c @ 5763474

barnowl_perlaimdebianrelease-1.10release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since 5763474 was 535d68b, checked in by Nelson Elhage <nelhage@mit.edu>, 17 years ago
Forgot these in the last commit; Make all the tests output TAP
  • Property mode set to 100644
File size: 8.5 KB
Line 
1#include <string.h>
2#include "owl.h"
3
4static const char fileIdent[] = "$Id$";
5
6#define OWL_FILTER_MAXRECURSE 20
7
8int owl_filter_init_fromstring(owl_filter *f, char *name, char *string)
9{
10  char **argv;
11  int argc, out;
12
13  argv=owl_parseline(string, &argc);
14  out=owl_filter_init(f, name, argc, argv);
15  owl_parsefree(argv, argc);
16  return(out);
17}
18
19int owl_filter_init(owl_filter *f, char *name, int argc, char **argv)
20{
21  f->name=owl_strdup(name);
22  f->polarity=0;
23  f->fgcolor=OWL_COLOR_DEFAULT;
24  f->bgcolor=OWL_COLOR_DEFAULT;
25  f->cachedmsgid=-1;
26
27  /* first take arguments that have to come first */
28  /* set the color */
29  while ( argc>=2 && ( !strcmp(argv[0], "-c") ||
30                       !strcmp(argv[0], "-b") ) ) {
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 {
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      }
42    }
43    argc-=2;
44    argv+=2;
45  }
46
47  if(!(f->root = owl_filter_parse_expression(argc, argv, NULL)))
48    return(-1);
49
50  /* Now check for recursion. */
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);
55  }
56
57  return(0);
58}
59
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{
65  if(!argc) return NULL;
66
67  owl_filterelement * fe = owl_malloc(sizeof(owl_filterelement));
68  owl_filterelement *op;
69
70  owl_filterelement_create(fe);
71  int i = 0, skip;
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;
78    if(i >= argc) goto err;
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;
120  owl_filterelement * op1 = NULL, * op2 = NULL;
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;
131    owl_filterelement * tmp = owl_malloc(sizeof(owl_filterelement));
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
156char *owl_filter_get_name(owl_filter *f)
157{
158  return(f->name);
159}
160
161void owl_filter_set_polarity_match(owl_filter *f)
162{
163  f->polarity=0;
164}
165
166void owl_filter_set_polarity_unmatch(owl_filter *f)
167{
168  f->polarity=1;
169}
170
171void owl_filter_set_fgcolor(owl_filter *f, int color)
172{
173  f->fgcolor=color;
174}
175
176int owl_filter_get_fgcolor(owl_filter *f)
177{
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);
189}
190
191void owl_filter_set_cachedmsgid(owl_filter *f, int cachedmsgid)
192{
193  f->cachedmsgid=cachedmsgid;
194}
195
196int owl_filter_get_cachedmsgid(owl_filter *f)
197{
198  return(f->cachedmsgid);
199}
200
201/* return 1 if the message matches the given filter, otherwise
202 * return 0.
203 */
204int owl_filter_message_match(owl_filter *f, owl_message *m)
205{
206  if(!f->root) return 0;
207  int ret = owl_filterelement_match(f->root, m);
208  if(f->polarity) ret = !ret;
209  return ret;
210}
211
212
213void owl_filter_print(owl_filter *f, char *out)
214{
215  strcpy(out, owl_filter_get_name(f));
216  strcat(out, ": ");
217
218  if (f->fgcolor!=OWL_COLOR_DEFAULT) {
219    strcat(out, "-c ");
220    if (f->fgcolor < 8) {
221      strcat(out, owl_util_color_to_string(f->fgcolor));
222    }
223    else {
224      char* c = owl_sprintf("%i",f->fgcolor);
225      strcat(out, c);
226      owl_free(c);
227    }
228    strcat(out, " ");
229  }
230  if (f->bgcolor!=OWL_COLOR_DEFAULT) {
231    strcat(out, "-b ");
232    if (f->bgcolor < 8) {
233      strcat(out, owl_util_color_to_string(f->bgcolor));
234    }
235    else {
236      char* c = owl_sprintf("%i",f->bgcolor);
237      strcat(out, c);
238      owl_free(c);
239    }
240    strcat(out, " ");
241  }
242  if(!f->root) return;
243  owl_filterelement_print(f->root, out);
244  strcat(out, "\n");
245}
246
247/* Return 1 if the filters 'a' and 'b' are equivalent, 0 otherwise */
248int owl_filter_equiv(owl_filter *a, owl_filter *b)
249{
250  char buff[LINE], buff2[LINE];
251
252  owl_filter_print(a, buff);
253  owl_filter_print(b, buff2);
254
255  if (!strcmp(buff, buff2)) return(1);
256  return(0);
257}
258
259
260int owl_filter_is_toodeep(owl_filter *f)
261{
262  return owl_filterelement_is_toodeep(f, f->root);
263}
264
265void owl_filter_free(owl_filter *f)
266{
267  if(f->root) {
268    owl_filterelement_free(f->root);
269    owl_free(f->root);
270  }
271  if (f->name) owl_free(f->name);
272}
273
274/**************************************************************************/
275/************************* REGRESSION TESTS *******************************/
276/**************************************************************************/
277
278#ifdef OWL_INCLUDE_REG_TESTS
279
280int owl_filter_test_string(char * filt, owl_message *m, int shouldmatch) /* noproto */ {
281  owl_filter f;
282  int ok;
283  int failed = 0;
284  if(owl_filter_init_fromstring(&f, "test-filter", filt)) {
285    printf("not ok can't parse %s\n", filt);
286    failed = 1;
287    goto out;
288  }
289  ok = owl_filter_message_match(&f, m);
290  if((shouldmatch && !ok) || (!shouldmatch && ok)) {
291    printf("not ok match %s (got %d, expected %d)\n", filt, ok, shouldmatch);
292    failed = 1;
293  }
294 out:
295  owl_filter_free(&f);
296  if(!failed) {
297    printf("ok %s %s\n", shouldmatch ? "matches" : "doesn't match", filt);
298  }
299  return failed;
300}
301
302
303#include "test.h"
304
305
306int owl_filter_regtest(void) {
307  owl_list_create(&(g.filterlist));
308  int numfailed=0;
309  owl_message m;
310  owl_message_init(&m);
311  owl_message_set_type_zephyr(&m);
312  owl_message_set_direction_in(&m);
313  owl_message_set_class(&m, "owl");
314  owl_message_set_instance(&m, "tester");
315  owl_message_set_sender(&m, "owl-user");
316  owl_message_set_recipient(&m, "joe");
317  owl_message_set_attribute(&m, "foo", "bar");
318
319#define TEST_FILTER(f, e) numfailed += owl_filter_test_string(f, &m, e)
320
321
322  TEST_FILTER("true", 1);
323  TEST_FILTER("false", 0);
324  TEST_FILTER("( true )", 1);
325  TEST_FILTER("not false", 1);
326  TEST_FILTER("( true ) or ( false )", 1);
327  TEST_FILTER("true and false", 0);
328  TEST_FILTER("( true or true ) or ( ( false ) )", 1);
329
330  TEST_FILTER("class owl", 1);
331  TEST_FILTER("class ^owl$", 1);
332  TEST_FILTER("instance test", 1);
333  TEST_FILTER("instance ^test$", 0);
334  TEST_FILTER("instance ^tester$", 1);
335
336  TEST_FILTER("foo bar", 1);
337  TEST_FILTER("class owl and instance tester", 1);
338  TEST_FILTER("type ^zephyr$ and direction ^in$ and ( class ^owl$ or instance ^owl$ )", 1);
339
340  // Order of operations and precedence
341  TEST_FILTER("not true or false", 0);
342  TEST_FILTER("true or true and false", 0);
343  TEST_FILTER("true and true and false or true", 1);
344  TEST_FILTER("false and false or true", 1);
345  TEST_FILTER("true and false or false", 0);
346
347  owl_filter f1, f2, f3, f4;
348
349  owl_filter_init_fromstring(&f1, "f1", "class owl");
350  owl_global_add_filter(&g, &f1);
351  TEST_FILTER("filter f1", 1);
352
353  // Test recursion prevention
354  FAIL_UNLESS("self reference", owl_filter_init_fromstring(&f2, "test", "filter test"));
355
356  // mutual recursion
357  owl_filter_init_fromstring(&f3, "f3", "filter f4");
358  owl_global_add_filter(&g, &f3);
359  FAIL_UNLESS("mutual recursion",   owl_filter_init_fromstring(&f4, "f4", "filter f3"));
360
361  return 0;
362}
363
364
365#endif /* OWL_INCLUDE_REG_TESTS */
Note: See TracBrowser for help on using the repository browser.