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:
466 if (!prop->preg && (prop->error == 0)) {
467 prop->preg = g_new0(regex_t, 1);
468 /* if regexp then don't use the escaped string */
469 if (regcomp(prop->preg, down_expr,
470 REG_NOSUB | REG_EXTENDED
471 | ((prop->matchtype == MATCHTYPE_REGEXPCASE)
472 ? REG_ICASE : 0)) != 0) {
478 if (prop->preg == NULL) {
483 if (regexec(prop->preg, str1, 0, NULL, 0) == 0)
489 if (debug_filtering_session
490 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
491 gchar *stripped = g_strdup(str);
493 strretchomp(stripped);
495 log_print(LOG_DEBUG_FILTERING,
496 "%s value [ %s ] matches regular expression [ %s ] (%s)\n",
497 debug_context, stripped, prop->expr,
498 prop->matchtype == MATCHTYPE_REGEXP ? _("Case sensitive"):_("Case insensitive"));
500 log_print(LOG_DEBUG_FILTERING,
501 "%s value [ %s ] does NOT match regular expression [ %s ] (%s)\n",
502 debug_context, stripped, prop->expr,
503 prop->matchtype == MATCHTYPE_REGEXP ? _("Case sensitive"):_("Case insensitive"));
509 case MATCHTYPE_MATCHCASE:
510 case MATCHTYPE_MATCH:
511 ret = (strstr(str1, down_expr) != NULL);
514 if (debug_filtering_session
515 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
516 gchar *stripped = g_strdup(str);
518 strretchomp(stripped);
520 log_print(LOG_DEBUG_FILTERING,
521 "%s value [ %s ] contains [ %s ] (%s)\n",
522 debug_context, stripped, prop->expr,
523 prop->matchtype == MATCHTYPE_MATCH ? _("Case sensitive"):_("Case insensitive"));
525 log_print(LOG_DEBUG_FILTERING,
526 "%s value [ %s ] does NOT contain [ %s ] (%s)\n",
527 debug_context, stripped, prop->expr,
528 prop->matchtype == MATCHTYPE_MATCH ? _("Case sensitive"):_("Case insensitive"));
547 *\brief Find out if a tag matches a condition
549 *\param prop Matcher structure
550 *\param msginfo message to check
552 *\return gboolean TRUE if msginfo matches the condition in the
555 static gboolean matcherprop_tag_match(MatcherProp *prop, MsgInfo *msginfo,
556 const gchar *debug_context)
558 gboolean ret = FALSE;
561 if (msginfo == NULL || msginfo->tags == NULL)
564 for (cur = msginfo->tags; cur; cur = cur->next) {
565 const gchar *str = tags_get_tag(GPOINTER_TO_INT(cur->data));
568 if (matcherprop_string_match(prop, str, debug_context)) {
577 *\brief Find out if the string-ed list matches a condition
579 *\param prop Matcher structure
580 *\param list GSList of strings to check
582 *\return gboolean TRUE if str matches the condition in the
585 static gboolean matcherprop_list_match(MatcherProp *prop, const GSList *list,
586 const gchar *debug_context)
590 for(cur = list; cur != NULL; cur = cur->next) {
591 if (matcherprop_string_match(prop, (gchar *)cur->data, debug_context))
597 /* FIXME body search is a hack. */
598 static gboolean matcherprop_string_decode_match(MatcherProp *prop, const gchar *str,
599 const gchar *debug_context)
603 gboolean res = FALSE;
608 /* we try to decode QP first, because it's faster than base64 */
609 qp_decode_const(tmp, BUFFSIZE-1, str);
610 if (!g_utf8_validate(tmp, -1, NULL)) {
611 utf = conv_codeset_strdup
612 (tmp, conv_get_locale_charset_str_no_utf8(),
614 res = matcherprop_string_match(prop, utf, debug_context);
617 res = matcherprop_string_match(prop, tmp, debug_context);
620 if (res == FALSE && (strchr(prop->expr, '=') || strchr(prop->expr, '_')
621 || strchr(str, '=') || strchr(str, '_'))) {
622 /* if searching for something with an equal char, maybe
623 * we should try to match the non-decoded string.
624 * In case it was not qp-encoded. */
625 if (!g_utf8_validate(str, -1, NULL)) {
626 utf = conv_codeset_strdup
627 (str, conv_get_locale_charset_str_no_utf8(),
629 res = matcherprop_string_match(prop, utf, debug_context);
632 res = matcherprop_string_match(prop, str, debug_context);
636 /* FIXME base64 decoding is too slow, especially since text can
637 * easily be handled as base64. Don't even try now. */
643 typedef struct _thread_data {
650 static void *matcher_test_thread(void *data)
652 thread_data *td = (thread_data *)data;
655 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
656 pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
658 result = system(td->cmd);
659 td->done = TRUE; /* let the caller thread join() */
660 return GINT_TO_POINTER(result);
665 *\brief Execute a command defined in the matcher structure
667 *\param prop Pointer to matcher structure
668 *\param info Pointer to message info structure
670 *\return gboolean TRUE if command was executed succesfully
672 static gboolean matcherprop_match_test(const MatcherProp *prop,
681 thread_data *td = g_new0(thread_data, 1);
683 time_t start_time = time(NULL);
686 file = procmsg_get_message_file(info);
691 cmd = matching_build_command(prop->expr, info);
697 if (debug_filtering_session
698 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
699 log_print(LOG_DEBUG_FILTERING,
700 "starting threaded command [ %s ]\n",
706 if (pthread_attr_init(&pta) != 0 ||
707 pthread_attr_setdetachstate(&pta, PTHREAD_CREATE_JOINABLE) != 0 ||
708 pthread_create(&pt, &pta, matcher_test_thread, td) != 0)
709 retval = system(cmd);
711 debug_print("waiting for test thread\n");
713 /* don't let the interface freeze while waiting */
715 if (time(NULL) - start_time > 30) {
721 pthread_join(pt, &res);
722 retval = GPOINTER_TO_INT(res);
723 debug_print(" test thread returned %d\n", retval);
728 if (debug_filtering_session
729 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
730 log_print(LOG_DEBUG_FILTERING,
731 "starting synchronous command [ %s ]\n",
735 retval = system(cmd);
737 debug_print("Command exit code: %d\n", retval);
740 if (debug_filtering_session
741 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
742 log_print(LOG_DEBUG_FILTERING,
743 "command returned [ %d ]\n",
748 return (retval == 0);
752 *\brief Check if a message matches the condition in a matcher
755 *\param prop Pointer to matcher structure
756 *\param info Pointer to message info
758 *\return gboolean TRUE if a match
760 gboolean matcherprop_match(MatcherProp *prop,
765 switch(prop->criteria) {
766 case MATCHCRITERIA_ALL:
768 case MATCHCRITERIA_UNREAD:
769 return MSG_IS_UNREAD(info->flags);
770 case MATCHCRITERIA_NOT_UNREAD:
771 return !MSG_IS_UNREAD(info->flags);
772 case MATCHCRITERIA_NEW:
773 return MSG_IS_NEW(info->flags);
774 case MATCHCRITERIA_NOT_NEW:
775 return !MSG_IS_NEW(info->flags);
776 case MATCHCRITERIA_MARKED:
777 return MSG_IS_MARKED(info->flags);
778 case MATCHCRITERIA_NOT_MARKED:
779 return !MSG_IS_MARKED(info->flags);
780 case MATCHCRITERIA_DELETED:
781 return MSG_IS_DELETED(info->flags);
782 case MATCHCRITERIA_NOT_DELETED:
783 return !MSG_IS_DELETED(info->flags);
784 case MATCHCRITERIA_REPLIED:
785 return MSG_IS_REPLIED(info->flags);
786 case MATCHCRITERIA_NOT_REPLIED:
787 return !MSG_IS_REPLIED(info->flags);
788 case MATCHCRITERIA_FORWARDED:
789 return MSG_IS_FORWARDED(info->flags);
790 case MATCHCRITERIA_NOT_FORWARDED:
791 return !MSG_IS_FORWARDED(info->flags);
792 case MATCHCRITERIA_LOCKED:
793 return MSG_IS_LOCKED(info->flags);
794 case MATCHCRITERIA_NOT_LOCKED:
795 return !MSG_IS_LOCKED(info->flags);
796 case MATCHCRITERIA_SPAM:
797 return MSG_IS_SPAM(info->flags);
798 case MATCHCRITERIA_NOT_SPAM:
799 return !MSG_IS_SPAM(info->flags);
800 case MATCHCRITERIA_COLORLABEL:
802 gint color = MSG_GET_COLORLABEL_VALUE(info->flags);
803 gboolean ret = (color == prop->value);
806 if (debug_filtering_session
807 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
809 log_print(LOG_DEBUG_FILTERING,
810 "message color value [ %d ] matches color value [ %d ]\n",
813 log_print(LOG_DEBUG_FILTERING,
814 "message color value [ %d ] does NOT match color value [ %d ]\n",
820 case MATCHCRITERIA_NOT_COLORLABEL:
822 gint color = MSG_GET_COLORLABEL_VALUE(info->flags);
823 gboolean ret = (color != prop->value);
826 if (debug_filtering_session
827 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
829 log_print(LOG_DEBUG_FILTERING,
830 "message color value [ %d ] matches color value [ %d ]\n",
833 log_print(LOG_DEBUG_FILTERING,
834 "message color value [ %d ] does NOT match color value [ %d ]\n",
840 case MATCHCRITERIA_IGNORE_THREAD:
841 return MSG_IS_IGNORE_THREAD(info->flags);
842 case MATCHCRITERIA_NOT_IGNORE_THREAD:
843 return !MSG_IS_IGNORE_THREAD(info->flags);
844 case MATCHCRITERIA_WATCH_THREAD:
845 return MSG_IS_WATCH_THREAD(info->flags);
846 case MATCHCRITERIA_NOT_WATCH_THREAD:
847 return !MSG_IS_WATCH_THREAD(info->flags);
848 case MATCHCRITERIA_SUBJECT:
849 return matcherprop_string_match(prop, info->subject,
850 prefs_common_translated_header_name("Subject:"));
851 case MATCHCRITERIA_NOT_SUBJECT:
852 return !matcherprop_string_match(prop, info->subject,
853 prefs_common_translated_header_name("Subject:"));
854 case MATCHCRITERIA_FROM:
855 case MATCHCRITERIA_NOT_FROM:
860 context = g_strdup_printf(_("%s header"), prefs_common_translated_header_name("From:"));
861 ret = matcherprop_string_match(prop, info->from, context);
863 return (prop->criteria == MATCHCRITERIA_FROM)? ret : !ret;
865 case MATCHCRITERIA_TO:
866 case MATCHCRITERIA_NOT_TO:
871 context = g_strdup_printf(_("%s header"), prefs_common_translated_header_name("To:"));
872 ret = matcherprop_string_match(prop, info->to, context);
874 return (prop->criteria == MATCHCRITERIA_TO)? ret : !ret;
876 case MATCHCRITERIA_CC:
877 case MATCHCRITERIA_NOT_CC:
882 context = g_strdup_printf(_("%s header"), prefs_common_translated_header_name("Cc:"));
883 ret = matcherprop_string_match(prop, info->cc, context);
885 return (prop->criteria == MATCHCRITERIA_CC)? ret : !ret;
887 case MATCHCRITERIA_TO_OR_CC:
889 gchar *context1, *context2;
892 context1 = g_strdup_printf(_("%s header"), prefs_common_translated_header_name("To:"));
893 context2 = g_strdup_printf(_("%s header"), prefs_common_translated_header_name("Cc:"));
894 ret = matcherprop_string_match(prop, info->to, context1)
895 || matcherprop_string_match(prop, info->cc, context2);
900 case MATCHCRITERIA_NOT_TO_AND_NOT_CC:
902 gchar *context1, *context2;
905 context1 = g_strdup_printf(_("%s header"), prefs_common_translated_header_name("To:"));
906 context2 = g_strdup_printf(_("%s header"), prefs_common_translated_header_name("Cc:"));
907 ret = !(matcherprop_string_match(prop, info->to, context1)
908 || matcherprop_string_match(prop, info->cc, context2));
913 case MATCHCRITERIA_TAG:
914 case MATCHCRITERIA_NOT_TAG:
918 ret = matcherprop_tag_match(prop, info, _("Tag"));
919 return (prop->criteria == MATCHCRITERIA_TAG)? ret : !ret;
921 case MATCHCRITERIA_TAGGED:
922 case MATCHCRITERIA_NOT_TAGGED:
926 ret = (info->tags != NULL);
927 return (prop->criteria == MATCHCRITERIA_TAGGED)? ret : !ret;
929 case MATCHCRITERIA_AGE_GREATER:
935 age = ((t - info->date_t) / (60 * 60 * 24));
936 ret = (age > prop->value);
939 if (debug_filtering_session
940 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
942 log_print(LOG_DEBUG_FILTERING,
943 "message age [ %d ] is greater than [ %d ]\n",
946 log_print(LOG_DEBUG_FILTERING,
947 "message age [ %d ] is not greater than [ %d ]\n",
953 case MATCHCRITERIA_AGE_LOWER:
959 age = ((t - info->date_t) / (60 * 60 * 24));
960 ret = (age < prop->value);
963 if (debug_filtering_session
964 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
966 log_print(LOG_DEBUG_FILTERING,
967 "message age [ %d ] is lower than [ %d ]\n",
970 log_print(LOG_DEBUG_FILTERING,
971 "message age [ %d ] is not lower than [ %d ]\n",
977 case MATCHCRITERIA_SCORE_GREATER:
979 gboolean ret = (info->score > prop->value);
982 if (debug_filtering_session
983 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
985 log_print(LOG_DEBUG_FILTERING,
986 "message score [ %d ] is greater than [ %d ]\n",
987 info->score, prop->value);
989 log_print(LOG_DEBUG_FILTERING,
990 "message score [ %d ] is not greater than [ %d ]\n",
991 info->score, prop->value);
996 case MATCHCRITERIA_SCORE_LOWER:
998 gboolean ret = (info->score < prop->value);
1001 if (debug_filtering_session
1002 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
1004 log_print(LOG_DEBUG_FILTERING,
1005 "message score [ %d ] is lower than [ %d ]\n",
1006 info->score, prop->value);
1008 log_print(LOG_DEBUG_FILTERING,
1009 "message score [ %d ] is not lower than [ %d ]\n",
1010 info->score, prop->value);
1015 case MATCHCRITERIA_SCORE_EQUAL:
1017 gboolean ret = (info->score == prop->value);
1020 if (debug_filtering_session
1021 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
1023 log_print(LOG_DEBUG_FILTERING,
1024 "message score [ %d ] is equal to [ %d ]\n",
1025 info->score, prop->value);
1027 log_print(LOG_DEBUG_FILTERING,
1028 "message score [ %d ] is not equal to [ %d ]\n",
1029 info->score, prop->value);
1034 case MATCHCRITERIA_SIZE_GREATER:
1036 /* FIXME: info->size is a goffset */
1037 gboolean ret = (info->size > (goffset) prop->value);
1040 if (debug_filtering_session
1041 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
1043 log_print(LOG_DEBUG_FILTERING,
1044 "message size is greater than [ %d ]\n",
1047 log_print(LOG_DEBUG_FILTERING,
1048 "message size is not greater than [ %d ]\n",
1054 case MATCHCRITERIA_SIZE_SMALLER:
1056 /* FIXME: info->size is a goffset */
1057 gboolean ret = (info->size < (goffset) prop->value);
1060 if (debug_filtering_session
1061 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
1063 log_print(LOG_DEBUG_FILTERING,
1064 "message size is smaller than [ %d ]\n",
1067 log_print(LOG_DEBUG_FILTERING,
1068 "message size is not smaller than [ %d ]\n",
1074 case MATCHCRITERIA_SIZE_EQUAL:
1076 /* FIXME: info->size is a goffset */
1077 gboolean ret = (info->size == (goffset) prop->value);
1080 if (debug_filtering_session
1081 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
1083 log_print(LOG_DEBUG_FILTERING,
1084 "message size is equal to [ %d ]\n",
1087 log_print(LOG_DEBUG_FILTERING,
1088 "message size is not equal to [ %d ]\n",
1094 case MATCHCRITERIA_PARTIAL:
1096 /* FIXME: info->size is a goffset */
1097 gboolean ret = (info->total_size != 0 && info->size != (goffset)info->total_size);
1100 if (debug_filtering_session
1101 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
1103 log_print(LOG_DEBUG_FILTERING,
1104 "message is partially downloaded, size is less than total size [ %d ])\n",
1107 log_print(LOG_DEBUG_FILTERING,
1108 "message is not partially downloaded\n");
1113 case MATCHCRITERIA_NOT_PARTIAL:
1115 /* FIXME: info->size is a goffset */
1116 gboolean ret = (info->total_size == 0 || info->size == (goffset)info->total_size);
1119 if (debug_filtering_session
1120 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
1122 log_print(LOG_DEBUG_FILTERING,
1123 "message is not partially downloaded\n");
1125 log_print(LOG_DEBUG_FILTERING,
1126 "message is partially downloaded, size is less than total size [ %d ])\n",
1132 case MATCHCRITERIA_NEWSGROUPS:
1133 case MATCHCRITERIA_NOT_NEWSGROUPS:
1138 context = g_strdup_printf(_("%s header"),
1139 prefs_common_translated_header_name("Newsgroups:"));
1140 ret = matcherprop_string_match(prop, info->newsgroups, context);
1142 return (prop->criteria == MATCHCRITERIA_NEWSGROUPS)? ret : !ret;
1144 case MATCHCRITERIA_INREPLYTO:
1145 case MATCHCRITERIA_NOT_INREPLYTO:
1150 context = g_strdup_printf(_("%s header"),
1151 prefs_common_translated_header_name("In-Reply-To:"));
1152 ret = matcherprop_string_match(prop, info->inreplyto, context);
1154 return (prop->criteria == MATCHCRITERIA_INREPLYTO)? ret : !ret;
1156 case MATCHCRITERIA_REFERENCES:
1157 case MATCHCRITERIA_NOT_REFERENCES:
1162 context = g_strdup_printf(_("%s header"),
1163 prefs_common_translated_header_name("References:"));
1164 ret = matcherprop_list_match(prop, info->references, context);
1166 return (prop->criteria == MATCHCRITERIA_REFERENCES)? ret : !ret;
1168 case MATCHCRITERIA_TEST:
1169 return matcherprop_match_test(prop, info);
1170 case MATCHCRITERIA_NOT_TEST:
1171 return !matcherprop_match_test(prop, info);
1177 /* ********************* MatcherList *************************** */
1180 *\brief Create a new list of matchers
1182 *\param matchers List of matcher structures
1183 *\param bool_and Operator
1185 *\return MatcherList * New list
1187 MatcherList *matcherlist_new(GSList *matchers, gboolean bool_and)
1191 cond = g_new0(MatcherList, 1);
1193 cond->matchers = matchers;
1194 cond->bool_and = bool_and;
1200 *\brief Frees a list of matchers
1202 *\param cond List of matchers
1204 void matcherlist_free(MatcherList *cond)
1208 g_return_if_fail(cond);
1209 for (l = cond->matchers ; l != NULL ; l = g_slist_next(l)) {
1210 matcherprop_free((MatcherProp *) l->data);
1216 *\brief Skip all headers in a message file
1218 *\param fp Message file
1220 static void matcherlist_skip_headers(FILE *fp)
1222 gchar buf[BUFFSIZE];
1224 while (procheader_get_one_field(buf, sizeof(buf), fp, NULL) != -1)
1229 *\brief Check if a header matches a matcher condition
1231 *\param matcher Matcher structure to check header for
1232 *\param buf Header name
1234 *\return boolean TRUE if matching header
1236 static gboolean matcherprop_match_one_header(MatcherProp *matcher,
1239 gboolean result = FALSE;
1240 Header *header = NULL;
1242 switch (matcher->criteria) {
1243 case MATCHCRITERIA_HEADER:
1244 case MATCHCRITERIA_NOT_HEADER:
1245 header = procheader_parse_header(buf);
1248 if (procheader_headername_equal(header->name,
1250 if (matcher->criteria == MATCHCRITERIA_HEADER)
1251 result = matcherprop_string_match(matcher, header->body, _("header"));
1253 result = !matcherprop_string_match(matcher, header->body, _("header"));
1254 procheader_header_free(header);
1258 procheader_header_free(header);
1261 case MATCHCRITERIA_HEADERS_PART:
1262 return matcherprop_string_match(matcher, buf, _("header line"));
1263 case MATCHCRITERIA_NOT_HEADERS_PART:
1264 return !matcherprop_string_match(matcher, buf, _("headers line"));
1265 case MATCHCRITERIA_MESSAGE:
1266 return matcherprop_string_decode_match(matcher, buf, _("message line"));
1267 case MATCHCRITERIA_NOT_MESSAGE:
1268 return !matcherprop_string_decode_match(matcher, buf, _("message line"));
1269 case MATCHCRITERIA_FOUND_IN_ADDRESSBOOK:
1270 case MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK:
1272 GSList *address_list = NULL;
1273 gint match = MATCH_ONE;
1274 gboolean found = FALSE;
1276 /* how many address headers are me trying to mach? */
1277 if (strcasecmp(matcher->header, "Any") == 0)
1279 else if (strcasecmp(matcher->header, "All") == 0)
1282 if (match == MATCH_ONE) {
1283 /* matching one address header exactly, is that the right one? */
1284 header = procheader_parse_header(buf);
1286 !procheader_headername_equal(header->name, matcher->header))
1288 address_list = address_list_append(address_list, header->body);
1289 if (address_list == NULL)
1293 header = procheader_parse_header(buf);
1296 /* address header is one of the headers we have to match when checking
1297 for any address header or all address headers? */
1298 if (procheader_headername_equal(header->name, "From") ||
1299 procheader_headername_equal(header->name, "To") ||
1300 procheader_headername_equal(header->name, "Cc") ||
1301 procheader_headername_equal(header->name, "Reply-To") ||
1302 procheader_headername_equal(header->name, "Sender"))
1303 address_list = address_list_append(address_list, header->body);
1304 if (address_list == NULL)
1308 found = match_with_addresses_in_addressbook
1309 (matcher, address_list, matcher->criteria,
1310 matcher->expr, match);
1311 g_slist_free(address_list);
1313 if (matcher->criteria == MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK)
1324 *\brief Check if the matcher structure wants headers to
1327 *\param matcher Matcher structure
1329 *\return gboolean TRUE if the matcher structure describes
1330 * a header match condition
1332 static gboolean matcherprop_criteria_headers(const MatcherProp *matcher)
1334 switch (matcher->criteria) {
1335 case MATCHCRITERIA_HEADER:
1336 case MATCHCRITERIA_NOT_HEADER:
1337 case MATCHCRITERIA_HEADERS_PART:
1338 case MATCHCRITERIA_NOT_HEADERS_PART:
1339 case MATCHCRITERIA_FOUND_IN_ADDRESSBOOK:
1340 case MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK:
1348 *\brief Check if the matcher structure wants the message
1349 * to be matched (just perform an action on any
1352 *\param matcher Matcher structure
1354 *\return gboolean TRUE if matcher condition should match
1357 static gboolean matcherprop_criteria_message(MatcherProp *matcher)
1359 switch (matcher->criteria) {
1360 case MATCHCRITERIA_MESSAGE:
1361 case MATCHCRITERIA_NOT_MESSAGE:
1369 *\brief Check if a list of conditions matches one header in
1372 *\param matchers List of conditions
1373 *\param fp Message file
1375 *\return gboolean TRUE if one of the headers is matched by
1376 * the list of conditions.
1378 static gboolean matcherlist_match_headers(MatcherList *matchers, FILE *fp)
1381 gchar buf[BUFFSIZE];
1383 while (procheader_get_one_field(buf, sizeof(buf), fp, NULL) != -1) {
1384 for (l = matchers->matchers ; l != NULL ; l = g_slist_next(l)) {
1385 MatcherProp *matcher = (MatcherProp *) l->data;
1386 gint match = MATCH_ANY;
1391 /* determine the match range (all, any are our concern here) */
1392 if (matcher->criteria == MATCHCRITERIA_NOT_HEADERS_PART ||
1393 matcher->criteria == MATCHCRITERIA_NOT_MESSAGE) {
1396 } else if (matcher->criteria == MATCHCRITERIA_FOUND_IN_ADDRESSBOOK ||
1397 matcher->criteria == MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK) {
1398 Header *header = NULL;
1400 /* address header is one of the headers we have to match when checking
1401 for any address header or all address headers? */
1402 header = procheader_parse_header(buf);
1404 (procheader_headername_equal(header->name, "From") ||
1405 procheader_headername_equal(header->name, "To") ||
1406 procheader_headername_equal(header->name, "Cc") ||
1407 procheader_headername_equal(header->name, "Reply-To") ||
1408 procheader_headername_equal(header->name, "Sender"))) {
1410 if (strcasecmp(matcher->header, "Any") == 0)
1412 else if (strcasecmp(matcher->header, "All") == 0)
1417 /* further call to matcherprop_match_one_header() can't match
1418 and it irrelevant, so: don't alter the match result */
1423 /* ZERO line must NOT match for the rule to match.
1425 if (match == MATCH_ALL) {
1426 if (matcherprop_match_one_header(matcher, buf)) {
1427 matcher->result = TRUE;
1429 matcher->result = FALSE;
1430 matcher->done = TRUE;
1432 /* else, just one line matching is enough for the rule to match
1434 } else if (matcherprop_criteria_headers(matcher) ||
1435 matcherprop_criteria_message(matcher)) {
1436 if (matcherprop_match_one_header(matcher, buf)) {
1437 matcher->result = TRUE;
1438 matcher->done = TRUE;
1442 /* if the rule matched and the matchers are OR, no need to
1443 * check the others */
1444 if (matcher->result && matcher->done) {
1445 if (!matchers->bool_and)
1455 *\brief Check if a matcher wants to check the message body
1457 *\param matcher Matcher structure
1459 *\return gboolean TRUE if body must be matched.
1461 static gboolean matcherprop_criteria_body(const MatcherProp *matcher)
1463 switch (matcher->criteria) {
1464 case MATCHCRITERIA_BODY_PART:
1465 case MATCHCRITERIA_NOT_BODY_PART:
1473 *\brief Check if a (line) string matches the criteria
1474 * described by a matcher structure
1476 *\param matcher Matcher structure
1479 *\return gboolean TRUE if string matches criteria
1481 static gboolean matcherprop_match_line(MatcherProp *matcher, const gchar *line)
1483 switch (matcher->criteria) {
1484 case MATCHCRITERIA_BODY_PART:
1485 case MATCHCRITERIA_MESSAGE:
1486 return matcherprop_string_decode_match(matcher, line, _("body line"));
1487 case MATCHCRITERIA_NOT_BODY_PART:
1488 case MATCHCRITERIA_NOT_MESSAGE:
1489 return !matcherprop_string_decode_match(matcher, line, _("body line"));
1495 *\brief Check if a line in a message file's body matches
1498 *\param matchers List of conditions
1499 *\param fp Message file
1501 *\return gboolean TRUE if succesful match
1503 static gboolean matcherlist_match_body(MatcherList *matchers, FILE *fp)
1506 gchar buf[BUFFSIZE];
1508 while (fgets(buf, sizeof(buf), fp) != NULL) {
1509 for (l = matchers->matchers ; l != NULL ; l = g_slist_next(l)) {
1510 MatcherProp *matcher = (MatcherProp *) l->data;
1515 /* if the criteria is ~body_part or ~message, ZERO lines
1516 * must NOT match for the rule to match. */
1517 if (matcher->criteria == MATCHCRITERIA_NOT_BODY_PART ||
1518 matcher->criteria == MATCHCRITERIA_NOT_MESSAGE) {
1519 if (matcherprop_match_line(matcher, buf)) {
1520 matcher->result = TRUE;
1522 matcher->result = FALSE;
1523 matcher->done = TRUE;
1525 /* else, just one line has to match */
1526 } else if (matcherprop_criteria_body(matcher) ||
1527 matcherprop_criteria_message(matcher)) {
1528 if (matcherprop_match_line(matcher, buf)) {
1529 matcher->result = TRUE;
1530 matcher->done = TRUE;
1534 /* if the matchers are OR'ed and the rule matched,
1535 * no need to check the others. */
1536 if (matcher->result && matcher->done) {
1537 if (!matchers->bool_and)
1546 *\brief Check if a message file matches criteria
1548 *\param matchers Criteria
1549 *\param info Message info
1550 *\param result Default result
1552 *\return gboolean TRUE if matched
1554 static gboolean matcherlist_match_file(MatcherList *matchers, MsgInfo *info,
1557 gboolean read_headers;
1563 /* file need to be read ? */
1565 read_headers = FALSE;
1567 for (l = matchers->matchers ; l != NULL ; l = g_slist_next(l)) {
1568 MatcherProp *matcher = (MatcherProp *) l->data;
1570 if (matcherprop_criteria_headers(matcher))
1571 read_headers = TRUE;
1572 if (matcherprop_criteria_body(matcher))
1574 if (matcherprop_criteria_message(matcher)) {
1575 read_headers = TRUE;
1578 matcher->result = FALSE;
1579 matcher->done = FALSE;
1582 if (!read_headers && !read_body)
1585 file = procmsg_get_message_file_full(info, read_headers, read_body);
1589 if ((fp = g_fopen(file, "rb")) == NULL) {
1590 FILE_OP_ERROR(file, "fopen");
1595 /* read the headers */
1598 if (matcherlist_match_headers(matchers, fp))
1601 matcherlist_skip_headers(fp);
1606 matcherlist_match_body(matchers, fp);
1609 for (l = matchers->matchers; l != NULL; l = g_slist_next(l)) {
1610 MatcherProp *matcher = (MatcherProp *) l->data;
1612 if (matcherprop_criteria_headers(matcher) ||
1613 matcherprop_criteria_body(matcher) ||
1614 matcherprop_criteria_message(matcher)) {
1615 if (matcher->result) {
1616 if (!matchers->bool_and) {
1622 if (matchers->bool_and) {
1638 *\brief Test list of conditions on a message.
1640 *\param matchers List of conditions
1641 *\param info Message info
1643 *\return gboolean TRUE if matched
1645 gboolean matcherlist_match(MatcherList *matchers, MsgInfo *info)
1653 if (matchers->bool_and)
1658 /* test the cached elements */
1660 for (l = matchers->matchers; l != NULL ;l = g_slist_next(l)) {
1661 MatcherProp *matcher = (MatcherProp *) l->data;
1663 if (debug_filtering_session) {
1664 gchar *buf = matcherprop_to_string(matcher);
1665 log_print(LOG_DEBUG_FILTERING, _("checking if message matches [ %s ]\n"), buf);
1669 switch(matcher->criteria) {
1670 case MATCHCRITERIA_ALL:
1671 case MATCHCRITERIA_UNREAD:
1672 case MATCHCRITERIA_NOT_UNREAD:
1673 case MATCHCRITERIA_NEW:
1674 case MATCHCRITERIA_NOT_NEW:
1675 case MATCHCRITERIA_MARKED:
1676 case MATCHCRITERIA_NOT_MARKED:
1677 case MATCHCRITERIA_DELETED:
1678 case MATCHCRITERIA_NOT_DELETED:
1679 case MATCHCRITERIA_REPLIED:
1680 case MATCHCRITERIA_NOT_REPLIED:
1681 case MATCHCRITERIA_FORWARDED:
1682 case MATCHCRITERIA_NOT_FORWARDED:
1683 case MATCHCRITERIA_LOCKED:
1684 case MATCHCRITERIA_NOT_LOCKED:
1685 case MATCHCRITERIA_SPAM:
1686 case MATCHCRITERIA_NOT_SPAM:
1687 case MATCHCRITERIA_COLORLABEL:
1688 case MATCHCRITERIA_NOT_COLORLABEL:
1689 case MATCHCRITERIA_IGNORE_THREAD:
1690 case MATCHCRITERIA_NOT_IGNORE_THREAD:
1691 case MATCHCRITERIA_WATCH_THREAD:
1692 case MATCHCRITERIA_NOT_WATCH_THREAD:
1693 case MATCHCRITERIA_SUBJECT:
1694 case MATCHCRITERIA_NOT_SUBJECT:
1695 case MATCHCRITERIA_FROM:
1696 case MATCHCRITERIA_NOT_FROM:
1697 case MATCHCRITERIA_TO:
1698 case MATCHCRITERIA_NOT_TO:
1699 case MATCHCRITERIA_CC:
1700 case MATCHCRITERIA_NOT_CC:
1701 case MATCHCRITERIA_TO_OR_CC:
1702 case MATCHCRITERIA_NOT_TO_AND_NOT_CC:
1703 case MATCHCRITERIA_TAG:
1704 case MATCHCRITERIA_NOT_TAG:
1705 case MATCHCRITERIA_TAGGED:
1706 case MATCHCRITERIA_NOT_TAGGED:
1707 case MATCHCRITERIA_AGE_GREATER:
1708 case MATCHCRITERIA_AGE_LOWER:
1709 case MATCHCRITERIA_NEWSGROUPS:
1710 case MATCHCRITERIA_NOT_NEWSGROUPS:
1711 case MATCHCRITERIA_INREPLYTO:
1712 case MATCHCRITERIA_NOT_INREPLYTO:
1713 case MATCHCRITERIA_REFERENCES:
1714 case MATCHCRITERIA_NOT_REFERENCES:
1715 case MATCHCRITERIA_SCORE_GREATER:
1716 case MATCHCRITERIA_SCORE_LOWER:
1717 case MATCHCRITERIA_SCORE_EQUAL:
1718 case MATCHCRITERIA_SIZE_GREATER:
1719 case MATCHCRITERIA_SIZE_SMALLER:
1720 case MATCHCRITERIA_SIZE_EQUAL:
1721 case MATCHCRITERIA_TEST:
1722 case MATCHCRITERIA_NOT_TEST:
1723 case MATCHCRITERIA_PARTIAL:
1724 case MATCHCRITERIA_NOT_PARTIAL:
1725 if (matcherprop_match(matcher, info)) {
1726 if (!matchers->bool_and) {
1727 if (debug_filtering_session)
1728 log_status_ok(LOG_DEBUG_FILTERING, _("message matches\n"));
1733 if (matchers->bool_and) {
1734 if (debug_filtering_session)
1735 log_status_nok(LOG_DEBUG_FILTERING, _("message does not match\n"));
1742 /* test the condition on the file */
1744 if (matcherlist_match_file(matchers, info, result)) {
1745 if (!matchers->bool_and) {
1746 if (debug_filtering_session)
1747 log_status_ok(LOG_DEBUG_FILTERING, _("message matches\n"));
1751 if (matchers->bool_and) {
1752 if (debug_filtering_session)
1753 log_status_nok(LOG_DEBUG_FILTERING, _("message does not match\n"));
1758 if (debug_filtering_session) {
1760 log_status_ok(LOG_DEBUG_FILTERING, _("message matches\n"));
1762 log_status_nok(LOG_DEBUG_FILTERING, _("message does not match\n"));
1768 static gint quote_filter_str(gchar * result, guint size,
1778 for(p = path ; * p != '\0' ; p ++) {
1780 if ((* p != '\"') && (* p != '\\')) {
1781 if (remaining > 0) {
1787 result[size - 1] = '\0';
1792 if (remaining >= 2) {
1800 result[size - 1] = '\0';
1805 if (remaining > 0) {
1809 result[size - 1] = '\0';
1817 gchar * matcher_quote_str(const gchar * src)
1822 len = strlen(src) * 2 + 1;
1823 res = g_malloc(len);
1824 quote_filter_str(res, len, src);
1830 *\brief Convert a matcher structure to a string
1832 *\param matcher Matcher structure
1834 *\return gchar * Newly allocated string
1836 gchar *matcherprop_to_string(MatcherProp *matcher)
1838 gchar *matcher_str = NULL;
1839 const gchar *criteria_str;
1840 const gchar *matchtype_str;
1842 gchar * quoted_expr;
1843 gchar * quoted_header;
1845 criteria_str = NULL;
1846 for (i = 0; i < (int) (sizeof(matchparser_tab) / sizeof(MatchParser)); i++) {
1847 if (matchparser_tab[i].id == matcher->criteria)
1848 criteria_str = matchparser_tab[i].str;
1850 if (criteria_str == NULL)
1853 switch (matcher->criteria) {
1854 case MATCHCRITERIA_AGE_GREATER:
1855 case MATCHCRITERIA_AGE_LOWER:
1856 case MATCHCRITERIA_SCORE_GREATER:
1857 case MATCHCRITERIA_SCORE_LOWER:
1858 case MATCHCRITERIA_SCORE_EQUAL:
1859 case MATCHCRITERIA_SIZE_GREATER:
1860 case MATCHCRITERIA_SIZE_SMALLER:
1861 case MATCHCRITERIA_SIZE_EQUAL:
1862 case MATCHCRITERIA_COLORLABEL:
1863 case MATCHCRITERIA_NOT_COLORLABEL:
1864 return g_strdup_printf("%s %i", criteria_str, matcher->value);
1865 case MATCHCRITERIA_ALL:
1866 case MATCHCRITERIA_UNREAD:
1867 case MATCHCRITERIA_NOT_UNREAD:
1868 case MATCHCRITERIA_NEW:
1869 case MATCHCRITERIA_NOT_NEW:
1870 case MATCHCRITERIA_MARKED:
1871 case MATCHCRITERIA_NOT_MARKED:
1872 case MATCHCRITERIA_DELETED:
1873 case MATCHCRITERIA_NOT_DELETED:
1874 case MATCHCRITERIA_REPLIED:
1875 case MATCHCRITERIA_NOT_REPLIED:
1876 case MATCHCRITERIA_FORWARDED:
1877 case MATCHCRITERIA_NOT_FORWARDED:
1878 case MATCHCRITERIA_LOCKED:
1879 case MATCHCRITERIA_NOT_LOCKED:
1880 case MATCHCRITERIA_SPAM:
1881 case MATCHCRITERIA_NOT_SPAM:
1882 case MATCHCRITERIA_PARTIAL:
1883 case MATCHCRITERIA_NOT_PARTIAL:
1884 case MATCHCRITERIA_IGNORE_THREAD:
1885 case MATCHCRITERIA_NOT_IGNORE_THREAD:
1886 case MATCHCRITERIA_WATCH_THREAD:
1887 case MATCHCRITERIA_NOT_WATCH_THREAD:
1888 case MATCHCRITERIA_TAGGED:
1889 case MATCHCRITERIA_NOT_TAGGED:
1890 return g_strdup(criteria_str);
1891 case MATCHCRITERIA_TEST:
1892 case MATCHCRITERIA_NOT_TEST:
1893 quoted_expr = matcher_quote_str(matcher->expr);
1894 matcher_str = g_strdup_printf("%s \"%s\"",
1895 criteria_str, quoted_expr);
1896 g_free(quoted_expr);
1898 case MATCHCRITERIA_FOUND_IN_ADDRESSBOOK:
1899 case MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK:
1900 quoted_header = matcher_quote_str(matcher->header);
1901 quoted_expr = matcher_quote_str(matcher->expr);
1902 matcher_str = g_strdup_printf("%s \"%s\" in \"%s\"",
1903 criteria_str, quoted_header, quoted_expr);
1904 g_free(quoted_header);
1905 g_free(quoted_expr);
1909 matchtype_str = NULL;
1910 for (i = 0; i < sizeof matchparser_tab / sizeof matchparser_tab[0]; i++) {
1911 if (matchparser_tab[i].id == matcher->matchtype)
1912 matchtype_str = matchparser_tab[i].str;
1915 if (matchtype_str == NULL)
1918 switch (matcher->matchtype) {
1919 case MATCHTYPE_MATCH:
1920 case MATCHTYPE_MATCHCASE:
1921 case MATCHTYPE_REGEXP:
1922 case MATCHTYPE_REGEXPCASE:
1923 quoted_expr = matcher_quote_str(matcher->expr);
1924 if (matcher->header) {
1925 quoted_header = matcher_quote_str(matcher->header);
1926 matcher_str = g_strdup_printf
1927 ("%s \"%s\" %s \"%s\"",
1928 criteria_str, quoted_header,
1929 matchtype_str, quoted_expr);
1930 g_free(quoted_header);
1933 matcher_str = g_strdup_printf
1934 ("%s %s \"%s\"", criteria_str,
1935 matchtype_str, quoted_expr);
1936 g_free(quoted_expr);
1944 *\brief Convert a list of conditions to a string
1946 *\param matchers List of conditions
1948 *\return gchar * Newly allocated string
1950 gchar *matcherlist_to_string(const MatcherList *matchers)
1956 gchar *result = NULL;
1958 count = g_slist_length(matchers->matchers);
1959 vstr = g_new(gchar *, count + 1);
1961 for (l = matchers->matchers, cur_str = vstr; l != NULL;
1962 l = g_slist_next(l), cur_str ++) {
1963 *cur_str = matcherprop_to_string((MatcherProp *) l->data);
1964 if (*cur_str == NULL)
1969 if (matchers->bool_and)
1970 result = g_strjoinv(" & ", vstr);
1972 result = g_strjoinv(" | ", vstr);
1974 for (cur_str = vstr ; *cur_str != NULL ; cur_str ++)
1982 #define STRLEN_ZERO(s) ((s) ? strlen(s) : 0)
1983 #define STRLEN_DEFAULT(s,d) ((s) ? strlen(s) : STRLEN_ZERO(d))
1985 static void add_str_default(gchar ** dest,
1986 const gchar * s, const gchar * d)
1988 gchar quoted_str[4096];
1996 quote_cmd_argument(quoted_str, sizeof(quoted_str), str);
1997 strcpy(* dest, quoted_str);
1999 (* dest) += strlen(* dest);
2002 /* matching_build_command() - preferably cmd should be unescaped */
2004 *\brief Build the command line to execute
2006 *\param cmd String with command line specifiers
2007 *\param info Message info to use for command
2009 *\return gchar * Newly allocated string
2011 gchar *matching_build_command(const gchar *cmd, MsgInfo *info)
2013 const gchar *s = cmd;
2014 gchar *filename = NULL;
2015 gchar *processed_cmd;
2019 const gchar *const no_subject = _("(none)") ;
2020 const gchar *const no_from = _("(none)") ;
2021 const gchar *const no_to = _("(none)") ;
2022 const gchar *const no_cc = _("(none)") ;
2023 const gchar *const no_date = _("(none)") ;
2024 const gchar *const no_msgid = _("(none)") ;
2025 const gchar *const no_newsgroups = _("(none)") ;
2026 const gchar *const no_references = _("(none)") ;
2028 size = STRLEN_ZERO(cmd) + 1;
2029 while (*s != '\0') {
2036 case 's': /* subject */
2037 size += STRLEN_DEFAULT(info->subject, no_subject) - 2;
2039 case 'f': /* from */
2040 size += STRLEN_DEFAULT(info->from, no_from) - 2;
2043 size += STRLEN_DEFAULT(info->to, no_to) - 2;
2046 size += STRLEN_DEFAULT(info->cc, no_cc) - 2;
2048 case 'd': /* date */
2049 size += STRLEN_DEFAULT(info->date, no_date) - 2;
2051 case 'i': /* message-id */
2052 size += STRLEN_DEFAULT(info->msgid, no_msgid) - 2;
2054 case 'n': /* newsgroups */
2055 size += STRLEN_DEFAULT(info->newsgroups, no_newsgroups) - 2;
2057 case 'r': /* references */
2058 /* FIXME: using the inreplyto header for reference */
2059 size += STRLEN_DEFAULT(info->inreplyto, no_references) - 2;
2061 case 'F': /* file */
2062 if (filename == NULL)
2063 filename = folder_item_fetch_msg(info->folder, info->msgnum);
2065 if (filename == NULL) {
2066 g_warning("filename is not set");
2070 size += strlen(filename) - 2;
2079 /* as the string can be quoted, we double the result */
2082 processed_cmd = g_new0(gchar, size);
2086 while (*s != '\0') {
2094 case 's': /* subject */
2095 add_str_default(&p, info->subject,
2098 case 'f': /* from */
2099 add_str_default(&p, info->from,
2103 add_str_default(&p, info->to,
2107 add_str_default(&p, info->cc,
2110 case 'd': /* date */
2111 add_str_default(&p, info->date,
2114 case 'i': /* message-id */
2115 add_str_default(&p, info->msgid,
2118 case 'n': /* newsgroups */
2119 add_str_default(&p, info->newsgroups,
2122 case 'r': /* references */
2123 /* FIXME: using the inreplyto header for references */
2124 add_str_default(&p, info->inreplyto, no_references);
2126 case 'F': /* file */
2127 if (filename != NULL)
2128 add_str_default(&p, filename, NULL);
2147 return processed_cmd;
2149 #undef STRLEN_DEFAULT
2152 /* ************************************************************ */
2156 *\brief Write filtering list to file
2159 *\param prefs_filtering List of filtering conditions
2161 static int prefs_filtering_write(FILE *fp, GSList *prefs_filtering)
2165 for (cur = prefs_filtering; cur != NULL; cur = cur->next) {
2166 gchar *filtering_str = NULL;
2167 gchar *tmp_name = NULL;
2168 FilteringProp *prop = NULL;
2170 if (NULL == (prop = (FilteringProp *) cur->data))
2173 if (NULL == (filtering_str = filteringprop_to_string(prop)))
2176 if (prop->enabled) {
2177 if (fputs("enabled ", fp) == EOF) {
2178 FILE_OP_ERROR("filtering config", "fputs");
2182 if (fputs("disabled ", fp) == EOF) {
2183 FILE_OP_ERROR("filtering config", "fputs");
2188 if (fputs("rulename \"", fp) == EOF) {
2189 FILE_OP_ERROR("filtering config", "fputs");
2190 g_free(filtering_str);
2193 tmp_name = prop->name;
2194 while (tmp_name && *tmp_name != '\0') {
2195 if (*tmp_name != '"') {
2196 if (fputc(*tmp_name, fp) == EOF) {
2197 FILE_OP_ERROR("filtering config", "fputs || fputc");
2198 g_free(filtering_str);
2201 } else if (*tmp_name == '"') {
2202 if (fputc('\\', fp) == EOF ||
2203 fputc('"', fp) == EOF) {
2204 FILE_OP_ERROR("filtering config", "fputs || fputc");
2205 g_free(filtering_str);
2211 if (fputs("\" ", fp) == EOF) {
2212 FILE_OP_ERROR("filtering config", "fputs");
2213 g_free(filtering_str);
2217 if (prop->account_id != 0) {
2220 tmp = g_strdup_printf("account %d ", prop->account_id);
2221 if (fputs(tmp, fp) == EOF) {
2222 FILE_OP_ERROR("filtering config", "fputs");
2229 if(fputs(filtering_str, fp) == EOF ||
2230 fputc('\n', fp) == EOF) {
2231 FILE_OP_ERROR("filtering config", "fputs || fputc");
2232 g_free(filtering_str);
2235 g_free(filtering_str);
2241 typedef struct _NodeLoopData {
2247 *\brief Write matchers from a folder item
2249 *\param node Node with folder info
2250 *\param data File pointer
2252 *\return gboolean FALSE
2254 static gboolean prefs_matcher_write_func(GNode *node, gpointer d)
2257 NodeLoopData *data = (NodeLoopData *)d;
2259 GSList *prefs_filtering;
2262 /* prevent warning */
2263 if (item->path == NULL)
2265 id = folder_item_get_identifier(item);
2268 prefs_filtering = item->prefs->processing;
2270 if (prefs_filtering != NULL) {
2271 if (fprintf(data->fp, "[%s]\n", id) < 0) {
2275 if (prefs_filtering_write(data->fp, prefs_filtering) < 0) {
2279 if (fputc('\n', data->fp) == EOF) {
2291 *\brief Save matchers from folder items
2295 static int prefs_matcher_save(FILE *fp)
2303 for (cur = folder_get_list() ; cur != NULL ; cur = g_list_next(cur)) {
2306 folder = (Folder *) cur->data;
2307 g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
2308 prefs_matcher_write_func, &data);
2311 if (data.error == TRUE)
2314 /* pre global rules */
2315 if (fprintf(fp, "[preglobal]\n") < 0 ||
2316 prefs_filtering_write(fp, pre_global_processing) < 0 ||
2317 fputc('\n', fp) == EOF)
2320 /* post global rules */
2321 if (fprintf(fp, "[postglobal]\n") < 0 ||
2322 prefs_filtering_write(fp, post_global_processing) < 0 ||
2323 fputc('\n', fp) == EOF)
2326 /* filtering rules */
2327 if (fprintf(fp, "[filtering]\n") < 0 ||
2328 prefs_filtering_write(fp, filtering_rules) < 0 ||
2329 fputc('\n', fp) == EOF)
2336 *\brief Write filtering / matcher configuration file
2338 void prefs_matcher_write_config(void)
2343 debug_print("Writing matcher configuration...\n");
2345 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
2348 if ((pfile = prefs_write_open(rcpath)) == NULL) {
2349 g_warning("failed to write configuration to file\n");
2356 if (prefs_matcher_save(pfile->fp) < 0) {
2357 g_warning("failed to write configuration to file\n");
2358 prefs_file_close_revert(pfile);
2359 } else if (prefs_file_close(pfile) < 0) {
2360 g_warning("failed to save configuration to file\n");
2364 /* ******************************************************************* */
2366 static void matcher_add_rulenames(const gchar *rcpath)
2368 gchar *newpath = g_strconcat(rcpath, ".new", NULL);
2369 FILE *src = g_fopen(rcpath, "rb");
2370 FILE *dst = g_fopen(newpath, "wb");
2371 gchar buf[BUFFSIZE];
2379 while (fgets (buf, sizeof(buf), src) != NULL) {
2380 if (strlen(buf) > 2 && buf[0] != '['
2381 && strncmp(buf, "rulename \"", 10)
2382 && strncmp(buf, "enabled rulename \"", 18)
2383 && strncmp(buf, "disabled rulename \"", 18)) {
2384 r = fwrite("enabled rulename \"\" ",
2385 strlen("enabled rulename \"\" "), 1, dst);
2387 r = fwrite(buf, strlen(buf), 1, dst);
2391 move_file(newpath, rcpath, TRUE);
2396 *\brief Read matcher configuration
2398 void prefs_matcher_read_config(void)
2401 gchar *rc_old_format;
2404 create_matchparser_hashtab();
2405 prefs_filtering_clear();
2407 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MATCHER_RC, NULL);
2408 rc_old_format = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MATCHER_RC,
2409 ".pre_names", NULL);
2411 if (!is_file_exist(rc_old_format) && is_file_exist(rcpath)) {
2412 /* backup file with no rules names, in case
2413 * anything goes wrong */
2414 copy_file(rcpath, rc_old_format, FALSE);
2415 /* now hack the file in order to have it to the new format */
2416 matcher_add_rulenames(rcpath);
2419 g_free(rc_old_format);
2421 f = g_fopen(rcpath, "rb");
2425 matcher_parser_start_parsing(f);
2426 fclose(matcher_parserin);
2429 /* previous version compatibility */
2431 /* g_print("reading filtering\n"); */
2432 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
2433 FILTERING_RC, NULL);
2434 f = g_fopen(rcpath, "rb");
2438 matcher_parser_start_parsing(f);
2439 fclose(matcher_parserin);
2442 /* g_print("reading scoring\n"); */
2443 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
2445 f = g_fopen(rcpath, "rb");
2449 matcher_parser_start_parsing(f);
2450 fclose(matcher_parserin);