source: filterelement.c @ dc2ad79

barnowl_perlaimdebianrelease-1.10release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since dc2ad79 was 5616acc, checked in by Nelson Elhage <nelhage@mit.edu>, 18 years ago
Revert r598 svk mismerge
  • Property mode set to 100644
File size: 8.3 KB
RevLine 
[7d4fbcd]1#include "owl.h"
2
[1aee7d9]3static const char fileIdent[] = "$Id$";
4
[cb769bb]5static 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]52static int owl_filterelement_match_false(owl_filterelement *fe, owl_message *m)
[e187445]53{
[cb769bb]54  return 0;
[7d4fbcd]55}
56
[cb769bb]57static int owl_filterelement_match_true(owl_filterelement *fe, owl_message *m)
[e187445]58{
[cb769bb]59  return 1;
[7d4fbcd]60}
61
[cb769bb]62static 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]68static 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]81static 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]100static 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
109static 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]115static 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]121static 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
[cb769bb]126// Print methods
127
128static void owl_filterelement_print_true(owl_filterelement *fe, char *buf)
[40458b9]129{
[cb769bb]130  strcat(buf, "true");
[40458b9]131}
132
[cb769bb]133static void owl_filterelement_print_false(owl_filterelement *fe, char *buf)
[32eed98]134{
[cb769bb]135  strcat(buf, "false");
[32eed98]136}
137
[cb769bb]138static void owl_filterelement_print_re(owl_filterelement *fe, char *buf)
[e187445]139{
[cb769bb]140  strcat(buf, fe->field);
141  strcat(buf, " ");
142  strcat(buf, owl_regex_get_string(&(fe->re)));
143}
144
145static void owl_filterelement_print_filter(owl_filterelement *fe, char *buf)
146{
147  strcat(buf, "filter ");
148  strcat(buf, fe->field);
149}
150
151static void owl_filterelement_print_perl(owl_filterelement *fe, char *buf)
152{
153  strcat(buf, "perl ");
154  strcat(buf, fe->field);
[7d4fbcd]155}
156
[cb769bb]157static void owl_filterelement_print_group(owl_filterelement *fe, char *buf)
[e187445]158{
[cb769bb]159  strcat(buf, "( ");
160  owl_filterelement_print(fe->left, buf) ;
161  strcat(buf, " )");
[7d4fbcd]162}
163
[cb769bb]164static void owl_filterelement_print_or(owl_filterelement *fe, char *buf)
[e187445]165{
[cb769bb]166  owl_filterelement_print(fe->left, buf);
167  strcat(buf, " or ");
168  owl_filterelement_print(fe->right, buf);
[7d4fbcd]169}
170
[cb769bb]171static void owl_filterelement_print_and(owl_filterelement *fe, char *buf)
[e187445]172{
[cb769bb]173  owl_filterelement_print(fe->left, buf);
174  strcat(buf, " and ");
175  owl_filterelement_print(fe->right, buf);
[7d4fbcd]176}
177
[cb769bb]178static void owl_filterelement_print_not(owl_filterelement *fe, char *buf)
[e187445]179{
[cb769bb]180  strcat(buf, " not ");
181  owl_filterelement_print(fe->left, buf);
182}
183
184// Constructors
185
186void owl_filterelement_create(owl_filterelement *fe) {
187  fe->field = NULL;
188  fe->left = fe->right = NULL;
189  fe->match_message = NULL;
190  fe->print_elt = NULL;
191  owl_regex_init(&(fe->re));
[7d4fbcd]192}
193
[cb769bb]194
195void owl_filterelement_create_true(owl_filterelement *fe)
196{
197  owl_filterelement_create(fe);
198  fe->match_message = owl_filterelement_match_true;
199  fe->print_elt = owl_filterelement_print_true;
200}
201
202void owl_filterelement_create_false(owl_filterelement *fe)
[e187445]203{
[cb769bb]204  owl_filterelement_create(fe);
205  fe->match_message = owl_filterelement_match_false;
206  fe->print_elt = owl_filterelement_print_false;
[7d4fbcd]207}
208
[cb769bb]209void owl_filterelement_create_re(owl_filterelement *fe, char *field, char *re)
[e187445]210{
[cb769bb]211  owl_filterelement_create(fe);
212  fe->field=owl_strdup(field);
213  owl_regex_create(&(fe->re), re);
214  fe->match_message = owl_filterelement_match_re;
215  fe->print_elt = owl_filterelement_print_re;
[7d4fbcd]216}
217
[cb769bb]218void owl_filterelement_create_filter(owl_filterelement *fe, char *name)
[e187445]219{
[cb769bb]220  owl_filterelement_create(fe);
221  fe->field=owl_strdup(name);
222  fe->match_message = owl_filterelement_match_filter;
223  fe->print_elt = owl_filterelement_print_filter;
[7d4fbcd]224}
225
[cb769bb]226void owl_filterelement_create_perl(owl_filterelement *fe, char *name)
[e187445]227{
[cb769bb]228  owl_filterelement_create(fe);
229  fe->field=owl_strdup(name);
230  fe->match_message = owl_filterelement_match_perl;
231  fe->print_elt = owl_filterelement_print_perl;
[7d4fbcd]232}
233
[cb769bb]234void owl_filterelement_create_group(owl_filterelement *fe, owl_filterelement *in)
[e187445]235{
[cb769bb]236  owl_filterelement_create(fe);
237  fe->left = in;
238  fe->match_message = owl_filterelement_match_group;
239  fe->print_elt = owl_filterelement_print_group;
[7d4fbcd]240}
241
[cb769bb]242void owl_filterelement_create_not(owl_filterelement *fe, owl_filterelement *in)
[32eed98]243{
[cb769bb]244  owl_filterelement_create(fe);
245  fe->left = in;
246  fe->match_message = owl_filterelement_match_not;
247  fe->print_elt = owl_filterelement_print_not;
[32eed98]248}
249
[cb769bb]250void owl_filterelement_create_and(owl_filterelement *fe, owl_filterelement *lhs, owl_filterelement *rhs)
[e187445]251{
[cb769bb]252  owl_filterelement_create(fe);
253  fe->left = lhs;
254  fe->right = rhs;
255  fe->match_message = owl_filterelement_match_and;
256  fe->print_elt = owl_filterelement_print_and;
[7d4fbcd]257}
258
[cb769bb]259void owl_filterelement_create_or(owl_filterelement *fe, owl_filterelement *lhs, owl_filterelement *rhs)
[40458b9]260{
[cb769bb]261  owl_filterelement_create(fe);
262  fe->left = lhs;
263  fe->right = rhs;
264  fe->match_message = owl_filterelement_match_or;
265  fe->print_elt = owl_filterelement_print_or;
[40458b9]266}
267
[cb769bb]268int owl_filterelement_match(owl_filterelement *fe, owl_message *m)
[e187445]269{
[cb769bb]270  if(!fe) return 0;
271  if(!fe->match_message) return 0;
272  return fe->match_message(fe, m);
[40458b9]273}
274
[cb769bb]275int owl_filterelement_is_toodeep(owl_filter *f, owl_filterelement *fe)
[40458b9]276{
[cb769bb]277  int one = 1;
278  owl_list nodes;
279  owl_dict filters;
280  owl_list_create(&nodes);
281  owl_dict_create(&filters);
282 
283  owl_list_append_element(&nodes, fe);
284  owl_dict_insert_element(&filters, f->name, &one, NULL);
285  while(owl_list_get_size(&nodes)) {
286    fe = owl_list_get_element(&nodes, 0);
287    owl_list_remove_element(&nodes, 0);
288    if(fe->left) owl_list_append_element(&nodes, fe->left);
289    if(fe->right) owl_list_append_element(&nodes, fe->right);
290    if(fe->match_message == owl_filterelement_match_filter) {
291      if(owl_dict_find_element(&filters, fe->field)) return 1;
292      owl_dict_insert_element(&filters, fe->field, &one, NULL);
293      f = owl_global_get_filter(&g, fe->field);
294      if(f) owl_list_append_element(&nodes, f->root);
295    }
296  }
297
298  owl_list_free_simple(&nodes);
299  owl_dict_free_simple(&filters);
300  return 0;
[7d4fbcd]301}
302
[cb769bb]303void owl_filterelement_free(owl_filterelement *fe)
[e187445]304{
[cb769bb]305  if (fe->field) owl_free(fe->field);
306  if (fe->left) {
307    owl_filterelement_free(fe->left);
308    owl_free(fe->left);
[7d4fbcd]309  }
[cb769bb]310  if (fe->right) {
311    owl_filterelement_free(fe->right);
312    owl_free(fe->right);
[7d4fbcd]313  }
[cb769bb]314  owl_regex_free(&(fe->re));
315}
[40458b9]316
[cb769bb]317void owl_filterelement_print(owl_filterelement *fe, char *buf)
318{
319  if(!fe || !fe->print_elt) return;
320  fe->print_elt(fe, buf);
[7d4fbcd]321}
Note: See TracBrowser for help on using the repository browser.