2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 2002-2014 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/>.
22 #include "claws-features.h"
26 #include <glib/gi18n.h>
38 #include "procheader.h"
40 #include "matcher_parser.h"
41 #include "prefs_gtk.h"
42 #include "addr_compl.h"
44 #include "quoted-printable.h"
47 #include "prefs_common.h"
50 #include "folder_item_prefs.h"
54 *\brief Keyword lookup element
57 gint id; /*!< keyword id */
58 gchar *str; /*!< keyword */
60 typedef struct _MatchParser MatchParser;
63 *\brief Table with strings and ids used by the lexer and
64 * the parser. New keywords can be added here.
66 static const MatchParser matchparser_tab[] = {
68 {MATCHCRITERIA_ALL, "all"},
69 {MATCHCRITERIA_UNREAD, "unread"},
70 {MATCHCRITERIA_NOT_UNREAD, "~unread"},
71 {MATCHCRITERIA_NEW, "new"},
72 {MATCHCRITERIA_NOT_NEW, "~new"},
73 {MATCHCRITERIA_MARKED, "marked"},
74 {MATCHCRITERIA_NOT_MARKED, "~marked"},
75 {MATCHCRITERIA_DELETED, "deleted"},
76 {MATCHCRITERIA_NOT_DELETED, "~deleted"},
77 {MATCHCRITERIA_REPLIED, "replied"},
78 {MATCHCRITERIA_NOT_REPLIED, "~replied"},
79 {MATCHCRITERIA_FORWARDED, "forwarded"},
80 {MATCHCRITERIA_NOT_FORWARDED, "~forwarded"},
81 {MATCHCRITERIA_LOCKED, "locked"},
82 {MATCHCRITERIA_NOT_LOCKED, "~locked"},
83 {MATCHCRITERIA_COLORLABEL, "colorlabel"},
84 {MATCHCRITERIA_NOT_COLORLABEL, "~colorlabel"},
85 {MATCHCRITERIA_IGNORE_THREAD, "ignore_thread"},
86 {MATCHCRITERIA_NOT_IGNORE_THREAD, "~ignore_thread"},
87 {MATCHCRITERIA_WATCH_THREAD, "watch_thread"},
88 {MATCHCRITERIA_NOT_WATCH_THREAD, "~watch_thread"},
89 {MATCHCRITERIA_SPAM, "spam"},
90 {MATCHCRITERIA_NOT_SPAM, "~spam"},
91 {MATCHCRITERIA_HAS_ATTACHMENT, "has_attachment"},
92 {MATCHCRITERIA_HAS_NO_ATTACHMENT, "~has_attachment"},
93 {MATCHCRITERIA_SIGNED, "signed"},
94 {MATCHCRITERIA_NOT_SIGNED, "~signed"},
97 {MATCHCRITERIA_SUBJECT, "subject"},
98 {MATCHCRITERIA_NOT_SUBJECT, "~subject"},
99 {MATCHCRITERIA_FROM, "from"},
100 {MATCHCRITERIA_NOT_FROM, "~from"},
101 {MATCHCRITERIA_TO, "to"},
102 {MATCHCRITERIA_NOT_TO, "~to"},
103 {MATCHCRITERIA_CC, "cc"},
104 {MATCHCRITERIA_NOT_CC, "~cc"},
105 {MATCHCRITERIA_TO_OR_CC, "to_or_cc"},
106 {MATCHCRITERIA_NOT_TO_AND_NOT_CC, "~to_or_cc"},
107 {MATCHCRITERIA_TAG, "tag"},
108 {MATCHCRITERIA_NOT_TAG, "~tag"},
109 {MATCHCRITERIA_TAGGED, "tagged"},
110 {MATCHCRITERIA_NOT_TAGGED, "~tagged"},
111 {MATCHCRITERIA_AGE_GREATER, "age_greater"},
112 {MATCHCRITERIA_AGE_LOWER, "age_lower"},
113 {MATCHCRITERIA_AGE_GREATER_HOURS, "age_greater_hours"},
114 {MATCHCRITERIA_AGE_LOWER_HOURS, "age_lower_hours"},
115 {MATCHCRITERIA_NEWSGROUPS, "newsgroups"},
116 {MATCHCRITERIA_NOT_NEWSGROUPS, "~newsgroups"},
117 {MATCHCRITERIA_MESSAGEID, "messageid"},
118 {MATCHCRITERIA_NOT_MESSAGEID, "~messageid"},
119 {MATCHCRITERIA_INREPLYTO, "inreplyto"},
120 {MATCHCRITERIA_NOT_INREPLYTO, "~inreplyto"},
121 {MATCHCRITERIA_REFERENCES, "references"},
122 {MATCHCRITERIA_NOT_REFERENCES, "~references"},
123 {MATCHCRITERIA_SCORE_GREATER, "score_greater"},
124 {MATCHCRITERIA_SCORE_LOWER, "score_lower"},
125 {MATCHCRITERIA_SCORE_EQUAL, "score_equal"},
126 {MATCHCRITERIA_PARTIAL, "partial"},
127 {MATCHCRITERIA_NOT_PARTIAL, "~partial"},
128 {MATCHCRITERIA_FOUND_IN_ADDRESSBOOK, "found_in_addressbook"},
129 {MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK, "~found_in_addressbook"},
131 {MATCHCRITERIA_SIZE_GREATER, "size_greater"},
132 {MATCHCRITERIA_SIZE_SMALLER, "size_smaller"},
133 {MATCHCRITERIA_SIZE_EQUAL, "size_equal"},
135 /* content have to be read */
136 {MATCHCRITERIA_HEADER, "header"},
137 {MATCHCRITERIA_NOT_HEADER, "~header"},
138 {MATCHCRITERIA_HEADERS_PART, "headers_part"},
139 {MATCHCRITERIA_NOT_HEADERS_PART, "~headers_part"},
140 {MATCHCRITERIA_HEADERS_CONT, "headers_cont"},
141 {MATCHCRITERIA_NOT_HEADERS_CONT, "~headers_cont"},
142 {MATCHCRITERIA_MESSAGE, "message"},
143 {MATCHCRITERIA_NOT_MESSAGE, "~message"},
144 {MATCHCRITERIA_BODY_PART, "body_part"},
145 {MATCHCRITERIA_NOT_BODY_PART, "~body_part"},
146 {MATCHCRITERIA_TEST, "test"},
147 {MATCHCRITERIA_NOT_TEST, "~test"},
150 {MATCHTYPE_MATCHCASE, "matchcase"},
151 {MATCHTYPE_MATCH, "match"},
152 {MATCHTYPE_REGEXPCASE, "regexpcase"},
153 {MATCHTYPE_REGEXP, "regexp"},
156 {MATCHACTION_SCORE, "score"}, /* for backward compatibility */
157 {MATCHACTION_MOVE, "move"},
158 {MATCHACTION_COPY, "copy"},
159 {MATCHACTION_DELETE, "delete"},
160 {MATCHACTION_MARK, "mark"},
161 {MATCHACTION_UNMARK, "unmark"},
162 {MATCHACTION_LOCK, "lock"},
163 {MATCHACTION_UNLOCK, "unlock"},
164 {MATCHACTION_MARK_AS_READ, "mark_as_read"},
165 {MATCHACTION_MARK_AS_UNREAD, "mark_as_unread"},
166 {MATCHACTION_MARK_AS_SPAM, "mark_as_spam"},
167 {MATCHACTION_MARK_AS_HAM, "mark_as_ham"},
168 {MATCHACTION_FORWARD, "forward"},
169 {MATCHACTION_FORWARD_AS_ATTACHMENT, "forward_as_attachment"},
170 {MATCHACTION_EXECUTE, "execute"},
171 {MATCHACTION_COLOR, "color"},
172 {MATCHACTION_REDIRECT, "redirect"},
173 {MATCHACTION_CHANGE_SCORE, "change_score"},
174 {MATCHACTION_SET_SCORE, "set_score"},
175 {MATCHACTION_STOP, "stop"},
176 {MATCHACTION_HIDE, "hide"},
177 {MATCHACTION_IGNORE, "ignore"},
178 {MATCHACTION_WATCH, "watch"},
179 {MATCHACTION_ADD_TO_ADDRESSBOOK, "add_to_addressbook"},
180 {MATCHACTION_SET_TAG, "set_tag"},
181 {MATCHACTION_UNSET_TAG, "unset_tag"},
182 {MATCHACTION_CLEAR_TAGS, "clear_tags"},
207 static gchar *context_str[N_CONTEXT_STRS];
209 void matcher_init(void)
211 if (context_str[CONTEXT_SUBJECT] != NULL)
214 context_str[CONTEXT_SUBJECT] = g_strdup_printf(_("%s header"), prefs_common_translated_header_name("Subject:"));
215 context_str[CONTEXT_FROM] = g_strdup_printf(_("%s header"), prefs_common_translated_header_name("From:"));
216 context_str[CONTEXT_TO] = g_strdup_printf(_("%s header"), prefs_common_translated_header_name("To:"));
217 context_str[CONTEXT_CC] = g_strdup_printf(_("%s header"), prefs_common_translated_header_name("Cc:"));
218 context_str[CONTEXT_NEWSGROUPS] = g_strdup_printf(_("%s header"), prefs_common_translated_header_name("Newsgroups:"));
219 context_str[CONTEXT_MESSAGEID] = g_strdup_printf(_("%s header"), prefs_common_translated_header_name("Message-ID:"));
220 context_str[CONTEXT_IN_REPLY_TO] = g_strdup_printf(_("%s header"), prefs_common_translated_header_name("In-Reply-To:"));
221 context_str[CONTEXT_REFERENCES] = g_strdup_printf(_("%s header"), prefs_common_translated_header_name("References:"));
222 context_str[CONTEXT_HEADER] = g_strdup(_("header"));
223 context_str[CONTEXT_HEADER_LINE] = g_strdup(_("header line"));
224 context_str[CONTEXT_BODY_LINE] = g_strdup(_("body line"));
225 context_str[CONTEXT_TAG] = g_strdup(_("tag"));
228 void matcher_done(void)
231 for (i = 0; i < N_CONTEXT_STRS; i++) {
232 g_free(context_str[i]);
233 context_str[i] = NULL;
237 extern gboolean debug_filtering_session;
240 *\brief Look up table with keywords defined in \sa matchparser_tab
242 static GHashTable *matchparser_hashtab;
245 *\brief Translate keyword id to keyword string
247 *\param id Id of keyword
249 *\return const gchar * Keyword
251 const gchar *get_matchparser_tab_str(gint id)
255 for (i = 0; i < sizeof matchparser_tab / sizeof matchparser_tab[0]; i++) {
256 if (matchparser_tab[i].id == id)
257 return matchparser_tab[i].str;
263 *\brief Create keyword lookup table
265 static void create_matchparser_hashtab(void)
269 if (matchparser_hashtab) return;
270 matchparser_hashtab = g_hash_table_new(g_str_hash, g_str_equal);
271 for (i = 0; i < sizeof matchparser_tab / sizeof matchparser_tab[0]; i++)
272 g_hash_table_insert(matchparser_hashtab,
273 matchparser_tab[i].str,
274 (gpointer) &matchparser_tab[i]);
278 *\brief Return a keyword id from a keyword string
280 *\param str Keyword string
282 *\return gint Keyword id
284 gint get_matchparser_tab_id(const gchar *str)
288 if (NULL != (res = g_hash_table_lookup(matchparser_hashtab, str))) {
294 /* **************** data structure allocation **************** */
297 *\brief Allocate a structure for a filtering / scoring
298 * "condition" (a matcher structure)
300 *\param criteria Criteria ID (MATCHCRITERIA_XXXX)
301 *\param header Header string (if criteria is MATCHCRITERIA_HEADER
302 or MATCHCRITERIA_FOUND_IN_ADDRESSBOOK)
303 *\param matchtype Type of action (MATCHTYPE_XXX)
304 *\param expr String value or expression to check
305 *\param value Integer value to check
307 *\return MatcherProp * Pointer to newly allocated structure
309 MatcherProp *matcherprop_new(gint criteria, const gchar *header,
310 gint matchtype, const gchar *expr,
315 prop = g_new0(MatcherProp, 1);
316 prop->criteria = criteria;
317 prop->header = header != NULL ? g_strdup(header) : NULL;
319 prop->expr = expr != NULL ? g_strdup(expr) : NULL;
321 prop->matchtype = matchtype;
332 *\brief Free a matcher structure
334 *\param prop Pointer to matcher structure allocated with
337 void matcherprop_free(MatcherProp *prop)
340 g_free(prop->header);
342 if (prop->preg != NULL) {
351 *\brief Copy a matcher structure
353 *\param src Matcher structure to copy
355 *\return MatcherProp * Pointer to newly allocated matcher structure
357 MatcherProp *matcherprop_copy(const MatcherProp *src)
359 MatcherProp *prop = g_new0(MatcherProp, 1);
361 prop->criteria = src->criteria;
362 prop->header = src->header ? g_strdup(src->header) : NULL;
363 prop->expr = src->expr ? g_strdup(src->expr) : NULL;
364 prop->matchtype = src->matchtype;
367 prop->preg = NULL; /* will be re-evaluated */
369 prop->value = src->value;
370 prop->error = src->error;
374 /* ************** match ******************************/
376 static gboolean match_with_addresses_in_addressbook
377 (MatcherProp *prop, GSList *address_list, gint type,
378 gchar* folderpath, gint match)
381 gboolean found = FALSE;
384 cm_return_val_if_fail(address_list != NULL, FALSE);
386 debug_print("match_with_addresses_in_addressbook(%d, %s)\n",
387 g_slist_length(address_list), folderpath?folderpath:"(null)");
389 if (folderpath == NULL ||
390 strcasecmp(folderpath, "Any") == 0 ||
396 start_address_completion(path);
398 for (walk = address_list; walk != NULL; walk = walk->next) {
399 /* exact matching of email address */
400 guint num_addr = complete_address(walk->data);
403 /* skip first item (this is the search string itself) */
405 for (; i < num_addr && !found; i++) {
406 gchar *addr = get_complete_address(i);
407 extract_address(addr);
408 if (strcasecmp(addr, walk->data) == 0) {
412 if (debug_filtering_session
413 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
414 log_print(LOG_DEBUG_FILTERING,
415 "address [ %s ] matches\n",
416 (gchar *)walk->data);
423 if (debug_filtering_session
424 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH
426 log_print(LOG_DEBUG_FILTERING,
427 "address [ %s ] does NOT match\n",
428 (gchar *)walk->data);
432 if (match == MATCH_ALL) {
433 /* if matching all addresses, stop if one doesn't match */
436 if (debug_filtering_session
437 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
438 log_print(LOG_DEBUG_FILTERING,
439 "not all address match (matching all)\n");
443 } else if (match == MATCH_ANY) {
444 /* if matching any address, stop if one does match */
447 if (debug_filtering_session
448 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
449 log_print(LOG_DEBUG_FILTERING,
450 "at least one address matches (matching any)\n");
455 /* MATCH_ONE: there should be only one loop iteration */
458 end_address_completion();
464 *\brief Find out if a string matches a condition
466 *\param prop Matcher structure
467 *\param str String to check
469 *\return gboolean TRUE if str matches the condition in the
472 static gboolean matcherprop_string_match(MatcherProp *prop, const gchar *str,
473 const gchar *debug_context)
477 gboolean ret = FALSE;
478 gboolean should_free = FALSE;
482 if (prop->matchtype == MATCHTYPE_REGEXPCASE ||
483 prop->matchtype == MATCHTYPE_MATCHCASE) {
484 str1 = g_utf8_casefold(str, -1);
485 down_expr = g_utf8_casefold(prop->expr, -1);
489 down_expr = (gchar *)prop->expr;
493 switch (prop->matchtype) {
494 case MATCHTYPE_REGEXPCASE:
495 case MATCHTYPE_REGEXP:
497 if (!prop->preg && (prop->error == 0)) {
498 prop->preg = g_new0(regex_t, 1);
499 /* if regexp then don't use the escaped string */
500 if (regcomp(prop->preg, down_expr,
501 REG_NOSUB | REG_EXTENDED
502 | ((prop->matchtype == MATCHTYPE_REGEXPCASE)
503 ? REG_ICASE : 0)) != 0) {
509 if (prop->preg == NULL) {
514 if (regexec(prop->preg, str1, 0, NULL, 0) == 0)
520 if (debug_filtering_session
521 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
522 gchar *stripped = g_strdup(str);
524 strretchomp(stripped);
526 log_print(LOG_DEBUG_FILTERING,
527 "%s value [ %s ] matches regular expression [ %s ] (%s)\n",
528 debug_context, stripped, prop->expr,
529 prop->matchtype == MATCHTYPE_REGEXP ? _("Case sensitive"):_("Case insensitive"));
531 log_print(LOG_DEBUG_FILTERING,
532 "%s value [ %s ] does NOT match regular expression [ %s ] (%s)\n",
533 debug_context, stripped, prop->expr,
534 prop->matchtype == MATCHTYPE_REGEXP ? _("Case sensitive"):_("Case insensitive"));
540 case MATCHTYPE_MATCHCASE:
541 case MATCHTYPE_MATCH:
542 ret = (strstr(str1, down_expr) != NULL);
545 if (debug_filtering_session
546 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
547 gchar *stripped = g_strdup(str);
549 strretchomp(stripped);
551 log_print(LOG_DEBUG_FILTERING,
552 "%s value [ %s ] contains [ %s ] (%s)\n",
553 debug_context, stripped, prop->expr,
554 prop->matchtype == MATCHTYPE_MATCH ? _("Case sensitive"):_("Case insensitive"));
556 log_print(LOG_DEBUG_FILTERING,
557 "%s value [ %s ] does NOT contain [ %s ] (%s)\n",
558 debug_context, stripped, prop->expr,
559 prop->matchtype == MATCHTYPE_MATCH ? _("Case sensitive"):_("Case insensitive"));
578 *\brief Find out if a tag matches a condition
580 *\param prop Matcher structure
581 *\param msginfo message to check
583 *\return gboolean TRUE if msginfo matches the condition in the
586 static gboolean matcherprop_tag_match(MatcherProp *prop, MsgInfo *msginfo,
587 const gchar *debug_context)
589 gboolean ret = FALSE;
592 if (msginfo == NULL || msginfo->tags == NULL)
595 for (cur = msginfo->tags; cur; cur = cur->next) {
596 const gchar *str = tags_get_tag(GPOINTER_TO_INT(cur->data));
599 if (matcherprop_string_match(prop, str, debug_context)) {
608 *\brief Find out if the string-ed list matches a condition
610 *\param prop Matcher structure
611 *\param list GSList of strings to check
613 *\return gboolean TRUE if str matches the condition in the
616 static gboolean matcherprop_list_match(MatcherProp *prop, const GSList *list,
617 const gchar *debug_context)
621 for(cur = list; cur != NULL; cur = cur->next) {
622 if (matcherprop_string_match(prop, (gchar *)cur->data, debug_context))
628 static gboolean matcherprop_header_line_match(MatcherProp *prop, const gchar *hdr,
629 const gchar *str, const gboolean both,
630 const gchar *debug_context)
632 gboolean res = FALSE;
634 if (hdr == NULL || str == NULL)
638 /* Search in all header names and content.
640 gchar *line = g_strdup_printf("%s %s", hdr, str);
641 res = matcherprop_string_match(prop, line, debug_context);
644 /* Search only in content and exclude private headers.
645 * E.g.: searching for "H foo" in folder x/foo would return
646 * *all* mail as SCF and RMID will match.
647 * Searching for "H sent" would return all resent messages
648 * as "Resent-From: whatever" will match.
650 if (procheader_header_is_internal(hdr))
652 res = matcherprop_string_match(prop, str, debug_context);
659 typedef struct _thread_data {
666 static void *matcher_test_thread(void *data)
668 thread_data *td = (thread_data *)data;
671 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
672 pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
674 result = system(td->cmd);
675 td->done = TRUE; /* let the caller thread join() */
676 return GINT_TO_POINTER(result);
681 *\brief Execute a command defined in the matcher structure
683 *\param prop Pointer to matcher structure
684 *\param info Pointer to message info structure
686 *\return gboolean TRUE if command was executed succesfully
688 static gboolean matcherprop_match_test(const MatcherProp *prop,
697 thread_data *td = g_new0(thread_data, 1);
699 time_t start_time = time(NULL);
702 file = procmsg_get_message_file(info);
711 cmd = matching_build_command(prop->expr, info);
721 if (debug_filtering_session
722 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
723 log_print(LOG_DEBUG_FILTERING,
724 "starting threaded command [ %s ]\n",
730 if (pthread_attr_init(&pta) != 0 ||
731 pthread_attr_setdetachstate(&pta, PTHREAD_CREATE_JOINABLE) != 0 ||
732 pthread_create(&pt, &pta, matcher_test_thread, td) != 0)
733 retval = system(cmd);
735 debug_print("waiting for test thread\n");
737 /* don't let the interface freeze while waiting */
739 if (time(NULL) - start_time > 30) {
745 pthread_join(pt, &res);
746 retval = GPOINTER_TO_INT(res);
747 debug_print(" test thread returned %d\n", retval);
752 if (debug_filtering_session
753 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
754 log_print(LOG_DEBUG_FILTERING,
755 "starting synchronous command [ %s ]\n",
759 retval = system(cmd);
761 debug_print("Command exit code: %d\n", retval);
764 if (debug_filtering_session
765 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
766 log_print(LOG_DEBUG_FILTERING,
767 "command returned [ %d ]\n",
772 return (retval == 0);
776 *\brief Check if a message matches the condition in a matcher
779 *\param prop Pointer to matcher structure
780 *\param info Pointer to message info
782 *\return gboolean TRUE if a match
784 static gboolean matcherprop_match(MatcherProp *prop,
788 gint age_mult_hours = 1;
790 switch(prop->criteria) {
791 case MATCHCRITERIA_ALL:
793 case MATCHCRITERIA_UNREAD:
794 return MSG_IS_UNREAD(info->flags);
795 case MATCHCRITERIA_NOT_UNREAD:
796 return !MSG_IS_UNREAD(info->flags);
797 case MATCHCRITERIA_NEW:
798 return MSG_IS_NEW(info->flags);
799 case MATCHCRITERIA_NOT_NEW:
800 return !MSG_IS_NEW(info->flags);
801 case MATCHCRITERIA_MARKED:
802 return MSG_IS_MARKED(info->flags);
803 case MATCHCRITERIA_NOT_MARKED:
804 return !MSG_IS_MARKED(info->flags);
805 case MATCHCRITERIA_DELETED:
806 return MSG_IS_DELETED(info->flags);
807 case MATCHCRITERIA_NOT_DELETED:
808 return !MSG_IS_DELETED(info->flags);
809 case MATCHCRITERIA_REPLIED:
810 return MSG_IS_REPLIED(info->flags);
811 case MATCHCRITERIA_NOT_REPLIED:
812 return !MSG_IS_REPLIED(info->flags);
813 case MATCHCRITERIA_FORWARDED:
814 return MSG_IS_FORWARDED(info->flags);
815 case MATCHCRITERIA_NOT_FORWARDED:
816 return !MSG_IS_FORWARDED(info->flags);
817 case MATCHCRITERIA_LOCKED:
818 return MSG_IS_LOCKED(info->flags);
819 case MATCHCRITERIA_NOT_LOCKED:
820 return !MSG_IS_LOCKED(info->flags);
821 case MATCHCRITERIA_SPAM:
822 return MSG_IS_SPAM(info->flags);
823 case MATCHCRITERIA_NOT_SPAM:
824 return !MSG_IS_SPAM(info->flags);
825 case MATCHCRITERIA_HAS_ATTACHMENT:
826 return MSG_IS_WITH_ATTACHMENT(info->flags);
827 case MATCHCRITERIA_HAS_NO_ATTACHMENT:
828 return !MSG_IS_WITH_ATTACHMENT(info->flags);
829 case MATCHCRITERIA_SIGNED:
830 return MSG_IS_SIGNED(info->flags);
831 case MATCHCRITERIA_NOT_SIGNED:
832 return !MSG_IS_SIGNED(info->flags);
833 case MATCHCRITERIA_COLORLABEL:
835 gint color = MSG_GET_COLORLABEL_VALUE(info->flags);
836 gboolean ret = (color == prop->value);
839 if (debug_filtering_session
840 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
842 log_print(LOG_DEBUG_FILTERING,
843 "message color value [ %d ] matches color value [ %d ]\n",
846 log_print(LOG_DEBUG_FILTERING,
847 "message color value [ %d ] does NOT match color value [ %d ]\n",
853 case MATCHCRITERIA_NOT_COLORLABEL:
855 gint color = MSG_GET_COLORLABEL_VALUE(info->flags);
856 gboolean ret = (color != prop->value);
859 if (debug_filtering_session
860 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
862 log_print(LOG_DEBUG_FILTERING,
863 "message color value [ %d ] matches color value [ %d ]\n",
866 log_print(LOG_DEBUG_FILTERING,
867 "message color value [ %d ] does NOT match color value [ %d ]\n",
873 case MATCHCRITERIA_IGNORE_THREAD:
874 return MSG_IS_IGNORE_THREAD(info->flags);
875 case MATCHCRITERIA_NOT_IGNORE_THREAD:
876 return !MSG_IS_IGNORE_THREAD(info->flags);
877 case MATCHCRITERIA_WATCH_THREAD:
878 return MSG_IS_WATCH_THREAD(info->flags);
879 case MATCHCRITERIA_NOT_WATCH_THREAD:
880 return !MSG_IS_WATCH_THREAD(info->flags);
881 case MATCHCRITERIA_SUBJECT:
882 return matcherprop_string_match(prop, info->subject, context_str[CONTEXT_SUBJECT]);
883 case MATCHCRITERIA_NOT_SUBJECT:
884 return !matcherprop_string_match(prop, info->subject, context_str[CONTEXT_SUBJECT]);
885 case MATCHCRITERIA_FROM:
886 return matcherprop_string_match(prop, info->from, context_str[CONTEXT_FROM]);
887 case MATCHCRITERIA_NOT_FROM:
888 return !matcherprop_string_match(prop, info->from, context_str[CONTEXT_FROM]);
889 case MATCHCRITERIA_TO:
890 return matcherprop_string_match(prop, info->to, context_str[CONTEXT_TO]);
891 case MATCHCRITERIA_NOT_TO:
892 return !matcherprop_string_match(prop, info->to, context_str[CONTEXT_TO]);
893 case MATCHCRITERIA_CC:
894 return matcherprop_string_match(prop, info->cc, context_str[CONTEXT_CC]);
895 case MATCHCRITERIA_NOT_CC:
896 return !matcherprop_string_match(prop, info->cc, context_str[CONTEXT_CC]);
897 case MATCHCRITERIA_TO_OR_CC:
898 return matcherprop_string_match(prop, info->to, context_str[CONTEXT_TO])
899 || matcherprop_string_match(prop, info->cc, context_str[CONTEXT_CC]);
900 case MATCHCRITERIA_NOT_TO_AND_NOT_CC:
901 return !matcherprop_string_match(prop, info->to, context_str[CONTEXT_TO])
902 && !matcherprop_string_match(prop, info->cc, context_str[CONTEXT_CC]);
903 case MATCHCRITERIA_TAG:
904 return matcherprop_tag_match(prop, info, context_str[CONTEXT_TAG]);
905 case MATCHCRITERIA_NOT_TAG:
906 return !matcherprop_tag_match(prop, info, context_str[CONTEXT_TAG]);
907 case MATCHCRITERIA_TAGGED:
908 return info->tags != NULL;
909 case MATCHCRITERIA_NOT_TAGGED:
910 return info->tags == NULL;
911 case MATCHCRITERIA_AGE_GREATER:
913 /* Fallthrough intended */
914 case MATCHCRITERIA_AGE_GREATER_HOURS:
920 age = ((t - info->date_t) / (60 * 60 * age_mult_hours));
921 ret = (age >= prop->value);
924 if (debug_filtering_session
925 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
927 log_print(LOG_DEBUG_FILTERING,
928 "message age [ %d ] is greater than [ %d ]\n",
931 log_print(LOG_DEBUG_FILTERING,
932 "message age [ %d ] is not greater than [ %d ]\n",
938 case MATCHCRITERIA_AGE_LOWER:
940 /* Fallthrough intended */
941 case MATCHCRITERIA_AGE_LOWER_HOURS:
947 age = ((t - info->date_t) / (60 * 60 * age_mult_hours));
948 ret = (age < prop->value);
951 if (debug_filtering_session
952 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
954 log_print(LOG_DEBUG_FILTERING,
955 "message age [ %d ] is lower than [ %d ]\n",
958 log_print(LOG_DEBUG_FILTERING,
959 "message age [ %d ] is not lower than [ %d ]\n",
965 case MATCHCRITERIA_SCORE_GREATER:
967 gboolean ret = (info->score > prop->value);
970 if (debug_filtering_session
971 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
973 log_print(LOG_DEBUG_FILTERING,
974 "message score [ %d ] is greater than [ %d ]\n",
975 info->score, prop->value);
977 log_print(LOG_DEBUG_FILTERING,
978 "message score [ %d ] is not greater than [ %d ]\n",
979 info->score, prop->value);
984 case MATCHCRITERIA_SCORE_LOWER:
986 gboolean ret = (info->score < prop->value);
989 if (debug_filtering_session
990 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
992 log_print(LOG_DEBUG_FILTERING,
993 "message score [ %d ] is lower than [ %d ]\n",
994 info->score, prop->value);
996 log_print(LOG_DEBUG_FILTERING,
997 "message score [ %d ] is not lower than [ %d ]\n",
998 info->score, prop->value);
1003 case MATCHCRITERIA_SCORE_EQUAL:
1005 gboolean ret = (info->score == prop->value);
1008 if (debug_filtering_session
1009 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
1011 log_print(LOG_DEBUG_FILTERING,
1012 "message score [ %d ] is equal to [ %d ]\n",
1013 info->score, prop->value);
1015 log_print(LOG_DEBUG_FILTERING,
1016 "message score [ %d ] is not equal to [ %d ]\n",
1017 info->score, prop->value);
1022 case MATCHCRITERIA_SIZE_GREATER:
1024 /* FIXME: info->size is a goffset */
1025 gboolean ret = (info->size > (goffset) prop->value);
1028 if (debug_filtering_session
1029 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
1031 log_print(LOG_DEBUG_FILTERING,
1032 "message size is greater than [ %d ]\n",
1035 log_print(LOG_DEBUG_FILTERING,
1036 "message size is not greater than [ %d ]\n",
1042 case MATCHCRITERIA_SIZE_SMALLER:
1044 /* FIXME: info->size is a goffset */
1045 gboolean ret = (info->size < (goffset) prop->value);
1048 if (debug_filtering_session
1049 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
1051 log_print(LOG_DEBUG_FILTERING,
1052 "message size is smaller than [ %d ]\n",
1055 log_print(LOG_DEBUG_FILTERING,
1056 "message size is not smaller than [ %d ]\n",
1062 case MATCHCRITERIA_SIZE_EQUAL:
1064 /* FIXME: info->size is a goffset */
1065 gboolean ret = (info->size == (goffset) prop->value);
1068 if (debug_filtering_session
1069 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
1071 log_print(LOG_DEBUG_FILTERING,
1072 "message size is equal to [ %d ]\n",
1075 log_print(LOG_DEBUG_FILTERING,
1076 "message size is not equal to [ %d ]\n",
1082 case MATCHCRITERIA_PARTIAL:
1084 /* FIXME: info->size is a goffset */
1085 gboolean ret = (info->total_size != 0 && info->size != (goffset)info->total_size);
1088 if (debug_filtering_session
1089 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
1091 log_print(LOG_DEBUG_FILTERING,
1092 "message is partially downloaded, size is less than total size [ %d ])\n",
1095 log_print(LOG_DEBUG_FILTERING,
1096 "message is not partially downloaded\n");
1101 case MATCHCRITERIA_NOT_PARTIAL:
1103 /* FIXME: info->size is a goffset */
1104 gboolean ret = (info->total_size == 0 || info->size == (goffset)info->total_size);
1107 if (debug_filtering_session
1108 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
1110 log_print(LOG_DEBUG_FILTERING,
1111 "message is not partially downloaded\n");
1113 log_print(LOG_DEBUG_FILTERING,
1114 "message is partially downloaded, size is less than total size [ %d ])\n",
1120 case MATCHCRITERIA_NEWSGROUPS:
1121 return matcherprop_string_match(prop, info->newsgroups, context_str[CONTEXT_NEWSGROUPS]);
1122 case MATCHCRITERIA_NOT_NEWSGROUPS:
1123 return !matcherprop_string_match(prop, info->newsgroups, context_str[CONTEXT_NEWSGROUPS]);
1124 case MATCHCRITERIA_MESSAGEID:
1125 return matcherprop_string_match(prop, info->msgid, context_str[CONTEXT_MESSAGEID]);
1126 case MATCHCRITERIA_NOT_MESSAGEID:
1127 return !matcherprop_string_match(prop, info->msgid, context_str[CONTEXT_MESSAGEID]);
1128 case MATCHCRITERIA_INREPLYTO:
1129 return matcherprop_string_match(prop, info->inreplyto, context_str[CONTEXT_IN_REPLY_TO]);
1130 case MATCHCRITERIA_NOT_INREPLYTO:
1131 return !matcherprop_string_match(prop, info->inreplyto, context_str[CONTEXT_IN_REPLY_TO]);
1132 case MATCHCRITERIA_REFERENCES:
1133 return matcherprop_list_match(prop, info->references, context_str[CONTEXT_REFERENCES]);
1134 case MATCHCRITERIA_NOT_REFERENCES:
1135 return !matcherprop_list_match(prop, info->references, context_str[CONTEXT_REFERENCES]);
1136 case MATCHCRITERIA_TEST:
1137 return matcherprop_match_test(prop, info);
1138 case MATCHCRITERIA_NOT_TEST:
1139 return !matcherprop_match_test(prop, info);
1145 /* ********************* MatcherList *************************** */
1148 *\brief Create a new list of matchers
1150 *\param matchers List of matcher structures
1151 *\param bool_and Operator
1153 *\return MatcherList * New list
1155 MatcherList *matcherlist_new(GSList *matchers, gboolean bool_and)
1159 cond = g_new0(MatcherList, 1);
1161 cond->matchers = matchers;
1162 cond->bool_and = bool_and;
1169 *\brief Builds a single regular expresion from an array of srings.
1171 *\param strings The lines containing the different sub-regexp.
1173 *\return The newly allocated regexp string.
1175 static gchar *build_complete_regexp(gchar **strings)
1179 while (strings && strings[i] && *strings[i]) {
1180 int old_len = expr ? strlen(expr):0;
1182 gchar *tmpstr = NULL;
1184 if (g_utf8_validate(strings[i], -1, NULL))
1185 tmpstr = g_strdup(strings[i]);
1187 tmpstr = conv_codeset_strdup(strings[i],
1188 conv_get_locale_charset_str_no_utf8(),
1191 if (strstr(tmpstr, "\n"))
1192 *(strstr(tmpstr, "\n")) = '\0';
1194 new_len = strlen(tmpstr);
1196 expr = g_realloc(expr,
1197 expr ? (old_len + strlen("|()") + new_len + 1)
1198 : (strlen("()") + new_len + 1));
1201 strcpy(expr + old_len, "|(");
1202 strcpy(expr + old_len + 2, tmpstr);
1203 strcpy(expr + old_len + 2 + new_len, ")");
1205 strcpy(expr+old_len, "(");
1206 strcpy(expr+old_len + 1, tmpstr);
1207 strcpy(expr+old_len + 1 + new_len, ")");
1217 *\brief Create a new list of matchers from a multi-line string
1219 *\param lines String with "\n"-separated expressions
1220 *\param bool_and Operator
1221 *\param case_sensitive If the matching is case sensitive or not
1223 *\return MatcherList * New matcher list
1225 MatcherList *matcherlist_new_from_lines(gchar *lines, gboolean bool_and,
1226 gboolean case_sensitive)
1228 MatcherProp *m = NULL;
1229 GSList *matchers = NULL;
1230 gchar **strings = g_strsplit(lines, "\n", -1);
1234 expr = build_complete_regexp(strings);
1235 debug_print("building matcherprop for expr '%s'\n", expr?expr:"NULL");
1237 m = matcherprop_new(MATCHCRITERIA_SUBJECT, NULL,
1238 case_sensitive? MATCHTYPE_REGEXP: MATCHTYPE_REGEXPCASE,
1241 /* print error message */
1242 debug_print("failed to allocate memory for matcherprop\n");
1244 matchers = g_slist_append(matchers, m);
1250 while (strings && strings[i] && *strings[i]) {
1251 m = matcherprop_new(MATCHCRITERIA_SUBJECT, NULL,
1252 case_sensitive? MATCHTYPE_MATCH: MATCHTYPE_MATCHCASE,
1255 /* print error message */
1256 debug_print("failed to allocate memory for matcherprop\n");
1258 matchers = g_slist_append(matchers, m);
1263 g_strfreev(strings);
1265 return matcherlist_new(matchers, bool_and);
1269 *\brief Frees a list of matchers
1271 *\param cond List of matchers
1273 void matcherlist_free(MatcherList *cond)
1277 cm_return_if_fail(cond);
1278 for (l = cond->matchers ; l != NULL ; l = g_slist_next(l)) {
1279 matcherprop_free((MatcherProp *) l->data);
1281 g_slist_free(cond->matchers);
1286 *\brief Skip all headers in a message file
1288 *\param fp Message file
1290 static void matcherlist_skip_headers(FILE *fp)
1292 gchar buf[BUFFSIZE];
1294 while (procheader_get_one_field(buf, sizeof(buf), fp, NULL) != -1)
1299 *\brief Check if a header matches a matcher condition
1301 *\param matcher Matcher structure to check header for
1302 *\param buf Header name
1304 *\return boolean TRUE if matching header
1306 static gboolean matcherprop_match_one_header(MatcherProp *matcher,
1309 gboolean result = FALSE;
1310 Header *header = NULL;
1312 switch (matcher->criteria) {
1313 case MATCHCRITERIA_HEADER:
1314 case MATCHCRITERIA_NOT_HEADER:
1315 header = procheader_parse_header(buf);
1318 if (procheader_headername_equal(header->name,
1320 if (matcher->criteria == MATCHCRITERIA_HEADER)
1321 result = matcherprop_string_match(matcher, header->body, context_str[CONTEXT_HEADER]);
1323 result = !matcherprop_string_match(matcher, header->body, context_str[CONTEXT_HEADER]);
1324 procheader_header_free(header);
1328 procheader_header_free(header);
1331 case MATCHCRITERIA_HEADERS_PART:
1332 case MATCHCRITERIA_HEADERS_CONT:
1333 case MATCHCRITERIA_MESSAGE:
1334 header = procheader_parse_header(buf);
1337 result = matcherprop_header_line_match(matcher,
1338 header->name, header->body,
1339 (matcher->criteria == MATCHCRITERIA_HEADERS_PART),
1340 context_str[CONTEXT_HEADER_LINE]);
1341 procheader_header_free(header);
1343 case MATCHCRITERIA_NOT_HEADERS_CONT:
1344 case MATCHCRITERIA_NOT_HEADERS_PART:
1345 case MATCHCRITERIA_NOT_MESSAGE:
1346 header = procheader_parse_header(buf);
1349 result = !matcherprop_header_line_match(matcher,
1350 header->name, header->body,
1351 (matcher->criteria == MATCHCRITERIA_NOT_HEADERS_PART),
1352 context_str[CONTEXT_HEADER_LINE]);
1353 procheader_header_free(header);
1355 case MATCHCRITERIA_FOUND_IN_ADDRESSBOOK:
1356 case MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK:
1358 GSList *address_list = NULL;
1359 gint match = MATCH_ONE;
1360 gboolean found = FALSE;
1362 /* how many address headers are we trying to match? */
1363 if (strcasecmp(matcher->header, "Any") == 0)
1365 else if (strcasecmp(matcher->header, "All") == 0)
1368 if (match == MATCH_ONE) {
1369 /* matching one address header exactly, is that the right one? */
1370 header = procheader_parse_header(buf);
1372 !procheader_headername_equal(header->name, matcher->header))
1374 address_list = address_list_append(address_list, header->body);
1375 if (address_list == NULL)
1379 header = procheader_parse_header(buf);
1382 /* address header is one of the headers we have to match when checking
1383 for any address header or all address headers? */
1384 if (procheader_headername_equal(header->name, "From") ||
1385 procheader_headername_equal(header->name, "To") ||
1386 procheader_headername_equal(header->name, "Cc") ||
1387 procheader_headername_equal(header->name, "Reply-To") ||
1388 procheader_headername_equal(header->name, "Sender"))
1389 address_list = address_list_append(address_list, header->body);
1390 if (address_list == NULL)
1394 found = match_with_addresses_in_addressbook
1395 (matcher, address_list, matcher->criteria,
1396 matcher->expr, match);
1397 g_slist_free(address_list);
1399 if (matcher->criteria == MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK)
1410 *\brief Check if the matcher structure wants headers to
1413 *\param matcher Matcher structure
1415 *\return gboolean TRUE if the matcher structure describes
1416 * a header match condition
1418 static gboolean matcherprop_criteria_headers(const MatcherProp *matcher)
1420 switch (matcher->criteria) {
1421 case MATCHCRITERIA_HEADER:
1422 case MATCHCRITERIA_NOT_HEADER:
1423 case MATCHCRITERIA_HEADERS_PART:
1424 case MATCHCRITERIA_HEADERS_CONT:
1425 case MATCHCRITERIA_NOT_HEADERS_PART:
1426 case MATCHCRITERIA_NOT_HEADERS_CONT:
1427 case MATCHCRITERIA_FOUND_IN_ADDRESSBOOK:
1428 case MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK:
1436 *\brief Check if the matcher structure wants the message
1437 * to be matched (just perform an action on any
1440 *\param matcher Matcher structure
1442 *\return gboolean TRUE if matcher condition should match
1445 static gboolean matcherprop_criteria_message(MatcherProp *matcher)
1447 switch (matcher->criteria) {
1448 case MATCHCRITERIA_MESSAGE:
1449 case MATCHCRITERIA_NOT_MESSAGE:
1457 *\brief Check if a list of conditions matches one header in
1460 *\param matchers List of conditions
1461 *\param fp Message file
1463 *\return gboolean TRUE if one of the headers is matched by
1464 * the list of conditions.
1466 static gboolean matcherlist_match_headers(MatcherList *matchers, FILE *fp)
1469 gchar buf[BUFFSIZE];
1471 while (procheader_get_one_field(buf, sizeof(buf), fp, NULL) != -1) {
1472 for (l = matchers->matchers ; l != NULL ; l = g_slist_next(l)) {
1473 MatcherProp *matcher = (MatcherProp *) l->data;
1474 gint match = MATCH_ANY;
1479 /* determine the match range (all, any are our concern here) */
1480 if (matcher->criteria == MATCHCRITERIA_NOT_HEADERS_PART ||
1481 matcher->criteria == MATCHCRITERIA_NOT_HEADERS_CONT ||
1482 matcher->criteria == MATCHCRITERIA_NOT_MESSAGE) {
1485 } else if (matcher->criteria == MATCHCRITERIA_FOUND_IN_ADDRESSBOOK ||
1486 matcher->criteria == MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK) {
1487 Header *header = NULL;
1489 /* address header is one of the headers we have to match when checking
1490 for any address header or all address headers? */
1491 header = procheader_parse_header(buf);
1493 (procheader_headername_equal(header->name, "From") ||
1494 procheader_headername_equal(header->name, "To") ||
1495 procheader_headername_equal(header->name, "Cc") ||
1496 procheader_headername_equal(header->name, "Reply-To") ||
1497 procheader_headername_equal(header->name, "Sender"))) {
1499 if (strcasecmp(matcher->header, "Any") == 0)
1501 else if (strcasecmp(matcher->header, "All") == 0)
1506 /* further call to matcherprop_match_one_header() can't match
1507 and it irrelevant, so: don't alter the match result */
1512 /* ZERO line must NOT match for the rule to match.
1514 if (match == MATCH_ALL) {
1515 if (matcherprop_match_one_header(matcher, buf)) {
1516 matcher->result = TRUE;
1518 matcher->result = FALSE;
1519 matcher->done = TRUE;
1521 /* else, just one line matching is enough for the rule to match
1523 } else if (matcherprop_criteria_headers(matcher) ||
1524 matcherprop_criteria_message(matcher)) {
1525 if (matcherprop_match_one_header(matcher, buf)) {
1526 matcher->result = TRUE;
1527 matcher->done = TRUE;
1531 /* if the rule matched and the matchers are OR, no need to
1532 * check the others */
1533 if (matcher->result && matcher->done) {
1534 if (!matchers->bool_and)
1544 *\brief Check if a matcher wants to check the message body
1546 *\param matcher Matcher structure
1548 *\return gboolean TRUE if body must be matched.
1550 static gboolean matcherprop_criteria_body(const MatcherProp *matcher)
1552 switch (matcher->criteria) {
1553 case MATCHCRITERIA_BODY_PART:
1554 case MATCHCRITERIA_NOT_BODY_PART:
1561 static gboolean matcherlist_match_binary_content(MatcherList *matchers, MimeInfo *partinfo)
1564 gchar buf[BUFFSIZE];
1567 if (!partinfo || partinfo->type == MIMETYPE_TEXT)
1570 outfp = procmime_get_binary_content(partinfo);
1575 while (fgets(buf, sizeof(buf), outfp) != NULL) {
1578 for (l = matchers->matchers ; l != NULL ; l = g_slist_next(l)) {
1579 MatcherProp *matcher = (MatcherProp *) l->data;
1584 /* Don't scan non-text parts when looking in body, only
1585 * when looking in whole message
1587 if (matcher->criteria == MATCHCRITERIA_NOT_BODY_PART ||
1588 matcher->criteria == MATCHCRITERIA_BODY_PART)
1591 /* if the criteria is ~body_part or ~message, ZERO lines
1592 * must match for the rule to match.
1594 if (matcher->criteria == MATCHCRITERIA_NOT_BODY_PART ||
1595 matcher->criteria == MATCHCRITERIA_NOT_MESSAGE) {
1596 if (matcherprop_string_match(matcher, buf,
1597 context_str[CONTEXT_BODY_LINE])) {
1598 matcher->result = FALSE;
1599 matcher->done = TRUE;
1601 matcher->result = TRUE;
1602 /* else, just one line has to match */
1603 } else if (matcherprop_criteria_body(matcher) ||
1604 matcherprop_criteria_message(matcher)) {
1605 if (matcherprop_string_match(matcher, buf,
1606 context_str[CONTEXT_BODY_LINE])) {
1607 matcher->result = TRUE;
1608 matcher->done = TRUE;
1612 /* if the matchers are OR'ed and the rule matched,
1613 * no need to check the others. */
1614 if (matcher->result && matcher->done) {
1615 if (!matchers->bool_and) {
1627 static gboolean match_content_cb(const gchar *buf, gpointer data)
1629 MatcherList *matchers = (MatcherList *)data;
1630 gboolean all_done = TRUE;
1633 for (l = matchers->matchers ; l != NULL ; l = g_slist_next(l)) {
1634 MatcherProp *matcher = (MatcherProp *) l->data;
1639 /* if the criteria is ~body_part or ~message, ZERO lines
1640 * must match for the rule to match.
1642 if (matcher->criteria == MATCHCRITERIA_NOT_BODY_PART ||
1643 matcher->criteria == MATCHCRITERIA_NOT_MESSAGE) {
1644 if (matcherprop_string_match(matcher, buf,
1645 context_str[CONTEXT_BODY_LINE])) {
1646 matcher->result = FALSE;
1647 matcher->done = TRUE;
1649 matcher->result = TRUE;
1650 /* else, just one line has to match */
1651 } else if (matcherprop_criteria_body(matcher) ||
1652 matcherprop_criteria_message(matcher)) {
1653 if (matcherprop_string_match(matcher, buf,
1654 context_str[CONTEXT_BODY_LINE])) {
1655 matcher->result = TRUE;
1656 matcher->done = TRUE;
1660 /* if the matchers are OR'ed and the rule matched,
1661 * no need to check the others. */
1662 if (matcher->result && matcher->done) {
1663 if (!matchers->bool_and) {
1674 static gboolean matcherlist_match_text_content(MatcherList *matchers, MimeInfo *partinfo)
1676 if (partinfo->type != MIMETYPE_TEXT)
1679 return procmime_scan_text_content(partinfo, match_content_cb, matchers);
1683 *\brief Check if a line in a message file's body matches
1686 *\param matchers List of conditions
1687 *\param fp Message file
1689 *\return gboolean TRUE if succesful match
1691 static gboolean matcherlist_match_body(MatcherList *matchers, gboolean body_only, MsgInfo *info)
1693 MimeInfo *mimeinfo = NULL;
1694 MimeInfo *partinfo = NULL;
1695 gboolean first_text_found = FALSE;
1697 cm_return_val_if_fail(info != NULL, FALSE);
1699 mimeinfo = procmime_scan_message(info);
1702 partinfo = procmime_mimeinfo_next(mimeinfo);
1704 for (; partinfo != NULL; partinfo = procmime_mimeinfo_next(partinfo)) {
1706 if (partinfo->type != MIMETYPE_TEXT && body_only)
1709 if (partinfo->type == MIMETYPE_TEXT) {
1710 first_text_found = TRUE;
1711 if (matcherlist_match_text_content(matchers, partinfo)) {
1712 procmime_mimeinfo_free_all(mimeinfo);
1715 } else if (matcherlist_match_binary_content(matchers, partinfo)) {
1716 procmime_mimeinfo_free_all(mimeinfo);
1720 if (body_only && first_text_found)
1723 procmime_mimeinfo_free_all(mimeinfo);
1729 *\brief Check if a message file matches criteria
1731 *\param matchers Criteria
1732 *\param info Message info
1733 *\param result Default result
1735 *\return gboolean TRUE if matched
1737 static gboolean matcherlist_match_file(MatcherList *matchers, MsgInfo *info,
1740 gboolean read_headers;
1747 /* file need to be read ? */
1749 read_headers = FALSE;
1752 for (l = matchers->matchers ; l != NULL ; l = g_slist_next(l)) {
1753 MatcherProp *matcher = (MatcherProp *) l->data;
1755 if (matcherprop_criteria_headers(matcher))
1756 read_headers = TRUE;
1757 if (matcherprop_criteria_body(matcher))
1759 if (matcherprop_criteria_message(matcher)) {
1760 read_headers = TRUE;
1764 matcher->result = FALSE;
1765 matcher->done = FALSE;
1768 if (!read_headers && !read_body)
1771 file = procmsg_get_message_file_full(info, read_headers, read_body);
1775 if ((fp = g_fopen(file, "rb")) == NULL) {
1776 FILE_OP_ERROR(file, "fopen");
1781 /* read the headers */
1784 if (matcherlist_match_headers(matchers, fp))
1787 matcherlist_skip_headers(fp);
1792 matcherlist_match_body(matchers, body_only, info);
1795 for (l = matchers->matchers; l != NULL; l = g_slist_next(l)) {
1796 MatcherProp *matcher = (MatcherProp *) l->data;
1798 if (matcherprop_criteria_headers(matcher) ||
1799 matcherprop_criteria_body(matcher) ||
1800 matcherprop_criteria_message(matcher)) {
1801 if (matcher->result) {
1802 if (!matchers->bool_and) {
1808 if (matchers->bool_and) {
1824 *\brief Test list of conditions on a message.
1826 *\param matchers List of conditions
1827 *\param info Message info
1829 *\return gboolean TRUE if matched
1831 gboolean matcherlist_match(MatcherList *matchers, MsgInfo *info)
1839 if (matchers->bool_and)
1844 /* test the cached elements */
1846 for (l = matchers->matchers; l != NULL ;l = g_slist_next(l)) {
1847 MatcherProp *matcher = (MatcherProp *) l->data;
1849 if (debug_filtering_session) {
1850 gchar *buf = matcherprop_to_string(matcher);
1851 log_print(LOG_DEBUG_FILTERING, _("checking if message matches [ %s ]\n"), buf);
1855 switch(matcher->criteria) {
1856 case MATCHCRITERIA_ALL:
1857 case MATCHCRITERIA_UNREAD:
1858 case MATCHCRITERIA_NOT_UNREAD:
1859 case MATCHCRITERIA_NEW:
1860 case MATCHCRITERIA_NOT_NEW:
1861 case MATCHCRITERIA_MARKED:
1862 case MATCHCRITERIA_NOT_MARKED:
1863 case MATCHCRITERIA_DELETED:
1864 case MATCHCRITERIA_NOT_DELETED:
1865 case MATCHCRITERIA_REPLIED:
1866 case MATCHCRITERIA_NOT_REPLIED:
1867 case MATCHCRITERIA_FORWARDED:
1868 case MATCHCRITERIA_NOT_FORWARDED:
1869 case MATCHCRITERIA_LOCKED:
1870 case MATCHCRITERIA_NOT_LOCKED:
1871 case MATCHCRITERIA_SPAM:
1872 case MATCHCRITERIA_NOT_SPAM:
1873 case MATCHCRITERIA_HAS_ATTACHMENT:
1874 case MATCHCRITERIA_HAS_NO_ATTACHMENT:
1875 case MATCHCRITERIA_SIGNED:
1876 case MATCHCRITERIA_NOT_SIGNED:
1877 case MATCHCRITERIA_COLORLABEL:
1878 case MATCHCRITERIA_NOT_COLORLABEL:
1879 case MATCHCRITERIA_IGNORE_THREAD:
1880 case MATCHCRITERIA_NOT_IGNORE_THREAD:
1881 case MATCHCRITERIA_WATCH_THREAD:
1882 case MATCHCRITERIA_NOT_WATCH_THREAD:
1883 case MATCHCRITERIA_SUBJECT:
1884 case MATCHCRITERIA_NOT_SUBJECT:
1885 case MATCHCRITERIA_FROM:
1886 case MATCHCRITERIA_NOT_FROM:
1887 case MATCHCRITERIA_TO:
1888 case MATCHCRITERIA_NOT_TO:
1889 case MATCHCRITERIA_CC:
1890 case MATCHCRITERIA_NOT_CC:
1891 case MATCHCRITERIA_TO_OR_CC:
1892 case MATCHCRITERIA_NOT_TO_AND_NOT_CC:
1893 case MATCHCRITERIA_TAG:
1894 case MATCHCRITERIA_NOT_TAG:
1895 case MATCHCRITERIA_TAGGED:
1896 case MATCHCRITERIA_NOT_TAGGED:
1897 case MATCHCRITERIA_AGE_GREATER:
1898 case MATCHCRITERIA_AGE_LOWER:
1899 case MATCHCRITERIA_AGE_GREATER_HOURS:
1900 case MATCHCRITERIA_AGE_LOWER_HOURS:
1901 case MATCHCRITERIA_NEWSGROUPS:
1902 case MATCHCRITERIA_NOT_NEWSGROUPS:
1903 case MATCHCRITERIA_MESSAGEID:
1904 case MATCHCRITERIA_NOT_MESSAGEID:
1905 case MATCHCRITERIA_INREPLYTO:
1906 case MATCHCRITERIA_NOT_INREPLYTO:
1907 case MATCHCRITERIA_REFERENCES:
1908 case MATCHCRITERIA_NOT_REFERENCES:
1909 case MATCHCRITERIA_SCORE_GREATER:
1910 case MATCHCRITERIA_SCORE_LOWER:
1911 case MATCHCRITERIA_SCORE_EQUAL:
1912 case MATCHCRITERIA_SIZE_GREATER:
1913 case MATCHCRITERIA_SIZE_SMALLER:
1914 case MATCHCRITERIA_SIZE_EQUAL:
1915 case MATCHCRITERIA_TEST:
1916 case MATCHCRITERIA_NOT_TEST:
1917 case MATCHCRITERIA_PARTIAL:
1918 case MATCHCRITERIA_NOT_PARTIAL:
1919 if (matcherprop_match(matcher, info)) {
1920 if (!matchers->bool_and) {
1921 if (debug_filtering_session)
1922 log_status_ok(LOG_DEBUG_FILTERING, _("message matches\n"));
1927 if (matchers->bool_and) {
1928 if (debug_filtering_session)
1929 log_status_nok(LOG_DEBUG_FILTERING, _("message does not match\n"));
1936 /* test the condition on the file */
1938 if (matcherlist_match_file(matchers, info, result)) {
1939 if (!matchers->bool_and) {
1940 if (debug_filtering_session)
1941 log_status_ok(LOG_DEBUG_FILTERING, _("message matches\n"));
1945 if (matchers->bool_and) {
1946 if (debug_filtering_session)
1947 log_status_nok(LOG_DEBUG_FILTERING, _("message does not match\n"));
1952 if (debug_filtering_session) {
1954 log_status_ok(LOG_DEBUG_FILTERING, _("message matches\n"));
1956 log_status_nok(LOG_DEBUG_FILTERING, _("message does not match\n"));
1962 static gint quote_filter_str(gchar * result, guint size,
1972 for(p = path ; * p != '\0' ; p ++) {
1974 if ((* p != '\"') && (* p != '\\')) {
1975 if (remaining > 0) {
1981 result[size - 1] = '\0';
1986 if (remaining >= 2) {
1994 result[size - 1] = '\0';
1999 if (remaining > 0) {
2003 result[size - 1] = '\0';
2011 gchar * matcher_quote_str(const gchar * src)
2016 len = strlen(src) * 2 + 1;
2017 res = g_malloc(len);
2018 quote_filter_str(res, len, src);
2024 *\brief Convert a matcher structure to a string
2026 *\param matcher Matcher structure
2028 *\return gchar * Newly allocated string
2030 gchar *matcherprop_to_string(MatcherProp *matcher)
2032 gchar *matcher_str = NULL;
2033 const gchar *criteria_str;
2034 const gchar *matchtype_str;
2036 gchar * quoted_expr;
2037 gchar * quoted_header;
2039 criteria_str = NULL;
2040 for (i = 0; i < (int) (sizeof(matchparser_tab) / sizeof(MatchParser)); i++) {
2041 if (matchparser_tab[i].id == matcher->criteria)
2042 criteria_str = matchparser_tab[i].str;
2044 if (criteria_str == NULL)
2047 switch (matcher->criteria) {
2048 case MATCHCRITERIA_AGE_GREATER:
2049 case MATCHCRITERIA_AGE_LOWER:
2050 case MATCHCRITERIA_AGE_GREATER_HOURS:
2051 case MATCHCRITERIA_AGE_LOWER_HOURS:
2052 case MATCHCRITERIA_SCORE_GREATER:
2053 case MATCHCRITERIA_SCORE_LOWER:
2054 case MATCHCRITERIA_SCORE_EQUAL:
2055 case MATCHCRITERIA_SIZE_GREATER:
2056 case MATCHCRITERIA_SIZE_SMALLER:
2057 case MATCHCRITERIA_SIZE_EQUAL:
2058 case MATCHCRITERIA_COLORLABEL:
2059 case MATCHCRITERIA_NOT_COLORLABEL:
2060 return g_strdup_printf("%s %i", criteria_str, matcher->value);
2061 case MATCHCRITERIA_ALL:
2062 case MATCHCRITERIA_UNREAD:
2063 case MATCHCRITERIA_NOT_UNREAD:
2064 case MATCHCRITERIA_NEW:
2065 case MATCHCRITERIA_NOT_NEW:
2066 case MATCHCRITERIA_MARKED:
2067 case MATCHCRITERIA_NOT_MARKED:
2068 case MATCHCRITERIA_DELETED:
2069 case MATCHCRITERIA_NOT_DELETED:
2070 case MATCHCRITERIA_REPLIED:
2071 case MATCHCRITERIA_NOT_REPLIED:
2072 case MATCHCRITERIA_FORWARDED:
2073 case MATCHCRITERIA_NOT_FORWARDED:
2074 case MATCHCRITERIA_LOCKED:
2075 case MATCHCRITERIA_NOT_LOCKED:
2076 case MATCHCRITERIA_SPAM:
2077 case MATCHCRITERIA_NOT_SPAM:
2078 case MATCHCRITERIA_HAS_ATTACHMENT:
2079 case MATCHCRITERIA_HAS_NO_ATTACHMENT:
2080 case MATCHCRITERIA_SIGNED:
2081 case MATCHCRITERIA_NOT_SIGNED:
2082 case MATCHCRITERIA_PARTIAL:
2083 case MATCHCRITERIA_NOT_PARTIAL:
2084 case MATCHCRITERIA_IGNORE_THREAD:
2085 case MATCHCRITERIA_NOT_IGNORE_THREAD:
2086 case MATCHCRITERIA_WATCH_THREAD:
2087 case MATCHCRITERIA_NOT_WATCH_THREAD:
2088 case MATCHCRITERIA_TAGGED:
2089 case MATCHCRITERIA_NOT_TAGGED:
2090 return g_strdup(criteria_str);
2091 case MATCHCRITERIA_TEST:
2092 case MATCHCRITERIA_NOT_TEST:
2093 quoted_expr = matcher_quote_str(matcher->expr);
2094 matcher_str = g_strdup_printf("%s \"%s\"",
2095 criteria_str, quoted_expr);
2096 g_free(quoted_expr);
2098 case MATCHCRITERIA_FOUND_IN_ADDRESSBOOK:
2099 case MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK:
2100 quoted_header = matcher_quote_str(matcher->header);
2101 quoted_expr = matcher_quote_str(matcher->expr);
2102 matcher_str = g_strdup_printf("%s \"%s\" in \"%s\"",
2103 criteria_str, quoted_header, quoted_expr);
2104 g_free(quoted_header);
2105 g_free(quoted_expr);
2109 matchtype_str = NULL;
2110 for (i = 0; i < sizeof matchparser_tab / sizeof matchparser_tab[0]; i++) {
2111 if (matchparser_tab[i].id == matcher->matchtype)
2112 matchtype_str = matchparser_tab[i].str;
2115 if (matchtype_str == NULL)
2118 switch (matcher->matchtype) {
2119 case MATCHTYPE_MATCH:
2120 case MATCHTYPE_MATCHCASE:
2121 case MATCHTYPE_REGEXP:
2122 case MATCHTYPE_REGEXPCASE:
2123 quoted_expr = matcher_quote_str(matcher->expr);
2124 if (matcher->header) {
2125 quoted_header = matcher_quote_str(matcher->header);
2126 matcher_str = g_strdup_printf
2127 ("%s \"%s\" %s \"%s\"",
2128 criteria_str, quoted_header,
2129 matchtype_str, quoted_expr);
2130 g_free(quoted_header);
2133 matcher_str = g_strdup_printf
2134 ("%s %s \"%s\"", criteria_str,
2135 matchtype_str, quoted_expr);
2136 g_free(quoted_expr);
2144 *\brief Convert a list of conditions to a string
2146 *\param matchers List of conditions
2148 *\return gchar * Newly allocated string
2150 gchar *matcherlist_to_string(const MatcherList *matchers)
2156 gchar *result = NULL;
2158 count = g_slist_length(matchers->matchers);
2159 vstr = g_new(gchar *, count + 1);
2161 for (l = matchers->matchers, cur_str = vstr; l != NULL;
2162 l = g_slist_next(l), cur_str ++) {
2163 *cur_str = matcherprop_to_string((MatcherProp *) l->data);
2164 if (*cur_str == NULL)
2169 if (matchers->bool_and)
2170 result = g_strjoinv(" & ", vstr);
2172 result = g_strjoinv(" | ", vstr);
2174 for (cur_str = vstr ; *cur_str != NULL ; cur_str ++)
2182 #define STRLEN_ZERO(s) ((s) ? strlen(s) : 0)
2183 #define STRLEN_DEFAULT(s,d) ((s) ? strlen(s) : STRLEN_ZERO(d))
2185 static void add_str_default(gchar ** dest,
2186 const gchar * s, const gchar * d)
2188 gchar quoted_str[4096];
2196 quote_cmd_argument(quoted_str, sizeof(quoted_str), str);
2197 strcpy(* dest, quoted_str);
2199 (* dest) += strlen(* dest);
2202 /* matching_build_command() - preferably cmd should be unescaped */
2204 *\brief Build the command-line to execute
2206 *\param cmd String with command-line specifiers
2207 *\param info Message info to use for command
2209 *\return gchar * Newly allocated string
2211 gchar *matching_build_command(const gchar *cmd, MsgInfo *info)
2213 const gchar *s = cmd;
2214 gchar *filename = NULL;
2215 gchar *processed_cmd;
2219 const gchar *const no_subject = _("(none)") ;
2220 const gchar *const no_from = _("(none)") ;
2221 const gchar *const no_to = _("(none)") ;
2222 const gchar *const no_cc = _("(none)") ;
2223 const gchar *const no_date = _("(none)") ;
2224 const gchar *const no_msgid = _("(none)") ;
2225 const gchar *const no_newsgroups = _("(none)") ;
2226 const gchar *const no_references = _("(none)") ;
2228 size = STRLEN_ZERO(cmd) + 1;
2229 while (*s != '\0') {
2236 case 's': /* subject */
2237 size += STRLEN_DEFAULT(info->subject, no_subject) - 2;
2239 case 'f': /* from */
2240 size += STRLEN_DEFAULT(info->from, no_from) - 2;
2243 size += STRLEN_DEFAULT(info->to, no_to) - 2;
2246 size += STRLEN_DEFAULT(info->cc, no_cc) - 2;
2248 case 'd': /* date */
2249 size += STRLEN_DEFAULT(info->date, no_date) - 2;
2251 case 'i': /* message-id */
2252 size += STRLEN_DEFAULT(info->msgid, no_msgid) - 2;
2254 case 'n': /* newsgroups */
2255 size += STRLEN_DEFAULT(info->newsgroups, no_newsgroups) - 2;
2257 case 'r': /* references */
2258 /* FIXME: using the inreplyto header for reference */
2259 size += STRLEN_DEFAULT(info->inreplyto, no_references) - 2;
2261 case 'F': /* file */
2262 if (filename == NULL)
2263 filename = folder_item_fetch_msg(info->folder, info->msgnum);
2265 if (filename == NULL) {
2266 g_warning("filename is not set");
2270 size += strlen(filename) - 2;
2279 /* as the string can be quoted, we double the result */
2282 processed_cmd = g_new0(gchar, size);
2286 while (*s != '\0') {
2294 case 's': /* subject */
2295 add_str_default(&p, info->subject,
2298 case 'f': /* from */
2299 add_str_default(&p, info->from,
2303 add_str_default(&p, info->to,
2307 add_str_default(&p, info->cc,
2310 case 'd': /* date */
2311 add_str_default(&p, info->date,
2314 case 'i': /* message-id */
2315 add_str_default(&p, info->msgid,
2318 case 'n': /* newsgroups */
2319 add_str_default(&p, info->newsgroups,
2322 case 'r': /* references */
2323 /* FIXME: using the inreplyto header for references */
2324 add_str_default(&p, info->inreplyto, no_references);
2326 case 'F': /* file */
2327 if (filename != NULL)
2328 add_str_default(&p, filename, NULL);
2347 return processed_cmd;
2349 #undef STRLEN_DEFAULT
2352 /* ************************************************************ */
2356 *\brief Write filtering list to file
2359 *\param prefs_filtering List of filtering conditions
2361 static int prefs_filtering_write(FILE *fp, GSList *prefs_filtering)
2365 for (cur = prefs_filtering; cur != NULL; cur = cur->next) {
2366 gchar *filtering_str = NULL;
2367 gchar *tmp_name = NULL;
2368 FilteringProp *prop = NULL;
2370 if (NULL == (prop = (FilteringProp *) cur->data))
2373 if (NULL == (filtering_str = filteringprop_to_string(prop)))
2376 if (prop->enabled) {
2377 if (fputs("enabled ", fp) == EOF) {
2378 FILE_OP_ERROR("filtering config", "fputs");
2382 if (fputs("disabled ", fp) == EOF) {
2383 FILE_OP_ERROR("filtering config", "fputs");
2388 if (fputs("rulename \"", fp) == EOF) {
2389 FILE_OP_ERROR("filtering config", "fputs");
2390 g_free(filtering_str);
2393 tmp_name = prop->name;
2394 while (tmp_name && *tmp_name != '\0') {
2395 if (*tmp_name != '"') {
2396 if (fputc(*tmp_name, fp) == EOF) {
2397 FILE_OP_ERROR("filtering config", "fputs || fputc");
2398 g_free(filtering_str);
2401 } else if (*tmp_name == '"') {
2402 if (fputc('\\', fp) == EOF ||
2403 fputc('"', fp) == EOF) {
2404 FILE_OP_ERROR("filtering config", "fputs || fputc");
2405 g_free(filtering_str);
2411 if (fputs("\" ", fp) == EOF) {
2412 FILE_OP_ERROR("filtering config", "fputs");
2413 g_free(filtering_str);
2417 if (prop->account_id != 0) {
2420 tmp = g_strdup_printf("account %d ", prop->account_id);
2421 if (fputs(tmp, fp) == EOF) {
2422 FILE_OP_ERROR("filtering config", "fputs");
2429 if(fputs(filtering_str, fp) == EOF ||
2430 fputc('\n', fp) == EOF) {
2431 FILE_OP_ERROR("filtering config", "fputs || fputc");
2432 g_free(filtering_str);
2435 g_free(filtering_str);
2441 typedef struct _NodeLoopData {
2447 *\brief Write matchers from a folder item
2449 *\param node Node with folder info
2450 *\param data File pointer
2452 *\return gboolean FALSE
2454 static gboolean prefs_matcher_write_func(GNode *node, gpointer d)
2457 NodeLoopData *data = (NodeLoopData *)d;
2459 GSList *prefs_filtering;
2462 /* prevent warning */
2463 if (item->path == NULL)
2465 id = folder_item_get_identifier(item);
2468 prefs_filtering = item->prefs->processing;
2470 if (prefs_filtering != NULL) {
2471 if (fprintf(data->fp, "[%s]\n", id) < 0) {
2475 if (prefs_filtering_write(data->fp, prefs_filtering) < 0) {
2479 if (fputc('\n', data->fp) == EOF) {
2491 *\brief Save matchers from folder items
2495 static int prefs_matcher_save(FILE *fp)
2503 for (cur = folder_get_list() ; cur != NULL ; cur = g_list_next(cur)) {
2506 folder = (Folder *) cur->data;
2507 g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
2508 prefs_matcher_write_func, &data);
2511 if (data.error == TRUE)
2514 /* pre global rules */
2515 if (fprintf(fp, "[preglobal]\n") < 0 ||
2516 prefs_filtering_write(fp, pre_global_processing) < 0 ||
2517 fputc('\n', fp) == EOF)
2520 /* post global rules */
2521 if (fprintf(fp, "[postglobal]\n") < 0 ||
2522 prefs_filtering_write(fp, post_global_processing) < 0 ||
2523 fputc('\n', fp) == EOF)
2526 /* filtering rules */
2527 if (fprintf(fp, "[filtering]\n") < 0 ||
2528 prefs_filtering_write(fp, filtering_rules) < 0 ||
2529 fputc('\n', fp) == EOF)
2536 *\brief Write filtering / matcher configuration file
2538 void prefs_matcher_write_config(void)
2543 debug_print("Writing matcher configuration...\n");
2545 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
2548 if ((pfile = prefs_write_open(rcpath)) == NULL) {
2549 g_warning("failed to write configuration to file");
2556 if (prefs_matcher_save(pfile->fp) < 0) {
2557 g_warning("failed to write configuration to file");
2558 prefs_file_close_revert(pfile);
2559 } else if (prefs_file_close(pfile) < 0) {
2560 g_warning("failed to save configuration to file");
2564 /* ******************************************************************* */
2566 static void matcher_add_rulenames(const gchar *rcpath)
2568 gchar *newpath = g_strconcat(rcpath, ".new", NULL);
2569 FILE *src = g_fopen(rcpath, "rb");
2570 FILE *dst = g_fopen(newpath, "wb");
2571 gchar buf[BUFFSIZE];
2589 while (fgets (buf, sizeof(buf), src) != NULL) {
2590 if (strlen(buf) > 2 && buf[0] != '['
2591 && strncmp(buf, "rulename \"", 10)
2592 && strncmp(buf, "enabled rulename \"", 18)
2593 && strncmp(buf, "disabled rulename \"", 18)) {
2594 r = fwrite("enabled rulename \"\" ",
2595 strlen("enabled rulename \"\" "), 1, dst);
2597 g_message("cannot fwrite rulename\n");
2600 r = fwrite(buf, strlen(buf), 1, dst);
2602 g_message("cannot fwrite rule\n");
2607 move_file(newpath, rcpath, TRUE);
2612 *\brief Read matcher configuration
2614 void prefs_matcher_read_config(void)
2617 gchar *rc_old_format;
2620 create_matchparser_hashtab();
2621 prefs_filtering_clear();
2623 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MATCHER_RC, NULL);
2624 rc_old_format = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MATCHER_RC,
2625 ".pre_names", NULL);
2627 if (!is_file_exist(rc_old_format) && is_file_exist(rcpath)) {
2628 /* backup file with no rules names, in case
2629 * anything goes wrong */
2630 copy_file(rcpath, rc_old_format, FALSE);
2631 /* now hack the file in order to have it to the new format */
2632 matcher_add_rulenames(rcpath);
2635 g_free(rc_old_format);
2637 f = g_fopen(rcpath, "rb");
2641 matcher_parser_start_parsing(f);
2642 fclose(matcher_parserin);
2645 /* previous version compatibility */
2647 /* g_print("reading filtering\n"); */
2648 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
2649 FILTERING_RC, NULL);
2650 f = g_fopen(rcpath, "rb");
2654 matcher_parser_start_parsing(f);
2655 fclose(matcher_parserin);
2658 /* g_print("reading scoring\n"); */
2659 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
2661 f = g_fopen(rcpath, "rb");
2665 matcher_parser_start_parsing(f);
2666 fclose(matcher_parserin);