[7d4fbcd] | 1 | #include <string.h> |
---|
| 2 | #include "owl.h" |
---|
| 3 | |
---|
[1aee7d9] | 4 | static const char fileIdent[] = "$Id$"; |
---|
| 5 | |
---|
[7d4fbcd] | 6 | int owl_filter_init_fromstring(owl_filter *f, char *name, char *string) { |
---|
| 7 | char **argv; |
---|
| 8 | int argc, out; |
---|
| 9 | |
---|
| 10 | argv=owl_parseline(string, &argc); |
---|
| 11 | out=owl_filter_init(f, name, argc, argv); |
---|
| 12 | /* owl_parsefree(argv, argc); */ |
---|
| 13 | return(out); |
---|
| 14 | } |
---|
| 15 | |
---|
| 16 | int owl_filter_init(owl_filter *f, char *name, int argc, char **argv) { |
---|
| 17 | int i, error; |
---|
| 18 | owl_filterelement *fe; |
---|
[65fc0900] | 19 | char *regexstr; |
---|
[7d4fbcd] | 20 | |
---|
| 21 | f->name=owl_strdup(name); |
---|
| 22 | f->polarity=0; |
---|
| 23 | f->color=OWL_COLOR_DEFAULT; |
---|
[59cf91c] | 24 | f->cachedmsgid=-1; |
---|
[7d4fbcd] | 25 | owl_list_create(&(f->fes)); |
---|
[65fc0900] | 26 | |
---|
[7d4fbcd] | 27 | /* first take arguments that have to come first */ |
---|
| 28 | /* set the color */ |
---|
| 29 | if (argc>=2 && !strcmp(argv[0], "-c")) { |
---|
| 30 | f->color=owl_util_string_to_color(argv[1]); |
---|
| 31 | argc-=2; |
---|
| 32 | argv+=2; |
---|
| 33 | } |
---|
[65fc0900] | 34 | |
---|
[7d4fbcd] | 35 | /* then deal with the expression */ |
---|
| 36 | for (i=0; i<argc; i++) { |
---|
| 37 | error=0; |
---|
| 38 | fe=owl_malloc(sizeof(owl_filterelement)); |
---|
[65fc0900] | 39 | |
---|
[7d4fbcd] | 40 | /* all the 0 argument possibilities */ |
---|
| 41 | if (!strcmp(argv[i], "(")) { |
---|
| 42 | owl_filterelement_create_openbrace(fe); |
---|
| 43 | } else if (!strcmp(argv[i], ")")) { |
---|
| 44 | owl_filterelement_create_closebrace(fe); |
---|
| 45 | } else if (!strcasecmp(argv[i], "and")) { |
---|
| 46 | owl_filterelement_create_and(fe); |
---|
| 47 | } else if (!strcasecmp(argv[i], "or")) { |
---|
| 48 | owl_filterelement_create_or(fe); |
---|
| 49 | } else if (!strcasecmp(argv[i], "not")) { |
---|
| 50 | owl_filterelement_create_not(fe); |
---|
[89426ab] | 51 | } else if (!strcasecmp(argv[i], "true")) { |
---|
| 52 | owl_filterelement_create_true(fe); |
---|
| 53 | } else if (!strcasecmp(argv[i], "false")) { |
---|
| 54 | owl_filterelement_create_false(fe); |
---|
[65fc0900] | 55 | |
---|
[7d4fbcd] | 56 | } else if (i==argc-1) { |
---|
| 57 | error=1; |
---|
| 58 | } else { |
---|
| 59 | if (!strcasecmp(argv[i], "class") || |
---|
| 60 | !strcasecmp(argv[i], "instance") || |
---|
| 61 | !strcasecmp(argv[i], "sender") || |
---|
| 62 | !strcasecmp(argv[i], "recipient") || |
---|
[75be7c0] | 63 | !strcasecmp(argv[i], "body") || |
---|
[7d4fbcd] | 64 | !strcasecmp(argv[i], "opcode") || |
---|
| 65 | !strcasecmp(argv[i], "realm") || |
---|
[4b464a4] | 66 | !strcasecmp(argv[i], "type") || |
---|
| 67 | !strcasecmp(argv[i], "direction")) { |
---|
[65fc0900] | 68 | regexstr=owl_util_substitute(argv[i+1], "%me%", ZGetSender()); |
---|
| 69 | owl_filterelement_create_re(fe, argv[i], regexstr); |
---|
| 70 | owl_free(regexstr); |
---|
[7d4fbcd] | 71 | i++; |
---|
| 72 | } else { |
---|
| 73 | error=1; |
---|
| 74 | } |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | if (!error) { |
---|
| 78 | owl_list_append_element(&(f->fes), fe); |
---|
| 79 | } else { |
---|
| 80 | owl_free(fe); |
---|
| 81 | owl_filter_free(f); |
---|
| 82 | return(-1); |
---|
| 83 | } |
---|
| 84 | |
---|
| 85 | } |
---|
| 86 | return(0); |
---|
| 87 | } |
---|
| 88 | |
---|
| 89 | char *owl_filter_get_name(owl_filter *f) { |
---|
| 90 | return(f->name); |
---|
| 91 | } |
---|
| 92 | |
---|
| 93 | void owl_filter_set_polarity_match(owl_filter *f) { |
---|
| 94 | f->polarity=0; |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | void owl_filter_set_polarity_unmatch(owl_filter *f) { |
---|
| 98 | f->polarity=1; |
---|
| 99 | } |
---|
| 100 | |
---|
| 101 | void owl_filter_set_color(owl_filter *f, int color) { |
---|
| 102 | f->color=color; |
---|
| 103 | } |
---|
| 104 | |
---|
| 105 | int owl_filter_get_color(owl_filter *f) { |
---|
| 106 | return(f->color); |
---|
| 107 | } |
---|
| 108 | |
---|
[59cf91c] | 109 | void owl_filter_set_cachedmsgid(owl_filter *f, int cachedmsgid) { |
---|
| 110 | f->cachedmsgid=cachedmsgid; |
---|
| 111 | } |
---|
| 112 | |
---|
| 113 | int owl_filter_get_cachedmsgid(owl_filter *f) { |
---|
| 114 | return(f->cachedmsgid); |
---|
| 115 | } |
---|
| 116 | |
---|
[7d4fbcd] | 117 | int owl_filter_message_match(owl_filter *f, owl_message *m) { |
---|
| 118 | int i, j, tmp; |
---|
| 119 | owl_list work_fes, *fes; |
---|
| 120 | owl_filterelement *fe; |
---|
| 121 | char *field, *match; |
---|
| 122 | |
---|
[d09e5a1] | 123 | /* create the working list of expression elements */ |
---|
[7d4fbcd] | 124 | fes=&(f->fes); |
---|
| 125 | owl_list_create(&work_fes); |
---|
| 126 | j=owl_list_get_size(fes); |
---|
| 127 | for (i=0; i<j; i++) { |
---|
| 128 | owl_list_append_element(&work_fes, owl_list_get_element(fes, i)); |
---|
| 129 | } |
---|
| 130 | |
---|
[d09e5a1] | 131 | /* first go thru and evaluate all RE elements to true or false */ |
---|
[7d4fbcd] | 132 | match=""; |
---|
| 133 | for (i=0; i<j; i++) { |
---|
| 134 | fe=owl_list_get_element(&work_fes, i); |
---|
| 135 | if (!owl_filterelement_is_re(fe)) continue; |
---|
| 136 | field=owl_filterelement_get_field(fe); |
---|
| 137 | if (!strcasecmp(field, "class")) { |
---|
| 138 | match=owl_message_get_class(m); |
---|
| 139 | } else if (!strcasecmp(field, "instance")) { |
---|
| 140 | match=owl_message_get_instance(m); |
---|
| 141 | } else if (!strcasecmp(field, "sender")) { |
---|
| 142 | match=owl_message_get_sender(m); |
---|
| 143 | } else if (!strcasecmp(field, "recipient")) { |
---|
| 144 | match=owl_message_get_recipient(m); |
---|
[75be7c0] | 145 | } else if (!strcasecmp(field, "body")) { |
---|
| 146 | match=owl_message_get_body(m); |
---|
[7d4fbcd] | 147 | } else if (!strcasecmp(field, "opcode")) { |
---|
| 148 | match=owl_message_get_opcode(m); |
---|
| 149 | } else if (!strcasecmp(field, "realm")) { |
---|
| 150 | match=owl_message_get_realm(m); |
---|
| 151 | } else if (!strcasecmp(field, "type")) { |
---|
[3abf28b] | 152 | match=owl_message_type_to_string(m); |
---|
[4b464a4] | 153 | } else if (!strcasecmp(field, "direction")) { |
---|
| 154 | if (owl_message_is_direction_out(m)) { |
---|
| 155 | match="out"; |
---|
| 156 | } else if (owl_message_is_direction_in(m)) { |
---|
| 157 | match="in"; |
---|
| 158 | } else if (owl_message_is_direction_none(m)) { |
---|
| 159 | match="none"; |
---|
| 160 | } else { |
---|
| 161 | match=""; |
---|
| 162 | } |
---|
[7d4fbcd] | 163 | } |
---|
| 164 | |
---|
| 165 | tmp=owl_regex_compare(owl_filterelement_get_re(fe), match); |
---|
| 166 | if (!tmp) { |
---|
| 167 | owl_list_replace_element(&work_fes, i, owl_global_get_filterelement_true(&g)); |
---|
| 168 | } else { |
---|
| 169 | owl_list_replace_element(&work_fes, i, owl_global_get_filterelement_false(&g)); |
---|
| 170 | } |
---|
| 171 | } |
---|
| 172 | |
---|
[75be7c0] | 173 | /* call the recrsive helper */ |
---|
[7d4fbcd] | 174 | i=_owl_filter_message_match_recurse(f, m, &work_fes, 0, owl_list_get_size(&(f->fes))-1); |
---|
| 175 | |
---|
[d09e5a1] | 176 | /* now there will be only one TRUE / FALSE, find it among the NULL's */ |
---|
[7d4fbcd] | 177 | tmp=0; |
---|
| 178 | for (i=0; i<j; i++) { |
---|
| 179 | fe=owl_list_get_element(&work_fes, i); |
---|
| 180 | if (owl_filterelement_is_null(fe)) continue; |
---|
| 181 | if (owl_filterelement_is_true(fe)) { |
---|
| 182 | tmp=1; |
---|
| 183 | break; |
---|
| 184 | } |
---|
| 185 | if (owl_filterelement_is_false(fe)) { |
---|
| 186 | tmp=0; |
---|
| 187 | break; |
---|
| 188 | } |
---|
| 189 | } |
---|
| 190 | |
---|
[d09e5a1] | 191 | /* reverse the answer if negative polarity is in use */ |
---|
| 192 | if (f->polarity) tmp=!tmp; |
---|
| 193 | |
---|
[7d4fbcd] | 194 | owl_list_free_simple(&work_fes); |
---|
| 195 | return(tmp); |
---|
| 196 | } |
---|
| 197 | |
---|
| 198 | int _owl_filter_message_match_recurse(owl_filter *f, owl_message *m, owl_list *fes, int start, int end) { |
---|
| 199 | int a=0, b=0, i, x, y, z, score, ret, type; |
---|
| 200 | owl_filterelement *fe, *tmpfe=NULL; |
---|
| 201 | |
---|
[d09e5a1] | 202 | /* Deal with parens first. */ |
---|
[7d4fbcd] | 203 | for (i=0; i<OWL_FILTER_MAX_DEPTH; i++) { |
---|
[d09e5a1] | 204 | /* Find first open paren and matching close paren, store in x, y */ |
---|
[7d4fbcd] | 205 | score=x=y=0; |
---|
| 206 | for (i=start; i<=end; i++) { |
---|
| 207 | fe=owl_list_get_element(fes, i); |
---|
| 208 | if (owl_filterelement_is_openbrace(fe)) { |
---|
| 209 | if (score==0) x=i; |
---|
| 210 | score++; |
---|
| 211 | } else if (owl_filterelement_is_closebrace(fe)) { |
---|
| 212 | score--; |
---|
| 213 | if (score<0) { |
---|
| 214 | /* unblanaced parens */ |
---|
| 215 | return(-1); |
---|
| 216 | } else if (score==0) { |
---|
| 217 | y=i; /* this is the matching close paren */ |
---|
| 218 | break; |
---|
| 219 | } |
---|
| 220 | } |
---|
| 221 | } |
---|
| 222 | if (score>0) { |
---|
| 223 | /* unblanaced parens */ |
---|
| 224 | return(-1); |
---|
| 225 | } |
---|
| 226 | |
---|
[d09e5a1] | 227 | /* Simply the parens by removing them and evaluating what was in between */ |
---|
[7d4fbcd] | 228 | if (y>0) { |
---|
| 229 | /* null out the parens */ |
---|
| 230 | owl_list_replace_element(fes, x, owl_global_get_filterelement_null(&g)); |
---|
| 231 | owl_list_replace_element(fes, y, owl_global_get_filterelement_null(&g)); |
---|
| 232 | |
---|
[d09e5a1] | 233 | /* evaluate expression in between */ |
---|
[7d4fbcd] | 234 | ret=_owl_filter_message_match_recurse(f, m, fes, x+1, y-1); |
---|
| 235 | if (ret<0) return(-1); |
---|
| 236 | |
---|
| 237 | /* there may be more, so we continue */ |
---|
| 238 | continue; |
---|
| 239 | } else { |
---|
| 240 | /* otherwise we're done with this part */ |
---|
| 241 | break; |
---|
| 242 | } |
---|
| 243 | } |
---|
| 244 | if (i==OWL_FILTER_MAX_DEPTH) { |
---|
[d09e5a1] | 245 | /* hit the saftey limit, consider it invalid */ |
---|
[7d4fbcd] | 246 | return(-1); |
---|
| 247 | } |
---|
| 248 | |
---|
[d09e5a1] | 249 | /* Find AND / OR / NOT. |
---|
| 250 | * For binary expressions (AND/OR): |
---|
| 251 | * "type" is 1 |
---|
| 252 | * "x" will index first val, "y" the operator and "z" the second val |
---|
| 253 | * For unary expressions (NOT): |
---|
| 254 | * "type" is 2 |
---|
| 255 | * "x" will index the operator, "y" the value |
---|
| 256 | * "score" tallys how many expression elements have been found so far |
---|
| 257 | */ |
---|
[7d4fbcd] | 258 | for (i=0; i<OWL_FILTER_MAX_DEPTH; i++) { |
---|
| 259 | type=score=x=y=z=0; |
---|
| 260 | for (i=start; i<=end; i++) { |
---|
| 261 | fe=owl_list_get_element(fes, i); |
---|
| 262 | if (owl_filterelement_is_null(fe)) continue; |
---|
| 263 | if (score==0) { |
---|
| 264 | if (owl_filterelement_is_value(fe)) { |
---|
| 265 | x=i; |
---|
| 266 | score=1; |
---|
| 267 | type=1; |
---|
| 268 | } else if (owl_filterelement_is_not(fe)) { |
---|
| 269 | x=i; |
---|
| 270 | score=1; |
---|
| 271 | type=2; |
---|
| 272 | } |
---|
| 273 | } else if (score==1) { |
---|
| 274 | if (type==1) { |
---|
| 275 | if (owl_filterelement_is_and(fe) || owl_filterelement_is_or(fe)) { |
---|
| 276 | score=2; |
---|
| 277 | y=i; |
---|
| 278 | } else { |
---|
[d09e5a1] | 279 | /* it's not a valid binary expression */ |
---|
[7d4fbcd] | 280 | x=y=z=score=0; |
---|
| 281 | } |
---|
| 282 | } else if (type==2) { |
---|
| 283 | if (owl_filterelement_is_value(fe)) { |
---|
[d09e5a1] | 284 | /* valid unary expression, we're done */ |
---|
[7d4fbcd] | 285 | y=i; |
---|
| 286 | break; |
---|
| 287 | } |
---|
| 288 | } |
---|
| 289 | } else if (score==2) { |
---|
| 290 | if (owl_filterelement_is_value(fe)) { |
---|
[d09e5a1] | 291 | /* valid binary expression, we're done */ |
---|
[7d4fbcd] | 292 | z=i; |
---|
| 293 | break; |
---|
| 294 | } else { |
---|
| 295 | x=y=z=score=0; |
---|
| 296 | } |
---|
| 297 | } |
---|
| 298 | } |
---|
| 299 | |
---|
[d09e5a1] | 300 | /* simplify AND / OR */ |
---|
[7d4fbcd] | 301 | if ((type==1) && (z>0)) { |
---|
| 302 | fe=owl_list_get_element(fes, x); |
---|
| 303 | if (owl_filterelement_is_true(fe)) { |
---|
| 304 | a=1; |
---|
| 305 | } else if (owl_filterelement_is_false(fe)) { |
---|
| 306 | a=0; |
---|
| 307 | } |
---|
| 308 | |
---|
| 309 | fe=owl_list_get_element(fes, z); |
---|
| 310 | if (owl_filterelement_is_true(fe)) { |
---|
| 311 | b=1; |
---|
| 312 | } else if (owl_filterelement_is_false(fe)) { |
---|
| 313 | b=0; |
---|
| 314 | } |
---|
| 315 | |
---|
| 316 | fe=owl_list_get_element(fes, y); |
---|
| 317 | if (owl_filterelement_is_and(fe)) { |
---|
| 318 | if (a && b) { |
---|
| 319 | tmpfe=owl_global_get_filterelement_true(&g); |
---|
| 320 | } else { |
---|
| 321 | tmpfe=owl_global_get_filterelement_false(&g); |
---|
| 322 | } |
---|
| 323 | } else if (owl_filterelement_is_or(fe)) { |
---|
| 324 | if (a || b) { |
---|
| 325 | tmpfe=owl_global_get_filterelement_true(&g); |
---|
| 326 | } else { |
---|
| 327 | tmpfe=owl_global_get_filterelement_false(&g); |
---|
| 328 | } |
---|
| 329 | } |
---|
| 330 | owl_list_replace_element(fes, x, owl_global_get_filterelement_null(&g)); |
---|
| 331 | owl_list_replace_element(fes, y, tmpfe); |
---|
| 332 | owl_list_replace_element(fes, z, owl_global_get_filterelement_null(&g)); |
---|
[d09e5a1] | 333 | } else if ((type==2) && (y>0)) { |
---|
| 334 | /* simplify NOT */ |
---|
[7d4fbcd] | 335 | fe=owl_list_get_element(fes, y); |
---|
| 336 | owl_list_replace_element(fes, x, owl_global_get_filterelement_null(&g)); |
---|
| 337 | if (owl_filterelement_is_false(fe)) { |
---|
| 338 | owl_list_replace_element(fes, y, owl_global_get_filterelement_true(&g)); |
---|
| 339 | } else { |
---|
| 340 | owl_list_replace_element(fes, y, owl_global_get_filterelement_false(&g)); |
---|
| 341 | } |
---|
| 342 | } else { |
---|
| 343 | break; |
---|
| 344 | } |
---|
| 345 | } |
---|
| 346 | return(0); |
---|
| 347 | |
---|
| 348 | } |
---|
| 349 | |
---|
| 350 | void owl_filter_print(owl_filter *f, char *out) { |
---|
| 351 | int i, j; |
---|
| 352 | owl_filterelement *fe; |
---|
| 353 | char *tmp; |
---|
| 354 | |
---|
| 355 | strcpy(out, owl_filter_get_name(f)); |
---|
| 356 | strcat(out, ": "); |
---|
| 357 | |
---|
| 358 | if (f->color!=OWL_COLOR_DEFAULT) { |
---|
| 359 | strcat(out, "-c "); |
---|
| 360 | strcat(out, owl_util_color_to_string(f->color)); |
---|
| 361 | strcat(out, " "); |
---|
| 362 | } |
---|
| 363 | |
---|
| 364 | j=owl_list_get_size(&(f->fes)); |
---|
| 365 | for (i=0; i<j; i++) { |
---|
| 366 | fe=owl_list_get_element(&(f->fes), i); |
---|
| 367 | tmp=owl_filterelement_to_string(fe); |
---|
| 368 | strcat(out, tmp); |
---|
| 369 | owl_free(tmp); |
---|
| 370 | } |
---|
| 371 | strcat(out, "\n"); |
---|
| 372 | } |
---|
| 373 | |
---|
| 374 | int owl_filter_equiv(owl_filter *a, owl_filter *b) { |
---|
| 375 | char buff[LINE], buff2[LINE]; |
---|
| 376 | |
---|
| 377 | owl_filter_print(a, buff); |
---|
| 378 | owl_filter_print(b, buff2); |
---|
| 379 | |
---|
| 380 | if (!strcmp(buff, buff2)) return(1); |
---|
| 381 | return(0); |
---|
| 382 | } |
---|
| 383 | |
---|
| 384 | void owl_filter_free(owl_filter *f) { |
---|
| 385 | void (*func)(); |
---|
| 386 | |
---|
| 387 | func=&owl_filterelement_free; |
---|
| 388 | |
---|
| 389 | if (f->name) owl_free(f->name); |
---|
| 390 | owl_list_free_all(&(f->fes), func); |
---|
| 391 | } |
---|