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