2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 2002-2012 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/>.
22 #include "claws-features.h"
26 #include <glib/gi18n.h>
38 #include "procheader.h"
40 #include "matcher_parser.h"
41 #include "prefs_gtk.h"
42 #include "addr_compl.h"
44 #include "quoted-printable.h"
47 #include "prefs_common.h"
52 *\brief Keyword lookup element
55 gint id; /*!< keyword id */
56 gchar *str; /*!< keyword */
58 typedef struct _MatchParser MatchParser;
61 *\brief Table with strings and ids used by the lexer and
62 * the parser. New keywords can be added here.
64 static const MatchParser matchparser_tab[] = {
66 {MATCHCRITERIA_ALL, "all"},
67 {MATCHCRITERIA_UNREAD, "unread"},
68 {MATCHCRITERIA_NOT_UNREAD, "~unread"},
69 {MATCHCRITERIA_NEW, "new"},
70 {MATCHCRITERIA_NOT_NEW, "~new"},
71 {MATCHCRITERIA_MARKED, "marked"},
72 {MATCHCRITERIA_NOT_MARKED, "~marked"},
73 {MATCHCRITERIA_DELETED, "deleted"},
74 {MATCHCRITERIA_NOT_DELETED, "~deleted"},
75 {MATCHCRITERIA_REPLIED, "replied"},
76 {MATCHCRITERIA_NOT_REPLIED, "~replied"},
77 {MATCHCRITERIA_FORWARDED, "forwarded"},
78 {MATCHCRITERIA_NOT_FORWARDED, "~forwarded"},
79 {MATCHCRITERIA_LOCKED, "locked"},
80 {MATCHCRITERIA_NOT_LOCKED, "~locked"},
81 {MATCHCRITERIA_COLORLABEL, "colorlabel"},
82 {MATCHCRITERIA_NOT_COLORLABEL, "~colorlabel"},
83 {MATCHCRITERIA_IGNORE_THREAD, "ignore_thread"},
84 {MATCHCRITERIA_NOT_IGNORE_THREAD, "~ignore_thread"},
85 {MATCHCRITERIA_WATCH_THREAD, "watch_thread"},
86 {MATCHCRITERIA_NOT_WATCH_THREAD, "~watch_thread"},
87 {MATCHCRITERIA_SPAM, "spam"},
88 {MATCHCRITERIA_NOT_SPAM, "~spam"},
89 {MATCHCRITERIA_HAS_ATTACHMENT, "has_attachment"},
90 {MATCHCRITERIA_HAS_NO_ATTACHMENT, "~has_attachment"},
91 {MATCHCRITERIA_SIGNED, "signed"},
92 {MATCHCRITERIA_NOT_SIGNED, "~signed"},
95 {MATCHCRITERIA_SUBJECT, "subject"},
96 {MATCHCRITERIA_NOT_SUBJECT, "~subject"},
97 {MATCHCRITERIA_FROM, "from"},
98 {MATCHCRITERIA_NOT_FROM, "~from"},
99 {MATCHCRITERIA_TO, "to"},
100 {MATCHCRITERIA_NOT_TO, "~to"},
101 {MATCHCRITERIA_CC, "cc"},
102 {MATCHCRITERIA_NOT_CC, "~cc"},
103 {MATCHCRITERIA_TO_OR_CC, "to_or_cc"},
104 {MATCHCRITERIA_NOT_TO_AND_NOT_CC, "~to_or_cc"},
105 {MATCHCRITERIA_TAG, "tag"},
106 {MATCHCRITERIA_NOT_TAG, "~tag"},
107 {MATCHCRITERIA_TAGGED, "tagged"},
108 {MATCHCRITERIA_NOT_TAGGED, "~tagged"},
109 {MATCHCRITERIA_AGE_GREATER, "age_greater"},
110 {MATCHCRITERIA_AGE_LOWER, "age_lower"},
111 {MATCHCRITERIA_NEWSGROUPS, "newsgroups"},
112 {MATCHCRITERIA_NOT_NEWSGROUPS, "~newsgroups"},
113 {MATCHCRITERIA_INREPLYTO, "inreplyto"},
114 {MATCHCRITERIA_NOT_INREPLYTO, "~inreplyto"},
115 {MATCHCRITERIA_REFERENCES, "references"},
116 {MATCHCRITERIA_NOT_REFERENCES, "~references"},
117 {MATCHCRITERIA_SCORE_GREATER, "score_greater"},
118 {MATCHCRITERIA_SCORE_LOWER, "score_lower"},
119 {MATCHCRITERIA_SCORE_EQUAL, "score_equal"},
120 {MATCHCRITERIA_PARTIAL, "partial"},
121 {MATCHCRITERIA_NOT_PARTIAL, "~partial"},
122 {MATCHCRITERIA_FOUND_IN_ADDRESSBOOK, "found_in_addressbook"},
123 {MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK, "~found_in_addressbook"},
125 {MATCHCRITERIA_SIZE_GREATER, "size_greater"},
126 {MATCHCRITERIA_SIZE_SMALLER, "size_smaller"},
127 {MATCHCRITERIA_SIZE_EQUAL, "size_equal"},
129 /* content have to be read */
130 {MATCHCRITERIA_HEADER, "header"},
131 {MATCHCRITERIA_NOT_HEADER, "~header"},
132 {MATCHCRITERIA_HEADERS_PART, "headers_part"},
133 {MATCHCRITERIA_NOT_HEADERS_PART, "~headers_part"},
134 {MATCHCRITERIA_MESSAGE, "message"},
135 {MATCHCRITERIA_NOT_MESSAGE, "~message"},
136 {MATCHCRITERIA_BODY_PART, "body_part"},
137 {MATCHCRITERIA_NOT_BODY_PART, "~body_part"},
138 {MATCHCRITERIA_TEST, "test"},
139 {MATCHCRITERIA_NOT_TEST, "~test"},
142 {MATCHTYPE_MATCHCASE, "matchcase"},
143 {MATCHTYPE_MATCH, "match"},
144 {MATCHTYPE_REGEXPCASE, "regexpcase"},
145 {MATCHTYPE_REGEXP, "regexp"},
148 {MATCHACTION_SCORE, "score"}, /* for backward compatibility */
149 {MATCHACTION_MOVE, "move"},
150 {MATCHACTION_COPY, "copy"},
151 {MATCHACTION_DELETE, "delete"},
152 {MATCHACTION_MARK, "mark"},
153 {MATCHACTION_UNMARK, "unmark"},
154 {MATCHACTION_LOCK, "lock"},
155 {MATCHACTION_UNLOCK, "unlock"},
156 {MATCHACTION_MARK_AS_READ, "mark_as_read"},
157 {MATCHACTION_MARK_AS_UNREAD, "mark_as_unread"},
158 {MATCHACTION_MARK_AS_SPAM, "mark_as_spam"},
159 {MATCHACTION_MARK_AS_HAM, "mark_as_ham"},
160 {MATCHACTION_FORWARD, "forward"},
161 {MATCHACTION_FORWARD_AS_ATTACHMENT, "forward_as_attachment"},
162 {MATCHACTION_EXECUTE, "execute"},
163 {MATCHACTION_COLOR, "color"},
164 {MATCHACTION_REDIRECT, "redirect"},
165 {MATCHACTION_CHANGE_SCORE, "change_score"},
166 {MATCHACTION_SET_SCORE, "set_score"},
167 {MATCHACTION_STOP, "stop"},
168 {MATCHACTION_HIDE, "hide"},
169 {MATCHACTION_IGNORE, "ignore"},
170 {MATCHACTION_WATCH, "watch"},
171 {MATCHACTION_ADD_TO_ADDRESSBOOK, "add_to_addressbook"},
172 {MATCHACTION_SET_TAG, "set_tag"},
173 {MATCHACTION_UNSET_TAG, "unset_tag"},
174 {MATCHACTION_CLEAR_TAGS, "clear_tags"},
183 extern gboolean debug_filtering_session;
186 *\brief Look up table with keywords defined in \sa matchparser_tab
188 static GHashTable *matchparser_hashtab;
191 *\brief Translate keyword id to keyword string
193 *\param id Id of keyword
195 *\return const gchar * Keyword
197 const gchar *get_matchparser_tab_str(gint id)
201 for (i = 0; i < sizeof matchparser_tab / sizeof matchparser_tab[0]; i++) {
202 if (matchparser_tab[i].id == id)
203 return matchparser_tab[i].str;
209 *\brief Create keyword lookup table
211 static void create_matchparser_hashtab(void)
215 if (matchparser_hashtab) return;
216 matchparser_hashtab = g_hash_table_new(g_str_hash, g_str_equal);
217 for (i = 0; i < sizeof matchparser_tab / sizeof matchparser_tab[0]; i++)
218 g_hash_table_insert(matchparser_hashtab,
219 matchparser_tab[i].str,
220 (gpointer) &matchparser_tab[i]);
224 *\brief Return a keyword id from a keyword string
226 *\param str Keyword string
228 *\return gint Keyword id
230 gint get_matchparser_tab_id(const gchar *str)
234 if (NULL != (res = g_hash_table_lookup(matchparser_hashtab, str))) {
240 /* **************** data structure allocation **************** */
243 *\brief Allocate a structure for a filtering / scoring
244 * "condition" (a matcher structure)
246 *\param criteria Criteria ID (MATCHCRITERIA_XXXX)
247 *\param header Header string (if criteria is MATCHCRITERIA_HEADER
248 or MATCHCRITERIA_FOUND_IN_ADDRESSBOOK)
249 *\param matchtype Type of action (MATCHTYPE_XXX)
250 *\param expr String value or expression to check
251 *\param value Integer value to check
253 *\return MatcherProp * Pointer to newly allocated structure
255 MatcherProp *matcherprop_new(gint criteria, const gchar *header,
256 gint matchtype, const gchar *expr,
261 prop = g_new0(MatcherProp, 1);
262 prop->criteria = criteria;
263 prop->header = header != NULL ? g_strdup(header) : NULL;
265 prop->expr = expr != NULL ? g_strdup(expr) : NULL;
267 prop->matchtype = matchtype;
276 *\brief Free a matcher structure
278 *\param prop Pointer to matcher structure allocated with
281 void matcherprop_free(MatcherProp *prop)
284 g_free(prop->header);
285 if (prop->preg != NULL) {
293 *\brief Copy a matcher structure
295 *\param src Matcher structure to copy
297 *\return MatcherProp * Pointer to newly allocated matcher structure
299 MatcherProp *matcherprop_copy(const MatcherProp *src)
301 MatcherProp *prop = g_new0(MatcherProp, 1);
303 prop->criteria = src->criteria;
304 prop->header = src->header ? g_strdup(src->header) : NULL;
305 prop->expr = src->expr ? g_strdup(src->expr) : NULL;
306 prop->matchtype = src->matchtype;
308 prop->preg = NULL; /* will be re-evaluated */
309 prop->value = src->value;
310 prop->error = src->error;
314 /* ************** match ******************************/
316 static gboolean match_with_addresses_in_addressbook
317 (MatcherProp *prop, GSList *address_list, gint type,
318 gchar* folderpath, gint match)
321 gboolean found = FALSE;
324 cm_return_val_if_fail(address_list != NULL, FALSE);
326 debug_print("match_with_addresses_in_addressbook(%d, %s)\n",
327 g_slist_length(address_list), folderpath?folderpath:"(null)");
329 if (folderpath == NULL ||
330 strcasecmp(folderpath, "Any") == 0 ||
336 start_address_completion(path);
338 for (walk = address_list; walk != NULL; walk = walk->next) {
339 /* exact matching of email address */
340 guint num_addr = complete_address(walk->data);
343 /* skip first item (this is the search string itself) */
345 for (; i < num_addr && !found; i++) {
346 gchar *addr = get_complete_address(i);
347 extract_address(addr);
348 if (strcasecmp(addr, walk->data) == 0) {
352 if (debug_filtering_session
353 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
354 log_print(LOG_DEBUG_FILTERING,
355 "address [ %s ] matches\n",
356 (gchar *)walk->data);
363 if (debug_filtering_session
364 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH
366 log_print(LOG_DEBUG_FILTERING,
367 "address [ %s ] does NOT match\n",
368 (gchar *)walk->data);
372 if (match == MATCH_ALL) {
373 /* if matching all addresses, stop if one doesn't match */
376 if (debug_filtering_session
377 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
378 log_print(LOG_DEBUG_FILTERING,
379 "not all address match (matching all)\n");
383 } else if (match == MATCH_ANY) {
384 /* if matching any address, stop if one does match */
387 if (debug_filtering_session
388 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
389 log_print(LOG_DEBUG_FILTERING,
390 "at least one address matches (matching any)\n");
395 /* MATCH_ONE: there should be only one loop iteration */
398 end_address_completion();
404 *\brief Find out if a string matches a condition
406 *\param prop Matcher structure
407 *\param str String to check
409 *\return gboolean TRUE if str matches the condition in the
412 static gboolean matcherprop_string_match(MatcherProp *prop, const gchar *str,
413 const gchar *debug_context)
417 gboolean ret = FALSE;
418 gboolean should_free = FALSE;
422 if (prop->matchtype == MATCHTYPE_REGEXPCASE ||
423 prop->matchtype == MATCHTYPE_MATCHCASE) {
424 str1 = g_utf8_casefold(str, -1);
425 down_expr = g_utf8_casefold(prop->expr, -1);
429 down_expr = (gchar *)prop->expr;
433 switch (prop->matchtype) {
434 case MATCHTYPE_REGEXPCASE:
435 case MATCHTYPE_REGEXP:
437 if (!prop->preg && (prop->error == 0)) {
438 prop->preg = g_new0(regex_t, 1);
439 /* if regexp then don't use the escaped string */
440 if (regcomp(prop->preg, down_expr,
441 REG_NOSUB | REG_EXTENDED
442 | ((prop->matchtype == MATCHTYPE_REGEXPCASE)
443 ? REG_ICASE : 0)) != 0) {
449 if (prop->preg == NULL) {
454 if (regexec(prop->preg, str1, 0, NULL, 0) == 0)
460 if (debug_filtering_session
461 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
462 gchar *stripped = g_strdup(str);
464 strretchomp(stripped);
466 log_print(LOG_DEBUG_FILTERING,
467 "%s value [ %s ] matches regular expression [ %s ] (%s)\n",
468 debug_context, stripped, prop->expr,
469 prop->matchtype == MATCHTYPE_REGEXP ? _("Case sensitive"):_("Case insensitive"));
471 log_print(LOG_DEBUG_FILTERING,
472 "%s value [ %s ] does NOT match regular expression [ %s ] (%s)\n",
473 debug_context, stripped, prop->expr,
474 prop->matchtype == MATCHTYPE_REGEXP ? _("Case sensitive"):_("Case insensitive"));
480 case MATCHTYPE_MATCHCASE:
481 case MATCHTYPE_MATCH:
482 ret = (strstr(str1, down_expr) != NULL);
485 if (debug_filtering_session
486 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
487 gchar *stripped = g_strdup(str);
489 strretchomp(stripped);
491 log_print(LOG_DEBUG_FILTERING,
492 "%s value [ %s ] contains [ %s ] (%s)\n",
493 debug_context, stripped, prop->expr,
494 prop->matchtype == MATCHTYPE_MATCH ? _("Case sensitive"):_("Case insensitive"));
496 log_print(LOG_DEBUG_FILTERING,
497 "%s value [ %s ] does NOT contain [ %s ] (%s)\n",
498 debug_context, stripped, prop->expr,
499 prop->matchtype == MATCHTYPE_MATCH ? _("Case sensitive"):_("Case insensitive"));
518 *\brief Find out if a tag matches a condition
520 *\param prop Matcher structure
521 *\param msginfo message to check
523 *\return gboolean TRUE if msginfo matches the condition in the
526 static gboolean matcherprop_tag_match(MatcherProp *prop, MsgInfo *msginfo,
527 const gchar *debug_context)
529 gboolean ret = FALSE;
532 if (msginfo == NULL || msginfo->tags == NULL)
535 for (cur = msginfo->tags; cur; cur = cur->next) {
536 const gchar *str = tags_get_tag(GPOINTER_TO_INT(cur->data));
539 if (matcherprop_string_match(prop, str, debug_context)) {
548 *\brief Find out if the string-ed list matches a condition
550 *\param prop Matcher structure
551 *\param list GSList of strings to check
553 *\return gboolean TRUE if str matches the condition in the
556 static gboolean matcherprop_list_match(MatcherProp *prop, const GSList *list,
557 const gchar *debug_context)
561 for(cur = list; cur != NULL; cur = cur->next) {
562 if (matcherprop_string_match(prop, (gchar *)cur->data, debug_context))
568 static gboolean matcherprop_header_line_match(MatcherProp *prop, const gchar *hdr,
569 const gchar *str, const gchar *debug_context)
572 gboolean res = FALSE;
574 if (hdr == NULL || str == NULL)
577 line = g_strdup_printf("%s %s", hdr, str);
578 res = matcherprop_string_match(prop, line, debug_context);
585 typedef struct _thread_data {
592 static void *matcher_test_thread(void *data)
594 thread_data *td = (thread_data *)data;
597 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
598 pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
600 result = system(td->cmd);
601 td->done = TRUE; /* let the caller thread join() */
602 return GINT_TO_POINTER(result);
607 *\brief Execute a command defined in the matcher structure
609 *\param prop Pointer to matcher structure
610 *\param info Pointer to message info structure
612 *\return gboolean TRUE if command was executed succesfully
614 static gboolean matcherprop_match_test(const MatcherProp *prop,
623 thread_data *td = g_new0(thread_data, 1);
625 time_t start_time = time(NULL);
628 file = procmsg_get_message_file(info);
637 cmd = matching_build_command(prop->expr, info);
647 if (debug_filtering_session
648 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
649 log_print(LOG_DEBUG_FILTERING,
650 "starting threaded command [ %s ]\n",
656 if (pthread_attr_init(&pta) != 0 ||
657 pthread_attr_setdetachstate(&pta, PTHREAD_CREATE_JOINABLE) != 0 ||
658 pthread_create(&pt, &pta, matcher_test_thread, td) != 0)
659 retval = system(cmd);
661 debug_print("waiting for test thread\n");
663 /* don't let the interface freeze while waiting */
665 if (time(NULL) - start_time > 30) {
671 pthread_join(pt, &res);
672 retval = GPOINTER_TO_INT(res);
673 debug_print(" test thread returned %d\n", retval);
678 if (debug_filtering_session
679 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
680 log_print(LOG_DEBUG_FILTERING,
681 "starting synchronous command [ %s ]\n",
685 retval = system(cmd);
687 debug_print("Command exit code: %d\n", retval);
690 if (debug_filtering_session
691 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
692 log_print(LOG_DEBUG_FILTERING,
693 "command returned [ %d ]\n",
698 return (retval == 0);
702 *\brief Check if a message matches the condition in a matcher
705 *\param prop Pointer to matcher structure
706 *\param info Pointer to message info
708 *\return gboolean TRUE if a match
710 static gboolean matcherprop_match(MatcherProp *prop,
715 switch(prop->criteria) {
716 case MATCHCRITERIA_ALL:
718 case MATCHCRITERIA_UNREAD:
719 return MSG_IS_UNREAD(info->flags);
720 case MATCHCRITERIA_NOT_UNREAD:
721 return !MSG_IS_UNREAD(info->flags);
722 case MATCHCRITERIA_NEW:
723 return MSG_IS_NEW(info->flags);
724 case MATCHCRITERIA_NOT_NEW:
725 return !MSG_IS_NEW(info->flags);
726 case MATCHCRITERIA_MARKED:
727 return MSG_IS_MARKED(info->flags);
728 case MATCHCRITERIA_NOT_MARKED:
729 return !MSG_IS_MARKED(info->flags);
730 case MATCHCRITERIA_DELETED:
731 return MSG_IS_DELETED(info->flags);
732 case MATCHCRITERIA_NOT_DELETED:
733 return !MSG_IS_DELETED(info->flags);
734 case MATCHCRITERIA_REPLIED:
735 return MSG_IS_REPLIED(info->flags);
736 case MATCHCRITERIA_NOT_REPLIED:
737 return !MSG_IS_REPLIED(info->flags);
738 case MATCHCRITERIA_FORWARDED:
739 return MSG_IS_FORWARDED(info->flags);
740 case MATCHCRITERIA_NOT_FORWARDED:
741 return !MSG_IS_FORWARDED(info->flags);
742 case MATCHCRITERIA_LOCKED:
743 return MSG_IS_LOCKED(info->flags);
744 case MATCHCRITERIA_NOT_LOCKED:
745 return !MSG_IS_LOCKED(info->flags);
746 case MATCHCRITERIA_SPAM:
747 return MSG_IS_SPAM(info->flags);
748 case MATCHCRITERIA_NOT_SPAM:
749 return !MSG_IS_SPAM(info->flags);
750 case MATCHCRITERIA_HAS_ATTACHMENT:
751 return MSG_IS_WITH_ATTACHMENT(info->flags);
752 case MATCHCRITERIA_HAS_NO_ATTACHMENT:
753 return !MSG_IS_WITH_ATTACHMENT(info->flags);
754 case MATCHCRITERIA_SIGNED:
755 return MSG_IS_SIGNED(info->flags);
756 case MATCHCRITERIA_NOT_SIGNED:
757 return !MSG_IS_SIGNED(info->flags);
758 case MATCHCRITERIA_COLORLABEL:
760 gint color = MSG_GET_COLORLABEL_VALUE(info->flags);
761 gboolean ret = (color == prop->value);
764 if (debug_filtering_session
765 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
767 log_print(LOG_DEBUG_FILTERING,
768 "message color value [ %d ] matches color value [ %d ]\n",
771 log_print(LOG_DEBUG_FILTERING,
772 "message color value [ %d ] does NOT match color value [ %d ]\n",
778 case MATCHCRITERIA_NOT_COLORLABEL:
780 gint color = MSG_GET_COLORLABEL_VALUE(info->flags);
781 gboolean ret = (color != prop->value);
784 if (debug_filtering_session
785 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
787 log_print(LOG_DEBUG_FILTERING,
788 "message color value [ %d ] matches color value [ %d ]\n",
791 log_print(LOG_DEBUG_FILTERING,
792 "message color value [ %d ] does NOT match color value [ %d ]\n",
798 case MATCHCRITERIA_IGNORE_THREAD:
799 return MSG_IS_IGNORE_THREAD(info->flags);
800 case MATCHCRITERIA_NOT_IGNORE_THREAD:
801 return !MSG_IS_IGNORE_THREAD(info->flags);
802 case MATCHCRITERIA_WATCH_THREAD:
803 return MSG_IS_WATCH_THREAD(info->flags);
804 case MATCHCRITERIA_NOT_WATCH_THREAD:
805 return !MSG_IS_WATCH_THREAD(info->flags);
806 case MATCHCRITERIA_SUBJECT:
807 return matcherprop_string_match(prop, info->subject,
808 prefs_common_translated_header_name("Subject:"));
809 case MATCHCRITERIA_NOT_SUBJECT:
810 return !matcherprop_string_match(prop, info->subject,
811 prefs_common_translated_header_name("Subject:"));
812 case MATCHCRITERIA_FROM:
813 case MATCHCRITERIA_NOT_FROM:
818 context = g_strdup_printf(_("%s header"), prefs_common_translated_header_name("From:"));
819 ret = matcherprop_string_match(prop, info->from, context);
821 return (prop->criteria == MATCHCRITERIA_FROM)? ret : !ret;
823 case MATCHCRITERIA_TO:
824 case MATCHCRITERIA_NOT_TO:
829 context = g_strdup_printf(_("%s header"), prefs_common_translated_header_name("To:"));
830 ret = matcherprop_string_match(prop, info->to, context);
832 return (prop->criteria == MATCHCRITERIA_TO)? ret : !ret;
834 case MATCHCRITERIA_CC:
835 case MATCHCRITERIA_NOT_CC:
840 context = g_strdup_printf(_("%s header"), prefs_common_translated_header_name("Cc:"));
841 ret = matcherprop_string_match(prop, info->cc, context);
843 return (prop->criteria == MATCHCRITERIA_CC)? ret : !ret;
845 case MATCHCRITERIA_TO_OR_CC:
847 gchar *context1, *context2;
850 context1 = g_strdup_printf(_("%s header"), prefs_common_translated_header_name("To:"));
851 context2 = g_strdup_printf(_("%s header"), prefs_common_translated_header_name("Cc:"));
852 ret = matcherprop_string_match(prop, info->to, context1)
853 || matcherprop_string_match(prop, info->cc, context2);
858 case MATCHCRITERIA_NOT_TO_AND_NOT_CC:
860 gchar *context1, *context2;
863 context1 = g_strdup_printf(_("%s header"), prefs_common_translated_header_name("To:"));
864 context2 = g_strdup_printf(_("%s header"), prefs_common_translated_header_name("Cc:"));
865 ret = !(matcherprop_string_match(prop, info->to, context1)
866 || matcherprop_string_match(prop, info->cc, context2));
871 case MATCHCRITERIA_TAG:
872 case MATCHCRITERIA_NOT_TAG:
876 ret = matcherprop_tag_match(prop, info, _("Tag"));
877 return (prop->criteria == MATCHCRITERIA_TAG)? ret : !ret;
879 case MATCHCRITERIA_TAGGED:
880 case MATCHCRITERIA_NOT_TAGGED:
884 ret = (info->tags != NULL);
885 return (prop->criteria == MATCHCRITERIA_TAGGED)? ret : !ret;
887 case MATCHCRITERIA_AGE_GREATER:
893 age = ((t - info->date_t) / (60 * 60 * 24));
894 ret = (age >= prop->value);
897 if (debug_filtering_session
898 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
900 log_print(LOG_DEBUG_FILTERING,
901 "message age [ %d ] is greater than [ %d ]\n",
904 log_print(LOG_DEBUG_FILTERING,
905 "message age [ %d ] is not greater than [ %d ]\n",
911 case MATCHCRITERIA_AGE_LOWER:
917 age = ((t - info->date_t) / (60 * 60 * 24));
918 ret = (age < prop->value);
921 if (debug_filtering_session
922 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
924 log_print(LOG_DEBUG_FILTERING,
925 "message age [ %d ] is lower than [ %d ]\n",
928 log_print(LOG_DEBUG_FILTERING,
929 "message age [ %d ] is not lower than [ %d ]\n",
935 case MATCHCRITERIA_SCORE_GREATER:
937 gboolean ret = (info->score > prop->value);
940 if (debug_filtering_session
941 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
943 log_print(LOG_DEBUG_FILTERING,
944 "message score [ %d ] is greater than [ %d ]\n",
945 info->score, prop->value);
947 log_print(LOG_DEBUG_FILTERING,
948 "message score [ %d ] is not greater than [ %d ]\n",
949 info->score, prop->value);
954 case MATCHCRITERIA_SCORE_LOWER:
956 gboolean ret = (info->score < prop->value);
959 if (debug_filtering_session
960 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
962 log_print(LOG_DEBUG_FILTERING,
963 "message score [ %d ] is lower than [ %d ]\n",
964 info->score, prop->value);
966 log_print(LOG_DEBUG_FILTERING,
967 "message score [ %d ] is not lower than [ %d ]\n",
968 info->score, prop->value);
973 case MATCHCRITERIA_SCORE_EQUAL:
975 gboolean ret = (info->score == prop->value);
978 if (debug_filtering_session
979 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
981 log_print(LOG_DEBUG_FILTERING,
982 "message score [ %d ] is equal to [ %d ]\n",
983 info->score, prop->value);
985 log_print(LOG_DEBUG_FILTERING,
986 "message score [ %d ] is not equal to [ %d ]\n",
987 info->score, prop->value);
992 case MATCHCRITERIA_SIZE_GREATER:
994 /* FIXME: info->size is a goffset */
995 gboolean ret = (info->size > (goffset) prop->value);
998 if (debug_filtering_session
999 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
1001 log_print(LOG_DEBUG_FILTERING,
1002 "message size is greater than [ %d ]\n",
1005 log_print(LOG_DEBUG_FILTERING,
1006 "message size is not greater than [ %d ]\n",
1012 case MATCHCRITERIA_SIZE_SMALLER:
1014 /* FIXME: info->size is a goffset */
1015 gboolean ret = (info->size < (goffset) prop->value);
1018 if (debug_filtering_session
1019 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
1021 log_print(LOG_DEBUG_FILTERING,
1022 "message size is smaller than [ %d ]\n",
1025 log_print(LOG_DEBUG_FILTERING,
1026 "message size is not smaller than [ %d ]\n",
1032 case MATCHCRITERIA_SIZE_EQUAL:
1034 /* FIXME: info->size is a goffset */
1035 gboolean ret = (info->size == (goffset) prop->value);
1038 if (debug_filtering_session
1039 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
1041 log_print(LOG_DEBUG_FILTERING,
1042 "message size is equal to [ %d ]\n",
1045 log_print(LOG_DEBUG_FILTERING,
1046 "message size is not equal to [ %d ]\n",
1052 case MATCHCRITERIA_PARTIAL:
1054 /* FIXME: info->size is a goffset */
1055 gboolean ret = (info->total_size != 0 && info->size != (goffset)info->total_size);
1058 if (debug_filtering_session
1059 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
1061 log_print(LOG_DEBUG_FILTERING,
1062 "message is partially downloaded, size is less than total size [ %d ])\n",
1065 log_print(LOG_DEBUG_FILTERING,
1066 "message is not partially downloaded\n");
1071 case MATCHCRITERIA_NOT_PARTIAL:
1073 /* FIXME: info->size is a goffset */
1074 gboolean ret = (info->total_size == 0 || info->size == (goffset)info->total_size);
1077 if (debug_filtering_session
1078 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
1080 log_print(LOG_DEBUG_FILTERING,
1081 "message is not partially downloaded\n");
1083 log_print(LOG_DEBUG_FILTERING,
1084 "message is partially downloaded, size is less than total size [ %d ])\n",
1090 case MATCHCRITERIA_NEWSGROUPS:
1091 case MATCHCRITERIA_NOT_NEWSGROUPS:
1096 context = g_strdup_printf(_("%s header"),
1097 prefs_common_translated_header_name("Newsgroups:"));
1098 ret = matcherprop_string_match(prop, info->newsgroups, context);
1100 return (prop->criteria == MATCHCRITERIA_NEWSGROUPS)? ret : !ret;
1102 case MATCHCRITERIA_INREPLYTO:
1103 case MATCHCRITERIA_NOT_INREPLYTO:
1108 context = g_strdup_printf(_("%s header"),
1109 prefs_common_translated_header_name("In-Reply-To:"));
1110 ret = matcherprop_string_match(prop, info->inreplyto, context);
1112 return (prop->criteria == MATCHCRITERIA_INREPLYTO)? ret : !ret;
1114 case MATCHCRITERIA_REFERENCES:
1115 case MATCHCRITERIA_NOT_REFERENCES:
1120 context = g_strdup_printf(_("%s header"),
1121 prefs_common_translated_header_name("References:"));
1122 ret = matcherprop_list_match(prop, info->references, context);
1124 return (prop->criteria == MATCHCRITERIA_REFERENCES)? ret : !ret;
1126 case MATCHCRITERIA_TEST:
1127 return matcherprop_match_test(prop, info);
1128 case MATCHCRITERIA_NOT_TEST:
1129 return !matcherprop_match_test(prop, info);
1135 /* ********************* MatcherList *************************** */
1138 *\brief Create a new list of matchers
1140 *\param matchers List of matcher structures
1141 *\param bool_and Operator
1143 *\return MatcherList * New list
1145 MatcherList *matcherlist_new(GSList *matchers, gboolean bool_and)
1149 cond = g_new0(MatcherList, 1);
1151 cond->matchers = matchers;
1152 cond->bool_and = bool_and;
1158 *\brief Frees a list of matchers
1160 *\param cond List of matchers
1162 void matcherlist_free(MatcherList *cond)
1166 cm_return_if_fail(cond);
1167 for (l = cond->matchers ; l != NULL ; l = g_slist_next(l)) {
1168 matcherprop_free((MatcherProp *) l->data);
1170 g_slist_free(cond->matchers);
1175 *\brief Skip all headers in a message file
1177 *\param fp Message file
1179 static void matcherlist_skip_headers(FILE *fp)
1181 gchar buf[BUFFSIZE];
1183 while (procheader_get_one_field(buf, sizeof(buf), fp, NULL) != -1)
1188 *\brief Check if a header matches a matcher condition
1190 *\param matcher Matcher structure to check header for
1191 *\param buf Header name
1193 *\return boolean TRUE if matching header
1195 static gboolean matcherprop_match_one_header(MatcherProp *matcher,
1198 gboolean result = FALSE;
1199 Header *header = NULL;
1201 switch (matcher->criteria) {
1202 case MATCHCRITERIA_HEADER:
1203 case MATCHCRITERIA_NOT_HEADER:
1204 header = procheader_parse_header(buf);
1207 if (procheader_headername_equal(header->name,
1209 if (matcher->criteria == MATCHCRITERIA_HEADER)
1210 result = matcherprop_string_match(matcher, header->body, _("header"));
1212 result = !matcherprop_string_match(matcher, header->body, _("header"));
1213 procheader_header_free(header);
1217 procheader_header_free(header);
1220 case MATCHCRITERIA_HEADERS_PART:
1221 case MATCHCRITERIA_MESSAGE:
1222 header = procheader_parse_header(buf);
1225 result = matcherprop_header_line_match(matcher,
1226 header->name, header->body, _("header line"));
1227 procheader_header_free(header);
1229 case MATCHCRITERIA_NOT_HEADERS_PART:
1230 case MATCHCRITERIA_NOT_MESSAGE:
1231 header = procheader_parse_header(buf);
1234 result = !matcherprop_header_line_match(matcher,
1235 header->name, header->body, _("header line"));
1236 procheader_header_free(header);
1238 case MATCHCRITERIA_FOUND_IN_ADDRESSBOOK:
1239 case MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK:
1241 GSList *address_list = NULL;
1242 gint match = MATCH_ONE;
1243 gboolean found = FALSE;
1245 /* how many address headers are me trying to mach? */
1246 if (strcasecmp(matcher->header, "Any") == 0)
1248 else if (strcasecmp(matcher->header, "All") == 0)
1251 if (match == MATCH_ONE) {
1252 /* matching one address header exactly, is that the right one? */
1253 header = procheader_parse_header(buf);
1255 !procheader_headername_equal(header->name, matcher->header))
1257 address_list = address_list_append(address_list, header->body);
1258 if (address_list == NULL)
1262 header = procheader_parse_header(buf);
1265 /* address header is one of the headers we have to match when checking
1266 for any address header or all address headers? */
1267 if (procheader_headername_equal(header->name, "From") ||
1268 procheader_headername_equal(header->name, "To") ||
1269 procheader_headername_equal(header->name, "Cc") ||
1270 procheader_headername_equal(header->name, "Reply-To") ||
1271 procheader_headername_equal(header->name, "Sender"))
1272 address_list = address_list_append(address_list, header->body);
1273 if (address_list == NULL)
1277 found = match_with_addresses_in_addressbook
1278 (matcher, address_list, matcher->criteria,
1279 matcher->expr, match);
1280 g_slist_free(address_list);
1282 if (matcher->criteria == MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK)
1293 *\brief Check if the matcher structure wants headers to
1296 *\param matcher Matcher structure
1298 *\return gboolean TRUE if the matcher structure describes
1299 * a header match condition
1301 static gboolean matcherprop_criteria_headers(const MatcherProp *matcher)
1303 switch (matcher->criteria) {
1304 case MATCHCRITERIA_HEADER:
1305 case MATCHCRITERIA_NOT_HEADER:
1306 case MATCHCRITERIA_HEADERS_PART:
1307 case MATCHCRITERIA_NOT_HEADERS_PART:
1308 case MATCHCRITERIA_FOUND_IN_ADDRESSBOOK:
1309 case MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK:
1317 *\brief Check if the matcher structure wants the message
1318 * to be matched (just perform an action on any
1321 *\param matcher Matcher structure
1323 *\return gboolean TRUE if matcher condition should match
1326 static gboolean matcherprop_criteria_message(MatcherProp *matcher)
1328 switch (matcher->criteria) {
1329 case MATCHCRITERIA_MESSAGE:
1330 case MATCHCRITERIA_NOT_MESSAGE:
1338 *\brief Check if a list of conditions matches one header in
1341 *\param matchers List of conditions
1342 *\param fp Message file
1344 *\return gboolean TRUE if one of the headers is matched by
1345 * the list of conditions.
1347 static gboolean matcherlist_match_headers(MatcherList *matchers, FILE *fp)
1350 gchar buf[BUFFSIZE];
1352 while (procheader_get_one_field(buf, sizeof(buf), fp, NULL) != -1) {
1353 for (l = matchers->matchers ; l != NULL ; l = g_slist_next(l)) {
1354 MatcherProp *matcher = (MatcherProp *) l->data;
1355 gint match = MATCH_ANY;
1360 /* determine the match range (all, any are our concern here) */
1361 if (matcher->criteria == MATCHCRITERIA_NOT_HEADERS_PART ||
1362 matcher->criteria == MATCHCRITERIA_NOT_MESSAGE) {
1365 } else if (matcher->criteria == MATCHCRITERIA_FOUND_IN_ADDRESSBOOK ||
1366 matcher->criteria == MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK) {
1367 Header *header = NULL;
1369 /* address header is one of the headers we have to match when checking
1370 for any address header or all address headers? */
1371 header = procheader_parse_header(buf);
1373 (procheader_headername_equal(header->name, "From") ||
1374 procheader_headername_equal(header->name, "To") ||
1375 procheader_headername_equal(header->name, "Cc") ||
1376 procheader_headername_equal(header->name, "Reply-To") ||
1377 procheader_headername_equal(header->name, "Sender"))) {
1379 if (strcasecmp(matcher->header, "Any") == 0)
1381 else if (strcasecmp(matcher->header, "All") == 0)
1386 /* further call to matcherprop_match_one_header() can't match
1387 and it irrelevant, so: don't alter the match result */
1392 /* ZERO line must NOT match for the rule to match.
1394 if (match == MATCH_ALL) {
1395 if (matcherprop_match_one_header(matcher, buf)) {
1396 matcher->result = TRUE;
1398 matcher->result = FALSE;
1399 matcher->done = TRUE;
1401 /* else, just one line matching is enough for the rule to match
1403 } else if (matcherprop_criteria_headers(matcher) ||
1404 matcherprop_criteria_message(matcher)) {
1405 if (matcherprop_match_one_header(matcher, buf)) {
1406 matcher->result = TRUE;
1407 matcher->done = TRUE;
1411 /* if the rule matched and the matchers are OR, no need to
1412 * check the others */
1413 if (matcher->result && matcher->done) {
1414 if (!matchers->bool_and)
1424 *\brief Check if a matcher wants to check the message body
1426 *\param matcher Matcher structure
1428 *\return gboolean TRUE if body must be matched.
1430 static gboolean matcherprop_criteria_body(const MatcherProp *matcher)
1432 switch (matcher->criteria) {
1433 case MATCHCRITERIA_BODY_PART:
1434 case MATCHCRITERIA_NOT_BODY_PART:
1442 *\brief Check if a line in a message file's body matches
1445 *\param matchers List of conditions
1446 *\param fp Message file
1448 *\return gboolean TRUE if succesful match
1450 static gboolean matcherlist_match_body(MatcherList *matchers, gboolean body_only, MsgInfo *info)
1453 MimeInfo *mimeinfo = NULL;
1454 MimeInfo *partinfo = NULL;
1455 gchar buf[BUFFSIZE];
1456 gboolean first_text_found = FALSE;
1459 cm_return_val_if_fail(info != NULL, FALSE);
1461 mimeinfo = procmime_scan_message(info);
1464 partinfo = procmime_mimeinfo_next(mimeinfo);
1466 for (; partinfo != NULL; partinfo = procmime_mimeinfo_next(partinfo)) {
1468 if (partinfo->type != MIMETYPE_TEXT && body_only)
1471 if (partinfo->type == MIMETYPE_TEXT) {
1472 first_text_found = TRUE;
1473 outfp = procmime_get_text_content(partinfo);
1475 outfp = procmime_get_binary_content(partinfo);
1478 procmime_mimeinfo_free_all(mimeinfo);
1482 while (fgets(buf, sizeof(buf), outfp) != NULL) {
1485 for (l = matchers->matchers ; l != NULL ; l = g_slist_next(l)) {
1486 MatcherProp *matcher = (MatcherProp *) l->data;
1491 /* Don't scan non-text parts when looking in body, only
1492 * when looking in whole message
1494 if (partinfo && partinfo->type != MIMETYPE_TEXT &&
1495 (matcher->criteria == MATCHCRITERIA_NOT_BODY_PART ||
1496 matcher->criteria == MATCHCRITERIA_BODY_PART))
1499 /* if the criteria is ~body_part or ~message, ZERO lines
1500 * must match for the rule to match.
1502 if (matcher->criteria == MATCHCRITERIA_NOT_BODY_PART ||
1503 matcher->criteria == MATCHCRITERIA_NOT_MESSAGE) {
1504 if (matcherprop_string_match(matcher, buf,
1506 matcher->result = FALSE;
1507 matcher->done = TRUE;
1509 matcher->result = TRUE;
1510 /* else, just one line has to match */
1511 } else if (matcherprop_criteria_body(matcher) ||
1512 matcherprop_criteria_message(matcher)) {
1513 if (matcherprop_string_match(matcher, buf,
1515 matcher->result = TRUE;
1516 matcher->done = TRUE;
1520 /* if the matchers are OR'ed and the rule matched,
1521 * no need to check the others. */
1522 if (matcher->result && matcher->done) {
1523 if (!matchers->bool_and) {
1524 procmime_mimeinfo_free_all(mimeinfo);
1533 if (body_only && first_text_found)
1536 procmime_mimeinfo_free_all(mimeinfo);
1542 *\brief Check if a message file matches criteria
1544 *\param matchers Criteria
1545 *\param info Message info
1546 *\param result Default result
1548 *\return gboolean TRUE if matched
1550 static gboolean matcherlist_match_file(MatcherList *matchers, MsgInfo *info,
1553 gboolean read_headers;
1560 /* file need to be read ? */
1562 read_headers = FALSE;
1565 for (l = matchers->matchers ; l != NULL ; l = g_slist_next(l)) {
1566 MatcherProp *matcher = (MatcherProp *) l->data;
1568 if (matcherprop_criteria_headers(matcher))
1569 read_headers = TRUE;
1570 if (matcherprop_criteria_body(matcher))
1572 if (matcherprop_criteria_message(matcher)) {
1573 read_headers = TRUE;
1577 matcher->result = FALSE;
1578 matcher->done = FALSE;
1581 if (!read_headers && !read_body)
1584 file = procmsg_get_message_file_full(info, read_headers, read_body);
1588 if ((fp = g_fopen(file, "rb")) == NULL) {
1589 FILE_OP_ERROR(file, "fopen");
1594 /* read the headers */
1597 if (matcherlist_match_headers(matchers, fp))
1600 matcherlist_skip_headers(fp);
1605 matcherlist_match_body(matchers, body_only, info);
1608 for (l = matchers->matchers; l != NULL; l = g_slist_next(l)) {
1609 MatcherProp *matcher = (MatcherProp *) l->data;
1611 if (matcherprop_criteria_headers(matcher) ||
1612 matcherprop_criteria_body(matcher) ||
1613 matcherprop_criteria_message(matcher)) {
1614 if (matcher->result) {
1615 if (!matchers->bool_and) {
1621 if (matchers->bool_and) {
1637 *\brief Test list of conditions on a message.
1639 *\param matchers List of conditions
1640 *\param info Message info
1642 *\return gboolean TRUE if matched
1644 gboolean matcherlist_match(MatcherList *matchers, MsgInfo *info)
1652 if (matchers->bool_and)
1657 /* test the cached elements */
1659 for (l = matchers->matchers; l != NULL ;l = g_slist_next(l)) {
1660 MatcherProp *matcher = (MatcherProp *) l->data;
1662 if (debug_filtering_session) {
1663 gchar *buf = matcherprop_to_string(matcher);
1664 log_print(LOG_DEBUG_FILTERING, _("checking if message matches [ %s ]\n"), buf);
1668 switch(matcher->criteria) {
1669 case MATCHCRITERIA_ALL:
1670 case MATCHCRITERIA_UNREAD:
1671 case MATCHCRITERIA_NOT_UNREAD:
1672 case MATCHCRITERIA_NEW:
1673 case MATCHCRITERIA_NOT_NEW:
1674 case MATCHCRITERIA_MARKED:
1675 case MATCHCRITERIA_NOT_MARKED:
1676 case MATCHCRITERIA_DELETED:
1677 case MATCHCRITERIA_NOT_DELETED:
1678 case MATCHCRITERIA_REPLIED:
1679 case MATCHCRITERIA_NOT_REPLIED:
1680 case MATCHCRITERIA_FORWARDED:
1681 case MATCHCRITERIA_NOT_FORWARDED:
1682 case MATCHCRITERIA_LOCKED:
1683 case MATCHCRITERIA_NOT_LOCKED:
1684 case MATCHCRITERIA_SPAM:
1685 case MATCHCRITERIA_NOT_SPAM:
1686 case MATCHCRITERIA_HAS_ATTACHMENT:
1687 case MATCHCRITERIA_HAS_NO_ATTACHMENT:
1688 case MATCHCRITERIA_SIGNED:
1689 case MATCHCRITERIA_NOT_SIGNED:
1690 case MATCHCRITERIA_COLORLABEL:
1691 case MATCHCRITERIA_NOT_COLORLABEL:
1692 case MATCHCRITERIA_IGNORE_THREAD:
1693 case MATCHCRITERIA_NOT_IGNORE_THREAD:
1694 case MATCHCRITERIA_WATCH_THREAD:
1695 case MATCHCRITERIA_NOT_WATCH_THREAD:
1696 case MATCHCRITERIA_SUBJECT:
1697 case MATCHCRITERIA_NOT_SUBJECT:
1698 case MATCHCRITERIA_FROM:
1699 case MATCHCRITERIA_NOT_FROM:
1700 case MATCHCRITERIA_TO:
1701 case MATCHCRITERIA_NOT_TO:
1702 case MATCHCRITERIA_CC:
1703 case MATCHCRITERIA_NOT_CC:
1704 case MATCHCRITERIA_TO_OR_CC:
1705 case MATCHCRITERIA_NOT_TO_AND_NOT_CC:
1706 case MATCHCRITERIA_TAG:
1707 case MATCHCRITERIA_NOT_TAG:
1708 case MATCHCRITERIA_TAGGED:
1709 case MATCHCRITERIA_NOT_TAGGED:
1710 case MATCHCRITERIA_AGE_GREATER:
1711 case MATCHCRITERIA_AGE_LOWER:
1712 case MATCHCRITERIA_NEWSGROUPS:
1713 case MATCHCRITERIA_NOT_NEWSGROUPS:
1714 case MATCHCRITERIA_INREPLYTO:
1715 case MATCHCRITERIA_NOT_INREPLYTO:
1716 case MATCHCRITERIA_REFERENCES:
1717 case MATCHCRITERIA_NOT_REFERENCES:
1718 case MATCHCRITERIA_SCORE_GREATER:
1719 case MATCHCRITERIA_SCORE_LOWER:
1720 case MATCHCRITERIA_SCORE_EQUAL:
1721 case MATCHCRITERIA_SIZE_GREATER:
1722 case MATCHCRITERIA_SIZE_SMALLER:
1723 case MATCHCRITERIA_SIZE_EQUAL:
1724 case MATCHCRITERIA_TEST:
1725 case MATCHCRITERIA_NOT_TEST:
1726 case MATCHCRITERIA_PARTIAL:
1727 case MATCHCRITERIA_NOT_PARTIAL:
1728 if (matcherprop_match(matcher, info)) {
1729 if (!matchers->bool_and) {
1730 if (debug_filtering_session)
1731 log_status_ok(LOG_DEBUG_FILTERING, _("message matches\n"));
1736 if (matchers->bool_and) {
1737 if (debug_filtering_session)
1738 log_status_nok(LOG_DEBUG_FILTERING, _("message does not match\n"));
1745 /* test the condition on the file */
1747 if (matcherlist_match_file(matchers, info, result)) {
1748 if (!matchers->bool_and) {
1749 if (debug_filtering_session)
1750 log_status_ok(LOG_DEBUG_FILTERING, _("message matches\n"));
1754 if (matchers->bool_and) {
1755 if (debug_filtering_session)
1756 log_status_nok(LOG_DEBUG_FILTERING, _("message does not match\n"));
1761 if (debug_filtering_session) {
1763 log_status_ok(LOG_DEBUG_FILTERING, _("message matches\n"));
1765 log_status_nok(LOG_DEBUG_FILTERING, _("message does not match\n"));
1771 static gint quote_filter_str(gchar * result, guint size,
1781 for(p = path ; * p != '\0' ; p ++) {
1783 if ((* p != '\"') && (* p != '\\')) {
1784 if (remaining > 0) {
1790 result[size - 1] = '\0';
1795 if (remaining >= 2) {
1803 result[size - 1] = '\0';
1808 if (remaining > 0) {
1812 result[size - 1] = '\0';
1820 gchar * matcher_quote_str(const gchar * src)
1825 len = strlen(src) * 2 + 1;
1826 res = g_malloc(len);
1827 quote_filter_str(res, len, src);
1833 *\brief Convert a matcher structure to a string
1835 *\param matcher Matcher structure
1837 *\return gchar * Newly allocated string
1839 gchar *matcherprop_to_string(MatcherProp *matcher)
1841 gchar *matcher_str = NULL;
1842 const gchar *criteria_str;
1843 const gchar *matchtype_str;
1845 gchar * quoted_expr;
1846 gchar * quoted_header;
1848 criteria_str = NULL;
1849 for (i = 0; i < (int) (sizeof(matchparser_tab) / sizeof(MatchParser)); i++) {
1850 if (matchparser_tab[i].id == matcher->criteria)
1851 criteria_str = matchparser_tab[i].str;
1853 if (criteria_str == NULL)
1856 switch (matcher->criteria) {
1857 case MATCHCRITERIA_AGE_GREATER:
1858 case MATCHCRITERIA_AGE_LOWER:
1859 case MATCHCRITERIA_SCORE_GREATER:
1860 case MATCHCRITERIA_SCORE_LOWER:
1861 case MATCHCRITERIA_SCORE_EQUAL:
1862 case MATCHCRITERIA_SIZE_GREATER:
1863 case MATCHCRITERIA_SIZE_SMALLER:
1864 case MATCHCRITERIA_SIZE_EQUAL:
1865 case MATCHCRITERIA_COLORLABEL:
1866 case MATCHCRITERIA_NOT_COLORLABEL:
1867 return g_strdup_printf("%s %i", criteria_str, matcher->value);
1868 case MATCHCRITERIA_ALL:
1869 case MATCHCRITERIA_UNREAD:
1870 case MATCHCRITERIA_NOT_UNREAD:
1871 case MATCHCRITERIA_NEW:
1872 case MATCHCRITERIA_NOT_NEW:
1873 case MATCHCRITERIA_MARKED:
1874 case MATCHCRITERIA_NOT_MARKED:
1875 case MATCHCRITERIA_DELETED:
1876 case MATCHCRITERIA_NOT_DELETED:
1877 case MATCHCRITERIA_REPLIED:
1878 case MATCHCRITERIA_NOT_REPLIED:
1879 case MATCHCRITERIA_FORWARDED:
1880 case MATCHCRITERIA_NOT_FORWARDED:
1881 case MATCHCRITERIA_LOCKED:
1882 case MATCHCRITERIA_NOT_LOCKED:
1883 case MATCHCRITERIA_SPAM:
1884 case MATCHCRITERIA_NOT_SPAM:
1885 case MATCHCRITERIA_HAS_ATTACHMENT:
1886 case MATCHCRITERIA_HAS_NO_ATTACHMENT:
1887 case MATCHCRITERIA_SIGNED:
1888 case MATCHCRITERIA_NOT_SIGNED:
1889 case MATCHCRITERIA_PARTIAL:
1890 case MATCHCRITERIA_NOT_PARTIAL:
1891 case MATCHCRITERIA_IGNORE_THREAD:
1892 case MATCHCRITERIA_NOT_IGNORE_THREAD:
1893 case MATCHCRITERIA_WATCH_THREAD:
1894 case MATCHCRITERIA_NOT_WATCH_THREAD:
1895 case MATCHCRITERIA_TAGGED:
1896 case MATCHCRITERIA_NOT_TAGGED:
1897 return g_strdup(criteria_str);
1898 case MATCHCRITERIA_TEST:
1899 case MATCHCRITERIA_NOT_TEST:
1900 quoted_expr = matcher_quote_str(matcher->expr);
1901 matcher_str = g_strdup_printf("%s \"%s\"",
1902 criteria_str, quoted_expr);
1903 g_free(quoted_expr);
1905 case MATCHCRITERIA_FOUND_IN_ADDRESSBOOK:
1906 case MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK:
1907 quoted_header = matcher_quote_str(matcher->header);
1908 quoted_expr = matcher_quote_str(matcher->expr);
1909 matcher_str = g_strdup_printf("%s \"%s\" in \"%s\"",
1910 criteria_str, quoted_header, quoted_expr);
1911 g_free(quoted_header);
1912 g_free(quoted_expr);
1916 matchtype_str = NULL;
1917 for (i = 0; i < sizeof matchparser_tab / sizeof matchparser_tab[0]; i++) {
1918 if (matchparser_tab[i].id == matcher->matchtype)
1919 matchtype_str = matchparser_tab[i].str;
1922 if (matchtype_str == NULL)
1925 switch (matcher->matchtype) {
1926 case MATCHTYPE_MATCH:
1927 case MATCHTYPE_MATCHCASE:
1928 case MATCHTYPE_REGEXP:
1929 case MATCHTYPE_REGEXPCASE:
1930 quoted_expr = matcher_quote_str(matcher->expr);
1931 if (matcher->header) {
1932 quoted_header = matcher_quote_str(matcher->header);
1933 matcher_str = g_strdup_printf
1934 ("%s \"%s\" %s \"%s\"",
1935 criteria_str, quoted_header,
1936 matchtype_str, quoted_expr);
1937 g_free(quoted_header);
1940 matcher_str = g_strdup_printf
1941 ("%s %s \"%s\"", criteria_str,
1942 matchtype_str, quoted_expr);
1943 g_free(quoted_expr);
1951 *\brief Convert a list of conditions to a string
1953 *\param matchers List of conditions
1955 *\return gchar * Newly allocated string
1957 gchar *matcherlist_to_string(const MatcherList *matchers)
1963 gchar *result = NULL;
1965 count = g_slist_length(matchers->matchers);
1966 vstr = g_new(gchar *, count + 1);
1968 for (l = matchers->matchers, cur_str = vstr; l != NULL;
1969 l = g_slist_next(l), cur_str ++) {
1970 *cur_str = matcherprop_to_string((MatcherProp *) l->data);
1971 if (*cur_str == NULL)
1976 if (matchers->bool_and)
1977 result = g_strjoinv(" & ", vstr);
1979 result = g_strjoinv(" | ", vstr);
1981 for (cur_str = vstr ; *cur_str != NULL ; cur_str ++)
1989 #define STRLEN_ZERO(s) ((s) ? strlen(s) : 0)
1990 #define STRLEN_DEFAULT(s,d) ((s) ? strlen(s) : STRLEN_ZERO(d))
1992 static void add_str_default(gchar ** dest,
1993 const gchar * s, const gchar * d)
1995 gchar quoted_str[4096];
2003 quote_cmd_argument(quoted_str, sizeof(quoted_str), str);
2004 strcpy(* dest, quoted_str);
2006 (* dest) += strlen(* dest);
2009 /* matching_build_command() - preferably cmd should be unescaped */
2011 *\brief Build the command-line to execute
2013 *\param cmd String with command-line specifiers
2014 *\param info Message info to use for command
2016 *\return gchar * Newly allocated string
2018 gchar *matching_build_command(const gchar *cmd, MsgInfo *info)
2020 const gchar *s = cmd;
2021 gchar *filename = NULL;
2022 gchar *processed_cmd;
2026 const gchar *const no_subject = _("(none)") ;
2027 const gchar *const no_from = _("(none)") ;
2028 const gchar *const no_to = _("(none)") ;
2029 const gchar *const no_cc = _("(none)") ;
2030 const gchar *const no_date = _("(none)") ;
2031 const gchar *const no_msgid = _("(none)") ;
2032 const gchar *const no_newsgroups = _("(none)") ;
2033 const gchar *const no_references = _("(none)") ;
2035 size = STRLEN_ZERO(cmd) + 1;
2036 while (*s != '\0') {
2043 case 's': /* subject */
2044 size += STRLEN_DEFAULT(info->subject, no_subject) - 2;
2046 case 'f': /* from */
2047 size += STRLEN_DEFAULT(info->from, no_from) - 2;
2050 size += STRLEN_DEFAULT(info->to, no_to) - 2;
2053 size += STRLEN_DEFAULT(info->cc, no_cc) - 2;
2055 case 'd': /* date */
2056 size += STRLEN_DEFAULT(info->date, no_date) - 2;
2058 case 'i': /* message-id */
2059 size += STRLEN_DEFAULT(info->msgid, no_msgid) - 2;
2061 case 'n': /* newsgroups */
2062 size += STRLEN_DEFAULT(info->newsgroups, no_newsgroups) - 2;
2064 case 'r': /* references */
2065 /* FIXME: using the inreplyto header for reference */
2066 size += STRLEN_DEFAULT(info->inreplyto, no_references) - 2;
2068 case 'F': /* file */
2069 if (filename == NULL)
2070 filename = folder_item_fetch_msg(info->folder, info->msgnum);
2072 if (filename == NULL) {
2073 g_warning("filename is not set");
2077 size += strlen(filename) - 2;
2086 /* as the string can be quoted, we double the result */
2089 processed_cmd = g_new0(gchar, size);
2093 while (*s != '\0') {
2101 case 's': /* subject */
2102 add_str_default(&p, info->subject,
2105 case 'f': /* from */
2106 add_str_default(&p, info->from,
2110 add_str_default(&p, info->to,
2114 add_str_default(&p, info->cc,
2117 case 'd': /* date */
2118 add_str_default(&p, info->date,
2121 case 'i': /* message-id */
2122 add_str_default(&p, info->msgid,
2125 case 'n': /* newsgroups */
2126 add_str_default(&p, info->newsgroups,
2129 case 'r': /* references */
2130 /* FIXME: using the inreplyto header for references */
2131 add_str_default(&p, info->inreplyto, no_references);
2133 case 'F': /* file */
2134 if (filename != NULL)
2135 add_str_default(&p, filename, NULL);
2154 return processed_cmd;
2156 #undef STRLEN_DEFAULT
2159 /* ************************************************************ */
2163 *\brief Write filtering list to file
2166 *\param prefs_filtering List of filtering conditions
2168 static int prefs_filtering_write(FILE *fp, GSList *prefs_filtering)
2172 for (cur = prefs_filtering; cur != NULL; cur = cur->next) {
2173 gchar *filtering_str = NULL;
2174 gchar *tmp_name = NULL;
2175 FilteringProp *prop = NULL;
2177 if (NULL == (prop = (FilteringProp *) cur->data))
2180 if (NULL == (filtering_str = filteringprop_to_string(prop)))
2183 if (prop->enabled) {
2184 if (fputs("enabled ", fp) == EOF) {
2185 FILE_OP_ERROR("filtering config", "fputs");
2189 if (fputs("disabled ", fp) == EOF) {
2190 FILE_OP_ERROR("filtering config", "fputs");
2195 if (fputs("rulename \"", fp) == EOF) {
2196 FILE_OP_ERROR("filtering config", "fputs");
2197 g_free(filtering_str);
2200 tmp_name = prop->name;
2201 while (tmp_name && *tmp_name != '\0') {
2202 if (*tmp_name != '"') {
2203 if (fputc(*tmp_name, fp) == EOF) {
2204 FILE_OP_ERROR("filtering config", "fputs || fputc");
2205 g_free(filtering_str);
2208 } else if (*tmp_name == '"') {
2209 if (fputc('\\', fp) == EOF ||
2210 fputc('"', fp) == EOF) {
2211 FILE_OP_ERROR("filtering config", "fputs || fputc");
2212 g_free(filtering_str);
2218 if (fputs("\" ", fp) == EOF) {
2219 FILE_OP_ERROR("filtering config", "fputs");
2220 g_free(filtering_str);
2224 if (prop->account_id != 0) {
2227 tmp = g_strdup_printf("account %d ", prop->account_id);
2228 if (fputs(tmp, fp) == EOF) {
2229 FILE_OP_ERROR("filtering config", "fputs");
2236 if(fputs(filtering_str, fp) == EOF ||
2237 fputc('\n', fp) == EOF) {
2238 FILE_OP_ERROR("filtering config", "fputs || fputc");
2239 g_free(filtering_str);
2242 g_free(filtering_str);
2248 typedef struct _NodeLoopData {
2254 *\brief Write matchers from a folder item
2256 *\param node Node with folder info
2257 *\param data File pointer
2259 *\return gboolean FALSE
2261 static gboolean prefs_matcher_write_func(GNode *node, gpointer d)
2264 NodeLoopData *data = (NodeLoopData *)d;
2266 GSList *prefs_filtering;
2269 /* prevent warning */
2270 if (item->path == NULL)
2272 id = folder_item_get_identifier(item);
2275 prefs_filtering = item->prefs->processing;
2277 if (prefs_filtering != NULL) {
2278 if (fprintf(data->fp, "[%s]\n", id) < 0) {
2282 if (prefs_filtering_write(data->fp, prefs_filtering) < 0) {
2286 if (fputc('\n', data->fp) == EOF) {
2298 *\brief Save matchers from folder items
2302 static int prefs_matcher_save(FILE *fp)
2310 for (cur = folder_get_list() ; cur != NULL ; cur = g_list_next(cur)) {
2313 folder = (Folder *) cur->data;
2314 g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
2315 prefs_matcher_write_func, &data);
2318 if (data.error == TRUE)
2321 /* pre global rules */
2322 if (fprintf(fp, "[preglobal]\n") < 0 ||
2323 prefs_filtering_write(fp, pre_global_processing) < 0 ||
2324 fputc('\n', fp) == EOF)
2327 /* post global rules */
2328 if (fprintf(fp, "[postglobal]\n") < 0 ||
2329 prefs_filtering_write(fp, post_global_processing) < 0 ||
2330 fputc('\n', fp) == EOF)
2333 /* filtering rules */
2334 if (fprintf(fp, "[filtering]\n") < 0 ||
2335 prefs_filtering_write(fp, filtering_rules) < 0 ||
2336 fputc('\n', fp) == EOF)
2343 *\brief Write filtering / matcher configuration file
2345 void prefs_matcher_write_config(void)
2350 debug_print("Writing matcher configuration...\n");
2352 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
2355 if ((pfile = prefs_write_open(rcpath)) == NULL) {
2356 g_warning("failed to write configuration to file\n");
2363 if (prefs_matcher_save(pfile->fp) < 0) {
2364 g_warning("failed to write configuration to file\n");
2365 prefs_file_close_revert(pfile);
2366 } else if (prefs_file_close(pfile) < 0) {
2367 g_warning("failed to save configuration to file\n");
2371 /* ******************************************************************* */
2373 static void matcher_add_rulenames(const gchar *rcpath)
2375 gchar *newpath = g_strconcat(rcpath, ".new", NULL);
2376 FILE *src = g_fopen(rcpath, "rb");
2377 FILE *dst = g_fopen(newpath, "wb");
2378 gchar buf[BUFFSIZE];
2395 while (fgets (buf, sizeof(buf), src) != NULL) {
2396 if (strlen(buf) > 2 && buf[0] != '['
2397 && strncmp(buf, "rulename \"", 10)
2398 && strncmp(buf, "enabled rulename \"", 18)
2399 && strncmp(buf, "disabled rulename \"", 18)) {
2400 r = fwrite("enabled rulename \"\" ",
2401 strlen("enabled rulename \"\" "), 1, dst);
2403 r = fwrite(buf, strlen(buf), 1, dst);
2407 move_file(newpath, rcpath, TRUE);
2412 *\brief Read matcher configuration
2414 void prefs_matcher_read_config(void)
2417 gchar *rc_old_format;
2420 create_matchparser_hashtab();
2421 prefs_filtering_clear();
2423 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MATCHER_RC, NULL);
2424 rc_old_format = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MATCHER_RC,
2425 ".pre_names", NULL);
2427 if (!is_file_exist(rc_old_format) && is_file_exist(rcpath)) {
2428 /* backup file with no rules names, in case
2429 * anything goes wrong */
2430 copy_file(rcpath, rc_old_format, FALSE);
2431 /* now hack the file in order to have it to the new format */
2432 matcher_add_rulenames(rcpath);
2435 g_free(rc_old_format);
2437 f = g_fopen(rcpath, "rb");
2441 matcher_parser_start_parsing(f);
2442 fclose(matcher_parserin);
2445 /* previous version compatibility */
2447 /* g_print("reading filtering\n"); */
2448 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
2449 FILTERING_RC, NULL);
2450 f = g_fopen(rcpath, "rb");
2454 matcher_parser_start_parsing(f);
2455 fclose(matcher_parserin);
2458 /* g_print("reading scoring\n"); */
2459 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
2461 f = g_fopen(rcpath, "rb");
2465 matcher_parser_start_parsing(f);
2466 fclose(matcher_parserin);