Fixed expansion of empty fields in matchactions
[claws.git] / src / matcher.c
1 #include <ctype.h>
2 #include <string.h>
3 #include <stdlib.h>
4 #include <errno.h>
5 #include "defs.h"
6 #include "utils.h"
7 #include "procheader.h"
8 #include "matcher.h"
9 #include "intl.h"
10 #include "matcher_parser.h"
11 #include "prefs.h"
12
13 struct _MatchParser {
14         gint id;
15         gchar * str;
16 };
17
18 typedef struct _MatchParser MatchParser;
19
20 static MatchParser matchparser_tab[] = {
21         /* msginfo flags */
22         {MATCHCRITERIA_ALL, "all"},
23         {MATCHCRITERIA_UNREAD, "unread"},
24         {MATCHCRITERIA_NOT_UNREAD, "~unread"},
25         {MATCHCRITERIA_NEW, "new"},
26         {MATCHCRITERIA_NOT_NEW, "~new"},
27         {MATCHCRITERIA_MARKED, "marked"},
28         {MATCHCRITERIA_NOT_MARKED, "~marked"},
29         {MATCHCRITERIA_DELETED, "deleted"},
30         {MATCHCRITERIA_NOT_DELETED, "~deleted"},
31         {MATCHCRITERIA_REPLIED, "replied"},
32         {MATCHCRITERIA_NOT_REPLIED, "~replied"},
33         {MATCHCRITERIA_FORWARDED, "forwarded"},
34         {MATCHCRITERIA_NOT_FORWARDED, "~forwarded"},
35
36         /* msginfo headers */
37         {MATCHCRITERIA_SUBJECT, "subject"},
38         {MATCHCRITERIA_NOT_SUBJECT, "~subject"},
39         {MATCHCRITERIA_FROM, "from"},
40         {MATCHCRITERIA_NOT_FROM, "~from"},
41         {MATCHCRITERIA_TO, "to"},
42         {MATCHCRITERIA_NOT_TO, "~to"},
43         {MATCHCRITERIA_CC, "cc"},
44         {MATCHCRITERIA_NOT_CC, "~cc"},
45         {MATCHCRITERIA_TO_OR_CC, "to_or_cc"},
46         {MATCHCRITERIA_NOT_TO_AND_NOT_CC, "~to_or_cc"},
47         {MATCHCRITERIA_AGE_GREATER, "age_greater"},
48         {MATCHCRITERIA_AGE_LOWER, "age_lower"},
49         {MATCHCRITERIA_NEWSGROUPS, "newsgroups"},
50         {MATCHCRITERIA_NOT_NEWSGROUPS, "~newsgroups"},
51         {MATCHCRITERIA_INREPLYTO, "inreplyto"},
52         {MATCHCRITERIA_NOT_INREPLYTO, "~inreplyto"},
53         {MATCHCRITERIA_REFERENCES, "references"},
54         {MATCHCRITERIA_NOT_REFERENCES, "~references"},
55         {MATCHCRITERIA_SCORE_GREATER, "score_greater"},
56         {MATCHCRITERIA_SCORE_LOWER, "score_lower"},
57         {MATCHCRITERIA_SCORE_EQUAL, "score_equal"},
58
59         {MATCHCRITERIA_SIZE_GREATER, "size_greater"},
60         {MATCHCRITERIA_SIZE_SMALLER, "size_smaller"},
61         {MATCHCRITERIA_SIZE_EQUAL,   "size_equal"},
62
63         /* content have to be read */
64         {MATCHCRITERIA_HEADER, "header"},
65         {MATCHCRITERIA_NOT_HEADER, "~header"},
66         {MATCHCRITERIA_HEADERS_PART, "headers_part"},
67         {MATCHCRITERIA_NOT_HEADERS_PART, "~headers_part"},
68         {MATCHCRITERIA_MESSAGE, "message"},
69         {MATCHCRITERIA_NOT_MESSAGE, "~message"},
70         {MATCHCRITERIA_BODY_PART, "body_part"},
71         {MATCHCRITERIA_NOT_BODY_PART, "~body_part"},
72         {MATCHCRITERIA_EXECUTE, "execute"},
73         {MATCHCRITERIA_NOT_EXECUTE, "~execute"},
74
75         /* match type */
76         {MATCHTYPE_MATCHCASE, "matchcase"},
77         {MATCHTYPE_MATCH, "match"},
78         {MATCHTYPE_REGEXPCASE, "regexpcase"},
79         {MATCHTYPE_REGEXP, "regexp"},
80
81         /* actions */
82         {MATCHACTION_SCORE, "score"},
83         {MATCHACTION_MOVE, "move"},
84         {MATCHACTION_COPY, "copy"},
85         {MATCHACTION_DELETE, "delete"},
86         {MATCHACTION_MARK, "mark"},
87         {MATCHACTION_UNMARK, "unmark"},
88         {MATCHACTION_MARK_AS_READ, "mark_as_read"},
89         {MATCHACTION_MARK_AS_UNREAD, "mark_as_unread"},
90         {MATCHACTION_FORWARD, "forward"},
91         {MATCHACTION_FORWARD_AS_ATTACHMENT, "forward_as_attachment"},
92         {MATCHACTION_EXECUTE, "execute"},
93         {MATCHACTION_COLOR, "color"},
94         {MATCHACTION_REDIRECT, "redirect"},
95         {MATCHACTION_DELETE_ON_SERVER, "delete_on_server"}
96 };
97
98 /* get_matchparser_tab_str() - used by filtering.c to translate 
99  * actions to debug strings */
100 gchar * get_matchparser_tab_str(gint id)
101 {
102         gint i;
103
104         for(i = 0 ; i < (int) (sizeof(matchparser_tab) / sizeof(MatchParser)) ;
105             i++) {
106                 if (matchparser_tab[i].id == id)
107                         return matchparser_tab[i].str;
108         }
109         return NULL;
110 }
111
112 /* matcher_escape_str() - escapes a string returns newly allocated escaped string */
113 gchar *matcher_escape_str(const gchar *str)
114 {
115         register const gchar *walk;
116         register int escape;
117         gchar *res;
118         register char *reswalk;
119
120         if (str == NULL)
121                 return NULL;
122
123         for (escape = 0, walk = str; *walk; walk++)
124                 if (*walk == '\'' || *walk == '\"')
125                         escape++;
126
127         if (!escape)
128                 return g_strdup(str);
129         
130         reswalk = res = g_new0(gchar, (walk - str) + escape + 1);
131         for (walk = str; *walk; walk++, reswalk++) {
132                 if (*walk == '\'' || *walk == '\"')
133                         *reswalk++ = '\\';
134                 *reswalk = *walk;
135         }
136
137         *reswalk = 0;
138         return res;
139 }
140
141 /* matcher_unescape_str() - assumes that unescaping frees up room
142  * in the string, so it returns the unescaped string in the 
143  * source */
144 gchar *matcher_unescape_str(gchar *str)
145 {
146         gchar *tmp = alloca(strlen(str) + 1);
147         register gchar *src = tmp;
148         register gchar *dst = str;
149         
150         strcpy(tmp, str);
151
152         for ( ; *src; src++) {
153                 if (*src != '\\') 
154                         *dst++ = *src;
155                 else {
156                         src++;
157                         if (*src == '\\')
158                                 *dst++ = '\\';                          /* insert backslash */
159                         else if (*src == 'n')                           /* insert control characters */
160                                 *dst++ = '\n';
161                         else if (*src == 'r') 
162                                 *dst++ = '\r';
163                         else if (*src == 't') 
164                                 *dst++ = '\t';
165                         else if (*src == 'r') 
166                                 *dst++ = '\r';
167                         else if (*src == 'b')
168                                 *dst++ = '\b';
169                         else if (*src == 'f')
170                                 *dst++ = '\f';
171                         else if (*src == '\'' || *src == '\"')          /* insert \' or \" */
172                                 *dst++ = *src;
173                         else {
174                                 /* FIXME: should perhaps escape character... */
175                                 src--;
176                                 *dst++ = *src;
177                         }                               
178                 }
179         }
180         *dst = 0;
181         return str;
182 }
183
184 /* **************** data structure allocation **************** */
185
186 /* matcherprop_new() - allocates a structure for one condition
187  */
188 MatcherProp * matcherprop_new(gint criteria, gchar * header,
189                               gint matchtype, gchar * expr,
190                               int value)
191 {
192         MatcherProp * prop;
193
194         prop = g_new0(MatcherProp, 1);
195         prop->criteria = criteria;
196         if (header != NULL) {
197                 prop->header       = g_strdup(header);
198                 prop->unesc_header = matcher_unescape_str(g_strdup(header)); 
199         }       
200         else {
201                 prop->header       = NULL;
202                 prop->unesc_header = NULL;
203         }       
204         if (expr != NULL) {
205                 prop->expr       = g_strdup(expr);
206                 prop->unesc_expr = matcher_unescape_str(g_strdup(expr));
207         }       
208         else {
209                 prop->expr       = NULL;
210                 prop->unesc_expr = NULL;
211         }       
212         prop->matchtype = matchtype;
213         prop->preg = NULL;
214         prop->value = value;
215         prop->error = 0;
216
217         return prop;
218 }
219
220 /* matcherprop_free()
221  */
222 void matcherprop_free(MatcherProp * prop)
223 {
224         if (prop->expr) 
225                 g_free(prop->expr);
226         if (prop->unesc_expr) 
227                 g_free(prop->unesc_expr);
228         if (prop->header)
229                 g_free(prop->header);
230         if (prop->unesc_header) 
231                 g_free(prop->unesc_header);
232         if (prop->preg != NULL) {
233                 regfree(prop->preg);
234                 g_free(prop->preg);
235         }
236         g_free(prop);
237 }
238
239
240 /* ************** match ******************************/
241
242
243 /* matcherprop_string_match() - finds out if a string matches
244  * with a criterium */
245 static gboolean matcherprop_string_match(MatcherProp * prop, gchar * str)
246 {
247         gchar * str1;
248         gchar * str2;
249
250         if (str == NULL)
251                 return FALSE;
252
253         switch(prop->matchtype) {
254         case MATCHTYPE_REGEXPCASE:
255         case MATCHTYPE_REGEXP:
256                 if (!prop->preg && (prop->error == 0)) {
257                         prop->preg = g_new0(regex_t, 1);
258                         /* if regexp then don't use the escaped string */
259                         if (regcomp(prop->preg, prop->expr,
260                                     REG_NOSUB | REG_EXTENDED
261                                     | ((prop->matchtype == MATCHTYPE_REGEXPCASE)
262                                     ? REG_ICASE : 0)) != 0) {
263                                 prop->error = 1;
264                                 g_free(prop->preg);
265                         }
266                 }
267                 if (prop->preg == NULL)
268                         return FALSE;
269                 
270                 if (regexec(prop->preg, str, 0, NULL, 0) == 0)
271                         return TRUE;
272                 else
273                         return FALSE;
274
275         case MATCHTYPE_MATCH:
276                 return (strstr(str, prop->unesc_expr) != NULL);
277
278         /* FIXME: put upper in unesc_str */
279         case MATCHTYPE_MATCHCASE:
280                 str2 = alloca(strlen(prop->unesc_expr) + 1);
281                 strcpy(str2, prop->unesc_expr);
282                 g_strup(str2);
283                 str1 = alloca(strlen(str) + 1);
284                 strcpy(str1, str);
285                 g_strup(str1);
286                 return (strstr(str1, str2) != NULL);
287                 
288         default:
289                 return FALSE;
290         }
291 }
292
293 gboolean matcherprop_match_execute(MatcherProp * prop, MsgInfo * info)
294 {
295         gchar * file;
296         gchar * cmd;
297         gint retval;
298
299         file = procmsg_get_message_file(info);
300         if (file == NULL)
301                 return FALSE;
302
303         cmd = matching_build_command(prop->unesc_expr, info);
304         if (cmd == NULL)
305                 return FALSE;
306
307         retval = system(cmd);
308         debug_print(_("Command exit code: %d\n"), retval);
309
310         return (retval == 0);
311 }
312
313 /* match a message and his headers, hlist can be NULL if you don't
314    want to use headers */
315
316 gboolean matcherprop_match(MatcherProp * prop, MsgInfo * info)
317 {
318         time_t t;
319
320         switch(prop->criteria) {
321         case MATCHCRITERIA_ALL:
322                 return 1;
323         case MATCHCRITERIA_UNREAD:
324                 return MSG_IS_UNREAD(info->flags);
325         case MATCHCRITERIA_NOT_UNREAD:
326                 return !MSG_IS_UNREAD(info->flags);
327         case MATCHCRITERIA_NEW:
328                 return MSG_IS_NEW(info->flags);
329         case MATCHCRITERIA_NOT_NEW:
330                 return !MSG_IS_NEW(info->flags);
331         case MATCHCRITERIA_MARKED:
332                 return MSG_IS_MARKED(info->flags);
333         case MATCHCRITERIA_NOT_MARKED:
334                 return !MSG_IS_MARKED(info->flags);
335         case MATCHCRITERIA_DELETED:
336                 return MSG_IS_DELETED(info->flags);
337         case MATCHCRITERIA_NOT_DELETED:
338                 return !MSG_IS_DELETED(info->flags);
339         case MATCHCRITERIA_REPLIED:
340                 return MSG_IS_REPLIED(info->flags);
341         case MATCHCRITERIA_NOT_REPLIED:
342                 return !MSG_IS_REPLIED(info->flags);
343         case MATCHCRITERIA_FORWARDED:
344                 return MSG_IS_FORWARDED(info->flags);
345         case MATCHCRITERIA_NOT_FORWARDED:
346                 return !MSG_IS_FORWARDED(info->flags);
347         case MATCHCRITERIA_SUBJECT:
348                 return matcherprop_string_match(prop, info->subject);
349         case MATCHCRITERIA_NOT_SUBJECT:
350                 return !matcherprop_string_match(prop, info->subject);
351         case MATCHCRITERIA_FROM:
352                 return matcherprop_string_match(prop, info->from);
353         case MATCHCRITERIA_NOT_FROM:
354                 return !matcherprop_string_match(prop, info->from);
355         case MATCHCRITERIA_TO:
356                 return matcherprop_string_match(prop, info->to);
357         case MATCHCRITERIA_NOT_TO:
358                 return !matcherprop_string_match(prop, info->to);
359         case MATCHCRITERIA_CC:
360                 return matcherprop_string_match(prop, info->cc);
361         case MATCHCRITERIA_NOT_CC:
362                 return !matcherprop_string_match(prop, info->cc);
363         case MATCHCRITERIA_TO_OR_CC:
364                 return matcherprop_string_match(prop, info->to)
365                         || matcherprop_string_match(prop, info->cc);
366         case MATCHCRITERIA_NOT_TO_AND_NOT_CC:
367                 return !(matcherprop_string_match(prop, info->to)
368                 || matcherprop_string_match(prop, info->cc));
369         case MATCHCRITERIA_AGE_GREATER:
370                 t = time(NULL);
371                 return ((t - info->date_t) / (60 * 60 * 24)) >= prop->value;
372         case MATCHCRITERIA_AGE_LOWER:
373                 t = time(NULL);
374                 return ((t - info->date_t) / (60 * 60 * 24)) <= prop->value;
375         case MATCHCRITERIA_SCORE_GREATER:
376                 return info->score >= prop->value;
377         case MATCHCRITERIA_SCORE_LOWER:
378                 return info->score <= prop->value;
379         case MATCHCRITERIA_SCORE_EQUAL:
380                 return info->score == prop->value;
381         case MATCHCRITERIA_SIZE_GREATER:
382                 /* FIXME: info->size is an off_t */
383                 return info->size > (off_t) prop->value;
384         case MATCHCRITERIA_SIZE_EQUAL:
385                 /* FIXME: info->size is an off_t */
386                 return info->size == (off_t) prop->value;
387         case MATCHCRITERIA_SIZE_SMALLER:
388                 /* FIXME: info->size is an off_t */
389                 return info->size <  (off_t) prop->value;
390         case MATCHCRITERIA_NEWSGROUPS:
391                 return matcherprop_string_match(prop, info->newsgroups);
392         case MATCHCRITERIA_NOT_NEWSGROUPS:
393                 return !matcherprop_string_match(prop, info->newsgroups);
394         case MATCHCRITERIA_INREPLYTO:
395                 return matcherprop_string_match(prop, info->inreplyto);
396         case MATCHCRITERIA_NOT_INREPLYTO:
397                 return !matcherprop_string_match(prop, info->inreplyto);
398         case MATCHCRITERIA_REFERENCES:
399                 return matcherprop_string_match(prop, info->references);
400         case MATCHCRITERIA_NOT_REFERENCES:
401                 return !matcherprop_string_match(prop, info->references);
402         case MATCHCRITERIA_EXECUTE:
403                 return matcherprop_match_execute(prop, info);
404         case MATCHCRITERIA_NOT_EXECUTE:
405                 return !matcherprop_match_execute(prop, info);
406         default:
407                 return 0;
408         }
409 }
410
411 /* ********************* MatcherList *************************** */
412
413
414 MatcherList * matcherlist_new(GSList * matchers, gboolean bool_and)
415 {
416         MatcherList * cond;
417
418         cond = g_new0(MatcherList, 1);
419
420         cond->matchers = matchers;
421         cond->bool_and = bool_and;
422
423         return cond;
424 }
425
426 void matcherlist_free(MatcherList * cond)
427 {
428         GSList * l;
429
430         for(l = cond->matchers ; l != NULL ; l = g_slist_next(l)) {
431                 matcherprop_free((MatcherProp *) l->data);
432         }
433         g_free(cond);
434 }
435
436 /*
437   skip the headers
438  */
439
440 static void matcherlist_skip_headers(FILE *fp)
441 {
442         gchar buf[BUFFSIZE];
443
444         while (procheader_get_one_field(buf, sizeof(buf), fp, NULL) != -1) {
445         }
446 }
447
448 /*
449   matcherprop_match_one_header
450   returns TRUE if buf matchs the MatchersProp criteria
451  */
452
453 static gboolean matcherprop_match_one_header(MatcherProp * matcher,
454                                              gchar * buf)
455 {
456         gboolean result;
457         Header *header;
458
459         switch(matcher->criteria) {
460         case MATCHCRITERIA_HEADER:
461         case MATCHCRITERIA_NOT_HEADER:
462                 header = procheader_parse_header(buf);
463                 if (!header)
464                         return FALSE;
465                 if (procheader_headername_equal(header->name,
466                                                 matcher->header)) {
467                         if (matcher->criteria == MATCHCRITERIA_HEADER)
468                                 result = matcherprop_string_match(matcher, header->body);
469                         else
470                                 result = !matcherprop_string_match(matcher, header->body);
471                         procheader_header_free(header);
472                         return result;
473                 }
474                 else {
475                         procheader_header_free(header);
476                 }
477                 break;
478         case MATCHCRITERIA_HEADERS_PART:
479         case MATCHCRITERIA_MESSAGE:
480                 return matcherprop_string_match(matcher, buf);
481         case MATCHCRITERIA_NOT_MESSAGE:
482         case MATCHCRITERIA_NOT_HEADERS_PART:
483                 return !matcherprop_string_match(matcher, buf);
484         }
485         return FALSE;
486 }
487
488 /*
489   matcherprop_criteria_header
490   returns TRUE if the headers must be matched
491  */
492
493 static gboolean matcherprop_criteria_headers(MatcherProp * matcher)
494 {
495         switch(matcher->criteria) {
496         case MATCHCRITERIA_HEADER:
497         case MATCHCRITERIA_NOT_HEADER:
498         case MATCHCRITERIA_HEADERS_PART:
499         case MATCHCRITERIA_NOT_HEADERS_PART:
500                 return TRUE;
501         default:
502                 return FALSE;
503         }
504 }
505
506 static gboolean matcherprop_criteria_message(MatcherProp * matcher)
507 {
508         switch(matcher->criteria) {
509         case MATCHCRITERIA_MESSAGE:
510         case MATCHCRITERIA_NOT_MESSAGE:
511                 return TRUE;
512         default:
513                 return FALSE;
514         }
515 }
516
517 /*
518   matcherlist_match_one_header
519   returns TRUE if match should stop
520  */
521
522 static gboolean matcherlist_match_one_header(MatcherList * matchers,
523                                          gchar * buf)
524 {
525         GSList * l;
526
527         for(l = matchers->matchers ; l != NULL ; l = g_slist_next(l)) {
528                 MatcherProp * matcher = (MatcherProp *) l->data;
529
530                 if (matcherprop_criteria_headers(matcher) ||
531                     matcherprop_criteria_message(matcher)) {
532                         if (matcherprop_match_one_header(matcher, buf)) {
533                                 matcher->result = TRUE;
534                         }
535                 }
536
537                 if (matcherprop_criteria_headers(matcher)) {
538                         if (matcher->result) {
539                                 if (!matchers->bool_and)
540                                         return TRUE;
541                         }
542                 }
543         }
544
545         return FALSE;
546 }
547
548 /*
549   matcherlist_match_headers
550   returns TRUE if one of the headers matchs the MatcherList criteria
551  */
552
553 static gboolean matcherlist_match_headers(MatcherList * matchers, FILE * fp)
554 {
555         gchar buf[BUFFSIZE];
556
557         while (procheader_get_one_field(buf, sizeof(buf), fp, NULL) != -1)
558                 if (matcherlist_match_one_header(matchers, buf))
559                         return TRUE;
560
561         return FALSE;
562 }
563
564 /*
565   matcherprop_criteria_body
566   returns TRUE if the body must be matched
567  */
568
569 static gboolean matcherprop_criteria_body(MatcherProp * matcher)
570 {
571         switch(matcher->criteria) {
572         case MATCHCRITERIA_BODY_PART:
573         case MATCHCRITERIA_NOT_BODY_PART:
574                 return TRUE;
575         default:
576                 return FALSE;
577         }
578 }
579
580 /*
581   matcherprop_match_line
582   returns TRUE if the string matchs the MatcherProp criteria
583  */
584
585 static gboolean matcherprop_match_line(MatcherProp * matcher, gchar * line)
586 {
587         switch(matcher->criteria) {
588         case MATCHCRITERIA_BODY_PART:
589         case MATCHCRITERIA_MESSAGE:
590                 return matcherprop_string_match(matcher, line);
591         case MATCHCRITERIA_NOT_BODY_PART:
592         case MATCHCRITERIA_NOT_MESSAGE:
593                 return !matcherprop_string_match(matcher, line);
594         }
595         return FALSE;
596 }
597
598 /*
599   matcherlist_match_line
600   returns TRUE if the string matchs the MatcherList criteria
601  */
602
603 static gboolean matcherlist_match_line(MatcherList * matchers, gchar * line)
604 {
605         GSList * l;
606
607         for(l = matchers->matchers ; l != NULL ; l = g_slist_next(l)) {
608                 MatcherProp * matcher = (MatcherProp *) l->data;
609
610                 if (matcherprop_criteria_body(matcher) ||
611                     matcherprop_criteria_message(matcher)) {
612                         if (matcherprop_match_line(matcher, line)) {
613                                 matcher->result = TRUE;
614                         }
615                 }
616                         
617                 if (matcher->result) {
618                         if (!matchers->bool_and)
619                                 return TRUE;
620                 }
621         }
622         return FALSE;
623 }
624
625 /*
626   matcherlist_match_body
627   returns TRUE if one line of the body matchs the MatcherList criteria
628  */
629
630 static gboolean matcherlist_match_body(MatcherList * matchers, FILE * fp)
631 {
632         gchar buf[BUFFSIZE];
633
634         while (fgets(buf, sizeof(buf), fp) != NULL)
635                 if (matcherlist_match_line(matchers, buf))
636                         return TRUE;
637
638         return FALSE;
639 }
640
641 gboolean matcherlist_match_file(MatcherList * matchers, MsgInfo * info,
642                                 gboolean result)
643 {
644         gboolean read_headers;
645         gboolean read_body;
646         GSList * l;
647         FILE * fp;
648         gchar * file;
649
650         /* file need to be read ? */
651
652         read_headers = FALSE;
653         read_body = FALSE;
654         for(l = matchers->matchers ; l != NULL ; l = g_slist_next(l)) {
655                 MatcherProp * matcher = (MatcherProp *) l->data;
656
657                 if (matcherprop_criteria_headers(matcher))
658                         read_headers = TRUE;
659                 if (matcherprop_criteria_body(matcher))
660                         read_body = TRUE;
661                 if (matcherprop_criteria_message(matcher)) {
662                         read_headers = TRUE;
663                         read_body = TRUE;
664                 }
665                 matcher->result = FALSE;
666         }
667
668         if (!read_headers && !read_body)
669                 return result;
670
671         file = procmsg_get_message_file(info);
672         if (file == NULL)
673                 return FALSE;
674
675         if ((fp = fopen(file, "rb")) == NULL) {
676                 FILE_OP_ERROR(file, "fopen");
677                 g_free(file);
678                 return result;
679         }
680
681         /* read the headers */
682
683         if (read_headers) {
684                 if (matcherlist_match_headers(matchers, fp))
685                         read_body = FALSE;
686         }
687         else {
688                 matcherlist_skip_headers(fp);
689         }
690
691         /* read the body */
692         if (read_body) {
693                 matcherlist_match_body(matchers, fp);
694         }
695         
696         for(l = matchers->matchers ; l != NULL ; l = g_slist_next(l)) {
697                 MatcherProp * matcher = (MatcherProp *) l->data;
698
699                 if (matcherprop_criteria_headers(matcher) ||
700                     matcherprop_criteria_body(matcher)    ||
701                     matcherprop_criteria_message(matcher)) {
702                         if (matcher->result) {
703                                 if (!matchers->bool_and) {
704                                         result = TRUE;
705                                         break;
706                                 }
707                         }
708                         else {
709                                 if (matchers->bool_and) {
710                                         result = FALSE;
711                                         break;
712                                 }
713                         }
714                 }                       
715         }
716
717         g_free(file);
718
719         fclose(fp);
720         
721         return result;
722 }
723
724 /* test a list of condition */
725
726 gboolean matcherlist_match(MatcherList * matchers, MsgInfo * info)
727 {
728         GSList * l;
729         gboolean result;
730
731         if (matchers->bool_and)
732                 result = TRUE;
733         else
734                 result = FALSE;
735
736         /* test the cached elements */
737
738         for(l = matchers->matchers ; l != NULL ; l = g_slist_next(l)) {
739                 MatcherProp * matcher = (MatcherProp *) l->data;
740
741                 switch(matcher->criteria) {
742                 case MATCHCRITERIA_ALL:
743                 case MATCHCRITERIA_UNREAD:
744                 case MATCHCRITERIA_NOT_UNREAD:
745                 case MATCHCRITERIA_NEW:
746                 case MATCHCRITERIA_NOT_NEW:
747                 case MATCHCRITERIA_MARKED:
748                 case MATCHCRITERIA_NOT_MARKED:
749                 case MATCHCRITERIA_DELETED:
750                 case MATCHCRITERIA_NOT_DELETED:
751                 case MATCHCRITERIA_REPLIED:
752                 case MATCHCRITERIA_NOT_REPLIED:
753                 case MATCHCRITERIA_FORWARDED:
754                 case MATCHCRITERIA_NOT_FORWARDED:
755                 case MATCHCRITERIA_SUBJECT:
756                 case MATCHCRITERIA_NOT_SUBJECT:
757                 case MATCHCRITERIA_FROM:
758                 case MATCHCRITERIA_NOT_FROM:
759                 case MATCHCRITERIA_TO:
760                 case MATCHCRITERIA_NOT_TO:
761                 case MATCHCRITERIA_CC:
762                 case MATCHCRITERIA_NOT_CC:
763                 case MATCHCRITERIA_TO_OR_CC:
764                 case MATCHCRITERIA_NOT_TO_AND_NOT_CC:
765                 case MATCHCRITERIA_AGE_GREATER:
766                 case MATCHCRITERIA_AGE_LOWER:
767                 case MATCHCRITERIA_NEWSGROUPS:
768                 case MATCHCRITERIA_NOT_NEWSGROUPS:
769                 case MATCHCRITERIA_INREPLYTO:
770                 case MATCHCRITERIA_NOT_INREPLYTO:
771                 case MATCHCRITERIA_REFERENCES:
772                 case MATCHCRITERIA_NOT_REFERENCES:
773                 case MATCHCRITERIA_SCORE_GREATER:
774                 case MATCHCRITERIA_SCORE_LOWER:
775                 case MATCHCRITERIA_SCORE_EQUAL:
776                 case MATCHCRITERIA_SIZE_GREATER:
777                 case MATCHCRITERIA_SIZE_SMALLER:
778                 case MATCHCRITERIA_SIZE_EQUAL:
779                 case MATCHCRITERIA_EXECUTE:
780                 case MATCHCRITERIA_NOT_EXECUTE:
781                         if (matcherprop_match(matcher, info)) {
782                                 if (!matchers->bool_and) {
783                                         return TRUE;
784                                 }
785                         }
786                         else {
787                                 if (matchers->bool_and) {
788                                         return FALSE;
789                                 }
790                         }
791                 }
792         }
793
794         /* test the condition on the file */
795
796         if (matcherlist_match_file(matchers, info, result)) {
797                 if (!matchers->bool_and)
798                         return TRUE;
799         }
800         else {
801                 if (matchers->bool_and)
802                         return FALSE;
803         }
804
805         return result;
806 }
807
808
809 gchar * matcherprop_to_string(MatcherProp * matcher)
810 {
811         gchar * matcher_str = NULL;
812         gchar * criteria_str;
813         gchar * matchtype_str;
814         int i;
815         gchar * p;
816         gint count;
817         gchar * expr_str;
818         gchar * out;
819
820         criteria_str = NULL;
821         for(i = 0 ; i < (int) (sizeof(matchparser_tab) / sizeof(MatchParser)) ;
822             i++) {
823                 if (matchparser_tab[i].id == matcher->criteria)
824                         criteria_str = matchparser_tab[i].str;
825         }
826         if (criteria_str == NULL)
827                 return NULL;
828
829         switch(matcher->criteria) {
830         case MATCHCRITERIA_AGE_GREATER:
831         case MATCHCRITERIA_AGE_LOWER:
832         case MATCHCRITERIA_SCORE_GREATER:
833         case MATCHCRITERIA_SCORE_LOWER:
834         case MATCHCRITERIA_SCORE_EQUAL:
835         case MATCHCRITERIA_SIZE_GREATER:
836         case MATCHCRITERIA_SIZE_SMALLER:
837         case MATCHCRITERIA_SIZE_EQUAL:
838                 return g_strdup_printf("%s %i", criteria_str, matcher->value);
839         case MATCHCRITERIA_ALL:
840         case MATCHCRITERIA_UNREAD:
841         case MATCHCRITERIA_NOT_UNREAD:
842         case MATCHCRITERIA_NEW:
843         case MATCHCRITERIA_NOT_NEW:
844         case MATCHCRITERIA_MARKED:
845         case MATCHCRITERIA_NOT_MARKED:
846         case MATCHCRITERIA_DELETED:
847         case MATCHCRITERIA_NOT_DELETED:
848         case MATCHCRITERIA_REPLIED:
849         case MATCHCRITERIA_NOT_REPLIED:
850         case MATCHCRITERIA_FORWARDED:
851         case MATCHCRITERIA_NOT_FORWARDED:
852                 return g_strdup(criteria_str);
853         case MATCHCRITERIA_EXECUTE:
854         case MATCHCRITERIA_NOT_EXECUTE:
855                 return g_strdup_printf("%s \"%s\"", criteria_str, matcher->expr);
856         }
857
858         matchtype_str = NULL;
859         for(i = 0 ; i < (int) (sizeof(matchparser_tab) / sizeof(MatchParser)) ;
860             i++) {
861                 if (matchparser_tab[i].id == matcher->matchtype)
862                         matchtype_str = matchparser_tab[i].str;
863         }
864
865         if (matchtype_str == NULL)
866                 return NULL;
867
868         switch (matcher->matchtype) {
869         case MATCHTYPE_MATCH:
870         case MATCHTYPE_MATCHCASE:
871         case MATCHTYPE_REGEXP:
872         case MATCHTYPE_REGEXPCASE:
873                 if (matcher->header)
874                         matcher_str =
875                                 g_strdup_printf("%s \"%s\" %s \"%s\"",
876                                            criteria_str, matcher->header,
877                                            matchtype_str, matcher->expr);
878                 else
879                         matcher_str =
880                                 g_strdup_printf("%s %s \"%s\"", criteria_str,
881                                                 matchtype_str, matcher->expr);
882                 break;
883         }
884
885         return matcher_str;
886 }
887
888 gchar * matcherlist_to_string(MatcherList * matchers)
889 {
890         gint count;
891         gchar ** vstr;
892         GSList * l;
893         gchar ** cur_str;
894         gchar * result;
895
896         count = g_slist_length(matchers->matchers);
897         vstr = g_new(gchar *, count + 1);
898
899         for (l = matchers->matchers, cur_str = vstr ; l != NULL ;
900              l = g_slist_next(l), cur_str ++) {
901                 *cur_str = matcherprop_to_string((MatcherProp *) l->data);
902                 if (*cur_str == NULL)
903                         break;
904         }
905         *cur_str = NULL;
906         
907         if (matchers->bool_and)
908                 result = g_strjoinv(" & ", vstr);
909         else
910                 result = g_strjoinv(" | ", vstr);
911
912         for(cur_str = vstr ; *cur_str != NULL ; cur_str ++)
913                 g_free(*cur_str);
914         g_free(vstr);
915
916         return result;
917 }
918
919 #define STRLEN_ZERO(s) ((s) ? strlen(s) : 0)
920 #define STRLEN_DEFAULT(s,d) ((s) ? strlen(s) : STRLEN_ZERO(d))
921 /* matching_build_command() - preferably cmd should be unescaped */
922 gchar * matching_build_command(gchar * cmd, MsgInfo * info)
923 {
924         gchar * s = cmd;
925         gchar * filename = NULL;
926         gchar * processed_cmd;
927         gchar * p;
928         gint size;
929
930         gchar *no_subject    = _("(none)") ;
931         gchar *no_from       = _("(none)") ;
932         gchar *no_to         = _("(none)") ;
933         gchar *no_cc         = _("(none)") ;
934         gchar *no_date       = _("(none)") ;
935         gchar *no_msgid      = _("(none)") ;
936         gchar *no_newsgroups = _("(none)") ;
937         gchar *no_references = _("(none)") ;
938
939         size = STRLEN_ZERO(cmd) + 1;
940         while (*s != '\0') {
941                 if (*s == '%') {
942                         s++;
943                         switch (*s) {
944                         case '%':
945                                 size -= 1;
946                                 break;
947                         case 's': /* subject */
948                                 size += STRLEN_DEFAULT(info->subject, no_subject) - 2;
949                                 break;
950                         case 'f': /* from */
951                                 size += STRLEN_DEFAULT(info->from, no_from) - 2;
952                                 break;
953                         case 't': /* to */
954                                 size += STRLEN_DEFAULT(info->to, no_to) - 2;
955                                 break;
956                         case 'c': /* cc */
957                                 size += STRLEN_DEFAULT(info->cc, no_cc) - 2;
958                                 break;
959                         case 'd': /* date */
960                                 size += STRLEN_DEFAULT(info->date, no_date) - 2;
961                                 break;
962                         case 'i': /* message-id */
963                                 size += STRLEN_DEFAULT(info->msgid, no_msgid) - 2;
964                                 break;
965                         case 'n': /* newsgroups */
966                                 size += STRLEN_DEFAULT(info->newsgroups, no_newsgroups) - 2;
967                                 break;
968                         case 'r': /* references */
969                                 size += STRLEN_DEFAULT(info->references, no_references) - 2;
970                                 break;
971                         case 'F': /* file */
972                                 filename = folder_item_fetch_msg(info->folder,
973                                                                  info->msgnum);
974                                 
975                                 if (filename == NULL) {
976                                         g_warning(_("filename is not set"));
977                                         return NULL;
978                                 }
979                                 else
980                                         size += strlen(filename) - 2;
981                                 break;
982                         }
983                         s++;
984                 }
985                 else s++;
986         }
987
988
989         processed_cmd = g_new0(gchar, size);
990         s = cmd;
991         p = processed_cmd;
992
993         while (*s != '\0') {
994                 if (*s == '%') {
995                         s++;
996                         switch (*s) {
997                         case '%':
998                                 *p = '%';
999                                 p++;
1000                                 break;
1001                         case 's': /* subject */
1002                                 if (info->subject != NULL)
1003                                         strcpy(p, info->subject);
1004                                 else
1005                                         strcpy(p, no_subject);
1006                                 p += strlen(p);
1007                                 break;
1008                         case 'f': /* from */
1009                                 if (info->from != NULL)
1010                                         strcpy(p, info->from);
1011                                 else
1012                                         strcpy(p, no_from);
1013                                 p += strlen(p);
1014                                 break;
1015                         case 't': /* to */
1016                                 if (info->to != NULL)
1017                                         strcpy(p, info->to);
1018                                 else
1019                                         strcpy(p, no_to);
1020                                 p += strlen(p);
1021                                 break;
1022                         case 'c': /* cc */
1023                                 if (info->cc != NULL)
1024                                         strcpy(p, info->cc);
1025                                 else
1026                                         strcpy(p, no_cc);
1027                                 p += strlen(p);
1028                                 break;
1029                         case 'd': /* date */
1030                                 if (info->date != NULL)
1031                                         strcpy(p, info->date);
1032                                 else
1033                                         strcpy(p, no_date);
1034                                 p += strlen(p);
1035                                 break;
1036                         case 'i': /* message-id */
1037                                 if (info->msgid != NULL)
1038                                         strcpy(p, info->msgid);
1039                                 else
1040                                         strcpy(p, no_msgid);
1041                                 p += strlen(p);
1042                                 break;
1043                         case 'n': /* newsgroups */
1044                                 if (info->newsgroups != NULL)
1045                                         strcpy(p, info->newsgroups);
1046                                 else
1047                                         strcpy(p, no_newsgroups);
1048                                 p += strlen(p);
1049                                 break;
1050                         case 'r': /* references */
1051                                 if (info->references != NULL)
1052                                         strcpy(p, info->references);
1053                                 else
1054                                         strcpy(p, no_references);
1055                                 p += strlen(p);
1056                                 break;
1057                         case 'F': /* file */
1058                                 strcpy(p, filename);
1059                                 p += strlen(p);
1060                                 break;
1061                         default:
1062                                 *p = '%';
1063                                 p++;
1064                                 *p = *s;
1065                                 p++;
1066                                 break;
1067                         }
1068                         s++;
1069                 }
1070                 else {
1071                         *p = *s;
1072                         p++;
1073                         s++;
1074                 }
1075         }
1076
1077         debug_print("*** exec string \"%s\"\n", processed_cmd);
1078         return processed_cmd;
1079 }
1080 #undef STRLEN_DEFAULT
1081 #undef STRLEN_ZERO
1082
1083 /* ************************************************************ */
1084
1085 static void prefs_scoring_write(FILE * fp, GSList * prefs_scoring)
1086 {
1087         GSList * cur;
1088
1089         for (cur = prefs_scoring; cur != NULL; cur = cur->next) {
1090                 gchar *scoring_str;
1091                 ScoringProp * prop;
1092
1093                 prop = (ScoringProp *) cur->data;
1094                 scoring_str = scoringprop_to_string(prop);
1095                 if (fputs(scoring_str, fp) == EOF ||
1096                     fputc('\n', fp) == EOF) {
1097                         FILE_OP_ERROR("scoring config", "fputs || fputc");
1098                         g_free(scoring_str);
1099                         return;
1100                 }
1101                 g_free(scoring_str);
1102         }
1103 }
1104
1105 static void prefs_filtering_write(FILE * fp, GSList * prefs_scoring)
1106 {
1107         GSList * cur;
1108
1109         for (cur = prefs_scoring; cur != NULL; cur = cur->next) {
1110                 gchar *filtering_str;
1111                 FilteringProp * prop;
1112
1113                 if (NULL == (prop = (FilteringProp *) cur->data))
1114                         continue;
1115                 
1116                 if (NULL == (filtering_str = filteringprop_to_string(prop)))
1117                         continue;
1118                 
1119                 if (fputs(filtering_str, fp) == EOF ||
1120                     fputc('\n', fp) == EOF) {
1121                         FILE_OP_ERROR("filtering config", "fputs || fputc");
1122                         g_free(filtering_str);
1123                         return;
1124                 }
1125                 g_free(filtering_str);
1126         }
1127 }
1128
1129 static gboolean prefs_matcher_write_func(GNode *node, gpointer data)
1130 {
1131         FolderItem *item;
1132         FILE * fp = data;
1133         gchar * id;
1134         GSList * prefs_scoring;
1135         GSList * prefs_filtering;
1136
1137         if (node != NULL) {
1138                 item = node->data;
1139                 /* prevent from the warning */
1140                 if (item->path == NULL)
1141                         return FALSE;
1142                 id = folder_item_get_identifier(item);
1143                 if (id == NULL)
1144                         return FALSE;
1145                 prefs_scoring = item->prefs->scoring;
1146                 prefs_filtering = item->prefs->processing;
1147         }
1148         else {
1149                 item = NULL;
1150                 id = g_strdup("global"); /* because it is g_freed */
1151                 prefs_scoring = global_scoring;
1152                 prefs_filtering = global_processing;
1153         }
1154
1155         if (prefs_filtering != NULL || prefs_scoring != NULL) {
1156                 fprintf(fp, "[%s]\n", id);
1157
1158                 prefs_filtering_write(fp, prefs_filtering);
1159                 prefs_scoring_write(fp, prefs_scoring);
1160
1161                 fputc('\n', fp);
1162         }
1163
1164         g_free(id);
1165
1166         return FALSE;
1167 }
1168
1169 static void prefs_matcher_save(FILE * fp)
1170 {
1171         GList * cur;
1172
1173         for (cur = folder_get_list() ; cur != NULL ; cur = g_list_next(cur)) {
1174                 Folder *folder;
1175
1176                 folder = (Folder *) cur->data;
1177                 g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
1178                                 prefs_matcher_write_func, fp);
1179         }
1180         prefs_matcher_write_func(NULL, fp);
1181 }
1182
1183
1184 void prefs_matcher_write_config(void)
1185 {
1186         gchar *rcpath;
1187         PrefFile *pfile;
1188         GSList *cur;
1189         ScoringProp * prop;
1190
1191         debug_print(_("Writing matcher configuration...\n"));
1192
1193         rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
1194                              MATCHER_RC, NULL);
1195
1196         if ((pfile = prefs_write_open(rcpath)) == NULL) {
1197                 g_warning(_("failed to write configuration to file\n"));
1198                 g_free(rcpath);
1199                 return;
1200         }
1201
1202
1203         prefs_matcher_save(pfile->fp);
1204
1205         g_free(rcpath);
1206
1207         if (prefs_write_close(pfile) < 0) {
1208                 g_warning(_("failed to write configuration to file\n"));
1209                 return;
1210         }
1211 }
1212
1213 /* ******************************************************************* */
1214
1215 void prefs_matcher_read_config(void)
1216 {
1217         gchar * rcpath;
1218         FILE * f;
1219
1220         prefs_scoring_clear();
1221         prefs_filtering_clear();
1222
1223         rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MATCHER_RC, NULL);
1224         f = fopen(rcpath, "rb");
1225         g_free(rcpath);
1226
1227         if (f != NULL) {
1228                 matcher_parser_start_parsing(f);
1229                 fclose(f);
1230         }
1231         else {
1232                 /* previous version compatibily */
1233
1234                 /* printf("reading filtering\n"); */
1235                 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
1236                                      FILTERING_RC, NULL);
1237                 f = fopen(rcpath, "rb");
1238                 g_free(rcpath);
1239                 
1240                 if (f != NULL) {
1241                         matcher_parser_start_parsing(f);
1242                         fclose(matcher_parserin);
1243                 }
1244                 
1245                 /* printf("reading scoring\n"); */
1246                 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
1247                                      SCORING_RC, NULL);
1248                 f = fopen(rcpath, "rb");
1249                 g_free(rcpath);
1250                 
1251                 if (f != NULL) {
1252                         matcher_parser_start_parsing(f);
1253                         fclose(matcher_parserin);
1254                 }
1255         }
1256 }