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