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