2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 2002-2004 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 2 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, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25 #include <glib/gi18n.h>
37 #include "procheader.h"
39 #include "matcher_parser.h"
40 #include "prefs_gtk.h"
41 #include "addr_compl.h"
43 #include "quoted-printable.h"
46 #include "prefs_common.h"
50 *\brief Keyword lookup element
53 gint id; /*!< keyword id */
54 gchar *str; /*!< keyword */
56 typedef struct _MatchParser MatchParser;
59 *\brief Table with strings and ids used by the lexer and
60 * the parser. New keywords can be added here.
62 static const MatchParser matchparser_tab[] = {
64 {MATCHCRITERIA_ALL, "all"},
65 {MATCHCRITERIA_UNREAD, "unread"},
66 {MATCHCRITERIA_NOT_UNREAD, "~unread"},
67 {MATCHCRITERIA_NEW, "new"},
68 {MATCHCRITERIA_NOT_NEW, "~new"},
69 {MATCHCRITERIA_MARKED, "marked"},
70 {MATCHCRITERIA_NOT_MARKED, "~marked"},
71 {MATCHCRITERIA_DELETED, "deleted"},
72 {MATCHCRITERIA_NOT_DELETED, "~deleted"},
73 {MATCHCRITERIA_REPLIED, "replied"},
74 {MATCHCRITERIA_NOT_REPLIED, "~replied"},
75 {MATCHCRITERIA_FORWARDED, "forwarded"},
76 {MATCHCRITERIA_NOT_FORWARDED, "~forwarded"},
77 {MATCHCRITERIA_LOCKED, "locked"},
78 {MATCHCRITERIA_NOT_LOCKED, "~locked"},
79 {MATCHCRITERIA_COLORLABEL, "colorlabel"},
80 {MATCHCRITERIA_NOT_COLORLABEL, "~colorlabel"},
81 {MATCHCRITERIA_IGNORE_THREAD, "ignore_thread"},
82 {MATCHCRITERIA_NOT_IGNORE_THREAD, "~ignore_thread"},
83 {MATCHCRITERIA_SPAM, "spam"},
84 {MATCHCRITERIA_NOT_SPAM, "~spam"},
87 {MATCHCRITERIA_SUBJECT, "subject"},
88 {MATCHCRITERIA_NOT_SUBJECT, "~subject"},
89 {MATCHCRITERIA_FROM, "from"},
90 {MATCHCRITERIA_NOT_FROM, "~from"},
91 {MATCHCRITERIA_TO, "to"},
92 {MATCHCRITERIA_NOT_TO, "~to"},
93 {MATCHCRITERIA_CC, "cc"},
94 {MATCHCRITERIA_NOT_CC, "~cc"},
95 {MATCHCRITERIA_TO_OR_CC, "to_or_cc"},
96 {MATCHCRITERIA_NOT_TO_AND_NOT_CC, "~to_or_cc"},
97 {MATCHCRITERIA_AGE_GREATER, "age_greater"},
98 {MATCHCRITERIA_AGE_LOWER, "age_lower"},
99 {MATCHCRITERIA_NEWSGROUPS, "newsgroups"},
100 {MATCHCRITERIA_NOT_NEWSGROUPS, "~newsgroups"},
101 {MATCHCRITERIA_INREPLYTO, "inreplyto"},
102 {MATCHCRITERIA_NOT_INREPLYTO, "~inreplyto"},
103 {MATCHCRITERIA_REFERENCES, "references"},
104 {MATCHCRITERIA_NOT_REFERENCES, "~references"},
105 {MATCHCRITERIA_SCORE_GREATER, "score_greater"},
106 {MATCHCRITERIA_SCORE_LOWER, "score_lower"},
107 {MATCHCRITERIA_SCORE_EQUAL, "score_equal"},
108 {MATCHCRITERIA_PARTIAL, "partial"},
109 {MATCHCRITERIA_NOT_PARTIAL, "~partial"},
110 {MATCHCRITERIA_FOUND_IN_ADDRESSBOOK, "found_in_addressbook"},
111 {MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK, "~found_in_addressbook"},
113 {MATCHCRITERIA_SIZE_GREATER, "size_greater"},
114 {MATCHCRITERIA_SIZE_SMALLER, "size_smaller"},
115 {MATCHCRITERIA_SIZE_EQUAL, "size_equal"},
117 /* content have to be read */
118 {MATCHCRITERIA_HEADER, "header"},
119 {MATCHCRITERIA_NOT_HEADER, "~header"},
120 {MATCHCRITERIA_HEADERS_PART, "headers_part"},
121 {MATCHCRITERIA_NOT_HEADERS_PART, "~headers_part"},
122 {MATCHCRITERIA_MESSAGE, "message"},
123 {MATCHCRITERIA_NOT_MESSAGE, "~message"},
124 {MATCHCRITERIA_BODY_PART, "body_part"},
125 {MATCHCRITERIA_NOT_BODY_PART, "~body_part"},
126 {MATCHCRITERIA_TEST, "test"},
127 {MATCHCRITERIA_NOT_TEST, "~test"},
130 {MATCHTYPE_MATCHCASE, "matchcase"},
131 {MATCHTYPE_MATCH, "match"},
132 {MATCHTYPE_REGEXPCASE, "regexpcase"},
133 {MATCHTYPE_REGEXP, "regexp"},
136 {MATCHACTION_SCORE, "score"}, /* for backward compatibility */
137 {MATCHACTION_MOVE, "move"},
138 {MATCHACTION_COPY, "copy"},
139 {MATCHACTION_DELETE, "delete"},
140 {MATCHACTION_MARK, "mark"},
141 {MATCHACTION_UNMARK, "unmark"},
142 {MATCHACTION_LOCK, "lock"},
143 {MATCHACTION_UNLOCK, "unlock"},
144 {MATCHACTION_MARK_AS_READ, "mark_as_read"},
145 {MATCHACTION_MARK_AS_UNREAD, "mark_as_unread"},
146 {MATCHACTION_MARK_AS_SPAM, "mark_as_spam"},
147 {MATCHACTION_MARK_AS_HAM, "mark_as_ham"},
148 {MATCHACTION_FORWARD, "forward"},
149 {MATCHACTION_FORWARD_AS_ATTACHMENT, "forward_as_attachment"},
150 {MATCHACTION_EXECUTE, "execute"},
151 {MATCHACTION_COLOR, "color"},
152 {MATCHACTION_REDIRECT, "redirect"},
153 {MATCHACTION_CHANGE_SCORE, "change_score"},
154 {MATCHACTION_SET_SCORE, "set_score"},
155 {MATCHACTION_STOP, "stop"},
156 {MATCHACTION_HIDE, "hide"},
157 {MATCHACTION_IGNORE, "ignore"},
158 {MATCHACTION_ADD_TO_ADDRESSBOOK, "add_to_addressbook"}
167 extern gboolean debug_filtering_session;
170 *\brief Look up table with keywords defined in \sa matchparser_tab
172 static GHashTable *matchparser_hashtab;
175 *\brief Translate keyword id to keyword string
177 *\param id Id of keyword
179 *\return const gchar * Keyword
181 const gchar *get_matchparser_tab_str(gint id)
185 for (i = 0; i < sizeof matchparser_tab / sizeof matchparser_tab[0]; i++) {
186 if (matchparser_tab[i].id == id)
187 return matchparser_tab[i].str;
193 *\brief Create keyword lookup table
195 static void create_matchparser_hashtab(void)
199 if (matchparser_hashtab) return;
200 matchparser_hashtab = g_hash_table_new(g_str_hash, g_str_equal);
201 for (i = 0; i < sizeof matchparser_tab / sizeof matchparser_tab[0]; i++)
202 g_hash_table_insert(matchparser_hashtab,
203 matchparser_tab[i].str,
204 (gpointer) &matchparser_tab[i]);
208 *\brief Return a keyword id from a keyword string
210 *\param str Keyword string
212 *\return gint Keyword id
214 gint get_matchparser_tab_id(const gchar *str)
218 if (NULL != (res = g_hash_table_lookup(matchparser_hashtab, str))) {
224 /* **************** data structure allocation **************** */
227 *\brief Allocate a structure for a filtering / scoring
228 * "condition" (a matcher structure)
230 *\param criteria Criteria ID (MATCHCRITERIA_XXXX)
231 *\param header Header string (if criteria is MATCHCRITERIA_HEADER
232 or MATCHCRITERIA_FOUND_IN_ADDRESSBOOK)
233 *\param matchtype Type of action (MATCHTYPE_XXX)
234 *\param expr String value or expression to check
235 *\param value Integer value to check
237 *\return MatcherProp * Pointer to newly allocated structure
239 MatcherProp *matcherprop_new(gint criteria, const gchar *header,
240 gint matchtype, const gchar *expr,
245 prop = g_new0(MatcherProp, 1);
246 prop->criteria = criteria;
247 prop->header = header != NULL ? g_strdup(header) : NULL;
248 prop->expr = expr != NULL ? g_strdup(expr) : NULL;
249 prop->matchtype = matchtype;
258 *\brief Free a matcher structure
260 *\param prop Pointer to matcher structure allocated with
263 void matcherprop_free(MatcherProp *prop)
266 g_free(prop->header);
267 if (prop->preg != NULL) {
275 *\brief Copy a matcher structure
277 *\param src Matcher structure to copy
279 *\return MatcherProp * Pointer to newly allocated matcher structure
281 MatcherProp *matcherprop_copy(const MatcherProp *src)
283 MatcherProp *prop = g_new0(MatcherProp, 1);
285 prop->criteria = src->criteria;
286 prop->header = src->header ? g_strdup(src->header) : NULL;
287 prop->expr = src->expr ? g_strdup(src->expr) : NULL;
288 prop->matchtype = src->matchtype;
290 prop->preg = NULL; /* will be re-evaluated */
291 prop->value = src->value;
292 prop->error = src->error;
296 /* ************** match ******************************/
298 static gboolean match_with_addresses_in_addressbook
299 (MatcherProp *prop, GSList *address_list, gint type,
300 gchar* folderpath, gint match)
303 gboolean found = FALSE;
306 g_return_val_if_fail(address_list != NULL, FALSE);
308 debug_print("match_with_addresses_in_addressbook(%d, %s)\n",
309 g_slist_length(address_list), folderpath);
311 if (folderpath == NULL ||
312 strcasecmp(folderpath, _("Any")) == 0 ||
318 start_address_completion(path);
320 for (walk = address_list; walk != NULL; walk = walk->next) {
321 /* exact matching of email address */
322 guint num_addr = complete_address(walk->data);
325 /* skip first item (this is the search string itself) */
327 for (; i < num_addr && !found; i++) {
328 gchar *addr = get_complete_address(i);
329 extract_address(addr);
330 if (strcasecmp(addr, walk->data) == 0) {
334 if (debug_filtering_session
335 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
336 log_print(LOG_DEBUG_FILTERING,
337 "address [ %s ] matches\n",
338 (gchar *)walk->data);
345 if (debug_filtering_session
346 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH
348 log_print(LOG_DEBUG_FILTERING,
349 "address [ %s ] doesn't match\n",
350 (gchar *)walk->data);
354 if (match == MATCH_ALL) {
355 /* if matching all addresses, stop if one doesn't match */
358 if (debug_filtering_session
359 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
360 log_print(LOG_DEBUG_FILTERING,
361 "not all address match (matching all)\n");
365 } else if (match == MATCH_ANY) {
366 /* if matching any address, stop if one does match */
369 if (debug_filtering_session
370 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
371 log_print(LOG_DEBUG_FILTERING,
372 "at least one address matches (matching any)\n");
377 /* MATCH_ONE: there should be only one loop iteration */
380 end_address_completion();
386 *\brief Find out if a string matches a condition
388 *\param prop Matcher structure
389 *\param str String to check
391 *\return gboolean TRUE if str matches the condition in the
394 static gboolean matcherprop_string_match(MatcherProp *prop, const gchar *str,
395 const gchar *debug_context)
399 gboolean ret = FALSE;
404 switch (prop->matchtype) {
405 case MATCHTYPE_REGEXPCASE:
406 case MATCHTYPE_REGEXP:
407 if (!prop->preg && (prop->error == 0)) {
408 prop->preg = g_new0(regex_t, 1);
409 /* if regexp then don't use the escaped string */
410 if (regcomp(prop->preg, prop->expr,
411 REG_NOSUB | REG_EXTENDED
412 | ((prop->matchtype == MATCHTYPE_REGEXPCASE)
413 ? REG_ICASE : 0)) != 0) {
419 if (prop->preg == NULL)
422 if (regexec(prop->preg, str, 0, NULL, 0) == 0)
428 if (debug_filtering_session
429 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
430 gchar *stripped = g_strdup(str);
432 strretchomp(stripped);
434 log_print(LOG_DEBUG_FILTERING,
435 "%s value [ %s ] matches regular expression [ %s ]\n",
436 debug_context, stripped, prop->expr);
438 log_print(LOG_DEBUG_FILTERING,
439 "%s value [ %s ] doesn't matches regular expression [ %s ]\n",
440 debug_context, stripped, prop->expr);
446 case MATCHTYPE_MATCH:
447 ret = (strstr(str, prop->expr) != NULL);
450 if (debug_filtering_session
451 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
452 gchar *stripped = g_strdup(str);
454 strretchomp(stripped);
456 log_print(LOG_DEBUG_FILTERING,
457 "%s value [ %s ] contains [ %s ] (case sensitive)\n",
458 debug_context, stripped, prop->expr);
460 log_print(LOG_DEBUG_FILTERING,
461 "%s value [ %s ] doesn't contains [ %s ] (case sensitive)\n",
462 debug_context, stripped, prop->expr);
468 /* FIXME: put upper in unesc_str */
469 case MATCHTYPE_MATCHCASE:
470 str2 = alloca(strlen(prop->expr) + 1);
471 strcpy(str2, prop->expr);
473 str1 = alloca(strlen(str) + 1);
476 ret = (strstr(str1, str2) != NULL);
479 if (debug_filtering_session
480 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
481 gchar *stripped = g_strdup(str);
483 strretchomp(stripped);
485 log_print(LOG_DEBUG_FILTERING,
486 "%s value [ %s ] contains [ %s ] (case insensitive)\n",
487 debug_context, stripped, prop->expr);
489 log_print(LOG_DEBUG_FILTERING,
490 "%s [ %s ] doesn't contains [ %s ] (case insensitive)\n",
491 debug_context, stripped, prop->expr);
504 *\brief Find out if the string-ed list matches a condition
506 *\param prop Matcher structure
507 *\param list GSList of strings to check
509 *\return gboolean TRUE if str matches the condition in the
512 static gboolean matcherprop_list_match(MatcherProp *prop, const GSList *list,
513 const gchar *debug_context)
517 for(cur = list; cur != NULL; cur = cur->next) {
518 if (matcherprop_string_match(prop, (gchar *)cur->data, debug_context))
524 /* FIXME body search is a hack. */
525 static gboolean matcherprop_string_decode_match(MatcherProp *prop, const gchar *str,
526 const gchar *debug_context)
530 gboolean res = FALSE;
535 /* we try to decode QP first, because it's faster than base64 */
536 qp_decode_const(tmp, BUFFSIZE-1, str);
537 if (!g_utf8_validate(tmp, -1, NULL)) {
538 utf = conv_codeset_strdup
539 (tmp, conv_get_locale_charset_str_no_utf8(),
541 res = matcherprop_string_match(prop, utf, debug_context);
544 res = matcherprop_string_match(prop, tmp, debug_context);
547 if (res == FALSE && (strchr(prop->expr, '=') || strchr(prop->expr, '_')
548 || strchr(str, '=') || strchr(str, '_'))) {
549 /* if searching for something with an equal char, maybe
550 * we should try to match the non-decoded string.
551 * In case it was not qp-encoded. */
552 if (!g_utf8_validate(str, -1, NULL)) {
553 utf = conv_codeset_strdup
554 (str, conv_get_locale_charset_str_no_utf8(),
556 res = matcherprop_string_match(prop, utf, debug_context);
559 res = matcherprop_string_match(prop, str, debug_context);
563 /* FIXME base64 decoding is too slow, especially since text can
564 * easily be handled as base64. Don't even try now. */
570 typedef struct _thread_data {
577 static void *matcher_test_thread(void *data)
579 thread_data *td = (thread_data *)data;
582 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
583 pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
585 result = system(td->cmd);
586 if (result) perror("system");
587 td->done = TRUE; /* let the caller thread join() */
588 return GINT_TO_POINTER(result);
593 *\brief Execute a command defined in the matcher structure
595 *\param prop Pointer to matcher structure
596 *\param info Pointer to message info structure
598 *\return gboolean TRUE if command was executed succesfully
600 static gboolean matcherprop_match_test(const MatcherProp *prop,
608 thread_data *td = g_new0(thread_data, 1);
610 time_t start_time = time(NULL);
613 file = procmsg_get_message_file(info);
618 cmd = matching_build_command(prop->expr, info);
622 #if (defined USE_PTHREAD && defined __GLIBC__ && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 3)))
624 if (debug_filtering_session
625 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
626 log_print(LOG_DEBUG_FILTERING,
627 "starting threaded command [ %s ]\n",
633 if (pthread_create(&pt, PTHREAD_CREATE_JOINABLE,
634 matcher_test_thread, td) != 0)
635 retval = system(cmd);
637 printf("waiting for test thread\n");
639 /* don't let the interface freeze while waiting */
641 if (time(NULL) - start_time > 30) {
647 pthread_join(pt, &res);
648 retval = GPOINTER_TO_INT(res);
649 printf(" test thread returned %d\n", retval);
654 if (debug_filtering_session
655 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
656 log_print(LOG_DEBUG_FILTERING,
657 "starting synchronous command [ %s ]\n",
661 retval = system(cmd);
663 debug_print("Command exit code: %d\n", retval);
666 if (debug_filtering_session
667 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
668 log_print(LOG_DEBUG_FILTERING,
669 "command returned [ %d ]\n",
674 return (retval == 0);
678 *\brief Check if a message matches the condition in a matcher
681 *\param prop Pointer to matcher structure
682 *\param info Pointer to message info
684 *\return gboolean TRUE if a match
686 gboolean matcherprop_match(MatcherProp *prop,
691 switch(prop->criteria) {
692 case MATCHCRITERIA_ALL:
694 case MATCHCRITERIA_UNREAD:
695 return MSG_IS_UNREAD(info->flags);
696 case MATCHCRITERIA_NOT_UNREAD:
697 return !MSG_IS_UNREAD(info->flags);
698 case MATCHCRITERIA_NEW:
699 return MSG_IS_NEW(info->flags);
700 case MATCHCRITERIA_NOT_NEW:
701 return !MSG_IS_NEW(info->flags);
702 case MATCHCRITERIA_MARKED:
703 return MSG_IS_MARKED(info->flags);
704 case MATCHCRITERIA_NOT_MARKED:
705 return !MSG_IS_MARKED(info->flags);
706 case MATCHCRITERIA_DELETED:
707 return MSG_IS_DELETED(info->flags);
708 case MATCHCRITERIA_NOT_DELETED:
709 return !MSG_IS_DELETED(info->flags);
710 case MATCHCRITERIA_REPLIED:
711 return MSG_IS_REPLIED(info->flags);
712 case MATCHCRITERIA_NOT_REPLIED:
713 return !MSG_IS_REPLIED(info->flags);
714 case MATCHCRITERIA_FORWARDED:
715 return MSG_IS_FORWARDED(info->flags);
716 case MATCHCRITERIA_NOT_FORWARDED:
717 return !MSG_IS_FORWARDED(info->flags);
718 case MATCHCRITERIA_LOCKED:
719 return MSG_IS_LOCKED(info->flags);
720 case MATCHCRITERIA_NOT_LOCKED:
721 return !MSG_IS_LOCKED(info->flags);
722 case MATCHCRITERIA_SPAM:
723 return MSG_IS_SPAM(info->flags);
724 case MATCHCRITERIA_NOT_SPAM:
725 return !MSG_IS_SPAM(info->flags);
726 case MATCHCRITERIA_COLORLABEL:
728 gint color = MSG_GET_COLORLABEL_VALUE(info->flags);
729 gboolean ret = (color == prop->value);
732 if (debug_filtering_session
733 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
735 log_print(LOG_DEBUG_FILTERING,
736 "message color value [ %d ] matches color value [ %d ]\n",
739 log_print(LOG_DEBUG_FILTERING,
740 "message color value [ %d ] doesn't matches color value [ %d ]\n",
746 case MATCHCRITERIA_NOT_COLORLABEL:
748 gint color = MSG_GET_COLORLABEL_VALUE(info->flags);
749 gboolean ret = (color != prop->value);
752 if (debug_filtering_session
753 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
755 log_print(LOG_DEBUG_FILTERING,
756 "message color value [ %d ] matches color value [ %d ]\n",
759 log_print(LOG_DEBUG_FILTERING,
760 "message color value [ %d ] doesn't matches color value [ %d ]\n",
766 case MATCHCRITERIA_IGNORE_THREAD:
767 return MSG_IS_IGNORE_THREAD(info->flags);
768 case MATCHCRITERIA_NOT_IGNORE_THREAD:
769 return !MSG_IS_IGNORE_THREAD(info->flags);
770 case MATCHCRITERIA_SUBJECT:
771 return matcherprop_string_match(prop, info->subject, _("subject"));
772 case MATCHCRITERIA_NOT_SUBJECT:
773 return !matcherprop_string_match(prop, info->subject, _("subject"));
774 case MATCHCRITERIA_FROM:
775 return matcherprop_string_match(prop, info->from, _("From: header"));
776 case MATCHCRITERIA_NOT_FROM:
777 return !matcherprop_string_match(prop, info->from, _("From: header"));
778 case MATCHCRITERIA_TO:
779 return matcherprop_string_match(prop, info->to, _("To: header"));
780 case MATCHCRITERIA_NOT_TO:
781 return !matcherprop_string_match(prop, info->to, _("To: header"));
782 case MATCHCRITERIA_CC:
783 return matcherprop_string_match(prop, info->cc, _("Cc: header"));
784 case MATCHCRITERIA_NOT_CC:
785 return !matcherprop_string_match(prop, info->cc, _("Cc: header"));
786 case MATCHCRITERIA_TO_OR_CC:
787 return matcherprop_string_match(prop, info->to, _("To: header"))
788 || matcherprop_string_match(prop, info->cc, _("Cc: header"));
789 case MATCHCRITERIA_NOT_TO_AND_NOT_CC:
790 return !(matcherprop_string_match(prop, info->to, _("To:"))
791 || matcherprop_string_match(prop, info->cc, _("Cc:")));
792 case MATCHCRITERIA_AGE_GREATER:
798 age = ((t - info->date_t) / (60 * 60 * 24));
799 ret = (age > prop->value);
802 if (debug_filtering_session
803 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
805 log_print(LOG_DEBUG_FILTERING,
806 "message age [ %d ] is greater than [ %d ]\n",
809 log_print(LOG_DEBUG_FILTERING,
810 "message age [ %d ] is not greater than [ %d ]\n",
816 case MATCHCRITERIA_AGE_LOWER:
822 age = ((t - info->date_t) / (60 * 60 * 24));
823 ret = (age < prop->value);
826 if (debug_filtering_session
827 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
829 log_print(LOG_DEBUG_FILTERING,
830 "message age [ %d ] is lower than [ %d ]\n",
833 log_print(LOG_DEBUG_FILTERING,
834 "message age [ %d ] is not lower than [ %d ]\n",
840 case MATCHCRITERIA_SCORE_GREATER:
842 gboolean ret = (info->score > 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 score [ %d ] is greater than [ %d ]\n",
850 info->score, prop->value);
852 log_print(LOG_DEBUG_FILTERING,
853 "message score [ %d ] is not greater than [ %d ]\n",
854 info->score, prop->value);
859 case MATCHCRITERIA_SCORE_LOWER:
861 gboolean ret = (info->score < prop->value);
864 if (debug_filtering_session
865 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
867 log_print(LOG_DEBUG_FILTERING,
868 "message score [ %d ] is lower than [ %d ]\n",
869 info->score, prop->value);
871 log_print(LOG_DEBUG_FILTERING,
872 "message score [ %d ] is not lower than [ %d ]\n",
873 info->score, prop->value);
878 case MATCHCRITERIA_SCORE_EQUAL:
880 gboolean ret = (info->score == prop->value);
883 if (debug_filtering_session
884 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
886 log_print(LOG_DEBUG_FILTERING,
887 "message score [ %d ] is equal to [ %d ]\n",
888 info->score, prop->value);
890 log_print(LOG_DEBUG_FILTERING,
891 "message score [ %d ] is not equal to [ %d ]\n",
892 info->score, prop->value);
897 case MATCHCRITERIA_SIZE_GREATER:
899 /* FIXME: info->size is an off_t */
900 gboolean ret = (info->size > (off_t) prop->value);
903 if (debug_filtering_session
904 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
906 log_print(LOG_DEBUG_FILTERING,
907 "message size [ %ld ] is greater than [ %d ]\n",
908 info->size, prop->value);
910 log_print(LOG_DEBUG_FILTERING,
911 "message size [ %ld ] is not greater than [ %d ]\n",
912 info->size, prop->value);
917 case MATCHCRITERIA_SIZE_SMALLER:
919 /* FIXME: info->size is an off_t */
920 gboolean ret = (info->size < (off_t) prop->value);
923 if (debug_filtering_session
924 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
926 log_print(LOG_DEBUG_FILTERING,
927 "message size [ %ld ] is smaller than [ %d ]\n",
928 info->size, prop->value);
930 log_print(LOG_DEBUG_FILTERING,
931 "message size [ %ld ] is not smaller than [ %d ]\n",
932 info->size, prop->value);
937 case MATCHCRITERIA_SIZE_EQUAL:
939 /* FIXME: info->size is an off_t */
940 gboolean ret = (info->size == (off_t) prop->value);
943 if (debug_filtering_session
944 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
946 log_print(LOG_DEBUG_FILTERING,
947 "message size [ %ld ] is equal to [ %d ]\n",
948 info->size, prop->value);
950 log_print(LOG_DEBUG_FILTERING,
951 "message size [ %ld ] is not equal to [ %d ]\n",
952 info->size, prop->value);
957 case MATCHCRITERIA_PARTIAL:
959 /* FIXME: info->size is an off_t */
960 gboolean ret = (info->total_size != 0 && info->size != (off_t)info->total_size);
963 if (debug_filtering_session
964 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
966 log_print(LOG_DEBUG_FILTERING,
967 "message is partially downloaded, size [ %ld ] is less than total size [ %d ])\n",
968 info->size, info->total_size);
970 log_print(LOG_DEBUG_FILTERING,
971 "message is not partially downloaded\n");
976 case MATCHCRITERIA_NOT_PARTIAL:
978 /* FIXME: info->size is an off_t */
979 gboolean ret = (info->total_size == 0 || info->size == (off_t)info->total_size);
982 if (debug_filtering_session
983 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
985 log_print(LOG_DEBUG_FILTERING,
986 "message is not partially downloaded\n");
988 log_print(LOG_DEBUG_FILTERING,
989 "message is partially downloaded, size [ %ld ] is less than total size [ %d ])\n",
990 info->size, info->total_size);
995 case MATCHCRITERIA_NEWSGROUPS:
996 return matcherprop_string_match(prop, info->newsgroups, _("Newsgroups: header"));
997 case MATCHCRITERIA_NOT_NEWSGROUPS:
998 return !matcherprop_string_match(prop, info->newsgroups, _("Newsgroups: header"));
999 case MATCHCRITERIA_INREPLYTO:
1000 return matcherprop_string_match(prop, info->inreplyto, _("InReplyTo: header"));
1001 case MATCHCRITERIA_NOT_INREPLYTO:
1002 return !matcherprop_string_match(prop, info->inreplyto, _("InReplyTo: header"));
1003 case MATCHCRITERIA_REFERENCES:
1004 return matcherprop_list_match(prop, info->references, _("References: header"));
1005 case MATCHCRITERIA_NOT_REFERENCES:
1006 return !matcherprop_list_match(prop, info->references, _("References: header"));
1007 case MATCHCRITERIA_TEST:
1008 return matcherprop_match_test(prop, info);
1009 case MATCHCRITERIA_NOT_TEST:
1010 return !matcherprop_match_test(prop, info);
1016 /* ********************* MatcherList *************************** */
1019 *\brief Create a new list of matchers
1021 *\param matchers List of matcher structures
1022 *\param bool_and Operator
1024 *\return MatcherList * New list
1026 MatcherList *matcherlist_new(GSList *matchers, gboolean bool_and)
1030 cond = g_new0(MatcherList, 1);
1032 cond->matchers = matchers;
1033 cond->bool_and = bool_and;
1039 *\brief Frees a list of matchers
1041 *\param cond List of matchers
1043 void matcherlist_free(MatcherList *cond)
1047 g_return_if_fail(cond);
1048 for (l = cond->matchers ; l != NULL ; l = g_slist_next(l)) {
1049 matcherprop_free((MatcherProp *) l->data);
1055 *\brief Skip all headers in a message file
1057 *\param fp Message file
1059 static void matcherlist_skip_headers(FILE *fp)
1061 gchar buf[BUFFSIZE];
1063 while (procheader_get_one_field(buf, sizeof(buf), fp, NULL) != -1)
1068 *\brief Check if a header matches a matcher condition
1070 *\param matcher Matcher structure to check header for
1071 *\param buf Header name
1073 *\return boolean TRUE if matching header
1075 static gboolean matcherprop_match_one_header(MatcherProp *matcher,
1078 gboolean result = FALSE;
1079 Header *header = NULL;
1081 switch (matcher->criteria) {
1082 case MATCHCRITERIA_HEADER:
1083 case MATCHCRITERIA_NOT_HEADER:
1084 header = procheader_parse_header(buf);
1087 if (procheader_headername_equal(header->name,
1089 if (matcher->criteria == MATCHCRITERIA_HEADER)
1090 result = matcherprop_string_match(matcher, header->body, _("header"));
1092 result = !matcherprop_string_match(matcher, header->body, _("header"));
1093 procheader_header_free(header);
1097 procheader_header_free(header);
1100 case MATCHCRITERIA_HEADERS_PART:
1101 return matcherprop_string_match(matcher, buf, _("header line"));
1102 case MATCHCRITERIA_NOT_HEADERS_PART:
1103 return !matcherprop_string_match(matcher, buf, _("headers line"));
1104 case MATCHCRITERIA_MESSAGE:
1105 return matcherprop_string_decode_match(matcher, buf, _("message line"));
1106 case MATCHCRITERIA_NOT_MESSAGE:
1107 return !matcherprop_string_decode_match(matcher, buf, _("message line"));
1108 case MATCHCRITERIA_FOUND_IN_ADDRESSBOOK:
1109 case MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK:
1111 GSList *address_list = NULL;
1112 gint match = MATCH_ONE;
1113 gboolean found = FALSE;
1115 /* how many address headers are me trying to mach? */
1116 if (strcasecmp(matcher->header, _("Any")) == 0)
1118 else if (strcasecmp(matcher->header, Q_("Filtering Matcher Menu|All")) == 0)
1121 if (match == MATCH_ONE) {
1122 /* matching one address header exactly, is that the right one? */
1123 header = procheader_parse_header(buf);
1125 !procheader_headername_equal(header->name, matcher->header))
1127 address_list = address_list_append(address_list, header->body);
1128 if (address_list == NULL)
1132 header = procheader_parse_header(buf);
1135 /* address header is one of the headers we have to match when checking
1136 for any address header or all address headers? */
1137 if (procheader_headername_equal(header->name, "From") ||
1138 procheader_headername_equal(header->name, "To") ||
1139 procheader_headername_equal(header->name, "Cc") ||
1140 procheader_headername_equal(header->name, "Reply-To") ||
1141 procheader_headername_equal(header->name, "Sender"))
1142 address_list = address_list_append(address_list, header->body);
1143 if (address_list == NULL)
1147 found = match_with_addresses_in_addressbook
1148 (matcher, address_list, matcher->criteria,
1149 matcher->expr, match);
1150 g_slist_free(address_list);
1152 if (matcher->criteria == MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK)
1163 *\brief Check if the matcher structure wants headers to
1166 *\param matcher Matcher structure
1168 *\return gboolean TRUE if the matcher structure describes
1169 * a header match condition
1171 static gboolean matcherprop_criteria_headers(const MatcherProp *matcher)
1173 switch (matcher->criteria) {
1174 case MATCHCRITERIA_HEADER:
1175 case MATCHCRITERIA_NOT_HEADER:
1176 case MATCHCRITERIA_HEADERS_PART:
1177 case MATCHCRITERIA_NOT_HEADERS_PART:
1178 case MATCHCRITERIA_FOUND_IN_ADDRESSBOOK:
1179 case MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK:
1187 *\brief Check if the matcher structure wants the message
1188 * to be matched (just perform an action on any
1191 *\param matcher Matcher structure
1193 *\return gboolean TRUE if matcher condition should match
1196 static gboolean matcherprop_criteria_message(MatcherProp *matcher)
1198 switch (matcher->criteria) {
1199 case MATCHCRITERIA_MESSAGE:
1200 case MATCHCRITERIA_NOT_MESSAGE:
1208 *\brief Check if a list of conditions matches one header in
1211 *\param matchers List of conditions
1212 *\param fp Message file
1214 *\return gboolean TRUE if one of the headers is matched by
1215 * the list of conditions.
1217 static gboolean matcherlist_match_headers(MatcherList *matchers, FILE *fp)
1220 gchar buf[BUFFSIZE];
1222 while (procheader_get_one_field(buf, sizeof(buf), fp, NULL) != -1) {
1223 for (l = matchers->matchers ; l != NULL ; l = g_slist_next(l)) {
1224 MatcherProp *matcher = (MatcherProp *) l->data;
1225 gint match = MATCH_ANY;
1230 /* determine the match range (all, any are our concern here) */
1231 if (matcher->criteria == MATCHCRITERIA_NOT_HEADERS_PART ||
1232 matcher->criteria == MATCHCRITERIA_NOT_MESSAGE) {
1235 } else if (matcher->criteria == MATCHCRITERIA_FOUND_IN_ADDRESSBOOK ||
1236 matcher->criteria == MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK) {
1237 Header *header = NULL;
1239 /* address header is one of the headers we have to match when checking
1240 for any address header or all address headers? */
1241 header = procheader_parse_header(buf);
1243 (procheader_headername_equal(header->name, "From") ||
1244 procheader_headername_equal(header->name, "To") ||
1245 procheader_headername_equal(header->name, "Cc") ||
1246 procheader_headername_equal(header->name, "Reply-To") ||
1247 procheader_headername_equal(header->name, "Sender"))) {
1249 if (strcasecmp(matcher->header, _("Any")) == 0)
1251 else if (strcasecmp(matcher->header, Q_("Filtering Matcher Menu|All")) == 0)
1256 /* further call to matcherprop_match_one_header() can't match
1257 and it irrelevant, so: don't alter the match result */
1262 /* ZERO line must NOT match for the rule to match.
1264 if (match == MATCH_ALL) {
1265 if (matcherprop_match_one_header(matcher, buf)) {
1266 matcher->result = TRUE;
1268 matcher->result = FALSE;
1269 matcher->done = TRUE;
1271 /* else, just one line matching is enough for the rule to match
1273 } else if (matcherprop_criteria_headers(matcher) ||
1274 matcherprop_criteria_message(matcher)) {
1275 if (matcherprop_match_one_header(matcher, buf)) {
1276 matcher->result = TRUE;
1277 matcher->done = TRUE;
1281 /* if the rule matched and the matchers are OR, no need to
1282 * check the others */
1283 if (matcher->result && matcher->done) {
1284 if (!matchers->bool_and)
1294 *\brief Check if a matcher wants to check the message body
1296 *\param matcher Matcher structure
1298 *\return gboolean TRUE if body must be matched.
1300 static gboolean matcherprop_criteria_body(const MatcherProp *matcher)
1302 switch (matcher->criteria) {
1303 case MATCHCRITERIA_BODY_PART:
1304 case MATCHCRITERIA_NOT_BODY_PART:
1312 *\brief Check if a (line) string matches the criteria
1313 * described by a matcher structure
1315 *\param matcher Matcher structure
1318 *\return gboolean TRUE if string matches criteria
1320 static gboolean matcherprop_match_line(MatcherProp *matcher, const gchar *line)
1322 switch (matcher->criteria) {
1323 case MATCHCRITERIA_BODY_PART:
1324 case MATCHCRITERIA_MESSAGE:
1325 return matcherprop_string_decode_match(matcher, line, _("body line"));
1326 case MATCHCRITERIA_NOT_BODY_PART:
1327 case MATCHCRITERIA_NOT_MESSAGE:
1328 return !matcherprop_string_decode_match(matcher, line, _("body line"));
1334 *\brief Check if a line in a message file's body matches
1337 *\param matchers List of conditions
1338 *\param fp Message file
1340 *\return gboolean TRUE if succesful match
1342 static gboolean matcherlist_match_body(MatcherList *matchers, FILE *fp)
1345 gchar buf[BUFFSIZE];
1347 while (fgets(buf, sizeof(buf), fp) != NULL) {
1348 for (l = matchers->matchers ; l != NULL ; l = g_slist_next(l)) {
1349 MatcherProp *matcher = (MatcherProp *) l->data;
1354 /* if the criteria is ~body_part or ~message, ZERO lines
1355 * must NOT match for the rule to match. */
1356 if (matcher->criteria == MATCHCRITERIA_NOT_BODY_PART ||
1357 matcher->criteria == MATCHCRITERIA_NOT_MESSAGE) {
1358 if (matcherprop_match_line(matcher, buf)) {
1359 matcher->result = TRUE;
1361 matcher->result = FALSE;
1362 matcher->done = TRUE;
1364 /* else, just one line has to match */
1365 } else if (matcherprop_criteria_body(matcher) ||
1366 matcherprop_criteria_message(matcher)) {
1367 if (matcherprop_match_line(matcher, buf)) {
1368 matcher->result = TRUE;
1369 matcher->done = TRUE;
1373 /* if the matchers are OR'ed and the rule matched,
1374 * no need to check the others. */
1375 if (matcher->result && matcher->done) {
1376 if (!matchers->bool_and)
1385 *\brief Check if a message file matches criteria
1387 *\param matchers Criteria
1388 *\param info Message info
1389 *\param result Default result
1391 *\return gboolean TRUE if matched
1393 static gboolean matcherlist_match_file(MatcherList *matchers, MsgInfo *info,
1396 gboolean read_headers;
1402 /* file need to be read ? */
1404 read_headers = FALSE;
1406 for (l = matchers->matchers ; l != NULL ; l = g_slist_next(l)) {
1407 MatcherProp *matcher = (MatcherProp *) l->data;
1409 if (matcherprop_criteria_headers(matcher))
1410 read_headers = TRUE;
1411 if (matcherprop_criteria_body(matcher))
1413 if (matcherprop_criteria_message(matcher)) {
1414 read_headers = TRUE;
1417 matcher->result = FALSE;
1418 matcher->done = FALSE;
1421 if (!read_headers && !read_body)
1424 file = procmsg_get_message_file_full(info, read_headers, read_body);
1428 if ((fp = g_fopen(file, "rb")) == NULL) {
1429 FILE_OP_ERROR(file, "fopen");
1434 /* read the headers */
1437 if (matcherlist_match_headers(matchers, fp))
1440 matcherlist_skip_headers(fp);
1445 matcherlist_match_body(matchers, fp);
1448 for (l = matchers->matchers; l != NULL; l = g_slist_next(l)) {
1449 MatcherProp *matcher = (MatcherProp *) l->data;
1451 if (matcherprop_criteria_headers(matcher) ||
1452 matcherprop_criteria_body(matcher) ||
1453 matcherprop_criteria_message(matcher)) {
1454 if (matcher->result) {
1455 if (!matchers->bool_and) {
1461 if (matchers->bool_and) {
1477 *\brief Test list of conditions on a message.
1479 *\param matchers List of conditions
1480 *\param info Message info
1482 *\return gboolean TRUE if matched
1484 gboolean matcherlist_match(MatcherList *matchers, MsgInfo *info)
1492 if (matchers->bool_and)
1497 /* test the cached elements */
1499 for (l = matchers->matchers; l != NULL ;l = g_slist_next(l)) {
1500 MatcherProp *matcher = (MatcherProp *) l->data;
1502 if (debug_filtering_session) {
1503 gchar *buf = matcherprop_to_string(matcher);
1504 log_print(LOG_DEBUG_FILTERING, _("checking if message matches [ %s ]\n"), buf);
1508 switch(matcher->criteria) {
1509 case MATCHCRITERIA_ALL:
1510 case MATCHCRITERIA_UNREAD:
1511 case MATCHCRITERIA_NOT_UNREAD:
1512 case MATCHCRITERIA_NEW:
1513 case MATCHCRITERIA_NOT_NEW:
1514 case MATCHCRITERIA_MARKED:
1515 case MATCHCRITERIA_NOT_MARKED:
1516 case MATCHCRITERIA_DELETED:
1517 case MATCHCRITERIA_NOT_DELETED:
1518 case MATCHCRITERIA_REPLIED:
1519 case MATCHCRITERIA_NOT_REPLIED:
1520 case MATCHCRITERIA_FORWARDED:
1521 case MATCHCRITERIA_NOT_FORWARDED:
1522 case MATCHCRITERIA_LOCKED:
1523 case MATCHCRITERIA_NOT_LOCKED:
1524 case MATCHCRITERIA_SPAM:
1525 case MATCHCRITERIA_NOT_SPAM:
1526 case MATCHCRITERIA_COLORLABEL:
1527 case MATCHCRITERIA_NOT_COLORLABEL:
1528 case MATCHCRITERIA_IGNORE_THREAD:
1529 case MATCHCRITERIA_NOT_IGNORE_THREAD:
1530 case MATCHCRITERIA_SUBJECT:
1531 case MATCHCRITERIA_NOT_SUBJECT:
1532 case MATCHCRITERIA_FROM:
1533 case MATCHCRITERIA_NOT_FROM:
1534 case MATCHCRITERIA_TO:
1535 case MATCHCRITERIA_NOT_TO:
1536 case MATCHCRITERIA_CC:
1537 case MATCHCRITERIA_NOT_CC:
1538 case MATCHCRITERIA_TO_OR_CC:
1539 case MATCHCRITERIA_NOT_TO_AND_NOT_CC:
1540 case MATCHCRITERIA_AGE_GREATER:
1541 case MATCHCRITERIA_AGE_LOWER:
1542 case MATCHCRITERIA_NEWSGROUPS:
1543 case MATCHCRITERIA_NOT_NEWSGROUPS:
1544 case MATCHCRITERIA_INREPLYTO:
1545 case MATCHCRITERIA_NOT_INREPLYTO:
1546 case MATCHCRITERIA_REFERENCES:
1547 case MATCHCRITERIA_NOT_REFERENCES:
1548 case MATCHCRITERIA_SCORE_GREATER:
1549 case MATCHCRITERIA_SCORE_LOWER:
1550 case MATCHCRITERIA_SCORE_EQUAL:
1551 case MATCHCRITERIA_SIZE_GREATER:
1552 case MATCHCRITERIA_SIZE_SMALLER:
1553 case MATCHCRITERIA_SIZE_EQUAL:
1554 case MATCHCRITERIA_TEST:
1555 case MATCHCRITERIA_NOT_TEST:
1556 case MATCHCRITERIA_PARTIAL:
1557 case MATCHCRITERIA_NOT_PARTIAL:
1558 if (matcherprop_match(matcher, info)) {
1559 if (!matchers->bool_and) {
1560 if (debug_filtering_session)
1561 log_status_ok(LOG_DEBUG_FILTERING, _("message matches\n"));
1566 if (matchers->bool_and) {
1567 if (debug_filtering_session)
1568 log_status_nok(LOG_DEBUG_FILTERING, _("message does not match\n"));
1575 /* test the condition on the file */
1577 if (matcherlist_match_file(matchers, info, result)) {
1578 if (!matchers->bool_and) {
1579 if (debug_filtering_session)
1580 log_status_ok(LOG_DEBUG_FILTERING, _("message matches\n"));
1584 if (matchers->bool_and) {
1585 if (debug_filtering_session)
1586 log_status_nok(LOG_DEBUG_FILTERING, _("message does not match\n"));
1591 if (debug_filtering_session) {
1593 log_status_ok(LOG_DEBUG_FILTERING, _("message matches\n"));
1595 log_status_nok(LOG_DEBUG_FILTERING, _("message does not match\n"));
1601 static gint quote_filter_str(gchar * result, guint size,
1611 for(p = path ; * p != '\0' ; p ++) {
1613 if ((* p != '\"') && (* p != '\\')) {
1614 if (remaining > 0) {
1620 result[size - 1] = '\0';
1625 if (remaining >= 2) {
1633 result[size - 1] = '\0';
1638 if (remaining > 0) {
1642 result[size - 1] = '\0';
1650 gchar * matcher_quote_str(const gchar * src)
1655 len = strlen(src) * 2 + 1;
1656 res = g_malloc(len);
1657 quote_filter_str(res, len, src);
1663 *\brief Convert a matcher structure to a string
1665 *\param matcher Matcher structure
1667 *\return gchar * Newly allocated string
1669 gchar *matcherprop_to_string(MatcherProp *matcher)
1671 gchar *matcher_str = NULL;
1672 const gchar *criteria_str;
1673 const gchar *matchtype_str;
1675 gchar * quoted_expr;
1676 gchar * quoted_header;
1678 criteria_str = NULL;
1679 for (i = 0; i < (int) (sizeof(matchparser_tab) / sizeof(MatchParser)); i++) {
1680 if (matchparser_tab[i].id == matcher->criteria)
1681 criteria_str = matchparser_tab[i].str;
1683 if (criteria_str == NULL)
1686 switch (matcher->criteria) {
1687 case MATCHCRITERIA_AGE_GREATER:
1688 case MATCHCRITERIA_AGE_LOWER:
1689 case MATCHCRITERIA_SCORE_GREATER:
1690 case MATCHCRITERIA_SCORE_LOWER:
1691 case MATCHCRITERIA_SCORE_EQUAL:
1692 case MATCHCRITERIA_SIZE_GREATER:
1693 case MATCHCRITERIA_SIZE_SMALLER:
1694 case MATCHCRITERIA_SIZE_EQUAL:
1695 case MATCHCRITERIA_COLORLABEL:
1696 case MATCHCRITERIA_NOT_COLORLABEL:
1697 return g_strdup_printf("%s %i", criteria_str, matcher->value);
1698 case MATCHCRITERIA_ALL:
1699 case MATCHCRITERIA_UNREAD:
1700 case MATCHCRITERIA_NOT_UNREAD:
1701 case MATCHCRITERIA_NEW:
1702 case MATCHCRITERIA_NOT_NEW:
1703 case MATCHCRITERIA_MARKED:
1704 case MATCHCRITERIA_NOT_MARKED:
1705 case MATCHCRITERIA_DELETED:
1706 case MATCHCRITERIA_NOT_DELETED:
1707 case MATCHCRITERIA_REPLIED:
1708 case MATCHCRITERIA_NOT_REPLIED:
1709 case MATCHCRITERIA_FORWARDED:
1710 case MATCHCRITERIA_NOT_FORWARDED:
1711 case MATCHCRITERIA_LOCKED:
1712 case MATCHCRITERIA_NOT_LOCKED:
1713 case MATCHCRITERIA_SPAM:
1714 case MATCHCRITERIA_NOT_SPAM:
1715 case MATCHCRITERIA_PARTIAL:
1716 case MATCHCRITERIA_NOT_PARTIAL:
1717 case MATCHCRITERIA_IGNORE_THREAD:
1718 case MATCHCRITERIA_NOT_IGNORE_THREAD:
1719 return g_strdup(criteria_str);
1720 case MATCHCRITERIA_TEST:
1721 case MATCHCRITERIA_NOT_TEST:
1722 quoted_expr = matcher_quote_str(matcher->expr);
1723 matcher_str = g_strdup_printf("%s \"%s\"",
1724 criteria_str, quoted_expr);
1725 g_free(quoted_expr);
1727 case MATCHCRITERIA_FOUND_IN_ADDRESSBOOK:
1728 case MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK:
1729 quoted_header = matcher_quote_str(matcher->header);
1730 quoted_expr = matcher_quote_str(matcher->expr);
1731 matcher_str = g_strdup_printf("%s \"%s\" in \"%s\"",
1732 criteria_str, quoted_header, quoted_expr);
1733 g_free(quoted_header);
1734 g_free(quoted_expr);
1738 matchtype_str = NULL;
1739 for (i = 0; i < sizeof matchparser_tab / sizeof matchparser_tab[0]; i++) {
1740 if (matchparser_tab[i].id == matcher->matchtype)
1741 matchtype_str = matchparser_tab[i].str;
1744 if (matchtype_str == NULL)
1747 switch (matcher->matchtype) {
1748 case MATCHTYPE_MATCH:
1749 case MATCHTYPE_MATCHCASE:
1750 case MATCHTYPE_REGEXP:
1751 case MATCHTYPE_REGEXPCASE:
1752 quoted_expr = matcher_quote_str(matcher->expr);
1753 if (matcher->header) {
1754 quoted_header = matcher_quote_str(matcher->header);
1755 matcher_str = g_strdup_printf
1756 ("%s \"%s\" %s \"%s\"",
1757 criteria_str, quoted_header,
1758 matchtype_str, quoted_expr);
1759 g_free(quoted_header);
1762 matcher_str = g_strdup_printf
1763 ("%s %s \"%s\"", criteria_str,
1764 matchtype_str, quoted_expr);
1765 g_free(quoted_expr);
1773 *\brief Convert a list of conditions to a string
1775 *\param matchers List of conditions
1777 *\return gchar * Newly allocated string
1779 gchar *matcherlist_to_string(const MatcherList *matchers)
1785 gchar *result = NULL;
1787 count = g_slist_length(matchers->matchers);
1788 vstr = g_new(gchar *, count + 1);
1790 for (l = matchers->matchers, cur_str = vstr; l != NULL;
1791 l = g_slist_next(l), cur_str ++) {
1792 *cur_str = matcherprop_to_string((MatcherProp *) l->data);
1793 if (*cur_str == NULL)
1798 if (matchers->bool_and)
1799 result = g_strjoinv(" & ", vstr);
1801 result = g_strjoinv(" | ", vstr);
1803 for (cur_str = vstr ; *cur_str != NULL ; cur_str ++)
1811 #define STRLEN_ZERO(s) ((s) ? strlen(s) : 0)
1812 #define STRLEN_DEFAULT(s,d) ((s) ? strlen(s) : STRLEN_ZERO(d))
1814 static void add_str_default(gchar ** dest,
1815 const gchar * s, const gchar * d)
1817 gchar quoted_str[4096];
1825 quote_cmd_argument(quoted_str, sizeof(quoted_str), str);
1826 strcpy(* dest, quoted_str);
1828 (* dest) += strlen(* dest);
1831 /* matching_build_command() - preferably cmd should be unescaped */
1833 *\brief Build the command line to execute
1835 *\param cmd String with command line specifiers
1836 *\param info Message info to use for command
1838 *\return gchar * Newly allocated string
1840 gchar *matching_build_command(const gchar *cmd, MsgInfo *info)
1842 const gchar *s = cmd;
1843 gchar *filename = NULL;
1844 gchar *processed_cmd;
1848 const gchar *const no_subject = _("(none)") ;
1849 const gchar *const no_from = _("(none)") ;
1850 const gchar *const no_to = _("(none)") ;
1851 const gchar *const no_cc = _("(none)") ;
1852 const gchar *const no_date = _("(none)") ;
1853 const gchar *const no_msgid = _("(none)") ;
1854 const gchar *const no_newsgroups = _("(none)") ;
1855 const gchar *const no_references = _("(none)") ;
1857 size = STRLEN_ZERO(cmd) + 1;
1858 while (*s != '\0') {
1865 case 's': /* subject */
1866 size += STRLEN_DEFAULT(info->subject, no_subject) - 2;
1868 case 'f': /* from */
1869 size += STRLEN_DEFAULT(info->from, no_from) - 2;
1872 size += STRLEN_DEFAULT(info->to, no_to) - 2;
1875 size += STRLEN_DEFAULT(info->cc, no_cc) - 2;
1877 case 'd': /* date */
1878 size += STRLEN_DEFAULT(info->date, no_date) - 2;
1880 case 'i': /* message-id */
1881 size += STRLEN_DEFAULT(info->msgid, no_msgid) - 2;
1883 case 'n': /* newsgroups */
1884 size += STRLEN_DEFAULT(info->newsgroups, no_newsgroups) - 2;
1886 case 'r': /* references */
1887 /* FIXME: using the inreplyto header for reference */
1888 size += STRLEN_DEFAULT(info->inreplyto, no_references) - 2;
1890 case 'F': /* file */
1891 if (filename == NULL)
1892 filename = folder_item_fetch_msg(info->folder, info->msgnum);
1894 if (filename == NULL) {
1895 g_warning("filename is not set");
1899 size += strlen(filename) - 2;
1908 /* as the string can be quoted, we double the result */
1911 processed_cmd = g_new0(gchar, size);
1915 while (*s != '\0') {
1923 case 's': /* subject */
1924 add_str_default(&p, info->subject,
1927 case 'f': /* from */
1928 add_str_default(&p, info->from,
1932 add_str_default(&p, info->to,
1936 add_str_default(&p, info->cc,
1939 case 'd': /* date */
1940 add_str_default(&p, info->date,
1943 case 'i': /* message-id */
1944 add_str_default(&p, info->msgid,
1947 case 'n': /* newsgroups */
1948 add_str_default(&p, info->newsgroups,
1951 case 'r': /* references */
1952 /* FIXME: using the inreplyto header for references */
1953 add_str_default(&p, info->inreplyto, no_references);
1955 case 'F': /* file */
1956 if (filename != NULL)
1957 add_str_default(&p, filename, NULL);
1976 return processed_cmd;
1978 #undef STRLEN_DEFAULT
1981 /* ************************************************************ */
1985 *\brief Write filtering list to file
1988 *\param prefs_filtering List of filtering conditions
1990 static void prefs_filtering_write(FILE *fp, GSList *prefs_filtering)
1994 for (cur = prefs_filtering; cur != NULL; cur = cur->next) {
1995 gchar *filtering_str = NULL;
1996 gchar *tmp_name = NULL;
1997 FilteringProp *prop = NULL;
1999 if (NULL == (prop = (FilteringProp *) cur->data))
2002 if (NULL == (filtering_str = filteringprop_to_string(prop)))
2005 if (prop->enabled) {
2006 if (fputs("enabled ", fp) == EOF) {
2007 FILE_OP_ERROR("filtering config", "fputs");
2011 if (fputs("disabled ", fp) == EOF) {
2012 FILE_OP_ERROR("filtering config", "fputs");
2017 if (fputs("rulename \"", fp) == EOF) {
2018 FILE_OP_ERROR("filtering config", "fputs");
2019 g_free(filtering_str);
2022 tmp_name = prop->name;
2023 while (tmp_name && *tmp_name != '\0') {
2024 if (*tmp_name != '"') {
2025 if (fputc(*tmp_name, fp) == EOF) {
2026 FILE_OP_ERROR("filtering config", "fputs || fputc");
2027 g_free(filtering_str);
2030 } else if (*tmp_name == '"') {
2031 if (fputc('\\', fp) == EOF ||
2032 fputc('"', fp) == EOF) {
2033 FILE_OP_ERROR("filtering config", "fputs || fputc");
2034 g_free(filtering_str);
2040 if (fputs("\" ", fp) == EOF) {
2041 FILE_OP_ERROR("filtering config", "fputs");
2042 g_free(filtering_str);
2046 if (prop->account_id != 0) {
2049 tmp = g_strdup_printf("account %d ", prop->account_id);
2050 if (fputs(tmp, fp) == EOF) {
2051 FILE_OP_ERROR("filtering config", "fputs");
2058 if(fputs(filtering_str, fp) == EOF ||
2059 fputc('\n', fp) == EOF) {
2060 FILE_OP_ERROR("filtering config", "fputs || fputc");
2061 g_free(filtering_str);
2064 g_free(filtering_str);
2069 *\brief Write matchers from a folder item
2071 *\param node Node with folder info
2072 *\param data File pointer
2074 *\return gboolean FALSE
2076 static gboolean prefs_matcher_write_func(GNode *node, gpointer data)
2081 GSList *prefs_filtering;
2084 /* prevent warning */
2085 if (item->path == NULL)
2087 id = folder_item_get_identifier(item);
2090 prefs_filtering = item->prefs->processing;
2092 if (prefs_filtering != NULL) {
2093 fprintf(fp, "[%s]\n", id);
2094 prefs_filtering_write(fp, prefs_filtering);
2104 *\brief Save matchers from folder items
2108 static void prefs_matcher_save(FILE *fp)
2112 for (cur = folder_get_list() ; cur != NULL ; cur = g_list_next(cur)) {
2115 folder = (Folder *) cur->data;
2116 g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
2117 prefs_matcher_write_func, fp);
2120 /* pre global rules */
2121 fprintf(fp, "[preglobal]\n");
2122 prefs_filtering_write(fp, pre_global_processing);
2125 /* post global rules */
2126 fprintf(fp, "[postglobal]\n");
2127 prefs_filtering_write(fp, post_global_processing);
2130 /* filtering rules */
2131 fprintf(fp, "[filtering]\n");
2132 prefs_filtering_write(fp, filtering_rules);
2137 *\brief Write filtering / matcher configuration file
2139 void prefs_matcher_write_config(void)
2144 debug_print("Writing matcher configuration...\n");
2146 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
2149 if ((pfile = prefs_write_open(rcpath)) == NULL) {
2150 g_warning("failed to write configuration to file\n");
2156 prefs_matcher_save(pfile->fp);
2160 if (prefs_file_close(pfile) < 0) {
2161 g_warning("failed to write configuration to file\n");
2166 /* ******************************************************************* */
2168 static void matcher_add_rulenames(const gchar *rcpath)
2170 gchar *newpath = g_strconcat(rcpath, ".new", NULL);
2171 FILE *src = g_fopen(rcpath, "rb");
2172 FILE *dst = g_fopen(newpath, "wb");
2173 gchar buf[BUFFSIZE];
2181 while (fgets (buf, sizeof(buf), src) != NULL) {
2182 if (strlen(buf) > 2 && buf[0] != '['
2183 && strncmp(buf, "rulename \"", 10)
2184 && strncmp(buf, "enabled rulename \"", 18)
2185 && strncmp(buf, "disabled rulename \"", 18)) {
2186 fwrite("enabled rulename \"\" ",
2187 strlen("enabled rulename \"\" "), 1, dst);
2189 fwrite(buf, strlen(buf), 1, dst);
2193 move_file(newpath, rcpath, TRUE);
2198 *\brief Read matcher configuration
2200 void prefs_matcher_read_config(void)
2203 gchar *rc_old_format;
2206 create_matchparser_hashtab();
2207 prefs_filtering_clear();
2209 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MATCHER_RC, NULL);
2210 rc_old_format = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MATCHER_RC,
2211 ".pre_names", NULL);
2213 if (!is_file_exist(rc_old_format) && is_file_exist(rcpath)) {
2214 /* backup file with no rules names, in case
2215 * anything goes wrong */
2216 copy_file(rcpath, rc_old_format, FALSE);
2217 /* now hack the file in order to have it to the new format */
2218 matcher_add_rulenames(rcpath);
2221 g_free(rc_old_format);
2223 f = g_fopen(rcpath, "rb");
2227 matcher_parser_start_parsing(f);
2228 fclose(matcher_parserin);
2231 /* previous version compatibility */
2233 /* printf("reading filtering\n"); */
2234 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
2235 FILTERING_RC, NULL);
2236 f = g_fopen(rcpath, "rb");
2240 matcher_parser_start_parsing(f);
2241 fclose(matcher_parserin);
2244 /* printf("reading scoring\n"); */
2245 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
2247 f = g_fopen(rcpath, "rb");
2251 matcher_parser_start_parsing(f);
2252 fclose(matcher_parserin);