source: filterelement.c @ cd5adec

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