source: filter.c @ c2c5c77

barnowl_perlaimdebianrelease-1.10release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since c2c5c77 was c2c5c77, checked in by Alejandro R. Sedeño <asedeno@mit.edu>, 17 years ago
Adding 256-color support. This requires a version of ncurses that supports ABI-6. Colors beyond the first eight are refered to by number. Perl now has the number of colors exposed to it by way of BarnOwl::getnumcolors() and also has a mechanism for undefining filters using BarnOwl::_remove_filter([filter-name]) You can't remove the 'all' filter or the current filter.
  • 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(strcasecmp(argv[i++], ")")) goto err;
79    owl_filterelement_create_group(fe, op);
80  } else if(!strcasecmp(argv[i], "not")) {
81    i++;
82    op = owl_filter_parse_primitive_expression(argc-i, argv+i, &skip);
83    if(!op) goto err;
84    i += skip;
85    owl_filterelement_create_not(fe, op);
86  } else if(!strcasecmp(argv[i], "true")) {
87    i++;
88    owl_filterelement_create_true(fe);
89  } else if(!strcasecmp(argv[i], "false")) {
90    i++;
91    owl_filterelement_create_false(fe);
92  } else {
93    if(argc == 1) goto err;
94    if(!strcasecmp(*argv, "filter")) {
95      owl_filterelement_create_filter(fe, *(argv+1));
96    } else if(!strcasecmp(*argv, "perl")) {
97      owl_filterelement_create_perl(fe, *(argv+1));
98    } else {
99      owl_filterelement_create_re(fe, *argv, *(argv+1));
100    }
101    i += 2;
102  }
103
104  if(next) {
105    *next = i;
106  } else if(i != argc) {
107    goto err;
108  }
109  return fe;
110err:
111  owl_filterelement_free(fe);
112  owl_free(fe);
113  return NULL;
114}
115
116owl_filterelement * owl_filter_parse_expression(int argc, char **argv, int *next)
117{
118  int i = 0, skip;
119  owl_filterelement * op1 = NULL, * op2 = NULL;
120
121  op1 = owl_filter_parse_primitive_expression(argc-i, argv+i, &skip);
122  i += skip;
123  if(!op1) goto err;
124
125  while(i < argc) {
126    if(strcasecmp(argv[i], "and") &&
127       strcasecmp(argv[i], "or")) break;
128    op2 = owl_filter_parse_primitive_expression(argc-i-1, argv+i+1, &skip);
129    if(!op2) goto err;
130    owl_filterelement * tmp = owl_malloc(sizeof(owl_filterelement));
131    if(!strcasecmp(argv[i], "and")) {
132      owl_filterelement_create_and(tmp, op1, op2);
133    } else {
134      owl_filterelement_create_or(tmp, op1, op2);
135    }
136    op1 = tmp;
137    op2 = NULL;
138    i += skip+1;
139  }
140
141  if(next) {
142    *next = i;
143  } else if(i != argc) {
144    goto err;
145  }
146  return op1;
147err:
148  if(op1) {
149    owl_filterelement_free(op1);
150    owl_free(op1);
151  }
152  return NULL;
153}
154
155char *owl_filter_get_name(owl_filter *f)
156{
157  return(f->name);
158}
159
160void owl_filter_set_polarity_match(owl_filter *f)
161{
162  f->polarity=0;
163}
164
165void owl_filter_set_polarity_unmatch(owl_filter *f)
166{
167  f->polarity=1;
168}
169
170void owl_filter_set_fgcolor(owl_filter *f, int color)
171{
172  f->fgcolor=color;
173}
174
175int owl_filter_get_fgcolor(owl_filter *f)
176{
177  return(f->fgcolor);
178}
179
180void owl_filter_set_bgcolor(owl_filter *f, int color)
181{
182  f->bgcolor=color;
183}
184
185int owl_filter_get_bgcolor(owl_filter *f)
186{
187  return(f->bgcolor);
188}
189
190void owl_filter_set_cachedmsgid(owl_filter *f, int cachedmsgid)
191{
192  f->cachedmsgid=cachedmsgid;
193}
194
195int owl_filter_get_cachedmsgid(owl_filter *f)
196{
197  return(f->cachedmsgid);
198}
199
200/* return 1 if the message matches the given filter, otherwise
201 * return 0.
202 */
203int owl_filter_message_match(owl_filter *f, owl_message *m)
204{
205  if(!f->root) return 0;
206  int ret = owl_filterelement_match(f->root, m);
207  if(f->polarity) ret = !ret;
208  return ret;
209}
210
211
212void owl_filter_print(owl_filter *f, char *out)
213{
214  strcpy(out, owl_filter_get_name(f));
215  strcat(out, ": ");
216
217  if (f->fgcolor!=OWL_COLOR_DEFAULT) {
218    strcat(out, "-c ");
219    if (f->fgcolor < 8) {
220      strcat(out, owl_util_color_to_string(f->fgcolor));
221    }
222    else {
223      char* c = owl_sprintf("%i",f->fgcolor);
224      strcat(out, c);
225      owl_free(c);
226    }
227    strcat(out, " ");
228  }
229  if (f->bgcolor!=OWL_COLOR_DEFAULT) {
230    strcat(out, "-b ");
231    if (f->bgcolor < 8) {
232      strcat(out, owl_util_color_to_string(f->bgcolor));
233    }
234    else {
235      char* c = owl_sprintf("%i",f->bgcolor);
236      strcat(out, c);
237      owl_free(c);
238    }
239    strcat(out, " ");
240  }
241  if(!f->root) return;
242  owl_filterelement_print(f->root, out);
243  strcat(out, "\n");
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 buff[LINE], buff2[LINE];
250
251  owl_filter_print(a, buff);
252  owl_filter_print(b, buff2);
253
254  if (!strcmp(buff, buff2)) return(1);
255  return(0);
256}
257
258
259int owl_filter_is_toodeep(owl_filter *f)
260{
261  return owl_filterelement_is_toodeep(f, f->root);
262}
263
264void owl_filter_free(owl_filter *f)
265{
266  if(f->root) {
267    owl_filterelement_free(f->root);
268    owl_free(f->root);
269  }
270  if (f->name) owl_free(f->name);
271}
272
273/**************************************************************************/
274/************************* REGRESSION TESTS *******************************/
275/**************************************************************************/
276
277#ifdef OWL_INCLUDE_REG_TESTS
278
279int owl_filter_test_string(char * filt, owl_message *m, int shouldmatch) /* noproto */ {
280  owl_filter f;
281  int ok;
282  int failed = 0;
283  if(owl_filter_init_fromstring(&f, "test-filter", filt)) {
284    printf("\tFAIL: parse %s\n", filt);
285    failed = 1;
286    goto out;
287  }
288  ok = owl_filter_message_match(&f, m);
289  if((shouldmatch && !ok) || (!shouldmatch && ok)) {
290    printf("\tFAIL: match %s (got %d, expected %d)\n", filt, ok, shouldmatch);
291    failed = 1;
292  }
293 out:
294  owl_filter_free(&f);
295  if(!failed) {
296    printf("\tok  : %s %s\n", shouldmatch ? "matches" : "doesn't match", filt);
297  }
298  return failed;
299}
300
301
302#include "test.h"
303
304
305int owl_filter_regtest(void) {
306  owl_list_create(&(g.filterlist));
307  int numfailed=0;
308  owl_message m;
309  owl_message_init(&m);
310  owl_message_set_type_zephyr(&m);
311  owl_message_set_direction_in(&m);
312  owl_message_set_class(&m, "owl");
313  owl_message_set_instance(&m, "tester");
314  owl_message_set_sender(&m, "owl-user");
315  owl_message_set_recipient(&m, "joe");
316  owl_message_set_attribute(&m, "foo", "bar");
317
318#define TEST_FILTER(f, e) numfailed += owl_filter_test_string(f, &m, e)
319
320
321  TEST_FILTER("true", 1);
322  TEST_FILTER("false", 0);
323  TEST_FILTER("( true )", 1);
324  TEST_FILTER("not false", 1);
325  TEST_FILTER("( true ) or ( false )", 1);
326  TEST_FILTER("true and false", 0);
327  TEST_FILTER("( true or true ) or ( ( false ) )", 1);
328
329  TEST_FILTER("class owl", 1);
330  TEST_FILTER("class ^owl$", 1);
331  TEST_FILTER("instance test", 1);
332  TEST_FILTER("instance ^test$", 0);
333  TEST_FILTER("instance ^tester$", 1);
334
335  TEST_FILTER("foo bar", 1);
336  TEST_FILTER("class owl and instance tester", 1);
337  TEST_FILTER("type ^zephyr$ and direction ^in$ and ( class ^owl$ or instance ^owl$ )", 1);
338
339  // Order of operations and precedence
340  TEST_FILTER("not true or false", 0);
341  TEST_FILTER("true or true and false", 0);
342  TEST_FILTER("true and true and false or true", 1);
343  TEST_FILTER("false and false or true", 1);
344  TEST_FILTER("true and false or false", 0);
345
346  owl_filter f1, f2, f3, f4;
347
348  owl_filter_init_fromstring(&f1, "f1", "class owl");
349  owl_global_add_filter(&g, &f1);
350  TEST_FILTER("filter f1", 1);
351
352  // Test recursion prevention
353  FAIL_UNLESS("self reference", owl_filter_init_fromstring(&f2, "test", "filter test"));
354
355  // mutual recursion
356  owl_filter_init_fromstring(&f3, "f3", "filter f4");
357  owl_global_add_filter(&g, &f3);
358  FAIL_UNLESS("mutual recursion",   owl_filter_init_fromstring(&f4, "f4", "filter f3"));
359
360  return 0;
361}
362
363
364#endif /* OWL_INCLUDE_REG_TESTS */
Note: See TracBrowser for help on using the repository browser.