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