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
;
326 *\brief Free a matcher structure
328 *\param prop Pointer to matcher structure allocated with
331 void matcherprop_free(MatcherProp
*prop
)
334 g_free(prop
->header
);
336 if (prop
->preg
!= NULL
) {
345 *\brief Copy a matcher structure
347 *\param src Matcher structure to copy
349 *\return MatcherProp * Pointer to newly allocated matcher structure
351 MatcherProp
*matcherprop_copy(const MatcherProp
*src
)
353 MatcherProp
*prop
= g_new0(MatcherProp
, 1);
355 prop
->criteria
= src
->criteria
;
356 prop
->header
= src
->header
? g_strdup(src
->header
) : NULL
;
357 prop
->expr
= src
->expr
? g_strdup(src
->expr
) : NULL
;
358 prop
->matchtype
= src
->matchtype
;
361 prop
->preg
= NULL
; /* will be re-evaluated */
363 prop
->value
= src
->value
;
364 prop
->error
= src
->error
;
368 /* ************** match ******************************/
370 static gboolean match_with_addresses_in_addressbook
371 (MatcherProp
*prop
, GSList
*address_list
, gint type
,
372 gchar
* folderpath
, gint match
)
375 gboolean found
= FALSE
;
378 cm_return_val_if_fail(address_list
!= NULL
, FALSE
);
380 debug_print("match_with_addresses_in_addressbook(%d, %s)\n",
381 g_slist_length(address_list
), folderpath
?folderpath
:"(null)");
383 if (folderpath
== NULL
||
384 strcasecmp(folderpath
, "Any") == 0 ||
390 start_address_completion(path
);
392 for (walk
= address_list
; walk
!= NULL
; walk
= walk
->next
) {
393 /* exact matching of email address */
394 guint num_addr
= complete_address(walk
->data
);
397 /* skip first item (this is the search string itself) */
399 for (; i
< num_addr
&& !found
; i
++) {
400 gchar
*addr
= get_complete_address(i
);
401 extract_address(addr
);
402 if (strcasecmp(addr
, walk
->data
) == 0) {
406 if (debug_filtering_session
407 && prefs_common
.filtering_debug_level
>= FILTERING_DEBUG_LEVEL_HIGH
) {
408 log_print(LOG_DEBUG_FILTERING
,
409 "address [ %s ] matches\n",
410 (gchar
*)walk
->data
);
417 if (debug_filtering_session
418 && prefs_common
.filtering_debug_level
>= FILTERING_DEBUG_LEVEL_HIGH
420 log_print(LOG_DEBUG_FILTERING
,
421 "address [ %s ] does NOT match\n",
422 (gchar
*)walk
->data
);
426 if (match
== MATCH_ALL
) {
427 /* if matching all addresses, stop if one doesn't match */
430 if (debug_filtering_session
431 && prefs_common
.filtering_debug_level
>= FILTERING_DEBUG_LEVEL_HIGH
) {
432 log_print(LOG_DEBUG_FILTERING
,
433 "not all address match (matching all)\n");
437 } else if (match
== MATCH_ANY
) {
438 /* if matching any address, stop if one does match */
441 if (debug_filtering_session
442 && prefs_common
.filtering_debug_level
>= FILTERING_DEBUG_LEVEL_HIGH
) {
443 log_print(LOG_DEBUG_FILTERING
,
444 "at least one address matches (matching any)\n");
449 /* MATCH_ONE: there should be only one loop iteration */
452 end_address_completion();
458 *\brief Find out if a string matches a condition
460 *\param prop Matcher structure
461 *\param str String to check
463 *\return gboolean TRUE if str matches the condition in the
466 static gboolean
matcherprop_string_match(MatcherProp
*prop
, const gchar
*str
,
467 const gchar
*debug_context
)
471 gboolean ret
= FALSE
;
472 gboolean should_free
= FALSE
;
476 if (prop
->matchtype
== MATCHTYPE_REGEXPCASE
||
477 prop
->matchtype
== MATCHTYPE_MATCHCASE
) {
478 str1
= g_utf8_casefold(str
, -1);
479 down_expr
= g_utf8_casefold(prop
->expr
, -1);
483 down_expr
= (gchar
*)prop
->expr
;
487 switch (prop
->matchtype
) {
488 case MATCHTYPE_REGEXPCASE
:
489 case MATCHTYPE_REGEXP
:
491 if (!prop
->preg
&& (prop
->error
== 0)) {
492 prop
->preg
= g_new0(regex_t
, 1);
493 /* if regexp then don't use the escaped string */
494 if (regcomp(prop
->preg
, down_expr
,
495 REG_NOSUB
| REG_EXTENDED
496 | ((prop
->matchtype
== MATCHTYPE_REGEXPCASE
)
497 ? REG_ICASE
: 0)) != 0) {
503 if (prop
->preg
== NULL
) {
508 if (regexec(prop
->preg
, str1
, 0, NULL
, 0) == 0)
514 if (debug_filtering_session
515 && prefs_common
.filtering_debug_level
>= FILTERING_DEBUG_LEVEL_HIGH
) {
516 gchar
*stripped
= g_strdup(str
);
518 strretchomp(stripped
);
520 log_print(LOG_DEBUG_FILTERING
,
521 "%s value [ %s ] matches regular expression [ %s ] (%s)\n",
522 debug_context
, stripped
, prop
->expr
,
523 prop
->matchtype
== MATCHTYPE_REGEXP
? _("Case sensitive"):_("Case insensitive"));
525 log_print(LOG_DEBUG_FILTERING
,
526 "%s value [ %s ] does NOT match regular expression [ %s ] (%s)\n",
527 debug_context
, stripped
, prop
->expr
,
528 prop
->matchtype
== MATCHTYPE_REGEXP
? _("Case sensitive"):_("Case insensitive"));
534 case MATCHTYPE_MATCHCASE
:
535 case MATCHTYPE_MATCH
:
536 ret
= (strstr(str1
, down_expr
) != NULL
);
539 if (debug_filtering_session
540 && prefs_common
.filtering_debug_level
>= FILTERING_DEBUG_LEVEL_HIGH
) {
541 gchar
*stripped
= g_strdup(str
);
543 strretchomp(stripped
);
545 log_print(LOG_DEBUG_FILTERING
,
546 "%s value [ %s ] contains [ %s ] (%s)\n",
547 debug_context
, stripped
, prop
->expr
,
548 prop
->matchtype
== MATCHTYPE_MATCH
? _("Case sensitive"):_("Case insensitive"));
550 log_print(LOG_DEBUG_FILTERING
,
551 "%s value [ %s ] does NOT contain [ %s ] (%s)\n",
552 debug_context
, stripped
, prop
->expr
,
553 prop
->matchtype
== MATCHTYPE_MATCH
? _("Case sensitive"):_("Case insensitive"));
572 *\brief Find out if a tag matches a condition
574 *\param prop Matcher structure
575 *\param msginfo message to check
577 *\return gboolean TRUE if msginfo matches the condition in the
580 static gboolean
matcherprop_tag_match(MatcherProp
*prop
, MsgInfo
*msginfo
,
581 const gchar
*debug_context
)
583 gboolean ret
= FALSE
;
586 if (msginfo
== NULL
|| msginfo
->tags
== NULL
)
589 for (cur
= msginfo
->tags
; cur
; cur
= cur
->next
) {
590 const gchar
*str
= tags_get_tag(GPOINTER_TO_INT(cur
->data
));
593 if (matcherprop_string_match(prop
, str
, debug_context
)) {
602 *\brief Find out if the string-ed list matches a condition
604 *\param prop Matcher structure
605 *\param list GSList of strings to check
607 *\return gboolean TRUE if str matches the condition in the
610 static gboolean
matcherprop_list_match(MatcherProp
*prop
, const GSList
*list
,
611 const gchar
*debug_context
)
615 for(cur
= list
; cur
!= NULL
; cur
= cur
->next
) {
616 if (matcherprop_string_match(prop
, (gchar
*)cur
->data
, debug_context
))
622 static gboolean
matcherprop_header_line_match(MatcherProp
*prop
, const gchar
*hdr
,
623 const gchar
*str
, const gchar
*debug_context
)
626 gboolean res
= FALSE
;
628 if (hdr
== NULL
|| str
== NULL
)
631 line
= g_strdup_printf("%s %s", hdr
, str
);
632 res
= matcherprop_string_match(prop
, line
, debug_context
);
639 typedef struct _thread_data
{
646 static void *matcher_test_thread(void *data
)
648 thread_data
*td
= (thread_data
*)data
;
651 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE
, NULL
);
652 pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS
, NULL
);
654 result
= system(td
->cmd
);
655 td
->done
= TRUE
; /* let the caller thread join() */
656 return GINT_TO_POINTER(result
);
661 *\brief Execute a command defined in the matcher structure
663 *\param prop Pointer to matcher structure
664 *\param info Pointer to message info structure
666 *\return gboolean TRUE if command was executed succesfully
668 static gboolean
matcherprop_match_test(const MatcherProp
*prop
,
677 thread_data
*td
= g_new0(thread_data
, 1);
679 time_t start_time
= time(NULL
);
682 file
= procmsg_get_message_file(info
);
691 cmd
= matching_build_command(prop
->expr
, info
);
701 if (debug_filtering_session
702 && prefs_common
.filtering_debug_level
>= FILTERING_DEBUG_LEVEL_HIGH
) {
703 log_print(LOG_DEBUG_FILTERING
,
704 "starting threaded command [ %s ]\n",
710 if (pthread_attr_init(&pta
) != 0 ||
711 pthread_attr_setdetachstate(&pta
, PTHREAD_CREATE_JOINABLE
) != 0 ||
712 pthread_create(&pt
, &pta
, matcher_test_thread
, td
) != 0)
713 retval
= system(cmd
);
715 debug_print("waiting for test thread\n");
717 /* don't let the interface freeze while waiting */
719 if (time(NULL
) - start_time
> 30) {
725 pthread_join(pt
, &res
);
726 retval
= GPOINTER_TO_INT(res
);
727 debug_print(" test thread returned %d\n", retval
);
732 if (debug_filtering_session
733 && prefs_common
.filtering_debug_level
>= FILTERING_DEBUG_LEVEL_HIGH
) {
734 log_print(LOG_DEBUG_FILTERING
,
735 "starting synchronous command [ %s ]\n",
739 retval
= system(cmd
);
741 debug_print("Command exit code: %d\n", retval
);
744 if (debug_filtering_session
745 && prefs_common
.filtering_debug_level
>= FILTERING_DEBUG_LEVEL_HIGH
) {
746 log_print(LOG_DEBUG_FILTERING
,
747 "command returned [ %d ]\n",
752 return (retval
== 0);
756 *\brief Check if a message matches the condition in a matcher
759 *\param prop Pointer to matcher structure
760 *\param info Pointer to message info
762 *\return gboolean TRUE if a match
764 static gboolean
matcherprop_match(MatcherProp
*prop
,
768 gint age_mult_hours
= 1;
770 switch(prop
->criteria
) {
771 case MATCHCRITERIA_ALL
:
773 case MATCHCRITERIA_UNREAD
:
774 return MSG_IS_UNREAD(info
->flags
);
775 case MATCHCRITERIA_NOT_UNREAD
:
776 return !MSG_IS_UNREAD(info
->flags
);
777 case MATCHCRITERIA_NEW
:
778 return MSG_IS_NEW(info
->flags
);
779 case MATCHCRITERIA_NOT_NEW
:
780 return !MSG_IS_NEW(info
->flags
);
781 case MATCHCRITERIA_MARKED
:
782 return MSG_IS_MARKED(info
->flags
);
783 case MATCHCRITERIA_NOT_MARKED
:
784 return !MSG_IS_MARKED(info
->flags
);
785 case MATCHCRITERIA_DELETED
:
786 return MSG_IS_DELETED(info
->flags
);
787 case MATCHCRITERIA_NOT_DELETED
:
788 return !MSG_IS_DELETED(info
->flags
);
789 case MATCHCRITERIA_REPLIED
:
790 return MSG_IS_REPLIED(info
->flags
);
791 case MATCHCRITERIA_NOT_REPLIED
:
792 return !MSG_IS_REPLIED(info
->flags
);
793 case MATCHCRITERIA_FORWARDED
:
794 return MSG_IS_FORWARDED(info
->flags
);
795 case MATCHCRITERIA_NOT_FORWARDED
:
796 return !MSG_IS_FORWARDED(info
->flags
);
797 case MATCHCRITERIA_LOCKED
:
798 return MSG_IS_LOCKED(info
->flags
);
799 case MATCHCRITERIA_NOT_LOCKED
:
800 return !MSG_IS_LOCKED(info
->flags
);
801 case MATCHCRITERIA_SPAM
:
802 return MSG_IS_SPAM(info
->flags
);
803 case MATCHCRITERIA_NOT_SPAM
:
804 return !MSG_IS_SPAM(info
->flags
);
805 case MATCHCRITERIA_HAS_ATTACHMENT
:
806 return MSG_IS_WITH_ATTACHMENT(info
->flags
);
807 case MATCHCRITERIA_HAS_NO_ATTACHMENT
:
808 return !MSG_IS_WITH_ATTACHMENT(info
->flags
);
809 case MATCHCRITERIA_SIGNED
:
810 return MSG_IS_SIGNED(info
->flags
);
811 case MATCHCRITERIA_NOT_SIGNED
:
812 return !MSG_IS_SIGNED(info
->flags
);
813 case MATCHCRITERIA_COLORLABEL
:
815 gint color
= MSG_GET_COLORLABEL_VALUE(info
->flags
);
816 gboolean ret
= (color
== prop
->value
);
819 if (debug_filtering_session
820 && prefs_common
.filtering_debug_level
>= FILTERING_DEBUG_LEVEL_HIGH
) {
822 log_print(LOG_DEBUG_FILTERING
,
823 "message color value [ %d ] matches color value [ %d ]\n",
826 log_print(LOG_DEBUG_FILTERING
,
827 "message color value [ %d ] does NOT match color value [ %d ]\n",
833 case MATCHCRITERIA_NOT_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_IGNORE_THREAD
:
854 return MSG_IS_IGNORE_THREAD(info
->flags
);
855 case MATCHCRITERIA_NOT_IGNORE_THREAD
:
856 return !MSG_IS_IGNORE_THREAD(info
->flags
);
857 case MATCHCRITERIA_WATCH_THREAD
:
858 return MSG_IS_WATCH_THREAD(info
->flags
);
859 case MATCHCRITERIA_NOT_WATCH_THREAD
:
860 return !MSG_IS_WATCH_THREAD(info
->flags
);
861 case MATCHCRITERIA_SUBJECT
:
862 return matcherprop_string_match(prop
, info
->subject
, context_str
[CONTEXT_SUBJECT
]);
863 case MATCHCRITERIA_NOT_SUBJECT
:
864 return !matcherprop_string_match(prop
, info
->subject
, context_str
[CONTEXT_SUBJECT
]);
865 case MATCHCRITERIA_FROM
:
866 return matcherprop_string_match(prop
, info
->from
, context_str
[CONTEXT_FROM
]);
867 case MATCHCRITERIA_NOT_FROM
:
868 return !matcherprop_string_match(prop
, info
->from
, context_str
[CONTEXT_FROM
]);
869 case MATCHCRITERIA_TO
:
870 return matcherprop_string_match(prop
, info
->to
, context_str
[CONTEXT_TO
]);
871 case MATCHCRITERIA_NOT_TO
:
872 return !matcherprop_string_match(prop
, info
->to
, context_str
[CONTEXT_TO
]);
873 case MATCHCRITERIA_CC
:
874 return matcherprop_string_match(prop
, info
->cc
, context_str
[CONTEXT_CC
]);
875 case MATCHCRITERIA_NOT_CC
:
876 return !matcherprop_string_match(prop
, info
->cc
, context_str
[CONTEXT_CC
]);
877 case MATCHCRITERIA_TO_OR_CC
:
878 return matcherprop_string_match(prop
, info
->to
, context_str
[CONTEXT_TO
])
879 || matcherprop_string_match(prop
, info
->cc
, context_str
[CONTEXT_CC
]);
880 case MATCHCRITERIA_NOT_TO_AND_NOT_CC
:
881 return !matcherprop_string_match(prop
, info
->to
, context_str
[CONTEXT_TO
])
882 && !matcherprop_string_match(prop
, info
->cc
, context_str
[CONTEXT_CC
]);
883 case MATCHCRITERIA_TAG
:
884 return matcherprop_tag_match(prop
, info
, context_str
[CONTEXT_TAG
]);
885 case MATCHCRITERIA_NOT_TAG
:
886 return !matcherprop_tag_match(prop
, info
, context_str
[CONTEXT_TAG
]);
887 case MATCHCRITERIA_TAGGED
:
888 return info
->tags
!= NULL
;
889 case MATCHCRITERIA_NOT_TAGGED
:
890 return info
->tags
== NULL
;
891 case MATCHCRITERIA_AGE_GREATER
:
893 /* Fallthrough intended */
894 case MATCHCRITERIA_AGE_GREATER_HOURS
:
900 age
= ((t
- info
->date_t
) / (60 * 60 * age_mult_hours
));
901 ret
= (age
>= prop
->value
);
904 if (debug_filtering_session
905 && prefs_common
.filtering_debug_level
>= FILTERING_DEBUG_LEVEL_HIGH
) {
907 log_print(LOG_DEBUG_FILTERING
,
908 "message age [ %d ] is greater than [ %d ]\n",
911 log_print(LOG_DEBUG_FILTERING
,
912 "message age [ %d ] is not greater than [ %d ]\n",
918 case MATCHCRITERIA_AGE_LOWER
:
920 /* Fallthrough intended */
921 case MATCHCRITERIA_AGE_LOWER_HOURS
:
927 age
= ((t
- info
->date_t
) / (60 * 60 * age_mult_hours
));
928 ret
= (age
< prop
->value
);
931 if (debug_filtering_session
932 && prefs_common
.filtering_debug_level
>= FILTERING_DEBUG_LEVEL_HIGH
) {
934 log_print(LOG_DEBUG_FILTERING
,
935 "message age [ %d ] is lower than [ %d ]\n",
938 log_print(LOG_DEBUG_FILTERING
,
939 "message age [ %d ] is not lower than [ %d ]\n",
945 case MATCHCRITERIA_SCORE_GREATER
:
947 gboolean ret
= (info
->score
> prop
->value
);
950 if (debug_filtering_session
951 && prefs_common
.filtering_debug_level
>= FILTERING_DEBUG_LEVEL_HIGH
) {
953 log_print(LOG_DEBUG_FILTERING
,
954 "message score [ %d ] is greater than [ %d ]\n",
955 info
->score
, prop
->value
);
957 log_print(LOG_DEBUG_FILTERING
,
958 "message score [ %d ] is not greater than [ %d ]\n",
959 info
->score
, prop
->value
);
964 case MATCHCRITERIA_SCORE_LOWER
:
966 gboolean ret
= (info
->score
< prop
->value
);
969 if (debug_filtering_session
970 && prefs_common
.filtering_debug_level
>= FILTERING_DEBUG_LEVEL_HIGH
) {
972 log_print(LOG_DEBUG_FILTERING
,
973 "message score [ %d ] is lower than [ %d ]\n",
974 info
->score
, prop
->value
);
976 log_print(LOG_DEBUG_FILTERING
,
977 "message score [ %d ] is not lower than [ %d ]\n",
978 info
->score
, prop
->value
);
983 case MATCHCRITERIA_SCORE_EQUAL
:
985 gboolean ret
= (info
->score
== prop
->value
);
988 if (debug_filtering_session
989 && prefs_common
.filtering_debug_level
>= FILTERING_DEBUG_LEVEL_HIGH
) {
991 log_print(LOG_DEBUG_FILTERING
,
992 "message score [ %d ] is equal to [ %d ]\n",
993 info
->score
, prop
->value
);
995 log_print(LOG_DEBUG_FILTERING
,
996 "message score [ %d ] is not equal to [ %d ]\n",
997 info
->score
, prop
->value
);
1002 case MATCHCRITERIA_SIZE_GREATER
:
1004 /* FIXME: info->size is a goffset */
1005 gboolean ret
= (info
->size
> (goffset
) 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 size is greater than [ %d ]\n",
1015 log_print(LOG_DEBUG_FILTERING
,
1016 "message size is not greater than [ %d ]\n",
1022 case MATCHCRITERIA_SIZE_SMALLER
:
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 smaller than [ %d ]\n",
1035 log_print(LOG_DEBUG_FILTERING
,
1036 "message size is not smaller than [ %d ]\n",
1042 case MATCHCRITERIA_SIZE_EQUAL
:
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 equal to [ %d ]\n",
1055 log_print(LOG_DEBUG_FILTERING
,
1056 "message size is not equal to [ %d ]\n",
1062 case MATCHCRITERIA_PARTIAL
:
1064 /* FIXME: info->size is a goffset */
1065 gboolean ret
= (info
->total_size
!= 0 && info
->size
!= (goffset
)info
->total_size
);
1068 if (debug_filtering_session
1069 && prefs_common
.filtering_debug_level
>= FILTERING_DEBUG_LEVEL_HIGH
) {
1071 log_print(LOG_DEBUG_FILTERING
,
1072 "message is partially downloaded, size is less than total size [ %d ])\n",
1075 log_print(LOG_DEBUG_FILTERING
,
1076 "message is not partially downloaded\n");
1081 case MATCHCRITERIA_NOT_PARTIAL
:
1083 /* FIXME: info->size is a goffset */
1084 gboolean ret
= (info
->total_size
== 0 || info
->size
== (goffset
)info
->total_size
);
1087 if (debug_filtering_session
1088 && prefs_common
.filtering_debug_level
>= FILTERING_DEBUG_LEVEL_HIGH
) {
1090 log_print(LOG_DEBUG_FILTERING
,
1091 "message is not partially downloaded\n");
1093 log_print(LOG_DEBUG_FILTERING
,
1094 "message is partially downloaded, size is less than total size [ %d ])\n",
1100 case MATCHCRITERIA_NEWSGROUPS
:
1101 return matcherprop_string_match(prop
, info
->newsgroups
, context_str
[CONTEXT_NEWSGROUPS
]);
1102 case MATCHCRITERIA_NOT_NEWSGROUPS
:
1103 return !matcherprop_string_match(prop
, info
->newsgroups
, context_str
[CONTEXT_NEWSGROUPS
]);
1104 case MATCHCRITERIA_INREPLYTO
:
1105 return matcherprop_string_match(prop
, info
->inreplyto
, context_str
[CONTEXT_IN_REPLY_TO
]);
1106 case MATCHCRITERIA_NOT_INREPLYTO
:
1107 return !matcherprop_string_match(prop
, info
->inreplyto
, context_str
[CONTEXT_IN_REPLY_TO
]);
1108 case MATCHCRITERIA_REFERENCES
:
1109 return matcherprop_list_match(prop
, info
->references
, context_str
[CONTEXT_REFERENCES
]);
1110 case MATCHCRITERIA_NOT_REFERENCES
:
1111 return !matcherprop_list_match(prop
, info
->references
, context_str
[CONTEXT_REFERENCES
]);
1112 case MATCHCRITERIA_TEST
:
1113 return matcherprop_match_test(prop
, info
);
1114 case MATCHCRITERIA_NOT_TEST
:
1115 return !matcherprop_match_test(prop
, info
);
1121 /* ********************* MatcherList *************************** */
1124 *\brief Create a new list of matchers
1126 *\param matchers List of matcher structures
1127 *\param bool_and Operator
1129 *\return MatcherList * New list
1131 MatcherList
*matcherlist_new(GSList
*matchers
, gboolean bool_and
)
1135 cond
= g_new0(MatcherList
, 1);
1137 cond
->matchers
= matchers
;
1138 cond
->bool_and
= bool_and
;
1145 *\brief Builds a single regular expresion from an array of srings.
1147 *\param strings The lines containing the different sub-regexp.
1149 *\return The newly allocated regexp string.
1151 static gchar
*build_complete_regexp(gchar
**strings
)
1155 while (strings
&& strings
[i
] && *strings
[i
]) {
1156 int old_len
= expr
? strlen(expr
):0;
1158 gchar
*tmpstr
= NULL
;
1160 if (g_utf8_validate(strings
[i
], -1, NULL
))
1161 tmpstr
= g_strdup(strings
[i
]);
1163 tmpstr
= conv_codeset_strdup(strings
[i
],
1164 conv_get_locale_charset_str_no_utf8(),
1167 if (strstr(tmpstr
, "\n"))
1168 *(strstr(tmpstr
, "\n")) = '\0';
1170 new_len
= strlen(tmpstr
);
1172 expr
= g_realloc(expr
,
1173 expr
? (old_len
+ strlen("|()") + new_len
+ 1)
1174 : (strlen("()") + new_len
+ 1));
1177 strcpy(expr
+ old_len
, "|(");
1178 strcpy(expr
+ old_len
+ 2, tmpstr
);
1179 strcpy(expr
+ old_len
+ 2 + new_len
, ")");
1181 strcpy(expr
+old_len
, "(");
1182 strcpy(expr
+old_len
+ 1, tmpstr
);
1183 strcpy(expr
+old_len
+ 1 + new_len
, ")");
1193 *\brief Create a new list of matchers from a multi-line string
1195 *\param lines String with "\n"-separated expressions
1196 *\param bool_and Operator
1197 *\param case_sensitive If the matching is case sensitive or not
1199 *\return MatcherList * New matcher list
1201 MatcherList
*matcherlist_new_from_lines(gchar
*lines
, gboolean bool_and
,
1202 gboolean case_sensitive
)
1204 MatcherProp
*m
= NULL
;
1205 GSList
*matchers
= NULL
;
1206 gchar
**strings
= g_strsplit(lines
, "\n", -1);
1210 expr
= build_complete_regexp(strings
);
1211 debug_print("building matcherprop for expr '%s'\n", expr
?expr
:"NULL");
1213 m
= matcherprop_new(MATCHCRITERIA_SUBJECT
, NULL
,
1214 case_sensitive
? MATCHTYPE_REGEXP
: MATCHTYPE_REGEXPCASE
,
1217 /* print error message */
1218 debug_print("failed to allocate memory for matcherprop\n");
1220 matchers
= g_slist_append(matchers
, m
);
1226 while (strings
&& strings
[i
] && *strings
[i
]) {
1227 m
= matcherprop_new(MATCHCRITERIA_SUBJECT
, NULL
,
1228 case_sensitive
? MATCHTYPE_MATCH
: MATCHTYPE_MATCHCASE
,
1231 /* print error message */
1232 debug_print("failed to allocate memory for matcherprop\n");
1234 matchers
= g_slist_append(matchers
, m
);
1239 g_strfreev(strings
);
1241 return matcherlist_new(matchers
, bool_and
);
1245 *\brief Frees a list of matchers
1247 *\param cond List of matchers
1249 void matcherlist_free(MatcherList
*cond
)
1253 cm_return_if_fail(cond
);
1254 for (l
= cond
->matchers
; l
!= NULL
; l
= g_slist_next(l
)) {
1255 matcherprop_free((MatcherProp
*) l
->data
);
1257 g_slist_free(cond
->matchers
);
1262 *\brief Skip all headers in a message file
1264 *\param fp Message file
1266 static void matcherlist_skip_headers(FILE *fp
)
1268 gchar buf
[BUFFSIZE
];
1270 while (procheader_get_one_field(buf
, sizeof(buf
), fp
, NULL
) != -1)
1275 *\brief Check if a header matches a matcher condition
1277 *\param matcher Matcher structure to check header for
1278 *\param buf Header name
1280 *\return boolean TRUE if matching header
1282 static gboolean
matcherprop_match_one_header(MatcherProp
*matcher
,
1285 gboolean result
= FALSE
;
1286 Header
*header
= NULL
;
1288 switch (matcher
->criteria
) {
1289 case MATCHCRITERIA_HEADER
:
1290 case MATCHCRITERIA_NOT_HEADER
:
1291 header
= procheader_parse_header(buf
);
1294 if (procheader_headername_equal(header
->name
,
1296 if (matcher
->criteria
== MATCHCRITERIA_HEADER
)
1297 result
= matcherprop_string_match(matcher
, header
->body
, context_str
[CONTEXT_HEADER
]);
1299 result
= !matcherprop_string_match(matcher
, header
->body
, context_str
[CONTEXT_HEADER
]);
1300 procheader_header_free(header
);
1304 procheader_header_free(header
);
1307 case MATCHCRITERIA_HEADERS_PART
:
1308 case MATCHCRITERIA_MESSAGE
:
1309 header
= procheader_parse_header(buf
);
1312 result
= matcherprop_header_line_match(matcher
,
1313 header
->name
, header
->body
, context_str
[CONTEXT_HEADER_LINE
]);
1314 procheader_header_free(header
);
1316 case MATCHCRITERIA_NOT_HEADERS_PART
:
1317 case MATCHCRITERIA_NOT_MESSAGE
:
1318 header
= procheader_parse_header(buf
);
1321 result
= !matcherprop_header_line_match(matcher
,
1322 header
->name
, header
->body
, context_str
[CONTEXT_HEADER_LINE
]);
1323 procheader_header_free(header
);
1325 case MATCHCRITERIA_FOUND_IN_ADDRESSBOOK
:
1326 case MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK
:
1328 GSList
*address_list
= NULL
;
1329 gint match
= MATCH_ONE
;
1330 gboolean found
= FALSE
;
1332 /* how many address headers are me trying to mach? */
1333 if (strcasecmp(matcher
->header
, "Any") == 0)
1335 else if (strcasecmp(matcher
->header
, "All") == 0)
1338 if (match
== MATCH_ONE
) {
1339 /* matching one address header exactly, is that the right one? */
1340 header
= procheader_parse_header(buf
);
1342 !procheader_headername_equal(header
->name
, matcher
->header
))
1344 address_list
= address_list_append(address_list
, header
->body
);
1345 if (address_list
== NULL
)
1349 header
= procheader_parse_header(buf
);
1352 /* address header is one of the headers we have to match when checking
1353 for any address header or all address headers? */
1354 if (procheader_headername_equal(header
->name
, "From") ||
1355 procheader_headername_equal(header
->name
, "To") ||
1356 procheader_headername_equal(header
->name
, "Cc") ||
1357 procheader_headername_equal(header
->name
, "Reply-To") ||
1358 procheader_headername_equal(header
->name
, "Sender"))
1359 address_list
= address_list_append(address_list
, header
->body
);
1360 if (address_list
== NULL
)
1364 found
= match_with_addresses_in_addressbook
1365 (matcher
, address_list
, matcher
->criteria
,
1366 matcher
->expr
, match
);
1367 g_slist_free(address_list
);
1369 if (matcher
->criteria
== MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK
)
1380 *\brief Check if the matcher structure wants headers to
1383 *\param matcher Matcher structure
1385 *\return gboolean TRUE if the matcher structure describes
1386 * a header match condition
1388 static gboolean
matcherprop_criteria_headers(const MatcherProp
*matcher
)
1390 switch (matcher
->criteria
) {
1391 case MATCHCRITERIA_HEADER
:
1392 case MATCHCRITERIA_NOT_HEADER
:
1393 case MATCHCRITERIA_HEADERS_PART
:
1394 case MATCHCRITERIA_NOT_HEADERS_PART
:
1395 case MATCHCRITERIA_FOUND_IN_ADDRESSBOOK
:
1396 case MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK
:
1404 *\brief Check if the matcher structure wants the message
1405 * to be matched (just perform an action on any
1408 *\param matcher Matcher structure
1410 *\return gboolean TRUE if matcher condition should match
1413 static gboolean
matcherprop_criteria_message(MatcherProp
*matcher
)
1415 switch (matcher
->criteria
) {
1416 case MATCHCRITERIA_MESSAGE
:
1417 case MATCHCRITERIA_NOT_MESSAGE
:
1425 *\brief Check if a list of conditions matches one header in
1428 *\param matchers List of conditions
1429 *\param fp Message file
1431 *\return gboolean TRUE if one of the headers is matched by
1432 * the list of conditions.
1434 static gboolean
matcherlist_match_headers(MatcherList
*matchers
, FILE *fp
)
1437 gchar buf
[BUFFSIZE
];
1439 while (procheader_get_one_field(buf
, sizeof(buf
), fp
, NULL
) != -1) {
1440 for (l
= matchers
->matchers
; l
!= NULL
; l
= g_slist_next(l
)) {
1441 MatcherProp
*matcher
= (MatcherProp
*) l
->data
;
1442 gint match
= MATCH_ANY
;
1447 /* determine the match range (all, any are our concern here) */
1448 if (matcher
->criteria
== MATCHCRITERIA_NOT_HEADERS_PART
||
1449 matcher
->criteria
== MATCHCRITERIA_NOT_MESSAGE
) {
1452 } else if (matcher
->criteria
== MATCHCRITERIA_FOUND_IN_ADDRESSBOOK
||
1453 matcher
->criteria
== MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK
) {
1454 Header
*header
= NULL
;
1456 /* address header is one of the headers we have to match when checking
1457 for any address header or all address headers? */
1458 header
= procheader_parse_header(buf
);
1460 (procheader_headername_equal(header
->name
, "From") ||
1461 procheader_headername_equal(header
->name
, "To") ||
1462 procheader_headername_equal(header
->name
, "Cc") ||
1463 procheader_headername_equal(header
->name
, "Reply-To") ||
1464 procheader_headername_equal(header
->name
, "Sender"))) {
1466 if (strcasecmp(matcher
->header
, "Any") == 0)
1468 else if (strcasecmp(matcher
->header
, "All") == 0)
1473 /* further call to matcherprop_match_one_header() can't match
1474 and it irrelevant, so: don't alter the match result */
1479 /* ZERO line must NOT match for the rule to match.
1481 if (match
== MATCH_ALL
) {
1482 if (matcherprop_match_one_header(matcher
, buf
)) {
1483 matcher
->result
= TRUE
;
1485 matcher
->result
= FALSE
;
1486 matcher
->done
= TRUE
;
1488 /* else, just one line matching is enough for the rule to match
1490 } else if (matcherprop_criteria_headers(matcher
) ||
1491 matcherprop_criteria_message(matcher
)) {
1492 if (matcherprop_match_one_header(matcher
, buf
)) {
1493 matcher
->result
= TRUE
;
1494 matcher
->done
= TRUE
;
1498 /* if the rule matched and the matchers are OR, no need to
1499 * check the others */
1500 if (matcher
->result
&& matcher
->done
) {
1501 if (!matchers
->bool_and
)
1511 *\brief Check if a matcher wants to check the message body
1513 *\param matcher Matcher structure
1515 *\return gboolean TRUE if body must be matched.
1517 static gboolean
matcherprop_criteria_body(const MatcherProp
*matcher
)
1519 switch (matcher
->criteria
) {
1520 case MATCHCRITERIA_BODY_PART
:
1521 case MATCHCRITERIA_NOT_BODY_PART
:
1528 static gboolean
matcherlist_match_binary_content(MatcherList
*matchers
, MimeInfo
*partinfo
)
1531 gchar buf
[BUFFSIZE
];
1534 if (!partinfo
|| partinfo
->type
== MIMETYPE_TEXT
)
1537 outfp
= procmime_get_binary_content(partinfo
);
1542 while (fgets(buf
, sizeof(buf
), outfp
) != NULL
) {
1545 for (l
= matchers
->matchers
; l
!= NULL
; l
= g_slist_next(l
)) {
1546 MatcherProp
*matcher
= (MatcherProp
*) l
->data
;
1551 /* Don't scan non-text parts when looking in body, only
1552 * when looking in whole message
1554 if (matcher
->criteria
== MATCHCRITERIA_NOT_BODY_PART
||
1555 matcher
->criteria
== MATCHCRITERIA_BODY_PART
)
1558 /* if the criteria is ~body_part or ~message, ZERO lines
1559 * must match for the rule to match.
1561 if (matcher
->criteria
== MATCHCRITERIA_NOT_BODY_PART
||
1562 matcher
->criteria
== MATCHCRITERIA_NOT_MESSAGE
) {
1563 if (matcherprop_string_match(matcher
, buf
,
1564 context_str
[CONTEXT_BODY_LINE
])) {
1565 matcher
->result
= FALSE
;
1566 matcher
->done
= TRUE
;
1568 matcher
->result
= TRUE
;
1569 /* else, just one line has to match */
1570 } else if (matcherprop_criteria_body(matcher
) ||
1571 matcherprop_criteria_message(matcher
)) {
1572 if (matcherprop_string_match(matcher
, buf
,
1573 context_str
[CONTEXT_BODY_LINE
])) {
1574 matcher
->result
= TRUE
;
1575 matcher
->done
= TRUE
;
1579 /* if the matchers are OR'ed and the rule matched,
1580 * no need to check the others. */
1581 if (matcher
->result
&& matcher
->done
) {
1582 if (!matchers
->bool_and
) {
1594 static gboolean
match_content_cb(const gchar
*buf
, gpointer data
)
1596 MatcherList
*matchers
= (MatcherList
*)data
;
1597 gboolean all_done
= TRUE
;
1600 for (l
= matchers
->matchers
; l
!= NULL
; l
= g_slist_next(l
)) {
1601 MatcherProp
*matcher
= (MatcherProp
*) l
->data
;
1606 /* if the criteria is ~body_part or ~message, ZERO lines
1607 * must match for the rule to match.
1609 if (matcher
->criteria
== MATCHCRITERIA_NOT_BODY_PART
||
1610 matcher
->criteria
== MATCHCRITERIA_NOT_MESSAGE
) {
1611 if (matcherprop_string_match(matcher
, buf
,
1612 context_str
[CONTEXT_BODY_LINE
])) {
1613 matcher
->result
= FALSE
;
1614 matcher
->done
= TRUE
;
1616 matcher
->result
= TRUE
;
1617 /* else, just one line has to match */
1618 } else if (matcherprop_criteria_body(matcher
) ||
1619 matcherprop_criteria_message(matcher
)) {
1620 if (matcherprop_string_match(matcher
, buf
,
1621 context_str
[CONTEXT_BODY_LINE
])) {
1622 matcher
->result
= TRUE
;
1623 matcher
->done
= TRUE
;
1627 /* if the matchers are OR'ed and the rule matched,
1628 * no need to check the others. */
1629 if (matcher
->result
&& matcher
->done
) {
1630 if (!matchers
->bool_and
) {
1641 static gboolean
matcherlist_match_text_content(MatcherList
*matchers
, MimeInfo
*partinfo
)
1643 if (partinfo
->type
!= MIMETYPE_TEXT
)
1646 return procmime_scan_text_content(partinfo
, match_content_cb
, matchers
);
1650 *\brief Check if a line in a message file's body matches
1653 *\param matchers List of conditions
1654 *\param fp Message file
1656 *\return gboolean TRUE if succesful match
1658 static gboolean
matcherlist_match_body(MatcherList
*matchers
, gboolean body_only
, MsgInfo
*info
)
1660 MimeInfo
*mimeinfo
= NULL
;
1661 MimeInfo
*partinfo
= NULL
;
1662 gboolean first_text_found
= FALSE
;
1664 cm_return_val_if_fail(info
!= NULL
, FALSE
);
1666 mimeinfo
= procmime_scan_message(info
);
1669 partinfo
= procmime_mimeinfo_next(mimeinfo
);
1671 for (; partinfo
!= NULL
; partinfo
= procmime_mimeinfo_next(partinfo
)) {
1673 if (partinfo
->type
!= MIMETYPE_TEXT
&& body_only
)
1676 if (partinfo
->type
== MIMETYPE_TEXT
) {
1677 first_text_found
= TRUE
;
1678 if (matcherlist_match_text_content(matchers
, partinfo
)) {
1679 procmime_mimeinfo_free_all(mimeinfo
);
1682 } else if (matcherlist_match_binary_content(matchers
, partinfo
)) {
1683 procmime_mimeinfo_free_all(mimeinfo
);
1687 if (body_only
&& first_text_found
)
1690 procmime_mimeinfo_free_all(mimeinfo
);
1696 *\brief Check if a message file matches criteria
1698 *\param matchers Criteria
1699 *\param info Message info
1700 *\param result Default result
1702 *\return gboolean TRUE if matched
1704 static gboolean
matcherlist_match_file(MatcherList
*matchers
, MsgInfo
*info
,
1707 gboolean read_headers
;
1714 /* file need to be read ? */
1716 read_headers
= FALSE
;
1719 for (l
= matchers
->matchers
; l
!= NULL
; l
= g_slist_next(l
)) {
1720 MatcherProp
*matcher
= (MatcherProp
*) l
->data
;
1722 if (matcherprop_criteria_headers(matcher
))
1723 read_headers
= TRUE
;
1724 if (matcherprop_criteria_body(matcher
))
1726 if (matcherprop_criteria_message(matcher
)) {
1727 read_headers
= TRUE
;
1731 matcher
->result
= FALSE
;
1732 matcher
->done
= FALSE
;
1735 if (!read_headers
&& !read_body
)
1738 file
= procmsg_get_message_file_full(info
, read_headers
, read_body
);
1742 if ((fp
= g_fopen(file
, "rb")) == NULL
) {
1743 FILE_OP_ERROR(file
, "fopen");
1748 /* read the headers */
1751 if (matcherlist_match_headers(matchers
, fp
))
1754 matcherlist_skip_headers(fp
);
1759 matcherlist_match_body(matchers
, body_only
, info
);
1762 for (l
= matchers
->matchers
; l
!= NULL
; l
= g_slist_next(l
)) {
1763 MatcherProp
*matcher
= (MatcherProp
*) l
->data
;
1765 if (matcherprop_criteria_headers(matcher
) ||
1766 matcherprop_criteria_body(matcher
) ||
1767 matcherprop_criteria_message(matcher
)) {
1768 if (matcher
->result
) {
1769 if (!matchers
->bool_and
) {
1775 if (matchers
->bool_and
) {
1791 *\brief Test list of conditions on a message.
1793 *\param matchers List of conditions
1794 *\param info Message info
1796 *\return gboolean TRUE if matched
1798 gboolean
matcherlist_match(MatcherList
*matchers
, MsgInfo
*info
)
1806 if (matchers
->bool_and
)
1811 /* test the cached elements */
1813 for (l
= matchers
->matchers
; l
!= NULL
;l
= g_slist_next(l
)) {
1814 MatcherProp
*matcher
= (MatcherProp
*) l
->data
;
1816 if (debug_filtering_session
) {
1817 gchar
*buf
= matcherprop_to_string(matcher
);
1818 log_print(LOG_DEBUG_FILTERING
, _("checking if message matches [ %s ]\n"), buf
);
1822 switch(matcher
->criteria
) {
1823 case MATCHCRITERIA_ALL
:
1824 case MATCHCRITERIA_UNREAD
:
1825 case MATCHCRITERIA_NOT_UNREAD
:
1826 case MATCHCRITERIA_NEW
:
1827 case MATCHCRITERIA_NOT_NEW
:
1828 case MATCHCRITERIA_MARKED
:
1829 case MATCHCRITERIA_NOT_MARKED
:
1830 case MATCHCRITERIA_DELETED
:
1831 case MATCHCRITERIA_NOT_DELETED
:
1832 case MATCHCRITERIA_REPLIED
:
1833 case MATCHCRITERIA_NOT_REPLIED
:
1834 case MATCHCRITERIA_FORWARDED
:
1835 case MATCHCRITERIA_NOT_FORWARDED
:
1836 case MATCHCRITERIA_LOCKED
:
1837 case MATCHCRITERIA_NOT_LOCKED
:
1838 case MATCHCRITERIA_SPAM
:
1839 case MATCHCRITERIA_NOT_SPAM
:
1840 case MATCHCRITERIA_HAS_ATTACHMENT
:
1841 case MATCHCRITERIA_HAS_NO_ATTACHMENT
:
1842 case MATCHCRITERIA_SIGNED
:
1843 case MATCHCRITERIA_NOT_SIGNED
:
1844 case MATCHCRITERIA_COLORLABEL
:
1845 case MATCHCRITERIA_NOT_COLORLABEL
:
1846 case MATCHCRITERIA_IGNORE_THREAD
:
1847 case MATCHCRITERIA_NOT_IGNORE_THREAD
:
1848 case MATCHCRITERIA_WATCH_THREAD
:
1849 case MATCHCRITERIA_NOT_WATCH_THREAD
:
1850 case MATCHCRITERIA_SUBJECT
:
1851 case MATCHCRITERIA_NOT_SUBJECT
:
1852 case MATCHCRITERIA_FROM
:
1853 case MATCHCRITERIA_NOT_FROM
:
1854 case MATCHCRITERIA_TO
:
1855 case MATCHCRITERIA_NOT_TO
:
1856 case MATCHCRITERIA_CC
:
1857 case MATCHCRITERIA_NOT_CC
:
1858 case MATCHCRITERIA_TO_OR_CC
:
1859 case MATCHCRITERIA_NOT_TO_AND_NOT_CC
:
1860 case MATCHCRITERIA_TAG
:
1861 case MATCHCRITERIA_NOT_TAG
:
1862 case MATCHCRITERIA_TAGGED
:
1863 case MATCHCRITERIA_NOT_TAGGED
:
1864 case MATCHCRITERIA_AGE_GREATER
:
1865 case MATCHCRITERIA_AGE_LOWER
:
1866 case MATCHCRITERIA_AGE_GREATER_HOURS
:
1867 case MATCHCRITERIA_AGE_LOWER_HOURS
:
1868 case MATCHCRITERIA_NEWSGROUPS
:
1869 case MATCHCRITERIA_NOT_NEWSGROUPS
:
1870 case MATCHCRITERIA_INREPLYTO
:
1871 case MATCHCRITERIA_NOT_INREPLYTO
:
1872 case MATCHCRITERIA_REFERENCES
:
1873 case MATCHCRITERIA_NOT_REFERENCES
:
1874 case MATCHCRITERIA_SCORE_GREATER
:
1875 case MATCHCRITERIA_SCORE_LOWER
:
1876 case MATCHCRITERIA_SCORE_EQUAL
:
1877 case MATCHCRITERIA_SIZE_GREATER
:
1878 case MATCHCRITERIA_SIZE_SMALLER
:
1879 case MATCHCRITERIA_SIZE_EQUAL
:
1880 case MATCHCRITERIA_TEST
:
1881 case MATCHCRITERIA_NOT_TEST
:
1882 case MATCHCRITERIA_PARTIAL
:
1883 case MATCHCRITERIA_NOT_PARTIAL
:
1884 if (matcherprop_match(matcher
, info
)) {
1885 if (!matchers
->bool_and
) {
1886 if (debug_filtering_session
)
1887 log_status_ok(LOG_DEBUG_FILTERING
, _("message matches\n"));
1892 if (matchers
->bool_and
) {
1893 if (debug_filtering_session
)
1894 log_status_nok(LOG_DEBUG_FILTERING
, _("message does not match\n"));
1901 /* test the condition on the file */
1903 if (matcherlist_match_file(matchers
, info
, result
)) {
1904 if (!matchers
->bool_and
) {
1905 if (debug_filtering_session
)
1906 log_status_ok(LOG_DEBUG_FILTERING
, _("message matches\n"));
1910 if (matchers
->bool_and
) {
1911 if (debug_filtering_session
)
1912 log_status_nok(LOG_DEBUG_FILTERING
, _("message does not match\n"));
1917 if (debug_filtering_session
) {
1919 log_status_ok(LOG_DEBUG_FILTERING
, _("message matches\n"));
1921 log_status_nok(LOG_DEBUG_FILTERING
, _("message does not match\n"));
1927 static gint
quote_filter_str(gchar
* result
, guint size
,
1937 for(p
= path
; * p
!= '\0' ; p
++) {
1939 if ((* p
!= '\"') && (* p
!= '\\')) {
1940 if (remaining
> 0) {
1946 result
[size
- 1] = '\0';
1951 if (remaining
>= 2) {
1959 result
[size
- 1] = '\0';
1964 if (remaining
> 0) {
1968 result
[size
- 1] = '\0';
1976 gchar
* matcher_quote_str(const gchar
* src
)
1981 len
= strlen(src
) * 2 + 1;
1982 res
= g_malloc(len
);
1983 quote_filter_str(res
, len
, src
);
1989 *\brief Convert a matcher structure to a string
1991 *\param matcher Matcher structure
1993 *\return gchar * Newly allocated string
1995 gchar
*matcherprop_to_string(MatcherProp
*matcher
)
1997 gchar
*matcher_str
= NULL
;
1998 const gchar
*criteria_str
;
1999 const gchar
*matchtype_str
;
2001 gchar
* quoted_expr
;
2002 gchar
* quoted_header
;
2004 criteria_str
= NULL
;
2005 for (i
= 0; i
< (int) (sizeof(matchparser_tab
) / sizeof(MatchParser
)); i
++) {
2006 if (matchparser_tab
[i
].id
== matcher
->criteria
)
2007 criteria_str
= matchparser_tab
[i
].str
;
2009 if (criteria_str
== NULL
)
2012 switch (matcher
->criteria
) {
2013 case MATCHCRITERIA_AGE_GREATER
:
2014 case MATCHCRITERIA_AGE_LOWER
:
2015 case MATCHCRITERIA_AGE_GREATER_HOURS
:
2016 case MATCHCRITERIA_AGE_LOWER_HOURS
:
2017 case MATCHCRITERIA_SCORE_GREATER
:
2018 case MATCHCRITERIA_SCORE_LOWER
:
2019 case MATCHCRITERIA_SCORE_EQUAL
:
2020 case MATCHCRITERIA_SIZE_GREATER
:
2021 case MATCHCRITERIA_SIZE_SMALLER
:
2022 case MATCHCRITERIA_SIZE_EQUAL
:
2023 case MATCHCRITERIA_COLORLABEL
:
2024 case MATCHCRITERIA_NOT_COLORLABEL
:
2025 return g_strdup_printf("%s %i", criteria_str
, matcher
->value
);
2026 case MATCHCRITERIA_ALL
:
2027 case MATCHCRITERIA_UNREAD
:
2028 case MATCHCRITERIA_NOT_UNREAD
:
2029 case MATCHCRITERIA_NEW
:
2030 case MATCHCRITERIA_NOT_NEW
:
2031 case MATCHCRITERIA_MARKED
:
2032 case MATCHCRITERIA_NOT_MARKED
:
2033 case MATCHCRITERIA_DELETED
:
2034 case MATCHCRITERIA_NOT_DELETED
:
2035 case MATCHCRITERIA_REPLIED
:
2036 case MATCHCRITERIA_NOT_REPLIED
:
2037 case MATCHCRITERIA_FORWARDED
:
2038 case MATCHCRITERIA_NOT_FORWARDED
:
2039 case MATCHCRITERIA_LOCKED
:
2040 case MATCHCRITERIA_NOT_LOCKED
:
2041 case MATCHCRITERIA_SPAM
:
2042 case MATCHCRITERIA_NOT_SPAM
:
2043 case MATCHCRITERIA_HAS_ATTACHMENT
:
2044 case MATCHCRITERIA_HAS_NO_ATTACHMENT
:
2045 case MATCHCRITERIA_SIGNED
:
2046 case MATCHCRITERIA_NOT_SIGNED
:
2047 case MATCHCRITERIA_PARTIAL
:
2048 case MATCHCRITERIA_NOT_PARTIAL
:
2049 case MATCHCRITERIA_IGNORE_THREAD
:
2050 case MATCHCRITERIA_NOT_IGNORE_THREAD
:
2051 case MATCHCRITERIA_WATCH_THREAD
:
2052 case MATCHCRITERIA_NOT_WATCH_THREAD
:
2053 case MATCHCRITERIA_TAGGED
:
2054 case MATCHCRITERIA_NOT_TAGGED
:
2055 return g_strdup(criteria_str
);
2056 case MATCHCRITERIA_TEST
:
2057 case MATCHCRITERIA_NOT_TEST
:
2058 quoted_expr
= matcher_quote_str(matcher
->expr
);
2059 matcher_str
= g_strdup_printf("%s \"%s\"",
2060 criteria_str
, quoted_expr
);
2061 g_free(quoted_expr
);
2063 case MATCHCRITERIA_FOUND_IN_ADDRESSBOOK
:
2064 case MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK
:
2065 quoted_header
= matcher_quote_str(matcher
->header
);
2066 quoted_expr
= matcher_quote_str(matcher
->expr
);
2067 matcher_str
= g_strdup_printf("%s \"%s\" in \"%s\"",
2068 criteria_str
, quoted_header
, quoted_expr
);
2069 g_free(quoted_header
);
2070 g_free(quoted_expr
);
2074 matchtype_str
= NULL
;
2075 for (i
= 0; i
< sizeof matchparser_tab
/ sizeof matchparser_tab
[0]; i
++) {
2076 if (matchparser_tab
[i
].id
== matcher
->matchtype
)
2077 matchtype_str
= matchparser_tab
[i
].str
;
2080 if (matchtype_str
== NULL
)
2083 switch (matcher
->matchtype
) {
2084 case MATCHTYPE_MATCH
:
2085 case MATCHTYPE_MATCHCASE
:
2086 case MATCHTYPE_REGEXP
:
2087 case MATCHTYPE_REGEXPCASE
:
2088 quoted_expr
= matcher_quote_str(matcher
->expr
);
2089 if (matcher
->header
) {
2090 quoted_header
= matcher_quote_str(matcher
->header
);
2091 matcher_str
= g_strdup_printf
2092 ("%s \"%s\" %s \"%s\"",
2093 criteria_str
, quoted_header
,
2094 matchtype_str
, quoted_expr
);
2095 g_free(quoted_header
);
2098 matcher_str
= g_strdup_printf
2099 ("%s %s \"%s\"", criteria_str
,
2100 matchtype_str
, quoted_expr
);
2101 g_free(quoted_expr
);
2109 *\brief Convert a list of conditions to a string
2111 *\param matchers List of conditions
2113 *\return gchar * Newly allocated string
2115 gchar
*matcherlist_to_string(const MatcherList
*matchers
)
2121 gchar
*result
= NULL
;
2123 count
= g_slist_length(matchers
->matchers
);
2124 vstr
= g_new(gchar
*, count
+ 1);
2126 for (l
= matchers
->matchers
, cur_str
= vstr
; l
!= NULL
;
2127 l
= g_slist_next(l
), cur_str
++) {
2128 *cur_str
= matcherprop_to_string((MatcherProp
*) l
->data
);
2129 if (*cur_str
== NULL
)
2134 if (matchers
->bool_and
)
2135 result
= g_strjoinv(" & ", vstr
);
2137 result
= g_strjoinv(" | ", vstr
);
2139 for (cur_str
= vstr
; *cur_str
!= NULL
; cur_str
++)
2147 #define STRLEN_ZERO(s) ((s) ? strlen(s) : 0)
2148 #define STRLEN_DEFAULT(s,d) ((s) ? strlen(s) : STRLEN_ZERO(d))
2150 static void add_str_default(gchar
** dest
,
2151 const gchar
* s
, const gchar
* d
)
2153 gchar quoted_str
[4096];
2161 quote_cmd_argument(quoted_str
, sizeof(quoted_str
), str
);
2162 strcpy(* dest
, quoted_str
);
2164 (* dest
) += strlen(* dest
);
2167 /* matching_build_command() - preferably cmd should be unescaped */
2169 *\brief Build the command-line to execute
2171 *\param cmd String with command-line specifiers
2172 *\param info Message info to use for command
2174 *\return gchar * Newly allocated string
2176 gchar
*matching_build_command(const gchar
*cmd
, MsgInfo
*info
)
2178 const gchar
*s
= cmd
;
2179 gchar
*filename
= NULL
;
2180 gchar
*processed_cmd
;
2184 const gchar
*const no_subject
= _("(none)") ;
2185 const gchar
*const no_from
= _("(none)") ;
2186 const gchar
*const no_to
= _("(none)") ;
2187 const gchar
*const no_cc
= _("(none)") ;
2188 const gchar
*const no_date
= _("(none)") ;
2189 const gchar
*const no_msgid
= _("(none)") ;
2190 const gchar
*const no_newsgroups
= _("(none)") ;
2191 const gchar
*const no_references
= _("(none)") ;
2193 size
= STRLEN_ZERO(cmd
) + 1;
2194 while (*s
!= '\0') {
2201 case 's': /* subject */
2202 size
+= STRLEN_DEFAULT(info
->subject
, no_subject
) - 2;
2204 case 'f': /* from */
2205 size
+= STRLEN_DEFAULT(info
->from
, no_from
) - 2;
2208 size
+= STRLEN_DEFAULT(info
->to
, no_to
) - 2;
2211 size
+= STRLEN_DEFAULT(info
->cc
, no_cc
) - 2;
2213 case 'd': /* date */
2214 size
+= STRLEN_DEFAULT(info
->date
, no_date
) - 2;
2216 case 'i': /* message-id */
2217 size
+= STRLEN_DEFAULT(info
->msgid
, no_msgid
) - 2;
2219 case 'n': /* newsgroups */
2220 size
+= STRLEN_DEFAULT(info
->newsgroups
, no_newsgroups
) - 2;
2222 case 'r': /* references */
2223 /* FIXME: using the inreplyto header for reference */
2224 size
+= STRLEN_DEFAULT(info
->inreplyto
, no_references
) - 2;
2226 case 'F': /* file */
2227 if (filename
== NULL
)
2228 filename
= folder_item_fetch_msg(info
->folder
, info
->msgnum
);
2230 if (filename
== NULL
) {
2231 g_warning("filename is not set");
2235 size
+= strlen(filename
) - 2;
2244 /* as the string can be quoted, we double the result */
2247 processed_cmd
= g_new0(gchar
, size
);
2251 while (*s
!= '\0') {
2259 case 's': /* subject */
2260 add_str_default(&p
, info
->subject
,
2263 case 'f': /* from */
2264 add_str_default(&p
, info
->from
,
2268 add_str_default(&p
, info
->to
,
2272 add_str_default(&p
, info
->cc
,
2275 case 'd': /* date */
2276 add_str_default(&p
, info
->date
,
2279 case 'i': /* message-id */
2280 add_str_default(&p
, info
->msgid
,
2283 case 'n': /* newsgroups */
2284 add_str_default(&p
, info
->newsgroups
,
2287 case 'r': /* references */
2288 /* FIXME: using the inreplyto header for references */
2289 add_str_default(&p
, info
->inreplyto
, no_references
);
2291 case 'F': /* file */
2292 if (filename
!= NULL
)
2293 add_str_default(&p
, filename
, NULL
);
2312 return processed_cmd
;
2314 #undef STRLEN_DEFAULT
2317 /* ************************************************************ */
2321 *\brief Write filtering list to file
2324 *\param prefs_filtering List of filtering conditions
2326 static int prefs_filtering_write(FILE *fp
, GSList
*prefs_filtering
)
2330 for (cur
= prefs_filtering
; cur
!= NULL
; cur
= cur
->next
) {
2331 gchar
*filtering_str
= NULL
;
2332 gchar
*tmp_name
= NULL
;
2333 FilteringProp
*prop
= NULL
;
2335 if (NULL
== (prop
= (FilteringProp
*) cur
->data
))
2338 if (NULL
== (filtering_str
= filteringprop_to_string(prop
)))
2341 if (prop
->enabled
) {
2342 if (fputs("enabled ", fp
) == EOF
) {
2343 FILE_OP_ERROR("filtering config", "fputs");
2347 if (fputs("disabled ", fp
) == EOF
) {
2348 FILE_OP_ERROR("filtering config", "fputs");
2353 if (fputs("rulename \"", fp
) == EOF
) {
2354 FILE_OP_ERROR("filtering config", "fputs");
2355 g_free(filtering_str
);
2358 tmp_name
= prop
->name
;
2359 while (tmp_name
&& *tmp_name
!= '\0') {
2360 if (*tmp_name
!= '"') {
2361 if (fputc(*tmp_name
, fp
) == EOF
) {
2362 FILE_OP_ERROR("filtering config", "fputs || fputc");
2363 g_free(filtering_str
);
2366 } else if (*tmp_name
== '"') {
2367 if (fputc('\\', fp
) == EOF
||
2368 fputc('"', fp
) == EOF
) {
2369 FILE_OP_ERROR("filtering config", "fputs || fputc");
2370 g_free(filtering_str
);
2376 if (fputs("\" ", fp
) == EOF
) {
2377 FILE_OP_ERROR("filtering config", "fputs");
2378 g_free(filtering_str
);
2382 if (prop
->account_id
!= 0) {
2385 tmp
= g_strdup_printf("account %d ", prop
->account_id
);
2386 if (fputs(tmp
, fp
) == EOF
) {
2387 FILE_OP_ERROR("filtering config", "fputs");
2394 if(fputs(filtering_str
, fp
) == EOF
||
2395 fputc('\n', fp
) == EOF
) {
2396 FILE_OP_ERROR("filtering config", "fputs || fputc");
2397 g_free(filtering_str
);
2400 g_free(filtering_str
);
2406 typedef struct _NodeLoopData
{
2412 *\brief Write matchers from a folder item
2414 *\param node Node with folder info
2415 *\param data File pointer
2417 *\return gboolean FALSE
2419 static gboolean
prefs_matcher_write_func(GNode
*node
, gpointer d
)
2422 NodeLoopData
*data
= (NodeLoopData
*)d
;
2424 GSList
*prefs_filtering
;
2427 /* prevent warning */
2428 if (item
->path
== NULL
)
2430 id
= folder_item_get_identifier(item
);
2433 prefs_filtering
= item
->prefs
->processing
;
2435 if (prefs_filtering
!= NULL
) {
2436 if (fprintf(data
->fp
, "[%s]\n", id
) < 0) {
2440 if (prefs_filtering_write(data
->fp
, prefs_filtering
) < 0) {
2444 if (fputc('\n', data
->fp
) == EOF
) {
2456 *\brief Save matchers from folder items
2460 static int prefs_matcher_save(FILE *fp
)
2468 for (cur
= folder_get_list() ; cur
!= NULL
; cur
= g_list_next(cur
)) {
2471 folder
= (Folder
*) cur
->data
;
2472 g_node_traverse(folder
->node
, G_PRE_ORDER
, G_TRAVERSE_ALL
, -1,
2473 prefs_matcher_write_func
, &data
);
2476 if (data
.error
== TRUE
)
2479 /* pre global rules */
2480 if (fprintf(fp
, "[preglobal]\n") < 0 ||
2481 prefs_filtering_write(fp
, pre_global_processing
) < 0 ||
2482 fputc('\n', fp
) == EOF
)
2485 /* post global rules */
2486 if (fprintf(fp
, "[postglobal]\n") < 0 ||
2487 prefs_filtering_write(fp
, post_global_processing
) < 0 ||
2488 fputc('\n', fp
) == EOF
)
2491 /* filtering rules */
2492 if (fprintf(fp
, "[filtering]\n") < 0 ||
2493 prefs_filtering_write(fp
, filtering_rules
) < 0 ||
2494 fputc('\n', fp
) == EOF
)
2501 *\brief Write filtering / matcher configuration file
2503 void prefs_matcher_write_config(void)
2508 debug_print("Writing matcher configuration...\n");
2510 rcpath
= g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S
,
2513 if ((pfile
= prefs_write_open(rcpath
)) == NULL
) {
2514 g_warning("failed to write configuration to file\n");
2521 if (prefs_matcher_save(pfile
->fp
) < 0) {
2522 g_warning("failed to write configuration to file\n");
2523 prefs_file_close_revert(pfile
);
2524 } else if (prefs_file_close(pfile
) < 0) {
2525 g_warning("failed to save configuration to file\n");
2529 /* ******************************************************************* */
2531 static void matcher_add_rulenames(const gchar
*rcpath
)
2533 gchar
*newpath
= g_strconcat(rcpath
, ".new", NULL
);
2534 FILE *src
= g_fopen(rcpath
, "rb");
2535 FILE *dst
= g_fopen(newpath
, "wb");
2536 gchar buf
[BUFFSIZE
];
2554 while (fgets (buf
, sizeof(buf
), src
) != NULL
) {
2555 if (strlen(buf
) > 2 && buf
[0] != '['
2556 && strncmp(buf
, "rulename \"", 10)
2557 && strncmp(buf
, "enabled rulename \"", 18)
2558 && strncmp(buf
, "disabled rulename \"", 18)) {
2559 r
= fwrite("enabled rulename \"\" ",
2560 strlen("enabled rulename \"\" "), 1, dst
);
2562 g_message("cannot fwrite rulename\n");
2565 r
= fwrite(buf
, strlen(buf
), 1, dst
);
2567 g_message("cannot fwrite rule\n");
2572 move_file(newpath
, rcpath
, TRUE
);
2577 *\brief Read matcher configuration
2579 void prefs_matcher_read_config(void)
2582 gchar
*rc_old_format
;
2585 create_matchparser_hashtab();
2586 prefs_filtering_clear();
2588 rcpath
= g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S
, MATCHER_RC
, NULL
);
2589 rc_old_format
= g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S
, MATCHER_RC
,
2590 ".pre_names", NULL
);
2592 if (!is_file_exist(rc_old_format
) && is_file_exist(rcpath
)) {
2593 /* backup file with no rules names, in case
2594 * anything goes wrong */
2595 copy_file(rcpath
, rc_old_format
, FALSE
);
2596 /* now hack the file in order to have it to the new format */
2597 matcher_add_rulenames(rcpath
);
2600 g_free(rc_old_format
);
2602 f
= g_fopen(rcpath
, "rb");
2606 matcher_parser_start_parsing(f
);
2607 fclose(matcher_parserin
);
2610 /* previous version compatibility */
2612 /* g_print("reading filtering\n"); */
2613 rcpath
= g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S
,
2614 FILTERING_RC
, NULL
);
2615 f
= g_fopen(rcpath
, "rb");
2619 matcher_parser_start_parsing(f
);
2620 fclose(matcher_parserin
);
2623 /* g_print("reading scoring\n"); */
2624 rcpath
= g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S
,
2626 f
= g_fopen(rcpath
, "rb");
2630 matcher_parser_start_parsing(f
);
2631 fclose(matcher_parserin
);