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