2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 2002-2004 by the Claws Mail Team and Hiroyuki Yamamoto
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #include <glib/gi18n.h>
37 #include "procheader.h"
39 #include "matcher_parser.h"
40 #include "prefs_gtk.h"
41 #include "addr_compl.h"
43 #include "quoted-printable.h"
46 #include "prefs_common.h"
51 *\brief Keyword lookup element
54 gint id; /*!< keyword id */
55 gchar *str; /*!< keyword */
57 typedef struct _MatchParser MatchParser;
60 *\brief Table with strings and ids used by the lexer and
61 * the parser. New keywords can be added here.
63 static const MatchParser matchparser_tab[] = {
65 {MATCHCRITERIA_ALL, "all"},
66 {MATCHCRITERIA_UNREAD, "unread"},
67 {MATCHCRITERIA_NOT_UNREAD, "~unread"},
68 {MATCHCRITERIA_NEW, "new"},
69 {MATCHCRITERIA_NOT_NEW, "~new"},
70 {MATCHCRITERIA_MARKED, "marked"},
71 {MATCHCRITERIA_NOT_MARKED, "~marked"},
72 {MATCHCRITERIA_DELETED, "deleted"},
73 {MATCHCRITERIA_NOT_DELETED, "~deleted"},
74 {MATCHCRITERIA_REPLIED, "replied"},
75 {MATCHCRITERIA_NOT_REPLIED, "~replied"},
76 {MATCHCRITERIA_FORWARDED, "forwarded"},
77 {MATCHCRITERIA_NOT_FORWARDED, "~forwarded"},
78 {MATCHCRITERIA_LOCKED, "locked"},
79 {MATCHCRITERIA_NOT_LOCKED, "~locked"},
80 {MATCHCRITERIA_COLORLABEL, "colorlabel"},
81 {MATCHCRITERIA_NOT_COLORLABEL, "~colorlabel"},
82 {MATCHCRITERIA_IGNORE_THREAD, "ignore_thread"},
83 {MATCHCRITERIA_NOT_IGNORE_THREAD, "~ignore_thread"},
84 {MATCHCRITERIA_WATCH_THREAD, "watch_thread"},
85 {MATCHCRITERIA_NOT_WATCH_THREAD, "~watch_thread"},
86 {MATCHCRITERIA_SPAM, "spam"},
87 {MATCHCRITERIA_NOT_SPAM, "~spam"},
88 {MATCHCRITERIA_HAS_ATTACHMENT, "has_attachment"},
89 {MATCHCRITERIA_HAS_NO_ATTACHMENT, "~has_attachment"},
90 {MATCHCRITERIA_SIGNED, "signed"},
91 {MATCHCRITERIA_NOT_SIGNED, "~signed"},
94 {MATCHCRITERIA_SUBJECT, "subject"},
95 {MATCHCRITERIA_NOT_SUBJECT, "~subject"},
96 {MATCHCRITERIA_FROM, "from"},
97 {MATCHCRITERIA_NOT_FROM, "~from"},
98 {MATCHCRITERIA_TO, "to"},
99 {MATCHCRITERIA_NOT_TO, "~to"},
100 {MATCHCRITERIA_CC, "cc"},
101 {MATCHCRITERIA_NOT_CC, "~cc"},
102 {MATCHCRITERIA_TO_OR_CC, "to_or_cc"},
103 {MATCHCRITERIA_NOT_TO_AND_NOT_CC, "~to_or_cc"},
104 {MATCHCRITERIA_TAG, "tag"},
105 {MATCHCRITERIA_NOT_TAG, "~tag"},
106 {MATCHCRITERIA_TAGGED, "tagged"},
107 {MATCHCRITERIA_NOT_TAGGED, "~tagged"},
108 {MATCHCRITERIA_AGE_GREATER, "age_greater"},
109 {MATCHCRITERIA_AGE_LOWER, "age_lower"},
110 {MATCHCRITERIA_NEWSGROUPS, "newsgroups"},
111 {MATCHCRITERIA_NOT_NEWSGROUPS, "~newsgroups"},
112 {MATCHCRITERIA_INREPLYTO, "inreplyto"},
113 {MATCHCRITERIA_NOT_INREPLYTO, "~inreplyto"},
114 {MATCHCRITERIA_REFERENCES, "references"},
115 {MATCHCRITERIA_NOT_REFERENCES, "~references"},
116 {MATCHCRITERIA_SCORE_GREATER, "score_greater"},
117 {MATCHCRITERIA_SCORE_LOWER, "score_lower"},
118 {MATCHCRITERIA_SCORE_EQUAL, "score_equal"},
119 {MATCHCRITERIA_PARTIAL, "partial"},
120 {MATCHCRITERIA_NOT_PARTIAL, "~partial"},
121 {MATCHCRITERIA_FOUND_IN_ADDRESSBOOK, "found_in_addressbook"},
122 {MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK, "~found_in_addressbook"},
124 {MATCHCRITERIA_SIZE_GREATER, "size_greater"},
125 {MATCHCRITERIA_SIZE_SMALLER, "size_smaller"},
126 {MATCHCRITERIA_SIZE_EQUAL, "size_equal"},
128 /* content have to be read */
129 {MATCHCRITERIA_HEADER, "header"},
130 {MATCHCRITERIA_NOT_HEADER, "~header"},
131 {MATCHCRITERIA_HEADERS_PART, "headers_part"},
132 {MATCHCRITERIA_NOT_HEADERS_PART, "~headers_part"},
133 {MATCHCRITERIA_MESSAGE, "message"},
134 {MATCHCRITERIA_NOT_MESSAGE, "~message"},
135 {MATCHCRITERIA_BODY_PART, "body_part"},
136 {MATCHCRITERIA_NOT_BODY_PART, "~body_part"},
137 {MATCHCRITERIA_TEST, "test"},
138 {MATCHCRITERIA_NOT_TEST, "~test"},
141 {MATCHTYPE_MATCHCASE, "matchcase"},
142 {MATCHTYPE_MATCH, "match"},
143 {MATCHTYPE_REGEXPCASE, "regexpcase"},
144 {MATCHTYPE_REGEXP, "regexp"},
147 {MATCHACTION_SCORE, "score"}, /* for backward compatibility */
148 {MATCHACTION_MOVE, "move"},
149 {MATCHACTION_COPY, "copy"},
150 {MATCHACTION_DELETE, "delete"},
151 {MATCHACTION_MARK, "mark"},
152 {MATCHACTION_UNMARK, "unmark"},
153 {MATCHACTION_LOCK, "lock"},
154 {MATCHACTION_UNLOCK, "unlock"},
155 {MATCHACTION_MARK_AS_READ, "mark_as_read"},
156 {MATCHACTION_MARK_AS_UNREAD, "mark_as_unread"},
157 {MATCHACTION_MARK_AS_SPAM, "mark_as_spam"},
158 {MATCHACTION_MARK_AS_HAM, "mark_as_ham"},
159 {MATCHACTION_FORWARD, "forward"},
160 {MATCHACTION_FORWARD_AS_ATTACHMENT, "forward_as_attachment"},
161 {MATCHACTION_EXECUTE, "execute"},
162 {MATCHACTION_COLOR, "color"},
163 {MATCHACTION_REDIRECT, "redirect"},
164 {MATCHACTION_CHANGE_SCORE, "change_score"},
165 {MATCHACTION_SET_SCORE, "set_score"},
166 {MATCHACTION_STOP, "stop"},
167 {MATCHACTION_HIDE, "hide"},
168 {MATCHACTION_IGNORE, "ignore"},
169 {MATCHACTION_WATCH, "watch"},
170 {MATCHACTION_ADD_TO_ADDRESSBOOK, "add_to_addressbook"},
171 {MATCHACTION_SET_TAG, "set_tag"},
172 {MATCHACTION_UNSET_TAG, "unset_tag"},
173 {MATCHACTION_CLEAR_TAGS, "clear_tags"},
182 extern gboolean debug_filtering_session;
185 *\brief Look up table with keywords defined in \sa matchparser_tab
187 static GHashTable *matchparser_hashtab;
190 *\brief Translate keyword id to keyword string
192 *\param id Id of keyword
194 *\return const gchar * Keyword
196 const gchar *get_matchparser_tab_str(gint id)
200 for (i = 0; i < sizeof matchparser_tab / sizeof matchparser_tab[0]; i++) {
201 if (matchparser_tab[i].id == id)
202 return matchparser_tab[i].str;
208 *\brief Create keyword lookup table
210 static void create_matchparser_hashtab(void)
214 if (matchparser_hashtab) return;
215 matchparser_hashtab = g_hash_table_new(g_str_hash, g_str_equal);
216 for (i = 0; i < sizeof matchparser_tab / sizeof matchparser_tab[0]; i++)
217 g_hash_table_insert(matchparser_hashtab,
218 matchparser_tab[i].str,
219 (gpointer) &matchparser_tab[i]);
223 *\brief Return a keyword id from a keyword string
225 *\param str Keyword string
227 *\return gint Keyword id
229 gint get_matchparser_tab_id(const gchar *str)
233 if (NULL != (res = g_hash_table_lookup(matchparser_hashtab, str))) {
239 /* **************** data structure allocation **************** */
242 *\brief Allocate a structure for a filtering / scoring
243 * "condition" (a matcher structure)
245 *\param criteria Criteria ID (MATCHCRITERIA_XXXX)
246 *\param header Header string (if criteria is MATCHCRITERIA_HEADER
247 or MATCHCRITERIA_FOUND_IN_ADDRESSBOOK)
248 *\param matchtype Type of action (MATCHTYPE_XXX)
249 *\param expr String value or expression to check
250 *\param value Integer value to check
252 *\return MatcherProp * Pointer to newly allocated structure
254 MatcherProp *matcherprop_new(gint criteria, const gchar *header,
255 gint matchtype, const gchar *expr,
260 prop = g_new0(MatcherProp, 1);
261 prop->criteria = criteria;
262 prop->header = header != NULL ? g_strdup(header) : NULL;
264 prop->expr = expr != NULL ? g_strdup(expr) : NULL;
266 prop->matchtype = matchtype;
275 *\brief Allocate a structure for a filtering / scoring
276 * "condition" (a matcher structure)
277 * Same as matcherprop_new, except it doesn't change the expr's
280 *\param criteria Criteria ID (MATCHCRITERIA_XXXX)
281 *\param header Header string (if criteria is MATCHCRITERIA_HEADER
282 or MATCHCRITERIA_FOUND_IN_ADDRESSBOOK)
283 *\param matchtype Type of action (MATCHTYPE_XXX)
284 *\param expr String value or expression to check
285 *\param value Integer value to check
287 *\return MatcherProp * Pointer to newly allocated structure
289 MatcherProp *matcherprop_new_create(gint criteria, const gchar *header,
290 gint matchtype, const gchar *expr,
295 prop = g_new0(MatcherProp, 1);
296 prop->criteria = criteria;
297 prop->header = header != NULL ? g_strdup(header) : NULL;
299 prop->expr = expr != NULL ? g_strdup(expr) : NULL;
301 prop->matchtype = matchtype;
310 *\brief Free a matcher structure
312 *\param prop Pointer to matcher structure allocated with
315 void matcherprop_free(MatcherProp *prop)
318 g_free(prop->header);
319 if (prop->preg != NULL) {
327 *\brief Copy a matcher structure
329 *\param src Matcher structure to copy
331 *\return MatcherProp * Pointer to newly allocated matcher structure
333 MatcherProp *matcherprop_copy(const MatcherProp *src)
335 MatcherProp *prop = g_new0(MatcherProp, 1);
337 prop->criteria = src->criteria;
338 prop->header = src->header ? g_strdup(src->header) : NULL;
339 prop->expr = src->expr ? g_strdup(src->expr) : NULL;
340 prop->matchtype = src->matchtype;
342 prop->preg = NULL; /* will be re-evaluated */
343 prop->value = src->value;
344 prop->error = src->error;
348 /* ************** match ******************************/
350 static gboolean match_with_addresses_in_addressbook
351 (MatcherProp *prop, GSList *address_list, gint type,
352 gchar* folderpath, gint match)
355 gboolean found = FALSE;
358 cm_return_val_if_fail(address_list != NULL, FALSE);
360 debug_print("match_with_addresses_in_addressbook(%d, %s)\n",
361 g_slist_length(address_list), folderpath?folderpath:"(null)");
363 if (folderpath == NULL ||
364 strcasecmp(folderpath, "Any") == 0 ||
370 start_address_completion(path);
372 for (walk = address_list; walk != NULL; walk = walk->next) {
373 /* exact matching of email address */
374 guint num_addr = complete_address(walk->data);
377 /* skip first item (this is the search string itself) */
379 for (; i < num_addr && !found; i++) {
380 gchar *addr = get_complete_address(i);
381 extract_address(addr);
382 if (strcasecmp(addr, walk->data) == 0) {
386 if (debug_filtering_session
387 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
388 log_print(LOG_DEBUG_FILTERING,
389 "address [ %s ] matches\n",
390 (gchar *)walk->data);
397 if (debug_filtering_session
398 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH
400 log_print(LOG_DEBUG_FILTERING,
401 "address [ %s ] does NOT match\n",
402 (gchar *)walk->data);
406 if (match == MATCH_ALL) {
407 /* if matching all addresses, stop if one doesn't match */
410 if (debug_filtering_session
411 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
412 log_print(LOG_DEBUG_FILTERING,
413 "not all address match (matching all)\n");
417 } else if (match == MATCH_ANY) {
418 /* if matching any address, stop if one does match */
421 if (debug_filtering_session
422 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
423 log_print(LOG_DEBUG_FILTERING,
424 "at least one address matches (matching any)\n");
429 /* MATCH_ONE: there should be only one loop iteration */
432 end_address_completion();
438 *\brief Find out if a string matches a condition
440 *\param prop Matcher structure
441 *\param str String to check
443 *\return gboolean TRUE if str matches the condition in the
446 static gboolean matcherprop_string_match(MatcherProp *prop, const gchar *str,
447 const gchar *debug_context)
451 gboolean ret = FALSE;
452 gboolean should_free = FALSE;
456 if (prop->matchtype == MATCHTYPE_REGEXPCASE ||
457 prop->matchtype == MATCHTYPE_MATCHCASE) {
458 str1 = g_utf8_casefold(str, -1);
459 down_expr = g_utf8_casefold(prop->expr, -1);
463 down_expr = (gchar *)prop->expr;
467 switch (prop->matchtype) {
468 case MATCHTYPE_REGEXPCASE:
469 case MATCHTYPE_REGEXP:
471 if (!prop->preg && (prop->error == 0)) {
472 prop->preg = g_new0(regex_t, 1);
473 /* if regexp then don't use the escaped string */
474 if (regcomp(prop->preg, down_expr,
475 REG_NOSUB | REG_EXTENDED
476 | ((prop->matchtype == MATCHTYPE_REGEXPCASE)
477 ? REG_ICASE : 0)) != 0) {
483 if (prop->preg == NULL) {
488 if (regexec(prop->preg, str1, 0, NULL, 0) == 0)
494 if (debug_filtering_session
495 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
496 gchar *stripped = g_strdup(str);
498 strretchomp(stripped);
500 log_print(LOG_DEBUG_FILTERING,
501 "%s value [ %s ] matches regular expression [ %s ] (%s)\n",
502 debug_context, stripped, prop->expr,
503 prop->matchtype == MATCHTYPE_REGEXP ? _("Case sensitive"):_("Case insensitive"));
505 log_print(LOG_DEBUG_FILTERING,
506 "%s value [ %s ] does NOT match regular expression [ %s ] (%s)\n",
507 debug_context, stripped, prop->expr,
508 prop->matchtype == MATCHTYPE_REGEXP ? _("Case sensitive"):_("Case insensitive"));
514 case MATCHTYPE_MATCHCASE:
515 case MATCHTYPE_MATCH:
516 ret = (strstr(str1, down_expr) != NULL);
519 if (debug_filtering_session
520 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
521 gchar *stripped = g_strdup(str);
523 strretchomp(stripped);
525 log_print(LOG_DEBUG_FILTERING,
526 "%s value [ %s ] contains [ %s ] (%s)\n",
527 debug_context, stripped, prop->expr,
528 prop->matchtype == MATCHTYPE_MATCH ? _("Case sensitive"):_("Case insensitive"));
530 log_print(LOG_DEBUG_FILTERING,
531 "%s value [ %s ] does NOT contain [ %s ] (%s)\n",
532 debug_context, stripped, prop->expr,
533 prop->matchtype == MATCHTYPE_MATCH ? _("Case sensitive"):_("Case insensitive"));
552 *\brief Find out if a tag matches a condition
554 *\param prop Matcher structure
555 *\param msginfo message to check
557 *\return gboolean TRUE if msginfo matches the condition in the
560 static gboolean matcherprop_tag_match(MatcherProp *prop, MsgInfo *msginfo,
561 const gchar *debug_context)
563 gboolean ret = FALSE;
566 if (msginfo == NULL || msginfo->tags == NULL)
569 for (cur = msginfo->tags; cur; cur = cur->next) {
570 const gchar *str = tags_get_tag(GPOINTER_TO_INT(cur->data));
573 if (matcherprop_string_match(prop, str, debug_context)) {
582 *\brief Find out if the string-ed list matches a condition
584 *\param prop Matcher structure
585 *\param list GSList of strings to check
587 *\return gboolean TRUE if str matches the condition in the
590 static gboolean matcherprop_list_match(MatcherProp *prop, const GSList *list,
591 const gchar *debug_context)
595 for(cur = list; cur != NULL; cur = cur->next) {
596 if (matcherprop_string_match(prop, (gchar *)cur->data, debug_context))
602 /* FIXME body search is a hack. */
603 static gboolean matcherprop_string_decode_match(MatcherProp *prop, const gchar *str,
604 const gchar *debug_context)
608 gboolean res = FALSE;
613 /* we try to decode QP first, because it's faster than base64 */
614 qp_decode_const(tmp, BUFFSIZE-1, str);
615 if (!g_utf8_validate(tmp, -1, NULL)) {
616 utf = conv_codeset_strdup
617 (tmp, conv_get_locale_charset_str_no_utf8(),
619 res = matcherprop_string_match(prop, utf, debug_context);
622 res = matcherprop_string_match(prop, tmp, debug_context);
625 if (res == FALSE && (strchr(prop->expr, '=') || strchr(prop->expr, '_')
626 || strchr(str, '=') || strchr(str, '_'))) {
627 /* if searching for something with an equal char, maybe
628 * we should try to match the non-decoded string.
629 * In case it was not qp-encoded. */
630 if (!g_utf8_validate(str, -1, NULL)) {
631 utf = conv_codeset_strdup
632 (str, conv_get_locale_charset_str_no_utf8(),
634 res = matcherprop_string_match(prop, utf, debug_context);
637 res = matcherprop_string_match(prop, str, debug_context);
641 /* FIXME base64 decoding is too slow, especially since text can
642 * easily be handled as base64. Don't even try now. */
648 typedef struct _thread_data {
655 static void *matcher_test_thread(void *data)
657 thread_data *td = (thread_data *)data;
660 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
661 pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
663 result = system(td->cmd);
664 td->done = TRUE; /* let the caller thread join() */
665 return GINT_TO_POINTER(result);
670 *\brief Execute a command defined in the matcher structure
672 *\param prop Pointer to matcher structure
673 *\param info Pointer to message info structure
675 *\return gboolean TRUE if command was executed succesfully
677 static gboolean matcherprop_match_test(const MatcherProp *prop,
686 thread_data *td = g_new0(thread_data, 1);
688 time_t start_time = time(NULL);
691 file = procmsg_get_message_file(info);
700 cmd = matching_build_command(prop->expr, info);
710 if (debug_filtering_session
711 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
712 log_print(LOG_DEBUG_FILTERING,
713 "starting threaded command [ %s ]\n",
719 if (pthread_attr_init(&pta) != 0 ||
720 pthread_attr_setdetachstate(&pta, PTHREAD_CREATE_JOINABLE) != 0 ||
721 pthread_create(&pt, &pta, matcher_test_thread, td) != 0)
722 retval = system(cmd);
724 debug_print("waiting for test thread\n");
726 /* don't let the interface freeze while waiting */
728 if (time(NULL) - start_time > 30) {
734 pthread_join(pt, &res);
735 retval = GPOINTER_TO_INT(res);
736 debug_print(" test thread returned %d\n", retval);
741 if (debug_filtering_session
742 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
743 log_print(LOG_DEBUG_FILTERING,
744 "starting synchronous command [ %s ]\n",
748 retval = system(cmd);
750 debug_print("Command exit code: %d\n", retval);
753 if (debug_filtering_session
754 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
755 log_print(LOG_DEBUG_FILTERING,
756 "command returned [ %d ]\n",
761 return (retval == 0);
765 *\brief Check if a message matches the condition in a matcher
768 *\param prop Pointer to matcher structure
769 *\param info Pointer to message info
771 *\return gboolean TRUE if a match
773 gboolean matcherprop_match(MatcherProp *prop,
778 switch(prop->criteria) {
779 case MATCHCRITERIA_ALL:
781 case MATCHCRITERIA_UNREAD:
782 return MSG_IS_UNREAD(info->flags);
783 case MATCHCRITERIA_NOT_UNREAD:
784 return !MSG_IS_UNREAD(info->flags);
785 case MATCHCRITERIA_NEW:
786 return MSG_IS_NEW(info->flags);
787 case MATCHCRITERIA_NOT_NEW:
788 return !MSG_IS_NEW(info->flags);
789 case MATCHCRITERIA_MARKED:
790 return MSG_IS_MARKED(info->flags);
791 case MATCHCRITERIA_NOT_MARKED:
792 return !MSG_IS_MARKED(info->flags);
793 case MATCHCRITERIA_DELETED:
794 return MSG_IS_DELETED(info->flags);
795 case MATCHCRITERIA_NOT_DELETED:
796 return !MSG_IS_DELETED(info->flags);
797 case MATCHCRITERIA_REPLIED:
798 return MSG_IS_REPLIED(info->flags);
799 case MATCHCRITERIA_NOT_REPLIED:
800 return !MSG_IS_REPLIED(info->flags);
801 case MATCHCRITERIA_FORWARDED:
802 return MSG_IS_FORWARDED(info->flags);
803 case MATCHCRITERIA_NOT_FORWARDED:
804 return !MSG_IS_FORWARDED(info->flags);
805 case MATCHCRITERIA_LOCKED:
806 return MSG_IS_LOCKED(info->flags);
807 case MATCHCRITERIA_NOT_LOCKED:
808 return !MSG_IS_LOCKED(info->flags);
809 case MATCHCRITERIA_SPAM:
810 return MSG_IS_SPAM(info->flags);
811 case MATCHCRITERIA_NOT_SPAM:
812 return !MSG_IS_SPAM(info->flags);
813 case MATCHCRITERIA_HAS_ATTACHMENT:
814 return MSG_IS_WITH_ATTACHMENT(info->flags);
815 case MATCHCRITERIA_HAS_NO_ATTACHMENT:
816 return !MSG_IS_WITH_ATTACHMENT(info->flags);
817 case MATCHCRITERIA_SIGNED:
818 return MSG_IS_SIGNED(info->flags);
819 case MATCHCRITERIA_NOT_SIGNED:
820 return !MSG_IS_SIGNED(info->flags);
821 case MATCHCRITERIA_COLORLABEL:
823 gint color = MSG_GET_COLORLABEL_VALUE(info->flags);
824 gboolean ret = (color == prop->value);
827 if (debug_filtering_session
828 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
830 log_print(LOG_DEBUG_FILTERING,
831 "message color value [ %d ] matches color value [ %d ]\n",
834 log_print(LOG_DEBUG_FILTERING,
835 "message color value [ %d ] does NOT match color value [ %d ]\n",
841 case MATCHCRITERIA_NOT_COLORLABEL:
843 gint color = MSG_GET_COLORLABEL_VALUE(info->flags);
844 gboolean ret = (color != prop->value);
847 if (debug_filtering_session
848 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
850 log_print(LOG_DEBUG_FILTERING,
851 "message color value [ %d ] matches color value [ %d ]\n",
854 log_print(LOG_DEBUG_FILTERING,
855 "message color value [ %d ] does NOT match color value [ %d ]\n",
861 case MATCHCRITERIA_IGNORE_THREAD:
862 return MSG_IS_IGNORE_THREAD(info->flags);
863 case MATCHCRITERIA_NOT_IGNORE_THREAD:
864 return !MSG_IS_IGNORE_THREAD(info->flags);
865 case MATCHCRITERIA_WATCH_THREAD:
866 return MSG_IS_WATCH_THREAD(info->flags);
867 case MATCHCRITERIA_NOT_WATCH_THREAD:
868 return !MSG_IS_WATCH_THREAD(info->flags);
869 case MATCHCRITERIA_SUBJECT:
870 return matcherprop_string_match(prop, info->subject,
871 prefs_common_translated_header_name("Subject:"));
872 case MATCHCRITERIA_NOT_SUBJECT:
873 return !matcherprop_string_match(prop, info->subject,
874 prefs_common_translated_header_name("Subject:"));
875 case MATCHCRITERIA_FROM:
876 case MATCHCRITERIA_NOT_FROM:
881 context = g_strdup_printf(_("%s header"), prefs_common_translated_header_name("From:"));
882 ret = matcherprop_string_match(prop, info->from, context);
884 return (prop->criteria == MATCHCRITERIA_FROM)? ret : !ret;
886 case MATCHCRITERIA_TO:
887 case MATCHCRITERIA_NOT_TO:
892 context = g_strdup_printf(_("%s header"), prefs_common_translated_header_name("To:"));
893 ret = matcherprop_string_match(prop, info->to, context);
895 return (prop->criteria == MATCHCRITERIA_TO)? ret : !ret;
897 case MATCHCRITERIA_CC:
898 case MATCHCRITERIA_NOT_CC:
903 context = g_strdup_printf(_("%s header"), prefs_common_translated_header_name("Cc:"));
904 ret = matcherprop_string_match(prop, info->cc, context);
906 return (prop->criteria == MATCHCRITERIA_CC)? ret : !ret;
908 case MATCHCRITERIA_TO_OR_CC:
910 gchar *context1, *context2;
913 context1 = g_strdup_printf(_("%s header"), prefs_common_translated_header_name("To:"));
914 context2 = g_strdup_printf(_("%s header"), prefs_common_translated_header_name("Cc:"));
915 ret = matcherprop_string_match(prop, info->to, context1)
916 || matcherprop_string_match(prop, info->cc, context2);
921 case MATCHCRITERIA_NOT_TO_AND_NOT_CC:
923 gchar *context1, *context2;
926 context1 = g_strdup_printf(_("%s header"), prefs_common_translated_header_name("To:"));
927 context2 = g_strdup_printf(_("%s header"), prefs_common_translated_header_name("Cc:"));
928 ret = !(matcherprop_string_match(prop, info->to, context1)
929 || matcherprop_string_match(prop, info->cc, context2));
934 case MATCHCRITERIA_TAG:
935 case MATCHCRITERIA_NOT_TAG:
939 ret = matcherprop_tag_match(prop, info, _("Tag"));
940 return (prop->criteria == MATCHCRITERIA_TAG)? ret : !ret;
942 case MATCHCRITERIA_TAGGED:
943 case MATCHCRITERIA_NOT_TAGGED:
947 ret = (info->tags != NULL);
948 return (prop->criteria == MATCHCRITERIA_TAGGED)? ret : !ret;
950 case MATCHCRITERIA_AGE_GREATER:
956 age = ((t - info->date_t) / (60 * 60 * 24));
957 ret = (age > prop->value);
960 if (debug_filtering_session
961 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
963 log_print(LOG_DEBUG_FILTERING,
964 "message age [ %d ] is greater than [ %d ]\n",
967 log_print(LOG_DEBUG_FILTERING,
968 "message age [ %d ] is not greater than [ %d ]\n",
974 case MATCHCRITERIA_AGE_LOWER:
980 age = ((t - info->date_t) / (60 * 60 * 24));
981 ret = (age < prop->value);
984 if (debug_filtering_session
985 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
987 log_print(LOG_DEBUG_FILTERING,
988 "message age [ %d ] is lower than [ %d ]\n",
991 log_print(LOG_DEBUG_FILTERING,
992 "message age [ %d ] is not lower than [ %d ]\n",
998 case MATCHCRITERIA_SCORE_GREATER:
1000 gboolean ret = (info->score > prop->value);
1003 if (debug_filtering_session
1004 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
1006 log_print(LOG_DEBUG_FILTERING,
1007 "message score [ %d ] is greater than [ %d ]\n",
1008 info->score, prop->value);
1010 log_print(LOG_DEBUG_FILTERING,
1011 "message score [ %d ] is not greater than [ %d ]\n",
1012 info->score, prop->value);
1017 case MATCHCRITERIA_SCORE_LOWER:
1019 gboolean ret = (info->score < prop->value);
1022 if (debug_filtering_session
1023 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
1025 log_print(LOG_DEBUG_FILTERING,
1026 "message score [ %d ] is lower than [ %d ]\n",
1027 info->score, prop->value);
1029 log_print(LOG_DEBUG_FILTERING,
1030 "message score [ %d ] is not lower than [ %d ]\n",
1031 info->score, prop->value);
1036 case MATCHCRITERIA_SCORE_EQUAL:
1038 gboolean ret = (info->score == prop->value);
1041 if (debug_filtering_session
1042 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
1044 log_print(LOG_DEBUG_FILTERING,
1045 "message score [ %d ] is equal to [ %d ]\n",
1046 info->score, prop->value);
1048 log_print(LOG_DEBUG_FILTERING,
1049 "message score [ %d ] is not equal to [ %d ]\n",
1050 info->score, prop->value);
1055 case MATCHCRITERIA_SIZE_GREATER:
1057 /* FIXME: info->size is a goffset */
1058 gboolean ret = (info->size > (goffset) prop->value);
1061 if (debug_filtering_session
1062 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
1064 log_print(LOG_DEBUG_FILTERING,
1065 "message size is greater than [ %d ]\n",
1068 log_print(LOG_DEBUG_FILTERING,
1069 "message size is not greater than [ %d ]\n",
1075 case MATCHCRITERIA_SIZE_SMALLER:
1077 /* FIXME: info->size is a goffset */
1078 gboolean ret = (info->size < (goffset) prop->value);
1081 if (debug_filtering_session
1082 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
1084 log_print(LOG_DEBUG_FILTERING,
1085 "message size is smaller than [ %d ]\n",
1088 log_print(LOG_DEBUG_FILTERING,
1089 "message size is not smaller than [ %d ]\n",
1095 case MATCHCRITERIA_SIZE_EQUAL:
1097 /* FIXME: info->size is a goffset */
1098 gboolean ret = (info->size == (goffset) prop->value);
1101 if (debug_filtering_session
1102 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
1104 log_print(LOG_DEBUG_FILTERING,
1105 "message size is equal to [ %d ]\n",
1108 log_print(LOG_DEBUG_FILTERING,
1109 "message size is not equal to [ %d ]\n",
1115 case MATCHCRITERIA_PARTIAL:
1117 /* FIXME: info->size is a goffset */
1118 gboolean ret = (info->total_size != 0 && info->size != (goffset)info->total_size);
1121 if (debug_filtering_session
1122 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
1124 log_print(LOG_DEBUG_FILTERING,
1125 "message is partially downloaded, size is less than total size [ %d ])\n",
1128 log_print(LOG_DEBUG_FILTERING,
1129 "message is not partially downloaded\n");
1134 case MATCHCRITERIA_NOT_PARTIAL:
1136 /* FIXME: info->size is a goffset */
1137 gboolean ret = (info->total_size == 0 || info->size == (goffset)info->total_size);
1140 if (debug_filtering_session
1141 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
1143 log_print(LOG_DEBUG_FILTERING,
1144 "message is not partially downloaded\n");
1146 log_print(LOG_DEBUG_FILTERING,
1147 "message is partially downloaded, size is less than total size [ %d ])\n",
1153 case MATCHCRITERIA_NEWSGROUPS:
1154 case MATCHCRITERIA_NOT_NEWSGROUPS:
1159 context = g_strdup_printf(_("%s header"),
1160 prefs_common_translated_header_name("Newsgroups:"));
1161 ret = matcherprop_string_match(prop, info->newsgroups, context);
1163 return (prop->criteria == MATCHCRITERIA_NEWSGROUPS)? ret : !ret;
1165 case MATCHCRITERIA_INREPLYTO:
1166 case MATCHCRITERIA_NOT_INREPLYTO:
1171 context = g_strdup_printf(_("%s header"),
1172 prefs_common_translated_header_name("In-Reply-To:"));
1173 ret = matcherprop_string_match(prop, info->inreplyto, context);
1175 return (prop->criteria == MATCHCRITERIA_INREPLYTO)? ret : !ret;
1177 case MATCHCRITERIA_REFERENCES:
1178 case MATCHCRITERIA_NOT_REFERENCES:
1183 context = g_strdup_printf(_("%s header"),
1184 prefs_common_translated_header_name("References:"));
1185 ret = matcherprop_list_match(prop, info->references, context);
1187 return (prop->criteria == MATCHCRITERIA_REFERENCES)? ret : !ret;
1189 case MATCHCRITERIA_TEST:
1190 return matcherprop_match_test(prop, info);
1191 case MATCHCRITERIA_NOT_TEST:
1192 return !matcherprop_match_test(prop, info);
1198 /* ********************* MatcherList *************************** */
1201 *\brief Create a new list of matchers
1203 *\param matchers List of matcher structures
1204 *\param bool_and Operator
1206 *\return MatcherList * New list
1208 MatcherList *matcherlist_new(GSList *matchers, gboolean bool_and)
1212 cond = g_new0(MatcherList, 1);
1214 cond->matchers = matchers;
1215 cond->bool_and = bool_and;
1221 *\brief Frees a list of matchers
1223 *\param cond List of matchers
1225 void matcherlist_free(MatcherList *cond)
1229 cm_return_if_fail(cond);
1230 for (l = cond->matchers ; l != NULL ; l = g_slist_next(l)) {
1231 matcherprop_free((MatcherProp *) l->data);
1233 g_slist_free(cond->matchers);
1238 *\brief Skip all headers in a message file
1240 *\param fp Message file
1242 static void matcherlist_skip_headers(FILE *fp)
1244 gchar buf[BUFFSIZE];
1246 while (procheader_get_one_field(buf, sizeof(buf), fp, NULL) != -1)
1251 *\brief Check if a header matches a matcher condition
1253 *\param matcher Matcher structure to check header for
1254 *\param buf Header name
1256 *\return boolean TRUE if matching header
1258 static gboolean matcherprop_match_one_header(MatcherProp *matcher,
1261 gboolean result = FALSE;
1262 Header *header = NULL;
1264 switch (matcher->criteria) {
1265 case MATCHCRITERIA_HEADER:
1266 case MATCHCRITERIA_NOT_HEADER:
1267 header = procheader_parse_header(buf);
1270 if (procheader_headername_equal(header->name,
1272 if (matcher->criteria == MATCHCRITERIA_HEADER)
1273 result = matcherprop_string_match(matcher, header->body, _("header"));
1275 result = !matcherprop_string_match(matcher, header->body, _("header"));
1276 procheader_header_free(header);
1280 procheader_header_free(header);
1283 case MATCHCRITERIA_HEADERS_PART:
1284 return matcherprop_string_match(matcher, buf, _("header line"));
1285 case MATCHCRITERIA_NOT_HEADERS_PART:
1286 return !matcherprop_string_match(matcher, buf, _("headers line"));
1287 case MATCHCRITERIA_MESSAGE:
1288 return matcherprop_string_decode_match(matcher, buf, _("message line"));
1289 case MATCHCRITERIA_NOT_MESSAGE:
1290 return !matcherprop_string_decode_match(matcher, buf, _("message line"));
1291 case MATCHCRITERIA_FOUND_IN_ADDRESSBOOK:
1292 case MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK:
1294 GSList *address_list = NULL;
1295 gint match = MATCH_ONE;
1296 gboolean found = FALSE;
1298 /* how many address headers are me trying to mach? */
1299 if (strcasecmp(matcher->header, "Any") == 0)
1301 else if (strcasecmp(matcher->header, "All") == 0)
1304 if (match == MATCH_ONE) {
1305 /* matching one address header exactly, is that the right one? */
1306 header = procheader_parse_header(buf);
1308 !procheader_headername_equal(header->name, matcher->header))
1310 address_list = address_list_append(address_list, header->body);
1311 if (address_list == NULL)
1315 header = procheader_parse_header(buf);
1318 /* address header is one of the headers we have to match when checking
1319 for any address header or all address headers? */
1320 if (procheader_headername_equal(header->name, "From") ||
1321 procheader_headername_equal(header->name, "To") ||
1322 procheader_headername_equal(header->name, "Cc") ||
1323 procheader_headername_equal(header->name, "Reply-To") ||
1324 procheader_headername_equal(header->name, "Sender"))
1325 address_list = address_list_append(address_list, header->body);
1326 if (address_list == NULL)
1330 found = match_with_addresses_in_addressbook
1331 (matcher, address_list, matcher->criteria,
1332 matcher->expr, match);
1333 g_slist_free(address_list);
1335 if (matcher->criteria == MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK)
1346 *\brief Check if the matcher structure wants headers to
1349 *\param matcher Matcher structure
1351 *\return gboolean TRUE if the matcher structure describes
1352 * a header match condition
1354 static gboolean matcherprop_criteria_headers(const MatcherProp *matcher)
1356 switch (matcher->criteria) {
1357 case MATCHCRITERIA_HEADER:
1358 case MATCHCRITERIA_NOT_HEADER:
1359 case MATCHCRITERIA_HEADERS_PART:
1360 case MATCHCRITERIA_NOT_HEADERS_PART:
1361 case MATCHCRITERIA_FOUND_IN_ADDRESSBOOK:
1362 case MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK:
1370 *\brief Check if the matcher structure wants the message
1371 * to be matched (just perform an action on any
1374 *\param matcher Matcher structure
1376 *\return gboolean TRUE if matcher condition should match
1379 static gboolean matcherprop_criteria_message(MatcherProp *matcher)
1381 switch (matcher->criteria) {
1382 case MATCHCRITERIA_MESSAGE:
1383 case MATCHCRITERIA_NOT_MESSAGE:
1391 *\brief Check if a list of conditions matches one header in
1394 *\param matchers List of conditions
1395 *\param fp Message file
1397 *\return gboolean TRUE if one of the headers is matched by
1398 * the list of conditions.
1400 static gboolean matcherlist_match_headers(MatcherList *matchers, FILE *fp)
1403 gchar buf[BUFFSIZE];
1405 while (procheader_get_one_field(buf, sizeof(buf), fp, NULL) != -1) {
1406 for (l = matchers->matchers ; l != NULL ; l = g_slist_next(l)) {
1407 MatcherProp *matcher = (MatcherProp *) l->data;
1408 gint match = MATCH_ANY;
1413 /* determine the match range (all, any are our concern here) */
1414 if (matcher->criteria == MATCHCRITERIA_NOT_HEADERS_PART ||
1415 matcher->criteria == MATCHCRITERIA_NOT_MESSAGE) {
1418 } else if (matcher->criteria == MATCHCRITERIA_FOUND_IN_ADDRESSBOOK ||
1419 matcher->criteria == MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK) {
1420 Header *header = NULL;
1422 /* address header is one of the headers we have to match when checking
1423 for any address header or all address headers? */
1424 header = procheader_parse_header(buf);
1426 (procheader_headername_equal(header->name, "From") ||
1427 procheader_headername_equal(header->name, "To") ||
1428 procheader_headername_equal(header->name, "Cc") ||
1429 procheader_headername_equal(header->name, "Reply-To") ||
1430 procheader_headername_equal(header->name, "Sender"))) {
1432 if (strcasecmp(matcher->header, "Any") == 0)
1434 else if (strcasecmp(matcher->header, "All") == 0)
1439 /* further call to matcherprop_match_one_header() can't match
1440 and it irrelevant, so: don't alter the match result */
1445 /* ZERO line must NOT match for the rule to match.
1447 if (match == MATCH_ALL) {
1448 if (matcherprop_match_one_header(matcher, buf)) {
1449 matcher->result = TRUE;
1451 matcher->result = FALSE;
1452 matcher->done = TRUE;
1454 /* else, just one line matching is enough for the rule to match
1456 } else if (matcherprop_criteria_headers(matcher) ||
1457 matcherprop_criteria_message(matcher)) {
1458 if (matcherprop_match_one_header(matcher, buf)) {
1459 matcher->result = TRUE;
1460 matcher->done = TRUE;
1464 /* if the rule matched and the matchers are OR, no need to
1465 * check the others */
1466 if (matcher->result && matcher->done) {
1467 if (!matchers->bool_and)
1477 *\brief Check if a matcher wants to check the message body
1479 *\param matcher Matcher structure
1481 *\return gboolean TRUE if body must be matched.
1483 static gboolean matcherprop_criteria_body(const MatcherProp *matcher)
1485 switch (matcher->criteria) {
1486 case MATCHCRITERIA_BODY_PART:
1487 case MATCHCRITERIA_NOT_BODY_PART:
1495 *\brief Check if a (line) string matches the criteria
1496 * described by a matcher structure
1498 *\param matcher Matcher structure
1501 *\return gboolean TRUE if string matches criteria
1503 static gboolean matcherprop_match_line(MatcherProp *matcher, const gchar *line)
1505 switch (matcher->criteria) {
1506 case MATCHCRITERIA_BODY_PART:
1507 case MATCHCRITERIA_MESSAGE:
1508 return matcherprop_string_decode_match(matcher, line, _("body line"));
1509 case MATCHCRITERIA_NOT_BODY_PART:
1510 case MATCHCRITERIA_NOT_MESSAGE:
1511 return !matcherprop_string_decode_match(matcher, line, _("body line"));
1517 *\brief Check if a line in a message file's body matches
1520 *\param matchers List of conditions
1521 *\param fp Message file
1523 *\return gboolean TRUE if succesful match
1525 static gboolean matcherlist_match_body(MatcherList *matchers, FILE *fp)
1528 gchar buf[BUFFSIZE];
1530 while (fgets(buf, sizeof(buf), fp) != NULL) {
1531 for (l = matchers->matchers ; l != NULL ; l = g_slist_next(l)) {
1532 MatcherProp *matcher = (MatcherProp *) l->data;
1537 /* if the criteria is ~body_part or ~message, ZERO lines
1538 * must NOT match for the rule to match. */
1539 if (matcher->criteria == MATCHCRITERIA_NOT_BODY_PART ||
1540 matcher->criteria == MATCHCRITERIA_NOT_MESSAGE) {
1541 if (matcherprop_match_line(matcher, buf)) {
1542 matcher->result = TRUE;
1544 matcher->result = FALSE;
1545 matcher->done = TRUE;
1547 /* else, just one line has to match */
1548 } else if (matcherprop_criteria_body(matcher) ||
1549 matcherprop_criteria_message(matcher)) {
1550 if (matcherprop_match_line(matcher, buf)) {
1551 matcher->result = TRUE;
1552 matcher->done = TRUE;
1556 /* if the matchers are OR'ed and the rule matched,
1557 * no need to check the others. */
1558 if (matcher->result && matcher->done) {
1559 if (!matchers->bool_and)
1568 *\brief Check if a message file matches criteria
1570 *\param matchers Criteria
1571 *\param info Message info
1572 *\param result Default result
1574 *\return gboolean TRUE if matched
1576 static gboolean matcherlist_match_file(MatcherList *matchers, MsgInfo *info,
1579 gboolean read_headers;
1585 /* file need to be read ? */
1587 read_headers = FALSE;
1589 for (l = matchers->matchers ; l != NULL ; l = g_slist_next(l)) {
1590 MatcherProp *matcher = (MatcherProp *) l->data;
1592 if (matcherprop_criteria_headers(matcher))
1593 read_headers = TRUE;
1594 if (matcherprop_criteria_body(matcher))
1596 if (matcherprop_criteria_message(matcher)) {
1597 read_headers = TRUE;
1600 matcher->result = FALSE;
1601 matcher->done = FALSE;
1604 if (!read_headers && !read_body)
1607 file = procmsg_get_message_file_full(info, read_headers, read_body);
1611 if ((fp = g_fopen(file, "rb")) == NULL) {
1612 FILE_OP_ERROR(file, "fopen");
1617 /* read the headers */
1620 if (matcherlist_match_headers(matchers, fp))
1623 matcherlist_skip_headers(fp);
1628 matcherlist_match_body(matchers, fp);
1631 for (l = matchers->matchers; l != NULL; l = g_slist_next(l)) {
1632 MatcherProp *matcher = (MatcherProp *) l->data;
1634 if (matcherprop_criteria_headers(matcher) ||
1635 matcherprop_criteria_body(matcher) ||
1636 matcherprop_criteria_message(matcher)) {
1637 if (matcher->result) {
1638 if (!matchers->bool_and) {
1644 if (matchers->bool_and) {
1660 *\brief Test list of conditions on a message.
1662 *\param matchers List of conditions
1663 *\param info Message info
1665 *\return gboolean TRUE if matched
1667 gboolean matcherlist_match(MatcherList *matchers, MsgInfo *info)
1675 if (matchers->bool_and)
1680 /* test the cached elements */
1682 for (l = matchers->matchers; l != NULL ;l = g_slist_next(l)) {
1683 MatcherProp *matcher = (MatcherProp *) l->data;
1685 if (debug_filtering_session) {
1686 gchar *buf = matcherprop_to_string(matcher);
1687 log_print(LOG_DEBUG_FILTERING, _("checking if message matches [ %s ]\n"), buf);
1691 switch(matcher->criteria) {
1692 case MATCHCRITERIA_ALL:
1693 case MATCHCRITERIA_UNREAD:
1694 case MATCHCRITERIA_NOT_UNREAD:
1695 case MATCHCRITERIA_NEW:
1696 case MATCHCRITERIA_NOT_NEW:
1697 case MATCHCRITERIA_MARKED:
1698 case MATCHCRITERIA_NOT_MARKED:
1699 case MATCHCRITERIA_DELETED:
1700 case MATCHCRITERIA_NOT_DELETED:
1701 case MATCHCRITERIA_REPLIED:
1702 case MATCHCRITERIA_NOT_REPLIED:
1703 case MATCHCRITERIA_FORWARDED:
1704 case MATCHCRITERIA_NOT_FORWARDED:
1705 case MATCHCRITERIA_LOCKED:
1706 case MATCHCRITERIA_NOT_LOCKED:
1707 case MATCHCRITERIA_SPAM:
1708 case MATCHCRITERIA_NOT_SPAM:
1709 case MATCHCRITERIA_HAS_ATTACHMENT:
1710 case MATCHCRITERIA_HAS_NO_ATTACHMENT:
1711 case MATCHCRITERIA_SIGNED:
1712 case MATCHCRITERIA_NOT_SIGNED:
1713 case MATCHCRITERIA_COLORLABEL:
1714 case MATCHCRITERIA_NOT_COLORLABEL:
1715 case MATCHCRITERIA_IGNORE_THREAD:
1716 case MATCHCRITERIA_NOT_IGNORE_THREAD:
1717 case MATCHCRITERIA_WATCH_THREAD:
1718 case MATCHCRITERIA_NOT_WATCH_THREAD:
1719 case MATCHCRITERIA_SUBJECT:
1720 case MATCHCRITERIA_NOT_SUBJECT:
1721 case MATCHCRITERIA_FROM:
1722 case MATCHCRITERIA_NOT_FROM:
1723 case MATCHCRITERIA_TO:
1724 case MATCHCRITERIA_NOT_TO:
1725 case MATCHCRITERIA_CC:
1726 case MATCHCRITERIA_NOT_CC:
1727 case MATCHCRITERIA_TO_OR_CC:
1728 case MATCHCRITERIA_NOT_TO_AND_NOT_CC:
1729 case MATCHCRITERIA_TAG:
1730 case MATCHCRITERIA_NOT_TAG:
1731 case MATCHCRITERIA_TAGGED:
1732 case MATCHCRITERIA_NOT_TAGGED:
1733 case MATCHCRITERIA_AGE_GREATER:
1734 case MATCHCRITERIA_AGE_LOWER:
1735 case MATCHCRITERIA_NEWSGROUPS:
1736 case MATCHCRITERIA_NOT_NEWSGROUPS:
1737 case MATCHCRITERIA_INREPLYTO:
1738 case MATCHCRITERIA_NOT_INREPLYTO:
1739 case MATCHCRITERIA_REFERENCES:
1740 case MATCHCRITERIA_NOT_REFERENCES:
1741 case MATCHCRITERIA_SCORE_GREATER:
1742 case MATCHCRITERIA_SCORE_LOWER:
1743 case MATCHCRITERIA_SCORE_EQUAL:
1744 case MATCHCRITERIA_SIZE_GREATER:
1745 case MATCHCRITERIA_SIZE_SMALLER:
1746 case MATCHCRITERIA_SIZE_EQUAL:
1747 case MATCHCRITERIA_TEST:
1748 case MATCHCRITERIA_NOT_TEST:
1749 case MATCHCRITERIA_PARTIAL:
1750 case MATCHCRITERIA_NOT_PARTIAL:
1751 if (matcherprop_match(matcher, info)) {
1752 if (!matchers->bool_and) {
1753 if (debug_filtering_session)
1754 log_status_ok(LOG_DEBUG_FILTERING, _("message matches\n"));
1759 if (matchers->bool_and) {
1760 if (debug_filtering_session)
1761 log_status_nok(LOG_DEBUG_FILTERING, _("message does not match\n"));
1768 /* test the condition on the file */
1770 if (matcherlist_match_file(matchers, info, result)) {
1771 if (!matchers->bool_and) {
1772 if (debug_filtering_session)
1773 log_status_ok(LOG_DEBUG_FILTERING, _("message matches\n"));
1777 if (matchers->bool_and) {
1778 if (debug_filtering_session)
1779 log_status_nok(LOG_DEBUG_FILTERING, _("message does not match\n"));
1784 if (debug_filtering_session) {
1786 log_status_ok(LOG_DEBUG_FILTERING, _("message matches\n"));
1788 log_status_nok(LOG_DEBUG_FILTERING, _("message does not match\n"));
1794 static gint quote_filter_str(gchar * result, guint size,
1804 for(p = path ; * p != '\0' ; p ++) {
1806 if ((* p != '\"') && (* p != '\\')) {
1807 if (remaining > 0) {
1813 result[size - 1] = '\0';
1818 if (remaining >= 2) {
1826 result[size - 1] = '\0';
1831 if (remaining > 0) {
1835 result[size - 1] = '\0';
1843 gchar * matcher_quote_str(const gchar * src)
1848 len = strlen(src) * 2 + 1;
1849 res = g_malloc(len);
1850 quote_filter_str(res, len, src);
1856 *\brief Convert a matcher structure to a string
1858 *\param matcher Matcher structure
1860 *\return gchar * Newly allocated string
1862 gchar *matcherprop_to_string(MatcherProp *matcher)
1864 gchar *matcher_str = NULL;
1865 const gchar *criteria_str;
1866 const gchar *matchtype_str;
1868 gchar * quoted_expr;
1869 gchar * quoted_header;
1871 criteria_str = NULL;
1872 for (i = 0; i < (int) (sizeof(matchparser_tab) / sizeof(MatchParser)); i++) {
1873 if (matchparser_tab[i].id == matcher->criteria)
1874 criteria_str = matchparser_tab[i].str;
1876 if (criteria_str == NULL)
1879 switch (matcher->criteria) {
1880 case MATCHCRITERIA_AGE_GREATER:
1881 case MATCHCRITERIA_AGE_LOWER:
1882 case MATCHCRITERIA_SCORE_GREATER:
1883 case MATCHCRITERIA_SCORE_LOWER:
1884 case MATCHCRITERIA_SCORE_EQUAL:
1885 case MATCHCRITERIA_SIZE_GREATER:
1886 case MATCHCRITERIA_SIZE_SMALLER:
1887 case MATCHCRITERIA_SIZE_EQUAL:
1888 case MATCHCRITERIA_COLORLABEL:
1889 case MATCHCRITERIA_NOT_COLORLABEL:
1890 return g_strdup_printf("%s %i", criteria_str, matcher->value);
1891 case MATCHCRITERIA_ALL:
1892 case MATCHCRITERIA_UNREAD:
1893 case MATCHCRITERIA_NOT_UNREAD:
1894 case MATCHCRITERIA_NEW:
1895 case MATCHCRITERIA_NOT_NEW:
1896 case MATCHCRITERIA_MARKED:
1897 case MATCHCRITERIA_NOT_MARKED:
1898 case MATCHCRITERIA_DELETED:
1899 case MATCHCRITERIA_NOT_DELETED:
1900 case MATCHCRITERIA_REPLIED:
1901 case MATCHCRITERIA_NOT_REPLIED:
1902 case MATCHCRITERIA_FORWARDED:
1903 case MATCHCRITERIA_NOT_FORWARDED:
1904 case MATCHCRITERIA_LOCKED:
1905 case MATCHCRITERIA_NOT_LOCKED:
1906 case MATCHCRITERIA_SPAM:
1907 case MATCHCRITERIA_NOT_SPAM:
1908 case MATCHCRITERIA_HAS_ATTACHMENT:
1909 case MATCHCRITERIA_HAS_NO_ATTACHMENT:
1910 case MATCHCRITERIA_SIGNED:
1911 case MATCHCRITERIA_NOT_SIGNED:
1912 case MATCHCRITERIA_PARTIAL:
1913 case MATCHCRITERIA_NOT_PARTIAL:
1914 case MATCHCRITERIA_IGNORE_THREAD:
1915 case MATCHCRITERIA_NOT_IGNORE_THREAD:
1916 case MATCHCRITERIA_WATCH_THREAD:
1917 case MATCHCRITERIA_NOT_WATCH_THREAD:
1918 case MATCHCRITERIA_TAGGED:
1919 case MATCHCRITERIA_NOT_TAGGED:
1920 return g_strdup(criteria_str);
1921 case MATCHCRITERIA_TEST:
1922 case MATCHCRITERIA_NOT_TEST:
1923 quoted_expr = matcher_quote_str(matcher->expr);
1924 matcher_str = g_strdup_printf("%s \"%s\"",
1925 criteria_str, quoted_expr);
1926 g_free(quoted_expr);
1928 case MATCHCRITERIA_FOUND_IN_ADDRESSBOOK:
1929 case MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK:
1930 quoted_header = matcher_quote_str(matcher->header);
1931 quoted_expr = matcher_quote_str(matcher->expr);
1932 matcher_str = g_strdup_printf("%s \"%s\" in \"%s\"",
1933 criteria_str, quoted_header, quoted_expr);
1934 g_free(quoted_header);
1935 g_free(quoted_expr);
1939 matchtype_str = NULL;
1940 for (i = 0; i < sizeof matchparser_tab / sizeof matchparser_tab[0]; i++) {
1941 if (matchparser_tab[i].id == matcher->matchtype)
1942 matchtype_str = matchparser_tab[i].str;
1945 if (matchtype_str == NULL)
1948 switch (matcher->matchtype) {
1949 case MATCHTYPE_MATCH:
1950 case MATCHTYPE_MATCHCASE:
1951 case MATCHTYPE_REGEXP:
1952 case MATCHTYPE_REGEXPCASE:
1953 quoted_expr = matcher_quote_str(matcher->expr);
1954 if (matcher->header) {
1955 quoted_header = matcher_quote_str(matcher->header);
1956 matcher_str = g_strdup_printf
1957 ("%s \"%s\" %s \"%s\"",
1958 criteria_str, quoted_header,
1959 matchtype_str, quoted_expr);
1960 g_free(quoted_header);
1963 matcher_str = g_strdup_printf
1964 ("%s %s \"%s\"", criteria_str,
1965 matchtype_str, quoted_expr);
1966 g_free(quoted_expr);
1974 *\brief Convert a list of conditions to a string
1976 *\param matchers List of conditions
1978 *\return gchar * Newly allocated string
1980 gchar *matcherlist_to_string(const MatcherList *matchers)
1986 gchar *result = NULL;
1988 count = g_slist_length(matchers->matchers);
1989 vstr = g_new(gchar *, count + 1);
1991 for (l = matchers->matchers, cur_str = vstr; l != NULL;
1992 l = g_slist_next(l), cur_str ++) {
1993 *cur_str = matcherprop_to_string((MatcherProp *) l->data);
1994 if (*cur_str == NULL)
1999 if (matchers->bool_and)
2000 result = g_strjoinv(" & ", vstr);
2002 result = g_strjoinv(" | ", vstr);
2004 for (cur_str = vstr ; *cur_str != NULL ; cur_str ++)
2012 #define STRLEN_ZERO(s) ((s) ? strlen(s) : 0)
2013 #define STRLEN_DEFAULT(s,d) ((s) ? strlen(s) : STRLEN_ZERO(d))
2015 static void add_str_default(gchar ** dest,
2016 const gchar * s, const gchar * d)
2018 gchar quoted_str[4096];
2026 quote_cmd_argument(quoted_str, sizeof(quoted_str), str);
2027 strcpy(* dest, quoted_str);
2029 (* dest) += strlen(* dest);
2032 /* matching_build_command() - preferably cmd should be unescaped */
2034 *\brief Build the command-line to execute
2036 *\param cmd String with command-line specifiers
2037 *\param info Message info to use for command
2039 *\return gchar * Newly allocated string
2041 gchar *matching_build_command(const gchar *cmd, MsgInfo *info)
2043 const gchar *s = cmd;
2044 gchar *filename = NULL;
2045 gchar *processed_cmd;
2049 const gchar *const no_subject = _("(none)") ;
2050 const gchar *const no_from = _("(none)") ;
2051 const gchar *const no_to = _("(none)") ;
2052 const gchar *const no_cc = _("(none)") ;
2053 const gchar *const no_date = _("(none)") ;
2054 const gchar *const no_msgid = _("(none)") ;
2055 const gchar *const no_newsgroups = _("(none)") ;
2056 const gchar *const no_references = _("(none)") ;
2058 size = STRLEN_ZERO(cmd) + 1;
2059 while (*s != '\0') {
2066 case 's': /* subject */
2067 size += STRLEN_DEFAULT(info->subject, no_subject) - 2;
2069 case 'f': /* from */
2070 size += STRLEN_DEFAULT(info->from, no_from) - 2;
2073 size += STRLEN_DEFAULT(info->to, no_to) - 2;
2076 size += STRLEN_DEFAULT(info->cc, no_cc) - 2;
2078 case 'd': /* date */
2079 size += STRLEN_DEFAULT(info->date, no_date) - 2;
2081 case 'i': /* message-id */
2082 size += STRLEN_DEFAULT(info->msgid, no_msgid) - 2;
2084 case 'n': /* newsgroups */
2085 size += STRLEN_DEFAULT(info->newsgroups, no_newsgroups) - 2;
2087 case 'r': /* references */
2088 /* FIXME: using the inreplyto header for reference */
2089 size += STRLEN_DEFAULT(info->inreplyto, no_references) - 2;
2091 case 'F': /* file */
2092 if (filename == NULL)
2093 filename = folder_item_fetch_msg(info->folder, info->msgnum);
2095 if (filename == NULL) {
2096 g_warning("filename is not set");
2100 size += strlen(filename) - 2;
2109 /* as the string can be quoted, we double the result */
2112 processed_cmd = g_new0(gchar, size);
2116 while (*s != '\0') {
2124 case 's': /* subject */
2125 add_str_default(&p, info->subject,
2128 case 'f': /* from */
2129 add_str_default(&p, info->from,
2133 add_str_default(&p, info->to,
2137 add_str_default(&p, info->cc,
2140 case 'd': /* date */
2141 add_str_default(&p, info->date,
2144 case 'i': /* message-id */
2145 add_str_default(&p, info->msgid,
2148 case 'n': /* newsgroups */
2149 add_str_default(&p, info->newsgroups,
2152 case 'r': /* references */
2153 /* FIXME: using the inreplyto header for references */
2154 add_str_default(&p, info->inreplyto, no_references);
2156 case 'F': /* file */
2157 if (filename != NULL)
2158 add_str_default(&p, filename, NULL);
2177 return processed_cmd;
2179 #undef STRLEN_DEFAULT
2182 /* ************************************************************ */
2186 *\brief Write filtering list to file
2189 *\param prefs_filtering List of filtering conditions
2191 static int prefs_filtering_write(FILE *fp, GSList *prefs_filtering)
2195 for (cur = prefs_filtering; cur != NULL; cur = cur->next) {
2196 gchar *filtering_str = NULL;
2197 gchar *tmp_name = NULL;
2198 FilteringProp *prop = NULL;
2200 if (NULL == (prop = (FilteringProp *) cur->data))
2203 if (NULL == (filtering_str = filteringprop_to_string(prop)))
2206 if (prop->enabled) {
2207 if (fputs("enabled ", fp) == EOF) {
2208 FILE_OP_ERROR("filtering config", "fputs");
2212 if (fputs("disabled ", fp) == EOF) {
2213 FILE_OP_ERROR("filtering config", "fputs");
2218 if (fputs("rulename \"", fp) == EOF) {
2219 FILE_OP_ERROR("filtering config", "fputs");
2220 g_free(filtering_str);
2223 tmp_name = prop->name;
2224 while (tmp_name && *tmp_name != '\0') {
2225 if (*tmp_name != '"') {
2226 if (fputc(*tmp_name, fp) == EOF) {
2227 FILE_OP_ERROR("filtering config", "fputs || fputc");
2228 g_free(filtering_str);
2231 } else if (*tmp_name == '"') {
2232 if (fputc('\\', fp) == EOF ||
2233 fputc('"', fp) == EOF) {
2234 FILE_OP_ERROR("filtering config", "fputs || fputc");
2235 g_free(filtering_str);
2241 if (fputs("\" ", fp) == EOF) {
2242 FILE_OP_ERROR("filtering config", "fputs");
2243 g_free(filtering_str);
2247 if (prop->account_id != 0) {
2250 tmp = g_strdup_printf("account %d ", prop->account_id);
2251 if (fputs(tmp, fp) == EOF) {
2252 FILE_OP_ERROR("filtering config", "fputs");
2259 if(fputs(filtering_str, fp) == EOF ||
2260 fputc('\n', fp) == EOF) {
2261 FILE_OP_ERROR("filtering config", "fputs || fputc");
2262 g_free(filtering_str);
2265 g_free(filtering_str);
2271 typedef struct _NodeLoopData {
2277 *\brief Write matchers from a folder item
2279 *\param node Node with folder info
2280 *\param data File pointer
2282 *\return gboolean FALSE
2284 static gboolean prefs_matcher_write_func(GNode *node, gpointer d)
2287 NodeLoopData *data = (NodeLoopData *)d;
2289 GSList *prefs_filtering;
2292 /* prevent warning */
2293 if (item->path == NULL)
2295 id = folder_item_get_identifier(item);
2298 prefs_filtering = item->prefs->processing;
2300 if (prefs_filtering != NULL) {
2301 if (fprintf(data->fp, "[%s]\n", id) < 0) {
2305 if (prefs_filtering_write(data->fp, prefs_filtering) < 0) {
2309 if (fputc('\n', data->fp) == EOF) {
2321 *\brief Save matchers from folder items
2325 static int prefs_matcher_save(FILE *fp)
2333 for (cur = folder_get_list() ; cur != NULL ; cur = g_list_next(cur)) {
2336 folder = (Folder *) cur->data;
2337 g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
2338 prefs_matcher_write_func, &data);
2341 if (data.error == TRUE)
2344 /* pre global rules */
2345 if (fprintf(fp, "[preglobal]\n") < 0 ||
2346 prefs_filtering_write(fp, pre_global_processing) < 0 ||
2347 fputc('\n', fp) == EOF)
2350 /* post global rules */
2351 if (fprintf(fp, "[postglobal]\n") < 0 ||
2352 prefs_filtering_write(fp, post_global_processing) < 0 ||
2353 fputc('\n', fp) == EOF)
2356 /* filtering rules */
2357 if (fprintf(fp, "[filtering]\n") < 0 ||
2358 prefs_filtering_write(fp, filtering_rules) < 0 ||
2359 fputc('\n', fp) == EOF)
2366 *\brief Write filtering / matcher configuration file
2368 void prefs_matcher_write_config(void)
2373 debug_print("Writing matcher configuration...\n");
2375 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
2378 if ((pfile = prefs_write_open(rcpath)) == NULL) {
2379 g_warning("failed to write configuration to file\n");
2386 if (prefs_matcher_save(pfile->fp) < 0) {
2387 g_warning("failed to write configuration to file\n");
2388 prefs_file_close_revert(pfile);
2389 } else if (prefs_file_close(pfile) < 0) {
2390 g_warning("failed to save configuration to file\n");
2394 /* ******************************************************************* */
2396 static void matcher_add_rulenames(const gchar *rcpath)
2398 gchar *newpath = g_strconcat(rcpath, ".new", NULL);
2399 FILE *src = g_fopen(rcpath, "rb");
2400 FILE *dst = g_fopen(newpath, "wb");
2401 gchar buf[BUFFSIZE];
2418 while (fgets (buf, sizeof(buf), src) != NULL) {
2419 if (strlen(buf) > 2 && buf[0] != '['
2420 && strncmp(buf, "rulename \"", 10)
2421 && strncmp(buf, "enabled rulename \"", 18)
2422 && strncmp(buf, "disabled rulename \"", 18)) {
2423 r = fwrite("enabled rulename \"\" ",
2424 strlen("enabled rulename \"\" "), 1, dst);
2426 r = fwrite(buf, strlen(buf), 1, dst);
2430 move_file(newpath, rcpath, TRUE);
2435 *\brief Read matcher configuration
2437 void prefs_matcher_read_config(void)
2440 gchar *rc_old_format;
2443 create_matchparser_hashtab();
2444 prefs_filtering_clear();
2446 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MATCHER_RC, NULL);
2447 rc_old_format = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MATCHER_RC,
2448 ".pre_names", NULL);
2450 if (!is_file_exist(rc_old_format) && is_file_exist(rcpath)) {
2451 /* backup file with no rules names, in case
2452 * anything goes wrong */
2453 copy_file(rcpath, rc_old_format, FALSE);
2454 /* now hack the file in order to have it to the new format */
2455 matcher_add_rulenames(rcpath);
2458 g_free(rc_old_format);
2460 f = g_fopen(rcpath, "rb");
2464 matcher_parser_start_parsing(f);
2465 fclose(matcher_parserin);
2468 /* previous version compatibility */
2470 /* g_print("reading filtering\n"); */
2471 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
2472 FILTERING_RC, NULL);
2473 f = g_fopen(rcpath, "rb");
2477 matcher_parser_start_parsing(f);
2478 fclose(matcher_parserin);
2481 /* g_print("reading scoring\n"); */
2482 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
2484 f = g_fopen(rcpath, "rb");
2488 matcher_parser_start_parsing(f);
2489 fclose(matcher_parserin);