#include #include "owl.h" static const char fileIdent[] = "$Id$"; #define OWL_FILTER_MAXRECURSE 20 int owl_filter_init_fromstring(owl_filter *f, char *name, char *string) { char **argv; int argc, out; argv=owl_parseline(string, &argc); out=owl_filter_init(f, name, argc, argv); /* owl_parsefree(argv, argc); */ return(out); } int owl_filter_init(owl_filter *f, char *name, int argc, char **argv) { f->name=owl_strdup(name); f->polarity=0; f->color=OWL_COLOR_DEFAULT; f->cachedmsgid=-1; /* first take arguments that have to come first */ /* set the color */ if (argc>=2 && !strcmp(argv[0], "-c")) { if (owl_util_string_to_color(argv[1])==-1) { owl_function_error("The color '%s' is not available, using default.", argv[1]); } else { f->color=owl_util_string_to_color(argv[1]); } argc-=2; argv+=2; } if(!(f->root = owl_filter_parse_expression(argc, argv, NULL))) return(-1); /* Now check for recursion. */ if (owl_filter_is_toodeep(f)) { owl_function_error("Filter loop or exceeds recursion depth"); owl_filter_free(f); return(-1); } return(0); } /* A primitive expression is one without any toplevel ``and'' or ``or''s*/ static owl_filterelement * owl_filter_parse_primitive_expression(int argc, char **argv, int *next) { if(!argc) return NULL; owl_filterelement * fe = owl_malloc(sizeof(owl_filterelement)); owl_filterelement *op; owl_filterelement_create(fe); int i = 0, skip; if(!strcasecmp(argv[i], "(")) { i++; op = owl_filter_parse_expression(argc-i, argv+i, &skip); if(!op) goto err; i += skip; if(strcasecmp(argv[i++], ")")) goto err; owl_filterelement_create_group(fe, op); } else if(!strcasecmp(argv[i], "not")) { i++; op = owl_filter_parse_primitive_expression(argc-i, argv+i, &skip); if(!op) goto err; i += skip; owl_filterelement_create_not(fe, op); } else if(!strcasecmp(argv[i], "true")) { i++; owl_filterelement_create_true(fe); } else if(!strcasecmp(argv[i], "false")) { i++; owl_filterelement_create_false(fe); } else { if(argc == 1) goto err; if(!strcasecmp(*argv, "filter")) { owl_filterelement_create_filter(fe, *(argv+1)); } else if(!strcasecmp(*argv, "perl")) { owl_filterelement_create_perl(fe, *(argv+1)); } else { owl_filterelement_create_re(fe, *argv, *(argv+1)); } i += 2; } if(next) { *next = i; } else if(i != argc) { goto err; } return fe; err: owl_filterelement_free(fe); owl_free(fe); return NULL; } owl_filterelement * owl_filter_parse_expression(int argc, char **argv, int *next) { int i = 0, skip; owl_filterelement * op1 = NULL, * op2 = NULL; op1 = owl_filter_parse_primitive_expression(argc-i, argv+i, &skip); i += skip; if(!op1) goto err; while(i < argc) { if(strcasecmp(argv[i], "and") && strcasecmp(argv[i], "or")) break; op2 = owl_filter_parse_primitive_expression(argc-i-1, argv+i+1, &skip); if(!op2) goto err; owl_filterelement * tmp = owl_malloc(sizeof(owl_filterelement)); if(!strcasecmp(argv[i], "and")) { owl_filterelement_create_and(tmp, op1, op2); } else { owl_filterelement_create_or(tmp, op1, op2); } op1 = tmp; op2 = NULL; i += skip+1; } if(next) { *next = i; } else if(i != argc) { goto err; } return op1; err: if(op1) { owl_filterelement_free(op1); owl_free(op1); } return NULL; } char *owl_filter_get_name(owl_filter *f) { return(f->name); } void owl_filter_set_polarity_match(owl_filter *f) { f->polarity=0; } void owl_filter_set_polarity_unmatch(owl_filter *f) { f->polarity=1; } void owl_filter_set_color(owl_filter *f, int color) { f->color=color; } int owl_filter_get_color(owl_filter *f) { return(f->color); } void owl_filter_set_cachedmsgid(owl_filter *f, int cachedmsgid) { f->cachedmsgid=cachedmsgid; } int owl_filter_get_cachedmsgid(owl_filter *f) { return(f->cachedmsgid); } /* return 1 if the message matches the given filter, otherwise * return 0. */ int owl_filter_message_match(owl_filter *f, owl_message *m) { if(!f->root) return 0; int ret = owl_filterelement_match(f->root, m); if(f->polarity) ret = !ret; return ret; } void owl_filter_print(owl_filter *f, char *out) { strcpy(out, owl_filter_get_name(f)); strcat(out, ": "); if (f->color!=OWL_COLOR_DEFAULT) { strcat(out, "-c "); strcat(out, owl_util_color_to_string(f->color)); strcat(out, " "); } if(!f->root) return; owl_filterelement_print(f->root, out); strcat(out, "\n"); } /* Return 1 if the filters 'a' and 'b' are equivalent, 0 otherwise */ int owl_filter_equiv(owl_filter *a, owl_filter *b) { char buff[LINE], buff2[LINE]; owl_filter_print(a, buff); owl_filter_print(b, buff2); if (!strcmp(buff, buff2)) return(1); return(0); } int owl_filter_is_toodeep(owl_filter *f) { return owl_filterelement_is_toodeep(f, f->root); } void owl_filter_free(owl_filter *f) { if(f->root) { owl_filterelement_free(f->root); owl_free(f->root); } if (f->name) owl_free(f->name); } /**************************************************************************/ /************************* REGRESSION TESTS *******************************/ /**************************************************************************/ #ifdef OWL_INCLUDE_REG_TESTS int owl_filter_test_string(char * filt, owl_message *m, int shouldmatch) /* noproto */ { owl_filter f; int ok; int failed = 0; if(owl_filter_init_fromstring(&f, "test-filter", filt)) { printf("\tFAIL: parse %s\n", filt); failed = 1; goto out; } ok = owl_filter_message_match(&f, m); if((shouldmatch && !ok) || (!shouldmatch && ok)) { printf("\tFAIL: match %s (got %d, expected %d)\n", filt, ok, shouldmatch); failed = 1; } out: owl_filter_free(&f); if(!failed) { printf("\tok : %s %s\n", shouldmatch ? "matches" : "doesn't match", filt); } return failed; } #include "test.h" int owl_filter_regtest(void) { owl_list_create(&(g.filterlist)); int numfailed=0; owl_message m; owl_message_init(&m); owl_message_set_type_zephyr(&m); owl_message_set_direction_in(&m); owl_message_set_class(&m, "owl"); owl_message_set_instance(&m, "tester"); owl_message_set_sender(&m, "owl-user"); owl_message_set_recipient(&m, "joe"); owl_message_set_attribute(&m, "foo", "bar"); #define TEST_FILTER(f, e) numfailed += owl_filter_test_string(f, &m, e) TEST_FILTER("true", 1); TEST_FILTER("false", 0); TEST_FILTER("( true )", 1); TEST_FILTER("not false", 1); TEST_FILTER("( true ) or ( false )", 1); TEST_FILTER("true and false", 0); TEST_FILTER("( true or true ) or ( ( false ) )", 1); TEST_FILTER("class owl", 1); TEST_FILTER("class ^owl$", 1); TEST_FILTER("instance test", 1); TEST_FILTER("instance ^test$", 0); TEST_FILTER("instance ^tester$", 1); TEST_FILTER("foo bar", 1); TEST_FILTER("class owl and instance tester", 1); TEST_FILTER("type ^zephyr$ and direction ^in$ and ( class ^owl$ or instance ^owl$ )", 1); // Order of operations and precedence TEST_FILTER("not true or false", 0); TEST_FILTER("true or true and false", 0); TEST_FILTER("true and true and false or true", 1); TEST_FILTER("false and false or true", 1); TEST_FILTER("true and false or false", 0); owl_filter f1, f2, f3, f4; owl_filter_init_fromstring(&f1, "f1", "class owl"); owl_global_add_filter(&g, &f1); TEST_FILTER("filter f1", 1); // Test recursion prevention FAIL_UNLESS("self reference", owl_filter_init_fromstring(&f2, "test", "filter test")); // mutual recursion owl_filter_init_fromstring(&f3, "f3", "filter f4"); owl_global_add_filter(&g, &f3); FAIL_UNLESS("mutual recursion", owl_filter_init_fromstring(&f4, "f4", "filter f3")); return 0; } #endif /* OWL_INCLUDE_REG_TESTS */