2007-11-28 [wwp] 3.1.0cvs43
[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 unsensitive"));
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 unsensitive"));
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 unsensitive"));
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 unsensitive"));
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         thread_data *td = g_new0(thread_data, 1);
678         void *res = NULL;
679         time_t start_time = time(NULL);
680 #endif
681
682         file = procmsg_get_message_file(info);
683         if (file == NULL)
684                 return FALSE;
685         g_free(file);           
686
687         cmd = matching_build_command(prop->expr, info);
688         if (cmd == NULL)
689                 return FALSE;
690
691 #if (defined USE_PTHREAD && ((defined __GLIBC__ && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 3))) || !defined __GLIBC__))
692         /* debug output */
693         if (debug_filtering_session
694                         && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
695                 log_print(LOG_DEBUG_FILTERING,
696                                 "starting threaded command [ %s ]\n",
697                                 cmd);
698         }
699
700         td->cmd = cmd;
701         td->done = FALSE;
702         if (pthread_create(&pt, PTHREAD_CREATE_JOINABLE, 
703                         matcher_test_thread, td) != 0)
704                 retval = system(cmd);
705         else {
706                 debug_print("waiting for test thread\n");
707                 while(!td->done) {
708                         /* don't let the interface freeze while waiting */
709                         claws_do_idle();
710                         if (time(NULL) - start_time > 30) {
711                                 pthread_cancel(pt);
712                                 td->done = TRUE;
713                                 retval = -1;
714                         }
715                 }
716                 pthread_join(pt, &res);
717                 retval = GPOINTER_TO_INT(res);
718                 debug_print(" test thread returned %d\n", retval);
719         }
720         g_free(td);
721 #else
722         /* debug output */
723         if (debug_filtering_session
724                         && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
725                 log_print(LOG_DEBUG_FILTERING,
726                                 "starting synchronous command [ %s ]\n",
727                                 cmd);
728         }
729
730         retval = system(cmd);
731 #endif
732         debug_print("Command exit code: %d\n", retval);
733
734         /* debug output */
735         if (debug_filtering_session
736                         && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
737                 log_print(LOG_DEBUG_FILTERING,
738                                 "command returned [ %d ]\n",
739                                 retval);
740         }
741
742         g_free(cmd);
743         return (retval == 0);
744 }
745
746 /*!
747  *\brief        Check if a message matches the condition in a matcher
748  *              structure.
749  *
750  *\param        prop Pointer to matcher structure
751  *\param        info Pointer to message info
752  *
753  *\return       gboolean TRUE if a match
754  */
755 gboolean matcherprop_match(MatcherProp *prop, 
756                            MsgInfo *info)
757 {
758         time_t t;
759
760         switch(prop->criteria) {
761         case MATCHCRITERIA_ALL:
762                 return TRUE;
763         case MATCHCRITERIA_UNREAD:
764                 return MSG_IS_UNREAD(info->flags);
765         case MATCHCRITERIA_NOT_UNREAD:
766                 return !MSG_IS_UNREAD(info->flags);
767         case MATCHCRITERIA_NEW:
768                 return MSG_IS_NEW(info->flags);
769         case MATCHCRITERIA_NOT_NEW:
770                 return !MSG_IS_NEW(info->flags);
771         case MATCHCRITERIA_MARKED:
772                 return MSG_IS_MARKED(info->flags);
773         case MATCHCRITERIA_NOT_MARKED:
774                 return !MSG_IS_MARKED(info->flags);
775         case MATCHCRITERIA_DELETED:
776                 return MSG_IS_DELETED(info->flags);
777         case MATCHCRITERIA_NOT_DELETED:
778                 return !MSG_IS_DELETED(info->flags);
779         case MATCHCRITERIA_REPLIED:
780                 return MSG_IS_REPLIED(info->flags);
781         case MATCHCRITERIA_NOT_REPLIED:
782                 return !MSG_IS_REPLIED(info->flags);
783         case MATCHCRITERIA_FORWARDED:
784                 return MSG_IS_FORWARDED(info->flags);
785         case MATCHCRITERIA_NOT_FORWARDED:
786                 return !MSG_IS_FORWARDED(info->flags);
787         case MATCHCRITERIA_LOCKED:
788                 return MSG_IS_LOCKED(info->flags);
789         case MATCHCRITERIA_NOT_LOCKED:
790                 return !MSG_IS_LOCKED(info->flags);
791         case MATCHCRITERIA_SPAM:
792                 return MSG_IS_SPAM(info->flags);
793         case MATCHCRITERIA_NOT_SPAM:
794                 return !MSG_IS_SPAM(info->flags);
795         case MATCHCRITERIA_COLORLABEL:
796         {
797                 gint color = MSG_GET_COLORLABEL_VALUE(info->flags);
798                 gboolean ret = (color == prop->value);
799
800                 /* debug output */
801                 if (debug_filtering_session
802                                 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
803                         if (ret) {
804                                 log_print(LOG_DEBUG_FILTERING,
805                                                 "message color value [ %d ] matches color value [ %d ]\n",
806                                                 color, prop->value);
807                         } else {
808                                 log_print(LOG_DEBUG_FILTERING,
809                                                 "message color value [ %d ] doesn't matches color value [ %d ]\n",
810                                                 color, prop->value);
811                         }
812                 }
813                 return ret;
814         }
815         case MATCHCRITERIA_NOT_COLORLABEL:
816         {
817                 gint color = MSG_GET_COLORLABEL_VALUE(info->flags);
818                 gboolean ret = (color != prop->value);
819
820                 /* debug output */
821                 if (debug_filtering_session
822                                 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
823                         if (ret) {
824                                 log_print(LOG_DEBUG_FILTERING,
825                                                 "message color value [ %d ] matches color value [ %d ]\n",
826                                                 color, prop->value);
827                         } else {
828                                 log_print(LOG_DEBUG_FILTERING,
829                                                 "message color value [ %d ] doesn't matches color value [ %d ]\n",
830                                                 color, prop->value);
831                         }
832                 }
833                 return ret;
834         }
835         case MATCHCRITERIA_IGNORE_THREAD:
836                 return MSG_IS_IGNORE_THREAD(info->flags);
837         case MATCHCRITERIA_NOT_IGNORE_THREAD:
838                 return !MSG_IS_IGNORE_THREAD(info->flags);
839         case MATCHCRITERIA_WATCH_THREAD:
840                 return MSG_IS_WATCH_THREAD(info->flags);
841         case MATCHCRITERIA_NOT_WATCH_THREAD:
842                 return !MSG_IS_WATCH_THREAD(info->flags);
843         case MATCHCRITERIA_SUBJECT:
844                 return matcherprop_string_match(prop, info->subject,
845                                                 prefs_common_translated_header_name("Subject:"));
846         case MATCHCRITERIA_NOT_SUBJECT:
847                 return !matcherprop_string_match(prop, info->subject,
848                                                 prefs_common_translated_header_name("Subject:"));
849         case MATCHCRITERIA_FROM:
850         case MATCHCRITERIA_NOT_FROM:
851         {
852                 gchar *context;
853                 gboolean ret;
854
855                 context = g_strdup_printf(_("%s header"), prefs_common_translated_header_name("From:"));
856                 ret = matcherprop_string_match(prop, info->from, context);
857                 g_free(context);
858                 return (prop->criteria == MATCHCRITERIA_FROM)? ret : !ret;
859         }
860         case MATCHCRITERIA_TO:
861         case MATCHCRITERIA_NOT_TO:
862         {
863                 gchar *context;
864                 gboolean ret;
865
866                 context = g_strdup_printf(_("%s header"), prefs_common_translated_header_name("To:"));
867                 ret = matcherprop_string_match(prop, info->to, context);
868                 g_free(context);
869                 return (prop->criteria == MATCHCRITERIA_TO)? ret : !ret;
870         }
871         case MATCHCRITERIA_CC:
872         case MATCHCRITERIA_NOT_CC:
873         {
874                 gchar *context;
875                 gboolean ret;
876
877                 context = g_strdup_printf(_("%s header"), prefs_common_translated_header_name("Cc:"));
878                 ret = matcherprop_string_match(prop, info->cc, context);
879                 g_free(context);
880                 return (prop->criteria == MATCHCRITERIA_CC)? ret : !ret;
881         }
882         case MATCHCRITERIA_TO_OR_CC:
883         {
884                 gchar *context1, *context2;
885                 gboolean ret;
886
887                 context1 = g_strdup_printf(_("%s header"), prefs_common_translated_header_name("To:"));
888                 context2 = g_strdup_printf(_("%s header"), prefs_common_translated_header_name("Cc:"));
889                 ret = matcherprop_string_match(prop, info->to, context1)
890                         || matcherprop_string_match(prop, info->cc, context2);
891                 g_free(context1);
892                 g_free(context2);
893                 return ret;
894         }
895         case MATCHCRITERIA_NOT_TO_AND_NOT_CC:
896         {
897                 gchar *context1, *context2;
898                 gboolean ret;
899
900                 context1 = g_strdup_printf(_("%s header"), prefs_common_translated_header_name("To:"));
901                 context2 = g_strdup_printf(_("%s header"), prefs_common_translated_header_name("Cc:"));
902                 ret = !(matcherprop_string_match(prop, info->to, context1)
903                         || matcherprop_string_match(prop, info->cc, context2));
904                 g_free(context1);
905                 g_free(context2);
906                 return ret;
907         }
908         case MATCHCRITERIA_TAG:
909         case MATCHCRITERIA_NOT_TAG:
910         {
911                 gboolean ret;
912
913                 ret = matcherprop_tag_match(prop, info, _("Tag"));
914                 return (prop->criteria == MATCHCRITERIA_TAG)? ret : !ret;
915         }
916         case MATCHCRITERIA_TAGGED:
917         case MATCHCRITERIA_NOT_TAGGED:
918         {
919                 gboolean ret;
920
921                 ret = (info->tags != NULL);
922                 return (prop->criteria == MATCHCRITERIA_TAGGED)? ret : !ret;
923         }
924         case MATCHCRITERIA_AGE_GREATER:
925         {
926                 gboolean ret;
927                 gint age;
928
929                 t = time(NULL);
930                 age = ((t - info->date_t) / (60 * 60 * 24));
931                 ret = (age > prop->value);
932
933                 /* debug output */
934                 if (debug_filtering_session
935                                 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
936                         if (ret) {
937                                 log_print(LOG_DEBUG_FILTERING,
938                                                 "message age [ %d ] is greater than [ %d ]\n",
939                                                 age, prop->value);
940                         } else {
941                                 log_print(LOG_DEBUG_FILTERING,
942                                                 "message age [ %d ] is not greater than [ %d ]\n",
943                                                 age, prop->value);
944                         }
945                 }
946                 return ret;
947         }
948         case MATCHCRITERIA_AGE_LOWER:
949         {
950                 gboolean ret;
951                 gint age;
952
953                 t = time(NULL);
954                 age = ((t - info->date_t) / (60 * 60 * 24));
955                 ret = (age < prop->value);
956
957                 /* debug output */
958                 if (debug_filtering_session
959                                 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
960                         if (ret) {
961                                 log_print(LOG_DEBUG_FILTERING,
962                                                 "message age [ %d ] is lower than [ %d ]\n",
963                                                 age, prop->value);
964                         } else {
965                                 log_print(LOG_DEBUG_FILTERING,
966                                                 "message age [ %d ] is not lower than [ %d ]\n",
967                                                 age, prop->value);
968                         }
969                 }
970                 return ret;
971         }
972         case MATCHCRITERIA_SCORE_GREATER:
973         {
974                 gboolean ret = (info->score > prop->value);
975
976                 /* debug output */
977                 if (debug_filtering_session
978                                 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
979                         if (ret) {
980                                 log_print(LOG_DEBUG_FILTERING,
981                                                 "message score [ %d ] is greater than [ %d ]\n",
982                                                 info->score, prop->value);
983                         } else {
984                                 log_print(LOG_DEBUG_FILTERING,
985                                                 "message score [ %d ] is not greater than [ %d ]\n",
986                                                 info->score, prop->value);
987                         }
988                 }
989                 return ret;
990         }
991         case MATCHCRITERIA_SCORE_LOWER:
992         {
993                 gboolean ret = (info->score < prop->value);
994
995                 /* debug output */
996                 if (debug_filtering_session
997                                 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
998                         if (ret) {
999                                 log_print(LOG_DEBUG_FILTERING,
1000                                                 "message score [ %d ] is lower than [ %d ]\n",
1001                                                 info->score, prop->value);
1002                         } else {
1003                                 log_print(LOG_DEBUG_FILTERING,
1004                                                 "message score [ %d ] is not lower than [ %d ]\n",
1005                                                 info->score, prop->value);
1006                         }
1007                 }
1008                 return ret;
1009         }
1010         case MATCHCRITERIA_SCORE_EQUAL:
1011         {
1012                 gboolean ret = (info->score == prop->value);
1013
1014                 /* debug output */
1015                 if (debug_filtering_session
1016                                 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
1017                         if (ret) {
1018                                 log_print(LOG_DEBUG_FILTERING,
1019                                                 "message score [ %d ] is equal to [ %d ]\n",
1020                                                 info->score, prop->value);
1021                         } else {
1022                                 log_print(LOG_DEBUG_FILTERING,
1023                                                 "message score [ %d ] is not equal to [ %d ]\n",
1024                                                 info->score, prop->value);
1025                         }
1026                 }
1027                 return ret;
1028         }
1029         case MATCHCRITERIA_SIZE_GREATER:
1030         {
1031                 /* FIXME: info->size is an off_t */
1032                 gboolean ret = (info->size > (off_t) prop->value);
1033
1034                 /* debug output */
1035                 if (debug_filtering_session
1036                                 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
1037                         if (ret) {
1038                                 log_print(LOG_DEBUG_FILTERING,
1039                                                 "message size [ %ld ] is greater than [ %d ]\n",
1040                                                 info->size, prop->value);
1041                         } else {
1042                                 log_print(LOG_DEBUG_FILTERING,
1043                                                 "message size [ %ld ] is not greater than [ %d ]\n",
1044                                                 info->size, prop->value);
1045                         }
1046                 }
1047                 return ret;
1048         }
1049         case MATCHCRITERIA_SIZE_SMALLER:
1050         {
1051                 /* FIXME: info->size is an off_t */
1052                 gboolean ret = (info->size < (off_t) prop->value);
1053
1054                 /* debug output */
1055                 if (debug_filtering_session
1056                                 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
1057                         if (ret) {
1058                                 log_print(LOG_DEBUG_FILTERING,
1059                                                 "message size [ %ld ] is smaller than [ %d ]\n",
1060                                                 info->size, prop->value);
1061                         } else {
1062                                 log_print(LOG_DEBUG_FILTERING,
1063                                                 "message size [ %ld ] is not smaller than [ %d ]\n",
1064                                                 info->size, prop->value);
1065                         }
1066                 }
1067                 return ret;
1068         }
1069         case MATCHCRITERIA_SIZE_EQUAL:
1070         {
1071                 /* FIXME: info->size is an off_t */
1072                 gboolean ret = (info->size == (off_t) prop->value);
1073
1074                 /* debug output */
1075                 if (debug_filtering_session
1076                                 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
1077                         if (ret) {
1078                                 log_print(LOG_DEBUG_FILTERING,
1079                                                 "message size [ %ld ] is equal to [ %d ]\n",
1080                                                 info->size, prop->value);
1081                         } else {
1082                                 log_print(LOG_DEBUG_FILTERING,
1083                                                 "message size [ %ld ] is not equal to [ %d ]\n",
1084                                                 info->size, prop->value);
1085                         }
1086                 }
1087                 return ret;
1088         }
1089         case MATCHCRITERIA_PARTIAL:
1090         {
1091                 /* FIXME: info->size is an off_t */
1092                 gboolean ret = (info->total_size != 0 && info->size != (off_t)info->total_size);
1093
1094                 /* debug output */
1095                 if (debug_filtering_session
1096                                 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
1097                         if (ret) {
1098                                 log_print(LOG_DEBUG_FILTERING,
1099                                                 "message is partially downloaded, size [ %ld ] is less than total size [ %d ])\n",
1100                                                 info->size, info->total_size);
1101                         } else {
1102                                 log_print(LOG_DEBUG_FILTERING,
1103                                                 "message is not partially downloaded\n");
1104                         }
1105                 }
1106                 return ret;
1107         }
1108         case MATCHCRITERIA_NOT_PARTIAL:
1109         {
1110                 /* FIXME: info->size is an off_t */
1111                 gboolean ret = (info->total_size == 0 || info->size == (off_t)info->total_size);
1112
1113                 /* debug output */
1114                 if (debug_filtering_session
1115                                 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
1116                         if (ret) {
1117                                 log_print(LOG_DEBUG_FILTERING,
1118                                                 "message is not partially downloaded\n");
1119                         } else {
1120                                 log_print(LOG_DEBUG_FILTERING,
1121                                                 "message is partially downloaded, size [ %ld ] is less than total size [ %d ])\n",
1122                                                 info->size, info->total_size);
1123                         }
1124                 }
1125                 return ret;
1126         }
1127         case MATCHCRITERIA_NEWSGROUPS:
1128         case MATCHCRITERIA_NOT_NEWSGROUPS:
1129         {
1130                 gchar *context;
1131                 gboolean ret;
1132
1133                 context = g_strdup_printf(_("%s header"),
1134                                                 prefs_common_translated_header_name("Newsgroups:"));
1135                 ret = matcherprop_string_match(prop, info->newsgroups, context);
1136                 g_free(context);
1137                 return (prop->criteria == MATCHCRITERIA_NEWSGROUPS)? ret : !ret;
1138         }
1139         case MATCHCRITERIA_INREPLYTO:
1140         case MATCHCRITERIA_NOT_INREPLYTO:
1141         {
1142                 gchar *context;
1143                 gboolean ret;
1144
1145                 context = g_strdup_printf(_("%s header"),
1146                                                 prefs_common_translated_header_name("In-Reply-To:"));
1147                 ret = matcherprop_string_match(prop, info->inreplyto, context);
1148                 g_free(context);
1149                 return (prop->criteria == MATCHCRITERIA_INREPLYTO)? ret : !ret;
1150         }
1151         case MATCHCRITERIA_REFERENCES:
1152         case MATCHCRITERIA_NOT_REFERENCES:
1153         {
1154                 gchar *context;
1155                 gboolean ret;
1156
1157                 context = g_strdup_printf(_("%s header"),
1158                                                 prefs_common_translated_header_name("References:"));
1159                 ret = matcherprop_list_match(prop, info->references, context);
1160                 g_free(context);
1161                 return (prop->criteria == MATCHCRITERIA_REFERENCES)? ret : !ret;
1162         }
1163         case MATCHCRITERIA_TEST:
1164                 return matcherprop_match_test(prop, info);
1165         case MATCHCRITERIA_NOT_TEST:
1166                 return !matcherprop_match_test(prop, info);
1167         default:
1168                 return FALSE;
1169         }
1170 }
1171
1172 /* ********************* MatcherList *************************** */
1173
1174 /*!
1175  *\brief        Create a new list of matchers 
1176  *
1177  *\param        matchers List of matcher structures
1178  *\param        bool_and Operator
1179  *
1180  *\return       MatcherList * New list
1181  */
1182 MatcherList *matcherlist_new(GSList *matchers, gboolean bool_and)
1183 {
1184         MatcherList *cond;
1185
1186         cond = g_new0(MatcherList, 1);
1187
1188         cond->matchers = matchers;
1189         cond->bool_and = bool_and;
1190
1191         return cond;
1192 }
1193
1194 /*!
1195  *\brief        Frees a list of matchers
1196  *
1197  *\param        cond List of matchers
1198  */
1199 void matcherlist_free(MatcherList *cond)
1200 {
1201         GSList *l;
1202
1203         g_return_if_fail(cond);
1204         for (l = cond->matchers ; l != NULL ; l = g_slist_next(l)) {
1205                 matcherprop_free((MatcherProp *) l->data);
1206         }
1207         g_free(cond);
1208 }
1209
1210 /*!
1211  *\brief        Skip all headers in a message file
1212  *
1213  *\param        fp Message file
1214  */
1215 static void matcherlist_skip_headers(FILE *fp)
1216 {
1217         gchar buf[BUFFSIZE];
1218
1219         while (procheader_get_one_field(buf, sizeof(buf), fp, NULL) != -1)
1220                 ;
1221 }
1222
1223 /*!
1224  *\brief        Check if a header matches a matcher condition
1225  *
1226  *\param        matcher Matcher structure to check header for
1227  *\param        buf Header name
1228  *
1229  *\return       boolean TRUE if matching header
1230  */
1231 static gboolean matcherprop_match_one_header(MatcherProp *matcher,
1232                                              gchar *buf)
1233 {
1234         gboolean result = FALSE;
1235         Header *header = NULL;
1236
1237         switch (matcher->criteria) {
1238         case MATCHCRITERIA_HEADER:
1239         case MATCHCRITERIA_NOT_HEADER:
1240                 header = procheader_parse_header(buf);
1241                 if (!header)
1242                         return FALSE;
1243                 if (procheader_headername_equal(header->name,
1244                                                 matcher->header)) {
1245                         if (matcher->criteria == MATCHCRITERIA_HEADER)
1246                                 result = matcherprop_string_match(matcher, header->body, _("header"));
1247                         else
1248                                 result = !matcherprop_string_match(matcher, header->body, _("header"));
1249                         procheader_header_free(header);
1250                         return result;
1251                 }
1252                 else {
1253                         procheader_header_free(header);
1254                 }
1255                 break;
1256         case MATCHCRITERIA_HEADERS_PART:
1257                 return matcherprop_string_match(matcher, buf, _("header line"));
1258         case MATCHCRITERIA_NOT_HEADERS_PART:
1259                 return !matcherprop_string_match(matcher, buf, _("headers line"));
1260         case MATCHCRITERIA_MESSAGE:
1261                 return matcherprop_string_decode_match(matcher, buf, _("message line"));
1262         case MATCHCRITERIA_NOT_MESSAGE:
1263                 return !matcherprop_string_decode_match(matcher, buf, _("message line"));
1264         case MATCHCRITERIA_FOUND_IN_ADDRESSBOOK:
1265         case MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK:
1266                 {
1267                         GSList *address_list = NULL;
1268                         gint match = MATCH_ONE;
1269                         gboolean found = FALSE;
1270
1271                         /* how many address headers are me trying to mach? */
1272                         if (strcasecmp(matcher->header, _("Any")) == 0)
1273                                 match = MATCH_ANY;
1274                         else if (strcasecmp(matcher->header, Q_("Filtering Matcher Menu|All")) == 0)
1275                                         match = MATCH_ALL;
1276
1277                         if (match == MATCH_ONE) {
1278                                 /* matching one address header exactly, is that the right one? */
1279                                 header = procheader_parse_header(buf);
1280                                 if (!header ||
1281                                                 !procheader_headername_equal(header->name, matcher->header))
1282                                         return FALSE;
1283                                 address_list = address_list_append(address_list, header->body);
1284                                 if (address_list == NULL)
1285                                         return FALSE;
1286
1287                         } else {
1288                                 header = procheader_parse_header(buf);
1289                                 if (!header)
1290                                         return FALSE;
1291                                 /* address header is one of the headers we have to match when checking
1292                                    for any address header or all address headers? */
1293                                 if (procheader_headername_equal(header->name, "From") ||
1294                                          procheader_headername_equal(header->name, "To") ||
1295                                          procheader_headername_equal(header->name, "Cc") ||
1296                                          procheader_headername_equal(header->name, "Reply-To") ||
1297                                          procheader_headername_equal(header->name, "Sender"))
1298                                         address_list = address_list_append(address_list, header->body);
1299                                 if (address_list == NULL)
1300                                         return FALSE;
1301                         }
1302
1303                         found = match_with_addresses_in_addressbook
1304                                                         (matcher, address_list, matcher->criteria,
1305                                                          matcher->expr, match);
1306                         g_slist_free(address_list);
1307
1308                         if (matcher->criteria == MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK)
1309                                 return !found;
1310                         else
1311                                 return found;
1312         }
1313         }
1314
1315         return FALSE;
1316 }
1317
1318 /*!
1319  *\brief        Check if the matcher structure wants headers to
1320  *              be matched
1321  *
1322  *\param        matcher Matcher structure
1323  *
1324  *\return       gboolean TRUE if the matcher structure describes
1325  *              a header match condition
1326  */
1327 static gboolean matcherprop_criteria_headers(const MatcherProp *matcher)
1328 {
1329         switch (matcher->criteria) {
1330         case MATCHCRITERIA_HEADER:
1331         case MATCHCRITERIA_NOT_HEADER:
1332         case MATCHCRITERIA_HEADERS_PART:
1333         case MATCHCRITERIA_NOT_HEADERS_PART:
1334         case MATCHCRITERIA_FOUND_IN_ADDRESSBOOK:
1335         case MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK:
1336                 return TRUE;
1337         default:
1338                 return FALSE;
1339         }
1340 }
1341
1342 /*!
1343  *\brief        Check if the matcher structure wants the message
1344  *              to be matched (just perform an action on any
1345  *              message)
1346  *
1347  *\param        matcher Matcher structure
1348  *
1349  *\return       gboolean TRUE if matcher condition should match
1350  *              a message
1351  */
1352 static gboolean matcherprop_criteria_message(MatcherProp *matcher)
1353 {
1354         switch (matcher->criteria) {
1355         case MATCHCRITERIA_MESSAGE:
1356         case MATCHCRITERIA_NOT_MESSAGE:
1357                 return TRUE;
1358         default:
1359                 return FALSE;
1360         }
1361 }
1362
1363 /*!
1364  *\brief        Check if a list of conditions matches one header in
1365  *              a message file.
1366  *
1367  *\param        matchers List of conditions
1368  *\param        fp Message file
1369  *
1370  *\return       gboolean TRUE if one of the headers is matched by
1371  *              the list of conditions. 
1372  */
1373 static gboolean matcherlist_match_headers(MatcherList *matchers, FILE *fp)
1374 {
1375         GSList *l;
1376         gchar buf[BUFFSIZE];
1377
1378         while (procheader_get_one_field(buf, sizeof(buf), fp, NULL) != -1) {
1379                 for (l = matchers->matchers ; l != NULL ; l = g_slist_next(l)) {
1380                         MatcherProp *matcher = (MatcherProp *) l->data;
1381                         gint match = MATCH_ANY;
1382
1383                         if (matcher->done)
1384                                 continue;
1385
1386                         /* determine the match range (all, any are our concern here) */
1387                         if (matcher->criteria == MATCHCRITERIA_NOT_HEADERS_PART ||
1388                             matcher->criteria == MATCHCRITERIA_NOT_MESSAGE) {
1389                                 match = MATCH_ALL;
1390
1391                         } else if (matcher->criteria == MATCHCRITERIA_FOUND_IN_ADDRESSBOOK ||
1392                                            matcher->criteria == MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK) {
1393                                 Header *header = NULL;
1394
1395                                 /* address header is one of the headers we have to match when checking
1396                                    for any address header or all address headers? */
1397                                 header = procheader_parse_header(buf);
1398                                 if (header &&
1399                                         (procheader_headername_equal(header->name, "From") ||
1400                                          procheader_headername_equal(header->name, "To") ||
1401                                          procheader_headername_equal(header->name, "Cc") ||
1402                                          procheader_headername_equal(header->name, "Reply-To") ||
1403                                          procheader_headername_equal(header->name, "Sender"))) {
1404
1405                                         if (strcasecmp(matcher->header, _("Any")) == 0)
1406                                                 match = MATCH_ANY;
1407                                         else if (strcasecmp(matcher->header, Q_("Filtering Matcher Menu|All")) == 0)
1408                                                 match = MATCH_ALL;
1409                                         else
1410                                                 match = MATCH_ONE;
1411                                 } else {
1412                                         /* further call to matcherprop_match_one_header() can't match
1413                                            and it irrelevant, so: don't alter the match result */
1414                                         continue;
1415                                 }
1416                         }
1417
1418                         /* ZERO line must NOT match for the rule to match.
1419                          */
1420                         if (match == MATCH_ALL) {
1421                                 if (matcherprop_match_one_header(matcher, buf)) {
1422                                         matcher->result = TRUE;
1423                                 } else {
1424                                         matcher->result = FALSE;
1425                                         matcher->done = TRUE;
1426                                 }
1427                         /* else, just one line matching is enough for the rule to match
1428                          */
1429                         } else if (matcherprop_criteria_headers(matcher) ||
1430                                    matcherprop_criteria_message(matcher)) {
1431                                 if (matcherprop_match_one_header(matcher, buf)) {
1432                                         matcher->result = TRUE;
1433                                         matcher->done = TRUE;
1434                                 }
1435                         }
1436                         
1437                         /* if the rule matched and the matchers are OR, no need to
1438                          * check the others */
1439                         if (matcher->result && matcher->done) {
1440                                 if (!matchers->bool_and)
1441                                         return TRUE;
1442                         }
1443                 }
1444         }
1445
1446         return FALSE;
1447 }
1448
1449 /*!
1450  *\brief        Check if a matcher wants to check the message body
1451  *
1452  *\param        matcher Matcher structure
1453  *
1454  *\return       gboolean TRUE if body must be matched.
1455  */
1456 static gboolean matcherprop_criteria_body(const MatcherProp *matcher)
1457 {
1458         switch (matcher->criteria) {
1459         case MATCHCRITERIA_BODY_PART:
1460         case MATCHCRITERIA_NOT_BODY_PART:
1461                 return TRUE;
1462         default:
1463                 return FALSE;
1464         }
1465 }
1466
1467 /*!
1468  *\brief        Check if a (line) string matches the criteria
1469  *              described by a matcher structure
1470  *
1471  *\param        matcher Matcher structure
1472  *\param        line String
1473  *
1474  *\return       gboolean TRUE if string matches criteria
1475  */
1476 static gboolean matcherprop_match_line(MatcherProp *matcher, const gchar *line)
1477 {
1478         switch (matcher->criteria) {
1479         case MATCHCRITERIA_BODY_PART:
1480         case MATCHCRITERIA_MESSAGE:
1481                 return matcherprop_string_decode_match(matcher, line, _("body line"));
1482         case MATCHCRITERIA_NOT_BODY_PART:
1483         case MATCHCRITERIA_NOT_MESSAGE:
1484                 return !matcherprop_string_decode_match(matcher, line, _("body line"));
1485         }
1486         return FALSE;
1487 }
1488
1489 /*!
1490  *\brief        Check if a line in a message file's body matches
1491  *              the criteria
1492  *
1493  *\param        matchers List of conditions
1494  *\param        fp Message file
1495  *
1496  *\return       gboolean TRUE if succesful match
1497  */
1498 static gboolean matcherlist_match_body(MatcherList *matchers, FILE *fp)
1499 {
1500         GSList *l;
1501         gchar buf[BUFFSIZE];
1502         
1503         while (fgets(buf, sizeof(buf), fp) != NULL) {
1504                 for (l = matchers->matchers ; l != NULL ; l = g_slist_next(l)) {
1505                         MatcherProp *matcher = (MatcherProp *) l->data;
1506                         
1507                         if (matcher->done) 
1508                                 continue;
1509
1510                         /* if the criteria is ~body_part or ~message, ZERO lines
1511                          * must NOT match for the rule to match. */
1512                         if (matcher->criteria == MATCHCRITERIA_NOT_BODY_PART ||
1513                             matcher->criteria == MATCHCRITERIA_NOT_MESSAGE) {
1514                                 if (matcherprop_match_line(matcher, buf)) {
1515                                         matcher->result = TRUE;
1516                                 } else {
1517                                         matcher->result = FALSE;
1518                                         matcher->done = TRUE;
1519                                 }
1520                         /* else, just one line has to match */
1521                         } else if (matcherprop_criteria_body(matcher) ||
1522                                    matcherprop_criteria_message(matcher)) {
1523                                 if (matcherprop_match_line(matcher, buf)) {
1524                                         matcher->result = TRUE;
1525                                         matcher->done = TRUE;
1526                                 }
1527                         }
1528
1529                         /* if the matchers are OR'ed and the rule matched,
1530                          * no need to check the others. */
1531                         if (matcher->result && matcher->done) {
1532                                 if (!matchers->bool_and)
1533                                         return TRUE;
1534                         }
1535                 }
1536         }
1537         return FALSE;
1538 }
1539
1540 /*!
1541  *\brief        Check if a message file matches criteria
1542  *
1543  *\param        matchers Criteria
1544  *\param        info Message info
1545  *\param        result Default result
1546  *
1547  *\return       gboolean TRUE if matched
1548  */
1549 static gboolean matcherlist_match_file(MatcherList *matchers, MsgInfo *info,
1550                                 gboolean result)
1551 {
1552         gboolean read_headers;
1553         gboolean read_body;
1554         GSList *l;
1555         FILE *fp;
1556         gchar *file;
1557
1558         /* file need to be read ? */
1559
1560         read_headers = FALSE;
1561         read_body = FALSE;
1562         for (l = matchers->matchers ; l != NULL ; l = g_slist_next(l)) {
1563                 MatcherProp *matcher = (MatcherProp *) l->data;
1564
1565                 if (matcherprop_criteria_headers(matcher))
1566                         read_headers = TRUE;
1567                 if (matcherprop_criteria_body(matcher))
1568                         read_body = TRUE;
1569                 if (matcherprop_criteria_message(matcher)) {
1570                         read_headers = TRUE;
1571                         read_body = TRUE;
1572                 }
1573                 matcher->result = FALSE;
1574                 matcher->done = FALSE;
1575         }
1576
1577         if (!read_headers && !read_body)
1578                 return result;
1579
1580         file = procmsg_get_message_file_full(info, read_headers, read_body);
1581         if (file == NULL)
1582                 return FALSE;
1583
1584         if ((fp = g_fopen(file, "rb")) == NULL) {
1585                 FILE_OP_ERROR(file, "fopen");
1586                 g_free(file);
1587                 return result;
1588         }
1589
1590         /* read the headers */
1591
1592         if (read_headers) {
1593                 if (matcherlist_match_headers(matchers, fp))
1594                         read_body = FALSE;
1595         } else {
1596                 matcherlist_skip_headers(fp);
1597         }
1598
1599         /* read the body */
1600         if (read_body) {
1601                 matcherlist_match_body(matchers, fp);
1602         }
1603         
1604         for (l = matchers->matchers; l != NULL; l = g_slist_next(l)) {
1605                 MatcherProp *matcher = (MatcherProp *) l->data;
1606
1607                 if (matcherprop_criteria_headers(matcher) ||
1608                     matcherprop_criteria_body(matcher)    ||
1609                     matcherprop_criteria_message(matcher)) {
1610                         if (matcher->result) {
1611                                 if (!matchers->bool_and) {
1612                                         result = TRUE;
1613                                         break;
1614                                 }
1615                         }
1616                         else {
1617                                 if (matchers->bool_and) {
1618                                         result = FALSE;
1619                                         break;
1620                                 }
1621                         }
1622                 }                       
1623         }
1624
1625         g_free(file);
1626
1627         fclose(fp);
1628         
1629         return result;
1630 }
1631
1632 /*!
1633  *\brief        Test list of conditions on a message.
1634  *
1635  *\param        matchers List of conditions
1636  *\param        info Message info
1637  *
1638  *\return       gboolean TRUE if matched
1639  */
1640 gboolean matcherlist_match(MatcherList *matchers, MsgInfo *info)
1641 {
1642         GSList *l;
1643         gboolean result;
1644
1645         if (!matchers)
1646                 return FALSE;
1647
1648         if (matchers->bool_and)
1649                 result = TRUE;
1650         else
1651                 result = FALSE;
1652
1653         /* test the cached elements */
1654
1655         for (l = matchers->matchers; l != NULL ;l = g_slist_next(l)) {
1656                 MatcherProp *matcher = (MatcherProp *) l->data;
1657
1658                 if (debug_filtering_session) {
1659                         gchar *buf = matcherprop_to_string(matcher);
1660                         log_print(LOG_DEBUG_FILTERING, _("checking if message matches [ %s ]\n"), buf);
1661                         g_free(buf);
1662                 }
1663
1664                 switch(matcher->criteria) {
1665                 case MATCHCRITERIA_ALL:
1666                 case MATCHCRITERIA_UNREAD:
1667                 case MATCHCRITERIA_NOT_UNREAD:
1668                 case MATCHCRITERIA_NEW:
1669                 case MATCHCRITERIA_NOT_NEW:
1670                 case MATCHCRITERIA_MARKED:
1671                 case MATCHCRITERIA_NOT_MARKED:
1672                 case MATCHCRITERIA_DELETED:
1673                 case MATCHCRITERIA_NOT_DELETED:
1674                 case MATCHCRITERIA_REPLIED:
1675                 case MATCHCRITERIA_NOT_REPLIED:
1676                 case MATCHCRITERIA_FORWARDED:
1677                 case MATCHCRITERIA_NOT_FORWARDED:
1678                 case MATCHCRITERIA_LOCKED:
1679                 case MATCHCRITERIA_NOT_LOCKED:
1680                 case MATCHCRITERIA_SPAM:
1681                 case MATCHCRITERIA_NOT_SPAM:
1682                 case MATCHCRITERIA_COLORLABEL:
1683                 case MATCHCRITERIA_NOT_COLORLABEL:
1684                 case MATCHCRITERIA_IGNORE_THREAD:
1685                 case MATCHCRITERIA_NOT_IGNORE_THREAD:
1686                 case MATCHCRITERIA_WATCH_THREAD:
1687                 case MATCHCRITERIA_NOT_WATCH_THREAD:
1688                 case MATCHCRITERIA_SUBJECT:
1689                 case MATCHCRITERIA_NOT_SUBJECT:
1690                 case MATCHCRITERIA_FROM:
1691                 case MATCHCRITERIA_NOT_FROM:
1692                 case MATCHCRITERIA_TO:
1693                 case MATCHCRITERIA_NOT_TO:
1694                 case MATCHCRITERIA_CC:
1695                 case MATCHCRITERIA_NOT_CC:
1696                 case MATCHCRITERIA_TO_OR_CC:
1697                 case MATCHCRITERIA_NOT_TO_AND_NOT_CC:
1698                 case MATCHCRITERIA_TAG:
1699                 case MATCHCRITERIA_NOT_TAG:
1700                 case MATCHCRITERIA_TAGGED:
1701                 case MATCHCRITERIA_NOT_TAGGED:
1702                 case MATCHCRITERIA_AGE_GREATER:
1703                 case MATCHCRITERIA_AGE_LOWER:
1704                 case MATCHCRITERIA_NEWSGROUPS:
1705                 case MATCHCRITERIA_NOT_NEWSGROUPS:
1706                 case MATCHCRITERIA_INREPLYTO:
1707                 case MATCHCRITERIA_NOT_INREPLYTO:
1708                 case MATCHCRITERIA_REFERENCES:
1709                 case MATCHCRITERIA_NOT_REFERENCES:
1710                 case MATCHCRITERIA_SCORE_GREATER:
1711                 case MATCHCRITERIA_SCORE_LOWER:
1712                 case MATCHCRITERIA_SCORE_EQUAL:
1713                 case MATCHCRITERIA_SIZE_GREATER:
1714                 case MATCHCRITERIA_SIZE_SMALLER:
1715                 case MATCHCRITERIA_SIZE_EQUAL:
1716                 case MATCHCRITERIA_TEST:
1717                 case MATCHCRITERIA_NOT_TEST:
1718                 case MATCHCRITERIA_PARTIAL:
1719                 case MATCHCRITERIA_NOT_PARTIAL:
1720                         if (matcherprop_match(matcher, info)) {
1721                                 if (!matchers->bool_and) {
1722                                         if (debug_filtering_session)
1723                                                 log_status_ok(LOG_DEBUG_FILTERING, _("message matches\n"));
1724                                         return TRUE;
1725                                 }
1726                         }
1727                         else {
1728                                 if (matchers->bool_and) {
1729                                         if (debug_filtering_session)
1730                                                 log_status_nok(LOG_DEBUG_FILTERING, _("message does not match\n"));
1731                                         return FALSE;
1732                                 }
1733                         }
1734                 }
1735         }
1736
1737         /* test the condition on the file */
1738
1739         if (matcherlist_match_file(matchers, info, result)) {
1740                 if (!matchers->bool_and) {
1741                         if (debug_filtering_session)
1742                                 log_status_ok(LOG_DEBUG_FILTERING, _("message matches\n"));
1743                         return TRUE;
1744                 }
1745         } else {
1746                 if (matchers->bool_and) {
1747                         if (debug_filtering_session)
1748                                 log_status_nok(LOG_DEBUG_FILTERING, _("message does not match\n"));
1749                         return FALSE;
1750                 }
1751         }
1752
1753         if (debug_filtering_session) {
1754                 if (result)
1755                         log_status_ok(LOG_DEBUG_FILTERING, _("message matches\n"));
1756                 else
1757                         log_status_nok(LOG_DEBUG_FILTERING, _("message does not match\n"));
1758         }
1759         return result;
1760 }
1761
1762
1763 static gint quote_filter_str(gchar * result, guint size,
1764                              const gchar * path)
1765 {
1766         const gchar * p;
1767         gchar * result_p;
1768         guint remaining;
1769
1770         result_p = result;
1771         remaining = size;
1772
1773         for(p = path ; * p != '\0' ; p ++) {
1774
1775                 if ((* p != '\"') && (* p != '\\')) {
1776                         if (remaining > 0) {
1777                                 * result_p = * p;
1778                                 result_p ++; 
1779                                 remaining --;
1780                         }
1781                         else {
1782                                 result[size - 1] = '\0';
1783                                 return -1;
1784                         }
1785                 }
1786                 else { 
1787                         if (remaining >= 2) {
1788                                 * result_p = '\\';
1789                                 result_p ++; 
1790                                 * result_p = * p;
1791                                 result_p ++; 
1792                                 remaining -= 2;
1793                         }
1794                         else {
1795                                 result[size - 1] = '\0';
1796                                 return -1;
1797                         }
1798                 }
1799         }
1800         if (remaining > 0) {
1801                 * result_p = '\0';
1802         }
1803         else {
1804                 result[size - 1] = '\0';
1805                 return -1;
1806         }
1807   
1808         return 0;
1809 }
1810
1811
1812 gchar * matcher_quote_str(const gchar * src)
1813 {
1814         gchar * res;
1815         gint len;
1816         
1817         len = strlen(src) * 2 + 1;
1818         res = g_malloc(len);
1819         quote_filter_str(res, len, src);
1820         
1821         return res;
1822 }
1823
1824 /*!
1825  *\brief        Convert a matcher structure to a string
1826  *
1827  *\param        matcher Matcher structure
1828  *
1829  *\return       gchar * Newly allocated string
1830  */
1831 gchar *matcherprop_to_string(MatcherProp *matcher)
1832 {
1833         gchar *matcher_str = NULL;
1834         const gchar *criteria_str;
1835         const gchar *matchtype_str;
1836         int i;
1837         gchar * quoted_expr;
1838         gchar * quoted_header;
1839         
1840         criteria_str = NULL;
1841         for (i = 0; i < (int) (sizeof(matchparser_tab) / sizeof(MatchParser)); i++) {
1842                 if (matchparser_tab[i].id == matcher->criteria)
1843                         criteria_str = matchparser_tab[i].str;
1844         }
1845         if (criteria_str == NULL)
1846                 return NULL;
1847
1848         switch (matcher->criteria) {
1849         case MATCHCRITERIA_AGE_GREATER:
1850         case MATCHCRITERIA_AGE_LOWER:
1851         case MATCHCRITERIA_SCORE_GREATER:
1852         case MATCHCRITERIA_SCORE_LOWER:
1853         case MATCHCRITERIA_SCORE_EQUAL:
1854         case MATCHCRITERIA_SIZE_GREATER:
1855         case MATCHCRITERIA_SIZE_SMALLER:
1856         case MATCHCRITERIA_SIZE_EQUAL:
1857         case MATCHCRITERIA_COLORLABEL:
1858         case MATCHCRITERIA_NOT_COLORLABEL:
1859                 return g_strdup_printf("%s %i", criteria_str, matcher->value);
1860         case MATCHCRITERIA_ALL:
1861         case MATCHCRITERIA_UNREAD:
1862         case MATCHCRITERIA_NOT_UNREAD:
1863         case MATCHCRITERIA_NEW:
1864         case MATCHCRITERIA_NOT_NEW:
1865         case MATCHCRITERIA_MARKED:
1866         case MATCHCRITERIA_NOT_MARKED:
1867         case MATCHCRITERIA_DELETED:
1868         case MATCHCRITERIA_NOT_DELETED:
1869         case MATCHCRITERIA_REPLIED:
1870         case MATCHCRITERIA_NOT_REPLIED:
1871         case MATCHCRITERIA_FORWARDED:
1872         case MATCHCRITERIA_NOT_FORWARDED:
1873         case MATCHCRITERIA_LOCKED:
1874         case MATCHCRITERIA_NOT_LOCKED:
1875         case MATCHCRITERIA_SPAM:
1876         case MATCHCRITERIA_NOT_SPAM:
1877         case MATCHCRITERIA_PARTIAL:
1878         case MATCHCRITERIA_NOT_PARTIAL:
1879         case MATCHCRITERIA_IGNORE_THREAD:
1880         case MATCHCRITERIA_NOT_IGNORE_THREAD:
1881         case MATCHCRITERIA_WATCH_THREAD:
1882         case MATCHCRITERIA_NOT_WATCH_THREAD:
1883         case MATCHCRITERIA_TAGGED:
1884         case MATCHCRITERIA_NOT_TAGGED:
1885                 return g_strdup(criteria_str);
1886         case MATCHCRITERIA_TEST:
1887         case MATCHCRITERIA_NOT_TEST:
1888                 quoted_expr = matcher_quote_str(matcher->expr);
1889                 matcher_str = g_strdup_printf("%s \"%s\"",
1890                                               criteria_str, quoted_expr);
1891                 g_free(quoted_expr);
1892                 return matcher_str;
1893         case MATCHCRITERIA_FOUND_IN_ADDRESSBOOK:
1894         case MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK:
1895                 quoted_header = matcher_quote_str(matcher->header);
1896                 quoted_expr = matcher_quote_str(matcher->expr);
1897                 matcher_str = g_strdup_printf("%s \"%s\" in \"%s\"",
1898                                               criteria_str, quoted_header, quoted_expr);
1899                 g_free(quoted_header);
1900                 g_free(quoted_expr);
1901                 return matcher_str;
1902         }
1903
1904         matchtype_str = NULL;
1905         for (i = 0; i < sizeof matchparser_tab / sizeof matchparser_tab[0]; i++) {
1906                 if (matchparser_tab[i].id == matcher->matchtype)
1907                         matchtype_str = matchparser_tab[i].str;
1908         }
1909
1910         if (matchtype_str == NULL)
1911                 return NULL;
1912
1913         switch (matcher->matchtype) {
1914         case MATCHTYPE_MATCH:
1915         case MATCHTYPE_MATCHCASE:
1916         case MATCHTYPE_REGEXP:
1917         case MATCHTYPE_REGEXPCASE:
1918                 quoted_expr = matcher_quote_str(matcher->expr);
1919                 if (matcher->header) {
1920                         quoted_header = matcher_quote_str(matcher->header);
1921                         matcher_str = g_strdup_printf
1922                                         ("%s \"%s\" %s \"%s\"",
1923                                          criteria_str, quoted_header,
1924                                          matchtype_str, quoted_expr);
1925                         g_free(quoted_header);
1926                 }
1927                 else
1928                         matcher_str = g_strdup_printf
1929                                         ("%s %s \"%s\"", criteria_str,
1930                                          matchtype_str, quoted_expr);
1931                 g_free(quoted_expr);
1932                 break;
1933         }
1934
1935         return matcher_str;
1936 }
1937
1938 /*!
1939  *\brief        Convert a list of conditions to a string
1940  *
1941  *\param        matchers List of conditions
1942  *
1943  *\return       gchar * Newly allocated string
1944  */
1945 gchar *matcherlist_to_string(const MatcherList *matchers)
1946 {
1947         gint count;
1948         gchar **vstr;
1949         GSList *l;
1950         gchar **cur_str;
1951         gchar *result = NULL;
1952
1953         count = g_slist_length(matchers->matchers);
1954         vstr = g_new(gchar *, count + 1);
1955
1956         for (l = matchers->matchers, cur_str = vstr; l != NULL;
1957              l = g_slist_next(l), cur_str ++) {
1958                 *cur_str = matcherprop_to_string((MatcherProp *) l->data);
1959                 if (*cur_str == NULL)
1960                         break;
1961         }
1962         *cur_str = NULL;
1963         
1964         if (matchers->bool_and)
1965                 result = g_strjoinv(" & ", vstr);
1966         else
1967                 result = g_strjoinv(" | ", vstr);
1968
1969         for (cur_str = vstr ; *cur_str != NULL ; cur_str ++)
1970                 g_free(*cur_str);
1971         g_free(vstr);
1972
1973         return result;
1974 }
1975
1976
1977 #define STRLEN_ZERO(s) ((s) ? strlen(s) : 0)
1978 #define STRLEN_DEFAULT(s,d) ((s) ? strlen(s) : STRLEN_ZERO(d))
1979
1980 static void add_str_default(gchar ** dest,
1981                             const gchar * s, const gchar * d)
1982 {
1983         gchar quoted_str[4096];
1984         const gchar * str;
1985         
1986         if (s != NULL)
1987                 str = s;
1988         else
1989                 str = d;
1990         
1991         quote_cmd_argument(quoted_str, sizeof(quoted_str), str);
1992         strcpy(* dest, quoted_str);
1993         
1994         (* dest) += strlen(* dest);
1995 }
1996
1997 /* matching_build_command() - preferably cmd should be unescaped */
1998 /*!
1999  *\brief        Build the command line to execute
2000  *
2001  *\param        cmd String with command line specifiers
2002  *\param        info Message info to use for command
2003  *
2004  *\return       gchar * Newly allocated string
2005  */
2006 gchar *matching_build_command(const gchar *cmd, MsgInfo *info)
2007 {
2008         const gchar *s = cmd;
2009         gchar *filename = NULL;
2010         gchar *processed_cmd;
2011         gchar *p;
2012         gint size;
2013
2014         const gchar *const no_subject    = _("(none)") ;
2015         const gchar *const no_from       = _("(none)") ;
2016         const gchar *const no_to         = _("(none)") ;
2017         const gchar *const no_cc         = _("(none)") ;
2018         const gchar *const no_date       = _("(none)") ;
2019         const gchar *const no_msgid      = _("(none)") ;
2020         const gchar *const no_newsgroups = _("(none)") ;
2021         const gchar *const no_references = _("(none)") ;
2022
2023         size = STRLEN_ZERO(cmd) + 1;
2024         while (*s != '\0') {
2025                 if (*s == '%') {
2026                         s++;
2027                         switch (*s) {
2028                         case '%':
2029                                 size -= 1;
2030                                 break;
2031                         case 's': /* subject */
2032                                 size += STRLEN_DEFAULT(info->subject, no_subject) - 2;
2033                                 break;
2034                         case 'f': /* from */
2035                                 size += STRLEN_DEFAULT(info->from, no_from) - 2;
2036                                 break;
2037                         case 't': /* to */
2038                                 size += STRLEN_DEFAULT(info->to, no_to) - 2;
2039                                 break;
2040                         case 'c': /* cc */
2041                                 size += STRLEN_DEFAULT(info->cc, no_cc) - 2;
2042                                 break;
2043                         case 'd': /* date */
2044                                 size += STRLEN_DEFAULT(info->date, no_date) - 2;
2045                                 break;
2046                         case 'i': /* message-id */
2047                                 size += STRLEN_DEFAULT(info->msgid, no_msgid) - 2;
2048                                 break;
2049                         case 'n': /* newsgroups */
2050                                 size += STRLEN_DEFAULT(info->newsgroups, no_newsgroups) - 2;
2051                                 break;
2052                         case 'r': /* references */
2053                                 /* FIXME: using the inreplyto header for reference */
2054                                 size += STRLEN_DEFAULT(info->inreplyto, no_references) - 2;
2055                                 break;
2056                         case 'F': /* file */
2057                                 if (filename == NULL)
2058                                         filename = folder_item_fetch_msg(info->folder, info->msgnum);
2059                                 
2060                                 if (filename == NULL) {
2061                                         g_warning("filename is not set");
2062                                         return NULL;
2063                                 }
2064                                 else {
2065                                         size += strlen(filename) - 2;
2066                                 }
2067                                 break;
2068                         }
2069                         s++;
2070                 }
2071                 else s++;
2072         }
2073         
2074         /* as the string can be quoted, we double the result */
2075         size *= 2;
2076
2077         processed_cmd = g_new0(gchar, size);
2078         s = cmd;
2079         p = processed_cmd;
2080
2081         while (*s != '\0') {
2082                 if (*s == '%') {
2083                         s++;
2084                         switch (*s) {
2085                         case '%':
2086                                 *p = '%';
2087                                 p++;
2088                                 break;
2089                         case 's': /* subject */
2090                                 add_str_default(&p, info->subject,
2091                                                 no_subject);
2092                                 break;
2093                         case 'f': /* from */
2094                                 add_str_default(&p, info->from,
2095                                                 no_from);
2096                                 break;
2097                         case 't': /* to */
2098                                 add_str_default(&p, info->to,
2099                                                 no_to);
2100                                 break;
2101                         case 'c': /* cc */
2102                                 add_str_default(&p, info->cc,
2103                                                 no_cc);
2104                                 break;
2105                         case 'd': /* date */
2106                                 add_str_default(&p, info->date,
2107                                                 no_date);
2108                                 break;
2109                         case 'i': /* message-id */
2110                                 add_str_default(&p, info->msgid,
2111                                                 no_msgid);
2112                                 break;
2113                         case 'n': /* newsgroups */
2114                                 add_str_default(&p, info->newsgroups,
2115                                                 no_newsgroups);
2116                                 break;
2117                         case 'r': /* references */
2118                                 /* FIXME: using the inreplyto header for references */
2119                                 add_str_default(&p, info->inreplyto, no_references);
2120                                 break;
2121                         case 'F': /* file */
2122                                 if (filename != NULL)
2123                                         add_str_default(&p, filename, NULL);
2124                                 break;
2125                         default:
2126                                 *p = '%';
2127                                 p++;
2128                                 *p = *s;
2129                                 p++;
2130                                 break;
2131                         }
2132                         s++;
2133                 }
2134                 else {
2135                         *p = *s;
2136                         p++;
2137                         s++;
2138                 }
2139         }
2140         g_free(filename);
2141         
2142         return processed_cmd;
2143 }
2144 #undef STRLEN_DEFAULT
2145 #undef STRLEN_ZERO
2146
2147 /* ************************************************************ */
2148
2149
2150 /*!
2151  *\brief        Write filtering list to file
2152  *
2153  *\param        fp File
2154  *\param        prefs_filtering List of filtering conditions
2155  */
2156 static int prefs_filtering_write(FILE *fp, GSList *prefs_filtering)
2157 {
2158         GSList *cur = NULL;
2159
2160         for (cur = prefs_filtering; cur != NULL; cur = cur->next) {
2161                 gchar *filtering_str = NULL;
2162                 gchar *tmp_name = NULL;
2163                 FilteringProp *prop = NULL;
2164
2165                 if (NULL == (prop = (FilteringProp *) cur->data))
2166                         continue;
2167                 
2168                 if (NULL == (filtering_str = filteringprop_to_string(prop)))
2169                         continue;
2170
2171                 if (prop->enabled) {
2172                         if (fputs("enabled ", fp) == EOF) {
2173                                 FILE_OP_ERROR("filtering config", "fputs");
2174                                 return -1;
2175                         }
2176                 } else {
2177                         if (fputs("disabled ", fp) == EOF) {
2178                                 FILE_OP_ERROR("filtering config", "fputs");
2179                                 return -1;
2180                         }
2181                 }
2182
2183                 if (fputs("rulename \"", fp) == EOF) {
2184                         FILE_OP_ERROR("filtering config", "fputs");
2185                         g_free(filtering_str);
2186                         return -1;
2187                 }
2188                 tmp_name = prop->name;
2189                 while (tmp_name && *tmp_name != '\0') {
2190                         if (*tmp_name != '"') {
2191                                 if (fputc(*tmp_name, fp) == EOF) {
2192                                         FILE_OP_ERROR("filtering config", "fputs || fputc");
2193                                         g_free(filtering_str);
2194                                         return -1;
2195                                 }
2196                         } else if (*tmp_name == '"') {
2197                                 if (fputc('\\', fp) == EOF ||
2198                                     fputc('"', fp) == EOF) {
2199                                         FILE_OP_ERROR("filtering config", "fputs || fputc");
2200                                         g_free(filtering_str);
2201                                         return -1;
2202                                 }
2203                         }
2204                         tmp_name ++;
2205                 }
2206                 if (fputs("\" ", fp) == EOF) {
2207                         FILE_OP_ERROR("filtering config", "fputs");
2208                         g_free(filtering_str);
2209                         return -1;
2210                 }
2211
2212                 if (prop->account_id != 0) {
2213                         gchar *tmp = NULL;
2214
2215                         tmp = g_strdup_printf("account %d ", prop->account_id);
2216                         if (fputs(tmp, fp) == EOF) {
2217                                 FILE_OP_ERROR("filtering config", "fputs");
2218                                 g_free(tmp);
2219                                 return -1;
2220                         }
2221                         g_free(tmp);
2222                 }
2223
2224                 if(fputs(filtering_str, fp) == EOF ||
2225                     fputc('\n', fp) == EOF) {
2226                         FILE_OP_ERROR("filtering config", "fputs || fputc");
2227                         g_free(filtering_str);
2228                         return -1;
2229                 }
2230                 g_free(filtering_str);
2231         }
2232         
2233         return 0;
2234 }
2235
2236 typedef struct _NodeLoopData {
2237         FILE *fp;
2238         gboolean error;
2239 } NodeLoopData;
2240
2241 /*!
2242  *\brief        Write matchers from a folder item
2243  *
2244  *\param        node Node with folder info
2245  *\param        data File pointer
2246  *
2247  *\return       gboolean FALSE
2248  */
2249 static gboolean prefs_matcher_write_func(GNode *node, gpointer d)
2250 {
2251         FolderItem *item;
2252         NodeLoopData *data = (NodeLoopData *)d;
2253         gchar *id;
2254         GSList *prefs_filtering;
2255
2256         item = node->data;
2257         /* prevent warning */
2258         if (item->path == NULL)
2259                 return FALSE;
2260         id = folder_item_get_identifier(item);
2261         if (id == NULL)
2262                 return FALSE;
2263         prefs_filtering = item->prefs->processing;
2264
2265         if (prefs_filtering != NULL) {
2266                 if (fprintf(data->fp, "[%s]\n", id) < 0) {
2267                         data->error = TRUE;
2268                         goto fail;
2269                 }
2270                 if (prefs_filtering_write(data->fp, prefs_filtering) < 0) {
2271                         data->error = TRUE;
2272                         goto fail;
2273                 }
2274                 if (fputc('\n', data->fp) == EOF) {
2275                         data->error = TRUE;
2276                         goto fail;
2277                 }
2278         }
2279 fail:
2280         g_free(id);
2281
2282         return FALSE;
2283 }
2284
2285 /*!
2286  *\brief        Save matchers from folder items
2287  *
2288  *\param        fp File
2289  */
2290 static int prefs_matcher_save(FILE *fp)
2291 {
2292         GList *cur;
2293         NodeLoopData data;
2294         
2295         data.fp = fp;
2296         data.error = FALSE;
2297
2298         for (cur = folder_get_list() ; cur != NULL ; cur = g_list_next(cur)) {
2299                 Folder *folder;
2300
2301                 folder = (Folder *) cur->data;
2302                 g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
2303                                 prefs_matcher_write_func, &data);
2304         }
2305         
2306         if (data.error == TRUE)
2307                 return -1;
2308
2309         /* pre global rules */
2310         if (fprintf(fp, "[preglobal]\n") < 0 ||
2311             prefs_filtering_write(fp, pre_global_processing) < 0 ||
2312             fputc('\n', fp) == EOF)
2313                 return -1;
2314
2315         /* post global rules */
2316         if (fprintf(fp, "[postglobal]\n") < 0 ||
2317             prefs_filtering_write(fp, post_global_processing) < 0 ||
2318             fputc('\n', fp) == EOF)
2319                 return -1;
2320         
2321         /* filtering rules */
2322         if (fprintf(fp, "[filtering]\n") < 0 ||
2323             prefs_filtering_write(fp, filtering_rules) < 0 ||
2324             fputc('\n', fp) == EOF)
2325                 return -1;
2326
2327         return 0;
2328 }
2329
2330 /*!
2331  *\brief        Write filtering / matcher configuration file
2332  */
2333 void prefs_matcher_write_config(void)
2334 {
2335         gchar *rcpath;
2336         PrefFile *pfile;
2337
2338         debug_print("Writing matcher configuration...\n");
2339
2340         rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
2341                              MATCHER_RC, NULL);
2342
2343         if ((pfile = prefs_write_open(rcpath)) == NULL) {
2344                 g_warning("failed to write configuration to file\n");
2345                 g_free(rcpath);
2346                 return;
2347         }
2348
2349         g_free(rcpath);
2350
2351         if (prefs_matcher_save(pfile->fp) < 0) {
2352                 g_warning("failed to write configuration to file\n");
2353                 prefs_file_close_revert(pfile);
2354         } else if (prefs_file_close(pfile) < 0) {
2355                 g_warning("failed to save configuration to file\n");
2356         }
2357 }
2358
2359 /* ******************************************************************* */
2360
2361 static void matcher_add_rulenames(const gchar *rcpath)
2362 {
2363         gchar *newpath = g_strconcat(rcpath, ".new", NULL);
2364         FILE *src = g_fopen(rcpath, "rb");
2365         FILE *dst = g_fopen(newpath, "wb");
2366         gchar buf[BUFFSIZE];
2367         int r;
2368         if (dst == NULL) {
2369                 perror("fopen");
2370                 g_free(newpath);
2371                 return;
2372         }
2373
2374         while (fgets (buf, sizeof(buf), src) != NULL) {
2375                 if (strlen(buf) > 2 && buf[0] != '['
2376                 && strncmp(buf, "rulename \"", 10)
2377                 && strncmp(buf, "enabled rulename \"", 18)
2378                 && strncmp(buf, "disabled rulename \"", 18)) {
2379                         r = fwrite("enabled rulename \"\" ",
2380                                 strlen("enabled rulename \"\" "), 1, dst);
2381                 }
2382                 r = fwrite(buf, strlen(buf), 1, dst);
2383         }
2384         fclose(dst);
2385         fclose(src);
2386         move_file(newpath, rcpath, TRUE);
2387         g_free(newpath);
2388 }
2389
2390 /*!
2391  *\brief        Read matcher configuration
2392  */
2393 void prefs_matcher_read_config(void)
2394 {
2395         gchar *rcpath;
2396         gchar *rc_old_format;
2397         FILE *f;
2398
2399         create_matchparser_hashtab();
2400         prefs_filtering_clear();
2401
2402         rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MATCHER_RC, NULL);
2403         rc_old_format = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MATCHER_RC, 
2404                                 ".pre_names", NULL);
2405         
2406         if (!is_file_exist(rc_old_format) && is_file_exist(rcpath)) {
2407                 /* backup file with no rules names, in case 
2408                  * anything goes wrong */
2409                 copy_file(rcpath, rc_old_format, FALSE);
2410                 /* now hack the file in order to have it to the new format */
2411                 matcher_add_rulenames(rcpath);
2412         }
2413         
2414         g_free(rc_old_format);
2415
2416         f = g_fopen(rcpath, "rb");
2417         g_free(rcpath);
2418
2419         if (f != NULL) {
2420                 matcher_parser_start_parsing(f);
2421                 fclose(matcher_parserin);
2422         }
2423         else {
2424                 /* previous version compatibility */
2425
2426                 /* g_print("reading filtering\n"); */
2427                 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
2428                                      FILTERING_RC, NULL);
2429                 f = g_fopen(rcpath, "rb");
2430                 g_free(rcpath);
2431                 
2432                 if (f != NULL) {
2433                         matcher_parser_start_parsing(f);
2434                         fclose(matcher_parserin);
2435                 }
2436                 
2437                 /* g_print("reading scoring\n"); */
2438                 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
2439                                      SCORING_RC, NULL);
2440                 f = g_fopen(rcpath, "rb");
2441                 g_free(rcpath);
2442                 
2443                 if (f != NULL) {
2444                         matcher_parser_start_parsing(f);
2445                         fclose(matcher_parserin);
2446                 }
2447         }
2448 }