source: filterelement.c @ 20eb22c

barnowl_perlaimdebianowlrelease-1.10release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since 20eb22c was 32eed98, checked in by Erik Nygren <nygren@mit.edu>, 21 years ago
2.1.3-pre-2 Added perl filter elements. Similar to having "filter <subfilter>" in a filter, you may also have "perl <functionname>" where <functionname> is passed an owl::Message object and returns 0 or 1 depending on whether the message matches that element of the filter. [Note: currently this overloads the filtername element of the filterelement structure to store the function name. Perhaps we should change the name of this element to something more general to avoid confusion?]
  • Property mode set to 100644
File size: 5.2 KB
Line 
1#include "owl.h"
2
3static const char fileIdent[] = "$Id$";
4
5#define OWL_FILTERELEMENT_NULL        0
6#define OWL_FILTERELEMENT_TRUE        1
7#define OWL_FILTERELEMENT_FALSE       2
8#define OWL_FILTERELEMENT_OPENBRACE   3
9#define OWL_FILTERELEMENT_CLOSEBRACE  4
10#define OWL_FILTERELEMENT_AND         5
11#define OWL_FILTERELEMENT_OR          6
12#define OWL_FILTERELEMENT_NOT         7
13#define OWL_FILTERELEMENT_RE          8
14#define OWL_FILTERELEMENT_FILTER      9
15#define OWL_FILTERELEMENT_PERL       10
16
17void owl_filterelement_create_null(owl_filterelement *fe)
18{
19  fe->type=OWL_FILTERELEMENT_NULL;
20  fe->field=NULL;
21  fe->filtername=NULL;
22}
23
24void owl_filterelement_create_openbrace(owl_filterelement *fe)
25{
26  owl_filterelement_create_null(fe);
27  fe->type=OWL_FILTERELEMENT_OPENBRACE;
28}
29
30void owl_filterelement_create_closebrace(owl_filterelement *fe)
31{
32  owl_filterelement_create_null(fe);
33  fe->type=OWL_FILTERELEMENT_CLOSEBRACE;
34}
35
36void owl_filterelement_create_and(owl_filterelement *fe)
37{
38  owl_filterelement_create_null(fe);
39  fe->type=OWL_FILTERELEMENT_AND;
40}
41
42void owl_filterelement_create_or(owl_filterelement *fe)
43{
44  owl_filterelement_create_null(fe);
45  fe->type=OWL_FILTERELEMENT_OR;
46}
47
48void owl_filterelement_create_not(owl_filterelement *fe)
49{
50  owl_filterelement_create_null(fe);
51  fe->type=OWL_FILTERELEMENT_NOT;
52}
53
54void owl_filterelement_create_true(owl_filterelement *fe)
55{
56  owl_filterelement_create_null(fe);
57  fe->type=OWL_FILTERELEMENT_TRUE;
58}
59
60void owl_filterelement_create_false(owl_filterelement *fe)
61{
62  owl_filterelement_create_null(fe);
63  fe->type=OWL_FILTERELEMENT_FALSE;
64}
65
66void owl_filterelement_create_re(owl_filterelement *fe, char *field, char *re)
67{
68  owl_filterelement_create_null(fe);
69  fe->type=OWL_FILTERELEMENT_RE;
70  fe->field=owl_strdup(field);
71  owl_regex_create(&(fe->re), re);
72}
73
74void owl_filterelement_create_filter(owl_filterelement *fe, char *name)
75{
76  owl_filterelement_create_null(fe);
77  fe->type=OWL_FILTERELEMENT_FILTER;
78  fe->filtername=owl_strdup(name);
79}
80
81void owl_filterelement_create_perl(owl_filterelement *fe, char *name)
82{
83  owl_filterelement_create_null(fe);
84  fe->type=OWL_FILTERELEMENT_PERL;
85  fe->filtername=owl_strdup(name);
86}
87
88void owl_filterelement_free(owl_filterelement *fe)
89{
90  if (fe->field) owl_free(fe->field);
91  if (fe->filtername) owl_free(fe->filtername);
92}
93
94int owl_filterelement_is_null(owl_filterelement *fe)
95{
96  if (fe->type==OWL_FILTERELEMENT_NULL) return(1);
97  return(0);
98}
99
100int owl_filterelement_is_openbrace(owl_filterelement *fe)
101{
102  if (fe->type==OWL_FILTERELEMENT_OPENBRACE) return(1);
103  return(0);
104}
105
106int owl_filterelement_is_closebrace(owl_filterelement *fe)
107{
108  if (fe->type==OWL_FILTERELEMENT_CLOSEBRACE) return(1);
109  return(0);
110}
111
112int owl_filterelement_is_and(owl_filterelement *fe)
113{
114  if (fe->type==OWL_FILTERELEMENT_AND) return(1);
115  return(0);
116}
117
118int owl_filterelement_is_or(owl_filterelement *fe)
119{
120  if (fe->type==OWL_FILTERELEMENT_OR) return(1);
121  return(0);
122}
123
124int owl_filterelement_is_not(owl_filterelement *fe)
125{
126  if (fe->type==OWL_FILTERELEMENT_NOT) return(1);
127  return(0);
128}
129
130int owl_filterelement_is_true(owl_filterelement *fe)
131{
132  if (fe->type==OWL_FILTERELEMENT_TRUE) return(1);
133  return(0);
134}
135
136int owl_filterelement_is_false(owl_filterelement *fe)
137{
138  if (fe->type==OWL_FILTERELEMENT_FALSE) return(1);
139  return(0);
140}
141
142int owl_filterelement_is_re(owl_filterelement *fe)
143{
144  if (fe->type==OWL_FILTERELEMENT_RE) return(1);
145  return(0);
146}
147
148int owl_filterelement_is_perl(owl_filterelement *fe)
149{
150  if (fe->type==OWL_FILTERELEMENT_PERL) return(1);
151  return(0);
152}
153
154owl_regex *owl_filterelement_get_re(owl_filterelement *fe)
155{
156  return(&(fe->re));
157}
158
159int owl_filterelement_is_filter(owl_filterelement *fe)
160{
161  if (fe->type==OWL_FILTERELEMENT_FILTER) return(1);
162  return(0);
163}
164
165char *owl_filterelement_get_field(owl_filterelement *fe)
166{
167  if (fe->field) return(fe->field);
168  return("unknown-field");
169}
170
171char *owl_filterelement_get_filtername(owl_filterelement *fe)
172{
173  if (fe->filtername) return(fe->filtername);
174  return("unknown-filter");
175}
176
177int owl_filterelement_is_value(owl_filterelement *fe)
178{
179  if ( (fe->type==OWL_FILTERELEMENT_TRUE) ||
180       (fe->type==OWL_FILTERELEMENT_FALSE) ||
181       (fe->type==OWL_FILTERELEMENT_RE) ||
182       (fe->type==OWL_FILTERELEMENT_PERL) ||
183       (fe->type==OWL_FILTERELEMENT_FILTER)) {
184    return(1);
185  }
186  return(0);
187}
188
189/* caller must free the return */
190char *owl_filterelement_to_string(owl_filterelement *fe)
191{
192  if (owl_filterelement_is_openbrace(fe)) {
193    return(owl_strdup("( "));
194  } else if (owl_filterelement_is_closebrace(fe)) {
195    return(owl_strdup(") "));
196  } else if (owl_filterelement_is_and(fe)) {
197    return(owl_strdup("and "));
198  } else if (owl_filterelement_is_or(fe)) {
199    return(owl_strdup("or "));
200  } else if (owl_filterelement_is_not(fe)) {
201    return(owl_strdup("not "));
202  } else if (owl_filterelement_is_true(fe)) {
203    return(owl_strdup("true "));
204  } else if (owl_filterelement_is_false(fe)) {
205    return(owl_strdup("false "));
206  } else if (owl_filterelement_is_re(fe)) {
207    return(owl_sprintf("%s %s ", fe->field, owl_regex_get_string(&(fe->re))));
208  } else if (owl_filterelement_is_filter(fe)) {
209    return(owl_sprintf("filter %s ", fe->filtername));
210  } else if (owl_filterelement_is_perl(fe)) {
211    return(owl_sprintf("perl %s ", fe->filtername));
212  }
213
214  return(owl_strdup("?"));
215}
Note: See TracBrowser for help on using the repository browser.