source: filter.c @ 93ee554

barnowl_perlaimdebianrelease-1.10release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since 93ee554 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
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  owl_filterelement *fe, *op;
66  int i = 0, skip;
67
68  if(!argc) return NULL;
69
70  fe = owl_malloc(sizeof(owl_filterelement));
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;
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, *tmp;
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    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  int ret;
207  if(!f->root) return 0;
208  ret = owl_filterelement_match(f->root, m);
209  if(f->polarity) ret = !ret;
210  return ret;
211}
212
213
214void owl_filter_print(owl_filter *f, char *out)
215{
216  strcpy(out, owl_filter_get_name(f));
217  strcat(out, ": ");
218
219  if (f->fgcolor!=OWL_COLOR_DEFAULT) {
220    strcat(out, "-c ");
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    }
229    strcat(out, " ");
230  }
231  if (f->bgcolor!=OWL_COLOR_DEFAULT) {
232    strcat(out, "-b ");
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    }
241    strcat(out, " ");
242  }
243  if(!f->root) return;
244  owl_filterelement_print(f->root, out);
245  strcat(out, "\n");
246}
247
248/* Return 1 if the filters 'a' and 'b' are equivalent, 0 otherwise */
249int owl_filter_equiv(owl_filter *a, owl_filter *b)
250{
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
260
261int owl_filter_is_toodeep(owl_filter *f)
262{
263  return owl_filterelement_is_toodeep(f, f->root);
264}
265
266void owl_filter_free(owl_filter *f)
267{
268  if(f->root) {
269    owl_filterelement_free(f->root);
270    owl_free(f->root);
271  }
272  if (f->name) owl_free(f->name);
273}
274
275/**************************************************************************/
276/************************* REGRESSION TESTS *******************************/
277/**************************************************************************/
278
279#ifdef OWL_INCLUDE_REG_TESTS
280
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)) {
286    printf("not ok can't parse %s\n", filt);
287    failed = 1;
288    goto out;
289  }
290  ok = owl_filter_message_match(&f, m);
291  if((shouldmatch && !ok) || (!shouldmatch && ok)) {
292    printf("not ok match %s (got %d, expected %d)\n", filt, ok, shouldmatch);
293    failed = 1;
294  }
295 out:
296  owl_filter_free(&f);
297  if(!failed) {
298    printf("ok %s %s\n", shouldmatch ? "matches" : "doesn't match", filt);
299  }
300  return failed;
301}
302
303
304#include "test.h"
305
306
307int owl_filter_regtest(void) {
308  int numfailed=0;
309  owl_message m;
310  owl_filter f1, f2, f3, f4;
311
312  owl_list_create(&(g.filterlist));
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");
321
322#define TEST_FILTER(f, e) numfailed += owl_filter_test_string(f, &m, e)
323
324
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;
363}
364
365
366#endif /* OWL_INCLUDE_REG_TESTS */
Note: See TracBrowser for help on using the repository browser.