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 prop->expr = expr != NULL ? g_strdup(expr) : NULL;
262 prop->matchtype = matchtype;
271 *\brief Allocate a structure for a filtering / scoring
272 * "condition" (a matcher structure)
273 * Same as matcherprop_new, except it doesn't change the expr's
276 *\param criteria Criteria ID (MATCHCRITERIA_XXXX)
277 *\param header Header string (if criteria is MATCHCRITERIA_HEADER
278 or MATCHCRITERIA_FOUND_IN_ADDRESSBOOK)
279 *\param matchtype Type of action (MATCHTYPE_XXX)
280 *\param expr String value or expression to check
281 *\param value Integer value to check
283 *\return MatcherProp * Pointer to newly allocated structure
285 MatcherProp *matcherprop_new_create(gint criteria, const gchar *header,
286 gint matchtype, const gchar *expr,
291 prop = g_new0(MatcherProp, 1);
292 prop->criteria = criteria;
293 prop->header = header != NULL ? g_strdup(header) : NULL;
295 prop->expr = expr != NULL ? g_strdup(expr) : NULL;
297 prop->matchtype = matchtype;
306 *\brief Free a matcher structure
308 *\param prop Pointer to matcher structure allocated with
311 void matcherprop_free(MatcherProp *prop)
314 g_free(prop->header);
315 if (prop->preg != NULL) {
323 *\brief Copy a matcher structure
325 *\param src Matcher structure to copy
327 *\return MatcherProp * Pointer to newly allocated matcher structure
329 MatcherProp *matcherprop_copy(const MatcherProp *src)
331 MatcherProp *prop = g_new0(MatcherProp, 1);
333 prop->criteria = src->criteria;
334 prop->header = src->header ? g_strdup(src->header) : NULL;
335 prop->expr = src->expr ? g_strdup(src->expr) : NULL;
336 prop->matchtype = src->matchtype;
338 prop->preg = NULL; /* will be re-evaluated */
339 prop->value = src->value;
340 prop->error = src->error;
344 /* ************** match ******************************/
346 static gboolean match_with_addresses_in_addressbook
347 (MatcherProp *prop, GSList *address_list, gint type,
348 gchar* folderpath, gint match)
351 gboolean found = FALSE;
354 g_return_val_if_fail(address_list != NULL, FALSE);
356 debug_print("match_with_addresses_in_addressbook(%d, %s)\n",
357 g_slist_length(address_list), folderpath?folderpath:"(null)");
359 if (folderpath == NULL ||
360 strcasecmp(folderpath, "Any") == 0 ||
366 start_address_completion(path);
368 for (walk = address_list; walk != NULL; walk = walk->next) {
369 /* exact matching of email address */
370 guint num_addr = complete_address(walk->data);
373 /* skip first item (this is the search string itself) */
375 for (; i < num_addr && !found; i++) {
376 gchar *addr = get_complete_address(i);
377 extract_address(addr);
378 if (strcasecmp(addr, walk->data) == 0) {
382 if (debug_filtering_session
383 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
384 log_print(LOG_DEBUG_FILTERING,
385 "address [ %s ] matches\n",
386 (gchar *)walk->data);
393 if (debug_filtering_session
394 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH
396 log_print(LOG_DEBUG_FILTERING,
397 "address [ %s ] does NOT match\n",
398 (gchar *)walk->data);
402 if (match == MATCH_ALL) {
403 /* if matching all addresses, stop if one doesn't match */
406 if (debug_filtering_session
407 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
408 log_print(LOG_DEBUG_FILTERING,
409 "not all address match (matching all)\n");
413 } else if (match == MATCH_ANY) {
414 /* if matching any address, stop if one does match */
417 if (debug_filtering_session
418 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
419 log_print(LOG_DEBUG_FILTERING,
420 "at least one address matches (matching any)\n");
425 /* MATCH_ONE: there should be only one loop iteration */
428 end_address_completion();
434 *\brief Find out if a string matches a condition
436 *\param prop Matcher structure
437 *\param str String to check
439 *\return gboolean TRUE if str matches the condition in the
442 static gboolean matcherprop_string_match(MatcherProp *prop, const gchar *str,
443 const gchar *debug_context)
447 gboolean ret = FALSE;
448 gboolean should_free = FALSE;
452 if (prop->matchtype == MATCHTYPE_REGEXPCASE ||
453 prop->matchtype == MATCHTYPE_MATCHCASE) {
454 str1 = g_utf8_casefold(str, -1);
455 down_expr = g_utf8_casefold(prop->expr, -1);
459 down_expr = (gchar *)prop->expr;
463 switch (prop->matchtype) {
464 case MATCHTYPE_REGEXPCASE:
465 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, down_expr,
471 REG_NOSUB | REG_EXTENDED
472 | ((prop->matchtype == MATCHTYPE_REGEXPCASE)
473 ? REG_ICASE : 0)) != 0) {
479 if (prop->preg == NULL) {
484 if (regexec(prop->preg, str1, 0, NULL, 0) == 0)
490 if (debug_filtering_session
491 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
492 gchar *stripped = g_strdup(str);
494 strretchomp(stripped);
496 log_print(LOG_DEBUG_FILTERING,
497 "%s value [ %s ] matches regular expression [ %s ] (%s)\n",
498 debug_context, stripped, prop->expr,
499 prop->matchtype == MATCHTYPE_REGEXP ? _("Case sensitive"):_("Case insensitive"));
501 log_print(LOG_DEBUG_FILTERING,
502 "%s value [ %s ] does NOT match regular expression [ %s ] (%s)\n",
503 debug_context, stripped, prop->expr,
504 prop->matchtype == MATCHTYPE_REGEXP ? _("Case sensitive"):_("Case insensitive"));
510 case MATCHTYPE_MATCHCASE:
511 case MATCHTYPE_MATCH:
512 ret = (strstr(str1, down_expr) != NULL);
515 if (debug_filtering_session
516 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
517 gchar *stripped = g_strdup(str);
519 strretchomp(stripped);
521 log_print(LOG_DEBUG_FILTERING,
522 "%s value [ %s ] contains [ %s ] (%s)\n",
523 debug_context, stripped, prop->expr,
524 prop->matchtype == MATCHTYPE_MATCH ? _("Case sensitive"):_("Case insensitive"));
526 log_print(LOG_DEBUG_FILTERING,
527 "%s value [ %s ] does NOT contain [ %s ] (%s)\n",
528 debug_context, stripped, prop->expr,
529 prop->matchtype == MATCHTYPE_MATCH ? _("Case sensitive"):_("Case insensitive"));
548 *\brief Find out if a tag matches a condition
550 *\param prop Matcher structure
551 *\param msginfo message to check
553 *\return gboolean TRUE if msginfo matches the condition in the
556 static gboolean matcherprop_tag_match(MatcherProp *prop, MsgInfo *msginfo,
557 const gchar *debug_context)
559 gboolean ret = FALSE;
562 if (msginfo == NULL || msginfo->tags == NULL)
565 for (cur = msginfo->tags; cur; cur = cur->next) {
566 const gchar *str = tags_get_tag(GPOINTER_TO_INT(cur->data));
569 if (matcherprop_string_match(prop, str, debug_context)) {
578 *\brief Find out if the string-ed list matches a condition
580 *\param prop Matcher structure
581 *\param list GSList of strings to check
583 *\return gboolean TRUE if str matches the condition in the
586 static gboolean matcherprop_list_match(MatcherProp *prop, const GSList *list,
587 const gchar *debug_context)
591 for(cur = list; cur != NULL; cur = cur->next) {
592 if (matcherprop_string_match(prop, (gchar *)cur->data, debug_context))
598 /* FIXME body search is a hack. */
599 static gboolean matcherprop_string_decode_match(MatcherProp *prop, const gchar *str,
600 const gchar *debug_context)
604 gboolean res = FALSE;
609 /* we try to decode QP first, because it's faster than base64 */
610 qp_decode_const(tmp, BUFFSIZE-1, str);
611 if (!g_utf8_validate(tmp, -1, NULL)) {
612 utf = conv_codeset_strdup
613 (tmp, conv_get_locale_charset_str_no_utf8(),
615 res = matcherprop_string_match(prop, utf, debug_context);
618 res = matcherprop_string_match(prop, tmp, debug_context);
621 if (res == FALSE && (strchr(prop->expr, '=') || strchr(prop->expr, '_')
622 || strchr(str, '=') || strchr(str, '_'))) {
623 /* if searching for something with an equal char, maybe
624 * we should try to match the non-decoded string.
625 * In case it was not qp-encoded. */
626 if (!g_utf8_validate(str, -1, NULL)) {
627 utf = conv_codeset_strdup
628 (str, conv_get_locale_charset_str_no_utf8(),
630 res = matcherprop_string_match(prop, utf, debug_context);
633 res = matcherprop_string_match(prop, str, debug_context);
637 /* FIXME base64 decoding is too slow, especially since text can
638 * easily be handled as base64. Don't even try now. */
644 typedef struct _thread_data {
651 static void *matcher_test_thread(void *data)
653 thread_data *td = (thread_data *)data;
656 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
657 pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
659 result = system(td->cmd);
660 td->done = TRUE; /* let the caller thread join() */
661 return GINT_TO_POINTER(result);
666 *\brief Execute a command defined in the matcher structure
668 *\param prop Pointer to matcher structure
669 *\param info Pointer to message info structure
671 *\return gboolean TRUE if command was executed succesfully
673 static gboolean matcherprop_match_test(const MatcherProp *prop,
682 thread_data *td = g_new0(thread_data, 1);
684 time_t start_time = time(NULL);
687 file = procmsg_get_message_file(info);
692 cmd = matching_build_command(prop->expr, info);
698 if (debug_filtering_session
699 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
700 log_print(LOG_DEBUG_FILTERING,
701 "starting threaded command [ %s ]\n",
707 if (pthread_attr_init(&pta) != 0 ||
708 pthread_attr_setdetachstate(&pta, PTHREAD_CREATE_JOINABLE) != 0 ||
709 pthread_create(&pt, &pta, matcher_test_thread, td) != 0)
710 retval = system(cmd);
712 debug_print("waiting for test thread\n");
714 /* don't let the interface freeze while waiting */
716 if (time(NULL) - start_time > 30) {
722 pthread_join(pt, &res);
723 retval = GPOINTER_TO_INT(res);
724 debug_print(" test thread returned %d\n", retval);
729 if (debug_filtering_session
730 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
731 log_print(LOG_DEBUG_FILTERING,
732 "starting synchronous command [ %s ]\n",
736 retval = system(cmd);
738 debug_print("Command exit code: %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 "command returned [ %d ]\n",
749 return (retval == 0);
753 *\brief Check if a message matches the condition in a matcher
756 *\param prop Pointer to matcher structure
757 *\param info Pointer to message info
759 *\return gboolean TRUE if a match
761 gboolean matcherprop_match(MatcherProp *prop,
766 switch(prop->criteria) {
767 case MATCHCRITERIA_ALL:
769 case MATCHCRITERIA_UNREAD:
770 return MSG_IS_UNREAD(info->flags);
771 case MATCHCRITERIA_NOT_UNREAD:
772 return !MSG_IS_UNREAD(info->flags);
773 case MATCHCRITERIA_NEW:
774 return MSG_IS_NEW(info->flags);
775 case MATCHCRITERIA_NOT_NEW:
776 return !MSG_IS_NEW(info->flags);
777 case MATCHCRITERIA_MARKED:
778 return MSG_IS_MARKED(info->flags);
779 case MATCHCRITERIA_NOT_MARKED:
780 return !MSG_IS_MARKED(info->flags);
781 case MATCHCRITERIA_DELETED:
782 return MSG_IS_DELETED(info->flags);
783 case MATCHCRITERIA_NOT_DELETED:
784 return !MSG_IS_DELETED(info->flags);
785 case MATCHCRITERIA_REPLIED:
786 return MSG_IS_REPLIED(info->flags);
787 case MATCHCRITERIA_NOT_REPLIED:
788 return !MSG_IS_REPLIED(info->flags);
789 case MATCHCRITERIA_FORWARDED:
790 return MSG_IS_FORWARDED(info->flags);
791 case MATCHCRITERIA_NOT_FORWARDED:
792 return !MSG_IS_FORWARDED(info->flags);
793 case MATCHCRITERIA_LOCKED:
794 return MSG_IS_LOCKED(info->flags);
795 case MATCHCRITERIA_NOT_LOCKED:
796 return !MSG_IS_LOCKED(info->flags);
797 case MATCHCRITERIA_SPAM:
798 return MSG_IS_SPAM(info->flags);
799 case MATCHCRITERIA_NOT_SPAM:
800 return !MSG_IS_SPAM(info->flags);
801 case MATCHCRITERIA_COLORLABEL:
803 gint color = MSG_GET_COLORLABEL_VALUE(info->flags);
804 gboolean ret = (color == prop->value);
807 if (debug_filtering_session
808 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
810 log_print(LOG_DEBUG_FILTERING,
811 "message color value [ %d ] matches color value [ %d ]\n",
814 log_print(LOG_DEBUG_FILTERING,
815 "message color value [ %d ] does NOT match color value [ %d ]\n",
821 case MATCHCRITERIA_NOT_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_IGNORE_THREAD:
842 return MSG_IS_IGNORE_THREAD(info->flags);
843 case MATCHCRITERIA_NOT_IGNORE_THREAD:
844 return !MSG_IS_IGNORE_THREAD(info->flags);
845 case MATCHCRITERIA_WATCH_THREAD:
846 return MSG_IS_WATCH_THREAD(info->flags);
847 case MATCHCRITERIA_NOT_WATCH_THREAD:
848 return !MSG_IS_WATCH_THREAD(info->flags);
849 case MATCHCRITERIA_SUBJECT:
850 return matcherprop_string_match(prop, info->subject,
851 prefs_common_translated_header_name("Subject:"));
852 case MATCHCRITERIA_NOT_SUBJECT:
853 return !matcherprop_string_match(prop, info->subject,
854 prefs_common_translated_header_name("Subject:"));
855 case MATCHCRITERIA_FROM:
856 case MATCHCRITERIA_NOT_FROM:
861 context = g_strdup_printf(_("%s header"), prefs_common_translated_header_name("From:"));
862 ret = matcherprop_string_match(prop, info->from, context);
864 return (prop->criteria == MATCHCRITERIA_FROM)? ret : !ret;
866 case MATCHCRITERIA_TO:
867 case MATCHCRITERIA_NOT_TO:
872 context = g_strdup_printf(_("%s header"), prefs_common_translated_header_name("To:"));
873 ret = matcherprop_string_match(prop, info->to, context);
875 return (prop->criteria == MATCHCRITERIA_TO)? ret : !ret;
877 case MATCHCRITERIA_CC:
878 case MATCHCRITERIA_NOT_CC:
883 context = g_strdup_printf(_("%s header"), prefs_common_translated_header_name("Cc:"));
884 ret = matcherprop_string_match(prop, info->cc, context);
886 return (prop->criteria == MATCHCRITERIA_CC)? ret : !ret;
888 case MATCHCRITERIA_TO_OR_CC:
890 gchar *context1, *context2;
893 context1 = g_strdup_printf(_("%s header"), prefs_common_translated_header_name("To:"));
894 context2 = g_strdup_printf(_("%s header"), prefs_common_translated_header_name("Cc:"));
895 ret = matcherprop_string_match(prop, info->to, context1)
896 || matcherprop_string_match(prop, info->cc, context2);
901 case MATCHCRITERIA_NOT_TO_AND_NOT_CC:
903 gchar *context1, *context2;
906 context1 = g_strdup_printf(_("%s header"), prefs_common_translated_header_name("To:"));
907 context2 = g_strdup_printf(_("%s header"), prefs_common_translated_header_name("Cc:"));
908 ret = !(matcherprop_string_match(prop, info->to, context1)
909 || matcherprop_string_match(prop, info->cc, context2));
914 case MATCHCRITERIA_TAG:
915 case MATCHCRITERIA_NOT_TAG:
919 ret = matcherprop_tag_match(prop, info, _("Tag"));
920 return (prop->criteria == MATCHCRITERIA_TAG)? ret : !ret;
922 case MATCHCRITERIA_TAGGED:
923 case MATCHCRITERIA_NOT_TAGGED:
927 ret = (info->tags != NULL);
928 return (prop->criteria == MATCHCRITERIA_TAGGED)? ret : !ret;
930 case MATCHCRITERIA_AGE_GREATER:
936 age = ((t - info->date_t) / (60 * 60 * 24));
937 ret = (age > prop->value);
940 if (debug_filtering_session
941 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
943 log_print(LOG_DEBUG_FILTERING,
944 "message age [ %d ] is greater than [ %d ]\n",
947 log_print(LOG_DEBUG_FILTERING,
948 "message age [ %d ] is not greater than [ %d ]\n",
954 case MATCHCRITERIA_AGE_LOWER:
960 age = ((t - info->date_t) / (60 * 60 * 24));
961 ret = (age < prop->value);
964 if (debug_filtering_session
965 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
967 log_print(LOG_DEBUG_FILTERING,
968 "message age [ %d ] is lower than [ %d ]\n",
971 log_print(LOG_DEBUG_FILTERING,
972 "message age [ %d ] is not lower than [ %d ]\n",
978 case MATCHCRITERIA_SCORE_GREATER:
980 gboolean ret = (info->score > prop->value);
983 if (debug_filtering_session
984 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
986 log_print(LOG_DEBUG_FILTERING,
987 "message score [ %d ] is greater than [ %d ]\n",
988 info->score, prop->value);
990 log_print(LOG_DEBUG_FILTERING,
991 "message score [ %d ] is not greater than [ %d ]\n",
992 info->score, prop->value);
997 case MATCHCRITERIA_SCORE_LOWER:
999 gboolean ret = (info->score < prop->value);
1002 if (debug_filtering_session
1003 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
1005 log_print(LOG_DEBUG_FILTERING,
1006 "message score [ %d ] is lower than [ %d ]\n",
1007 info->score, prop->value);
1009 log_print(LOG_DEBUG_FILTERING,
1010 "message score [ %d ] is not lower than [ %d ]\n",
1011 info->score, prop->value);
1016 case MATCHCRITERIA_SCORE_EQUAL:
1018 gboolean ret = (info->score == prop->value);
1021 if (debug_filtering_session
1022 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
1024 log_print(LOG_DEBUG_FILTERING,
1025 "message score [ %d ] is equal to [ %d ]\n",
1026 info->score, prop->value);
1028 log_print(LOG_DEBUG_FILTERING,
1029 "message score [ %d ] is not equal to [ %d ]\n",
1030 info->score, prop->value);
1035 case MATCHCRITERIA_SIZE_GREATER:
1037 /* FIXME: info->size is a goffset */
1038 gboolean ret = (info->size > (goffset) 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 size is greater than [ %d ]\n",
1048 log_print(LOG_DEBUG_FILTERING,
1049 "message size is not greater than [ %d ]\n",
1055 case MATCHCRITERIA_SIZE_SMALLER:
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 smaller than [ %d ]\n",
1068 log_print(LOG_DEBUG_FILTERING,
1069 "message size is not smaller than [ %d ]\n",
1075 case MATCHCRITERIA_SIZE_EQUAL:
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 equal to [ %d ]\n",
1088 log_print(LOG_DEBUG_FILTERING,
1089 "message size is not equal to [ %d ]\n",
1095 case MATCHCRITERIA_PARTIAL:
1097 /* FIXME: info->size is a goffset */
1098 gboolean ret = (info->total_size != 0 && info->size != (goffset)info->total_size);
1101 if (debug_filtering_session
1102 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
1104 log_print(LOG_DEBUG_FILTERING,
1105 "message is partially downloaded, size is less than total size [ %d ])\n",
1108 log_print(LOG_DEBUG_FILTERING,
1109 "message is not partially downloaded\n");
1114 case MATCHCRITERIA_NOT_PARTIAL:
1116 /* FIXME: info->size is a goffset */
1117 gboolean ret = (info->total_size == 0 || info->size == (goffset)info->total_size);
1120 if (debug_filtering_session
1121 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
1123 log_print(LOG_DEBUG_FILTERING,
1124 "message is not partially downloaded\n");
1126 log_print(LOG_DEBUG_FILTERING,
1127 "message is partially downloaded, size is less than total size [ %d ])\n",
1133 case MATCHCRITERIA_NEWSGROUPS:
1134 case MATCHCRITERIA_NOT_NEWSGROUPS:
1139 context = g_strdup_printf(_("%s header"),
1140 prefs_common_translated_header_name("Newsgroups:"));
1141 ret = matcherprop_string_match(prop, info->newsgroups, context);
1143 return (prop->criteria == MATCHCRITERIA_NEWSGROUPS)? ret : !ret;
1145 case MATCHCRITERIA_INREPLYTO:
1146 case MATCHCRITERIA_NOT_INREPLYTO:
1151 context = g_strdup_printf(_("%s header"),
1152 prefs_common_translated_header_name("In-Reply-To:"));
1153 ret = matcherprop_string_match(prop, info->inreplyto, context);
1155 return (prop->criteria == MATCHCRITERIA_INREPLYTO)? ret : !ret;
1157 case MATCHCRITERIA_REFERENCES:
1158 case MATCHCRITERIA_NOT_REFERENCES:
1163 context = g_strdup_printf(_("%s header"),
1164 prefs_common_translated_header_name("References:"));
1165 ret = matcherprop_list_match(prop, info->references, context);
1167 return (prop->criteria == MATCHCRITERIA_REFERENCES)? ret : !ret;
1169 case MATCHCRITERIA_TEST:
1170 return matcherprop_match_test(prop, info);
1171 case MATCHCRITERIA_NOT_TEST:
1172 return !matcherprop_match_test(prop, info);
1178 /* ********************* MatcherList *************************** */
1181 *\brief Create a new list of matchers
1183 *\param matchers List of matcher structures
1184 *\param bool_and Operator
1186 *\return MatcherList * New list
1188 MatcherList *matcherlist_new(GSList *matchers, gboolean bool_and)
1192 cond = g_new0(MatcherList, 1);
1194 cond->matchers = matchers;
1195 cond->bool_and = bool_and;
1201 *\brief Frees a list of matchers
1203 *\param cond List of matchers
1205 void matcherlist_free(MatcherList *cond)
1209 g_return_if_fail(cond);
1210 for (l = cond->matchers ; l != NULL ; l = g_slist_next(l)) {
1211 matcherprop_free((MatcherProp *) l->data);
1213 g_slist_free(cond->matchers);
1218 *\brief Skip all headers in a message file
1220 *\param fp Message file
1222 static void matcherlist_skip_headers(FILE *fp)
1224 gchar buf[BUFFSIZE];
1226 while (procheader_get_one_field(buf, sizeof(buf), fp, NULL) != -1)
1231 *\brief Check if a header matches a matcher condition
1233 *\param matcher Matcher structure to check header for
1234 *\param buf Header name
1236 *\return boolean TRUE if matching header
1238 static gboolean matcherprop_match_one_header(MatcherProp *matcher,
1241 gboolean result = FALSE;
1242 Header *header = NULL;
1244 switch (matcher->criteria) {
1245 case MATCHCRITERIA_HEADER:
1246 case MATCHCRITERIA_NOT_HEADER:
1247 header = procheader_parse_header(buf);
1250 if (procheader_headername_equal(header->name,
1252 if (matcher->criteria == MATCHCRITERIA_HEADER)
1253 result = matcherprop_string_match(matcher, header->body, _("header"));
1255 result = !matcherprop_string_match(matcher, header->body, _("header"));
1256 procheader_header_free(header);
1260 procheader_header_free(header);
1263 case MATCHCRITERIA_HEADERS_PART:
1264 return matcherprop_string_match(matcher, buf, _("header line"));
1265 case MATCHCRITERIA_NOT_HEADERS_PART:
1266 return !matcherprop_string_match(matcher, buf, _("headers line"));
1267 case MATCHCRITERIA_MESSAGE:
1268 return matcherprop_string_decode_match(matcher, buf, _("message line"));
1269 case MATCHCRITERIA_NOT_MESSAGE:
1270 return !matcherprop_string_decode_match(matcher, buf, _("message line"));
1271 case MATCHCRITERIA_FOUND_IN_ADDRESSBOOK:
1272 case MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK:
1274 GSList *address_list = NULL;
1275 gint match = MATCH_ONE;
1276 gboolean found = FALSE;
1278 /* how many address headers are me trying to mach? */
1279 if (strcasecmp(matcher->header, "Any") == 0)
1281 else if (strcasecmp(matcher->header, "All") == 0)
1284 if (match == MATCH_ONE) {
1285 /* matching one address header exactly, is that the right one? */
1286 header = procheader_parse_header(buf);
1288 !procheader_headername_equal(header->name, matcher->header))
1290 address_list = address_list_append(address_list, header->body);
1291 if (address_list == NULL)
1295 header = procheader_parse_header(buf);
1298 /* address header is one of the headers we have to match when checking
1299 for any address header or all address headers? */
1300 if (procheader_headername_equal(header->name, "From") ||
1301 procheader_headername_equal(header->name, "To") ||
1302 procheader_headername_equal(header->name, "Cc") ||
1303 procheader_headername_equal(header->name, "Reply-To") ||
1304 procheader_headername_equal(header->name, "Sender"))
1305 address_list = address_list_append(address_list, header->body);
1306 if (address_list == NULL)
1310 found = match_with_addresses_in_addressbook
1311 (matcher, address_list, matcher->criteria,
1312 matcher->expr, match);
1313 g_slist_free(address_list);
1315 if (matcher->criteria == MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK)
1326 *\brief Check if the matcher structure wants headers to
1329 *\param matcher Matcher structure
1331 *\return gboolean TRUE if the matcher structure describes
1332 * a header match condition
1334 static gboolean matcherprop_criteria_headers(const MatcherProp *matcher)
1336 switch (matcher->criteria) {
1337 case MATCHCRITERIA_HEADER:
1338 case MATCHCRITERIA_NOT_HEADER:
1339 case MATCHCRITERIA_HEADERS_PART:
1340 case MATCHCRITERIA_NOT_HEADERS_PART:
1341 case MATCHCRITERIA_FOUND_IN_ADDRESSBOOK:
1342 case MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK:
1350 *\brief Check if the matcher structure wants the message
1351 * to be matched (just perform an action on any
1354 *\param matcher Matcher structure
1356 *\return gboolean TRUE if matcher condition should match
1359 static gboolean matcherprop_criteria_message(MatcherProp *matcher)
1361 switch (matcher->criteria) {
1362 case MATCHCRITERIA_MESSAGE:
1363 case MATCHCRITERIA_NOT_MESSAGE:
1371 *\brief Check if a list of conditions matches one header in
1374 *\param matchers List of conditions
1375 *\param fp Message file
1377 *\return gboolean TRUE if one of the headers is matched by
1378 * the list of conditions.
1380 static gboolean matcherlist_match_headers(MatcherList *matchers, FILE *fp)
1383 gchar buf[BUFFSIZE];
1385 while (procheader_get_one_field(buf, sizeof(buf), fp, NULL) != -1) {
1386 for (l = matchers->matchers ; l != NULL ; l = g_slist_next(l)) {
1387 MatcherProp *matcher = (MatcherProp *) l->data;
1388 gint match = MATCH_ANY;
1393 /* determine the match range (all, any are our concern here) */
1394 if (matcher->criteria == MATCHCRITERIA_NOT_HEADERS_PART ||
1395 matcher->criteria == MATCHCRITERIA_NOT_MESSAGE) {
1398 } else if (matcher->criteria == MATCHCRITERIA_FOUND_IN_ADDRESSBOOK ||
1399 matcher->criteria == MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK) {
1400 Header *header = NULL;
1402 /* address header is one of the headers we have to match when checking
1403 for any address header or all address headers? */
1404 header = procheader_parse_header(buf);
1406 (procheader_headername_equal(header->name, "From") ||
1407 procheader_headername_equal(header->name, "To") ||
1408 procheader_headername_equal(header->name, "Cc") ||
1409 procheader_headername_equal(header->name, "Reply-To") ||
1410 procheader_headername_equal(header->name, "Sender"))) {
1412 if (strcasecmp(matcher->header, "Any") == 0)
1414 else if (strcasecmp(matcher->header, "All") == 0)
1419 /* further call to matcherprop_match_one_header() can't match
1420 and it irrelevant, so: don't alter the match result */
1425 /* ZERO line must NOT match for the rule to match.
1427 if (match == MATCH_ALL) {
1428 if (matcherprop_match_one_header(matcher, buf)) {
1429 matcher->result = TRUE;
1431 matcher->result = FALSE;
1432 matcher->done = TRUE;
1434 /* else, just one line matching is enough for the rule to match
1436 } else if (matcherprop_criteria_headers(matcher) ||
1437 matcherprop_criteria_message(matcher)) {
1438 if (matcherprop_match_one_header(matcher, buf)) {
1439 matcher->result = TRUE;
1440 matcher->done = TRUE;
1444 /* if the rule matched and the matchers are OR, no need to
1445 * check the others */
1446 if (matcher->result && matcher->done) {
1447 if (!matchers->bool_and)
1457 *\brief Check if a matcher wants to check the message body
1459 *\param matcher Matcher structure
1461 *\return gboolean TRUE if body must be matched.
1463 static gboolean matcherprop_criteria_body(const MatcherProp *matcher)
1465 switch (matcher->criteria) {
1466 case MATCHCRITERIA_BODY_PART:
1467 case MATCHCRITERIA_NOT_BODY_PART:
1475 *\brief Check if a (line) string matches the criteria
1476 * described by a matcher structure
1478 *\param matcher Matcher structure
1481 *\return gboolean TRUE if string matches criteria
1483 static gboolean matcherprop_match_line(MatcherProp *matcher, const gchar *line)
1485 switch (matcher->criteria) {
1486 case MATCHCRITERIA_BODY_PART:
1487 case MATCHCRITERIA_MESSAGE:
1488 return matcherprop_string_decode_match(matcher, line, _("body line"));
1489 case MATCHCRITERIA_NOT_BODY_PART:
1490 case MATCHCRITERIA_NOT_MESSAGE:
1491 return !matcherprop_string_decode_match(matcher, line, _("body line"));
1497 *\brief Check if a line in a message file's body matches
1500 *\param matchers List of conditions
1501 *\param fp Message file
1503 *\return gboolean TRUE if succesful match
1505 static gboolean matcherlist_match_body(MatcherList *matchers, FILE *fp)
1508 gchar buf[BUFFSIZE];
1510 while (fgets(buf, sizeof(buf), fp) != NULL) {
1511 for (l = matchers->matchers ; l != NULL ; l = g_slist_next(l)) {
1512 MatcherProp *matcher = (MatcherProp *) l->data;
1517 /* if the criteria is ~body_part or ~message, ZERO lines
1518 * must NOT match for the rule to match. */
1519 if (matcher->criteria == MATCHCRITERIA_NOT_BODY_PART ||
1520 matcher->criteria == MATCHCRITERIA_NOT_MESSAGE) {
1521 if (matcherprop_match_line(matcher, buf)) {
1522 matcher->result = TRUE;
1524 matcher->result = FALSE;
1525 matcher->done = TRUE;
1527 /* else, just one line has to match */
1528 } else if (matcherprop_criteria_body(matcher) ||
1529 matcherprop_criteria_message(matcher)) {
1530 if (matcherprop_match_line(matcher, buf)) {
1531 matcher->result = TRUE;
1532 matcher->done = TRUE;
1536 /* if the matchers are OR'ed and the rule matched,
1537 * no need to check the others. */
1538 if (matcher->result && matcher->done) {
1539 if (!matchers->bool_and)
1548 *\brief Check if a message file matches criteria
1550 *\param matchers Criteria
1551 *\param info Message info
1552 *\param result Default result
1554 *\return gboolean TRUE if matched
1556 static gboolean matcherlist_match_file(MatcherList *matchers, MsgInfo *info,
1559 gboolean read_headers;
1565 /* file need to be read ? */
1567 read_headers = FALSE;
1569 for (l = matchers->matchers ; l != NULL ; l = g_slist_next(l)) {
1570 MatcherProp *matcher = (MatcherProp *) l->data;
1572 if (matcherprop_criteria_headers(matcher))
1573 read_headers = TRUE;
1574 if (matcherprop_criteria_body(matcher))
1576 if (matcherprop_criteria_message(matcher)) {
1577 read_headers = TRUE;
1580 matcher->result = FALSE;
1581 matcher->done = FALSE;
1584 if (!read_headers && !read_body)
1587 file = procmsg_get_message_file_full(info, read_headers, read_body);
1591 if ((fp = g_fopen(file, "rb")) == NULL) {
1592 FILE_OP_ERROR(file, "fopen");
1597 /* read the headers */
1600 if (matcherlist_match_headers(matchers, fp))
1603 matcherlist_skip_headers(fp);
1608 matcherlist_match_body(matchers, fp);
1611 for (l = matchers->matchers; l != NULL; l = g_slist_next(l)) {
1612 MatcherProp *matcher = (MatcherProp *) l->data;
1614 if (matcherprop_criteria_headers(matcher) ||
1615 matcherprop_criteria_body(matcher) ||
1616 matcherprop_criteria_message(matcher)) {
1617 if (matcher->result) {
1618 if (!matchers->bool_and) {
1624 if (matchers->bool_and) {
1640 *\brief Test list of conditions on a message.
1642 *\param matchers List of conditions
1643 *\param info Message info
1645 *\return gboolean TRUE if matched
1647 gboolean matcherlist_match(MatcherList *matchers, MsgInfo *info)
1655 if (matchers->bool_and)
1660 /* test the cached elements */
1662 for (l = matchers->matchers; l != NULL ;l = g_slist_next(l)) {
1663 MatcherProp *matcher = (MatcherProp *) l->data;
1665 if (debug_filtering_session) {
1666 gchar *buf = matcherprop_to_string(matcher);
1667 log_print(LOG_DEBUG_FILTERING, _("checking if message matches [ %s ]\n"), buf);
1671 switch(matcher->criteria) {
1672 case MATCHCRITERIA_ALL:
1673 case MATCHCRITERIA_UNREAD:
1674 case MATCHCRITERIA_NOT_UNREAD:
1675 case MATCHCRITERIA_NEW:
1676 case MATCHCRITERIA_NOT_NEW:
1677 case MATCHCRITERIA_MARKED:
1678 case MATCHCRITERIA_NOT_MARKED:
1679 case MATCHCRITERIA_DELETED:
1680 case MATCHCRITERIA_NOT_DELETED:
1681 case MATCHCRITERIA_REPLIED:
1682 case MATCHCRITERIA_NOT_REPLIED:
1683 case MATCHCRITERIA_FORWARDED:
1684 case MATCHCRITERIA_NOT_FORWARDED:
1685 case MATCHCRITERIA_LOCKED:
1686 case MATCHCRITERIA_NOT_LOCKED:
1687 case MATCHCRITERIA_SPAM:
1688 case MATCHCRITERIA_NOT_SPAM:
1689 case MATCHCRITERIA_COLORLABEL:
1690 case MATCHCRITERIA_NOT_COLORLABEL:
1691 case MATCHCRITERIA_IGNORE_THREAD:
1692 case MATCHCRITERIA_NOT_IGNORE_THREAD:
1693 case MATCHCRITERIA_WATCH_THREAD:
1694 case MATCHCRITERIA_NOT_WATCH_THREAD:
1695 case MATCHCRITERIA_SUBJECT:
1696 case MATCHCRITERIA_NOT_SUBJECT:
1697 case MATCHCRITERIA_FROM:
1698 case MATCHCRITERIA_NOT_FROM:
1699 case MATCHCRITERIA_TO:
1700 case MATCHCRITERIA_NOT_TO:
1701 case MATCHCRITERIA_CC:
1702 case MATCHCRITERIA_NOT_CC:
1703 case MATCHCRITERIA_TO_OR_CC:
1704 case MATCHCRITERIA_NOT_TO_AND_NOT_CC:
1705 case MATCHCRITERIA_TAG:
1706 case MATCHCRITERIA_NOT_TAG:
1707 case MATCHCRITERIA_TAGGED:
1708 case MATCHCRITERIA_NOT_TAGGED:
1709 case MATCHCRITERIA_AGE_GREATER:
1710 case MATCHCRITERIA_AGE_LOWER:
1711 case MATCHCRITERIA_NEWSGROUPS:
1712 case MATCHCRITERIA_NOT_NEWSGROUPS:
1713 case MATCHCRITERIA_INREPLYTO:
1714 case MATCHCRITERIA_NOT_INREPLYTO:
1715 case MATCHCRITERIA_REFERENCES:
1716 case MATCHCRITERIA_NOT_REFERENCES:
1717 case MATCHCRITERIA_SCORE_GREATER:
1718 case MATCHCRITERIA_SCORE_LOWER:
1719 case MATCHCRITERIA_SCORE_EQUAL:
1720 case MATCHCRITERIA_SIZE_GREATER:
1721 case MATCHCRITERIA_SIZE_SMALLER:
1722 case MATCHCRITERIA_SIZE_EQUAL:
1723 case MATCHCRITERIA_TEST:
1724 case MATCHCRITERIA_NOT_TEST:
1725 case MATCHCRITERIA_PARTIAL:
1726 case MATCHCRITERIA_NOT_PARTIAL:
1727 if (matcherprop_match(matcher, info)) {
1728 if (!matchers->bool_and) {
1729 if (debug_filtering_session)
1730 log_status_ok(LOG_DEBUG_FILTERING, _("message matches\n"));
1735 if (matchers->bool_and) {
1736 if (debug_filtering_session)
1737 log_status_nok(LOG_DEBUG_FILTERING, _("message does not match\n"));
1744 /* test the condition on the file */
1746 if (matcherlist_match_file(matchers, info, result)) {
1747 if (!matchers->bool_and) {
1748 if (debug_filtering_session)
1749 log_status_ok(LOG_DEBUG_FILTERING, _("message matches\n"));
1753 if (matchers->bool_and) {
1754 if (debug_filtering_session)
1755 log_status_nok(LOG_DEBUG_FILTERING, _("message does not match\n"));
1760 if (debug_filtering_session) {
1762 log_status_ok(LOG_DEBUG_FILTERING, _("message matches\n"));
1764 log_status_nok(LOG_DEBUG_FILTERING, _("message does not match\n"));
1770 static gint quote_filter_str(gchar * result, guint size,
1780 for(p = path ; * p != '\0' ; p ++) {
1782 if ((* p != '\"') && (* p != '\\')) {
1783 if (remaining > 0) {
1789 result[size - 1] = '\0';
1794 if (remaining >= 2) {
1802 result[size - 1] = '\0';
1807 if (remaining > 0) {
1811 result[size - 1] = '\0';
1819 gchar * matcher_quote_str(const gchar * src)
1824 len = strlen(src) * 2 + 1;
1825 res = g_malloc(len);
1826 quote_filter_str(res, len, src);
1832 *\brief Convert a matcher structure to a string
1834 *\param matcher Matcher structure
1836 *\return gchar * Newly allocated string
1838 gchar *matcherprop_to_string(MatcherProp *matcher)
1840 gchar *matcher_str = NULL;
1841 const gchar *criteria_str;
1842 const gchar *matchtype_str;
1844 gchar * quoted_expr;
1845 gchar * quoted_header;
1847 criteria_str = NULL;
1848 for (i = 0; i < (int) (sizeof(matchparser_tab) / sizeof(MatchParser)); i++) {
1849 if (matchparser_tab[i].id == matcher->criteria)
1850 criteria_str = matchparser_tab[i].str;
1852 if (criteria_str == NULL)
1855 switch (matcher->criteria) {
1856 case MATCHCRITERIA_AGE_GREATER:
1857 case MATCHCRITERIA_AGE_LOWER:
1858 case MATCHCRITERIA_SCORE_GREATER:
1859 case MATCHCRITERIA_SCORE_LOWER:
1860 case MATCHCRITERIA_SCORE_EQUAL:
1861 case MATCHCRITERIA_SIZE_GREATER:
1862 case MATCHCRITERIA_SIZE_SMALLER:
1863 case MATCHCRITERIA_SIZE_EQUAL:
1864 case MATCHCRITERIA_COLORLABEL:
1865 case MATCHCRITERIA_NOT_COLORLABEL:
1866 return g_strdup_printf("%s %i", criteria_str, matcher->value);
1867 case MATCHCRITERIA_ALL:
1868 case MATCHCRITERIA_UNREAD:
1869 case MATCHCRITERIA_NOT_UNREAD:
1870 case MATCHCRITERIA_NEW:
1871 case MATCHCRITERIA_NOT_NEW:
1872 case MATCHCRITERIA_MARKED:
1873 case MATCHCRITERIA_NOT_MARKED:
1874 case MATCHCRITERIA_DELETED:
1875 case MATCHCRITERIA_NOT_DELETED:
1876 case MATCHCRITERIA_REPLIED:
1877 case MATCHCRITERIA_NOT_REPLIED:
1878 case MATCHCRITERIA_FORWARDED:
1879 case MATCHCRITERIA_NOT_FORWARDED:
1880 case MATCHCRITERIA_LOCKED:
1881 case MATCHCRITERIA_NOT_LOCKED:
1882 case MATCHCRITERIA_SPAM:
1883 case MATCHCRITERIA_NOT_SPAM:
1884 case MATCHCRITERIA_PARTIAL:
1885 case MATCHCRITERIA_NOT_PARTIAL:
1886 case MATCHCRITERIA_IGNORE_THREAD:
1887 case MATCHCRITERIA_NOT_IGNORE_THREAD:
1888 case MATCHCRITERIA_WATCH_THREAD:
1889 case MATCHCRITERIA_NOT_WATCH_THREAD:
1890 case MATCHCRITERIA_TAGGED:
1891 case MATCHCRITERIA_NOT_TAGGED:
1892 return g_strdup(criteria_str);
1893 case MATCHCRITERIA_TEST:
1894 case MATCHCRITERIA_NOT_TEST:
1895 quoted_expr = matcher_quote_str(matcher->expr);
1896 matcher_str = g_strdup_printf("%s \"%s\"",
1897 criteria_str, quoted_expr);
1898 g_free(quoted_expr);
1900 case MATCHCRITERIA_FOUND_IN_ADDRESSBOOK:
1901 case MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK:
1902 quoted_header = matcher_quote_str(matcher->header);
1903 quoted_expr = matcher_quote_str(matcher->expr);
1904 matcher_str = g_strdup_printf("%s \"%s\" in \"%s\"",
1905 criteria_str, quoted_header, quoted_expr);
1906 g_free(quoted_header);
1907 g_free(quoted_expr);
1911 matchtype_str = NULL;
1912 for (i = 0; i < sizeof matchparser_tab / sizeof matchparser_tab[0]; i++) {
1913 if (matchparser_tab[i].id == matcher->matchtype)
1914 matchtype_str = matchparser_tab[i].str;
1917 if (matchtype_str == NULL)
1920 switch (matcher->matchtype) {
1921 case MATCHTYPE_MATCH:
1922 case MATCHTYPE_MATCHCASE:
1923 case MATCHTYPE_REGEXP:
1924 case MATCHTYPE_REGEXPCASE:
1925 quoted_expr = matcher_quote_str(matcher->expr);
1926 if (matcher->header) {
1927 quoted_header = matcher_quote_str(matcher->header);
1928 matcher_str = g_strdup_printf
1929 ("%s \"%s\" %s \"%s\"",
1930 criteria_str, quoted_header,
1931 matchtype_str, quoted_expr);
1932 g_free(quoted_header);
1935 matcher_str = g_strdup_printf
1936 ("%s %s \"%s\"", criteria_str,
1937 matchtype_str, quoted_expr);
1938 g_free(quoted_expr);
1946 *\brief Convert a list of conditions to a string
1948 *\param matchers List of conditions
1950 *\return gchar * Newly allocated string
1952 gchar *matcherlist_to_string(const MatcherList *matchers)
1958 gchar *result = NULL;
1960 count = g_slist_length(matchers->matchers);
1961 vstr = g_new(gchar *, count + 1);
1963 for (l = matchers->matchers, cur_str = vstr; l != NULL;
1964 l = g_slist_next(l), cur_str ++) {
1965 *cur_str = matcherprop_to_string((MatcherProp *) l->data);
1966 if (*cur_str == NULL)
1971 if (matchers->bool_and)
1972 result = g_strjoinv(" & ", vstr);
1974 result = g_strjoinv(" | ", vstr);
1976 for (cur_str = vstr ; *cur_str != NULL ; cur_str ++)
1984 #define STRLEN_ZERO(s) ((s) ? strlen(s) : 0)
1985 #define STRLEN_DEFAULT(s,d) ((s) ? strlen(s) : STRLEN_ZERO(d))
1987 static void add_str_default(gchar ** dest,
1988 const gchar * s, const gchar * d)
1990 gchar quoted_str[4096];
1998 quote_cmd_argument(quoted_str, sizeof(quoted_str), str);
1999 strcpy(* dest, quoted_str);
2001 (* dest) += strlen(* dest);
2004 /* matching_build_command() - preferably cmd should be unescaped */
2006 *\brief Build the command-line to execute
2008 *\param cmd String with command-line specifiers
2009 *\param info Message info to use for command
2011 *\return gchar * Newly allocated string
2013 gchar *matching_build_command(const gchar *cmd, MsgInfo *info)
2015 const gchar *s = cmd;
2016 gchar *filename = NULL;
2017 gchar *processed_cmd;
2021 const gchar *const no_subject = _("(none)") ;
2022 const gchar *const no_from = _("(none)") ;
2023 const gchar *const no_to = _("(none)") ;
2024 const gchar *const no_cc = _("(none)") ;
2025 const gchar *const no_date = _("(none)") ;
2026 const gchar *const no_msgid = _("(none)") ;
2027 const gchar *const no_newsgroups = _("(none)") ;
2028 const gchar *const no_references = _("(none)") ;
2030 size = STRLEN_ZERO(cmd) + 1;
2031 while (*s != '\0') {
2038 case 's': /* subject */
2039 size += STRLEN_DEFAULT(info->subject, no_subject) - 2;
2041 case 'f': /* from */
2042 size += STRLEN_DEFAULT(info->from, no_from) - 2;
2045 size += STRLEN_DEFAULT(info->to, no_to) - 2;
2048 size += STRLEN_DEFAULT(info->cc, no_cc) - 2;
2050 case 'd': /* date */
2051 size += STRLEN_DEFAULT(info->date, no_date) - 2;
2053 case 'i': /* message-id */
2054 size += STRLEN_DEFAULT(info->msgid, no_msgid) - 2;
2056 case 'n': /* newsgroups */
2057 size += STRLEN_DEFAULT(info->newsgroups, no_newsgroups) - 2;
2059 case 'r': /* references */
2060 /* FIXME: using the inreplyto header for reference */
2061 size += STRLEN_DEFAULT(info->inreplyto, no_references) - 2;
2063 case 'F': /* file */
2064 if (filename == NULL)
2065 filename = folder_item_fetch_msg(info->folder, info->msgnum);
2067 if (filename == NULL) {
2068 g_warning("filename is not set");
2072 size += strlen(filename) - 2;
2081 /* as the string can be quoted, we double the result */
2084 processed_cmd = g_new0(gchar, size);
2088 while (*s != '\0') {
2096 case 's': /* subject */
2097 add_str_default(&p, info->subject,
2100 case 'f': /* from */
2101 add_str_default(&p, info->from,
2105 add_str_default(&p, info->to,
2109 add_str_default(&p, info->cc,
2112 case 'd': /* date */
2113 add_str_default(&p, info->date,
2116 case 'i': /* message-id */
2117 add_str_default(&p, info->msgid,
2120 case 'n': /* newsgroups */
2121 add_str_default(&p, info->newsgroups,
2124 case 'r': /* references */
2125 /* FIXME: using the inreplyto header for references */
2126 add_str_default(&p, info->inreplyto, no_references);
2128 case 'F': /* file */
2129 if (filename != NULL)
2130 add_str_default(&p, filename, NULL);
2149 return processed_cmd;
2151 #undef STRLEN_DEFAULT
2154 /* ************************************************************ */
2158 *\brief Write filtering list to file
2161 *\param prefs_filtering List of filtering conditions
2163 static int prefs_filtering_write(FILE *fp, GSList *prefs_filtering)
2167 for (cur = prefs_filtering; cur != NULL; cur = cur->next) {
2168 gchar *filtering_str = NULL;
2169 gchar *tmp_name = NULL;
2170 FilteringProp *prop = NULL;
2172 if (NULL == (prop = (FilteringProp *) cur->data))
2175 if (NULL == (filtering_str = filteringprop_to_string(prop)))
2178 if (prop->enabled) {
2179 if (fputs("enabled ", fp) == EOF) {
2180 FILE_OP_ERROR("filtering config", "fputs");
2184 if (fputs("disabled ", fp) == EOF) {
2185 FILE_OP_ERROR("filtering config", "fputs");
2190 if (fputs("rulename \"", fp) == EOF) {
2191 FILE_OP_ERROR("filtering config", "fputs");
2192 g_free(filtering_str);
2195 tmp_name = prop->name;
2196 while (tmp_name && *tmp_name != '\0') {
2197 if (*tmp_name != '"') {
2198 if (fputc(*tmp_name, fp) == EOF) {
2199 FILE_OP_ERROR("filtering config", "fputs || fputc");
2200 g_free(filtering_str);
2203 } else if (*tmp_name == '"') {
2204 if (fputc('\\', fp) == EOF ||
2205 fputc('"', fp) == EOF) {
2206 FILE_OP_ERROR("filtering config", "fputs || fputc");
2207 g_free(filtering_str);
2213 if (fputs("\" ", fp) == EOF) {
2214 FILE_OP_ERROR("filtering config", "fputs");
2215 g_free(filtering_str);
2219 if (prop->account_id != 0) {
2222 tmp = g_strdup_printf("account %d ", prop->account_id);
2223 if (fputs(tmp, fp) == EOF) {
2224 FILE_OP_ERROR("filtering config", "fputs");
2231 if(fputs(filtering_str, fp) == EOF ||
2232 fputc('\n', fp) == EOF) {
2233 FILE_OP_ERROR("filtering config", "fputs || fputc");
2234 g_free(filtering_str);
2237 g_free(filtering_str);
2243 typedef struct _NodeLoopData {
2249 *\brief Write matchers from a folder item
2251 *\param node Node with folder info
2252 *\param data File pointer
2254 *\return gboolean FALSE
2256 static gboolean prefs_matcher_write_func(GNode *node, gpointer d)
2259 NodeLoopData *data = (NodeLoopData *)d;
2261 GSList *prefs_filtering;
2264 /* prevent warning */
2265 if (item->path == NULL)
2267 id = folder_item_get_identifier(item);
2270 prefs_filtering = item->prefs->processing;
2272 if (prefs_filtering != NULL) {
2273 if (fprintf(data->fp, "[%s]\n", id) < 0) {
2277 if (prefs_filtering_write(data->fp, prefs_filtering) < 0) {
2281 if (fputc('\n', data->fp) == EOF) {
2293 *\brief Save matchers from folder items
2297 static int prefs_matcher_save(FILE *fp)
2305 for (cur = folder_get_list() ; cur != NULL ; cur = g_list_next(cur)) {
2308 folder = (Folder *) cur->data;
2309 g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
2310 prefs_matcher_write_func, &data);
2313 if (data.error == TRUE)
2316 /* pre global rules */
2317 if (fprintf(fp, "[preglobal]\n") < 0 ||
2318 prefs_filtering_write(fp, pre_global_processing) < 0 ||
2319 fputc('\n', fp) == EOF)
2322 /* post global rules */
2323 if (fprintf(fp, "[postglobal]\n") < 0 ||
2324 prefs_filtering_write(fp, post_global_processing) < 0 ||
2325 fputc('\n', fp) == EOF)
2328 /* filtering rules */
2329 if (fprintf(fp, "[filtering]\n") < 0 ||
2330 prefs_filtering_write(fp, filtering_rules) < 0 ||
2331 fputc('\n', fp) == EOF)
2338 *\brief Write filtering / matcher configuration file
2340 void prefs_matcher_write_config(void)
2345 debug_print("Writing matcher configuration...\n");
2347 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
2350 if ((pfile = prefs_write_open(rcpath)) == NULL) {
2351 g_warning("failed to write configuration to file\n");
2358 if (prefs_matcher_save(pfile->fp) < 0) {
2359 g_warning("failed to write configuration to file\n");
2360 prefs_file_close_revert(pfile);
2361 } else if (prefs_file_close(pfile) < 0) {
2362 g_warning("failed to save configuration to file\n");
2366 /* ******************************************************************* */
2368 static void matcher_add_rulenames(const gchar *rcpath)
2370 gchar *newpath = g_strconcat(rcpath, ".new", NULL);
2371 FILE *src = g_fopen(rcpath, "rb");
2372 FILE *dst = g_fopen(newpath, "wb");
2373 gchar buf[BUFFSIZE];
2381 while (fgets (buf, sizeof(buf), src) != NULL) {
2382 if (strlen(buf) > 2 && buf[0] != '['
2383 && strncmp(buf, "rulename \"", 10)
2384 && strncmp(buf, "enabled rulename \"", 18)
2385 && strncmp(buf, "disabled rulename \"", 18)) {
2386 r = fwrite("enabled rulename \"\" ",
2387 strlen("enabled rulename \"\" "), 1, dst);
2389 r = fwrite(buf, strlen(buf), 1, dst);
2393 move_file(newpath, rcpath, TRUE);
2398 *\brief Read matcher configuration
2400 void prefs_matcher_read_config(void)
2403 gchar *rc_old_format;
2406 create_matchparser_hashtab();
2407 prefs_filtering_clear();
2409 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MATCHER_RC, NULL);
2410 rc_old_format = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MATCHER_RC,
2411 ".pre_names", NULL);
2413 if (!is_file_exist(rc_old_format) && is_file_exist(rcpath)) {
2414 /* backup file with no rules names, in case
2415 * anything goes wrong */
2416 copy_file(rcpath, rc_old_format, FALSE);
2417 /* now hack the file in order to have it to the new format */
2418 matcher_add_rulenames(rcpath);
2421 g_free(rc_old_format);
2423 f = g_fopen(rcpath, "rb");
2427 matcher_parser_start_parsing(f);
2428 fclose(matcher_parserin);
2431 /* previous version compatibility */
2433 /* g_print("reading filtering\n"); */
2434 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
2435 FILTERING_RC, NULL);
2436 f = g_fopen(rcpath, "rb");
2440 matcher_parser_start_parsing(f);
2441 fclose(matcher_parserin);
2444 /* g_print("reading scoring\n"); */
2445 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
2447 f = g_fopen(rcpath, "rb");
2451 matcher_parser_start_parsing(f);
2452 fclose(matcher_parserin);