Remove inclusion of sys/socket.h from nntp-thread.c
[claws.git] / src / matcher.c
blob889d460d34df3fa4db4c1830fb773a627da10d9d
1 /*
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/>.
19 #ifdef HAVE_CONFIG_H
20 # include "config.h"
21 #include "claws-features.h"
22 #endif
24 #include <glib.h>
25 #include <glib/gi18n.h>
26 #include <ctype.h>
27 #include <string.h>
28 #include <stdlib.h>
29 #include <errno.h>
31 #ifdef USE_PTHREAD
32 #include <pthread.h>
33 #endif
35 #include "defs.h"
36 #include "utils.h"
37 #include "procheader.h"
38 #include "matcher.h"
39 #include "matcher_parser.h"
40 #include "prefs_gtk.h"
41 #include "addr_compl.h"
42 #include "codeconv.h"
43 #include "quoted-printable.h"
44 #include "claws.h"
45 #include "prefs_common.h"
46 #include "log.h"
47 #include "tags.h"
48 #include "folder_item_prefs.h"
49 #include "procmsg.h"
50 #include "file-utils.h"
52 /*!
53 *\brief Keyword lookup element
55 struct _MatchParser {
56 gint id; /*!< keyword id */
57 gchar *str; /*!< keyword */
59 typedef struct _MatchParser MatchParser;
61 /*!
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[] = {
66 /* msginfo flags */
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"},
95 /* msginfo headers */
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"},
150 /* match type */
151 {MATCHTYPE_MATCHCASE, "matchcase"},
152 {MATCHTYPE_MATCH, "match"},
153 {MATCHTYPE_REGEXPCASE, "regexpcase"},
154 {MATCHTYPE_REGEXP, "regexp"},
156 /* actions */
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"},
186 enum {
187 MATCH_ANY = 0,
188 MATCH_ALL = 1,
189 MATCH_ONE = 2
192 enum {
193 CONTEXT_SUBJECT,
194 CONTEXT_FROM,
195 CONTEXT_TO,
196 CONTEXT_CC,
197 CONTEXT_NEWSGROUPS,
198 CONTEXT_MESSAGEID,
199 CONTEXT_IN_REPLY_TO,
200 CONTEXT_REFERENCES,
201 CONTEXT_HEADER,
202 CONTEXT_HEADER_LINE,
203 CONTEXT_BODY_LINE,
204 CONTEXT_TAG,
205 N_CONTEXT_STRS
208 static gchar *context_str[N_CONTEXT_STRS];
210 void matcher_init(void)
212 if (context_str[CONTEXT_SUBJECT] != NULL)
213 return;
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)
231 int i;
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)
254 gint i;
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;
260 return NULL;
264 *\brief Create keyword lookup table
266 static void create_matchparser_hashtab(void)
268 int i;
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)
287 MatchParser *res;
289 if (NULL != (res = g_hash_table_lookup(matchparser_hashtab, str))) {
290 return res->id;
291 } else
292 return -1;
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,
312 int value)
314 MatcherProp *prop;
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;
323 #ifndef G_OS_WIN32
324 prop->preg = NULL;
325 #endif
326 prop->casefold_expr = NULL;
327 prop->value = value;
328 prop->error = 0;
330 return prop;
334 *\brief Free a matcher structure
336 *\param prop Pointer to matcher structure allocated with
337 * #matcherprop_new
339 void matcherprop_free(MatcherProp *prop)
341 g_free(prop->expr);
342 g_free(prop->header);
343 #ifndef G_OS_WIN32
344 if (prop->preg != NULL) {
345 regfree(prop->preg);
346 g_free(prop->preg);
348 g_free(prop->casefold_expr);
349 #endif
350 g_free(prop);
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;
369 #ifndef G_OS_WIN32
370 prop->preg = NULL; /* will be re-evaluated */
371 #endif
372 prop->casefold_expr = src->casefold_expr ? g_strdup(src->casefold_expr) : NULL;
373 prop->value = src->value;
374 prop->error = src->error;
375 return prop;
378 /* ************** match ******************************/
380 static gboolean match_with_addresses_in_addressbook
381 (MatcherProp *prop, GSList *address_list, gint type,
382 gchar* folderpath, gint match)
384 GSList *walk = NULL;
385 gboolean found = FALSE;
386 gchar *path = NULL;
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 ||
395 *folderpath == '\0')
396 path = NULL;
397 else
398 path = folderpath;
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);
405 found = FALSE;
406 if (num_addr > 1) {
407 /* skip first item (this is the search string itself) */
408 int i = 1;
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) {
413 found = TRUE;
415 /* debug output */
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);
423 g_free(addr);
426 /* debug output */
427 if (debug_filtering_session
428 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH
429 && !found) {
430 log_print(LOG_DEBUG_FILTERING,
431 "address [ %s ] does NOT match\n",
432 (gchar *)walk->data);
434 g_free(walk->data);
436 if (match == MATCH_ALL) {
437 /* if matching all addresses, stop if one doesn't match */
438 if (!found) {
439 /* debug output */
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");
445 break;
447 } else if (match == MATCH_ANY) {
448 /* if matching any address, stop if one does match */
449 if (found) {
450 /* debug output */
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");
456 break;
459 /* MATCH_ONE: there should be only one loop iteration */
462 end_address_completion();
464 return found;
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
474 * matcher structure
476 static gboolean matcherprop_string_match(MatcherProp *prop, const gchar *str,
477 const gchar *debug_context)
479 gchar *str1;
480 const gchar *down_expr;
481 gboolean ret = FALSE;
482 gboolean should_free = FALSE;
483 if (str == NULL)
484 return 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;
494 should_free = TRUE;
495 } else {
496 str1 = (gchar *)str;
497 down_expr = (gchar *)prop->expr;
498 should_free = FALSE;
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) {
511 prop->error = 1;
512 regfree(prop->preg);
513 g_free(prop->preg);
514 prop->preg = NULL;
517 if (prop->preg == NULL) {
518 ret = FALSE;
519 goto free_strs;
522 if (regexec(prop->preg, str1, 0, NULL, 0) == 0)
523 ret = TRUE;
524 else
525 ret = FALSE;
527 /* debug output */
528 if (debug_filtering_session
529 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
530 gchar *stripped = g_strdup(str);
532 strretchomp(stripped);
533 if (ret) {
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"));
538 } else {
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"));
544 g_free(stripped);
546 break;
547 case MATCHTYPE_MATCHCASE:
548 case MATCHTYPE_MATCH:
549 ret = (strstr(str1, down_expr) != NULL);
551 /* debug output */
552 if (debug_filtering_session
553 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
554 gchar *stripped = g_strdup(str);
556 strretchomp(stripped);
557 if (ret) {
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"));
562 } else {
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"));
568 g_free(stripped);
570 break;
572 default:
573 break;
576 free_strs:
577 if (should_free) {
578 g_free(str1);
580 return ret;
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
590 * matcher structure
592 static gboolean matcherprop_tag_match(MatcherProp *prop, MsgInfo *msginfo,
593 const gchar *debug_context)
595 gboolean ret = FALSE;
596 GSList *cur;
598 if (msginfo == NULL || msginfo->tags == NULL)
599 return FALSE;
601 for (cur = msginfo->tags; cur; cur = cur->next) {
602 const gchar *str = tags_get_tag(GPOINTER_TO_INT(cur->data));
603 if (!str)
604 continue;
605 if (matcherprop_string_match(prop, str, debug_context)) {
606 ret = TRUE;
607 break;
610 return ret;
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
620 * matcher structure
622 static gboolean matcherprop_list_match(MatcherProp *prop, const GSList *list,
623 const gchar *debug_context)
625 const GSList *cur;
627 for(cur = list; cur != NULL; cur = cur->next) {
628 if (matcherprop_string_match(prop, (gchar *)cur->data, debug_context))
629 return TRUE;
631 return FALSE;
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)
641 return FALSE;
643 if (both) {
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);
648 g_free(line);
649 } else {
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))
657 return FALSE;
658 res = matcherprop_string_match(prop, str, debug_context);
661 return res;
664 #ifdef USE_PTHREAD
665 typedef struct _thread_data {
666 const gchar *cmd;
667 gboolean done;
668 } thread_data;
669 #endif
671 #ifdef USE_PTHREAD
672 static void *matcher_test_thread(void *data)
674 thread_data *td = (thread_data *)data;
675 int result = -1;
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);
684 #endif
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,
695 MsgInfo *info)
697 gchar *file;
698 gchar *cmd;
699 gint retval;
700 #ifdef USE_PTHREAD
701 pthread_t pt;
702 thread_data *td = g_new0(thread_data, 1);
703 void *res = NULL;
704 time_t start_time = time(NULL);
705 #endif
707 file = procmsg_get_message_file(info);
708 if (file == NULL) {
709 #ifdef USE_PTHREAD
710 g_free(td);
711 #endif
712 return FALSE;
714 g_free(file);
716 cmd = matching_build_command(prop->expr, info);
717 if (cmd == NULL) {
718 #ifdef USE_PTHREAD
719 g_free(td);
720 #endif
721 return FALSE;
724 #ifdef USE_PTHREAD
725 /* debug output */
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",
730 cmd);
733 td->cmd = cmd;
734 td->done = FALSE;
735 if (pthread_create(&pt, NULL, matcher_test_thread, td) != 0)
736 retval = system(cmd);
737 else {
738 debug_print("waiting for test thread\n");
739 while(!td->done) {
740 /* don't let the interface freeze while waiting */
741 if (time(NULL) - start_time > 0) {
742 claws_do_idle();
744 if (time(NULL) - start_time > 30) {
745 pthread_cancel(pt);
746 td->done = TRUE;
747 retval = -1;
750 pthread_join(pt, &res);
751 retval = GPOINTER_TO_INT(res);
752 debug_print(" test thread returned %d\n", retval);
754 g_free(td);
755 #else
756 /* debug output */
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",
761 cmd);
764 retval = system(cmd);
765 #endif
766 debug_print("Command exit code: %d\n", retval);
768 /* debug output */
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",
773 retval);
776 g_free(cmd);
777 return (retval == 0);
781 *\brief Check if a message matches the condition in a matcher
782 * structure.
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,
790 MsgInfo *info)
792 time_t t;
793 gint age_mult_hours = 1;
795 switch(prop->criteria) {
796 case MATCHCRITERIA_ALL:
797 return TRUE;
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);
843 /* debug output */
844 if (debug_filtering_session
845 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
846 if (ret) {
847 log_print(LOG_DEBUG_FILTERING,
848 "message color value [ %d ] matches color value [ %d ]\n",
849 color, prop->value);
850 } else {
851 log_print(LOG_DEBUG_FILTERING,
852 "message color value [ %d ] does NOT match color value [ %d ]\n",
853 color, prop->value);
856 return ret;
858 case MATCHCRITERIA_NOT_COLORLABEL:
860 gint color = MSG_GET_COLORLABEL_VALUE(info->flags);
861 gboolean ret = (color != prop->value);
863 /* debug output */
864 if (debug_filtering_session
865 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
866 if (ret) {
867 log_print(LOG_DEBUG_FILTERING,
868 "message color value [ %d ] matches color value [ %d ]\n",
869 color, prop->value);
870 } else {
871 log_print(LOG_DEBUG_FILTERING,
872 "message color value [ %d ] does NOT match color value [ %d ]\n",
873 color, prop->value);
876 return ret;
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:
917 age_mult_hours = 24;
918 /* Fallthrough intended */
919 case MATCHCRITERIA_AGE_GREATER_HOURS:
921 gboolean ret;
922 gint age;
924 t = time(NULL);
925 age = ((t - info->date_t) / (60 * 60 * age_mult_hours));
926 ret = (age >= prop->value);
928 /* debug output */
929 if (debug_filtering_session
930 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
931 if (ret) {
932 log_print(LOG_DEBUG_FILTERING,
933 "message age [ %d ] is greater than [ %d ]\n",
934 age, prop->value);
935 } else {
936 log_print(LOG_DEBUG_FILTERING,
937 "message age [ %d ] is not greater than [ %d ]\n",
938 age, prop->value);
941 return ret;
943 case MATCHCRITERIA_DATE_AFTER:
945 gboolean ret;
947 ret = prop->value < info->date_t;
949 /* debug output */
950 if (debug_filtering_session
951 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
952 if (ret) {
953 log_print(LOG_DEBUG_FILTERING,
954 "message date [ %" CM_TIME_FORMAT " ] is after [ %d ]\n",
955 info->date_t, prop->value);
956 } else {
957 log_print(LOG_DEBUG_FILTERING,
958 "message date [ %" CM_TIME_FORMAT " ] is not after [ %d ]\n",
959 info->date_t, prop->value);
962 return ret;
964 case MATCHCRITERIA_AGE_LOWER:
965 age_mult_hours = 24;
966 /* Fallthrough intended */
967 case MATCHCRITERIA_AGE_LOWER_HOURS:
969 gboolean ret;
970 gint age;
972 t = time(NULL);
973 age = ((t - info->date_t) / (60 * 60 * age_mult_hours));
974 ret = (age < prop->value);
976 /* debug output */
977 if (debug_filtering_session
978 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
979 if (ret) {
980 log_print(LOG_DEBUG_FILTERING,
981 "message age [ %d ] is lower than [ %d ]\n",
982 age, prop->value);
983 } else {
984 log_print(LOG_DEBUG_FILTERING,
985 "message age [ %d ] is not lower than [ %d ]\n",
986 age, prop->value);
989 return ret;
991 case MATCHCRITERIA_DATE_BEFORE:
993 gboolean ret;
995 ret = prop->value > info->date_t;
997 /* debug output */
998 if (debug_filtering_session
999 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
1000 if (ret) {
1001 log_print(LOG_DEBUG_FILTERING,
1002 "message date [ %" CM_TIME_FORMAT " ] is before [ %d ]\n",
1003 info->date_t, prop->value);
1004 } else {
1005 log_print(LOG_DEBUG_FILTERING,
1006 "message date [ %" CM_TIME_FORMAT " ] is not before [ %d ]\n",
1007 info->date_t, prop->value);
1010 return ret;
1012 case MATCHCRITERIA_SCORE_GREATER:
1014 gboolean ret = (info->score > prop->value);
1016 /* debug output */
1017 if (debug_filtering_session
1018 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
1019 if (ret) {
1020 log_print(LOG_DEBUG_FILTERING,
1021 "message score [ %d ] is greater than [ %d ]\n",
1022 info->score, prop->value);
1023 } else {
1024 log_print(LOG_DEBUG_FILTERING,
1025 "message score [ %d ] is not greater than [ %d ]\n",
1026 info->score, prop->value);
1029 return ret;
1031 case MATCHCRITERIA_SCORE_LOWER:
1033 gboolean ret = (info->score < prop->value);
1035 /* debug output */
1036 if (debug_filtering_session
1037 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
1038 if (ret) {
1039 log_print(LOG_DEBUG_FILTERING,
1040 "message score [ %d ] is lower than [ %d ]\n",
1041 info->score, prop->value);
1042 } else {
1043 log_print(LOG_DEBUG_FILTERING,
1044 "message score [ %d ] is not lower than [ %d ]\n",
1045 info->score, prop->value);
1048 return ret;
1050 case MATCHCRITERIA_SCORE_EQUAL:
1052 gboolean ret = (info->score == prop->value);
1054 /* debug output */
1055 if (debug_filtering_session
1056 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
1057 if (ret) {
1058 log_print(LOG_DEBUG_FILTERING,
1059 "message score [ %d ] is equal to [ %d ]\n",
1060 info->score, prop->value);
1061 } else {
1062 log_print(LOG_DEBUG_FILTERING,
1063 "message score [ %d ] is not equal to [ %d ]\n",
1064 info->score, prop->value);
1067 return ret;
1069 case MATCHCRITERIA_SIZE_GREATER:
1071 /* FIXME: info->size is a goffset */
1072 gboolean ret = (info->size > (goffset) prop->value);
1074 /* debug output */
1075 if (debug_filtering_session
1076 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
1077 if (ret) {
1078 log_print(LOG_DEBUG_FILTERING,
1079 "message size is greater than [ %d ]\n",
1080 prop->value);
1081 } else {
1082 log_print(LOG_DEBUG_FILTERING,
1083 "message size is not greater than [ %d ]\n",
1084 prop->value);
1087 return ret;
1089 case MATCHCRITERIA_SIZE_SMALLER:
1091 /* FIXME: info->size is a goffset */
1092 gboolean ret = (info->size < (goffset) prop->value);
1094 /* debug output */
1095 if (debug_filtering_session
1096 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
1097 if (ret) {
1098 log_print(LOG_DEBUG_FILTERING,
1099 "message size is smaller than [ %d ]\n",
1100 prop->value);
1101 } else {
1102 log_print(LOG_DEBUG_FILTERING,
1103 "message size is not smaller than [ %d ]\n",
1104 prop->value);
1107 return ret;
1109 case MATCHCRITERIA_SIZE_EQUAL:
1111 /* FIXME: info->size is a goffset */
1112 gboolean ret = (info->size == (goffset) prop->value);
1114 /* debug output */
1115 if (debug_filtering_session
1116 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
1117 if (ret) {
1118 log_print(LOG_DEBUG_FILTERING,
1119 "message size is equal to [ %d ]\n",
1120 prop->value);
1121 } else {
1122 log_print(LOG_DEBUG_FILTERING,
1123 "message size is not equal to [ %d ]\n",
1124 prop->value);
1127 return ret;
1129 case MATCHCRITERIA_PARTIAL:
1131 /* FIXME: info->size is a goffset */
1132 gboolean ret = (info->total_size != 0 && info->size != (goffset)info->total_size);
1134 /* debug output */
1135 if (debug_filtering_session
1136 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
1137 if (ret) {
1138 log_print(LOG_DEBUG_FILTERING,
1139 "message is partially downloaded, size is less than total size [ %d ])\n",
1140 info->total_size);
1141 } else {
1142 log_print(LOG_DEBUG_FILTERING,
1143 "message is not partially downloaded\n");
1146 return ret;
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);
1153 /* debug output */
1154 if (debug_filtering_session
1155 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
1156 if (ret) {
1157 log_print(LOG_DEBUG_FILTERING,
1158 "message is not partially downloaded\n");
1159 } else {
1160 log_print(LOG_DEBUG_FILTERING,
1161 "message is partially downloaded, size is less than total size [ %d ])\n",
1162 info->total_size);
1165 return ret;
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);
1187 default:
1188 return FALSE;
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)
1204 MatcherList *cond;
1206 cond = g_new0(MatcherList, 1);
1208 cond->matchers = matchers;
1209 cond->bool_and = bool_and;
1211 return cond;
1214 #ifdef G_OS_UNIX
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)
1224 int i = 0;
1225 gchar *expr = NULL;
1226 while (strings && strings[i] && *strings[i]) {
1227 int old_len = expr ? strlen(expr):0;
1228 int new_len = 0;
1229 gchar *tmpstr = NULL;
1231 if (g_utf8_validate(strings[i], -1, NULL))
1232 tmpstr = g_strdup(strings[i]);
1233 else
1234 tmpstr = conv_codeset_strdup(strings[i],
1235 conv_get_locale_charset_str_no_utf8(),
1236 CS_INTERNAL);
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));
1247 if (old_len) {
1248 strcpy(expr + old_len, "|(");
1249 strcpy(expr + old_len + 2, tmpstr);
1250 strcpy(expr + old_len + 2 + new_len, ")");
1251 } else {
1252 strcpy(expr+old_len, "(");
1253 strcpy(expr+old_len + 1, tmpstr);
1254 strcpy(expr+old_len + 1 + new_len, ")");
1256 g_free(tmpstr);
1257 i++;
1259 return expr;
1261 #endif
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);
1279 #ifdef G_OS_UNIX
1280 gchar *expr = NULL;
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,
1286 expr, 0);
1287 if (m == NULL) {
1288 /* print error message */
1289 debug_print("failed to allocate memory for matcherprop\n");
1290 } else {
1291 matchers = g_slist_append(matchers, m);
1294 g_free(expr);
1295 #else
1296 int i = 0;
1297 while (strings && strings[i] && *strings[i]) {
1298 m = matcherprop_new(MATCHCRITERIA_SUBJECT, NULL,
1299 case_sensitive? MATCHTYPE_MATCH: MATCHTYPE_MATCHCASE,
1300 strings[i], 0);
1301 if (m == NULL) {
1302 /* print error message */
1303 debug_print("failed to allocate memory for matcherprop\n");
1304 } else {
1305 matchers = g_slist_append(matchers, m);
1307 i++;
1309 #endif
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)
1322 GSList *l;
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);
1329 g_free(cond);
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,
1341 gchar *buf)
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);
1350 if (!header)
1351 return FALSE;
1352 if (procheader_headername_equal(header->name,
1353 matcher->header)) {
1354 if (matcher->criteria == MATCHCRITERIA_HEADER)
1355 result = matcherprop_string_match(matcher, header->body, context_str[CONTEXT_HEADER]);
1356 else
1357 result = !matcherprop_string_match(matcher, header->body, context_str[CONTEXT_HEADER]);
1358 procheader_header_free(header);
1359 return result;
1361 else {
1362 procheader_header_free(header);
1364 break;
1365 case MATCHCRITERIA_HEADERS_PART:
1366 case MATCHCRITERIA_HEADERS_CONT:
1367 case MATCHCRITERIA_MESSAGE:
1368 header = procheader_parse_header(buf);
1369 if (!header)
1370 return FALSE;
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);
1376 return result;
1377 case MATCHCRITERIA_NOT_HEADERS_CONT:
1378 case MATCHCRITERIA_NOT_HEADERS_PART:
1379 case MATCHCRITERIA_NOT_MESSAGE:
1380 header = procheader_parse_header(buf);
1381 if (!header)
1382 return FALSE;
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);
1388 return result;
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)
1398 match = MATCH_ANY;
1399 else if (strcasecmp(matcher->header, "All") == 0)
1400 match = MATCH_ALL;
1402 if (match == MATCH_ONE) {
1403 /* matching one address header exactly, is that the right one? */
1404 header = procheader_parse_header(buf);
1405 if (!header ||
1406 !procheader_headername_equal(header->name, matcher->header)) {
1407 procheader_header_free(header);
1408 return FALSE;
1410 address_list = address_list_append(address_list, header->body);
1411 if (address_list == NULL) {
1412 procheader_header_free(header);
1413 return FALSE;
1415 procheader_header_free(header);
1417 } else {
1418 header = procheader_parse_header(buf);
1419 if (!header)
1420 return FALSE;
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)
1433 return FALSE;
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)
1442 return !found;
1443 else
1444 return found;
1448 return FALSE;
1452 *\brief Check if the matcher structure wants headers to
1453 * be matched
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:
1471 return TRUE;
1472 default:
1473 return FALSE;
1478 *\brief Check if the matcher structure wants the message
1479 * to be matched (just perform an action on any
1480 * message)
1482 *\param matcher Matcher structure
1484 *\return gboolean TRUE if matcher condition should match
1485 * a message
1487 static gboolean matcherprop_criteria_message(MatcherProp *matcher)
1489 switch (matcher->criteria) {
1490 case MATCHCRITERIA_MESSAGE:
1491 case MATCHCRITERIA_NOT_MESSAGE:
1492 return TRUE;
1493 default:
1494 return FALSE;
1499 *\brief Check if a list of conditions matches one header in
1500 * a message file.
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)
1510 GSList *l;
1511 gchar *buf = NULL;
1512 gint ret;
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;
1519 if (matcher->done)
1520 continue;
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) {
1526 match = MATCH_ALL;
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);
1535 if (header &&
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)
1545 match = MATCH_ANY;
1546 else if (strcasecmp(matcher->header, "All") == 0)
1547 match = MATCH_ALL;
1548 else
1549 match = MATCH_ONE;
1550 } else {
1551 if (!header)
1552 continue;
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;
1564 } else {
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) {
1582 g_free(buf);
1583 return TRUE;
1587 g_free(buf);
1588 buf = NULL;
1591 return FALSE;
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:
1606 return TRUE;
1607 default:
1608 return FALSE;
1612 static gboolean matcherlist_match_binary_content(MatcherList *matchers, MimeInfo *partinfo)
1614 FILE *outfp;
1615 gchar buf[BUFFSIZE];
1616 GSList *l;
1618 if (!partinfo || partinfo->type == MIMETYPE_TEXT)
1619 return FALSE;
1620 else
1621 outfp = procmime_get_binary_content(partinfo);
1623 if (!outfp)
1624 return FALSE;
1626 while (claws_fgets(buf, sizeof(buf), outfp) != NULL) {
1627 strretchomp(buf);
1629 for (l = matchers->matchers ; l != NULL ; l = g_slist_next(l)) {
1630 MatcherProp *matcher = (MatcherProp *) l->data;
1632 if (matcher->done)
1633 continue;
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)
1640 continue;
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;
1651 } else
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);
1668 return TRUE;
1674 claws_fclose(outfp);
1675 return FALSE;
1678 static gboolean match_content_cb(const gchar *buf, gpointer data)
1680 MatcherList *matchers = (MatcherList *)data;
1681 gboolean all_done = TRUE;
1682 GSList *l;
1684 for (l = matchers->matchers ; l != NULL ; l = g_slist_next(l)) {
1685 MatcherProp *matcher = (MatcherProp *) l->data;
1687 if (matcher->done)
1688 continue;
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;
1699 } else
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) {
1715 return TRUE;
1719 if (!matcher->done)
1720 all_done = FALSE;
1722 return all_done;
1725 static gboolean matcherlist_match_text_content(MatcherList *matchers, MimeInfo *partinfo)
1727 if (partinfo->type != MIMETYPE_TEXT)
1728 return FALSE;
1730 return procmime_scan_text_content(partinfo, match_content_cb, matchers);
1734 *\brief Check if a line in a message file's body matches
1735 * the criteria
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);
1752 /* Skip headers */
1753 partinfo = procmime_mimeinfo_next(mimeinfo);
1755 for (; partinfo != NULL; partinfo = procmime_mimeinfo_next(partinfo)) {
1757 if (partinfo->type != MIMETYPE_TEXT && body_only)
1758 continue;
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);
1764 return TRUE;
1766 } else if (matcherlist_match_binary_content(matchers, partinfo)) {
1767 procmime_mimeinfo_free_all(&mimeinfo);
1768 return TRUE;
1771 if (body_only && first_text_found)
1772 break;
1774 procmime_mimeinfo_free_all(&mimeinfo);
1776 return FALSE;
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,
1789 gboolean result)
1791 gboolean read_headers;
1792 gboolean read_body;
1793 gboolean body_only;
1794 GSList *l;
1795 FILE *fp;
1796 gchar *file;
1798 /* file need to be read ? */
1800 read_headers = FALSE;
1801 read_body = FALSE;
1802 body_only = TRUE;
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))
1809 read_body = TRUE;
1810 if (matcherprop_criteria_message(matcher)) {
1811 read_headers = TRUE;
1812 read_body = TRUE;
1813 body_only = FALSE;
1815 matcher->result = FALSE;
1816 matcher->done = FALSE;
1819 if (!read_headers && !read_body)
1820 return result;
1822 file = procmsg_get_message_file_full(info, read_headers, read_body);
1823 if (file == NULL)
1824 return FALSE;
1826 if ((fp = claws_fopen(file, "rb")) == NULL) {
1827 FILE_OP_ERROR(file, "claws_fopen");
1828 g_free(file);
1829 return result;
1832 /* read the headers */
1834 if (read_headers) {
1835 if (matcherlist_match_headers(matchers, fp))
1836 read_body = FALSE;
1837 } else {
1838 procheader_skip_headers(fp);
1841 /* read the body */
1842 if (read_body) {
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) {
1854 result = TRUE;
1855 break;
1858 else {
1859 if (matchers->bool_and) {
1860 result = FALSE;
1861 break;
1867 g_free(file);
1869 claws_fclose(fp);
1871 return result;
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)
1884 GSList *l;
1885 gboolean result;
1887 if (!matchers)
1888 return FALSE;
1890 if (matchers->bool_and)
1891 result = TRUE;
1892 else
1893 result = FALSE;
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);
1903 g_free(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"));
1976 return TRUE;
1979 else {
1980 if (matchers->bool_and) {
1981 if (debug_filtering_session)
1982 log_status_nok(LOG_DEBUG_FILTERING, _("message does not match\n"));
1983 return FALSE;
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"));
1995 return TRUE;
1997 } else {
1998 if (matchers->bool_and) {
1999 if (debug_filtering_session)
2000 log_status_nok(LOG_DEBUG_FILTERING, _("message does not match\n"));
2001 return FALSE;
2005 if (debug_filtering_session) {
2006 if (result)
2007 log_status_ok(LOG_DEBUG_FILTERING, _("message matches\n"));
2008 else
2009 log_status_nok(LOG_DEBUG_FILTERING, _("message does not match\n"));
2011 return result;
2015 static gint quote_filter_str(gchar * result, guint size,
2016 const gchar * path)
2018 const gchar * p;
2019 gchar * result_p;
2020 guint remaining;
2022 result_p = result;
2023 remaining = size;
2025 for(p = path ; * p != '\0' ; p ++) {
2027 if ((* p != '\"') && (* p != '\\')) {
2028 if (remaining > 0) {
2029 * result_p = * p;
2030 result_p ++;
2031 remaining --;
2033 else {
2034 result[size - 1] = '\0';
2035 return -1;
2038 else {
2039 if (remaining >= 2) {
2040 * result_p = '\\';
2041 result_p ++;
2042 * result_p = * p;
2043 result_p ++;
2044 remaining -= 2;
2046 else {
2047 result[size - 1] = '\0';
2048 return -1;
2052 if (remaining > 0) {
2053 * result_p = '\0';
2055 else {
2056 result[size - 1] = '\0';
2057 return -1;
2060 return 0;
2064 gchar * matcher_quote_str(const gchar * src)
2066 gchar * res;
2067 gint len;
2069 len = strlen(src) * 2 + 1;
2070 res = g_malloc(len);
2071 quote_filter_str(res, len, src);
2073 return res;
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;
2088 int i;
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)
2098 return 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);
2152 return matcher_str;
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);
2161 return matcher_str;
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)
2171 return 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);
2187 else
2188 matcher_str = g_strdup_printf
2189 ("%s %s \"%s\"", criteria_str,
2190 matchtype_str, quoted_expr);
2191 g_free(quoted_expr);
2192 break;
2195 return matcher_str;
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)
2207 gint count;
2208 gchar **vstr;
2209 GSList *l;
2210 gchar **cur_str;
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)
2220 break;
2222 *cur_str = NULL;
2224 if (matchers->bool_and)
2225 result = g_strjoinv(" & ", vstr);
2226 else
2227 result = g_strjoinv(" | ", vstr);
2229 for (cur_str = vstr ; *cur_str != NULL ; cur_str ++)
2230 g_free(*cur_str);
2231 g_free(vstr);
2233 return result;
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];
2244 const gchar * str;
2246 if (s != NULL)
2247 str = s;
2248 else
2249 str = d;
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;
2271 gchar *p;
2272 gint size;
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') {
2285 if (*s == '%') {
2286 s++;
2287 switch (*s) {
2288 case '%':
2289 size -= 1;
2290 break;
2291 case 's': /* subject */
2292 size += STRLEN_DEFAULT(info->subject, no_subject) - 2;
2293 break;
2294 case 'f': /* from */
2295 size += STRLEN_DEFAULT(info->from, no_from) - 2;
2296 break;
2297 case 't': /* to */
2298 size += STRLEN_DEFAULT(info->to, no_to) - 2;
2299 break;
2300 case 'c': /* cc */
2301 size += STRLEN_DEFAULT(info->cc, no_cc) - 2;
2302 break;
2303 case 'd': /* date */
2304 size += STRLEN_DEFAULT(info->date, no_date) - 2;
2305 break;
2306 case 'i': /* message-id */
2307 size += STRLEN_DEFAULT(info->msgid, no_msgid) - 2;
2308 break;
2309 case 'n': /* newsgroups */
2310 size += STRLEN_DEFAULT(info->newsgroups, no_newsgroups) - 2;
2311 break;
2312 case 'r': /* references */
2313 /* FIXME: using the inreplyto header for reference */
2314 size += STRLEN_DEFAULT(info->inreplyto, no_references) - 2;
2315 break;
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");
2322 return NULL;
2324 else {
2325 size += strlen(filename) - 2;
2327 break;
2329 s++;
2331 else s++;
2334 /* as the string can be quoted, we double the result */
2335 size *= 2;
2337 processed_cmd = g_new0(gchar, size);
2338 s = cmd;
2339 p = processed_cmd;
2341 while (*s != '\0') {
2342 if (*s == '%') {
2343 s++;
2344 switch (*s) {
2345 case '%':
2346 *p = '%';
2347 p++;
2348 break;
2349 case 's': /* subject */
2350 add_str_default(&p, info->subject,
2351 no_subject);
2352 break;
2353 case 'f': /* from */
2354 add_str_default(&p, info->from,
2355 no_from);
2356 break;
2357 case 't': /* to */
2358 add_str_default(&p, info->to,
2359 no_to);
2360 break;
2361 case 'c': /* cc */
2362 add_str_default(&p, info->cc,
2363 no_cc);
2364 break;
2365 case 'd': /* date */
2366 add_str_default(&p, info->date,
2367 no_date);
2368 break;
2369 case 'i': /* message-id */
2370 add_str_default(&p, info->msgid,
2371 no_msgid);
2372 break;
2373 case 'n': /* newsgroups */
2374 add_str_default(&p, info->newsgroups,
2375 no_newsgroups);
2376 break;
2377 case 'r': /* references */
2378 /* FIXME: using the inreplyto header for references */
2379 add_str_default(&p, info->inreplyto, no_references);
2380 break;
2381 case 'F': /* file */
2382 if (filename != NULL)
2383 add_str_default(&p, filename, NULL);
2384 break;
2385 default:
2386 *p = '%';
2387 p++;
2388 *p = *s;
2389 p++;
2390 break;
2392 s++;
2394 else {
2395 *p = *s;
2396 p++;
2397 s++;
2400 g_free(filename);
2402 return processed_cmd;
2404 #undef STRLEN_DEFAULT
2405 #undef STRLEN_ZERO
2407 /* ************************************************************ */
2411 *\brief Write filtering list to file
2413 *\param fp File
2414 *\param prefs_filtering List of filtering conditions
2416 static int prefs_filtering_write(FILE *fp, GSList *prefs_filtering)
2418 GSList *cur = NULL;
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))
2426 continue;
2428 if (NULL == (filtering_str = filteringprop_to_string(prop)))
2429 continue;
2431 if (prop->enabled) {
2432 if (claws_fputs("enabled ", fp) == EOF) {
2433 FILE_OP_ERROR("filtering config", "claws_fputs");
2434 g_free(filtering_str);
2435 return -1;
2437 } else {
2438 if (claws_fputs("disabled ", fp) == EOF) {
2439 FILE_OP_ERROR("filtering config", "claws_fputs");
2440 g_free(filtering_str);
2441 return -1;
2445 if (claws_fputs("rulename \"", fp) == EOF) {
2446 FILE_OP_ERROR("filtering config", "claws_fputs");
2447 g_free(filtering_str);
2448 return -1;
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);
2456 return -1;
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);
2463 return -1;
2466 tmp_name ++;
2468 if (claws_fputs("\" ", fp) == EOF) {
2469 FILE_OP_ERROR("filtering config", "claws_fputs");
2470 g_free(filtering_str);
2471 return -1;
2474 if (prop->account_id != 0) {
2475 gchar *tmp = NULL;
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);
2481 g_free(tmp);
2482 return -1;
2484 g_free(tmp);
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);
2491 return -1;
2493 g_free(filtering_str);
2496 return 0;
2499 typedef struct _NodeLoopData {
2500 FILE *fp;
2501 gboolean error;
2502 } 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)
2514 FolderItem *item;
2515 NodeLoopData *data = (NodeLoopData *)d;
2516 gchar *id;
2517 GSList *prefs_filtering;
2519 item = node->data;
2520 /* prevent warning */
2521 if (item->path == NULL)
2522 return FALSE;
2523 id = folder_item_get_identifier(item);
2524 if (id == NULL)
2525 return FALSE;
2526 prefs_filtering = item->prefs->processing;
2528 if (prefs_filtering != NULL) {
2529 if (fprintf(data->fp, "[%s]\n", id) < 0) {
2530 data->error = TRUE;
2531 goto fail;
2533 if (prefs_filtering_write(data->fp, prefs_filtering) < 0) {
2534 data->error = TRUE;
2535 goto fail;
2537 if (claws_fputc('\n', data->fp) == EOF) {
2538 data->error = TRUE;
2539 goto fail;
2542 fail:
2543 g_free(id);
2545 return FALSE;
2549 *\brief Save matchers from folder items
2551 *\param fp File
2553 static int prefs_matcher_save(FILE *fp)
2555 GList *cur;
2556 NodeLoopData data;
2558 data.fp = fp;
2559 data.error = FALSE;
2561 for (cur = folder_get_list() ; cur != NULL ; cur = g_list_next(cur)) {
2562 Folder *folder;
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)
2570 return -1;
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)
2576 return -1;
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)
2582 return -1;
2584 /* filtering rules */
2585 if (fprintf(fp, "[filtering]\n") < 0 ||
2586 prefs_filtering_write(fp, filtering_rules) < 0 ||
2587 claws_fputc('\n', fp) == EOF)
2588 return -1;
2590 return 0;
2594 *\brief Write filtering / matcher configuration file
2596 void prefs_matcher_write_config(void)
2598 gchar *rcpath;
2599 PrefFile *pfile;
2601 debug_print("Writing matcher configuration...\n");
2603 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
2604 MATCHER_RC, NULL);
2606 if ((pfile = prefs_write_open(rcpath)) == NULL) {
2607 g_warning("failed to write configuration to file");
2608 g_free(rcpath);
2609 return;
2612 g_free(rcpath);
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)
2627 gchar *rcpath;
2628 FILE *f;
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");
2636 g_free(rcpath);
2638 if (f != NULL) {
2639 matcher_parser_start_parsing(f);
2640 claws_fclose(matcher_parserin);