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