[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 | |
---|
| 123 | /* create the working list */ |
---|
| 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 | |
---|
| 131 | /* first go thru and turn all RE elements into true or false */ |
---|
| 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")) { |
---|
[4b464a4] | 152 | if (owl_message_is_type_zephyr(m)) { |
---|
[7d4fbcd] | 153 | match="zephyr"; |
---|
[4b464a4] | 154 | } else if (owl_message_is_type_admin(m)) { |
---|
[7d4fbcd] | 155 | match="admin"; |
---|
| 156 | } else { |
---|
| 157 | match=""; |
---|
| 158 | } |
---|
[4b464a4] | 159 | } else if (!strcasecmp(field, "direction")) { |
---|
| 160 | if (owl_message_is_direction_out(m)) { |
---|
| 161 | match="out"; |
---|
| 162 | } else if (owl_message_is_direction_in(m)) { |
---|
| 163 | match="in"; |
---|
| 164 | } else if (owl_message_is_direction_none(m)) { |
---|
| 165 | match="none"; |
---|
| 166 | } else { |
---|
| 167 | match=""; |
---|
| 168 | } |
---|
[7d4fbcd] | 169 | } |
---|
| 170 | |
---|
| 171 | tmp=owl_regex_compare(owl_filterelement_get_re(fe), match); |
---|
| 172 | if (!tmp) { |
---|
| 173 | owl_list_replace_element(&work_fes, i, owl_global_get_filterelement_true(&g)); |
---|
| 174 | } else { |
---|
| 175 | owl_list_replace_element(&work_fes, i, owl_global_get_filterelement_false(&g)); |
---|
| 176 | } |
---|
| 177 | } |
---|
| 178 | |
---|
| 179 | |
---|
[75be7c0] | 180 | /* call the recrsive helper */ |
---|
[7d4fbcd] | 181 | i=_owl_filter_message_match_recurse(f, m, &work_fes, 0, owl_list_get_size(&(f->fes))-1); |
---|
| 182 | |
---|
| 183 | tmp=0; |
---|
| 184 | /* now we should have one value */ |
---|
| 185 | for (i=0; i<j; i++) { |
---|
| 186 | fe=owl_list_get_element(&work_fes, i); |
---|
| 187 | if (owl_filterelement_is_null(fe)) continue; |
---|
| 188 | if (owl_filterelement_is_true(fe)) { |
---|
| 189 | tmp=1; |
---|
| 190 | break; |
---|
| 191 | } |
---|
| 192 | if (owl_filterelement_is_false(fe)) { |
---|
| 193 | tmp=0; |
---|
| 194 | break; |
---|
| 195 | } |
---|
| 196 | } |
---|
| 197 | |
---|
| 198 | if (f->polarity) { |
---|
| 199 | tmp=!tmp; |
---|
| 200 | } |
---|
| 201 | owl_list_free_simple(&work_fes); |
---|
| 202 | return(tmp); |
---|
| 203 | } |
---|
| 204 | |
---|
| 205 | int _owl_filter_message_match_recurse(owl_filter *f, owl_message *m, owl_list *fes, int start, int end) { |
---|
| 206 | int a=0, b=0, i, x, y, z, score, ret, type; |
---|
| 207 | owl_filterelement *fe, *tmpfe=NULL; |
---|
| 208 | |
---|
| 209 | /* deal with parens first */ |
---|
| 210 | for (i=0; i<OWL_FILTER_MAX_DEPTH; i++) { |
---|
| 211 | score=x=y=0; |
---|
| 212 | for (i=start; i<=end; i++) { |
---|
| 213 | fe=owl_list_get_element(fes, i); |
---|
| 214 | if (owl_filterelement_is_openbrace(fe)) { |
---|
| 215 | if (score==0) x=i; |
---|
| 216 | score++; |
---|
| 217 | } else if (owl_filterelement_is_closebrace(fe)) { |
---|
| 218 | score--; |
---|
| 219 | if (score<0) { |
---|
| 220 | /* unblanaced parens */ |
---|
| 221 | return(-1); |
---|
| 222 | } else if (score==0) { |
---|
| 223 | y=i; /* this is the matching close paren */ |
---|
| 224 | break; |
---|
| 225 | } |
---|
| 226 | } |
---|
| 227 | } |
---|
| 228 | if (score>0) { |
---|
| 229 | /* unblanaced parens */ |
---|
| 230 | return(-1); |
---|
| 231 | } |
---|
| 232 | |
---|
| 233 | if (y>0) { |
---|
| 234 | /* null out the parens */ |
---|
| 235 | owl_list_replace_element(fes, x, owl_global_get_filterelement_null(&g)); |
---|
| 236 | owl_list_replace_element(fes, y, owl_global_get_filterelement_null(&g)); |
---|
| 237 | |
---|
| 238 | /* simplify the part that was in between */ |
---|
| 239 | ret=_owl_filter_message_match_recurse(f, m, fes, x+1, y-1); |
---|
| 240 | if (ret<0) return(-1); |
---|
| 241 | |
---|
| 242 | /* there may be more, so we continue */ |
---|
| 243 | continue; |
---|
| 244 | } else { |
---|
| 245 | /* otherwise we're done with this part */ |
---|
| 246 | break; |
---|
| 247 | } |
---|
| 248 | } |
---|
| 249 | if (i==OWL_FILTER_MAX_DEPTH) { |
---|
| 250 | return(-1); |
---|
| 251 | } |
---|
| 252 | |
---|
| 253 | /* and / or / not */ |
---|
| 254 | for (i=0; i<OWL_FILTER_MAX_DEPTH; i++) { |
---|
| 255 | type=score=x=y=z=0; |
---|
| 256 | for (i=start; i<=end; i++) { |
---|
| 257 | fe=owl_list_get_element(fes, i); |
---|
| 258 | if (owl_filterelement_is_null(fe)) continue; |
---|
| 259 | if (score==0) { |
---|
| 260 | if (owl_filterelement_is_value(fe)) { |
---|
| 261 | x=i; |
---|
| 262 | score=1; |
---|
| 263 | type=1; |
---|
| 264 | } else if (owl_filterelement_is_not(fe)) { |
---|
| 265 | x=i; |
---|
| 266 | score=1; |
---|
| 267 | type=2; |
---|
| 268 | } |
---|
| 269 | } else if (score==1) { |
---|
| 270 | if (type==1) { |
---|
| 271 | if (owl_filterelement_is_and(fe) || owl_filterelement_is_or(fe)) { |
---|
| 272 | score=2; |
---|
| 273 | y=i; |
---|
| 274 | } else { |
---|
| 275 | x=y=z=score=0; |
---|
| 276 | } |
---|
| 277 | } else if (type==2) { |
---|
| 278 | if (owl_filterelement_is_value(fe)) { |
---|
| 279 | /* it's a valid "NOT expr" */ |
---|
| 280 | y=i; |
---|
| 281 | break; |
---|
| 282 | } |
---|
| 283 | } |
---|
| 284 | } else if (score==2) { |
---|
| 285 | if (owl_filterelement_is_value(fe)) { |
---|
| 286 | /* yes, it's a good match */ |
---|
| 287 | z=i; |
---|
| 288 | break; |
---|
| 289 | } else { |
---|
| 290 | x=y=z=score=0; |
---|
| 291 | } |
---|
| 292 | } |
---|
| 293 | } |
---|
| 294 | |
---|
| 295 | /* process and / or */ |
---|
| 296 | if ((type==1) && (z>0)) { |
---|
| 297 | fe=owl_list_get_element(fes, x); |
---|
| 298 | if (owl_filterelement_is_true(fe)) { |
---|
| 299 | a=1; |
---|
| 300 | } else if (owl_filterelement_is_false(fe)) { |
---|
| 301 | a=0; |
---|
| 302 | } |
---|
| 303 | |
---|
| 304 | fe=owl_list_get_element(fes, z); |
---|
| 305 | if (owl_filterelement_is_true(fe)) { |
---|
| 306 | b=1; |
---|
| 307 | } else if (owl_filterelement_is_false(fe)) { |
---|
| 308 | b=0; |
---|
| 309 | } |
---|
| 310 | |
---|
| 311 | fe=owl_list_get_element(fes, y); |
---|
| 312 | if (owl_filterelement_is_and(fe)) { |
---|
| 313 | if (a && b) { |
---|
| 314 | tmpfe=owl_global_get_filterelement_true(&g); |
---|
| 315 | } else { |
---|
| 316 | tmpfe=owl_global_get_filterelement_false(&g); |
---|
| 317 | } |
---|
| 318 | } else if (owl_filterelement_is_or(fe)) { |
---|
| 319 | if (a || b) { |
---|
| 320 | tmpfe=owl_global_get_filterelement_true(&g); |
---|
| 321 | } else { |
---|
| 322 | tmpfe=owl_global_get_filterelement_false(&g); |
---|
| 323 | } |
---|
| 324 | } |
---|
| 325 | owl_list_replace_element(fes, x, owl_global_get_filterelement_null(&g)); |
---|
| 326 | owl_list_replace_element(fes, y, tmpfe); |
---|
| 327 | owl_list_replace_element(fes, z, owl_global_get_filterelement_null(&g)); |
---|
| 328 | } else if ((type==2) && (y>0)) { /* process NOT */ |
---|
| 329 | fe=owl_list_get_element(fes, y); |
---|
| 330 | owl_list_replace_element(fes, x, owl_global_get_filterelement_null(&g)); |
---|
| 331 | if (owl_filterelement_is_false(fe)) { |
---|
| 332 | owl_list_replace_element(fes, y, owl_global_get_filterelement_true(&g)); |
---|
| 333 | } else { |
---|
| 334 | owl_list_replace_element(fes, y, owl_global_get_filterelement_false(&g)); |
---|
| 335 | } |
---|
| 336 | } else { |
---|
| 337 | break; |
---|
| 338 | } |
---|
| 339 | } |
---|
| 340 | return(0); |
---|
| 341 | |
---|
| 342 | } |
---|
| 343 | |
---|
| 344 | void owl_filter_print(owl_filter *f, char *out) { |
---|
| 345 | int i, j; |
---|
| 346 | owl_filterelement *fe; |
---|
| 347 | char *tmp; |
---|
| 348 | |
---|
| 349 | strcpy(out, owl_filter_get_name(f)); |
---|
| 350 | strcat(out, ": "); |
---|
| 351 | |
---|
| 352 | if (f->color!=OWL_COLOR_DEFAULT) { |
---|
| 353 | strcat(out, "-c "); |
---|
| 354 | strcat(out, owl_util_color_to_string(f->color)); |
---|
| 355 | strcat(out, " "); |
---|
| 356 | } |
---|
| 357 | |
---|
| 358 | j=owl_list_get_size(&(f->fes)); |
---|
| 359 | for (i=0; i<j; i++) { |
---|
| 360 | fe=owl_list_get_element(&(f->fes), i); |
---|
| 361 | tmp=owl_filterelement_to_string(fe); |
---|
| 362 | strcat(out, tmp); |
---|
| 363 | owl_free(tmp); |
---|
| 364 | } |
---|
| 365 | strcat(out, "\n"); |
---|
| 366 | } |
---|
| 367 | |
---|
| 368 | int owl_filter_equiv(owl_filter *a, owl_filter *b) { |
---|
| 369 | char buff[LINE], buff2[LINE]; |
---|
| 370 | |
---|
| 371 | owl_filter_print(a, buff); |
---|
| 372 | owl_filter_print(b, buff2); |
---|
| 373 | |
---|
| 374 | if (!strcmp(buff, buff2)) return(1); |
---|
| 375 | return(0); |
---|
| 376 | } |
---|
| 377 | |
---|
| 378 | void owl_filter_free(owl_filter *f) { |
---|
| 379 | void (*func)(); |
---|
| 380 | |
---|
| 381 | func=&owl_filterelement_free; |
---|
| 382 | |
---|
| 383 | if (f->name) owl_free(f->name); |
---|
| 384 | owl_list_free_all(&(f->fes), func); |
---|
| 385 | } |
---|