2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 2002-2011 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/>.
25 #include <glib/gi18n.h>
37 #include "procheader.h"
39 #include "matcher_parser.h"
40 #include "prefs_gtk.h"
41 #include "addr_compl.h"
43 #include "quoted-printable.h"
46 #include "prefs_common.h"
51 *\brief Keyword lookup element
54 gint id; /*!< keyword id */
55 gchar *str; /*!< keyword */
57 typedef struct _MatchParser MatchParser;
60 *\brief Table with strings and ids used by the lexer and
61 * the parser. New keywords can be added here.
63 static const MatchParser matchparser_tab[] = {
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 {MATCHCRITERIA_HAS_ATTACHMENT, "has_attachment"},
89 {MATCHCRITERIA_HAS_NO_ATTACHMENT, "~has_attachment"},
90 {MATCHCRITERIA_SIGNED, "signed"},
91 {MATCHCRITERIA_NOT_SIGNED, "~signed"},
94 {MATCHCRITERIA_SUBJECT, "subject"},
95 {MATCHCRITERIA_NOT_SUBJECT, "~subject"},
96 {MATCHCRITERIA_FROM, "from"},
97 {MATCHCRITERIA_NOT_FROM, "~from"},
98 {MATCHCRITERIA_TO, "to"},
99 {MATCHCRITERIA_NOT_TO, "~to"},
100 {MATCHCRITERIA_CC, "cc"},
101 {MATCHCRITERIA_NOT_CC, "~cc"},
102 {MATCHCRITERIA_TO_OR_CC, "to_or_cc"},
103 {MATCHCRITERIA_NOT_TO_AND_NOT_CC, "~to_or_cc"},
104 {MATCHCRITERIA_TAG, "tag"},
105 {MATCHCRITERIA_NOT_TAG, "~tag"},
106 {MATCHCRITERIA_TAGGED, "tagged"},
107 {MATCHCRITERIA_NOT_TAGGED, "~tagged"},
108 {MATCHCRITERIA_AGE_GREATER, "age_greater"},
109 {MATCHCRITERIA_AGE_LOWER, "age_lower"},
110 {MATCHCRITERIA_NEWSGROUPS, "newsgroups"},
111 {MATCHCRITERIA_NOT_NEWSGROUPS, "~newsgroups"},
112 {MATCHCRITERIA_INREPLYTO, "inreplyto"},
113 {MATCHCRITERIA_NOT_INREPLYTO, "~inreplyto"},
114 {MATCHCRITERIA_REFERENCES, "references"},
115 {MATCHCRITERIA_NOT_REFERENCES, "~references"},
116 {MATCHCRITERIA_SCORE_GREATER, "score_greater"},
117 {MATCHCRITERIA_SCORE_LOWER, "score_lower"},
118 {MATCHCRITERIA_SCORE_EQUAL, "score_equal"},
119 {MATCHCRITERIA_PARTIAL, "partial"},
120 {MATCHCRITERIA_NOT_PARTIAL, "~partial"},
121 {MATCHCRITERIA_FOUND_IN_ADDRESSBOOK, "found_in_addressbook"},
122 {MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK, "~found_in_addressbook"},
124 {MATCHCRITERIA_SIZE_GREATER, "size_greater"},
125 {MATCHCRITERIA_SIZE_SMALLER, "size_smaller"},
126 {MATCHCRITERIA_SIZE_EQUAL, "size_equal"},
128 /* content have to be read */
129 {MATCHCRITERIA_HEADER, "header"},
130 {MATCHCRITERIA_NOT_HEADER, "~header"},
131 {MATCHCRITERIA_HEADERS_PART, "headers_part"},
132 {MATCHCRITERIA_NOT_HEADERS_PART, "~headers_part"},
133 {MATCHCRITERIA_MESSAGE, "message"},
134 {MATCHCRITERIA_NOT_MESSAGE, "~message"},
135 {MATCHCRITERIA_BODY_PART, "body_part"},
136 {MATCHCRITERIA_NOT_BODY_PART, "~body_part"},
137 {MATCHCRITERIA_TEST, "test"},
138 {MATCHCRITERIA_NOT_TEST, "~test"},
141 {MATCHTYPE_MATCHCASE, "matchcase"},
142 {MATCHTYPE_MATCH, "match"},
143 {MATCHTYPE_REGEXPCASE, "regexpcase"},
144 {MATCHTYPE_REGEXP, "regexp"},
147 {MATCHACTION_SCORE, "score"}, /* for backward compatibility */
148 {MATCHACTION_MOVE, "move"},
149 {MATCHACTION_COPY, "copy"},
150 {MATCHACTION_DELETE, "delete"},
151 {MATCHACTION_MARK, "mark"},
152 {MATCHACTION_UNMARK, "unmark"},
153 {MATCHACTION_LOCK, "lock"},
154 {MATCHACTION_UNLOCK, "unlock"},
155 {MATCHACTION_MARK_AS_READ, "mark_as_read"},
156 {MATCHACTION_MARK_AS_UNREAD, "mark_as_unread"},
157 {MATCHACTION_MARK_AS_SPAM, "mark_as_spam"},
158 {MATCHACTION_MARK_AS_HAM, "mark_as_ham"},
159 {MATCHACTION_FORWARD, "forward"},
160 {MATCHACTION_FORWARD_AS_ATTACHMENT, "forward_as_attachment"},
161 {MATCHACTION_EXECUTE, "execute"},
162 {MATCHACTION_COLOR, "color"},
163 {MATCHACTION_REDIRECT, "redirect"},
164 {MATCHACTION_CHANGE_SCORE, "change_score"},
165 {MATCHACTION_SET_SCORE, "set_score"},
166 {MATCHACTION_STOP, "stop"},
167 {MATCHACTION_HIDE, "hide"},
168 {MATCHACTION_IGNORE, "ignore"},
169 {MATCHACTION_WATCH, "watch"},
170 {MATCHACTION_ADD_TO_ADDRESSBOOK, "add_to_addressbook"},
171 {MATCHACTION_SET_TAG, "set_tag"},
172 {MATCHACTION_UNSET_TAG, "unset_tag"},
173 {MATCHACTION_CLEAR_TAGS, "clear_tags"},
182 extern gboolean debug_filtering_session;
185 *\brief Look up table with keywords defined in \sa matchparser_tab
187 static GHashTable *matchparser_hashtab;
190 *\brief Translate keyword id to keyword string
192 *\param id Id of keyword
194 *\return const gchar * Keyword
196 const gchar *get_matchparser_tab_str(gint id)
200 for (i = 0; i < sizeof matchparser_tab / sizeof matchparser_tab[0]; i++) {
201 if (matchparser_tab[i].id == id)
202 return matchparser_tab[i].str;
208 *\brief Create keyword lookup table
210 static void create_matchparser_hashtab(void)
214 if (matchparser_hashtab) return;
215 matchparser_hashtab = g_hash_table_new(g_str_hash, g_str_equal);
216 for (i = 0; i < sizeof matchparser_tab / sizeof matchparser_tab[0]; i++)
217 g_hash_table_insert(matchparser_hashtab,
218 matchparser_tab[i].str,
219 (gpointer) &matchparser_tab[i]);
223 *\brief Return a keyword id from a keyword string
225 *\param str Keyword string
227 *\return gint Keyword id
229 gint get_matchparser_tab_id(const gchar *str)
233 if (NULL != (res = g_hash_table_lookup(matchparser_hashtab, str))) {
239 /* **************** data structure allocation **************** */
242 *\brief Allocate a structure for a filtering / scoring
243 * "condition" (a matcher structure)
245 *\param criteria Criteria ID (MATCHCRITERIA_XXXX)
246 *\param header Header string (if criteria is MATCHCRITERIA_HEADER
247 or MATCHCRITERIA_FOUND_IN_ADDRESSBOOK)
248 *\param matchtype Type of action (MATCHTYPE_XXX)
249 *\param expr String value or expression to check
250 *\param value Integer value to check
252 *\return MatcherProp * Pointer to newly allocated structure
254 MatcherProp *matcherprop_new(gint criteria, const gchar *header,
255 gint matchtype, const gchar *expr,
260 prop = g_new0(MatcherProp, 1);
261 prop->criteria = criteria;
262 prop->header = header != NULL ? g_strdup(header) : NULL;
264 prop->expr = expr != NULL ? g_strdup(expr) : NULL;
266 prop->matchtype = matchtype;
275 *\brief Allocate a structure for a filtering / scoring
276 * "condition" (a matcher structure)
277 * Same as matcherprop_new, except it doesn't change the expr's
280 *\param criteria Criteria ID (MATCHCRITERIA_XXXX)
281 *\param header Header string (if criteria is MATCHCRITERIA_HEADER
282 or MATCHCRITERIA_FOUND_IN_ADDRESSBOOK)
283 *\param matchtype Type of action (MATCHTYPE_XXX)
284 *\param expr String value or expression to check
285 *\param value Integer value to check
287 *\return MatcherProp * Pointer to newly allocated structure
289 MatcherProp *matcherprop_new_create(gint criteria, const gchar *header,
290 gint matchtype, const gchar *expr,
295 prop = g_new0(MatcherProp, 1);
296 prop->criteria = criteria;
297 prop->header = header != NULL ? g_strdup(header) : NULL;
299 prop->expr = expr != NULL ? g_strdup(expr) : NULL;
301 prop->matchtype = matchtype;
310 *\brief Free a matcher structure
312 *\param prop Pointer to matcher structure allocated with
315 void matcherprop_free(MatcherProp *prop)
318 g_free(prop->header);
319 if (prop->preg != NULL) {
327 *\brief Copy a matcher structure
329 *\param src Matcher structure to copy
331 *\return MatcherProp * Pointer to newly allocated matcher structure
333 MatcherProp *matcherprop_copy(const MatcherProp *src)
335 MatcherProp *prop = g_new0(MatcherProp, 1);
337 prop->criteria = src->criteria;
338 prop->header = src->header ? g_strdup(src->header) : NULL;
339 prop->expr = src->expr ? g_strdup(src->expr) : NULL;
340 prop->matchtype = src->matchtype;
342 prop->preg = NULL; /* will be re-evaluated */
343 prop->value = src->value;
344 prop->error = src->error;
348 /* ************** match ******************************/
350 static gboolean match_with_addresses_in_addressbook
351 (MatcherProp *prop, GSList *address_list, gint type,
352 gchar* folderpath, gint match)
355 gboolean found = FALSE;
358 cm_return_val_if_fail(address_list != NULL, FALSE);
360 debug_print("match_with_addresses_in_addressbook(%d, %s)\n",
361 g_slist_length(address_list), folderpath?folderpath:"(null)");
363 if (folderpath == NULL ||
364 strcasecmp(folderpath, "Any") == 0 ||
370 start_address_completion(path);
372 for (walk = address_list; walk != NULL; walk = walk->next) {
373 /* exact matching of email address */
374 guint num_addr = complete_address(walk->data);
377 /* skip first item (this is the search string itself) */
379 for (; i < num_addr && !found; i++) {
380 gchar *addr = get_complete_address(i);
381 extract_address(addr);
382 if (strcasecmp(addr, walk->data) == 0) {
386 if (debug_filtering_session
387 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
388 log_print(LOG_DEBUG_FILTERING,
389 "address [ %s ] matches\n",
390 (gchar *)walk->data);
397 if (debug_filtering_session
398 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH
400 log_print(LOG_DEBUG_FILTERING,
401 "address [ %s ] does NOT match\n",
402 (gchar *)walk->data);
406 if (match == MATCH_ALL) {
407 /* if matching all addresses, stop if one doesn't match */
410 if (debug_filtering_session
411 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
412 log_print(LOG_DEBUG_FILTERING,
413 "not all address match (matching all)\n");
417 } else if (match == MATCH_ANY) {
418 /* if matching any address, stop if one does match */
421 if (debug_filtering_session
422 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
423 log_print(LOG_DEBUG_FILTERING,
424 "at least one address matches (matching any)\n");
429 /* MATCH_ONE: there should be only one loop iteration */
432 end_address_completion();
438 *\brief Find out if a string matches a condition
440 *\param prop Matcher structure
441 *\param str String to check
443 *\return gboolean TRUE if str matches the condition in the
446 static gboolean matcherprop_string_match(MatcherProp *prop, const gchar *str,
447 const gchar *debug_context)
451 gboolean ret = FALSE;
452 gboolean should_free = FALSE;
456 if (prop->matchtype == MATCHTYPE_REGEXPCASE ||
457 prop->matchtype == MATCHTYPE_MATCHCASE) {
458 str1 = g_utf8_casefold(str, -1);
459 down_expr = g_utf8_casefold(prop->expr, -1);
463 down_expr = (gchar *)prop->expr;
467 switch (prop->matchtype) {
468 case MATCHTYPE_REGEXPCASE:
469 case MATCHTYPE_REGEXP:
471 if (!prop->preg && (prop->error == 0)) {
472 prop->preg = g_new0(regex_t, 1);
473 /* if regexp then don't use the escaped string */
474 if (regcomp(prop->preg, down_expr,
475 REG_NOSUB | REG_EXTENDED
476 | ((prop->matchtype == MATCHTYPE_REGEXPCASE)
477 ? REG_ICASE : 0)) != 0) {
483 if (prop->preg == NULL) {
488 if (regexec(prop->preg, str1, 0, NULL, 0) == 0)
494 if (debug_filtering_session
495 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
496 gchar *stripped = g_strdup(str);
498 strretchomp(stripped);
500 log_print(LOG_DEBUG_FILTERING,
501 "%s value [ %s ] matches regular expression [ %s ] (%s)\n",
502 debug_context, stripped, prop->expr,
503 prop->matchtype == MATCHTYPE_REGEXP ? _("Case sensitive"):_("Case insensitive"));
505 log_print(LOG_DEBUG_FILTERING,
506 "%s value [ %s ] does NOT match regular expression [ %s ] (%s)\n",
507 debug_context, stripped, prop->expr,
508 prop->matchtype == MATCHTYPE_REGEXP ? _("Case sensitive"):_("Case insensitive"));
514 case MATCHTYPE_MATCHCASE:
515 case MATCHTYPE_MATCH:
516 ret = (strstr(str1, down_expr) != NULL);
519 if (debug_filtering_session
520 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
521 gchar *stripped = g_strdup(str);
523 strretchomp(stripped);
525 log_print(LOG_DEBUG_FILTERING,
526 "%s value [ %s ] contains [ %s ] (%s)\n",
527 debug_context, stripped, prop->expr,
528 prop->matchtype == MATCHTYPE_MATCH ? _("Case sensitive"):_("Case insensitive"));
530 log_print(LOG_DEBUG_FILTERING,
531 "%s value [ %s ] does NOT contain [ %s ] (%s)\n",
532 debug_context, stripped, prop->expr,
533 prop->matchtype == MATCHTYPE_MATCH ? _("Case sensitive"):_("Case insensitive"));
552 *\brief Find out if a tag matches a condition
554 *\param prop Matcher structure
555 *\param msginfo message to check
557 *\return gboolean TRUE if msginfo matches the condition in the
560 static gboolean matcherprop_tag_match(MatcherProp *prop, MsgInfo *msginfo,
561 const gchar *debug_context)
563 gboolean ret = FALSE;
566 if (msginfo == NULL || msginfo->tags == NULL)
569 for (cur = msginfo->tags; cur; cur = cur->next) {
570 const gchar *str = tags_get_tag(GPOINTER_TO_INT(cur->data));
573 if (matcherprop_string_match(prop, str, debug_context)) {
582 *\brief Find out if the string-ed list matches a condition
584 *\param prop Matcher structure
585 *\param list GSList of strings to check
587 *\return gboolean TRUE if str matches the condition in the
590 static gboolean matcherprop_list_match(MatcherProp *prop, const GSList *list,
591 const gchar *debug_context)
595 for(cur = list; cur != NULL; cur = cur->next) {
596 if (matcherprop_string_match(prop, (gchar *)cur->data, debug_context))
602 static gboolean matcherprop_header_line_match(MatcherProp *prop, const gchar *hdr,
603 const gchar *str, const gchar *debug_context)
606 gboolean res = FALSE;
608 if (hdr == NULL || str == NULL)
611 line = g_strdup_printf("%s %s", hdr, str);
612 res = matcherprop_string_match(prop, line, debug_context);
619 typedef struct _thread_data {
626 static void *matcher_test_thread(void *data)
628 thread_data *td = (thread_data *)data;
631 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
632 pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
634 result = system(td->cmd);
635 td->done = TRUE; /* let the caller thread join() */
636 return GINT_TO_POINTER(result);
641 *\brief Execute a command defined in the matcher structure
643 *\param prop Pointer to matcher structure
644 *\param info Pointer to message info structure
646 *\return gboolean TRUE if command was executed succesfully
648 static gboolean matcherprop_match_test(const MatcherProp *prop,
657 thread_data *td = g_new0(thread_data, 1);
659 time_t start_time = time(NULL);
662 file = procmsg_get_message_file(info);
671 cmd = matching_build_command(prop->expr, info);
681 if (debug_filtering_session
682 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
683 log_print(LOG_DEBUG_FILTERING,
684 "starting threaded command [ %s ]\n",
690 if (pthread_attr_init(&pta) != 0 ||
691 pthread_attr_setdetachstate(&pta, PTHREAD_CREATE_JOINABLE) != 0 ||
692 pthread_create(&pt, &pta, matcher_test_thread, td) != 0)
693 retval = system(cmd);
695 debug_print("waiting for test thread\n");
697 /* don't let the interface freeze while waiting */
699 if (time(NULL) - start_time > 30) {
705 pthread_join(pt, &res);
706 retval = GPOINTER_TO_INT(res);
707 debug_print(" test thread returned %d\n", retval);
712 if (debug_filtering_session
713 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
714 log_print(LOG_DEBUG_FILTERING,
715 "starting synchronous command [ %s ]\n",
719 retval = system(cmd);
721 debug_print("Command exit code: %d\n", retval);
724 if (debug_filtering_session
725 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
726 log_print(LOG_DEBUG_FILTERING,
727 "command returned [ %d ]\n",
732 return (retval == 0);
736 *\brief Check if a message matches the condition in a matcher
739 *\param prop Pointer to matcher structure
740 *\param info Pointer to message info
742 *\return gboolean TRUE if a match
744 gboolean matcherprop_match(MatcherProp *prop,
749 switch(prop->criteria) {
750 case MATCHCRITERIA_ALL:
752 case MATCHCRITERIA_UNREAD:
753 return MSG_IS_UNREAD(info->flags);
754 case MATCHCRITERIA_NOT_UNREAD:
755 return !MSG_IS_UNREAD(info->flags);
756 case MATCHCRITERIA_NEW:
757 return MSG_IS_NEW(info->flags);
758 case MATCHCRITERIA_NOT_NEW:
759 return !MSG_IS_NEW(info->flags);
760 case MATCHCRITERIA_MARKED:
761 return MSG_IS_MARKED(info->flags);
762 case MATCHCRITERIA_NOT_MARKED:
763 return !MSG_IS_MARKED(info->flags);
764 case MATCHCRITERIA_DELETED:
765 return MSG_IS_DELETED(info->flags);
766 case MATCHCRITERIA_NOT_DELETED:
767 return !MSG_IS_DELETED(info->flags);
768 case MATCHCRITERIA_REPLIED:
769 return MSG_IS_REPLIED(info->flags);
770 case MATCHCRITERIA_NOT_REPLIED:
771 return !MSG_IS_REPLIED(info->flags);
772 case MATCHCRITERIA_FORWARDED:
773 return MSG_IS_FORWARDED(info->flags);
774 case MATCHCRITERIA_NOT_FORWARDED:
775 return !MSG_IS_FORWARDED(info->flags);
776 case MATCHCRITERIA_LOCKED:
777 return MSG_IS_LOCKED(info->flags);
778 case MATCHCRITERIA_NOT_LOCKED:
779 return !MSG_IS_LOCKED(info->flags);
780 case MATCHCRITERIA_SPAM:
781 return MSG_IS_SPAM(info->flags);
782 case MATCHCRITERIA_NOT_SPAM:
783 return !MSG_IS_SPAM(info->flags);
784 case MATCHCRITERIA_HAS_ATTACHMENT:
785 return MSG_IS_WITH_ATTACHMENT(info->flags);
786 case MATCHCRITERIA_HAS_NO_ATTACHMENT:
787 return !MSG_IS_WITH_ATTACHMENT(info->flags);
788 case MATCHCRITERIA_SIGNED:
789 return MSG_IS_SIGNED(info->flags);
790 case MATCHCRITERIA_NOT_SIGNED:
791 return !MSG_IS_SIGNED(info->flags);
792 case MATCHCRITERIA_COLORLABEL:
794 gint color = MSG_GET_COLORLABEL_VALUE(info->flags);
795 gboolean ret = (color == prop->value);
798 if (debug_filtering_session
799 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
801 log_print(LOG_DEBUG_FILTERING,
802 "message color value [ %d ] matches color value [ %d ]\n",
805 log_print(LOG_DEBUG_FILTERING,
806 "message color value [ %d ] does NOT match color value [ %d ]\n",
812 case MATCHCRITERIA_NOT_COLORLABEL:
814 gint color = MSG_GET_COLORLABEL_VALUE(info->flags);
815 gboolean ret = (color != prop->value);
818 if (debug_filtering_session
819 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
821 log_print(LOG_DEBUG_FILTERING,
822 "message color value [ %d ] matches color value [ %d ]\n",
825 log_print(LOG_DEBUG_FILTERING,
826 "message color value [ %d ] does NOT match color value [ %d ]\n",
832 case MATCHCRITERIA_IGNORE_THREAD:
833 return MSG_IS_IGNORE_THREAD(info->flags);
834 case MATCHCRITERIA_NOT_IGNORE_THREAD:
835 return !MSG_IS_IGNORE_THREAD(info->flags);
836 case MATCHCRITERIA_WATCH_THREAD:
837 return MSG_IS_WATCH_THREAD(info->flags);
838 case MATCHCRITERIA_NOT_WATCH_THREAD:
839 return !MSG_IS_WATCH_THREAD(info->flags);
840 case MATCHCRITERIA_SUBJECT:
841 return matcherprop_string_match(prop, info->subject,
842 prefs_common_translated_header_name("Subject:"));
843 case MATCHCRITERIA_NOT_SUBJECT:
844 return !matcherprop_string_match(prop, info->subject,
845 prefs_common_translated_header_name("Subject:"));
846 case MATCHCRITERIA_FROM:
847 case MATCHCRITERIA_NOT_FROM:
852 context = g_strdup_printf(_("%s header"), prefs_common_translated_header_name("From:"));
853 ret = matcherprop_string_match(prop, info->from, context);
855 return (prop->criteria == MATCHCRITERIA_FROM)? ret : !ret;
857 case MATCHCRITERIA_TO:
858 case MATCHCRITERIA_NOT_TO:
863 context = g_strdup_printf(_("%s header"), prefs_common_translated_header_name("To:"));
864 ret = matcherprop_string_match(prop, info->to, context);
866 return (prop->criteria == MATCHCRITERIA_TO)? ret : !ret;
868 case MATCHCRITERIA_CC:
869 case MATCHCRITERIA_NOT_CC:
874 context = g_strdup_printf(_("%s header"), prefs_common_translated_header_name("Cc:"));
875 ret = matcherprop_string_match(prop, info->cc, context);
877 return (prop->criteria == MATCHCRITERIA_CC)? ret : !ret;
879 case MATCHCRITERIA_TO_OR_CC:
881 gchar *context1, *context2;
884 context1 = g_strdup_printf(_("%s header"), prefs_common_translated_header_name("To:"));
885 context2 = g_strdup_printf(_("%s header"), prefs_common_translated_header_name("Cc:"));
886 ret = matcherprop_string_match(prop, info->to, context1)
887 || matcherprop_string_match(prop, info->cc, context2);
892 case MATCHCRITERIA_NOT_TO_AND_NOT_CC:
894 gchar *context1, *context2;
897 context1 = g_strdup_printf(_("%s header"), prefs_common_translated_header_name("To:"));
898 context2 = g_strdup_printf(_("%s header"), prefs_common_translated_header_name("Cc:"));
899 ret = !(matcherprop_string_match(prop, info->to, context1)
900 || matcherprop_string_match(prop, info->cc, context2));
905 case MATCHCRITERIA_TAG:
906 case MATCHCRITERIA_NOT_TAG:
910 ret = matcherprop_tag_match(prop, info, _("Tag"));
911 return (prop->criteria == MATCHCRITERIA_TAG)? ret : !ret;
913 case MATCHCRITERIA_TAGGED:
914 case MATCHCRITERIA_NOT_TAGGED:
918 ret = (info->tags != NULL);
919 return (prop->criteria == MATCHCRITERIA_TAGGED)? ret : !ret;
921 case MATCHCRITERIA_AGE_GREATER:
927 age = ((t - info->date_t) / (60 * 60 * 24));
928 ret = (age > prop->value);
931 if (debug_filtering_session
932 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
934 log_print(LOG_DEBUG_FILTERING,
935 "message age [ %d ] is greater than [ %d ]\n",
938 log_print(LOG_DEBUG_FILTERING,
939 "message age [ %d ] is not greater than [ %d ]\n",
945 case MATCHCRITERIA_AGE_LOWER:
951 age = ((t - info->date_t) / (60 * 60 * 24));
952 ret = (age < prop->value);
955 if (debug_filtering_session
956 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
958 log_print(LOG_DEBUG_FILTERING,
959 "message age [ %d ] is lower than [ %d ]\n",
962 log_print(LOG_DEBUG_FILTERING,
963 "message age [ %d ] is not lower than [ %d ]\n",
969 case MATCHCRITERIA_SCORE_GREATER:
971 gboolean ret = (info->score > prop->value);
974 if (debug_filtering_session
975 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
977 log_print(LOG_DEBUG_FILTERING,
978 "message score [ %d ] is greater than [ %d ]\n",
979 info->score, prop->value);
981 log_print(LOG_DEBUG_FILTERING,
982 "message score [ %d ] is not greater than [ %d ]\n",
983 info->score, prop->value);
988 case MATCHCRITERIA_SCORE_LOWER:
990 gboolean ret = (info->score < prop->value);
993 if (debug_filtering_session
994 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
996 log_print(LOG_DEBUG_FILTERING,
997 "message score [ %d ] is lower than [ %d ]\n",
998 info->score, prop->value);
1000 log_print(LOG_DEBUG_FILTERING,
1001 "message score [ %d ] is not lower than [ %d ]\n",
1002 info->score, prop->value);
1007 case MATCHCRITERIA_SCORE_EQUAL:
1009 gboolean ret = (info->score == prop->value);
1012 if (debug_filtering_session
1013 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
1015 log_print(LOG_DEBUG_FILTERING,
1016 "message score [ %d ] is equal to [ %d ]\n",
1017 info->score, prop->value);
1019 log_print(LOG_DEBUG_FILTERING,
1020 "message score [ %d ] is not equal to [ %d ]\n",
1021 info->score, prop->value);
1026 case MATCHCRITERIA_SIZE_GREATER:
1028 /* FIXME: info->size is a goffset */
1029 gboolean ret = (info->size > (goffset) prop->value);
1032 if (debug_filtering_session
1033 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
1035 log_print(LOG_DEBUG_FILTERING,
1036 "message size is greater than [ %d ]\n",
1039 log_print(LOG_DEBUG_FILTERING,
1040 "message size is not greater than [ %d ]\n",
1046 case MATCHCRITERIA_SIZE_SMALLER:
1048 /* FIXME: info->size is a goffset */
1049 gboolean ret = (info->size < (goffset) prop->value);
1052 if (debug_filtering_session
1053 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
1055 log_print(LOG_DEBUG_FILTERING,
1056 "message size is smaller than [ %d ]\n",
1059 log_print(LOG_DEBUG_FILTERING,
1060 "message size is not smaller than [ %d ]\n",
1066 case MATCHCRITERIA_SIZE_EQUAL:
1068 /* FIXME: info->size is a goffset */
1069 gboolean ret = (info->size == (goffset) prop->value);
1072 if (debug_filtering_session
1073 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
1075 log_print(LOG_DEBUG_FILTERING,
1076 "message size is equal to [ %d ]\n",
1079 log_print(LOG_DEBUG_FILTERING,
1080 "message size is not equal to [ %d ]\n",
1086 case MATCHCRITERIA_PARTIAL:
1088 /* FIXME: info->size is a goffset */
1089 gboolean ret = (info->total_size != 0 && info->size != (goffset)info->total_size);
1092 if (debug_filtering_session
1093 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
1095 log_print(LOG_DEBUG_FILTERING,
1096 "message is partially downloaded, size is less than total size [ %d ])\n",
1099 log_print(LOG_DEBUG_FILTERING,
1100 "message is not partially downloaded\n");
1105 case MATCHCRITERIA_NOT_PARTIAL:
1107 /* FIXME: info->size is a goffset */
1108 gboolean ret = (info->total_size == 0 || info->size == (goffset)info->total_size);
1111 if (debug_filtering_session
1112 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
1114 log_print(LOG_DEBUG_FILTERING,
1115 "message is not partially downloaded\n");
1117 log_print(LOG_DEBUG_FILTERING,
1118 "message is partially downloaded, size is less than total size [ %d ])\n",
1124 case MATCHCRITERIA_NEWSGROUPS:
1125 case MATCHCRITERIA_NOT_NEWSGROUPS:
1130 context = g_strdup_printf(_("%s header"),
1131 prefs_common_translated_header_name("Newsgroups:"));
1132 ret = matcherprop_string_match(prop, info->newsgroups, context);
1134 return (prop->criteria == MATCHCRITERIA_NEWSGROUPS)? ret : !ret;
1136 case MATCHCRITERIA_INREPLYTO:
1137 case MATCHCRITERIA_NOT_INREPLYTO:
1142 context = g_strdup_printf(_("%s header"),
1143 prefs_common_translated_header_name("In-Reply-To:"));
1144 ret = matcherprop_string_match(prop, info->inreplyto, context);
1146 return (prop->criteria == MATCHCRITERIA_INREPLYTO)? ret : !ret;
1148 case MATCHCRITERIA_REFERENCES:
1149 case MATCHCRITERIA_NOT_REFERENCES:
1154 context = g_strdup_printf(_("%s header"),
1155 prefs_common_translated_header_name("References:"));
1156 ret = matcherprop_list_match(prop, info->references, context);
1158 return (prop->criteria == MATCHCRITERIA_REFERENCES)? ret : !ret;
1160 case MATCHCRITERIA_TEST:
1161 return matcherprop_match_test(prop, info);
1162 case MATCHCRITERIA_NOT_TEST:
1163 return !matcherprop_match_test(prop, info);
1169 /* ********************* MatcherList *************************** */
1172 *\brief Create a new list of matchers
1174 *\param matchers List of matcher structures
1175 *\param bool_and Operator
1177 *\return MatcherList * New list
1179 MatcherList *matcherlist_new(GSList *matchers, gboolean bool_and)
1183 cond = g_new0(MatcherList, 1);
1185 cond->matchers = matchers;
1186 cond->bool_and = bool_and;
1192 *\brief Frees a list of matchers
1194 *\param cond List of matchers
1196 void matcherlist_free(MatcherList *cond)
1200 cm_return_if_fail(cond);
1201 for (l = cond->matchers ; l != NULL ; l = g_slist_next(l)) {
1202 matcherprop_free((MatcherProp *) l->data);
1204 g_slist_free(cond->matchers);
1209 *\brief Skip all headers in a message file
1211 *\param fp Message file
1213 static void matcherlist_skip_headers(FILE *fp)
1215 gchar buf[BUFFSIZE];
1217 while (procheader_get_one_field(buf, sizeof(buf), fp, NULL) != -1)
1222 *\brief Check if a header matches a matcher condition
1224 *\param matcher Matcher structure to check header for
1225 *\param buf Header name
1227 *\return boolean TRUE if matching header
1229 static gboolean matcherprop_match_one_header(MatcherProp *matcher,
1232 gboolean result = FALSE;
1233 Header *header = NULL;
1235 switch (matcher->criteria) {
1236 case MATCHCRITERIA_HEADER:
1237 case MATCHCRITERIA_NOT_HEADER:
1238 header = procheader_parse_header(buf);
1241 if (procheader_headername_equal(header->name,
1243 if (matcher->criteria == MATCHCRITERIA_HEADER)
1244 result = matcherprop_string_match(matcher, header->body, _("header"));
1246 result = !matcherprop_string_match(matcher, header->body, _("header"));
1247 procheader_header_free(header);
1251 procheader_header_free(header);
1254 case MATCHCRITERIA_HEADERS_PART:
1255 case MATCHCRITERIA_MESSAGE:
1256 header = procheader_parse_header(buf);
1259 result = matcherprop_header_line_match(matcher,
1260 header->name, header->body, _("header line"));
1261 procheader_header_free(header);
1263 case MATCHCRITERIA_NOT_HEADERS_PART:
1264 case MATCHCRITERIA_NOT_MESSAGE:
1265 header = procheader_parse_header(buf);
1268 result = !matcherprop_header_line_match(matcher,
1269 header->name, header->body, _("header line"));
1270 procheader_header_free(header);
1272 case MATCHCRITERIA_FOUND_IN_ADDRESSBOOK:
1273 case MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK:
1275 GSList *address_list = NULL;
1276 gint match = MATCH_ONE;
1277 gboolean found = FALSE;
1279 /* how many address headers are me trying to mach? */
1280 if (strcasecmp(matcher->header, "Any") == 0)
1282 else if (strcasecmp(matcher->header, "All") == 0)
1285 if (match == MATCH_ONE) {
1286 /* matching one address header exactly, is that the right one? */
1287 header = procheader_parse_header(buf);
1289 !procheader_headername_equal(header->name, matcher->header))
1291 address_list = address_list_append(address_list, header->body);
1292 if (address_list == NULL)
1296 header = procheader_parse_header(buf);
1299 /* address header is one of the headers we have to match when checking
1300 for any address header or all address headers? */
1301 if (procheader_headername_equal(header->name, "From") ||
1302 procheader_headername_equal(header->name, "To") ||
1303 procheader_headername_equal(header->name, "Cc") ||
1304 procheader_headername_equal(header->name, "Reply-To") ||
1305 procheader_headername_equal(header->name, "Sender"))
1306 address_list = address_list_append(address_list, header->body);
1307 if (address_list == NULL)
1311 found = match_with_addresses_in_addressbook
1312 (matcher, address_list, matcher->criteria,
1313 matcher->expr, match);
1314 g_slist_free(address_list);
1316 if (matcher->criteria == MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK)
1327 *\brief Check if the matcher structure wants headers to
1330 *\param matcher Matcher structure
1332 *\return gboolean TRUE if the matcher structure describes
1333 * a header match condition
1335 static gboolean matcherprop_criteria_headers(const MatcherProp *matcher)
1337 switch (matcher->criteria) {
1338 case MATCHCRITERIA_HEADER:
1339 case MATCHCRITERIA_NOT_HEADER:
1340 case MATCHCRITERIA_HEADERS_PART:
1341 case MATCHCRITERIA_NOT_HEADERS_PART:
1342 case MATCHCRITERIA_FOUND_IN_ADDRESSBOOK:
1343 case MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK:
1351 *\brief Check if the matcher structure wants the message
1352 * to be matched (just perform an action on any
1355 *\param matcher Matcher structure
1357 *\return gboolean TRUE if matcher condition should match
1360 static gboolean matcherprop_criteria_message(MatcherProp *matcher)
1362 switch (matcher->criteria) {
1363 case MATCHCRITERIA_MESSAGE:
1364 case MATCHCRITERIA_NOT_MESSAGE:
1372 *\brief Check if a list of conditions matches one header in
1375 *\param matchers List of conditions
1376 *\param fp Message file
1378 *\return gboolean TRUE if one of the headers is matched by
1379 * the list of conditions.
1381 static gboolean matcherlist_match_headers(MatcherList *matchers, FILE *fp)
1384 gchar buf[BUFFSIZE];
1386 while (procheader_get_one_field(buf, sizeof(buf), fp, NULL) != -1) {
1387 for (l = matchers->matchers ; l != NULL ; l = g_slist_next(l)) {
1388 MatcherProp *matcher = (MatcherProp *) l->data;
1389 gint match = MATCH_ANY;
1394 /* determine the match range (all, any are our concern here) */
1395 if (matcher->criteria == MATCHCRITERIA_NOT_HEADERS_PART ||
1396 matcher->criteria == MATCHCRITERIA_NOT_MESSAGE) {
1399 } else if (matcher->criteria == MATCHCRITERIA_FOUND_IN_ADDRESSBOOK ||
1400 matcher->criteria == MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK) {
1401 Header *header = NULL;
1403 /* address header is one of the headers we have to match when checking
1404 for any address header or all address headers? */
1405 header = procheader_parse_header(buf);
1407 (procheader_headername_equal(header->name, "From") ||
1408 procheader_headername_equal(header->name, "To") ||
1409 procheader_headername_equal(header->name, "Cc") ||
1410 procheader_headername_equal(header->name, "Reply-To") ||
1411 procheader_headername_equal(header->name, "Sender"))) {
1413 if (strcasecmp(matcher->header, "Any") == 0)
1415 else if (strcasecmp(matcher->header, "All") == 0)
1420 /* further call to matcherprop_match_one_header() can't match
1421 and it irrelevant, so: don't alter the match result */
1426 /* ZERO line must NOT match for the rule to match.
1428 if (match == MATCH_ALL) {
1429 if (matcherprop_match_one_header(matcher, buf)) {
1430 matcher->result = TRUE;
1432 matcher->result = FALSE;
1433 matcher->done = TRUE;
1435 /* else, just one line matching is enough for the rule to match
1437 } else if (matcherprop_criteria_headers(matcher) ||
1438 matcherprop_criteria_message(matcher)) {
1439 if (matcherprop_match_one_header(matcher, buf)) {
1440 matcher->result = TRUE;
1441 matcher->done = TRUE;
1445 /* if the rule matched and the matchers are OR, no need to
1446 * check the others */
1447 if (matcher->result && matcher->done) {
1448 if (!matchers->bool_and)
1458 *\brief Check if a matcher wants to check the message body
1460 *\param matcher Matcher structure
1462 *\return gboolean TRUE if body must be matched.
1464 static gboolean matcherprop_criteria_body(const MatcherProp *matcher)
1466 switch (matcher->criteria) {
1467 case MATCHCRITERIA_BODY_PART:
1468 case MATCHCRITERIA_NOT_BODY_PART:
1476 *\brief Check if a line in a message file's body matches
1479 *\param matchers List of conditions
1480 *\param fp Message file
1482 *\return gboolean TRUE if succesful match
1484 static gboolean matcherlist_match_body(MatcherList *matchers, gboolean body_only, MsgInfo *info)
1487 MimeInfo *mimeinfo = NULL;
1488 MimeInfo *partinfo = NULL;
1489 gchar buf[BUFFSIZE];
1490 gboolean first_text_found = FALSE;
1493 cm_return_val_if_fail(info != NULL, FALSE);
1495 mimeinfo = procmime_scan_message(info);
1498 partinfo = procmime_mimeinfo_next(mimeinfo);
1500 for (; partinfo != NULL; partinfo = procmime_mimeinfo_next(partinfo)) {
1502 if (partinfo->type != MIMETYPE_TEXT && body_only)
1505 if (partinfo->type == MIMETYPE_TEXT) {
1506 first_text_found = TRUE;
1507 outfp = procmime_get_text_content(partinfo);
1509 outfp = procmime_get_binary_content(partinfo);
1512 procmime_mimeinfo_free_all(mimeinfo);
1516 while (fgets(buf, sizeof(buf), outfp) != NULL) {
1519 for (l = matchers->matchers ; l != NULL ; l = g_slist_next(l)) {
1520 MatcherProp *matcher = (MatcherProp *) l->data;
1525 /* Don't scan non-text parts when looking in body, only
1526 * when looking in whole message
1528 if (partinfo && partinfo->type != MIMETYPE_TEXT &&
1529 (matcher->criteria == MATCHCRITERIA_NOT_BODY_PART ||
1530 matcher->criteria == MATCHCRITERIA_BODY_PART))
1533 /* if the criteria is ~body_part or ~message, ZERO lines
1534 * must match for the rule to match.
1536 if (matcher->criteria == MATCHCRITERIA_NOT_BODY_PART ||
1537 matcher->criteria == MATCHCRITERIA_NOT_MESSAGE) {
1538 if (matcherprop_string_match(matcher, buf,
1540 matcher->result = FALSE;
1541 matcher->done = TRUE;
1543 matcher->result = TRUE;
1544 /* else, just one line has to match */
1545 } else if (matcherprop_criteria_body(matcher) ||
1546 matcherprop_criteria_message(matcher)) {
1547 if (matcherprop_string_match(matcher, buf,
1549 matcher->result = TRUE;
1550 matcher->done = TRUE;
1554 /* if the matchers are OR'ed and the rule matched,
1555 * no need to check the others. */
1556 if (matcher->result && matcher->done) {
1557 if (!matchers->bool_and) {
1558 procmime_mimeinfo_free_all(mimeinfo);
1567 if (body_only && first_text_found)
1570 procmime_mimeinfo_free_all(mimeinfo);
1576 *\brief Check if a message file matches criteria
1578 *\param matchers Criteria
1579 *\param info Message info
1580 *\param result Default result
1582 *\return gboolean TRUE if matched
1584 static gboolean matcherlist_match_file(MatcherList *matchers, MsgInfo *info,
1587 gboolean read_headers;
1594 /* file need to be read ? */
1596 read_headers = FALSE;
1599 for (l = matchers->matchers ; l != NULL ; l = g_slist_next(l)) {
1600 MatcherProp *matcher = (MatcherProp *) l->data;
1602 if (matcherprop_criteria_headers(matcher))
1603 read_headers = TRUE;
1604 if (matcherprop_criteria_body(matcher))
1606 if (matcherprop_criteria_message(matcher)) {
1607 read_headers = TRUE;
1611 matcher->result = FALSE;
1612 matcher->done = FALSE;
1615 if (!read_headers && !read_body)
1618 file = procmsg_get_message_file_full(info, read_headers, read_body);
1622 if ((fp = g_fopen(file, "rb")) == NULL) {
1623 FILE_OP_ERROR(file, "fopen");
1628 /* read the headers */
1631 if (matcherlist_match_headers(matchers, fp))
1634 matcherlist_skip_headers(fp);
1639 matcherlist_match_body(matchers, body_only, info);
1642 for (l = matchers->matchers; l != NULL; l = g_slist_next(l)) {
1643 MatcherProp *matcher = (MatcherProp *) l->data;
1645 if (matcherprop_criteria_headers(matcher) ||
1646 matcherprop_criteria_body(matcher) ||
1647 matcherprop_criteria_message(matcher)) {
1648 if (matcher->result) {
1649 if (!matchers->bool_and) {
1655 if (matchers->bool_and) {
1671 *\brief Test list of conditions on a message.
1673 *\param matchers List of conditions
1674 *\param info Message info
1676 *\return gboolean TRUE if matched
1678 gboolean matcherlist_match(MatcherList *matchers, MsgInfo *info)
1686 if (matchers->bool_and)
1691 /* test the cached elements */
1693 for (l = matchers->matchers; l != NULL ;l = g_slist_next(l)) {
1694 MatcherProp *matcher = (MatcherProp *) l->data;
1696 if (debug_filtering_session) {
1697 gchar *buf = matcherprop_to_string(matcher);
1698 log_print(LOG_DEBUG_FILTERING, _("checking if message matches [ %s ]\n"), buf);
1702 switch(matcher->criteria) {
1703 case MATCHCRITERIA_ALL:
1704 case MATCHCRITERIA_UNREAD:
1705 case MATCHCRITERIA_NOT_UNREAD:
1706 case MATCHCRITERIA_NEW:
1707 case MATCHCRITERIA_NOT_NEW:
1708 case MATCHCRITERIA_MARKED:
1709 case MATCHCRITERIA_NOT_MARKED:
1710 case MATCHCRITERIA_DELETED:
1711 case MATCHCRITERIA_NOT_DELETED:
1712 case MATCHCRITERIA_REPLIED:
1713 case MATCHCRITERIA_NOT_REPLIED:
1714 case MATCHCRITERIA_FORWARDED:
1715 case MATCHCRITERIA_NOT_FORWARDED:
1716 case MATCHCRITERIA_LOCKED:
1717 case MATCHCRITERIA_NOT_LOCKED:
1718 case MATCHCRITERIA_SPAM:
1719 case MATCHCRITERIA_NOT_SPAM:
1720 case MATCHCRITERIA_HAS_ATTACHMENT:
1721 case MATCHCRITERIA_HAS_NO_ATTACHMENT:
1722 case MATCHCRITERIA_SIGNED:
1723 case MATCHCRITERIA_NOT_SIGNED:
1724 case MATCHCRITERIA_COLORLABEL:
1725 case MATCHCRITERIA_NOT_COLORLABEL:
1726 case MATCHCRITERIA_IGNORE_THREAD:
1727 case MATCHCRITERIA_NOT_IGNORE_THREAD:
1728 case MATCHCRITERIA_WATCH_THREAD:
1729 case MATCHCRITERIA_NOT_WATCH_THREAD:
1730 case MATCHCRITERIA_SUBJECT:
1731 case MATCHCRITERIA_NOT_SUBJECT:
1732 case MATCHCRITERIA_FROM:
1733 case MATCHCRITERIA_NOT_FROM:
1734 case MATCHCRITERIA_TO:
1735 case MATCHCRITERIA_NOT_TO:
1736 case MATCHCRITERIA_CC:
1737 case MATCHCRITERIA_NOT_CC:
1738 case MATCHCRITERIA_TO_OR_CC:
1739 case MATCHCRITERIA_NOT_TO_AND_NOT_CC:
1740 case MATCHCRITERIA_TAG:
1741 case MATCHCRITERIA_NOT_TAG:
1742 case MATCHCRITERIA_TAGGED:
1743 case MATCHCRITERIA_NOT_TAGGED:
1744 case MATCHCRITERIA_AGE_GREATER:
1745 case MATCHCRITERIA_AGE_LOWER:
1746 case MATCHCRITERIA_NEWSGROUPS:
1747 case MATCHCRITERIA_NOT_NEWSGROUPS:
1748 case MATCHCRITERIA_INREPLYTO:
1749 case MATCHCRITERIA_NOT_INREPLYTO:
1750 case MATCHCRITERIA_REFERENCES:
1751 case MATCHCRITERIA_NOT_REFERENCES:
1752 case MATCHCRITERIA_SCORE_GREATER:
1753 case MATCHCRITERIA_SCORE_LOWER:
1754 case MATCHCRITERIA_SCORE_EQUAL:
1755 case MATCHCRITERIA_SIZE_GREATER:
1756 case MATCHCRITERIA_SIZE_SMALLER:
1757 case MATCHCRITERIA_SIZE_EQUAL:
1758 case MATCHCRITERIA_TEST:
1759 case MATCHCRITERIA_NOT_TEST:
1760 case MATCHCRITERIA_PARTIAL:
1761 case MATCHCRITERIA_NOT_PARTIAL:
1762 if (matcherprop_match(matcher, info)) {
1763 if (!matchers->bool_and) {
1764 if (debug_filtering_session)
1765 log_status_ok(LOG_DEBUG_FILTERING, _("message matches\n"));
1770 if (matchers->bool_and) {
1771 if (debug_filtering_session)
1772 log_status_nok(LOG_DEBUG_FILTERING, _("message does not match\n"));
1779 /* test the condition on the file */
1781 if (matcherlist_match_file(matchers, info, result)) {
1782 if (!matchers->bool_and) {
1783 if (debug_filtering_session)
1784 log_status_ok(LOG_DEBUG_FILTERING, _("message matches\n"));
1788 if (matchers->bool_and) {
1789 if (debug_filtering_session)
1790 log_status_nok(LOG_DEBUG_FILTERING, _("message does not match\n"));
1795 if (debug_filtering_session) {
1797 log_status_ok(LOG_DEBUG_FILTERING, _("message matches\n"));
1799 log_status_nok(LOG_DEBUG_FILTERING, _("message does not match\n"));
1805 static gint quote_filter_str(gchar * result, guint size,
1815 for(p = path ; * p != '\0' ; p ++) {
1817 if ((* p != '\"') && (* p != '\\')) {
1818 if (remaining > 0) {
1824 result[size - 1] = '\0';
1829 if (remaining >= 2) {
1837 result[size - 1] = '\0';
1842 if (remaining > 0) {
1846 result[size - 1] = '\0';
1854 gchar * matcher_quote_str(const gchar * src)
1859 len = strlen(src) * 2 + 1;
1860 res = g_malloc(len);
1861 quote_filter_str(res, len, src);
1867 *\brief Convert a matcher structure to a string
1869 *\param matcher Matcher structure
1871 *\return gchar * Newly allocated string
1873 gchar *matcherprop_to_string(MatcherProp *matcher)
1875 gchar *matcher_str = NULL;
1876 const gchar *criteria_str;
1877 const gchar *matchtype_str;
1879 gchar * quoted_expr;
1880 gchar * quoted_header;
1882 criteria_str = NULL;
1883 for (i = 0; i < (int) (sizeof(matchparser_tab) / sizeof(MatchParser)); i++) {
1884 if (matchparser_tab[i].id == matcher->criteria)
1885 criteria_str = matchparser_tab[i].str;
1887 if (criteria_str == NULL)
1890 switch (matcher->criteria) {
1891 case MATCHCRITERIA_AGE_GREATER:
1892 case MATCHCRITERIA_AGE_LOWER:
1893 case MATCHCRITERIA_SCORE_GREATER:
1894 case MATCHCRITERIA_SCORE_LOWER:
1895 case MATCHCRITERIA_SCORE_EQUAL:
1896 case MATCHCRITERIA_SIZE_GREATER:
1897 case MATCHCRITERIA_SIZE_SMALLER:
1898 case MATCHCRITERIA_SIZE_EQUAL:
1899 case MATCHCRITERIA_COLORLABEL:
1900 case MATCHCRITERIA_NOT_COLORLABEL:
1901 return g_strdup_printf("%s %i", criteria_str, matcher->value);
1902 case MATCHCRITERIA_ALL:
1903 case MATCHCRITERIA_UNREAD:
1904 case MATCHCRITERIA_NOT_UNREAD:
1905 case MATCHCRITERIA_NEW:
1906 case MATCHCRITERIA_NOT_NEW:
1907 case MATCHCRITERIA_MARKED:
1908 case MATCHCRITERIA_NOT_MARKED:
1909 case MATCHCRITERIA_DELETED:
1910 case MATCHCRITERIA_NOT_DELETED:
1911 case MATCHCRITERIA_REPLIED:
1912 case MATCHCRITERIA_NOT_REPLIED:
1913 case MATCHCRITERIA_FORWARDED:
1914 case MATCHCRITERIA_NOT_FORWARDED:
1915 case MATCHCRITERIA_LOCKED:
1916 case MATCHCRITERIA_NOT_LOCKED:
1917 case MATCHCRITERIA_SPAM:
1918 case MATCHCRITERIA_NOT_SPAM:
1919 case MATCHCRITERIA_HAS_ATTACHMENT:
1920 case MATCHCRITERIA_HAS_NO_ATTACHMENT:
1921 case MATCHCRITERIA_SIGNED:
1922 case MATCHCRITERIA_NOT_SIGNED:
1923 case MATCHCRITERIA_PARTIAL:
1924 case MATCHCRITERIA_NOT_PARTIAL:
1925 case MATCHCRITERIA_IGNORE_THREAD:
1926 case MATCHCRITERIA_NOT_IGNORE_THREAD:
1927 case MATCHCRITERIA_WATCH_THREAD:
1928 case MATCHCRITERIA_NOT_WATCH_THREAD:
1929 case MATCHCRITERIA_TAGGED:
1930 case MATCHCRITERIA_NOT_TAGGED:
1931 return g_strdup(criteria_str);
1932 case MATCHCRITERIA_TEST:
1933 case MATCHCRITERIA_NOT_TEST:
1934 quoted_expr = matcher_quote_str(matcher->expr);
1935 matcher_str = g_strdup_printf("%s \"%s\"",
1936 criteria_str, quoted_expr);
1937 g_free(quoted_expr);
1939 case MATCHCRITERIA_FOUND_IN_ADDRESSBOOK:
1940 case MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK:
1941 quoted_header = matcher_quote_str(matcher->header);
1942 quoted_expr = matcher_quote_str(matcher->expr);
1943 matcher_str = g_strdup_printf("%s \"%s\" in \"%s\"",
1944 criteria_str, quoted_header, quoted_expr);
1945 g_free(quoted_header);
1946 g_free(quoted_expr);
1950 matchtype_str = NULL;
1951 for (i = 0; i < sizeof matchparser_tab / sizeof matchparser_tab[0]; i++) {
1952 if (matchparser_tab[i].id == matcher->matchtype)
1953 matchtype_str = matchparser_tab[i].str;
1956 if (matchtype_str == NULL)
1959 switch (matcher->matchtype) {
1960 case MATCHTYPE_MATCH:
1961 case MATCHTYPE_MATCHCASE:
1962 case MATCHTYPE_REGEXP:
1963 case MATCHTYPE_REGEXPCASE:
1964 quoted_expr = matcher_quote_str(matcher->expr);
1965 if (matcher->header) {
1966 quoted_header = matcher_quote_str(matcher->header);
1967 matcher_str = g_strdup_printf
1968 ("%s \"%s\" %s \"%s\"",
1969 criteria_str, quoted_header,
1970 matchtype_str, quoted_expr);
1971 g_free(quoted_header);
1974 matcher_str = g_strdup_printf
1975 ("%s %s \"%s\"", criteria_str,
1976 matchtype_str, quoted_expr);
1977 g_free(quoted_expr);
1985 *\brief Convert a list of conditions to a string
1987 *\param matchers List of conditions
1989 *\return gchar * Newly allocated string
1991 gchar *matcherlist_to_string(const MatcherList *matchers)
1997 gchar *result = NULL;
1999 count = g_slist_length(matchers->matchers);
2000 vstr = g_new(gchar *, count + 1);
2002 for (l = matchers->matchers, cur_str = vstr; l != NULL;
2003 l = g_slist_next(l), cur_str ++) {
2004 *cur_str = matcherprop_to_string((MatcherProp *) l->data);
2005 if (*cur_str == NULL)
2010 if (matchers->bool_and)
2011 result = g_strjoinv(" & ", vstr);
2013 result = g_strjoinv(" | ", vstr);
2015 for (cur_str = vstr ; *cur_str != NULL ; cur_str ++)
2023 #define STRLEN_ZERO(s) ((s) ? strlen(s) : 0)
2024 #define STRLEN_DEFAULT(s,d) ((s) ? strlen(s) : STRLEN_ZERO(d))
2026 static void add_str_default(gchar ** dest,
2027 const gchar * s, const gchar * d)
2029 gchar quoted_str[4096];
2037 quote_cmd_argument(quoted_str, sizeof(quoted_str), str);
2038 strcpy(* dest, quoted_str);
2040 (* dest) += strlen(* dest);
2043 /* matching_build_command() - preferably cmd should be unescaped */
2045 *\brief Build the command-line to execute
2047 *\param cmd String with command-line specifiers
2048 *\param info Message info to use for command
2050 *\return gchar * Newly allocated string
2052 gchar *matching_build_command(const gchar *cmd, MsgInfo *info)
2054 const gchar *s = cmd;
2055 gchar *filename = NULL;
2056 gchar *processed_cmd;
2060 const gchar *const no_subject = _("(none)") ;
2061 const gchar *const no_from = _("(none)") ;
2062 const gchar *const no_to = _("(none)") ;
2063 const gchar *const no_cc = _("(none)") ;
2064 const gchar *const no_date = _("(none)") ;
2065 const gchar *const no_msgid = _("(none)") ;
2066 const gchar *const no_newsgroups = _("(none)") ;
2067 const gchar *const no_references = _("(none)") ;
2069 size = STRLEN_ZERO(cmd) + 1;
2070 while (*s != '\0') {
2077 case 's': /* subject */
2078 size += STRLEN_DEFAULT(info->subject, no_subject) - 2;
2080 case 'f': /* from */
2081 size += STRLEN_DEFAULT(info->from, no_from) - 2;
2084 size += STRLEN_DEFAULT(info->to, no_to) - 2;
2087 size += STRLEN_DEFAULT(info->cc, no_cc) - 2;
2089 case 'd': /* date */
2090 size += STRLEN_DEFAULT(info->date, no_date) - 2;
2092 case 'i': /* message-id */
2093 size += STRLEN_DEFAULT(info->msgid, no_msgid) - 2;
2095 case 'n': /* newsgroups */
2096 size += STRLEN_DEFAULT(info->newsgroups, no_newsgroups) - 2;
2098 case 'r': /* references */
2099 /* FIXME: using the inreplyto header for reference */
2100 size += STRLEN_DEFAULT(info->inreplyto, no_references) - 2;
2102 case 'F': /* file */
2103 if (filename == NULL)
2104 filename = folder_item_fetch_msg(info->folder, info->msgnum);
2106 if (filename == NULL) {
2107 g_warning("filename is not set");
2111 size += strlen(filename) - 2;
2120 /* as the string can be quoted, we double the result */
2123 processed_cmd = g_new0(gchar, size);
2127 while (*s != '\0') {
2135 case 's': /* subject */
2136 add_str_default(&p, info->subject,
2139 case 'f': /* from */
2140 add_str_default(&p, info->from,
2144 add_str_default(&p, info->to,
2148 add_str_default(&p, info->cc,
2151 case 'd': /* date */
2152 add_str_default(&p, info->date,
2155 case 'i': /* message-id */
2156 add_str_default(&p, info->msgid,
2159 case 'n': /* newsgroups */
2160 add_str_default(&p, info->newsgroups,
2163 case 'r': /* references */
2164 /* FIXME: using the inreplyto header for references */
2165 add_str_default(&p, info->inreplyto, no_references);
2167 case 'F': /* file */
2168 if (filename != NULL)
2169 add_str_default(&p, filename, NULL);
2188 return processed_cmd;
2190 #undef STRLEN_DEFAULT
2193 /* ************************************************************ */
2197 *\brief Write filtering list to file
2200 *\param prefs_filtering List of filtering conditions
2202 static int prefs_filtering_write(FILE *fp, GSList *prefs_filtering)
2206 for (cur = prefs_filtering; cur != NULL; cur = cur->next) {
2207 gchar *filtering_str = NULL;
2208 gchar *tmp_name = NULL;
2209 FilteringProp *prop = NULL;
2211 if (NULL == (prop = (FilteringProp *) cur->data))
2214 if (NULL == (filtering_str = filteringprop_to_string(prop)))
2217 if (prop->enabled) {
2218 if (fputs("enabled ", fp) == EOF) {
2219 FILE_OP_ERROR("filtering config", "fputs");
2223 if (fputs("disabled ", fp) == EOF) {
2224 FILE_OP_ERROR("filtering config", "fputs");
2229 if (fputs("rulename \"", fp) == EOF) {
2230 FILE_OP_ERROR("filtering config", "fputs");
2231 g_free(filtering_str);
2234 tmp_name = prop->name;
2235 while (tmp_name && *tmp_name != '\0') {
2236 if (*tmp_name != '"') {
2237 if (fputc(*tmp_name, fp) == EOF) {
2238 FILE_OP_ERROR("filtering config", "fputs || fputc");
2239 g_free(filtering_str);
2242 } else if (*tmp_name == '"') {
2243 if (fputc('\\', fp) == EOF ||
2244 fputc('"', fp) == EOF) {
2245 FILE_OP_ERROR("filtering config", "fputs || fputc");
2246 g_free(filtering_str);
2252 if (fputs("\" ", fp) == EOF) {
2253 FILE_OP_ERROR("filtering config", "fputs");
2254 g_free(filtering_str);
2258 if (prop->account_id != 0) {
2261 tmp = g_strdup_printf("account %d ", prop->account_id);
2262 if (fputs(tmp, fp) == EOF) {
2263 FILE_OP_ERROR("filtering config", "fputs");
2270 if(fputs(filtering_str, fp) == EOF ||
2271 fputc('\n', fp) == EOF) {
2272 FILE_OP_ERROR("filtering config", "fputs || fputc");
2273 g_free(filtering_str);
2276 g_free(filtering_str);
2282 typedef struct _NodeLoopData {
2288 *\brief Write matchers from a folder item
2290 *\param node Node with folder info
2291 *\param data File pointer
2293 *\return gboolean FALSE
2295 static gboolean prefs_matcher_write_func(GNode *node, gpointer d)
2298 NodeLoopData *data = (NodeLoopData *)d;
2300 GSList *prefs_filtering;
2303 /* prevent warning */
2304 if (item->path == NULL)
2306 id = folder_item_get_identifier(item);
2309 prefs_filtering = item->prefs->processing;
2311 if (prefs_filtering != NULL) {
2312 if (fprintf(data->fp, "[%s]\n", id) < 0) {
2316 if (prefs_filtering_write(data->fp, prefs_filtering) < 0) {
2320 if (fputc('\n', data->fp) == EOF) {
2332 *\brief Save matchers from folder items
2336 static int prefs_matcher_save(FILE *fp)
2344 for (cur = folder_get_list() ; cur != NULL ; cur = g_list_next(cur)) {
2347 folder = (Folder *) cur->data;
2348 g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
2349 prefs_matcher_write_func, &data);
2352 if (data.error == TRUE)
2355 /* pre global rules */
2356 if (fprintf(fp, "[preglobal]\n") < 0 ||
2357 prefs_filtering_write(fp, pre_global_processing) < 0 ||
2358 fputc('\n', fp) == EOF)
2361 /* post global rules */
2362 if (fprintf(fp, "[postglobal]\n") < 0 ||
2363 prefs_filtering_write(fp, post_global_processing) < 0 ||
2364 fputc('\n', fp) == EOF)
2367 /* filtering rules */
2368 if (fprintf(fp, "[filtering]\n") < 0 ||
2369 prefs_filtering_write(fp, filtering_rules) < 0 ||
2370 fputc('\n', fp) == EOF)
2377 *\brief Write filtering / matcher configuration file
2379 void prefs_matcher_write_config(void)
2384 debug_print("Writing matcher configuration...\n");
2386 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
2389 if ((pfile = prefs_write_open(rcpath)) == NULL) {
2390 g_warning("failed to write configuration to file\n");
2397 if (prefs_matcher_save(pfile->fp) < 0) {
2398 g_warning("failed to write configuration to file\n");
2399 prefs_file_close_revert(pfile);
2400 } else if (prefs_file_close(pfile) < 0) {
2401 g_warning("failed to save configuration to file\n");
2405 /* ******************************************************************* */
2407 static void matcher_add_rulenames(const gchar *rcpath)
2409 gchar *newpath = g_strconcat(rcpath, ".new", NULL);
2410 FILE *src = g_fopen(rcpath, "rb");
2411 FILE *dst = g_fopen(newpath, "wb");
2412 gchar buf[BUFFSIZE];
2429 while (fgets (buf, sizeof(buf), src) != NULL) {
2430 if (strlen(buf) > 2 && buf[0] != '['
2431 && strncmp(buf, "rulename \"", 10)
2432 && strncmp(buf, "enabled rulename \"", 18)
2433 && strncmp(buf, "disabled rulename \"", 18)) {
2434 r = fwrite("enabled rulename \"\" ",
2435 strlen("enabled rulename \"\" "), 1, dst);
2437 r = fwrite(buf, strlen(buf), 1, dst);
2441 move_file(newpath, rcpath, TRUE);
2446 *\brief Read matcher configuration
2448 void prefs_matcher_read_config(void)
2451 gchar *rc_old_format;
2454 create_matchparser_hashtab();
2455 prefs_filtering_clear();
2457 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MATCHER_RC, NULL);
2458 rc_old_format = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MATCHER_RC,
2459 ".pre_names", NULL);
2461 if (!is_file_exist(rc_old_format) && is_file_exist(rcpath)) {
2462 /* backup file with no rules names, in case
2463 * anything goes wrong */
2464 copy_file(rcpath, rc_old_format, FALSE);
2465 /* now hack the file in order to have it to the new format */
2466 matcher_add_rulenames(rcpath);
2469 g_free(rc_old_format);
2471 f = g_fopen(rcpath, "rb");
2475 matcher_parser_start_parsing(f);
2476 fclose(matcher_parserin);
2479 /* previous version compatibility */
2481 /* g_print("reading filtering\n"); */
2482 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
2483 FILTERING_RC, NULL);
2484 f = g_fopen(rcpath, "rb");
2488 matcher_parser_start_parsing(f);
2489 fclose(matcher_parserin);
2492 /* g_print("reading scoring\n"); */
2493 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
2495 f = g_fopen(rcpath, "rb");
2499 matcher_parser_start_parsing(f);
2500 fclose(matcher_parserin);