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"},
90 {MATCHCRITERIA_SUBJECT, "subject"},
91 {MATCHCRITERIA_NOT_SUBJECT, "~subject"},
92 {MATCHCRITERIA_FROM, "from"},
93 {MATCHCRITERIA_NOT_FROM, "~from"},
94 {MATCHCRITERIA_TO, "to"},
95 {MATCHCRITERIA_NOT_TO, "~to"},
96 {MATCHCRITERIA_CC, "cc"},
97 {MATCHCRITERIA_NOT_CC, "~cc"},
98 {MATCHCRITERIA_TO_OR_CC, "to_or_cc"},
99 {MATCHCRITERIA_NOT_TO_AND_NOT_CC, "~to_or_cc"},
100 {MATCHCRITERIA_TAG, "tag"},
101 {MATCHCRITERIA_NOT_TAG, "~tag"},
102 {MATCHCRITERIA_TAGGED, "tagged"},
103 {MATCHCRITERIA_NOT_TAGGED, "~tagged"},
104 {MATCHCRITERIA_AGE_GREATER, "age_greater"},
105 {MATCHCRITERIA_AGE_LOWER, "age_lower"},
106 {MATCHCRITERIA_NEWSGROUPS, "newsgroups"},
107 {MATCHCRITERIA_NOT_NEWSGROUPS, "~newsgroups"},
108 {MATCHCRITERIA_INREPLYTO, "inreplyto"},
109 {MATCHCRITERIA_NOT_INREPLYTO, "~inreplyto"},
110 {MATCHCRITERIA_REFERENCES, "references"},
111 {MATCHCRITERIA_NOT_REFERENCES, "~references"},
112 {MATCHCRITERIA_SCORE_GREATER, "score_greater"},
113 {MATCHCRITERIA_SCORE_LOWER, "score_lower"},
114 {MATCHCRITERIA_SCORE_EQUAL, "score_equal"},
115 {MATCHCRITERIA_PARTIAL, "partial"},
116 {MATCHCRITERIA_NOT_PARTIAL, "~partial"},
117 {MATCHCRITERIA_FOUND_IN_ADDRESSBOOK, "found_in_addressbook"},
118 {MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK, "~found_in_addressbook"},
120 {MATCHCRITERIA_SIZE_GREATER, "size_greater"},
121 {MATCHCRITERIA_SIZE_SMALLER, "size_smaller"},
122 {MATCHCRITERIA_SIZE_EQUAL, "size_equal"},
124 /* content have to be read */
125 {MATCHCRITERIA_HEADER, "header"},
126 {MATCHCRITERIA_NOT_HEADER, "~header"},
127 {MATCHCRITERIA_HEADERS_PART, "headers_part"},
128 {MATCHCRITERIA_NOT_HEADERS_PART, "~headers_part"},
129 {MATCHCRITERIA_MESSAGE, "message"},
130 {MATCHCRITERIA_NOT_MESSAGE, "~message"},
131 {MATCHCRITERIA_BODY_PART, "body_part"},
132 {MATCHCRITERIA_NOT_BODY_PART, "~body_part"},
133 {MATCHCRITERIA_TEST, "test"},
134 {MATCHCRITERIA_NOT_TEST, "~test"},
137 {MATCHTYPE_MATCHCASE, "matchcase"},
138 {MATCHTYPE_MATCH, "match"},
139 {MATCHTYPE_REGEXPCASE, "regexpcase"},
140 {MATCHTYPE_REGEXP, "regexp"},
143 {MATCHACTION_SCORE, "score"}, /* for backward compatibility */
144 {MATCHACTION_MOVE, "move"},
145 {MATCHACTION_COPY, "copy"},
146 {MATCHACTION_DELETE, "delete"},
147 {MATCHACTION_MARK, "mark"},
148 {MATCHACTION_UNMARK, "unmark"},
149 {MATCHACTION_LOCK, "lock"},
150 {MATCHACTION_UNLOCK, "unlock"},
151 {MATCHACTION_MARK_AS_READ, "mark_as_read"},
152 {MATCHACTION_MARK_AS_UNREAD, "mark_as_unread"},
153 {MATCHACTION_MARK_AS_SPAM, "mark_as_spam"},
154 {MATCHACTION_MARK_AS_HAM, "mark_as_ham"},
155 {MATCHACTION_FORWARD, "forward"},
156 {MATCHACTION_FORWARD_AS_ATTACHMENT, "forward_as_attachment"},
157 {MATCHACTION_EXECUTE, "execute"},
158 {MATCHACTION_COLOR, "color"},
159 {MATCHACTION_REDIRECT, "redirect"},
160 {MATCHACTION_CHANGE_SCORE, "change_score"},
161 {MATCHACTION_SET_SCORE, "set_score"},
162 {MATCHACTION_STOP, "stop"},
163 {MATCHACTION_HIDE, "hide"},
164 {MATCHACTION_IGNORE, "ignore"},
165 {MATCHACTION_WATCH, "watch"},
166 {MATCHACTION_ADD_TO_ADDRESSBOOK, "add_to_addressbook"},
167 {MATCHACTION_SET_TAG, "set_tag"},
168 {MATCHACTION_UNSET_TAG, "unset_tag"},
169 {MATCHACTION_CLEAR_TAGS, "clear_tags"},
178 extern gboolean debug_filtering_session;
181 *\brief Look up table with keywords defined in \sa matchparser_tab
183 static GHashTable *matchparser_hashtab;
186 *\brief Translate keyword id to keyword string
188 *\param id Id of keyword
190 *\return const gchar * Keyword
192 const gchar *get_matchparser_tab_str(gint id)
196 for (i = 0; i < sizeof matchparser_tab / sizeof matchparser_tab[0]; i++) {
197 if (matchparser_tab[i].id == id)
198 return matchparser_tab[i].str;
204 *\brief Create keyword lookup table
206 static void create_matchparser_hashtab(void)
210 if (matchparser_hashtab) return;
211 matchparser_hashtab = g_hash_table_new(g_str_hash, g_str_equal);
212 for (i = 0; i < sizeof matchparser_tab / sizeof matchparser_tab[0]; i++)
213 g_hash_table_insert(matchparser_hashtab,
214 matchparser_tab[i].str,
215 (gpointer) &matchparser_tab[i]);
219 *\brief Return a keyword id from a keyword string
221 *\param str Keyword string
223 *\return gint Keyword id
225 gint get_matchparser_tab_id(const gchar *str)
229 if (NULL != (res = g_hash_table_lookup(matchparser_hashtab, str))) {
235 /* **************** data structure allocation **************** */
238 *\brief Allocate a structure for a filtering / scoring
239 * "condition" (a matcher structure)
241 *\param criteria Criteria ID (MATCHCRITERIA_XXXX)
242 *\param header Header string (if criteria is MATCHCRITERIA_HEADER
243 or MATCHCRITERIA_FOUND_IN_ADDRESSBOOK)
244 *\param matchtype Type of action (MATCHTYPE_XXX)
245 *\param expr String value or expression to check
246 *\param value Integer value to check
248 *\return MatcherProp * Pointer to newly allocated structure
250 MatcherProp *matcherprop_new(gint criteria, const gchar *header,
251 gint matchtype, const gchar *expr,
256 prop = g_new0(MatcherProp, 1);
257 prop->criteria = criteria;
258 prop->header = header != NULL ? g_strdup(header) : NULL;
260 if (matchtype == MATCHTYPE_MATCHCASE ||
261 matchtype == MATCHTYPE_REGEXPCASE)
262 prop->expr = expr != NULL ? g_utf8_casefold(expr, -1) : 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 g_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 ] doesn't 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)
450 gboolean ret = FALSE;
451 gboolean should_free = FALSE;
455 if (prop->matchtype == MATCHTYPE_REGEXPCASE ||
456 prop->matchtype == MATCHTYPE_MATCHCASE) {
457 str1 = g_utf8_casefold(str, -1);
464 switch (prop->matchtype) {
465 case MATCHTYPE_REGEXPCASE:
466 case MATCHTYPE_REGEXP:
467 if (!prop->preg && (prop->error == 0)) {
468 prop->preg = g_new0(regex_t, 1);
469 /* if regexp then don't use the escaped string */
470 if (regcomp(prop->preg, prop->expr,
471 REG_NOSUB | REG_EXTENDED
472 | ((prop->matchtype == MATCHTYPE_REGEXPCASE)
473 ? REG_ICASE : 0)) != 0) {
479 if (prop->preg == NULL)
482 if (regexec(prop->preg, str1, 0, NULL, 0) == 0)
488 if (debug_filtering_session
489 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
490 gchar *stripped = g_strdup(str);
492 strretchomp(stripped);
494 log_print(LOG_DEBUG_FILTERING,
495 "%s value [ %s ] matches regular expression [ %s ] (%s)\n",
496 debug_context, stripped, prop->expr,
497 prop->matchtype == MATCHTYPE_REGEXP ? _("Case sensitive"):_("Case insensitive"));
499 log_print(LOG_DEBUG_FILTERING,
500 "%s value [ %s ] doesn't matches regular expression [ %s ] (%s)\n",
501 debug_context, stripped, prop->expr,
502 prop->matchtype == MATCHTYPE_REGEXP ? _("Case sensitive"):_("Case insensitive"));
508 case MATCHTYPE_MATCHCASE:
509 case MATCHTYPE_MATCH:
510 ret = (strstr(str1, prop->expr) != NULL);
513 if (debug_filtering_session
514 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
515 gchar *stripped = g_strdup(str);
517 strretchomp(stripped);
519 log_print(LOG_DEBUG_FILTERING,
520 "%s value [ %s ] contains [ %s ] (%s)\n",
521 debug_context, stripped, prop->expr,
522 prop->matchtype == MATCHTYPE_MATCH ? _("Case sensitive"):_("Case insensitive"));
524 log_print(LOG_DEBUG_FILTERING,
525 "%s value [ %s ] doesn't contains [ %s ] (%s)\n",
526 debug_context, stripped, prop->expr,
527 prop->matchtype == MATCHTYPE_MATCH ? _("Case sensitive"):_("Case insensitive"));
544 *\brief Find out if a tag matches a condition
546 *\param prop Matcher structure
547 *\param msginfo message to check
549 *\return gboolean TRUE if msginfo matches the condition in the
552 static gboolean matcherprop_tag_match(MatcherProp *prop, MsgInfo *msginfo,
553 const gchar *debug_context)
555 gboolean ret = FALSE;
558 if (msginfo == NULL || msginfo->tags == NULL)
561 for (cur = msginfo->tags; cur; cur = cur->next) {
562 const gchar *str = tags_get_tag(GPOINTER_TO_INT(cur->data));
565 if (matcherprop_string_match(prop, str, debug_context)) {
574 *\brief Find out if the string-ed list matches a condition
576 *\param prop Matcher structure
577 *\param list GSList of strings to check
579 *\return gboolean TRUE if str matches the condition in the
582 static gboolean matcherprop_list_match(MatcherProp *prop, const GSList *list,
583 const gchar *debug_context)
587 for(cur = list; cur != NULL; cur = cur->next) {
588 if (matcherprop_string_match(prop, (gchar *)cur->data, debug_context))
594 /* FIXME body search is a hack. */
595 static gboolean matcherprop_string_decode_match(MatcherProp *prop, const gchar *str,
596 const gchar *debug_context)
600 gboolean res = FALSE;
605 /* we try to decode QP first, because it's faster than base64 */
606 qp_decode_const(tmp, BUFFSIZE-1, str);
607 if (!g_utf8_validate(tmp, -1, NULL)) {
608 utf = conv_codeset_strdup
609 (tmp, conv_get_locale_charset_str_no_utf8(),
611 res = matcherprop_string_match(prop, utf, debug_context);
614 res = matcherprop_string_match(prop, tmp, debug_context);
617 if (res == FALSE && (strchr(prop->expr, '=') || strchr(prop->expr, '_')
618 || strchr(str, '=') || strchr(str, '_'))) {
619 /* if searching for something with an equal char, maybe
620 * we should try to match the non-decoded string.
621 * In case it was not qp-encoded. */
622 if (!g_utf8_validate(str, -1, NULL)) {
623 utf = conv_codeset_strdup
624 (str, conv_get_locale_charset_str_no_utf8(),
626 res = matcherprop_string_match(prop, utf, debug_context);
629 res = matcherprop_string_match(prop, str, debug_context);
633 /* FIXME base64 decoding is too slow, especially since text can
634 * easily be handled as base64. Don't even try now. */
640 typedef struct _thread_data {
647 static void *matcher_test_thread(void *data)
649 thread_data *td = (thread_data *)data;
652 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
653 pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
655 result = system(td->cmd);
656 td->done = TRUE; /* let the caller thread join() */
657 return GINT_TO_POINTER(result);
662 *\brief Execute a command defined in the matcher structure
664 *\param prop Pointer to matcher structure
665 *\param info Pointer to message info structure
667 *\return gboolean TRUE if command was executed succesfully
669 static gboolean matcherprop_match_test(const MatcherProp *prop,
678 thread_data *td = g_new0(thread_data, 1);
680 time_t start_time = time(NULL);
683 file = procmsg_get_message_file(info);
688 cmd = matching_build_command(prop->expr, info);
694 if (debug_filtering_session
695 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
696 log_print(LOG_DEBUG_FILTERING,
697 "starting threaded command [ %s ]\n",
703 if (pthread_attr_init(&pta) != 0 ||
704 pthread_attr_setdetachstate(&pta, PTHREAD_CREATE_JOINABLE) != 0 ||
705 pthread_create(&pt, &pta, matcher_test_thread, td) != 0)
706 retval = system(cmd);
708 debug_print("waiting for test thread\n");
710 /* don't let the interface freeze while waiting */
712 if (time(NULL) - start_time > 30) {
718 pthread_join(pt, &res);
719 retval = GPOINTER_TO_INT(res);
720 debug_print(" test thread returned %d\n", retval);
725 if (debug_filtering_session
726 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
727 log_print(LOG_DEBUG_FILTERING,
728 "starting synchronous command [ %s ]\n",
732 retval = system(cmd);
734 debug_print("Command exit code: %d\n", retval);
737 if (debug_filtering_session
738 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
739 log_print(LOG_DEBUG_FILTERING,
740 "command returned [ %d ]\n",
745 return (retval == 0);
749 *\brief Check if a message matches the condition in a matcher
752 *\param prop Pointer to matcher structure
753 *\param info Pointer to message info
755 *\return gboolean TRUE if a match
757 gboolean matcherprop_match(MatcherProp *prop,
762 switch(prop->criteria) {
763 case MATCHCRITERIA_ALL:
765 case MATCHCRITERIA_UNREAD:
766 return MSG_IS_UNREAD(info->flags);
767 case MATCHCRITERIA_NOT_UNREAD:
768 return !MSG_IS_UNREAD(info->flags);
769 case MATCHCRITERIA_NEW:
770 return MSG_IS_NEW(info->flags);
771 case MATCHCRITERIA_NOT_NEW:
772 return !MSG_IS_NEW(info->flags);
773 case MATCHCRITERIA_MARKED:
774 return MSG_IS_MARKED(info->flags);
775 case MATCHCRITERIA_NOT_MARKED:
776 return !MSG_IS_MARKED(info->flags);
777 case MATCHCRITERIA_DELETED:
778 return MSG_IS_DELETED(info->flags);
779 case MATCHCRITERIA_NOT_DELETED:
780 return !MSG_IS_DELETED(info->flags);
781 case MATCHCRITERIA_REPLIED:
782 return MSG_IS_REPLIED(info->flags);
783 case MATCHCRITERIA_NOT_REPLIED:
784 return !MSG_IS_REPLIED(info->flags);
785 case MATCHCRITERIA_FORWARDED:
786 return MSG_IS_FORWARDED(info->flags);
787 case MATCHCRITERIA_NOT_FORWARDED:
788 return !MSG_IS_FORWARDED(info->flags);
789 case MATCHCRITERIA_LOCKED:
790 return MSG_IS_LOCKED(info->flags);
791 case MATCHCRITERIA_NOT_LOCKED:
792 return !MSG_IS_LOCKED(info->flags);
793 case MATCHCRITERIA_SPAM:
794 return MSG_IS_SPAM(info->flags);
795 case MATCHCRITERIA_NOT_SPAM:
796 return !MSG_IS_SPAM(info->flags);
797 case MATCHCRITERIA_COLORLABEL:
799 gint color = MSG_GET_COLORLABEL_VALUE(info->flags);
800 gboolean ret = (color == prop->value);
803 if (debug_filtering_session
804 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
806 log_print(LOG_DEBUG_FILTERING,
807 "message color value [ %d ] matches color value [ %d ]\n",
810 log_print(LOG_DEBUG_FILTERING,
811 "message color value [ %d ] doesn't matches color value [ %d ]\n",
817 case MATCHCRITERIA_NOT_COLORLABEL:
819 gint color = MSG_GET_COLORLABEL_VALUE(info->flags);
820 gboolean ret = (color != prop->value);
823 if (debug_filtering_session
824 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
826 log_print(LOG_DEBUG_FILTERING,
827 "message color value [ %d ] matches color value [ %d ]\n",
830 log_print(LOG_DEBUG_FILTERING,
831 "message color value [ %d ] doesn't matches color value [ %d ]\n",
837 case MATCHCRITERIA_IGNORE_THREAD:
838 return MSG_IS_IGNORE_THREAD(info->flags);
839 case MATCHCRITERIA_NOT_IGNORE_THREAD:
840 return !MSG_IS_IGNORE_THREAD(info->flags);
841 case MATCHCRITERIA_WATCH_THREAD:
842 return MSG_IS_WATCH_THREAD(info->flags);
843 case MATCHCRITERIA_NOT_WATCH_THREAD:
844 return !MSG_IS_WATCH_THREAD(info->flags);
845 case MATCHCRITERIA_SUBJECT:
846 return matcherprop_string_match(prop, info->subject,
847 prefs_common_translated_header_name("Subject:"));
848 case MATCHCRITERIA_NOT_SUBJECT:
849 return !matcherprop_string_match(prop, info->subject,
850 prefs_common_translated_header_name("Subject:"));
851 case MATCHCRITERIA_FROM:
852 case MATCHCRITERIA_NOT_FROM:
857 context = g_strdup_printf(_("%s header"), prefs_common_translated_header_name("From:"));
858 ret = matcherprop_string_match(prop, info->from, context);
860 return (prop->criteria == MATCHCRITERIA_FROM)? ret : !ret;
862 case MATCHCRITERIA_TO:
863 case MATCHCRITERIA_NOT_TO:
868 context = g_strdup_printf(_("%s header"), prefs_common_translated_header_name("To:"));
869 ret = matcherprop_string_match(prop, info->to, context);
871 return (prop->criteria == MATCHCRITERIA_TO)? ret : !ret;
873 case MATCHCRITERIA_CC:
874 case MATCHCRITERIA_NOT_CC:
879 context = g_strdup_printf(_("%s header"), prefs_common_translated_header_name("Cc:"));
880 ret = matcherprop_string_match(prop, info->cc, context);
882 return (prop->criteria == MATCHCRITERIA_CC)? ret : !ret;
884 case MATCHCRITERIA_TO_OR_CC:
886 gchar *context1, *context2;
889 context1 = g_strdup_printf(_("%s header"), prefs_common_translated_header_name("To:"));
890 context2 = g_strdup_printf(_("%s header"), prefs_common_translated_header_name("Cc:"));
891 ret = matcherprop_string_match(prop, info->to, context1)
892 || matcherprop_string_match(prop, info->cc, context2);
897 case MATCHCRITERIA_NOT_TO_AND_NOT_CC:
899 gchar *context1, *context2;
902 context1 = g_strdup_printf(_("%s header"), prefs_common_translated_header_name("To:"));
903 context2 = g_strdup_printf(_("%s header"), prefs_common_translated_header_name("Cc:"));
904 ret = !(matcherprop_string_match(prop, info->to, context1)
905 || matcherprop_string_match(prop, info->cc, context2));
910 case MATCHCRITERIA_TAG:
911 case MATCHCRITERIA_NOT_TAG:
915 ret = matcherprop_tag_match(prop, info, _("Tag"));
916 return (prop->criteria == MATCHCRITERIA_TAG)? ret : !ret;
918 case MATCHCRITERIA_TAGGED:
919 case MATCHCRITERIA_NOT_TAGGED:
923 ret = (info->tags != NULL);
924 return (prop->criteria == MATCHCRITERIA_TAGGED)? ret : !ret;
926 case MATCHCRITERIA_AGE_GREATER:
932 age = ((t - info->date_t) / (60 * 60 * 24));
933 ret = (age > prop->value);
936 if (debug_filtering_session
937 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
939 log_print(LOG_DEBUG_FILTERING,
940 "message age [ %d ] is greater than [ %d ]\n",
943 log_print(LOG_DEBUG_FILTERING,
944 "message age [ %d ] is not greater than [ %d ]\n",
950 case MATCHCRITERIA_AGE_LOWER:
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 lower than [ %d ]\n",
967 log_print(LOG_DEBUG_FILTERING,
968 "message age [ %d ] is not lower than [ %d ]\n",
974 case MATCHCRITERIA_SCORE_GREATER:
976 gboolean ret = (info->score > prop->value);
979 if (debug_filtering_session
980 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
982 log_print(LOG_DEBUG_FILTERING,
983 "message score [ %d ] is greater than [ %d ]\n",
984 info->score, prop->value);
986 log_print(LOG_DEBUG_FILTERING,
987 "message score [ %d ] is not greater than [ %d ]\n",
988 info->score, prop->value);
993 case MATCHCRITERIA_SCORE_LOWER:
995 gboolean ret = (info->score < prop->value);
998 if (debug_filtering_session
999 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
1001 log_print(LOG_DEBUG_FILTERING,
1002 "message score [ %d ] is lower than [ %d ]\n",
1003 info->score, prop->value);
1005 log_print(LOG_DEBUG_FILTERING,
1006 "message score [ %d ] is not lower than [ %d ]\n",
1007 info->score, prop->value);
1012 case MATCHCRITERIA_SCORE_EQUAL:
1014 gboolean ret = (info->score == prop->value);
1017 if (debug_filtering_session
1018 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
1020 log_print(LOG_DEBUG_FILTERING,
1021 "message score [ %d ] is equal to [ %d ]\n",
1022 info->score, prop->value);
1024 log_print(LOG_DEBUG_FILTERING,
1025 "message score [ %d ] is not equal to [ %d ]\n",
1026 info->score, prop->value);
1031 case MATCHCRITERIA_SIZE_GREATER:
1033 /* FIXME: info->size is an off_t */
1034 gboolean ret = (info->size > (off_t) prop->value);
1037 if (debug_filtering_session
1038 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
1040 log_print(LOG_DEBUG_FILTERING,
1041 "message size [ %ld ] is greater than [ %d ]\n",
1042 info->size, prop->value);
1044 log_print(LOG_DEBUG_FILTERING,
1045 "message size [ %ld ] is not greater than [ %d ]\n",
1046 info->size, prop->value);
1051 case MATCHCRITERIA_SIZE_SMALLER:
1053 /* FIXME: info->size is an off_t */
1054 gboolean ret = (info->size < (off_t) prop->value);
1057 if (debug_filtering_session
1058 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
1060 log_print(LOG_DEBUG_FILTERING,
1061 "message size [ %ld ] is smaller than [ %d ]\n",
1062 info->size, prop->value);
1064 log_print(LOG_DEBUG_FILTERING,
1065 "message size [ %ld ] is not smaller than [ %d ]\n",
1066 info->size, prop->value);
1071 case MATCHCRITERIA_SIZE_EQUAL:
1073 /* FIXME: info->size is an off_t */
1074 gboolean ret = (info->size == (off_t) prop->value);
1077 if (debug_filtering_session
1078 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
1080 log_print(LOG_DEBUG_FILTERING,
1081 "message size [ %ld ] is equal to [ %d ]\n",
1082 info->size, prop->value);
1084 log_print(LOG_DEBUG_FILTERING,
1085 "message size [ %ld ] is not equal to [ %d ]\n",
1086 info->size, prop->value);
1091 case MATCHCRITERIA_PARTIAL:
1093 /* FIXME: info->size is an off_t */
1094 gboolean ret = (info->total_size != 0 && info->size != (off_t)info->total_size);
1097 if (debug_filtering_session
1098 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
1100 log_print(LOG_DEBUG_FILTERING,
1101 "message is partially downloaded, size [ %ld ] is less than total size [ %d ])\n",
1102 info->size, info->total_size);
1104 log_print(LOG_DEBUG_FILTERING,
1105 "message is not partially downloaded\n");
1110 case MATCHCRITERIA_NOT_PARTIAL:
1112 /* FIXME: info->size is an off_t */
1113 gboolean ret = (info->total_size == 0 || info->size == (off_t)info->total_size);
1116 if (debug_filtering_session
1117 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
1119 log_print(LOG_DEBUG_FILTERING,
1120 "message is not partially downloaded\n");
1122 log_print(LOG_DEBUG_FILTERING,
1123 "message is partially downloaded, size [ %ld ] is less than total size [ %d ])\n",
1124 info->size, info->total_size);
1129 case MATCHCRITERIA_NEWSGROUPS:
1130 case MATCHCRITERIA_NOT_NEWSGROUPS:
1135 context = g_strdup_printf(_("%s header"),
1136 prefs_common_translated_header_name("Newsgroups:"));
1137 ret = matcherprop_string_match(prop, info->newsgroups, context);
1139 return (prop->criteria == MATCHCRITERIA_NEWSGROUPS)? ret : !ret;
1141 case MATCHCRITERIA_INREPLYTO:
1142 case MATCHCRITERIA_NOT_INREPLYTO:
1147 context = g_strdup_printf(_("%s header"),
1148 prefs_common_translated_header_name("In-Reply-To:"));
1149 ret = matcherprop_string_match(prop, info->inreplyto, context);
1151 return (prop->criteria == MATCHCRITERIA_INREPLYTO)? ret : !ret;
1153 case MATCHCRITERIA_REFERENCES:
1154 case MATCHCRITERIA_NOT_REFERENCES:
1159 context = g_strdup_printf(_("%s header"),
1160 prefs_common_translated_header_name("References:"));
1161 ret = matcherprop_list_match(prop, info->references, context);
1163 return (prop->criteria == MATCHCRITERIA_REFERENCES)? ret : !ret;
1165 case MATCHCRITERIA_TEST:
1166 return matcherprop_match_test(prop, info);
1167 case MATCHCRITERIA_NOT_TEST:
1168 return !matcherprop_match_test(prop, info);
1174 /* ********************* MatcherList *************************** */
1177 *\brief Create a new list of matchers
1179 *\param matchers List of matcher structures
1180 *\param bool_and Operator
1182 *\return MatcherList * New list
1184 MatcherList *matcherlist_new(GSList *matchers, gboolean bool_and)
1188 cond = g_new0(MatcherList, 1);
1190 cond->matchers = matchers;
1191 cond->bool_and = bool_and;
1197 *\brief Frees a list of matchers
1199 *\param cond List of matchers
1201 void matcherlist_free(MatcherList *cond)
1205 g_return_if_fail(cond);
1206 for (l = cond->matchers ; l != NULL ; l = g_slist_next(l)) {
1207 matcherprop_free((MatcherProp *) l->data);
1213 *\brief Skip all headers in a message file
1215 *\param fp Message file
1217 static void matcherlist_skip_headers(FILE *fp)
1219 gchar buf[BUFFSIZE];
1221 while (procheader_get_one_field(buf, sizeof(buf), fp, NULL) != -1)
1226 *\brief Check if a header matches a matcher condition
1228 *\param matcher Matcher structure to check header for
1229 *\param buf Header name
1231 *\return boolean TRUE if matching header
1233 static gboolean matcherprop_match_one_header(MatcherProp *matcher,
1236 gboolean result = FALSE;
1237 Header *header = NULL;
1239 switch (matcher->criteria) {
1240 case MATCHCRITERIA_HEADER:
1241 case MATCHCRITERIA_NOT_HEADER:
1242 header = procheader_parse_header(buf);
1245 if (procheader_headername_equal(header->name,
1247 if (matcher->criteria == MATCHCRITERIA_HEADER)
1248 result = matcherprop_string_match(matcher, header->body, _("header"));
1250 result = !matcherprop_string_match(matcher, header->body, _("header"));
1251 procheader_header_free(header);
1255 procheader_header_free(header);
1258 case MATCHCRITERIA_HEADERS_PART:
1259 return matcherprop_string_match(matcher, buf, _("header line"));
1260 case MATCHCRITERIA_NOT_HEADERS_PART:
1261 return !matcherprop_string_match(matcher, buf, _("headers line"));
1262 case MATCHCRITERIA_MESSAGE:
1263 return matcherprop_string_decode_match(matcher, buf, _("message line"));
1264 case MATCHCRITERIA_NOT_MESSAGE:
1265 return !matcherprop_string_decode_match(matcher, buf, _("message line"));
1266 case MATCHCRITERIA_FOUND_IN_ADDRESSBOOK:
1267 case MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK:
1269 GSList *address_list = NULL;
1270 gint match = MATCH_ONE;
1271 gboolean found = FALSE;
1273 /* how many address headers are me trying to mach? */
1274 if (strcasecmp(matcher->header, _("Any")) == 0)
1276 else if (strcasecmp(matcher->header, Q_("Filtering Matcher Menu|All")) == 0)
1279 if (match == MATCH_ONE) {
1280 /* matching one address header exactly, is that the right one? */
1281 header = procheader_parse_header(buf);
1283 !procheader_headername_equal(header->name, matcher->header))
1285 address_list = address_list_append(address_list, header->body);
1286 if (address_list == NULL)
1290 header = procheader_parse_header(buf);
1293 /* address header is one of the headers we have to match when checking
1294 for any address header or all address headers? */
1295 if (procheader_headername_equal(header->name, "From") ||
1296 procheader_headername_equal(header->name, "To") ||
1297 procheader_headername_equal(header->name, "Cc") ||
1298 procheader_headername_equal(header->name, "Reply-To") ||
1299 procheader_headername_equal(header->name, "Sender"))
1300 address_list = address_list_append(address_list, header->body);
1301 if (address_list == NULL)
1305 found = match_with_addresses_in_addressbook
1306 (matcher, address_list, matcher->criteria,
1307 matcher->expr, match);
1308 g_slist_free(address_list);
1310 if (matcher->criteria == MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK)
1321 *\brief Check if the matcher structure wants headers to
1324 *\param matcher Matcher structure
1326 *\return gboolean TRUE if the matcher structure describes
1327 * a header match condition
1329 static gboolean matcherprop_criteria_headers(const MatcherProp *matcher)
1331 switch (matcher->criteria) {
1332 case MATCHCRITERIA_HEADER:
1333 case MATCHCRITERIA_NOT_HEADER:
1334 case MATCHCRITERIA_HEADERS_PART:
1335 case MATCHCRITERIA_NOT_HEADERS_PART:
1336 case MATCHCRITERIA_FOUND_IN_ADDRESSBOOK:
1337 case MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK:
1345 *\brief Check if the matcher structure wants the message
1346 * to be matched (just perform an action on any
1349 *\param matcher Matcher structure
1351 *\return gboolean TRUE if matcher condition should match
1354 static gboolean matcherprop_criteria_message(MatcherProp *matcher)
1356 switch (matcher->criteria) {
1357 case MATCHCRITERIA_MESSAGE:
1358 case MATCHCRITERIA_NOT_MESSAGE:
1366 *\brief Check if a list of conditions matches one header in
1369 *\param matchers List of conditions
1370 *\param fp Message file
1372 *\return gboolean TRUE if one of the headers is matched by
1373 * the list of conditions.
1375 static gboolean matcherlist_match_headers(MatcherList *matchers, FILE *fp)
1378 gchar buf[BUFFSIZE];
1380 while (procheader_get_one_field(buf, sizeof(buf), fp, NULL) != -1) {
1381 for (l = matchers->matchers ; l != NULL ; l = g_slist_next(l)) {
1382 MatcherProp *matcher = (MatcherProp *) l->data;
1383 gint match = MATCH_ANY;
1388 /* determine the match range (all, any are our concern here) */
1389 if (matcher->criteria == MATCHCRITERIA_NOT_HEADERS_PART ||
1390 matcher->criteria == MATCHCRITERIA_NOT_MESSAGE) {
1393 } else if (matcher->criteria == MATCHCRITERIA_FOUND_IN_ADDRESSBOOK ||
1394 matcher->criteria == MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK) {
1395 Header *header = NULL;
1397 /* address header is one of the headers we have to match when checking
1398 for any address header or all address headers? */
1399 header = procheader_parse_header(buf);
1401 (procheader_headername_equal(header->name, "From") ||
1402 procheader_headername_equal(header->name, "To") ||
1403 procheader_headername_equal(header->name, "Cc") ||
1404 procheader_headername_equal(header->name, "Reply-To") ||
1405 procheader_headername_equal(header->name, "Sender"))) {
1407 if (strcasecmp(matcher->header, _("Any")) == 0)
1409 else if (strcasecmp(matcher->header, Q_("Filtering Matcher Menu|All")) == 0)
1414 /* further call to matcherprop_match_one_header() can't match
1415 and it irrelevant, so: don't alter the match result */
1420 /* ZERO line must NOT match for the rule to match.
1422 if (match == MATCH_ALL) {
1423 if (matcherprop_match_one_header(matcher, buf)) {
1424 matcher->result = TRUE;
1426 matcher->result = FALSE;
1427 matcher->done = TRUE;
1429 /* else, just one line matching is enough for the rule to match
1431 } else if (matcherprop_criteria_headers(matcher) ||
1432 matcherprop_criteria_message(matcher)) {
1433 if (matcherprop_match_one_header(matcher, buf)) {
1434 matcher->result = TRUE;
1435 matcher->done = TRUE;
1439 /* if the rule matched and the matchers are OR, no need to
1440 * check the others */
1441 if (matcher->result && matcher->done) {
1442 if (!matchers->bool_and)
1452 *\brief Check if a matcher wants to check the message body
1454 *\param matcher Matcher structure
1456 *\return gboolean TRUE if body must be matched.
1458 static gboolean matcherprop_criteria_body(const MatcherProp *matcher)
1460 switch (matcher->criteria) {
1461 case MATCHCRITERIA_BODY_PART:
1462 case MATCHCRITERIA_NOT_BODY_PART:
1470 *\brief Check if a (line) string matches the criteria
1471 * described by a matcher structure
1473 *\param matcher Matcher structure
1476 *\return gboolean TRUE if string matches criteria
1478 static gboolean matcherprop_match_line(MatcherProp *matcher, const gchar *line)
1480 switch (matcher->criteria) {
1481 case MATCHCRITERIA_BODY_PART:
1482 case MATCHCRITERIA_MESSAGE:
1483 return matcherprop_string_decode_match(matcher, line, _("body line"));
1484 case MATCHCRITERIA_NOT_BODY_PART:
1485 case MATCHCRITERIA_NOT_MESSAGE:
1486 return !matcherprop_string_decode_match(matcher, line, _("body line"));
1492 *\brief Check if a line in a message file's body matches
1495 *\param matchers List of conditions
1496 *\param fp Message file
1498 *\return gboolean TRUE if succesful match
1500 static gboolean matcherlist_match_body(MatcherList *matchers, FILE *fp)
1503 gchar buf[BUFFSIZE];
1505 while (fgets(buf, sizeof(buf), fp) != NULL) {
1506 for (l = matchers->matchers ; l != NULL ; l = g_slist_next(l)) {
1507 MatcherProp *matcher = (MatcherProp *) l->data;
1512 /* if the criteria is ~body_part or ~message, ZERO lines
1513 * must NOT match for the rule to match. */
1514 if (matcher->criteria == MATCHCRITERIA_NOT_BODY_PART ||
1515 matcher->criteria == MATCHCRITERIA_NOT_MESSAGE) {
1516 if (matcherprop_match_line(matcher, buf)) {
1517 matcher->result = TRUE;
1519 matcher->result = FALSE;
1520 matcher->done = TRUE;
1522 /* else, just one line has to match */
1523 } else if (matcherprop_criteria_body(matcher) ||
1524 matcherprop_criteria_message(matcher)) {
1525 if (matcherprop_match_line(matcher, buf)) {
1526 matcher->result = TRUE;
1527 matcher->done = TRUE;
1531 /* if the matchers are OR'ed and the rule matched,
1532 * no need to check the others. */
1533 if (matcher->result && matcher->done) {
1534 if (!matchers->bool_and)
1543 *\brief Check if a message file matches criteria
1545 *\param matchers Criteria
1546 *\param info Message info
1547 *\param result Default result
1549 *\return gboolean TRUE if matched
1551 static gboolean matcherlist_match_file(MatcherList *matchers, MsgInfo *info,
1554 gboolean read_headers;
1560 /* file need to be read ? */
1562 read_headers = FALSE;
1564 for (l = matchers->matchers ; l != NULL ; l = g_slist_next(l)) {
1565 MatcherProp *matcher = (MatcherProp *) l->data;
1567 if (matcherprop_criteria_headers(matcher))
1568 read_headers = TRUE;
1569 if (matcherprop_criteria_body(matcher))
1571 if (matcherprop_criteria_message(matcher)) {
1572 read_headers = TRUE;
1575 matcher->result = FALSE;
1576 matcher->done = FALSE;
1579 if (!read_headers && !read_body)
1582 file = procmsg_get_message_file_full(info, read_headers, read_body);
1586 if ((fp = g_fopen(file, "rb")) == NULL) {
1587 FILE_OP_ERROR(file, "fopen");
1592 /* read the headers */
1595 if (matcherlist_match_headers(matchers, fp))
1598 matcherlist_skip_headers(fp);
1603 matcherlist_match_body(matchers, fp);
1606 for (l = matchers->matchers; l != NULL; l = g_slist_next(l)) {
1607 MatcherProp *matcher = (MatcherProp *) l->data;
1609 if (matcherprop_criteria_headers(matcher) ||
1610 matcherprop_criteria_body(matcher) ||
1611 matcherprop_criteria_message(matcher)) {
1612 if (matcher->result) {
1613 if (!matchers->bool_and) {
1619 if (matchers->bool_and) {
1635 *\brief Test list of conditions on a message.
1637 *\param matchers List of conditions
1638 *\param info Message info
1640 *\return gboolean TRUE if matched
1642 gboolean matcherlist_match(MatcherList *matchers, MsgInfo *info)
1650 if (matchers->bool_and)
1655 /* test the cached elements */
1657 for (l = matchers->matchers; l != NULL ;l = g_slist_next(l)) {
1658 MatcherProp *matcher = (MatcherProp *) l->data;
1660 if (debug_filtering_session) {
1661 gchar *buf = matcherprop_to_string(matcher);
1662 log_print(LOG_DEBUG_FILTERING, _("checking if message matches [ %s ]\n"), buf);
1666 switch(matcher->criteria) {
1667 case MATCHCRITERIA_ALL:
1668 case MATCHCRITERIA_UNREAD:
1669 case MATCHCRITERIA_NOT_UNREAD:
1670 case MATCHCRITERIA_NEW:
1671 case MATCHCRITERIA_NOT_NEW:
1672 case MATCHCRITERIA_MARKED:
1673 case MATCHCRITERIA_NOT_MARKED:
1674 case MATCHCRITERIA_DELETED:
1675 case MATCHCRITERIA_NOT_DELETED:
1676 case MATCHCRITERIA_REPLIED:
1677 case MATCHCRITERIA_NOT_REPLIED:
1678 case MATCHCRITERIA_FORWARDED:
1679 case MATCHCRITERIA_NOT_FORWARDED:
1680 case MATCHCRITERIA_LOCKED:
1681 case MATCHCRITERIA_NOT_LOCKED:
1682 case MATCHCRITERIA_SPAM:
1683 case MATCHCRITERIA_NOT_SPAM:
1684 case MATCHCRITERIA_COLORLABEL:
1685 case MATCHCRITERIA_NOT_COLORLABEL:
1686 case MATCHCRITERIA_IGNORE_THREAD:
1687 case MATCHCRITERIA_NOT_IGNORE_THREAD:
1688 case MATCHCRITERIA_WATCH_THREAD:
1689 case MATCHCRITERIA_NOT_WATCH_THREAD:
1690 case MATCHCRITERIA_SUBJECT:
1691 case MATCHCRITERIA_NOT_SUBJECT:
1692 case MATCHCRITERIA_FROM:
1693 case MATCHCRITERIA_NOT_FROM:
1694 case MATCHCRITERIA_TO:
1695 case MATCHCRITERIA_NOT_TO:
1696 case MATCHCRITERIA_CC:
1697 case MATCHCRITERIA_NOT_CC:
1698 case MATCHCRITERIA_TO_OR_CC:
1699 case MATCHCRITERIA_NOT_TO_AND_NOT_CC:
1700 case MATCHCRITERIA_TAG:
1701 case MATCHCRITERIA_NOT_TAG:
1702 case MATCHCRITERIA_TAGGED:
1703 case MATCHCRITERIA_NOT_TAGGED:
1704 case MATCHCRITERIA_AGE_GREATER:
1705 case MATCHCRITERIA_AGE_LOWER:
1706 case MATCHCRITERIA_NEWSGROUPS:
1707 case MATCHCRITERIA_NOT_NEWSGROUPS:
1708 case MATCHCRITERIA_INREPLYTO:
1709 case MATCHCRITERIA_NOT_INREPLYTO:
1710 case MATCHCRITERIA_REFERENCES:
1711 case MATCHCRITERIA_NOT_REFERENCES:
1712 case MATCHCRITERIA_SCORE_GREATER:
1713 case MATCHCRITERIA_SCORE_LOWER:
1714 case MATCHCRITERIA_SCORE_EQUAL:
1715 case MATCHCRITERIA_SIZE_GREATER:
1716 case MATCHCRITERIA_SIZE_SMALLER:
1717 case MATCHCRITERIA_SIZE_EQUAL:
1718 case MATCHCRITERIA_TEST:
1719 case MATCHCRITERIA_NOT_TEST:
1720 case MATCHCRITERIA_PARTIAL:
1721 case MATCHCRITERIA_NOT_PARTIAL:
1722 if (matcherprop_match(matcher, info)) {
1723 if (!matchers->bool_and) {
1724 if (debug_filtering_session)
1725 log_status_ok(LOG_DEBUG_FILTERING, _("message matches\n"));
1730 if (matchers->bool_and) {
1731 if (debug_filtering_session)
1732 log_status_nok(LOG_DEBUG_FILTERING, _("message does not match\n"));
1739 /* test the condition on the file */
1741 if (matcherlist_match_file(matchers, info, result)) {
1742 if (!matchers->bool_and) {
1743 if (debug_filtering_session)
1744 log_status_ok(LOG_DEBUG_FILTERING, _("message matches\n"));
1748 if (matchers->bool_and) {
1749 if (debug_filtering_session)
1750 log_status_nok(LOG_DEBUG_FILTERING, _("message does not match\n"));
1755 if (debug_filtering_session) {
1757 log_status_ok(LOG_DEBUG_FILTERING, _("message matches\n"));
1759 log_status_nok(LOG_DEBUG_FILTERING, _("message does not match\n"));
1765 static gint quote_filter_str(gchar * result, guint size,
1775 for(p = path ; * p != '\0' ; p ++) {
1777 if ((* p != '\"') && (* p != '\\')) {
1778 if (remaining > 0) {
1784 result[size - 1] = '\0';
1789 if (remaining >= 2) {
1797 result[size - 1] = '\0';
1802 if (remaining > 0) {
1806 result[size - 1] = '\0';
1814 gchar * matcher_quote_str(const gchar * src)
1819 len = strlen(src) * 2 + 1;
1820 res = g_malloc(len);
1821 quote_filter_str(res, len, src);
1827 *\brief Convert a matcher structure to a string
1829 *\param matcher Matcher structure
1831 *\return gchar * Newly allocated string
1833 gchar *matcherprop_to_string(MatcherProp *matcher)
1835 gchar *matcher_str = NULL;
1836 const gchar *criteria_str;
1837 const gchar *matchtype_str;
1839 gchar * quoted_expr;
1840 gchar * quoted_header;
1842 criteria_str = NULL;
1843 for (i = 0; i < (int) (sizeof(matchparser_tab) / sizeof(MatchParser)); i++) {
1844 if (matchparser_tab[i].id == matcher->criteria)
1845 criteria_str = matchparser_tab[i].str;
1847 if (criteria_str == NULL)
1850 switch (matcher->criteria) {
1851 case MATCHCRITERIA_AGE_GREATER:
1852 case MATCHCRITERIA_AGE_LOWER:
1853 case MATCHCRITERIA_SCORE_GREATER:
1854 case MATCHCRITERIA_SCORE_LOWER:
1855 case MATCHCRITERIA_SCORE_EQUAL:
1856 case MATCHCRITERIA_SIZE_GREATER:
1857 case MATCHCRITERIA_SIZE_SMALLER:
1858 case MATCHCRITERIA_SIZE_EQUAL:
1859 case MATCHCRITERIA_COLORLABEL:
1860 case MATCHCRITERIA_NOT_COLORLABEL:
1861 return g_strdup_printf("%s %i", criteria_str, matcher->value);
1862 case MATCHCRITERIA_ALL:
1863 case MATCHCRITERIA_UNREAD:
1864 case MATCHCRITERIA_NOT_UNREAD:
1865 case MATCHCRITERIA_NEW:
1866 case MATCHCRITERIA_NOT_NEW:
1867 case MATCHCRITERIA_MARKED:
1868 case MATCHCRITERIA_NOT_MARKED:
1869 case MATCHCRITERIA_DELETED:
1870 case MATCHCRITERIA_NOT_DELETED:
1871 case MATCHCRITERIA_REPLIED:
1872 case MATCHCRITERIA_NOT_REPLIED:
1873 case MATCHCRITERIA_FORWARDED:
1874 case MATCHCRITERIA_NOT_FORWARDED:
1875 case MATCHCRITERIA_LOCKED:
1876 case MATCHCRITERIA_NOT_LOCKED:
1877 case MATCHCRITERIA_SPAM:
1878 case MATCHCRITERIA_NOT_SPAM:
1879 case MATCHCRITERIA_PARTIAL:
1880 case MATCHCRITERIA_NOT_PARTIAL:
1881 case MATCHCRITERIA_IGNORE_THREAD:
1882 case MATCHCRITERIA_NOT_IGNORE_THREAD:
1883 case MATCHCRITERIA_WATCH_THREAD:
1884 case MATCHCRITERIA_NOT_WATCH_THREAD:
1885 case MATCHCRITERIA_TAGGED:
1886 case MATCHCRITERIA_NOT_TAGGED:
1887 return g_strdup(criteria_str);
1888 case MATCHCRITERIA_TEST:
1889 case MATCHCRITERIA_NOT_TEST:
1890 quoted_expr = matcher_quote_str(matcher->expr);
1891 matcher_str = g_strdup_printf("%s \"%s\"",
1892 criteria_str, quoted_expr);
1893 g_free(quoted_expr);
1895 case MATCHCRITERIA_FOUND_IN_ADDRESSBOOK:
1896 case MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK:
1897 quoted_header = matcher_quote_str(matcher->header);
1898 quoted_expr = matcher_quote_str(matcher->expr);
1899 matcher_str = g_strdup_printf("%s \"%s\" in \"%s\"",
1900 criteria_str, quoted_header, quoted_expr);
1901 g_free(quoted_header);
1902 g_free(quoted_expr);
1906 matchtype_str = NULL;
1907 for (i = 0; i < sizeof matchparser_tab / sizeof matchparser_tab[0]; i++) {
1908 if (matchparser_tab[i].id == matcher->matchtype)
1909 matchtype_str = matchparser_tab[i].str;
1912 if (matchtype_str == NULL)
1915 switch (matcher->matchtype) {
1916 case MATCHTYPE_MATCH:
1917 case MATCHTYPE_MATCHCASE:
1918 case MATCHTYPE_REGEXP:
1919 case MATCHTYPE_REGEXPCASE:
1920 quoted_expr = matcher_quote_str(matcher->expr);
1921 if (matcher->header) {
1922 quoted_header = matcher_quote_str(matcher->header);
1923 matcher_str = g_strdup_printf
1924 ("%s \"%s\" %s \"%s\"",
1925 criteria_str, quoted_header,
1926 matchtype_str, quoted_expr);
1927 g_free(quoted_header);
1930 matcher_str = g_strdup_printf
1931 ("%s %s \"%s\"", criteria_str,
1932 matchtype_str, quoted_expr);
1933 g_free(quoted_expr);
1941 *\brief Convert a list of conditions to a string
1943 *\param matchers List of conditions
1945 *\return gchar * Newly allocated string
1947 gchar *matcherlist_to_string(const MatcherList *matchers)
1953 gchar *result = NULL;
1955 count = g_slist_length(matchers->matchers);
1956 vstr = g_new(gchar *, count + 1);
1958 for (l = matchers->matchers, cur_str = vstr; l != NULL;
1959 l = g_slist_next(l), cur_str ++) {
1960 *cur_str = matcherprop_to_string((MatcherProp *) l->data);
1961 if (*cur_str == NULL)
1966 if (matchers->bool_and)
1967 result = g_strjoinv(" & ", vstr);
1969 result = g_strjoinv(" | ", vstr);
1971 for (cur_str = vstr ; *cur_str != NULL ; cur_str ++)
1979 #define STRLEN_ZERO(s) ((s) ? strlen(s) : 0)
1980 #define STRLEN_DEFAULT(s,d) ((s) ? strlen(s) : STRLEN_ZERO(d))
1982 static void add_str_default(gchar ** dest,
1983 const gchar * s, const gchar * d)
1985 gchar quoted_str[4096];
1993 quote_cmd_argument(quoted_str, sizeof(quoted_str), str);
1994 strcpy(* dest, quoted_str);
1996 (* dest) += strlen(* dest);
1999 /* matching_build_command() - preferably cmd should be unescaped */
2001 *\brief Build the command line to execute
2003 *\param cmd String with command line specifiers
2004 *\param info Message info to use for command
2006 *\return gchar * Newly allocated string
2008 gchar *matching_build_command(const gchar *cmd, MsgInfo *info)
2010 const gchar *s = cmd;
2011 gchar *filename = NULL;
2012 gchar *processed_cmd;
2016 const gchar *const no_subject = _("(none)") ;
2017 const gchar *const no_from = _("(none)") ;
2018 const gchar *const no_to = _("(none)") ;
2019 const gchar *const no_cc = _("(none)") ;
2020 const gchar *const no_date = _("(none)") ;
2021 const gchar *const no_msgid = _("(none)") ;
2022 const gchar *const no_newsgroups = _("(none)") ;
2023 const gchar *const no_references = _("(none)") ;
2025 size = STRLEN_ZERO(cmd) + 1;
2026 while (*s != '\0') {
2033 case 's': /* subject */
2034 size += STRLEN_DEFAULT(info->subject, no_subject) - 2;
2036 case 'f': /* from */
2037 size += STRLEN_DEFAULT(info->from, no_from) - 2;
2040 size += STRLEN_DEFAULT(info->to, no_to) - 2;
2043 size += STRLEN_DEFAULT(info->cc, no_cc) - 2;
2045 case 'd': /* date */
2046 size += STRLEN_DEFAULT(info->date, no_date) - 2;
2048 case 'i': /* message-id */
2049 size += STRLEN_DEFAULT(info->msgid, no_msgid) - 2;
2051 case 'n': /* newsgroups */
2052 size += STRLEN_DEFAULT(info->newsgroups, no_newsgroups) - 2;
2054 case 'r': /* references */
2055 /* FIXME: using the inreplyto header for reference */
2056 size += STRLEN_DEFAULT(info->inreplyto, no_references) - 2;
2058 case 'F': /* file */
2059 if (filename == NULL)
2060 filename = folder_item_fetch_msg(info->folder, info->msgnum);
2062 if (filename == NULL) {
2063 g_warning("filename is not set");
2067 size += strlen(filename) - 2;
2076 /* as the string can be quoted, we double the result */
2079 processed_cmd = g_new0(gchar, size);
2083 while (*s != '\0') {
2091 case 's': /* subject */
2092 add_str_default(&p, info->subject,
2095 case 'f': /* from */
2096 add_str_default(&p, info->from,
2100 add_str_default(&p, info->to,
2104 add_str_default(&p, info->cc,
2107 case 'd': /* date */
2108 add_str_default(&p, info->date,
2111 case 'i': /* message-id */
2112 add_str_default(&p, info->msgid,
2115 case 'n': /* newsgroups */
2116 add_str_default(&p, info->newsgroups,
2119 case 'r': /* references */
2120 /* FIXME: using the inreplyto header for references */
2121 add_str_default(&p, info->inreplyto, no_references);
2123 case 'F': /* file */
2124 if (filename != NULL)
2125 add_str_default(&p, filename, NULL);
2144 return processed_cmd;
2146 #undef STRLEN_DEFAULT
2149 /* ************************************************************ */
2153 *\brief Write filtering list to file
2156 *\param prefs_filtering List of filtering conditions
2158 static int prefs_filtering_write(FILE *fp, GSList *prefs_filtering)
2162 for (cur = prefs_filtering; cur != NULL; cur = cur->next) {
2163 gchar *filtering_str = NULL;
2164 gchar *tmp_name = NULL;
2165 FilteringProp *prop = NULL;
2167 if (NULL == (prop = (FilteringProp *) cur->data))
2170 if (NULL == (filtering_str = filteringprop_to_string(prop)))
2173 if (prop->enabled) {
2174 if (fputs("enabled ", fp) == EOF) {
2175 FILE_OP_ERROR("filtering config", "fputs");
2179 if (fputs("disabled ", fp) == EOF) {
2180 FILE_OP_ERROR("filtering config", "fputs");
2185 if (fputs("rulename \"", fp) == EOF) {
2186 FILE_OP_ERROR("filtering config", "fputs");
2187 g_free(filtering_str);
2190 tmp_name = prop->name;
2191 while (tmp_name && *tmp_name != '\0') {
2192 if (*tmp_name != '"') {
2193 if (fputc(*tmp_name, fp) == EOF) {
2194 FILE_OP_ERROR("filtering config", "fputs || fputc");
2195 g_free(filtering_str);
2198 } else if (*tmp_name == '"') {
2199 if (fputc('\\', fp) == EOF ||
2200 fputc('"', fp) == EOF) {
2201 FILE_OP_ERROR("filtering config", "fputs || fputc");
2202 g_free(filtering_str);
2208 if (fputs("\" ", fp) == EOF) {
2209 FILE_OP_ERROR("filtering config", "fputs");
2210 g_free(filtering_str);
2214 if (prop->account_id != 0) {
2217 tmp = g_strdup_printf("account %d ", prop->account_id);
2218 if (fputs(tmp, fp) == EOF) {
2219 FILE_OP_ERROR("filtering config", "fputs");
2226 if(fputs(filtering_str, fp) == EOF ||
2227 fputc('\n', fp) == EOF) {
2228 FILE_OP_ERROR("filtering config", "fputs || fputc");
2229 g_free(filtering_str);
2232 g_free(filtering_str);
2238 typedef struct _NodeLoopData {
2244 *\brief Write matchers from a folder item
2246 *\param node Node with folder info
2247 *\param data File pointer
2249 *\return gboolean FALSE
2251 static gboolean prefs_matcher_write_func(GNode *node, gpointer d)
2254 NodeLoopData *data = (NodeLoopData *)d;
2256 GSList *prefs_filtering;
2259 /* prevent warning */
2260 if (item->path == NULL)
2262 id = folder_item_get_identifier(item);
2265 prefs_filtering = item->prefs->processing;
2267 if (prefs_filtering != NULL) {
2268 if (fprintf(data->fp, "[%s]\n", id) < 0) {
2272 if (prefs_filtering_write(data->fp, prefs_filtering) < 0) {
2276 if (fputc('\n', data->fp) == EOF) {
2288 *\brief Save matchers from folder items
2292 static int prefs_matcher_save(FILE *fp)
2300 for (cur = folder_get_list() ; cur != NULL ; cur = g_list_next(cur)) {
2303 folder = (Folder *) cur->data;
2304 g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
2305 prefs_matcher_write_func, &data);
2308 if (data.error == TRUE)
2311 /* pre global rules */
2312 if (fprintf(fp, "[preglobal]\n") < 0 ||
2313 prefs_filtering_write(fp, pre_global_processing) < 0 ||
2314 fputc('\n', fp) == EOF)
2317 /* post global rules */
2318 if (fprintf(fp, "[postglobal]\n") < 0 ||
2319 prefs_filtering_write(fp, post_global_processing) < 0 ||
2320 fputc('\n', fp) == EOF)
2323 /* filtering rules */
2324 if (fprintf(fp, "[filtering]\n") < 0 ||
2325 prefs_filtering_write(fp, filtering_rules) < 0 ||
2326 fputc('\n', fp) == EOF)
2333 *\brief Write filtering / matcher configuration file
2335 void prefs_matcher_write_config(void)
2340 debug_print("Writing matcher configuration...\n");
2342 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
2345 if ((pfile = prefs_write_open(rcpath)) == NULL) {
2346 g_warning("failed to write configuration to file\n");
2353 if (prefs_matcher_save(pfile->fp) < 0) {
2354 g_warning("failed to write configuration to file\n");
2355 prefs_file_close_revert(pfile);
2356 } else if (prefs_file_close(pfile) < 0) {
2357 g_warning("failed to save configuration to file\n");
2361 /* ******************************************************************* */
2363 static void matcher_add_rulenames(const gchar *rcpath)
2365 gchar *newpath = g_strconcat(rcpath, ".new", NULL);
2366 FILE *src = g_fopen(rcpath, "rb");
2367 FILE *dst = g_fopen(newpath, "wb");
2368 gchar buf[BUFFSIZE];
2376 while (fgets (buf, sizeof(buf), src) != NULL) {
2377 if (strlen(buf) > 2 && buf[0] != '['
2378 && strncmp(buf, "rulename \"", 10)
2379 && strncmp(buf, "enabled rulename \"", 18)
2380 && strncmp(buf, "disabled rulename \"", 18)) {
2381 r = fwrite("enabled rulename \"\" ",
2382 strlen("enabled rulename \"\" "), 1, dst);
2384 r = fwrite(buf, strlen(buf), 1, dst);
2388 move_file(newpath, rcpath, TRUE);
2393 *\brief Read matcher configuration
2395 void prefs_matcher_read_config(void)
2398 gchar *rc_old_format;
2401 create_matchparser_hashtab();
2402 prefs_filtering_clear();
2404 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MATCHER_RC, NULL);
2405 rc_old_format = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MATCHER_RC,
2406 ".pre_names", NULL);
2408 if (!is_file_exist(rc_old_format) && is_file_exist(rcpath)) {
2409 /* backup file with no rules names, in case
2410 * anything goes wrong */
2411 copy_file(rcpath, rc_old_format, FALSE);
2412 /* now hack the file in order to have it to the new format */
2413 matcher_add_rulenames(rcpath);
2416 g_free(rc_old_format);
2418 f = g_fopen(rcpath, "rb");
2422 matcher_parser_start_parsing(f);
2423 fclose(matcher_parserin);
2426 /* previous version compatibility */
2428 /* g_print("reading filtering\n"); */
2429 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
2430 FILTERING_RC, NULL);
2431 f = g_fopen(rcpath, "rb");
2435 matcher_parser_start_parsing(f);
2436 fclose(matcher_parserin);
2439 /* g_print("reading scoring\n"); */
2440 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
2442 f = g_fopen(rcpath, "rb");
2446 matcher_parser_start_parsing(f);
2447 fclose(matcher_parserin);