[7d4fbcd] | 1 | #include "owl.h" |
---|
| 2 | |
---|
[1aee7d9] | 3 | static const char fileIdent[] = "$Id$"; |
---|
| 4 | |
---|
[cb769bb] | 5 | static char * owl_filterelement_get_field(owl_message *m, char * field) |
---|
| 6 | { |
---|
| 7 | char *match; |
---|
| 8 | if (!strcasecmp(field, "class")) { |
---|
| 9 | match=owl_message_get_class(m); |
---|
| 10 | } else if (!strcasecmp(field, "instance")) { |
---|
| 11 | match=owl_message_get_instance(m); |
---|
| 12 | } else if (!strcasecmp(field, "sender")) { |
---|
| 13 | match=owl_message_get_sender(m); |
---|
| 14 | } else if (!strcasecmp(field, "recipient")) { |
---|
| 15 | match=owl_message_get_recipient(m); |
---|
| 16 | } else if (!strcasecmp(field, "body")) { |
---|
| 17 | match=owl_message_get_body(m); |
---|
| 18 | } else if (!strcasecmp(field, "opcode")) { |
---|
| 19 | match=owl_message_get_opcode(m); |
---|
| 20 | } else if (!strcasecmp(field, "realm")) { |
---|
| 21 | match=owl_message_get_realm(m); |
---|
| 22 | } else if (!strcasecmp(field, "type")) { |
---|
| 23 | match=owl_message_get_type(m); |
---|
| 24 | } else if (!strcasecmp(field, "hostname")) { |
---|
| 25 | match=owl_message_get_hostname(m); |
---|
| 26 | } else if (!strcasecmp(field, "direction")) { |
---|
| 27 | if (owl_message_is_direction_out(m)) { |
---|
| 28 | match="out"; |
---|
| 29 | } else if (owl_message_is_direction_in(m)) { |
---|
| 30 | match="in"; |
---|
| 31 | } else if (owl_message_is_direction_none(m)) { |
---|
| 32 | match="none"; |
---|
| 33 | } else { |
---|
| 34 | match=""; |
---|
| 35 | } |
---|
| 36 | } else if (!strcasecmp(field, "login")) { |
---|
| 37 | if (owl_message_is_login(m)) { |
---|
| 38 | match="login"; |
---|
| 39 | } else if (owl_message_is_logout(m)) { |
---|
| 40 | match="logout"; |
---|
| 41 | } else { |
---|
| 42 | match="none"; |
---|
| 43 | } |
---|
| 44 | } else { |
---|
| 45 | match = owl_message_get_attribute_value(m,field); |
---|
| 46 | if(match == NULL) match = ""; |
---|
| 47 | } |
---|
| 48 | |
---|
| 49 | return match; |
---|
| 50 | } |
---|
[7d4fbcd] | 51 | |
---|
[cb769bb] | 52 | static int owl_filterelement_match_false(owl_filterelement *fe, owl_message *m) |
---|
[e187445] | 53 | { |
---|
[cb769bb] | 54 | return 0; |
---|
[7d4fbcd] | 55 | } |
---|
| 56 | |
---|
[cb769bb] | 57 | static int owl_filterelement_match_true(owl_filterelement *fe, owl_message *m) |
---|
[e187445] | 58 | { |
---|
[cb769bb] | 59 | return 1; |
---|
[7d4fbcd] | 60 | } |
---|
| 61 | |
---|
[cb769bb] | 62 | static int owl_filterelement_match_re(owl_filterelement *fe, owl_message *m) |
---|
[e187445] | 63 | { |
---|
[cb769bb] | 64 | char * val = owl_filterelement_get_field(m, fe->field); |
---|
| 65 | return !owl_regex_compare(&(fe->re), val); |
---|
[7d4fbcd] | 66 | } |
---|
| 67 | |
---|
[cb769bb] | 68 | static int owl_filterelement_match_filter(owl_filterelement *fe, owl_message *m) |
---|
[e187445] | 69 | { |
---|
[cb769bb] | 70 | owl_filter *subfilter; |
---|
| 71 | subfilter=owl_global_get_filter(&g, fe->field); |
---|
| 72 | if (!subfilter) { |
---|
| 73 | /* the filter does not exist, maybe because it was deleted. |
---|
| 74 | * Default to not matching |
---|
| 75 | */ |
---|
| 76 | return 0; |
---|
| 77 | } |
---|
| 78 | return owl_filter_message_match(subfilter, m); |
---|
[7d4fbcd] | 79 | } |
---|
| 80 | |
---|
[cb769bb] | 81 | static int owl_filterelement_match_perl(owl_filterelement *fe, owl_message *m) |
---|
[e187445] | 82 | { |
---|
[cb769bb] | 83 | char *subname, *perlrv; |
---|
| 84 | int tf=0; |
---|
| 85 | |
---|
| 86 | subname = fe->field; |
---|
| 87 | if (!owl_perlconfig_is_function(subname)) { |
---|
| 88 | return 0; |
---|
| 89 | } |
---|
| 90 | perlrv = owl_perlconfig_call_with_message(subname, m); |
---|
| 91 | if (perlrv) { |
---|
| 92 | if (0 == strcmp(perlrv, "1")) { |
---|
| 93 | tf=1; |
---|
| 94 | } |
---|
| 95 | owl_free(perlrv); |
---|
| 96 | } |
---|
| 97 | return tf; |
---|
[7d4fbcd] | 98 | } |
---|
| 99 | |
---|
[cb769bb] | 100 | static int owl_filterelement_match_group(owl_filterelement *fe, owl_message *m) |
---|
[e187445] | 101 | { |
---|
[cb769bb] | 102 | return owl_filterelement_match(fe->left, m); |
---|
[7d4fbcd] | 103 | } |
---|
| 104 | |
---|
[cb769bb] | 105 | /* XXX: Our boolea operators short-circuit here. The original owl did |
---|
| 106 | not. Do we care? |
---|
| 107 | */ |
---|
| 108 | |
---|
| 109 | static int owl_filterelement_match_and(owl_filterelement *fe, owl_message *m) |
---|
[e187445] | 110 | { |
---|
[cb769bb] | 111 | return owl_filterelement_match(fe->left, m) && |
---|
| 112 | owl_filterelement_match(fe->right, m); |
---|
[7d4fbcd] | 113 | } |
---|
| 114 | |
---|
[cb769bb] | 115 | static int owl_filterelement_match_or(owl_filterelement *fe, owl_message *m) |
---|
[e187445] | 116 | { |
---|
[cb769bb] | 117 | return owl_filterelement_match(fe->left, m) || |
---|
| 118 | owl_filterelement_match(fe->right, m); |
---|
[7d4fbcd] | 119 | } |
---|
| 120 | |
---|
[cb769bb] | 121 | static int owl_filterelement_match_not(owl_filterelement *fe, owl_message *m) |
---|
[e187445] | 122 | { |
---|
[cb769bb] | 123 | return !owl_filterelement_match(fe->left, m); |
---|
[7d4fbcd] | 124 | } |
---|
[5616acc] | 125 | |
---|
[af1920fd] | 126 | /* Print methods */ |
---|
[cb769bb] | 127 | |
---|
[0504f63] | 128 | static void owl_filterelement_print_true(owl_filterelement *fe, GString *buf) |
---|
[40458b9] | 129 | { |
---|
[0504f63] | 130 | g_string_append(buf, "true"); |
---|
[40458b9] | 131 | } |
---|
| 132 | |
---|
[0504f63] | 133 | static void owl_filterelement_print_false(owl_filterelement *fe, GString *buf) |
---|
[32eed98] | 134 | { |
---|
[0504f63] | 135 | g_string_append(buf, "false"); |
---|
[32eed98] | 136 | } |
---|
| 137 | |
---|
[0504f63] | 138 | static void owl_filterelement_print_re(owl_filterelement *fe, GString *buf) |
---|
[e187445] | 139 | { |
---|
[81655f8] | 140 | char *re, *q; |
---|
[0504f63] | 141 | g_string_append(buf, fe->field); |
---|
| 142 | g_string_append(buf, " "); |
---|
[81655f8] | 143 | |
---|
| 144 | re = owl_regex_get_string(&(fe->re)); |
---|
| 145 | q = owl_getquoting(re); |
---|
[0504f63] | 146 | g_string_append(buf, q); |
---|
| 147 | g_string_append(buf, re); |
---|
| 148 | g_string_append(buf, q); |
---|
[cb769bb] | 149 | } |
---|
| 150 | |
---|
[0504f63] | 151 | static void owl_filterelement_print_filter(owl_filterelement *fe, GString *buf) |
---|
[cb769bb] | 152 | { |
---|
[0504f63] | 153 | g_string_append(buf, "filter "); |
---|
| 154 | g_string_append(buf, fe->field); |
---|
[cb769bb] | 155 | } |
---|
| 156 | |
---|
[0504f63] | 157 | static void owl_filterelement_print_perl(owl_filterelement *fe, GString *buf) |
---|
[cb769bb] | 158 | { |
---|
[0504f63] | 159 | g_string_append(buf, "perl "); |
---|
| 160 | g_string_append(buf, fe->field); |
---|
[7d4fbcd] | 161 | } |
---|
| 162 | |
---|
[0504f63] | 163 | static void owl_filterelement_print_group(owl_filterelement *fe, GString *buf) |
---|
[e187445] | 164 | { |
---|
[0504f63] | 165 | g_string_append(buf, "( "); |
---|
[cb769bb] | 166 | owl_filterelement_print(fe->left, buf) ; |
---|
[0504f63] | 167 | g_string_append(buf, " )"); |
---|
[7d4fbcd] | 168 | } |
---|
| 169 | |
---|
[0504f63] | 170 | static void owl_filterelement_print_or(owl_filterelement *fe, GString *buf) |
---|
[e187445] | 171 | { |
---|
[cb769bb] | 172 | owl_filterelement_print(fe->left, buf); |
---|
[0504f63] | 173 | g_string_append(buf, " or "); |
---|
[cb769bb] | 174 | owl_filterelement_print(fe->right, buf); |
---|
[7d4fbcd] | 175 | } |
---|
| 176 | |
---|
[0504f63] | 177 | static void owl_filterelement_print_and(owl_filterelement *fe, GString *buf) |
---|
[e187445] | 178 | { |
---|
[cb769bb] | 179 | owl_filterelement_print(fe->left, buf); |
---|
[0504f63] | 180 | g_string_append(buf, " and "); |
---|
[cb769bb] | 181 | owl_filterelement_print(fe->right, buf); |
---|
[7d4fbcd] | 182 | } |
---|
| 183 | |
---|
[0504f63] | 184 | static void owl_filterelement_print_not(owl_filterelement *fe, GString *buf) |
---|
[e187445] | 185 | { |
---|
[0504f63] | 186 | g_string_append(buf, " not "); |
---|
[cb769bb] | 187 | owl_filterelement_print(fe->left, buf); |
---|
| 188 | } |
---|
| 189 | |
---|
[af1920fd] | 190 | /* Constructors */ |
---|
[cb769bb] | 191 | |
---|
| 192 | void owl_filterelement_create(owl_filterelement *fe) { |
---|
| 193 | fe->field = NULL; |
---|
| 194 | fe->left = fe->right = NULL; |
---|
| 195 | fe->match_message = NULL; |
---|
| 196 | fe->print_elt = NULL; |
---|
| 197 | owl_regex_init(&(fe->re)); |
---|
[7d4fbcd] | 198 | } |
---|
| 199 | |
---|
[cb769bb] | 200 | |
---|
| 201 | void owl_filterelement_create_true(owl_filterelement *fe) |
---|
| 202 | { |
---|
| 203 | owl_filterelement_create(fe); |
---|
| 204 | fe->match_message = owl_filterelement_match_true; |
---|
| 205 | fe->print_elt = owl_filterelement_print_true; |
---|
| 206 | } |
---|
| 207 | |
---|
| 208 | void owl_filterelement_create_false(owl_filterelement *fe) |
---|
[e187445] | 209 | { |
---|
[cb769bb] | 210 | owl_filterelement_create(fe); |
---|
| 211 | fe->match_message = owl_filterelement_match_false; |
---|
| 212 | fe->print_elt = owl_filterelement_print_false; |
---|
[7d4fbcd] | 213 | } |
---|
| 214 | |
---|
[cb769bb] | 215 | void owl_filterelement_create_re(owl_filterelement *fe, char *field, char *re) |
---|
[e187445] | 216 | { |
---|
[cb769bb] | 217 | owl_filterelement_create(fe); |
---|
| 218 | fe->field=owl_strdup(field); |
---|
| 219 | owl_regex_create(&(fe->re), re); |
---|
| 220 | fe->match_message = owl_filterelement_match_re; |
---|
| 221 | fe->print_elt = owl_filterelement_print_re; |
---|
[7d4fbcd] | 222 | } |
---|
| 223 | |
---|
[cb769bb] | 224 | void owl_filterelement_create_filter(owl_filterelement *fe, char *name) |
---|
[e187445] | 225 | { |
---|
[cb769bb] | 226 | owl_filterelement_create(fe); |
---|
| 227 | fe->field=owl_strdup(name); |
---|
| 228 | fe->match_message = owl_filterelement_match_filter; |
---|
| 229 | fe->print_elt = owl_filterelement_print_filter; |
---|
[7d4fbcd] | 230 | } |
---|
| 231 | |
---|
[cb769bb] | 232 | void owl_filterelement_create_perl(owl_filterelement *fe, char *name) |
---|
[e187445] | 233 | { |
---|
[cb769bb] | 234 | owl_filterelement_create(fe); |
---|
| 235 | fe->field=owl_strdup(name); |
---|
| 236 | fe->match_message = owl_filterelement_match_perl; |
---|
| 237 | fe->print_elt = owl_filterelement_print_perl; |
---|
[7d4fbcd] | 238 | } |
---|
| 239 | |
---|
[cb769bb] | 240 | void owl_filterelement_create_group(owl_filterelement *fe, owl_filterelement *in) |
---|
[e187445] | 241 | { |
---|
[cb769bb] | 242 | owl_filterelement_create(fe); |
---|
| 243 | fe->left = in; |
---|
| 244 | fe->match_message = owl_filterelement_match_group; |
---|
| 245 | fe->print_elt = owl_filterelement_print_group; |
---|
[7d4fbcd] | 246 | } |
---|
| 247 | |
---|
[cb769bb] | 248 | void owl_filterelement_create_not(owl_filterelement *fe, owl_filterelement *in) |
---|
[32eed98] | 249 | { |
---|
[cb769bb] | 250 | owl_filterelement_create(fe); |
---|
| 251 | fe->left = in; |
---|
| 252 | fe->match_message = owl_filterelement_match_not; |
---|
| 253 | fe->print_elt = owl_filterelement_print_not; |
---|
[32eed98] | 254 | } |
---|
| 255 | |
---|
[cb769bb] | 256 | void owl_filterelement_create_and(owl_filterelement *fe, owl_filterelement *lhs, owl_filterelement *rhs) |
---|
[e187445] | 257 | { |
---|
[cb769bb] | 258 | owl_filterelement_create(fe); |
---|
| 259 | fe->left = lhs; |
---|
| 260 | fe->right = rhs; |
---|
| 261 | fe->match_message = owl_filterelement_match_and; |
---|
| 262 | fe->print_elt = owl_filterelement_print_and; |
---|
[7d4fbcd] | 263 | } |
---|
| 264 | |
---|
[cb769bb] | 265 | void owl_filterelement_create_or(owl_filterelement *fe, owl_filterelement *lhs, owl_filterelement *rhs) |
---|
[40458b9] | 266 | { |
---|
[cb769bb] | 267 | owl_filterelement_create(fe); |
---|
| 268 | fe->left = lhs; |
---|
| 269 | fe->right = rhs; |
---|
| 270 | fe->match_message = owl_filterelement_match_or; |
---|
| 271 | fe->print_elt = owl_filterelement_print_or; |
---|
[40458b9] | 272 | } |
---|
| 273 | |
---|
[cb769bb] | 274 | int owl_filterelement_match(owl_filterelement *fe, owl_message *m) |
---|
[e187445] | 275 | { |
---|
[cb769bb] | 276 | if(!fe) return 0; |
---|
| 277 | if(!fe->match_message) return 0; |
---|
| 278 | return fe->match_message(fe, m); |
---|
[40458b9] | 279 | } |
---|
| 280 | |
---|
[cb769bb] | 281 | int owl_filterelement_is_toodeep(owl_filter *f, owl_filterelement *fe) |
---|
[40458b9] | 282 | { |
---|
[cb769bb] | 283 | int one = 1; |
---|
| 284 | owl_list nodes; |
---|
| 285 | owl_dict filters; |
---|
| 286 | owl_list_create(&nodes); |
---|
| 287 | owl_dict_create(&filters); |
---|
| 288 | |
---|
| 289 | owl_list_append_element(&nodes, fe); |
---|
| 290 | owl_dict_insert_element(&filters, f->name, &one, NULL); |
---|
| 291 | while(owl_list_get_size(&nodes)) { |
---|
| 292 | fe = owl_list_get_element(&nodes, 0); |
---|
| 293 | owl_list_remove_element(&nodes, 0); |
---|
| 294 | if(fe->left) owl_list_append_element(&nodes, fe->left); |
---|
| 295 | if(fe->right) owl_list_append_element(&nodes, fe->right); |
---|
| 296 | if(fe->match_message == owl_filterelement_match_filter) { |
---|
| 297 | if(owl_dict_find_element(&filters, fe->field)) return 1; |
---|
| 298 | owl_dict_insert_element(&filters, fe->field, &one, NULL); |
---|
| 299 | f = owl_global_get_filter(&g, fe->field); |
---|
| 300 | if(f) owl_list_append_element(&nodes, f->root); |
---|
| 301 | } |
---|
| 302 | } |
---|
| 303 | |
---|
| 304 | owl_list_free_simple(&nodes); |
---|
| 305 | owl_dict_free_simple(&filters); |
---|
| 306 | return 0; |
---|
[7d4fbcd] | 307 | } |
---|
| 308 | |
---|
[cb769bb] | 309 | void owl_filterelement_free(owl_filterelement *fe) |
---|
[e187445] | 310 | { |
---|
[cb769bb] | 311 | if (fe->field) owl_free(fe->field); |
---|
| 312 | if (fe->left) { |
---|
| 313 | owl_filterelement_free(fe->left); |
---|
| 314 | owl_free(fe->left); |
---|
[7d4fbcd] | 315 | } |
---|
[cb769bb] | 316 | if (fe->right) { |
---|
| 317 | owl_filterelement_free(fe->right); |
---|
| 318 | owl_free(fe->right); |
---|
[7d4fbcd] | 319 | } |
---|
[cb769bb] | 320 | owl_regex_free(&(fe->re)); |
---|
| 321 | } |
---|
[40458b9] | 322 | |
---|
[0504f63] | 323 | void owl_filterelement_print(owl_filterelement *fe, GString *buf) |
---|
[cb769bb] | 324 | { |
---|
| 325 | if(!fe || !fe->print_elt) return; |
---|
| 326 | fe->print_elt(fe, buf); |
---|
[7d4fbcd] | 327 | } |
---|