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