18 |
extern filter_entry **dfilters, **afilters; |
extern filter_entry **dfilters, **afilters; |
19 |
extern account_data account; |
extern account_data account; |
20 |
extern int options; |
extern int options; |
21 |
|
extern unsigned int dlimit, alimit; |
22 |
|
|
23 |
|
|
24 |
/* |
/* |
122 |
/* |
/* |
123 |
* IMAP SEARCH: searches the mailbox for messages that match certain criteria. |
* IMAP SEARCH: searches the mailbox for messages that match certain criteria. |
124 |
*/ |
*/ |
125 |
int imap_search(void) |
int imap_search(char *results) |
126 |
{ |
{ |
127 |
char command[BIG_COMMAND_MAX]; |
char command[BIG_COMMAND_MAX]; |
128 |
|
|
129 |
verbose("Client request: SEARCH\n"); |
verbose("Client request: SEARCH\n"); |
130 |
|
|
131 |
generate_search_filters(command); |
generate_search_filters(command); |
132 |
|
if (send_command(command)) |
133 |
|
return FAILURE; |
134 |
|
search_response(results); |
135 |
|
|
136 |
|
verbose("Client request: SEARCH\n"); |
137 |
|
|
138 |
return send_command(command); |
if (alimit && afcnt) { |
139 |
|
generate_search_limits(command); |
140 |
|
if (send_command(command)) |
141 |
|
return FAILURE; |
142 |
|
search_response(results); |
143 |
|
} |
144 |
|
|
145 |
|
return SUCCESS; |
146 |
} |
} |
147 |
|
|
148 |
|
|
200 |
|
|
201 |
/* |
/* |
202 |
* Prepare according to the filters defined by the user the IMAP SEARCH |
* Prepare according to the filters defined by the user the IMAP SEARCH |
203 |
* command that will be sent. |
* command that will be sent. This command will search for DENY, ALLOW, |
204 |
|
* and DENY_LIMIT filters. |
205 |
*/ |
*/ |
206 |
void generate_search_filters(char *command) |
void generate_search_filters(char *command) |
207 |
{ |
{ |
|
int f = 0; |
|
208 |
int len; |
int len; |
209 |
|
|
210 |
snprintf(command, BIG_COMMAND_MAX, "%X SEARCH", tag++); |
snprintf(command, BIG_COMMAND_MAX, "%X SEARCH", tag++); |
211 |
|
|
212 |
|
|
213 |
|
deny_filters(command); |
214 |
|
|
215 |
|
if (dlimit) { |
216 |
|
len = strlen(command); |
217 |
|
snprintf(command + len, BIG_COMMAND_MAX - len, " LARGER %d", |
218 |
|
dlimit); |
219 |
|
} |
220 |
|
|
221 |
|
if (!dfcnt) { /* If no DENY filters were defined, then |
222 |
|
deny all except the ALLOW filters. */ |
223 |
|
len = strlen(command); |
224 |
|
snprintf(command + len, BIG_COMMAND_MAX - len, " ALL"); |
225 |
|
} |
226 |
|
|
227 |
|
allow_filters(command, "NOT"); |
228 |
|
|
229 |
|
len = strlen(command); |
230 |
|
snprintf(command + len, BIG_COMMAND_MAX - len, "\r\n"); |
231 |
|
} |
232 |
|
|
233 |
|
|
234 |
|
/* |
235 |
|
* Prepare according to the filters defined by the user the IMAP SEARCH |
236 |
|
* command that will be sent. This command will search for the ALLOW_LIMIT |
237 |
|
* filter. |
238 |
|
*/ |
239 |
|
void generate_search_limits(char *command) |
240 |
|
{ |
241 |
|
int len; |
242 |
|
|
243 |
|
snprintf(command, BIG_COMMAND_MAX, "%X SEARCH", tag++); |
244 |
|
|
245 |
|
allow_filters(command, "OR"); |
246 |
|
|
247 |
/* Go through the DENY type filters... */ |
len = strlen(command); |
248 |
|
snprintf(command + len, BIG_COMMAND_MAX - len, |
249 |
|
" LARGER %d", alimit); |
250 |
|
|
251 |
|
len = strlen(command); |
252 |
|
snprintf(command + len, BIG_COMMAND_MAX - len, "\r\n"); |
253 |
|
|
254 |
|
} |
255 |
|
|
|
if (dfcnt > 0) { |
|
256 |
|
|
257 |
while (f < (dfcnt - 1)) { |
/* |
258 |
len = strlen(command); |
* Adds to the IMAP SEARCH command all the DENY type filters. |
259 |
snprintf(command + len, BIG_COMMAND_MAX - len, |
*/ |
260 |
" OR %s%s \"%s\"", |
void deny_filters(char *command) |
261 |
(dfilters[f]->custom ? "HEADER " : ""), |
{ |
262 |
dfilters[f]->name, dfilters[f]->body); |
int f = 0; |
263 |
f++; |
int len; |
|
} |
|
264 |
|
|
265 |
|
if (dfcnt > 0) { |
266 |
|
while (f < (dfcnt - 1)) { |
267 |
len = strlen(command); |
len = strlen(command); |
268 |
snprintf(command + len, BIG_COMMAND_MAX - len, " %s%s \"%s\"", |
snprintf(command + len, BIG_COMMAND_MAX - len, |
269 |
(dfilters[f]->custom ? "HEADER " : ""), |
" OR %s%s \"%s\"", |
270 |
|
custom_header(dfilters[f]->custom), |
271 |
dfilters[f]->name, dfilters[f]->body); |
dfilters[f]->name, dfilters[f]->body); |
272 |
} else { |
f++; |
273 |
len = strlen(command); |
} |
274 |
snprintf(command + len, BIG_COMMAND_MAX - len, " ALL"); |
|
275 |
} |
len = strlen(command); |
276 |
|
snprintf(command + len, BIG_COMMAND_MAX - len, " %s%s%s \"%s\"", |
277 |
|
(dlimit ? "OR " : ""), |
278 |
|
custom_header(dfilters[f]->custom), |
279 |
|
dfilters[f]->name, dfilters[f]->body); |
280 |
|
} |
281 |
|
} |
282 |
|
|
|
/* ... and then continue to the ALLOW entries. */ |
|
283 |
|
|
284 |
f = 0; |
/* |
285 |
|
* Adds to IMAP SEARCH command all the ALLOW type filters. |
286 |
|
*/ |
287 |
|
void allow_filters(char *command, char *key) |
288 |
|
{ |
289 |
|
int f = 0; |
290 |
|
int len; |
291 |
|
|
292 |
while (f < afcnt) { |
if (afcnt > 0) { |
293 |
|
while (f < (afcnt - 1)) { |
294 |
len = strlen(command); |
len = strlen(command); |
295 |
snprintf(command + len, BIG_COMMAND_MAX - len, |
snprintf(command + len, BIG_COMMAND_MAX - len, |
296 |
" NOT %s%s \"%s\"", |
" %s %s%s \"%s\"", key, |
297 |
(afilters[f]->custom ? "HEADER " : ""), |
custom_header(afilters[f]->custom), |
298 |
afilters[f]->name, afilters[f]->body); |
afilters[f]->name, afilters[f]->body); |
299 |
f++; |
f++; |
300 |
} |
} |
301 |
|
|
302 |
len = strlen(command); |
len = strlen(command); |
303 |
snprintf(command + len, BIG_COMMAND_MAX - len, "\r\n"); |
snprintf(command + len, BIG_COMMAND_MAX - len, " %s%s%s \"%s\"", |
304 |
|
(!strncmp(key, "NOT", 3) ? "NOT " : ""), |
305 |
|
custom_header(afilters[f]->custom), |
306 |
|
afilters[f]->name, afilters[f]->body); |
307 |
|
} |
308 |
} |
} |
309 |
|
|
310 |
|
|