Mercurial > noffle
annotate src/filter.c @ 198:79e324213734 noffle
[svn] * packages/redhat/noffle.spec: Update to version to 1.1-1 and fix up some
items causing RPM3 vs RPM4 problems.
* configure,configure.in: Bump version to 1.1.1-unstable-develop, and add
--with-spooldir and --with-configfile options (idea due to Mirko Liss).
author | bears |
---|---|
date | Tue, 06 Nov 2001 21:22:19 +0000 |
parents | 24d4cd032da5 |
children | 21200ce10e68 |
rev | line source |
---|---|
128 | 1 /* |
2 filter.c | |
3 | |
4 Article filtering. | |
5 | |
197
24d4cd032da5
[svn] * AUTHORS,INSTALL,NEWS,README,TODO,docs/NOTES,src/client.c,src/protocol.c,
bears
parents:
194
diff
changeset
|
6 $Id: filter.c 316 2001-10-31 11:44:53Z bears $ |
128 | 7 */ |
8 | |
9 #if HAVE_CONFIG_H | |
10 #include <config.h> | |
11 #endif | |
12 | |
13 #include <ctype.h> | |
14 #include "common.h" | |
197
24d4cd032da5
[svn] * AUTHORS,INSTALL,NEWS,README,TODO,docs/NOTES,src/client.c,src/protocol.c,
bears
parents:
194
diff
changeset
|
15 #include "filter.h" |
128 | 16 #include "itemlist.h" |
17 #include "log.h" | |
18 #include "wildmat.h" | |
19 | |
20 struct | |
21 { | |
22 int nFilters; | |
23 int maxFilters; | |
24 const Filter **filters; | |
25 Bool needGroups; | |
26 } filter = { 0, 0, NULL, FALSE }; | |
27 | |
28 static unsigned long | |
29 countGroups( const char *grps ) | |
30 { | |
31 unsigned long res; | |
32 | |
33 res = 1; | |
34 while ( *grps != '\0' ) | |
35 { | |
36 if ( *grps == ',' ) | |
37 res++; | |
38 grps++; | |
39 } | |
40 | |
41 return res; | |
42 } | |
43 static unsigned long | |
44 countRefs( const char *refs ) | |
45 { | |
46 unsigned long res; | |
47 Bool inRef; | |
48 | |
49 res = 0; | |
50 inRef = FALSE; | |
51 | |
52 while ( *refs != '\0' ) | |
53 { | |
54 if ( inRef ) | |
55 { | |
56 if ( *refs == '>' ) | |
57 { | |
58 inRef = FALSE; | |
59 res++; | |
60 } | |
61 } | |
62 else if ( *refs == '<' ) | |
63 inRef = TRUE; | |
64 refs++; | |
65 } | |
66 | |
67 return res; | |
68 } | |
69 | |
70 /* Check a single rule to see if it passes. */ | |
71 static Bool | |
72 checkRule( const char *thisGrp, const char *newsgroups, | |
73 const Over *ov, const FilterRule *r ) | |
74 { | |
75 unsigned long ul; | |
76 ItemList *grps; | |
77 const char *p; | |
78 | |
79 switch( r->type ) | |
80 { | |
81 case RULE_NEWSGROUP: | |
82 if ( Wld_match( thisGrp, r->data.grp ) ) | |
83 return TRUE; | |
84 if ( newsgroups != NULL ) | |
85 { | |
86 grps = new_Itl( newsgroups, " ,\t" ); | |
87 for ( p = Itl_first( grps ); p != NULL; p = Itl_next( grps ) ) | |
88 if ( Wld_match( p, r->data.grp ) ) | |
89 return TRUE; | |
90 del_Itl( grps ); | |
91 } | |
92 return FALSE; | |
93 | |
94 case RULE_SUBJECT: | |
95 return ( regexec( &r->data.regex, Ov_subj( ov ), 0, NULL, 0 ) == 0 ); | |
96 | |
97 case RULE_FROM: | |
98 return ( regexec( &r->data.regex, Ov_from( ov ), 0, NULL, 0 ) == 0 ); | |
99 | |
100 case RULE_BYTES_LT: | |
101 return ( Ov_bytes( ov ) < r->data.amount ); | |
102 | |
103 case RULE_BYTES_EQ: | |
104 return ( Ov_bytes( ov ) == r->data.amount ); | |
105 | |
106 case RULE_BYTES_GT: | |
107 return ( Ov_bytes( ov ) > r->data.amount ); | |
108 | |
109 case RULE_LINES_LT: | |
110 return ( Ov_lines( ov ) < r->data.amount ); | |
111 | |
112 case RULE_LINES_EQ: | |
113 return ( Ov_lines( ov ) == r->data.amount ); | |
114 | |
115 case RULE_LINES_GT: | |
116 return ( Ov_lines( ov ) > r->data.amount ); | |
117 | |
118 case RULE_MSGID: | |
119 return ( regexec( &r->data.regex, Ov_msgId( ov ), 0, NULL, 0 ) == 0 ); | |
120 | |
121 case RULE_NOREFS_LT: | |
122 ul = countRefs( Ov_ref( ov ) ); | |
123 return ( ul < r->data.amount ); | |
124 | |
125 case RULE_NOREFS_EQ: | |
126 ul = countRefs( Ov_ref( ov ) ); | |
127 return ( ul == r->data.amount ); | |
128 | |
129 case RULE_NOREFS_GT: | |
130 ul = countRefs( Ov_ref( ov ) ); | |
131 return ( ul > r->data.amount ); | |
132 | |
133 case RULE_XPOSTS_LT: | |
134 if ( newsgroups == NULL ) | |
135 return FALSE; | |
136 ul = countGroups( newsgroups ); | |
137 return ( ul < r->data.amount ); | |
138 | |
139 case RULE_XPOSTS_EQ: | |
140 if ( newsgroups == NULL ) | |
141 return FALSE; | |
142 ul = countGroups( newsgroups ); | |
143 return ( ul == r->data.amount ); | |
144 | |
145 case RULE_XPOSTS_GT: | |
146 if ( newsgroups == NULL ) | |
147 return FALSE; | |
148 ul = countGroups( newsgroups ); | |
149 return ( ul > r->data.amount ); | |
150 } | |
151 | |
152 ASSERT( FALSE ); /* Shouldn't get here */ | |
185
fed1334d766b
[svn] * src/client.c: Change variable only used on constant to 'const'.
bears
parents:
128
diff
changeset
|
153 return 0; /* Keep compiler quiet */ |
128 | 154 } |
155 | |
156 /* Check a single filter to see if it fires. */ | |
157 static Bool | |
158 checkFilter( const char *thisGrp, const char *newsgroups, | |
159 const Over *ov, const Filter *f ) | |
160 { | |
161 int i; | |
162 | |
163 for ( i = 0; i < f->nRules; i++ ) | |
164 if ( ! checkRule( thisGrp, newsgroups, ov, &f->rules[i] ) ) | |
165 return FALSE; | |
166 | |
167 return TRUE; | |
168 } | |
169 | |
170 /* Add a filter to the list of filters. */ | |
171 void | |
172 Flt_addFilter( const Filter *f ) | |
173 { | |
174 ASSERT( f != NULL ); | |
175 | |
176 if ( ( filter.nFilters + 1 ) > filter.maxFilters ) | |
177 { | |
178 filter.filters = | |
179 ( const Filter ** ) realloc( filter.filters, | |
180 ( filter.maxFilters + 5 ) | |
181 * sizeof( Filter * ) ); | |
182 if ( filter.filters == NULL ) | |
183 { | |
184 Log_err( "Could not realloc filter list" ); | |
185 exit( EXIT_FAILURE ); | |
186 } | |
187 filter.maxFilters += 5; | |
188 } | |
189 filter.filters[ filter.nFilters++ ] = f; | |
190 } | |
191 | |
192 /* | |
193 * Run the rules over the supplied overview. If a specific rule fires, | |
194
a4e9a20e50e5
[svn] * docs/noffle.conf.5,src/configfile.c,src/filter.h,src/filter.c:
bears
parents:
185
diff
changeset
|
194 * returns its action. If no rule fires, or a rule specifying the default |
a4e9a20e50e5
[svn] * docs/noffle.conf.5,src/configfile.c,src/filter.h,src/filter.c:
bears
parents:
185
diff
changeset
|
195 * action fires, return the default read mode. |
128 | 196 */ |
197 FilterAction | |
198 Flt_checkFilters( const char *thisGrp, const char *newsgroups, | |
199 const Over *ov, FetchMode mode ) | |
200 { | |
201 int i; | |
202 | |
203 for ( i = 0; i < filter.nFilters; i++ ) | |
204 if ( checkFilter( thisGrp, newsgroups, ov, filter.filters[ i ] ) ) | |
205 { | |
194
a4e9a20e50e5
[svn] * docs/noffle.conf.5,src/configfile.c,src/filter.h,src/filter.c:
bears
parents:
185
diff
changeset
|
206 FilterAction action = filter.filters[ i ]->action; |
a4e9a20e50e5
[svn] * docs/noffle.conf.5,src/configfile.c,src/filter.h,src/filter.c:
bears
parents:
185
diff
changeset
|
207 |
185
fed1334d766b
[svn] * src/client.c: Change variable only used on constant to 'const'.
bears
parents:
128
diff
changeset
|
208 Log_dbg( LOG_DBG_FILTER, |
fed1334d766b
[svn] * src/client.c: Change variable only used on constant to 'const'.
bears
parents:
128
diff
changeset
|
209 "Filter %d fired on message %s", |
fed1334d766b
[svn] * src/client.c: Change variable only used on constant to 'const'.
bears
parents:
128
diff
changeset
|
210 i, Ov_msgId( ov ) ); |
194
a4e9a20e50e5
[svn] * docs/noffle.conf.5,src/configfile.c,src/filter.h,src/filter.c:
bears
parents:
185
diff
changeset
|
211 if ( action == FILTER_DEFAULT ) |
a4e9a20e50e5
[svn] * docs/noffle.conf.5,src/configfile.c,src/filter.h,src/filter.c:
bears
parents:
185
diff
changeset
|
212 break; |
a4e9a20e50e5
[svn] * docs/noffle.conf.5,src/configfile.c,src/filter.h,src/filter.c:
bears
parents:
185
diff
changeset
|
213 else |
a4e9a20e50e5
[svn] * docs/noffle.conf.5,src/configfile.c,src/filter.h,src/filter.c:
bears
parents:
185
diff
changeset
|
214 return action; |
128 | 215 } |
216 | |
217 switch( mode ) | |
218 { | |
219 case FULL: return FILTER_FULL; | |
220 case THREAD: return FILTER_THREAD; | |
221 case OVER: return FILTER_XOVER; | |
222 } | |
223 | |
224 ASSERT( FALSE ); /* Shouldn't get here */ | |
185
fed1334d766b
[svn] * src/client.c: Change variable only used on constant to 'const'.
bears
parents:
128
diff
changeset
|
225 return FILTER_FULL; /* Keep compiler quiet */ |
128 | 226 } |
227 | |
228 Filter * | |
229 new_Filter( void ) | |
230 { | |
231 Filter *f; | |
232 | |
233 if ( ! ( f = ( Filter * ) malloc( sizeof( Filter ) ) ) ) | |
234 { | |
235 Log_err( "Cannot allocate Filter" ); | |
236 exit( EXIT_FAILURE ); | |
237 } | |
238 f->nRules = 0; | |
239 f->maxRules = 0; | |
240 f->rules = NULL; | |
194
a4e9a20e50e5
[svn] * docs/noffle.conf.5,src/configfile.c,src/filter.h,src/filter.c:
bears
parents:
185
diff
changeset
|
241 f->action = FILTER_DEFAULT; |
128 | 242 return f; |
243 } | |
244 | |
245 void | |
246 del_Filter( Filter *f ) | |
247 { | |
248 if ( f == NULL ) | |
249 return; | |
250 | |
251 if ( f->rules != NULL ) | |
252 free( f->rules ); | |
253 free( f ); | |
254 } | |
255 | |
256 FilterAction | |
257 Flt_action( const Filter *f ) | |
258 { | |
259 return f->action; | |
260 } | |
261 | |
262 int | |
263 Flt_nRules( const Filter *f ) | |
264 { | |
265 return f->nRules; | |
266 } | |
267 | |
268 /* | |
269 * Do we have a rule requiring us to fetch the Newsgroups: headers of | |
270 * articles? | |
271 */ | |
272 Bool | |
273 Flt_getNewsgroups( void ) | |
274 { | |
275 return filter.needGroups; | |
276 } | |
277 | |
278 FilterRule | |
279 Flt_rule( const Filter *f, int ruleNo ) | |
280 { | |
281 ASSERT( ruleNo < f->nRules ); | |
282 return f->rules[ ruleNo ]; | |
283 } | |
284 | |
285 void | |
286 Flt_setAction( Filter *f, FilterAction action ) | |
287 { | |
288 f->action = action; | |
289 } | |
290 | |
291 void | |
292 Flt_addRule( Filter *f, FilterRule rule ) | |
293 { | |
294 /* Does the rule require Newsgroups: headers to be fetched? */ | |
295 if ( rule.type == RULE_NEWSGROUP || | |
296 ( rule.type >= RULE_XPOSTS_LT && rule.type <= RULE_XPOSTS_GT ) ) | |
297 filter.needGroups = TRUE; | |
298 | |
299 if ( f->nRules + 1 > f->maxRules ) | |
300 { | |
301 f->rules = | |
302 ( FilterRule * ) realloc( f->rules, | |
303 ( f->maxRules + 5 ) | |
304 * sizeof( FilterRule ) ); | |
305 | |
306 if ( f->rules == NULL ) | |
307 { | |
308 Log_err( "Could not realloc rule list" ); | |
309 exit( EXIT_FAILURE ); | |
310 } | |
311 f->maxRules += 5; | |
312 } | |
313 f->rules[ f->nRules++ ] = rule; | |
314 } | |
315 | |
316 |