Use custom get_default_font_size() and _name()
[claws.git] / src / matcher.c
blobfccf954cf9ac0235eaeb70de88472af99bc5a022
1 /*
2 * Claws Mail -- a GTK based, lightweight, and fast e-mail client
3 * Copyright (C) 2002-2023 by the Claws Mail Team and Hiroyuki Yamamoto
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.
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.
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/>.
19 #ifdef HAVE_CONFIG_H
20 # include "config.h"
21 #include "claws-features.h"
22 #endif
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>
31 #ifdef USE_PTHREAD
32 #include <pthread.h>
33 #endif
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 "prefs_common.h"
46 #include "log.h"
47 #include "tags.h"
48 #include "folder_item_prefs.h"
49 #include "procmsg.h"
50 #include "file-utils.h"
52 /*!
53 *\brief Keyword lookup element
55 struct _MatchParser {
56 gint id; /*!< keyword id */
57 gchar *str; /*!< keyword */
59 typedef struct _MatchParser MatchParser;
61 /*!
62 *\brief Table with strings and ids used by the lexer and
63 * the parser. New keywords can be added here.
65 static const MatchParser matchparser_tab[] = {
66 /* msginfo flags */
67 {MATCHCRITERIA_ALL, "all"},
68 {MATCHCRITERIA_UNREAD, "unread"},
69 {MATCHCRITERIA_NOT_UNREAD, "~unread"},
70 {MATCHCRITERIA_NEW, "new"},
71 {MATCHCRITERIA_NOT_NEW, "~new"},
72 {MATCHCRITERIA_MARKED, "marked"},
73 {MATCHCRITERIA_NOT_MARKED, "~marked"},
74 {MATCHCRITERIA_DELETED, "deleted"},
75 {MATCHCRITERIA_NOT_DELETED, "~deleted"},
76 {MATCHCRITERIA_REPLIED, "replied"},
77 {MATCHCRITERIA_NOT_REPLIED, "~replied"},
78 {MATCHCRITERIA_FORWARDED, "forwarded"},
79 {MATCHCRITERIA_NOT_FORWARDED, "~forwarded"},
80 {MATCHCRITERIA_LOCKED, "locked"},
81 {MATCHCRITERIA_NOT_LOCKED, "~locked"},
82 {MATCHCRITERIA_COLORLABEL, "colorlabel"},
83 {MATCHCRITERIA_NOT_COLORLABEL, "~colorlabel"},
84 {MATCHCRITERIA_IGNORE_THREAD, "ignore_thread"},
85 {MATCHCRITERIA_NOT_IGNORE_THREAD, "~ignore_thread"},
86 {MATCHCRITERIA_WATCH_THREAD, "watch_thread"},
87 {MATCHCRITERIA_NOT_WATCH_THREAD, "~watch_thread"},
88 {MATCHCRITERIA_SPAM, "spam"},
89 {MATCHCRITERIA_NOT_SPAM, "~spam"},
90 {MATCHCRITERIA_HAS_ATTACHMENT, "has_attachment"},
91 {MATCHCRITERIA_HAS_NO_ATTACHMENT, "~has_attachment"},
92 {MATCHCRITERIA_SIGNED, "signed"},
93 {MATCHCRITERIA_NOT_SIGNED, "~signed"},
95 /* msginfo headers */
96 {MATCHCRITERIA_SUBJECT, "subject"},
97 {MATCHCRITERIA_NOT_SUBJECT, "~subject"},
98 {MATCHCRITERIA_FROM, "from"},
99 {MATCHCRITERIA_NOT_FROM, "~from"},
100 {MATCHCRITERIA_TO, "to"},
101 {MATCHCRITERIA_NOT_TO, "~to"},
102 {MATCHCRITERIA_CC, "cc"},
103 {MATCHCRITERIA_NOT_CC, "~cc"},
104 {MATCHCRITERIA_TO_OR_CC, "to_or_cc"},
105 {MATCHCRITERIA_NOT_TO_AND_NOT_CC, "~to_or_cc"},
106 {MATCHCRITERIA_TAG, "tag"},
107 {MATCHCRITERIA_NOT_TAG, "~tag"},
108 {MATCHCRITERIA_TAGGED, "tagged"},
109 {MATCHCRITERIA_NOT_TAGGED, "~tagged"},
110 {MATCHCRITERIA_AGE_GREATER, "age_greater"},
111 {MATCHCRITERIA_AGE_LOWER, "age_lower"},
112 {MATCHCRITERIA_AGE_GREATER_HOURS, "age_greater_hours"},
113 {MATCHCRITERIA_AGE_LOWER_HOURS, "age_lower_hours"},
114 {MATCHCRITERIA_DATE_AFTER, "date_after"},
115 {MATCHCRITERIA_DATE_BEFORE, "date_before"},
116 {MATCHCRITERIA_NEWSGROUPS, "newsgroups"},
117 {MATCHCRITERIA_NOT_NEWSGROUPS, "~newsgroups"},
118 {MATCHCRITERIA_MESSAGEID, "messageid"},
119 {MATCHCRITERIA_NOT_MESSAGEID, "~messageid"},
120 {MATCHCRITERIA_INREPLYTO, "inreplyto"},
121 {MATCHCRITERIA_NOT_INREPLYTO, "~inreplyto"},
122 {MATCHCRITERIA_REFERENCES, "references"},
123 {MATCHCRITERIA_NOT_REFERENCES, "~references"},
124 {MATCHCRITERIA_SCORE_GREATER, "score_greater"},
125 {MATCHCRITERIA_SCORE_LOWER, "score_lower"},
126 {MATCHCRITERIA_SCORE_EQUAL, "score_equal"},
127 {MATCHCRITERIA_PARTIAL, "partial"},
128 {MATCHCRITERIA_NOT_PARTIAL, "~partial"},
129 {MATCHCRITERIA_FOUND_IN_ADDRESSBOOK, "found_in_addressbook"},
130 {MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK, "~found_in_addressbook"},
132 {MATCHCRITERIA_SIZE_GREATER, "size_greater"},
133 {MATCHCRITERIA_SIZE_SMALLER, "size_smaller"},
134 {MATCHCRITERIA_SIZE_EQUAL, "size_equal"},
136 /* content have to be read */
137 {MATCHCRITERIA_HEADER, "header"},
138 {MATCHCRITERIA_NOT_HEADER, "~header"},
139 {MATCHCRITERIA_HEADERS_PART, "headers_part"},
140 {MATCHCRITERIA_NOT_HEADERS_PART, "~headers_part"},
141 {MATCHCRITERIA_HEADERS_CONT, "headers_cont"},
142 {MATCHCRITERIA_NOT_HEADERS_CONT, "~headers_cont"},
143 {MATCHCRITERIA_MESSAGE, "message"},
144 {MATCHCRITERIA_NOT_MESSAGE, "~message"},
145 {MATCHCRITERIA_BODY_PART, "body_part"},
146 {MATCHCRITERIA_NOT_BODY_PART, "~body_part"},
147 {MATCHCRITERIA_TEST, "test"},
148 {MATCHCRITERIA_NOT_TEST, "~test"},
150 /* match type */
151 {MATCHTYPE_MATCHCASE, "matchcase"},
152 {MATCHTYPE_MATCH, "match"},
153 {MATCHTYPE_REGEXPCASE, "regexpcase"},
154 {MATCHTYPE_REGEXP, "regexp"},
156 /* actions */
157 {MATCHACTION_SCORE, "score"}, /* for backward compatibility */
158 {MATCHACTION_MOVE, "move"},
159 {MATCHACTION_COPY, "copy"},
160 {MATCHACTION_DELETE, "delete"},
161 {MATCHACTION_MARK, "mark"},
162 {MATCHACTION_UNMARK, "unmark"},
163 {MATCHACTION_LOCK, "lock"},
164 {MATCHACTION_UNLOCK, "unlock"},
165 {MATCHACTION_MARK_AS_READ, "mark_as_read"},
166 {MATCHACTION_MARK_AS_UNREAD, "mark_as_unread"},
167 {MATCHACTION_MARK_AS_SPAM, "mark_as_spam"},
168 {MATCHACTION_MARK_AS_HAM, "mark_as_ham"},
169 {MATCHACTION_FORWARD, "forward"},
170 {MATCHACTION_FORWARD_AS_ATTACHMENT, "forward_as_attachment"},
171 {MATCHACTION_EXECUTE, "execute"},
172 {MATCHACTION_COLOR, "color"},
173 {MATCHACTION_REDIRECT, "redirect"},
174 {MATCHACTION_CHANGE_SCORE, "change_score"},
175 {MATCHACTION_SET_SCORE, "set_score"},
176 {MATCHACTION_STOP, "stop"},
177 {MATCHACTION_HIDE, "hide"},
178 {MATCHACTION_IGNORE, "ignore"},
179 {MATCHACTION_WATCH, "watch"},
180 {MATCHACTION_ADD_TO_ADDRESSBOOK, "add_to_addressbook"},
181 {MATCHACTION_SET_TAG, "set_tag"},
182 {MATCHACTION_UNSET_TAG, "unset_tag"},
183 {MATCHACTION_CLEAR_TAGS, "clear_tags"},
186 enum {
187 MATCH_ANY = 0,
188 MATCH_ALL = 1,
189 MATCH_ONE = 2
192 enum {
193 CONTEXT_SUBJECT,
194 CONTEXT_FROM,
195 CONTEXT_TO,
196 CONTEXT_CC,
197 CONTEXT_NEWSGROUPS,
198 CONTEXT_MESSAGEID,
199 CONTEXT_IN_REPLY_TO,
200 CONTEXT_REFERENCES,
201 CONTEXT_HEADER,
202 CONTEXT_HEADER_LINE,
203 CONTEXT_BODY_LINE,
204 CONTEXT_TAG,
205 N_CONTEXT_STRS
208 static gchar *context_str[N_CONTEXT_STRS];
210 void matcher_init(void)
212 if (context_str[CONTEXT_SUBJECT] != NULL)
213 return;
215 context_str[CONTEXT_SUBJECT] = g_strdup_printf(_("%s header"), prefs_common_translated_header_name("Subject:"));
216 context_str[CONTEXT_FROM] = g_strdup_printf(_("%s header"), prefs_common_translated_header_name("From:"));
217 context_str[CONTEXT_TO] = g_strdup_printf(_("%s header"), prefs_common_translated_header_name("To:"));
218 context_str[CONTEXT_CC] = g_strdup_printf(_("%s header"), prefs_common_translated_header_name("Cc:"));
219 context_str[CONTEXT_NEWSGROUPS] = g_strdup_printf(_("%s header"), prefs_common_translated_header_name("Newsgroups:"));
220 context_str[CONTEXT_MESSAGEID] = g_strdup_printf(_("%s header"), prefs_common_translated_header_name("Message-ID:"));
221 context_str[CONTEXT_IN_REPLY_TO] = g_strdup_printf(_("%s header"), prefs_common_translated_header_name("In-Reply-To:"));
222 context_str[CONTEXT_REFERENCES] = g_strdup_printf(_("%s header"), prefs_common_translated_header_name("References:"));
223 context_str[CONTEXT_HEADER] = g_strdup(_("header"));
224 context_str[CONTEXT_HEADER_LINE] = g_strdup(_("header line"));
225 context_str[CONTEXT_BODY_LINE] = g_strdup(_("body line"));
226 context_str[CONTEXT_TAG] = g_strdup(_("tag"));
229 void matcher_done(void)
231 int i;
232 for (i = 0; i < N_CONTEXT_STRS; i++) {
233 g_free(context_str[i]);
234 context_str[i] = NULL;
238 extern gboolean debug_filtering_session;
241 *\brief Look up table with keywords defined in \sa matchparser_tab
243 static GHashTable *matchparser_hashtab;
246 *\brief Translate keyword id to keyword string
248 *\param id Id of keyword
250 *\return const gchar * Keyword
252 const gchar *get_matchparser_tab_str(gint id)
254 gint i;
256 for (i = 0; i < sizeof matchparser_tab / sizeof matchparser_tab[0]; i++) {
257 if (matchparser_tab[i].id == id)
258 return matchparser_tab[i].str;
260 return NULL;
264 *\brief Create keyword lookup table
266 static void create_matchparser_hashtab(void)
268 int i;
270 if (matchparser_hashtab) return;
271 matchparser_hashtab = g_hash_table_new(g_str_hash, g_str_equal);
272 for (i = 0; i < sizeof matchparser_tab / sizeof matchparser_tab[0]; i++)
273 g_hash_table_insert(matchparser_hashtab,
274 matchparser_tab[i].str,
275 (gpointer) &matchparser_tab[i]);
279 *\brief Return a keyword id from a keyword string
281 *\param str Keyword string
283 *\return gint Keyword id
285 gint get_matchparser_tab_id(const gchar *str)
287 MatchParser *res;
289 if (NULL != (res = g_hash_table_lookup(matchparser_hashtab, str))) {
290 return res->id;
291 } else
292 return -1;
295 /* **************** data structure allocation **************** */
298 *\brief Allocate a structure for a filtering / scoring
299 * "condition" (a matcher structure)
301 *\param criteria Criteria ID (MATCHCRITERIA_XXXX)
302 *\param header Header string (if criteria is MATCHCRITERIA_HEADER
303 or MATCHCRITERIA_FOUND_IN_ADDRESSBOOK)
304 *\param matchtype Type of action (MATCHTYPE_XXX)
305 *\param expr String value or expression to check
306 *\param value Integer value to check
308 *\return MatcherProp * Pointer to newly allocated structure
310 MatcherProp *matcherprop_new(gint criteria, const gchar *header,
311 gint matchtype, const gchar *expr,
312 int value)
314 MatcherProp *prop;
316 prop = g_new0(MatcherProp, 1);
317 prop->criteria = criteria;
318 prop->header = header != NULL ? g_strdup(header) : NULL;
320 prop->expr = expr != NULL ? g_strdup(expr) : NULL;
322 prop->matchtype = matchtype;
323 #ifndef G_OS_WIN32
324 prop->preg = NULL;
325 #endif
326 prop->casefold_expr = NULL;
327 prop->value = value;
328 prop->error = 0;
330 return prop;
334 *\brief Free a matcher structure
336 *\param prop Pointer to matcher structure allocated with
337 * #matcherprop_new
339 void matcherprop_free(MatcherProp *prop)
341 g_free(prop->expr);
342 g_free(prop->header);
343 #ifndef G_OS_WIN32
344 if (prop->preg != NULL) {
345 regfree(prop->preg);
346 g_free(prop->preg);
348 g_free(prop->casefold_expr);
349 #endif
350 g_free(prop);
354 *\brief Copy a matcher structure
356 *\param src Matcher structure to copy
358 *\return MatcherProp * Pointer to newly allocated matcher structure
360 MatcherProp *matcherprop_copy(const MatcherProp *src)
362 MatcherProp *prop = g_new0(MatcherProp, 1);
364 prop->criteria = src->criteria;
365 prop->header = src->header ? g_strdup(src->header) : NULL;
366 prop->expr = src->expr ? g_strdup(src->expr) : NULL;
367 prop->matchtype = src->matchtype;
369 #ifndef G_OS_WIN32
370 prop->preg = NULL; /* will be re-evaluated */
371 #endif
372 prop->casefold_expr = src->casefold_expr ? g_strdup(src->casefold_expr) : NULL;
373 prop->value = src->value;
374 prop->error = src->error;
375 return prop;
378 /* ************** match ******************************/
380 static gboolean match_with_addresses_in_addressbook
381 (MatcherProp *prop, GSList *address_list, gint type,
382 gchar* folderpath, gint match)
384 GSList *walk = NULL;
385 gboolean found = FALSE;
386 gchar *path = NULL;
388 cm_return_val_if_fail(address_list != NULL, FALSE);
390 debug_print("match_with_addresses_in_addressbook(%d, %s)\n",
391 g_slist_length(address_list), folderpath?folderpath:"(null)");
393 if (folderpath == NULL ||
394 strcasecmp(folderpath, "Any") == 0 ||
395 *folderpath == '\0')
396 path = NULL;
397 else
398 path = folderpath;
400 start_address_completion(path);
402 for (walk = address_list; walk != NULL; walk = walk->next) {
403 /* exact matching of email address */
404 guint num_addr = complete_address(walk->data);
405 found = FALSE;
406 if (num_addr > 1) {
407 /* skip first item (this is the search string itself) */
408 int i = 1;
409 for (; i < num_addr && !found; i++) {
410 gchar *addr = get_complete_address(i);
411 extract_address(addr);
412 if (strcasecmp(addr, walk->data) == 0) {
413 found = TRUE;
415 /* debug output */
416 if (debug_filtering_session
417 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
418 log_print(LOG_DEBUG_FILTERING,
419 "address [ %s ] matches\n",
420 (gchar *)walk->data);
423 g_free(addr);
426 /* debug output */
427 if (debug_filtering_session
428 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH
429 && !found) {
430 log_print(LOG_DEBUG_FILTERING,
431 "address [ %s ] does NOT match\n",
432 (gchar *)walk->data);
434 g_free(walk->data);
436 if (match == MATCH_ALL) {
437 /* if matching all addresses, stop if one doesn't match */
438 if (!found) {
439 /* debug output */
440 if (debug_filtering_session
441 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
442 log_print(LOG_DEBUG_FILTERING,
443 "not all address match (matching all)\n");
445 break;
447 } else if (match == MATCH_ANY) {
448 /* if matching any address, stop if one does match */
449 if (found) {
450 /* debug output */
451 if (debug_filtering_session
452 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
453 log_print(LOG_DEBUG_FILTERING,
454 "at least one address matches (matching any)\n");
456 break;
459 /* MATCH_ONE: there should be only one loop iteration */
462 end_address_completion();
464 return found;
468 *\brief Find out if a string matches a condition
470 *\param prop Matcher structure
471 *\param str String to check
473 *\return gboolean TRUE if str matches the condition in the
474 * matcher structure
476 static gboolean matcherprop_string_match(MatcherProp *prop, const gchar *str,
477 const gchar *debug_context)
479 gchar *str1;
480 const gchar *down_expr;
481 gboolean ret = FALSE;
482 gboolean should_free = FALSE;
483 if (str == NULL)
484 return FALSE;
486 if (prop->matchtype == MATCHTYPE_REGEXPCASE ||
487 prop->matchtype == MATCHTYPE_MATCHCASE) {
488 str1 = g_utf8_casefold(str, -1);
489 if (!prop->casefold_expr) {
490 prop->casefold_expr = g_utf8_casefold(prop->expr, -1);
492 down_expr = prop->casefold_expr;
494 should_free = TRUE;
495 } else {
496 str1 = (gchar *)str;
497 down_expr = (gchar *)prop->expr;
498 should_free = FALSE;
501 switch (prop->matchtype) {
502 case MATCHTYPE_REGEXPCASE:
503 case MATCHTYPE_REGEXP:
504 if (!prop->preg && (prop->error == 0)) {
505 prop->preg = g_new0(regex_t, 1);
506 /* if regexp then don't use the escaped string */
507 if (regcomp(prop->preg, down_expr,
508 REG_NOSUB | REG_EXTENDED
509 | ((prop->matchtype == MATCHTYPE_REGEXPCASE)
510 ? REG_ICASE : 0)) != 0) {
511 prop->error = 1;
512 g_free(prop->preg);
513 prop->preg = NULL;
516 if (prop->preg == NULL) {
517 ret = FALSE;
518 goto free_strs;
521 if (regexec(prop->preg, str1, 0, NULL, 0) == 0)
522 ret = TRUE;
523 else
524 ret = FALSE;
526 /* debug output */
527 if (debug_filtering_session
528 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
529 gchar *stripped = g_strdup(str);
531 strretchomp(stripped);
532 if (ret) {
533 log_print(LOG_DEBUG_FILTERING,
534 "%s value [ %s ] matches regular expression [ %s ] (%s)\n",
535 debug_context, stripped, prop->expr,
536 prop->matchtype == MATCHTYPE_REGEXP ? _("Case sensitive"):_("Case insensitive"));
537 } else {
538 log_print(LOG_DEBUG_FILTERING,
539 "%s value [ %s ] does NOT match regular expression [ %s ] (%s)\n",
540 debug_context, stripped, prop->expr,
541 prop->matchtype == MATCHTYPE_REGEXP ? _("Case sensitive"):_("Case insensitive"));
543 g_free(stripped);
545 break;
546 case MATCHTYPE_MATCHCASE:
547 case MATCHTYPE_MATCH:
548 ret = (strstr(str1, down_expr) != NULL);
550 /* debug output */
551 if (debug_filtering_session
552 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
553 gchar *stripped = g_strdup(str);
555 strretchomp(stripped);
556 if (ret) {
557 log_print(LOG_DEBUG_FILTERING,
558 "%s value [ %s ] contains [ %s ] (%s)\n",
559 debug_context, stripped, prop->expr,
560 prop->matchtype == MATCHTYPE_MATCH ? _("Case sensitive"):_("Case insensitive"));
561 } else {
562 log_print(LOG_DEBUG_FILTERING,
563 "%s value [ %s ] does NOT contain [ %s ] (%s)\n",
564 debug_context, stripped, prop->expr,
565 prop->matchtype == MATCHTYPE_MATCH ? _("Case sensitive"):_("Case insensitive"));
567 g_free(stripped);
569 break;
571 default:
572 break;
575 free_strs:
576 if (should_free) {
577 g_free(str1);
579 return ret;
583 *\brief Find out if a tag matches a condition
585 *\param prop Matcher structure
586 *\param msginfo message to check
588 *\return gboolean TRUE if msginfo matches the condition in the
589 * matcher structure
591 static gboolean matcherprop_tag_match(MatcherProp *prop, MsgInfo *msginfo,
592 const gchar *debug_context)
594 gboolean ret = FALSE;
595 GSList *cur;
597 if (msginfo == NULL || msginfo->tags == NULL)
598 return FALSE;
600 for (cur = msginfo->tags; cur; cur = cur->next) {
601 const gchar *str = tags_get_tag(GPOINTER_TO_INT(cur->data));
602 if (!str)
603 continue;
604 if (matcherprop_string_match(prop, str, debug_context)) {
605 ret = TRUE;
606 break;
609 return ret;
613 *\brief Find out if the string-ed list matches a condition
615 *\param prop Matcher structure
616 *\param list GSList of strings to check
618 *\return gboolean TRUE if str matches the condition in the
619 * matcher structure
621 static gboolean matcherprop_list_match(MatcherProp *prop, const GSList *list,
622 const gchar *debug_context)
624 const GSList *cur;
626 for(cur = list; cur != NULL; cur = cur->next) {
627 if (matcherprop_string_match(prop, (gchar *)cur->data, debug_context))
628 return TRUE;
630 return FALSE;
633 static gboolean matcherprop_header_line_match(MatcherProp *prop, const gchar *hdr,
634 const gchar *str, const gboolean both,
635 const gchar *debug_context)
637 gboolean res = FALSE;
639 if (hdr == NULL || str == NULL)
640 return FALSE;
642 if (both) {
643 /* Search in all header names and content.
645 gchar *line = g_strdup_printf("%s %s", hdr, str);
646 res = matcherprop_string_match(prop, line, debug_context);
647 g_free(line);
648 } else {
649 /* Search only in content and exclude private headers.
650 * E.g.: searching for "H foo" in folder x/foo would return
651 * *all* mail as SCF and RMID will match.
652 * Searching for "H sent" would return all resent messages
653 * as "Resent-From: whatever" will match.
655 if (procheader_header_is_internal(hdr))
656 return FALSE;
657 res = matcherprop_string_match(prop, str, debug_context);
660 return res;
663 #ifdef USE_PTHREAD
664 typedef struct _thread_data {
665 const gchar *cmd;
666 gboolean done;
667 } thread_data;
668 #endif
670 #ifdef USE_PTHREAD
671 static void *matcher_test_thread(void *data)
673 thread_data *td = (thread_data *)data;
674 int result = -1;
676 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
677 pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
679 result = system(td->cmd);
680 td->done = TRUE; /* let the caller thread join() */
681 return GINT_TO_POINTER(result);
683 #endif
686 *\brief Execute a command defined in the matcher structure
688 *\param prop Pointer to matcher structure
689 *\param info Pointer to message info structure
691 *\return gboolean TRUE if command was executed successfully
693 static gboolean matcherprop_match_test(const MatcherProp *prop,
694 MsgInfo *info)
696 gchar *file;
697 gchar *cmd;
698 gint retval;
699 #ifdef USE_PTHREAD
700 pthread_t pt;
701 thread_data *td = g_new0(thread_data, 1);
702 void *res = NULL;
703 time_t start_time = time(NULL);
704 #endif
706 file = procmsg_get_message_file(info);
707 if (file == NULL) {
708 #ifdef USE_PTHREAD
709 g_free(td);
710 #endif
711 return FALSE;
713 g_free(file);
715 cmd = matching_build_command(prop->expr, info);
716 if (cmd == NULL) {
717 #ifdef USE_PTHREAD
718 g_free(td);
719 #endif
720 return FALSE;
723 #ifdef USE_PTHREAD
724 /* debug output */
725 if (debug_filtering_session
726 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
727 log_print(LOG_DEBUG_FILTERING,
728 "starting threaded command [ %s ]\n",
729 cmd);
732 td->cmd = cmd;
733 td->done = FALSE;
734 if (pthread_create(&pt, NULL, matcher_test_thread, td) != 0)
735 retval = system(cmd);
736 else {
737 debug_print("waiting for test thread\n");
738 while(!td->done) {
739 /* don't let the interface freeze while waiting */
740 if (time(NULL) - start_time > 0) {
741 claws_do_idle();
743 if (time(NULL) - start_time > 30) {
744 pthread_cancel(pt);
745 td->done = TRUE;
746 retval = -1;
749 pthread_join(pt, &res);
750 retval = GPOINTER_TO_INT(res);
751 debug_print(" test thread returned %d\n", retval);
753 g_free(td);
754 #else
755 /* debug output */
756 if (debug_filtering_session
757 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
758 log_print(LOG_DEBUG_FILTERING,
759 "starting synchronous command [ %s ]\n",
760 cmd);
763 retval = system(cmd);
764 #endif
765 debug_print("Command exit code: %d\n", retval);
767 /* debug output */
768 if (debug_filtering_session
769 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
770 log_print(LOG_DEBUG_FILTERING,
771 "command returned [ %d ]\n",
772 retval);
775 g_free(cmd);
776 return (retval == 0);
780 *\brief Check if a message matches the condition in a matcher
781 * structure.
783 *\param prop Pointer to matcher structure
784 *\param info Pointer to message info
786 *\return gboolean TRUE if a match
788 static gboolean matcherprop_match(MatcherProp *prop,
789 MsgInfo *info)
791 time_t t;
792 gint age_mult_hours = 1;
794 switch(prop->criteria) {
795 case MATCHCRITERIA_ALL:
796 return TRUE;
797 case MATCHCRITERIA_UNREAD:
798 return MSG_IS_UNREAD(info->flags);
799 case MATCHCRITERIA_NOT_UNREAD:
800 return !MSG_IS_UNREAD(info->flags);
801 case MATCHCRITERIA_NEW:
802 return MSG_IS_NEW(info->flags);
803 case MATCHCRITERIA_NOT_NEW:
804 return !MSG_IS_NEW(info->flags);
805 case MATCHCRITERIA_MARKED:
806 return MSG_IS_MARKED(info->flags);
807 case MATCHCRITERIA_NOT_MARKED:
808 return !MSG_IS_MARKED(info->flags);
809 case MATCHCRITERIA_DELETED:
810 return MSG_IS_DELETED(info->flags);
811 case MATCHCRITERIA_NOT_DELETED:
812 return !MSG_IS_DELETED(info->flags);
813 case MATCHCRITERIA_REPLIED:
814 return MSG_IS_REPLIED(info->flags);
815 case MATCHCRITERIA_NOT_REPLIED:
816 return !MSG_IS_REPLIED(info->flags);
817 case MATCHCRITERIA_FORWARDED:
818 return MSG_IS_FORWARDED(info->flags);
819 case MATCHCRITERIA_NOT_FORWARDED:
820 return !MSG_IS_FORWARDED(info->flags);
821 case MATCHCRITERIA_LOCKED:
822 return MSG_IS_LOCKED(info->flags);
823 case MATCHCRITERIA_NOT_LOCKED:
824 return !MSG_IS_LOCKED(info->flags);
825 case MATCHCRITERIA_SPAM:
826 return MSG_IS_SPAM(info->flags);
827 case MATCHCRITERIA_NOT_SPAM:
828 return !MSG_IS_SPAM(info->flags);
829 case MATCHCRITERIA_HAS_ATTACHMENT:
830 return MSG_IS_WITH_ATTACHMENT(info->flags);
831 case MATCHCRITERIA_HAS_NO_ATTACHMENT:
832 return !MSG_IS_WITH_ATTACHMENT(info->flags);
833 case MATCHCRITERIA_SIGNED:
834 return MSG_IS_SIGNED(info->flags);
835 case MATCHCRITERIA_NOT_SIGNED:
836 return !MSG_IS_SIGNED(info->flags);
837 case MATCHCRITERIA_COLORLABEL:
839 gint color = MSG_GET_COLORLABEL_VALUE(info->flags);
840 gboolean ret = (color == prop->value);
842 /* debug output */
843 if (debug_filtering_session
844 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
845 if (ret) {
846 log_print(LOG_DEBUG_FILTERING,
847 "message color value [ %d ] matches color value [ %d ]\n",
848 color, prop->value);
849 } else {
850 log_print(LOG_DEBUG_FILTERING,
851 "message color value [ %d ] does NOT match color value [ %d ]\n",
852 color, prop->value);
855 return ret;
857 case MATCHCRITERIA_NOT_COLORLABEL:
859 gint color = MSG_GET_COLORLABEL_VALUE(info->flags);
860 gboolean ret = (color != prop->value);
862 /* debug output */
863 if (debug_filtering_session
864 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
865 if (ret) {
866 log_print(LOG_DEBUG_FILTERING,
867 "message color value [ %d ] matches color value [ %d ]\n",
868 color, prop->value);
869 } else {
870 log_print(LOG_DEBUG_FILTERING,
871 "message color value [ %d ] does NOT match color value [ %d ]\n",
872 color, prop->value);
875 return ret;
877 case MATCHCRITERIA_IGNORE_THREAD:
878 return MSG_IS_IGNORE_THREAD(info->flags);
879 case MATCHCRITERIA_NOT_IGNORE_THREAD:
880 return !MSG_IS_IGNORE_THREAD(info->flags);
881 case MATCHCRITERIA_WATCH_THREAD:
882 return MSG_IS_WATCH_THREAD(info->flags);
883 case MATCHCRITERIA_NOT_WATCH_THREAD:
884 return !MSG_IS_WATCH_THREAD(info->flags);
885 case MATCHCRITERIA_SUBJECT:
886 return matcherprop_string_match(prop, info->subject, context_str[CONTEXT_SUBJECT]);
887 case MATCHCRITERIA_NOT_SUBJECT:
888 return !matcherprop_string_match(prop, info->subject, context_str[CONTEXT_SUBJECT]);
889 case MATCHCRITERIA_FROM:
890 return matcherprop_string_match(prop, info->from, context_str[CONTEXT_FROM]);
891 case MATCHCRITERIA_NOT_FROM:
892 return !matcherprop_string_match(prop, info->from, context_str[CONTEXT_FROM]);
893 case MATCHCRITERIA_TO:
894 return matcherprop_string_match(prop, info->to, context_str[CONTEXT_TO]);
895 case MATCHCRITERIA_NOT_TO:
896 return !matcherprop_string_match(prop, info->to, context_str[CONTEXT_TO]);
897 case MATCHCRITERIA_CC:
898 return matcherprop_string_match(prop, info->cc, context_str[CONTEXT_CC]);
899 case MATCHCRITERIA_NOT_CC:
900 return !matcherprop_string_match(prop, info->cc, context_str[CONTEXT_CC]);
901 case MATCHCRITERIA_TO_OR_CC:
902 return matcherprop_string_match(prop, info->to, context_str[CONTEXT_TO])
903 || matcherprop_string_match(prop, info->cc, context_str[CONTEXT_CC]);
904 case MATCHCRITERIA_NOT_TO_AND_NOT_CC:
905 return !matcherprop_string_match(prop, info->to, context_str[CONTEXT_TO])
906 && !matcherprop_string_match(prop, info->cc, context_str[CONTEXT_CC]);
907 case MATCHCRITERIA_TAG:
908 return matcherprop_tag_match(prop, info, context_str[CONTEXT_TAG]);
909 case MATCHCRITERIA_NOT_TAG:
910 return !matcherprop_tag_match(prop, info, context_str[CONTEXT_TAG]);
911 case MATCHCRITERIA_TAGGED:
912 return info->tags != NULL;
913 case MATCHCRITERIA_NOT_TAGGED:
914 return info->tags == NULL;
915 case MATCHCRITERIA_AGE_GREATER:
916 age_mult_hours = 24;
917 /* Fallthrough intended */
918 case MATCHCRITERIA_AGE_GREATER_HOURS:
920 gboolean ret;
921 gint age;
923 t = time(NULL);
924 age = ((t - info->date_t) / (60 * 60 * age_mult_hours));
925 ret = (age >= prop->value);
927 /* debug output */
928 if (debug_filtering_session
929 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
930 if (ret) {
931 log_print(LOG_DEBUG_FILTERING,
932 "message age [ %d ] is greater than [ %d ]\n",
933 age, prop->value);
934 } else {
935 log_print(LOG_DEBUG_FILTERING,
936 "message age [ %d ] is not greater than [ %d ]\n",
937 age, prop->value);
940 return ret;
942 case MATCHCRITERIA_DATE_AFTER:
944 gboolean ret;
946 ret = prop->value < info->date_t;
948 /* debug output */
949 if (debug_filtering_session
950 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
951 if (ret) {
952 log_print(LOG_DEBUG_FILTERING,
953 "message date [ %" CM_TIME_FORMAT " ] is after [ %d ]\n",
954 info->date_t, prop->value);
955 } else {
956 log_print(LOG_DEBUG_FILTERING,
957 "message date [ %" CM_TIME_FORMAT " ] is not after [ %d ]\n",
958 info->date_t, prop->value);
961 return ret;
963 case MATCHCRITERIA_AGE_LOWER:
964 age_mult_hours = 24;
965 /* Fallthrough intended */
966 case MATCHCRITERIA_AGE_LOWER_HOURS:
968 gboolean ret;
969 gint age;
971 t = time(NULL);
972 age = ((t - info->date_t) / (60 * 60 * age_mult_hours));
973 ret = (age < prop->value);
975 /* debug output */
976 if (debug_filtering_session
977 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
978 if (ret) {
979 log_print(LOG_DEBUG_FILTERING,
980 "message age [ %d ] is lower than [ %d ]\n",
981 age, prop->value);
982 } else {
983 log_print(LOG_DEBUG_FILTERING,
984 "message age [ %d ] is not lower than [ %d ]\n",
985 age, prop->value);
988 return ret;
990 case MATCHCRITERIA_DATE_BEFORE:
992 gboolean ret;
994 ret = prop->value > info->date_t;
996 /* debug output */
997 if (debug_filtering_session
998 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
999 if (ret) {
1000 log_print(LOG_DEBUG_FILTERING,
1001 "message date [ %" CM_TIME_FORMAT " ] is before [ %d ]\n",
1002 info->date_t, prop->value);
1003 } else {
1004 log_print(LOG_DEBUG_FILTERING,
1005 "message date [ %" CM_TIME_FORMAT " ] is not before [ %d ]\n",
1006 info->date_t, prop->value);
1009 return ret;
1011 case MATCHCRITERIA_SCORE_GREATER:
1013 gboolean ret = (info->score > prop->value);
1015 /* debug output */
1016 if (debug_filtering_session
1017 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
1018 if (ret) {
1019 log_print(LOG_DEBUG_FILTERING,
1020 "message score [ %d ] is greater than [ %d ]\n",
1021 info->score, prop->value);
1022 } else {
1023 log_print(LOG_DEBUG_FILTERING,
1024 "message score [ %d ] is not greater than [ %d ]\n",
1025 info->score, prop->value);
1028 return ret;
1030 case MATCHCRITERIA_SCORE_LOWER:
1032 gboolean ret = (info->score < prop->value);
1034 /* debug output */
1035 if (debug_filtering_session
1036 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
1037 if (ret) {
1038 log_print(LOG_DEBUG_FILTERING,
1039 "message score [ %d ] is lower than [ %d ]\n",
1040 info->score, prop->value);
1041 } else {
1042 log_print(LOG_DEBUG_FILTERING,
1043 "message score [ %d ] is not lower than [ %d ]\n",
1044 info->score, prop->value);
1047 return ret;
1049 case MATCHCRITERIA_SCORE_EQUAL:
1051 gboolean ret = (info->score == prop->value);
1053 /* debug output */
1054 if (debug_filtering_session
1055 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
1056 if (ret) {
1057 log_print(LOG_DEBUG_FILTERING,
1058 "message score [ %d ] is equal to [ %d ]\n",
1059 info->score, prop->value);
1060 } else {
1061 log_print(LOG_DEBUG_FILTERING,
1062 "message score [ %d ] is not equal to [ %d ]\n",
1063 info->score, prop->value);
1066 return ret;
1068 case MATCHCRITERIA_SIZE_GREATER:
1070 /* FIXME: info->size is a goffset */
1071 gboolean ret = (info->size > (goffset) prop->value);
1073 /* debug output */
1074 if (debug_filtering_session
1075 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
1076 if (ret) {
1077 log_print(LOG_DEBUG_FILTERING,
1078 "message size is greater than [ %d ]\n",
1079 prop->value);
1080 } else {
1081 log_print(LOG_DEBUG_FILTERING,
1082 "message size is not greater than [ %d ]\n",
1083 prop->value);
1086 return ret;
1088 case MATCHCRITERIA_SIZE_SMALLER:
1090 /* FIXME: info->size is a goffset */
1091 gboolean ret = (info->size < (goffset) prop->value);
1093 /* debug output */
1094 if (debug_filtering_session
1095 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
1096 if (ret) {
1097 log_print(LOG_DEBUG_FILTERING,
1098 "message size is smaller than [ %d ]\n",
1099 prop->value);
1100 } else {
1101 log_print(LOG_DEBUG_FILTERING,
1102 "message size is not smaller than [ %d ]\n",
1103 prop->value);
1106 return ret;
1108 case MATCHCRITERIA_SIZE_EQUAL:
1110 /* FIXME: info->size is a goffset */
1111 gboolean ret = (info->size == (goffset) prop->value);
1113 /* debug output */
1114 if (debug_filtering_session
1115 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
1116 if (ret) {
1117 log_print(LOG_DEBUG_FILTERING,
1118 "message size is equal to [ %d ]\n",
1119 prop->value);
1120 } else {
1121 log_print(LOG_DEBUG_FILTERING,
1122 "message size is not equal to [ %d ]\n",
1123 prop->value);
1126 return ret;
1128 case MATCHCRITERIA_PARTIAL:
1130 /* FIXME: info->size is a goffset */
1131 gboolean ret = (info->total_size != 0 && info->size != (goffset)info->total_size);
1133 /* debug output */
1134 if (debug_filtering_session
1135 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
1136 if (ret) {
1137 log_print(LOG_DEBUG_FILTERING,
1138 "message is partially downloaded, size is less than total size [ %d ])\n",
1139 info->total_size);
1140 } else {
1141 log_print(LOG_DEBUG_FILTERING,
1142 "message is not partially downloaded\n");
1145 return ret;
1147 case MATCHCRITERIA_NOT_PARTIAL:
1149 /* FIXME: info->size is a goffset */
1150 gboolean ret = (info->total_size == 0 || info->size == (goffset)info->total_size);
1152 /* debug output */
1153 if (debug_filtering_session
1154 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
1155 if (ret) {
1156 log_print(LOG_DEBUG_FILTERING,
1157 "message is not partially downloaded\n");
1158 } else {
1159 log_print(LOG_DEBUG_FILTERING,
1160 "message is partially downloaded, size is less than total size [ %d ])\n",
1161 info->total_size);
1164 return ret;
1166 case MATCHCRITERIA_NEWSGROUPS:
1167 return matcherprop_string_match(prop, info->newsgroups, context_str[CONTEXT_NEWSGROUPS]);
1168 case MATCHCRITERIA_NOT_NEWSGROUPS:
1169 return !matcherprop_string_match(prop, info->newsgroups, context_str[CONTEXT_NEWSGROUPS]);
1170 case MATCHCRITERIA_MESSAGEID:
1171 return matcherprop_string_match(prop, info->msgid, context_str[CONTEXT_MESSAGEID]);
1172 case MATCHCRITERIA_NOT_MESSAGEID:
1173 return !matcherprop_string_match(prop, info->msgid, context_str[CONTEXT_MESSAGEID]);
1174 case MATCHCRITERIA_INREPLYTO:
1175 return matcherprop_string_match(prop, info->inreplyto, context_str[CONTEXT_IN_REPLY_TO]);
1176 case MATCHCRITERIA_NOT_INREPLYTO:
1177 return !matcherprop_string_match(prop, info->inreplyto, context_str[CONTEXT_IN_REPLY_TO]);
1178 case MATCHCRITERIA_REFERENCES:
1179 return matcherprop_list_match(prop, info->references, context_str[CONTEXT_REFERENCES]);
1180 case MATCHCRITERIA_NOT_REFERENCES:
1181 return !matcherprop_list_match(prop, info->references, context_str[CONTEXT_REFERENCES]);
1182 case MATCHCRITERIA_TEST:
1183 return matcherprop_match_test(prop, info);
1184 case MATCHCRITERIA_NOT_TEST:
1185 return !matcherprop_match_test(prop, info);
1186 default:
1187 return FALSE;
1191 /* ********************* MatcherList *************************** */
1194 *\brief Create a new list of matchers
1196 *\param matchers List of matcher structures
1197 *\param bool_and Operator
1199 *\return MatcherList * New list
1201 MatcherList *matcherlist_new(GSList *matchers, gboolean bool_and)
1203 MatcherList *cond;
1205 cond = g_new0(MatcherList, 1);
1207 cond->matchers = matchers;
1208 cond->bool_and = bool_and;
1210 return cond;
1213 #ifdef G_OS_UNIX
1215 *\brief Builds a single regular expresion from an array of srings.
1217 *\param strings The lines containing the different sub-regexp.
1219 *\return The newly allocated regexp string.
1221 static gchar *build_complete_regexp(gchar **strings)
1223 int i = 0;
1224 gchar *expr = NULL;
1225 while (strings && strings[i] && *strings[i]) {
1226 int old_len = expr ? strlen(expr):0;
1227 int new_len = 0;
1228 gchar *tmpstr = NULL;
1230 if (g_utf8_validate(strings[i], -1, NULL))
1231 tmpstr = g_strdup(strings[i]);
1232 else
1233 tmpstr = conv_codeset_strdup(strings[i],
1234 conv_get_locale_charset_str_no_utf8(),
1235 CS_INTERNAL);
1237 if (strstr(tmpstr, "\n"))
1238 *(strstr(tmpstr, "\n")) = '\0';
1240 new_len = strlen(tmpstr);
1242 expr = g_realloc(expr,
1243 expr ? (old_len + strlen("|()") + new_len + 1)
1244 : (strlen("()") + new_len + 1));
1246 if (old_len) {
1247 strcpy(expr + old_len, "|(");
1248 strcpy(expr + old_len + 2, tmpstr);
1249 strcpy(expr + old_len + 2 + new_len, ")");
1250 } else {
1251 strcpy(expr+old_len, "(");
1252 strcpy(expr+old_len + 1, tmpstr);
1253 strcpy(expr+old_len + 1 + new_len, ")");
1255 g_free(tmpstr);
1256 i++;
1258 return expr;
1260 #endif
1263 *\brief Create a new list of matchers from a multi-line string
1265 *\param lines String with "\n"-separated expressions
1266 *\param bool_and Operator
1267 *\param case_sensitive If the matching is case sensitive or not
1269 *\return MatcherList * New matcher list
1271 MatcherList *matcherlist_new_from_lines(gchar *lines, gboolean bool_and,
1272 gboolean case_sensitive)
1274 MatcherProp *m = NULL;
1275 GSList *matchers = NULL;
1276 gchar **strings = g_strsplit(lines, "\n", -1);
1278 #ifdef G_OS_UNIX
1279 gchar *expr = NULL;
1280 expr = build_complete_regexp(strings);
1281 debug_print("building matcherprop for expr '%s'\n", expr?expr:"NULL");
1283 m = matcherprop_new(MATCHCRITERIA_SUBJECT, NULL,
1284 case_sensitive? MATCHTYPE_REGEXP: MATCHTYPE_REGEXPCASE,
1285 expr, 0);
1286 if (m == NULL) {
1287 /* print error message */
1288 debug_print("failed to allocate memory for matcherprop\n");
1289 } else {
1290 matchers = g_slist_append(matchers, m);
1293 g_free(expr);
1294 #else
1295 int i = 0;
1296 while (strings && strings[i] && *strings[i]) {
1297 m = matcherprop_new(MATCHCRITERIA_SUBJECT, NULL,
1298 case_sensitive? MATCHTYPE_MATCH: MATCHTYPE_MATCHCASE,
1299 strings[i], 0);
1300 if (m == NULL) {
1301 /* print error message */
1302 debug_print("failed to allocate memory for matcherprop\n");
1303 } else {
1304 matchers = g_slist_append(matchers, m);
1306 i++;
1308 #endif
1309 g_strfreev(strings);
1311 return matcherlist_new(matchers, bool_and);
1315 *\brief Frees a list of matchers
1317 *\param cond List of matchers
1319 void matcherlist_free(MatcherList *cond)
1321 GSList *l;
1323 cm_return_if_fail(cond);
1324 for (l = cond->matchers ; l != NULL ; l = g_slist_next(l)) {
1325 matcherprop_free((MatcherProp *) l->data);
1327 g_slist_free(cond->matchers);
1328 g_free(cond);
1332 *\brief Check if a header matches a matcher condition
1334 *\param matcher Matcher structure to check header for
1335 *\param buf Header name
1337 *\return boolean TRUE if matching header
1339 static gboolean matcherprop_match_one_header(MatcherProp *matcher,
1340 gchar *buf)
1342 gboolean result = FALSE;
1343 Header *header = NULL;
1345 switch (matcher->criteria) {
1346 case MATCHCRITERIA_HEADER:
1347 case MATCHCRITERIA_NOT_HEADER:
1348 header = procheader_parse_header(buf);
1349 if (!header)
1350 return FALSE;
1351 if (procheader_headername_equal(header->name,
1352 matcher->header)) {
1353 if (matcher->criteria == MATCHCRITERIA_HEADER)
1354 result = matcherprop_string_match(matcher, header->body, context_str[CONTEXT_HEADER]);
1355 else
1356 result = !matcherprop_string_match(matcher, header->body, context_str[CONTEXT_HEADER]);
1357 procheader_header_free(header);
1358 return result;
1360 else {
1361 procheader_header_free(header);
1363 break;
1364 case MATCHCRITERIA_HEADERS_PART:
1365 case MATCHCRITERIA_HEADERS_CONT:
1366 case MATCHCRITERIA_MESSAGE:
1367 header = procheader_parse_header(buf);
1368 if (!header)
1369 return FALSE;
1370 result = matcherprop_header_line_match(matcher,
1371 header->name, header->body,
1372 (matcher->criteria == MATCHCRITERIA_HEADERS_PART),
1373 context_str[CONTEXT_HEADER_LINE]);
1374 procheader_header_free(header);
1375 return result;
1376 case MATCHCRITERIA_NOT_HEADERS_CONT:
1377 case MATCHCRITERIA_NOT_HEADERS_PART:
1378 case MATCHCRITERIA_NOT_MESSAGE:
1379 header = procheader_parse_header(buf);
1380 if (!header)
1381 return FALSE;
1382 result = !matcherprop_header_line_match(matcher,
1383 header->name, header->body,
1384 (matcher->criteria == MATCHCRITERIA_NOT_HEADERS_PART),
1385 context_str[CONTEXT_HEADER_LINE]);
1386 procheader_header_free(header);
1387 return result;
1388 case MATCHCRITERIA_FOUND_IN_ADDRESSBOOK:
1389 case MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK:
1391 GSList *address_list = NULL;
1392 gint match = MATCH_ONE;
1393 gboolean found = FALSE;
1395 /* how many address headers are we trying to match? */
1396 if (strcasecmp(matcher->header, "Any") == 0)
1397 match = MATCH_ANY;
1398 else if (strcasecmp(matcher->header, "All") == 0)
1399 match = MATCH_ALL;
1401 if (match == MATCH_ONE) {
1402 /* matching one address header exactly, is that the right one? */
1403 header = procheader_parse_header(buf);
1404 if (!header ||
1405 !procheader_headername_equal(header->name, matcher->header)) {
1406 procheader_header_free(header);
1407 return FALSE;
1409 address_list = address_list_append(address_list, header->body);
1410 if (address_list == NULL) {
1411 procheader_header_free(header);
1412 return FALSE;
1414 procheader_header_free(header);
1416 } else {
1417 header = procheader_parse_header(buf);
1418 if (!header)
1419 return FALSE;
1420 /* address header is one of the headers we have to match when checking
1421 for any address header or all address headers? */
1422 if (procheader_headername_equal(header->name, "From") ||
1423 procheader_headername_equal(header->name, "To") ||
1424 procheader_headername_equal(header->name, "Cc") ||
1425 procheader_headername_equal(header->name, "Reply-To") ||
1426 procheader_headername_equal(header->name, "Sender") ||
1427 procheader_headername_equal(header->name, "Resent-From") ||
1428 procheader_headername_equal(header->name, "Resent-To"))
1429 address_list = address_list_append(address_list, header->body);
1430 procheader_header_free(header);
1431 if (address_list == NULL)
1432 return FALSE;
1435 found = match_with_addresses_in_addressbook
1436 (matcher, address_list, matcher->criteria,
1437 matcher->expr, match);
1438 g_slist_free(address_list);
1440 if (matcher->criteria == MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK)
1441 return !found;
1442 else
1443 return found;
1447 return FALSE;
1451 *\brief Check if the matcher structure wants headers to
1452 * be matched
1454 *\param matcher Matcher structure
1456 *\return gboolean TRUE if the matcher structure describes
1457 * a header match condition
1459 static gboolean matcherprop_criteria_headers(const MatcherProp *matcher)
1461 switch (matcher->criteria) {
1462 case MATCHCRITERIA_HEADER:
1463 case MATCHCRITERIA_NOT_HEADER:
1464 case MATCHCRITERIA_HEADERS_PART:
1465 case MATCHCRITERIA_HEADERS_CONT:
1466 case MATCHCRITERIA_NOT_HEADERS_PART:
1467 case MATCHCRITERIA_NOT_HEADERS_CONT:
1468 case MATCHCRITERIA_FOUND_IN_ADDRESSBOOK:
1469 case MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK:
1470 return TRUE;
1471 default:
1472 return FALSE;
1477 *\brief Check if the matcher structure wants the message
1478 * to be matched (just perform an action on any
1479 * message)
1481 *\param matcher Matcher structure
1483 *\return gboolean TRUE if matcher condition should match
1484 * a message
1486 static gboolean matcherprop_criteria_message(MatcherProp *matcher)
1488 switch (matcher->criteria) {
1489 case MATCHCRITERIA_MESSAGE:
1490 case MATCHCRITERIA_NOT_MESSAGE:
1491 return TRUE;
1492 default:
1493 return FALSE;
1498 *\brief Check if a list of conditions matches one header in
1499 * a message file.
1501 *\param matchers List of conditions
1502 *\param fp Message file
1504 *\return gboolean TRUE if one of the headers is matched by
1505 * the list of conditions.
1507 static gboolean matcherlist_match_headers(MatcherList *matchers, FILE *fp)
1509 GSList *l;
1510 gchar *buf = NULL;
1511 gint ret;
1513 while ((ret = procheader_get_one_field(&buf, fp, NULL)) != -1) {
1514 for (l = matchers->matchers ; l != NULL ; l = g_slist_next(l)) {
1515 MatcherProp *matcher = (MatcherProp *) l->data;
1516 gint match = MATCH_ANY;
1518 if (matcher->done)
1519 continue;
1521 /* determine the match range (all, any are our concern here) */
1522 if (matcher->criteria == MATCHCRITERIA_NOT_HEADERS_PART ||
1523 matcher->criteria == MATCHCRITERIA_NOT_HEADERS_CONT ||
1524 matcher->criteria == MATCHCRITERIA_NOT_MESSAGE) {
1525 match = MATCH_ALL;
1527 } else if (matcher->criteria == MATCHCRITERIA_FOUND_IN_ADDRESSBOOK ||
1528 matcher->criteria == MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK) {
1529 Header *header = NULL;
1531 /* address header is one of the headers we have to match when checking
1532 for any address header or all address headers? */
1533 header = procheader_parse_header(buf);
1534 if (header &&
1535 (procheader_headername_equal(header->name, "From") ||
1536 procheader_headername_equal(header->name, "To") ||
1537 procheader_headername_equal(header->name, "Cc") ||
1538 procheader_headername_equal(header->name, "Reply-To") ||
1539 procheader_headername_equal(header->name, "Sender") ||
1540 procheader_headername_equal(header->name, "Resent-From") ||
1541 procheader_headername_equal(header->name, "Resent-To"))) {
1543 if (strcasecmp(matcher->header, "Any") == 0)
1544 match = MATCH_ANY;
1545 else if (strcasecmp(matcher->header, "All") == 0)
1546 match = MATCH_ALL;
1547 else
1548 match = MATCH_ONE;
1549 } else {
1550 if (!header)
1551 continue;
1552 /* matching one address header exactly, is that the right one?
1553 further call to matcherprop_match_one_header() will tell us */
1555 procheader_header_free(header);
1558 /* ZERO line must NOT match for the rule to match.
1560 if (match == MATCH_ALL) {
1561 if (matcherprop_match_one_header(matcher, buf)) {
1562 matcher->result = TRUE;
1563 } else {
1564 matcher->result = FALSE;
1565 matcher->done = TRUE;
1567 /* else, just one line matching is enough for the rule to match
1569 } else if (matcherprop_criteria_headers(matcher) ||
1570 matcherprop_criteria_message(matcher)) {
1571 if (matcherprop_match_one_header(matcher, buf)) {
1572 matcher->result = TRUE;
1573 matcher->done = TRUE;
1577 /* if the rule matched and the matchers are OR, no need to
1578 * check the others */
1579 if (matcher->result && matcher->done) {
1580 if (!matchers->bool_and) {
1581 g_free(buf);
1582 return TRUE;
1586 g_free(buf);
1587 buf = NULL;
1590 return FALSE;
1594 *\brief Check if a matcher wants to check the message body
1596 *\param matcher Matcher structure
1598 *\return gboolean TRUE if body must be matched.
1600 static gboolean matcherprop_criteria_body(const MatcherProp *matcher)
1602 switch (matcher->criteria) {
1603 case MATCHCRITERIA_BODY_PART:
1604 case MATCHCRITERIA_NOT_BODY_PART:
1605 return TRUE;
1606 default:
1607 return FALSE;
1611 static gboolean matcherlist_match_binary_content(MatcherList *matchers, MimeInfo *partinfo)
1613 FILE *outfp;
1614 gchar buf[BUFFSIZE];
1615 GSList *l;
1617 if (!partinfo || partinfo->type == MIMETYPE_TEXT)
1618 return FALSE;
1619 else
1620 outfp = procmime_get_binary_content(partinfo);
1622 if (!outfp)
1623 return FALSE;
1625 while (claws_fgets(buf, sizeof(buf), outfp) != NULL) {
1626 strretchomp(buf);
1628 for (l = matchers->matchers ; l != NULL ; l = g_slist_next(l)) {
1629 MatcherProp *matcher = (MatcherProp *) l->data;
1631 if (matcher->done)
1632 continue;
1634 /* Don't scan non-text parts when looking in body, only
1635 * when looking in whole message
1637 if (matcher->criteria == MATCHCRITERIA_NOT_BODY_PART ||
1638 matcher->criteria == MATCHCRITERIA_BODY_PART)
1639 continue;
1641 /* if the criteria is ~body_part or ~message, ZERO lines
1642 * must match for the rule to match.
1644 if (matcher->criteria == MATCHCRITERIA_NOT_BODY_PART ||
1645 matcher->criteria == MATCHCRITERIA_NOT_MESSAGE) {
1646 if (matcherprop_string_match(matcher, buf,
1647 context_str[CONTEXT_BODY_LINE])) {
1648 matcher->result = FALSE;
1649 matcher->done = TRUE;
1650 } else
1651 matcher->result = TRUE;
1652 /* else, just one line has to match */
1653 } else if (matcherprop_criteria_body(matcher) ||
1654 matcherprop_criteria_message(matcher)) {
1655 if (matcherprop_string_match(matcher, buf,
1656 context_str[CONTEXT_BODY_LINE])) {
1657 matcher->result = TRUE;
1658 matcher->done = TRUE;
1662 /* if the matchers are OR'ed and the rule matched,
1663 * no need to check the others. */
1664 if (matcher->result && matcher->done) {
1665 if (!matchers->bool_and) {
1666 claws_fclose(outfp);
1667 return TRUE;
1673 claws_fclose(outfp);
1674 return FALSE;
1677 static gboolean match_content_cb(const gchar *buf, gpointer data)
1679 MatcherList *matchers = (MatcherList *)data;
1680 gboolean all_done = TRUE;
1681 GSList *l;
1683 for (l = matchers->matchers ; l != NULL ; l = g_slist_next(l)) {
1684 MatcherProp *matcher = (MatcherProp *) l->data;
1686 if (matcher->done)
1687 continue;
1689 /* if the criteria is ~body_part or ~message, ZERO lines
1690 * must match for the rule to match.
1692 if (matcher->criteria == MATCHCRITERIA_NOT_BODY_PART ||
1693 matcher->criteria == MATCHCRITERIA_NOT_MESSAGE) {
1694 if (matcherprop_string_match(matcher, buf,
1695 context_str[CONTEXT_BODY_LINE])) {
1696 matcher->result = FALSE;
1697 matcher->done = TRUE;
1698 } else
1699 matcher->result = TRUE;
1700 /* else, just one line has to match */
1701 } else if (matcherprop_criteria_body(matcher) ||
1702 matcherprop_criteria_message(matcher)) {
1703 if (matcherprop_string_match(matcher, buf,
1704 context_str[CONTEXT_BODY_LINE])) {
1705 matcher->result = TRUE;
1706 matcher->done = TRUE;
1710 /* if the matchers are OR'ed and the rule matched,
1711 * no need to check the others. */
1712 if (matcher->result && matcher->done) {
1713 if (!matchers->bool_and) {
1714 return TRUE;
1718 if (!matcher->done)
1719 all_done = FALSE;
1721 return all_done;
1724 static gboolean matcherlist_match_text_content(MatcherList *matchers, MimeInfo *partinfo)
1726 if (partinfo->type != MIMETYPE_TEXT)
1727 return FALSE;
1729 return procmime_scan_text_content(partinfo, match_content_cb, matchers);
1733 *\brief Check if a line in a message file's body matches
1734 * the criteria
1736 *\param matchers List of conditions
1737 *\param fp Message file
1739 *\return gboolean TRUE if successful match
1741 static gboolean matcherlist_match_body(MatcherList *matchers, gboolean body_only, MsgInfo *info)
1743 MimeInfo *mimeinfo = NULL;
1744 MimeInfo *partinfo = NULL;
1745 gboolean first_text_found = FALSE;
1747 cm_return_val_if_fail(info != NULL, FALSE);
1749 mimeinfo = procmime_scan_message(info);
1751 /* Skip headers */
1752 partinfo = procmime_mimeinfo_next(mimeinfo);
1754 for (; partinfo != NULL; partinfo = procmime_mimeinfo_next(partinfo)) {
1756 if (partinfo->type != MIMETYPE_TEXT && body_only)
1757 continue;
1759 if (partinfo->type == MIMETYPE_TEXT) {
1760 first_text_found = TRUE;
1761 if (matcherlist_match_text_content(matchers, partinfo)) {
1762 procmime_mimeinfo_free_all(&mimeinfo);
1763 return TRUE;
1765 } else if (matcherlist_match_binary_content(matchers, partinfo)) {
1766 procmime_mimeinfo_free_all(&mimeinfo);
1767 return TRUE;
1770 if (body_only && first_text_found)
1771 break;
1773 procmime_mimeinfo_free_all(&mimeinfo);
1775 return FALSE;
1779 *\brief Check if a message file matches criteria
1781 *\param matchers Criteria
1782 *\param info Message info
1783 *\param result Default result
1785 *\return gboolean TRUE if matched
1787 static gboolean matcherlist_match_file(MatcherList *matchers, MsgInfo *info,
1788 gboolean result)
1790 gboolean read_headers;
1791 gboolean read_body;
1792 gboolean body_only;
1793 GSList *l;
1794 FILE *fp;
1795 gchar *file;
1797 /* file need to be read ? */
1799 read_headers = FALSE;
1800 read_body = FALSE;
1801 body_only = TRUE;
1802 for (l = matchers->matchers ; l != NULL ; l = g_slist_next(l)) {
1803 MatcherProp *matcher = (MatcherProp *) l->data;
1805 if (matcherprop_criteria_headers(matcher))
1806 read_headers = TRUE;
1807 if (matcherprop_criteria_body(matcher))
1808 read_body = TRUE;
1809 if (matcherprop_criteria_message(matcher)) {
1810 read_headers = TRUE;
1811 read_body = TRUE;
1812 body_only = FALSE;
1814 matcher->result = FALSE;
1815 matcher->done = FALSE;
1818 if (!read_headers && !read_body)
1819 return result;
1821 file = procmsg_get_message_file_full(info, read_headers, read_body);
1822 if (file == NULL)
1823 return FALSE;
1825 if ((fp = claws_fopen(file, "rb")) == NULL) {
1826 FILE_OP_ERROR(file, "claws_fopen");
1827 g_free(file);
1828 return result;
1831 /* read the headers */
1833 if (read_headers) {
1834 if (matcherlist_match_headers(matchers, fp))
1835 read_body = FALSE;
1836 } else {
1837 procheader_skip_headers(fp);
1840 /* read the body */
1841 if (read_body) {
1842 matcherlist_match_body(matchers, body_only, info);
1845 for (l = matchers->matchers; l != NULL; l = g_slist_next(l)) {
1846 MatcherProp *matcher = (MatcherProp *) l->data;
1848 if (matcherprop_criteria_headers(matcher) ||
1849 matcherprop_criteria_body(matcher) ||
1850 matcherprop_criteria_message(matcher)) {
1851 if (matcher->result) {
1852 if (!matchers->bool_and) {
1853 result = TRUE;
1854 break;
1857 else {
1858 if (matchers->bool_and) {
1859 result = FALSE;
1860 break;
1866 g_free(file);
1868 claws_fclose(fp);
1870 return result;
1874 *\brief Test list of conditions on a message.
1876 *\param matchers List of conditions
1877 *\param info Message info
1879 *\return gboolean TRUE if matched
1881 gboolean matcherlist_match(MatcherList *matchers, MsgInfo *info)
1883 GSList *l;
1884 gboolean result;
1886 if (!matchers)
1887 return FALSE;
1889 if (matchers->bool_and)
1890 result = TRUE;
1891 else
1892 result = FALSE;
1894 /* test the cached elements */
1896 for (l = matchers->matchers; l != NULL ;l = g_slist_next(l)) {
1897 MatcherProp *matcher = (MatcherProp *) l->data;
1899 if (debug_filtering_session) {
1900 gchar *buf = matcherprop_to_string(matcher);
1901 log_print(LOG_DEBUG_FILTERING, _("checking if message matches [ %s ]\n"), buf);
1902 g_free(buf);
1905 switch(matcher->criteria) {
1906 case MATCHCRITERIA_ALL:
1907 case MATCHCRITERIA_UNREAD:
1908 case MATCHCRITERIA_NOT_UNREAD:
1909 case MATCHCRITERIA_NEW:
1910 case MATCHCRITERIA_NOT_NEW:
1911 case MATCHCRITERIA_MARKED:
1912 case MATCHCRITERIA_NOT_MARKED:
1913 case MATCHCRITERIA_DELETED:
1914 case MATCHCRITERIA_NOT_DELETED:
1915 case MATCHCRITERIA_REPLIED:
1916 case MATCHCRITERIA_NOT_REPLIED:
1917 case MATCHCRITERIA_FORWARDED:
1918 case MATCHCRITERIA_NOT_FORWARDED:
1919 case MATCHCRITERIA_LOCKED:
1920 case MATCHCRITERIA_NOT_LOCKED:
1921 case MATCHCRITERIA_SPAM:
1922 case MATCHCRITERIA_NOT_SPAM:
1923 case MATCHCRITERIA_HAS_ATTACHMENT:
1924 case MATCHCRITERIA_HAS_NO_ATTACHMENT:
1925 case MATCHCRITERIA_SIGNED:
1926 case MATCHCRITERIA_NOT_SIGNED:
1927 case MATCHCRITERIA_COLORLABEL:
1928 case MATCHCRITERIA_NOT_COLORLABEL:
1929 case MATCHCRITERIA_IGNORE_THREAD:
1930 case MATCHCRITERIA_NOT_IGNORE_THREAD:
1931 case MATCHCRITERIA_WATCH_THREAD:
1932 case MATCHCRITERIA_NOT_WATCH_THREAD:
1933 case MATCHCRITERIA_SUBJECT:
1934 case MATCHCRITERIA_NOT_SUBJECT:
1935 case MATCHCRITERIA_FROM:
1936 case MATCHCRITERIA_NOT_FROM:
1937 case MATCHCRITERIA_TO:
1938 case MATCHCRITERIA_NOT_TO:
1939 case MATCHCRITERIA_CC:
1940 case MATCHCRITERIA_NOT_CC:
1941 case MATCHCRITERIA_TO_OR_CC:
1942 case MATCHCRITERIA_NOT_TO_AND_NOT_CC:
1943 case MATCHCRITERIA_TAG:
1944 case MATCHCRITERIA_NOT_TAG:
1945 case MATCHCRITERIA_TAGGED:
1946 case MATCHCRITERIA_NOT_TAGGED:
1947 case MATCHCRITERIA_AGE_GREATER:
1948 case MATCHCRITERIA_AGE_LOWER:
1949 case MATCHCRITERIA_AGE_GREATER_HOURS:
1950 case MATCHCRITERIA_AGE_LOWER_HOURS:
1951 case MATCHCRITERIA_DATE_AFTER:
1952 case MATCHCRITERIA_DATE_BEFORE:
1953 case MATCHCRITERIA_NEWSGROUPS:
1954 case MATCHCRITERIA_NOT_NEWSGROUPS:
1955 case MATCHCRITERIA_MESSAGEID:
1956 case MATCHCRITERIA_NOT_MESSAGEID:
1957 case MATCHCRITERIA_INREPLYTO:
1958 case MATCHCRITERIA_NOT_INREPLYTO:
1959 case MATCHCRITERIA_REFERENCES:
1960 case MATCHCRITERIA_NOT_REFERENCES:
1961 case MATCHCRITERIA_SCORE_GREATER:
1962 case MATCHCRITERIA_SCORE_LOWER:
1963 case MATCHCRITERIA_SCORE_EQUAL:
1964 case MATCHCRITERIA_SIZE_GREATER:
1965 case MATCHCRITERIA_SIZE_SMALLER:
1966 case MATCHCRITERIA_SIZE_EQUAL:
1967 case MATCHCRITERIA_TEST:
1968 case MATCHCRITERIA_NOT_TEST:
1969 case MATCHCRITERIA_PARTIAL:
1970 case MATCHCRITERIA_NOT_PARTIAL:
1971 if (matcherprop_match(matcher, info)) {
1972 if (!matchers->bool_and) {
1973 if (debug_filtering_session)
1974 log_status_ok(LOG_DEBUG_FILTERING, _("message matches\n"));
1975 return TRUE;
1978 else {
1979 if (matchers->bool_and) {
1980 if (debug_filtering_session)
1981 log_status_nok(LOG_DEBUG_FILTERING, _("message does not match\n"));
1982 return FALSE;
1988 /* test the condition on the file */
1990 if (matcherlist_match_file(matchers, info, result)) {
1991 if (!matchers->bool_and) {
1992 if (debug_filtering_session)
1993 log_status_ok(LOG_DEBUG_FILTERING, _("message matches\n"));
1994 return TRUE;
1996 } else {
1997 if (matchers->bool_and) {
1998 if (debug_filtering_session)
1999 log_status_nok(LOG_DEBUG_FILTERING, _("message does not match\n"));
2000 return FALSE;
2004 if (debug_filtering_session) {
2005 if (result)
2006 log_status_ok(LOG_DEBUG_FILTERING, _("message matches\n"));
2007 else
2008 log_status_nok(LOG_DEBUG_FILTERING, _("message does not match\n"));
2010 return result;
2014 static gint quote_filter_str(gchar * result, guint size,
2015 const gchar * path)
2017 const gchar * p;
2018 gchar * result_p;
2019 guint remaining;
2021 result_p = result;
2022 remaining = size;
2024 for(p = path ; * p != '\0' ; p ++) {
2026 if ((* p != '\"') && (* p != '\\')) {
2027 if (remaining > 0) {
2028 * result_p = * p;
2029 result_p ++;
2030 remaining --;
2032 else {
2033 result[size - 1] = '\0';
2034 return -1;
2037 else {
2038 if (remaining >= 2) {
2039 * result_p = '\\';
2040 result_p ++;
2041 * result_p = * p;
2042 result_p ++;
2043 remaining -= 2;
2045 else {
2046 result[size - 1] = '\0';
2047 return -1;
2051 if (remaining > 0) {
2052 * result_p = '\0';
2054 else {
2055 result[size - 1] = '\0';
2056 return -1;
2059 return 0;
2063 gchar * matcher_quote_str(const gchar * src)
2065 gchar * res;
2066 gint len;
2068 len = strlen(src) * 2 + 1;
2069 res = g_malloc(len);
2070 quote_filter_str(res, len, src);
2072 return res;
2076 *\brief Convert a matcher structure to a string
2078 *\param matcher Matcher structure
2080 *\return gchar * Newly allocated string
2082 gchar *matcherprop_to_string(MatcherProp *matcher)
2084 gchar *matcher_str = NULL;
2085 const gchar *criteria_str;
2086 const gchar *matchtype_str;
2087 int i;
2088 gchar * quoted_expr;
2089 gchar * quoted_header;
2091 criteria_str = NULL;
2092 for (i = 0; i < (int) (sizeof(matchparser_tab) / sizeof(MatchParser)); i++) {
2093 if (matchparser_tab[i].id == matcher->criteria)
2094 criteria_str = matchparser_tab[i].str;
2096 if (criteria_str == NULL)
2097 return NULL;
2099 switch (matcher->criteria) {
2100 case MATCHCRITERIA_AGE_GREATER:
2101 case MATCHCRITERIA_AGE_LOWER:
2102 case MATCHCRITERIA_AGE_GREATER_HOURS:
2103 case MATCHCRITERIA_AGE_LOWER_HOURS:
2104 case MATCHCRITERIA_SCORE_GREATER:
2105 case MATCHCRITERIA_SCORE_LOWER:
2106 case MATCHCRITERIA_SCORE_EQUAL:
2107 case MATCHCRITERIA_SIZE_GREATER:
2108 case MATCHCRITERIA_SIZE_SMALLER:
2109 case MATCHCRITERIA_SIZE_EQUAL:
2110 case MATCHCRITERIA_COLORLABEL:
2111 case MATCHCRITERIA_NOT_COLORLABEL:
2112 return g_strdup_printf("%s %i", criteria_str, matcher->value);
2113 case MATCHCRITERIA_ALL:
2114 case MATCHCRITERIA_UNREAD:
2115 case MATCHCRITERIA_NOT_UNREAD:
2116 case MATCHCRITERIA_NEW:
2117 case MATCHCRITERIA_NOT_NEW:
2118 case MATCHCRITERIA_MARKED:
2119 case MATCHCRITERIA_NOT_MARKED:
2120 case MATCHCRITERIA_DELETED:
2121 case MATCHCRITERIA_NOT_DELETED:
2122 case MATCHCRITERIA_REPLIED:
2123 case MATCHCRITERIA_NOT_REPLIED:
2124 case MATCHCRITERIA_FORWARDED:
2125 case MATCHCRITERIA_NOT_FORWARDED:
2126 case MATCHCRITERIA_LOCKED:
2127 case MATCHCRITERIA_NOT_LOCKED:
2128 case MATCHCRITERIA_SPAM:
2129 case MATCHCRITERIA_NOT_SPAM:
2130 case MATCHCRITERIA_HAS_ATTACHMENT:
2131 case MATCHCRITERIA_HAS_NO_ATTACHMENT:
2132 case MATCHCRITERIA_SIGNED:
2133 case MATCHCRITERIA_NOT_SIGNED:
2134 case MATCHCRITERIA_PARTIAL:
2135 case MATCHCRITERIA_NOT_PARTIAL:
2136 case MATCHCRITERIA_IGNORE_THREAD:
2137 case MATCHCRITERIA_NOT_IGNORE_THREAD:
2138 case MATCHCRITERIA_WATCH_THREAD:
2139 case MATCHCRITERIA_NOT_WATCH_THREAD:
2140 case MATCHCRITERIA_TAGGED:
2141 case MATCHCRITERIA_NOT_TAGGED:
2142 return g_strdup(criteria_str);
2143 case MATCHCRITERIA_TEST:
2144 case MATCHCRITERIA_NOT_TEST:
2145 case MATCHCRITERIA_DATE_AFTER:
2146 case MATCHCRITERIA_DATE_BEFORE:
2147 quoted_expr = matcher_quote_str(matcher->expr);
2148 matcher_str = g_strdup_printf("%s \"%s\"",
2149 criteria_str, quoted_expr);
2150 g_free(quoted_expr);
2151 return matcher_str;
2152 case MATCHCRITERIA_FOUND_IN_ADDRESSBOOK:
2153 case MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK:
2154 quoted_header = matcher_quote_str(matcher->header);
2155 quoted_expr = matcher_quote_str(matcher->expr);
2156 matcher_str = g_strdup_printf("%s \"%s\" in \"%s\"",
2157 criteria_str, quoted_header, quoted_expr);
2158 g_free(quoted_header);
2159 g_free(quoted_expr);
2160 return matcher_str;
2163 matchtype_str = NULL;
2164 for (i = 0; i < sizeof matchparser_tab / sizeof matchparser_tab[0]; i++) {
2165 if (matchparser_tab[i].id == matcher->matchtype)
2166 matchtype_str = matchparser_tab[i].str;
2169 if (matchtype_str == NULL)
2170 return NULL;
2172 switch (matcher->matchtype) {
2173 case MATCHTYPE_MATCH:
2174 case MATCHTYPE_MATCHCASE:
2175 case MATCHTYPE_REGEXP:
2176 case MATCHTYPE_REGEXPCASE:
2177 quoted_expr = matcher_quote_str(matcher->expr);
2178 if (matcher->header) {
2179 quoted_header = matcher_quote_str(matcher->header);
2180 matcher_str = g_strdup_printf
2181 ("%s \"%s\" %s \"%s\"",
2182 criteria_str, quoted_header,
2183 matchtype_str, quoted_expr);
2184 g_free(quoted_header);
2186 else
2187 matcher_str = g_strdup_printf
2188 ("%s %s \"%s\"", criteria_str,
2189 matchtype_str, quoted_expr);
2190 g_free(quoted_expr);
2191 break;
2194 return matcher_str;
2198 *\brief Convert a list of conditions to a string
2200 *\param matchers List of conditions
2202 *\return gchar * Newly allocated string
2204 gchar *matcherlist_to_string(const MatcherList *matchers)
2206 gint count;
2207 gchar **vstr;
2208 GSList *l;
2209 gchar **cur_str;
2210 gchar *result = NULL;
2212 count = g_slist_length(matchers->matchers);
2213 vstr = g_new(gchar *, count + 1);
2215 for (l = matchers->matchers, cur_str = vstr; l != NULL;
2216 l = g_slist_next(l), cur_str ++) {
2217 *cur_str = matcherprop_to_string((MatcherProp *) l->data);
2218 if (*cur_str == NULL)
2219 break;
2221 *cur_str = NULL;
2223 if (matchers->bool_and)
2224 result = g_strjoinv(" & ", vstr);
2225 else
2226 result = g_strjoinv(" | ", vstr);
2228 for (cur_str = vstr ; *cur_str != NULL ; cur_str ++)
2229 g_free(*cur_str);
2230 g_free(vstr);
2232 return result;
2236 #define STRLEN_ZERO(s) ((s) ? strlen(s) : 0)
2237 #define STRLEN_DEFAULT(s,d) ((s) ? strlen(s) : STRLEN_ZERO(d))
2239 static void add_str_default(gchar ** dest,
2240 const gchar * s, const gchar * d)
2242 gchar quoted_str[4096];
2243 const gchar * str;
2245 if (s != NULL)
2246 str = s;
2247 else
2248 str = d;
2250 quote_cmd_argument(quoted_str, sizeof(quoted_str), str);
2251 strcpy(* dest, quoted_str);
2253 (* dest) += strlen(* dest);
2256 /* matching_build_command() - preferably cmd should be unescaped */
2258 *\brief Build the command-line to execute
2260 *\param cmd String with command-line specifiers
2261 *\param info Message info to use for command
2263 *\return gchar * Newly allocated string
2265 gchar *matching_build_command(const gchar *cmd, MsgInfo *info)
2267 const gchar *s = cmd;
2268 gchar *filename = NULL;
2269 gchar *processed_cmd;
2270 gchar *p;
2271 gint size;
2273 const gchar *const no_subject = _("(none)") ;
2274 const gchar *const no_from = _("(none)") ;
2275 const gchar *const no_to = _("(none)") ;
2276 const gchar *const no_cc = _("(none)") ;
2277 const gchar *const no_date = _("(none)") ;
2278 const gchar *const no_msgid = _("(none)") ;
2279 const gchar *const no_newsgroups = _("(none)") ;
2280 const gchar *const no_references = _("(none)") ;
2282 size = STRLEN_ZERO(cmd) + 1;
2283 while (*s != '\0') {
2284 if (*s == '%') {
2285 s++;
2286 switch (*s) {
2287 case '%':
2288 size -= 1;
2289 break;
2290 case 's': /* subject */
2291 size += STRLEN_DEFAULT(info->subject, no_subject) - 2;
2292 break;
2293 case 'f': /* from */
2294 size += STRLEN_DEFAULT(info->from, no_from) - 2;
2295 break;
2296 case 't': /* to */
2297 size += STRLEN_DEFAULT(info->to, no_to) - 2;
2298 break;
2299 case 'c': /* cc */
2300 size += STRLEN_DEFAULT(info->cc, no_cc) - 2;
2301 break;
2302 case 'd': /* date */
2303 size += STRLEN_DEFAULT(info->date, no_date) - 2;
2304 break;
2305 case 'i': /* message-id */
2306 size += STRLEN_DEFAULT(info->msgid, no_msgid) - 2;
2307 break;
2308 case 'n': /* newsgroups */
2309 size += STRLEN_DEFAULT(info->newsgroups, no_newsgroups) - 2;
2310 break;
2311 case 'r': /* references */
2312 /* FIXME: using the inreplyto header for reference */
2313 size += STRLEN_DEFAULT(info->inreplyto, no_references) - 2;
2314 break;
2315 case 'F': /* file */
2316 if (filename == NULL)
2317 filename = folder_item_fetch_msg(info->folder, info->msgnum);
2319 if (filename == NULL) {
2320 g_warning("filename is not set");
2321 return NULL;
2323 else {
2324 size += strlen(filename) - 2;
2326 break;
2328 s++;
2330 else s++;
2333 /* as the string can be quoted, we double the result */
2334 size *= 2;
2336 processed_cmd = g_new0(gchar, size);
2337 s = cmd;
2338 p = processed_cmd;
2340 while (*s != '\0') {
2341 if (*s == '%') {
2342 s++;
2343 switch (*s) {
2344 case '%':
2345 *p = '%';
2346 p++;
2347 break;
2348 case 's': /* subject */
2349 add_str_default(&p, info->subject,
2350 no_subject);
2351 break;
2352 case 'f': /* from */
2353 add_str_default(&p, info->from,
2354 no_from);
2355 break;
2356 case 't': /* to */
2357 add_str_default(&p, info->to,
2358 no_to);
2359 break;
2360 case 'c': /* cc */
2361 add_str_default(&p, info->cc,
2362 no_cc);
2363 break;
2364 case 'd': /* date */
2365 add_str_default(&p, info->date,
2366 no_date);
2367 break;
2368 case 'i': /* message-id */
2369 add_str_default(&p, info->msgid,
2370 no_msgid);
2371 break;
2372 case 'n': /* newsgroups */
2373 add_str_default(&p, info->newsgroups,
2374 no_newsgroups);
2375 break;
2376 case 'r': /* references */
2377 /* FIXME: using the inreplyto header for references */
2378 add_str_default(&p, info->inreplyto, no_references);
2379 break;
2380 case 'F': /* file */
2381 if (filename != NULL)
2382 add_str_default(&p, filename, NULL);
2383 break;
2384 default:
2385 *p = '%';
2386 p++;
2387 *p = *s;
2388 p++;
2389 break;
2391 s++;
2393 else {
2394 *p = *s;
2395 p++;
2396 s++;
2399 g_free(filename);
2401 return processed_cmd;
2403 #undef STRLEN_DEFAULT
2404 #undef STRLEN_ZERO
2406 /* ************************************************************ */
2410 *\brief Write filtering list to file
2412 *\param fp File
2413 *\param prefs_filtering List of filtering conditions
2415 static int prefs_filtering_write(FILE *fp, GSList *prefs_filtering)
2417 GSList *cur = NULL;
2419 for (cur = prefs_filtering; cur != NULL; cur = cur->next) {
2420 gchar *filtering_str = NULL;
2421 gchar *tmp_name = NULL;
2422 FilteringProp *prop = NULL;
2424 if (NULL == (prop = (FilteringProp *) cur->data))
2425 continue;
2427 if (NULL == (filtering_str = filteringprop_to_string(prop)))
2428 continue;
2430 if (prop->enabled) {
2431 if (claws_fputs("enabled ", fp) == EOF) {
2432 FILE_OP_ERROR("filtering config", "claws_fputs");
2433 g_free(filtering_str);
2434 return -1;
2436 } else {
2437 if (claws_fputs("disabled ", fp) == EOF) {
2438 FILE_OP_ERROR("filtering config", "claws_fputs");
2439 g_free(filtering_str);
2440 return -1;
2444 if (claws_fputs("rulename \"", fp) == EOF) {
2445 FILE_OP_ERROR("filtering config", "claws_fputs");
2446 g_free(filtering_str);
2447 return -1;
2449 tmp_name = prop->name;
2450 while (tmp_name && *tmp_name != '\0') {
2451 if (*tmp_name != '"') {
2452 if (claws_fputc(*tmp_name, fp) == EOF) {
2453 FILE_OP_ERROR("filtering config", "claws_fputs || claws_fputc");
2454 g_free(filtering_str);
2455 return -1;
2457 } else if (*tmp_name == '"') {
2458 if (claws_fputc('\\', fp) == EOF ||
2459 claws_fputc('"', fp) == EOF) {
2460 FILE_OP_ERROR("filtering config", "claws_fputs || claws_fputc");
2461 g_free(filtering_str);
2462 return -1;
2465 tmp_name ++;
2467 if (claws_fputs("\" ", fp) == EOF) {
2468 FILE_OP_ERROR("filtering config", "claws_fputs");
2469 g_free(filtering_str);
2470 return -1;
2473 if (prop->account_id != 0) {
2474 gchar *tmp = NULL;
2476 tmp = g_strdup_printf("account %d ", prop->account_id);
2477 if (claws_fputs(tmp, fp) == EOF) {
2478 FILE_OP_ERROR("filtering config", "claws_fputs");
2479 g_free(filtering_str);
2480 g_free(tmp);
2481 return -1;
2483 g_free(tmp);
2486 if(claws_fputs(filtering_str, fp) == EOF ||
2487 claws_fputc('\n', fp) == EOF) {
2488 FILE_OP_ERROR("filtering config", "claws_fputs || claws_fputc");
2489 g_free(filtering_str);
2490 return -1;
2492 g_free(filtering_str);
2495 return 0;
2498 typedef struct _NodeLoopData {
2499 FILE *fp;
2500 gboolean error;
2501 } NodeLoopData;
2504 *\brief Write matchers from a folder item
2506 *\param node Node with folder info
2507 *\param data File pointer
2509 *\return gboolean FALSE
2511 static gboolean prefs_matcher_write_func(GNode *node, gpointer d)
2513 FolderItem *item;
2514 NodeLoopData *data = (NodeLoopData *)d;
2515 gchar *id;
2516 GSList *prefs_filtering;
2518 item = node->data;
2519 /* prevent warning */
2520 if (item->path == NULL)
2521 return FALSE;
2522 id = folder_item_get_identifier(item);
2523 if (id == NULL)
2524 return FALSE;
2525 prefs_filtering = item->prefs->processing;
2527 if (prefs_filtering != NULL) {
2528 if (fprintf(data->fp, "[%s]\n", id) < 0) {
2529 data->error = TRUE;
2530 goto fail;
2532 if (prefs_filtering_write(data->fp, prefs_filtering) < 0) {
2533 data->error = TRUE;
2534 goto fail;
2536 if (claws_fputc('\n', data->fp) == EOF) {
2537 data->error = TRUE;
2538 goto fail;
2541 fail:
2542 g_free(id);
2544 return FALSE;
2548 *\brief Save matchers from folder items
2550 *\param fp File
2552 static int prefs_matcher_save(FILE *fp)
2554 GList *cur;
2555 NodeLoopData data;
2557 data.fp = fp;
2558 data.error = FALSE;
2560 for (cur = folder_get_list() ; cur != NULL ; cur = g_list_next(cur)) {
2561 Folder *folder;
2563 folder = (Folder *) cur->data;
2564 g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
2565 prefs_matcher_write_func, &data);
2568 if (data.error == TRUE)
2569 return -1;
2571 /* pre global rules */
2572 if (fprintf(fp, "[preglobal]\n") < 0 ||
2573 prefs_filtering_write(fp, pre_global_processing) < 0 ||
2574 claws_fputc('\n', fp) == EOF)
2575 return -1;
2577 /* post global rules */
2578 if (fprintf(fp, "[postglobal]\n") < 0 ||
2579 prefs_filtering_write(fp, post_global_processing) < 0 ||
2580 claws_fputc('\n', fp) == EOF)
2581 return -1;
2583 /* filtering rules */
2584 if (fprintf(fp, "[filtering]\n") < 0 ||
2585 prefs_filtering_write(fp, filtering_rules) < 0 ||
2586 claws_fputc('\n', fp) == EOF)
2587 return -1;
2589 return 0;
2593 *\brief Write filtering / matcher configuration file
2595 void prefs_matcher_write_config(void)
2597 gchar *rcpath;
2598 PrefFile *pfile;
2600 debug_print("Writing matcher configuration...\n");
2602 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
2603 MATCHER_RC, NULL);
2605 if ((pfile = prefs_write_open(rcpath)) == NULL) {
2606 g_warning("failed to write configuration to file");
2607 g_free(rcpath);
2608 return;
2611 g_free(rcpath);
2613 if (prefs_matcher_save(pfile->fp) < 0) {
2614 g_warning("failed to write configuration to file");
2615 prefs_file_close_revert(pfile);
2616 } else if (prefs_file_close(pfile) < 0) {
2617 g_warning("failed to save configuration to file");
2622 *\brief Read matcher configuration
2624 void prefs_matcher_read_config(void)
2626 gchar *rcpath;
2627 FILE *f;
2629 create_matchparser_hashtab();
2630 prefs_filtering_clear();
2632 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MATCHER_RC, NULL);
2634 f = claws_fopen(rcpath, "rb");
2635 g_free(rcpath);
2637 if (f != NULL) {
2638 matcher_parser_start_parsing(f);
2639 claws_fclose(matcher_parserin);