2 * Claws Mail -- a GTK based, lightweight, and fast e-mail client
3 * Copyright (C) 2002-2023 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/>.
21 #include "claws-features.h"
25 #include <glib/gi18n.h>
37 #include "procheader.h"
39 #include "matcher_parser.h"
40 #include "prefs_gtk.h"
41 #include "addr_compl.h"
43 #include "quoted-printable.h"
45 #include "prefs_common.h"
48 #include "folder_item_prefs.h"
50 #include "file-utils.h"
53 *\brief Keyword lookup element
56 gint id
; /*!< keyword id */
57 gchar
*str
; /*!< keyword */
59 typedef struct _MatchParser MatchParser
;
62 *\brief Table with strings and ids used by the lexer and
63 * the parser. New keywords can be added here.
65 static const MatchParser matchparser_tab
[] = {
67 {MATCHCRITERIA_ALL
, "all"},
68 {MATCHCRITERIA_UNREAD
, "unread"},
69 {MATCHCRITERIA_NOT_UNREAD
, "~unread"},
70 {MATCHCRITERIA_NEW
, "new"},
71 {MATCHCRITERIA_NOT_NEW
, "~new"},
72 {MATCHCRITERIA_MARKED
, "marked"},
73 {MATCHCRITERIA_NOT_MARKED
, "~marked"},
74 {MATCHCRITERIA_DELETED
, "deleted"},
75 {MATCHCRITERIA_NOT_DELETED
, "~deleted"},
76 {MATCHCRITERIA_REPLIED
, "replied"},
77 {MATCHCRITERIA_NOT_REPLIED
, "~replied"},
78 {MATCHCRITERIA_FORWARDED
, "forwarded"},
79 {MATCHCRITERIA_NOT_FORWARDED
, "~forwarded"},
80 {MATCHCRITERIA_LOCKED
, "locked"},
81 {MATCHCRITERIA_NOT_LOCKED
, "~locked"},
82 {MATCHCRITERIA_COLORLABEL
, "colorlabel"},
83 {MATCHCRITERIA_NOT_COLORLABEL
, "~colorlabel"},
84 {MATCHCRITERIA_IGNORE_THREAD
, "ignore_thread"},
85 {MATCHCRITERIA_NOT_IGNORE_THREAD
, "~ignore_thread"},
86 {MATCHCRITERIA_WATCH_THREAD
, "watch_thread"},
87 {MATCHCRITERIA_NOT_WATCH_THREAD
, "~watch_thread"},
88 {MATCHCRITERIA_SPAM
, "spam"},
89 {MATCHCRITERIA_NOT_SPAM
, "~spam"},
90 {MATCHCRITERIA_HAS_ATTACHMENT
, "has_attachment"},
91 {MATCHCRITERIA_HAS_NO_ATTACHMENT
, "~has_attachment"},
92 {MATCHCRITERIA_SIGNED
, "signed"},
93 {MATCHCRITERIA_NOT_SIGNED
, "~signed"},
96 {MATCHCRITERIA_SUBJECT
, "subject"},
97 {MATCHCRITERIA_NOT_SUBJECT
, "~subject"},
98 {MATCHCRITERIA_FROM
, "from"},
99 {MATCHCRITERIA_NOT_FROM
, "~from"},
100 {MATCHCRITERIA_TO
, "to"},
101 {MATCHCRITERIA_NOT_TO
, "~to"},
102 {MATCHCRITERIA_CC
, "cc"},
103 {MATCHCRITERIA_NOT_CC
, "~cc"},
104 {MATCHCRITERIA_TO_OR_CC
, "to_or_cc"},
105 {MATCHCRITERIA_NOT_TO_AND_NOT_CC
, "~to_or_cc"},
106 {MATCHCRITERIA_TAG
, "tag"},
107 {MATCHCRITERIA_NOT_TAG
, "~tag"},
108 {MATCHCRITERIA_TAGGED
, "tagged"},
109 {MATCHCRITERIA_NOT_TAGGED
, "~tagged"},
110 {MATCHCRITERIA_AGE_GREATER
, "age_greater"},
111 {MATCHCRITERIA_AGE_LOWER
, "age_lower"},
112 {MATCHCRITERIA_AGE_GREATER_HOURS
, "age_greater_hours"},
113 {MATCHCRITERIA_AGE_LOWER_HOURS
, "age_lower_hours"},
114 {MATCHCRITERIA_DATE_AFTER
, "date_after"},
115 {MATCHCRITERIA_DATE_BEFORE
, "date_before"},
116 {MATCHCRITERIA_NEWSGROUPS
, "newsgroups"},
117 {MATCHCRITERIA_NOT_NEWSGROUPS
, "~newsgroups"},
118 {MATCHCRITERIA_MESSAGEID
, "messageid"},
119 {MATCHCRITERIA_NOT_MESSAGEID
, "~messageid"},
120 {MATCHCRITERIA_INREPLYTO
, "inreplyto"},
121 {MATCHCRITERIA_NOT_INREPLYTO
, "~inreplyto"},
122 {MATCHCRITERIA_REFERENCES
, "references"},
123 {MATCHCRITERIA_NOT_REFERENCES
, "~references"},
124 {MATCHCRITERIA_SCORE_GREATER
, "score_greater"},
125 {MATCHCRITERIA_SCORE_LOWER
, "score_lower"},
126 {MATCHCRITERIA_SCORE_EQUAL
, "score_equal"},
127 {MATCHCRITERIA_PARTIAL
, "partial"},
128 {MATCHCRITERIA_NOT_PARTIAL
, "~partial"},
129 {MATCHCRITERIA_FOUND_IN_ADDRESSBOOK
, "found_in_addressbook"},
130 {MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK
, "~found_in_addressbook"},
132 {MATCHCRITERIA_SIZE_GREATER
, "size_greater"},
133 {MATCHCRITERIA_SIZE_SMALLER
, "size_smaller"},
134 {MATCHCRITERIA_SIZE_EQUAL
, "size_equal"},
136 /* content have to be read */
137 {MATCHCRITERIA_HEADER
, "header"},
138 {MATCHCRITERIA_NOT_HEADER
, "~header"},
139 {MATCHCRITERIA_HEADERS_PART
, "headers_part"},
140 {MATCHCRITERIA_NOT_HEADERS_PART
, "~headers_part"},
141 {MATCHCRITERIA_HEADERS_CONT
, "headers_cont"},
142 {MATCHCRITERIA_NOT_HEADERS_CONT
, "~headers_cont"},
143 {MATCHCRITERIA_MESSAGE
, "message"},
144 {MATCHCRITERIA_NOT_MESSAGE
, "~message"},
145 {MATCHCRITERIA_BODY_PART
, "body_part"},
146 {MATCHCRITERIA_NOT_BODY_PART
, "~body_part"},
147 {MATCHCRITERIA_TEST
, "test"},
148 {MATCHCRITERIA_NOT_TEST
, "~test"},
151 {MATCHTYPE_MATCHCASE
, "matchcase"},
152 {MATCHTYPE_MATCH
, "match"},
153 {MATCHTYPE_REGEXPCASE
, "regexpcase"},
154 {MATCHTYPE_REGEXP
, "regexp"},
157 {MATCHACTION_SCORE
, "score"}, /* for backward compatibility */
158 {MATCHACTION_MOVE
, "move"},
159 {MATCHACTION_COPY
, "copy"},
160 {MATCHACTION_DELETE
, "delete"},
161 {MATCHACTION_MARK
, "mark"},
162 {MATCHACTION_UNMARK
, "unmark"},
163 {MATCHACTION_LOCK
, "lock"},
164 {MATCHACTION_UNLOCK
, "unlock"},
165 {MATCHACTION_MARK_AS_READ
, "mark_as_read"},
166 {MATCHACTION_MARK_AS_UNREAD
, "mark_as_unread"},
167 {MATCHACTION_MARK_AS_SPAM
, "mark_as_spam"},
168 {MATCHACTION_MARK_AS_HAM
, "mark_as_ham"},
169 {MATCHACTION_FORWARD
, "forward"},
170 {MATCHACTION_FORWARD_AS_ATTACHMENT
, "forward_as_attachment"},
171 {MATCHACTION_EXECUTE
, "execute"},
172 {MATCHACTION_COLOR
, "color"},
173 {MATCHACTION_REDIRECT
, "redirect"},
174 {MATCHACTION_CHANGE_SCORE
, "change_score"},
175 {MATCHACTION_SET_SCORE
, "set_score"},
176 {MATCHACTION_STOP
, "stop"},
177 {MATCHACTION_HIDE
, "hide"},
178 {MATCHACTION_IGNORE
, "ignore"},
179 {MATCHACTION_WATCH
, "watch"},
180 {MATCHACTION_ADD_TO_ADDRESSBOOK
, "add_to_addressbook"},
181 {MATCHACTION_SET_TAG
, "set_tag"},
182 {MATCHACTION_UNSET_TAG
, "unset_tag"},
183 {MATCHACTION_CLEAR_TAGS
, "clear_tags"},
208 static gchar
*context_str
[N_CONTEXT_STRS
];
210 void matcher_init(void)
212 if (context_str
[CONTEXT_SUBJECT
] != NULL
)
215 context_str
[CONTEXT_SUBJECT
] = g_strdup_printf(_("%s header"), prefs_common_translated_header_name("Subject:"));
216 context_str
[CONTEXT_FROM
] = g_strdup_printf(_("%s header"), prefs_common_translated_header_name("From:"));
217 context_str
[CONTEXT_TO
] = g_strdup_printf(_("%s header"), prefs_common_translated_header_name("To:"));
218 context_str
[CONTEXT_CC
] = g_strdup_printf(_("%s header"), prefs_common_translated_header_name("Cc:"));
219 context_str
[CONTEXT_NEWSGROUPS
] = g_strdup_printf(_("%s header"), prefs_common_translated_header_name("Newsgroups:"));
220 context_str
[CONTEXT_MESSAGEID
] = g_strdup_printf(_("%s header"), prefs_common_translated_header_name("Message-ID:"));
221 context_str
[CONTEXT_IN_REPLY_TO
] = g_strdup_printf(_("%s header"), prefs_common_translated_header_name("In-Reply-To:"));
222 context_str
[CONTEXT_REFERENCES
] = g_strdup_printf(_("%s header"), prefs_common_translated_header_name("References:"));
223 context_str
[CONTEXT_HEADER
] = g_strdup(_("header"));
224 context_str
[CONTEXT_HEADER_LINE
] = g_strdup(_("header line"));
225 context_str
[CONTEXT_BODY_LINE
] = g_strdup(_("body line"));
226 context_str
[CONTEXT_TAG
] = g_strdup(_("tag"));
229 void matcher_done(void)
232 for (i
= 0; i
< N_CONTEXT_STRS
; i
++) {
233 g_free(context_str
[i
]);
234 context_str
[i
] = NULL
;
238 extern gboolean debug_filtering_session
;
241 *\brief Look up table with keywords defined in \sa matchparser_tab
243 static GHashTable
*matchparser_hashtab
;
246 *\brief Translate keyword id to keyword string
248 *\param id Id of keyword
250 *\return const gchar * Keyword
252 const gchar
*get_matchparser_tab_str(gint id
)
256 for (i
= 0; i
< sizeof matchparser_tab
/ sizeof matchparser_tab
[0]; i
++) {
257 if (matchparser_tab
[i
].id
== id
)
258 return matchparser_tab
[i
].str
;
264 *\brief Create keyword lookup table
266 static void create_matchparser_hashtab(void)
270 if (matchparser_hashtab
) return;
271 matchparser_hashtab
= g_hash_table_new(g_str_hash
, g_str_equal
);
272 for (i
= 0; i
< sizeof matchparser_tab
/ sizeof matchparser_tab
[0]; i
++)
273 g_hash_table_insert(matchparser_hashtab
,
274 matchparser_tab
[i
].str
,
275 (gpointer
) &matchparser_tab
[i
]);
279 *\brief Return a keyword id from a keyword string
281 *\param str Keyword string
283 *\return gint Keyword id
285 gint
get_matchparser_tab_id(const gchar
*str
)
289 if (NULL
!= (res
= g_hash_table_lookup(matchparser_hashtab
, str
))) {
295 /* **************** data structure allocation **************** */
298 *\brief Allocate a structure for a filtering / scoring
299 * "condition" (a matcher structure)
301 *\param criteria Criteria ID (MATCHCRITERIA_XXXX)
302 *\param header Header string (if criteria is MATCHCRITERIA_HEADER
303 or MATCHCRITERIA_FOUND_IN_ADDRESSBOOK)
304 *\param matchtype Type of action (MATCHTYPE_XXX)
305 *\param expr String value or expression to check
306 *\param value Integer value to check
308 *\return MatcherProp * Pointer to newly allocated structure
310 MatcherProp
*matcherprop_new(gint criteria
, const gchar
*header
,
311 gint matchtype
, const gchar
*expr
,
316 prop
= g_new0(MatcherProp
, 1);
317 prop
->criteria
= criteria
;
318 prop
->header
= header
!= NULL
? g_strdup(header
) : NULL
;
320 prop
->expr
= expr
!= NULL
? g_strdup(expr
) : NULL
;
322 prop
->matchtype
= matchtype
;
326 prop
->casefold_expr
= NULL
;
334 *\brief Free a matcher structure
336 *\param prop Pointer to matcher structure allocated with
339 void matcherprop_free(MatcherProp
*prop
)
342 g_free(prop
->header
);
344 if (prop
->preg
!= NULL
) {
348 g_free(prop
->casefold_expr
);
354 *\brief Copy a matcher structure
356 *\param src Matcher structure to copy
358 *\return MatcherProp * Pointer to newly allocated matcher structure
360 MatcherProp
*matcherprop_copy(const MatcherProp
*src
)
362 MatcherProp
*prop
= g_new0(MatcherProp
, 1);
364 prop
->criteria
= src
->criteria
;
365 prop
->header
= src
->header
? g_strdup(src
->header
) : NULL
;
366 prop
->expr
= src
->expr
? g_strdup(src
->expr
) : NULL
;
367 prop
->matchtype
= src
->matchtype
;
370 prop
->preg
= NULL
; /* will be re-evaluated */
372 prop
->casefold_expr
= src
->casefold_expr
? g_strdup(src
->casefold_expr
) : NULL
;
373 prop
->value
= src
->value
;
374 prop
->error
= src
->error
;
378 /* ************** match ******************************/
380 static gboolean match_with_addresses_in_addressbook
381 (MatcherProp
*prop
, GSList
*address_list
, gint type
,
382 gchar
* folderpath
, gint match
)
385 gboolean found
= FALSE
;
388 cm_return_val_if_fail(address_list
!= NULL
, FALSE
);
390 debug_print("match_with_addresses_in_addressbook(%d, %s)\n",
391 g_slist_length(address_list
), folderpath
?folderpath
:"(null)");
393 if (folderpath
== NULL
||
394 strcasecmp(folderpath
, "Any") == 0 ||
400 start_address_completion(path
);
402 for (walk
= address_list
; walk
!= NULL
; walk
= walk
->next
) {
403 /* exact matching of email address */
404 guint num_addr
= complete_address(walk
->data
);
407 /* skip first item (this is the search string itself) */
409 for (; i
< num_addr
&& !found
; i
++) {
410 gchar
*addr
= get_complete_address(i
);
411 extract_address(addr
);
412 if (strcasecmp(addr
, walk
->data
) == 0) {
416 if (debug_filtering_session
417 && prefs_common
.filtering_debug_level
>= FILTERING_DEBUG_LEVEL_HIGH
) {
418 log_print(LOG_DEBUG_FILTERING
,
419 "address [ %s ] matches\n",
420 (gchar
*)walk
->data
);
427 if (debug_filtering_session
428 && prefs_common
.filtering_debug_level
>= FILTERING_DEBUG_LEVEL_HIGH
430 log_print(LOG_DEBUG_FILTERING
,
431 "address [ %s ] does NOT match\n",
432 (gchar
*)walk
->data
);
436 if (match
== MATCH_ALL
) {
437 /* if matching all addresses, stop if one doesn't match */
440 if (debug_filtering_session
441 && prefs_common
.filtering_debug_level
>= FILTERING_DEBUG_LEVEL_HIGH
) {
442 log_print(LOG_DEBUG_FILTERING
,
443 "not all address match (matching all)\n");
447 } else if (match
== MATCH_ANY
) {
448 /* if matching any address, stop if one does match */
451 if (debug_filtering_session
452 && prefs_common
.filtering_debug_level
>= FILTERING_DEBUG_LEVEL_HIGH
) {
453 log_print(LOG_DEBUG_FILTERING
,
454 "at least one address matches (matching any)\n");
459 /* MATCH_ONE: there should be only one loop iteration */
462 end_address_completion();
468 *\brief Find out if a string matches a condition
470 *\param prop Matcher structure
471 *\param str String to check
473 *\return gboolean TRUE if str matches the condition in the
476 static gboolean
matcherprop_string_match(MatcherProp
*prop
, const gchar
*str
,
477 const gchar
*debug_context
)
480 const gchar
*down_expr
;
481 gboolean ret
= FALSE
;
482 gboolean should_free
= FALSE
;
486 if (prop
->matchtype
== MATCHTYPE_REGEXPCASE
||
487 prop
->matchtype
== MATCHTYPE_MATCHCASE
) {
488 str1
= g_utf8_casefold(str
, -1);
489 if (!prop
->casefold_expr
) {
490 prop
->casefold_expr
= g_utf8_casefold(prop
->expr
, -1);
492 down_expr
= prop
->casefold_expr
;
497 down_expr
= (gchar
*)prop
->expr
;
501 switch (prop
->matchtype
) {
502 case MATCHTYPE_REGEXPCASE
:
503 case MATCHTYPE_REGEXP
:
504 if (!prop
->preg
&& (prop
->error
== 0)) {
505 prop
->preg
= g_new0(regex_t
, 1);
506 /* if regexp then don't use the escaped string */
507 if (regcomp(prop
->preg
, down_expr
,
508 REG_NOSUB
| REG_EXTENDED
509 | ((prop
->matchtype
== MATCHTYPE_REGEXPCASE
)
510 ? REG_ICASE
: 0)) != 0) {
517 if (prop
->preg
== NULL
) {
522 if (regexec(prop
->preg
, str1
, 0, NULL
, 0) == 0)
528 if (debug_filtering_session
529 && prefs_common
.filtering_debug_level
>= FILTERING_DEBUG_LEVEL_HIGH
) {
530 gchar
*stripped
= g_strdup(str
);
532 strretchomp(stripped
);
534 log_print(LOG_DEBUG_FILTERING
,
535 "%s value [ %s ] matches regular expression [ %s ] (%s)\n",
536 debug_context
, stripped
, prop
->expr
,
537 prop
->matchtype
== MATCHTYPE_REGEXP
? _("Case sensitive"):_("Case insensitive"));
539 log_print(LOG_DEBUG_FILTERING
,
540 "%s value [ %s ] does NOT match regular expression [ %s ] (%s)\n",
541 debug_context
, stripped
, prop
->expr
,
542 prop
->matchtype
== MATCHTYPE_REGEXP
? _("Case sensitive"):_("Case insensitive"));
547 case MATCHTYPE_MATCHCASE
:
548 case MATCHTYPE_MATCH
:
549 ret
= (strstr(str1
, down_expr
) != NULL
);
552 if (debug_filtering_session
553 && prefs_common
.filtering_debug_level
>= FILTERING_DEBUG_LEVEL_HIGH
) {
554 gchar
*stripped
= g_strdup(str
);
556 strretchomp(stripped
);
558 log_print(LOG_DEBUG_FILTERING
,
559 "%s value [ %s ] contains [ %s ] (%s)\n",
560 debug_context
, stripped
, prop
->expr
,
561 prop
->matchtype
== MATCHTYPE_MATCH
? _("Case sensitive"):_("Case insensitive"));
563 log_print(LOG_DEBUG_FILTERING
,
564 "%s value [ %s ] does NOT contain [ %s ] (%s)\n",
565 debug_context
, stripped
, prop
->expr
,
566 prop
->matchtype
== MATCHTYPE_MATCH
? _("Case sensitive"):_("Case insensitive"));
584 *\brief Find out if a tag matches a condition
586 *\param prop Matcher structure
587 *\param msginfo message to check
589 *\return gboolean TRUE if msginfo matches the condition in the
592 static gboolean
matcherprop_tag_match(MatcherProp
*prop
, MsgInfo
*msginfo
,
593 const gchar
*debug_context
)
595 gboolean ret
= FALSE
;
598 if (msginfo
== NULL
|| msginfo
->tags
== NULL
)
601 for (cur
= msginfo
->tags
; cur
; cur
= cur
->next
) {
602 const gchar
*str
= tags_get_tag(GPOINTER_TO_INT(cur
->data
));
605 if (matcherprop_string_match(prop
, str
, debug_context
)) {
614 *\brief Find out if the string-ed list matches a condition
616 *\param prop Matcher structure
617 *\param list GSList of strings to check
619 *\return gboolean TRUE if str matches the condition in the
622 static gboolean
matcherprop_list_match(MatcherProp
*prop
, const GSList
*list
,
623 const gchar
*debug_context
)
627 for(cur
= list
; cur
!= NULL
; cur
= cur
->next
) {
628 if (matcherprop_string_match(prop
, (gchar
*)cur
->data
, debug_context
))
634 static gboolean
matcherprop_header_line_match(MatcherProp
*prop
, const gchar
*hdr
,
635 const gchar
*str
, const gboolean both
,
636 const gchar
*debug_context
)
638 gboolean res
= FALSE
;
640 if (hdr
== NULL
|| str
== NULL
)
644 /* Search in all header names and content.
646 gchar
*line
= g_strdup_printf("%s %s", hdr
, str
);
647 res
= matcherprop_string_match(prop
, line
, debug_context
);
650 /* Search only in content and exclude private headers.
651 * E.g.: searching for "H foo" in folder x/foo would return
652 * *all* mail as SCF and RMID will match.
653 * Searching for "H sent" would return all resent messages
654 * as "Resent-From: whatever" will match.
656 if (procheader_header_is_internal(hdr
))
658 res
= matcherprop_string_match(prop
, str
, debug_context
);
665 typedef struct _thread_data
{
672 static void *matcher_test_thread(void *data
)
674 thread_data
*td
= (thread_data
*)data
;
677 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE
, NULL
);
678 pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS
, NULL
);
680 result
= system(td
->cmd
);
681 td
->done
= TRUE
; /* let the caller thread join() */
682 return GINT_TO_POINTER(result
);
687 *\brief Execute a command defined in the matcher structure
689 *\param prop Pointer to matcher structure
690 *\param info Pointer to message info structure
692 *\return gboolean TRUE if command was executed successfully
694 static gboolean
matcherprop_match_test(const MatcherProp
*prop
,
702 thread_data
*td
= g_new0(thread_data
, 1);
704 time_t start_time
= time(NULL
);
707 file
= procmsg_get_message_file(info
);
716 cmd
= matching_build_command(prop
->expr
, info
);
726 if (debug_filtering_session
727 && prefs_common
.filtering_debug_level
>= FILTERING_DEBUG_LEVEL_HIGH
) {
728 log_print(LOG_DEBUG_FILTERING
,
729 "starting threaded command [ %s ]\n",
735 if (pthread_create(&pt
, NULL
, matcher_test_thread
, td
) != 0)
736 retval
= system(cmd
);
738 debug_print("waiting for test thread\n");
740 /* don't let the interface freeze while waiting */
741 if (time(NULL
) - start_time
> 0) {
744 if (time(NULL
) - start_time
> 30) {
750 pthread_join(pt
, &res
);
751 retval
= GPOINTER_TO_INT(res
);
752 debug_print(" test thread returned %d\n", retval
);
757 if (debug_filtering_session
758 && prefs_common
.filtering_debug_level
>= FILTERING_DEBUG_LEVEL_HIGH
) {
759 log_print(LOG_DEBUG_FILTERING
,
760 "starting synchronous command [ %s ]\n",
764 retval
= system(cmd
);
766 debug_print("Command exit code: %d\n", retval
);
769 if (debug_filtering_session
770 && prefs_common
.filtering_debug_level
>= FILTERING_DEBUG_LEVEL_HIGH
) {
771 log_print(LOG_DEBUG_FILTERING
,
772 "command returned [ %d ]\n",
777 return (retval
== 0);
781 *\brief Check if a message matches the condition in a matcher
784 *\param prop Pointer to matcher structure
785 *\param info Pointer to message info
787 *\return gboolean TRUE if a match
789 static gboolean
matcherprop_match(MatcherProp
*prop
,
793 gint age_mult_hours
= 1;
795 switch(prop
->criteria
) {
796 case MATCHCRITERIA_ALL
:
798 case MATCHCRITERIA_UNREAD
:
799 return MSG_IS_UNREAD(info
->flags
);
800 case MATCHCRITERIA_NOT_UNREAD
:
801 return !MSG_IS_UNREAD(info
->flags
);
802 case MATCHCRITERIA_NEW
:
803 return MSG_IS_NEW(info
->flags
);
804 case MATCHCRITERIA_NOT_NEW
:
805 return !MSG_IS_NEW(info
->flags
);
806 case MATCHCRITERIA_MARKED
:
807 return MSG_IS_MARKED(info
->flags
);
808 case MATCHCRITERIA_NOT_MARKED
:
809 return !MSG_IS_MARKED(info
->flags
);
810 case MATCHCRITERIA_DELETED
:
811 return MSG_IS_DELETED(info
->flags
);
812 case MATCHCRITERIA_NOT_DELETED
:
813 return !MSG_IS_DELETED(info
->flags
);
814 case MATCHCRITERIA_REPLIED
:
815 return MSG_IS_REPLIED(info
->flags
);
816 case MATCHCRITERIA_NOT_REPLIED
:
817 return !MSG_IS_REPLIED(info
->flags
);
818 case MATCHCRITERIA_FORWARDED
:
819 return MSG_IS_FORWARDED(info
->flags
);
820 case MATCHCRITERIA_NOT_FORWARDED
:
821 return !MSG_IS_FORWARDED(info
->flags
);
822 case MATCHCRITERIA_LOCKED
:
823 return MSG_IS_LOCKED(info
->flags
);
824 case MATCHCRITERIA_NOT_LOCKED
:
825 return !MSG_IS_LOCKED(info
->flags
);
826 case MATCHCRITERIA_SPAM
:
827 return MSG_IS_SPAM(info
->flags
);
828 case MATCHCRITERIA_NOT_SPAM
:
829 return !MSG_IS_SPAM(info
->flags
);
830 case MATCHCRITERIA_HAS_ATTACHMENT
:
831 return MSG_IS_WITH_ATTACHMENT(info
->flags
);
832 case MATCHCRITERIA_HAS_NO_ATTACHMENT
:
833 return !MSG_IS_WITH_ATTACHMENT(info
->flags
);
834 case MATCHCRITERIA_SIGNED
:
835 return MSG_IS_SIGNED(info
->flags
);
836 case MATCHCRITERIA_NOT_SIGNED
:
837 return !MSG_IS_SIGNED(info
->flags
);
838 case MATCHCRITERIA_COLORLABEL
:
840 gint color
= MSG_GET_COLORLABEL_VALUE(info
->flags
);
841 gboolean ret
= (color
== prop
->value
);
844 if (debug_filtering_session
845 && prefs_common
.filtering_debug_level
>= FILTERING_DEBUG_LEVEL_HIGH
) {
847 log_print(LOG_DEBUG_FILTERING
,
848 "message color value [ %d ] matches color value [ %d ]\n",
851 log_print(LOG_DEBUG_FILTERING
,
852 "message color value [ %d ] does NOT match color value [ %d ]\n",
858 case MATCHCRITERIA_NOT_COLORLABEL
:
860 gint color
= MSG_GET_COLORLABEL_VALUE(info
->flags
);
861 gboolean ret
= (color
!= prop
->value
);
864 if (debug_filtering_session
865 && prefs_common
.filtering_debug_level
>= FILTERING_DEBUG_LEVEL_HIGH
) {
867 log_print(LOG_DEBUG_FILTERING
,
868 "message color value [ %d ] matches color value [ %d ]\n",
871 log_print(LOG_DEBUG_FILTERING
,
872 "message color value [ %d ] does NOT match color value [ %d ]\n",
878 case MATCHCRITERIA_IGNORE_THREAD
:
879 return MSG_IS_IGNORE_THREAD(info
->flags
);
880 case MATCHCRITERIA_NOT_IGNORE_THREAD
:
881 return !MSG_IS_IGNORE_THREAD(info
->flags
);
882 case MATCHCRITERIA_WATCH_THREAD
:
883 return MSG_IS_WATCH_THREAD(info
->flags
);
884 case MATCHCRITERIA_NOT_WATCH_THREAD
:
885 return !MSG_IS_WATCH_THREAD(info
->flags
);
886 case MATCHCRITERIA_SUBJECT
:
887 return matcherprop_string_match(prop
, info
->subject
, context_str
[CONTEXT_SUBJECT
]);
888 case MATCHCRITERIA_NOT_SUBJECT
:
889 return !matcherprop_string_match(prop
, info
->subject
, context_str
[CONTEXT_SUBJECT
]);
890 case MATCHCRITERIA_FROM
:
891 return matcherprop_string_match(prop
, info
->from
, context_str
[CONTEXT_FROM
]);
892 case MATCHCRITERIA_NOT_FROM
:
893 return !matcherprop_string_match(prop
, info
->from
, context_str
[CONTEXT_FROM
]);
894 case MATCHCRITERIA_TO
:
895 return matcherprop_string_match(prop
, info
->to
, context_str
[CONTEXT_TO
]);
896 case MATCHCRITERIA_NOT_TO
:
897 return !matcherprop_string_match(prop
, info
->to
, context_str
[CONTEXT_TO
]);
898 case MATCHCRITERIA_CC
:
899 return matcherprop_string_match(prop
, info
->cc
, context_str
[CONTEXT_CC
]);
900 case MATCHCRITERIA_NOT_CC
:
901 return !matcherprop_string_match(prop
, info
->cc
, context_str
[CONTEXT_CC
]);
902 case MATCHCRITERIA_TO_OR_CC
:
903 return matcherprop_string_match(prop
, info
->to
, context_str
[CONTEXT_TO
])
904 || matcherprop_string_match(prop
, info
->cc
, context_str
[CONTEXT_CC
]);
905 case MATCHCRITERIA_NOT_TO_AND_NOT_CC
:
906 return !matcherprop_string_match(prop
, info
->to
, context_str
[CONTEXT_TO
])
907 && !matcherprop_string_match(prop
, info
->cc
, context_str
[CONTEXT_CC
]);
908 case MATCHCRITERIA_TAG
:
909 return matcherprop_tag_match(prop
, info
, context_str
[CONTEXT_TAG
]);
910 case MATCHCRITERIA_NOT_TAG
:
911 return !matcherprop_tag_match(prop
, info
, context_str
[CONTEXT_TAG
]);
912 case MATCHCRITERIA_TAGGED
:
913 return info
->tags
!= NULL
;
914 case MATCHCRITERIA_NOT_TAGGED
:
915 return info
->tags
== NULL
;
916 case MATCHCRITERIA_AGE_GREATER
:
918 /* Fallthrough intended */
919 case MATCHCRITERIA_AGE_GREATER_HOURS
:
925 age
= ((t
- info
->date_t
) / (60 * 60 * age_mult_hours
));
926 ret
= (age
>= prop
->value
);
929 if (debug_filtering_session
930 && prefs_common
.filtering_debug_level
>= FILTERING_DEBUG_LEVEL_HIGH
) {
932 log_print(LOG_DEBUG_FILTERING
,
933 "message age [ %d ] is greater than [ %d ]\n",
936 log_print(LOG_DEBUG_FILTERING
,
937 "message age [ %d ] is not greater than [ %d ]\n",
943 case MATCHCRITERIA_DATE_AFTER
:
947 ret
= prop
->value
< info
->date_t
;
950 if (debug_filtering_session
951 && prefs_common
.filtering_debug_level
>= FILTERING_DEBUG_LEVEL_HIGH
) {
953 log_print(LOG_DEBUG_FILTERING
,
954 "message date [ %" CM_TIME_FORMAT
" ] is after [ %d ]\n",
955 info
->date_t
, prop
->value
);
957 log_print(LOG_DEBUG_FILTERING
,
958 "message date [ %" CM_TIME_FORMAT
" ] is not after [ %d ]\n",
959 info
->date_t
, prop
->value
);
964 case MATCHCRITERIA_AGE_LOWER
:
966 /* Fallthrough intended */
967 case MATCHCRITERIA_AGE_LOWER_HOURS
:
973 age
= ((t
- info
->date_t
) / (60 * 60 * age_mult_hours
));
974 ret
= (age
< prop
->value
);
977 if (debug_filtering_session
978 && prefs_common
.filtering_debug_level
>= FILTERING_DEBUG_LEVEL_HIGH
) {
980 log_print(LOG_DEBUG_FILTERING
,
981 "message age [ %d ] is lower than [ %d ]\n",
984 log_print(LOG_DEBUG_FILTERING
,
985 "message age [ %d ] is not lower than [ %d ]\n",
991 case MATCHCRITERIA_DATE_BEFORE
:
995 ret
= prop
->value
> info
->date_t
;
998 if (debug_filtering_session
999 && prefs_common
.filtering_debug_level
>= FILTERING_DEBUG_LEVEL_HIGH
) {
1001 log_print(LOG_DEBUG_FILTERING
,
1002 "message date [ %" CM_TIME_FORMAT
" ] is before [ %d ]\n",
1003 info
->date_t
, prop
->value
);
1005 log_print(LOG_DEBUG_FILTERING
,
1006 "message date [ %" CM_TIME_FORMAT
" ] is not before [ %d ]\n",
1007 info
->date_t
, prop
->value
);
1012 case MATCHCRITERIA_SCORE_GREATER
:
1014 gboolean ret
= (info
->score
> prop
->value
);
1017 if (debug_filtering_session
1018 && prefs_common
.filtering_debug_level
>= FILTERING_DEBUG_LEVEL_HIGH
) {
1020 log_print(LOG_DEBUG_FILTERING
,
1021 "message score [ %d ] is greater than [ %d ]\n",
1022 info
->score
, prop
->value
);
1024 log_print(LOG_DEBUG_FILTERING
,
1025 "message score [ %d ] is not greater than [ %d ]\n",
1026 info
->score
, prop
->value
);
1031 case MATCHCRITERIA_SCORE_LOWER
:
1033 gboolean ret
= (info
->score
< prop
->value
);
1036 if (debug_filtering_session
1037 && prefs_common
.filtering_debug_level
>= FILTERING_DEBUG_LEVEL_HIGH
) {
1039 log_print(LOG_DEBUG_FILTERING
,
1040 "message score [ %d ] is lower than [ %d ]\n",
1041 info
->score
, prop
->value
);
1043 log_print(LOG_DEBUG_FILTERING
,
1044 "message score [ %d ] is not lower than [ %d ]\n",
1045 info
->score
, prop
->value
);
1050 case MATCHCRITERIA_SCORE_EQUAL
:
1052 gboolean ret
= (info
->score
== prop
->value
);
1055 if (debug_filtering_session
1056 && prefs_common
.filtering_debug_level
>= FILTERING_DEBUG_LEVEL_HIGH
) {
1058 log_print(LOG_DEBUG_FILTERING
,
1059 "message score [ %d ] is equal to [ %d ]\n",
1060 info
->score
, prop
->value
);
1062 log_print(LOG_DEBUG_FILTERING
,
1063 "message score [ %d ] is not equal to [ %d ]\n",
1064 info
->score
, prop
->value
);
1069 case MATCHCRITERIA_SIZE_GREATER
:
1071 /* FIXME: info->size is a goffset */
1072 gboolean ret
= (info
->size
> (goffset
) prop
->value
);
1075 if (debug_filtering_session
1076 && prefs_common
.filtering_debug_level
>= FILTERING_DEBUG_LEVEL_HIGH
) {
1078 log_print(LOG_DEBUG_FILTERING
,
1079 "message size is greater than [ %d ]\n",
1082 log_print(LOG_DEBUG_FILTERING
,
1083 "message size is not greater than [ %d ]\n",
1089 case MATCHCRITERIA_SIZE_SMALLER
:
1091 /* FIXME: info->size is a goffset */
1092 gboolean ret
= (info
->size
< (goffset
) prop
->value
);
1095 if (debug_filtering_session
1096 && prefs_common
.filtering_debug_level
>= FILTERING_DEBUG_LEVEL_HIGH
) {
1098 log_print(LOG_DEBUG_FILTERING
,
1099 "message size is smaller than [ %d ]\n",
1102 log_print(LOG_DEBUG_FILTERING
,
1103 "message size is not smaller than [ %d ]\n",
1109 case MATCHCRITERIA_SIZE_EQUAL
:
1111 /* FIXME: info->size is a goffset */
1112 gboolean ret
= (info
->size
== (goffset
) prop
->value
);
1115 if (debug_filtering_session
1116 && prefs_common
.filtering_debug_level
>= FILTERING_DEBUG_LEVEL_HIGH
) {
1118 log_print(LOG_DEBUG_FILTERING
,
1119 "message size is equal to [ %d ]\n",
1122 log_print(LOG_DEBUG_FILTERING
,
1123 "message size is not equal to [ %d ]\n",
1129 case MATCHCRITERIA_PARTIAL
:
1131 /* FIXME: info->size is a goffset */
1132 gboolean ret
= (info
->total_size
!= 0 && info
->size
!= (goffset
)info
->total_size
);
1135 if (debug_filtering_session
1136 && prefs_common
.filtering_debug_level
>= FILTERING_DEBUG_LEVEL_HIGH
) {
1138 log_print(LOG_DEBUG_FILTERING
,
1139 "message is partially downloaded, size is less than total size [ %d ])\n",
1142 log_print(LOG_DEBUG_FILTERING
,
1143 "message is not partially downloaded\n");
1148 case MATCHCRITERIA_NOT_PARTIAL
:
1150 /* FIXME: info->size is a goffset */
1151 gboolean ret
= (info
->total_size
== 0 || info
->size
== (goffset
)info
->total_size
);
1154 if (debug_filtering_session
1155 && prefs_common
.filtering_debug_level
>= FILTERING_DEBUG_LEVEL_HIGH
) {
1157 log_print(LOG_DEBUG_FILTERING
,
1158 "message is not partially downloaded\n");
1160 log_print(LOG_DEBUG_FILTERING
,
1161 "message is partially downloaded, size is less than total size [ %d ])\n",
1167 case MATCHCRITERIA_NEWSGROUPS
:
1168 return matcherprop_string_match(prop
, info
->newsgroups
, context_str
[CONTEXT_NEWSGROUPS
]);
1169 case MATCHCRITERIA_NOT_NEWSGROUPS
:
1170 return !matcherprop_string_match(prop
, info
->newsgroups
, context_str
[CONTEXT_NEWSGROUPS
]);
1171 case MATCHCRITERIA_MESSAGEID
:
1172 return matcherprop_string_match(prop
, info
->msgid
, context_str
[CONTEXT_MESSAGEID
]);
1173 case MATCHCRITERIA_NOT_MESSAGEID
:
1174 return !matcherprop_string_match(prop
, info
->msgid
, context_str
[CONTEXT_MESSAGEID
]);
1175 case MATCHCRITERIA_INREPLYTO
:
1176 return matcherprop_string_match(prop
, info
->inreplyto
, context_str
[CONTEXT_IN_REPLY_TO
]);
1177 case MATCHCRITERIA_NOT_INREPLYTO
:
1178 return !matcherprop_string_match(prop
, info
->inreplyto
, context_str
[CONTEXT_IN_REPLY_TO
]);
1179 case MATCHCRITERIA_REFERENCES
:
1180 return matcherprop_list_match(prop
, info
->references
, context_str
[CONTEXT_REFERENCES
]);
1181 case MATCHCRITERIA_NOT_REFERENCES
:
1182 return !matcherprop_list_match(prop
, info
->references
, context_str
[CONTEXT_REFERENCES
]);
1183 case MATCHCRITERIA_TEST
:
1184 return matcherprop_match_test(prop
, info
);
1185 case MATCHCRITERIA_NOT_TEST
:
1186 return !matcherprop_match_test(prop
, info
);
1192 /* ********************* MatcherList *************************** */
1195 *\brief Create a new list of matchers
1197 *\param matchers List of matcher structures
1198 *\param bool_and Operator
1200 *\return MatcherList * New list
1202 MatcherList
*matcherlist_new(GSList
*matchers
, gboolean bool_and
)
1206 cond
= g_new0(MatcherList
, 1);
1208 cond
->matchers
= matchers
;
1209 cond
->bool_and
= bool_and
;
1216 *\brief Builds a single regular expresion from an array of srings.
1218 *\param strings The lines containing the different sub-regexp.
1220 *\return The newly allocated regexp string.
1222 static gchar
*build_complete_regexp(gchar
**strings
)
1226 while (strings
&& strings
[i
] && *strings
[i
]) {
1227 int old_len
= expr
? strlen(expr
):0;
1229 gchar
*tmpstr
= NULL
;
1231 if (g_utf8_validate(strings
[i
], -1, NULL
))
1232 tmpstr
= g_strdup(strings
[i
]);
1234 tmpstr
= conv_codeset_strdup(strings
[i
],
1235 conv_get_locale_charset_str_no_utf8(),
1238 if (strstr(tmpstr
, "\n"))
1239 *(strstr(tmpstr
, "\n")) = '\0';
1241 new_len
= strlen(tmpstr
);
1243 expr
= g_realloc(expr
,
1244 expr
? (old_len
+ strlen("|()") + new_len
+ 1)
1245 : (strlen("()") + new_len
+ 1));
1248 strcpy(expr
+ old_len
, "|(");
1249 strcpy(expr
+ old_len
+ 2, tmpstr
);
1250 strcpy(expr
+ old_len
+ 2 + new_len
, ")");
1252 strcpy(expr
+old_len
, "(");
1253 strcpy(expr
+old_len
+ 1, tmpstr
);
1254 strcpy(expr
+old_len
+ 1 + new_len
, ")");
1264 *\brief Create a new list of matchers from a multi-line string
1266 *\param lines String with "\n"-separated expressions
1267 *\param bool_and Operator
1268 *\param case_sensitive If the matching is case sensitive or not
1270 *\return MatcherList * New matcher list
1272 MatcherList
*matcherlist_new_from_lines(gchar
*lines
, gboolean bool_and
,
1273 gboolean case_sensitive
)
1275 MatcherProp
*m
= NULL
;
1276 GSList
*matchers
= NULL
;
1277 gchar
**strings
= g_strsplit(lines
, "\n", -1);
1281 expr
= build_complete_regexp(strings
);
1282 debug_print("building matcherprop for expr '%s'\n", expr
?expr
:"NULL");
1284 m
= matcherprop_new(MATCHCRITERIA_SUBJECT
, NULL
,
1285 case_sensitive
? MATCHTYPE_REGEXP
: MATCHTYPE_REGEXPCASE
,
1288 /* print error message */
1289 debug_print("failed to allocate memory for matcherprop\n");
1291 matchers
= g_slist_append(matchers
, m
);
1297 while (strings
&& strings
[i
] && *strings
[i
]) {
1298 m
= matcherprop_new(MATCHCRITERIA_SUBJECT
, NULL
,
1299 case_sensitive
? MATCHTYPE_MATCH
: MATCHTYPE_MATCHCASE
,
1302 /* print error message */
1303 debug_print("failed to allocate memory for matcherprop\n");
1305 matchers
= g_slist_append(matchers
, m
);
1310 g_strfreev(strings
);
1312 return matcherlist_new(matchers
, bool_and
);
1316 *\brief Frees a list of matchers
1318 *\param cond List of matchers
1320 void matcherlist_free(MatcherList
*cond
)
1324 cm_return_if_fail(cond
);
1325 for (l
= cond
->matchers
; l
!= NULL
; l
= g_slist_next(l
)) {
1326 matcherprop_free((MatcherProp
*) l
->data
);
1328 g_slist_free(cond
->matchers
);
1333 *\brief Check if a header matches a matcher condition
1335 *\param matcher Matcher structure to check header for
1336 *\param buf Header name
1338 *\return boolean TRUE if matching header
1340 static gboolean
matcherprop_match_one_header(MatcherProp
*matcher
,
1343 gboolean result
= FALSE
;
1344 Header
*header
= NULL
;
1346 switch (matcher
->criteria
) {
1347 case MATCHCRITERIA_HEADER
:
1348 case MATCHCRITERIA_NOT_HEADER
:
1349 header
= procheader_parse_header(buf
);
1352 if (procheader_headername_equal(header
->name
,
1354 if (matcher
->criteria
== MATCHCRITERIA_HEADER
)
1355 result
= matcherprop_string_match(matcher
, header
->body
, context_str
[CONTEXT_HEADER
]);
1357 result
= !matcherprop_string_match(matcher
, header
->body
, context_str
[CONTEXT_HEADER
]);
1358 procheader_header_free(header
);
1362 procheader_header_free(header
);
1365 case MATCHCRITERIA_HEADERS_PART
:
1366 case MATCHCRITERIA_HEADERS_CONT
:
1367 case MATCHCRITERIA_MESSAGE
:
1368 header
= procheader_parse_header(buf
);
1371 result
= matcherprop_header_line_match(matcher
,
1372 header
->name
, header
->body
,
1373 (matcher
->criteria
== MATCHCRITERIA_HEADERS_PART
),
1374 context_str
[CONTEXT_HEADER_LINE
]);
1375 procheader_header_free(header
);
1377 case MATCHCRITERIA_NOT_HEADERS_CONT
:
1378 case MATCHCRITERIA_NOT_HEADERS_PART
:
1379 case MATCHCRITERIA_NOT_MESSAGE
:
1380 header
= procheader_parse_header(buf
);
1383 result
= !matcherprop_header_line_match(matcher
,
1384 header
->name
, header
->body
,
1385 (matcher
->criteria
== MATCHCRITERIA_NOT_HEADERS_PART
),
1386 context_str
[CONTEXT_HEADER_LINE
]);
1387 procheader_header_free(header
);
1389 case MATCHCRITERIA_FOUND_IN_ADDRESSBOOK
:
1390 case MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK
:
1392 GSList
*address_list
= NULL
;
1393 gint match
= MATCH_ONE
;
1394 gboolean found
= FALSE
;
1396 /* how many address headers are we trying to match? */
1397 if (strcasecmp(matcher
->header
, "Any") == 0)
1399 else if (strcasecmp(matcher
->header
, "All") == 0)
1402 if (match
== MATCH_ONE
) {
1403 /* matching one address header exactly, is that the right one? */
1404 header
= procheader_parse_header(buf
);
1406 !procheader_headername_equal(header
->name
, matcher
->header
)) {
1407 procheader_header_free(header
);
1410 address_list
= address_list_append(address_list
, header
->body
);
1411 if (address_list
== NULL
) {
1412 procheader_header_free(header
);
1415 procheader_header_free(header
);
1418 header
= procheader_parse_header(buf
);
1421 /* address header is one of the headers we have to match when checking
1422 for any address header or all address headers? */
1423 if (procheader_headername_equal(header
->name
, "From") ||
1424 procheader_headername_equal(header
->name
, "To") ||
1425 procheader_headername_equal(header
->name
, "Cc") ||
1426 procheader_headername_equal(header
->name
, "Reply-To") ||
1427 procheader_headername_equal(header
->name
, "Sender") ||
1428 procheader_headername_equal(header
->name
, "Resent-From") ||
1429 procheader_headername_equal(header
->name
, "Resent-To"))
1430 address_list
= address_list_append(address_list
, header
->body
);
1431 procheader_header_free(header
);
1432 if (address_list
== NULL
)
1436 found
= match_with_addresses_in_addressbook
1437 (matcher
, address_list
, matcher
->criteria
,
1438 matcher
->expr
, match
);
1439 g_slist_free(address_list
);
1441 if (matcher
->criteria
== MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK
)
1452 *\brief Check if the matcher structure wants headers to
1455 *\param matcher Matcher structure
1457 *\return gboolean TRUE if the matcher structure describes
1458 * a header match condition
1460 static gboolean
matcherprop_criteria_headers(const MatcherProp
*matcher
)
1462 switch (matcher
->criteria
) {
1463 case MATCHCRITERIA_HEADER
:
1464 case MATCHCRITERIA_NOT_HEADER
:
1465 case MATCHCRITERIA_HEADERS_PART
:
1466 case MATCHCRITERIA_HEADERS_CONT
:
1467 case MATCHCRITERIA_NOT_HEADERS_PART
:
1468 case MATCHCRITERIA_NOT_HEADERS_CONT
:
1469 case MATCHCRITERIA_FOUND_IN_ADDRESSBOOK
:
1470 case MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK
:
1478 *\brief Check if the matcher structure wants the message
1479 * to be matched (just perform an action on any
1482 *\param matcher Matcher structure
1484 *\return gboolean TRUE if matcher condition should match
1487 static gboolean
matcherprop_criteria_message(MatcherProp
*matcher
)
1489 switch (matcher
->criteria
) {
1490 case MATCHCRITERIA_MESSAGE
:
1491 case MATCHCRITERIA_NOT_MESSAGE
:
1499 *\brief Check if a list of conditions matches one header in
1502 *\param matchers List of conditions
1503 *\param fp Message file
1505 *\return gboolean TRUE if one of the headers is matched by
1506 * the list of conditions.
1508 static gboolean
matcherlist_match_headers(MatcherList
*matchers
, FILE *fp
)
1514 while ((ret
= procheader_get_one_field(&buf
, fp
, NULL
)) != -1) {
1515 for (l
= matchers
->matchers
; l
!= NULL
; l
= g_slist_next(l
)) {
1516 MatcherProp
*matcher
= (MatcherProp
*) l
->data
;
1517 gint match
= MATCH_ANY
;
1522 /* determine the match range (all, any are our concern here) */
1523 if (matcher
->criteria
== MATCHCRITERIA_NOT_HEADERS_PART
||
1524 matcher
->criteria
== MATCHCRITERIA_NOT_HEADERS_CONT
||
1525 matcher
->criteria
== MATCHCRITERIA_NOT_MESSAGE
) {
1528 } else if (matcher
->criteria
== MATCHCRITERIA_FOUND_IN_ADDRESSBOOK
||
1529 matcher
->criteria
== MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK
) {
1530 Header
*header
= NULL
;
1532 /* address header is one of the headers we have to match when checking
1533 for any address header or all address headers? */
1534 header
= procheader_parse_header(buf
);
1536 (procheader_headername_equal(header
->name
, "From") ||
1537 procheader_headername_equal(header
->name
, "To") ||
1538 procheader_headername_equal(header
->name
, "Cc") ||
1539 procheader_headername_equal(header
->name
, "Reply-To") ||
1540 procheader_headername_equal(header
->name
, "Sender") ||
1541 procheader_headername_equal(header
->name
, "Resent-From") ||
1542 procheader_headername_equal(header
->name
, "Resent-To"))) {
1544 if (strcasecmp(matcher
->header
, "Any") == 0)
1546 else if (strcasecmp(matcher
->header
, "All") == 0)
1553 /* matching one address header exactly, is that the right one?
1554 further call to matcherprop_match_one_header() will tell us */
1556 procheader_header_free(header
);
1559 /* ZERO line must NOT match for the rule to match.
1561 if (match
== MATCH_ALL
) {
1562 if (matcherprop_match_one_header(matcher
, buf
)) {
1563 matcher
->result
= TRUE
;
1565 matcher
->result
= FALSE
;
1566 matcher
->done
= TRUE
;
1568 /* else, just one line matching is enough for the rule to match
1570 } else if (matcherprop_criteria_headers(matcher
) ||
1571 matcherprop_criteria_message(matcher
)) {
1572 if (matcherprop_match_one_header(matcher
, buf
)) {
1573 matcher
->result
= TRUE
;
1574 matcher
->done
= TRUE
;
1578 /* if the rule matched and the matchers are OR, no need to
1579 * check the others */
1580 if (matcher
->result
&& matcher
->done
) {
1581 if (!matchers
->bool_and
) {
1595 *\brief Check if a matcher wants to check the message body
1597 *\param matcher Matcher structure
1599 *\return gboolean TRUE if body must be matched.
1601 static gboolean
matcherprop_criteria_body(const MatcherProp
*matcher
)
1603 switch (matcher
->criteria
) {
1604 case MATCHCRITERIA_BODY_PART
:
1605 case MATCHCRITERIA_NOT_BODY_PART
:
1612 static gboolean
matcherlist_match_binary_content(MatcherList
*matchers
, MimeInfo
*partinfo
)
1615 gchar buf
[BUFFSIZE
];
1618 if (!partinfo
|| partinfo
->type
== MIMETYPE_TEXT
)
1621 outfp
= procmime_get_binary_content(partinfo
);
1626 while (claws_fgets(buf
, sizeof(buf
), outfp
) != NULL
) {
1629 for (l
= matchers
->matchers
; l
!= NULL
; l
= g_slist_next(l
)) {
1630 MatcherProp
*matcher
= (MatcherProp
*) l
->data
;
1635 /* Don't scan non-text parts when looking in body, only
1636 * when looking in whole message
1638 if (matcher
->criteria
== MATCHCRITERIA_NOT_BODY_PART
||
1639 matcher
->criteria
== MATCHCRITERIA_BODY_PART
)
1642 /* if the criteria is ~body_part or ~message, ZERO lines
1643 * must match for the rule to match.
1645 if (matcher
->criteria
== MATCHCRITERIA_NOT_BODY_PART
||
1646 matcher
->criteria
== MATCHCRITERIA_NOT_MESSAGE
) {
1647 if (matcherprop_string_match(matcher
, buf
,
1648 context_str
[CONTEXT_BODY_LINE
])) {
1649 matcher
->result
= FALSE
;
1650 matcher
->done
= TRUE
;
1652 matcher
->result
= TRUE
;
1653 /* else, just one line has to match */
1654 } else if (matcherprop_criteria_body(matcher
) ||
1655 matcherprop_criteria_message(matcher
)) {
1656 if (matcherprop_string_match(matcher
, buf
,
1657 context_str
[CONTEXT_BODY_LINE
])) {
1658 matcher
->result
= TRUE
;
1659 matcher
->done
= TRUE
;
1663 /* if the matchers are OR'ed and the rule matched,
1664 * no need to check the others. */
1665 if (matcher
->result
&& matcher
->done
) {
1666 if (!matchers
->bool_and
) {
1667 claws_fclose(outfp
);
1674 claws_fclose(outfp
);
1678 static gboolean
match_content_cb(const gchar
*buf
, gpointer data
)
1680 MatcherList
*matchers
= (MatcherList
*)data
;
1681 gboolean all_done
= TRUE
;
1684 for (l
= matchers
->matchers
; l
!= NULL
; l
= g_slist_next(l
)) {
1685 MatcherProp
*matcher
= (MatcherProp
*) l
->data
;
1690 /* if the criteria is ~body_part or ~message, ZERO lines
1691 * must match for the rule to match.
1693 if (matcher
->criteria
== MATCHCRITERIA_NOT_BODY_PART
||
1694 matcher
->criteria
== MATCHCRITERIA_NOT_MESSAGE
) {
1695 if (matcherprop_string_match(matcher
, buf
,
1696 context_str
[CONTEXT_BODY_LINE
])) {
1697 matcher
->result
= FALSE
;
1698 matcher
->done
= TRUE
;
1700 matcher
->result
= TRUE
;
1701 /* else, just one line has to match */
1702 } else if (matcherprop_criteria_body(matcher
) ||
1703 matcherprop_criteria_message(matcher
)) {
1704 if (matcherprop_string_match(matcher
, buf
,
1705 context_str
[CONTEXT_BODY_LINE
])) {
1706 matcher
->result
= TRUE
;
1707 matcher
->done
= TRUE
;
1711 /* if the matchers are OR'ed and the rule matched,
1712 * no need to check the others. */
1713 if (matcher
->result
&& matcher
->done
) {
1714 if (!matchers
->bool_and
) {
1725 static gboolean
matcherlist_match_text_content(MatcherList
*matchers
, MimeInfo
*partinfo
)
1727 if (partinfo
->type
!= MIMETYPE_TEXT
)
1730 return procmime_scan_text_content(partinfo
, match_content_cb
, matchers
);
1734 *\brief Check if a line in a message file's body matches
1737 *\param matchers List of conditions
1738 *\param fp Message file
1740 *\return gboolean TRUE if successful match
1742 static gboolean
matcherlist_match_body(MatcherList
*matchers
, gboolean body_only
, MsgInfo
*info
)
1744 MimeInfo
*mimeinfo
= NULL
;
1745 MimeInfo
*partinfo
= NULL
;
1746 gboolean first_text_found
= FALSE
;
1748 cm_return_val_if_fail(info
!= NULL
, FALSE
);
1750 mimeinfo
= procmime_scan_message(info
);
1753 partinfo
= procmime_mimeinfo_next(mimeinfo
);
1755 for (; partinfo
!= NULL
; partinfo
= procmime_mimeinfo_next(partinfo
)) {
1757 if (partinfo
->type
!= MIMETYPE_TEXT
&& body_only
)
1760 if (partinfo
->type
== MIMETYPE_TEXT
) {
1761 first_text_found
= TRUE
;
1762 if (matcherlist_match_text_content(matchers
, partinfo
)) {
1763 procmime_mimeinfo_free_all(&mimeinfo
);
1766 } else if (matcherlist_match_binary_content(matchers
, partinfo
)) {
1767 procmime_mimeinfo_free_all(&mimeinfo
);
1771 if (body_only
&& first_text_found
)
1774 procmime_mimeinfo_free_all(&mimeinfo
);
1780 *\brief Check if a message file matches criteria
1782 *\param matchers Criteria
1783 *\param info Message info
1784 *\param result Default result
1786 *\return gboolean TRUE if matched
1788 static gboolean
matcherlist_match_file(MatcherList
*matchers
, MsgInfo
*info
,
1791 gboolean read_headers
;
1798 /* file need to be read ? */
1800 read_headers
= FALSE
;
1803 for (l
= matchers
->matchers
; l
!= NULL
; l
= g_slist_next(l
)) {
1804 MatcherProp
*matcher
= (MatcherProp
*) l
->data
;
1806 if (matcherprop_criteria_headers(matcher
))
1807 read_headers
= TRUE
;
1808 if (matcherprop_criteria_body(matcher
))
1810 if (matcherprop_criteria_message(matcher
)) {
1811 read_headers
= TRUE
;
1815 matcher
->result
= FALSE
;
1816 matcher
->done
= FALSE
;
1819 if (!read_headers
&& !read_body
)
1822 file
= procmsg_get_message_file_full(info
, read_headers
, read_body
);
1826 if ((fp
= claws_fopen(file
, "rb")) == NULL
) {
1827 FILE_OP_ERROR(file
, "claws_fopen");
1832 /* read the headers */
1835 if (matcherlist_match_headers(matchers
, fp
))
1838 procheader_skip_headers(fp
);
1843 matcherlist_match_body(matchers
, body_only
, info
);
1846 for (l
= matchers
->matchers
; l
!= NULL
; l
= g_slist_next(l
)) {
1847 MatcherProp
*matcher
= (MatcherProp
*) l
->data
;
1849 if (matcherprop_criteria_headers(matcher
) ||
1850 matcherprop_criteria_body(matcher
) ||
1851 matcherprop_criteria_message(matcher
)) {
1852 if (matcher
->result
) {
1853 if (!matchers
->bool_and
) {
1859 if (matchers
->bool_and
) {
1875 *\brief Test list of conditions on a message.
1877 *\param matchers List of conditions
1878 *\param info Message info
1880 *\return gboolean TRUE if matched
1882 gboolean
matcherlist_match(MatcherList
*matchers
, MsgInfo
*info
)
1890 if (matchers
->bool_and
)
1895 /* test the cached elements */
1897 for (l
= matchers
->matchers
; l
!= NULL
;l
= g_slist_next(l
)) {
1898 MatcherProp
*matcher
= (MatcherProp
*) l
->data
;
1900 if (debug_filtering_session
) {
1901 gchar
*buf
= matcherprop_to_string(matcher
);
1902 log_print(LOG_DEBUG_FILTERING
, _("checking if message matches [ %s ]\n"), buf
);
1906 switch(matcher
->criteria
) {
1907 case MATCHCRITERIA_ALL
:
1908 case MATCHCRITERIA_UNREAD
:
1909 case MATCHCRITERIA_NOT_UNREAD
:
1910 case MATCHCRITERIA_NEW
:
1911 case MATCHCRITERIA_NOT_NEW
:
1912 case MATCHCRITERIA_MARKED
:
1913 case MATCHCRITERIA_NOT_MARKED
:
1914 case MATCHCRITERIA_DELETED
:
1915 case MATCHCRITERIA_NOT_DELETED
:
1916 case MATCHCRITERIA_REPLIED
:
1917 case MATCHCRITERIA_NOT_REPLIED
:
1918 case MATCHCRITERIA_FORWARDED
:
1919 case MATCHCRITERIA_NOT_FORWARDED
:
1920 case MATCHCRITERIA_LOCKED
:
1921 case MATCHCRITERIA_NOT_LOCKED
:
1922 case MATCHCRITERIA_SPAM
:
1923 case MATCHCRITERIA_NOT_SPAM
:
1924 case MATCHCRITERIA_HAS_ATTACHMENT
:
1925 case MATCHCRITERIA_HAS_NO_ATTACHMENT
:
1926 case MATCHCRITERIA_SIGNED
:
1927 case MATCHCRITERIA_NOT_SIGNED
:
1928 case MATCHCRITERIA_COLORLABEL
:
1929 case MATCHCRITERIA_NOT_COLORLABEL
:
1930 case MATCHCRITERIA_IGNORE_THREAD
:
1931 case MATCHCRITERIA_NOT_IGNORE_THREAD
:
1932 case MATCHCRITERIA_WATCH_THREAD
:
1933 case MATCHCRITERIA_NOT_WATCH_THREAD
:
1934 case MATCHCRITERIA_SUBJECT
:
1935 case MATCHCRITERIA_NOT_SUBJECT
:
1936 case MATCHCRITERIA_FROM
:
1937 case MATCHCRITERIA_NOT_FROM
:
1938 case MATCHCRITERIA_TO
:
1939 case MATCHCRITERIA_NOT_TO
:
1940 case MATCHCRITERIA_CC
:
1941 case MATCHCRITERIA_NOT_CC
:
1942 case MATCHCRITERIA_TO_OR_CC
:
1943 case MATCHCRITERIA_NOT_TO_AND_NOT_CC
:
1944 case MATCHCRITERIA_TAG
:
1945 case MATCHCRITERIA_NOT_TAG
:
1946 case MATCHCRITERIA_TAGGED
:
1947 case MATCHCRITERIA_NOT_TAGGED
:
1948 case MATCHCRITERIA_AGE_GREATER
:
1949 case MATCHCRITERIA_AGE_LOWER
:
1950 case MATCHCRITERIA_AGE_GREATER_HOURS
:
1951 case MATCHCRITERIA_AGE_LOWER_HOURS
:
1952 case MATCHCRITERIA_DATE_AFTER
:
1953 case MATCHCRITERIA_DATE_BEFORE
:
1954 case MATCHCRITERIA_NEWSGROUPS
:
1955 case MATCHCRITERIA_NOT_NEWSGROUPS
:
1956 case MATCHCRITERIA_MESSAGEID
:
1957 case MATCHCRITERIA_NOT_MESSAGEID
:
1958 case MATCHCRITERIA_INREPLYTO
:
1959 case MATCHCRITERIA_NOT_INREPLYTO
:
1960 case MATCHCRITERIA_REFERENCES
:
1961 case MATCHCRITERIA_NOT_REFERENCES
:
1962 case MATCHCRITERIA_SCORE_GREATER
:
1963 case MATCHCRITERIA_SCORE_LOWER
:
1964 case MATCHCRITERIA_SCORE_EQUAL
:
1965 case MATCHCRITERIA_SIZE_GREATER
:
1966 case MATCHCRITERIA_SIZE_SMALLER
:
1967 case MATCHCRITERIA_SIZE_EQUAL
:
1968 case MATCHCRITERIA_TEST
:
1969 case MATCHCRITERIA_NOT_TEST
:
1970 case MATCHCRITERIA_PARTIAL
:
1971 case MATCHCRITERIA_NOT_PARTIAL
:
1972 if (matcherprop_match(matcher
, info
)) {
1973 if (!matchers
->bool_and
) {
1974 if (debug_filtering_session
)
1975 log_status_ok(LOG_DEBUG_FILTERING
, _("message matches\n"));
1980 if (matchers
->bool_and
) {
1981 if (debug_filtering_session
)
1982 log_status_nok(LOG_DEBUG_FILTERING
, _("message does not match\n"));
1989 /* test the condition on the file */
1991 if (matcherlist_match_file(matchers
, info
, result
)) {
1992 if (!matchers
->bool_and
) {
1993 if (debug_filtering_session
)
1994 log_status_ok(LOG_DEBUG_FILTERING
, _("message matches\n"));
1998 if (matchers
->bool_and
) {
1999 if (debug_filtering_session
)
2000 log_status_nok(LOG_DEBUG_FILTERING
, _("message does not match\n"));
2005 if (debug_filtering_session
) {
2007 log_status_ok(LOG_DEBUG_FILTERING
, _("message matches\n"));
2009 log_status_nok(LOG_DEBUG_FILTERING
, _("message does not match\n"));
2015 static gint
quote_filter_str(gchar
* result
, guint size
,
2025 for(p
= path
; * p
!= '\0' ; p
++) {
2027 if ((* p
!= '\"') && (* p
!= '\\')) {
2028 if (remaining
> 0) {
2034 result
[size
- 1] = '\0';
2039 if (remaining
>= 2) {
2047 result
[size
- 1] = '\0';
2052 if (remaining
> 0) {
2056 result
[size
- 1] = '\0';
2064 gchar
* matcher_quote_str(const gchar
* src
)
2069 len
= strlen(src
) * 2 + 1;
2070 res
= g_malloc(len
);
2071 quote_filter_str(res
, len
, src
);
2077 *\brief Convert a matcher structure to a string
2079 *\param matcher Matcher structure
2081 *\return gchar * Newly allocated string
2083 gchar
*matcherprop_to_string(MatcherProp
*matcher
)
2085 gchar
*matcher_str
= NULL
;
2086 const gchar
*criteria_str
;
2087 const gchar
*matchtype_str
;
2089 gchar
* quoted_expr
;
2090 gchar
* quoted_header
;
2092 criteria_str
= NULL
;
2093 for (i
= 0; i
< (int) (sizeof(matchparser_tab
) / sizeof(MatchParser
)); i
++) {
2094 if (matchparser_tab
[i
].id
== matcher
->criteria
)
2095 criteria_str
= matchparser_tab
[i
].str
;
2097 if (criteria_str
== NULL
)
2100 switch (matcher
->criteria
) {
2101 case MATCHCRITERIA_AGE_GREATER
:
2102 case MATCHCRITERIA_AGE_LOWER
:
2103 case MATCHCRITERIA_AGE_GREATER_HOURS
:
2104 case MATCHCRITERIA_AGE_LOWER_HOURS
:
2105 case MATCHCRITERIA_SCORE_GREATER
:
2106 case MATCHCRITERIA_SCORE_LOWER
:
2107 case MATCHCRITERIA_SCORE_EQUAL
:
2108 case MATCHCRITERIA_SIZE_GREATER
:
2109 case MATCHCRITERIA_SIZE_SMALLER
:
2110 case MATCHCRITERIA_SIZE_EQUAL
:
2111 case MATCHCRITERIA_COLORLABEL
:
2112 case MATCHCRITERIA_NOT_COLORLABEL
:
2113 return g_strdup_printf("%s %i", criteria_str
, matcher
->value
);
2114 case MATCHCRITERIA_ALL
:
2115 case MATCHCRITERIA_UNREAD
:
2116 case MATCHCRITERIA_NOT_UNREAD
:
2117 case MATCHCRITERIA_NEW
:
2118 case MATCHCRITERIA_NOT_NEW
:
2119 case MATCHCRITERIA_MARKED
:
2120 case MATCHCRITERIA_NOT_MARKED
:
2121 case MATCHCRITERIA_DELETED
:
2122 case MATCHCRITERIA_NOT_DELETED
:
2123 case MATCHCRITERIA_REPLIED
:
2124 case MATCHCRITERIA_NOT_REPLIED
:
2125 case MATCHCRITERIA_FORWARDED
:
2126 case MATCHCRITERIA_NOT_FORWARDED
:
2127 case MATCHCRITERIA_LOCKED
:
2128 case MATCHCRITERIA_NOT_LOCKED
:
2129 case MATCHCRITERIA_SPAM
:
2130 case MATCHCRITERIA_NOT_SPAM
:
2131 case MATCHCRITERIA_HAS_ATTACHMENT
:
2132 case MATCHCRITERIA_HAS_NO_ATTACHMENT
:
2133 case MATCHCRITERIA_SIGNED
:
2134 case MATCHCRITERIA_NOT_SIGNED
:
2135 case MATCHCRITERIA_PARTIAL
:
2136 case MATCHCRITERIA_NOT_PARTIAL
:
2137 case MATCHCRITERIA_IGNORE_THREAD
:
2138 case MATCHCRITERIA_NOT_IGNORE_THREAD
:
2139 case MATCHCRITERIA_WATCH_THREAD
:
2140 case MATCHCRITERIA_NOT_WATCH_THREAD
:
2141 case MATCHCRITERIA_TAGGED
:
2142 case MATCHCRITERIA_NOT_TAGGED
:
2143 return g_strdup(criteria_str
);
2144 case MATCHCRITERIA_TEST
:
2145 case MATCHCRITERIA_NOT_TEST
:
2146 case MATCHCRITERIA_DATE_AFTER
:
2147 case MATCHCRITERIA_DATE_BEFORE
:
2148 quoted_expr
= matcher_quote_str(matcher
->expr
);
2149 matcher_str
= g_strdup_printf("%s \"%s\"",
2150 criteria_str
, quoted_expr
);
2151 g_free(quoted_expr
);
2153 case MATCHCRITERIA_FOUND_IN_ADDRESSBOOK
:
2154 case MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK
:
2155 quoted_header
= matcher_quote_str(matcher
->header
);
2156 quoted_expr
= matcher_quote_str(matcher
->expr
);
2157 matcher_str
= g_strdup_printf("%s \"%s\" in \"%s\"",
2158 criteria_str
, quoted_header
, quoted_expr
);
2159 g_free(quoted_header
);
2160 g_free(quoted_expr
);
2164 matchtype_str
= NULL
;
2165 for (i
= 0; i
< sizeof matchparser_tab
/ sizeof matchparser_tab
[0]; i
++) {
2166 if (matchparser_tab
[i
].id
== matcher
->matchtype
)
2167 matchtype_str
= matchparser_tab
[i
].str
;
2170 if (matchtype_str
== NULL
)
2173 switch (matcher
->matchtype
) {
2174 case MATCHTYPE_MATCH
:
2175 case MATCHTYPE_MATCHCASE
:
2176 case MATCHTYPE_REGEXP
:
2177 case MATCHTYPE_REGEXPCASE
:
2178 quoted_expr
= matcher_quote_str(matcher
->expr
);
2179 if (matcher
->header
) {
2180 quoted_header
= matcher_quote_str(matcher
->header
);
2181 matcher_str
= g_strdup_printf
2182 ("%s \"%s\" %s \"%s\"",
2183 criteria_str
, quoted_header
,
2184 matchtype_str
, quoted_expr
);
2185 g_free(quoted_header
);
2188 matcher_str
= g_strdup_printf
2189 ("%s %s \"%s\"", criteria_str
,
2190 matchtype_str
, quoted_expr
);
2191 g_free(quoted_expr
);
2199 *\brief Convert a list of conditions to a string
2201 *\param matchers List of conditions
2203 *\return gchar * Newly allocated string
2205 gchar
*matcherlist_to_string(const MatcherList
*matchers
)
2211 gchar
*result
= NULL
;
2213 count
= g_slist_length(matchers
->matchers
);
2214 vstr
= g_new(gchar
*, count
+ 1);
2216 for (l
= matchers
->matchers
, cur_str
= vstr
; l
!= NULL
;
2217 l
= g_slist_next(l
), cur_str
++) {
2218 *cur_str
= matcherprop_to_string((MatcherProp
*) l
->data
);
2219 if (*cur_str
== NULL
)
2224 if (matchers
->bool_and
)
2225 result
= g_strjoinv(" & ", vstr
);
2227 result
= g_strjoinv(" | ", vstr
);
2229 for (cur_str
= vstr
; *cur_str
!= NULL
; cur_str
++)
2237 #define STRLEN_ZERO(s) ((s) ? strlen(s) : 0)
2238 #define STRLEN_DEFAULT(s,d) ((s) ? strlen(s) : STRLEN_ZERO(d))
2240 static void add_str_default(gchar
** dest
,
2241 const gchar
* s
, const gchar
* d
)
2243 gchar quoted_str
[4096];
2251 quote_cmd_argument(quoted_str
, sizeof(quoted_str
), str
);
2252 strcpy(* dest
, quoted_str
);
2254 (* dest
) += strlen(* dest
);
2257 /* matching_build_command() - preferably cmd should be unescaped */
2259 *\brief Build the command-line to execute
2261 *\param cmd String with command-line specifiers
2262 *\param info Message info to use for command
2264 *\return gchar * Newly allocated string
2266 gchar
*matching_build_command(const gchar
*cmd
, MsgInfo
*info
)
2268 const gchar
*s
= cmd
;
2269 gchar
*filename
= NULL
;
2270 gchar
*processed_cmd
;
2274 const gchar
*const no_subject
= _("(none)") ;
2275 const gchar
*const no_from
= _("(none)") ;
2276 const gchar
*const no_to
= _("(none)") ;
2277 const gchar
*const no_cc
= _("(none)") ;
2278 const gchar
*const no_date
= _("(none)") ;
2279 const gchar
*const no_msgid
= _("(none)") ;
2280 const gchar
*const no_newsgroups
= _("(none)") ;
2281 const gchar
*const no_references
= _("(none)") ;
2283 size
= STRLEN_ZERO(cmd
) + 1;
2284 while (*s
!= '\0') {
2291 case 's': /* subject */
2292 size
+= STRLEN_DEFAULT(info
->subject
, no_subject
) - 2;
2294 case 'f': /* from */
2295 size
+= STRLEN_DEFAULT(info
->from
, no_from
) - 2;
2298 size
+= STRLEN_DEFAULT(info
->to
, no_to
) - 2;
2301 size
+= STRLEN_DEFAULT(info
->cc
, no_cc
) - 2;
2303 case 'd': /* date */
2304 size
+= STRLEN_DEFAULT(info
->date
, no_date
) - 2;
2306 case 'i': /* message-id */
2307 size
+= STRLEN_DEFAULT(info
->msgid
, no_msgid
) - 2;
2309 case 'n': /* newsgroups */
2310 size
+= STRLEN_DEFAULT(info
->newsgroups
, no_newsgroups
) - 2;
2312 case 'r': /* references */
2313 /* FIXME: using the inreplyto header for reference */
2314 size
+= STRLEN_DEFAULT(info
->inreplyto
, no_references
) - 2;
2316 case 'F': /* file */
2317 if (filename
== NULL
)
2318 filename
= folder_item_fetch_msg(info
->folder
, info
->msgnum
);
2320 if (filename
== NULL
) {
2321 g_warning("filename is not set");
2325 size
+= strlen(filename
) - 2;
2334 /* as the string can be quoted, we double the result */
2337 processed_cmd
= g_new0(gchar
, size
);
2341 while (*s
!= '\0') {
2349 case 's': /* subject */
2350 add_str_default(&p
, info
->subject
,
2353 case 'f': /* from */
2354 add_str_default(&p
, info
->from
,
2358 add_str_default(&p
, info
->to
,
2362 add_str_default(&p
, info
->cc
,
2365 case 'd': /* date */
2366 add_str_default(&p
, info
->date
,
2369 case 'i': /* message-id */
2370 add_str_default(&p
, info
->msgid
,
2373 case 'n': /* newsgroups */
2374 add_str_default(&p
, info
->newsgroups
,
2377 case 'r': /* references */
2378 /* FIXME: using the inreplyto header for references */
2379 add_str_default(&p
, info
->inreplyto
, no_references
);
2381 case 'F': /* file */
2382 if (filename
!= NULL
)
2383 add_str_default(&p
, filename
, NULL
);
2402 return processed_cmd
;
2404 #undef STRLEN_DEFAULT
2407 /* ************************************************************ */
2411 *\brief Write filtering list to file
2414 *\param prefs_filtering List of filtering conditions
2416 static int prefs_filtering_write(FILE *fp
, GSList
*prefs_filtering
)
2420 for (cur
= prefs_filtering
; cur
!= NULL
; cur
= cur
->next
) {
2421 gchar
*filtering_str
= NULL
;
2422 gchar
*tmp_name
= NULL
;
2423 FilteringProp
*prop
= NULL
;
2425 if (NULL
== (prop
= (FilteringProp
*) cur
->data
))
2428 if (NULL
== (filtering_str
= filteringprop_to_string(prop
)))
2431 if (prop
->enabled
) {
2432 if (claws_fputs("enabled ", fp
) == EOF
) {
2433 FILE_OP_ERROR("filtering config", "claws_fputs");
2434 g_free(filtering_str
);
2438 if (claws_fputs("disabled ", fp
) == EOF
) {
2439 FILE_OP_ERROR("filtering config", "claws_fputs");
2440 g_free(filtering_str
);
2445 if (claws_fputs("rulename \"", fp
) == EOF
) {
2446 FILE_OP_ERROR("filtering config", "claws_fputs");
2447 g_free(filtering_str
);
2450 tmp_name
= prop
->name
;
2451 while (tmp_name
&& *tmp_name
!= '\0') {
2452 if (*tmp_name
!= '"') {
2453 if (claws_fputc(*tmp_name
, fp
) == EOF
) {
2454 FILE_OP_ERROR("filtering config", "claws_fputs || claws_fputc");
2455 g_free(filtering_str
);
2458 } else if (*tmp_name
== '"') {
2459 if (claws_fputc('\\', fp
) == EOF
||
2460 claws_fputc('"', fp
) == EOF
) {
2461 FILE_OP_ERROR("filtering config", "claws_fputs || claws_fputc");
2462 g_free(filtering_str
);
2468 if (claws_fputs("\" ", fp
) == EOF
) {
2469 FILE_OP_ERROR("filtering config", "claws_fputs");
2470 g_free(filtering_str
);
2474 if (prop
->account_id
!= 0) {
2477 tmp
= g_strdup_printf("account %d ", prop
->account_id
);
2478 if (claws_fputs(tmp
, fp
) == EOF
) {
2479 FILE_OP_ERROR("filtering config", "claws_fputs");
2480 g_free(filtering_str
);
2487 if(claws_fputs(filtering_str
, fp
) == EOF
||
2488 claws_fputc('\n', fp
) == EOF
) {
2489 FILE_OP_ERROR("filtering config", "claws_fputs || claws_fputc");
2490 g_free(filtering_str
);
2493 g_free(filtering_str
);
2499 typedef struct _NodeLoopData
{
2505 *\brief Write matchers from a folder item
2507 *\param node Node with folder info
2508 *\param data File pointer
2510 *\return gboolean FALSE
2512 static gboolean
prefs_matcher_write_func(GNode
*node
, gpointer d
)
2515 NodeLoopData
*data
= (NodeLoopData
*)d
;
2517 GSList
*prefs_filtering
;
2520 /* prevent warning */
2521 if (item
->path
== NULL
)
2523 id
= folder_item_get_identifier(item
);
2526 prefs_filtering
= item
->prefs
->processing
;
2528 if (prefs_filtering
!= NULL
) {
2529 if (fprintf(data
->fp
, "[%s]\n", id
) < 0) {
2533 if (prefs_filtering_write(data
->fp
, prefs_filtering
) < 0) {
2537 if (claws_fputc('\n', data
->fp
) == EOF
) {
2549 *\brief Save matchers from folder items
2553 static int prefs_matcher_save(FILE *fp
)
2561 for (cur
= folder_get_list() ; cur
!= NULL
; cur
= g_list_next(cur
)) {
2564 folder
= (Folder
*) cur
->data
;
2565 g_node_traverse(folder
->node
, G_PRE_ORDER
, G_TRAVERSE_ALL
, -1,
2566 prefs_matcher_write_func
, &data
);
2569 if (data
.error
== TRUE
)
2572 /* pre global rules */
2573 if (fprintf(fp
, "[preglobal]\n") < 0 ||
2574 prefs_filtering_write(fp
, pre_global_processing
) < 0 ||
2575 claws_fputc('\n', fp
) == EOF
)
2578 /* post global rules */
2579 if (fprintf(fp
, "[postglobal]\n") < 0 ||
2580 prefs_filtering_write(fp
, post_global_processing
) < 0 ||
2581 claws_fputc('\n', fp
) == EOF
)
2584 /* filtering rules */
2585 if (fprintf(fp
, "[filtering]\n") < 0 ||
2586 prefs_filtering_write(fp
, filtering_rules
) < 0 ||
2587 claws_fputc('\n', fp
) == EOF
)
2594 *\brief Write filtering / matcher configuration file
2596 void prefs_matcher_write_config(void)
2601 debug_print("Writing matcher configuration...\n");
2603 rcpath
= g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S
,
2606 if ((pfile
= prefs_write_open(rcpath
)) == NULL
) {
2607 g_warning("failed to write configuration to file");
2614 if (prefs_matcher_save(pfile
->fp
) < 0) {
2615 g_warning("failed to write configuration to file");
2616 prefs_file_close_revert(pfile
);
2617 } else if (prefs_file_close(pfile
) < 0) {
2618 g_warning("failed to save configuration to file");
2623 *\brief Read matcher configuration
2625 void prefs_matcher_read_config(void)
2630 create_matchparser_hashtab();
2631 prefs_filtering_clear();
2633 rcpath
= g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S
, MATCHER_RC
, NULL
);
2635 f
= claws_fopen(rcpath
, "rb");
2639 matcher_parser_start_parsing(f
);
2640 claws_fclose(matcher_parserin
);