source: filter.c @ 7ba9ead9

debianrelease-1.10release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since 7ba9ead9 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
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])==OWL_COLOR_INVALID) {
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
214char* owl_filter_print(owl_filter *f)
215{
216  GString *out = g_string_new("");
217
218  if (f->fgcolor!=OWL_COLOR_DEFAULT) {
219    g_string_append(out, "-c ");
220    if (f->fgcolor < 8) {
221      g_string_append(out, owl_util_color_to_string(f->fgcolor));
222    }
223    else {
224      g_string_append_printf(out, "%i",f->fgcolor);
225    }
226    g_string_append(out, " ");
227  }
228  if (f->bgcolor!=OWL_COLOR_DEFAULT) {
229    g_string_append(out, "-b ");
230    if (f->bgcolor < 8) {
231      g_string_append(out, owl_util_color_to_string(f->bgcolor));
232    }
233    else {
234      g_string_append_printf(out, "%i",f->fgcolor);
235    }
236    g_string_append(out, " ");
237  }
238  if(f->root) {
239    owl_filterelement_print(f->root, out);
240    g_string_append(out, "\n");
241  }
242
243  return g_string_free(out, 0);
244}
245
246/* Return 1 if the filters 'a' and 'b' are equivalent, 0 otherwise */
247int owl_filter_equiv(owl_filter *a, owl_filter *b)
248{
249  char *buffa, *buffb;
250  int ret;
251
252  buffa = owl_filter_print(a);
253  buffb = owl_filter_print(b);
254
255  ret = !strcmp(buffa, buffb);
256  ret = ret && !strcmp(owl_filter_get_name(a),
257                       owl_filter_get_name(b));
258
259  owl_free(buffa);
260  owl_free(buffb);
261
262  return ret;
263}
264
265
266int owl_filter_is_toodeep(owl_filter *f)
267{
268  return owl_filterelement_is_toodeep(f, f->root);
269}
270
271void owl_filter_free(owl_filter *f)
272{
273  if(f->root) {
274    owl_filterelement_free(f->root);
275    owl_free(f->root);
276  }
277  if (f->name) owl_free(f->name);
278}
279
280/**************************************************************************/
281/************************* REGRESSION TESTS *******************************/
282/**************************************************************************/
283
284#ifdef OWL_INCLUDE_REG_TESTS
285
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)) {
291    printf("not ok can't parse %s\n", filt);
292    failed = 1;
293    goto out;
294  }
295  ok = owl_filter_message_match(&f, m);
296  if((shouldmatch && !ok) || (!shouldmatch && ok)) {
297    printf("not ok match %s (got %d, expected %d)\n", filt, ok, shouldmatch);
298    failed = 1;
299  }
300 out:
301  owl_filter_free(&f);
302  if(!failed) {
303    printf("ok %s %s\n", shouldmatch ? "matches" : "doesn't match", filt);
304  }
305  return failed;
306}
307
308
309#include "test.h"
310
311
312int owl_filter_regtest(void) {
313  int numfailed=0;
314  owl_message m;
315  owl_filter f1, f2, f3, f4;
316
317  owl_list_create(&(g.filterlist));
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");
326
327#define TEST_FILTER(f, e) numfailed += owl_filter_test_string(f, &m, e)
328
329
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
348  /* Order of operations and precedence */
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
359  /* Test recursion prevention */
360  FAIL_UNLESS("self reference", owl_filter_init_fromstring(&f2, "test", "filter test"));
361
362  /* mutual recursion */
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;
368}
369
370
371#endif /* OWL_INCLUDE_REG_TESTS */
Note: See TracBrowser for help on using the repository browser.