1 |
#include <stdio.h> |
#include <stdio.h> |
2 |
#include <stdlib.h> |
#include <stdlib.h> |
3 |
#include <string.h> |
#include <string.h> |
4 |
|
#include <strings.h> |
5 |
#include <ctype.h> |
#include <ctype.h> |
6 |
|
#include <sys/types.h> |
7 |
#include <time.h> |
#include <time.h> |
8 |
|
#include <regex.h> |
9 |
|
|
10 |
#include "config.h" |
#include "config.h" |
11 |
#include "imapfilter.h" |
#include "imapfilter.h" |
25 |
void init_filter(filter_t * node); |
void init_filter(filter_t * node); |
26 |
|
|
27 |
void init_mask(mask_t * node); |
void init_mask(mask_t * node); |
|
void convert_date(mask_t * node); |
|
28 |
|
|
29 |
void link_mbox_filter(filter_t * cf, mboxgrp_t * cg); |
void link_mbox_filter(filter_t * cf, mboxgrp_t * cg); |
30 |
|
|
232 |
int |
int |
233 |
set_mask(char *line, regmatch_t * m, int mmt) |
set_mask(char *line, regmatch_t * m, int mmt) |
234 |
{ |
{ |
235 |
int n; |
int n, len; |
236 |
mask_t *node; |
mask_t *node; |
237 |
char *bp; |
char *bp; |
238 |
|
|
239 |
|
len = 0; |
240 |
|
|
241 |
if (cur_fltr == NULL) |
if (cur_fltr == NULL) |
242 |
return ERROR_CONFIG_PARSE; |
return ERROR_CONFIG_PARSE; |
243 |
|
|
272 |
*(bp + n) = '\0'; |
*(bp + n) = '\0'; |
273 |
bp += n; |
bp += n; |
274 |
|
|
275 |
if (mmt != MASK_MATCH_1) { |
switch (mmt) { |
276 |
|
case MASK_MATCH_1: |
277 |
|
/* Mask is ANSWERED, DELETED, DRAFT, FLAGGED, NEW, OLD, etc. */ |
278 |
|
|
279 |
|
len = bp - node->body; |
280 |
|
|
281 |
|
break; |
282 |
|
|
283 |
|
case MASK_MATCH_2: |
284 |
|
/* Mask is BCC, BODY, CC, FROM, SUBJECT, TEXT, TO. */ |
285 |
|
|
286 |
*(bp++) = ' '; |
*(bp++) = ' '; |
287 |
|
|
288 |
if (mmt != MASK_MATCH_4 && *(line + m[5].rm_so) != '"') |
if (*(line + m[5].rm_so) != '"') |
289 |
*(bp++) = '"'; |
*(bp++) = '"'; |
290 |
*bp = '\0'; |
*bp = '\0'; |
291 |
|
|
295 |
*(bp + n) = '\0'; |
*(bp + n) = '\0'; |
296 |
bp += n; |
bp += n; |
297 |
|
|
298 |
if (mmt != MASK_MATCH_4 && *(line + m[5].rm_so) != '"') |
if (*(line + m[5].rm_so) != '"') |
299 |
*(bp++) = '"'; |
*(bp++) = '"'; |
300 |
*bp = '\0'; |
*bp = '\0'; |
301 |
|
|
302 |
if (mmt == MASK_MATCH_3) { |
len = bp - node->body; |
|
*(bp++) = ' '; |
|
303 |
|
|
304 |
if (*(line + m[6].rm_so) != '"') |
break; |
|
*(bp++) = '"'; |
|
|
*bp = '\0'; |
|
|
|
|
|
n = min(m[6].rm_eo - m[6].rm_so, |
|
|
MASK_BODY_LEN - (bp - node->body) - 2); |
|
|
xstrncpy(bp, line + m[6].rm_so, n); |
|
|
*(bp + n) = '\0'; |
|
|
bp += n; |
|
|
|
|
|
if (*(line + m[6].rm_so) != '"') |
|
|
*(bp++) = '"'; |
|
|
*bp = '\0'; |
|
|
} |
|
|
if (mmt == MASK_MATCH_4 && (strstr(node->body, "OLDER") || |
|
|
strstr(node->body, "NEWER"))) { |
|
|
convert_date(node); |
|
|
bp = node->body + strlen(node->body); |
|
|
} |
|
|
} |
|
|
APPEND_LINKED_LIST(cur_fltr->masks, node, mask); |
|
305 |
|
|
306 |
cur_fltr->masknum++; |
case MASK_MATCH_3: |
307 |
cur_fltr->masklen += (bp - node->body); |
/* Mask is HEADER. */ |
308 |
|
|
309 |
#ifdef DEBUG |
*(bp++) = ' '; |
|
fprintf(stderr, "debug: MASK: '%s'\n", node->body); |
|
|
#endif |
|
310 |
|
|
311 |
return 0; |
if (*(line + m[5].rm_so) != '"') |
312 |
} |
*(bp++) = '"'; |
313 |
|
*bp = '\0'; |
314 |
|
|
315 |
|
n = min(m[5].rm_eo - m[5].rm_so, |
316 |
|
MASK_BODY_LEN - (bp - node->body) - 2); |
317 |
|
xstrncpy(bp, line + m[5].rm_so, n); |
318 |
|
*(bp + n) = '\0'; |
319 |
|
bp += n; |
320 |
|
|
321 |
/* |
if (*(line + m[5].rm_so) != '"') |
322 |
* Converts masks related to date filtering, because IMAP servers do not |
*(bp++) = '"'; |
323 |
* understand for example "OLDER 3", but "BEFORE 18-Oct-2001" (if |
*bp = '\0'; |
|
* hypothetically current date was 21-Oct-2001). |
|
|
*/ |
|
|
void |
|
|
convert_date(mask_t * node) |
|
|
{ |
|
|
char *cp, *c; |
|
|
char s[16]; |
|
|
time_t te; |
|
|
struct tm *tl; |
|
324 |
|
|
325 |
cp = xstrdup(node->body); |
*(bp++) = ' '; |
|
node->body[0] = '\0'; |
|
326 |
|
|
327 |
if (strstr(cp, "NOT")) |
if (*(line + m[6].rm_so) != '"') |
328 |
strncat(node->body, "NOT ", 4); |
*(bp++) = '"'; |
329 |
|
*bp = '\0'; |
330 |
|
|
331 |
|
n = min(m[6].rm_eo - m[6].rm_so, |
332 |
|
MASK_BODY_LEN - (bp - node->body) - 2); |
333 |
|
xstrncpy(bp, line + m[6].rm_so, n); |
334 |
|
*(bp + n) = '\0'; |
335 |
|
bp += n; |
336 |
|
|
337 |
|
if (*(line + m[6].rm_so) != '"') |
338 |
|
*(bp++) = '"'; |
339 |
|
*bp = '\0'; |
340 |
|
|
341 |
|
len = bp - node->body; |
342 |
|
|
343 |
|
break; |
344 |
|
|
345 |
|
case MASK_MATCH_4: |
346 |
|
/* Mask is LARGER, SMALLER, OLDER, NEWER. */ |
347 |
|
|
348 |
|
*(bp++) = ' '; |
349 |
|
|
350 |
|
n = min(m[5].rm_eo - m[5].rm_so, |
351 |
|
MASK_BODY_LEN - (bp - node->body) - 2); |
352 |
|
xstrncpy(bp, line + m[5].rm_so, n); |
353 |
|
*(bp + n) = '\0'; |
354 |
|
bp += n; |
355 |
|
*bp = '\0'; |
356 |
|
|
357 |
if ((c = strstr(cp, "OLDER"))) |
/* Mask length after conversion to IMAP4rev1 date format. */ |
358 |
strncat(node->body, "BEFORE ", 7); |
if (strstr(node->body, "OLDER")) |
359 |
else if ((c = strstr(cp, "NEWER"))) |
len = strlen("NOT BEFORE DD-MMM-YYYY"); |
360 |
strncat(node->body, "SINCE ", 6); |
else if (strstr(node->body, "NEWER")) |
361 |
|
len = strlen("NOT SINCE DD-MMM-YYYY"); |
362 |
|
else |
363 |
|
len = bp - node->body; |
364 |
|
|
365 |
c += 6; |
break; |
366 |
|
default: |
367 |
|
break; |
368 |
|
} |
369 |
|
|
370 |
te = time(NULL) - (time_t) (strtoul(c, NULL, 10) * 24 * 60 * 60); |
APPEND_LINKED_LIST(cur_fltr->masks, node, mask); |
|
tl = localtime(&te); |
|
371 |
|
|
372 |
if (strftime(s, 15, "%d-%b-%Y", tl)) |
cur_fltr->masknum++; |
373 |
strncat(node->body, s, 15); |
cur_fltr->masklen += len; |
374 |
|
|
375 |
xfree(cp); |
#ifdef DEBUG |
376 |
|
fprintf(stderr, "debug: MASK: '%s'\n", node->body); |
377 |
|
#endif |
378 |
|
|
379 |
|
return 0; |
380 |
} |
} |
381 |
|
|
382 |
|
|