2007-10-03 [colin] 3.0.2cvs4
[claws.git] / src / matcher.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 2002-2004 by the Claws Mail Team and Hiroyuki Yamamoto
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  * 
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include <glib.h>
25 #include <glib/gi18n.h>
26 #include <ctype.h>
27 #include <string.h>
28 #include <stdlib.h>
29 #include <errno.h>
30
31 #ifdef USE_PTHREAD
32 #include <pthread.h>
33 #endif
34
35 #include "defs.h"
36 #include "utils.h"
37 #include "procheader.h"
38 #include "matcher.h"
39 #include "matcher_parser.h"
40 #include "prefs_gtk.h"
41 #include "addr_compl.h"
42 #include "codeconv.h"
43 #include "quoted-printable.h"
44 #include "claws.h"
45 #include <ctype.h>
46 #include "prefs_common.h"
47 #include "log.h"
48 #include "tags.h"
49
50 /*!
51  *\brief        Keyword lookup element
52  */
53 struct _MatchParser {
54         gint id;                /*!< keyword id */ 
55         gchar *str;             /*!< keyword */
56 };
57 typedef struct _MatchParser MatchParser;
58
59 /*!
60  *\brief        Table with strings and ids used by the lexer and
61  *              the parser. New keywords can be added here.
62  */
63 static const MatchParser matchparser_tab[] = {
64         /* msginfo flags */
65         {MATCHCRITERIA_ALL, "all"},
66         {MATCHCRITERIA_UNREAD, "unread"},
67         {MATCHCRITERIA_NOT_UNREAD, "~unread"},
68         {MATCHCRITERIA_NEW, "new"},
69         {MATCHCRITERIA_NOT_NEW, "~new"},
70         {MATCHCRITERIA_MARKED, "marked"},
71         {MATCHCRITERIA_NOT_MARKED, "~marked"},
72         {MATCHCRITERIA_DELETED, "deleted"},
73         {MATCHCRITERIA_NOT_DELETED, "~deleted"},
74         {MATCHCRITERIA_REPLIED, "replied"},
75         {MATCHCRITERIA_NOT_REPLIED, "~replied"},
76         {MATCHCRITERIA_FORWARDED, "forwarded"},
77         {MATCHCRITERIA_NOT_FORWARDED, "~forwarded"},
78         {MATCHCRITERIA_LOCKED, "locked"},
79         {MATCHCRITERIA_NOT_LOCKED, "~locked"},
80         {MATCHCRITERIA_COLORLABEL, "colorlabel"},
81         {MATCHCRITERIA_NOT_COLORLABEL, "~colorlabel"},
82         {MATCHCRITERIA_IGNORE_THREAD, "ignore_thread"},
83         {MATCHCRITERIA_NOT_IGNORE_THREAD, "~ignore_thread"},
84         {MATCHCRITERIA_WATCH_THREAD, "watch_thread"},
85         {MATCHCRITERIA_NOT_WATCH_THREAD, "~watch_thread"},
86         {MATCHCRITERIA_SPAM, "spam"},
87         {MATCHCRITERIA_NOT_SPAM, "~spam"},
88
89         /* msginfo headers */
90         {MATCHCRITERIA_SUBJECT, "subject"},
91         {MATCHCRITERIA_NOT_SUBJECT, "~subject"},
92         {MATCHCRITERIA_FROM, "from"},
93         {MATCHCRITERIA_NOT_FROM, "~from"},
94         {MATCHCRITERIA_TO, "to"},
95         {MATCHCRITERIA_NOT_TO, "~to"},
96         {MATCHCRITERIA_CC, "cc"},
97         {MATCHCRITERIA_NOT_CC, "~cc"},
98         {MATCHCRITERIA_TO_OR_CC, "to_or_cc"},
99         {MATCHCRITERIA_NOT_TO_AND_NOT_CC, "~to_or_cc"},
100         {MATCHCRITERIA_TAG, "tag"},
101         {MATCHCRITERIA_NOT_TAG, "~tag"},
102         {MATCHCRITERIA_TAGGED, "tagged"},
103         {MATCHCRITERIA_NOT_TAGGED, "~tagged"},
104         {MATCHCRITERIA_AGE_GREATER, "age_greater"},
105         {MATCHCRITERIA_AGE_LOWER, "age_lower"},
106         {MATCHCRITERIA_NEWSGROUPS, "newsgroups"},
107         {MATCHCRITERIA_NOT_NEWSGROUPS, "~newsgroups"},
108         {MATCHCRITERIA_INREPLYTO, "inreplyto"},
109         {MATCHCRITERIA_NOT_INREPLYTO, "~inreplyto"},
110         {MATCHCRITERIA_REFERENCES, "references"},
111         {MATCHCRITERIA_NOT_REFERENCES, "~references"},
112         {MATCHCRITERIA_SCORE_GREATER, "score_greater"},
113         {MATCHCRITERIA_SCORE_LOWER, "score_lower"},
114         {MATCHCRITERIA_SCORE_EQUAL, "score_equal"},
115         {MATCHCRITERIA_PARTIAL, "partial"},
116         {MATCHCRITERIA_NOT_PARTIAL, "~partial"},
117         {MATCHCRITERIA_FOUND_IN_ADDRESSBOOK, "found_in_addressbook"},
118         {MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK, "~found_in_addressbook"},
119
120         {MATCHCRITERIA_SIZE_GREATER, "size_greater"},
121         {MATCHCRITERIA_SIZE_SMALLER, "size_smaller"},
122         {MATCHCRITERIA_SIZE_EQUAL,   "size_equal"},
123
124         /* content have to be read */
125         {MATCHCRITERIA_HEADER, "header"},
126         {MATCHCRITERIA_NOT_HEADER, "~header"},
127         {MATCHCRITERIA_HEADERS_PART, "headers_part"},
128         {MATCHCRITERIA_NOT_HEADERS_PART, "~headers_part"},
129         {MATCHCRITERIA_MESSAGE, "message"},
130         {MATCHCRITERIA_NOT_MESSAGE, "~message"},
131         {MATCHCRITERIA_BODY_PART, "body_part"},
132         {MATCHCRITERIA_NOT_BODY_PART, "~body_part"},
133         {MATCHCRITERIA_TEST, "test"},
134         {MATCHCRITERIA_NOT_TEST, "~test"},
135
136         /* match type */
137         {MATCHTYPE_MATCHCASE, "matchcase"},
138         {MATCHTYPE_MATCH, "match"},
139         {MATCHTYPE_REGEXPCASE, "regexpcase"},
140         {MATCHTYPE_REGEXP, "regexp"},
141
142         /* actions */
143         {MATCHACTION_SCORE, "score"},    /* for backward compatibility */
144         {MATCHACTION_MOVE, "move"},
145         {MATCHACTION_COPY, "copy"},
146         {MATCHACTION_DELETE, "delete"},
147         {MATCHACTION_MARK, "mark"},
148         {MATCHACTION_UNMARK, "unmark"},
149         {MATCHACTION_LOCK, "lock"},
150         {MATCHACTION_UNLOCK, "unlock"},
151         {MATCHACTION_MARK_AS_READ, "mark_as_read"},
152         {MATCHACTION_MARK_AS_UNREAD, "mark_as_unread"},
153         {MATCHACTION_MARK_AS_SPAM, "mark_as_spam"},
154         {MATCHACTION_MARK_AS_HAM, "mark_as_ham"},
155         {MATCHACTION_FORWARD, "forward"},
156         {MATCHACTION_FORWARD_AS_ATTACHMENT, "forward_as_attachment"},
157         {MATCHACTION_EXECUTE, "execute"},
158         {MATCHACTION_COLOR, "color"},
159         {MATCHACTION_REDIRECT, "redirect"},
160         {MATCHACTION_CHANGE_SCORE, "change_score"},
161         {MATCHACTION_SET_SCORE, "set_score"},
162         {MATCHACTION_STOP, "stop"},
163         {MATCHACTION_HIDE, "hide"},
164         {MATCHACTION_IGNORE, "ignore"},
165         {MATCHACTION_WATCH, "watch"},
166         {MATCHACTION_ADD_TO_ADDRESSBOOK, "add_to_addressbook"},
167         {MATCHACTION_SET_TAG, "set_tag"},
168         {MATCHACTION_UNSET_TAG, "unset_tag"},
169         {MATCHACTION_CLEAR_TAGS, "clear_tags"},
170 };
171
172 enum {
173         MATCH_ANY = 0,
174         MATCH_ALL = 1,
175         MATCH_ONE = 2
176 };
177
178 extern gboolean debug_filtering_session;
179
180 /*!
181  *\brief        Look up table with keywords defined in \sa matchparser_tab
182  */
183 static GHashTable *matchparser_hashtab;
184
185 /*!
186  *\brief        Translate keyword id to keyword string
187  *
188  *\param        id Id of keyword
189  *
190  *\return       const gchar * Keyword
191  */
192 const gchar *get_matchparser_tab_str(gint id)
193 {
194         gint i;
195
196         for (i = 0; i < sizeof matchparser_tab / sizeof matchparser_tab[0]; i++) {
197                 if (matchparser_tab[i].id == id)
198                         return matchparser_tab[i].str;
199         }
200         return NULL;
201 }
202
203 /*!
204  *\brief        Create keyword lookup table
205  */
206 static void create_matchparser_hashtab(void)
207 {
208         int i;
209         
210         if (matchparser_hashtab) return;
211         matchparser_hashtab = g_hash_table_new(g_str_hash, g_str_equal);
212         for (i = 0; i < sizeof matchparser_tab / sizeof matchparser_tab[0]; i++)
213                 g_hash_table_insert(matchparser_hashtab,
214                                     matchparser_tab[i].str,
215                                     (gpointer) &matchparser_tab[i]);
216 }
217
218 /*!
219  *\brief        Return a keyword id from a keyword string
220  *
221  *\param        str Keyword string
222  *
223  *\return       gint Keyword id
224  */
225 gint get_matchparser_tab_id(const gchar *str)
226 {
227         MatchParser *res;
228
229         if (NULL != (res = g_hash_table_lookup(matchparser_hashtab, str))) {
230                 return res->id;
231         } else
232                 return -1;
233 }
234
235 /* **************** data structure allocation **************** */
236
237 /*!
238  *\brief        Allocate a structure for a filtering / scoring
239  *              "condition" (a matcher structure)
240  *
241  *\param        criteria Criteria ID (MATCHCRITERIA_XXXX)
242  *\param        header Header string (if criteria is MATCHCRITERIA_HEADER
243                         or MATCHCRITERIA_FOUND_IN_ADDRESSBOOK)
244  *\param        matchtype Type of action (MATCHTYPE_XXX)
245  *\param        expr String value or expression to check
246  *\param        value Integer value to check
247  *
248  *\return       MatcherProp * Pointer to newly allocated structure
249  */
250 MatcherProp *matcherprop_new(gint criteria, const gchar *header,
251                               gint matchtype, const gchar *expr,
252                               int value)
253 {
254         MatcherProp *prop;
255
256         prop = g_new0(MatcherProp, 1);
257         prop->criteria = criteria;
258         prop->header = header != NULL ? g_strdup(header) : NULL;
259
260         if (matchtype == MATCHTYPE_MATCHCASE ||
261             matchtype == MATCHTYPE_REGEXPCASE)
262                 prop->expr = expr != NULL ? g_utf8_casefold(expr, -1) : NULL;
263         else
264                 prop->expr = expr != NULL ? g_strdup(expr) : NULL;
265
266         prop->matchtype = matchtype;
267         prop->preg = NULL;
268         prop->value = value;
269         prop->error = 0;
270
271         return prop;
272 }
273
274 /*!
275  *\brief        Free a matcher structure
276  *
277  *\param        prop Pointer to matcher structure allocated with
278  *              #matcherprop_new
279  */
280 void matcherprop_free(MatcherProp *prop)
281 {
282         g_free(prop->expr);
283         g_free(prop->header);
284         if (prop->preg != NULL) {
285                 regfree(prop->preg);
286                 g_free(prop->preg);
287         }
288         g_free(prop);
289 }
290
291 /*!
292  *\brief        Copy a matcher structure
293  *
294  *\param        src Matcher structure to copy
295  *
296  *\return       MatcherProp * Pointer to newly allocated matcher structure
297  */
298 MatcherProp *matcherprop_copy(const MatcherProp *src)
299 {
300         MatcherProp *prop = g_new0(MatcherProp, 1);
301         
302         prop->criteria = src->criteria;
303         prop->header = src->header ? g_strdup(src->header) : NULL;
304         prop->expr = src->expr ? g_strdup(src->expr) : NULL;
305         prop->matchtype = src->matchtype;
306         
307         prop->preg = NULL; /* will be re-evaluated */
308         prop->value = src->value;
309         prop->error = src->error;       
310         return prop;            
311 }
312
313 /* ************** match ******************************/
314
315 static gboolean match_with_addresses_in_addressbook
316         (MatcherProp *prop, GSList *address_list, gint type,
317          gchar* folderpath, gint match)
318 {
319         GSList *walk = NULL;
320         gboolean found = FALSE;
321         gchar *path = NULL;
322
323         g_return_val_if_fail(address_list != NULL, FALSE);
324
325         debug_print("match_with_addresses_in_addressbook(%d, %s)\n",
326                                 g_slist_length(address_list), folderpath?folderpath:"(null)");
327
328         if (folderpath == NULL ||
329                 strcasecmp(folderpath, _("Any")) == 0 ||
330                 *folderpath == '\0')
331                 path = NULL;
332         else
333                 path = folderpath;
334         
335         start_address_completion(path);
336
337         for (walk = address_list; walk != NULL; walk = walk->next) {
338                 /* exact matching of email address */
339                 guint num_addr = complete_address(walk->data);
340                 found = FALSE;
341                 if (num_addr > 1) {
342                         /* skip first item (this is the search string itself) */
343                         int i = 1;
344                         for (; i < num_addr && !found; i++) {
345                                 gchar *addr = get_complete_address(i);
346                                 extract_address(addr);
347                                 if (strcasecmp(addr, walk->data) == 0) {
348                                         found = TRUE;
349
350                                         /* debug output */
351                                         if (debug_filtering_session
352                                                         && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
353                                                 log_print(LOG_DEBUG_FILTERING,
354                                                                 "address [ %s ] matches\n",
355                                                                 (gchar *)walk->data);
356                                         }
357                                 }
358                                 g_free(addr);
359                         }
360                 }
361                 /* debug output */
362                 if (debug_filtering_session
363                                 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH
364                                 && !found) {
365                         log_print(LOG_DEBUG_FILTERING,
366                                         "address [ %s ] doesn't match\n",
367                                         (gchar *)walk->data);
368                 }
369                 g_free(walk->data);
370
371                 if (match == MATCH_ALL) {
372                         /* if matching all addresses, stop if one doesn't match */
373                         if (!found) {
374                                 /* debug output */
375                                 if (debug_filtering_session
376                                                 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
377                                         log_print(LOG_DEBUG_FILTERING,
378                                                         "not all address match (matching all)\n");
379                                 }
380                                 break;
381                         }
382                 } else if (match == MATCH_ANY) {
383                         /* if matching any address, stop if one does match */
384                         if (found) {
385                                 /* debug output */
386                                 if (debug_filtering_session
387                                                 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
388                                         log_print(LOG_DEBUG_FILTERING,
389                                                         "at least one address matches (matching any)\n");
390                                 }
391                                 break;
392                         }
393                 }
394                 /* MATCH_ONE: there should be only one loop iteration */
395         }
396
397         end_address_completion();
398         
399         return found;
400 }
401
402 /*!
403  *\brief        Find out if a string matches a condition
404  *
405  *\param        prop Matcher structure
406  *\param        str String to check 
407  *
408  *\return       gboolean TRUE if str matches the condition in the 
409  *              matcher structure
410  */
411 static gboolean matcherprop_string_match(MatcherProp *prop, const gchar *str,
412                                          const gchar *debug_context)
413 {
414         gchar *str1;
415         gboolean ret = FALSE;
416         gboolean should_free = FALSE;
417         if (str == NULL)
418                 return FALSE;
419
420         if (prop->matchtype == MATCHTYPE_REGEXPCASE ||
421             prop->matchtype == MATCHTYPE_MATCHCASE) {
422                 str1 = g_utf8_casefold(str, -1);
423                 should_free = TRUE;
424         } else {
425                 str1 = (gchar *)str;
426                 should_free = FALSE;
427         }
428
429         switch (prop->matchtype) {
430         case MATCHTYPE_REGEXPCASE:
431         case MATCHTYPE_REGEXP:
432                 if (!prop->preg && (prop->error == 0)) {
433                         prop->preg = g_new0(regex_t, 1);
434                         /* if regexp then don't use the escaped string */
435                         if (regcomp(prop->preg, prop->expr,
436                                     REG_NOSUB | REG_EXTENDED
437                                     | ((prop->matchtype == MATCHTYPE_REGEXPCASE)
438                                     ? REG_ICASE : 0)) != 0) {
439                                 prop->error = 1;
440                                 g_free(prop->preg);
441                                 prop->preg = NULL;
442                         }
443                 }
444                 if (prop->preg == NULL)
445                         return FALSE;
446                 
447                 if (regexec(prop->preg, str1, 0, NULL, 0) == 0)
448                         ret = TRUE;
449                 else
450                         ret = FALSE;
451
452                 /* debug output */
453                 if (debug_filtering_session
454                                 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
455                         gchar *stripped = g_strdup(str);
456
457                         strretchomp(stripped);
458                         if (ret) {
459                                 log_print(LOG_DEBUG_FILTERING,
460                                                 "%s value [ %s ] matches regular expression [ %s ] (%s)\n",
461                                                 debug_context, stripped, prop->expr,
462                                                 prop->matchtype == MATCHTYPE_REGEXP ? _("Case sensitive"):_("Case unsensitive"));
463                         } else {
464                                 log_print(LOG_DEBUG_FILTERING,
465                                                 "%s value [ %s ] doesn't matches regular expression [ %s ] (%s)\n",
466                                                 debug_context, stripped, prop->expr,
467                                                 prop->matchtype == MATCHTYPE_REGEXP ? _("Case sensitive"):_("Case unsensitive"));
468                         }
469                         g_free(stripped);
470                 }
471                 break;
472                         
473         case MATCHTYPE_MATCHCASE:
474         case MATCHTYPE_MATCH:
475                 ret = (strstr(str1, prop->expr) != NULL);
476
477                 /* debug output */
478                 if (debug_filtering_session
479                                 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
480                         gchar *stripped = g_strdup(str);
481
482                         strretchomp(stripped);
483                         if (ret) {
484                                 log_print(LOG_DEBUG_FILTERING,
485                                                 "%s value [ %s ] contains [ %s ] (%s)\n",
486                                                 debug_context, stripped, prop->expr,
487                                                 prop->matchtype == MATCHTYPE_MATCH ? _("Case sensitive"):_("Case unsensitive"));
488                         } else {
489                                 log_print(LOG_DEBUG_FILTERING,
490                                                 "%s value [ %s ] doesn't contains [ %s ] (%s)\n",
491                                                 debug_context, stripped, prop->expr,
492                                                 prop->matchtype == MATCHTYPE_MATCH ? _("Case sensitive"):_("Case unsensitive"));
493                         }
494                         g_free(stripped);
495                 }
496                 break;
497
498         default:
499                 break;
500         }
501         
502         if (should_free) {
503                 g_free(str1);
504         }
505         return ret;
506 }
507
508 /*!
509  *\brief        Find out if a tag matches a condition
510  *
511  *\param        prop Matcher structure
512  *\param        msginfo message to check
513  *
514  *\return       gboolean TRUE if msginfo matches the condition in the 
515  *              matcher structure
516  */
517 static gboolean matcherprop_tag_match(MatcherProp *prop, MsgInfo *msginfo,
518                                          const gchar *debug_context)
519 {
520         gboolean ret = FALSE;
521         GSList *cur;
522
523         if (msginfo == NULL || msginfo->tags == NULL)
524                 return FALSE;
525
526         for (cur = msginfo->tags; cur; cur = cur->next) {
527                 const gchar *str = tags_get_tag(GPOINTER_TO_INT(cur->data));
528                 if (!str)
529                         continue;
530                 if (matcherprop_string_match(prop, str, debug_context)) {
531                         ret = TRUE;
532                         break;
533                 }
534         }
535         return ret;
536 }
537
538 /*!
539  *\brief        Find out if the string-ed list matches a condition
540  *
541  *\param        prop Matcher structure
542  *\param        list GSList of strings to check
543  *
544  *\return       gboolean TRUE if str matches the condition in the 
545  *              matcher structure
546  */
547 static gboolean matcherprop_list_match(MatcherProp *prop, const GSList *list,
548 const gchar *debug_context)
549 {
550         const GSList *cur;
551
552         for(cur = list; cur != NULL; cur = cur->next) {
553                 if (matcherprop_string_match(prop, (gchar *)cur->data, debug_context))
554                         return TRUE;
555         }
556         return FALSE;
557 }
558
559 /* FIXME body search is a hack. */
560 static gboolean matcherprop_string_decode_match(MatcherProp *prop, const gchar *str,
561                                                                                                 const gchar *debug_context)
562 {
563         gchar *utf = NULL;
564         gchar tmp[BUFFSIZE];
565         gboolean res = FALSE;
566
567         if (str == NULL)
568                 return FALSE;
569
570         /* we try to decode QP first, because it's faster than base64 */
571         qp_decode_const(tmp, BUFFSIZE-1, str);
572         if (!g_utf8_validate(tmp, -1, NULL)) {
573                 utf = conv_codeset_strdup
574                         (tmp, conv_get_locale_charset_str_no_utf8(),
575                          CS_INTERNAL);
576                 res = matcherprop_string_match(prop, utf, debug_context);
577                 g_free(utf);
578         } else {
579                 res = matcherprop_string_match(prop, tmp, debug_context);
580         }
581         
582         if (res == FALSE && (strchr(prop->expr, '=') || strchr(prop->expr, '_')
583                             || strchr(str, '=') || strchr(str, '_'))) {
584                 /* if searching for something with an equal char, maybe 
585                  * we should try to match the non-decoded string. 
586                  * In case it was not qp-encoded. */
587                 if (!g_utf8_validate(str, -1, NULL)) {
588                         utf = conv_codeset_strdup
589                                 (str, conv_get_locale_charset_str_no_utf8(),
590                                  CS_INTERNAL);
591                         res = matcherprop_string_match(prop, utf, debug_context);
592                         g_free(utf);
593                 } else {
594                         res = matcherprop_string_match(prop, str, debug_context);
595                 }
596         }
597
598         /* FIXME base64 decoding is too slow, especially since text can 
599          * easily be handled as base64. Don't even try now. */
600
601         return res;
602 }
603
604 #ifdef USE_PTHREAD
605 typedef struct _thread_data {
606         const gchar *cmd;
607         gboolean done;
608 } thread_data;
609 #endif
610
611 #ifdef USE_PTHREAD
612 static void *matcher_test_thread(void *data)
613 {
614         thread_data *td = (thread_data *)data;
615         int result = -1;
616
617         pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
618         pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
619
620         result = execute_command_line(td->cmd, FALSE);
621         td->done = TRUE; /* let the caller thread join() */
622         return GINT_TO_POINTER(result);
623 }
624 #endif
625
626 /*!
627  *\brief        Execute a command defined in the matcher structure
628  *
629  *\param        prop Pointer to matcher structure
630  *\param        info Pointer to message info structure
631  *
632  *\return       gboolean TRUE if command was executed succesfully
633  */
634 static gboolean matcherprop_match_test(const MatcherProp *prop, 
635                                           MsgInfo *info)
636 {
637         gchar *file;
638         gchar *cmd;
639         gint retval;
640 #ifdef USE_PTHREAD
641         pthread_t pt;
642         thread_data *td = g_new0(thread_data, 1);
643         void *res = NULL;
644         time_t start_time = time(NULL);
645 #endif
646
647         file = procmsg_get_message_file(info);
648         if (file == NULL)
649                 return FALSE;
650         g_free(file);           
651
652         cmd = matching_build_command(prop->expr, info);
653         if (cmd == NULL)
654                 return FALSE;
655
656 #if (defined USE_PTHREAD && ((defined __GLIBC__ && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 3))) || !defined __GLIBC__))
657         /* debug output */
658         if (debug_filtering_session
659                         && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
660                 log_print(LOG_DEBUG_FILTERING,
661                                 "starting threaded command [ %s ]\n",
662                                 cmd);
663         }
664
665         td->cmd = cmd;
666         td->done = FALSE;
667         if (pthread_create(&pt, PTHREAD_CREATE_JOINABLE, 
668                         matcher_test_thread, td) != 0)
669                 retval = execute_command_line(cmd, FALSE);
670         else {
671                 debug_print("waiting for test thread\n");
672                 while(!td->done) {
673                         /* don't let the interface freeze while waiting */
674                         claws_do_idle();
675                         if (time(NULL) - start_time > 30) {
676                                 pthread_cancel(pt);
677                                 td->done = TRUE;
678                                 retval = -1;
679                         }
680                 }
681                 pthread_join(pt, &res);
682                 retval = GPOINTER_TO_INT(res);
683                 debug_print(" test thread returned %d\n", retval);
684         }
685         g_free(td);
686 #else
687         /* debug output */
688         if (debug_filtering_session
689                         && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
690                 log_print(LOG_DEBUG_FILTERING,
691                                 "starting synchronous command [ %s ]\n",
692                                 cmd);
693         }
694
695         retval = execute_command_line(cmd, FALSE);
696 #endif
697         debug_print("Command exit code: %d\n", retval);
698
699         /* debug output */
700         if (debug_filtering_session
701                         && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
702                 log_print(LOG_DEBUG_FILTERING,
703                                 "command returned [ %d ]\n",
704                                 retval);
705         }
706
707         g_free(cmd);
708         return (retval == 0);
709 }
710
711 /*!
712  *\brief        Check if a message matches the condition in a matcher
713  *              structure.
714  *
715  *\param        prop Pointer to matcher structure
716  *\param        info Pointer to message info
717  *
718  *\return       gboolean TRUE if a match
719  */
720 gboolean matcherprop_match(MatcherProp *prop, 
721                            MsgInfo *info)
722 {
723         time_t t;
724
725         switch(prop->criteria) {
726         case MATCHCRITERIA_ALL:
727                 return TRUE;
728         case MATCHCRITERIA_UNREAD:
729                 return MSG_IS_UNREAD(info->flags);
730         case MATCHCRITERIA_NOT_UNREAD:
731                 return !MSG_IS_UNREAD(info->flags);
732         case MATCHCRITERIA_NEW:
733                 return MSG_IS_NEW(info->flags);
734         case MATCHCRITERIA_NOT_NEW:
735                 return !MSG_IS_NEW(info->flags);
736         case MATCHCRITERIA_MARKED:
737                 return MSG_IS_MARKED(info->flags);
738         case MATCHCRITERIA_NOT_MARKED:
739                 return !MSG_IS_MARKED(info->flags);
740         case MATCHCRITERIA_DELETED:
741                 return MSG_IS_DELETED(info->flags);
742         case MATCHCRITERIA_NOT_DELETED:
743                 return !MSG_IS_DELETED(info->flags);
744         case MATCHCRITERIA_REPLIED:
745                 return MSG_IS_REPLIED(info->flags);
746         case MATCHCRITERIA_NOT_REPLIED:
747                 return !MSG_IS_REPLIED(info->flags);
748         case MATCHCRITERIA_FORWARDED:
749                 return MSG_IS_FORWARDED(info->flags);
750         case MATCHCRITERIA_NOT_FORWARDED:
751                 return !MSG_IS_FORWARDED(info->flags);
752         case MATCHCRITERIA_LOCKED:
753                 return MSG_IS_LOCKED(info->flags);
754         case MATCHCRITERIA_NOT_LOCKED:
755                 return !MSG_IS_LOCKED(info->flags);
756         case MATCHCRITERIA_SPAM:
757                 return MSG_IS_SPAM(info->flags);
758         case MATCHCRITERIA_NOT_SPAM:
759                 return !MSG_IS_SPAM(info->flags);
760         case MATCHCRITERIA_COLORLABEL:
761         {
762                 gint color = MSG_GET_COLORLABEL_VALUE(info->flags);
763                 gboolean ret = (color == prop->value);
764
765                 /* debug output */
766                 if (debug_filtering_session
767                                 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
768                         if (ret) {
769                                 log_print(LOG_DEBUG_FILTERING,
770                                                 "message color value [ %d ] matches color value [ %d ]\n",
771                                                 color, prop->value);
772                         } else {
773                                 log_print(LOG_DEBUG_FILTERING,
774                                                 "message color value [ %d ] doesn't matches color value [ %d ]\n",
775                                                 color, prop->value);
776                         }
777                 }
778                 return ret;
779         }
780         case MATCHCRITERIA_NOT_COLORLABEL:
781         {
782                 gint color = MSG_GET_COLORLABEL_VALUE(info->flags);
783                 gboolean ret = (color != prop->value);
784
785                 /* debug output */
786                 if (debug_filtering_session
787                                 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
788                         if (ret) {
789                                 log_print(LOG_DEBUG_FILTERING,
790                                                 "message color value [ %d ] matches color value [ %d ]\n",
791                                                 color, prop->value);
792                         } else {
793                                 log_print(LOG_DEBUG_FILTERING,
794                                                 "message color value [ %d ] doesn't matches color value [ %d ]\n",
795                                                 color, prop->value);
796                         }
797                 }
798                 return ret;
799         }
800         case MATCHCRITERIA_IGNORE_THREAD:
801                 return MSG_IS_IGNORE_THREAD(info->flags);
802         case MATCHCRITERIA_NOT_IGNORE_THREAD:
803                 return !MSG_IS_IGNORE_THREAD(info->flags);
804         case MATCHCRITERIA_WATCH_THREAD:
805                 return MSG_IS_WATCH_THREAD(info->flags);
806         case MATCHCRITERIA_NOT_WATCH_THREAD:
807                 return !MSG_IS_WATCH_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_WATCH_THREAD:
1652                 case MATCHCRITERIA_NOT_WATCH_THREAD:
1653                 case MATCHCRITERIA_SUBJECT:
1654                 case MATCHCRITERIA_NOT_SUBJECT:
1655                 case MATCHCRITERIA_FROM:
1656                 case MATCHCRITERIA_NOT_FROM:
1657                 case MATCHCRITERIA_TO:
1658                 case MATCHCRITERIA_NOT_TO:
1659                 case MATCHCRITERIA_CC:
1660                 case MATCHCRITERIA_NOT_CC:
1661                 case MATCHCRITERIA_TO_OR_CC:
1662                 case MATCHCRITERIA_NOT_TO_AND_NOT_CC:
1663                 case MATCHCRITERIA_TAG:
1664                 case MATCHCRITERIA_NOT_TAG:
1665                 case MATCHCRITERIA_TAGGED:
1666                 case MATCHCRITERIA_NOT_TAGGED:
1667                 case MATCHCRITERIA_AGE_GREATER:
1668                 case MATCHCRITERIA_AGE_LOWER:
1669                 case MATCHCRITERIA_NEWSGROUPS:
1670                 case MATCHCRITERIA_NOT_NEWSGROUPS:
1671                 case MATCHCRITERIA_INREPLYTO:
1672                 case MATCHCRITERIA_NOT_INREPLYTO:
1673                 case MATCHCRITERIA_REFERENCES:
1674                 case MATCHCRITERIA_NOT_REFERENCES:
1675                 case MATCHCRITERIA_SCORE_GREATER:
1676                 case MATCHCRITERIA_SCORE_LOWER:
1677                 case MATCHCRITERIA_SCORE_EQUAL:
1678                 case MATCHCRITERIA_SIZE_GREATER:
1679                 case MATCHCRITERIA_SIZE_SMALLER:
1680                 case MATCHCRITERIA_SIZE_EQUAL:
1681                 case MATCHCRITERIA_TEST:
1682                 case MATCHCRITERIA_NOT_TEST:
1683                 case MATCHCRITERIA_PARTIAL:
1684                 case MATCHCRITERIA_NOT_PARTIAL:
1685                         if (matcherprop_match(matcher, info)) {
1686                                 if (!matchers->bool_and) {
1687                                         if (debug_filtering_session)
1688                                                 log_status_ok(LOG_DEBUG_FILTERING, _("message matches\n"));
1689                                         return TRUE;
1690                                 }
1691                         }
1692                         else {
1693                                 if (matchers->bool_and) {
1694                                         if (debug_filtering_session)
1695                                                 log_status_nok(LOG_DEBUG_FILTERING, _("message does not match\n"));
1696                                         return FALSE;
1697                                 }
1698                         }
1699                 }
1700         }
1701
1702         /* test the condition on the file */
1703
1704         if (matcherlist_match_file(matchers, info, result)) {
1705                 if (!matchers->bool_and) {
1706                         if (debug_filtering_session)
1707                                 log_status_ok(LOG_DEBUG_FILTERING, _("message matches\n"));
1708                         return TRUE;
1709                 }
1710         } else {
1711                 if (matchers->bool_and) {
1712                         if (debug_filtering_session)
1713                                 log_status_nok(LOG_DEBUG_FILTERING, _("message does not match\n"));
1714                         return FALSE;
1715                 }
1716         }
1717
1718         if (debug_filtering_session) {
1719                 if (result)
1720                         log_status_ok(LOG_DEBUG_FILTERING, _("message matches\n"));
1721                 else
1722                         log_status_nok(LOG_DEBUG_FILTERING, _("message does not match\n"));
1723         }
1724         return result;
1725 }
1726
1727
1728 static gint quote_filter_str(gchar * result, guint size,
1729                              const gchar * path)
1730 {
1731         const gchar * p;
1732         gchar * result_p;
1733         guint remaining;
1734
1735         result_p = result;
1736         remaining = size;
1737
1738         for(p = path ; * p != '\0' ; p ++) {
1739
1740                 if ((* p != '\"') && (* p != '\\')) {
1741                         if (remaining > 0) {
1742                                 * result_p = * p;
1743                                 result_p ++; 
1744                                 remaining --;
1745                         }
1746                         else {
1747                                 result[size - 1] = '\0';
1748                                 return -1;
1749                         }
1750                 }
1751                 else { 
1752                         if (remaining >= 2) {
1753                                 * result_p = '\\';
1754                                 result_p ++; 
1755                                 * result_p = * p;
1756                                 result_p ++; 
1757                                 remaining -= 2;
1758                         }
1759                         else {
1760                                 result[size - 1] = '\0';
1761                                 return -1;
1762                         }
1763                 }
1764         }
1765         if (remaining > 0) {
1766                 * result_p = '\0';
1767         }
1768         else {
1769                 result[size - 1] = '\0';
1770                 return -1;
1771         }
1772   
1773         return 0;
1774 }
1775
1776
1777 gchar * matcher_quote_str(const gchar * src)
1778 {
1779         gchar * res;
1780         gint len;
1781         
1782         len = strlen(src) * 2 + 1;
1783         res = g_malloc(len);
1784         quote_filter_str(res, len, src);
1785         
1786         return res;
1787 }
1788
1789 /*!
1790  *\brief        Convert a matcher structure to a string
1791  *
1792  *\param        matcher Matcher structure
1793  *
1794  *\return       gchar * Newly allocated string
1795  */
1796 gchar *matcherprop_to_string(MatcherProp *matcher)
1797 {
1798         gchar *matcher_str = NULL;
1799         const gchar *criteria_str;
1800         const gchar *matchtype_str;
1801         int i;
1802         gchar * quoted_expr;
1803         gchar * quoted_header;
1804         
1805         criteria_str = NULL;
1806         for (i = 0; i < (int) (sizeof(matchparser_tab) / sizeof(MatchParser)); i++) {
1807                 if (matchparser_tab[i].id == matcher->criteria)
1808                         criteria_str = matchparser_tab[i].str;
1809         }
1810         if (criteria_str == NULL)
1811                 return NULL;
1812
1813         switch (matcher->criteria) {
1814         case MATCHCRITERIA_AGE_GREATER:
1815         case MATCHCRITERIA_AGE_LOWER:
1816         case MATCHCRITERIA_SCORE_GREATER:
1817         case MATCHCRITERIA_SCORE_LOWER:
1818         case MATCHCRITERIA_SCORE_EQUAL:
1819         case MATCHCRITERIA_SIZE_GREATER:
1820         case MATCHCRITERIA_SIZE_SMALLER:
1821         case MATCHCRITERIA_SIZE_EQUAL:
1822         case MATCHCRITERIA_COLORLABEL:
1823         case MATCHCRITERIA_NOT_COLORLABEL:
1824                 return g_strdup_printf("%s %i", criteria_str, matcher->value);
1825         case MATCHCRITERIA_ALL:
1826         case MATCHCRITERIA_UNREAD:
1827         case MATCHCRITERIA_NOT_UNREAD:
1828         case MATCHCRITERIA_NEW:
1829         case MATCHCRITERIA_NOT_NEW:
1830         case MATCHCRITERIA_MARKED:
1831         case MATCHCRITERIA_NOT_MARKED:
1832         case MATCHCRITERIA_DELETED:
1833         case MATCHCRITERIA_NOT_DELETED:
1834         case MATCHCRITERIA_REPLIED:
1835         case MATCHCRITERIA_NOT_REPLIED:
1836         case MATCHCRITERIA_FORWARDED:
1837         case MATCHCRITERIA_NOT_FORWARDED:
1838         case MATCHCRITERIA_LOCKED:
1839         case MATCHCRITERIA_NOT_LOCKED:
1840         case MATCHCRITERIA_SPAM:
1841         case MATCHCRITERIA_NOT_SPAM:
1842         case MATCHCRITERIA_PARTIAL:
1843         case MATCHCRITERIA_NOT_PARTIAL:
1844         case MATCHCRITERIA_IGNORE_THREAD:
1845         case MATCHCRITERIA_NOT_IGNORE_THREAD:
1846         case MATCHCRITERIA_WATCH_THREAD:
1847         case MATCHCRITERIA_NOT_WATCH_THREAD:
1848         case MATCHCRITERIA_TAGGED:
1849         case MATCHCRITERIA_NOT_TAGGED:
1850                 return g_strdup(criteria_str);
1851         case MATCHCRITERIA_TEST:
1852         case MATCHCRITERIA_NOT_TEST:
1853                 quoted_expr = matcher_quote_str(matcher->expr);
1854                 matcher_str = g_strdup_printf("%s \"%s\"",
1855                                               criteria_str, quoted_expr);
1856                 g_free(quoted_expr);
1857                 return matcher_str;
1858         case MATCHCRITERIA_FOUND_IN_ADDRESSBOOK:
1859         case MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK:
1860                 quoted_header = matcher_quote_str(matcher->header);
1861                 quoted_expr = matcher_quote_str(matcher->expr);
1862                 matcher_str = g_strdup_printf("%s \"%s\" in \"%s\"",
1863                                               criteria_str, quoted_header, quoted_expr);
1864                 g_free(quoted_header);
1865                 g_free(quoted_expr);
1866                 return matcher_str;
1867         }
1868
1869         matchtype_str = NULL;
1870         for (i = 0; i < sizeof matchparser_tab / sizeof matchparser_tab[0]; i++) {
1871                 if (matchparser_tab[i].id == matcher->matchtype)
1872                         matchtype_str = matchparser_tab[i].str;
1873         }
1874
1875         if (matchtype_str == NULL)
1876                 return NULL;
1877
1878         switch (matcher->matchtype) {
1879         case MATCHTYPE_MATCH:
1880         case MATCHTYPE_MATCHCASE:
1881         case MATCHTYPE_REGEXP:
1882         case MATCHTYPE_REGEXPCASE:
1883                 quoted_expr = matcher_quote_str(matcher->expr);
1884                 if (matcher->header) {
1885                         quoted_header = matcher_quote_str(matcher->header);
1886                         matcher_str = g_strdup_printf
1887                                         ("%s \"%s\" %s \"%s\"",
1888                                          criteria_str, quoted_header,
1889                                          matchtype_str, quoted_expr);
1890                         g_free(quoted_header);
1891                 }
1892                 else
1893                         matcher_str = g_strdup_printf
1894                                         ("%s %s \"%s\"", criteria_str,
1895                                          matchtype_str, quoted_expr);
1896                 g_free(quoted_expr);
1897                 break;
1898         }
1899
1900         return matcher_str;
1901 }
1902
1903 /*!
1904  *\brief        Convert a list of conditions to a string
1905  *
1906  *\param        matchers List of conditions
1907  *
1908  *\return       gchar * Newly allocated string
1909  */
1910 gchar *matcherlist_to_string(const MatcherList *matchers)
1911 {
1912         gint count;
1913         gchar **vstr;
1914         GSList *l;
1915         gchar **cur_str;
1916         gchar *result = NULL;
1917
1918         count = g_slist_length(matchers->matchers);
1919         vstr = g_new(gchar *, count + 1);
1920
1921         for (l = matchers->matchers, cur_str = vstr; l != NULL;
1922              l = g_slist_next(l), cur_str ++) {
1923                 *cur_str = matcherprop_to_string((MatcherProp *) l->data);
1924                 if (*cur_str == NULL)
1925                         break;
1926         }
1927         *cur_str = NULL;
1928         
1929         if (matchers->bool_and)
1930                 result = g_strjoinv(" & ", vstr);
1931         else
1932                 result = g_strjoinv(" | ", vstr);
1933
1934         for (cur_str = vstr ; *cur_str != NULL ; cur_str ++)
1935                 g_free(*cur_str);
1936         g_free(vstr);
1937
1938         return result;
1939 }
1940
1941
1942 #define STRLEN_ZERO(s) ((s) ? strlen(s) : 0)
1943 #define STRLEN_DEFAULT(s,d) ((s) ? strlen(s) : STRLEN_ZERO(d))
1944
1945 static void add_str_default(gchar ** dest,
1946                             const gchar * s, const gchar * d)
1947 {
1948         gchar quoted_str[4096];
1949         const gchar * str;
1950         
1951         if (s != NULL)
1952                 str = s;
1953         else
1954                 str = d;
1955         
1956         quote_cmd_argument(quoted_str, sizeof(quoted_str), str);
1957         strcpy(* dest, quoted_str);
1958         
1959         (* dest) += strlen(* dest);
1960 }
1961
1962 /* matching_build_command() - preferably cmd should be unescaped */
1963 /*!
1964  *\brief        Build the command line to execute
1965  *
1966  *\param        cmd String with command line specifiers
1967  *\param        info Message info to use for command
1968  *
1969  *\return       gchar * Newly allocated string
1970  */
1971 gchar *matching_build_command(const gchar *cmd, MsgInfo *info)
1972 {
1973         const gchar *s = cmd;
1974         gchar *filename = NULL;
1975         gchar *processed_cmd;
1976         gchar *p;
1977         gint size;
1978
1979         const gchar *const no_subject    = _("(none)") ;
1980         const gchar *const no_from       = _("(none)") ;
1981         const gchar *const no_to         = _("(none)") ;
1982         const gchar *const no_cc         = _("(none)") ;
1983         const gchar *const no_date       = _("(none)") ;
1984         const gchar *const no_msgid      = _("(none)") ;
1985         const gchar *const no_newsgroups = _("(none)") ;
1986         const gchar *const no_references = _("(none)") ;
1987
1988         size = STRLEN_ZERO(cmd) + 1;
1989         while (*s != '\0') {
1990                 if (*s == '%') {
1991                         s++;
1992                         switch (*s) {
1993                         case '%':
1994                                 size -= 1;
1995                                 break;
1996                         case 's': /* subject */
1997                                 size += STRLEN_DEFAULT(info->subject, no_subject) - 2;
1998                                 break;
1999                         case 'f': /* from */
2000                                 size += STRLEN_DEFAULT(info->from, no_from) - 2;
2001                                 break;
2002                         case 't': /* to */
2003                                 size += STRLEN_DEFAULT(info->to, no_to) - 2;
2004                                 break;
2005                         case 'c': /* cc */
2006                                 size += STRLEN_DEFAULT(info->cc, no_cc) - 2;
2007                                 break;
2008                         case 'd': /* date */
2009                                 size += STRLEN_DEFAULT(info->date, no_date) - 2;
2010                                 break;
2011                         case 'i': /* message-id */
2012                                 size += STRLEN_DEFAULT(info->msgid, no_msgid) - 2;
2013                                 break;
2014                         case 'n': /* newsgroups */
2015                                 size += STRLEN_DEFAULT(info->newsgroups, no_newsgroups) - 2;
2016                                 break;
2017                         case 'r': /* references */
2018                                 /* FIXME: using the inreplyto header for reference */
2019                                 size += STRLEN_DEFAULT(info->inreplyto, no_references) - 2;
2020                                 break;
2021                         case 'F': /* file */
2022                                 if (filename == NULL)
2023                                         filename = folder_item_fetch_msg(info->folder, info->msgnum);
2024                                 
2025                                 if (filename == NULL) {
2026                                         g_warning("filename is not set");
2027                                         return NULL;
2028                                 }
2029                                 else {
2030                                         size += strlen(filename) - 2;
2031                                 }
2032                                 break;
2033                         }
2034                         s++;
2035                 }
2036                 else s++;
2037         }
2038         
2039         /* as the string can be quoted, we double the result */
2040         size *= 2;
2041
2042         processed_cmd = g_new0(gchar, size);
2043         s = cmd;
2044         p = processed_cmd;
2045
2046         while (*s != '\0') {
2047                 if (*s == '%') {
2048                         s++;
2049                         switch (*s) {
2050                         case '%':
2051                                 *p = '%';
2052                                 p++;
2053                                 break;
2054                         case 's': /* subject */
2055                                 add_str_default(&p, info->subject,
2056                                                 no_subject);
2057                                 break;
2058                         case 'f': /* from */
2059                                 add_str_default(&p, info->from,
2060                                                 no_from);
2061                                 break;
2062                         case 't': /* to */
2063                                 add_str_default(&p, info->to,
2064                                                 no_to);
2065                                 break;
2066                         case 'c': /* cc */
2067                                 add_str_default(&p, info->cc,
2068                                                 no_cc);
2069                                 break;
2070                         case 'd': /* date */
2071                                 add_str_default(&p, info->date,
2072                                                 no_date);
2073                                 break;
2074                         case 'i': /* message-id */
2075                                 add_str_default(&p, info->msgid,
2076                                                 no_msgid);
2077                                 break;
2078                         case 'n': /* newsgroups */
2079                                 add_str_default(&p, info->newsgroups,
2080                                                 no_newsgroups);
2081                                 break;
2082                         case 'r': /* references */
2083                                 /* FIXME: using the inreplyto header for references */
2084                                 add_str_default(&p, info->inreplyto, no_references);
2085                                 break;
2086                         case 'F': /* file */
2087                                 if (filename != NULL)
2088                                         add_str_default(&p, filename, NULL);
2089                                 break;
2090                         default:
2091                                 *p = '%';
2092                                 p++;
2093                                 *p = *s;
2094                                 p++;
2095                                 break;
2096                         }
2097                         s++;
2098                 }
2099                 else {
2100                         *p = *s;
2101                         p++;
2102                         s++;
2103                 }
2104         }
2105         g_free(filename);
2106         
2107         return processed_cmd;
2108 }
2109 #undef STRLEN_DEFAULT
2110 #undef STRLEN_ZERO
2111
2112 /* ************************************************************ */
2113
2114
2115 /*!
2116  *\brief        Write filtering list to file
2117  *
2118  *\param        fp File
2119  *\param        prefs_filtering List of filtering conditions
2120  */
2121 static void prefs_filtering_write(FILE *fp, GSList *prefs_filtering)
2122 {
2123         GSList *cur = NULL;
2124
2125         for (cur = prefs_filtering; cur != NULL; cur = cur->next) {
2126                 gchar *filtering_str = NULL;
2127                 gchar *tmp_name = NULL;
2128                 FilteringProp *prop = NULL;
2129
2130                 if (NULL == (prop = (FilteringProp *) cur->data))
2131                         continue;
2132                 
2133                 if (NULL == (filtering_str = filteringprop_to_string(prop)))
2134                         continue;
2135
2136                 if (prop->enabled) {
2137                         if (fputs("enabled ", fp) == EOF) {
2138                                 FILE_OP_ERROR("filtering config", "fputs");
2139                                 return;
2140                         }
2141                 } else {
2142                         if (fputs("disabled ", fp) == EOF) {
2143                                 FILE_OP_ERROR("filtering config", "fputs");
2144                                 return;
2145                         }
2146                 }
2147
2148                 if (fputs("rulename \"", fp) == EOF) {
2149                         FILE_OP_ERROR("filtering config", "fputs");
2150                         g_free(filtering_str);
2151                         return;
2152                 }
2153                 tmp_name = prop->name;
2154                 while (tmp_name && *tmp_name != '\0') {
2155                         if (*tmp_name != '"') {
2156                                 if (fputc(*tmp_name, fp) == EOF) {
2157                                         FILE_OP_ERROR("filtering config", "fputs || fputc");
2158                                         g_free(filtering_str);
2159                                         return;
2160                                 }
2161                         } else if (*tmp_name == '"') {
2162                                 if (fputc('\\', fp) == EOF ||
2163                                     fputc('"', fp) == EOF) {
2164                                         FILE_OP_ERROR("filtering config", "fputs || fputc");
2165                                         g_free(filtering_str);
2166                                         return;
2167                                 }
2168                         }
2169                         tmp_name ++;
2170                 }
2171                 if (fputs("\" ", fp) == EOF) {
2172                         FILE_OP_ERROR("filtering config", "fputs");
2173                         g_free(filtering_str);
2174                         return;
2175                 }
2176
2177                 if (prop->account_id != 0) {
2178                         gchar *tmp = NULL;
2179
2180                         tmp = g_strdup_printf("account %d ", prop->account_id);
2181                         if (fputs(tmp, fp) == EOF) {
2182                                 FILE_OP_ERROR("filtering config", "fputs");
2183                                 g_free(tmp);
2184                                 return;
2185                         }
2186                         g_free(tmp);
2187                 }
2188
2189                 if(fputs(filtering_str, fp) == EOF ||
2190                     fputc('\n', fp) == EOF) {
2191                         FILE_OP_ERROR("filtering config", "fputs || fputc");
2192                         g_free(filtering_str);
2193                         return;
2194                 }
2195                 g_free(filtering_str);
2196         }
2197 }
2198
2199 /*!
2200  *\brief        Write matchers from a folder item
2201  *
2202  *\param        node Node with folder info
2203  *\param        data File pointer
2204  *
2205  *\return       gboolean FALSE
2206  */
2207 static gboolean prefs_matcher_write_func(GNode *node, gpointer data)
2208 {
2209         FolderItem *item;
2210         FILE *fp = data;
2211         gchar *id;
2212         GSList *prefs_filtering;
2213
2214         item = node->data;
2215         /* prevent warning */
2216         if (item->path == NULL)
2217                 return FALSE;
2218         id = folder_item_get_identifier(item);
2219         if (id == NULL)
2220                 return FALSE;
2221         prefs_filtering = item->prefs->processing;
2222
2223         if (prefs_filtering != NULL) {
2224                 fprintf(fp, "[%s]\n", id);
2225                 prefs_filtering_write(fp, prefs_filtering);
2226                 fputc('\n', fp);
2227         }
2228
2229         g_free(id);
2230
2231         return FALSE;
2232 }
2233
2234 /*!
2235  *\brief        Save matchers from folder items
2236  *
2237  *\param        fp File
2238  */
2239 static void prefs_matcher_save(FILE *fp)
2240 {
2241         GList *cur;
2242
2243         for (cur = folder_get_list() ; cur != NULL ; cur = g_list_next(cur)) {
2244                 Folder *folder;
2245
2246                 folder = (Folder *) cur->data;
2247                 g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
2248                                 prefs_matcher_write_func, fp);
2249         }
2250         
2251         /* pre global rules */
2252         fprintf(fp, "[preglobal]\n");
2253         prefs_filtering_write(fp, pre_global_processing);
2254         fputc('\n', fp);
2255
2256         /* post global rules */
2257         fprintf(fp, "[postglobal]\n");
2258         prefs_filtering_write(fp, post_global_processing);
2259         fputc('\n', fp);
2260         
2261         /* filtering rules */
2262         fprintf(fp, "[filtering]\n");
2263         prefs_filtering_write(fp, filtering_rules);
2264         fputc('\n', fp);
2265 }
2266
2267 /*!
2268  *\brief        Write filtering / matcher configuration file
2269  */
2270 void prefs_matcher_write_config(void)
2271 {
2272         gchar *rcpath;
2273         PrefFile *pfile;
2274
2275         debug_print("Writing matcher configuration...\n");
2276
2277         rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
2278                              MATCHER_RC, NULL);
2279
2280         if ((pfile = prefs_write_open(rcpath)) == NULL) {
2281                 g_warning("failed to write configuration to file\n");
2282                 g_free(rcpath);
2283                 return;
2284         }
2285
2286
2287         prefs_matcher_save(pfile->fp);
2288
2289         g_free(rcpath);
2290
2291         if (prefs_file_close(pfile) < 0) {
2292                 g_warning("failed to write configuration to file\n");
2293                 return;
2294         }
2295 }
2296
2297 /* ******************************************************************* */
2298
2299 static void matcher_add_rulenames(const gchar *rcpath)
2300 {
2301         gchar *newpath = g_strconcat(rcpath, ".new", NULL);
2302         FILE *src = g_fopen(rcpath, "rb");
2303         FILE *dst = g_fopen(newpath, "wb");
2304         gchar buf[BUFFSIZE];
2305
2306         if (dst == NULL) {
2307                 perror("fopen");
2308                 g_free(newpath);
2309                 return;
2310         }
2311
2312         while (fgets (buf, sizeof(buf), src) != NULL) {
2313                 if (strlen(buf) > 2 && buf[0] != '['
2314                 && strncmp(buf, "rulename \"", 10)
2315                 && strncmp(buf, "enabled rulename \"", 18)
2316                 && strncmp(buf, "disabled rulename \"", 18)) {
2317                         fwrite("enabled rulename \"\" ",
2318                                 strlen("enabled rulename \"\" "), 1, dst);
2319                 }
2320                 fwrite(buf, strlen(buf), 1, dst);
2321         }
2322         fclose(dst);
2323         fclose(src);
2324         move_file(newpath, rcpath, TRUE);
2325         g_free(newpath);
2326 }
2327
2328 /*!
2329  *\brief        Read matcher configuration
2330  */
2331 void prefs_matcher_read_config(void)
2332 {
2333         gchar *rcpath;
2334         gchar *rc_old_format;
2335         FILE *f;
2336
2337         create_matchparser_hashtab();
2338         prefs_filtering_clear();
2339
2340         rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MATCHER_RC, NULL);
2341         rc_old_format = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MATCHER_RC, 
2342                                 ".pre_names", NULL);
2343         
2344         if (!is_file_exist(rc_old_format) && is_file_exist(rcpath)) {
2345                 /* backup file with no rules names, in case 
2346                  * anything goes wrong */
2347                 copy_file(rcpath, rc_old_format, FALSE);
2348                 /* now hack the file in order to have it to the new format */
2349                 matcher_add_rulenames(rcpath);
2350         }
2351         
2352         g_free(rc_old_format);
2353
2354         f = g_fopen(rcpath, "rb");
2355         g_free(rcpath);
2356
2357         if (f != NULL) {
2358                 matcher_parser_start_parsing(f);
2359                 fclose(matcher_parserin);
2360         }
2361         else {
2362                 /* previous version compatibility */
2363
2364                 /* g_print("reading filtering\n"); */
2365                 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
2366                                      FILTERING_RC, NULL);
2367                 f = g_fopen(rcpath, "rb");
2368                 g_free(rcpath);
2369                 
2370                 if (f != NULL) {
2371                         matcher_parser_start_parsing(f);
2372                         fclose(matcher_parserin);
2373                 }
2374                 
2375                 /* g_print("reading scoring\n"); */
2376                 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
2377                                      SCORING_RC, NULL);
2378                 f = g_fopen(rcpath, "rb");
2379                 g_free(rcpath);
2380                 
2381                 if (f != NULL) {
2382                         matcher_parser_start_parsing(f);
2383                         fclose(matcher_parserin);
2384                 }
2385         }
2386 }