/[imapfilter]/imapfilter/filter.c
ViewVC logotype

Diff of /imapfilter/filter.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.3 by lefcha, Sun Aug 3 16:01:53 2003 UTC revision 1.3.2.3 by lefcha, Sun Nov 23 22:51:44 2003 UTC
# Line 1  Line 1 
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"
# Line 22  filter_t *cur_fltr = NULL;     /* Current fi Line 25  filter_t *cur_fltr = NULL;     /* Current fi
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    
# Line 230  init_mask(mask_t * node) Line 232  init_mask(mask_t * node)
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    
# Line 268  set_mask(char *line, regmatch_t * m, int Line 270  set_mask(char *line, regmatch_t * m, int
270          *(bp + n) = '\0';          *(bp + n) = '\0';
271          bp += n;          bp += n;
272    
273          if (mmt != MASK_MATCH_1) {          switch (mmt) {
274            case MASK_MATCH_1:
275                    /* Mask is ANSWERED, DELETED, DRAFT, FLAGGED, NEW, OLD, etc. */
276    
277                    len = bp - node->body;
278    
279                    break;
280    
281            case MASK_MATCH_2:
282                    /* Mask is BCC, BODY, CC, FROM, SUBJECT, TEXT, TO. */
283    
284                  *(bp++) = ' ';                  *(bp++) = ' ';
285    
286                  if (mmt != MASK_MATCH_4 && *(line + m[5].rm_so) != '"')                  if (*(line + m[5].rm_so) != '"')
287                          *(bp++) = '"';                          *(bp++) = '"';
288                  *bp = '\0';                  *bp = '\0';
289    
# Line 281  set_mask(char *line, regmatch_t * m, int Line 293  set_mask(char *line, regmatch_t * m, int
293                  *(bp + n) = '\0';                  *(bp + n) = '\0';
294                  bp += n;                  bp += n;
295    
296                  if (mmt != MASK_MATCH_4 && *(line + m[5].rm_so) != '"')                  if (*(line + m[5].rm_so) != '"')
297                          *(bp++) = '"';                          *(bp++) = '"';
298                  *bp = '\0';                  *bp = '\0';
299    
300                  if (mmt == MASK_MATCH_3) {                  len = bp - node->body;
                         *(bp++) = ' ';  
301    
302                          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);  
303    
304          cur_fltr->masknum++;          case MASK_MATCH_3:
305          cur_fltr->masklen += (bp - node->body);                  /* Mask is HEADER. */
306    
307  #ifdef DEBUG                  *(bp++) = ' ';
         fprintf(stderr, "debug: MASK: '%s'\n", node->body);  
 #endif  
308    
309          return 0;                  if (*(line + m[5].rm_so) != '"')
310  }                          *(bp++) = '"';
311                    *bp = '\0';
312    
313                    n = min(m[5].rm_eo - m[5].rm_so,
314                        MASK_BODY_LEN - (bp - node->body) - 2);
315                    xstrncpy(bp, line + m[5].rm_so, n);
316                    *(bp + n) = '\0';
317                    bp += n;
318    
319  /*                  if (*(line + m[5].rm_so) != '"')
320   * Converts masks related to date filtering, because IMAP servers do not                          *(bp++) = '"';
321   * 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;  
322    
323          cp = xstrdup(node->body);                  *(bp++) = ' ';
         node->body[0] = '\0';  
324    
325          if (strstr(cp, "NOT"))                  if (*(line + m[6].rm_so) != '"')
326                  strncat(node->body, "NOT ", 4);                          *(bp++) = '"';
327                    *bp = '\0';
328    
329                    n = min(m[6].rm_eo - m[6].rm_so,
330                        MASK_BODY_LEN - (bp - node->body) - 2);
331                    xstrncpy(bp, line + m[6].rm_so, n);
332                    *(bp + n) = '\0';
333                    bp += n;
334    
335                    if (*(line + m[6].rm_so) != '"')
336                            *(bp++) = '"';
337                    *bp = '\0';
338    
339                    len = bp - node->body;
340    
341                    break;
342    
343            case MASK_MATCH_4:
344                    /* Mask is LARGER, SMALLER, OLDER, NEWER. */
345    
346                    *(bp++) = ' ';
347    
348                    n = min(m[5].rm_eo - m[5].rm_so,
349                        MASK_BODY_LEN - (bp - node->body) - 2);
350                    xstrncpy(bp, line + m[5].rm_so, n);
351                    *(bp + n) = '\0';
352                    bp += n;
353                    *bp = '\0';
354    
355          if ((c = strstr(cp, "OLDER")))                  /* Mask length after conversion to IMAP4rev1 date format. */
356                  strncat(node->body, "BEFORE ", 7);                  if (strstr(node->body, "OLDER"))
357          else if ((c = strstr(cp, "NEWER")))                          len = strlen("NOT BEFORE DD-MMM-YYYY");
358                  strncat(node->body, "SINCE ", 6);                  else if (strstr(node->body, "NEWER"))
359                            len = strlen("NOT SINCE DD-MMM-YYYY");
360                    else
361                            len = bp - node->body;
362    
363          c += 6;                  break;
364            default:
365                    break;
366            }
367    
368          te = time(NULL) - (time_t) (strtoul(c, NULL, 10) * 24 * 60 * 60);          APPEND_LINKED_LIST(cur_fltr->masks, node, mask);
         tl = localtime(&te);  
369    
370          if (strftime(s, 15, "%d-%b-%Y", tl))          cur_fltr->masknum++;
371                  strncat(node->body, s, 15);          cur_fltr->masklen += len;
372    
373          xfree(cp);  #ifdef DEBUG
374            fprintf(stderr, "debug: MASK: '%s'\n", node->body);
375    #endif
376    
377            return 0;
378  }  }
379    
380    

Legend:
Removed from v.1.3  
changed lines
  Added in v.1.3.2.3

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26