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"
50 #include "folder_item_prefs.h"
54 *\brief Keyword lookup element
57 gint id; /*!< keyword id */
58 gchar *str; /*!< keyword */
60 typedef struct _MatchParser MatchParser;
63 *\brief Table with strings and ids used by the lexer and
64 * the parser. New keywords can be added here.
66 static const MatchParser matchparser_tab[] = {
68 {MATCHCRITERIA_ALL, "all"},
69 {MATCHCRITERIA_UNREAD, "unread"},
70 {MATCHCRITERIA_NOT_UNREAD, "~unread"},
71 {MATCHCRITERIA_NEW, "new"},
72 {MATCHCRITERIA_NOT_NEW, "~new"},
73 {MATCHCRITERIA_MARKED, "marked"},
74 {MATCHCRITERIA_NOT_MARKED, "~marked"},
75 {MATCHCRITERIA_DELETED, "deleted"},
76 {MATCHCRITERIA_NOT_DELETED, "~deleted"},
77 {MATCHCRITERIA_REPLIED, "replied"},
78 {MATCHCRITERIA_NOT_REPLIED, "~replied"},
79 {MATCHCRITERIA_FORWARDED, "forwarded"},
80 {MATCHCRITERIA_NOT_FORWARDED, "~forwarded"},
81 {MATCHCRITERIA_LOCKED, "locked"},
82 {MATCHCRITERIA_NOT_LOCKED, "~locked"},
83 {MATCHCRITERIA_COLORLABEL, "colorlabel"},
84 {MATCHCRITERIA_NOT_COLORLABEL, "~colorlabel"},
85 {MATCHCRITERIA_IGNORE_THREAD, "ignore_thread"},
86 {MATCHCRITERIA_NOT_IGNORE_THREAD, "~ignore_thread"},
87 {MATCHCRITERIA_WATCH_THREAD, "watch_thread"},
88 {MATCHCRITERIA_NOT_WATCH_THREAD, "~watch_thread"},
89 {MATCHCRITERIA_SPAM, "spam"},
90 {MATCHCRITERIA_NOT_SPAM, "~spam"},
91 {MATCHCRITERIA_HAS_ATTACHMENT, "has_attachment"},
92 {MATCHCRITERIA_HAS_NO_ATTACHMENT, "~has_attachment"},
93 {MATCHCRITERIA_SIGNED, "signed"},
94 {MATCHCRITERIA_NOT_SIGNED, "~signed"},
97 {MATCHCRITERIA_SUBJECT, "subject"},
98 {MATCHCRITERIA_NOT_SUBJECT, "~subject"},
99 {MATCHCRITERIA_FROM, "from"},
100 {MATCHCRITERIA_NOT_FROM, "~from"},
101 {MATCHCRITERIA_TO, "to"},
102 {MATCHCRITERIA_NOT_TO, "~to"},
103 {MATCHCRITERIA_CC, "cc"},
104 {MATCHCRITERIA_NOT_CC, "~cc"},
105 {MATCHCRITERIA_TO_OR_CC, "to_or_cc"},
106 {MATCHCRITERIA_NOT_TO_AND_NOT_CC, "~to_or_cc"},
107 {MATCHCRITERIA_TAG, "tag"},
108 {MATCHCRITERIA_NOT_TAG, "~tag"},
109 {MATCHCRITERIA_TAGGED, "tagged"},
110 {MATCHCRITERIA_NOT_TAGGED, "~tagged"},
111 {MATCHCRITERIA_AGE_GREATER, "age_greater"},
112 {MATCHCRITERIA_AGE_LOWER, "age_lower"},
113 {MATCHCRITERIA_NEWSGROUPS, "newsgroups"},
114 {MATCHCRITERIA_NOT_NEWSGROUPS, "~newsgroups"},
115 {MATCHCRITERIA_INREPLYTO, "inreplyto"},
116 {MATCHCRITERIA_NOT_INREPLYTO, "~inreplyto"},
117 {MATCHCRITERIA_REFERENCES, "references"},
118 {MATCHCRITERIA_NOT_REFERENCES, "~references"},
119 {MATCHCRITERIA_SCORE_GREATER, "score_greater"},
120 {MATCHCRITERIA_SCORE_LOWER, "score_lower"},
121 {MATCHCRITERIA_SCORE_EQUAL, "score_equal"},
122 {MATCHCRITERIA_PARTIAL, "partial"},
123 {MATCHCRITERIA_NOT_PARTIAL, "~partial"},
124 {MATCHCRITERIA_FOUND_IN_ADDRESSBOOK, "found_in_addressbook"},
125 {MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK, "~found_in_addressbook"},
127 {MATCHCRITERIA_SIZE_GREATER, "size_greater"},
128 {MATCHCRITERIA_SIZE_SMALLER, "size_smaller"},
129 {MATCHCRITERIA_SIZE_EQUAL, "size_equal"},
131 /* content have to be read */
132 {MATCHCRITERIA_HEADER, "header"},
133 {MATCHCRITERIA_NOT_HEADER, "~header"},
134 {MATCHCRITERIA_HEADERS_PART, "headers_part"},
135 {MATCHCRITERIA_NOT_HEADERS_PART, "~headers_part"},
136 {MATCHCRITERIA_MESSAGE, "message"},
137 {MATCHCRITERIA_NOT_MESSAGE, "~message"},
138 {MATCHCRITERIA_BODY_PART, "body_part"},
139 {MATCHCRITERIA_NOT_BODY_PART, "~body_part"},
140 {MATCHCRITERIA_TEST, "test"},
141 {MATCHCRITERIA_NOT_TEST, "~test"},
144 {MATCHTYPE_MATCHCASE, "matchcase"},
145 {MATCHTYPE_MATCH, "match"},
146 {MATCHTYPE_REGEXPCASE, "regexpcase"},
147 {MATCHTYPE_REGEXP, "regexp"},
150 {MATCHACTION_SCORE, "score"}, /* for backward compatibility */
151 {MATCHACTION_MOVE, "move"},
152 {MATCHACTION_COPY, "copy"},
153 {MATCHACTION_DELETE, "delete"},
154 {MATCHACTION_MARK, "mark"},
155 {MATCHACTION_UNMARK, "unmark"},
156 {MATCHACTION_LOCK, "lock"},
157 {MATCHACTION_UNLOCK, "unlock"},
158 {MATCHACTION_MARK_AS_READ, "mark_as_read"},
159 {MATCHACTION_MARK_AS_UNREAD, "mark_as_unread"},
160 {MATCHACTION_MARK_AS_SPAM, "mark_as_spam"},
161 {MATCHACTION_MARK_AS_HAM, "mark_as_ham"},
162 {MATCHACTION_FORWARD, "forward"},
163 {MATCHACTION_FORWARD_AS_ATTACHMENT, "forward_as_attachment"},
164 {MATCHACTION_EXECUTE, "execute"},
165 {MATCHACTION_COLOR, "color"},
166 {MATCHACTION_REDIRECT, "redirect"},
167 {MATCHACTION_CHANGE_SCORE, "change_score"},
168 {MATCHACTION_SET_SCORE, "set_score"},
169 {MATCHACTION_STOP, "stop"},
170 {MATCHACTION_HIDE, "hide"},
171 {MATCHACTION_IGNORE, "ignore"},
172 {MATCHACTION_WATCH, "watch"},
173 {MATCHACTION_ADD_TO_ADDRESSBOOK, "add_to_addressbook"},
174 {MATCHACTION_SET_TAG, "set_tag"},
175 {MATCHACTION_UNSET_TAG, "unset_tag"},
176 {MATCHACTION_CLEAR_TAGS, "clear_tags"},
200 static gchar *context_str[N_CONTEXT_STRS];
202 void matcher_init(void)
204 if (context_str[CONTEXT_SUBJECT] != NULL)
207 context_str[CONTEXT_SUBJECT] = g_strdup_printf(_("%s header"), prefs_common_translated_header_name("Subject:"));
208 context_str[CONTEXT_FROM] = g_strdup_printf(_("%s header"), prefs_common_translated_header_name("From:"));
209 context_str[CONTEXT_TO] = g_strdup_printf(_("%s header"), prefs_common_translated_header_name("To:"));
210 context_str[CONTEXT_CC] = g_strdup_printf(_("%s header"), prefs_common_translated_header_name("Cc:"));
211 context_str[CONTEXT_NEWSGROUPS] = g_strdup_printf(_("%s header"), prefs_common_translated_header_name("Newsgroups:"));
212 context_str[CONTEXT_IN_REPLY_TO] = g_strdup_printf(_("%s header"), prefs_common_translated_header_name("In-Reply-To:"));
213 context_str[CONTEXT_REFERENCES] = g_strdup_printf(_("%s header"), prefs_common_translated_header_name("References:"));
214 context_str[CONTEXT_HEADER] = g_strdup(_("header"));
215 context_str[CONTEXT_HEADER_LINE] = g_strdup(_("header line"));
216 context_str[CONTEXT_BODY_LINE] = g_strdup(_("body line"));
217 context_str[CONTEXT_TAG] = g_strdup(_("tag"));
220 void matcher_done(void)
223 for (i = 0; i < N_CONTEXT_STRS; i++) {
224 g_free(context_str[i]);
225 context_str[i] = NULL;
229 extern gboolean debug_filtering_session;
232 *\brief Look up table with keywords defined in \sa matchparser_tab
234 static GHashTable *matchparser_hashtab;
237 *\brief Translate keyword id to keyword string
239 *\param id Id of keyword
241 *\return const gchar * Keyword
243 const gchar *get_matchparser_tab_str(gint id)
247 for (i = 0; i < sizeof matchparser_tab / sizeof matchparser_tab[0]; i++) {
248 if (matchparser_tab[i].id == id)
249 return matchparser_tab[i].str;
255 *\brief Create keyword lookup table
257 static void create_matchparser_hashtab(void)
261 if (matchparser_hashtab) return;
262 matchparser_hashtab = g_hash_table_new(g_str_hash, g_str_equal);
263 for (i = 0; i < sizeof matchparser_tab / sizeof matchparser_tab[0]; i++)
264 g_hash_table_insert(matchparser_hashtab,
265 matchparser_tab[i].str,
266 (gpointer) &matchparser_tab[i]);
270 *\brief Return a keyword id from a keyword string
272 *\param str Keyword string
274 *\return gint Keyword id
276 gint get_matchparser_tab_id(const gchar *str)
280 if (NULL != (res = g_hash_table_lookup(matchparser_hashtab, str))) {
286 /* **************** data structure allocation **************** */
289 *\brief Allocate a structure for a filtering / scoring
290 * "condition" (a matcher structure)
292 *\param criteria Criteria ID (MATCHCRITERIA_XXXX)
293 *\param header Header string (if criteria is MATCHCRITERIA_HEADER
294 or MATCHCRITERIA_FOUND_IN_ADDRESSBOOK)
295 *\param matchtype Type of action (MATCHTYPE_XXX)
296 *\param expr String value or expression to check
297 *\param value Integer value to check
299 *\return MatcherProp * Pointer to newly allocated structure
301 MatcherProp *matcherprop_new(gint criteria, const gchar *header,
302 gint matchtype, const gchar *expr,
307 prop = g_new0(MatcherProp, 1);
308 prop->criteria = criteria;
309 prop->header = header != NULL ? g_strdup(header) : NULL;
311 prop->expr = expr != NULL ? g_strdup(expr) : NULL;
313 prop->matchtype = matchtype;
322 *\brief Free a matcher structure
324 *\param prop Pointer to matcher structure allocated with
327 void matcherprop_free(MatcherProp *prop)
330 g_free(prop->header);
331 if (prop->preg != NULL) {
339 *\brief Copy a matcher structure
341 *\param src Matcher structure to copy
343 *\return MatcherProp * Pointer to newly allocated matcher structure
345 MatcherProp *matcherprop_copy(const MatcherProp *src)
347 MatcherProp *prop = g_new0(MatcherProp, 1);
349 prop->criteria = src->criteria;
350 prop->header = src->header ? g_strdup(src->header) : NULL;
351 prop->expr = src->expr ? g_strdup(src->expr) : NULL;
352 prop->matchtype = src->matchtype;
354 prop->preg = NULL; /* will be re-evaluated */
355 prop->value = src->value;
356 prop->error = src->error;
360 /* ************** match ******************************/
362 static gboolean match_with_addresses_in_addressbook
363 (MatcherProp *prop, GSList *address_list, gint type,
364 gchar* folderpath, gint match)
367 gboolean found = FALSE;
370 cm_return_val_if_fail(address_list != NULL, FALSE);
372 debug_print("match_with_addresses_in_addressbook(%d, %s)\n",
373 g_slist_length(address_list), folderpath?folderpath:"(null)");
375 if (folderpath == NULL ||
376 strcasecmp(folderpath, "Any") == 0 ||
382 start_address_completion(path);
384 for (walk = address_list; walk != NULL; walk = walk->next) {
385 /* exact matching of email address */
386 guint num_addr = complete_address(walk->data);
389 /* skip first item (this is the search string itself) */
391 for (; i < num_addr && !found; i++) {
392 gchar *addr = get_complete_address(i);
393 extract_address(addr);
394 if (strcasecmp(addr, walk->data) == 0) {
398 if (debug_filtering_session
399 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
400 log_print(LOG_DEBUG_FILTERING,
401 "address [ %s ] matches\n",
402 (gchar *)walk->data);
409 if (debug_filtering_session
410 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH
412 log_print(LOG_DEBUG_FILTERING,
413 "address [ %s ] does NOT match\n",
414 (gchar *)walk->data);
418 if (match == MATCH_ALL) {
419 /* if matching all addresses, stop if one doesn't match */
422 if (debug_filtering_session
423 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
424 log_print(LOG_DEBUG_FILTERING,
425 "not all address match (matching all)\n");
429 } else if (match == MATCH_ANY) {
430 /* if matching any address, stop if one does match */
433 if (debug_filtering_session
434 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
435 log_print(LOG_DEBUG_FILTERING,
436 "at least one address matches (matching any)\n");
441 /* MATCH_ONE: there should be only one loop iteration */
444 end_address_completion();
450 *\brief Find out if a string matches a condition
452 *\param prop Matcher structure
453 *\param str String to check
455 *\return gboolean TRUE if str matches the condition in the
458 static gboolean matcherprop_string_match(MatcherProp *prop, const gchar *str,
459 const gchar *debug_context)
463 gboolean ret = FALSE;
464 gboolean should_free = FALSE;
468 if (prop->matchtype == MATCHTYPE_REGEXPCASE ||
469 prop->matchtype == MATCHTYPE_MATCHCASE) {
470 str1 = g_utf8_casefold(str, -1);
471 down_expr = g_utf8_casefold(prop->expr, -1);
475 down_expr = (gchar *)prop->expr;
479 switch (prop->matchtype) {
480 case MATCHTYPE_REGEXPCASE:
481 case MATCHTYPE_REGEXP:
483 if (!prop->preg && (prop->error == 0)) {
484 prop->preg = g_new0(regex_t, 1);
485 /* if regexp then don't use the escaped string */
486 if (regcomp(prop->preg, down_expr,
487 REG_NOSUB | REG_EXTENDED
488 | ((prop->matchtype == MATCHTYPE_REGEXPCASE)
489 ? REG_ICASE : 0)) != 0) {
495 if (prop->preg == NULL) {
500 if (regexec(prop->preg, str1, 0, NULL, 0) == 0)
506 if (debug_filtering_session
507 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
508 gchar *stripped = g_strdup(str);
510 strretchomp(stripped);
512 log_print(LOG_DEBUG_FILTERING,
513 "%s value [ %s ] matches regular expression [ %s ] (%s)\n",
514 debug_context, stripped, prop->expr,
515 prop->matchtype == MATCHTYPE_REGEXP ? _("Case sensitive"):_("Case insensitive"));
517 log_print(LOG_DEBUG_FILTERING,
518 "%s value [ %s ] does NOT match regular expression [ %s ] (%s)\n",
519 debug_context, stripped, prop->expr,
520 prop->matchtype == MATCHTYPE_REGEXP ? _("Case sensitive"):_("Case insensitive"));
526 case MATCHTYPE_MATCHCASE:
527 case MATCHTYPE_MATCH:
528 ret = (strstr(str1, down_expr) != NULL);
531 if (debug_filtering_session
532 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
533 gchar *stripped = g_strdup(str);
535 strretchomp(stripped);
537 log_print(LOG_DEBUG_FILTERING,
538 "%s value [ %s ] contains [ %s ] (%s)\n",
539 debug_context, stripped, prop->expr,
540 prop->matchtype == MATCHTYPE_MATCH ? _("Case sensitive"):_("Case insensitive"));
542 log_print(LOG_DEBUG_FILTERING,
543 "%s value [ %s ] does NOT contain [ %s ] (%s)\n",
544 debug_context, stripped, prop->expr,
545 prop->matchtype == MATCHTYPE_MATCH ? _("Case sensitive"):_("Case insensitive"));
564 *\brief Find out if a tag matches a condition
566 *\param prop Matcher structure
567 *\param msginfo message to check
569 *\return gboolean TRUE if msginfo matches the condition in the
572 static gboolean matcherprop_tag_match(MatcherProp *prop, MsgInfo *msginfo,
573 const gchar *debug_context)
575 gboolean ret = FALSE;
578 if (msginfo == NULL || msginfo->tags == NULL)
581 for (cur = msginfo->tags; cur; cur = cur->next) {
582 const gchar *str = tags_get_tag(GPOINTER_TO_INT(cur->data));
585 if (matcherprop_string_match(prop, str, debug_context)) {
594 *\brief Find out if the string-ed list matches a condition
596 *\param prop Matcher structure
597 *\param list GSList of strings to check
599 *\return gboolean TRUE if str matches the condition in the
602 static gboolean matcherprop_list_match(MatcherProp *prop, const GSList *list,
603 const gchar *debug_context)
607 for(cur = list; cur != NULL; cur = cur->next) {
608 if (matcherprop_string_match(prop, (gchar *)cur->data, debug_context))
614 static gboolean matcherprop_header_line_match(MatcherProp *prop, const gchar *hdr,
615 const gchar *str, const gchar *debug_context)
618 gboolean res = FALSE;
620 if (hdr == NULL || str == NULL)
623 line = g_strdup_printf("%s %s", hdr, str);
624 res = matcherprop_string_match(prop, line, debug_context);
631 typedef struct _thread_data {
638 static void *matcher_test_thread(void *data)
640 thread_data *td = (thread_data *)data;
643 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
644 pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
646 result = system(td->cmd);
647 td->done = TRUE; /* let the caller thread join() */
648 return GINT_TO_POINTER(result);
653 *\brief Execute a command defined in the matcher structure
655 *\param prop Pointer to matcher structure
656 *\param info Pointer to message info structure
658 *\return gboolean TRUE if command was executed succesfully
660 static gboolean matcherprop_match_test(const MatcherProp *prop,
669 thread_data *td = g_new0(thread_data, 1);
671 time_t start_time = time(NULL);
674 file = procmsg_get_message_file(info);
683 cmd = matching_build_command(prop->expr, info);
693 if (debug_filtering_session
694 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
695 log_print(LOG_DEBUG_FILTERING,
696 "starting threaded command [ %s ]\n",
702 if (pthread_attr_init(&pta) != 0 ||
703 pthread_attr_setdetachstate(&pta, PTHREAD_CREATE_JOINABLE) != 0 ||
704 pthread_create(&pt, &pta, matcher_test_thread, td) != 0)
705 retval = system(cmd);
707 debug_print("waiting for test thread\n");
709 /* don't let the interface freeze while waiting */
711 if (time(NULL) - start_time > 30) {
717 pthread_join(pt, &res);
718 retval = GPOINTER_TO_INT(res);
719 debug_print(" test thread returned %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 "starting synchronous command [ %s ]\n",
731 retval = system(cmd);
733 debug_print("Command exit code: %d\n", retval);
736 if (debug_filtering_session
737 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
738 log_print(LOG_DEBUG_FILTERING,
739 "command returned [ %d ]\n",
744 return (retval == 0);
748 *\brief Check if a message matches the condition in a matcher
751 *\param prop Pointer to matcher structure
752 *\param info Pointer to message info
754 *\return gboolean TRUE if a match
756 static gboolean matcherprop_match(MatcherProp *prop,
761 switch(prop->criteria) {
762 case MATCHCRITERIA_ALL:
764 case MATCHCRITERIA_UNREAD:
765 return MSG_IS_UNREAD(info->flags);
766 case MATCHCRITERIA_NOT_UNREAD:
767 return !MSG_IS_UNREAD(info->flags);
768 case MATCHCRITERIA_NEW:
769 return MSG_IS_NEW(info->flags);
770 case MATCHCRITERIA_NOT_NEW:
771 return !MSG_IS_NEW(info->flags);
772 case MATCHCRITERIA_MARKED:
773 return MSG_IS_MARKED(info->flags);
774 case MATCHCRITERIA_NOT_MARKED:
775 return !MSG_IS_MARKED(info->flags);
776 case MATCHCRITERIA_DELETED:
777 return MSG_IS_DELETED(info->flags);
778 case MATCHCRITERIA_NOT_DELETED:
779 return !MSG_IS_DELETED(info->flags);
780 case MATCHCRITERIA_REPLIED:
781 return MSG_IS_REPLIED(info->flags);
782 case MATCHCRITERIA_NOT_REPLIED:
783 return !MSG_IS_REPLIED(info->flags);
784 case MATCHCRITERIA_FORWARDED:
785 return MSG_IS_FORWARDED(info->flags);
786 case MATCHCRITERIA_NOT_FORWARDED:
787 return !MSG_IS_FORWARDED(info->flags);
788 case MATCHCRITERIA_LOCKED:
789 return MSG_IS_LOCKED(info->flags);
790 case MATCHCRITERIA_NOT_LOCKED:
791 return !MSG_IS_LOCKED(info->flags);
792 case MATCHCRITERIA_SPAM:
793 return MSG_IS_SPAM(info->flags);
794 case MATCHCRITERIA_NOT_SPAM:
795 return !MSG_IS_SPAM(info->flags);
796 case MATCHCRITERIA_HAS_ATTACHMENT:
797 return MSG_IS_WITH_ATTACHMENT(info->flags);
798 case MATCHCRITERIA_HAS_NO_ATTACHMENT:
799 return !MSG_IS_WITH_ATTACHMENT(info->flags);
800 case MATCHCRITERIA_SIGNED:
801 return MSG_IS_SIGNED(info->flags);
802 case MATCHCRITERIA_NOT_SIGNED:
803 return !MSG_IS_SIGNED(info->flags);
804 case MATCHCRITERIA_COLORLABEL:
806 gint color = MSG_GET_COLORLABEL_VALUE(info->flags);
807 gboolean ret = (color == prop->value);
810 if (debug_filtering_session
811 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
813 log_print(LOG_DEBUG_FILTERING,
814 "message color value [ %d ] matches color value [ %d ]\n",
817 log_print(LOG_DEBUG_FILTERING,
818 "message color value [ %d ] does NOT match color value [ %d ]\n",
824 case MATCHCRITERIA_NOT_COLORLABEL:
826 gint color = MSG_GET_COLORLABEL_VALUE(info->flags);
827 gboolean ret = (color != prop->value);
830 if (debug_filtering_session
831 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
833 log_print(LOG_DEBUG_FILTERING,
834 "message color value [ %d ] matches color value [ %d ]\n",
837 log_print(LOG_DEBUG_FILTERING,
838 "message color value [ %d ] does NOT match color value [ %d ]\n",
844 case MATCHCRITERIA_IGNORE_THREAD:
845 return MSG_IS_IGNORE_THREAD(info->flags);
846 case MATCHCRITERIA_NOT_IGNORE_THREAD:
847 return !MSG_IS_IGNORE_THREAD(info->flags);
848 case MATCHCRITERIA_WATCH_THREAD:
849 return MSG_IS_WATCH_THREAD(info->flags);
850 case MATCHCRITERIA_NOT_WATCH_THREAD:
851 return !MSG_IS_WATCH_THREAD(info->flags);
852 case MATCHCRITERIA_SUBJECT:
853 return matcherprop_string_match(prop, info->subject, context_str[CONTEXT_SUBJECT]);
854 case MATCHCRITERIA_NOT_SUBJECT:
855 return !matcherprop_string_match(prop, info->subject, context_str[CONTEXT_SUBJECT]);
856 case MATCHCRITERIA_FROM:
857 return matcherprop_string_match(prop, info->from, context_str[CONTEXT_FROM]);
858 case MATCHCRITERIA_NOT_FROM:
859 return !matcherprop_string_match(prop, info->from, context_str[CONTEXT_FROM]);
860 case MATCHCRITERIA_TO:
861 return matcherprop_string_match(prop, info->to, context_str[CONTEXT_TO]);
862 case MATCHCRITERIA_NOT_TO:
863 return !matcherprop_string_match(prop, info->to, context_str[CONTEXT_TO]);
864 case MATCHCRITERIA_CC:
865 return matcherprop_string_match(prop, info->cc, context_str[CONTEXT_CC]);
866 case MATCHCRITERIA_NOT_CC:
867 return !matcherprop_string_match(prop, info->cc, context_str[CONTEXT_CC]);
868 case MATCHCRITERIA_TO_OR_CC:
869 return matcherprop_string_match(prop, info->to, context_str[CONTEXT_TO])
870 || matcherprop_string_match(prop, info->cc, context_str[CONTEXT_CC]);
871 case MATCHCRITERIA_NOT_TO_AND_NOT_CC:
872 return !matcherprop_string_match(prop, info->to, context_str[CONTEXT_TO])
873 && !matcherprop_string_match(prop, info->cc, context_str[CONTEXT_CC]);
874 case MATCHCRITERIA_TAG:
875 return matcherprop_tag_match(prop, info, context_str[CONTEXT_TAG]);
876 case MATCHCRITERIA_NOT_TAG:
877 return !matcherprop_tag_match(prop, info, context_str[CONTEXT_TAG]);
878 case MATCHCRITERIA_TAGGED:
879 return info->tags != NULL;
880 case MATCHCRITERIA_NOT_TAGGED:
881 return info->tags == NULL;
882 case MATCHCRITERIA_AGE_GREATER:
888 age = ((t - info->date_t) / (60 * 60 * 24));
889 ret = (age >= prop->value);
892 if (debug_filtering_session
893 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
895 log_print(LOG_DEBUG_FILTERING,
896 "message age [ %d ] is greater than [ %d ]\n",
899 log_print(LOG_DEBUG_FILTERING,
900 "message age [ %d ] is not greater than [ %d ]\n",
906 case MATCHCRITERIA_AGE_LOWER:
912 age = ((t - info->date_t) / (60 * 60 * 24));
913 ret = (age < prop->value);
916 if (debug_filtering_session
917 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
919 log_print(LOG_DEBUG_FILTERING,
920 "message age [ %d ] is lower than [ %d ]\n",
923 log_print(LOG_DEBUG_FILTERING,
924 "message age [ %d ] is not lower than [ %d ]\n",
930 case MATCHCRITERIA_SCORE_GREATER:
932 gboolean ret = (info->score > prop->value);
935 if (debug_filtering_session
936 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
938 log_print(LOG_DEBUG_FILTERING,
939 "message score [ %d ] is greater than [ %d ]\n",
940 info->score, prop->value);
942 log_print(LOG_DEBUG_FILTERING,
943 "message score [ %d ] is not greater than [ %d ]\n",
944 info->score, prop->value);
949 case MATCHCRITERIA_SCORE_LOWER:
951 gboolean ret = (info->score < prop->value);
954 if (debug_filtering_session
955 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
957 log_print(LOG_DEBUG_FILTERING,
958 "message score [ %d ] is lower than [ %d ]\n",
959 info->score, prop->value);
961 log_print(LOG_DEBUG_FILTERING,
962 "message score [ %d ] is not lower than [ %d ]\n",
963 info->score, prop->value);
968 case MATCHCRITERIA_SCORE_EQUAL:
970 gboolean ret = (info->score == prop->value);
973 if (debug_filtering_session
974 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
976 log_print(LOG_DEBUG_FILTERING,
977 "message score [ %d ] is equal to [ %d ]\n",
978 info->score, prop->value);
980 log_print(LOG_DEBUG_FILTERING,
981 "message score [ %d ] is not equal to [ %d ]\n",
982 info->score, prop->value);
987 case MATCHCRITERIA_SIZE_GREATER:
989 /* FIXME: info->size is a goffset */
990 gboolean ret = (info->size > (goffset) 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 size is greater than [ %d ]\n",
1000 log_print(LOG_DEBUG_FILTERING,
1001 "message size is not greater than [ %d ]\n",
1007 case MATCHCRITERIA_SIZE_SMALLER:
1009 /* FIXME: info->size is a goffset */
1010 gboolean ret = (info->size < (goffset) prop->value);
1013 if (debug_filtering_session
1014 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
1016 log_print(LOG_DEBUG_FILTERING,
1017 "message size is smaller than [ %d ]\n",
1020 log_print(LOG_DEBUG_FILTERING,
1021 "message size is not smaller than [ %d ]\n",
1027 case MATCHCRITERIA_SIZE_EQUAL:
1029 /* FIXME: info->size is a goffset */
1030 gboolean ret = (info->size == (goffset) prop->value);
1033 if (debug_filtering_session
1034 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
1036 log_print(LOG_DEBUG_FILTERING,
1037 "message size is equal to [ %d ]\n",
1040 log_print(LOG_DEBUG_FILTERING,
1041 "message size is not equal to [ %d ]\n",
1047 case MATCHCRITERIA_PARTIAL:
1049 /* FIXME: info->size is a goffset */
1050 gboolean ret = (info->total_size != 0 && info->size != (goffset)info->total_size);
1053 if (debug_filtering_session
1054 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
1056 log_print(LOG_DEBUG_FILTERING,
1057 "message is partially downloaded, size is less than total size [ %d ])\n",
1060 log_print(LOG_DEBUG_FILTERING,
1061 "message is not partially downloaded\n");
1066 case MATCHCRITERIA_NOT_PARTIAL:
1068 /* FIXME: info->size is a goffset */
1069 gboolean ret = (info->total_size == 0 || info->size == (goffset)info->total_size);
1072 if (debug_filtering_session
1073 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
1075 log_print(LOG_DEBUG_FILTERING,
1076 "message is not partially downloaded\n");
1078 log_print(LOG_DEBUG_FILTERING,
1079 "message is partially downloaded, size is less than total size [ %d ])\n",
1085 case MATCHCRITERIA_NEWSGROUPS:
1086 return matcherprop_string_match(prop, info->newsgroups, context_str[CONTEXT_NEWSGROUPS]);
1087 case MATCHCRITERIA_NOT_NEWSGROUPS:
1088 return !matcherprop_string_match(prop, info->newsgroups, context_str[CONTEXT_NEWSGROUPS]);
1089 case MATCHCRITERIA_INREPLYTO:
1090 return matcherprop_string_match(prop, info->inreplyto, context_str[CONTEXT_IN_REPLY_TO]);
1091 case MATCHCRITERIA_NOT_INREPLYTO:
1092 return !matcherprop_string_match(prop, info->inreplyto, context_str[CONTEXT_IN_REPLY_TO]);
1093 case MATCHCRITERIA_REFERENCES:
1094 return matcherprop_list_match(prop, info->references, context_str[CONTEXT_REFERENCES]);
1095 case MATCHCRITERIA_NOT_REFERENCES:
1096 return !matcherprop_list_match(prop, info->references, context_str[CONTEXT_REFERENCES]);
1097 case MATCHCRITERIA_TEST:
1098 return matcherprop_match_test(prop, info);
1099 case MATCHCRITERIA_NOT_TEST:
1100 return !matcherprop_match_test(prop, info);
1106 /* ********************* MatcherList *************************** */
1109 *\brief Create a new list of matchers
1111 *\param matchers List of matcher structures
1112 *\param bool_and Operator
1114 *\return MatcherList * New list
1116 MatcherList *matcherlist_new(GSList *matchers, gboolean bool_and)
1120 cond = g_new0(MatcherList, 1);
1122 cond->matchers = matchers;
1123 cond->bool_and = bool_and;
1129 *\brief Frees a list of matchers
1131 *\param cond List of matchers
1133 void matcherlist_free(MatcherList *cond)
1137 cm_return_if_fail(cond);
1138 for (l = cond->matchers ; l != NULL ; l = g_slist_next(l)) {
1139 matcherprop_free((MatcherProp *) l->data);
1141 g_slist_free(cond->matchers);
1146 *\brief Skip all headers in a message file
1148 *\param fp Message file
1150 static void matcherlist_skip_headers(FILE *fp)
1152 gchar buf[BUFFSIZE];
1154 while (procheader_get_one_field(buf, sizeof(buf), fp, NULL) != -1)
1159 *\brief Check if a header matches a matcher condition
1161 *\param matcher Matcher structure to check header for
1162 *\param buf Header name
1164 *\return boolean TRUE if matching header
1166 static gboolean matcherprop_match_one_header(MatcherProp *matcher,
1169 gboolean result = FALSE;
1170 Header *header = NULL;
1172 switch (matcher->criteria) {
1173 case MATCHCRITERIA_HEADER:
1174 case MATCHCRITERIA_NOT_HEADER:
1175 header = procheader_parse_header(buf);
1178 if (procheader_headername_equal(header->name,
1180 if (matcher->criteria == MATCHCRITERIA_HEADER)
1181 result = matcherprop_string_match(matcher, header->body, context_str[CONTEXT_HEADER]);
1183 result = !matcherprop_string_match(matcher, header->body, context_str[CONTEXT_HEADER]);
1184 procheader_header_free(header);
1188 procheader_header_free(header);
1191 case MATCHCRITERIA_HEADERS_PART:
1192 case MATCHCRITERIA_MESSAGE:
1193 header = procheader_parse_header(buf);
1196 result = matcherprop_header_line_match(matcher,
1197 header->name, header->body, context_str[CONTEXT_HEADER_LINE]);
1198 procheader_header_free(header);
1200 case MATCHCRITERIA_NOT_HEADERS_PART:
1201 case MATCHCRITERIA_NOT_MESSAGE:
1202 header = procheader_parse_header(buf);
1205 result = !matcherprop_header_line_match(matcher,
1206 header->name, header->body, context_str[CONTEXT_HEADER_LINE]);
1207 procheader_header_free(header);
1209 case MATCHCRITERIA_FOUND_IN_ADDRESSBOOK:
1210 case MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK:
1212 GSList *address_list = NULL;
1213 gint match = MATCH_ONE;
1214 gboolean found = FALSE;
1216 /* how many address headers are me trying to mach? */
1217 if (strcasecmp(matcher->header, "Any") == 0)
1219 else if (strcasecmp(matcher->header, "All") == 0)
1222 if (match == MATCH_ONE) {
1223 /* matching one address header exactly, is that the right one? */
1224 header = procheader_parse_header(buf);
1226 !procheader_headername_equal(header->name, matcher->header))
1228 address_list = address_list_append(address_list, header->body);
1229 if (address_list == NULL)
1233 header = procheader_parse_header(buf);
1236 /* address header is one of the headers we have to match when checking
1237 for any address header or all address headers? */
1238 if (procheader_headername_equal(header->name, "From") ||
1239 procheader_headername_equal(header->name, "To") ||
1240 procheader_headername_equal(header->name, "Cc") ||
1241 procheader_headername_equal(header->name, "Reply-To") ||
1242 procheader_headername_equal(header->name, "Sender"))
1243 address_list = address_list_append(address_list, header->body);
1244 if (address_list == NULL)
1248 found = match_with_addresses_in_addressbook
1249 (matcher, address_list, matcher->criteria,
1250 matcher->expr, match);
1251 g_slist_free(address_list);
1253 if (matcher->criteria == MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK)
1264 *\brief Check if the matcher structure wants headers to
1267 *\param matcher Matcher structure
1269 *\return gboolean TRUE if the matcher structure describes
1270 * a header match condition
1272 static gboolean matcherprop_criteria_headers(const MatcherProp *matcher)
1274 switch (matcher->criteria) {
1275 case MATCHCRITERIA_HEADER:
1276 case MATCHCRITERIA_NOT_HEADER:
1277 case MATCHCRITERIA_HEADERS_PART:
1278 case MATCHCRITERIA_NOT_HEADERS_PART:
1279 case MATCHCRITERIA_FOUND_IN_ADDRESSBOOK:
1280 case MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK:
1288 *\brief Check if the matcher structure wants the message
1289 * to be matched (just perform an action on any
1292 *\param matcher Matcher structure
1294 *\return gboolean TRUE if matcher condition should match
1297 static gboolean matcherprop_criteria_message(MatcherProp *matcher)
1299 switch (matcher->criteria) {
1300 case MATCHCRITERIA_MESSAGE:
1301 case MATCHCRITERIA_NOT_MESSAGE:
1309 *\brief Check if a list of conditions matches one header in
1312 *\param matchers List of conditions
1313 *\param fp Message file
1315 *\return gboolean TRUE if one of the headers is matched by
1316 * the list of conditions.
1318 static gboolean matcherlist_match_headers(MatcherList *matchers, FILE *fp)
1321 gchar buf[BUFFSIZE];
1323 while (procheader_get_one_field(buf, sizeof(buf), fp, NULL) != -1) {
1324 for (l = matchers->matchers ; l != NULL ; l = g_slist_next(l)) {
1325 MatcherProp *matcher = (MatcherProp *) l->data;
1326 gint match = MATCH_ANY;
1331 /* determine the match range (all, any are our concern here) */
1332 if (matcher->criteria == MATCHCRITERIA_NOT_HEADERS_PART ||
1333 matcher->criteria == MATCHCRITERIA_NOT_MESSAGE) {
1336 } else if (matcher->criteria == MATCHCRITERIA_FOUND_IN_ADDRESSBOOK ||
1337 matcher->criteria == MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK) {
1338 Header *header = NULL;
1340 /* address header is one of the headers we have to match when checking
1341 for any address header or all address headers? */
1342 header = procheader_parse_header(buf);
1344 (procheader_headername_equal(header->name, "From") ||
1345 procheader_headername_equal(header->name, "To") ||
1346 procheader_headername_equal(header->name, "Cc") ||
1347 procheader_headername_equal(header->name, "Reply-To") ||
1348 procheader_headername_equal(header->name, "Sender"))) {
1350 if (strcasecmp(matcher->header, "Any") == 0)
1352 else if (strcasecmp(matcher->header, "All") == 0)
1357 /* further call to matcherprop_match_one_header() can't match
1358 and it irrelevant, so: don't alter the match result */
1363 /* ZERO line must NOT match for the rule to match.
1365 if (match == MATCH_ALL) {
1366 if (matcherprop_match_one_header(matcher, buf)) {
1367 matcher->result = TRUE;
1369 matcher->result = FALSE;
1370 matcher->done = TRUE;
1372 /* else, just one line matching is enough for the rule to match
1374 } else if (matcherprop_criteria_headers(matcher) ||
1375 matcherprop_criteria_message(matcher)) {
1376 if (matcherprop_match_one_header(matcher, buf)) {
1377 matcher->result = TRUE;
1378 matcher->done = TRUE;
1382 /* if the rule matched and the matchers are OR, no need to
1383 * check the others */
1384 if (matcher->result && matcher->done) {
1385 if (!matchers->bool_and)
1395 *\brief Check if a matcher wants to check the message body
1397 *\param matcher Matcher structure
1399 *\return gboolean TRUE if body must be matched.
1401 static gboolean matcherprop_criteria_body(const MatcherProp *matcher)
1403 switch (matcher->criteria) {
1404 case MATCHCRITERIA_BODY_PART:
1405 case MATCHCRITERIA_NOT_BODY_PART:
1412 static gboolean matcherlist_match_binary_content(MatcherList *matchers, MimeInfo *partinfo)
1415 gchar buf[BUFFSIZE];
1418 if (partinfo->type == MIMETYPE_TEXT)
1421 outfp = procmime_get_binary_content(partinfo);
1426 while (fgets(buf, sizeof(buf), outfp) != NULL) {
1429 for (l = matchers->matchers ; l != NULL ; l = g_slist_next(l)) {
1430 MatcherProp *matcher = (MatcherProp *) l->data;
1435 /* Don't scan non-text parts when looking in body, only
1436 * when looking in whole message
1438 if (partinfo && partinfo->type != MIMETYPE_TEXT &&
1439 (matcher->criteria == MATCHCRITERIA_NOT_BODY_PART ||
1440 matcher->criteria == MATCHCRITERIA_BODY_PART))
1443 /* if the criteria is ~body_part or ~message, ZERO lines
1444 * must match for the rule to match.
1446 if (matcher->criteria == MATCHCRITERIA_NOT_BODY_PART ||
1447 matcher->criteria == MATCHCRITERIA_NOT_MESSAGE) {
1448 if (matcherprop_string_match(matcher, buf,
1449 context_str[CONTEXT_BODY_LINE])) {
1450 matcher->result = FALSE;
1451 matcher->done = TRUE;
1453 matcher->result = TRUE;
1454 /* else, just one line has to match */
1455 } else if (matcherprop_criteria_body(matcher) ||
1456 matcherprop_criteria_message(matcher)) {
1457 if (matcherprop_string_match(matcher, buf,
1458 context_str[CONTEXT_BODY_LINE])) {
1459 matcher->result = TRUE;
1460 matcher->done = TRUE;
1464 /* if the matchers are OR'ed and the rule matched,
1465 * no need to check the others. */
1466 if (matcher->result && matcher->done) {
1467 if (!matchers->bool_and) {
1479 static gboolean match_content_cb(const gchar *buf, gpointer data)
1481 MatcherList *matchers = (MatcherList *)data;
1482 gboolean all_done = TRUE;
1485 for (l = matchers->matchers ; l != NULL ; l = g_slist_next(l)) {
1486 MatcherProp *matcher = (MatcherProp *) l->data;
1491 /* if the criteria is ~body_part or ~message, ZERO lines
1492 * must match for the rule to match.
1494 if (matcher->criteria == MATCHCRITERIA_NOT_BODY_PART ||
1495 matcher->criteria == MATCHCRITERIA_NOT_MESSAGE) {
1496 if (matcherprop_string_match(matcher, buf,
1497 context_str[CONTEXT_BODY_LINE])) {
1498 matcher->result = FALSE;
1499 matcher->done = TRUE;
1501 matcher->result = TRUE;
1502 /* else, just one line has to match */
1503 } else if (matcherprop_criteria_body(matcher) ||
1504 matcherprop_criteria_message(matcher)) {
1505 if (matcherprop_string_match(matcher, buf,
1506 context_str[CONTEXT_BODY_LINE])) {
1507 matcher->result = TRUE;
1508 matcher->done = TRUE;
1512 /* if the matchers are OR'ed and the rule matched,
1513 * no need to check the others. */
1514 if (matcher->result && matcher->done) {
1515 if (!matchers->bool_and) {
1526 static gboolean matcherlist_match_text_content(MatcherList *matchers, MimeInfo *partinfo)
1528 if (partinfo->type != MIMETYPE_TEXT)
1531 return procmime_scan_text_content(partinfo, match_content_cb, matchers);
1535 *\brief Check if a line in a message file's body matches
1538 *\param matchers List of conditions
1539 *\param fp Message file
1541 *\return gboolean TRUE if succesful match
1543 static gboolean matcherlist_match_body(MatcherList *matchers, gboolean body_only, MsgInfo *info)
1545 MimeInfo *mimeinfo = NULL;
1546 MimeInfo *partinfo = NULL;
1547 gboolean first_text_found = FALSE;
1549 cm_return_val_if_fail(info != NULL, FALSE);
1551 mimeinfo = procmime_scan_message(info);
1554 partinfo = procmime_mimeinfo_next(mimeinfo);
1556 for (; partinfo != NULL; partinfo = procmime_mimeinfo_next(partinfo)) {
1558 if (partinfo->type != MIMETYPE_TEXT && body_only)
1561 if (partinfo->type == MIMETYPE_TEXT) {
1562 first_text_found = TRUE;
1563 if (matcherlist_match_text_content(matchers, partinfo)) {
1564 procmime_mimeinfo_free_all(mimeinfo);
1567 } else if (matcherlist_match_binary_content(matchers, partinfo)) {
1568 procmime_mimeinfo_free_all(mimeinfo);
1572 if (body_only && first_text_found)
1575 procmime_mimeinfo_free_all(mimeinfo);
1581 *\brief Check if a message file matches criteria
1583 *\param matchers Criteria
1584 *\param info Message info
1585 *\param result Default result
1587 *\return gboolean TRUE if matched
1589 static gboolean matcherlist_match_file(MatcherList *matchers, MsgInfo *info,
1592 gboolean read_headers;
1599 /* file need to be read ? */
1601 read_headers = FALSE;
1604 for (l = matchers->matchers ; l != NULL ; l = g_slist_next(l)) {
1605 MatcherProp *matcher = (MatcherProp *) l->data;
1607 if (matcherprop_criteria_headers(matcher))
1608 read_headers = TRUE;
1609 if (matcherprop_criteria_body(matcher))
1611 if (matcherprop_criteria_message(matcher)) {
1612 read_headers = TRUE;
1616 matcher->result = FALSE;
1617 matcher->done = FALSE;
1620 if (!read_headers && !read_body)
1623 file = procmsg_get_message_file_full(info, read_headers, read_body);
1627 if ((fp = g_fopen(file, "rb")) == NULL) {
1628 FILE_OP_ERROR(file, "fopen");
1633 /* read the headers */
1636 if (matcherlist_match_headers(matchers, fp))
1639 matcherlist_skip_headers(fp);
1644 matcherlist_match_body(matchers, body_only, info);
1647 for (l = matchers->matchers; l != NULL; l = g_slist_next(l)) {
1648 MatcherProp *matcher = (MatcherProp *) l->data;
1650 if (matcherprop_criteria_headers(matcher) ||
1651 matcherprop_criteria_body(matcher) ||
1652 matcherprop_criteria_message(matcher)) {
1653 if (matcher->result) {
1654 if (!matchers->bool_and) {
1660 if (matchers->bool_and) {
1676 *\brief Test list of conditions on a message.
1678 *\param matchers List of conditions
1679 *\param info Message info
1681 *\return gboolean TRUE if matched
1683 gboolean matcherlist_match(MatcherList *matchers, MsgInfo *info)
1691 if (matchers->bool_and)
1696 /* test the cached elements */
1698 for (l = matchers->matchers; l != NULL ;l = g_slist_next(l)) {
1699 MatcherProp *matcher = (MatcherProp *) l->data;
1701 if (debug_filtering_session) {
1702 gchar *buf = matcherprop_to_string(matcher);
1703 log_print(LOG_DEBUG_FILTERING, _("checking if message matches [ %s ]\n"), buf);
1707 switch(matcher->criteria) {
1708 case MATCHCRITERIA_ALL:
1709 case MATCHCRITERIA_UNREAD:
1710 case MATCHCRITERIA_NOT_UNREAD:
1711 case MATCHCRITERIA_NEW:
1712 case MATCHCRITERIA_NOT_NEW:
1713 case MATCHCRITERIA_MARKED:
1714 case MATCHCRITERIA_NOT_MARKED:
1715 case MATCHCRITERIA_DELETED:
1716 case MATCHCRITERIA_NOT_DELETED:
1717 case MATCHCRITERIA_REPLIED:
1718 case MATCHCRITERIA_NOT_REPLIED:
1719 case MATCHCRITERIA_FORWARDED:
1720 case MATCHCRITERIA_NOT_FORWARDED:
1721 case MATCHCRITERIA_LOCKED:
1722 case MATCHCRITERIA_NOT_LOCKED:
1723 case MATCHCRITERIA_SPAM:
1724 case MATCHCRITERIA_NOT_SPAM:
1725 case MATCHCRITERIA_HAS_ATTACHMENT:
1726 case MATCHCRITERIA_HAS_NO_ATTACHMENT:
1727 case MATCHCRITERIA_SIGNED:
1728 case MATCHCRITERIA_NOT_SIGNED:
1729 case MATCHCRITERIA_COLORLABEL:
1730 case MATCHCRITERIA_NOT_COLORLABEL:
1731 case MATCHCRITERIA_IGNORE_THREAD:
1732 case MATCHCRITERIA_NOT_IGNORE_THREAD:
1733 case MATCHCRITERIA_WATCH_THREAD:
1734 case MATCHCRITERIA_NOT_WATCH_THREAD:
1735 case MATCHCRITERIA_SUBJECT:
1736 case MATCHCRITERIA_NOT_SUBJECT:
1737 case MATCHCRITERIA_FROM:
1738 case MATCHCRITERIA_NOT_FROM:
1739 case MATCHCRITERIA_TO:
1740 case MATCHCRITERIA_NOT_TO:
1741 case MATCHCRITERIA_CC:
1742 case MATCHCRITERIA_NOT_CC:
1743 case MATCHCRITERIA_TO_OR_CC:
1744 case MATCHCRITERIA_NOT_TO_AND_NOT_CC:
1745 case MATCHCRITERIA_TAG:
1746 case MATCHCRITERIA_NOT_TAG:
1747 case MATCHCRITERIA_TAGGED:
1748 case MATCHCRITERIA_NOT_TAGGED:
1749 case MATCHCRITERIA_AGE_GREATER:
1750 case MATCHCRITERIA_AGE_LOWER:
1751 case MATCHCRITERIA_NEWSGROUPS:
1752 case MATCHCRITERIA_NOT_NEWSGROUPS:
1753 case MATCHCRITERIA_INREPLYTO:
1754 case MATCHCRITERIA_NOT_INREPLYTO:
1755 case MATCHCRITERIA_REFERENCES:
1756 case MATCHCRITERIA_NOT_REFERENCES:
1757 case MATCHCRITERIA_SCORE_GREATER:
1758 case MATCHCRITERIA_SCORE_LOWER:
1759 case MATCHCRITERIA_SCORE_EQUAL:
1760 case MATCHCRITERIA_SIZE_GREATER:
1761 case MATCHCRITERIA_SIZE_SMALLER:
1762 case MATCHCRITERIA_SIZE_EQUAL:
1763 case MATCHCRITERIA_TEST:
1764 case MATCHCRITERIA_NOT_TEST:
1765 case MATCHCRITERIA_PARTIAL:
1766 case MATCHCRITERIA_NOT_PARTIAL:
1767 if (matcherprop_match(matcher, info)) {
1768 if (!matchers->bool_and) {
1769 if (debug_filtering_session)
1770 log_status_ok(LOG_DEBUG_FILTERING, _("message matches\n"));
1775 if (matchers->bool_and) {
1776 if (debug_filtering_session)
1777 log_status_nok(LOG_DEBUG_FILTERING, _("message does not match\n"));
1784 /* test the condition on the file */
1786 if (matcherlist_match_file(matchers, info, result)) {
1787 if (!matchers->bool_and) {
1788 if (debug_filtering_session)
1789 log_status_ok(LOG_DEBUG_FILTERING, _("message matches\n"));
1793 if (matchers->bool_and) {
1794 if (debug_filtering_session)
1795 log_status_nok(LOG_DEBUG_FILTERING, _("message does not match\n"));
1800 if (debug_filtering_session) {
1802 log_status_ok(LOG_DEBUG_FILTERING, _("message matches\n"));
1804 log_status_nok(LOG_DEBUG_FILTERING, _("message does not match\n"));
1810 static gint quote_filter_str(gchar * result, guint size,
1820 for(p = path ; * p != '\0' ; p ++) {
1822 if ((* p != '\"') && (* p != '\\')) {
1823 if (remaining > 0) {
1829 result[size - 1] = '\0';
1834 if (remaining >= 2) {
1842 result[size - 1] = '\0';
1847 if (remaining > 0) {
1851 result[size - 1] = '\0';
1859 gchar * matcher_quote_str(const gchar * src)
1864 len = strlen(src) * 2 + 1;
1865 res = g_malloc(len);
1866 quote_filter_str(res, len, src);
1872 *\brief Convert a matcher structure to a string
1874 *\param matcher Matcher structure
1876 *\return gchar * Newly allocated string
1878 gchar *matcherprop_to_string(MatcherProp *matcher)
1880 gchar *matcher_str = NULL;
1881 const gchar *criteria_str;
1882 const gchar *matchtype_str;
1884 gchar * quoted_expr;
1885 gchar * quoted_header;
1887 criteria_str = NULL;
1888 for (i = 0; i < (int) (sizeof(matchparser_tab) / sizeof(MatchParser)); i++) {
1889 if (matchparser_tab[i].id == matcher->criteria)
1890 criteria_str = matchparser_tab[i].str;
1892 if (criteria_str == NULL)
1895 switch (matcher->criteria) {
1896 case MATCHCRITERIA_AGE_GREATER:
1897 case MATCHCRITERIA_AGE_LOWER:
1898 case MATCHCRITERIA_SCORE_GREATER:
1899 case MATCHCRITERIA_SCORE_LOWER:
1900 case MATCHCRITERIA_SCORE_EQUAL:
1901 case MATCHCRITERIA_SIZE_GREATER:
1902 case MATCHCRITERIA_SIZE_SMALLER:
1903 case MATCHCRITERIA_SIZE_EQUAL:
1904 case MATCHCRITERIA_COLORLABEL:
1905 case MATCHCRITERIA_NOT_COLORLABEL:
1906 return g_strdup_printf("%s %i", criteria_str, matcher->value);
1907 case MATCHCRITERIA_ALL:
1908 case MATCHCRITERIA_UNREAD:
1909 case MATCHCRITERIA_NOT_UNREAD:
1910 case MATCHCRITERIA_NEW:
1911 case MATCHCRITERIA_NOT_NEW:
1912 case MATCHCRITERIA_MARKED:
1913 case MATCHCRITERIA_NOT_MARKED:
1914 case MATCHCRITERIA_DELETED:
1915 case MATCHCRITERIA_NOT_DELETED:
1916 case MATCHCRITERIA_REPLIED:
1917 case MATCHCRITERIA_NOT_REPLIED:
1918 case MATCHCRITERIA_FORWARDED:
1919 case MATCHCRITERIA_NOT_FORWARDED:
1920 case MATCHCRITERIA_LOCKED:
1921 case MATCHCRITERIA_NOT_LOCKED:
1922 case MATCHCRITERIA_SPAM:
1923 case MATCHCRITERIA_NOT_SPAM:
1924 case MATCHCRITERIA_HAS_ATTACHMENT:
1925 case MATCHCRITERIA_HAS_NO_ATTACHMENT:
1926 case MATCHCRITERIA_SIGNED:
1927 case MATCHCRITERIA_NOT_SIGNED:
1928 case MATCHCRITERIA_PARTIAL:
1929 case MATCHCRITERIA_NOT_PARTIAL:
1930 case MATCHCRITERIA_IGNORE_THREAD:
1931 case MATCHCRITERIA_NOT_IGNORE_THREAD:
1932 case MATCHCRITERIA_WATCH_THREAD:
1933 case MATCHCRITERIA_NOT_WATCH_THREAD:
1934 case MATCHCRITERIA_TAGGED:
1935 case MATCHCRITERIA_NOT_TAGGED:
1936 return g_strdup(criteria_str);
1937 case MATCHCRITERIA_TEST:
1938 case MATCHCRITERIA_NOT_TEST:
1939 quoted_expr = matcher_quote_str(matcher->expr);
1940 matcher_str = g_strdup_printf("%s \"%s\"",
1941 criteria_str, quoted_expr);
1942 g_free(quoted_expr);
1944 case MATCHCRITERIA_FOUND_IN_ADDRESSBOOK:
1945 case MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK:
1946 quoted_header = matcher_quote_str(matcher->header);
1947 quoted_expr = matcher_quote_str(matcher->expr);
1948 matcher_str = g_strdup_printf("%s \"%s\" in \"%s\"",
1949 criteria_str, quoted_header, quoted_expr);
1950 g_free(quoted_header);
1951 g_free(quoted_expr);
1955 matchtype_str = NULL;
1956 for (i = 0; i < sizeof matchparser_tab / sizeof matchparser_tab[0]; i++) {
1957 if (matchparser_tab[i].id == matcher->matchtype)
1958 matchtype_str = matchparser_tab[i].str;
1961 if (matchtype_str == NULL)
1964 switch (matcher->matchtype) {
1965 case MATCHTYPE_MATCH:
1966 case MATCHTYPE_MATCHCASE:
1967 case MATCHTYPE_REGEXP:
1968 case MATCHTYPE_REGEXPCASE:
1969 quoted_expr = matcher_quote_str(matcher->expr);
1970 if (matcher->header) {
1971 quoted_header = matcher_quote_str(matcher->header);
1972 matcher_str = g_strdup_printf
1973 ("%s \"%s\" %s \"%s\"",
1974 criteria_str, quoted_header,
1975 matchtype_str, quoted_expr);
1976 g_free(quoted_header);
1979 matcher_str = g_strdup_printf
1980 ("%s %s \"%s\"", criteria_str,
1981 matchtype_str, quoted_expr);
1982 g_free(quoted_expr);
1990 *\brief Convert a list of conditions to a string
1992 *\param matchers List of conditions
1994 *\return gchar * Newly allocated string
1996 gchar *matcherlist_to_string(const MatcherList *matchers)
2002 gchar *result = NULL;
2004 count = g_slist_length(matchers->matchers);
2005 vstr = g_new(gchar *, count + 1);
2007 for (l = matchers->matchers, cur_str = vstr; l != NULL;
2008 l = g_slist_next(l), cur_str ++) {
2009 *cur_str = matcherprop_to_string((MatcherProp *) l->data);
2010 if (*cur_str == NULL)
2015 if (matchers->bool_and)
2016 result = g_strjoinv(" & ", vstr);
2018 result = g_strjoinv(" | ", vstr);
2020 for (cur_str = vstr ; *cur_str != NULL ; cur_str ++)
2028 #define STRLEN_ZERO(s) ((s) ? strlen(s) : 0)
2029 #define STRLEN_DEFAULT(s,d) ((s) ? strlen(s) : STRLEN_ZERO(d))
2031 static void add_str_default(gchar ** dest,
2032 const gchar * s, const gchar * d)
2034 gchar quoted_str[4096];
2042 quote_cmd_argument(quoted_str, sizeof(quoted_str), str);
2043 strcpy(* dest, quoted_str);
2045 (* dest) += strlen(* dest);
2048 /* matching_build_command() - preferably cmd should be unescaped */
2050 *\brief Build the command-line to execute
2052 *\param cmd String with command-line specifiers
2053 *\param info Message info to use for command
2055 *\return gchar * Newly allocated string
2057 gchar *matching_build_command(const gchar *cmd, MsgInfo *info)
2059 const gchar *s = cmd;
2060 gchar *filename = NULL;
2061 gchar *processed_cmd;
2065 const gchar *const no_subject = _("(none)") ;
2066 const gchar *const no_from = _("(none)") ;
2067 const gchar *const no_to = _("(none)") ;
2068 const gchar *const no_cc = _("(none)") ;
2069 const gchar *const no_date = _("(none)") ;
2070 const gchar *const no_msgid = _("(none)") ;
2071 const gchar *const no_newsgroups = _("(none)") ;
2072 const gchar *const no_references = _("(none)") ;
2074 size = STRLEN_ZERO(cmd) + 1;
2075 while (*s != '\0') {
2082 case 's': /* subject */
2083 size += STRLEN_DEFAULT(info->subject, no_subject) - 2;
2085 case 'f': /* from */
2086 size += STRLEN_DEFAULT(info->from, no_from) - 2;
2089 size += STRLEN_DEFAULT(info->to, no_to) - 2;
2092 size += STRLEN_DEFAULT(info->cc, no_cc) - 2;
2094 case 'd': /* date */
2095 size += STRLEN_DEFAULT(info->date, no_date) - 2;
2097 case 'i': /* message-id */
2098 size += STRLEN_DEFAULT(info->msgid, no_msgid) - 2;
2100 case 'n': /* newsgroups */
2101 size += STRLEN_DEFAULT(info->newsgroups, no_newsgroups) - 2;
2103 case 'r': /* references */
2104 /* FIXME: using the inreplyto header for reference */
2105 size += STRLEN_DEFAULT(info->inreplyto, no_references) - 2;
2107 case 'F': /* file */
2108 if (filename == NULL)
2109 filename = folder_item_fetch_msg(info->folder, info->msgnum);
2111 if (filename == NULL) {
2112 g_warning("filename is not set");
2116 size += strlen(filename) - 2;
2125 /* as the string can be quoted, we double the result */
2128 processed_cmd = g_new0(gchar, size);
2132 while (*s != '\0') {
2140 case 's': /* subject */
2141 add_str_default(&p, info->subject,
2144 case 'f': /* from */
2145 add_str_default(&p, info->from,
2149 add_str_default(&p, info->to,
2153 add_str_default(&p, info->cc,
2156 case 'd': /* date */
2157 add_str_default(&p, info->date,
2160 case 'i': /* message-id */
2161 add_str_default(&p, info->msgid,
2164 case 'n': /* newsgroups */
2165 add_str_default(&p, info->newsgroups,
2168 case 'r': /* references */
2169 /* FIXME: using the inreplyto header for references */
2170 add_str_default(&p, info->inreplyto, no_references);
2172 case 'F': /* file */
2173 if (filename != NULL)
2174 add_str_default(&p, filename, NULL);
2193 return processed_cmd;
2195 #undef STRLEN_DEFAULT
2198 /* ************************************************************ */
2202 *\brief Write filtering list to file
2205 *\param prefs_filtering List of filtering conditions
2207 static int prefs_filtering_write(FILE *fp, GSList *prefs_filtering)
2211 for (cur = prefs_filtering; cur != NULL; cur = cur->next) {
2212 gchar *filtering_str = NULL;
2213 gchar *tmp_name = NULL;
2214 FilteringProp *prop = NULL;
2216 if (NULL == (prop = (FilteringProp *) cur->data))
2219 if (NULL == (filtering_str = filteringprop_to_string(prop)))
2222 if (prop->enabled) {
2223 if (fputs("enabled ", fp) == EOF) {
2224 FILE_OP_ERROR("filtering config", "fputs");
2228 if (fputs("disabled ", fp) == EOF) {
2229 FILE_OP_ERROR("filtering config", "fputs");
2234 if (fputs("rulename \"", fp) == EOF) {
2235 FILE_OP_ERROR("filtering config", "fputs");
2236 g_free(filtering_str);
2239 tmp_name = prop->name;
2240 while (tmp_name && *tmp_name != '\0') {
2241 if (*tmp_name != '"') {
2242 if (fputc(*tmp_name, fp) == EOF) {
2243 FILE_OP_ERROR("filtering config", "fputs || fputc");
2244 g_free(filtering_str);
2247 } else if (*tmp_name == '"') {
2248 if (fputc('\\', fp) == EOF ||
2249 fputc('"', fp) == EOF) {
2250 FILE_OP_ERROR("filtering config", "fputs || fputc");
2251 g_free(filtering_str);
2257 if (fputs("\" ", fp) == EOF) {
2258 FILE_OP_ERROR("filtering config", "fputs");
2259 g_free(filtering_str);
2263 if (prop->account_id != 0) {
2266 tmp = g_strdup_printf("account %d ", prop->account_id);
2267 if (fputs(tmp, fp) == EOF) {
2268 FILE_OP_ERROR("filtering config", "fputs");
2275 if(fputs(filtering_str, fp) == EOF ||
2276 fputc('\n', fp) == EOF) {
2277 FILE_OP_ERROR("filtering config", "fputs || fputc");
2278 g_free(filtering_str);
2281 g_free(filtering_str);
2287 typedef struct _NodeLoopData {
2293 *\brief Write matchers from a folder item
2295 *\param node Node with folder info
2296 *\param data File pointer
2298 *\return gboolean FALSE
2300 static gboolean prefs_matcher_write_func(GNode *node, gpointer d)
2303 NodeLoopData *data = (NodeLoopData *)d;
2305 GSList *prefs_filtering;
2308 /* prevent warning */
2309 if (item->path == NULL)
2311 id = folder_item_get_identifier(item);
2314 prefs_filtering = item->prefs->processing;
2316 if (prefs_filtering != NULL) {
2317 if (fprintf(data->fp, "[%s]\n", id) < 0) {
2321 if (prefs_filtering_write(data->fp, prefs_filtering) < 0) {
2325 if (fputc('\n', data->fp) == EOF) {
2337 *\brief Save matchers from folder items
2341 static int prefs_matcher_save(FILE *fp)
2349 for (cur = folder_get_list() ; cur != NULL ; cur = g_list_next(cur)) {
2352 folder = (Folder *) cur->data;
2353 g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
2354 prefs_matcher_write_func, &data);
2357 if (data.error == TRUE)
2360 /* pre global rules */
2361 if (fprintf(fp, "[preglobal]\n") < 0 ||
2362 prefs_filtering_write(fp, pre_global_processing) < 0 ||
2363 fputc('\n', fp) == EOF)
2366 /* post global rules */
2367 if (fprintf(fp, "[postglobal]\n") < 0 ||
2368 prefs_filtering_write(fp, post_global_processing) < 0 ||
2369 fputc('\n', fp) == EOF)
2372 /* filtering rules */
2373 if (fprintf(fp, "[filtering]\n") < 0 ||
2374 prefs_filtering_write(fp, filtering_rules) < 0 ||
2375 fputc('\n', fp) == EOF)
2382 *\brief Write filtering / matcher configuration file
2384 void prefs_matcher_write_config(void)
2389 debug_print("Writing matcher configuration...\n");
2391 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
2394 if ((pfile = prefs_write_open(rcpath)) == NULL) {
2395 g_warning("failed to write configuration to file\n");
2402 if (prefs_matcher_save(pfile->fp) < 0) {
2403 g_warning("failed to write configuration to file\n");
2404 prefs_file_close_revert(pfile);
2405 } else if (prefs_file_close(pfile) < 0) {
2406 g_warning("failed to save configuration to file\n");
2410 /* ******************************************************************* */
2412 static void matcher_add_rulenames(const gchar *rcpath)
2414 gchar *newpath = g_strconcat(rcpath, ".new", NULL);
2415 FILE *src = g_fopen(rcpath, "rb");
2416 FILE *dst = g_fopen(newpath, "wb");
2417 gchar buf[BUFFSIZE];
2434 while (fgets (buf, sizeof(buf), src) != NULL) {
2435 if (strlen(buf) > 2 && buf[0] != '['
2436 && strncmp(buf, "rulename \"", 10)
2437 && strncmp(buf, "enabled rulename \"", 18)
2438 && strncmp(buf, "disabled rulename \"", 18)) {
2439 r = fwrite("enabled rulename \"\" ",
2440 strlen("enabled rulename \"\" "), 1, dst);
2442 g_message("cannot fwrite rulename\n");
2445 r = fwrite(buf, strlen(buf), 1, dst);
2447 g_message("cannot fwrite rule\n");
2452 move_file(newpath, rcpath, TRUE);
2457 *\brief Read matcher configuration
2459 void prefs_matcher_read_config(void)
2462 gchar *rc_old_format;
2465 create_matchparser_hashtab();
2466 prefs_filtering_clear();
2468 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MATCHER_RC, NULL);
2469 rc_old_format = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MATCHER_RC,
2470 ".pre_names", NULL);
2472 if (!is_file_exist(rc_old_format) && is_file_exist(rcpath)) {
2473 /* backup file with no rules names, in case
2474 * anything goes wrong */
2475 copy_file(rcpath, rc_old_format, FALSE);
2476 /* now hack the file in order to have it to the new format */
2477 matcher_add_rulenames(rcpath);
2480 g_free(rc_old_format);
2482 f = g_fopen(rcpath, "rb");
2486 matcher_parser_start_parsing(f);
2487 fclose(matcher_parserin);
2490 /* previous version compatibility */
2492 /* g_print("reading filtering\n"); */
2493 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
2494 FILTERING_RC, NULL);
2495 f = g_fopen(rcpath, "rb");
2499 matcher_parser_start_parsing(f);
2500 fclose(matcher_parserin);
2503 /* g_print("reading scoring\n"); */
2504 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
2506 f = g_fopen(rcpath, "rb");
2510 matcher_parser_start_parsing(f);
2511 fclose(matcher_parserin);