2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 2002-2012 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_INREPLYTO
, "inreplyto"},
118 {MATCHCRITERIA_NOT_INREPLYTO
, "~inreplyto"},
119 {MATCHCRITERIA_REFERENCES
, "references"},
120 {MATCHCRITERIA_NOT_REFERENCES
, "~references"},
121 {MATCHCRITERIA_SCORE_GREATER
, "score_greater"},
122 {MATCHCRITERIA_SCORE_LOWER
, "score_lower"},
123 {MATCHCRITERIA_SCORE_EQUAL
, "score_equal"},
124 {MATCHCRITERIA_PARTIAL
, "partial"},
125 {MATCHCRITERIA_NOT_PARTIAL
, "~partial"},
126 {MATCHCRITERIA_FOUND_IN_ADDRESSBOOK
, "found_in_addressbook"},
127 {MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK
, "~found_in_addressbook"},
129 {MATCHCRITERIA_SIZE_GREATER
, "size_greater"},
130 {MATCHCRITERIA_SIZE_SMALLER
, "size_smaller"},
131 {MATCHCRITERIA_SIZE_EQUAL
, "size_equal"},
133 /* content have to be read */
134 {MATCHCRITERIA_HEADER
, "header"},
135 {MATCHCRITERIA_NOT_HEADER
, "~header"},
136 {MATCHCRITERIA_HEADERS_PART
, "headers_part"},
137 {MATCHCRITERIA_NOT_HEADERS_PART
, "~headers_part"},
138 {MATCHCRITERIA_MESSAGE
, "message"},
139 {MATCHCRITERIA_NOT_MESSAGE
, "~message"},
140 {MATCHCRITERIA_BODY_PART
, "body_part"},
141 {MATCHCRITERIA_NOT_BODY_PART
, "~body_part"},
142 {MATCHCRITERIA_TEST
, "test"},
143 {MATCHCRITERIA_NOT_TEST
, "~test"},
146 {MATCHTYPE_MATCHCASE
, "matchcase"},
147 {MATCHTYPE_MATCH
, "match"},
148 {MATCHTYPE_REGEXPCASE
, "regexpcase"},
149 {MATCHTYPE_REGEXP
, "regexp"},
152 {MATCHACTION_SCORE
, "score"}, /* for backward compatibility */
153 {MATCHACTION_MOVE
, "move"},
154 {MATCHACTION_COPY
, "copy"},
155 {MATCHACTION_DELETE
, "delete"},
156 {MATCHACTION_MARK
, "mark"},
157 {MATCHACTION_UNMARK
, "unmark"},
158 {MATCHACTION_LOCK
, "lock"},
159 {MATCHACTION_UNLOCK
, "unlock"},
160 {MATCHACTION_MARK_AS_READ
, "mark_as_read"},
161 {MATCHACTION_MARK_AS_UNREAD
, "mark_as_unread"},
162 {MATCHACTION_MARK_AS_SPAM
, "mark_as_spam"},
163 {MATCHACTION_MARK_AS_HAM
, "mark_as_ham"},
164 {MATCHACTION_FORWARD
, "forward"},
165 {MATCHACTION_FORWARD_AS_ATTACHMENT
, "forward_as_attachment"},
166 {MATCHACTION_EXECUTE
, "execute"},
167 {MATCHACTION_COLOR
, "color"},
168 {MATCHACTION_REDIRECT
, "redirect"},
169 {MATCHACTION_CHANGE_SCORE
, "change_score"},
170 {MATCHACTION_SET_SCORE
, "set_score"},
171 {MATCHACTION_STOP
, "stop"},
172 {MATCHACTION_HIDE
, "hide"},
173 {MATCHACTION_IGNORE
, "ignore"},
174 {MATCHACTION_WATCH
, "watch"},
175 {MATCHACTION_ADD_TO_ADDRESSBOOK
, "add_to_addressbook"},
176 {MATCHACTION_SET_TAG
, "set_tag"},
177 {MATCHACTION_UNSET_TAG
, "unset_tag"},
178 {MATCHACTION_CLEAR_TAGS
, "clear_tags"},
202 static gchar
*context_str
[N_CONTEXT_STRS
];
204 void matcher_init(void)
206 if (context_str
[CONTEXT_SUBJECT
] != NULL
)
209 context_str
[CONTEXT_SUBJECT
] = g_strdup_printf(_("%s header"), prefs_common_translated_header_name("Subject:"));
210 context_str
[CONTEXT_FROM
] = g_strdup_printf(_("%s header"), prefs_common_translated_header_name("From:"));
211 context_str
[CONTEXT_TO
] = g_strdup_printf(_("%s header"), prefs_common_translated_header_name("To:"));
212 context_str
[CONTEXT_CC
] = g_strdup_printf(_("%s header"), prefs_common_translated_header_name("Cc:"));
213 context_str
[CONTEXT_NEWSGROUPS
] = g_strdup_printf(_("%s header"), prefs_common_translated_header_name("Newsgroups:"));
214 context_str
[CONTEXT_IN_REPLY_TO
] = g_strdup_printf(_("%s header"), prefs_common_translated_header_name("In-Reply-To:"));
215 context_str
[CONTEXT_REFERENCES
] = g_strdup_printf(_("%s header"), prefs_common_translated_header_name("References:"));
216 context_str
[CONTEXT_HEADER
] = g_strdup(_("header"));
217 context_str
[CONTEXT_HEADER_LINE
] = g_strdup(_("header line"));
218 context_str
[CONTEXT_BODY_LINE
] = g_strdup(_("body line"));
219 context_str
[CONTEXT_TAG
] = g_strdup(_("tag"));
222 void matcher_done(void)
225 for (i
= 0; i
< N_CONTEXT_STRS
; i
++) {
226 g_free(context_str
[i
]);
227 context_str
[i
] = NULL
;
231 extern gboolean debug_filtering_session
;
234 *\brief Look up table with keywords defined in \sa matchparser_tab
236 static GHashTable
*matchparser_hashtab
;
239 *\brief Translate keyword id to keyword string
241 *\param id Id of keyword
243 *\return const gchar * Keyword
245 const gchar
*get_matchparser_tab_str(gint id
)
249 for (i
= 0; i
< sizeof matchparser_tab
/ sizeof matchparser_tab
[0]; i
++) {
250 if (matchparser_tab
[i
].id
== id
)
251 return matchparser_tab
[i
].str
;
257 *\brief Create keyword lookup table
259 static void create_matchparser_hashtab(void)
263 if (matchparser_hashtab
) return;
264 matchparser_hashtab
= g_hash_table_new(g_str_hash
, g_str_equal
);
265 for (i
= 0; i
< sizeof matchparser_tab
/ sizeof matchparser_tab
[0]; i
++)
266 g_hash_table_insert(matchparser_hashtab
,
267 matchparser_tab
[i
].str
,
268 (gpointer
) &matchparser_tab
[i
]);
272 *\brief Return a keyword id from a keyword string
274 *\param str Keyword string
276 *\return gint Keyword id
278 gint
get_matchparser_tab_id(const gchar
*str
)
282 if (NULL
!= (res
= g_hash_table_lookup(matchparser_hashtab
, str
))) {
288 /* **************** data structure allocation **************** */
291 *\brief Allocate a structure for a filtering / scoring
292 * "condition" (a matcher structure)
294 *\param criteria Criteria ID (MATCHCRITERIA_XXXX)
295 *\param header Header string (if criteria is MATCHCRITERIA_HEADER
296 or MATCHCRITERIA_FOUND_IN_ADDRESSBOOK)
297 *\param matchtype Type of action (MATCHTYPE_XXX)
298 *\param expr String value or expression to check
299 *\param value Integer value to check
301 *\return MatcherProp * Pointer to newly allocated structure
303 MatcherProp
*matcherprop_new(gint criteria
, const gchar
*header
,
304 gint matchtype
, const gchar
*expr
,
309 prop
= g_new0(MatcherProp
, 1);
310 prop
->criteria
= criteria
;
311 prop
->header
= header
!= NULL
? g_strdup(header
) : NULL
;
313 prop
->expr
= expr
!= NULL
? g_strdup(expr
) : NULL
;
315 prop
->matchtype
= matchtype
;
324 *\brief Free a matcher structure
326 *\param prop Pointer to matcher structure allocated with
329 void matcherprop_free(MatcherProp
*prop
)
332 g_free(prop
->header
);
333 if (prop
->preg
!= NULL
) {
341 *\brief Copy a matcher structure
343 *\param src Matcher structure to copy
345 *\return MatcherProp * Pointer to newly allocated matcher structure
347 MatcherProp
*matcherprop_copy(const MatcherProp
*src
)
349 MatcherProp
*prop
= g_new0(MatcherProp
, 1);
351 prop
->criteria
= src
->criteria
;
352 prop
->header
= src
->header
? g_strdup(src
->header
) : NULL
;
353 prop
->expr
= src
->expr
? g_strdup(src
->expr
) : NULL
;
354 prop
->matchtype
= src
->matchtype
;
356 prop
->preg
= NULL
; /* will be re-evaluated */
357 prop
->value
= src
->value
;
358 prop
->error
= src
->error
;
362 /* ************** match ******************************/
364 static gboolean match_with_addresses_in_addressbook
365 (MatcherProp
*prop
, GSList
*address_list
, gint type
,
366 gchar
* folderpath
, gint match
)
369 gboolean found
= FALSE
;
372 cm_return_val_if_fail(address_list
!= NULL
, FALSE
);
374 debug_print("match_with_addresses_in_addressbook(%d, %s)\n",
375 g_slist_length(address_list
), folderpath
?folderpath
:"(null)");
377 if (folderpath
== NULL
||
378 strcasecmp(folderpath
, "Any") == 0 ||
384 start_address_completion(path
);
386 for (walk
= address_list
; walk
!= NULL
; walk
= walk
->next
) {
387 /* exact matching of email address */
388 guint num_addr
= complete_address(walk
->data
);
391 /* skip first item (this is the search string itself) */
393 for (; i
< num_addr
&& !found
; i
++) {
394 gchar
*addr
= get_complete_address(i
);
395 extract_address(addr
);
396 if (strcasecmp(addr
, walk
->data
) == 0) {
400 if (debug_filtering_session
401 && prefs_common
.filtering_debug_level
>= FILTERING_DEBUG_LEVEL_HIGH
) {
402 log_print(LOG_DEBUG_FILTERING
,
403 "address [ %s ] matches\n",
404 (gchar
*)walk
->data
);
411 if (debug_filtering_session
412 && prefs_common
.filtering_debug_level
>= FILTERING_DEBUG_LEVEL_HIGH
414 log_print(LOG_DEBUG_FILTERING
,
415 "address [ %s ] does NOT match\n",
416 (gchar
*)walk
->data
);
420 if (match
== MATCH_ALL
) {
421 /* if matching all addresses, stop if one doesn't match */
424 if (debug_filtering_session
425 && prefs_common
.filtering_debug_level
>= FILTERING_DEBUG_LEVEL_HIGH
) {
426 log_print(LOG_DEBUG_FILTERING
,
427 "not all address match (matching all)\n");
431 } else if (match
== MATCH_ANY
) {
432 /* if matching any address, stop if one does match */
435 if (debug_filtering_session
436 && prefs_common
.filtering_debug_level
>= FILTERING_DEBUG_LEVEL_HIGH
) {
437 log_print(LOG_DEBUG_FILTERING
,
438 "at least one address matches (matching any)\n");
443 /* MATCH_ONE: there should be only one loop iteration */
446 end_address_completion();
452 *\brief Find out if a string matches a condition
454 *\param prop Matcher structure
455 *\param str String to check
457 *\return gboolean TRUE if str matches the condition in the
460 static gboolean
matcherprop_string_match(MatcherProp
*prop
, const gchar
*str
,
461 const gchar
*debug_context
)
465 gboolean ret
= FALSE
;
466 gboolean should_free
= FALSE
;
470 if (prop
->matchtype
== MATCHTYPE_REGEXPCASE
||
471 prop
->matchtype
== MATCHTYPE_MATCHCASE
) {
472 str1
= g_utf8_casefold(str
, -1);
473 down_expr
= g_utf8_casefold(prop
->expr
, -1);
477 down_expr
= (gchar
*)prop
->expr
;
481 switch (prop
->matchtype
) {
482 case MATCHTYPE_REGEXPCASE
:
483 case MATCHTYPE_REGEXP
:
485 if (!prop
->preg
&& (prop
->error
== 0)) {
486 prop
->preg
= g_new0(regex_t
, 1);
487 /* if regexp then don't use the escaped string */
488 if (regcomp(prop
->preg
, down_expr
,
489 REG_NOSUB
| REG_EXTENDED
490 | ((prop
->matchtype
== MATCHTYPE_REGEXPCASE
)
491 ? REG_ICASE
: 0)) != 0) {
497 if (prop
->preg
== NULL
) {
502 if (regexec(prop
->preg
, str1
, 0, NULL
, 0) == 0)
508 if (debug_filtering_session
509 && prefs_common
.filtering_debug_level
>= FILTERING_DEBUG_LEVEL_HIGH
) {
510 gchar
*stripped
= g_strdup(str
);
512 strretchomp(stripped
);
514 log_print(LOG_DEBUG_FILTERING
,
515 "%s value [ %s ] matches regular expression [ %s ] (%s)\n",
516 debug_context
, stripped
, prop
->expr
,
517 prop
->matchtype
== MATCHTYPE_REGEXP
? _("Case sensitive"):_("Case insensitive"));
519 log_print(LOG_DEBUG_FILTERING
,
520 "%s value [ %s ] does NOT match regular expression [ %s ] (%s)\n",
521 debug_context
, stripped
, prop
->expr
,
522 prop
->matchtype
== MATCHTYPE_REGEXP
? _("Case sensitive"):_("Case insensitive"));
528 case MATCHTYPE_MATCHCASE
:
529 case MATCHTYPE_MATCH
:
530 ret
= (strstr(str1
, down_expr
) != NULL
);
533 if (debug_filtering_session
534 && prefs_common
.filtering_debug_level
>= FILTERING_DEBUG_LEVEL_HIGH
) {
535 gchar
*stripped
= g_strdup(str
);
537 strretchomp(stripped
);
539 log_print(LOG_DEBUG_FILTERING
,
540 "%s value [ %s ] contains [ %s ] (%s)\n",
541 debug_context
, stripped
, prop
->expr
,
542 prop
->matchtype
== MATCHTYPE_MATCH
? _("Case sensitive"):_("Case insensitive"));
544 log_print(LOG_DEBUG_FILTERING
,
545 "%s value [ %s ] does NOT contain [ %s ] (%s)\n",
546 debug_context
, stripped
, prop
->expr
,
547 prop
->matchtype
== MATCHTYPE_MATCH
? _("Case sensitive"):_("Case insensitive"));
566 *\brief Find out if a tag matches a condition
568 *\param prop Matcher structure
569 *\param msginfo message to check
571 *\return gboolean TRUE if msginfo matches the condition in the
574 static gboolean
matcherprop_tag_match(MatcherProp
*prop
, MsgInfo
*msginfo
,
575 const gchar
*debug_context
)
577 gboolean ret
= FALSE
;
580 if (msginfo
== NULL
|| msginfo
->tags
== NULL
)
583 for (cur
= msginfo
->tags
; cur
; cur
= cur
->next
) {
584 const gchar
*str
= tags_get_tag(GPOINTER_TO_INT(cur
->data
));
587 if (matcherprop_string_match(prop
, str
, debug_context
)) {
596 *\brief Find out if the string-ed list matches a condition
598 *\param prop Matcher structure
599 *\param list GSList of strings to check
601 *\return gboolean TRUE if str matches the condition in the
604 static gboolean
matcherprop_list_match(MatcherProp
*prop
, const GSList
*list
,
605 const gchar
*debug_context
)
609 for(cur
= list
; cur
!= NULL
; cur
= cur
->next
) {
610 if (matcherprop_string_match(prop
, (gchar
*)cur
->data
, debug_context
))
616 static gboolean
matcherprop_header_line_match(MatcherProp
*prop
, const gchar
*hdr
,
617 const gchar
*str
, const gchar
*debug_context
)
620 gboolean res
= FALSE
;
622 if (hdr
== NULL
|| str
== NULL
)
625 line
= g_strdup_printf("%s %s", hdr
, str
);
626 res
= matcherprop_string_match(prop
, line
, debug_context
);
633 typedef struct _thread_data
{
640 static void *matcher_test_thread(void *data
)
642 thread_data
*td
= (thread_data
*)data
;
645 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE
, NULL
);
646 pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS
, NULL
);
648 result
= system(td
->cmd
);
649 td
->done
= TRUE
; /* let the caller thread join() */
650 return GINT_TO_POINTER(result
);
655 *\brief Execute a command defined in the matcher structure
657 *\param prop Pointer to matcher structure
658 *\param info Pointer to message info structure
660 *\return gboolean TRUE if command was executed succesfully
662 static gboolean
matcherprop_match_test(const MatcherProp
*prop
,
671 thread_data
*td
= g_new0(thread_data
, 1);
673 time_t start_time
= time(NULL
);
676 file
= procmsg_get_message_file(info
);
685 cmd
= matching_build_command(prop
->expr
, info
);
695 if (debug_filtering_session
696 && prefs_common
.filtering_debug_level
>= FILTERING_DEBUG_LEVEL_HIGH
) {
697 log_print(LOG_DEBUG_FILTERING
,
698 "starting threaded command [ %s ]\n",
704 if (pthread_attr_init(&pta
) != 0 ||
705 pthread_attr_setdetachstate(&pta
, PTHREAD_CREATE_JOINABLE
) != 0 ||
706 pthread_create(&pt
, &pta
, matcher_test_thread
, td
) != 0)
707 retval
= system(cmd
);
709 debug_print("waiting for test thread\n");
711 /* don't let the interface freeze while waiting */
713 if (time(NULL
) - start_time
> 30) {
719 pthread_join(pt
, &res
);
720 retval
= GPOINTER_TO_INT(res
);
721 debug_print(" test thread returned %d\n", retval
);
726 if (debug_filtering_session
727 && prefs_common
.filtering_debug_level
>= FILTERING_DEBUG_LEVEL_HIGH
) {
728 log_print(LOG_DEBUG_FILTERING
,
729 "starting synchronous command [ %s ]\n",
733 retval
= system(cmd
);
735 debug_print("Command exit code: %d\n", retval
);
738 if (debug_filtering_session
739 && prefs_common
.filtering_debug_level
>= FILTERING_DEBUG_LEVEL_HIGH
) {
740 log_print(LOG_DEBUG_FILTERING
,
741 "command returned [ %d ]\n",
746 return (retval
== 0);
750 *\brief Check if a message matches the condition in a matcher
753 *\param prop Pointer to matcher structure
754 *\param info Pointer to message info
756 *\return gboolean TRUE if a match
758 static gboolean
matcherprop_match(MatcherProp
*prop
,
762 gint age_mult_hours
= 1;
764 switch(prop
->criteria
) {
765 case MATCHCRITERIA_ALL
:
767 case MATCHCRITERIA_UNREAD
:
768 return MSG_IS_UNREAD(info
->flags
);
769 case MATCHCRITERIA_NOT_UNREAD
:
770 return !MSG_IS_UNREAD(info
->flags
);
771 case MATCHCRITERIA_NEW
:
772 return MSG_IS_NEW(info
->flags
);
773 case MATCHCRITERIA_NOT_NEW
:
774 return !MSG_IS_NEW(info
->flags
);
775 case MATCHCRITERIA_MARKED
:
776 return MSG_IS_MARKED(info
->flags
);
777 case MATCHCRITERIA_NOT_MARKED
:
778 return !MSG_IS_MARKED(info
->flags
);
779 case MATCHCRITERIA_DELETED
:
780 return MSG_IS_DELETED(info
->flags
);
781 case MATCHCRITERIA_NOT_DELETED
:
782 return !MSG_IS_DELETED(info
->flags
);
783 case MATCHCRITERIA_REPLIED
:
784 return MSG_IS_REPLIED(info
->flags
);
785 case MATCHCRITERIA_NOT_REPLIED
:
786 return !MSG_IS_REPLIED(info
->flags
);
787 case MATCHCRITERIA_FORWARDED
:
788 return MSG_IS_FORWARDED(info
->flags
);
789 case MATCHCRITERIA_NOT_FORWARDED
:
790 return !MSG_IS_FORWARDED(info
->flags
);
791 case MATCHCRITERIA_LOCKED
:
792 return MSG_IS_LOCKED(info
->flags
);
793 case MATCHCRITERIA_NOT_LOCKED
:
794 return !MSG_IS_LOCKED(info
->flags
);
795 case MATCHCRITERIA_SPAM
:
796 return MSG_IS_SPAM(info
->flags
);
797 case MATCHCRITERIA_NOT_SPAM
:
798 return !MSG_IS_SPAM(info
->flags
);
799 case MATCHCRITERIA_HAS_ATTACHMENT
:
800 return MSG_IS_WITH_ATTACHMENT(info
->flags
);
801 case MATCHCRITERIA_HAS_NO_ATTACHMENT
:
802 return !MSG_IS_WITH_ATTACHMENT(info
->flags
);
803 case MATCHCRITERIA_SIGNED
:
804 return MSG_IS_SIGNED(info
->flags
);
805 case MATCHCRITERIA_NOT_SIGNED
:
806 return !MSG_IS_SIGNED(info
->flags
);
807 case MATCHCRITERIA_COLORLABEL
:
809 gint color
= MSG_GET_COLORLABEL_VALUE(info
->flags
);
810 gboolean ret
= (color
== prop
->value
);
813 if (debug_filtering_session
814 && prefs_common
.filtering_debug_level
>= FILTERING_DEBUG_LEVEL_HIGH
) {
816 log_print(LOG_DEBUG_FILTERING
,
817 "message color value [ %d ] matches color value [ %d ]\n",
820 log_print(LOG_DEBUG_FILTERING
,
821 "message color value [ %d ] does NOT match color value [ %d ]\n",
827 case MATCHCRITERIA_NOT_COLORLABEL
:
829 gint color
= MSG_GET_COLORLABEL_VALUE(info
->flags
);
830 gboolean ret
= (color
!= prop
->value
);
833 if (debug_filtering_session
834 && prefs_common
.filtering_debug_level
>= FILTERING_DEBUG_LEVEL_HIGH
) {
836 log_print(LOG_DEBUG_FILTERING
,
837 "message color value [ %d ] matches color value [ %d ]\n",
840 log_print(LOG_DEBUG_FILTERING
,
841 "message color value [ %d ] does NOT match color value [ %d ]\n",
847 case MATCHCRITERIA_IGNORE_THREAD
:
848 return MSG_IS_IGNORE_THREAD(info
->flags
);
849 case MATCHCRITERIA_NOT_IGNORE_THREAD
:
850 return !MSG_IS_IGNORE_THREAD(info
->flags
);
851 case MATCHCRITERIA_WATCH_THREAD
:
852 return MSG_IS_WATCH_THREAD(info
->flags
);
853 case MATCHCRITERIA_NOT_WATCH_THREAD
:
854 return !MSG_IS_WATCH_THREAD(info
->flags
);
855 case MATCHCRITERIA_SUBJECT
:
856 return matcherprop_string_match(prop
, info
->subject
, context_str
[CONTEXT_SUBJECT
]);
857 case MATCHCRITERIA_NOT_SUBJECT
:
858 return !matcherprop_string_match(prop
, info
->subject
, context_str
[CONTEXT_SUBJECT
]);
859 case MATCHCRITERIA_FROM
:
860 return matcherprop_string_match(prop
, info
->from
, context_str
[CONTEXT_FROM
]);
861 case MATCHCRITERIA_NOT_FROM
:
862 return !matcherprop_string_match(prop
, info
->from
, context_str
[CONTEXT_FROM
]);
863 case MATCHCRITERIA_TO
:
864 return matcherprop_string_match(prop
, info
->to
, context_str
[CONTEXT_TO
]);
865 case MATCHCRITERIA_NOT_TO
:
866 return !matcherprop_string_match(prop
, info
->to
, context_str
[CONTEXT_TO
]);
867 case MATCHCRITERIA_CC
:
868 return matcherprop_string_match(prop
, info
->cc
, context_str
[CONTEXT_CC
]);
869 case MATCHCRITERIA_NOT_CC
:
870 return !matcherprop_string_match(prop
, info
->cc
, context_str
[CONTEXT_CC
]);
871 case MATCHCRITERIA_TO_OR_CC
:
872 return matcherprop_string_match(prop
, info
->to
, context_str
[CONTEXT_TO
])
873 || matcherprop_string_match(prop
, info
->cc
, context_str
[CONTEXT_CC
]);
874 case MATCHCRITERIA_NOT_TO_AND_NOT_CC
:
875 return !matcherprop_string_match(prop
, info
->to
, context_str
[CONTEXT_TO
])
876 && !matcherprop_string_match(prop
, info
->cc
, context_str
[CONTEXT_CC
]);
877 case MATCHCRITERIA_TAG
:
878 return matcherprop_tag_match(prop
, info
, context_str
[CONTEXT_TAG
]);
879 case MATCHCRITERIA_NOT_TAG
:
880 return !matcherprop_tag_match(prop
, info
, context_str
[CONTEXT_TAG
]);
881 case MATCHCRITERIA_TAGGED
:
882 return info
->tags
!= NULL
;
883 case MATCHCRITERIA_NOT_TAGGED
:
884 return info
->tags
== NULL
;
885 case MATCHCRITERIA_AGE_GREATER
:
887 /* Fallthrough intended */
888 case MATCHCRITERIA_AGE_GREATER_HOURS
:
894 age
= ((t
- info
->date_t
) / (60 * 60 * age_mult_hours
));
895 ret
= (age
>= prop
->value
);
898 if (debug_filtering_session
899 && prefs_common
.filtering_debug_level
>= FILTERING_DEBUG_LEVEL_HIGH
) {
901 log_print(LOG_DEBUG_FILTERING
,
902 "message age [ %d ] is greater than [ %d ]\n",
905 log_print(LOG_DEBUG_FILTERING
,
906 "message age [ %d ] is not greater than [ %d ]\n",
912 case MATCHCRITERIA_AGE_LOWER
:
914 /* Fallthrough intended */
915 case MATCHCRITERIA_AGE_LOWER_HOURS
:
921 age
= ((t
- info
->date_t
) / (60 * 60 * age_mult_hours
));
922 ret
= (age
< prop
->value
);
925 if (debug_filtering_session
926 && prefs_common
.filtering_debug_level
>= FILTERING_DEBUG_LEVEL_HIGH
) {
928 log_print(LOG_DEBUG_FILTERING
,
929 "message age [ %d ] is lower than [ %d ]\n",
932 log_print(LOG_DEBUG_FILTERING
,
933 "message age [ %d ] is not lower than [ %d ]\n",
939 case MATCHCRITERIA_SCORE_GREATER
:
941 gboolean ret
= (info
->score
> prop
->value
);
944 if (debug_filtering_session
945 && prefs_common
.filtering_debug_level
>= FILTERING_DEBUG_LEVEL_HIGH
) {
947 log_print(LOG_DEBUG_FILTERING
,
948 "message score [ %d ] is greater than [ %d ]\n",
949 info
->score
, prop
->value
);
951 log_print(LOG_DEBUG_FILTERING
,
952 "message score [ %d ] is not greater than [ %d ]\n",
953 info
->score
, prop
->value
);
958 case MATCHCRITERIA_SCORE_LOWER
:
960 gboolean ret
= (info
->score
< prop
->value
);
963 if (debug_filtering_session
964 && prefs_common
.filtering_debug_level
>= FILTERING_DEBUG_LEVEL_HIGH
) {
966 log_print(LOG_DEBUG_FILTERING
,
967 "message score [ %d ] is lower than [ %d ]\n",
968 info
->score
, prop
->value
);
970 log_print(LOG_DEBUG_FILTERING
,
971 "message score [ %d ] is not lower than [ %d ]\n",
972 info
->score
, prop
->value
);
977 case MATCHCRITERIA_SCORE_EQUAL
:
979 gboolean ret
= (info
->score
== prop
->value
);
982 if (debug_filtering_session
983 && prefs_common
.filtering_debug_level
>= FILTERING_DEBUG_LEVEL_HIGH
) {
985 log_print(LOG_DEBUG_FILTERING
,
986 "message score [ %d ] is equal to [ %d ]\n",
987 info
->score
, prop
->value
);
989 log_print(LOG_DEBUG_FILTERING
,
990 "message score [ %d ] is not equal to [ %d ]\n",
991 info
->score
, prop
->value
);
996 case MATCHCRITERIA_SIZE_GREATER
:
998 /* FIXME: info->size is a goffset */
999 gboolean ret
= (info
->size
> (goffset
) prop
->value
);
1002 if (debug_filtering_session
1003 && prefs_common
.filtering_debug_level
>= FILTERING_DEBUG_LEVEL_HIGH
) {
1005 log_print(LOG_DEBUG_FILTERING
,
1006 "message size is greater than [ %d ]\n",
1009 log_print(LOG_DEBUG_FILTERING
,
1010 "message size is not greater than [ %d ]\n",
1016 case MATCHCRITERIA_SIZE_SMALLER
:
1018 /* FIXME: info->size is a goffset */
1019 gboolean ret
= (info
->size
< (goffset
) prop
->value
);
1022 if (debug_filtering_session
1023 && prefs_common
.filtering_debug_level
>= FILTERING_DEBUG_LEVEL_HIGH
) {
1025 log_print(LOG_DEBUG_FILTERING
,
1026 "message size is smaller than [ %d ]\n",
1029 log_print(LOG_DEBUG_FILTERING
,
1030 "message size is not smaller than [ %d ]\n",
1036 case MATCHCRITERIA_SIZE_EQUAL
:
1038 /* FIXME: info->size is a goffset */
1039 gboolean ret
= (info
->size
== (goffset
) prop
->value
);
1042 if (debug_filtering_session
1043 && prefs_common
.filtering_debug_level
>= FILTERING_DEBUG_LEVEL_HIGH
) {
1045 log_print(LOG_DEBUG_FILTERING
,
1046 "message size is equal to [ %d ]\n",
1049 log_print(LOG_DEBUG_FILTERING
,
1050 "message size is not equal to [ %d ]\n",
1056 case MATCHCRITERIA_PARTIAL
:
1058 /* FIXME: info->size is a goffset */
1059 gboolean ret
= (info
->total_size
!= 0 && info
->size
!= (goffset
)info
->total_size
);
1062 if (debug_filtering_session
1063 && prefs_common
.filtering_debug_level
>= FILTERING_DEBUG_LEVEL_HIGH
) {
1065 log_print(LOG_DEBUG_FILTERING
,
1066 "message is partially downloaded, size is less than total size [ %d ])\n",
1069 log_print(LOG_DEBUG_FILTERING
,
1070 "message is not partially downloaded\n");
1075 case MATCHCRITERIA_NOT_PARTIAL
:
1077 /* FIXME: info->size is a goffset */
1078 gboolean ret
= (info
->total_size
== 0 || info
->size
== (goffset
)info
->total_size
);
1081 if (debug_filtering_session
1082 && prefs_common
.filtering_debug_level
>= FILTERING_DEBUG_LEVEL_HIGH
) {
1084 log_print(LOG_DEBUG_FILTERING
,
1085 "message is not partially downloaded\n");
1087 log_print(LOG_DEBUG_FILTERING
,
1088 "message is partially downloaded, size is less than total size [ %d ])\n",
1094 case MATCHCRITERIA_NEWSGROUPS
:
1095 return matcherprop_string_match(prop
, info
->newsgroups
, context_str
[CONTEXT_NEWSGROUPS
]);
1096 case MATCHCRITERIA_NOT_NEWSGROUPS
:
1097 return !matcherprop_string_match(prop
, info
->newsgroups
, context_str
[CONTEXT_NEWSGROUPS
]);
1098 case MATCHCRITERIA_INREPLYTO
:
1099 return matcherprop_string_match(prop
, info
->inreplyto
, context_str
[CONTEXT_IN_REPLY_TO
]);
1100 case MATCHCRITERIA_NOT_INREPLYTO
:
1101 return !matcherprop_string_match(prop
, info
->inreplyto
, context_str
[CONTEXT_IN_REPLY_TO
]);
1102 case MATCHCRITERIA_REFERENCES
:
1103 return matcherprop_list_match(prop
, info
->references
, context_str
[CONTEXT_REFERENCES
]);
1104 case MATCHCRITERIA_NOT_REFERENCES
:
1105 return !matcherprop_list_match(prop
, info
->references
, context_str
[CONTEXT_REFERENCES
]);
1106 case MATCHCRITERIA_TEST
:
1107 return matcherprop_match_test(prop
, info
);
1108 case MATCHCRITERIA_NOT_TEST
:
1109 return !matcherprop_match_test(prop
, info
);
1115 /* ********************* MatcherList *************************** */
1118 *\brief Create a new list of matchers
1120 *\param matchers List of matcher structures
1121 *\param bool_and Operator
1123 *\return MatcherList * New list
1125 MatcherList
*matcherlist_new(GSList
*matchers
, gboolean bool_and
)
1129 cond
= g_new0(MatcherList
, 1);
1131 cond
->matchers
= matchers
;
1132 cond
->bool_and
= bool_and
;
1138 *\brief Frees a list of matchers
1140 *\param cond List of matchers
1142 void matcherlist_free(MatcherList
*cond
)
1146 cm_return_if_fail(cond
);
1147 for (l
= cond
->matchers
; l
!= NULL
; l
= g_slist_next(l
)) {
1148 matcherprop_free((MatcherProp
*) l
->data
);
1150 g_slist_free(cond
->matchers
);
1155 *\brief Skip all headers in a message file
1157 *\param fp Message file
1159 static void matcherlist_skip_headers(FILE *fp
)
1161 gchar buf
[BUFFSIZE
];
1163 while (procheader_get_one_field(buf
, sizeof(buf
), fp
, NULL
) != -1)
1168 *\brief Check if a header matches a matcher condition
1170 *\param matcher Matcher structure to check header for
1171 *\param buf Header name
1173 *\return boolean TRUE if matching header
1175 static gboolean
matcherprop_match_one_header(MatcherProp
*matcher
,
1178 gboolean result
= FALSE
;
1179 Header
*header
= NULL
;
1181 switch (matcher
->criteria
) {
1182 case MATCHCRITERIA_HEADER
:
1183 case MATCHCRITERIA_NOT_HEADER
:
1184 header
= procheader_parse_header(buf
);
1187 if (procheader_headername_equal(header
->name
,
1189 if (matcher
->criteria
== MATCHCRITERIA_HEADER
)
1190 result
= matcherprop_string_match(matcher
, header
->body
, context_str
[CONTEXT_HEADER
]);
1192 result
= !matcherprop_string_match(matcher
, header
->body
, context_str
[CONTEXT_HEADER
]);
1193 procheader_header_free(header
);
1197 procheader_header_free(header
);
1200 case MATCHCRITERIA_HEADERS_PART
:
1201 case MATCHCRITERIA_MESSAGE
:
1202 header
= procheader_parse_header(buf
);
1205 result
= matcherprop_header_line_match(matcher
,
1206 header
->name
, header
->body
, context_str
[CONTEXT_HEADER_LINE
]);
1207 procheader_header_free(header
);
1209 case MATCHCRITERIA_NOT_HEADERS_PART
:
1210 case MATCHCRITERIA_NOT_MESSAGE
:
1211 header
= procheader_parse_header(buf
);
1214 result
= !matcherprop_header_line_match(matcher
,
1215 header
->name
, header
->body
, context_str
[CONTEXT_HEADER_LINE
]);
1216 procheader_header_free(header
);
1218 case MATCHCRITERIA_FOUND_IN_ADDRESSBOOK
:
1219 case MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK
:
1221 GSList
*address_list
= NULL
;
1222 gint match
= MATCH_ONE
;
1223 gboolean found
= FALSE
;
1225 /* how many address headers are me trying to mach? */
1226 if (strcasecmp(matcher
->header
, "Any") == 0)
1228 else if (strcasecmp(matcher
->header
, "All") == 0)
1231 if (match
== MATCH_ONE
) {
1232 /* matching one address header exactly, is that the right one? */
1233 header
= procheader_parse_header(buf
);
1235 !procheader_headername_equal(header
->name
, matcher
->header
))
1237 address_list
= address_list_append(address_list
, header
->body
);
1238 if (address_list
== NULL
)
1242 header
= procheader_parse_header(buf
);
1245 /* address header is one of the headers we have to match when checking
1246 for any address header or all address headers? */
1247 if (procheader_headername_equal(header
->name
, "From") ||
1248 procheader_headername_equal(header
->name
, "To") ||
1249 procheader_headername_equal(header
->name
, "Cc") ||
1250 procheader_headername_equal(header
->name
, "Reply-To") ||
1251 procheader_headername_equal(header
->name
, "Sender"))
1252 address_list
= address_list_append(address_list
, header
->body
);
1253 if (address_list
== NULL
)
1257 found
= match_with_addresses_in_addressbook
1258 (matcher
, address_list
, matcher
->criteria
,
1259 matcher
->expr
, match
);
1260 g_slist_free(address_list
);
1262 if (matcher
->criteria
== MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK
)
1273 *\brief Check if the matcher structure wants headers to
1276 *\param matcher Matcher structure
1278 *\return gboolean TRUE if the matcher structure describes
1279 * a header match condition
1281 static gboolean
matcherprop_criteria_headers(const MatcherProp
*matcher
)
1283 switch (matcher
->criteria
) {
1284 case MATCHCRITERIA_HEADER
:
1285 case MATCHCRITERIA_NOT_HEADER
:
1286 case MATCHCRITERIA_HEADERS_PART
:
1287 case MATCHCRITERIA_NOT_HEADERS_PART
:
1288 case MATCHCRITERIA_FOUND_IN_ADDRESSBOOK
:
1289 case MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK
:
1297 *\brief Check if the matcher structure wants the message
1298 * to be matched (just perform an action on any
1301 *\param matcher Matcher structure
1303 *\return gboolean TRUE if matcher condition should match
1306 static gboolean
matcherprop_criteria_message(MatcherProp
*matcher
)
1308 switch (matcher
->criteria
) {
1309 case MATCHCRITERIA_MESSAGE
:
1310 case MATCHCRITERIA_NOT_MESSAGE
:
1318 *\brief Check if a list of conditions matches one header in
1321 *\param matchers List of conditions
1322 *\param fp Message file
1324 *\return gboolean TRUE if one of the headers is matched by
1325 * the list of conditions.
1327 static gboolean
matcherlist_match_headers(MatcherList
*matchers
, FILE *fp
)
1330 gchar buf
[BUFFSIZE
];
1332 while (procheader_get_one_field(buf
, sizeof(buf
), fp
, NULL
) != -1) {
1333 for (l
= matchers
->matchers
; l
!= NULL
; l
= g_slist_next(l
)) {
1334 MatcherProp
*matcher
= (MatcherProp
*) l
->data
;
1335 gint match
= MATCH_ANY
;
1340 /* determine the match range (all, any are our concern here) */
1341 if (matcher
->criteria
== MATCHCRITERIA_NOT_HEADERS_PART
||
1342 matcher
->criteria
== MATCHCRITERIA_NOT_MESSAGE
) {
1345 } else if (matcher
->criteria
== MATCHCRITERIA_FOUND_IN_ADDRESSBOOK
||
1346 matcher
->criteria
== MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK
) {
1347 Header
*header
= NULL
;
1349 /* address header is one of the headers we have to match when checking
1350 for any address header or all address headers? */
1351 header
= procheader_parse_header(buf
);
1353 (procheader_headername_equal(header
->name
, "From") ||
1354 procheader_headername_equal(header
->name
, "To") ||
1355 procheader_headername_equal(header
->name
, "Cc") ||
1356 procheader_headername_equal(header
->name
, "Reply-To") ||
1357 procheader_headername_equal(header
->name
, "Sender"))) {
1359 if (strcasecmp(matcher
->header
, "Any") == 0)
1361 else if (strcasecmp(matcher
->header
, "All") == 0)
1366 /* further call to matcherprop_match_one_header() can't match
1367 and it irrelevant, so: don't alter the match result */
1372 /* ZERO line must NOT match for the rule to match.
1374 if (match
== MATCH_ALL
) {
1375 if (matcherprop_match_one_header(matcher
, buf
)) {
1376 matcher
->result
= TRUE
;
1378 matcher
->result
= FALSE
;
1379 matcher
->done
= TRUE
;
1381 /* else, just one line matching is enough for the rule to match
1383 } else if (matcherprop_criteria_headers(matcher
) ||
1384 matcherprop_criteria_message(matcher
)) {
1385 if (matcherprop_match_one_header(matcher
, buf
)) {
1386 matcher
->result
= TRUE
;
1387 matcher
->done
= TRUE
;
1391 /* if the rule matched and the matchers are OR, no need to
1392 * check the others */
1393 if (matcher
->result
&& matcher
->done
) {
1394 if (!matchers
->bool_and
)
1404 *\brief Check if a matcher wants to check the message body
1406 *\param matcher Matcher structure
1408 *\return gboolean TRUE if body must be matched.
1410 static gboolean
matcherprop_criteria_body(const MatcherProp
*matcher
)
1412 switch (matcher
->criteria
) {
1413 case MATCHCRITERIA_BODY_PART
:
1414 case MATCHCRITERIA_NOT_BODY_PART
:
1421 static gboolean
matcherlist_match_binary_content(MatcherList
*matchers
, MimeInfo
*partinfo
)
1424 gchar buf
[BUFFSIZE
];
1427 if (partinfo
->type
== MIMETYPE_TEXT
)
1430 outfp
= procmime_get_binary_content(partinfo
);
1435 while (fgets(buf
, sizeof(buf
), outfp
) != NULL
) {
1438 for (l
= matchers
->matchers
; l
!= NULL
; l
= g_slist_next(l
)) {
1439 MatcherProp
*matcher
= (MatcherProp
*) l
->data
;
1444 /* Don't scan non-text parts when looking in body, only
1445 * when looking in whole message
1447 if (partinfo
&& partinfo
->type
!= MIMETYPE_TEXT
&&
1448 (matcher
->criteria
== MATCHCRITERIA_NOT_BODY_PART
||
1449 matcher
->criteria
== MATCHCRITERIA_BODY_PART
))
1452 /* if the criteria is ~body_part or ~message, ZERO lines
1453 * must match for the rule to match.
1455 if (matcher
->criteria
== MATCHCRITERIA_NOT_BODY_PART
||
1456 matcher
->criteria
== MATCHCRITERIA_NOT_MESSAGE
) {
1457 if (matcherprop_string_match(matcher
, buf
,
1458 context_str
[CONTEXT_BODY_LINE
])) {
1459 matcher
->result
= FALSE
;
1460 matcher
->done
= TRUE
;
1462 matcher
->result
= TRUE
;
1463 /* else, just one line has to match */
1464 } else if (matcherprop_criteria_body(matcher
) ||
1465 matcherprop_criteria_message(matcher
)) {
1466 if (matcherprop_string_match(matcher
, buf
,
1467 context_str
[CONTEXT_BODY_LINE
])) {
1468 matcher
->result
= TRUE
;
1469 matcher
->done
= TRUE
;
1473 /* if the matchers are OR'ed and the rule matched,
1474 * no need to check the others. */
1475 if (matcher
->result
&& matcher
->done
) {
1476 if (!matchers
->bool_and
) {
1488 static gboolean
match_content_cb(const gchar
*buf
, gpointer data
)
1490 MatcherList
*matchers
= (MatcherList
*)data
;
1491 gboolean all_done
= TRUE
;
1494 for (l
= matchers
->matchers
; l
!= NULL
; l
= g_slist_next(l
)) {
1495 MatcherProp
*matcher
= (MatcherProp
*) l
->data
;
1500 /* if the criteria is ~body_part or ~message, ZERO lines
1501 * must match for the rule to match.
1503 if (matcher
->criteria
== MATCHCRITERIA_NOT_BODY_PART
||
1504 matcher
->criteria
== MATCHCRITERIA_NOT_MESSAGE
) {
1505 if (matcherprop_string_match(matcher
, buf
,
1506 context_str
[CONTEXT_BODY_LINE
])) {
1507 matcher
->result
= FALSE
;
1508 matcher
->done
= TRUE
;
1510 matcher
->result
= TRUE
;
1511 /* else, just one line has to match */
1512 } else if (matcherprop_criteria_body(matcher
) ||
1513 matcherprop_criteria_message(matcher
)) {
1514 if (matcherprop_string_match(matcher
, buf
,
1515 context_str
[CONTEXT_BODY_LINE
])) {
1516 matcher
->result
= TRUE
;
1517 matcher
->done
= TRUE
;
1521 /* if the matchers are OR'ed and the rule matched,
1522 * no need to check the others. */
1523 if (matcher
->result
&& matcher
->done
) {
1524 if (!matchers
->bool_and
) {
1535 static gboolean
matcherlist_match_text_content(MatcherList
*matchers
, MimeInfo
*partinfo
)
1537 if (partinfo
->type
!= MIMETYPE_TEXT
)
1540 return procmime_scan_text_content(partinfo
, match_content_cb
, matchers
);
1544 *\brief Check if a line in a message file's body matches
1547 *\param matchers List of conditions
1548 *\param fp Message file
1550 *\return gboolean TRUE if succesful match
1552 static gboolean
matcherlist_match_body(MatcherList
*matchers
, gboolean body_only
, MsgInfo
*info
)
1554 MimeInfo
*mimeinfo
= NULL
;
1555 MimeInfo
*partinfo
= NULL
;
1556 gboolean first_text_found
= FALSE
;
1558 cm_return_val_if_fail(info
!= NULL
, FALSE
);
1560 mimeinfo
= procmime_scan_message(info
);
1563 partinfo
= procmime_mimeinfo_next(mimeinfo
);
1565 for (; partinfo
!= NULL
; partinfo
= procmime_mimeinfo_next(partinfo
)) {
1567 if (partinfo
->type
!= MIMETYPE_TEXT
&& body_only
)
1570 if (partinfo
->type
== MIMETYPE_TEXT
) {
1571 first_text_found
= TRUE
;
1572 if (matcherlist_match_text_content(matchers
, partinfo
)) {
1573 procmime_mimeinfo_free_all(mimeinfo
);
1576 } else if (matcherlist_match_binary_content(matchers
, partinfo
)) {
1577 procmime_mimeinfo_free_all(mimeinfo
);
1581 if (body_only
&& first_text_found
)
1584 procmime_mimeinfo_free_all(mimeinfo
);
1590 *\brief Check if a message file matches criteria
1592 *\param matchers Criteria
1593 *\param info Message info
1594 *\param result Default result
1596 *\return gboolean TRUE if matched
1598 static gboolean
matcherlist_match_file(MatcherList
*matchers
, MsgInfo
*info
,
1601 gboolean read_headers
;
1608 /* file need to be read ? */
1610 read_headers
= FALSE
;
1613 for (l
= matchers
->matchers
; l
!= NULL
; l
= g_slist_next(l
)) {
1614 MatcherProp
*matcher
= (MatcherProp
*) l
->data
;
1616 if (matcherprop_criteria_headers(matcher
))
1617 read_headers
= TRUE
;
1618 if (matcherprop_criteria_body(matcher
))
1620 if (matcherprop_criteria_message(matcher
)) {
1621 read_headers
= TRUE
;
1625 matcher
->result
= FALSE
;
1626 matcher
->done
= FALSE
;
1629 if (!read_headers
&& !read_body
)
1632 file
= procmsg_get_message_file_full(info
, read_headers
, read_body
);
1636 if ((fp
= g_fopen(file
, "rb")) == NULL
) {
1637 FILE_OP_ERROR(file
, "fopen");
1642 /* read the headers */
1645 if (matcherlist_match_headers(matchers
, fp
))
1648 matcherlist_skip_headers(fp
);
1653 matcherlist_match_body(matchers
, body_only
, info
);
1656 for (l
= matchers
->matchers
; l
!= NULL
; l
= g_slist_next(l
)) {
1657 MatcherProp
*matcher
= (MatcherProp
*) l
->data
;
1659 if (matcherprop_criteria_headers(matcher
) ||
1660 matcherprop_criteria_body(matcher
) ||
1661 matcherprop_criteria_message(matcher
)) {
1662 if (matcher
->result
) {
1663 if (!matchers
->bool_and
) {
1669 if (matchers
->bool_and
) {
1685 *\brief Test list of conditions on a message.
1687 *\param matchers List of conditions
1688 *\param info Message info
1690 *\return gboolean TRUE if matched
1692 gboolean
matcherlist_match(MatcherList
*matchers
, MsgInfo
*info
)
1700 if (matchers
->bool_and
)
1705 /* test the cached elements */
1707 for (l
= matchers
->matchers
; l
!= NULL
;l
= g_slist_next(l
)) {
1708 MatcherProp
*matcher
= (MatcherProp
*) l
->data
;
1710 if (debug_filtering_session
) {
1711 gchar
*buf
= matcherprop_to_string(matcher
);
1712 log_print(LOG_DEBUG_FILTERING
, _("checking if message matches [ %s ]\n"), buf
);
1716 switch(matcher
->criteria
) {
1717 case MATCHCRITERIA_ALL
:
1718 case MATCHCRITERIA_UNREAD
:
1719 case MATCHCRITERIA_NOT_UNREAD
:
1720 case MATCHCRITERIA_NEW
:
1721 case MATCHCRITERIA_NOT_NEW
:
1722 case MATCHCRITERIA_MARKED
:
1723 case MATCHCRITERIA_NOT_MARKED
:
1724 case MATCHCRITERIA_DELETED
:
1725 case MATCHCRITERIA_NOT_DELETED
:
1726 case MATCHCRITERIA_REPLIED
:
1727 case MATCHCRITERIA_NOT_REPLIED
:
1728 case MATCHCRITERIA_FORWARDED
:
1729 case MATCHCRITERIA_NOT_FORWARDED
:
1730 case MATCHCRITERIA_LOCKED
:
1731 case MATCHCRITERIA_NOT_LOCKED
:
1732 case MATCHCRITERIA_SPAM
:
1733 case MATCHCRITERIA_NOT_SPAM
:
1734 case MATCHCRITERIA_HAS_ATTACHMENT
:
1735 case MATCHCRITERIA_HAS_NO_ATTACHMENT
:
1736 case MATCHCRITERIA_SIGNED
:
1737 case MATCHCRITERIA_NOT_SIGNED
:
1738 case MATCHCRITERIA_COLORLABEL
:
1739 case MATCHCRITERIA_NOT_COLORLABEL
:
1740 case MATCHCRITERIA_IGNORE_THREAD
:
1741 case MATCHCRITERIA_NOT_IGNORE_THREAD
:
1742 case MATCHCRITERIA_WATCH_THREAD
:
1743 case MATCHCRITERIA_NOT_WATCH_THREAD
:
1744 case MATCHCRITERIA_SUBJECT
:
1745 case MATCHCRITERIA_NOT_SUBJECT
:
1746 case MATCHCRITERIA_FROM
:
1747 case MATCHCRITERIA_NOT_FROM
:
1748 case MATCHCRITERIA_TO
:
1749 case MATCHCRITERIA_NOT_TO
:
1750 case MATCHCRITERIA_CC
:
1751 case MATCHCRITERIA_NOT_CC
:
1752 case MATCHCRITERIA_TO_OR_CC
:
1753 case MATCHCRITERIA_NOT_TO_AND_NOT_CC
:
1754 case MATCHCRITERIA_TAG
:
1755 case MATCHCRITERIA_NOT_TAG
:
1756 case MATCHCRITERIA_TAGGED
:
1757 case MATCHCRITERIA_NOT_TAGGED
:
1758 case MATCHCRITERIA_AGE_GREATER
:
1759 case MATCHCRITERIA_AGE_LOWER
:
1760 case MATCHCRITERIA_AGE_GREATER_HOURS
:
1761 case MATCHCRITERIA_AGE_LOWER_HOURS
:
1762 case MATCHCRITERIA_NEWSGROUPS
:
1763 case MATCHCRITERIA_NOT_NEWSGROUPS
:
1764 case MATCHCRITERIA_INREPLYTO
:
1765 case MATCHCRITERIA_NOT_INREPLYTO
:
1766 case MATCHCRITERIA_REFERENCES
:
1767 case MATCHCRITERIA_NOT_REFERENCES
:
1768 case MATCHCRITERIA_SCORE_GREATER
:
1769 case MATCHCRITERIA_SCORE_LOWER
:
1770 case MATCHCRITERIA_SCORE_EQUAL
:
1771 case MATCHCRITERIA_SIZE_GREATER
:
1772 case MATCHCRITERIA_SIZE_SMALLER
:
1773 case MATCHCRITERIA_SIZE_EQUAL
:
1774 case MATCHCRITERIA_TEST
:
1775 case MATCHCRITERIA_NOT_TEST
:
1776 case MATCHCRITERIA_PARTIAL
:
1777 case MATCHCRITERIA_NOT_PARTIAL
:
1778 if (matcherprop_match(matcher
, info
)) {
1779 if (!matchers
->bool_and
) {
1780 if (debug_filtering_session
)
1781 log_status_ok(LOG_DEBUG_FILTERING
, _("message matches\n"));
1786 if (matchers
->bool_and
) {
1787 if (debug_filtering_session
)
1788 log_status_nok(LOG_DEBUG_FILTERING
, _("message does not match\n"));
1795 /* test the condition on the file */
1797 if (matcherlist_match_file(matchers
, info
, result
)) {
1798 if (!matchers
->bool_and
) {
1799 if (debug_filtering_session
)
1800 log_status_ok(LOG_DEBUG_FILTERING
, _("message matches\n"));
1804 if (matchers
->bool_and
) {
1805 if (debug_filtering_session
)
1806 log_status_nok(LOG_DEBUG_FILTERING
, _("message does not match\n"));
1811 if (debug_filtering_session
) {
1813 log_status_ok(LOG_DEBUG_FILTERING
, _("message matches\n"));
1815 log_status_nok(LOG_DEBUG_FILTERING
, _("message does not match\n"));
1821 static gint
quote_filter_str(gchar
* result
, guint size
,
1831 for(p
= path
; * p
!= '\0' ; p
++) {
1833 if ((* p
!= '\"') && (* p
!= '\\')) {
1834 if (remaining
> 0) {
1840 result
[size
- 1] = '\0';
1845 if (remaining
>= 2) {
1853 result
[size
- 1] = '\0';
1858 if (remaining
> 0) {
1862 result
[size
- 1] = '\0';
1870 gchar
* matcher_quote_str(const gchar
* src
)
1875 len
= strlen(src
) * 2 + 1;
1876 res
= g_malloc(len
);
1877 quote_filter_str(res
, len
, src
);
1883 *\brief Convert a matcher structure to a string
1885 *\param matcher Matcher structure
1887 *\return gchar * Newly allocated string
1889 gchar
*matcherprop_to_string(MatcherProp
*matcher
)
1891 gchar
*matcher_str
= NULL
;
1892 const gchar
*criteria_str
;
1893 const gchar
*matchtype_str
;
1895 gchar
* quoted_expr
;
1896 gchar
* quoted_header
;
1898 criteria_str
= NULL
;
1899 for (i
= 0; i
< (int) (sizeof(matchparser_tab
) / sizeof(MatchParser
)); i
++) {
1900 if (matchparser_tab
[i
].id
== matcher
->criteria
)
1901 criteria_str
= matchparser_tab
[i
].str
;
1903 if (criteria_str
== NULL
)
1906 switch (matcher
->criteria
) {
1907 case MATCHCRITERIA_AGE_GREATER
:
1908 case MATCHCRITERIA_AGE_LOWER
:
1909 case MATCHCRITERIA_AGE_GREATER_HOURS
:
1910 case MATCHCRITERIA_AGE_LOWER_HOURS
:
1911 case MATCHCRITERIA_SCORE_GREATER
:
1912 case MATCHCRITERIA_SCORE_LOWER
:
1913 case MATCHCRITERIA_SCORE_EQUAL
:
1914 case MATCHCRITERIA_SIZE_GREATER
:
1915 case MATCHCRITERIA_SIZE_SMALLER
:
1916 case MATCHCRITERIA_SIZE_EQUAL
:
1917 case MATCHCRITERIA_COLORLABEL
:
1918 case MATCHCRITERIA_NOT_COLORLABEL
:
1919 return g_strdup_printf("%s %i", criteria_str
, matcher
->value
);
1920 case MATCHCRITERIA_ALL
:
1921 case MATCHCRITERIA_UNREAD
:
1922 case MATCHCRITERIA_NOT_UNREAD
:
1923 case MATCHCRITERIA_NEW
:
1924 case MATCHCRITERIA_NOT_NEW
:
1925 case MATCHCRITERIA_MARKED
:
1926 case MATCHCRITERIA_NOT_MARKED
:
1927 case MATCHCRITERIA_DELETED
:
1928 case MATCHCRITERIA_NOT_DELETED
:
1929 case MATCHCRITERIA_REPLIED
:
1930 case MATCHCRITERIA_NOT_REPLIED
:
1931 case MATCHCRITERIA_FORWARDED
:
1932 case MATCHCRITERIA_NOT_FORWARDED
:
1933 case MATCHCRITERIA_LOCKED
:
1934 case MATCHCRITERIA_NOT_LOCKED
:
1935 case MATCHCRITERIA_SPAM
:
1936 case MATCHCRITERIA_NOT_SPAM
:
1937 case MATCHCRITERIA_HAS_ATTACHMENT
:
1938 case MATCHCRITERIA_HAS_NO_ATTACHMENT
:
1939 case MATCHCRITERIA_SIGNED
:
1940 case MATCHCRITERIA_NOT_SIGNED
:
1941 case MATCHCRITERIA_PARTIAL
:
1942 case MATCHCRITERIA_NOT_PARTIAL
:
1943 case MATCHCRITERIA_IGNORE_THREAD
:
1944 case MATCHCRITERIA_NOT_IGNORE_THREAD
:
1945 case MATCHCRITERIA_WATCH_THREAD
:
1946 case MATCHCRITERIA_NOT_WATCH_THREAD
:
1947 case MATCHCRITERIA_TAGGED
:
1948 case MATCHCRITERIA_NOT_TAGGED
:
1949 return g_strdup(criteria_str
);
1950 case MATCHCRITERIA_TEST
:
1951 case MATCHCRITERIA_NOT_TEST
:
1952 quoted_expr
= matcher_quote_str(matcher
->expr
);
1953 matcher_str
= g_strdup_printf("%s \"%s\"",
1954 criteria_str
, quoted_expr
);
1955 g_free(quoted_expr
);
1957 case MATCHCRITERIA_FOUND_IN_ADDRESSBOOK
:
1958 case MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK
:
1959 quoted_header
= matcher_quote_str(matcher
->header
);
1960 quoted_expr
= matcher_quote_str(matcher
->expr
);
1961 matcher_str
= g_strdup_printf("%s \"%s\" in \"%s\"",
1962 criteria_str
, quoted_header
, quoted_expr
);
1963 g_free(quoted_header
);
1964 g_free(quoted_expr
);
1968 matchtype_str
= NULL
;
1969 for (i
= 0; i
< sizeof matchparser_tab
/ sizeof matchparser_tab
[0]; i
++) {
1970 if (matchparser_tab
[i
].id
== matcher
->matchtype
)
1971 matchtype_str
= matchparser_tab
[i
].str
;
1974 if (matchtype_str
== NULL
)
1977 switch (matcher
->matchtype
) {
1978 case MATCHTYPE_MATCH
:
1979 case MATCHTYPE_MATCHCASE
:
1980 case MATCHTYPE_REGEXP
:
1981 case MATCHTYPE_REGEXPCASE
:
1982 quoted_expr
= matcher_quote_str(matcher
->expr
);
1983 if (matcher
->header
) {
1984 quoted_header
= matcher_quote_str(matcher
->header
);
1985 matcher_str
= g_strdup_printf
1986 ("%s \"%s\" %s \"%s\"",
1987 criteria_str
, quoted_header
,
1988 matchtype_str
, quoted_expr
);
1989 g_free(quoted_header
);
1992 matcher_str
= g_strdup_printf
1993 ("%s %s \"%s\"", criteria_str
,
1994 matchtype_str
, quoted_expr
);
1995 g_free(quoted_expr
);
2003 *\brief Convert a list of conditions to a string
2005 *\param matchers List of conditions
2007 *\return gchar * Newly allocated string
2009 gchar
*matcherlist_to_string(const MatcherList
*matchers
)
2015 gchar
*result
= NULL
;
2017 count
= g_slist_length(matchers
->matchers
);
2018 vstr
= g_new(gchar
*, count
+ 1);
2020 for (l
= matchers
->matchers
, cur_str
= vstr
; l
!= NULL
;
2021 l
= g_slist_next(l
), cur_str
++) {
2022 *cur_str
= matcherprop_to_string((MatcherProp
*) l
->data
);
2023 if (*cur_str
== NULL
)
2028 if (matchers
->bool_and
)
2029 result
= g_strjoinv(" & ", vstr
);
2031 result
= g_strjoinv(" | ", vstr
);
2033 for (cur_str
= vstr
; *cur_str
!= NULL
; cur_str
++)
2041 #define STRLEN_ZERO(s) ((s) ? strlen(s) : 0)
2042 #define STRLEN_DEFAULT(s,d) ((s) ? strlen(s) : STRLEN_ZERO(d))
2044 static void add_str_default(gchar
** dest
,
2045 const gchar
* s
, const gchar
* d
)
2047 gchar quoted_str
[4096];
2055 quote_cmd_argument(quoted_str
, sizeof(quoted_str
), str
);
2056 strcpy(* dest
, quoted_str
);
2058 (* dest
) += strlen(* dest
);
2061 /* matching_build_command() - preferably cmd should be unescaped */
2063 *\brief Build the command-line to execute
2065 *\param cmd String with command-line specifiers
2066 *\param info Message info to use for command
2068 *\return gchar * Newly allocated string
2070 gchar
*matching_build_command(const gchar
*cmd
, MsgInfo
*info
)
2072 const gchar
*s
= cmd
;
2073 gchar
*filename
= NULL
;
2074 gchar
*processed_cmd
;
2078 const gchar
*const no_subject
= _("(none)") ;
2079 const gchar
*const no_from
= _("(none)") ;
2080 const gchar
*const no_to
= _("(none)") ;
2081 const gchar
*const no_cc
= _("(none)") ;
2082 const gchar
*const no_date
= _("(none)") ;
2083 const gchar
*const no_msgid
= _("(none)") ;
2084 const gchar
*const no_newsgroups
= _("(none)") ;
2085 const gchar
*const no_references
= _("(none)") ;
2087 size
= STRLEN_ZERO(cmd
) + 1;
2088 while (*s
!= '\0') {
2095 case 's': /* subject */
2096 size
+= STRLEN_DEFAULT(info
->subject
, no_subject
) - 2;
2098 case 'f': /* from */
2099 size
+= STRLEN_DEFAULT(info
->from
, no_from
) - 2;
2102 size
+= STRLEN_DEFAULT(info
->to
, no_to
) - 2;
2105 size
+= STRLEN_DEFAULT(info
->cc
, no_cc
) - 2;
2107 case 'd': /* date */
2108 size
+= STRLEN_DEFAULT(info
->date
, no_date
) - 2;
2110 case 'i': /* message-id */
2111 size
+= STRLEN_DEFAULT(info
->msgid
, no_msgid
) - 2;
2113 case 'n': /* newsgroups */
2114 size
+= STRLEN_DEFAULT(info
->newsgroups
, no_newsgroups
) - 2;
2116 case 'r': /* references */
2117 /* FIXME: using the inreplyto header for reference */
2118 size
+= STRLEN_DEFAULT(info
->inreplyto
, no_references
) - 2;
2120 case 'F': /* file */
2121 if (filename
== NULL
)
2122 filename
= folder_item_fetch_msg(info
->folder
, info
->msgnum
);
2124 if (filename
== NULL
) {
2125 g_warning("filename is not set");
2129 size
+= strlen(filename
) - 2;
2138 /* as the string can be quoted, we double the result */
2141 processed_cmd
= g_new0(gchar
, size
);
2145 while (*s
!= '\0') {
2153 case 's': /* subject */
2154 add_str_default(&p
, info
->subject
,
2157 case 'f': /* from */
2158 add_str_default(&p
, info
->from
,
2162 add_str_default(&p
, info
->to
,
2166 add_str_default(&p
, info
->cc
,
2169 case 'd': /* date */
2170 add_str_default(&p
, info
->date
,
2173 case 'i': /* message-id */
2174 add_str_default(&p
, info
->msgid
,
2177 case 'n': /* newsgroups */
2178 add_str_default(&p
, info
->newsgroups
,
2181 case 'r': /* references */
2182 /* FIXME: using the inreplyto header for references */
2183 add_str_default(&p
, info
->inreplyto
, no_references
);
2185 case 'F': /* file */
2186 if (filename
!= NULL
)
2187 add_str_default(&p
, filename
, NULL
);
2206 return processed_cmd
;
2208 #undef STRLEN_DEFAULT
2211 /* ************************************************************ */
2215 *\brief Write filtering list to file
2218 *\param prefs_filtering List of filtering conditions
2220 static int prefs_filtering_write(FILE *fp
, GSList
*prefs_filtering
)
2224 for (cur
= prefs_filtering
; cur
!= NULL
; cur
= cur
->next
) {
2225 gchar
*filtering_str
= NULL
;
2226 gchar
*tmp_name
= NULL
;
2227 FilteringProp
*prop
= NULL
;
2229 if (NULL
== (prop
= (FilteringProp
*) cur
->data
))
2232 if (NULL
== (filtering_str
= filteringprop_to_string(prop
)))
2235 if (prop
->enabled
) {
2236 if (fputs("enabled ", fp
) == EOF
) {
2237 FILE_OP_ERROR("filtering config", "fputs");
2241 if (fputs("disabled ", fp
) == EOF
) {
2242 FILE_OP_ERROR("filtering config", "fputs");
2247 if (fputs("rulename \"", fp
) == EOF
) {
2248 FILE_OP_ERROR("filtering config", "fputs");
2249 g_free(filtering_str
);
2252 tmp_name
= prop
->name
;
2253 while (tmp_name
&& *tmp_name
!= '\0') {
2254 if (*tmp_name
!= '"') {
2255 if (fputc(*tmp_name
, fp
) == EOF
) {
2256 FILE_OP_ERROR("filtering config", "fputs || fputc");
2257 g_free(filtering_str
);
2260 } else if (*tmp_name
== '"') {
2261 if (fputc('\\', fp
) == EOF
||
2262 fputc('"', fp
) == EOF
) {
2263 FILE_OP_ERROR("filtering config", "fputs || fputc");
2264 g_free(filtering_str
);
2270 if (fputs("\" ", fp
) == EOF
) {
2271 FILE_OP_ERROR("filtering config", "fputs");
2272 g_free(filtering_str
);
2276 if (prop
->account_id
!= 0) {
2279 tmp
= g_strdup_printf("account %d ", prop
->account_id
);
2280 if (fputs(tmp
, fp
) == EOF
) {
2281 FILE_OP_ERROR("filtering config", "fputs");
2288 if(fputs(filtering_str
, fp
) == EOF
||
2289 fputc('\n', fp
) == EOF
) {
2290 FILE_OP_ERROR("filtering config", "fputs || fputc");
2291 g_free(filtering_str
);
2294 g_free(filtering_str
);
2300 typedef struct _NodeLoopData
{
2306 *\brief Write matchers from a folder item
2308 *\param node Node with folder info
2309 *\param data File pointer
2311 *\return gboolean FALSE
2313 static gboolean
prefs_matcher_write_func(GNode
*node
, gpointer d
)
2316 NodeLoopData
*data
= (NodeLoopData
*)d
;
2318 GSList
*prefs_filtering
;
2321 /* prevent warning */
2322 if (item
->path
== NULL
)
2324 id
= folder_item_get_identifier(item
);
2327 prefs_filtering
= item
->prefs
->processing
;
2329 if (prefs_filtering
!= NULL
) {
2330 if (fprintf(data
->fp
, "[%s]\n", id
) < 0) {
2334 if (prefs_filtering_write(data
->fp
, prefs_filtering
) < 0) {
2338 if (fputc('\n', data
->fp
) == EOF
) {
2350 *\brief Save matchers from folder items
2354 static int prefs_matcher_save(FILE *fp
)
2362 for (cur
= folder_get_list() ; cur
!= NULL
; cur
= g_list_next(cur
)) {
2365 folder
= (Folder
*) cur
->data
;
2366 g_node_traverse(folder
->node
, G_PRE_ORDER
, G_TRAVERSE_ALL
, -1,
2367 prefs_matcher_write_func
, &data
);
2370 if (data
.error
== TRUE
)
2373 /* pre global rules */
2374 if (fprintf(fp
, "[preglobal]\n") < 0 ||
2375 prefs_filtering_write(fp
, pre_global_processing
) < 0 ||
2376 fputc('\n', fp
) == EOF
)
2379 /* post global rules */
2380 if (fprintf(fp
, "[postglobal]\n") < 0 ||
2381 prefs_filtering_write(fp
, post_global_processing
) < 0 ||
2382 fputc('\n', fp
) == EOF
)
2385 /* filtering rules */
2386 if (fprintf(fp
, "[filtering]\n") < 0 ||
2387 prefs_filtering_write(fp
, filtering_rules
) < 0 ||
2388 fputc('\n', fp
) == EOF
)
2395 *\brief Write filtering / matcher configuration file
2397 void prefs_matcher_write_config(void)
2402 debug_print("Writing matcher configuration...\n");
2404 rcpath
= g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S
,
2407 if ((pfile
= prefs_write_open(rcpath
)) == NULL
) {
2408 g_warning("failed to write configuration to file\n");
2415 if (prefs_matcher_save(pfile
->fp
) < 0) {
2416 g_warning("failed to write configuration to file\n");
2417 prefs_file_close_revert(pfile
);
2418 } else if (prefs_file_close(pfile
) < 0) {
2419 g_warning("failed to save configuration to file\n");
2423 /* ******************************************************************* */
2425 static void matcher_add_rulenames(const gchar
*rcpath
)
2427 gchar
*newpath
= g_strconcat(rcpath
, ".new", NULL
);
2428 FILE *src
= g_fopen(rcpath
, "rb");
2429 FILE *dst
= g_fopen(newpath
, "wb");
2430 gchar buf
[BUFFSIZE
];
2448 while (fgets (buf
, sizeof(buf
), src
) != NULL
) {
2449 if (strlen(buf
) > 2 && buf
[0] != '['
2450 && strncmp(buf
, "rulename \"", 10)
2451 && strncmp(buf
, "enabled rulename \"", 18)
2452 && strncmp(buf
, "disabled rulename \"", 18)) {
2453 r
= fwrite("enabled rulename \"\" ",
2454 strlen("enabled rulename \"\" "), 1, dst
);
2456 g_message("cannot fwrite rulename\n");
2459 r
= fwrite(buf
, strlen(buf
), 1, dst
);
2461 g_message("cannot fwrite rule\n");
2466 move_file(newpath
, rcpath
, TRUE
);
2471 *\brief Read matcher configuration
2473 void prefs_matcher_read_config(void)
2476 gchar
*rc_old_format
;
2479 create_matchparser_hashtab();
2480 prefs_filtering_clear();
2482 rcpath
= g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S
, MATCHER_RC
, NULL
);
2483 rc_old_format
= g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S
, MATCHER_RC
,
2484 ".pre_names", NULL
);
2486 if (!is_file_exist(rc_old_format
) && is_file_exist(rcpath
)) {
2487 /* backup file with no rules names, in case
2488 * anything goes wrong */
2489 copy_file(rcpath
, rc_old_format
, FALSE
);
2490 /* now hack the file in order to have it to the new format */
2491 matcher_add_rulenames(rcpath
);
2494 g_free(rc_old_format
);
2496 f
= g_fopen(rcpath
, "rb");
2500 matcher_parser_start_parsing(f
);
2501 fclose(matcher_parserin
);
2504 /* previous version compatibility */
2506 /* g_print("reading filtering\n"); */
2507 rcpath
= g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S
,
2508 FILTERING_RC
, NULL
);
2509 f
= g_fopen(rcpath
, "rb");
2513 matcher_parser_start_parsing(f
);
2514 fclose(matcher_parserin
);
2517 /* g_print("reading scoring\n"); */
2518 rcpath
= g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S
,
2520 f
= g_fopen(rcpath
, "rb");
2524 matcher_parser_start_parsing(f
);
2525 fclose(matcher_parserin
);