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_SPAM, "spam"},
85 {MATCHCRITERIA_NOT_SPAM, "~spam"},
88 {MATCHCRITERIA_SUBJECT, "subject"},
89 {MATCHCRITERIA_NOT_SUBJECT, "~subject"},
90 {MATCHCRITERIA_FROM, "from"},
91 {MATCHCRITERIA_NOT_FROM, "~from"},
92 {MATCHCRITERIA_TO, "to"},
93 {MATCHCRITERIA_NOT_TO, "~to"},
94 {MATCHCRITERIA_CC, "cc"},
95 {MATCHCRITERIA_NOT_CC, "~cc"},
96 {MATCHCRITERIA_TO_OR_CC, "to_or_cc"},
97 {MATCHCRITERIA_NOT_TO_AND_NOT_CC, "~to_or_cc"},
98 {MATCHCRITERIA_TAG, "tag"},
99 {MATCHCRITERIA_NOT_TAG, "~tag"},
100 {MATCHCRITERIA_TAGGED, "tagged"},
101 {MATCHCRITERIA_NOT_TAGGED, "~tagged"},
102 {MATCHCRITERIA_AGE_GREATER, "age_greater"},
103 {MATCHCRITERIA_AGE_LOWER, "age_lower"},
104 {MATCHCRITERIA_NEWSGROUPS, "newsgroups"},
105 {MATCHCRITERIA_NOT_NEWSGROUPS, "~newsgroups"},
106 {MATCHCRITERIA_INREPLYTO, "inreplyto"},
107 {MATCHCRITERIA_NOT_INREPLYTO, "~inreplyto"},
108 {MATCHCRITERIA_REFERENCES, "references"},
109 {MATCHCRITERIA_NOT_REFERENCES, "~references"},
110 {MATCHCRITERIA_SCORE_GREATER, "score_greater"},
111 {MATCHCRITERIA_SCORE_LOWER, "score_lower"},
112 {MATCHCRITERIA_SCORE_EQUAL, "score_equal"},
113 {MATCHCRITERIA_PARTIAL, "partial"},
114 {MATCHCRITERIA_NOT_PARTIAL, "~partial"},
115 {MATCHCRITERIA_FOUND_IN_ADDRESSBOOK, "found_in_addressbook"},
116 {MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK, "~found_in_addressbook"},
118 {MATCHCRITERIA_SIZE_GREATER, "size_greater"},
119 {MATCHCRITERIA_SIZE_SMALLER, "size_smaller"},
120 {MATCHCRITERIA_SIZE_EQUAL, "size_equal"},
122 /* content have to be read */
123 {MATCHCRITERIA_HEADER, "header"},
124 {MATCHCRITERIA_NOT_HEADER, "~header"},
125 {MATCHCRITERIA_HEADERS_PART, "headers_part"},
126 {MATCHCRITERIA_NOT_HEADERS_PART, "~headers_part"},
127 {MATCHCRITERIA_MESSAGE, "message"},
128 {MATCHCRITERIA_NOT_MESSAGE, "~message"},
129 {MATCHCRITERIA_BODY_PART, "body_part"},
130 {MATCHCRITERIA_NOT_BODY_PART, "~body_part"},
131 {MATCHCRITERIA_TEST, "test"},
132 {MATCHCRITERIA_NOT_TEST, "~test"},
135 {MATCHTYPE_MATCHCASE, "matchcase"},
136 {MATCHTYPE_MATCH, "match"},
137 {MATCHTYPE_REGEXPCASE, "regexpcase"},
138 {MATCHTYPE_REGEXP, "regexp"},
141 {MATCHACTION_SCORE, "score"}, /* for backward compatibility */
142 {MATCHACTION_MOVE, "move"},
143 {MATCHACTION_COPY, "copy"},
144 {MATCHACTION_DELETE, "delete"},
145 {MATCHACTION_MARK, "mark"},
146 {MATCHACTION_UNMARK, "unmark"},
147 {MATCHACTION_LOCK, "lock"},
148 {MATCHACTION_UNLOCK, "unlock"},
149 {MATCHACTION_MARK_AS_READ, "mark_as_read"},
150 {MATCHACTION_MARK_AS_UNREAD, "mark_as_unread"},
151 {MATCHACTION_MARK_AS_SPAM, "mark_as_spam"},
152 {MATCHACTION_MARK_AS_HAM, "mark_as_ham"},
153 {MATCHACTION_FORWARD, "forward"},
154 {MATCHACTION_FORWARD_AS_ATTACHMENT, "forward_as_attachment"},
155 {MATCHACTION_EXECUTE, "execute"},
156 {MATCHACTION_COLOR, "color"},
157 {MATCHACTION_REDIRECT, "redirect"},
158 {MATCHACTION_CHANGE_SCORE, "change_score"},
159 {MATCHACTION_SET_SCORE, "set_score"},
160 {MATCHACTION_STOP, "stop"},
161 {MATCHACTION_HIDE, "hide"},
162 {MATCHACTION_IGNORE, "ignore"},
163 {MATCHACTION_ADD_TO_ADDRESSBOOK, "add_to_addressbook"},
164 {MATCHACTION_SET_TAG, "set_tag"},
165 {MATCHACTION_UNSET_TAG, "unset_tag"},
166 {MATCHACTION_CLEAR_TAGS, "clear_tags"},
175 extern gboolean debug_filtering_session;
178 *\brief Look up table with keywords defined in \sa matchparser_tab
180 static GHashTable *matchparser_hashtab;
183 *\brief Translate keyword id to keyword string
185 *\param id Id of keyword
187 *\return const gchar * Keyword
189 const gchar *get_matchparser_tab_str(gint id)
193 for (i = 0; i < sizeof matchparser_tab / sizeof matchparser_tab[0]; i++) {
194 if (matchparser_tab[i].id == id)
195 return matchparser_tab[i].str;
201 *\brief Create keyword lookup table
203 static void create_matchparser_hashtab(void)
207 if (matchparser_hashtab) return;
208 matchparser_hashtab = g_hash_table_new(g_str_hash, g_str_equal);
209 for (i = 0; i < sizeof matchparser_tab / sizeof matchparser_tab[0]; i++)
210 g_hash_table_insert(matchparser_hashtab,
211 matchparser_tab[i].str,
212 (gpointer) &matchparser_tab[i]);
216 *\brief Return a keyword id from a keyword string
218 *\param str Keyword string
220 *\return gint Keyword id
222 gint get_matchparser_tab_id(const gchar *str)
226 if (NULL != (res = g_hash_table_lookup(matchparser_hashtab, str))) {
232 /* **************** data structure allocation **************** */
235 *\brief Allocate a structure for a filtering / scoring
236 * "condition" (a matcher structure)
238 *\param criteria Criteria ID (MATCHCRITERIA_XXXX)
239 *\param header Header string (if criteria is MATCHCRITERIA_HEADER
240 or MATCHCRITERIA_FOUND_IN_ADDRESSBOOK)
241 *\param matchtype Type of action (MATCHTYPE_XXX)
242 *\param expr String value or expression to check
243 *\param value Integer value to check
245 *\return MatcherProp * Pointer to newly allocated structure
247 MatcherProp *matcherprop_new(gint criteria, const gchar *header,
248 gint matchtype, const gchar *expr,
253 prop = g_new0(MatcherProp, 1);
254 prop->criteria = criteria;
255 prop->header = header != NULL ? g_strdup(header) : NULL;
256 prop->expr = expr != NULL ? g_strdup(expr) : NULL;
257 prop->matchtype = matchtype;
266 *\brief Free a matcher structure
268 *\param prop Pointer to matcher structure allocated with
271 void matcherprop_free(MatcherProp *prop)
274 g_free(prop->header);
275 if (prop->preg != NULL) {
283 *\brief Copy a matcher structure
285 *\param src Matcher structure to copy
287 *\return MatcherProp * Pointer to newly allocated matcher structure
289 MatcherProp *matcherprop_copy(const MatcherProp *src)
291 MatcherProp *prop = g_new0(MatcherProp, 1);
293 prop->criteria = src->criteria;
294 prop->header = src->header ? g_strdup(src->header) : NULL;
295 prop->expr = src->expr ? g_strdup(src->expr) : NULL;
296 prop->matchtype = src->matchtype;
298 prop->preg = NULL; /* will be re-evaluated */
299 prop->value = src->value;
300 prop->error = src->error;
304 /* ************** match ******************************/
306 static gboolean match_with_addresses_in_addressbook
307 (MatcherProp *prop, GSList *address_list, gint type,
308 gchar* folderpath, gint match)
311 gboolean found = FALSE;
314 g_return_val_if_fail(address_list != NULL, FALSE);
316 debug_print("match_with_addresses_in_addressbook(%d, %s)\n",
317 g_slist_length(address_list), folderpath);
319 if (folderpath == NULL ||
320 strcasecmp(folderpath, _("Any")) == 0 ||
326 start_address_completion(path);
328 for (walk = address_list; walk != NULL; walk = walk->next) {
329 /* exact matching of email address */
330 guint num_addr = complete_address(walk->data);
333 /* skip first item (this is the search string itself) */
335 for (; i < num_addr && !found; i++) {
336 gchar *addr = get_complete_address(i);
337 extract_address(addr);
338 if (strcasecmp(addr, walk->data) == 0) {
342 if (debug_filtering_session
343 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
344 log_print(LOG_DEBUG_FILTERING,
345 "address [ %s ] matches\n",
346 (gchar *)walk->data);
353 if (debug_filtering_session
354 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH
356 log_print(LOG_DEBUG_FILTERING,
357 "address [ %s ] doesn't match\n",
358 (gchar *)walk->data);
362 if (match == MATCH_ALL) {
363 /* if matching all addresses, stop if one doesn't match */
366 if (debug_filtering_session
367 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
368 log_print(LOG_DEBUG_FILTERING,
369 "not all address match (matching all)\n");
373 } else if (match == MATCH_ANY) {
374 /* if matching any address, stop if one does match */
377 if (debug_filtering_session
378 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
379 log_print(LOG_DEBUG_FILTERING,
380 "at least one address matches (matching any)\n");
385 /* MATCH_ONE: there should be only one loop iteration */
388 end_address_completion();
394 *\brief Find out if a string matches a condition
396 *\param prop Matcher structure
397 *\param str String to check
399 *\return gboolean TRUE if str matches the condition in the
402 static gboolean matcherprop_string_match(MatcherProp *prop, const gchar *str,
403 const gchar *debug_context)
407 gboolean ret = FALSE;
412 switch (prop->matchtype) {
413 case MATCHTYPE_REGEXPCASE:
414 case MATCHTYPE_REGEXP:
415 if (!prop->preg && (prop->error == 0)) {
416 prop->preg = g_new0(regex_t, 1);
417 /* if regexp then don't use the escaped string */
418 if (regcomp(prop->preg, prop->expr,
419 REG_NOSUB | REG_EXTENDED
420 | ((prop->matchtype == MATCHTYPE_REGEXPCASE)
421 ? REG_ICASE : 0)) != 0) {
427 if (prop->preg == NULL)
430 if (regexec(prop->preg, str, 0, NULL, 0) == 0)
436 if (debug_filtering_session
437 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
438 gchar *stripped = g_strdup(str);
440 strretchomp(stripped);
442 log_print(LOG_DEBUG_FILTERING,
443 "%s value [ %s ] matches regular expression [ %s ]\n",
444 debug_context, stripped, prop->expr);
446 log_print(LOG_DEBUG_FILTERING,
447 "%s value [ %s ] doesn't matches regular expression [ %s ]\n",
448 debug_context, stripped, prop->expr);
454 case MATCHTYPE_MATCH:
455 ret = (strstr(str, prop->expr) != NULL);
458 if (debug_filtering_session
459 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
460 gchar *stripped = g_strdup(str);
462 strretchomp(stripped);
464 log_print(LOG_DEBUG_FILTERING,
465 "%s value [ %s ] contains [ %s ] (case sensitive)\n",
466 debug_context, stripped, prop->expr);
468 log_print(LOG_DEBUG_FILTERING,
469 "%s value [ %s ] doesn't contains [ %s ] (case sensitive)\n",
470 debug_context, stripped, prop->expr);
476 /* FIXME: put upper in unesc_str */
477 case MATCHTYPE_MATCHCASE:
478 str2 = alloca(strlen(prop->expr) + 1);
479 strcpy(str2, prop->expr);
481 str1 = alloca(strlen(str) + 1);
484 ret = (strstr(str1, str2) != NULL);
487 if (debug_filtering_session
488 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
489 gchar *stripped = g_strdup(str);
491 strretchomp(stripped);
493 log_print(LOG_DEBUG_FILTERING,
494 "%s value [ %s ] contains [ %s ] (case insensitive)\n",
495 debug_context, stripped, prop->expr);
497 log_print(LOG_DEBUG_FILTERING,
498 "%s [ %s ] doesn't contains [ %s ] (case insensitive)\n",
499 debug_context, stripped, prop->expr);
512 *\brief Find out if a tag matches a condition
514 *\param prop Matcher structure
515 *\param msginfo message to check
517 *\return gboolean TRUE if msginfo matches the condition in the
520 static gboolean matcherprop_tag_match(MatcherProp *prop, MsgInfo *msginfo,
521 const gchar *debug_context)
523 gboolean ret = FALSE;
526 if (msginfo == NULL || msginfo->tags == NULL)
529 for (cur = msginfo->tags; cur; cur = cur->next) {
530 const gchar *str = tags_get_tag(GPOINTER_TO_INT(cur->data));
533 if (matcherprop_string_match(prop, str, debug_context)) {
542 *\brief Find out if the string-ed list matches a condition
544 *\param prop Matcher structure
545 *\param list GSList of strings to check
547 *\return gboolean TRUE if str matches the condition in the
550 static gboolean matcherprop_list_match(MatcherProp *prop, const GSList *list,
551 const gchar *debug_context)
555 for(cur = list; cur != NULL; cur = cur->next) {
556 if (matcherprop_string_match(prop, (gchar *)cur->data, debug_context))
562 /* FIXME body search is a hack. */
563 static gboolean matcherprop_string_decode_match(MatcherProp *prop, const gchar *str,
564 const gchar *debug_context)
568 gboolean res = FALSE;
573 /* we try to decode QP first, because it's faster than base64 */
574 qp_decode_const(tmp, BUFFSIZE-1, str);
575 if (!g_utf8_validate(tmp, -1, NULL)) {
576 utf = conv_codeset_strdup
577 (tmp, conv_get_locale_charset_str_no_utf8(),
579 res = matcherprop_string_match(prop, utf, debug_context);
582 res = matcherprop_string_match(prop, tmp, debug_context);
585 if (res == FALSE && (strchr(prop->expr, '=') || strchr(prop->expr, '_')
586 || strchr(str, '=') || strchr(str, '_'))) {
587 /* if searching for something with an equal char, maybe
588 * we should try to match the non-decoded string.
589 * In case it was not qp-encoded. */
590 if (!g_utf8_validate(str, -1, NULL)) {
591 utf = conv_codeset_strdup
592 (str, conv_get_locale_charset_str_no_utf8(),
594 res = matcherprop_string_match(prop, utf, debug_context);
597 res = matcherprop_string_match(prop, str, debug_context);
601 /* FIXME base64 decoding is too slow, especially since text can
602 * easily be handled as base64. Don't even try now. */
608 typedef struct _thread_data {
615 static void *matcher_test_thread(void *data)
617 thread_data *td = (thread_data *)data;
620 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
621 pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
623 result = system(td->cmd);
624 if (result) perror("system");
625 td->done = TRUE; /* let the caller thread join() */
626 return GINT_TO_POINTER(result);
631 *\brief Execute a command defined in the matcher structure
633 *\param prop Pointer to matcher structure
634 *\param info Pointer to message info structure
636 *\return gboolean TRUE if command was executed succesfully
638 static gboolean matcherprop_match_test(const MatcherProp *prop,
646 thread_data *td = g_new0(thread_data, 1);
648 time_t start_time = time(NULL);
651 file = procmsg_get_message_file(info);
656 cmd = matching_build_command(prop->expr, info);
660 #if (defined USE_PTHREAD && ((defined __GLIBC__ && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 3))) || !defined __GLIBC__))
662 if (debug_filtering_session
663 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
664 log_print(LOG_DEBUG_FILTERING,
665 "starting threaded command [ %s ]\n",
671 if (pthread_create(&pt, PTHREAD_CREATE_JOINABLE,
672 matcher_test_thread, td) != 0)
673 retval = system(cmd);
675 printf("waiting for test thread\n");
677 /* don't let the interface freeze while waiting */
679 if (time(NULL) - start_time > 30) {
685 pthread_join(pt, &res);
686 retval = GPOINTER_TO_INT(res);
687 printf(" test thread returned %d\n", retval);
692 if (debug_filtering_session
693 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
694 log_print(LOG_DEBUG_FILTERING,
695 "starting synchronous command [ %s ]\n",
699 retval = system(cmd);
701 debug_print("Command exit code: %d\n", retval);
704 if (debug_filtering_session
705 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
706 log_print(LOG_DEBUG_FILTERING,
707 "command returned [ %d ]\n",
712 return (retval == 0);
716 *\brief Check if a message matches the condition in a matcher
719 *\param prop Pointer to matcher structure
720 *\param info Pointer to message info
722 *\return gboolean TRUE if a match
724 gboolean matcherprop_match(MatcherProp *prop,
729 switch(prop->criteria) {
730 case MATCHCRITERIA_ALL:
732 case MATCHCRITERIA_UNREAD:
733 return MSG_IS_UNREAD(info->flags);
734 case MATCHCRITERIA_NOT_UNREAD:
735 return !MSG_IS_UNREAD(info->flags);
736 case MATCHCRITERIA_NEW:
737 return MSG_IS_NEW(info->flags);
738 case MATCHCRITERIA_NOT_NEW:
739 return !MSG_IS_NEW(info->flags);
740 case MATCHCRITERIA_MARKED:
741 return MSG_IS_MARKED(info->flags);
742 case MATCHCRITERIA_NOT_MARKED:
743 return !MSG_IS_MARKED(info->flags);
744 case MATCHCRITERIA_DELETED:
745 return MSG_IS_DELETED(info->flags);
746 case MATCHCRITERIA_NOT_DELETED:
747 return !MSG_IS_DELETED(info->flags);
748 case MATCHCRITERIA_REPLIED:
749 return MSG_IS_REPLIED(info->flags);
750 case MATCHCRITERIA_NOT_REPLIED:
751 return !MSG_IS_REPLIED(info->flags);
752 case MATCHCRITERIA_FORWARDED:
753 return MSG_IS_FORWARDED(info->flags);
754 case MATCHCRITERIA_NOT_FORWARDED:
755 return !MSG_IS_FORWARDED(info->flags);
756 case MATCHCRITERIA_LOCKED:
757 return MSG_IS_LOCKED(info->flags);
758 case MATCHCRITERIA_NOT_LOCKED:
759 return !MSG_IS_LOCKED(info->flags);
760 case MATCHCRITERIA_SPAM:
761 return MSG_IS_SPAM(info->flags);
762 case MATCHCRITERIA_NOT_SPAM:
763 return !MSG_IS_SPAM(info->flags);
764 case MATCHCRITERIA_COLORLABEL:
766 gint color = MSG_GET_COLORLABEL_VALUE(info->flags);
767 gboolean ret = (color == prop->value);
770 if (debug_filtering_session
771 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
773 log_print(LOG_DEBUG_FILTERING,
774 "message color value [ %d ] matches color value [ %d ]\n",
777 log_print(LOG_DEBUG_FILTERING,
778 "message color value [ %d ] doesn't matches color value [ %d ]\n",
784 case MATCHCRITERIA_NOT_COLORLABEL:
786 gint color = MSG_GET_COLORLABEL_VALUE(info->flags);
787 gboolean ret = (color != prop->value);
790 if (debug_filtering_session
791 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
793 log_print(LOG_DEBUG_FILTERING,
794 "message color value [ %d ] matches color value [ %d ]\n",
797 log_print(LOG_DEBUG_FILTERING,
798 "message color value [ %d ] doesn't matches color value [ %d ]\n",
804 case MATCHCRITERIA_IGNORE_THREAD:
805 return MSG_IS_IGNORE_THREAD(info->flags);
806 case MATCHCRITERIA_NOT_IGNORE_THREAD:
807 return !MSG_IS_IGNORE_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_SUBJECT:
1652 case MATCHCRITERIA_NOT_SUBJECT:
1653 case MATCHCRITERIA_FROM:
1654 case MATCHCRITERIA_NOT_FROM:
1655 case MATCHCRITERIA_TO:
1656 case MATCHCRITERIA_NOT_TO:
1657 case MATCHCRITERIA_CC:
1658 case MATCHCRITERIA_NOT_CC:
1659 case MATCHCRITERIA_TO_OR_CC:
1660 case MATCHCRITERIA_NOT_TO_AND_NOT_CC:
1661 case MATCHCRITERIA_TAG:
1662 case MATCHCRITERIA_NOT_TAG:
1663 case MATCHCRITERIA_TAGGED:
1664 case MATCHCRITERIA_NOT_TAGGED:
1665 case MATCHCRITERIA_AGE_GREATER:
1666 case MATCHCRITERIA_AGE_LOWER:
1667 case MATCHCRITERIA_NEWSGROUPS:
1668 case MATCHCRITERIA_NOT_NEWSGROUPS:
1669 case MATCHCRITERIA_INREPLYTO:
1670 case MATCHCRITERIA_NOT_INREPLYTO:
1671 case MATCHCRITERIA_REFERENCES:
1672 case MATCHCRITERIA_NOT_REFERENCES:
1673 case MATCHCRITERIA_SCORE_GREATER:
1674 case MATCHCRITERIA_SCORE_LOWER:
1675 case MATCHCRITERIA_SCORE_EQUAL:
1676 case MATCHCRITERIA_SIZE_GREATER:
1677 case MATCHCRITERIA_SIZE_SMALLER:
1678 case MATCHCRITERIA_SIZE_EQUAL:
1679 case MATCHCRITERIA_TEST:
1680 case MATCHCRITERIA_NOT_TEST:
1681 case MATCHCRITERIA_PARTIAL:
1682 case MATCHCRITERIA_NOT_PARTIAL:
1683 if (matcherprop_match(matcher, info)) {
1684 if (!matchers->bool_and) {
1685 if (debug_filtering_session)
1686 log_status_ok(LOG_DEBUG_FILTERING, _("message matches\n"));
1691 if (matchers->bool_and) {
1692 if (debug_filtering_session)
1693 log_status_nok(LOG_DEBUG_FILTERING, _("message does not match\n"));
1700 /* test the condition on the file */
1702 if (matcherlist_match_file(matchers, info, result)) {
1703 if (!matchers->bool_and) {
1704 if (debug_filtering_session)
1705 log_status_ok(LOG_DEBUG_FILTERING, _("message matches\n"));
1709 if (matchers->bool_and) {
1710 if (debug_filtering_session)
1711 log_status_nok(LOG_DEBUG_FILTERING, _("message does not match\n"));
1716 if (debug_filtering_session) {
1718 log_status_ok(LOG_DEBUG_FILTERING, _("message matches\n"));
1720 log_status_nok(LOG_DEBUG_FILTERING, _("message does not match\n"));
1726 static gint quote_filter_str(gchar * result, guint size,
1736 for(p = path ; * p != '\0' ; p ++) {
1738 if ((* p != '\"') && (* p != '\\')) {
1739 if (remaining > 0) {
1745 result[size - 1] = '\0';
1750 if (remaining >= 2) {
1758 result[size - 1] = '\0';
1763 if (remaining > 0) {
1767 result[size - 1] = '\0';
1775 gchar * matcher_quote_str(const gchar * src)
1780 len = strlen(src) * 2 + 1;
1781 res = g_malloc(len);
1782 quote_filter_str(res, len, src);
1788 *\brief Convert a matcher structure to a string
1790 *\param matcher Matcher structure
1792 *\return gchar * Newly allocated string
1794 gchar *matcherprop_to_string(MatcherProp *matcher)
1796 gchar *matcher_str = NULL;
1797 const gchar *criteria_str;
1798 const gchar *matchtype_str;
1800 gchar * quoted_expr;
1801 gchar * quoted_header;
1803 criteria_str = NULL;
1804 for (i = 0; i < (int) (sizeof(matchparser_tab) / sizeof(MatchParser)); i++) {
1805 if (matchparser_tab[i].id == matcher->criteria)
1806 criteria_str = matchparser_tab[i].str;
1808 if (criteria_str == NULL)
1811 switch (matcher->criteria) {
1812 case MATCHCRITERIA_AGE_GREATER:
1813 case MATCHCRITERIA_AGE_LOWER:
1814 case MATCHCRITERIA_SCORE_GREATER:
1815 case MATCHCRITERIA_SCORE_LOWER:
1816 case MATCHCRITERIA_SCORE_EQUAL:
1817 case MATCHCRITERIA_SIZE_GREATER:
1818 case MATCHCRITERIA_SIZE_SMALLER:
1819 case MATCHCRITERIA_SIZE_EQUAL:
1820 case MATCHCRITERIA_COLORLABEL:
1821 case MATCHCRITERIA_NOT_COLORLABEL:
1822 return g_strdup_printf("%s %i", criteria_str, matcher->value);
1823 case MATCHCRITERIA_ALL:
1824 case MATCHCRITERIA_UNREAD:
1825 case MATCHCRITERIA_NOT_UNREAD:
1826 case MATCHCRITERIA_NEW:
1827 case MATCHCRITERIA_NOT_NEW:
1828 case MATCHCRITERIA_MARKED:
1829 case MATCHCRITERIA_NOT_MARKED:
1830 case MATCHCRITERIA_DELETED:
1831 case MATCHCRITERIA_NOT_DELETED:
1832 case MATCHCRITERIA_REPLIED:
1833 case MATCHCRITERIA_NOT_REPLIED:
1834 case MATCHCRITERIA_FORWARDED:
1835 case MATCHCRITERIA_NOT_FORWARDED:
1836 case MATCHCRITERIA_LOCKED:
1837 case MATCHCRITERIA_NOT_LOCKED:
1838 case MATCHCRITERIA_SPAM:
1839 case MATCHCRITERIA_NOT_SPAM:
1840 case MATCHCRITERIA_PARTIAL:
1841 case MATCHCRITERIA_NOT_PARTIAL:
1842 case MATCHCRITERIA_IGNORE_THREAD:
1843 case MATCHCRITERIA_NOT_IGNORE_THREAD:
1844 case MATCHCRITERIA_TAGGED:
1845 case MATCHCRITERIA_NOT_TAGGED:
1846 return g_strdup(criteria_str);
1847 case MATCHCRITERIA_TEST:
1848 case MATCHCRITERIA_NOT_TEST:
1849 quoted_expr = matcher_quote_str(matcher->expr);
1850 matcher_str = g_strdup_printf("%s \"%s\"",
1851 criteria_str, quoted_expr);
1852 g_free(quoted_expr);
1854 case MATCHCRITERIA_FOUND_IN_ADDRESSBOOK:
1855 case MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK:
1856 quoted_header = matcher_quote_str(matcher->header);
1857 quoted_expr = matcher_quote_str(matcher->expr);
1858 matcher_str = g_strdup_printf("%s \"%s\" in \"%s\"",
1859 criteria_str, quoted_header, quoted_expr);
1860 g_free(quoted_header);
1861 g_free(quoted_expr);
1865 matchtype_str = NULL;
1866 for (i = 0; i < sizeof matchparser_tab / sizeof matchparser_tab[0]; i++) {
1867 if (matchparser_tab[i].id == matcher->matchtype)
1868 matchtype_str = matchparser_tab[i].str;
1871 if (matchtype_str == NULL)
1874 switch (matcher->matchtype) {
1875 case MATCHTYPE_MATCH:
1876 case MATCHTYPE_MATCHCASE:
1877 case MATCHTYPE_REGEXP:
1878 case MATCHTYPE_REGEXPCASE:
1879 quoted_expr = matcher_quote_str(matcher->expr);
1880 if (matcher->header) {
1881 quoted_header = matcher_quote_str(matcher->header);
1882 matcher_str = g_strdup_printf
1883 ("%s \"%s\" %s \"%s\"",
1884 criteria_str, quoted_header,
1885 matchtype_str, quoted_expr);
1886 g_free(quoted_header);
1889 matcher_str = g_strdup_printf
1890 ("%s %s \"%s\"", criteria_str,
1891 matchtype_str, quoted_expr);
1892 g_free(quoted_expr);
1900 *\brief Convert a list of conditions to a string
1902 *\param matchers List of conditions
1904 *\return gchar * Newly allocated string
1906 gchar *matcherlist_to_string(const MatcherList *matchers)
1912 gchar *result = NULL;
1914 count = g_slist_length(matchers->matchers);
1915 vstr = g_new(gchar *, count + 1);
1917 for (l = matchers->matchers, cur_str = vstr; l != NULL;
1918 l = g_slist_next(l), cur_str ++) {
1919 *cur_str = matcherprop_to_string((MatcherProp *) l->data);
1920 if (*cur_str == NULL)
1925 if (matchers->bool_and)
1926 result = g_strjoinv(" & ", vstr);
1928 result = g_strjoinv(" | ", vstr);
1930 for (cur_str = vstr ; *cur_str != NULL ; cur_str ++)
1938 #define STRLEN_ZERO(s) ((s) ? strlen(s) : 0)
1939 #define STRLEN_DEFAULT(s,d) ((s) ? strlen(s) : STRLEN_ZERO(d))
1941 static void add_str_default(gchar ** dest,
1942 const gchar * s, const gchar * d)
1944 gchar quoted_str[4096];
1952 quote_cmd_argument(quoted_str, sizeof(quoted_str), str);
1953 strcpy(* dest, quoted_str);
1955 (* dest) += strlen(* dest);
1958 /* matching_build_command() - preferably cmd should be unescaped */
1960 *\brief Build the command line to execute
1962 *\param cmd String with command line specifiers
1963 *\param info Message info to use for command
1965 *\return gchar * Newly allocated string
1967 gchar *matching_build_command(const gchar *cmd, MsgInfo *info)
1969 const gchar *s = cmd;
1970 gchar *filename = NULL;
1971 gchar *processed_cmd;
1975 const gchar *const no_subject = _("(none)") ;
1976 const gchar *const no_from = _("(none)") ;
1977 const gchar *const no_to = _("(none)") ;
1978 const gchar *const no_cc = _("(none)") ;
1979 const gchar *const no_date = _("(none)") ;
1980 const gchar *const no_msgid = _("(none)") ;
1981 const gchar *const no_newsgroups = _("(none)") ;
1982 const gchar *const no_references = _("(none)") ;
1984 size = STRLEN_ZERO(cmd) + 1;
1985 while (*s != '\0') {
1992 case 's': /* subject */
1993 size += STRLEN_DEFAULT(info->subject, no_subject) - 2;
1995 case 'f': /* from */
1996 size += STRLEN_DEFAULT(info->from, no_from) - 2;
1999 size += STRLEN_DEFAULT(info->to, no_to) - 2;
2002 size += STRLEN_DEFAULT(info->cc, no_cc) - 2;
2004 case 'd': /* date */
2005 size += STRLEN_DEFAULT(info->date, no_date) - 2;
2007 case 'i': /* message-id */
2008 size += STRLEN_DEFAULT(info->msgid, no_msgid) - 2;
2010 case 'n': /* newsgroups */
2011 size += STRLEN_DEFAULT(info->newsgroups, no_newsgroups) - 2;
2013 case 'r': /* references */
2014 /* FIXME: using the inreplyto header for reference */
2015 size += STRLEN_DEFAULT(info->inreplyto, no_references) - 2;
2017 case 'F': /* file */
2018 if (filename == NULL)
2019 filename = folder_item_fetch_msg(info->folder, info->msgnum);
2021 if (filename == NULL) {
2022 g_warning("filename is not set");
2026 size += strlen(filename) - 2;
2035 /* as the string can be quoted, we double the result */
2038 processed_cmd = g_new0(gchar, size);
2042 while (*s != '\0') {
2050 case 's': /* subject */
2051 add_str_default(&p, info->subject,
2054 case 'f': /* from */
2055 add_str_default(&p, info->from,
2059 add_str_default(&p, info->to,
2063 add_str_default(&p, info->cc,
2066 case 'd': /* date */
2067 add_str_default(&p, info->date,
2070 case 'i': /* message-id */
2071 add_str_default(&p, info->msgid,
2074 case 'n': /* newsgroups */
2075 add_str_default(&p, info->newsgroups,
2078 case 'r': /* references */
2079 /* FIXME: using the inreplyto header for references */
2080 add_str_default(&p, info->inreplyto, no_references);
2082 case 'F': /* file */
2083 if (filename != NULL)
2084 add_str_default(&p, filename, NULL);
2103 return processed_cmd;
2105 #undef STRLEN_DEFAULT
2108 /* ************************************************************ */
2112 *\brief Write filtering list to file
2115 *\param prefs_filtering List of filtering conditions
2117 static void prefs_filtering_write(FILE *fp, GSList *prefs_filtering)
2121 for (cur = prefs_filtering; cur != NULL; cur = cur->next) {
2122 gchar *filtering_str = NULL;
2123 gchar *tmp_name = NULL;
2124 FilteringProp *prop = NULL;
2126 if (NULL == (prop = (FilteringProp *) cur->data))
2129 if (NULL == (filtering_str = filteringprop_to_string(prop)))
2132 if (prop->enabled) {
2133 if (fputs("enabled ", fp) == EOF) {
2134 FILE_OP_ERROR("filtering config", "fputs");
2138 if (fputs("disabled ", fp) == EOF) {
2139 FILE_OP_ERROR("filtering config", "fputs");
2144 if (fputs("rulename \"", fp) == EOF) {
2145 FILE_OP_ERROR("filtering config", "fputs");
2146 g_free(filtering_str);
2149 tmp_name = prop->name;
2150 while (tmp_name && *tmp_name != '\0') {
2151 if (*tmp_name != '"') {
2152 if (fputc(*tmp_name, fp) == EOF) {
2153 FILE_OP_ERROR("filtering config", "fputs || fputc");
2154 g_free(filtering_str);
2157 } else if (*tmp_name == '"') {
2158 if (fputc('\\', fp) == EOF ||
2159 fputc('"', fp) == EOF) {
2160 FILE_OP_ERROR("filtering config", "fputs || fputc");
2161 g_free(filtering_str);
2167 if (fputs("\" ", fp) == EOF) {
2168 FILE_OP_ERROR("filtering config", "fputs");
2169 g_free(filtering_str);
2173 if (prop->account_id != 0) {
2176 tmp = g_strdup_printf("account %d ", prop->account_id);
2177 if (fputs(tmp, fp) == EOF) {
2178 FILE_OP_ERROR("filtering config", "fputs");
2185 if(fputs(filtering_str, fp) == EOF ||
2186 fputc('\n', fp) == EOF) {
2187 FILE_OP_ERROR("filtering config", "fputs || fputc");
2188 g_free(filtering_str);
2191 g_free(filtering_str);
2196 *\brief Write matchers from a folder item
2198 *\param node Node with folder info
2199 *\param data File pointer
2201 *\return gboolean FALSE
2203 static gboolean prefs_matcher_write_func(GNode *node, gpointer data)
2208 GSList *prefs_filtering;
2211 /* prevent warning */
2212 if (item->path == NULL)
2214 id = folder_item_get_identifier(item);
2217 prefs_filtering = item->prefs->processing;
2219 if (prefs_filtering != NULL) {
2220 fprintf(fp, "[%s]\n", id);
2221 prefs_filtering_write(fp, prefs_filtering);
2231 *\brief Save matchers from folder items
2235 static void prefs_matcher_save(FILE *fp)
2239 for (cur = folder_get_list() ; cur != NULL ; cur = g_list_next(cur)) {
2242 folder = (Folder *) cur->data;
2243 g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
2244 prefs_matcher_write_func, fp);
2247 /* pre global rules */
2248 fprintf(fp, "[preglobal]\n");
2249 prefs_filtering_write(fp, pre_global_processing);
2252 /* post global rules */
2253 fprintf(fp, "[postglobal]\n");
2254 prefs_filtering_write(fp, post_global_processing);
2257 /* filtering rules */
2258 fprintf(fp, "[filtering]\n");
2259 prefs_filtering_write(fp, filtering_rules);
2264 *\brief Write filtering / matcher configuration file
2266 void prefs_matcher_write_config(void)
2271 debug_print("Writing matcher configuration...\n");
2273 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
2276 if ((pfile = prefs_write_open(rcpath)) == NULL) {
2277 g_warning("failed to write configuration to file\n");
2283 prefs_matcher_save(pfile->fp);
2287 if (prefs_file_close(pfile) < 0) {
2288 g_warning("failed to write configuration to file\n");
2293 /* ******************************************************************* */
2295 static void matcher_add_rulenames(const gchar *rcpath)
2297 gchar *newpath = g_strconcat(rcpath, ".new", NULL);
2298 FILE *src = g_fopen(rcpath, "rb");
2299 FILE *dst = g_fopen(newpath, "wb");
2300 gchar buf[BUFFSIZE];
2308 while (fgets (buf, sizeof(buf), src) != NULL) {
2309 if (strlen(buf) > 2 && buf[0] != '['
2310 && strncmp(buf, "rulename \"", 10)
2311 && strncmp(buf, "enabled rulename \"", 18)
2312 && strncmp(buf, "disabled rulename \"", 18)) {
2313 fwrite("enabled rulename \"\" ",
2314 strlen("enabled rulename \"\" "), 1, dst);
2316 fwrite(buf, strlen(buf), 1, dst);
2320 move_file(newpath, rcpath, TRUE);
2325 *\brief Read matcher configuration
2327 void prefs_matcher_read_config(void)
2330 gchar *rc_old_format;
2333 create_matchparser_hashtab();
2334 prefs_filtering_clear();
2336 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MATCHER_RC, NULL);
2337 rc_old_format = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MATCHER_RC,
2338 ".pre_names", NULL);
2340 if (!is_file_exist(rc_old_format) && is_file_exist(rcpath)) {
2341 /* backup file with no rules names, in case
2342 * anything goes wrong */
2343 copy_file(rcpath, rc_old_format, FALSE);
2344 /* now hack the file in order to have it to the new format */
2345 matcher_add_rulenames(rcpath);
2348 g_free(rc_old_format);
2350 f = g_fopen(rcpath, "rb");
2354 matcher_parser_start_parsing(f);
2355 fclose(matcher_parserin);
2358 /* previous version compatibility */
2360 /* printf("reading filtering\n"); */
2361 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
2362 FILTERING_RC, NULL);
2363 f = g_fopen(rcpath, "rb");
2367 matcher_parser_start_parsing(f);
2368 fclose(matcher_parserin);
2371 /* printf("reading scoring\n"); */
2372 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
2374 f = g_fopen(rcpath, "rb");
2378 matcher_parser_start_parsing(f);
2379 fclose(matcher_parserin);