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 Free a matcher structure
277 *\param prop Pointer to matcher structure allocated with
280 void matcherprop_free(MatcherProp *prop)
283 g_free(prop->header);
284 if (prop->preg != NULL) {
292 *\brief Copy a matcher structure
294 *\param src Matcher structure to copy
296 *\return MatcherProp * Pointer to newly allocated matcher structure
298 MatcherProp *matcherprop_copy(const MatcherProp *src)
300 MatcherProp *prop = g_new0(MatcherProp, 1);
302 prop->criteria = src->criteria;
303 prop->header = src->header ? g_strdup(src->header) : NULL;
304 prop->expr = src->expr ? g_strdup(src->expr) : NULL;
305 prop->matchtype = src->matchtype;
307 prop->preg = NULL; /* will be re-evaluated */
308 prop->value = src->value;
309 prop->error = src->error;
313 /* ************** match ******************************/
315 static gboolean match_with_addresses_in_addressbook
316 (MatcherProp *prop, GSList *address_list, gint type,
317 gchar* folderpath, gint match)
320 gboolean found = FALSE;
323 g_return_val_if_fail(address_list != NULL, FALSE);
325 debug_print("match_with_addresses_in_addressbook(%d, %s)\n",
326 g_slist_length(address_list), folderpath?folderpath:"(null)");
328 if (folderpath == NULL ||
329 strcasecmp(folderpath, _("Any")) == 0 ||
335 start_address_completion(path);
337 for (walk = address_list; walk != NULL; walk = walk->next) {
338 /* exact matching of email address */
339 guint num_addr = complete_address(walk->data);
342 /* skip first item (this is the search string itself) */
344 for (; i < num_addr && !found; i++) {
345 gchar *addr = get_complete_address(i);
346 extract_address(addr);
347 if (strcasecmp(addr, walk->data) == 0) {
351 if (debug_filtering_session
352 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
353 log_print(LOG_DEBUG_FILTERING,
354 "address [ %s ] matches\n",
355 (gchar *)walk->data);
362 if (debug_filtering_session
363 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH
365 log_print(LOG_DEBUG_FILTERING,
366 "address [ %s ] doesn't match\n",
367 (gchar *)walk->data);
371 if (match == MATCH_ALL) {
372 /* if matching all addresses, stop if one doesn't match */
375 if (debug_filtering_session
376 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
377 log_print(LOG_DEBUG_FILTERING,
378 "not all address match (matching all)\n");
382 } else if (match == MATCH_ANY) {
383 /* if matching any address, stop if one does match */
386 if (debug_filtering_session
387 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
388 log_print(LOG_DEBUG_FILTERING,
389 "at least one address matches (matching any)\n");
394 /* MATCH_ONE: there should be only one loop iteration */
397 end_address_completion();
403 *\brief Find out if a string matches a condition
405 *\param prop Matcher structure
406 *\param str String to check
408 *\return gboolean TRUE if str matches the condition in the
411 static gboolean matcherprop_string_match(MatcherProp *prop, const gchar *str,
412 const gchar *debug_context)
415 gboolean ret = FALSE;
416 gboolean should_free = FALSE;
420 if (prop->matchtype == MATCHTYPE_REGEXPCASE ||
421 prop->matchtype == MATCHTYPE_MATCHCASE) {
422 str1 = g_utf8_casefold(str, -1);
429 switch (prop->matchtype) {
430 case MATCHTYPE_REGEXPCASE:
431 case MATCHTYPE_REGEXP:
432 if (!prop->preg && (prop->error == 0)) {
433 prop->preg = g_new0(regex_t, 1);
434 /* if regexp then don't use the escaped string */
435 if (regcomp(prop->preg, prop->expr,
436 REG_NOSUB | REG_EXTENDED
437 | ((prop->matchtype == MATCHTYPE_REGEXPCASE)
438 ? REG_ICASE : 0)) != 0) {
444 if (prop->preg == NULL)
447 if (regexec(prop->preg, str1, 0, NULL, 0) == 0)
453 if (debug_filtering_session
454 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
455 gchar *stripped = g_strdup(str);
457 strretchomp(stripped);
459 log_print(LOG_DEBUG_FILTERING,
460 "%s value [ %s ] matches regular expression [ %s ] (%s)\n",
461 debug_context, stripped, prop->expr,
462 prop->matchtype == MATCHTYPE_REGEXP ? _("Case sensitive"):_("Case unsensitive"));
464 log_print(LOG_DEBUG_FILTERING,
465 "%s value [ %s ] doesn't matches regular expression [ %s ] (%s)\n",
466 debug_context, stripped, prop->expr,
467 prop->matchtype == MATCHTYPE_REGEXP ? _("Case sensitive"):_("Case unsensitive"));
473 case MATCHTYPE_MATCHCASE:
474 case MATCHTYPE_MATCH:
475 ret = (strstr(str1, prop->expr) != NULL);
478 if (debug_filtering_session
479 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
480 gchar *stripped = g_strdup(str);
482 strretchomp(stripped);
484 log_print(LOG_DEBUG_FILTERING,
485 "%s value [ %s ] contains [ %s ] (%s)\n",
486 debug_context, stripped, prop->expr,
487 prop->matchtype == MATCHTYPE_MATCH ? _("Case sensitive"):_("Case unsensitive"));
489 log_print(LOG_DEBUG_FILTERING,
490 "%s value [ %s ] doesn't contains [ %s ] (%s)\n",
491 debug_context, stripped, prop->expr,
492 prop->matchtype == MATCHTYPE_MATCH ? _("Case sensitive"):_("Case unsensitive"));
509 *\brief Find out if a tag matches a condition
511 *\param prop Matcher structure
512 *\param msginfo message to check
514 *\return gboolean TRUE if msginfo matches the condition in the
517 static gboolean matcherprop_tag_match(MatcherProp *prop, MsgInfo *msginfo,
518 const gchar *debug_context)
520 gboolean ret = FALSE;
523 if (msginfo == NULL || msginfo->tags == NULL)
526 for (cur = msginfo->tags; cur; cur = cur->next) {
527 const gchar *str = tags_get_tag(GPOINTER_TO_INT(cur->data));
530 if (matcherprop_string_match(prop, str, debug_context)) {
539 *\brief Find out if the string-ed list matches a condition
541 *\param prop Matcher structure
542 *\param list GSList of strings to check
544 *\return gboolean TRUE if str matches the condition in the
547 static gboolean matcherprop_list_match(MatcherProp *prop, const GSList *list,
548 const gchar *debug_context)
552 for(cur = list; cur != NULL; cur = cur->next) {
553 if (matcherprop_string_match(prop, (gchar *)cur->data, debug_context))
559 /* FIXME body search is a hack. */
560 static gboolean matcherprop_string_decode_match(MatcherProp *prop, const gchar *str,
561 const gchar *debug_context)
565 gboolean res = FALSE;
570 /* we try to decode QP first, because it's faster than base64 */
571 qp_decode_const(tmp, BUFFSIZE-1, str);
572 if (!g_utf8_validate(tmp, -1, NULL)) {
573 utf = conv_codeset_strdup
574 (tmp, conv_get_locale_charset_str_no_utf8(),
576 res = matcherprop_string_match(prop, utf, debug_context);
579 res = matcherprop_string_match(prop, tmp, debug_context);
582 if (res == FALSE && (strchr(prop->expr, '=') || strchr(prop->expr, '_')
583 || strchr(str, '=') || strchr(str, '_'))) {
584 /* if searching for something with an equal char, maybe
585 * we should try to match the non-decoded string.
586 * In case it was not qp-encoded. */
587 if (!g_utf8_validate(str, -1, NULL)) {
588 utf = conv_codeset_strdup
589 (str, conv_get_locale_charset_str_no_utf8(),
591 res = matcherprop_string_match(prop, utf, debug_context);
594 res = matcherprop_string_match(prop, str, debug_context);
598 /* FIXME base64 decoding is too slow, especially since text can
599 * easily be handled as base64. Don't even try now. */
605 typedef struct _thread_data {
612 static void *matcher_test_thread(void *data)
614 thread_data *td = (thread_data *)data;
617 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
618 pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
620 result = system(td->cmd);
621 td->done = TRUE; /* let the caller thread join() */
622 return GINT_TO_POINTER(result);
627 *\brief Execute a command defined in the matcher structure
629 *\param prop Pointer to matcher structure
630 *\param info Pointer to message info structure
632 *\return gboolean TRUE if command was executed succesfully
634 static gboolean matcherprop_match_test(const MatcherProp *prop,
642 thread_data *td = g_new0(thread_data, 1);
644 time_t start_time = time(NULL);
647 file = procmsg_get_message_file(info);
652 cmd = matching_build_command(prop->expr, info);
656 #if (defined USE_PTHREAD && ((defined __GLIBC__ && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 3))) || !defined __GLIBC__))
658 if (debug_filtering_session
659 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
660 log_print(LOG_DEBUG_FILTERING,
661 "starting threaded command [ %s ]\n",
667 if (pthread_create(&pt, PTHREAD_CREATE_JOINABLE,
668 matcher_test_thread, td) != 0)
669 retval = system(cmd);
671 debug_print("waiting for test thread\n");
673 /* don't let the interface freeze while waiting */
675 if (time(NULL) - start_time > 30) {
681 pthread_join(pt, &res);
682 retval = GPOINTER_TO_INT(res);
683 debug_print(" test thread returned %d\n", retval);
688 if (debug_filtering_session
689 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
690 log_print(LOG_DEBUG_FILTERING,
691 "starting synchronous command [ %s ]\n",
695 retval = system(cmd);
697 debug_print("Command exit code: %d\n", retval);
700 if (debug_filtering_session
701 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
702 log_print(LOG_DEBUG_FILTERING,
703 "command returned [ %d ]\n",
708 return (retval == 0);
712 *\brief Check if a message matches the condition in a matcher
715 *\param prop Pointer to matcher structure
716 *\param info Pointer to message info
718 *\return gboolean TRUE if a match
720 gboolean matcherprop_match(MatcherProp *prop,
725 switch(prop->criteria) {
726 case MATCHCRITERIA_ALL:
728 case MATCHCRITERIA_UNREAD:
729 return MSG_IS_UNREAD(info->flags);
730 case MATCHCRITERIA_NOT_UNREAD:
731 return !MSG_IS_UNREAD(info->flags);
732 case MATCHCRITERIA_NEW:
733 return MSG_IS_NEW(info->flags);
734 case MATCHCRITERIA_NOT_NEW:
735 return !MSG_IS_NEW(info->flags);
736 case MATCHCRITERIA_MARKED:
737 return MSG_IS_MARKED(info->flags);
738 case MATCHCRITERIA_NOT_MARKED:
739 return !MSG_IS_MARKED(info->flags);
740 case MATCHCRITERIA_DELETED:
741 return MSG_IS_DELETED(info->flags);
742 case MATCHCRITERIA_NOT_DELETED:
743 return !MSG_IS_DELETED(info->flags);
744 case MATCHCRITERIA_REPLIED:
745 return MSG_IS_REPLIED(info->flags);
746 case MATCHCRITERIA_NOT_REPLIED:
747 return !MSG_IS_REPLIED(info->flags);
748 case MATCHCRITERIA_FORWARDED:
749 return MSG_IS_FORWARDED(info->flags);
750 case MATCHCRITERIA_NOT_FORWARDED:
751 return !MSG_IS_FORWARDED(info->flags);
752 case MATCHCRITERIA_LOCKED:
753 return MSG_IS_LOCKED(info->flags);
754 case MATCHCRITERIA_NOT_LOCKED:
755 return !MSG_IS_LOCKED(info->flags);
756 case MATCHCRITERIA_SPAM:
757 return MSG_IS_SPAM(info->flags);
758 case MATCHCRITERIA_NOT_SPAM:
759 return !MSG_IS_SPAM(info->flags);
760 case MATCHCRITERIA_COLORLABEL:
762 gint color = MSG_GET_COLORLABEL_VALUE(info->flags);
763 gboolean ret = (color == prop->value);
766 if (debug_filtering_session
767 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
769 log_print(LOG_DEBUG_FILTERING,
770 "message color value [ %d ] matches color value [ %d ]\n",
773 log_print(LOG_DEBUG_FILTERING,
774 "message color value [ %d ] doesn't matches color value [ %d ]\n",
780 case MATCHCRITERIA_NOT_COLORLABEL:
782 gint color = MSG_GET_COLORLABEL_VALUE(info->flags);
783 gboolean ret = (color != prop->value);
786 if (debug_filtering_session
787 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
789 log_print(LOG_DEBUG_FILTERING,
790 "message color value [ %d ] matches color value [ %d ]\n",
793 log_print(LOG_DEBUG_FILTERING,
794 "message color value [ %d ] doesn't matches color value [ %d ]\n",
800 case MATCHCRITERIA_IGNORE_THREAD:
801 return MSG_IS_IGNORE_THREAD(info->flags);
802 case MATCHCRITERIA_NOT_IGNORE_THREAD:
803 return !MSG_IS_IGNORE_THREAD(info->flags);
804 case MATCHCRITERIA_WATCH_THREAD:
805 return MSG_IS_WATCH_THREAD(info->flags);
806 case MATCHCRITERIA_NOT_WATCH_THREAD:
807 return !MSG_IS_WATCH_THREAD(info->flags);
808 case MATCHCRITERIA_SUBJECT:
809 return matcherprop_string_match(prop, info->subject,
810 prefs_common_translated_header_name("Subject:"));
811 case MATCHCRITERIA_NOT_SUBJECT:
812 return !matcherprop_string_match(prop, info->subject,
813 prefs_common_translated_header_name("Subject:"));
814 case MATCHCRITERIA_FROM:
815 case MATCHCRITERIA_NOT_FROM:
820 context = g_strdup_printf(_("%s header"), prefs_common_translated_header_name("From:"));
821 ret = matcherprop_string_match(prop, info->from, context);
823 return (prop->criteria == MATCHCRITERIA_FROM)? ret : !ret;
825 case MATCHCRITERIA_TO:
826 case MATCHCRITERIA_NOT_TO:
831 context = g_strdup_printf(_("%s header"), prefs_common_translated_header_name("To:"));
832 ret = matcherprop_string_match(prop, info->to, context);
834 return (prop->criteria == MATCHCRITERIA_TO)? ret : !ret;
836 case MATCHCRITERIA_CC:
837 case MATCHCRITERIA_NOT_CC:
842 context = g_strdup_printf(_("%s header"), prefs_common_translated_header_name("Cc:"));
843 ret = matcherprop_string_match(prop, info->cc, context);
845 return (prop->criteria == MATCHCRITERIA_CC)? ret : !ret;
847 case MATCHCRITERIA_TO_OR_CC:
849 gchar *context1, *context2;
852 context1 = g_strdup_printf(_("%s header"), prefs_common_translated_header_name("To:"));
853 context2 = g_strdup_printf(_("%s header"), prefs_common_translated_header_name("Cc:"));
854 ret = matcherprop_string_match(prop, info->to, context1)
855 || matcherprop_string_match(prop, info->cc, context2);
860 case MATCHCRITERIA_NOT_TO_AND_NOT_CC:
862 gchar *context1, *context2;
865 context1 = g_strdup_printf(_("%s header"), prefs_common_translated_header_name("To:"));
866 context2 = g_strdup_printf(_("%s header"), prefs_common_translated_header_name("Cc:"));
867 ret = !(matcherprop_string_match(prop, info->to, context1)
868 || matcherprop_string_match(prop, info->cc, context2));
873 case MATCHCRITERIA_TAG:
874 case MATCHCRITERIA_NOT_TAG:
878 ret = matcherprop_tag_match(prop, info, _("Tag"));
879 return (prop->criteria == MATCHCRITERIA_TAG)? ret : !ret;
881 case MATCHCRITERIA_TAGGED:
882 case MATCHCRITERIA_NOT_TAGGED:
886 ret = (info->tags != NULL);
887 return (prop->criteria == MATCHCRITERIA_TAGGED)? ret : !ret;
889 case MATCHCRITERIA_AGE_GREATER:
895 age = ((t - info->date_t) / (60 * 60 * 24));
896 ret = (age > prop->value);
899 if (debug_filtering_session
900 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
902 log_print(LOG_DEBUG_FILTERING,
903 "message age [ %d ] is greater than [ %d ]\n",
906 log_print(LOG_DEBUG_FILTERING,
907 "message age [ %d ] is not greater than [ %d ]\n",
913 case MATCHCRITERIA_AGE_LOWER:
919 age = ((t - info->date_t) / (60 * 60 * 24));
920 ret = (age < prop->value);
923 if (debug_filtering_session
924 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
926 log_print(LOG_DEBUG_FILTERING,
927 "message age [ %d ] is lower than [ %d ]\n",
930 log_print(LOG_DEBUG_FILTERING,
931 "message age [ %d ] is not lower than [ %d ]\n",
937 case MATCHCRITERIA_SCORE_GREATER:
939 gboolean ret = (info->score > prop->value);
942 if (debug_filtering_session
943 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
945 log_print(LOG_DEBUG_FILTERING,
946 "message score [ %d ] is greater than [ %d ]\n",
947 info->score, prop->value);
949 log_print(LOG_DEBUG_FILTERING,
950 "message score [ %d ] is not greater than [ %d ]\n",
951 info->score, prop->value);
956 case MATCHCRITERIA_SCORE_LOWER:
958 gboolean ret = (info->score < prop->value);
961 if (debug_filtering_session
962 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
964 log_print(LOG_DEBUG_FILTERING,
965 "message score [ %d ] is lower than [ %d ]\n",
966 info->score, prop->value);
968 log_print(LOG_DEBUG_FILTERING,
969 "message score [ %d ] is not lower than [ %d ]\n",
970 info->score, prop->value);
975 case MATCHCRITERIA_SCORE_EQUAL:
977 gboolean ret = (info->score == prop->value);
980 if (debug_filtering_session
981 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
983 log_print(LOG_DEBUG_FILTERING,
984 "message score [ %d ] is equal to [ %d ]\n",
985 info->score, prop->value);
987 log_print(LOG_DEBUG_FILTERING,
988 "message score [ %d ] is not equal to [ %d ]\n",
989 info->score, prop->value);
994 case MATCHCRITERIA_SIZE_GREATER:
996 /* FIXME: info->size is an off_t */
997 gboolean ret = (info->size > (off_t) prop->value);
1000 if (debug_filtering_session
1001 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
1003 log_print(LOG_DEBUG_FILTERING,
1004 "message size [ %ld ] is greater than [ %d ]\n",
1005 info->size, prop->value);
1007 log_print(LOG_DEBUG_FILTERING,
1008 "message size [ %ld ] is not greater than [ %d ]\n",
1009 info->size, prop->value);
1014 case MATCHCRITERIA_SIZE_SMALLER:
1016 /* FIXME: info->size is an off_t */
1017 gboolean ret = (info->size < (off_t) 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 size [ %ld ] is smaller than [ %d ]\n",
1025 info->size, prop->value);
1027 log_print(LOG_DEBUG_FILTERING,
1028 "message size [ %ld ] is not smaller than [ %d ]\n",
1029 info->size, prop->value);
1034 case MATCHCRITERIA_SIZE_EQUAL:
1036 /* FIXME: info->size is an off_t */
1037 gboolean ret = (info->size == (off_t) 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 [ %ld ] is equal to [ %d ]\n",
1045 info->size, prop->value);
1047 log_print(LOG_DEBUG_FILTERING,
1048 "message size [ %ld ] is not equal to [ %d ]\n",
1049 info->size, prop->value);
1054 case MATCHCRITERIA_PARTIAL:
1056 /* FIXME: info->size is an off_t */
1057 gboolean ret = (info->total_size != 0 && info->size != (off_t)info->total_size);
1060 if (debug_filtering_session
1061 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
1063 log_print(LOG_DEBUG_FILTERING,
1064 "message is partially downloaded, size [ %ld ] is less than total size [ %d ])\n",
1065 info->size, info->total_size);
1067 log_print(LOG_DEBUG_FILTERING,
1068 "message is not partially downloaded\n");
1073 case MATCHCRITERIA_NOT_PARTIAL:
1075 /* FIXME: info->size is an off_t */
1076 gboolean ret = (info->total_size == 0 || info->size == (off_t)info->total_size);
1079 if (debug_filtering_session
1080 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
1082 log_print(LOG_DEBUG_FILTERING,
1083 "message is not partially downloaded\n");
1085 log_print(LOG_DEBUG_FILTERING,
1086 "message is partially downloaded, size [ %ld ] is less than total size [ %d ])\n",
1087 info->size, info->total_size);
1092 case MATCHCRITERIA_NEWSGROUPS:
1093 case MATCHCRITERIA_NOT_NEWSGROUPS:
1098 context = g_strdup_printf(_("%s header"),
1099 prefs_common_translated_header_name("Newsgroups:"));
1100 ret = matcherprop_string_match(prop, info->newsgroups, context);
1102 return (prop->criteria == MATCHCRITERIA_NEWSGROUPS)? ret : !ret;
1104 case MATCHCRITERIA_INREPLYTO:
1105 case MATCHCRITERIA_NOT_INREPLYTO:
1110 context = g_strdup_printf(_("%s header"),
1111 prefs_common_translated_header_name("In-Reply-To:"));
1112 ret = matcherprop_string_match(prop, info->inreplyto, context);
1114 return (prop->criteria == MATCHCRITERIA_INREPLYTO)? ret : !ret;
1116 case MATCHCRITERIA_REFERENCES:
1117 case MATCHCRITERIA_NOT_REFERENCES:
1122 context = g_strdup_printf(_("%s header"),
1123 prefs_common_translated_header_name("References:"));
1124 ret = matcherprop_list_match(prop, info->references, context);
1126 return (prop->criteria == MATCHCRITERIA_REFERENCES)? ret : !ret;
1128 case MATCHCRITERIA_TEST:
1129 return matcherprop_match_test(prop, info);
1130 case MATCHCRITERIA_NOT_TEST:
1131 return !matcherprop_match_test(prop, info);
1137 /* ********************* MatcherList *************************** */
1140 *\brief Create a new list of matchers
1142 *\param matchers List of matcher structures
1143 *\param bool_and Operator
1145 *\return MatcherList * New list
1147 MatcherList *matcherlist_new(GSList *matchers, gboolean bool_and)
1151 cond = g_new0(MatcherList, 1);
1153 cond->matchers = matchers;
1154 cond->bool_and = bool_and;
1160 *\brief Frees a list of matchers
1162 *\param cond List of matchers
1164 void matcherlist_free(MatcherList *cond)
1168 g_return_if_fail(cond);
1169 for (l = cond->matchers ; l != NULL ; l = g_slist_next(l)) {
1170 matcherprop_free((MatcherProp *) l->data);
1176 *\brief Skip all headers in a message file
1178 *\param fp Message file
1180 static void matcherlist_skip_headers(FILE *fp)
1182 gchar buf[BUFFSIZE];
1184 while (procheader_get_one_field(buf, sizeof(buf), fp, NULL) != -1)
1189 *\brief Check if a header matches a matcher condition
1191 *\param matcher Matcher structure to check header for
1192 *\param buf Header name
1194 *\return boolean TRUE if matching header
1196 static gboolean matcherprop_match_one_header(MatcherProp *matcher,
1199 gboolean result = FALSE;
1200 Header *header = NULL;
1202 switch (matcher->criteria) {
1203 case MATCHCRITERIA_HEADER:
1204 case MATCHCRITERIA_NOT_HEADER:
1205 header = procheader_parse_header(buf);
1208 if (procheader_headername_equal(header->name,
1210 if (matcher->criteria == MATCHCRITERIA_HEADER)
1211 result = matcherprop_string_match(matcher, header->body, _("header"));
1213 result = !matcherprop_string_match(matcher, header->body, _("header"));
1214 procheader_header_free(header);
1218 procheader_header_free(header);
1221 case MATCHCRITERIA_HEADERS_PART:
1222 return matcherprop_string_match(matcher, buf, _("header line"));
1223 case MATCHCRITERIA_NOT_HEADERS_PART:
1224 return !matcherprop_string_match(matcher, buf, _("headers line"));
1225 case MATCHCRITERIA_MESSAGE:
1226 return matcherprop_string_decode_match(matcher, buf, _("message line"));
1227 case MATCHCRITERIA_NOT_MESSAGE:
1228 return !matcherprop_string_decode_match(matcher, buf, _("message line"));
1229 case MATCHCRITERIA_FOUND_IN_ADDRESSBOOK:
1230 case MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK:
1232 GSList *address_list = NULL;
1233 gint match = MATCH_ONE;
1234 gboolean found = FALSE;
1236 /* how many address headers are me trying to mach? */
1237 if (strcasecmp(matcher->header, _("Any")) == 0)
1239 else if (strcasecmp(matcher->header, Q_("Filtering Matcher Menu|All")) == 0)
1242 if (match == MATCH_ONE) {
1243 /* matching one address header exactly, is that the right one? */
1244 header = procheader_parse_header(buf);
1246 !procheader_headername_equal(header->name, matcher->header))
1248 address_list = address_list_append(address_list, header->body);
1249 if (address_list == NULL)
1253 header = procheader_parse_header(buf);
1256 /* address header is one of the headers we have to match when checking
1257 for any address header or all address headers? */
1258 if (procheader_headername_equal(header->name, "From") ||
1259 procheader_headername_equal(header->name, "To") ||
1260 procheader_headername_equal(header->name, "Cc") ||
1261 procheader_headername_equal(header->name, "Reply-To") ||
1262 procheader_headername_equal(header->name, "Sender"))
1263 address_list = address_list_append(address_list, header->body);
1264 if (address_list == NULL)
1268 found = match_with_addresses_in_addressbook
1269 (matcher, address_list, matcher->criteria,
1270 matcher->expr, match);
1271 g_slist_free(address_list);
1273 if (matcher->criteria == MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK)
1284 *\brief Check if the matcher structure wants headers to
1287 *\param matcher Matcher structure
1289 *\return gboolean TRUE if the matcher structure describes
1290 * a header match condition
1292 static gboolean matcherprop_criteria_headers(const MatcherProp *matcher)
1294 switch (matcher->criteria) {
1295 case MATCHCRITERIA_HEADER:
1296 case MATCHCRITERIA_NOT_HEADER:
1297 case MATCHCRITERIA_HEADERS_PART:
1298 case MATCHCRITERIA_NOT_HEADERS_PART:
1299 case MATCHCRITERIA_FOUND_IN_ADDRESSBOOK:
1300 case MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK:
1308 *\brief Check if the matcher structure wants the message
1309 * to be matched (just perform an action on any
1312 *\param matcher Matcher structure
1314 *\return gboolean TRUE if matcher condition should match
1317 static gboolean matcherprop_criteria_message(MatcherProp *matcher)
1319 switch (matcher->criteria) {
1320 case MATCHCRITERIA_MESSAGE:
1321 case MATCHCRITERIA_NOT_MESSAGE:
1329 *\brief Check if a list of conditions matches one header in
1332 *\param matchers List of conditions
1333 *\param fp Message file
1335 *\return gboolean TRUE if one of the headers is matched by
1336 * the list of conditions.
1338 static gboolean matcherlist_match_headers(MatcherList *matchers, FILE *fp)
1341 gchar buf[BUFFSIZE];
1343 while (procheader_get_one_field(buf, sizeof(buf), fp, NULL) != -1) {
1344 for (l = matchers->matchers ; l != NULL ; l = g_slist_next(l)) {
1345 MatcherProp *matcher = (MatcherProp *) l->data;
1346 gint match = MATCH_ANY;
1351 /* determine the match range (all, any are our concern here) */
1352 if (matcher->criteria == MATCHCRITERIA_NOT_HEADERS_PART ||
1353 matcher->criteria == MATCHCRITERIA_NOT_MESSAGE) {
1356 } else if (matcher->criteria == MATCHCRITERIA_FOUND_IN_ADDRESSBOOK ||
1357 matcher->criteria == MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK) {
1358 Header *header = NULL;
1360 /* address header is one of the headers we have to match when checking
1361 for any address header or all address headers? */
1362 header = procheader_parse_header(buf);
1364 (procheader_headername_equal(header->name, "From") ||
1365 procheader_headername_equal(header->name, "To") ||
1366 procheader_headername_equal(header->name, "Cc") ||
1367 procheader_headername_equal(header->name, "Reply-To") ||
1368 procheader_headername_equal(header->name, "Sender"))) {
1370 if (strcasecmp(matcher->header, _("Any")) == 0)
1372 else if (strcasecmp(matcher->header, Q_("Filtering Matcher Menu|All")) == 0)
1377 /* further call to matcherprop_match_one_header() can't match
1378 and it irrelevant, so: don't alter the match result */
1383 /* ZERO line must NOT match for the rule to match.
1385 if (match == MATCH_ALL) {
1386 if (matcherprop_match_one_header(matcher, buf)) {
1387 matcher->result = TRUE;
1389 matcher->result = FALSE;
1390 matcher->done = TRUE;
1392 /* else, just one line matching is enough for the rule to match
1394 } else if (matcherprop_criteria_headers(matcher) ||
1395 matcherprop_criteria_message(matcher)) {
1396 if (matcherprop_match_one_header(matcher, buf)) {
1397 matcher->result = TRUE;
1398 matcher->done = TRUE;
1402 /* if the rule matched and the matchers are OR, no need to
1403 * check the others */
1404 if (matcher->result && matcher->done) {
1405 if (!matchers->bool_and)
1415 *\brief Check if a matcher wants to check the message body
1417 *\param matcher Matcher structure
1419 *\return gboolean TRUE if body must be matched.
1421 static gboolean matcherprop_criteria_body(const MatcherProp *matcher)
1423 switch (matcher->criteria) {
1424 case MATCHCRITERIA_BODY_PART:
1425 case MATCHCRITERIA_NOT_BODY_PART:
1433 *\brief Check if a (line) string matches the criteria
1434 * described by a matcher structure
1436 *\param matcher Matcher structure
1439 *\return gboolean TRUE if string matches criteria
1441 static gboolean matcherprop_match_line(MatcherProp *matcher, const gchar *line)
1443 switch (matcher->criteria) {
1444 case MATCHCRITERIA_BODY_PART:
1445 case MATCHCRITERIA_MESSAGE:
1446 return matcherprop_string_decode_match(matcher, line, _("body line"));
1447 case MATCHCRITERIA_NOT_BODY_PART:
1448 case MATCHCRITERIA_NOT_MESSAGE:
1449 return !matcherprop_string_decode_match(matcher, line, _("body line"));
1455 *\brief Check if a line in a message file's body matches
1458 *\param matchers List of conditions
1459 *\param fp Message file
1461 *\return gboolean TRUE if succesful match
1463 static gboolean matcherlist_match_body(MatcherList *matchers, FILE *fp)
1466 gchar buf[BUFFSIZE];
1468 while (fgets(buf, sizeof(buf), fp) != NULL) {
1469 for (l = matchers->matchers ; l != NULL ; l = g_slist_next(l)) {
1470 MatcherProp *matcher = (MatcherProp *) l->data;
1475 /* if the criteria is ~body_part or ~message, ZERO lines
1476 * must NOT match for the rule to match. */
1477 if (matcher->criteria == MATCHCRITERIA_NOT_BODY_PART ||
1478 matcher->criteria == MATCHCRITERIA_NOT_MESSAGE) {
1479 if (matcherprop_match_line(matcher, buf)) {
1480 matcher->result = TRUE;
1482 matcher->result = FALSE;
1483 matcher->done = TRUE;
1485 /* else, just one line has to match */
1486 } else if (matcherprop_criteria_body(matcher) ||
1487 matcherprop_criteria_message(matcher)) {
1488 if (matcherprop_match_line(matcher, buf)) {
1489 matcher->result = TRUE;
1490 matcher->done = TRUE;
1494 /* if the matchers are OR'ed and the rule matched,
1495 * no need to check the others. */
1496 if (matcher->result && matcher->done) {
1497 if (!matchers->bool_and)
1506 *\brief Check if a message file matches criteria
1508 *\param matchers Criteria
1509 *\param info Message info
1510 *\param result Default result
1512 *\return gboolean TRUE if matched
1514 static gboolean matcherlist_match_file(MatcherList *matchers, MsgInfo *info,
1517 gboolean read_headers;
1523 /* file need to be read ? */
1525 read_headers = FALSE;
1527 for (l = matchers->matchers ; l != NULL ; l = g_slist_next(l)) {
1528 MatcherProp *matcher = (MatcherProp *) l->data;
1530 if (matcherprop_criteria_headers(matcher))
1531 read_headers = TRUE;
1532 if (matcherprop_criteria_body(matcher))
1534 if (matcherprop_criteria_message(matcher)) {
1535 read_headers = TRUE;
1538 matcher->result = FALSE;
1539 matcher->done = FALSE;
1542 if (!read_headers && !read_body)
1545 file = procmsg_get_message_file_full(info, read_headers, read_body);
1549 if ((fp = g_fopen(file, "rb")) == NULL) {
1550 FILE_OP_ERROR(file, "fopen");
1555 /* read the headers */
1558 if (matcherlist_match_headers(matchers, fp))
1561 matcherlist_skip_headers(fp);
1566 matcherlist_match_body(matchers, fp);
1569 for (l = matchers->matchers; l != NULL; l = g_slist_next(l)) {
1570 MatcherProp *matcher = (MatcherProp *) l->data;
1572 if (matcherprop_criteria_headers(matcher) ||
1573 matcherprop_criteria_body(matcher) ||
1574 matcherprop_criteria_message(matcher)) {
1575 if (matcher->result) {
1576 if (!matchers->bool_and) {
1582 if (matchers->bool_and) {
1598 *\brief Test list of conditions on a message.
1600 *\param matchers List of conditions
1601 *\param info Message info
1603 *\return gboolean TRUE if matched
1605 gboolean matcherlist_match(MatcherList *matchers, MsgInfo *info)
1613 if (matchers->bool_and)
1618 /* test the cached elements */
1620 for (l = matchers->matchers; l != NULL ;l = g_slist_next(l)) {
1621 MatcherProp *matcher = (MatcherProp *) l->data;
1623 if (debug_filtering_session) {
1624 gchar *buf = matcherprop_to_string(matcher);
1625 log_print(LOG_DEBUG_FILTERING, _("checking if message matches [ %s ]\n"), buf);
1629 switch(matcher->criteria) {
1630 case MATCHCRITERIA_ALL:
1631 case MATCHCRITERIA_UNREAD:
1632 case MATCHCRITERIA_NOT_UNREAD:
1633 case MATCHCRITERIA_NEW:
1634 case MATCHCRITERIA_NOT_NEW:
1635 case MATCHCRITERIA_MARKED:
1636 case MATCHCRITERIA_NOT_MARKED:
1637 case MATCHCRITERIA_DELETED:
1638 case MATCHCRITERIA_NOT_DELETED:
1639 case MATCHCRITERIA_REPLIED:
1640 case MATCHCRITERIA_NOT_REPLIED:
1641 case MATCHCRITERIA_FORWARDED:
1642 case MATCHCRITERIA_NOT_FORWARDED:
1643 case MATCHCRITERIA_LOCKED:
1644 case MATCHCRITERIA_NOT_LOCKED:
1645 case MATCHCRITERIA_SPAM:
1646 case MATCHCRITERIA_NOT_SPAM:
1647 case MATCHCRITERIA_COLORLABEL:
1648 case MATCHCRITERIA_NOT_COLORLABEL:
1649 case MATCHCRITERIA_IGNORE_THREAD:
1650 case MATCHCRITERIA_NOT_IGNORE_THREAD:
1651 case MATCHCRITERIA_WATCH_THREAD:
1652 case MATCHCRITERIA_NOT_WATCH_THREAD:
1653 case MATCHCRITERIA_SUBJECT:
1654 case MATCHCRITERIA_NOT_SUBJECT:
1655 case MATCHCRITERIA_FROM:
1656 case MATCHCRITERIA_NOT_FROM:
1657 case MATCHCRITERIA_TO:
1658 case MATCHCRITERIA_NOT_TO:
1659 case MATCHCRITERIA_CC:
1660 case MATCHCRITERIA_NOT_CC:
1661 case MATCHCRITERIA_TO_OR_CC:
1662 case MATCHCRITERIA_NOT_TO_AND_NOT_CC:
1663 case MATCHCRITERIA_TAG:
1664 case MATCHCRITERIA_NOT_TAG:
1665 case MATCHCRITERIA_TAGGED:
1666 case MATCHCRITERIA_NOT_TAGGED:
1667 case MATCHCRITERIA_AGE_GREATER:
1668 case MATCHCRITERIA_AGE_LOWER:
1669 case MATCHCRITERIA_NEWSGROUPS:
1670 case MATCHCRITERIA_NOT_NEWSGROUPS:
1671 case MATCHCRITERIA_INREPLYTO:
1672 case MATCHCRITERIA_NOT_INREPLYTO:
1673 case MATCHCRITERIA_REFERENCES:
1674 case MATCHCRITERIA_NOT_REFERENCES:
1675 case MATCHCRITERIA_SCORE_GREATER:
1676 case MATCHCRITERIA_SCORE_LOWER:
1677 case MATCHCRITERIA_SCORE_EQUAL:
1678 case MATCHCRITERIA_SIZE_GREATER:
1679 case MATCHCRITERIA_SIZE_SMALLER:
1680 case MATCHCRITERIA_SIZE_EQUAL:
1681 case MATCHCRITERIA_TEST:
1682 case MATCHCRITERIA_NOT_TEST:
1683 case MATCHCRITERIA_PARTIAL:
1684 case MATCHCRITERIA_NOT_PARTIAL:
1685 if (matcherprop_match(matcher, info)) {
1686 if (!matchers->bool_and) {
1687 if (debug_filtering_session)
1688 log_status_ok(LOG_DEBUG_FILTERING, _("message matches\n"));
1693 if (matchers->bool_and) {
1694 if (debug_filtering_session)
1695 log_status_nok(LOG_DEBUG_FILTERING, _("message does not match\n"));
1702 /* test the condition on the file */
1704 if (matcherlist_match_file(matchers, info, result)) {
1705 if (!matchers->bool_and) {
1706 if (debug_filtering_session)
1707 log_status_ok(LOG_DEBUG_FILTERING, _("message matches\n"));
1711 if (matchers->bool_and) {
1712 if (debug_filtering_session)
1713 log_status_nok(LOG_DEBUG_FILTERING, _("message does not match\n"));
1718 if (debug_filtering_session) {
1720 log_status_ok(LOG_DEBUG_FILTERING, _("message matches\n"));
1722 log_status_nok(LOG_DEBUG_FILTERING, _("message does not match\n"));
1728 static gint quote_filter_str(gchar * result, guint size,
1738 for(p = path ; * p != '\0' ; p ++) {
1740 if ((* p != '\"') && (* p != '\\')) {
1741 if (remaining > 0) {
1747 result[size - 1] = '\0';
1752 if (remaining >= 2) {
1760 result[size - 1] = '\0';
1765 if (remaining > 0) {
1769 result[size - 1] = '\0';
1777 gchar * matcher_quote_str(const gchar * src)
1782 len = strlen(src) * 2 + 1;
1783 res = g_malloc(len);
1784 quote_filter_str(res, len, src);
1790 *\brief Convert a matcher structure to a string
1792 *\param matcher Matcher structure
1794 *\return gchar * Newly allocated string
1796 gchar *matcherprop_to_string(MatcherProp *matcher)
1798 gchar *matcher_str = NULL;
1799 const gchar *criteria_str;
1800 const gchar *matchtype_str;
1802 gchar * quoted_expr;
1803 gchar * quoted_header;
1805 criteria_str = NULL;
1806 for (i = 0; i < (int) (sizeof(matchparser_tab) / sizeof(MatchParser)); i++) {
1807 if (matchparser_tab[i].id == matcher->criteria)
1808 criteria_str = matchparser_tab[i].str;
1810 if (criteria_str == NULL)
1813 switch (matcher->criteria) {
1814 case MATCHCRITERIA_AGE_GREATER:
1815 case MATCHCRITERIA_AGE_LOWER:
1816 case MATCHCRITERIA_SCORE_GREATER:
1817 case MATCHCRITERIA_SCORE_LOWER:
1818 case MATCHCRITERIA_SCORE_EQUAL:
1819 case MATCHCRITERIA_SIZE_GREATER:
1820 case MATCHCRITERIA_SIZE_SMALLER:
1821 case MATCHCRITERIA_SIZE_EQUAL:
1822 case MATCHCRITERIA_COLORLABEL:
1823 case MATCHCRITERIA_NOT_COLORLABEL:
1824 return g_strdup_printf("%s %i", criteria_str, matcher->value);
1825 case MATCHCRITERIA_ALL:
1826 case MATCHCRITERIA_UNREAD:
1827 case MATCHCRITERIA_NOT_UNREAD:
1828 case MATCHCRITERIA_NEW:
1829 case MATCHCRITERIA_NOT_NEW:
1830 case MATCHCRITERIA_MARKED:
1831 case MATCHCRITERIA_NOT_MARKED:
1832 case MATCHCRITERIA_DELETED:
1833 case MATCHCRITERIA_NOT_DELETED:
1834 case MATCHCRITERIA_REPLIED:
1835 case MATCHCRITERIA_NOT_REPLIED:
1836 case MATCHCRITERIA_FORWARDED:
1837 case MATCHCRITERIA_NOT_FORWARDED:
1838 case MATCHCRITERIA_LOCKED:
1839 case MATCHCRITERIA_NOT_LOCKED:
1840 case MATCHCRITERIA_SPAM:
1841 case MATCHCRITERIA_NOT_SPAM:
1842 case MATCHCRITERIA_PARTIAL:
1843 case MATCHCRITERIA_NOT_PARTIAL:
1844 case MATCHCRITERIA_IGNORE_THREAD:
1845 case MATCHCRITERIA_NOT_IGNORE_THREAD:
1846 case MATCHCRITERIA_WATCH_THREAD:
1847 case MATCHCRITERIA_NOT_WATCH_THREAD:
1848 case MATCHCRITERIA_TAGGED:
1849 case MATCHCRITERIA_NOT_TAGGED:
1850 return g_strdup(criteria_str);
1851 case MATCHCRITERIA_TEST:
1852 case MATCHCRITERIA_NOT_TEST:
1853 quoted_expr = matcher_quote_str(matcher->expr);
1854 matcher_str = g_strdup_printf("%s \"%s\"",
1855 criteria_str, quoted_expr);
1856 g_free(quoted_expr);
1858 case MATCHCRITERIA_FOUND_IN_ADDRESSBOOK:
1859 case MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK:
1860 quoted_header = matcher_quote_str(matcher->header);
1861 quoted_expr = matcher_quote_str(matcher->expr);
1862 matcher_str = g_strdup_printf("%s \"%s\" in \"%s\"",
1863 criteria_str, quoted_header, quoted_expr);
1864 g_free(quoted_header);
1865 g_free(quoted_expr);
1869 matchtype_str = NULL;
1870 for (i = 0; i < sizeof matchparser_tab / sizeof matchparser_tab[0]; i++) {
1871 if (matchparser_tab[i].id == matcher->matchtype)
1872 matchtype_str = matchparser_tab[i].str;
1875 if (matchtype_str == NULL)
1878 switch (matcher->matchtype) {
1879 case MATCHTYPE_MATCH:
1880 case MATCHTYPE_MATCHCASE:
1881 case MATCHTYPE_REGEXP:
1882 case MATCHTYPE_REGEXPCASE:
1883 quoted_expr = matcher_quote_str(matcher->expr);
1884 if (matcher->header) {
1885 quoted_header = matcher_quote_str(matcher->header);
1886 matcher_str = g_strdup_printf
1887 ("%s \"%s\" %s \"%s\"",
1888 criteria_str, quoted_header,
1889 matchtype_str, quoted_expr);
1890 g_free(quoted_header);
1893 matcher_str = g_strdup_printf
1894 ("%s %s \"%s\"", criteria_str,
1895 matchtype_str, quoted_expr);
1896 g_free(quoted_expr);
1904 *\brief Convert a list of conditions to a string
1906 *\param matchers List of conditions
1908 *\return gchar * Newly allocated string
1910 gchar *matcherlist_to_string(const MatcherList *matchers)
1916 gchar *result = NULL;
1918 count = g_slist_length(matchers->matchers);
1919 vstr = g_new(gchar *, count + 1);
1921 for (l = matchers->matchers, cur_str = vstr; l != NULL;
1922 l = g_slist_next(l), cur_str ++) {
1923 *cur_str = matcherprop_to_string((MatcherProp *) l->data);
1924 if (*cur_str == NULL)
1929 if (matchers->bool_and)
1930 result = g_strjoinv(" & ", vstr);
1932 result = g_strjoinv(" | ", vstr);
1934 for (cur_str = vstr ; *cur_str != NULL ; cur_str ++)
1942 #define STRLEN_ZERO(s) ((s) ? strlen(s) : 0)
1943 #define STRLEN_DEFAULT(s,d) ((s) ? strlen(s) : STRLEN_ZERO(d))
1945 static void add_str_default(gchar ** dest,
1946 const gchar * s, const gchar * d)
1948 gchar quoted_str[4096];
1956 quote_cmd_argument(quoted_str, sizeof(quoted_str), str);
1957 strcpy(* dest, quoted_str);
1959 (* dest) += strlen(* dest);
1962 /* matching_build_command() - preferably cmd should be unescaped */
1964 *\brief Build the command line to execute
1966 *\param cmd String with command line specifiers
1967 *\param info Message info to use for command
1969 *\return gchar * Newly allocated string
1971 gchar *matching_build_command(const gchar *cmd, MsgInfo *info)
1973 const gchar *s = cmd;
1974 gchar *filename = NULL;
1975 gchar *processed_cmd;
1979 const gchar *const no_subject = _("(none)") ;
1980 const gchar *const no_from = _("(none)") ;
1981 const gchar *const no_to = _("(none)") ;
1982 const gchar *const no_cc = _("(none)") ;
1983 const gchar *const no_date = _("(none)") ;
1984 const gchar *const no_msgid = _("(none)") ;
1985 const gchar *const no_newsgroups = _("(none)") ;
1986 const gchar *const no_references = _("(none)") ;
1988 size = STRLEN_ZERO(cmd) + 1;
1989 while (*s != '\0') {
1996 case 's': /* subject */
1997 size += STRLEN_DEFAULT(info->subject, no_subject) - 2;
1999 case 'f': /* from */
2000 size += STRLEN_DEFAULT(info->from, no_from) - 2;
2003 size += STRLEN_DEFAULT(info->to, no_to) - 2;
2006 size += STRLEN_DEFAULT(info->cc, no_cc) - 2;
2008 case 'd': /* date */
2009 size += STRLEN_DEFAULT(info->date, no_date) - 2;
2011 case 'i': /* message-id */
2012 size += STRLEN_DEFAULT(info->msgid, no_msgid) - 2;
2014 case 'n': /* newsgroups */
2015 size += STRLEN_DEFAULT(info->newsgroups, no_newsgroups) - 2;
2017 case 'r': /* references */
2018 /* FIXME: using the inreplyto header for reference */
2019 size += STRLEN_DEFAULT(info->inreplyto, no_references) - 2;
2021 case 'F': /* file */
2022 if (filename == NULL)
2023 filename = folder_item_fetch_msg(info->folder, info->msgnum);
2025 if (filename == NULL) {
2026 g_warning("filename is not set");
2030 size += strlen(filename) - 2;
2039 /* as the string can be quoted, we double the result */
2042 processed_cmd = g_new0(gchar, size);
2046 while (*s != '\0') {
2054 case 's': /* subject */
2055 add_str_default(&p, info->subject,
2058 case 'f': /* from */
2059 add_str_default(&p, info->from,
2063 add_str_default(&p, info->to,
2067 add_str_default(&p, info->cc,
2070 case 'd': /* date */
2071 add_str_default(&p, info->date,
2074 case 'i': /* message-id */
2075 add_str_default(&p, info->msgid,
2078 case 'n': /* newsgroups */
2079 add_str_default(&p, info->newsgroups,
2082 case 'r': /* references */
2083 /* FIXME: using the inreplyto header for references */
2084 add_str_default(&p, info->inreplyto, no_references);
2086 case 'F': /* file */
2087 if (filename != NULL)
2088 add_str_default(&p, filename, NULL);
2107 return processed_cmd;
2109 #undef STRLEN_DEFAULT
2112 /* ************************************************************ */
2116 *\brief Write filtering list to file
2119 *\param prefs_filtering List of filtering conditions
2121 static int prefs_filtering_write(FILE *fp, GSList *prefs_filtering)
2125 for (cur = prefs_filtering; cur != NULL; cur = cur->next) {
2126 gchar *filtering_str = NULL;
2127 gchar *tmp_name = NULL;
2128 FilteringProp *prop = NULL;
2130 if (NULL == (prop = (FilteringProp *) cur->data))
2133 if (NULL == (filtering_str = filteringprop_to_string(prop)))
2136 if (prop->enabled) {
2137 if (fputs("enabled ", fp) == EOF) {
2138 FILE_OP_ERROR("filtering config", "fputs");
2142 if (fputs("disabled ", fp) == EOF) {
2143 FILE_OP_ERROR("filtering config", "fputs");
2148 if (fputs("rulename \"", fp) == EOF) {
2149 FILE_OP_ERROR("filtering config", "fputs");
2150 g_free(filtering_str);
2153 tmp_name = prop->name;
2154 while (tmp_name && *tmp_name != '\0') {
2155 if (*tmp_name != '"') {
2156 if (fputc(*tmp_name, fp) == EOF) {
2157 FILE_OP_ERROR("filtering config", "fputs || fputc");
2158 g_free(filtering_str);
2161 } else if (*tmp_name == '"') {
2162 if (fputc('\\', fp) == EOF ||
2163 fputc('"', fp) == EOF) {
2164 FILE_OP_ERROR("filtering config", "fputs || fputc");
2165 g_free(filtering_str);
2171 if (fputs("\" ", fp) == EOF) {
2172 FILE_OP_ERROR("filtering config", "fputs");
2173 g_free(filtering_str);
2177 if (prop->account_id != 0) {
2180 tmp = g_strdup_printf("account %d ", prop->account_id);
2181 if (fputs(tmp, fp) == EOF) {
2182 FILE_OP_ERROR("filtering config", "fputs");
2189 if(fputs(filtering_str, fp) == EOF ||
2190 fputc('\n', fp) == EOF) {
2191 FILE_OP_ERROR("filtering config", "fputs || fputc");
2192 g_free(filtering_str);
2195 g_free(filtering_str);
2201 typedef struct _NodeLoopData {
2207 *\brief Write matchers from a folder item
2209 *\param node Node with folder info
2210 *\param data File pointer
2212 *\return gboolean FALSE
2214 static gboolean prefs_matcher_write_func(GNode *node, gpointer d)
2217 NodeLoopData *data = (NodeLoopData *)d;
2219 GSList *prefs_filtering;
2222 /* prevent warning */
2223 if (item->path == NULL)
2225 id = folder_item_get_identifier(item);
2228 prefs_filtering = item->prefs->processing;
2230 if (prefs_filtering != NULL) {
2231 if (fprintf(data->fp, "[%s]\n", id) < 0) {
2235 if (prefs_filtering_write(data->fp, prefs_filtering) < 0) {
2239 if (fputc('\n', data->fp) == EOF) {
2251 *\brief Save matchers from folder items
2255 static int prefs_matcher_save(FILE *fp)
2263 for (cur = folder_get_list() ; cur != NULL ; cur = g_list_next(cur)) {
2266 folder = (Folder *) cur->data;
2267 g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
2268 prefs_matcher_write_func, &data);
2271 if (data.error == TRUE)
2274 /* pre global rules */
2275 if (fprintf(fp, "[preglobal]\n") < 0 ||
2276 prefs_filtering_write(fp, pre_global_processing) < 0 ||
2277 fputc('\n', fp) == EOF)
2280 /* post global rules */
2281 if (fprintf(fp, "[postglobal]\n") < 0 ||
2282 prefs_filtering_write(fp, post_global_processing) < 0 ||
2283 fputc('\n', fp) == EOF)
2286 /* filtering rules */
2287 if (fprintf(fp, "[filtering]\n") < 0 ||
2288 prefs_filtering_write(fp, filtering_rules) < 0 ||
2289 fputc('\n', fp) == EOF)
2296 *\brief Write filtering / matcher configuration file
2298 void prefs_matcher_write_config(void)
2303 debug_print("Writing matcher configuration...\n");
2305 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
2308 if ((pfile = prefs_write_open(rcpath)) == NULL) {
2309 g_warning("failed to write configuration to file\n");
2316 if (prefs_matcher_save(pfile->fp) < 0) {
2317 g_warning("failed to write configuration to file\n");
2318 prefs_file_close_revert(pfile);
2319 } else if (prefs_file_close(pfile) < 0) {
2320 g_warning("failed to save configuration to file\n");
2324 /* ******************************************************************* */
2326 static void matcher_add_rulenames(const gchar *rcpath)
2328 gchar *newpath = g_strconcat(rcpath, ".new", NULL);
2329 FILE *src = g_fopen(rcpath, "rb");
2330 FILE *dst = g_fopen(newpath, "wb");
2331 gchar buf[BUFFSIZE];
2339 while (fgets (buf, sizeof(buf), src) != NULL) {
2340 if (strlen(buf) > 2 && buf[0] != '['
2341 && strncmp(buf, "rulename \"", 10)
2342 && strncmp(buf, "enabled rulename \"", 18)
2343 && strncmp(buf, "disabled rulename \"", 18)) {
2344 r = fwrite("enabled rulename \"\" ",
2345 strlen("enabled rulename \"\" "), 1, dst);
2347 r = fwrite(buf, strlen(buf), 1, dst);
2351 move_file(newpath, rcpath, TRUE);
2356 *\brief Read matcher configuration
2358 void prefs_matcher_read_config(void)
2361 gchar *rc_old_format;
2364 create_matchparser_hashtab();
2365 prefs_filtering_clear();
2367 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MATCHER_RC, NULL);
2368 rc_old_format = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MATCHER_RC,
2369 ".pre_names", NULL);
2371 if (!is_file_exist(rc_old_format) && is_file_exist(rcpath)) {
2372 /* backup file with no rules names, in case
2373 * anything goes wrong */
2374 copy_file(rcpath, rc_old_format, FALSE);
2375 /* now hack the file in order to have it to the new format */
2376 matcher_add_rulenames(rcpath);
2379 g_free(rc_old_format);
2381 f = g_fopen(rcpath, "rb");
2385 matcher_parser_start_parsing(f);
2386 fclose(matcher_parserin);
2389 /* previous version compatibility */
2391 /* g_print("reading filtering\n"); */
2392 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
2393 FILTERING_RC, NULL);
2394 f = g_fopen(rcpath, "rb");
2398 matcher_parser_start_parsing(f);
2399 fclose(matcher_parserin);
2402 /* g_print("reading scoring\n"); */
2403 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
2405 f = g_fopen(rcpath, "rb");
2409 matcher_parser_start_parsing(f);
2410 fclose(matcher_parserin);