2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 2002-2004 by the Claws Mail Team and Hiroyuki Yamamoto
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #include <glib/gi18n.h>
37 #include "procheader.h"
39 #include "matcher_parser.h"
40 #include "prefs_gtk.h"
41 #include "addr_compl.h"
43 #include "quoted-printable.h"
46 #include "prefs_common.h"
51 *\brief Keyword lookup element
54 gint id; /*!< keyword id */
55 gchar *str; /*!< keyword */
57 typedef struct _MatchParser MatchParser;
60 *\brief Table with strings and ids used by the lexer and
61 * the parser. New keywords can be added here.
63 static const MatchParser matchparser_tab[] = {
65 {MATCHCRITERIA_ALL, "all"},
66 {MATCHCRITERIA_UNREAD, "unread"},
67 {MATCHCRITERIA_NOT_UNREAD, "~unread"},
68 {MATCHCRITERIA_NEW, "new"},
69 {MATCHCRITERIA_NOT_NEW, "~new"},
70 {MATCHCRITERIA_MARKED, "marked"},
71 {MATCHCRITERIA_NOT_MARKED, "~marked"},
72 {MATCHCRITERIA_DELETED, "deleted"},
73 {MATCHCRITERIA_NOT_DELETED, "~deleted"},
74 {MATCHCRITERIA_REPLIED, "replied"},
75 {MATCHCRITERIA_NOT_REPLIED, "~replied"},
76 {MATCHCRITERIA_FORWARDED, "forwarded"},
77 {MATCHCRITERIA_NOT_FORWARDED, "~forwarded"},
78 {MATCHCRITERIA_LOCKED, "locked"},
79 {MATCHCRITERIA_NOT_LOCKED, "~locked"},
80 {MATCHCRITERIA_COLORLABEL, "colorlabel"},
81 {MATCHCRITERIA_NOT_COLORLABEL, "~colorlabel"},
82 {MATCHCRITERIA_IGNORE_THREAD, "ignore_thread"},
83 {MATCHCRITERIA_NOT_IGNORE_THREAD, "~ignore_thread"},
84 {MATCHCRITERIA_WATCH_THREAD, "watch_thread"},
85 {MATCHCRITERIA_NOT_WATCH_THREAD, "~watch_thread"},
86 {MATCHCRITERIA_SPAM, "spam"},
87 {MATCHCRITERIA_NOT_SPAM, "~spam"},
90 {MATCHCRITERIA_SUBJECT, "subject"},
91 {MATCHCRITERIA_NOT_SUBJECT, "~subject"},
92 {MATCHCRITERIA_FROM, "from"},
93 {MATCHCRITERIA_NOT_FROM, "~from"},
94 {MATCHCRITERIA_TO, "to"},
95 {MATCHCRITERIA_NOT_TO, "~to"},
96 {MATCHCRITERIA_CC, "cc"},
97 {MATCHCRITERIA_NOT_CC, "~cc"},
98 {MATCHCRITERIA_TO_OR_CC, "to_or_cc"},
99 {MATCHCRITERIA_NOT_TO_AND_NOT_CC, "~to_or_cc"},
100 {MATCHCRITERIA_TAG, "tag"},
101 {MATCHCRITERIA_NOT_TAG, "~tag"},
102 {MATCHCRITERIA_TAGGED, "tagged"},
103 {MATCHCRITERIA_NOT_TAGGED, "~tagged"},
104 {MATCHCRITERIA_AGE_GREATER, "age_greater"},
105 {MATCHCRITERIA_AGE_LOWER, "age_lower"},
106 {MATCHCRITERIA_NEWSGROUPS, "newsgroups"},
107 {MATCHCRITERIA_NOT_NEWSGROUPS, "~newsgroups"},
108 {MATCHCRITERIA_INREPLYTO, "inreplyto"},
109 {MATCHCRITERIA_NOT_INREPLYTO, "~inreplyto"},
110 {MATCHCRITERIA_REFERENCES, "references"},
111 {MATCHCRITERIA_NOT_REFERENCES, "~references"},
112 {MATCHCRITERIA_SCORE_GREATER, "score_greater"},
113 {MATCHCRITERIA_SCORE_LOWER, "score_lower"},
114 {MATCHCRITERIA_SCORE_EQUAL, "score_equal"},
115 {MATCHCRITERIA_PARTIAL, "partial"},
116 {MATCHCRITERIA_NOT_PARTIAL, "~partial"},
117 {MATCHCRITERIA_FOUND_IN_ADDRESSBOOK, "found_in_addressbook"},
118 {MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK, "~found_in_addressbook"},
120 {MATCHCRITERIA_SIZE_GREATER, "size_greater"},
121 {MATCHCRITERIA_SIZE_SMALLER, "size_smaller"},
122 {MATCHCRITERIA_SIZE_EQUAL, "size_equal"},
124 /* content have to be read */
125 {MATCHCRITERIA_HEADER, "header"},
126 {MATCHCRITERIA_NOT_HEADER, "~header"},
127 {MATCHCRITERIA_HEADERS_PART, "headers_part"},
128 {MATCHCRITERIA_NOT_HEADERS_PART, "~headers_part"},
129 {MATCHCRITERIA_MESSAGE, "message"},
130 {MATCHCRITERIA_NOT_MESSAGE, "~message"},
131 {MATCHCRITERIA_BODY_PART, "body_part"},
132 {MATCHCRITERIA_NOT_BODY_PART, "~body_part"},
133 {MATCHCRITERIA_TEST, "test"},
134 {MATCHCRITERIA_NOT_TEST, "~test"},
137 {MATCHTYPE_MATCHCASE, "matchcase"},
138 {MATCHTYPE_MATCH, "match"},
139 {MATCHTYPE_REGEXPCASE, "regexpcase"},
140 {MATCHTYPE_REGEXP, "regexp"},
143 {MATCHACTION_SCORE, "score"}, /* for backward compatibility */
144 {MATCHACTION_MOVE, "move"},
145 {MATCHACTION_COPY, "copy"},
146 {MATCHACTION_DELETE, "delete"},
147 {MATCHACTION_MARK, "mark"},
148 {MATCHACTION_UNMARK, "unmark"},
149 {MATCHACTION_LOCK, "lock"},
150 {MATCHACTION_UNLOCK, "unlock"},
151 {MATCHACTION_MARK_AS_READ, "mark_as_read"},
152 {MATCHACTION_MARK_AS_UNREAD, "mark_as_unread"},
153 {MATCHACTION_MARK_AS_SPAM, "mark_as_spam"},
154 {MATCHACTION_MARK_AS_HAM, "mark_as_ham"},
155 {MATCHACTION_FORWARD, "forward"},
156 {MATCHACTION_FORWARD_AS_ATTACHMENT, "forward_as_attachment"},
157 {MATCHACTION_EXECUTE, "execute"},
158 {MATCHACTION_COLOR, "color"},
159 {MATCHACTION_REDIRECT, "redirect"},
160 {MATCHACTION_CHANGE_SCORE, "change_score"},
161 {MATCHACTION_SET_SCORE, "set_score"},
162 {MATCHACTION_STOP, "stop"},
163 {MATCHACTION_HIDE, "hide"},
164 {MATCHACTION_IGNORE, "ignore"},
165 {MATCHACTION_WATCH, "watch"},
166 {MATCHACTION_ADD_TO_ADDRESSBOOK, "add_to_addressbook"},
167 {MATCHACTION_SET_TAG, "set_tag"},
168 {MATCHACTION_UNSET_TAG, "unset_tag"},
169 {MATCHACTION_CLEAR_TAGS, "clear_tags"},
178 extern gboolean debug_filtering_session;
181 *\brief Look up table with keywords defined in \sa matchparser_tab
183 static GHashTable *matchparser_hashtab;
186 *\brief Translate keyword id to keyword string
188 *\param id Id of keyword
190 *\return const gchar * Keyword
192 const gchar *get_matchparser_tab_str(gint id)
196 for (i = 0; i < sizeof matchparser_tab / sizeof matchparser_tab[0]; i++) {
197 if (matchparser_tab[i].id == id)
198 return matchparser_tab[i].str;
204 *\brief Create keyword lookup table
206 static void create_matchparser_hashtab(void)
210 if (matchparser_hashtab) return;
211 matchparser_hashtab = g_hash_table_new(g_str_hash, g_str_equal);
212 for (i = 0; i < sizeof matchparser_tab / sizeof matchparser_tab[0]; i++)
213 g_hash_table_insert(matchparser_hashtab,
214 matchparser_tab[i].str,
215 (gpointer) &matchparser_tab[i]);
219 *\brief Return a keyword id from a keyword string
221 *\param str Keyword string
223 *\return gint Keyword id
225 gint get_matchparser_tab_id(const gchar *str)
229 if (NULL != (res = g_hash_table_lookup(matchparser_hashtab, str))) {
235 /* **************** data structure allocation **************** */
238 *\brief Allocate a structure for a filtering / scoring
239 * "condition" (a matcher structure)
241 *\param criteria Criteria ID (MATCHCRITERIA_XXXX)
242 *\param header Header string (if criteria is MATCHCRITERIA_HEADER
243 or MATCHCRITERIA_FOUND_IN_ADDRESSBOOK)
244 *\param matchtype Type of action (MATCHTYPE_XXX)
245 *\param expr String value or expression to check
246 *\param value Integer value to check
248 *\return MatcherProp * Pointer to newly allocated structure
250 MatcherProp *matcherprop_new(gint criteria, const gchar *header,
251 gint matchtype, const gchar *expr,
256 prop = g_new0(MatcherProp, 1);
257 prop->criteria = criteria;
258 prop->header = header != NULL ? g_strdup(header) : NULL;
260 if (matchtype == MATCHTYPE_MATCHCASE ||
261 matchtype == MATCHTYPE_REGEXPCASE)
262 prop->expr = expr != NULL ? g_utf8_casefold(expr, -1) : NULL;
264 prop->expr = expr != NULL ? g_strdup(expr) : NULL;
266 prop->matchtype = matchtype;
275 *\brief Allocate a structure for a filtering / scoring
276 * "condition" (a matcher structure)
277 * Same as matcherprop_new, except it doesn't change the expr's
280 *\param criteria Criteria ID (MATCHCRITERIA_XXXX)
281 *\param header Header string (if criteria is MATCHCRITERIA_HEADER
282 or MATCHCRITERIA_FOUND_IN_ADDRESSBOOK)
283 *\param matchtype Type of action (MATCHTYPE_XXX)
284 *\param expr String value or expression to check
285 *\param value Integer value to check
287 *\return MatcherProp * Pointer to newly allocated structure
289 MatcherProp *matcherprop_new_create(gint criteria, const gchar *header,
290 gint matchtype, const gchar *expr,
295 prop = g_new0(MatcherProp, 1);
296 prop->criteria = criteria;
297 prop->header = header != NULL ? g_strdup(header) : NULL;
299 prop->expr = expr != NULL ? g_strdup(expr) : NULL;
301 prop->matchtype = matchtype;
310 *\brief Free a matcher structure
312 *\param prop Pointer to matcher structure allocated with
315 void matcherprop_free(MatcherProp *prop)
318 g_free(prop->header);
319 if (prop->preg != NULL) {
327 *\brief Copy a matcher structure
329 *\param src Matcher structure to copy
331 *\return MatcherProp * Pointer to newly allocated matcher structure
333 MatcherProp *matcherprop_copy(const MatcherProp *src)
335 MatcherProp *prop = g_new0(MatcherProp, 1);
337 prop->criteria = src->criteria;
338 prop->header = src->header ? g_strdup(src->header) : NULL;
339 prop->expr = src->expr ? g_strdup(src->expr) : NULL;
340 prop->matchtype = src->matchtype;
342 prop->preg = NULL; /* will be re-evaluated */
343 prop->value = src->value;
344 prop->error = src->error;
348 /* ************** match ******************************/
350 static gboolean match_with_addresses_in_addressbook
351 (MatcherProp *prop, GSList *address_list, gint type,
352 gchar* folderpath, gint match)
355 gboolean found = FALSE;
358 g_return_val_if_fail(address_list != NULL, FALSE);
360 debug_print("match_with_addresses_in_addressbook(%d, %s)\n",
361 g_slist_length(address_list), folderpath?folderpath:"(null)");
363 if (folderpath == NULL ||
364 strcasecmp(folderpath, _("Any")) == 0 ||
370 start_address_completion(path);
372 for (walk = address_list; walk != NULL; walk = walk->next) {
373 /* exact matching of email address */
374 guint num_addr = complete_address(walk->data);
377 /* skip first item (this is the search string itself) */
379 for (; i < num_addr && !found; i++) {
380 gchar *addr = get_complete_address(i);
381 extract_address(addr);
382 if (strcasecmp(addr, walk->data) == 0) {
386 if (debug_filtering_session
387 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
388 log_print(LOG_DEBUG_FILTERING,
389 "address [ %s ] matches\n",
390 (gchar *)walk->data);
397 if (debug_filtering_session
398 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH
400 log_print(LOG_DEBUG_FILTERING,
401 "address [ %s ] doesn't match\n",
402 (gchar *)walk->data);
406 if (match == MATCH_ALL) {
407 /* if matching all addresses, stop if one doesn't match */
410 if (debug_filtering_session
411 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
412 log_print(LOG_DEBUG_FILTERING,
413 "not all address match (matching all)\n");
417 } else if (match == MATCH_ANY) {
418 /* if matching any address, stop if one does match */
421 if (debug_filtering_session
422 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
423 log_print(LOG_DEBUG_FILTERING,
424 "at least one address matches (matching any)\n");
429 /* MATCH_ONE: there should be only one loop iteration */
432 end_address_completion();
438 *\brief Find out if a string matches a condition
440 *\param prop Matcher structure
441 *\param str String to check
443 *\return gboolean TRUE if str matches the condition in the
446 static gboolean matcherprop_string_match(MatcherProp *prop, const gchar *str,
447 const gchar *debug_context)
450 gboolean ret = FALSE;
451 gboolean should_free = FALSE;
455 if (prop->matchtype == MATCHTYPE_REGEXPCASE ||
456 prop->matchtype == MATCHTYPE_MATCHCASE) {
457 str1 = g_utf8_casefold(str, -1);
464 switch (prop->matchtype) {
465 case MATCHTYPE_REGEXPCASE:
466 case MATCHTYPE_REGEXP:
467 if (!prop->preg && (prop->error == 0)) {
468 prop->preg = g_new0(regex_t, 1);
469 /* if regexp then don't use the escaped string */
470 if (regcomp(prop->preg, prop->expr,
471 REG_NOSUB | REG_EXTENDED
472 | ((prop->matchtype == MATCHTYPE_REGEXPCASE)
473 ? REG_ICASE : 0)) != 0) {
479 if (prop->preg == NULL)
482 if (regexec(prop->preg, str1, 0, NULL, 0) == 0)
488 if (debug_filtering_session
489 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
490 gchar *stripped = g_strdup(str);
492 strretchomp(stripped);
494 log_print(LOG_DEBUG_FILTERING,
495 "%s value [ %s ] matches regular expression [ %s ] (%s)\n",
496 debug_context, stripped, prop->expr,
497 prop->matchtype == MATCHTYPE_REGEXP ? _("Case sensitive"):_("Case unsensitive"));
499 log_print(LOG_DEBUG_FILTERING,
500 "%s value [ %s ] doesn't matches regular expression [ %s ] (%s)\n",
501 debug_context, stripped, prop->expr,
502 prop->matchtype == MATCHTYPE_REGEXP ? _("Case sensitive"):_("Case unsensitive"));
508 case MATCHTYPE_MATCHCASE:
509 case MATCHTYPE_MATCH:
510 ret = (strstr(str1, prop->expr) != NULL);
513 if (debug_filtering_session
514 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
515 gchar *stripped = g_strdup(str);
517 strretchomp(stripped);
519 log_print(LOG_DEBUG_FILTERING,
520 "%s value [ %s ] contains [ %s ] (%s)\n",
521 debug_context, stripped, prop->expr,
522 prop->matchtype == MATCHTYPE_MATCH ? _("Case sensitive"):_("Case unsensitive"));
524 log_print(LOG_DEBUG_FILTERING,
525 "%s value [ %s ] doesn't contains [ %s ] (%s)\n",
526 debug_context, stripped, prop->expr,
527 prop->matchtype == MATCHTYPE_MATCH ? _("Case sensitive"):_("Case unsensitive"));
544 *\brief Find out if a tag matches a condition
546 *\param prop Matcher structure
547 *\param msginfo message to check
549 *\return gboolean TRUE if msginfo matches the condition in the
552 static gboolean matcherprop_tag_match(MatcherProp *prop, MsgInfo *msginfo,
553 const gchar *debug_context)
555 gboolean ret = FALSE;
558 if (msginfo == NULL || msginfo->tags == NULL)
561 for (cur = msginfo->tags; cur; cur = cur->next) {
562 const gchar *str = tags_get_tag(GPOINTER_TO_INT(cur->data));
565 if (matcherprop_string_match(prop, str, debug_context)) {
574 *\brief Find out if the string-ed list matches a condition
576 *\param prop Matcher structure
577 *\param list GSList of strings to check
579 *\return gboolean TRUE if str matches the condition in the
582 static gboolean matcherprop_list_match(MatcherProp *prop, const GSList *list,
583 const gchar *debug_context)
587 for(cur = list; cur != NULL; cur = cur->next) {
588 if (matcherprop_string_match(prop, (gchar *)cur->data, debug_context))
594 /* FIXME body search is a hack. */
595 static gboolean matcherprop_string_decode_match(MatcherProp *prop, const gchar *str,
596 const gchar *debug_context)
600 gboolean res = FALSE;
605 /* we try to decode QP first, because it's faster than base64 */
606 qp_decode_const(tmp, BUFFSIZE-1, str);
607 if (!g_utf8_validate(tmp, -1, NULL)) {
608 utf = conv_codeset_strdup
609 (tmp, conv_get_locale_charset_str_no_utf8(),
611 res = matcherprop_string_match(prop, utf, debug_context);
614 res = matcherprop_string_match(prop, tmp, debug_context);
617 if (res == FALSE && (strchr(prop->expr, '=') || strchr(prop->expr, '_')
618 || strchr(str, '=') || strchr(str, '_'))) {
619 /* if searching for something with an equal char, maybe
620 * we should try to match the non-decoded string.
621 * In case it was not qp-encoded. */
622 if (!g_utf8_validate(str, -1, NULL)) {
623 utf = conv_codeset_strdup
624 (str, conv_get_locale_charset_str_no_utf8(),
626 res = matcherprop_string_match(prop, utf, debug_context);
629 res = matcherprop_string_match(prop, str, debug_context);
633 /* FIXME base64 decoding is too slow, especially since text can
634 * easily be handled as base64. Don't even try now. */
640 typedef struct _thread_data {
647 static void *matcher_test_thread(void *data)
649 thread_data *td = (thread_data *)data;
652 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
653 pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
655 result = system(td->cmd);
656 td->done = TRUE; /* let the caller thread join() */
657 return GINT_TO_POINTER(result);
662 *\brief Execute a command defined in the matcher structure
664 *\param prop Pointer to matcher structure
665 *\param info Pointer to message info structure
667 *\return gboolean TRUE if command was executed succesfully
669 static gboolean matcherprop_match_test(const MatcherProp *prop,
677 thread_data *td = g_new0(thread_data, 1);
679 time_t start_time = time(NULL);
682 file = procmsg_get_message_file(info);
687 cmd = matching_build_command(prop->expr, info);
691 #if (defined USE_PTHREAD && ((defined __GLIBC__ && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 3))) || !defined __GLIBC__))
693 if (debug_filtering_session
694 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
695 log_print(LOG_DEBUG_FILTERING,
696 "starting threaded command [ %s ]\n",
702 if (pthread_create(&pt, PTHREAD_CREATE_JOINABLE,
703 matcher_test_thread, td) != 0)
704 retval = system(cmd);
706 debug_print("waiting for test thread\n");
708 /* don't let the interface freeze while waiting */
710 if (time(NULL) - start_time > 30) {
716 pthread_join(pt, &res);
717 retval = GPOINTER_TO_INT(res);
718 debug_print(" test thread returned %d\n", retval);
723 if (debug_filtering_session
724 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
725 log_print(LOG_DEBUG_FILTERING,
726 "starting synchronous command [ %s ]\n",
730 retval = system(cmd);
732 debug_print("Command exit code: %d\n", retval);
735 if (debug_filtering_session
736 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
737 log_print(LOG_DEBUG_FILTERING,
738 "command returned [ %d ]\n",
743 return (retval == 0);
747 *\brief Check if a message matches the condition in a matcher
750 *\param prop Pointer to matcher structure
751 *\param info Pointer to message info
753 *\return gboolean TRUE if a match
755 gboolean matcherprop_match(MatcherProp *prop,
760 switch(prop->criteria) {
761 case MATCHCRITERIA_ALL:
763 case MATCHCRITERIA_UNREAD:
764 return MSG_IS_UNREAD(info->flags);
765 case MATCHCRITERIA_NOT_UNREAD:
766 return !MSG_IS_UNREAD(info->flags);
767 case MATCHCRITERIA_NEW:
768 return MSG_IS_NEW(info->flags);
769 case MATCHCRITERIA_NOT_NEW:
770 return !MSG_IS_NEW(info->flags);
771 case MATCHCRITERIA_MARKED:
772 return MSG_IS_MARKED(info->flags);
773 case MATCHCRITERIA_NOT_MARKED:
774 return !MSG_IS_MARKED(info->flags);
775 case MATCHCRITERIA_DELETED:
776 return MSG_IS_DELETED(info->flags);
777 case MATCHCRITERIA_NOT_DELETED:
778 return !MSG_IS_DELETED(info->flags);
779 case MATCHCRITERIA_REPLIED:
780 return MSG_IS_REPLIED(info->flags);
781 case MATCHCRITERIA_NOT_REPLIED:
782 return !MSG_IS_REPLIED(info->flags);
783 case MATCHCRITERIA_FORWARDED:
784 return MSG_IS_FORWARDED(info->flags);
785 case MATCHCRITERIA_NOT_FORWARDED:
786 return !MSG_IS_FORWARDED(info->flags);
787 case MATCHCRITERIA_LOCKED:
788 return MSG_IS_LOCKED(info->flags);
789 case MATCHCRITERIA_NOT_LOCKED:
790 return !MSG_IS_LOCKED(info->flags);
791 case MATCHCRITERIA_SPAM:
792 return MSG_IS_SPAM(info->flags);
793 case MATCHCRITERIA_NOT_SPAM:
794 return !MSG_IS_SPAM(info->flags);
795 case MATCHCRITERIA_COLORLABEL:
797 gint color = MSG_GET_COLORLABEL_VALUE(info->flags);
798 gboolean ret = (color == prop->value);
801 if (debug_filtering_session
802 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
804 log_print(LOG_DEBUG_FILTERING,
805 "message color value [ %d ] matches color value [ %d ]\n",
808 log_print(LOG_DEBUG_FILTERING,
809 "message color value [ %d ] doesn't matches color value [ %d ]\n",
815 case MATCHCRITERIA_NOT_COLORLABEL:
817 gint color = MSG_GET_COLORLABEL_VALUE(info->flags);
818 gboolean ret = (color != prop->value);
821 if (debug_filtering_session
822 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
824 log_print(LOG_DEBUG_FILTERING,
825 "message color value [ %d ] matches color value [ %d ]\n",
828 log_print(LOG_DEBUG_FILTERING,
829 "message color value [ %d ] doesn't matches color value [ %d ]\n",
835 case MATCHCRITERIA_IGNORE_THREAD:
836 return MSG_IS_IGNORE_THREAD(info->flags);
837 case MATCHCRITERIA_NOT_IGNORE_THREAD:
838 return !MSG_IS_IGNORE_THREAD(info->flags);
839 case MATCHCRITERIA_WATCH_THREAD:
840 return MSG_IS_WATCH_THREAD(info->flags);
841 case MATCHCRITERIA_NOT_WATCH_THREAD:
842 return !MSG_IS_WATCH_THREAD(info->flags);
843 case MATCHCRITERIA_SUBJECT:
844 return matcherprop_string_match(prop, info->subject,
845 prefs_common_translated_header_name("Subject:"));
846 case MATCHCRITERIA_NOT_SUBJECT:
847 return !matcherprop_string_match(prop, info->subject,
848 prefs_common_translated_header_name("Subject:"));
849 case MATCHCRITERIA_FROM:
850 case MATCHCRITERIA_NOT_FROM:
855 context = g_strdup_printf(_("%s header"), prefs_common_translated_header_name("From:"));
856 ret = matcherprop_string_match(prop, info->from, context);
858 return (prop->criteria == MATCHCRITERIA_FROM)? ret : !ret;
860 case MATCHCRITERIA_TO:
861 case MATCHCRITERIA_NOT_TO:
866 context = g_strdup_printf(_("%s header"), prefs_common_translated_header_name("To:"));
867 ret = matcherprop_string_match(prop, info->to, context);
869 return (prop->criteria == MATCHCRITERIA_TO)? ret : !ret;
871 case MATCHCRITERIA_CC:
872 case MATCHCRITERIA_NOT_CC:
877 context = g_strdup_printf(_("%s header"), prefs_common_translated_header_name("Cc:"));
878 ret = matcherprop_string_match(prop, info->cc, context);
880 return (prop->criteria == MATCHCRITERIA_CC)? ret : !ret;
882 case MATCHCRITERIA_TO_OR_CC:
884 gchar *context1, *context2;
887 context1 = g_strdup_printf(_("%s header"), prefs_common_translated_header_name("To:"));
888 context2 = g_strdup_printf(_("%s header"), prefs_common_translated_header_name("Cc:"));
889 ret = matcherprop_string_match(prop, info->to, context1)
890 || matcherprop_string_match(prop, info->cc, context2);
895 case MATCHCRITERIA_NOT_TO_AND_NOT_CC:
897 gchar *context1, *context2;
900 context1 = g_strdup_printf(_("%s header"), prefs_common_translated_header_name("To:"));
901 context2 = g_strdup_printf(_("%s header"), prefs_common_translated_header_name("Cc:"));
902 ret = !(matcherprop_string_match(prop, info->to, context1)
903 || matcherprop_string_match(prop, info->cc, context2));
908 case MATCHCRITERIA_TAG:
909 case MATCHCRITERIA_NOT_TAG:
913 ret = matcherprop_tag_match(prop, info, _("Tag"));
914 return (prop->criteria == MATCHCRITERIA_TAG)? ret : !ret;
916 case MATCHCRITERIA_TAGGED:
917 case MATCHCRITERIA_NOT_TAGGED:
921 ret = (info->tags != NULL);
922 return (prop->criteria == MATCHCRITERIA_TAGGED)? ret : !ret;
924 case MATCHCRITERIA_AGE_GREATER:
930 age = ((t - info->date_t) / (60 * 60 * 24));
931 ret = (age > prop->value);
934 if (debug_filtering_session
935 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
937 log_print(LOG_DEBUG_FILTERING,
938 "message age [ %d ] is greater than [ %d ]\n",
941 log_print(LOG_DEBUG_FILTERING,
942 "message age [ %d ] is not greater than [ %d ]\n",
948 case MATCHCRITERIA_AGE_LOWER:
954 age = ((t - info->date_t) / (60 * 60 * 24));
955 ret = (age < prop->value);
958 if (debug_filtering_session
959 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
961 log_print(LOG_DEBUG_FILTERING,
962 "message age [ %d ] is lower than [ %d ]\n",
965 log_print(LOG_DEBUG_FILTERING,
966 "message age [ %d ] is not lower than [ %d ]\n",
972 case MATCHCRITERIA_SCORE_GREATER:
974 gboolean ret = (info->score > prop->value);
977 if (debug_filtering_session
978 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
980 log_print(LOG_DEBUG_FILTERING,
981 "message score [ %d ] is greater than [ %d ]\n",
982 info->score, prop->value);
984 log_print(LOG_DEBUG_FILTERING,
985 "message score [ %d ] is not greater than [ %d ]\n",
986 info->score, prop->value);
991 case MATCHCRITERIA_SCORE_LOWER:
993 gboolean ret = (info->score < prop->value);
996 if (debug_filtering_session
997 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
999 log_print(LOG_DEBUG_FILTERING,
1000 "message score [ %d ] is lower than [ %d ]\n",
1001 info->score, prop->value);
1003 log_print(LOG_DEBUG_FILTERING,
1004 "message score [ %d ] is not lower than [ %d ]\n",
1005 info->score, prop->value);
1010 case MATCHCRITERIA_SCORE_EQUAL:
1012 gboolean ret = (info->score == prop->value);
1015 if (debug_filtering_session
1016 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
1018 log_print(LOG_DEBUG_FILTERING,
1019 "message score [ %d ] is equal to [ %d ]\n",
1020 info->score, prop->value);
1022 log_print(LOG_DEBUG_FILTERING,
1023 "message score [ %d ] is not equal to [ %d ]\n",
1024 info->score, prop->value);
1029 case MATCHCRITERIA_SIZE_GREATER:
1031 /* FIXME: info->size is an off_t */
1032 gboolean ret = (info->size > (off_t) prop->value);
1035 if (debug_filtering_session
1036 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
1038 log_print(LOG_DEBUG_FILTERING,
1039 "message size [ %ld ] is greater than [ %d ]\n",
1040 info->size, prop->value);
1042 log_print(LOG_DEBUG_FILTERING,
1043 "message size [ %ld ] is not greater than [ %d ]\n",
1044 info->size, prop->value);
1049 case MATCHCRITERIA_SIZE_SMALLER:
1051 /* FIXME: info->size is an off_t */
1052 gboolean ret = (info->size < (off_t) prop->value);
1055 if (debug_filtering_session
1056 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
1058 log_print(LOG_DEBUG_FILTERING,
1059 "message size [ %ld ] is smaller than [ %d ]\n",
1060 info->size, prop->value);
1062 log_print(LOG_DEBUG_FILTERING,
1063 "message size [ %ld ] is not smaller than [ %d ]\n",
1064 info->size, prop->value);
1069 case MATCHCRITERIA_SIZE_EQUAL:
1071 /* FIXME: info->size is an off_t */
1072 gboolean ret = (info->size == (off_t) prop->value);
1075 if (debug_filtering_session
1076 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
1078 log_print(LOG_DEBUG_FILTERING,
1079 "message size [ %ld ] is equal to [ %d ]\n",
1080 info->size, prop->value);
1082 log_print(LOG_DEBUG_FILTERING,
1083 "message size [ %ld ] is not equal to [ %d ]\n",
1084 info->size, prop->value);
1089 case MATCHCRITERIA_PARTIAL:
1091 /* FIXME: info->size is an off_t */
1092 gboolean ret = (info->total_size != 0 && info->size != (off_t)info->total_size);
1095 if (debug_filtering_session
1096 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
1098 log_print(LOG_DEBUG_FILTERING,
1099 "message is partially downloaded, size [ %ld ] is less than total size [ %d ])\n",
1100 info->size, info->total_size);
1102 log_print(LOG_DEBUG_FILTERING,
1103 "message is not partially downloaded\n");
1108 case MATCHCRITERIA_NOT_PARTIAL:
1110 /* FIXME: info->size is an off_t */
1111 gboolean ret = (info->total_size == 0 || info->size == (off_t)info->total_size);
1114 if (debug_filtering_session
1115 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
1117 log_print(LOG_DEBUG_FILTERING,
1118 "message is not partially downloaded\n");
1120 log_print(LOG_DEBUG_FILTERING,
1121 "message is partially downloaded, size [ %ld ] is less than total size [ %d ])\n",
1122 info->size, info->total_size);
1127 case MATCHCRITERIA_NEWSGROUPS:
1128 case MATCHCRITERIA_NOT_NEWSGROUPS:
1133 context = g_strdup_printf(_("%s header"),
1134 prefs_common_translated_header_name("Newsgroups:"));
1135 ret = matcherprop_string_match(prop, info->newsgroups, context);
1137 return (prop->criteria == MATCHCRITERIA_NEWSGROUPS)? ret : !ret;
1139 case MATCHCRITERIA_INREPLYTO:
1140 case MATCHCRITERIA_NOT_INREPLYTO:
1145 context = g_strdup_printf(_("%s header"),
1146 prefs_common_translated_header_name("In-Reply-To:"));
1147 ret = matcherprop_string_match(prop, info->inreplyto, context);
1149 return (prop->criteria == MATCHCRITERIA_INREPLYTO)? ret : !ret;
1151 case MATCHCRITERIA_REFERENCES:
1152 case MATCHCRITERIA_NOT_REFERENCES:
1157 context = g_strdup_printf(_("%s header"),
1158 prefs_common_translated_header_name("References:"));
1159 ret = matcherprop_list_match(prop, info->references, context);
1161 return (prop->criteria == MATCHCRITERIA_REFERENCES)? ret : !ret;
1163 case MATCHCRITERIA_TEST:
1164 return matcherprop_match_test(prop, info);
1165 case MATCHCRITERIA_NOT_TEST:
1166 return !matcherprop_match_test(prop, info);
1172 /* ********************* MatcherList *************************** */
1175 *\brief Create a new list of matchers
1177 *\param matchers List of matcher structures
1178 *\param bool_and Operator
1180 *\return MatcherList * New list
1182 MatcherList *matcherlist_new(GSList *matchers, gboolean bool_and)
1186 cond = g_new0(MatcherList, 1);
1188 cond->matchers = matchers;
1189 cond->bool_and = bool_and;
1195 *\brief Frees a list of matchers
1197 *\param cond List of matchers
1199 void matcherlist_free(MatcherList *cond)
1203 g_return_if_fail(cond);
1204 for (l = cond->matchers ; l != NULL ; l = g_slist_next(l)) {
1205 matcherprop_free((MatcherProp *) l->data);
1211 *\brief Skip all headers in a message file
1213 *\param fp Message file
1215 static void matcherlist_skip_headers(FILE *fp)
1217 gchar buf[BUFFSIZE];
1219 while (procheader_get_one_field(buf, sizeof(buf), fp, NULL) != -1)
1224 *\brief Check if a header matches a matcher condition
1226 *\param matcher Matcher structure to check header for
1227 *\param buf Header name
1229 *\return boolean TRUE if matching header
1231 static gboolean matcherprop_match_one_header(MatcherProp *matcher,
1234 gboolean result = FALSE;
1235 Header *header = NULL;
1237 switch (matcher->criteria) {
1238 case MATCHCRITERIA_HEADER:
1239 case MATCHCRITERIA_NOT_HEADER:
1240 header = procheader_parse_header(buf);
1243 if (procheader_headername_equal(header->name,
1245 if (matcher->criteria == MATCHCRITERIA_HEADER)
1246 result = matcherprop_string_match(matcher, header->body, _("header"));
1248 result = !matcherprop_string_match(matcher, header->body, _("header"));
1249 procheader_header_free(header);
1253 procheader_header_free(header);
1256 case MATCHCRITERIA_HEADERS_PART:
1257 return matcherprop_string_match(matcher, buf, _("header line"));
1258 case MATCHCRITERIA_NOT_HEADERS_PART:
1259 return !matcherprop_string_match(matcher, buf, _("headers line"));
1260 case MATCHCRITERIA_MESSAGE:
1261 return matcherprop_string_decode_match(matcher, buf, _("message line"));
1262 case MATCHCRITERIA_NOT_MESSAGE:
1263 return !matcherprop_string_decode_match(matcher, buf, _("message line"));
1264 case MATCHCRITERIA_FOUND_IN_ADDRESSBOOK:
1265 case MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK:
1267 GSList *address_list = NULL;
1268 gint match = MATCH_ONE;
1269 gboolean found = FALSE;
1271 /* how many address headers are me trying to mach? */
1272 if (strcasecmp(matcher->header, _("Any")) == 0)
1274 else if (strcasecmp(matcher->header, Q_("Filtering Matcher Menu|All")) == 0)
1277 if (match == MATCH_ONE) {
1278 /* matching one address header exactly, is that the right one? */
1279 header = procheader_parse_header(buf);
1281 !procheader_headername_equal(header->name, matcher->header))
1283 address_list = address_list_append(address_list, header->body);
1284 if (address_list == NULL)
1288 header = procheader_parse_header(buf);
1291 /* address header is one of the headers we have to match when checking
1292 for any address header or all address headers? */
1293 if (procheader_headername_equal(header->name, "From") ||
1294 procheader_headername_equal(header->name, "To") ||
1295 procheader_headername_equal(header->name, "Cc") ||
1296 procheader_headername_equal(header->name, "Reply-To") ||
1297 procheader_headername_equal(header->name, "Sender"))
1298 address_list = address_list_append(address_list, header->body);
1299 if (address_list == NULL)
1303 found = match_with_addresses_in_addressbook
1304 (matcher, address_list, matcher->criteria,
1305 matcher->expr, match);
1306 g_slist_free(address_list);
1308 if (matcher->criteria == MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK)
1319 *\brief Check if the matcher structure wants headers to
1322 *\param matcher Matcher structure
1324 *\return gboolean TRUE if the matcher structure describes
1325 * a header match condition
1327 static gboolean matcherprop_criteria_headers(const MatcherProp *matcher)
1329 switch (matcher->criteria) {
1330 case MATCHCRITERIA_HEADER:
1331 case MATCHCRITERIA_NOT_HEADER:
1332 case MATCHCRITERIA_HEADERS_PART:
1333 case MATCHCRITERIA_NOT_HEADERS_PART:
1334 case MATCHCRITERIA_FOUND_IN_ADDRESSBOOK:
1335 case MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK:
1343 *\brief Check if the matcher structure wants the message
1344 * to be matched (just perform an action on any
1347 *\param matcher Matcher structure
1349 *\return gboolean TRUE if matcher condition should match
1352 static gboolean matcherprop_criteria_message(MatcherProp *matcher)
1354 switch (matcher->criteria) {
1355 case MATCHCRITERIA_MESSAGE:
1356 case MATCHCRITERIA_NOT_MESSAGE:
1364 *\brief Check if a list of conditions matches one header in
1367 *\param matchers List of conditions
1368 *\param fp Message file
1370 *\return gboolean TRUE if one of the headers is matched by
1371 * the list of conditions.
1373 static gboolean matcherlist_match_headers(MatcherList *matchers, FILE *fp)
1376 gchar buf[BUFFSIZE];
1378 while (procheader_get_one_field(buf, sizeof(buf), fp, NULL) != -1) {
1379 for (l = matchers->matchers ; l != NULL ; l = g_slist_next(l)) {
1380 MatcherProp *matcher = (MatcherProp *) l->data;
1381 gint match = MATCH_ANY;
1386 /* determine the match range (all, any are our concern here) */
1387 if (matcher->criteria == MATCHCRITERIA_NOT_HEADERS_PART ||
1388 matcher->criteria == MATCHCRITERIA_NOT_MESSAGE) {
1391 } else if (matcher->criteria == MATCHCRITERIA_FOUND_IN_ADDRESSBOOK ||
1392 matcher->criteria == MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK) {
1393 Header *header = NULL;
1395 /* address header is one of the headers we have to match when checking
1396 for any address header or all address headers? */
1397 header = procheader_parse_header(buf);
1399 (procheader_headername_equal(header->name, "From") ||
1400 procheader_headername_equal(header->name, "To") ||
1401 procheader_headername_equal(header->name, "Cc") ||
1402 procheader_headername_equal(header->name, "Reply-To") ||
1403 procheader_headername_equal(header->name, "Sender"))) {
1405 if (strcasecmp(matcher->header, _("Any")) == 0)
1407 else if (strcasecmp(matcher->header, Q_("Filtering Matcher Menu|All")) == 0)
1412 /* further call to matcherprop_match_one_header() can't match
1413 and it irrelevant, so: don't alter the match result */
1418 /* ZERO line must NOT match for the rule to match.
1420 if (match == MATCH_ALL) {
1421 if (matcherprop_match_one_header(matcher, buf)) {
1422 matcher->result = TRUE;
1424 matcher->result = FALSE;
1425 matcher->done = TRUE;
1427 /* else, just one line matching is enough for the rule to match
1429 } else if (matcherprop_criteria_headers(matcher) ||
1430 matcherprop_criteria_message(matcher)) {
1431 if (matcherprop_match_one_header(matcher, buf)) {
1432 matcher->result = TRUE;
1433 matcher->done = TRUE;
1437 /* if the rule matched and the matchers are OR, no need to
1438 * check the others */
1439 if (matcher->result && matcher->done) {
1440 if (!matchers->bool_and)
1450 *\brief Check if a matcher wants to check the message body
1452 *\param matcher Matcher structure
1454 *\return gboolean TRUE if body must be matched.
1456 static gboolean matcherprop_criteria_body(const MatcherProp *matcher)
1458 switch (matcher->criteria) {
1459 case MATCHCRITERIA_BODY_PART:
1460 case MATCHCRITERIA_NOT_BODY_PART:
1468 *\brief Check if a (line) string matches the criteria
1469 * described by a matcher structure
1471 *\param matcher Matcher structure
1474 *\return gboolean TRUE if string matches criteria
1476 static gboolean matcherprop_match_line(MatcherProp *matcher, const gchar *line)
1478 switch (matcher->criteria) {
1479 case MATCHCRITERIA_BODY_PART:
1480 case MATCHCRITERIA_MESSAGE:
1481 return matcherprop_string_decode_match(matcher, line, _("body line"));
1482 case MATCHCRITERIA_NOT_BODY_PART:
1483 case MATCHCRITERIA_NOT_MESSAGE:
1484 return !matcherprop_string_decode_match(matcher, line, _("body line"));
1490 *\brief Check if a line in a message file's body matches
1493 *\param matchers List of conditions
1494 *\param fp Message file
1496 *\return gboolean TRUE if succesful match
1498 static gboolean matcherlist_match_body(MatcherList *matchers, FILE *fp)
1501 gchar buf[BUFFSIZE];
1503 while (fgets(buf, sizeof(buf), fp) != NULL) {
1504 for (l = matchers->matchers ; l != NULL ; l = g_slist_next(l)) {
1505 MatcherProp *matcher = (MatcherProp *) l->data;
1510 /* if the criteria is ~body_part or ~message, ZERO lines
1511 * must NOT match for the rule to match. */
1512 if (matcher->criteria == MATCHCRITERIA_NOT_BODY_PART ||
1513 matcher->criteria == MATCHCRITERIA_NOT_MESSAGE) {
1514 if (matcherprop_match_line(matcher, buf)) {
1515 matcher->result = TRUE;
1517 matcher->result = FALSE;
1518 matcher->done = TRUE;
1520 /* else, just one line has to match */
1521 } else if (matcherprop_criteria_body(matcher) ||
1522 matcherprop_criteria_message(matcher)) {
1523 if (matcherprop_match_line(matcher, buf)) {
1524 matcher->result = TRUE;
1525 matcher->done = TRUE;
1529 /* if the matchers are OR'ed and the rule matched,
1530 * no need to check the others. */
1531 if (matcher->result && matcher->done) {
1532 if (!matchers->bool_and)
1541 *\brief Check if a message file matches criteria
1543 *\param matchers Criteria
1544 *\param info Message info
1545 *\param result Default result
1547 *\return gboolean TRUE if matched
1549 static gboolean matcherlist_match_file(MatcherList *matchers, MsgInfo *info,
1552 gboolean read_headers;
1558 /* file need to be read ? */
1560 read_headers = FALSE;
1562 for (l = matchers->matchers ; l != NULL ; l = g_slist_next(l)) {
1563 MatcherProp *matcher = (MatcherProp *) l->data;
1565 if (matcherprop_criteria_headers(matcher))
1566 read_headers = TRUE;
1567 if (matcherprop_criteria_body(matcher))
1569 if (matcherprop_criteria_message(matcher)) {
1570 read_headers = TRUE;
1573 matcher->result = FALSE;
1574 matcher->done = FALSE;
1577 if (!read_headers && !read_body)
1580 file = procmsg_get_message_file_full(info, read_headers, read_body);
1584 if ((fp = g_fopen(file, "rb")) == NULL) {
1585 FILE_OP_ERROR(file, "fopen");
1590 /* read the headers */
1593 if (matcherlist_match_headers(matchers, fp))
1596 matcherlist_skip_headers(fp);
1601 matcherlist_match_body(matchers, fp);
1604 for (l = matchers->matchers; l != NULL; l = g_slist_next(l)) {
1605 MatcherProp *matcher = (MatcherProp *) l->data;
1607 if (matcherprop_criteria_headers(matcher) ||
1608 matcherprop_criteria_body(matcher) ||
1609 matcherprop_criteria_message(matcher)) {
1610 if (matcher->result) {
1611 if (!matchers->bool_and) {
1617 if (matchers->bool_and) {
1633 *\brief Test list of conditions on a message.
1635 *\param matchers List of conditions
1636 *\param info Message info
1638 *\return gboolean TRUE if matched
1640 gboolean matcherlist_match(MatcherList *matchers, MsgInfo *info)
1648 if (matchers->bool_and)
1653 /* test the cached elements */
1655 for (l = matchers->matchers; l != NULL ;l = g_slist_next(l)) {
1656 MatcherProp *matcher = (MatcherProp *) l->data;
1658 if (debug_filtering_session) {
1659 gchar *buf = matcherprop_to_string(matcher);
1660 log_print(LOG_DEBUG_FILTERING, _("checking if message matches [ %s ]\n"), buf);
1664 switch(matcher->criteria) {
1665 case MATCHCRITERIA_ALL:
1666 case MATCHCRITERIA_UNREAD:
1667 case MATCHCRITERIA_NOT_UNREAD:
1668 case MATCHCRITERIA_NEW:
1669 case MATCHCRITERIA_NOT_NEW:
1670 case MATCHCRITERIA_MARKED:
1671 case MATCHCRITERIA_NOT_MARKED:
1672 case MATCHCRITERIA_DELETED:
1673 case MATCHCRITERIA_NOT_DELETED:
1674 case MATCHCRITERIA_REPLIED:
1675 case MATCHCRITERIA_NOT_REPLIED:
1676 case MATCHCRITERIA_FORWARDED:
1677 case MATCHCRITERIA_NOT_FORWARDED:
1678 case MATCHCRITERIA_LOCKED:
1679 case MATCHCRITERIA_NOT_LOCKED:
1680 case MATCHCRITERIA_SPAM:
1681 case MATCHCRITERIA_NOT_SPAM:
1682 case MATCHCRITERIA_COLORLABEL:
1683 case MATCHCRITERIA_NOT_COLORLABEL:
1684 case MATCHCRITERIA_IGNORE_THREAD:
1685 case MATCHCRITERIA_NOT_IGNORE_THREAD:
1686 case MATCHCRITERIA_WATCH_THREAD:
1687 case MATCHCRITERIA_NOT_WATCH_THREAD:
1688 case MATCHCRITERIA_SUBJECT:
1689 case MATCHCRITERIA_NOT_SUBJECT:
1690 case MATCHCRITERIA_FROM:
1691 case MATCHCRITERIA_NOT_FROM:
1692 case MATCHCRITERIA_TO:
1693 case MATCHCRITERIA_NOT_TO:
1694 case MATCHCRITERIA_CC:
1695 case MATCHCRITERIA_NOT_CC:
1696 case MATCHCRITERIA_TO_OR_CC:
1697 case MATCHCRITERIA_NOT_TO_AND_NOT_CC:
1698 case MATCHCRITERIA_TAG:
1699 case MATCHCRITERIA_NOT_TAG:
1700 case MATCHCRITERIA_TAGGED:
1701 case MATCHCRITERIA_NOT_TAGGED:
1702 case MATCHCRITERIA_AGE_GREATER:
1703 case MATCHCRITERIA_AGE_LOWER:
1704 case MATCHCRITERIA_NEWSGROUPS:
1705 case MATCHCRITERIA_NOT_NEWSGROUPS:
1706 case MATCHCRITERIA_INREPLYTO:
1707 case MATCHCRITERIA_NOT_INREPLYTO:
1708 case MATCHCRITERIA_REFERENCES:
1709 case MATCHCRITERIA_NOT_REFERENCES:
1710 case MATCHCRITERIA_SCORE_GREATER:
1711 case MATCHCRITERIA_SCORE_LOWER:
1712 case MATCHCRITERIA_SCORE_EQUAL:
1713 case MATCHCRITERIA_SIZE_GREATER:
1714 case MATCHCRITERIA_SIZE_SMALLER:
1715 case MATCHCRITERIA_SIZE_EQUAL:
1716 case MATCHCRITERIA_TEST:
1717 case MATCHCRITERIA_NOT_TEST:
1718 case MATCHCRITERIA_PARTIAL:
1719 case MATCHCRITERIA_NOT_PARTIAL:
1720 if (matcherprop_match(matcher, info)) {
1721 if (!matchers->bool_and) {
1722 if (debug_filtering_session)
1723 log_status_ok(LOG_DEBUG_FILTERING, _("message matches\n"));
1728 if (matchers->bool_and) {
1729 if (debug_filtering_session)
1730 log_status_nok(LOG_DEBUG_FILTERING, _("message does not match\n"));
1737 /* test the condition on the file */
1739 if (matcherlist_match_file(matchers, info, result)) {
1740 if (!matchers->bool_and) {
1741 if (debug_filtering_session)
1742 log_status_ok(LOG_DEBUG_FILTERING, _("message matches\n"));
1746 if (matchers->bool_and) {
1747 if (debug_filtering_session)
1748 log_status_nok(LOG_DEBUG_FILTERING, _("message does not match\n"));
1753 if (debug_filtering_session) {
1755 log_status_ok(LOG_DEBUG_FILTERING, _("message matches\n"));
1757 log_status_nok(LOG_DEBUG_FILTERING, _("message does not match\n"));
1763 static gint quote_filter_str(gchar * result, guint size,
1773 for(p = path ; * p != '\0' ; p ++) {
1775 if ((* p != '\"') && (* p != '\\')) {
1776 if (remaining > 0) {
1782 result[size - 1] = '\0';
1787 if (remaining >= 2) {
1795 result[size - 1] = '\0';
1800 if (remaining > 0) {
1804 result[size - 1] = '\0';
1812 gchar * matcher_quote_str(const gchar * src)
1817 len = strlen(src) * 2 + 1;
1818 res = g_malloc(len);
1819 quote_filter_str(res, len, src);
1825 *\brief Convert a matcher structure to a string
1827 *\param matcher Matcher structure
1829 *\return gchar * Newly allocated string
1831 gchar *matcherprop_to_string(MatcherProp *matcher)
1833 gchar *matcher_str = NULL;
1834 const gchar *criteria_str;
1835 const gchar *matchtype_str;
1837 gchar * quoted_expr;
1838 gchar * quoted_header;
1840 criteria_str = NULL;
1841 for (i = 0; i < (int) (sizeof(matchparser_tab) / sizeof(MatchParser)); i++) {
1842 if (matchparser_tab[i].id == matcher->criteria)
1843 criteria_str = matchparser_tab[i].str;
1845 if (criteria_str == NULL)
1848 switch (matcher->criteria) {
1849 case MATCHCRITERIA_AGE_GREATER:
1850 case MATCHCRITERIA_AGE_LOWER:
1851 case MATCHCRITERIA_SCORE_GREATER:
1852 case MATCHCRITERIA_SCORE_LOWER:
1853 case MATCHCRITERIA_SCORE_EQUAL:
1854 case MATCHCRITERIA_SIZE_GREATER:
1855 case MATCHCRITERIA_SIZE_SMALLER:
1856 case MATCHCRITERIA_SIZE_EQUAL:
1857 case MATCHCRITERIA_COLORLABEL:
1858 case MATCHCRITERIA_NOT_COLORLABEL:
1859 return g_strdup_printf("%s %i", criteria_str, matcher->value);
1860 case MATCHCRITERIA_ALL:
1861 case MATCHCRITERIA_UNREAD:
1862 case MATCHCRITERIA_NOT_UNREAD:
1863 case MATCHCRITERIA_NEW:
1864 case MATCHCRITERIA_NOT_NEW:
1865 case MATCHCRITERIA_MARKED:
1866 case MATCHCRITERIA_NOT_MARKED:
1867 case MATCHCRITERIA_DELETED:
1868 case MATCHCRITERIA_NOT_DELETED:
1869 case MATCHCRITERIA_REPLIED:
1870 case MATCHCRITERIA_NOT_REPLIED:
1871 case MATCHCRITERIA_FORWARDED:
1872 case MATCHCRITERIA_NOT_FORWARDED:
1873 case MATCHCRITERIA_LOCKED:
1874 case MATCHCRITERIA_NOT_LOCKED:
1875 case MATCHCRITERIA_SPAM:
1876 case MATCHCRITERIA_NOT_SPAM:
1877 case MATCHCRITERIA_PARTIAL:
1878 case MATCHCRITERIA_NOT_PARTIAL:
1879 case MATCHCRITERIA_IGNORE_THREAD:
1880 case MATCHCRITERIA_NOT_IGNORE_THREAD:
1881 case MATCHCRITERIA_WATCH_THREAD:
1882 case MATCHCRITERIA_NOT_WATCH_THREAD:
1883 case MATCHCRITERIA_TAGGED:
1884 case MATCHCRITERIA_NOT_TAGGED:
1885 return g_strdup(criteria_str);
1886 case MATCHCRITERIA_TEST:
1887 case MATCHCRITERIA_NOT_TEST:
1888 quoted_expr = matcher_quote_str(matcher->expr);
1889 matcher_str = g_strdup_printf("%s \"%s\"",
1890 criteria_str, quoted_expr);
1891 g_free(quoted_expr);
1893 case MATCHCRITERIA_FOUND_IN_ADDRESSBOOK:
1894 case MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK:
1895 quoted_header = matcher_quote_str(matcher->header);
1896 quoted_expr = matcher_quote_str(matcher->expr);
1897 matcher_str = g_strdup_printf("%s \"%s\" in \"%s\"",
1898 criteria_str, quoted_header, quoted_expr);
1899 g_free(quoted_header);
1900 g_free(quoted_expr);
1904 matchtype_str = NULL;
1905 for (i = 0; i < sizeof matchparser_tab / sizeof matchparser_tab[0]; i++) {
1906 if (matchparser_tab[i].id == matcher->matchtype)
1907 matchtype_str = matchparser_tab[i].str;
1910 if (matchtype_str == NULL)
1913 switch (matcher->matchtype) {
1914 case MATCHTYPE_MATCH:
1915 case MATCHTYPE_MATCHCASE:
1916 case MATCHTYPE_REGEXP:
1917 case MATCHTYPE_REGEXPCASE:
1918 quoted_expr = matcher_quote_str(matcher->expr);
1919 if (matcher->header) {
1920 quoted_header = matcher_quote_str(matcher->header);
1921 matcher_str = g_strdup_printf
1922 ("%s \"%s\" %s \"%s\"",
1923 criteria_str, quoted_header,
1924 matchtype_str, quoted_expr);
1925 g_free(quoted_header);
1928 matcher_str = g_strdup_printf
1929 ("%s %s \"%s\"", criteria_str,
1930 matchtype_str, quoted_expr);
1931 g_free(quoted_expr);
1939 *\brief Convert a list of conditions to a string
1941 *\param matchers List of conditions
1943 *\return gchar * Newly allocated string
1945 gchar *matcherlist_to_string(const MatcherList *matchers)
1951 gchar *result = NULL;
1953 count = g_slist_length(matchers->matchers);
1954 vstr = g_new(gchar *, count + 1);
1956 for (l = matchers->matchers, cur_str = vstr; l != NULL;
1957 l = g_slist_next(l), cur_str ++) {
1958 *cur_str = matcherprop_to_string((MatcherProp *) l->data);
1959 if (*cur_str == NULL)
1964 if (matchers->bool_and)
1965 result = g_strjoinv(" & ", vstr);
1967 result = g_strjoinv(" | ", vstr);
1969 for (cur_str = vstr ; *cur_str != NULL ; cur_str ++)
1977 #define STRLEN_ZERO(s) ((s) ? strlen(s) : 0)
1978 #define STRLEN_DEFAULT(s,d) ((s) ? strlen(s) : STRLEN_ZERO(d))
1980 static void add_str_default(gchar ** dest,
1981 const gchar * s, const gchar * d)
1983 gchar quoted_str[4096];
1991 quote_cmd_argument(quoted_str, sizeof(quoted_str), str);
1992 strcpy(* dest, quoted_str);
1994 (* dest) += strlen(* dest);
1997 /* matching_build_command() - preferably cmd should be unescaped */
1999 *\brief Build the command line to execute
2001 *\param cmd String with command line specifiers
2002 *\param info Message info to use for command
2004 *\return gchar * Newly allocated string
2006 gchar *matching_build_command(const gchar *cmd, MsgInfo *info)
2008 const gchar *s = cmd;
2009 gchar *filename = NULL;
2010 gchar *processed_cmd;
2014 const gchar *const no_subject = _("(none)") ;
2015 const gchar *const no_from = _("(none)") ;
2016 const gchar *const no_to = _("(none)") ;
2017 const gchar *const no_cc = _("(none)") ;
2018 const gchar *const no_date = _("(none)") ;
2019 const gchar *const no_msgid = _("(none)") ;
2020 const gchar *const no_newsgroups = _("(none)") ;
2021 const gchar *const no_references = _("(none)") ;
2023 size = STRLEN_ZERO(cmd) + 1;
2024 while (*s != '\0') {
2031 case 's': /* subject */
2032 size += STRLEN_DEFAULT(info->subject, no_subject) - 2;
2034 case 'f': /* from */
2035 size += STRLEN_DEFAULT(info->from, no_from) - 2;
2038 size += STRLEN_DEFAULT(info->to, no_to) - 2;
2041 size += STRLEN_DEFAULT(info->cc, no_cc) - 2;
2043 case 'd': /* date */
2044 size += STRLEN_DEFAULT(info->date, no_date) - 2;
2046 case 'i': /* message-id */
2047 size += STRLEN_DEFAULT(info->msgid, no_msgid) - 2;
2049 case 'n': /* newsgroups */
2050 size += STRLEN_DEFAULT(info->newsgroups, no_newsgroups) - 2;
2052 case 'r': /* references */
2053 /* FIXME: using the inreplyto header for reference */
2054 size += STRLEN_DEFAULT(info->inreplyto, no_references) - 2;
2056 case 'F': /* file */
2057 if (filename == NULL)
2058 filename = folder_item_fetch_msg(info->folder, info->msgnum);
2060 if (filename == NULL) {
2061 g_warning("filename is not set");
2065 size += strlen(filename) - 2;
2074 /* as the string can be quoted, we double the result */
2077 processed_cmd = g_new0(gchar, size);
2081 while (*s != '\0') {
2089 case 's': /* subject */
2090 add_str_default(&p, info->subject,
2093 case 'f': /* from */
2094 add_str_default(&p, info->from,
2098 add_str_default(&p, info->to,
2102 add_str_default(&p, info->cc,
2105 case 'd': /* date */
2106 add_str_default(&p, info->date,
2109 case 'i': /* message-id */
2110 add_str_default(&p, info->msgid,
2113 case 'n': /* newsgroups */
2114 add_str_default(&p, info->newsgroups,
2117 case 'r': /* references */
2118 /* FIXME: using the inreplyto header for references */
2119 add_str_default(&p, info->inreplyto, no_references);
2121 case 'F': /* file */
2122 if (filename != NULL)
2123 add_str_default(&p, filename, NULL);
2142 return processed_cmd;
2144 #undef STRLEN_DEFAULT
2147 /* ************************************************************ */
2151 *\brief Write filtering list to file
2154 *\param prefs_filtering List of filtering conditions
2156 static int prefs_filtering_write(FILE *fp, GSList *prefs_filtering)
2160 for (cur = prefs_filtering; cur != NULL; cur = cur->next) {
2161 gchar *filtering_str = NULL;
2162 gchar *tmp_name = NULL;
2163 FilteringProp *prop = NULL;
2165 if (NULL == (prop = (FilteringProp *) cur->data))
2168 if (NULL == (filtering_str = filteringprop_to_string(prop)))
2171 if (prop->enabled) {
2172 if (fputs("enabled ", fp) == EOF) {
2173 FILE_OP_ERROR("filtering config", "fputs");
2177 if (fputs("disabled ", fp) == EOF) {
2178 FILE_OP_ERROR("filtering config", "fputs");
2183 if (fputs("rulename \"", fp) == EOF) {
2184 FILE_OP_ERROR("filtering config", "fputs");
2185 g_free(filtering_str);
2188 tmp_name = prop->name;
2189 while (tmp_name && *tmp_name != '\0') {
2190 if (*tmp_name != '"') {
2191 if (fputc(*tmp_name, fp) == EOF) {
2192 FILE_OP_ERROR("filtering config", "fputs || fputc");
2193 g_free(filtering_str);
2196 } else if (*tmp_name == '"') {
2197 if (fputc('\\', fp) == EOF ||
2198 fputc('"', fp) == EOF) {
2199 FILE_OP_ERROR("filtering config", "fputs || fputc");
2200 g_free(filtering_str);
2206 if (fputs("\" ", fp) == EOF) {
2207 FILE_OP_ERROR("filtering config", "fputs");
2208 g_free(filtering_str);
2212 if (prop->account_id != 0) {
2215 tmp = g_strdup_printf("account %d ", prop->account_id);
2216 if (fputs(tmp, fp) == EOF) {
2217 FILE_OP_ERROR("filtering config", "fputs");
2224 if(fputs(filtering_str, fp) == EOF ||
2225 fputc('\n', fp) == EOF) {
2226 FILE_OP_ERROR("filtering config", "fputs || fputc");
2227 g_free(filtering_str);
2230 g_free(filtering_str);
2236 typedef struct _NodeLoopData {
2242 *\brief Write matchers from a folder item
2244 *\param node Node with folder info
2245 *\param data File pointer
2247 *\return gboolean FALSE
2249 static gboolean prefs_matcher_write_func(GNode *node, gpointer d)
2252 NodeLoopData *data = (NodeLoopData *)d;
2254 GSList *prefs_filtering;
2257 /* prevent warning */
2258 if (item->path == NULL)
2260 id = folder_item_get_identifier(item);
2263 prefs_filtering = item->prefs->processing;
2265 if (prefs_filtering != NULL) {
2266 if (fprintf(data->fp, "[%s]\n", id) < 0) {
2270 if (prefs_filtering_write(data->fp, prefs_filtering) < 0) {
2274 if (fputc('\n', data->fp) == EOF) {
2286 *\brief Save matchers from folder items
2290 static int prefs_matcher_save(FILE *fp)
2298 for (cur = folder_get_list() ; cur != NULL ; cur = g_list_next(cur)) {
2301 folder = (Folder *) cur->data;
2302 g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
2303 prefs_matcher_write_func, &data);
2306 if (data.error == TRUE)
2309 /* pre global rules */
2310 if (fprintf(fp, "[preglobal]\n") < 0 ||
2311 prefs_filtering_write(fp, pre_global_processing) < 0 ||
2312 fputc('\n', fp) == EOF)
2315 /* post global rules */
2316 if (fprintf(fp, "[postglobal]\n") < 0 ||
2317 prefs_filtering_write(fp, post_global_processing) < 0 ||
2318 fputc('\n', fp) == EOF)
2321 /* filtering rules */
2322 if (fprintf(fp, "[filtering]\n") < 0 ||
2323 prefs_filtering_write(fp, filtering_rules) < 0 ||
2324 fputc('\n', fp) == EOF)
2331 *\brief Write filtering / matcher configuration file
2333 void prefs_matcher_write_config(void)
2338 debug_print("Writing matcher configuration...\n");
2340 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
2343 if ((pfile = prefs_write_open(rcpath)) == NULL) {
2344 g_warning("failed to write configuration to file\n");
2351 if (prefs_matcher_save(pfile->fp) < 0) {
2352 g_warning("failed to write configuration to file\n");
2353 prefs_file_close_revert(pfile);
2354 } else if (prefs_file_close(pfile) < 0) {
2355 g_warning("failed to save configuration to file\n");
2359 /* ******************************************************************* */
2361 static void matcher_add_rulenames(const gchar *rcpath)
2363 gchar *newpath = g_strconcat(rcpath, ".new", NULL);
2364 FILE *src = g_fopen(rcpath, "rb");
2365 FILE *dst = g_fopen(newpath, "wb");
2366 gchar buf[BUFFSIZE];
2374 while (fgets (buf, sizeof(buf), src) != NULL) {
2375 if (strlen(buf) > 2 && buf[0] != '['
2376 && strncmp(buf, "rulename \"", 10)
2377 && strncmp(buf, "enabled rulename \"", 18)
2378 && strncmp(buf, "disabled rulename \"", 18)) {
2379 r = fwrite("enabled rulename \"\" ",
2380 strlen("enabled rulename \"\" "), 1, dst);
2382 r = fwrite(buf, strlen(buf), 1, dst);
2386 move_file(newpath, rcpath, TRUE);
2391 *\brief Read matcher configuration
2393 void prefs_matcher_read_config(void)
2396 gchar *rc_old_format;
2399 create_matchparser_hashtab();
2400 prefs_filtering_clear();
2402 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MATCHER_RC, NULL);
2403 rc_old_format = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MATCHER_RC,
2404 ".pre_names", NULL);
2406 if (!is_file_exist(rc_old_format) && is_file_exist(rcpath)) {
2407 /* backup file with no rules names, in case
2408 * anything goes wrong */
2409 copy_file(rcpath, rc_old_format, FALSE);
2410 /* now hack the file in order to have it to the new format */
2411 matcher_add_rulenames(rcpath);
2414 g_free(rc_old_format);
2416 f = g_fopen(rcpath, "rb");
2420 matcher_parser_start_parsing(f);
2421 fclose(matcher_parserin);
2424 /* previous version compatibility */
2426 /* g_print("reading filtering\n"); */
2427 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
2428 FILTERING_RC, NULL);
2429 f = g_fopen(rcpath, "rb");
2433 matcher_parser_start_parsing(f);
2434 fclose(matcher_parserin);
2437 /* g_print("reading scoring\n"); */
2438 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
2440 f = g_fopen(rcpath, "rb");
2444 matcher_parser_start_parsing(f);
2445 fclose(matcher_parserin);