2d5d9c9985c7d500081267bc3de9b82aaf6091a5
[claws.git] / src / matcher.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 2002-2012 by the Claws Mail Team and Hiroyuki Yamamoto
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  * 
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #include "claws-features.h"
23 #endif
24
25 #include <glib.h>
26 #include <glib/gi18n.h>
27 #include <ctype.h>
28 #include <string.h>
29 #include <stdlib.h>
30 #include <errno.h>
31
32 #ifdef USE_PTHREAD
33 #include <pthread.h>
34 #endif
35
36 #include "defs.h"
37 #include "utils.h"
38 #include "procheader.h"
39 #include "matcher.h"
40 #include "matcher_parser.h"
41 #include "prefs_gtk.h"
42 #include "addr_compl.h"
43 #include "codeconv.h"
44 #include "quoted-printable.h"
45 #include "claws.h"
46 #include <ctype.h>
47 #include "prefs_common.h"
48 #include "log.h"
49 #include "tags.h"
50
51 /*!
52  *\brief        Keyword lookup element
53  */
54 struct _MatchParser {
55         gint id;                /*!< keyword id */ 
56         gchar *str;             /*!< keyword */
57 };
58 typedef struct _MatchParser MatchParser;
59
60 /*!
61  *\brief        Table with strings and ids used by the lexer and
62  *              the parser. New keywords can be added here.
63  */
64 static const MatchParser matchparser_tab[] = {
65         /* msginfo flags */
66         {MATCHCRITERIA_ALL, "all"},
67         {MATCHCRITERIA_UNREAD, "unread"},
68         {MATCHCRITERIA_NOT_UNREAD, "~unread"},
69         {MATCHCRITERIA_NEW, "new"},
70         {MATCHCRITERIA_NOT_NEW, "~new"},
71         {MATCHCRITERIA_MARKED, "marked"},
72         {MATCHCRITERIA_NOT_MARKED, "~marked"},
73         {MATCHCRITERIA_DELETED, "deleted"},
74         {MATCHCRITERIA_NOT_DELETED, "~deleted"},
75         {MATCHCRITERIA_REPLIED, "replied"},
76         {MATCHCRITERIA_NOT_REPLIED, "~replied"},
77         {MATCHCRITERIA_FORWARDED, "forwarded"},
78         {MATCHCRITERIA_NOT_FORWARDED, "~forwarded"},
79         {MATCHCRITERIA_LOCKED, "locked"},
80         {MATCHCRITERIA_NOT_LOCKED, "~locked"},
81         {MATCHCRITERIA_COLORLABEL, "colorlabel"},
82         {MATCHCRITERIA_NOT_COLORLABEL, "~colorlabel"},
83         {MATCHCRITERIA_IGNORE_THREAD, "ignore_thread"},
84         {MATCHCRITERIA_NOT_IGNORE_THREAD, "~ignore_thread"},
85         {MATCHCRITERIA_WATCH_THREAD, "watch_thread"},
86         {MATCHCRITERIA_NOT_WATCH_THREAD, "~watch_thread"},
87         {MATCHCRITERIA_SPAM, "spam"},
88         {MATCHCRITERIA_NOT_SPAM, "~spam"},
89         {MATCHCRITERIA_HAS_ATTACHMENT, "has_attachment"},
90         {MATCHCRITERIA_HAS_NO_ATTACHMENT, "~has_attachment"},
91         {MATCHCRITERIA_SIGNED, "signed"},
92         {MATCHCRITERIA_NOT_SIGNED, "~signed"},
93
94         /* msginfo headers */
95         {MATCHCRITERIA_SUBJECT, "subject"},
96         {MATCHCRITERIA_NOT_SUBJECT, "~subject"},
97         {MATCHCRITERIA_FROM, "from"},
98         {MATCHCRITERIA_NOT_FROM, "~from"},
99         {MATCHCRITERIA_TO, "to"},
100         {MATCHCRITERIA_NOT_TO, "~to"},
101         {MATCHCRITERIA_CC, "cc"},
102         {MATCHCRITERIA_NOT_CC, "~cc"},
103         {MATCHCRITERIA_TO_OR_CC, "to_or_cc"},
104         {MATCHCRITERIA_NOT_TO_AND_NOT_CC, "~to_or_cc"},
105         {MATCHCRITERIA_TAG, "tag"},
106         {MATCHCRITERIA_NOT_TAG, "~tag"},
107         {MATCHCRITERIA_TAGGED, "tagged"},
108         {MATCHCRITERIA_NOT_TAGGED, "~tagged"},
109         {MATCHCRITERIA_AGE_GREATER, "age_greater"},
110         {MATCHCRITERIA_AGE_LOWER, "age_lower"},
111         {MATCHCRITERIA_NEWSGROUPS, "newsgroups"},
112         {MATCHCRITERIA_NOT_NEWSGROUPS, "~newsgroups"},
113         {MATCHCRITERIA_INREPLYTO, "inreplyto"},
114         {MATCHCRITERIA_NOT_INREPLYTO, "~inreplyto"},
115         {MATCHCRITERIA_REFERENCES, "references"},
116         {MATCHCRITERIA_NOT_REFERENCES, "~references"},
117         {MATCHCRITERIA_SCORE_GREATER, "score_greater"},
118         {MATCHCRITERIA_SCORE_LOWER, "score_lower"},
119         {MATCHCRITERIA_SCORE_EQUAL, "score_equal"},
120         {MATCHCRITERIA_PARTIAL, "partial"},
121         {MATCHCRITERIA_NOT_PARTIAL, "~partial"},
122         {MATCHCRITERIA_FOUND_IN_ADDRESSBOOK, "found_in_addressbook"},
123         {MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK, "~found_in_addressbook"},
124
125         {MATCHCRITERIA_SIZE_GREATER, "size_greater"},
126         {MATCHCRITERIA_SIZE_SMALLER, "size_smaller"},
127         {MATCHCRITERIA_SIZE_EQUAL,   "size_equal"},
128
129         /* content have to be read */
130         {MATCHCRITERIA_HEADER, "header"},
131         {MATCHCRITERIA_NOT_HEADER, "~header"},
132         {MATCHCRITERIA_HEADERS_PART, "headers_part"},
133         {MATCHCRITERIA_NOT_HEADERS_PART, "~headers_part"},
134         {MATCHCRITERIA_MESSAGE, "message"},
135         {MATCHCRITERIA_NOT_MESSAGE, "~message"},
136         {MATCHCRITERIA_BODY_PART, "body_part"},
137         {MATCHCRITERIA_NOT_BODY_PART, "~body_part"},
138         {MATCHCRITERIA_TEST, "test"},
139         {MATCHCRITERIA_NOT_TEST, "~test"},
140
141         /* match type */
142         {MATCHTYPE_MATCHCASE, "matchcase"},
143         {MATCHTYPE_MATCH, "match"},
144         {MATCHTYPE_REGEXPCASE, "regexpcase"},
145         {MATCHTYPE_REGEXP, "regexp"},
146
147         /* actions */
148         {MATCHACTION_SCORE, "score"},    /* for backward compatibility */
149         {MATCHACTION_MOVE, "move"},
150         {MATCHACTION_COPY, "copy"},
151         {MATCHACTION_DELETE, "delete"},
152         {MATCHACTION_MARK, "mark"},
153         {MATCHACTION_UNMARK, "unmark"},
154         {MATCHACTION_LOCK, "lock"},
155         {MATCHACTION_UNLOCK, "unlock"},
156         {MATCHACTION_MARK_AS_READ, "mark_as_read"},
157         {MATCHACTION_MARK_AS_UNREAD, "mark_as_unread"},
158         {MATCHACTION_MARK_AS_SPAM, "mark_as_spam"},
159         {MATCHACTION_MARK_AS_HAM, "mark_as_ham"},
160         {MATCHACTION_FORWARD, "forward"},
161         {MATCHACTION_FORWARD_AS_ATTACHMENT, "forward_as_attachment"},
162         {MATCHACTION_EXECUTE, "execute"},
163         {MATCHACTION_COLOR, "color"},
164         {MATCHACTION_REDIRECT, "redirect"},
165         {MATCHACTION_CHANGE_SCORE, "change_score"},
166         {MATCHACTION_SET_SCORE, "set_score"},
167         {MATCHACTION_STOP, "stop"},
168         {MATCHACTION_HIDE, "hide"},
169         {MATCHACTION_IGNORE, "ignore"},
170         {MATCHACTION_WATCH, "watch"},
171         {MATCHACTION_ADD_TO_ADDRESSBOOK, "add_to_addressbook"},
172         {MATCHACTION_SET_TAG, "set_tag"},
173         {MATCHACTION_UNSET_TAG, "unset_tag"},
174         {MATCHACTION_CLEAR_TAGS, "clear_tags"},
175 };
176
177 enum {
178         MATCH_ANY = 0,
179         MATCH_ALL = 1,
180         MATCH_ONE = 2
181 };
182
183 extern gboolean debug_filtering_session;
184
185 /*!
186  *\brief        Look up table with keywords defined in \sa matchparser_tab
187  */
188 static GHashTable *matchparser_hashtab;
189
190 /*!
191  *\brief        Translate keyword id to keyword string
192  *
193  *\param        id Id of keyword
194  *
195  *\return       const gchar * Keyword
196  */
197 const gchar *get_matchparser_tab_str(gint id)
198 {
199         gint i;
200
201         for (i = 0; i < sizeof matchparser_tab / sizeof matchparser_tab[0]; i++) {
202                 if (matchparser_tab[i].id == id)
203                         return matchparser_tab[i].str;
204         }
205         return NULL;
206 }
207
208 /*!
209  *\brief        Create keyword lookup table
210  */
211 static void create_matchparser_hashtab(void)
212 {
213         int i;
214         
215         if (matchparser_hashtab) return;
216         matchparser_hashtab = g_hash_table_new(g_str_hash, g_str_equal);
217         for (i = 0; i < sizeof matchparser_tab / sizeof matchparser_tab[0]; i++)
218                 g_hash_table_insert(matchparser_hashtab,
219                                     matchparser_tab[i].str,
220                                     (gpointer) &matchparser_tab[i]);
221 }
222
223 /*!
224  *\brief        Return a keyword id from a keyword string
225  *
226  *\param        str Keyword string
227  *
228  *\return       gint Keyword id
229  */
230 gint get_matchparser_tab_id(const gchar *str)
231 {
232         MatchParser *res;
233
234         if (NULL != (res = g_hash_table_lookup(matchparser_hashtab, str))) {
235                 return res->id;
236         } else
237                 return -1;
238 }
239
240 /* **************** data structure allocation **************** */
241
242 /*!
243  *\brief        Allocate a structure for a filtering / scoring
244  *              "condition" (a matcher structure)
245  *
246  *\param        criteria Criteria ID (MATCHCRITERIA_XXXX)
247  *\param        header Header string (if criteria is MATCHCRITERIA_HEADER
248                         or MATCHCRITERIA_FOUND_IN_ADDRESSBOOK)
249  *\param        matchtype Type of action (MATCHTYPE_XXX)
250  *\param        expr String value or expression to check
251  *\param        value Integer value to check
252  *
253  *\return       MatcherProp * Pointer to newly allocated structure
254  */
255 MatcherProp *matcherprop_new(gint criteria, const gchar *header,
256                               gint matchtype, const gchar *expr,
257                               int value)
258 {
259         MatcherProp *prop;
260
261         prop = g_new0(MatcherProp, 1);
262         prop->criteria = criteria;
263         prop->header = header != NULL ? g_strdup(header) : NULL;
264
265         prop->expr = expr != NULL ? g_strdup(expr) : NULL;
266
267         prop->matchtype = matchtype;
268         prop->preg = NULL;
269         prop->value = value;
270         prop->error = 0;
271
272         return prop;
273 }
274
275 /*!
276  *\brief        Free a matcher structure
277  *
278  *\param        prop Pointer to matcher structure allocated with
279  *              #matcherprop_new
280  */
281 void matcherprop_free(MatcherProp *prop)
282 {
283         g_free(prop->expr);
284         g_free(prop->header);
285         if (prop->preg != NULL) {
286                 regfree(prop->preg);
287                 g_free(prop->preg);
288         }
289         g_free(prop);
290 }
291
292 /*!
293  *\brief        Copy a matcher structure
294  *
295  *\param        src Matcher structure to copy
296  *
297  *\return       MatcherProp * Pointer to newly allocated matcher structure
298  */
299 MatcherProp *matcherprop_copy(const MatcherProp *src)
300 {
301         MatcherProp *prop = g_new0(MatcherProp, 1);
302         
303         prop->criteria = src->criteria;
304         prop->header = src->header ? g_strdup(src->header) : NULL;
305         prop->expr = src->expr ? g_strdup(src->expr) : NULL;
306         prop->matchtype = src->matchtype;
307         
308         prop->preg = NULL; /* will be re-evaluated */
309         prop->value = src->value;
310         prop->error = src->error;       
311         return prop;            
312 }
313
314 /* ************** match ******************************/
315
316 static gboolean match_with_addresses_in_addressbook
317         (MatcherProp *prop, GSList *address_list, gint type,
318          gchar* folderpath, gint match)
319 {
320         GSList *walk = NULL;
321         gboolean found = FALSE;
322         gchar *path = NULL;
323
324         cm_return_val_if_fail(address_list != NULL, FALSE);
325
326         debug_print("match_with_addresses_in_addressbook(%d, %s)\n",
327                                 g_slist_length(address_list), folderpath?folderpath:"(null)");
328
329         if (folderpath == NULL ||
330                 strcasecmp(folderpath, "Any") == 0 ||
331                 *folderpath == '\0')
332                 path = NULL;
333         else
334                 path = folderpath;
335         
336         start_address_completion(path);
337
338         for (walk = address_list; walk != NULL; walk = walk->next) {
339                 /* exact matching of email address */
340                 guint num_addr = complete_address(walk->data);
341                 found = FALSE;
342                 if (num_addr > 1) {
343                         /* skip first item (this is the search string itself) */
344                         int i = 1;
345                         for (; i < num_addr && !found; i++) {
346                                 gchar *addr = get_complete_address(i);
347                                 extract_address(addr);
348                                 if (strcasecmp(addr, walk->data) == 0) {
349                                         found = TRUE;
350
351                                         /* debug output */
352                                         if (debug_filtering_session
353                                                         && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
354                                                 log_print(LOG_DEBUG_FILTERING,
355                                                                 "address [ %s ] matches\n",
356                                                                 (gchar *)walk->data);
357                                         }
358                                 }
359                                 g_free(addr);
360                         }
361                 }
362                 /* debug output */
363                 if (debug_filtering_session
364                                 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH
365                                 && !found) {
366                         log_print(LOG_DEBUG_FILTERING,
367                                         "address [ %s ] does NOT match\n",
368                                         (gchar *)walk->data);
369                 }
370                 g_free(walk->data);
371
372                 if (match == MATCH_ALL) {
373                         /* if matching all addresses, stop if one doesn't match */
374                         if (!found) {
375                                 /* debug output */
376                                 if (debug_filtering_session
377                                                 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
378                                         log_print(LOG_DEBUG_FILTERING,
379                                                         "not all address match (matching all)\n");
380                                 }
381                                 break;
382                         }
383                 } else if (match == MATCH_ANY) {
384                         /* if matching any address, stop if one does match */
385                         if (found) {
386                                 /* debug output */
387                                 if (debug_filtering_session
388                                                 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
389                                         log_print(LOG_DEBUG_FILTERING,
390                                                         "at least one address matches (matching any)\n");
391                                 }
392                                 break;
393                         }
394                 }
395                 /* MATCH_ONE: there should be only one loop iteration */
396         }
397
398         end_address_completion();
399         
400         return found;
401 }
402
403 /*!
404  *\brief        Find out if a string matches a condition
405  *
406  *\param        prop Matcher structure
407  *\param        str String to check 
408  *
409  *\return       gboolean TRUE if str matches the condition in the 
410  *              matcher structure
411  */
412 static gboolean matcherprop_string_match(MatcherProp *prop, const gchar *str,
413                                          const gchar *debug_context)
414 {
415         gchar *str1;
416         gchar *down_expr;
417         gboolean ret = FALSE;
418         gboolean should_free = FALSE;
419         if (str == NULL)
420                 return FALSE;
421
422         if (prop->matchtype == MATCHTYPE_REGEXPCASE ||
423             prop->matchtype == MATCHTYPE_MATCHCASE) {
424                 str1 = g_utf8_casefold(str, -1);
425                 down_expr = g_utf8_casefold(prop->expr, -1);
426                 should_free = TRUE;
427         } else {
428                 str1 = (gchar *)str;
429                 down_expr = (gchar *)prop->expr;
430                 should_free = FALSE;
431         }
432
433         switch (prop->matchtype) {
434         case MATCHTYPE_REGEXPCASE:
435         case MATCHTYPE_REGEXP:
436 #ifndef G_OS_WIN32
437                 if (!prop->preg && (prop->error == 0)) {
438                         prop->preg = g_new0(regex_t, 1);
439                         /* if regexp then don't use the escaped string */
440                         if (regcomp(prop->preg, down_expr,
441                                     REG_NOSUB | REG_EXTENDED
442                                     | ((prop->matchtype == MATCHTYPE_REGEXPCASE)
443                                     ? REG_ICASE : 0)) != 0) {
444                                 prop->error = 1;
445                                 g_free(prop->preg);
446                                 prop->preg = NULL;
447                         }
448                 }
449                 if (prop->preg == NULL) {
450                         ret = FALSE;
451                         goto free_strs;
452                 }
453                 
454                 if (regexec(prop->preg, str1, 0, NULL, 0) == 0)
455                         ret = TRUE;
456                 else
457                         ret = FALSE;
458
459                 /* debug output */
460                 if (debug_filtering_session
461                                 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
462                         gchar *stripped = g_strdup(str);
463
464                         strretchomp(stripped);
465                         if (ret) {
466                                 log_print(LOG_DEBUG_FILTERING,
467                                                 "%s value [ %s ] matches regular expression [ %s ] (%s)\n",
468                                                 debug_context, stripped, prop->expr,
469                                                 prop->matchtype == MATCHTYPE_REGEXP ? _("Case sensitive"):_("Case insensitive"));
470                         } else {
471                                 log_print(LOG_DEBUG_FILTERING,
472                                                 "%s value [ %s ] does NOT match regular expression [ %s ] (%s)\n",
473                                                 debug_context, stripped, prop->expr,
474                                                 prop->matchtype == MATCHTYPE_REGEXP ? _("Case sensitive"):_("Case insensitive"));
475                         }
476                         g_free(stripped);
477                 }
478                 break;
479 #endif                  
480         case MATCHTYPE_MATCHCASE:
481         case MATCHTYPE_MATCH:
482                 ret = (strstr(str1, down_expr) != NULL);
483
484                 /* debug output */
485                 if (debug_filtering_session
486                                 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
487                         gchar *stripped = g_strdup(str);
488
489                         strretchomp(stripped);
490                         if (ret) {
491                                 log_print(LOG_DEBUG_FILTERING,
492                                                 "%s value [ %s ] contains [ %s ] (%s)\n",
493                                                 debug_context, stripped, prop->expr,
494                                                 prop->matchtype == MATCHTYPE_MATCH ? _("Case sensitive"):_("Case insensitive"));
495                         } else {
496                                 log_print(LOG_DEBUG_FILTERING,
497                                                 "%s value [ %s ] does NOT contain [ %s ] (%s)\n",
498                                                 debug_context, stripped, prop->expr,
499                                                 prop->matchtype == MATCHTYPE_MATCH ? _("Case sensitive"):_("Case insensitive"));
500                         }
501                         g_free(stripped);
502                 }
503                 break;
504
505         default:
506                 break;
507         }
508         
509 free_strs:
510         if (should_free) {
511                 g_free(str1);
512                 g_free(down_expr);
513         }
514         return ret;
515 }
516
517 /*!
518  *\brief        Find out if a tag matches a condition
519  *
520  *\param        prop Matcher structure
521  *\param        msginfo message to check
522  *
523  *\return       gboolean TRUE if msginfo matches the condition in the 
524  *              matcher structure
525  */
526 static gboolean matcherprop_tag_match(MatcherProp *prop, MsgInfo *msginfo,
527                                          const gchar *debug_context)
528 {
529         gboolean ret = FALSE;
530         GSList *cur;
531
532         if (msginfo == NULL || msginfo->tags == NULL)
533                 return FALSE;
534
535         for (cur = msginfo->tags; cur; cur = cur->next) {
536                 const gchar *str = tags_get_tag(GPOINTER_TO_INT(cur->data));
537                 if (!str)
538                         continue;
539                 if (matcherprop_string_match(prop, str, debug_context)) {
540                         ret = TRUE;
541                         break;
542                 }
543         }
544         return ret;
545 }
546
547 /*!
548  *\brief        Find out if the string-ed list matches a condition
549  *
550  *\param        prop Matcher structure
551  *\param        list GSList of strings to check
552  *
553  *\return       gboolean TRUE if str matches the condition in the 
554  *              matcher structure
555  */
556 static gboolean matcherprop_list_match(MatcherProp *prop, const GSList *list,
557 const gchar *debug_context)
558 {
559         const GSList *cur;
560
561         for(cur = list; cur != NULL; cur = cur->next) {
562                 if (matcherprop_string_match(prop, (gchar *)cur->data, debug_context))
563                         return TRUE;
564         }
565         return FALSE;
566 }
567
568 static gboolean matcherprop_header_line_match(MatcherProp *prop, const gchar *hdr,
569                                               const gchar *str, const gchar *debug_context)
570 {
571         gchar *line = NULL;
572         gboolean res = FALSE;
573
574         if (hdr == NULL || str == NULL)
575                 return FALSE;
576
577         line = g_strdup_printf("%s %s", hdr, str);
578         res = matcherprop_string_match(prop, line, debug_context);
579         g_free(line);
580        
581         return res;
582 }
583
584 #ifdef USE_PTHREAD
585 typedef struct _thread_data {
586         const gchar *cmd;
587         gboolean done;
588 } thread_data;
589 #endif
590
591 #ifdef USE_PTHREAD
592 static void *matcher_test_thread(void *data)
593 {
594         thread_data *td = (thread_data *)data;
595         int result = -1;
596
597         pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
598         pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
599
600         result = system(td->cmd);
601         td->done = TRUE; /* let the caller thread join() */
602         return GINT_TO_POINTER(result);
603 }
604 #endif
605
606 /*!
607  *\brief        Execute a command defined in the matcher structure
608  *
609  *\param        prop Pointer to matcher structure
610  *\param        info Pointer to message info structure
611  *
612  *\return       gboolean TRUE if command was executed succesfully
613  */
614 static gboolean matcherprop_match_test(const MatcherProp *prop, 
615                                           MsgInfo *info)
616 {
617         gchar *file;
618         gchar *cmd;
619         gint retval;
620 #ifdef USE_PTHREAD
621         pthread_t pt;
622         pthread_attr_t pta;
623         thread_data *td = g_new0(thread_data, 1);
624         void *res = NULL;
625         time_t start_time = time(NULL);
626 #endif
627
628         file = procmsg_get_message_file(info);
629         if (file == NULL) {
630 #ifdef USE_PTHREAD
631                 g_free(td);
632 #endif
633                 return FALSE;
634         }
635         g_free(file);           
636
637         cmd = matching_build_command(prop->expr, info);
638         if (cmd == NULL) {
639 #ifdef USE_PTHREAD
640                 g_free(td);
641 #endif  
642                 return FALSE;
643 }
644
645 #ifdef USE_PTHREAD
646         /* debug output */
647         if (debug_filtering_session
648                         && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
649                 log_print(LOG_DEBUG_FILTERING,
650                                 "starting threaded command [ %s ]\n",
651                                 cmd);
652         }
653
654         td->cmd = cmd;
655         td->done = FALSE;
656         if (pthread_attr_init(&pta) != 0 ||
657             pthread_attr_setdetachstate(&pta, PTHREAD_CREATE_JOINABLE) != 0 ||
658             pthread_create(&pt, &pta, matcher_test_thread, td) != 0)
659                 retval = system(cmd);
660         else {
661                 debug_print("waiting for test thread\n");
662                 while(!td->done) {
663                         /* don't let the interface freeze while waiting */
664                         claws_do_idle();
665                         if (time(NULL) - start_time > 30) {
666                                 pthread_cancel(pt);
667                                 td->done = TRUE;
668                                 retval = -1;
669                         }
670                 }
671                 pthread_join(pt, &res);
672                 retval = GPOINTER_TO_INT(res);
673                 debug_print(" test thread returned %d\n", retval);
674         }
675         g_free(td);
676 #else
677         /* debug output */
678         if (debug_filtering_session
679                         && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
680                 log_print(LOG_DEBUG_FILTERING,
681                                 "starting synchronous command [ %s ]\n",
682                                 cmd);
683         }
684
685         retval = system(cmd);
686 #endif
687         debug_print("Command exit code: %d\n", retval);
688
689         /* debug output */
690         if (debug_filtering_session
691                         && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
692                 log_print(LOG_DEBUG_FILTERING,
693                                 "command returned [ %d ]\n",
694                                 retval);
695         }
696
697         g_free(cmd);
698         return (retval == 0);
699 }
700
701 /*!
702  *\brief        Check if a message matches the condition in a matcher
703  *              structure.
704  *
705  *\param        prop Pointer to matcher structure
706  *\param        info Pointer to message info
707  *
708  *\return       gboolean TRUE if a match
709  */
710 static gboolean matcherprop_match(MatcherProp *prop, 
711                                   MsgInfo *info)
712 {
713         time_t t;
714
715         switch(prop->criteria) {
716         case MATCHCRITERIA_ALL:
717                 return TRUE;
718         case MATCHCRITERIA_UNREAD:
719                 return MSG_IS_UNREAD(info->flags);
720         case MATCHCRITERIA_NOT_UNREAD:
721                 return !MSG_IS_UNREAD(info->flags);
722         case MATCHCRITERIA_NEW:
723                 return MSG_IS_NEW(info->flags);
724         case MATCHCRITERIA_NOT_NEW:
725                 return !MSG_IS_NEW(info->flags);
726         case MATCHCRITERIA_MARKED:
727                 return MSG_IS_MARKED(info->flags);
728         case MATCHCRITERIA_NOT_MARKED:
729                 return !MSG_IS_MARKED(info->flags);
730         case MATCHCRITERIA_DELETED:
731                 return MSG_IS_DELETED(info->flags);
732         case MATCHCRITERIA_NOT_DELETED:
733                 return !MSG_IS_DELETED(info->flags);
734         case MATCHCRITERIA_REPLIED:
735                 return MSG_IS_REPLIED(info->flags);
736         case MATCHCRITERIA_NOT_REPLIED:
737                 return !MSG_IS_REPLIED(info->flags);
738         case MATCHCRITERIA_FORWARDED:
739                 return MSG_IS_FORWARDED(info->flags);
740         case MATCHCRITERIA_NOT_FORWARDED:
741                 return !MSG_IS_FORWARDED(info->flags);
742         case MATCHCRITERIA_LOCKED:
743                 return MSG_IS_LOCKED(info->flags);
744         case MATCHCRITERIA_NOT_LOCKED:
745                 return !MSG_IS_LOCKED(info->flags);
746         case MATCHCRITERIA_SPAM:
747                 return MSG_IS_SPAM(info->flags);
748         case MATCHCRITERIA_NOT_SPAM:
749                 return !MSG_IS_SPAM(info->flags);
750         case MATCHCRITERIA_HAS_ATTACHMENT:
751                 return MSG_IS_WITH_ATTACHMENT(info->flags);
752         case MATCHCRITERIA_HAS_NO_ATTACHMENT:
753                 return !MSG_IS_WITH_ATTACHMENT(info->flags);
754         case MATCHCRITERIA_SIGNED:
755                 return MSG_IS_SIGNED(info->flags);
756         case MATCHCRITERIA_NOT_SIGNED:
757                 return !MSG_IS_SIGNED(info->flags);
758         case MATCHCRITERIA_COLORLABEL:
759         {
760                 gint color = MSG_GET_COLORLABEL_VALUE(info->flags);
761                 gboolean ret = (color == prop->value);
762
763                 /* debug output */
764                 if (debug_filtering_session
765                                 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
766                         if (ret) {
767                                 log_print(LOG_DEBUG_FILTERING,
768                                                 "message color value [ %d ] matches color value [ %d ]\n",
769                                                 color, prop->value);
770                         } else {
771                                 log_print(LOG_DEBUG_FILTERING,
772                                                 "message color value [ %d ] does NOT match color value [ %d ]\n",
773                                                 color, prop->value);
774                         }
775                 }
776                 return ret;
777         }
778         case MATCHCRITERIA_NOT_COLORLABEL:
779         {
780                 gint color = MSG_GET_COLORLABEL_VALUE(info->flags);
781                 gboolean ret = (color != prop->value);
782
783                 /* debug output */
784                 if (debug_filtering_session
785                                 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
786                         if (ret) {
787                                 log_print(LOG_DEBUG_FILTERING,
788                                                 "message color value [ %d ] matches color value [ %d ]\n",
789                                                 color, prop->value);
790                         } else {
791                                 log_print(LOG_DEBUG_FILTERING,
792                                                 "message color value [ %d ] does NOT match color value [ %d ]\n",
793                                                 color, prop->value);
794                         }
795                 }
796                 return ret;
797         }
798         case MATCHCRITERIA_IGNORE_THREAD:
799                 return MSG_IS_IGNORE_THREAD(info->flags);
800         case MATCHCRITERIA_NOT_IGNORE_THREAD:
801                 return !MSG_IS_IGNORE_THREAD(info->flags);
802         case MATCHCRITERIA_WATCH_THREAD:
803                 return MSG_IS_WATCH_THREAD(info->flags);
804         case MATCHCRITERIA_NOT_WATCH_THREAD:
805                 return !MSG_IS_WATCH_THREAD(info->flags);
806         case MATCHCRITERIA_SUBJECT:
807                 return matcherprop_string_match(prop, info->subject,
808                                                 prefs_common_translated_header_name("Subject:"));
809         case MATCHCRITERIA_NOT_SUBJECT:
810                 return !matcherprop_string_match(prop, info->subject,
811                                                 prefs_common_translated_header_name("Subject:"));
812         case MATCHCRITERIA_FROM:
813         case MATCHCRITERIA_NOT_FROM:
814         {
815                 gchar *context;
816                 gboolean ret;
817
818                 context = g_strdup_printf(_("%s header"), prefs_common_translated_header_name("From:"));
819                 ret = matcherprop_string_match(prop, info->from, context);
820                 g_free(context);
821                 return (prop->criteria == MATCHCRITERIA_FROM)? ret : !ret;
822         }
823         case MATCHCRITERIA_TO:
824         case MATCHCRITERIA_NOT_TO:
825         {
826                 gchar *context;
827                 gboolean ret;
828
829                 context = g_strdup_printf(_("%s header"), prefs_common_translated_header_name("To:"));
830                 ret = matcherprop_string_match(prop, info->to, context);
831                 g_free(context);
832                 return (prop->criteria == MATCHCRITERIA_TO)? ret : !ret;
833         }
834         case MATCHCRITERIA_CC:
835         case MATCHCRITERIA_NOT_CC:
836         {
837                 gchar *context;
838                 gboolean ret;
839
840                 context = g_strdup_printf(_("%s header"), prefs_common_translated_header_name("Cc:"));
841                 ret = matcherprop_string_match(prop, info->cc, context);
842                 g_free(context);
843                 return (prop->criteria == MATCHCRITERIA_CC)? ret : !ret;
844         }
845         case MATCHCRITERIA_TO_OR_CC:
846         {
847                 gchar *context1, *context2;
848                 gboolean ret;
849
850                 context1 = g_strdup_printf(_("%s header"), prefs_common_translated_header_name("To:"));
851                 context2 = g_strdup_printf(_("%s header"), prefs_common_translated_header_name("Cc:"));
852                 ret = matcherprop_string_match(prop, info->to, context1)
853                         || matcherprop_string_match(prop, info->cc, context2);
854                 g_free(context1);
855                 g_free(context2);
856                 return ret;
857         }
858         case MATCHCRITERIA_NOT_TO_AND_NOT_CC:
859         {
860                 gchar *context1, *context2;
861                 gboolean ret;
862
863                 context1 = g_strdup_printf(_("%s header"), prefs_common_translated_header_name("To:"));
864                 context2 = g_strdup_printf(_("%s header"), prefs_common_translated_header_name("Cc:"));
865                 ret = !(matcherprop_string_match(prop, info->to, context1)
866                         || matcherprop_string_match(prop, info->cc, context2));
867                 g_free(context1);
868                 g_free(context2);
869                 return ret;
870         }
871         case MATCHCRITERIA_TAG:
872         case MATCHCRITERIA_NOT_TAG:
873         {
874                 gboolean ret;
875
876                 ret = matcherprop_tag_match(prop, info, _("Tag"));
877                 return (prop->criteria == MATCHCRITERIA_TAG)? ret : !ret;
878         }
879         case MATCHCRITERIA_TAGGED:
880         case MATCHCRITERIA_NOT_TAGGED:
881         {
882                 gboolean ret;
883
884                 ret = (info->tags != NULL);
885                 return (prop->criteria == MATCHCRITERIA_TAGGED)? ret : !ret;
886         }
887         case MATCHCRITERIA_AGE_GREATER:
888         {
889                 gboolean ret;
890                 gint age;
891
892                 t = time(NULL);
893                 age = ((t - info->date_t) / (60 * 60 * 24));
894                 ret = (age >= prop->value);
895
896                 /* debug output */
897                 if (debug_filtering_session
898                                 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
899                         if (ret) {
900                                 log_print(LOG_DEBUG_FILTERING,
901                                                 "message age [ %d ] is greater than [ %d ]\n",
902                                                 age, prop->value);
903                         } else {
904                                 log_print(LOG_DEBUG_FILTERING,
905                                                 "message age [ %d ] is not greater than [ %d ]\n",
906                                                 age, prop->value);
907                         }
908                 }
909                 return ret;
910         }
911         case MATCHCRITERIA_AGE_LOWER:
912         {
913                 gboolean ret;
914                 gint age;
915
916                 t = time(NULL);
917                 age = ((t - info->date_t) / (60 * 60 * 24));
918                 ret = (age < prop->value);
919
920                 /* debug output */
921                 if (debug_filtering_session
922                                 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
923                         if (ret) {
924                                 log_print(LOG_DEBUG_FILTERING,
925                                                 "message age [ %d ] is lower than [ %d ]\n",
926                                                 age, prop->value);
927                         } else {
928                                 log_print(LOG_DEBUG_FILTERING,
929                                                 "message age [ %d ] is not lower than [ %d ]\n",
930                                                 age, prop->value);
931                         }
932                 }
933                 return ret;
934         }
935         case MATCHCRITERIA_SCORE_GREATER:
936         {
937                 gboolean ret = (info->score > prop->value);
938
939                 /* debug output */
940                 if (debug_filtering_session
941                                 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
942                         if (ret) {
943                                 log_print(LOG_DEBUG_FILTERING,
944                                                 "message score [ %d ] is greater than [ %d ]\n",
945                                                 info->score, prop->value);
946                         } else {
947                                 log_print(LOG_DEBUG_FILTERING,
948                                                 "message score [ %d ] is not greater than [ %d ]\n",
949                                                 info->score, prop->value);
950                         }
951                 }
952                 return ret;
953         }
954         case MATCHCRITERIA_SCORE_LOWER:
955         {
956                 gboolean ret = (info->score < prop->value);
957
958                 /* debug output */
959                 if (debug_filtering_session
960                                 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
961                         if (ret) {
962                                 log_print(LOG_DEBUG_FILTERING,
963                                                 "message score [ %d ] is lower than [ %d ]\n",
964                                                 info->score, prop->value);
965                         } else {
966                                 log_print(LOG_DEBUG_FILTERING,
967                                                 "message score [ %d ] is not lower than [ %d ]\n",
968                                                 info->score, prop->value);
969                         }
970                 }
971                 return ret;
972         }
973         case MATCHCRITERIA_SCORE_EQUAL:
974         {
975                 gboolean ret = (info->score == prop->value);
976
977                 /* debug output */
978                 if (debug_filtering_session
979                                 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
980                         if (ret) {
981                                 log_print(LOG_DEBUG_FILTERING,
982                                                 "message score [ %d ] is equal to [ %d ]\n",
983                                                 info->score, prop->value);
984                         } else {
985                                 log_print(LOG_DEBUG_FILTERING,
986                                                 "message score [ %d ] is not equal to [ %d ]\n",
987                                                 info->score, prop->value);
988                         }
989                 }
990                 return ret;
991         }
992         case MATCHCRITERIA_SIZE_GREATER:
993         {
994                 /* FIXME: info->size is a goffset */
995                 gboolean ret = (info->size > (goffset) prop->value);
996
997                 /* debug output */
998                 if (debug_filtering_session
999                                 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
1000                         if (ret) {
1001                                 log_print(LOG_DEBUG_FILTERING,
1002                                                 "message size is greater than [ %d ]\n",
1003                                                 prop->value);
1004                         } else {
1005                                 log_print(LOG_DEBUG_FILTERING,
1006                                                 "message size is not greater than [ %d ]\n",
1007                                                 prop->value);
1008                         }
1009                 }
1010                 return ret;
1011         }
1012         case MATCHCRITERIA_SIZE_SMALLER:
1013         {
1014                 /* FIXME: info->size is a goffset */
1015                 gboolean ret = (info->size < (goffset) prop->value);
1016
1017                 /* debug output */
1018                 if (debug_filtering_session
1019                                 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
1020                         if (ret) {
1021                                 log_print(LOG_DEBUG_FILTERING,
1022                                                 "message size is smaller than [ %d ]\n",
1023                                                 prop->value);
1024                         } else {
1025                                 log_print(LOG_DEBUG_FILTERING,
1026                                                 "message size is not smaller than [ %d ]\n",
1027                                                 prop->value);
1028                         }
1029                 }
1030                 return ret;
1031         }
1032         case MATCHCRITERIA_SIZE_EQUAL:
1033         {
1034                 /* FIXME: info->size is a goffset */
1035                 gboolean ret = (info->size == (goffset) prop->value);
1036
1037                 /* debug output */
1038                 if (debug_filtering_session
1039                                 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
1040                         if (ret) {
1041                                 log_print(LOG_DEBUG_FILTERING,
1042                                                 "message size is equal to [ %d ]\n",
1043                                                 prop->value);
1044                         } else {
1045                                 log_print(LOG_DEBUG_FILTERING,
1046                                                 "message size is not equal to [ %d ]\n",
1047                                                 prop->value);
1048                         }
1049                 }
1050                 return ret;
1051         }
1052         case MATCHCRITERIA_PARTIAL:
1053         {
1054                 /* FIXME: info->size is a goffset */
1055                 gboolean ret = (info->total_size != 0 && info->size != (goffset)info->total_size);
1056
1057                 /* debug output */
1058                 if (debug_filtering_session
1059                                 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
1060                         if (ret) {
1061                                 log_print(LOG_DEBUG_FILTERING,
1062                                                 "message is partially downloaded, size is less than total size [ %d ])\n",
1063                                                 info->total_size);
1064                         } else {
1065                                 log_print(LOG_DEBUG_FILTERING,
1066                                                 "message is not partially downloaded\n");
1067                         }
1068                 }
1069                 return ret;
1070         }
1071         case MATCHCRITERIA_NOT_PARTIAL:
1072         {
1073                 /* FIXME: info->size is a goffset */
1074                 gboolean ret = (info->total_size == 0 || info->size == (goffset)info->total_size);
1075
1076                 /* debug output */
1077                 if (debug_filtering_session
1078                                 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
1079                         if (ret) {
1080                                 log_print(LOG_DEBUG_FILTERING,
1081                                                 "message is not partially downloaded\n");
1082                         } else {
1083                                 log_print(LOG_DEBUG_FILTERING,
1084                                                 "message is partially downloaded, size is less than total size [ %d ])\n",
1085                                                 info->total_size);
1086                         }
1087                 }
1088                 return ret;
1089         }
1090         case MATCHCRITERIA_NEWSGROUPS:
1091         case MATCHCRITERIA_NOT_NEWSGROUPS:
1092         {
1093                 gchar *context;
1094                 gboolean ret;
1095
1096                 context = g_strdup_printf(_("%s header"),
1097                                                 prefs_common_translated_header_name("Newsgroups:"));
1098                 ret = matcherprop_string_match(prop, info->newsgroups, context);
1099                 g_free(context);
1100                 return (prop->criteria == MATCHCRITERIA_NEWSGROUPS)? ret : !ret;
1101         }
1102         case MATCHCRITERIA_INREPLYTO:
1103         case MATCHCRITERIA_NOT_INREPLYTO:
1104         {
1105                 gchar *context;
1106                 gboolean ret;
1107
1108                 context = g_strdup_printf(_("%s header"),
1109                                                 prefs_common_translated_header_name("In-Reply-To:"));
1110                 ret = matcherprop_string_match(prop, info->inreplyto, context);
1111                 g_free(context);
1112                 return (prop->criteria == MATCHCRITERIA_INREPLYTO)? ret : !ret;
1113         }
1114         case MATCHCRITERIA_REFERENCES:
1115         case MATCHCRITERIA_NOT_REFERENCES:
1116         {
1117                 gchar *context;
1118                 gboolean ret;
1119
1120                 context = g_strdup_printf(_("%s header"),
1121                                                 prefs_common_translated_header_name("References:"));
1122                 ret = matcherprop_list_match(prop, info->references, context);
1123                 g_free(context);
1124                 return (prop->criteria == MATCHCRITERIA_REFERENCES)? ret : !ret;
1125         }
1126         case MATCHCRITERIA_TEST:
1127                 return matcherprop_match_test(prop, info);
1128         case MATCHCRITERIA_NOT_TEST:
1129                 return !matcherprop_match_test(prop, info);
1130         default:
1131                 return FALSE;
1132         }
1133 }
1134
1135 /* ********************* MatcherList *************************** */
1136
1137 /*!
1138  *\brief        Create a new list of matchers 
1139  *
1140  *\param        matchers List of matcher structures
1141  *\param        bool_and Operator
1142  *
1143  *\return       MatcherList * New list
1144  */
1145 MatcherList *matcherlist_new(GSList *matchers, gboolean bool_and)
1146 {
1147         MatcherList *cond;
1148
1149         cond = g_new0(MatcherList, 1);
1150
1151         cond->matchers = matchers;
1152         cond->bool_and = bool_and;
1153
1154         return cond;
1155 }
1156
1157 /*!
1158  *\brief        Frees a list of matchers
1159  *
1160  *\param        cond List of matchers
1161  */
1162 void matcherlist_free(MatcherList *cond)
1163 {
1164         GSList *l;
1165
1166         cm_return_if_fail(cond);
1167         for (l = cond->matchers ; l != NULL ; l = g_slist_next(l)) {
1168                 matcherprop_free((MatcherProp *) l->data);
1169         }
1170         g_slist_free(cond->matchers);
1171         g_free(cond);
1172 }
1173
1174 /*!
1175  *\brief        Skip all headers in a message file
1176  *
1177  *\param        fp Message file
1178  */
1179 static void matcherlist_skip_headers(FILE *fp)
1180 {
1181         gchar buf[BUFFSIZE];
1182
1183         while (procheader_get_one_field(buf, sizeof(buf), fp, NULL) != -1)
1184                 ;
1185 }
1186
1187 /*!
1188  *\brief        Check if a header matches a matcher condition
1189  *
1190  *\param        matcher Matcher structure to check header for
1191  *\param        buf Header name
1192  *
1193  *\return       boolean TRUE if matching header
1194  */
1195 static gboolean matcherprop_match_one_header(MatcherProp *matcher,
1196                                              gchar *buf)
1197 {
1198         gboolean result = FALSE;
1199         Header *header = NULL;
1200
1201         switch (matcher->criteria) {
1202         case MATCHCRITERIA_HEADER:
1203         case MATCHCRITERIA_NOT_HEADER:
1204                 header = procheader_parse_header(buf);
1205                 if (!header)
1206                         return FALSE;
1207                 if (procheader_headername_equal(header->name,
1208                                                 matcher->header)) {
1209                         if (matcher->criteria == MATCHCRITERIA_HEADER)
1210                                 result = matcherprop_string_match(matcher, header->body, _("header"));
1211                         else
1212                                 result = !matcherprop_string_match(matcher, header->body, _("header"));
1213                         procheader_header_free(header);
1214                         return result;
1215                 }
1216                 else {
1217                         procheader_header_free(header);
1218                 }
1219                 break;
1220         case MATCHCRITERIA_HEADERS_PART:
1221         case MATCHCRITERIA_MESSAGE:
1222                 header = procheader_parse_header(buf);
1223                 if (!header)
1224                         return FALSE;
1225                 result = matcherprop_header_line_match(matcher, 
1226                                header->name, header->body, _("header line"));
1227                 procheader_header_free(header);
1228                 return result;
1229         case MATCHCRITERIA_NOT_HEADERS_PART:
1230         case MATCHCRITERIA_NOT_MESSAGE:
1231                 header = procheader_parse_header(buf);
1232                 if (!header)
1233                         return FALSE;
1234                 result = !matcherprop_header_line_match(matcher, 
1235                                header->name, header->body, _("header line"));
1236                 procheader_header_free(header);
1237                 return result;
1238         case MATCHCRITERIA_FOUND_IN_ADDRESSBOOK:
1239         case MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK:
1240                 {
1241                         GSList *address_list = NULL;
1242                         gint match = MATCH_ONE;
1243                         gboolean found = FALSE;
1244
1245                         /* how many address headers are me trying to mach? */
1246                         if (strcasecmp(matcher->header, "Any") == 0)
1247                                 match = MATCH_ANY;
1248                         else if (strcasecmp(matcher->header, "All") == 0)
1249                                         match = MATCH_ALL;
1250
1251                         if (match == MATCH_ONE) {
1252                                 /* matching one address header exactly, is that the right one? */
1253                                 header = procheader_parse_header(buf);
1254                                 if (!header ||
1255                                                 !procheader_headername_equal(header->name, matcher->header))
1256                                         return FALSE;
1257                                 address_list = address_list_append(address_list, header->body);
1258                                 if (address_list == NULL)
1259                                         return FALSE;
1260
1261                         } else {
1262                                 header = procheader_parse_header(buf);
1263                                 if (!header)
1264                                         return FALSE;
1265                                 /* address header is one of the headers we have to match when checking
1266                                    for any address header or all address headers? */
1267                                 if (procheader_headername_equal(header->name, "From") ||
1268                                          procheader_headername_equal(header->name, "To") ||
1269                                          procheader_headername_equal(header->name, "Cc") ||
1270                                          procheader_headername_equal(header->name, "Reply-To") ||
1271                                          procheader_headername_equal(header->name, "Sender"))
1272                                         address_list = address_list_append(address_list, header->body);
1273                                 if (address_list == NULL)
1274                                         return FALSE;
1275                         }
1276
1277                         found = match_with_addresses_in_addressbook
1278                                                         (matcher, address_list, matcher->criteria,
1279                                                          matcher->expr, match);
1280                         g_slist_free(address_list);
1281
1282                         if (matcher->criteria == MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK)
1283                                 return !found;
1284                         else
1285                                 return found;
1286         }
1287         }
1288
1289         return FALSE;
1290 }
1291
1292 /*!
1293  *\brief        Check if the matcher structure wants headers to
1294  *              be matched
1295  *
1296  *\param        matcher Matcher structure
1297  *
1298  *\return       gboolean TRUE if the matcher structure describes
1299  *              a header match condition
1300  */
1301 static gboolean matcherprop_criteria_headers(const MatcherProp *matcher)
1302 {
1303         switch (matcher->criteria) {
1304         case MATCHCRITERIA_HEADER:
1305         case MATCHCRITERIA_NOT_HEADER:
1306         case MATCHCRITERIA_HEADERS_PART:
1307         case MATCHCRITERIA_NOT_HEADERS_PART:
1308         case MATCHCRITERIA_FOUND_IN_ADDRESSBOOK:
1309         case MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK:
1310                 return TRUE;
1311         default:
1312                 return FALSE;
1313         }
1314 }
1315
1316 /*!
1317  *\brief        Check if the matcher structure wants the message
1318  *              to be matched (just perform an action on any
1319  *              message)
1320  *
1321  *\param        matcher Matcher structure
1322  *
1323  *\return       gboolean TRUE if matcher condition should match
1324  *              a message
1325  */
1326 static gboolean matcherprop_criteria_message(MatcherProp *matcher)
1327 {
1328         switch (matcher->criteria) {
1329         case MATCHCRITERIA_MESSAGE:
1330         case MATCHCRITERIA_NOT_MESSAGE:
1331                 return TRUE;
1332         default:
1333                 return FALSE;
1334         }
1335 }
1336
1337 /*!
1338  *\brief        Check if a list of conditions matches one header in
1339  *              a message file.
1340  *
1341  *\param        matchers List of conditions
1342  *\param        fp Message file
1343  *
1344  *\return       gboolean TRUE if one of the headers is matched by
1345  *              the list of conditions. 
1346  */
1347 static gboolean matcherlist_match_headers(MatcherList *matchers, FILE *fp)
1348 {
1349         GSList *l;
1350         gchar buf[BUFFSIZE];
1351
1352         while (procheader_get_one_field(buf, sizeof(buf), fp, NULL) != -1) {
1353                 for (l = matchers->matchers ; l != NULL ; l = g_slist_next(l)) {
1354                         MatcherProp *matcher = (MatcherProp *) l->data;
1355                         gint match = MATCH_ANY;
1356
1357                         if (matcher->done)
1358                                 continue;
1359
1360                         /* determine the match range (all, any are our concern here) */
1361                         if (matcher->criteria == MATCHCRITERIA_NOT_HEADERS_PART ||
1362                             matcher->criteria == MATCHCRITERIA_NOT_MESSAGE) {
1363                                 match = MATCH_ALL;
1364
1365                         } else if (matcher->criteria == MATCHCRITERIA_FOUND_IN_ADDRESSBOOK ||
1366                                            matcher->criteria == MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK) {
1367                                 Header *header = NULL;
1368
1369                                 /* address header is one of the headers we have to match when checking
1370                                    for any address header or all address headers? */
1371                                 header = procheader_parse_header(buf);
1372                                 if (header &&
1373                                         (procheader_headername_equal(header->name, "From") ||
1374                                          procheader_headername_equal(header->name, "To") ||
1375                                          procheader_headername_equal(header->name, "Cc") ||
1376                                          procheader_headername_equal(header->name, "Reply-To") ||
1377                                          procheader_headername_equal(header->name, "Sender"))) {
1378
1379                                         if (strcasecmp(matcher->header, "Any") == 0)
1380                                                 match = MATCH_ANY;
1381                                         else if (strcasecmp(matcher->header, "All") == 0)
1382                                                 match = MATCH_ALL;
1383                                         else
1384                                                 match = MATCH_ONE;
1385                                 } else {
1386                                         /* further call to matcherprop_match_one_header() can't match
1387                                            and it irrelevant, so: don't alter the match result */
1388                                         continue;
1389                                 }
1390                         }
1391
1392                         /* ZERO line must NOT match for the rule to match.
1393                          */
1394                         if (match == MATCH_ALL) {
1395                                 if (matcherprop_match_one_header(matcher, buf)) {
1396                                         matcher->result = TRUE;
1397                                 } else {
1398                                         matcher->result = FALSE;
1399                                         matcher->done = TRUE;
1400                                 }
1401                         /* else, just one line matching is enough for the rule to match
1402                          */
1403                         } else if (matcherprop_criteria_headers(matcher) ||
1404                                    matcherprop_criteria_message(matcher)) {
1405                                 if (matcherprop_match_one_header(matcher, buf)) {
1406                                         matcher->result = TRUE;
1407                                         matcher->done = TRUE;
1408                                 }
1409                         }
1410                         
1411                         /* if the rule matched and the matchers are OR, no need to
1412                          * check the others */
1413                         if (matcher->result && matcher->done) {
1414                                 if (!matchers->bool_and)
1415                                         return TRUE;
1416                         }
1417                 }
1418         }
1419
1420         return FALSE;
1421 }
1422
1423 /*!
1424  *\brief        Check if a matcher wants to check the message body
1425  *
1426  *\param        matcher Matcher structure
1427  *
1428  *\return       gboolean TRUE if body must be matched.
1429  */
1430 static gboolean matcherprop_criteria_body(const MatcherProp *matcher)
1431 {
1432         switch (matcher->criteria) {
1433         case MATCHCRITERIA_BODY_PART:
1434         case MATCHCRITERIA_NOT_BODY_PART:
1435                 return TRUE;
1436         default:
1437                 return FALSE;
1438         }
1439 }
1440
1441 /*!
1442  *\brief        Check if a line in a message file's body matches
1443  *              the criteria
1444  *
1445  *\param        matchers List of conditions
1446  *\param        fp Message file
1447  *
1448  *\return       gboolean TRUE if succesful match
1449  */
1450 static gboolean matcherlist_match_body(MatcherList *matchers, gboolean body_only, MsgInfo *info)
1451 {
1452         GSList *l;
1453         MimeInfo *mimeinfo = NULL;
1454         MimeInfo *partinfo = NULL;
1455         gchar buf[BUFFSIZE];
1456         gboolean first_text_found = FALSE;
1457         FILE *outfp = NULL;
1458
1459         cm_return_val_if_fail(info != NULL, FALSE);
1460
1461         mimeinfo = procmime_scan_message(info);
1462
1463         /* Skip headers */
1464         partinfo = procmime_mimeinfo_next(mimeinfo);
1465
1466         for (; partinfo != NULL; partinfo = procmime_mimeinfo_next(partinfo)) {
1467
1468                 if (partinfo->type != MIMETYPE_TEXT && body_only)
1469                         continue;
1470
1471                 if (partinfo->type == MIMETYPE_TEXT) {
1472                         first_text_found = TRUE;
1473                         outfp = procmime_get_text_content(partinfo);
1474                 } else
1475                         outfp = procmime_get_binary_content(partinfo);
1476
1477                 if (!outfp) {
1478                         procmime_mimeinfo_free_all(mimeinfo);
1479                         return FALSE;
1480                 }
1481
1482                 while (fgets(buf, sizeof(buf), outfp) != NULL) {
1483                         strretchomp(buf);
1484
1485                         for (l = matchers->matchers ; l != NULL ; l = g_slist_next(l)) {
1486                                 MatcherProp *matcher = (MatcherProp *) l->data;
1487
1488                                 if (matcher->done) 
1489                                         continue;
1490
1491                                 /* Don't scan non-text parts when looking in body, only
1492                                  * when looking in whole message
1493                                  */
1494                                 if (partinfo && partinfo->type != MIMETYPE_TEXT &&
1495                                 (matcher->criteria == MATCHCRITERIA_NOT_BODY_PART ||
1496                                 matcher->criteria == MATCHCRITERIA_BODY_PART))
1497                                         continue;
1498
1499                                 /* if the criteria is ~body_part or ~message, ZERO lines
1500                                  * must match for the rule to match.
1501                                  */
1502                                 if (matcher->criteria == MATCHCRITERIA_NOT_BODY_PART ||
1503                                     matcher->criteria == MATCHCRITERIA_NOT_MESSAGE) {
1504                                         if (matcherprop_string_match(matcher, buf, 
1505                                                                 _("body line"))) {
1506                                                 matcher->result = FALSE;
1507                                                 matcher->done = TRUE;
1508                                         } else
1509                                                 matcher->result = TRUE;
1510                                 /* else, just one line has to match */
1511                                 } else if (matcherprop_criteria_body(matcher) ||
1512                                            matcherprop_criteria_message(matcher)) {
1513                                         if (matcherprop_string_match(matcher, buf,
1514                                                                 _("body line"))) {
1515                                                 matcher->result = TRUE;
1516                                                 matcher->done = TRUE;
1517                                         }
1518                                 }
1519
1520                                 /* if the matchers are OR'ed and the rule matched,
1521                                  * no need to check the others. */
1522                                 if (matcher->result && matcher->done) {
1523                                         if (!matchers->bool_and) {
1524                                                 procmime_mimeinfo_free_all(mimeinfo);
1525                                                 fclose(outfp);
1526                                                 return TRUE;
1527                                         }
1528                                 }
1529                         }
1530                 }
1531                 fclose(outfp);
1532
1533                 if (body_only && first_text_found)
1534                         break;
1535         }
1536         procmime_mimeinfo_free_all(mimeinfo);
1537
1538         return FALSE;
1539 }
1540
1541 /*!
1542  *\brief        Check if a message file matches criteria
1543  *
1544  *\param        matchers Criteria
1545  *\param        info Message info
1546  *\param        result Default result
1547  *
1548  *\return       gboolean TRUE if matched
1549  */
1550 static gboolean matcherlist_match_file(MatcherList *matchers, MsgInfo *info,
1551                                 gboolean result)
1552 {
1553         gboolean read_headers;
1554         gboolean read_body;
1555         gboolean body_only;
1556         GSList *l;
1557         FILE *fp;
1558         gchar *file;
1559
1560         /* file need to be read ? */
1561
1562         read_headers = FALSE;
1563         read_body = FALSE;
1564         body_only = TRUE;
1565         for (l = matchers->matchers ; l != NULL ; l = g_slist_next(l)) {
1566                 MatcherProp *matcher = (MatcherProp *) l->data;
1567
1568                 if (matcherprop_criteria_headers(matcher))
1569                         read_headers = TRUE;
1570                 if (matcherprop_criteria_body(matcher))
1571                         read_body = TRUE;
1572                 if (matcherprop_criteria_message(matcher)) {
1573                         read_headers = TRUE;
1574                         read_body = TRUE;
1575                         body_only = FALSE;
1576                 }
1577                 matcher->result = FALSE;
1578                 matcher->done = FALSE;
1579         }
1580
1581         if (!read_headers && !read_body)
1582                 return result;
1583
1584         file = procmsg_get_message_file_full(info, read_headers, read_body);
1585         if (file == NULL)
1586                 return FALSE;
1587
1588         if ((fp = g_fopen(file, "rb")) == NULL) {
1589                 FILE_OP_ERROR(file, "fopen");
1590                 g_free(file);
1591                 return result;
1592         }
1593
1594         /* read the headers */
1595
1596         if (read_headers) {
1597                 if (matcherlist_match_headers(matchers, fp))
1598                         read_body = FALSE;
1599         } else {
1600                 matcherlist_skip_headers(fp);
1601         }
1602
1603         /* read the body */
1604         if (read_body) {
1605                 matcherlist_match_body(matchers, body_only, info);
1606         }
1607         
1608         for (l = matchers->matchers; l != NULL; l = g_slist_next(l)) {
1609                 MatcherProp *matcher = (MatcherProp *) l->data;
1610
1611                 if (matcherprop_criteria_headers(matcher) ||
1612                     matcherprop_criteria_body(matcher)    ||
1613                     matcherprop_criteria_message(matcher)) {
1614                         if (matcher->result) {
1615                                 if (!matchers->bool_and) {
1616                                         result = TRUE;
1617                                         break;
1618                                 }
1619                         }
1620                         else {
1621                                 if (matchers->bool_and) {
1622                                         result = FALSE;
1623                                         break;
1624                                 }
1625                         }
1626                 }                       
1627         }
1628
1629         g_free(file);
1630
1631         fclose(fp);
1632         
1633         return result;
1634 }
1635
1636 /*!
1637  *\brief        Test list of conditions on a message.
1638  *
1639  *\param        matchers List of conditions
1640  *\param        info Message info
1641  *
1642  *\return       gboolean TRUE if matched
1643  */
1644 gboolean matcherlist_match(MatcherList *matchers, MsgInfo *info)
1645 {
1646         GSList *l;
1647         gboolean result;
1648
1649         if (!matchers)
1650                 return FALSE;
1651
1652         if (matchers->bool_and)
1653                 result = TRUE;
1654         else
1655                 result = FALSE;
1656
1657         /* test the cached elements */
1658
1659         for (l = matchers->matchers; l != NULL ;l = g_slist_next(l)) {
1660                 MatcherProp *matcher = (MatcherProp *) l->data;
1661
1662                 if (debug_filtering_session) {
1663                         gchar *buf = matcherprop_to_string(matcher);
1664                         log_print(LOG_DEBUG_FILTERING, _("checking if message matches [ %s ]\n"), buf);
1665                         g_free(buf);
1666                 }
1667
1668                 switch(matcher->criteria) {
1669                 case MATCHCRITERIA_ALL:
1670                 case MATCHCRITERIA_UNREAD:
1671                 case MATCHCRITERIA_NOT_UNREAD:
1672                 case MATCHCRITERIA_NEW:
1673                 case MATCHCRITERIA_NOT_NEW:
1674                 case MATCHCRITERIA_MARKED:
1675                 case MATCHCRITERIA_NOT_MARKED:
1676                 case MATCHCRITERIA_DELETED:
1677                 case MATCHCRITERIA_NOT_DELETED:
1678                 case MATCHCRITERIA_REPLIED:
1679                 case MATCHCRITERIA_NOT_REPLIED:
1680                 case MATCHCRITERIA_FORWARDED:
1681                 case MATCHCRITERIA_NOT_FORWARDED:
1682                 case MATCHCRITERIA_LOCKED:
1683                 case MATCHCRITERIA_NOT_LOCKED:
1684                 case MATCHCRITERIA_SPAM:
1685                 case MATCHCRITERIA_NOT_SPAM:
1686                 case MATCHCRITERIA_HAS_ATTACHMENT:
1687                 case MATCHCRITERIA_HAS_NO_ATTACHMENT:
1688                 case MATCHCRITERIA_SIGNED:
1689                 case MATCHCRITERIA_NOT_SIGNED:
1690                 case MATCHCRITERIA_COLORLABEL:
1691                 case MATCHCRITERIA_NOT_COLORLABEL:
1692                 case MATCHCRITERIA_IGNORE_THREAD:
1693                 case MATCHCRITERIA_NOT_IGNORE_THREAD:
1694                 case MATCHCRITERIA_WATCH_THREAD:
1695                 case MATCHCRITERIA_NOT_WATCH_THREAD:
1696                 case MATCHCRITERIA_SUBJECT:
1697                 case MATCHCRITERIA_NOT_SUBJECT:
1698                 case MATCHCRITERIA_FROM:
1699                 case MATCHCRITERIA_NOT_FROM:
1700                 case MATCHCRITERIA_TO:
1701                 case MATCHCRITERIA_NOT_TO:
1702                 case MATCHCRITERIA_CC:
1703                 case MATCHCRITERIA_NOT_CC:
1704                 case MATCHCRITERIA_TO_OR_CC:
1705                 case MATCHCRITERIA_NOT_TO_AND_NOT_CC:
1706                 case MATCHCRITERIA_TAG:
1707                 case MATCHCRITERIA_NOT_TAG:
1708                 case MATCHCRITERIA_TAGGED:
1709                 case MATCHCRITERIA_NOT_TAGGED:
1710                 case MATCHCRITERIA_AGE_GREATER:
1711                 case MATCHCRITERIA_AGE_LOWER:
1712                 case MATCHCRITERIA_NEWSGROUPS:
1713                 case MATCHCRITERIA_NOT_NEWSGROUPS:
1714                 case MATCHCRITERIA_INREPLYTO:
1715                 case MATCHCRITERIA_NOT_INREPLYTO:
1716                 case MATCHCRITERIA_REFERENCES:
1717                 case MATCHCRITERIA_NOT_REFERENCES:
1718                 case MATCHCRITERIA_SCORE_GREATER:
1719                 case MATCHCRITERIA_SCORE_LOWER:
1720                 case MATCHCRITERIA_SCORE_EQUAL:
1721                 case MATCHCRITERIA_SIZE_GREATER:
1722                 case MATCHCRITERIA_SIZE_SMALLER:
1723                 case MATCHCRITERIA_SIZE_EQUAL:
1724                 case MATCHCRITERIA_TEST:
1725                 case MATCHCRITERIA_NOT_TEST:
1726                 case MATCHCRITERIA_PARTIAL:
1727                 case MATCHCRITERIA_NOT_PARTIAL:
1728                         if (matcherprop_match(matcher, info)) {
1729                                 if (!matchers->bool_and) {
1730                                         if (debug_filtering_session)
1731                                                 log_status_ok(LOG_DEBUG_FILTERING, _("message matches\n"));
1732                                         return TRUE;
1733                                 }
1734                         }
1735                         else {
1736                                 if (matchers->bool_and) {
1737                                         if (debug_filtering_session)
1738                                                 log_status_nok(LOG_DEBUG_FILTERING, _("message does not match\n"));
1739                                         return FALSE;
1740                                 }
1741                         }
1742                 }
1743         }
1744
1745         /* test the condition on the file */
1746
1747         if (matcherlist_match_file(matchers, info, result)) {
1748                 if (!matchers->bool_and) {
1749                         if (debug_filtering_session)
1750                                 log_status_ok(LOG_DEBUG_FILTERING, _("message matches\n"));
1751                         return TRUE;
1752                 }
1753         } else {
1754                 if (matchers->bool_and) {
1755                         if (debug_filtering_session)
1756                                 log_status_nok(LOG_DEBUG_FILTERING, _("message does not match\n"));
1757                         return FALSE;
1758                 }
1759         }
1760
1761         if (debug_filtering_session) {
1762                 if (result)
1763                         log_status_ok(LOG_DEBUG_FILTERING, _("message matches\n"));
1764                 else
1765                         log_status_nok(LOG_DEBUG_FILTERING, _("message does not match\n"));
1766         }
1767         return result;
1768 }
1769
1770
1771 static gint quote_filter_str(gchar * result, guint size,
1772                              const gchar * path)
1773 {
1774         const gchar * p;
1775         gchar * result_p;
1776         guint remaining;
1777
1778         result_p = result;
1779         remaining = size;
1780
1781         for(p = path ; * p != '\0' ; p ++) {
1782
1783                 if ((* p != '\"') && (* p != '\\')) {
1784                         if (remaining > 0) {
1785                                 * result_p = * p;
1786                                 result_p ++; 
1787                                 remaining --;
1788                         }
1789                         else {
1790                                 result[size - 1] = '\0';
1791                                 return -1;
1792                         }
1793                 }
1794                 else { 
1795                         if (remaining >= 2) {
1796                                 * result_p = '\\';
1797                                 result_p ++; 
1798                                 * result_p = * p;
1799                                 result_p ++; 
1800                                 remaining -= 2;
1801                         }
1802                         else {
1803                                 result[size - 1] = '\0';
1804                                 return -1;
1805                         }
1806                 }
1807         }
1808         if (remaining > 0) {
1809                 * result_p = '\0';
1810         }
1811         else {
1812                 result[size - 1] = '\0';
1813                 return -1;
1814         }
1815   
1816         return 0;
1817 }
1818
1819
1820 gchar * matcher_quote_str(const gchar * src)
1821 {
1822         gchar * res;
1823         gint len;
1824         
1825         len = strlen(src) * 2 + 1;
1826         res = g_malloc(len);
1827         quote_filter_str(res, len, src);
1828         
1829         return res;
1830 }
1831
1832 /*!
1833  *\brief        Convert a matcher structure to a string
1834  *
1835  *\param        matcher Matcher structure
1836  *
1837  *\return       gchar * Newly allocated string
1838  */
1839 gchar *matcherprop_to_string(MatcherProp *matcher)
1840 {
1841         gchar *matcher_str = NULL;
1842         const gchar *criteria_str;
1843         const gchar *matchtype_str;
1844         int i;
1845         gchar * quoted_expr;
1846         gchar * quoted_header;
1847         
1848         criteria_str = NULL;
1849         for (i = 0; i < (int) (sizeof(matchparser_tab) / sizeof(MatchParser)); i++) {
1850                 if (matchparser_tab[i].id == matcher->criteria)
1851                         criteria_str = matchparser_tab[i].str;
1852         }
1853         if (criteria_str == NULL)
1854                 return NULL;
1855
1856         switch (matcher->criteria) {
1857         case MATCHCRITERIA_AGE_GREATER:
1858         case MATCHCRITERIA_AGE_LOWER:
1859         case MATCHCRITERIA_SCORE_GREATER:
1860         case MATCHCRITERIA_SCORE_LOWER:
1861         case MATCHCRITERIA_SCORE_EQUAL:
1862         case MATCHCRITERIA_SIZE_GREATER:
1863         case MATCHCRITERIA_SIZE_SMALLER:
1864         case MATCHCRITERIA_SIZE_EQUAL:
1865         case MATCHCRITERIA_COLORLABEL:
1866         case MATCHCRITERIA_NOT_COLORLABEL:
1867                 return g_strdup_printf("%s %i", criteria_str, matcher->value);
1868         case MATCHCRITERIA_ALL:
1869         case MATCHCRITERIA_UNREAD:
1870         case MATCHCRITERIA_NOT_UNREAD:
1871         case MATCHCRITERIA_NEW:
1872         case MATCHCRITERIA_NOT_NEW:
1873         case MATCHCRITERIA_MARKED:
1874         case MATCHCRITERIA_NOT_MARKED:
1875         case MATCHCRITERIA_DELETED:
1876         case MATCHCRITERIA_NOT_DELETED:
1877         case MATCHCRITERIA_REPLIED:
1878         case MATCHCRITERIA_NOT_REPLIED:
1879         case MATCHCRITERIA_FORWARDED:
1880         case MATCHCRITERIA_NOT_FORWARDED:
1881         case MATCHCRITERIA_LOCKED:
1882         case MATCHCRITERIA_NOT_LOCKED:
1883         case MATCHCRITERIA_SPAM:
1884         case MATCHCRITERIA_NOT_SPAM:
1885         case MATCHCRITERIA_HAS_ATTACHMENT:
1886         case MATCHCRITERIA_HAS_NO_ATTACHMENT:
1887         case MATCHCRITERIA_SIGNED:
1888         case MATCHCRITERIA_NOT_SIGNED:
1889         case MATCHCRITERIA_PARTIAL:
1890         case MATCHCRITERIA_NOT_PARTIAL:
1891         case MATCHCRITERIA_IGNORE_THREAD:
1892         case MATCHCRITERIA_NOT_IGNORE_THREAD:
1893         case MATCHCRITERIA_WATCH_THREAD:
1894         case MATCHCRITERIA_NOT_WATCH_THREAD:
1895         case MATCHCRITERIA_TAGGED:
1896         case MATCHCRITERIA_NOT_TAGGED:
1897                 return g_strdup(criteria_str);
1898         case MATCHCRITERIA_TEST:
1899         case MATCHCRITERIA_NOT_TEST:
1900                 quoted_expr = matcher_quote_str(matcher->expr);
1901                 matcher_str = g_strdup_printf("%s \"%s\"",
1902                                               criteria_str, quoted_expr);
1903                 g_free(quoted_expr);
1904                 return matcher_str;
1905         case MATCHCRITERIA_FOUND_IN_ADDRESSBOOK:
1906         case MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK:
1907                 quoted_header = matcher_quote_str(matcher->header);
1908                 quoted_expr = matcher_quote_str(matcher->expr);
1909                 matcher_str = g_strdup_printf("%s \"%s\" in \"%s\"",
1910                                               criteria_str, quoted_header, quoted_expr);
1911                 g_free(quoted_header);
1912                 g_free(quoted_expr);
1913                 return matcher_str;
1914         }
1915
1916         matchtype_str = NULL;
1917         for (i = 0; i < sizeof matchparser_tab / sizeof matchparser_tab[0]; i++) {
1918                 if (matchparser_tab[i].id == matcher->matchtype)
1919                         matchtype_str = matchparser_tab[i].str;
1920         }
1921
1922         if (matchtype_str == NULL)
1923                 return NULL;
1924
1925         switch (matcher->matchtype) {
1926         case MATCHTYPE_MATCH:
1927         case MATCHTYPE_MATCHCASE:
1928         case MATCHTYPE_REGEXP:
1929         case MATCHTYPE_REGEXPCASE:
1930                 quoted_expr = matcher_quote_str(matcher->expr);
1931                 if (matcher->header) {
1932                         quoted_header = matcher_quote_str(matcher->header);
1933                         matcher_str = g_strdup_printf
1934                                         ("%s \"%s\" %s \"%s\"",
1935                                          criteria_str, quoted_header,
1936                                          matchtype_str, quoted_expr);
1937                         g_free(quoted_header);
1938                 }
1939                 else
1940                         matcher_str = g_strdup_printf
1941                                         ("%s %s \"%s\"", criteria_str,
1942                                          matchtype_str, quoted_expr);
1943                 g_free(quoted_expr);
1944                 break;
1945         }
1946
1947         return matcher_str;
1948 }
1949
1950 /*!
1951  *\brief        Convert a list of conditions to a string
1952  *
1953  *\param        matchers List of conditions
1954  *
1955  *\return       gchar * Newly allocated string
1956  */
1957 gchar *matcherlist_to_string(const MatcherList *matchers)
1958 {
1959         gint count;
1960         gchar **vstr;
1961         GSList *l;
1962         gchar **cur_str;
1963         gchar *result = NULL;
1964
1965         count = g_slist_length(matchers->matchers);
1966         vstr = g_new(gchar *, count + 1);
1967
1968         for (l = matchers->matchers, cur_str = vstr; l != NULL;
1969              l = g_slist_next(l), cur_str ++) {
1970                 *cur_str = matcherprop_to_string((MatcherProp *) l->data);
1971                 if (*cur_str == NULL)
1972                         break;
1973         }
1974         *cur_str = NULL;
1975         
1976         if (matchers->bool_and)
1977                 result = g_strjoinv(" & ", vstr);
1978         else
1979                 result = g_strjoinv(" | ", vstr);
1980
1981         for (cur_str = vstr ; *cur_str != NULL ; cur_str ++)
1982                 g_free(*cur_str);
1983         g_free(vstr);
1984
1985         return result;
1986 }
1987
1988
1989 #define STRLEN_ZERO(s) ((s) ? strlen(s) : 0)
1990 #define STRLEN_DEFAULT(s,d) ((s) ? strlen(s) : STRLEN_ZERO(d))
1991
1992 static void add_str_default(gchar ** dest,
1993                             const gchar * s, const gchar * d)
1994 {
1995         gchar quoted_str[4096];
1996         const gchar * str;
1997         
1998         if (s != NULL)
1999                 str = s;
2000         else
2001                 str = d;
2002         
2003         quote_cmd_argument(quoted_str, sizeof(quoted_str), str);
2004         strcpy(* dest, quoted_str);
2005         
2006         (* dest) += strlen(* dest);
2007 }
2008
2009 /* matching_build_command() - preferably cmd should be unescaped */
2010 /*!
2011  *\brief        Build the command-line to execute
2012  *
2013  *\param        cmd String with command-line specifiers
2014  *\param        info Message info to use for command
2015  *
2016  *\return       gchar * Newly allocated string
2017  */
2018 gchar *matching_build_command(const gchar *cmd, MsgInfo *info)
2019 {
2020         const gchar *s = cmd;
2021         gchar *filename = NULL;
2022         gchar *processed_cmd;
2023         gchar *p;
2024         gint size;
2025
2026         const gchar *const no_subject    = _("(none)") ;
2027         const gchar *const no_from       = _("(none)") ;
2028         const gchar *const no_to         = _("(none)") ;
2029         const gchar *const no_cc         = _("(none)") ;
2030         const gchar *const no_date       = _("(none)") ;
2031         const gchar *const no_msgid      = _("(none)") ;
2032         const gchar *const no_newsgroups = _("(none)") ;
2033         const gchar *const no_references = _("(none)") ;
2034
2035         size = STRLEN_ZERO(cmd) + 1;
2036         while (*s != '\0') {
2037                 if (*s == '%') {
2038                         s++;
2039                         switch (*s) {
2040                         case '%':
2041                                 size -= 1;
2042                                 break;
2043                         case 's': /* subject */
2044                                 size += STRLEN_DEFAULT(info->subject, no_subject) - 2;
2045                                 break;
2046                         case 'f': /* from */
2047                                 size += STRLEN_DEFAULT(info->from, no_from) - 2;
2048                                 break;
2049                         case 't': /* to */
2050                                 size += STRLEN_DEFAULT(info->to, no_to) - 2;
2051                                 break;
2052                         case 'c': /* cc */
2053                                 size += STRLEN_DEFAULT(info->cc, no_cc) - 2;
2054                                 break;
2055                         case 'd': /* date */
2056                                 size += STRLEN_DEFAULT(info->date, no_date) - 2;
2057                                 break;
2058                         case 'i': /* message-id */
2059                                 size += STRLEN_DEFAULT(info->msgid, no_msgid) - 2;
2060                                 break;
2061                         case 'n': /* newsgroups */
2062                                 size += STRLEN_DEFAULT(info->newsgroups, no_newsgroups) - 2;
2063                                 break;
2064                         case 'r': /* references */
2065                                 /* FIXME: using the inreplyto header for reference */
2066                                 size += STRLEN_DEFAULT(info->inreplyto, no_references) - 2;
2067                                 break;
2068                         case 'F': /* file */
2069                                 if (filename == NULL)
2070                                         filename = folder_item_fetch_msg(info->folder, info->msgnum);
2071                                 
2072                                 if (filename == NULL) {
2073                                         g_warning("filename is not set");
2074                                         return NULL;
2075                                 }
2076                                 else {
2077                                         size += strlen(filename) - 2;
2078                                 }
2079                                 break;
2080                         }
2081                         s++;
2082                 }
2083                 else s++;
2084         }
2085         
2086         /* as the string can be quoted, we double the result */
2087         size *= 2;
2088
2089         processed_cmd = g_new0(gchar, size);
2090         s = cmd;
2091         p = processed_cmd;
2092
2093         while (*s != '\0') {
2094                 if (*s == '%') {
2095                         s++;
2096                         switch (*s) {
2097                         case '%':
2098                                 *p = '%';
2099                                 p++;
2100                                 break;
2101                         case 's': /* subject */
2102                                 add_str_default(&p, info->subject,
2103                                                 no_subject);
2104                                 break;
2105                         case 'f': /* from */
2106                                 add_str_default(&p, info->from,
2107                                                 no_from);
2108                                 break;
2109                         case 't': /* to */
2110                                 add_str_default(&p, info->to,
2111                                                 no_to);
2112                                 break;
2113                         case 'c': /* cc */
2114                                 add_str_default(&p, info->cc,
2115                                                 no_cc);
2116                                 break;
2117                         case 'd': /* date */
2118                                 add_str_default(&p, info->date,
2119                                                 no_date);
2120                                 break;
2121                         case 'i': /* message-id */
2122                                 add_str_default(&p, info->msgid,
2123                                                 no_msgid);
2124                                 break;
2125                         case 'n': /* newsgroups */
2126                                 add_str_default(&p, info->newsgroups,
2127                                                 no_newsgroups);
2128                                 break;
2129                         case 'r': /* references */
2130                                 /* FIXME: using the inreplyto header for references */
2131                                 add_str_default(&p, info->inreplyto, no_references);
2132                                 break;
2133                         case 'F': /* file */
2134                                 if (filename != NULL)
2135                                         add_str_default(&p, filename, NULL);
2136                                 break;
2137                         default:
2138                                 *p = '%';
2139                                 p++;
2140                                 *p = *s;
2141                                 p++;
2142                                 break;
2143                         }
2144                         s++;
2145                 }
2146                 else {
2147                         *p = *s;
2148                         p++;
2149                         s++;
2150                 }
2151         }
2152         g_free(filename);
2153         
2154         return processed_cmd;
2155 }
2156 #undef STRLEN_DEFAULT
2157 #undef STRLEN_ZERO
2158
2159 /* ************************************************************ */
2160
2161
2162 /*!
2163  *\brief        Write filtering list to file
2164  *
2165  *\param        fp File
2166  *\param        prefs_filtering List of filtering conditions
2167  */
2168 static int prefs_filtering_write(FILE *fp, GSList *prefs_filtering)
2169 {
2170         GSList *cur = NULL;
2171
2172         for (cur = prefs_filtering; cur != NULL; cur = cur->next) {
2173                 gchar *filtering_str = NULL;
2174                 gchar *tmp_name = NULL;
2175                 FilteringProp *prop = NULL;
2176
2177                 if (NULL == (prop = (FilteringProp *) cur->data))
2178                         continue;
2179                 
2180                 if (NULL == (filtering_str = filteringprop_to_string(prop)))
2181                         continue;
2182
2183                 if (prop->enabled) {
2184                         if (fputs("enabled ", fp) == EOF) {
2185                                 FILE_OP_ERROR("filtering config", "fputs");
2186                                 return -1;
2187                         }
2188                 } else {
2189                         if (fputs("disabled ", fp) == EOF) {
2190                                 FILE_OP_ERROR("filtering config", "fputs");
2191                                 return -1;
2192                         }
2193                 }
2194
2195                 if (fputs("rulename \"", fp) == EOF) {
2196                         FILE_OP_ERROR("filtering config", "fputs");
2197                         g_free(filtering_str);
2198                         return -1;
2199                 }
2200                 tmp_name = prop->name;
2201                 while (tmp_name && *tmp_name != '\0') {
2202                         if (*tmp_name != '"') {
2203                                 if (fputc(*tmp_name, fp) == EOF) {
2204                                         FILE_OP_ERROR("filtering config", "fputs || fputc");
2205                                         g_free(filtering_str);
2206                                         return -1;
2207                                 }
2208                         } else if (*tmp_name == '"') {
2209                                 if (fputc('\\', fp) == EOF ||
2210                                     fputc('"', fp) == EOF) {
2211                                         FILE_OP_ERROR("filtering config", "fputs || fputc");
2212                                         g_free(filtering_str);
2213                                         return -1;
2214                                 }
2215                         }
2216                         tmp_name ++;
2217                 }
2218                 if (fputs("\" ", fp) == EOF) {
2219                         FILE_OP_ERROR("filtering config", "fputs");
2220                         g_free(filtering_str);
2221                         return -1;
2222                 }
2223
2224                 if (prop->account_id != 0) {
2225                         gchar *tmp = NULL;
2226
2227                         tmp = g_strdup_printf("account %d ", prop->account_id);
2228                         if (fputs(tmp, fp) == EOF) {
2229                                 FILE_OP_ERROR("filtering config", "fputs");
2230                                 g_free(tmp);
2231                                 return -1;
2232                         }
2233                         g_free(tmp);
2234                 }
2235
2236                 if(fputs(filtering_str, fp) == EOF ||
2237                     fputc('\n', fp) == EOF) {
2238                         FILE_OP_ERROR("filtering config", "fputs || fputc");
2239                         g_free(filtering_str);
2240                         return -1;
2241                 }
2242                 g_free(filtering_str);
2243         }
2244         
2245         return 0;
2246 }
2247
2248 typedef struct _NodeLoopData {
2249         FILE *fp;
2250         gboolean error;
2251 } NodeLoopData;
2252
2253 /*!
2254  *\brief        Write matchers from a folder item
2255  *
2256  *\param        node Node with folder info
2257  *\param        data File pointer
2258  *
2259  *\return       gboolean FALSE
2260  */
2261 static gboolean prefs_matcher_write_func(GNode *node, gpointer d)
2262 {
2263         FolderItem *item;
2264         NodeLoopData *data = (NodeLoopData *)d;
2265         gchar *id;
2266         GSList *prefs_filtering;
2267
2268         item = node->data;
2269         /* prevent warning */
2270         if (item->path == NULL)
2271                 return FALSE;
2272         id = folder_item_get_identifier(item);
2273         if (id == NULL)
2274                 return FALSE;
2275         prefs_filtering = item->prefs->processing;
2276
2277         if (prefs_filtering != NULL) {
2278                 if (fprintf(data->fp, "[%s]\n", id) < 0) {
2279                         data->error = TRUE;
2280                         goto fail;
2281                 }
2282                 if (prefs_filtering_write(data->fp, prefs_filtering) < 0) {
2283                         data->error = TRUE;
2284                         goto fail;
2285                 }
2286                 if (fputc('\n', data->fp) == EOF) {
2287                         data->error = TRUE;
2288                         goto fail;
2289                 }
2290         }
2291 fail:
2292         g_free(id);
2293
2294         return FALSE;
2295 }
2296
2297 /*!
2298  *\brief        Save matchers from folder items
2299  *
2300  *\param        fp File
2301  */
2302 static int prefs_matcher_save(FILE *fp)
2303 {
2304         GList *cur;
2305         NodeLoopData data;
2306         
2307         data.fp = fp;
2308         data.error = FALSE;
2309
2310         for (cur = folder_get_list() ; cur != NULL ; cur = g_list_next(cur)) {
2311                 Folder *folder;
2312
2313                 folder = (Folder *) cur->data;
2314                 g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
2315                                 prefs_matcher_write_func, &data);
2316         }
2317         
2318         if (data.error == TRUE)
2319                 return -1;
2320
2321         /* pre global rules */
2322         if (fprintf(fp, "[preglobal]\n") < 0 ||
2323             prefs_filtering_write(fp, pre_global_processing) < 0 ||
2324             fputc('\n', fp) == EOF)
2325                 return -1;
2326
2327         /* post global rules */
2328         if (fprintf(fp, "[postglobal]\n") < 0 ||
2329             prefs_filtering_write(fp, post_global_processing) < 0 ||
2330             fputc('\n', fp) == EOF)
2331                 return -1;
2332         
2333         /* filtering rules */
2334         if (fprintf(fp, "[filtering]\n") < 0 ||
2335             prefs_filtering_write(fp, filtering_rules) < 0 ||
2336             fputc('\n', fp) == EOF)
2337                 return -1;
2338
2339         return 0;
2340 }
2341
2342 /*!
2343  *\brief        Write filtering / matcher configuration file
2344  */
2345 void prefs_matcher_write_config(void)
2346 {
2347         gchar *rcpath;
2348         PrefFile *pfile;
2349
2350         debug_print("Writing matcher configuration...\n");
2351
2352         rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
2353                              MATCHER_RC, NULL);
2354
2355         if ((pfile = prefs_write_open(rcpath)) == NULL) {
2356                 g_warning("failed to write configuration to file\n");
2357                 g_free(rcpath);
2358                 return;
2359         }
2360
2361         g_free(rcpath);
2362
2363         if (prefs_matcher_save(pfile->fp) < 0) {
2364                 g_warning("failed to write configuration to file\n");
2365                 prefs_file_close_revert(pfile);
2366         } else if (prefs_file_close(pfile) < 0) {
2367                 g_warning("failed to save configuration to file\n");
2368         }
2369 }
2370
2371 /* ******************************************************************* */
2372
2373 static void matcher_add_rulenames(const gchar *rcpath)
2374 {
2375         gchar *newpath = g_strconcat(rcpath, ".new", NULL);
2376         FILE *src = g_fopen(rcpath, "rb");
2377         FILE *dst = g_fopen(newpath, "wb");
2378         gchar buf[BUFFSIZE];
2379         int r;
2380         if (src == NULL) {
2381                 perror("fopen");
2382                 if (dst)
2383                         fclose(dst);
2384                 g_free(newpath);
2385                 return;
2386         }
2387         if (dst == NULL) {
2388                 perror("fopen");
2389                 if (src)
2390                         fclose(src);
2391                 g_free(newpath);
2392                 return;
2393         }
2394
2395         while (fgets (buf, sizeof(buf), src) != NULL) {
2396                 if (strlen(buf) > 2 && buf[0] != '['
2397                 && strncmp(buf, "rulename \"", 10)
2398                 && strncmp(buf, "enabled rulename \"", 18)
2399                 && strncmp(buf, "disabled rulename \"", 18)) {
2400                         r = fwrite("enabled rulename \"\" ",
2401                                 strlen("enabled rulename \"\" "), 1, dst);
2402                 }
2403                 r = fwrite(buf, strlen(buf), 1, dst);
2404         }
2405         fclose(dst);
2406         fclose(src);
2407         move_file(newpath, rcpath, TRUE);
2408         g_free(newpath);
2409 }
2410
2411 /*!
2412  *\brief        Read matcher configuration
2413  */
2414 void prefs_matcher_read_config(void)
2415 {
2416         gchar *rcpath;
2417         gchar *rc_old_format;
2418         FILE *f;
2419
2420         create_matchparser_hashtab();
2421         prefs_filtering_clear();
2422
2423         rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MATCHER_RC, NULL);
2424         rc_old_format = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MATCHER_RC, 
2425                                 ".pre_names", NULL);
2426         
2427         if (!is_file_exist(rc_old_format) && is_file_exist(rcpath)) {
2428                 /* backup file with no rules names, in case 
2429                  * anything goes wrong */
2430                 copy_file(rcpath, rc_old_format, FALSE);
2431                 /* now hack the file in order to have it to the new format */
2432                 matcher_add_rulenames(rcpath);
2433         }
2434         
2435         g_free(rc_old_format);
2436
2437         f = g_fopen(rcpath, "rb");
2438         g_free(rcpath);
2439
2440         if (f != NULL) {
2441                 matcher_parser_start_parsing(f);
2442                 fclose(matcher_parserin);
2443         }
2444         else {
2445                 /* previous version compatibility */
2446
2447                 /* g_print("reading filtering\n"); */
2448                 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
2449                                      FILTERING_RC, NULL);
2450                 f = g_fopen(rcpath, "rb");
2451                 g_free(rcpath);
2452                 
2453                 if (f != NULL) {
2454                         matcher_parser_start_parsing(f);
2455                         fclose(matcher_parserin);
2456                 }
2457                 
2458                 /* g_print("reading scoring\n"); */
2459                 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
2460                                      SCORING_RC, NULL);
2461                 f = g_fopen(rcpath, "rb");
2462                 g_free(rcpath);
2463                 
2464                 if (f != NULL) {
2465                         matcher_parser_start_parsing(f);
2466                         fclose(matcher_parserin);
2467                 }
2468         }
2469 }