2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 2002-2014 by the Claws Mail Team and Hiroyuki Yamamoto
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "claws-features.h"
26 #include <glib/gi18n.h>
38 #include "procheader.h"
40 #include "matcher_parser.h"
41 #include "prefs_gtk.h"
42 #include "addr_compl.h"
44 #include "quoted-printable.h"
47 #include "prefs_common.h"
50 #include "folder_item_prefs.h"
52 #include "file-utils.h"
55 *\brief Keyword lookup element
58 gint id
; /*!< keyword id */
59 gchar
*str
; /*!< keyword */
61 typedef struct _MatchParser MatchParser
;
64 *\brief Table with strings and ids used by the lexer and
65 * the parser. New keywords can be added here.
67 static const MatchParser matchparser_tab
[] = {
69 {MATCHCRITERIA_ALL
, "all"},
70 {MATCHCRITERIA_UNREAD
, "unread"},
71 {MATCHCRITERIA_NOT_UNREAD
, "~unread"},
72 {MATCHCRITERIA_NEW
, "new"},
73 {MATCHCRITERIA_NOT_NEW
, "~new"},
74 {MATCHCRITERIA_MARKED
, "marked"},
75 {MATCHCRITERIA_NOT_MARKED
, "~marked"},
76 {MATCHCRITERIA_DELETED
, "deleted"},
77 {MATCHCRITERIA_NOT_DELETED
, "~deleted"},
78 {MATCHCRITERIA_REPLIED
, "replied"},
79 {MATCHCRITERIA_NOT_REPLIED
, "~replied"},
80 {MATCHCRITERIA_FORWARDED
, "forwarded"},
81 {MATCHCRITERIA_NOT_FORWARDED
, "~forwarded"},
82 {MATCHCRITERIA_LOCKED
, "locked"},
83 {MATCHCRITERIA_NOT_LOCKED
, "~locked"},
84 {MATCHCRITERIA_COLORLABEL
, "colorlabel"},
85 {MATCHCRITERIA_NOT_COLORLABEL
, "~colorlabel"},
86 {MATCHCRITERIA_IGNORE_THREAD
, "ignore_thread"},
87 {MATCHCRITERIA_NOT_IGNORE_THREAD
, "~ignore_thread"},
88 {MATCHCRITERIA_WATCH_THREAD
, "watch_thread"},
89 {MATCHCRITERIA_NOT_WATCH_THREAD
, "~watch_thread"},
90 {MATCHCRITERIA_SPAM
, "spam"},
91 {MATCHCRITERIA_NOT_SPAM
, "~spam"},
92 {MATCHCRITERIA_HAS_ATTACHMENT
, "has_attachment"},
93 {MATCHCRITERIA_HAS_NO_ATTACHMENT
, "~has_attachment"},
94 {MATCHCRITERIA_SIGNED
, "signed"},
95 {MATCHCRITERIA_NOT_SIGNED
, "~signed"},
98 {MATCHCRITERIA_SUBJECT
, "subject"},
99 {MATCHCRITERIA_NOT_SUBJECT
, "~subject"},
100 {MATCHCRITERIA_FROM
, "from"},
101 {MATCHCRITERIA_NOT_FROM
, "~from"},
102 {MATCHCRITERIA_TO
, "to"},
103 {MATCHCRITERIA_NOT_TO
, "~to"},
104 {MATCHCRITERIA_CC
, "cc"},
105 {MATCHCRITERIA_NOT_CC
, "~cc"},
106 {MATCHCRITERIA_TO_OR_CC
, "to_or_cc"},
107 {MATCHCRITERIA_NOT_TO_AND_NOT_CC
, "~to_or_cc"},
108 {MATCHCRITERIA_TAG
, "tag"},
109 {MATCHCRITERIA_NOT_TAG
, "~tag"},
110 {MATCHCRITERIA_TAGGED
, "tagged"},
111 {MATCHCRITERIA_NOT_TAGGED
, "~tagged"},
112 {MATCHCRITERIA_AGE_GREATER
, "age_greater"},
113 {MATCHCRITERIA_AGE_LOWER
, "age_lower"},
114 {MATCHCRITERIA_AGE_GREATER_HOURS
, "age_greater_hours"},
115 {MATCHCRITERIA_AGE_LOWER_HOURS
, "age_lower_hours"},
116 {MATCHCRITERIA_DATE_AFTER
, "date_after"},
117 {MATCHCRITERIA_DATE_BEFORE
, "date_before"},
118 {MATCHCRITERIA_NEWSGROUPS
, "newsgroups"},
119 {MATCHCRITERIA_NOT_NEWSGROUPS
, "~newsgroups"},
120 {MATCHCRITERIA_MESSAGEID
, "messageid"},
121 {MATCHCRITERIA_NOT_MESSAGEID
, "~messageid"},
122 {MATCHCRITERIA_INREPLYTO
, "inreplyto"},
123 {MATCHCRITERIA_NOT_INREPLYTO
, "~inreplyto"},
124 {MATCHCRITERIA_REFERENCES
, "references"},
125 {MATCHCRITERIA_NOT_REFERENCES
, "~references"},
126 {MATCHCRITERIA_SCORE_GREATER
, "score_greater"},
127 {MATCHCRITERIA_SCORE_LOWER
, "score_lower"},
128 {MATCHCRITERIA_SCORE_EQUAL
, "score_equal"},
129 {MATCHCRITERIA_PARTIAL
, "partial"},
130 {MATCHCRITERIA_NOT_PARTIAL
, "~partial"},
131 {MATCHCRITERIA_FOUND_IN_ADDRESSBOOK
, "found_in_addressbook"},
132 {MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK
, "~found_in_addressbook"},
134 {MATCHCRITERIA_SIZE_GREATER
, "size_greater"},
135 {MATCHCRITERIA_SIZE_SMALLER
, "size_smaller"},
136 {MATCHCRITERIA_SIZE_EQUAL
, "size_equal"},
138 /* content have to be read */
139 {MATCHCRITERIA_HEADER
, "header"},
140 {MATCHCRITERIA_NOT_HEADER
, "~header"},
141 {MATCHCRITERIA_HEADERS_PART
, "headers_part"},
142 {MATCHCRITERIA_NOT_HEADERS_PART
, "~headers_part"},
143 {MATCHCRITERIA_HEADERS_CONT
, "headers_cont"},
144 {MATCHCRITERIA_NOT_HEADERS_CONT
, "~headers_cont"},
145 {MATCHCRITERIA_MESSAGE
, "message"},
146 {MATCHCRITERIA_NOT_MESSAGE
, "~message"},
147 {MATCHCRITERIA_BODY_PART
, "body_part"},
148 {MATCHCRITERIA_NOT_BODY_PART
, "~body_part"},
149 {MATCHCRITERIA_TEST
, "test"},
150 {MATCHCRITERIA_NOT_TEST
, "~test"},
153 {MATCHTYPE_MATCHCASE
, "matchcase"},
154 {MATCHTYPE_MATCH
, "match"},
155 {MATCHTYPE_REGEXPCASE
, "regexpcase"},
156 {MATCHTYPE_REGEXP
, "regexp"},
159 {MATCHACTION_SCORE
, "score"}, /* for backward compatibility */
160 {MATCHACTION_MOVE
, "move"},
161 {MATCHACTION_COPY
, "copy"},
162 {MATCHACTION_DELETE
, "delete"},
163 {MATCHACTION_MARK
, "mark"},
164 {MATCHACTION_UNMARK
, "unmark"},
165 {MATCHACTION_LOCK
, "lock"},
166 {MATCHACTION_UNLOCK
, "unlock"},
167 {MATCHACTION_MARK_AS_READ
, "mark_as_read"},
168 {MATCHACTION_MARK_AS_UNREAD
, "mark_as_unread"},
169 {MATCHACTION_MARK_AS_SPAM
, "mark_as_spam"},
170 {MATCHACTION_MARK_AS_HAM
, "mark_as_ham"},
171 {MATCHACTION_FORWARD
, "forward"},
172 {MATCHACTION_FORWARD_AS_ATTACHMENT
, "forward_as_attachment"},
173 {MATCHACTION_EXECUTE
, "execute"},
174 {MATCHACTION_COLOR
, "color"},
175 {MATCHACTION_REDIRECT
, "redirect"},
176 {MATCHACTION_CHANGE_SCORE
, "change_score"},
177 {MATCHACTION_SET_SCORE
, "set_score"},
178 {MATCHACTION_STOP
, "stop"},
179 {MATCHACTION_HIDE
, "hide"},
180 {MATCHACTION_IGNORE
, "ignore"},
181 {MATCHACTION_WATCH
, "watch"},
182 {MATCHACTION_ADD_TO_ADDRESSBOOK
, "add_to_addressbook"},
183 {MATCHACTION_SET_TAG
, "set_tag"},
184 {MATCHACTION_UNSET_TAG
, "unset_tag"},
185 {MATCHACTION_CLEAR_TAGS
, "clear_tags"},
210 static gchar
*context_str
[N_CONTEXT_STRS
];
212 void matcher_init(void)
214 if (context_str
[CONTEXT_SUBJECT
] != NULL
)
217 context_str
[CONTEXT_SUBJECT
] = g_strdup_printf(_("%s header"), prefs_common_translated_header_name("Subject:"));
218 context_str
[CONTEXT_FROM
] = g_strdup_printf(_("%s header"), prefs_common_translated_header_name("From:"));
219 context_str
[CONTEXT_TO
] = g_strdup_printf(_("%s header"), prefs_common_translated_header_name("To:"));
220 context_str
[CONTEXT_CC
] = g_strdup_printf(_("%s header"), prefs_common_translated_header_name("Cc:"));
221 context_str
[CONTEXT_NEWSGROUPS
] = g_strdup_printf(_("%s header"), prefs_common_translated_header_name("Newsgroups:"));
222 context_str
[CONTEXT_MESSAGEID
] = g_strdup_printf(_("%s header"), prefs_common_translated_header_name("Message-ID:"));
223 context_str
[CONTEXT_IN_REPLY_TO
] = g_strdup_printf(_("%s header"), prefs_common_translated_header_name("In-Reply-To:"));
224 context_str
[CONTEXT_REFERENCES
] = g_strdup_printf(_("%s header"), prefs_common_translated_header_name("References:"));
225 context_str
[CONTEXT_HEADER
] = g_strdup(_("header"));
226 context_str
[CONTEXT_HEADER_LINE
] = g_strdup(_("header line"));
227 context_str
[CONTEXT_BODY_LINE
] = g_strdup(_("body line"));
228 context_str
[CONTEXT_TAG
] = g_strdup(_("tag"));
231 void matcher_done(void)
234 for (i
= 0; i
< N_CONTEXT_STRS
; i
++) {
235 g_free(context_str
[i
]);
236 context_str
[i
] = NULL
;
240 extern gboolean debug_filtering_session
;
243 *\brief Look up table with keywords defined in \sa matchparser_tab
245 static GHashTable
*matchparser_hashtab
;
248 *\brief Translate keyword id to keyword string
250 *\param id Id of keyword
252 *\return const gchar * Keyword
254 const gchar
*get_matchparser_tab_str(gint id
)
258 for (i
= 0; i
< sizeof matchparser_tab
/ sizeof matchparser_tab
[0]; i
++) {
259 if (matchparser_tab
[i
].id
== id
)
260 return matchparser_tab
[i
].str
;
266 *\brief Create keyword lookup table
268 static void create_matchparser_hashtab(void)
272 if (matchparser_hashtab
) return;
273 matchparser_hashtab
= g_hash_table_new(g_str_hash
, g_str_equal
);
274 for (i
= 0; i
< sizeof matchparser_tab
/ sizeof matchparser_tab
[0]; i
++)
275 g_hash_table_insert(matchparser_hashtab
,
276 matchparser_tab
[i
].str
,
277 (gpointer
) &matchparser_tab
[i
]);
281 *\brief Return a keyword id from a keyword string
283 *\param str Keyword string
285 *\return gint Keyword id
287 gint
get_matchparser_tab_id(const gchar
*str
)
291 if (NULL
!= (res
= g_hash_table_lookup(matchparser_hashtab
, str
))) {
297 /* **************** data structure allocation **************** */
300 *\brief Allocate a structure for a filtering / scoring
301 * "condition" (a matcher structure)
303 *\param criteria Criteria ID (MATCHCRITERIA_XXXX)
304 *\param header Header string (if criteria is MATCHCRITERIA_HEADER
305 or MATCHCRITERIA_FOUND_IN_ADDRESSBOOK)
306 *\param matchtype Type of action (MATCHTYPE_XXX)
307 *\param expr String value or expression to check
308 *\param value Integer value to check
310 *\return MatcherProp * Pointer to newly allocated structure
312 MatcherProp
*matcherprop_new(gint criteria
, const gchar
*header
,
313 gint matchtype
, const gchar
*expr
,
318 prop
= g_new0(MatcherProp
, 1);
319 prop
->criteria
= criteria
;
320 prop
->header
= header
!= NULL
? g_strdup(header
) : NULL
;
322 prop
->expr
= expr
!= NULL
? g_strdup(expr
) : NULL
;
324 prop
->matchtype
= matchtype
;
328 prop
->casefold_expr
= NULL
;
336 *\brief Free a matcher structure
338 *\param prop Pointer to matcher structure allocated with
341 void matcherprop_free(MatcherProp
*prop
)
344 g_free(prop
->header
);
346 if (prop
->preg
!= NULL
) {
350 g_free(prop
->casefold_expr
);
356 *\brief Copy a matcher structure
358 *\param src Matcher structure to copy
360 *\return MatcherProp * Pointer to newly allocated matcher structure
362 MatcherProp
*matcherprop_copy(const MatcherProp
*src
)
364 MatcherProp
*prop
= g_new0(MatcherProp
, 1);
366 prop
->criteria
= src
->criteria
;
367 prop
->header
= src
->header
? g_strdup(src
->header
) : NULL
;
368 prop
->expr
= src
->expr
? g_strdup(src
->expr
) : NULL
;
369 prop
->matchtype
= src
->matchtype
;
372 prop
->preg
= NULL
; /* will be re-evaluated */
374 prop
->casefold_expr
= src
->casefold_expr
? g_strdup(src
->casefold_expr
) : NULL
;
375 prop
->value
= src
->value
;
376 prop
->error
= src
->error
;
380 /* ************** match ******************************/
382 static gboolean match_with_addresses_in_addressbook
383 (MatcherProp
*prop
, GSList
*address_list
, gint type
,
384 gchar
* folderpath
, gint match
)
387 gboolean found
= FALSE
;
390 cm_return_val_if_fail(address_list
!= NULL
, FALSE
);
392 debug_print("match_with_addresses_in_addressbook(%d, %s)\n",
393 g_slist_length(address_list
), folderpath
?folderpath
:"(null)");
395 if (folderpath
== NULL
||
396 strcasecmp(folderpath
, "Any") == 0 ||
402 start_address_completion(path
);
404 for (walk
= address_list
; walk
!= NULL
; walk
= walk
->next
) {
405 /* exact matching of email address */
406 guint num_addr
= complete_address(walk
->data
);
409 /* skip first item (this is the search string itself) */
411 for (; i
< num_addr
&& !found
; i
++) {
412 gchar
*addr
= get_complete_address(i
);
413 extract_address(addr
);
414 if (strcasecmp(addr
, walk
->data
) == 0) {
418 if (debug_filtering_session
419 && prefs_common
.filtering_debug_level
>= FILTERING_DEBUG_LEVEL_HIGH
) {
420 log_print(LOG_DEBUG_FILTERING
,
421 "address [ %s ] matches\n",
422 (gchar
*)walk
->data
);
429 if (debug_filtering_session
430 && prefs_common
.filtering_debug_level
>= FILTERING_DEBUG_LEVEL_HIGH
432 log_print(LOG_DEBUG_FILTERING
,
433 "address [ %s ] does NOT match\n",
434 (gchar
*)walk
->data
);
438 if (match
== MATCH_ALL
) {
439 /* if matching all addresses, stop if one doesn't match */
442 if (debug_filtering_session
443 && prefs_common
.filtering_debug_level
>= FILTERING_DEBUG_LEVEL_HIGH
) {
444 log_print(LOG_DEBUG_FILTERING
,
445 "not all address match (matching all)\n");
449 } else if (match
== MATCH_ANY
) {
450 /* if matching any address, stop if one does match */
453 if (debug_filtering_session
454 && prefs_common
.filtering_debug_level
>= FILTERING_DEBUG_LEVEL_HIGH
) {
455 log_print(LOG_DEBUG_FILTERING
,
456 "at least one address matches (matching any)\n");
461 /* MATCH_ONE: there should be only one loop iteration */
464 end_address_completion();
470 *\brief Find out if a string matches a condition
472 *\param prop Matcher structure
473 *\param str String to check
475 *\return gboolean TRUE if str matches the condition in the
478 static gboolean
matcherprop_string_match(MatcherProp
*prop
, const gchar
*str
,
479 const gchar
*debug_context
)
482 const gchar
*down_expr
;
483 gboolean ret
= FALSE
;
484 gboolean should_free
= FALSE
;
488 if (prop
->matchtype
== MATCHTYPE_REGEXPCASE
||
489 prop
->matchtype
== MATCHTYPE_MATCHCASE
) {
490 str1
= g_utf8_casefold(str
, -1);
491 if (!prop
->casefold_expr
) {
492 prop
->casefold_expr
= g_utf8_casefold(prop
->expr
, -1);
494 down_expr
= prop
->casefold_expr
;
499 down_expr
= (gchar
*)prop
->expr
;
503 switch (prop
->matchtype
) {
504 case MATCHTYPE_REGEXPCASE
:
505 case MATCHTYPE_REGEXP
:
506 if (!prop
->preg
&& (prop
->error
== 0)) {
507 prop
->preg
= g_new0(regex_t
, 1);
508 /* if regexp then don't use the escaped string */
509 if (regcomp(prop
->preg
, down_expr
,
510 REG_NOSUB
| REG_EXTENDED
511 | ((prop
->matchtype
== MATCHTYPE_REGEXPCASE
)
512 ? REG_ICASE
: 0)) != 0) {
518 if (prop
->preg
== NULL
) {
523 if (regexec(prop
->preg
, str1
, 0, NULL
, 0) == 0)
529 if (debug_filtering_session
530 && prefs_common
.filtering_debug_level
>= FILTERING_DEBUG_LEVEL_HIGH
) {
531 gchar
*stripped
= g_strdup(str
);
533 strretchomp(stripped
);
535 log_print(LOG_DEBUG_FILTERING
,
536 "%s value [ %s ] matches regular expression [ %s ] (%s)\n",
537 debug_context
, stripped
, prop
->expr
,
538 prop
->matchtype
== MATCHTYPE_REGEXP
? _("Case sensitive"):_("Case insensitive"));
540 log_print(LOG_DEBUG_FILTERING
,
541 "%s value [ %s ] does NOT match regular expression [ %s ] (%s)\n",
542 debug_context
, stripped
, prop
->expr
,
543 prop
->matchtype
== MATCHTYPE_REGEXP
? _("Case sensitive"):_("Case insensitive"));
548 case MATCHTYPE_MATCHCASE
:
549 case MATCHTYPE_MATCH
:
550 ret
= (strstr(str1
, down_expr
) != NULL
);
553 if (debug_filtering_session
554 && prefs_common
.filtering_debug_level
>= FILTERING_DEBUG_LEVEL_HIGH
) {
555 gchar
*stripped
= g_strdup(str
);
557 strretchomp(stripped
);
559 log_print(LOG_DEBUG_FILTERING
,
560 "%s value [ %s ] contains [ %s ] (%s)\n",
561 debug_context
, stripped
, prop
->expr
,
562 prop
->matchtype
== MATCHTYPE_MATCH
? _("Case sensitive"):_("Case insensitive"));
564 log_print(LOG_DEBUG_FILTERING
,
565 "%s value [ %s ] does NOT contain [ %s ] (%s)\n",
566 debug_context
, stripped
, prop
->expr
,
567 prop
->matchtype
== MATCHTYPE_MATCH
? _("Case sensitive"):_("Case insensitive"));
585 *\brief Find out if a tag matches a condition
587 *\param prop Matcher structure
588 *\param msginfo message to check
590 *\return gboolean TRUE if msginfo matches the condition in the
593 static gboolean
matcherprop_tag_match(MatcherProp
*prop
, MsgInfo
*msginfo
,
594 const gchar
*debug_context
)
596 gboolean ret
= FALSE
;
599 if (msginfo
== NULL
|| msginfo
->tags
== NULL
)
602 for (cur
= msginfo
->tags
; cur
; cur
= cur
->next
) {
603 const gchar
*str
= tags_get_tag(GPOINTER_TO_INT(cur
->data
));
606 if (matcherprop_string_match(prop
, str
, debug_context
)) {
615 *\brief Find out if the string-ed list matches a condition
617 *\param prop Matcher structure
618 *\param list GSList of strings to check
620 *\return gboolean TRUE if str matches the condition in the
623 static gboolean
matcherprop_list_match(MatcherProp
*prop
, const GSList
*list
,
624 const gchar
*debug_context
)
628 for(cur
= list
; cur
!= NULL
; cur
= cur
->next
) {
629 if (matcherprop_string_match(prop
, (gchar
*)cur
->data
, debug_context
))
635 static gboolean
matcherprop_header_line_match(MatcherProp
*prop
, const gchar
*hdr
,
636 const gchar
*str
, const gboolean both
,
637 const gchar
*debug_context
)
639 gboolean res
= FALSE
;
641 if (hdr
== NULL
|| str
== NULL
)
645 /* Search in all header names and content.
647 gchar
*line
= g_strdup_printf("%s %s", hdr
, str
);
648 res
= matcherprop_string_match(prop
, line
, debug_context
);
651 /* Search only in content and exclude private headers.
652 * E.g.: searching for "H foo" in folder x/foo would return
653 * *all* mail as SCF and RMID will match.
654 * Searching for "H sent" would return all resent messages
655 * as "Resent-From: whatever" will match.
657 if (procheader_header_is_internal(hdr
))
659 res
= matcherprop_string_match(prop
, str
, debug_context
);
666 typedef struct _thread_data
{
673 static void *matcher_test_thread(void *data
)
675 thread_data
*td
= (thread_data
*)data
;
678 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE
, NULL
);
679 pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS
, NULL
);
681 result
= system(td
->cmd
);
682 td
->done
= TRUE
; /* let the caller thread join() */
683 return GINT_TO_POINTER(result
);
688 *\brief Execute a command defined in the matcher structure
690 *\param prop Pointer to matcher structure
691 *\param info Pointer to message info structure
693 *\return gboolean TRUE if command was executed successfully
695 static gboolean
matcherprop_match_test(const MatcherProp
*prop
,
703 thread_data
*td
= g_new0(thread_data
, 1);
705 time_t start_time
= time(NULL
);
708 file
= procmsg_get_message_file(info
);
717 cmd
= matching_build_command(prop
->expr
, info
);
727 if (debug_filtering_session
728 && prefs_common
.filtering_debug_level
>= FILTERING_DEBUG_LEVEL_HIGH
) {
729 log_print(LOG_DEBUG_FILTERING
,
730 "starting threaded command [ %s ]\n",
736 if (pthread_create(&pt
, NULL
, matcher_test_thread
, td
) != 0)
737 retval
= system(cmd
);
739 debug_print("waiting for test thread\n");
741 /* don't let the interface freeze while waiting */
742 if (time(NULL
) - start_time
> 0) {
745 if (time(NULL
) - start_time
> 30) {
751 pthread_join(pt
, &res
);
752 retval
= GPOINTER_TO_INT(res
);
753 debug_print(" test thread returned %d\n", retval
);
758 if (debug_filtering_session
759 && prefs_common
.filtering_debug_level
>= FILTERING_DEBUG_LEVEL_HIGH
) {
760 log_print(LOG_DEBUG_FILTERING
,
761 "starting synchronous command [ %s ]\n",
765 retval
= system(cmd
);
767 debug_print("Command exit code: %d\n", retval
);
770 if (debug_filtering_session
771 && prefs_common
.filtering_debug_level
>= FILTERING_DEBUG_LEVEL_HIGH
) {
772 log_print(LOG_DEBUG_FILTERING
,
773 "command returned [ %d ]\n",
778 return (retval
== 0);
782 *\brief Check if a message matches the condition in a matcher
785 *\param prop Pointer to matcher structure
786 *\param info Pointer to message info
788 *\return gboolean TRUE if a match
790 static gboolean
matcherprop_match(MatcherProp
*prop
,
794 gint age_mult_hours
= 1;
796 switch(prop
->criteria
) {
797 case MATCHCRITERIA_ALL
:
799 case MATCHCRITERIA_UNREAD
:
800 return MSG_IS_UNREAD(info
->flags
);
801 case MATCHCRITERIA_NOT_UNREAD
:
802 return !MSG_IS_UNREAD(info
->flags
);
803 case MATCHCRITERIA_NEW
:
804 return MSG_IS_NEW(info
->flags
);
805 case MATCHCRITERIA_NOT_NEW
:
806 return !MSG_IS_NEW(info
->flags
);
807 case MATCHCRITERIA_MARKED
:
808 return MSG_IS_MARKED(info
->flags
);
809 case MATCHCRITERIA_NOT_MARKED
:
810 return !MSG_IS_MARKED(info
->flags
);
811 case MATCHCRITERIA_DELETED
:
812 return MSG_IS_DELETED(info
->flags
);
813 case MATCHCRITERIA_NOT_DELETED
:
814 return !MSG_IS_DELETED(info
->flags
);
815 case MATCHCRITERIA_REPLIED
:
816 return MSG_IS_REPLIED(info
->flags
);
817 case MATCHCRITERIA_NOT_REPLIED
:
818 return !MSG_IS_REPLIED(info
->flags
);
819 case MATCHCRITERIA_FORWARDED
:
820 return MSG_IS_FORWARDED(info
->flags
);
821 case MATCHCRITERIA_NOT_FORWARDED
:
822 return !MSG_IS_FORWARDED(info
->flags
);
823 case MATCHCRITERIA_LOCKED
:
824 return MSG_IS_LOCKED(info
->flags
);
825 case MATCHCRITERIA_NOT_LOCKED
:
826 return !MSG_IS_LOCKED(info
->flags
);
827 case MATCHCRITERIA_SPAM
:
828 return MSG_IS_SPAM(info
->flags
);
829 case MATCHCRITERIA_NOT_SPAM
:
830 return !MSG_IS_SPAM(info
->flags
);
831 case MATCHCRITERIA_HAS_ATTACHMENT
:
832 return MSG_IS_WITH_ATTACHMENT(info
->flags
);
833 case MATCHCRITERIA_HAS_NO_ATTACHMENT
:
834 return !MSG_IS_WITH_ATTACHMENT(info
->flags
);
835 case MATCHCRITERIA_SIGNED
:
836 return MSG_IS_SIGNED(info
->flags
);
837 case MATCHCRITERIA_NOT_SIGNED
:
838 return !MSG_IS_SIGNED(info
->flags
);
839 case MATCHCRITERIA_COLORLABEL
:
841 gint color
= MSG_GET_COLORLABEL_VALUE(info
->flags
);
842 gboolean ret
= (color
== prop
->value
);
845 if (debug_filtering_session
846 && prefs_common
.filtering_debug_level
>= FILTERING_DEBUG_LEVEL_HIGH
) {
848 log_print(LOG_DEBUG_FILTERING
,
849 "message color value [ %d ] matches color value [ %d ]\n",
852 log_print(LOG_DEBUG_FILTERING
,
853 "message color value [ %d ] does NOT match color value [ %d ]\n",
859 case MATCHCRITERIA_NOT_COLORLABEL
:
861 gint color
= MSG_GET_COLORLABEL_VALUE(info
->flags
);
862 gboolean ret
= (color
!= prop
->value
);
865 if (debug_filtering_session
866 && prefs_common
.filtering_debug_level
>= FILTERING_DEBUG_LEVEL_HIGH
) {
868 log_print(LOG_DEBUG_FILTERING
,
869 "message color value [ %d ] matches color value [ %d ]\n",
872 log_print(LOG_DEBUG_FILTERING
,
873 "message color value [ %d ] does NOT match color value [ %d ]\n",
879 case MATCHCRITERIA_IGNORE_THREAD
:
880 return MSG_IS_IGNORE_THREAD(info
->flags
);
881 case MATCHCRITERIA_NOT_IGNORE_THREAD
:
882 return !MSG_IS_IGNORE_THREAD(info
->flags
);
883 case MATCHCRITERIA_WATCH_THREAD
:
884 return MSG_IS_WATCH_THREAD(info
->flags
);
885 case MATCHCRITERIA_NOT_WATCH_THREAD
:
886 return !MSG_IS_WATCH_THREAD(info
->flags
);
887 case MATCHCRITERIA_SUBJECT
:
888 return matcherprop_string_match(prop
, info
->subject
, context_str
[CONTEXT_SUBJECT
]);
889 case MATCHCRITERIA_NOT_SUBJECT
:
890 return !matcherprop_string_match(prop
, info
->subject
, context_str
[CONTEXT_SUBJECT
]);
891 case MATCHCRITERIA_FROM
:
892 return matcherprop_string_match(prop
, info
->from
, context_str
[CONTEXT_FROM
]);
893 case MATCHCRITERIA_NOT_FROM
:
894 return !matcherprop_string_match(prop
, info
->from
, context_str
[CONTEXT_FROM
]);
895 case MATCHCRITERIA_TO
:
896 return matcherprop_string_match(prop
, info
->to
, context_str
[CONTEXT_TO
]);
897 case MATCHCRITERIA_NOT_TO
:
898 return !matcherprop_string_match(prop
, info
->to
, context_str
[CONTEXT_TO
]);
899 case MATCHCRITERIA_CC
:
900 return matcherprop_string_match(prop
, info
->cc
, context_str
[CONTEXT_CC
]);
901 case MATCHCRITERIA_NOT_CC
:
902 return !matcherprop_string_match(prop
, info
->cc
, context_str
[CONTEXT_CC
]);
903 case MATCHCRITERIA_TO_OR_CC
:
904 return matcherprop_string_match(prop
, info
->to
, context_str
[CONTEXT_TO
])
905 || matcherprop_string_match(prop
, info
->cc
, context_str
[CONTEXT_CC
]);
906 case MATCHCRITERIA_NOT_TO_AND_NOT_CC
:
907 return !matcherprop_string_match(prop
, info
->to
, context_str
[CONTEXT_TO
])
908 && !matcherprop_string_match(prop
, info
->cc
, context_str
[CONTEXT_CC
]);
909 case MATCHCRITERIA_TAG
:
910 return matcherprop_tag_match(prop
, info
, context_str
[CONTEXT_TAG
]);
911 case MATCHCRITERIA_NOT_TAG
:
912 return !matcherprop_tag_match(prop
, info
, context_str
[CONTEXT_TAG
]);
913 case MATCHCRITERIA_TAGGED
:
914 return info
->tags
!= NULL
;
915 case MATCHCRITERIA_NOT_TAGGED
:
916 return info
->tags
== NULL
;
917 case MATCHCRITERIA_AGE_GREATER
:
919 /* Fallthrough intended */
920 case MATCHCRITERIA_AGE_GREATER_HOURS
:
926 age
= ((t
- info
->date_t
) / (60 * 60 * age_mult_hours
));
927 ret
= (age
>= prop
->value
);
930 if (debug_filtering_session
931 && prefs_common
.filtering_debug_level
>= FILTERING_DEBUG_LEVEL_HIGH
) {
933 log_print(LOG_DEBUG_FILTERING
,
934 "message age [ %d ] is greater than [ %d ]\n",
937 log_print(LOG_DEBUG_FILTERING
,
938 "message age [ %d ] is not greater than [ %d ]\n",
944 case MATCHCRITERIA_DATE_AFTER
:
948 ret
= prop
->value
< info
->date_t
;
951 if (debug_filtering_session
952 && prefs_common
.filtering_debug_level
>= FILTERING_DEBUG_LEVEL_HIGH
) {
954 log_print(LOG_DEBUG_FILTERING
,
955 "message date [ %"G_GSIZE_FORMAT
" ] is after [ %d ]\n",
956 info
->date_t
, prop
->value
);
958 log_print(LOG_DEBUG_FILTERING
,
959 "message date [ %ld ] is not after [ %d ]\n",
960 info
->date_t
, prop
->value
);
965 case MATCHCRITERIA_AGE_LOWER
:
967 /* Fallthrough intended */
968 case MATCHCRITERIA_AGE_LOWER_HOURS
:
974 age
= ((t
- info
->date_t
) / (60 * 60 * age_mult_hours
));
975 ret
= (age
< prop
->value
);
978 if (debug_filtering_session
979 && prefs_common
.filtering_debug_level
>= FILTERING_DEBUG_LEVEL_HIGH
) {
981 log_print(LOG_DEBUG_FILTERING
,
982 "message age [ %d ] is lower than [ %d ]\n",
985 log_print(LOG_DEBUG_FILTERING
,
986 "message age [ %d ] is not lower than [ %d ]\n",
992 case MATCHCRITERIA_DATE_BEFORE
:
996 ret
= prop
->value
> info
->date_t
;
999 if (debug_filtering_session
1000 && prefs_common
.filtering_debug_level
>= FILTERING_DEBUG_LEVEL_HIGH
) {
1002 log_print(LOG_DEBUG_FILTERING
,
1003 "message date [ %ld ] is before [ %d ]\n",
1004 info
->date_t
, prop
->value
);
1006 log_print(LOG_DEBUG_FILTERING
,
1007 "message date [ %ld ] is not before [ %d ]\n",
1008 info
->date_t
, prop
->value
);
1013 case MATCHCRITERIA_SCORE_GREATER
:
1015 gboolean ret
= (info
->score
> prop
->value
);
1018 if (debug_filtering_session
1019 && prefs_common
.filtering_debug_level
>= FILTERING_DEBUG_LEVEL_HIGH
) {
1021 log_print(LOG_DEBUG_FILTERING
,
1022 "message score [ %d ] is greater than [ %d ]\n",
1023 info
->score
, prop
->value
);
1025 log_print(LOG_DEBUG_FILTERING
,
1026 "message score [ %d ] is not greater than [ %d ]\n",
1027 info
->score
, prop
->value
);
1032 case MATCHCRITERIA_SCORE_LOWER
:
1034 gboolean ret
= (info
->score
< prop
->value
);
1037 if (debug_filtering_session
1038 && prefs_common
.filtering_debug_level
>= FILTERING_DEBUG_LEVEL_HIGH
) {
1040 log_print(LOG_DEBUG_FILTERING
,
1041 "message score [ %d ] is lower than [ %d ]\n",
1042 info
->score
, prop
->value
);
1044 log_print(LOG_DEBUG_FILTERING
,
1045 "message score [ %d ] is not lower than [ %d ]\n",
1046 info
->score
, prop
->value
);
1051 case MATCHCRITERIA_SCORE_EQUAL
:
1053 gboolean ret
= (info
->score
== prop
->value
);
1056 if (debug_filtering_session
1057 && prefs_common
.filtering_debug_level
>= FILTERING_DEBUG_LEVEL_HIGH
) {
1059 log_print(LOG_DEBUG_FILTERING
,
1060 "message score [ %d ] is equal to [ %d ]\n",
1061 info
->score
, prop
->value
);
1063 log_print(LOG_DEBUG_FILTERING
,
1064 "message score [ %d ] is not equal to [ %d ]\n",
1065 info
->score
, prop
->value
);
1070 case MATCHCRITERIA_SIZE_GREATER
:
1072 /* FIXME: info->size is a goffset */
1073 gboolean ret
= (info
->size
> (goffset
) prop
->value
);
1076 if (debug_filtering_session
1077 && prefs_common
.filtering_debug_level
>= FILTERING_DEBUG_LEVEL_HIGH
) {
1079 log_print(LOG_DEBUG_FILTERING
,
1080 "message size is greater than [ %d ]\n",
1083 log_print(LOG_DEBUG_FILTERING
,
1084 "message size is not greater than [ %d ]\n",
1090 case MATCHCRITERIA_SIZE_SMALLER
:
1092 /* FIXME: info->size is a goffset */
1093 gboolean ret
= (info
->size
< (goffset
) prop
->value
);
1096 if (debug_filtering_session
1097 && prefs_common
.filtering_debug_level
>= FILTERING_DEBUG_LEVEL_HIGH
) {
1099 log_print(LOG_DEBUG_FILTERING
,
1100 "message size is smaller than [ %d ]\n",
1103 log_print(LOG_DEBUG_FILTERING
,
1104 "message size is not smaller than [ %d ]\n",
1110 case MATCHCRITERIA_SIZE_EQUAL
:
1112 /* FIXME: info->size is a goffset */
1113 gboolean ret
= (info
->size
== (goffset
) prop
->value
);
1116 if (debug_filtering_session
1117 && prefs_common
.filtering_debug_level
>= FILTERING_DEBUG_LEVEL_HIGH
) {
1119 log_print(LOG_DEBUG_FILTERING
,
1120 "message size is equal to [ %d ]\n",
1123 log_print(LOG_DEBUG_FILTERING
,
1124 "message size is not equal to [ %d ]\n",
1130 case MATCHCRITERIA_PARTIAL
:
1132 /* FIXME: info->size is a goffset */
1133 gboolean ret
= (info
->total_size
!= 0 && info
->size
!= (goffset
)info
->total_size
);
1136 if (debug_filtering_session
1137 && prefs_common
.filtering_debug_level
>= FILTERING_DEBUG_LEVEL_HIGH
) {
1139 log_print(LOG_DEBUG_FILTERING
,
1140 "message is partially downloaded, size is less than total size [ %d ])\n",
1143 log_print(LOG_DEBUG_FILTERING
,
1144 "message is not partially downloaded\n");
1149 case MATCHCRITERIA_NOT_PARTIAL
:
1151 /* FIXME: info->size is a goffset */
1152 gboolean ret
= (info
->total_size
== 0 || info
->size
== (goffset
)info
->total_size
);
1155 if (debug_filtering_session
1156 && prefs_common
.filtering_debug_level
>= FILTERING_DEBUG_LEVEL_HIGH
) {
1158 log_print(LOG_DEBUG_FILTERING
,
1159 "message is not partially downloaded\n");
1161 log_print(LOG_DEBUG_FILTERING
,
1162 "message is partially downloaded, size is less than total size [ %d ])\n",
1168 case MATCHCRITERIA_NEWSGROUPS
:
1169 return matcherprop_string_match(prop
, info
->newsgroups
, context_str
[CONTEXT_NEWSGROUPS
]);
1170 case MATCHCRITERIA_NOT_NEWSGROUPS
:
1171 return !matcherprop_string_match(prop
, info
->newsgroups
, context_str
[CONTEXT_NEWSGROUPS
]);
1172 case MATCHCRITERIA_MESSAGEID
:
1173 return matcherprop_string_match(prop
, info
->msgid
, context_str
[CONTEXT_MESSAGEID
]);
1174 case MATCHCRITERIA_NOT_MESSAGEID
:
1175 return !matcherprop_string_match(prop
, info
->msgid
, context_str
[CONTEXT_MESSAGEID
]);
1176 case MATCHCRITERIA_INREPLYTO
:
1177 return matcherprop_string_match(prop
, info
->inreplyto
, context_str
[CONTEXT_IN_REPLY_TO
]);
1178 case MATCHCRITERIA_NOT_INREPLYTO
:
1179 return !matcherprop_string_match(prop
, info
->inreplyto
, context_str
[CONTEXT_IN_REPLY_TO
]);
1180 case MATCHCRITERIA_REFERENCES
:
1181 return matcherprop_list_match(prop
, info
->references
, context_str
[CONTEXT_REFERENCES
]);
1182 case MATCHCRITERIA_NOT_REFERENCES
:
1183 return !matcherprop_list_match(prop
, info
->references
, context_str
[CONTEXT_REFERENCES
]);
1184 case MATCHCRITERIA_TEST
:
1185 return matcherprop_match_test(prop
, info
);
1186 case MATCHCRITERIA_NOT_TEST
:
1187 return !matcherprop_match_test(prop
, info
);
1193 /* ********************* MatcherList *************************** */
1196 *\brief Create a new list of matchers
1198 *\param matchers List of matcher structures
1199 *\param bool_and Operator
1201 *\return MatcherList * New list
1203 MatcherList
*matcherlist_new(GSList
*matchers
, gboolean bool_and
)
1207 cond
= g_new0(MatcherList
, 1);
1209 cond
->matchers
= matchers
;
1210 cond
->bool_and
= bool_and
;
1217 *\brief Builds a single regular expresion from an array of srings.
1219 *\param strings The lines containing the different sub-regexp.
1221 *\return The newly allocated regexp string.
1223 static gchar
*build_complete_regexp(gchar
**strings
)
1227 while (strings
&& strings
[i
] && *strings
[i
]) {
1228 int old_len
= expr
? strlen(expr
):0;
1230 gchar
*tmpstr
= NULL
;
1232 if (g_utf8_validate(strings
[i
], -1, NULL
))
1233 tmpstr
= g_strdup(strings
[i
]);
1235 tmpstr
= conv_codeset_strdup(strings
[i
],
1236 conv_get_locale_charset_str_no_utf8(),
1239 if (strstr(tmpstr
, "\n"))
1240 *(strstr(tmpstr
, "\n")) = '\0';
1242 new_len
= strlen(tmpstr
);
1244 expr
= g_realloc(expr
,
1245 expr
? (old_len
+ strlen("|()") + new_len
+ 1)
1246 : (strlen("()") + new_len
+ 1));
1249 strcpy(expr
+ old_len
, "|(");
1250 strcpy(expr
+ old_len
+ 2, tmpstr
);
1251 strcpy(expr
+ old_len
+ 2 + new_len
, ")");
1253 strcpy(expr
+old_len
, "(");
1254 strcpy(expr
+old_len
+ 1, tmpstr
);
1255 strcpy(expr
+old_len
+ 1 + new_len
, ")");
1265 *\brief Create a new list of matchers from a multi-line string
1267 *\param lines String with "\n"-separated expressions
1268 *\param bool_and Operator
1269 *\param case_sensitive If the matching is case sensitive or not
1271 *\return MatcherList * New matcher list
1273 MatcherList
*matcherlist_new_from_lines(gchar
*lines
, gboolean bool_and
,
1274 gboolean case_sensitive
)
1276 MatcherProp
*m
= NULL
;
1277 GSList
*matchers
= NULL
;
1278 gchar
**strings
= g_strsplit(lines
, "\n", -1);
1282 expr
= build_complete_regexp(strings
);
1283 debug_print("building matcherprop for expr '%s'\n", expr
?expr
:"NULL");
1285 m
= matcherprop_new(MATCHCRITERIA_SUBJECT
, NULL
,
1286 case_sensitive
? MATCHTYPE_REGEXP
: MATCHTYPE_REGEXPCASE
,
1289 /* print error message */
1290 debug_print("failed to allocate memory for matcherprop\n");
1292 matchers
= g_slist_append(matchers
, m
);
1298 while (strings
&& strings
[i
] && *strings
[i
]) {
1299 m
= matcherprop_new(MATCHCRITERIA_SUBJECT
, NULL
,
1300 case_sensitive
? MATCHTYPE_MATCH
: MATCHTYPE_MATCHCASE
,
1303 /* print error message */
1304 debug_print("failed to allocate memory for matcherprop\n");
1306 matchers
= g_slist_append(matchers
, m
);
1311 g_strfreev(strings
);
1313 return matcherlist_new(matchers
, bool_and
);
1317 *\brief Frees a list of matchers
1319 *\param cond List of matchers
1321 void matcherlist_free(MatcherList
*cond
)
1325 cm_return_if_fail(cond
);
1326 for (l
= cond
->matchers
; l
!= NULL
; l
= g_slist_next(l
)) {
1327 matcherprop_free((MatcherProp
*) l
->data
);
1329 g_slist_free(cond
->matchers
);
1334 *\brief Check if a header matches a matcher condition
1336 *\param matcher Matcher structure to check header for
1337 *\param buf Header name
1339 *\return boolean TRUE if matching header
1341 static gboolean
matcherprop_match_one_header(MatcherProp
*matcher
,
1344 gboolean result
= FALSE
;
1345 Header
*header
= NULL
;
1347 switch (matcher
->criteria
) {
1348 case MATCHCRITERIA_HEADER
:
1349 case MATCHCRITERIA_NOT_HEADER
:
1350 header
= procheader_parse_header(buf
);
1353 if (procheader_headername_equal(header
->name
,
1355 if (matcher
->criteria
== MATCHCRITERIA_HEADER
)
1356 result
= matcherprop_string_match(matcher
, header
->body
, context_str
[CONTEXT_HEADER
]);
1358 result
= !matcherprop_string_match(matcher
, header
->body
, context_str
[CONTEXT_HEADER
]);
1359 procheader_header_free(header
);
1363 procheader_header_free(header
);
1366 case MATCHCRITERIA_HEADERS_PART
:
1367 case MATCHCRITERIA_HEADERS_CONT
:
1368 case MATCHCRITERIA_MESSAGE
:
1369 header
= procheader_parse_header(buf
);
1372 result
= matcherprop_header_line_match(matcher
,
1373 header
->name
, header
->body
,
1374 (matcher
->criteria
== MATCHCRITERIA_HEADERS_PART
),
1375 context_str
[CONTEXT_HEADER_LINE
]);
1376 procheader_header_free(header
);
1378 case MATCHCRITERIA_NOT_HEADERS_CONT
:
1379 case MATCHCRITERIA_NOT_HEADERS_PART
:
1380 case MATCHCRITERIA_NOT_MESSAGE
:
1381 header
= procheader_parse_header(buf
);
1384 result
= !matcherprop_header_line_match(matcher
,
1385 header
->name
, header
->body
,
1386 (matcher
->criteria
== MATCHCRITERIA_NOT_HEADERS_PART
),
1387 context_str
[CONTEXT_HEADER_LINE
]);
1388 procheader_header_free(header
);
1390 case MATCHCRITERIA_FOUND_IN_ADDRESSBOOK
:
1391 case MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK
:
1393 GSList
*address_list
= NULL
;
1394 gint match
= MATCH_ONE
;
1395 gboolean found
= FALSE
;
1397 /* how many address headers are we trying to match? */
1398 if (strcasecmp(matcher
->header
, "Any") == 0)
1400 else if (strcasecmp(matcher
->header
, "All") == 0)
1403 if (match
== MATCH_ONE
) {
1404 /* matching one address header exactly, is that the right one? */
1405 header
= procheader_parse_header(buf
);
1407 !procheader_headername_equal(header
->name
, matcher
->header
)) {
1408 procheader_header_free(header
);
1411 address_list
= address_list_append(address_list
, header
->body
);
1412 if (address_list
== NULL
) {
1413 procheader_header_free(header
);
1416 procheader_header_free(header
);
1419 header
= procheader_parse_header(buf
);
1422 /* address header is one of the headers we have to match when checking
1423 for any address header or all address headers? */
1424 if (procheader_headername_equal(header
->name
, "From") ||
1425 procheader_headername_equal(header
->name
, "To") ||
1426 procheader_headername_equal(header
->name
, "Cc") ||
1427 procheader_headername_equal(header
->name
, "Reply-To") ||
1428 procheader_headername_equal(header
->name
, "Sender") ||
1429 procheader_headername_equal(header
->name
, "Resent-From") ||
1430 procheader_headername_equal(header
->name
, "Resent-To"))
1431 address_list
= address_list_append(address_list
, header
->body
);
1432 procheader_header_free(header
);
1433 if (address_list
== NULL
)
1437 found
= match_with_addresses_in_addressbook
1438 (matcher
, address_list
, matcher
->criteria
,
1439 matcher
->expr
, match
);
1440 g_slist_free(address_list
);
1442 if (matcher
->criteria
== MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK
)
1453 *\brief Check if the matcher structure wants headers to
1456 *\param matcher Matcher structure
1458 *\return gboolean TRUE if the matcher structure describes
1459 * a header match condition
1461 static gboolean
matcherprop_criteria_headers(const MatcherProp
*matcher
)
1463 switch (matcher
->criteria
) {
1464 case MATCHCRITERIA_HEADER
:
1465 case MATCHCRITERIA_NOT_HEADER
:
1466 case MATCHCRITERIA_HEADERS_PART
:
1467 case MATCHCRITERIA_HEADERS_CONT
:
1468 case MATCHCRITERIA_NOT_HEADERS_PART
:
1469 case MATCHCRITERIA_NOT_HEADERS_CONT
:
1470 case MATCHCRITERIA_FOUND_IN_ADDRESSBOOK
:
1471 case MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK
:
1479 *\brief Check if the matcher structure wants the message
1480 * to be matched (just perform an action on any
1483 *\param matcher Matcher structure
1485 *\return gboolean TRUE if matcher condition should match
1488 static gboolean
matcherprop_criteria_message(MatcherProp
*matcher
)
1490 switch (matcher
->criteria
) {
1491 case MATCHCRITERIA_MESSAGE
:
1492 case MATCHCRITERIA_NOT_MESSAGE
:
1500 *\brief Check if a list of conditions matches one header in
1503 *\param matchers List of conditions
1504 *\param fp Message file
1506 *\return gboolean TRUE if one of the headers is matched by
1507 * the list of conditions.
1509 static gboolean
matcherlist_match_headers(MatcherList
*matchers
, FILE *fp
)
1515 while ((ret
= procheader_get_one_field(&buf
, fp
, NULL
)) != -1) {
1516 for (l
= matchers
->matchers
; l
!= NULL
; l
= g_slist_next(l
)) {
1517 MatcherProp
*matcher
= (MatcherProp
*) l
->data
;
1518 gint match
= MATCH_ANY
;
1523 /* determine the match range (all, any are our concern here) */
1524 if (matcher
->criteria
== MATCHCRITERIA_NOT_HEADERS_PART
||
1525 matcher
->criteria
== MATCHCRITERIA_NOT_HEADERS_CONT
||
1526 matcher
->criteria
== MATCHCRITERIA_NOT_MESSAGE
) {
1529 } else if (matcher
->criteria
== MATCHCRITERIA_FOUND_IN_ADDRESSBOOK
||
1530 matcher
->criteria
== MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK
) {
1531 Header
*header
= NULL
;
1533 /* address header is one of the headers we have to match when checking
1534 for any address header or all address headers? */
1535 header
= procheader_parse_header(buf
);
1537 (procheader_headername_equal(header
->name
, "From") ||
1538 procheader_headername_equal(header
->name
, "To") ||
1539 procheader_headername_equal(header
->name
, "Cc") ||
1540 procheader_headername_equal(header
->name
, "Reply-To") ||
1541 procheader_headername_equal(header
->name
, "Sender") ||
1542 procheader_headername_equal(header
->name
, "Resent-From") ||
1543 procheader_headername_equal(header
->name
, "Resent-To"))) {
1545 if (strcasecmp(matcher
->header
, "Any") == 0)
1547 else if (strcasecmp(matcher
->header
, "All") == 0)
1552 /* further call to matcherprop_match_one_header() can't match
1553 and it irrelevant, so: don't alter the match result */
1554 procheader_header_free(header
);
1557 procheader_header_free(header
);
1560 /* ZERO line must NOT match for the rule to match.
1562 if (match
== MATCH_ALL
) {
1563 if (matcherprop_match_one_header(matcher
, buf
)) {
1564 matcher
->result
= TRUE
;
1566 matcher
->result
= FALSE
;
1567 matcher
->done
= TRUE
;
1569 /* else, just one line matching is enough for the rule to match
1571 } else if (matcherprop_criteria_headers(matcher
) ||
1572 matcherprop_criteria_message(matcher
)) {
1573 if (matcherprop_match_one_header(matcher
, buf
)) {
1574 matcher
->result
= TRUE
;
1575 matcher
->done
= TRUE
;
1579 /* if the rule matched and the matchers are OR, no need to
1580 * check the others */
1581 if (matcher
->result
&& matcher
->done
) {
1582 if (!matchers
->bool_and
) {
1596 *\brief Check if a matcher wants to check the message body
1598 *\param matcher Matcher structure
1600 *\return gboolean TRUE if body must be matched.
1602 static gboolean
matcherprop_criteria_body(const MatcherProp
*matcher
)
1604 switch (matcher
->criteria
) {
1605 case MATCHCRITERIA_BODY_PART
:
1606 case MATCHCRITERIA_NOT_BODY_PART
:
1613 static gboolean
matcherlist_match_binary_content(MatcherList
*matchers
, MimeInfo
*partinfo
)
1616 gchar buf
[BUFFSIZE
];
1619 if (!partinfo
|| partinfo
->type
== MIMETYPE_TEXT
)
1622 outfp
= procmime_get_binary_content(partinfo
);
1627 while (claws_fgets(buf
, sizeof(buf
), outfp
) != NULL
) {
1630 for (l
= matchers
->matchers
; l
!= NULL
; l
= g_slist_next(l
)) {
1631 MatcherProp
*matcher
= (MatcherProp
*) l
->data
;
1636 /* Don't scan non-text parts when looking in body, only
1637 * when looking in whole message
1639 if (matcher
->criteria
== MATCHCRITERIA_NOT_BODY_PART
||
1640 matcher
->criteria
== MATCHCRITERIA_BODY_PART
)
1643 /* if the criteria is ~body_part or ~message, ZERO lines
1644 * must match for the rule to match.
1646 if (matcher
->criteria
== MATCHCRITERIA_NOT_BODY_PART
||
1647 matcher
->criteria
== MATCHCRITERIA_NOT_MESSAGE
) {
1648 if (matcherprop_string_match(matcher
, buf
,
1649 context_str
[CONTEXT_BODY_LINE
])) {
1650 matcher
->result
= FALSE
;
1651 matcher
->done
= TRUE
;
1653 matcher
->result
= TRUE
;
1654 /* else, just one line has to match */
1655 } else if (matcherprop_criteria_body(matcher
) ||
1656 matcherprop_criteria_message(matcher
)) {
1657 if (matcherprop_string_match(matcher
, buf
,
1658 context_str
[CONTEXT_BODY_LINE
])) {
1659 matcher
->result
= TRUE
;
1660 matcher
->done
= TRUE
;
1664 /* if the matchers are OR'ed and the rule matched,
1665 * no need to check the others. */
1666 if (matcher
->result
&& matcher
->done
) {
1667 if (!matchers
->bool_and
) {
1668 claws_fclose(outfp
);
1675 claws_fclose(outfp
);
1679 static gboolean
match_content_cb(const gchar
*buf
, gpointer data
)
1681 MatcherList
*matchers
= (MatcherList
*)data
;
1682 gboolean all_done
= TRUE
;
1685 for (l
= matchers
->matchers
; l
!= NULL
; l
= g_slist_next(l
)) {
1686 MatcherProp
*matcher
= (MatcherProp
*) l
->data
;
1691 /* if the criteria is ~body_part or ~message, ZERO lines
1692 * must match for the rule to match.
1694 if (matcher
->criteria
== MATCHCRITERIA_NOT_BODY_PART
||
1695 matcher
->criteria
== MATCHCRITERIA_NOT_MESSAGE
) {
1696 if (matcherprop_string_match(matcher
, buf
,
1697 context_str
[CONTEXT_BODY_LINE
])) {
1698 matcher
->result
= FALSE
;
1699 matcher
->done
= TRUE
;
1701 matcher
->result
= TRUE
;
1702 /* else, just one line has to match */
1703 } else if (matcherprop_criteria_body(matcher
) ||
1704 matcherprop_criteria_message(matcher
)) {
1705 if (matcherprop_string_match(matcher
, buf
,
1706 context_str
[CONTEXT_BODY_LINE
])) {
1707 matcher
->result
= TRUE
;
1708 matcher
->done
= TRUE
;
1712 /* if the matchers are OR'ed and the rule matched,
1713 * no need to check the others. */
1714 if (matcher
->result
&& matcher
->done
) {
1715 if (!matchers
->bool_and
) {
1726 static gboolean
matcherlist_match_text_content(MatcherList
*matchers
, MimeInfo
*partinfo
)
1728 if (partinfo
->type
!= MIMETYPE_TEXT
)
1731 return procmime_scan_text_content(partinfo
, match_content_cb
, matchers
);
1735 *\brief Check if a line in a message file's body matches
1738 *\param matchers List of conditions
1739 *\param fp Message file
1741 *\return gboolean TRUE if successful match
1743 static gboolean
matcherlist_match_body(MatcherList
*matchers
, gboolean body_only
, MsgInfo
*info
)
1745 MimeInfo
*mimeinfo
= NULL
;
1746 MimeInfo
*partinfo
= NULL
;
1747 gboolean first_text_found
= FALSE
;
1749 cm_return_val_if_fail(info
!= NULL
, FALSE
);
1751 mimeinfo
= procmime_scan_message(info
);
1754 partinfo
= procmime_mimeinfo_next(mimeinfo
);
1756 for (; partinfo
!= NULL
; partinfo
= procmime_mimeinfo_next(partinfo
)) {
1758 if (partinfo
->type
!= MIMETYPE_TEXT
&& body_only
)
1761 if (partinfo
->type
== MIMETYPE_TEXT
) {
1762 first_text_found
= TRUE
;
1763 if (matcherlist_match_text_content(matchers
, partinfo
)) {
1764 procmime_mimeinfo_free_all(&mimeinfo
);
1767 } else if (matcherlist_match_binary_content(matchers
, partinfo
)) {
1768 procmime_mimeinfo_free_all(&mimeinfo
);
1772 if (body_only
&& first_text_found
)
1775 procmime_mimeinfo_free_all(&mimeinfo
);
1781 *\brief Check if a message file matches criteria
1783 *\param matchers Criteria
1784 *\param info Message info
1785 *\param result Default result
1787 *\return gboolean TRUE if matched
1789 static gboolean
matcherlist_match_file(MatcherList
*matchers
, MsgInfo
*info
,
1792 gboolean read_headers
;
1799 /* file need to be read ? */
1801 read_headers
= FALSE
;
1804 for (l
= matchers
->matchers
; l
!= NULL
; l
= g_slist_next(l
)) {
1805 MatcherProp
*matcher
= (MatcherProp
*) l
->data
;
1807 if (matcherprop_criteria_headers(matcher
))
1808 read_headers
= TRUE
;
1809 if (matcherprop_criteria_body(matcher
))
1811 if (matcherprop_criteria_message(matcher
)) {
1812 read_headers
= TRUE
;
1816 matcher
->result
= FALSE
;
1817 matcher
->done
= FALSE
;
1820 if (!read_headers
&& !read_body
)
1823 file
= procmsg_get_message_file_full(info
, read_headers
, read_body
);
1827 if ((fp
= claws_fopen(file
, "rb")) == NULL
) {
1828 FILE_OP_ERROR(file
, "claws_fopen");
1833 /* read the headers */
1836 if (matcherlist_match_headers(matchers
, fp
))
1839 procheader_skip_headers(fp
);
1844 matcherlist_match_body(matchers
, body_only
, info
);
1847 for (l
= matchers
->matchers
; l
!= NULL
; l
= g_slist_next(l
)) {
1848 MatcherProp
*matcher
= (MatcherProp
*) l
->data
;
1850 if (matcherprop_criteria_headers(matcher
) ||
1851 matcherprop_criteria_body(matcher
) ||
1852 matcherprop_criteria_message(matcher
)) {
1853 if (matcher
->result
) {
1854 if (!matchers
->bool_and
) {
1860 if (matchers
->bool_and
) {
1876 *\brief Test list of conditions on a message.
1878 *\param matchers List of conditions
1879 *\param info Message info
1881 *\return gboolean TRUE if matched
1883 gboolean
matcherlist_match(MatcherList
*matchers
, MsgInfo
*info
)
1891 if (matchers
->bool_and
)
1896 /* test the cached elements */
1898 for (l
= matchers
->matchers
; l
!= NULL
;l
= g_slist_next(l
)) {
1899 MatcherProp
*matcher
= (MatcherProp
*) l
->data
;
1901 if (debug_filtering_session
) {
1902 gchar
*buf
= matcherprop_to_string(matcher
);
1903 log_print(LOG_DEBUG_FILTERING
, _("checking if message matches [ %s ]\n"), buf
);
1907 switch(matcher
->criteria
) {
1908 case MATCHCRITERIA_ALL
:
1909 case MATCHCRITERIA_UNREAD
:
1910 case MATCHCRITERIA_NOT_UNREAD
:
1911 case MATCHCRITERIA_NEW
:
1912 case MATCHCRITERIA_NOT_NEW
:
1913 case MATCHCRITERIA_MARKED
:
1914 case MATCHCRITERIA_NOT_MARKED
:
1915 case MATCHCRITERIA_DELETED
:
1916 case MATCHCRITERIA_NOT_DELETED
:
1917 case MATCHCRITERIA_REPLIED
:
1918 case MATCHCRITERIA_NOT_REPLIED
:
1919 case MATCHCRITERIA_FORWARDED
:
1920 case MATCHCRITERIA_NOT_FORWARDED
:
1921 case MATCHCRITERIA_LOCKED
:
1922 case MATCHCRITERIA_NOT_LOCKED
:
1923 case MATCHCRITERIA_SPAM
:
1924 case MATCHCRITERIA_NOT_SPAM
:
1925 case MATCHCRITERIA_HAS_ATTACHMENT
:
1926 case MATCHCRITERIA_HAS_NO_ATTACHMENT
:
1927 case MATCHCRITERIA_SIGNED
:
1928 case MATCHCRITERIA_NOT_SIGNED
:
1929 case MATCHCRITERIA_COLORLABEL
:
1930 case MATCHCRITERIA_NOT_COLORLABEL
:
1931 case MATCHCRITERIA_IGNORE_THREAD
:
1932 case MATCHCRITERIA_NOT_IGNORE_THREAD
:
1933 case MATCHCRITERIA_WATCH_THREAD
:
1934 case MATCHCRITERIA_NOT_WATCH_THREAD
:
1935 case MATCHCRITERIA_SUBJECT
:
1936 case MATCHCRITERIA_NOT_SUBJECT
:
1937 case MATCHCRITERIA_FROM
:
1938 case MATCHCRITERIA_NOT_FROM
:
1939 case MATCHCRITERIA_TO
:
1940 case MATCHCRITERIA_NOT_TO
:
1941 case MATCHCRITERIA_CC
:
1942 case MATCHCRITERIA_NOT_CC
:
1943 case MATCHCRITERIA_TO_OR_CC
:
1944 case MATCHCRITERIA_NOT_TO_AND_NOT_CC
:
1945 case MATCHCRITERIA_TAG
:
1946 case MATCHCRITERIA_NOT_TAG
:
1947 case MATCHCRITERIA_TAGGED
:
1948 case MATCHCRITERIA_NOT_TAGGED
:
1949 case MATCHCRITERIA_AGE_GREATER
:
1950 case MATCHCRITERIA_AGE_LOWER
:
1951 case MATCHCRITERIA_AGE_GREATER_HOURS
:
1952 case MATCHCRITERIA_AGE_LOWER_HOURS
:
1953 case MATCHCRITERIA_DATE_AFTER
:
1954 case MATCHCRITERIA_DATE_BEFORE
:
1955 case MATCHCRITERIA_NEWSGROUPS
:
1956 case MATCHCRITERIA_NOT_NEWSGROUPS
:
1957 case MATCHCRITERIA_MESSAGEID
:
1958 case MATCHCRITERIA_NOT_MESSAGEID
:
1959 case MATCHCRITERIA_INREPLYTO
:
1960 case MATCHCRITERIA_NOT_INREPLYTO
:
1961 case MATCHCRITERIA_REFERENCES
:
1962 case MATCHCRITERIA_NOT_REFERENCES
:
1963 case MATCHCRITERIA_SCORE_GREATER
:
1964 case MATCHCRITERIA_SCORE_LOWER
:
1965 case MATCHCRITERIA_SCORE_EQUAL
:
1966 case MATCHCRITERIA_SIZE_GREATER
:
1967 case MATCHCRITERIA_SIZE_SMALLER
:
1968 case MATCHCRITERIA_SIZE_EQUAL
:
1969 case MATCHCRITERIA_TEST
:
1970 case MATCHCRITERIA_NOT_TEST
:
1971 case MATCHCRITERIA_PARTIAL
:
1972 case MATCHCRITERIA_NOT_PARTIAL
:
1973 if (matcherprop_match(matcher
, info
)) {
1974 if (!matchers
->bool_and
) {
1975 if (debug_filtering_session
)
1976 log_status_ok(LOG_DEBUG_FILTERING
, _("message matches\n"));
1981 if (matchers
->bool_and
) {
1982 if (debug_filtering_session
)
1983 log_status_nok(LOG_DEBUG_FILTERING
, _("message does not match\n"));
1990 /* test the condition on the file */
1992 if (matcherlist_match_file(matchers
, info
, result
)) {
1993 if (!matchers
->bool_and
) {
1994 if (debug_filtering_session
)
1995 log_status_ok(LOG_DEBUG_FILTERING
, _("message matches\n"));
1999 if (matchers
->bool_and
) {
2000 if (debug_filtering_session
)
2001 log_status_nok(LOG_DEBUG_FILTERING
, _("message does not match\n"));
2006 if (debug_filtering_session
) {
2008 log_status_ok(LOG_DEBUG_FILTERING
, _("message matches\n"));
2010 log_status_nok(LOG_DEBUG_FILTERING
, _("message does not match\n"));
2016 static gint
quote_filter_str(gchar
* result
, guint size
,
2026 for(p
= path
; * p
!= '\0' ; p
++) {
2028 if ((* p
!= '\"') && (* p
!= '\\')) {
2029 if (remaining
> 0) {
2035 result
[size
- 1] = '\0';
2040 if (remaining
>= 2) {
2048 result
[size
- 1] = '\0';
2053 if (remaining
> 0) {
2057 result
[size
- 1] = '\0';
2065 gchar
* matcher_quote_str(const gchar
* src
)
2070 len
= strlen(src
) * 2 + 1;
2071 res
= g_malloc(len
);
2072 quote_filter_str(res
, len
, src
);
2078 *\brief Convert a matcher structure to a string
2080 *\param matcher Matcher structure
2082 *\return gchar * Newly allocated string
2084 gchar
*matcherprop_to_string(MatcherProp
*matcher
)
2086 gchar
*matcher_str
= NULL
;
2087 const gchar
*criteria_str
;
2088 const gchar
*matchtype_str
;
2090 gchar
* quoted_expr
;
2091 gchar
* quoted_header
;
2093 criteria_str
= NULL
;
2094 for (i
= 0; i
< (int) (sizeof(matchparser_tab
) / sizeof(MatchParser
)); i
++) {
2095 if (matchparser_tab
[i
].id
== matcher
->criteria
)
2096 criteria_str
= matchparser_tab
[i
].str
;
2098 if (criteria_str
== NULL
)
2101 switch (matcher
->criteria
) {
2102 case MATCHCRITERIA_AGE_GREATER
:
2103 case MATCHCRITERIA_AGE_LOWER
:
2104 case MATCHCRITERIA_AGE_GREATER_HOURS
:
2105 case MATCHCRITERIA_AGE_LOWER_HOURS
:
2106 case MATCHCRITERIA_SCORE_GREATER
:
2107 case MATCHCRITERIA_SCORE_LOWER
:
2108 case MATCHCRITERIA_SCORE_EQUAL
:
2109 case MATCHCRITERIA_SIZE_GREATER
:
2110 case MATCHCRITERIA_SIZE_SMALLER
:
2111 case MATCHCRITERIA_SIZE_EQUAL
:
2112 case MATCHCRITERIA_COLORLABEL
:
2113 case MATCHCRITERIA_NOT_COLORLABEL
:
2114 return g_strdup_printf("%s %i", criteria_str
, matcher
->value
);
2115 case MATCHCRITERIA_ALL
:
2116 case MATCHCRITERIA_UNREAD
:
2117 case MATCHCRITERIA_NOT_UNREAD
:
2118 case MATCHCRITERIA_NEW
:
2119 case MATCHCRITERIA_NOT_NEW
:
2120 case MATCHCRITERIA_MARKED
:
2121 case MATCHCRITERIA_NOT_MARKED
:
2122 case MATCHCRITERIA_DELETED
:
2123 case MATCHCRITERIA_NOT_DELETED
:
2124 case MATCHCRITERIA_REPLIED
:
2125 case MATCHCRITERIA_NOT_REPLIED
:
2126 case MATCHCRITERIA_FORWARDED
:
2127 case MATCHCRITERIA_NOT_FORWARDED
:
2128 case MATCHCRITERIA_LOCKED
:
2129 case MATCHCRITERIA_NOT_LOCKED
:
2130 case MATCHCRITERIA_SPAM
:
2131 case MATCHCRITERIA_NOT_SPAM
:
2132 case MATCHCRITERIA_HAS_ATTACHMENT
:
2133 case MATCHCRITERIA_HAS_NO_ATTACHMENT
:
2134 case MATCHCRITERIA_SIGNED
:
2135 case MATCHCRITERIA_NOT_SIGNED
:
2136 case MATCHCRITERIA_PARTIAL
:
2137 case MATCHCRITERIA_NOT_PARTIAL
:
2138 case MATCHCRITERIA_IGNORE_THREAD
:
2139 case MATCHCRITERIA_NOT_IGNORE_THREAD
:
2140 case MATCHCRITERIA_WATCH_THREAD
:
2141 case MATCHCRITERIA_NOT_WATCH_THREAD
:
2142 case MATCHCRITERIA_TAGGED
:
2143 case MATCHCRITERIA_NOT_TAGGED
:
2144 return g_strdup(criteria_str
);
2145 case MATCHCRITERIA_TEST
:
2146 case MATCHCRITERIA_NOT_TEST
:
2147 case MATCHCRITERIA_DATE_AFTER
:
2148 case MATCHCRITERIA_DATE_BEFORE
:
2149 quoted_expr
= matcher_quote_str(matcher
->expr
);
2150 matcher_str
= g_strdup_printf("%s \"%s\"",
2151 criteria_str
, quoted_expr
);
2152 g_free(quoted_expr
);
2154 case MATCHCRITERIA_FOUND_IN_ADDRESSBOOK
:
2155 case MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK
:
2156 quoted_header
= matcher_quote_str(matcher
->header
);
2157 quoted_expr
= matcher_quote_str(matcher
->expr
);
2158 matcher_str
= g_strdup_printf("%s \"%s\" in \"%s\"",
2159 criteria_str
, quoted_header
, quoted_expr
);
2160 g_free(quoted_header
);
2161 g_free(quoted_expr
);
2165 matchtype_str
= NULL
;
2166 for (i
= 0; i
< sizeof matchparser_tab
/ sizeof matchparser_tab
[0]; i
++) {
2167 if (matchparser_tab
[i
].id
== matcher
->matchtype
)
2168 matchtype_str
= matchparser_tab
[i
].str
;
2171 if (matchtype_str
== NULL
)
2174 switch (matcher
->matchtype
) {
2175 case MATCHTYPE_MATCH
:
2176 case MATCHTYPE_MATCHCASE
:
2177 case MATCHTYPE_REGEXP
:
2178 case MATCHTYPE_REGEXPCASE
:
2179 quoted_expr
= matcher_quote_str(matcher
->expr
);
2180 if (matcher
->header
) {
2181 quoted_header
= matcher_quote_str(matcher
->header
);
2182 matcher_str
= g_strdup_printf
2183 ("%s \"%s\" %s \"%s\"",
2184 criteria_str
, quoted_header
,
2185 matchtype_str
, quoted_expr
);
2186 g_free(quoted_header
);
2189 matcher_str
= g_strdup_printf
2190 ("%s %s \"%s\"", criteria_str
,
2191 matchtype_str
, quoted_expr
);
2192 g_free(quoted_expr
);
2200 *\brief Convert a list of conditions to a string
2202 *\param matchers List of conditions
2204 *\return gchar * Newly allocated string
2206 gchar
*matcherlist_to_string(const MatcherList
*matchers
)
2212 gchar
*result
= NULL
;
2214 count
= g_slist_length(matchers
->matchers
);
2215 vstr
= g_new(gchar
*, count
+ 1);
2217 for (l
= matchers
->matchers
, cur_str
= vstr
; l
!= NULL
;
2218 l
= g_slist_next(l
), cur_str
++) {
2219 *cur_str
= matcherprop_to_string((MatcherProp
*) l
->data
);
2220 if (*cur_str
== NULL
)
2225 if (matchers
->bool_and
)
2226 result
= g_strjoinv(" & ", vstr
);
2228 result
= g_strjoinv(" | ", vstr
);
2230 for (cur_str
= vstr
; *cur_str
!= NULL
; cur_str
++)
2238 #define STRLEN_ZERO(s) ((s) ? strlen(s) : 0)
2239 #define STRLEN_DEFAULT(s,d) ((s) ? strlen(s) : STRLEN_ZERO(d))
2241 static void add_str_default(gchar
** dest
,
2242 const gchar
* s
, const gchar
* d
)
2244 gchar quoted_str
[4096];
2252 quote_cmd_argument(quoted_str
, sizeof(quoted_str
), str
);
2253 strcpy(* dest
, quoted_str
);
2255 (* dest
) += strlen(* dest
);
2258 /* matching_build_command() - preferably cmd should be unescaped */
2260 *\brief Build the command-line to execute
2262 *\param cmd String with command-line specifiers
2263 *\param info Message info to use for command
2265 *\return gchar * Newly allocated string
2267 gchar
*matching_build_command(const gchar
*cmd
, MsgInfo
*info
)
2269 const gchar
*s
= cmd
;
2270 gchar
*filename
= NULL
;
2271 gchar
*processed_cmd
;
2275 const gchar
*const no_subject
= _("(none)") ;
2276 const gchar
*const no_from
= _("(none)") ;
2277 const gchar
*const no_to
= _("(none)") ;
2278 const gchar
*const no_cc
= _("(none)") ;
2279 const gchar
*const no_date
= _("(none)") ;
2280 const gchar
*const no_msgid
= _("(none)") ;
2281 const gchar
*const no_newsgroups
= _("(none)") ;
2282 const gchar
*const no_references
= _("(none)") ;
2284 size
= STRLEN_ZERO(cmd
) + 1;
2285 while (*s
!= '\0') {
2292 case 's': /* subject */
2293 size
+= STRLEN_DEFAULT(info
->subject
, no_subject
) - 2;
2295 case 'f': /* from */
2296 size
+= STRLEN_DEFAULT(info
->from
, no_from
) - 2;
2299 size
+= STRLEN_DEFAULT(info
->to
, no_to
) - 2;
2302 size
+= STRLEN_DEFAULT(info
->cc
, no_cc
) - 2;
2304 case 'd': /* date */
2305 size
+= STRLEN_DEFAULT(info
->date
, no_date
) - 2;
2307 case 'i': /* message-id */
2308 size
+= STRLEN_DEFAULT(info
->msgid
, no_msgid
) - 2;
2310 case 'n': /* newsgroups */
2311 size
+= STRLEN_DEFAULT(info
->newsgroups
, no_newsgroups
) - 2;
2313 case 'r': /* references */
2314 /* FIXME: using the inreplyto header for reference */
2315 size
+= STRLEN_DEFAULT(info
->inreplyto
, no_references
) - 2;
2317 case 'F': /* file */
2318 if (filename
== NULL
)
2319 filename
= folder_item_fetch_msg(info
->folder
, info
->msgnum
);
2321 if (filename
== NULL
) {
2322 g_warning("filename is not set");
2326 size
+= strlen(filename
) - 2;
2335 /* as the string can be quoted, we double the result */
2338 processed_cmd
= g_new0(gchar
, size
);
2342 while (*s
!= '\0') {
2350 case 's': /* subject */
2351 add_str_default(&p
, info
->subject
,
2354 case 'f': /* from */
2355 add_str_default(&p
, info
->from
,
2359 add_str_default(&p
, info
->to
,
2363 add_str_default(&p
, info
->cc
,
2366 case 'd': /* date */
2367 add_str_default(&p
, info
->date
,
2370 case 'i': /* message-id */
2371 add_str_default(&p
, info
->msgid
,
2374 case 'n': /* newsgroups */
2375 add_str_default(&p
, info
->newsgroups
,
2378 case 'r': /* references */
2379 /* FIXME: using the inreplyto header for references */
2380 add_str_default(&p
, info
->inreplyto
, no_references
);
2382 case 'F': /* file */
2383 if (filename
!= NULL
)
2384 add_str_default(&p
, filename
, NULL
);
2403 return processed_cmd
;
2405 #undef STRLEN_DEFAULT
2408 /* ************************************************************ */
2412 *\brief Write filtering list to file
2415 *\param prefs_filtering List of filtering conditions
2417 static int prefs_filtering_write(FILE *fp
, GSList
*prefs_filtering
)
2421 for (cur
= prefs_filtering
; cur
!= NULL
; cur
= cur
->next
) {
2422 gchar
*filtering_str
= NULL
;
2423 gchar
*tmp_name
= NULL
;
2424 FilteringProp
*prop
= NULL
;
2426 if (NULL
== (prop
= (FilteringProp
*) cur
->data
))
2429 if (NULL
== (filtering_str
= filteringprop_to_string(prop
)))
2432 if (prop
->enabled
) {
2433 if (claws_fputs("enabled ", fp
) == EOF
) {
2434 FILE_OP_ERROR("filtering config", "claws_fputs");
2438 if (claws_fputs("disabled ", fp
) == EOF
) {
2439 FILE_OP_ERROR("filtering config", "claws_fputs");
2444 if (claws_fputs("rulename \"", fp
) == EOF
) {
2445 FILE_OP_ERROR("filtering config", "claws_fputs");
2446 g_free(filtering_str
);
2449 tmp_name
= prop
->name
;
2450 while (tmp_name
&& *tmp_name
!= '\0') {
2451 if (*tmp_name
!= '"') {
2452 if (claws_fputc(*tmp_name
, fp
) == EOF
) {
2453 FILE_OP_ERROR("filtering config", "claws_fputs || claws_fputc");
2454 g_free(filtering_str
);
2457 } else if (*tmp_name
== '"') {
2458 if (claws_fputc('\\', fp
) == EOF
||
2459 claws_fputc('"', fp
) == EOF
) {
2460 FILE_OP_ERROR("filtering config", "claws_fputs || claws_fputc");
2461 g_free(filtering_str
);
2467 if (claws_fputs("\" ", fp
) == EOF
) {
2468 FILE_OP_ERROR("filtering config", "claws_fputs");
2469 g_free(filtering_str
);
2473 if (prop
->account_id
!= 0) {
2476 tmp
= g_strdup_printf("account %d ", prop
->account_id
);
2477 if (claws_fputs(tmp
, fp
) == EOF
) {
2478 FILE_OP_ERROR("filtering config", "claws_fputs");
2485 if(claws_fputs(filtering_str
, fp
) == EOF
||
2486 claws_fputc('\n', fp
) == EOF
) {
2487 FILE_OP_ERROR("filtering config", "claws_fputs || claws_fputc");
2488 g_free(filtering_str
);
2491 g_free(filtering_str
);
2497 typedef struct _NodeLoopData
{
2503 *\brief Write matchers from a folder item
2505 *\param node Node with folder info
2506 *\param data File pointer
2508 *\return gboolean FALSE
2510 static gboolean
prefs_matcher_write_func(GNode
*node
, gpointer d
)
2513 NodeLoopData
*data
= (NodeLoopData
*)d
;
2515 GSList
*prefs_filtering
;
2518 /* prevent warning */
2519 if (item
->path
== NULL
)
2521 id
= folder_item_get_identifier(item
);
2524 prefs_filtering
= item
->prefs
->processing
;
2526 if (prefs_filtering
!= NULL
) {
2527 if (fprintf(data
->fp
, "[%s]\n", id
) < 0) {
2531 if (prefs_filtering_write(data
->fp
, prefs_filtering
) < 0) {
2535 if (claws_fputc('\n', data
->fp
) == EOF
) {
2547 *\brief Save matchers from folder items
2551 static int prefs_matcher_save(FILE *fp
)
2559 for (cur
= folder_get_list() ; cur
!= NULL
; cur
= g_list_next(cur
)) {
2562 folder
= (Folder
*) cur
->data
;
2563 g_node_traverse(folder
->node
, G_PRE_ORDER
, G_TRAVERSE_ALL
, -1,
2564 prefs_matcher_write_func
, &data
);
2567 if (data
.error
== TRUE
)
2570 /* pre global rules */
2571 if (fprintf(fp
, "[preglobal]\n") < 0 ||
2572 prefs_filtering_write(fp
, pre_global_processing
) < 0 ||
2573 claws_fputc('\n', fp
) == EOF
)
2576 /* post global rules */
2577 if (fprintf(fp
, "[postglobal]\n") < 0 ||
2578 prefs_filtering_write(fp
, post_global_processing
) < 0 ||
2579 claws_fputc('\n', fp
) == EOF
)
2582 /* filtering rules */
2583 if (fprintf(fp
, "[filtering]\n") < 0 ||
2584 prefs_filtering_write(fp
, filtering_rules
) < 0 ||
2585 claws_fputc('\n', fp
) == EOF
)
2592 *\brief Write filtering / matcher configuration file
2594 void prefs_matcher_write_config(void)
2599 debug_print("Writing matcher configuration...\n");
2601 rcpath
= g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S
,
2604 if ((pfile
= prefs_write_open(rcpath
)) == NULL
) {
2605 g_warning("failed to write configuration to file");
2612 if (prefs_matcher_save(pfile
->fp
) < 0) {
2613 g_warning("failed to write configuration to file");
2614 prefs_file_close_revert(pfile
);
2615 } else if (prefs_file_close(pfile
) < 0) {
2616 g_warning("failed to save configuration to file");
2621 *\brief Read matcher configuration
2623 void prefs_matcher_read_config(void)
2628 create_matchparser_hashtab();
2629 prefs_filtering_clear();
2631 rcpath
= g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S
, MATCHER_RC
, NULL
);
2633 f
= claws_fopen(rcpath
, "rb");
2637 matcher_parser_start_parsing(f
);
2638 claws_fclose(matcher_parserin
);