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,
772 prefs_common_translated_header_name("Subject:"));
773 case MATCHCRITERIA_NOT_SUBJECT:
774 return !matcherprop_string_match(prop, info->subject,
775 prefs_common_translated_header_name("Subject:"));
776 case MATCHCRITERIA_FROM:
777 case MATCHCRITERIA_NOT_FROM:
782 context = g_strdup_printf(_("%s header"), prefs_common_translated_header_name("From:"));
783 ret = matcherprop_string_match(prop, info->from, context);
785 return (prop->criteria == MATCHCRITERIA_FROM)? ret : !ret;
787 case MATCHCRITERIA_TO:
788 case MATCHCRITERIA_NOT_TO:
793 context = g_strdup_printf(_("%s header"), prefs_common_translated_header_name("To:"));
794 ret = matcherprop_string_match(prop, info->to, context);
796 return (prop->criteria == MATCHCRITERIA_TO)? ret : !ret;
798 case MATCHCRITERIA_CC:
799 case MATCHCRITERIA_NOT_CC:
804 context = g_strdup_printf(_("%s header"), prefs_common_translated_header_name("Cc:"));
805 ret = matcherprop_string_match(prop, info->cc, context);
807 return (prop->criteria == MATCHCRITERIA_CC)? ret : !ret;
809 case MATCHCRITERIA_TO_OR_CC:
811 gchar *context1, *context2;
814 context1 = g_strdup_printf(_("%s header"), prefs_common_translated_header_name("To:"));
815 context2 = g_strdup_printf(_("%s header"), prefs_common_translated_header_name("Cc:"));
816 ret = matcherprop_string_match(prop, info->to, context1)
817 || matcherprop_string_match(prop, info->cc, context2);
822 case MATCHCRITERIA_NOT_TO_AND_NOT_CC:
824 gchar *context1, *context2;
827 context1 = g_strdup_printf(_("%s header"), prefs_common_translated_header_name("To:"));
828 context2 = g_strdup_printf(_("%s header"), prefs_common_translated_header_name("Cc:"));
829 ret = !(matcherprop_string_match(prop, info->to, context1)
830 || matcherprop_string_match(prop, info->cc, context2));
835 case MATCHCRITERIA_AGE_GREATER:
841 age = ((t - info->date_t) / (60 * 60 * 24));
842 ret = (age > 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 age [ %d ] is greater than [ %d ]\n",
852 log_print(LOG_DEBUG_FILTERING,
853 "message age [ %d ] is not greater than [ %d ]\n",
859 case MATCHCRITERIA_AGE_LOWER:
865 age = ((t - info->date_t) / (60 * 60 * 24));
866 ret = (age < prop->value);
869 if (debug_filtering_session
870 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
872 log_print(LOG_DEBUG_FILTERING,
873 "message age [ %d ] is lower than [ %d ]\n",
876 log_print(LOG_DEBUG_FILTERING,
877 "message age [ %d ] is not lower than [ %d ]\n",
883 case MATCHCRITERIA_SCORE_GREATER:
885 gboolean ret = (info->score > prop->value);
888 if (debug_filtering_session
889 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
891 log_print(LOG_DEBUG_FILTERING,
892 "message score [ %d ] is greater than [ %d ]\n",
893 info->score, prop->value);
895 log_print(LOG_DEBUG_FILTERING,
896 "message score [ %d ] is not greater than [ %d ]\n",
897 info->score, prop->value);
902 case MATCHCRITERIA_SCORE_LOWER:
904 gboolean ret = (info->score < prop->value);
907 if (debug_filtering_session
908 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
910 log_print(LOG_DEBUG_FILTERING,
911 "message score [ %d ] is lower than [ %d ]\n",
912 info->score, prop->value);
914 log_print(LOG_DEBUG_FILTERING,
915 "message score [ %d ] is not lower than [ %d ]\n",
916 info->score, prop->value);
921 case MATCHCRITERIA_SCORE_EQUAL:
923 gboolean ret = (info->score == prop->value);
926 if (debug_filtering_session
927 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
929 log_print(LOG_DEBUG_FILTERING,
930 "message score [ %d ] is equal to [ %d ]\n",
931 info->score, prop->value);
933 log_print(LOG_DEBUG_FILTERING,
934 "message score [ %d ] is not equal to [ %d ]\n",
935 info->score, prop->value);
940 case MATCHCRITERIA_SIZE_GREATER:
942 /* FIXME: info->size is an off_t */
943 gboolean ret = (info->size > (off_t) prop->value);
946 if (debug_filtering_session
947 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
949 log_print(LOG_DEBUG_FILTERING,
950 "message size [ %ld ] is greater than [ %d ]\n",
951 info->size, prop->value);
953 log_print(LOG_DEBUG_FILTERING,
954 "message size [ %ld ] is not greater than [ %d ]\n",
955 info->size, prop->value);
960 case MATCHCRITERIA_SIZE_SMALLER:
962 /* FIXME: info->size is an off_t */
963 gboolean ret = (info->size < (off_t) prop->value);
966 if (debug_filtering_session
967 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
969 log_print(LOG_DEBUG_FILTERING,
970 "message size [ %ld ] is smaller than [ %d ]\n",
971 info->size, prop->value);
973 log_print(LOG_DEBUG_FILTERING,
974 "message size [ %ld ] is not smaller than [ %d ]\n",
975 info->size, prop->value);
980 case MATCHCRITERIA_SIZE_EQUAL:
982 /* FIXME: info->size is an off_t */
983 gboolean ret = (info->size == (off_t) prop->value);
986 if (debug_filtering_session
987 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
989 log_print(LOG_DEBUG_FILTERING,
990 "message size [ %ld ] is equal to [ %d ]\n",
991 info->size, prop->value);
993 log_print(LOG_DEBUG_FILTERING,
994 "message size [ %ld ] is not equal to [ %d ]\n",
995 info->size, prop->value);
1000 case MATCHCRITERIA_PARTIAL:
1002 /* FIXME: info->size is an off_t */
1003 gboolean ret = (info->total_size != 0 && info->size != (off_t)info->total_size);
1006 if (debug_filtering_session
1007 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
1009 log_print(LOG_DEBUG_FILTERING,
1010 "message is partially downloaded, size [ %ld ] is less than total size [ %d ])\n",
1011 info->size, info->total_size);
1013 log_print(LOG_DEBUG_FILTERING,
1014 "message is not partially downloaded\n");
1019 case MATCHCRITERIA_NOT_PARTIAL:
1021 /* FIXME: info->size is an off_t */
1022 gboolean ret = (info->total_size == 0 || info->size == (off_t)info->total_size);
1025 if (debug_filtering_session
1026 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
1028 log_print(LOG_DEBUG_FILTERING,
1029 "message is not partially downloaded\n");
1031 log_print(LOG_DEBUG_FILTERING,
1032 "message is partially downloaded, size [ %ld ] is less than total size [ %d ])\n",
1033 info->size, info->total_size);
1038 case MATCHCRITERIA_NEWSGROUPS:
1039 case MATCHCRITERIA_NOT_NEWSGROUPS:
1044 context = g_strdup_printf(_("%s header"),
1045 prefs_common_translated_header_name("Newsgroups:"));
1046 ret = matcherprop_string_match(prop, info->newsgroups, context);
1048 return (prop->criteria == MATCHCRITERIA_NEWSGROUPS)? ret : !ret;
1050 case MATCHCRITERIA_INREPLYTO:
1051 case MATCHCRITERIA_NOT_INREPLYTO:
1056 context = g_strdup_printf(_("%s header"),
1057 prefs_common_translated_header_name("In-Reply-To:"));
1058 ret = matcherprop_string_match(prop, info->inreplyto, context);
1060 return (prop->criteria == MATCHCRITERIA_INREPLYTO)? ret : !ret;
1062 case MATCHCRITERIA_REFERENCES:
1063 case MATCHCRITERIA_NOT_REFERENCES:
1068 context = g_strdup_printf(_("%s header"),
1069 prefs_common_translated_header_name("References:"));
1070 ret = matcherprop_list_match(prop, info->references, context);
1072 return (prop->criteria == MATCHCRITERIA_REFERENCES)? ret : !ret;
1074 case MATCHCRITERIA_TEST:
1075 return matcherprop_match_test(prop, info);
1076 case MATCHCRITERIA_NOT_TEST:
1077 return !matcherprop_match_test(prop, info);
1083 /* ********************* MatcherList *************************** */
1086 *\brief Create a new list of matchers
1088 *\param matchers List of matcher structures
1089 *\param bool_and Operator
1091 *\return MatcherList * New list
1093 MatcherList *matcherlist_new(GSList *matchers, gboolean bool_and)
1097 cond = g_new0(MatcherList, 1);
1099 cond->matchers = matchers;
1100 cond->bool_and = bool_and;
1106 *\brief Frees a list of matchers
1108 *\param cond List of matchers
1110 void matcherlist_free(MatcherList *cond)
1114 g_return_if_fail(cond);
1115 for (l = cond->matchers ; l != NULL ; l = g_slist_next(l)) {
1116 matcherprop_free((MatcherProp *) l->data);
1122 *\brief Skip all headers in a message file
1124 *\param fp Message file
1126 static void matcherlist_skip_headers(FILE *fp)
1128 gchar buf[BUFFSIZE];
1130 while (procheader_get_one_field(buf, sizeof(buf), fp, NULL) != -1)
1135 *\brief Check if a header matches a matcher condition
1137 *\param matcher Matcher structure to check header for
1138 *\param buf Header name
1140 *\return boolean TRUE if matching header
1142 static gboolean matcherprop_match_one_header(MatcherProp *matcher,
1145 gboolean result = FALSE;
1146 Header *header = NULL;
1148 switch (matcher->criteria) {
1149 case MATCHCRITERIA_HEADER:
1150 case MATCHCRITERIA_NOT_HEADER:
1151 header = procheader_parse_header(buf);
1154 if (procheader_headername_equal(header->name,
1156 if (matcher->criteria == MATCHCRITERIA_HEADER)
1157 result = matcherprop_string_match(matcher, header->body, _("header"));
1159 result = !matcherprop_string_match(matcher, header->body, _("header"));
1160 procheader_header_free(header);
1164 procheader_header_free(header);
1167 case MATCHCRITERIA_HEADERS_PART:
1168 return matcherprop_string_match(matcher, buf, _("header line"));
1169 case MATCHCRITERIA_NOT_HEADERS_PART:
1170 return !matcherprop_string_match(matcher, buf, _("headers line"));
1171 case MATCHCRITERIA_MESSAGE:
1172 return matcherprop_string_decode_match(matcher, buf, _("message line"));
1173 case MATCHCRITERIA_NOT_MESSAGE:
1174 return !matcherprop_string_decode_match(matcher, buf, _("message line"));
1175 case MATCHCRITERIA_FOUND_IN_ADDRESSBOOK:
1176 case MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK:
1178 GSList *address_list = NULL;
1179 gint match = MATCH_ONE;
1180 gboolean found = FALSE;
1182 /* how many address headers are me trying to mach? */
1183 if (strcasecmp(matcher->header, _("Any")) == 0)
1185 else if (strcasecmp(matcher->header, Q_("Filtering Matcher Menu|All")) == 0)
1188 if (match == MATCH_ONE) {
1189 /* matching one address header exactly, is that the right one? */
1190 header = procheader_parse_header(buf);
1192 !procheader_headername_equal(header->name, matcher->header))
1194 address_list = address_list_append(address_list, header->body);
1195 if (address_list == NULL)
1199 header = procheader_parse_header(buf);
1202 /* address header is one of the headers we have to match when checking
1203 for any address header or all address headers? */
1204 if (procheader_headername_equal(header->name, "From") ||
1205 procheader_headername_equal(header->name, "To") ||
1206 procheader_headername_equal(header->name, "Cc") ||
1207 procheader_headername_equal(header->name, "Reply-To") ||
1208 procheader_headername_equal(header->name, "Sender"))
1209 address_list = address_list_append(address_list, header->body);
1210 if (address_list == NULL)
1214 found = match_with_addresses_in_addressbook
1215 (matcher, address_list, matcher->criteria,
1216 matcher->expr, match);
1217 g_slist_free(address_list);
1219 if (matcher->criteria == MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK)
1230 *\brief Check if the matcher structure wants headers to
1233 *\param matcher Matcher structure
1235 *\return gboolean TRUE if the matcher structure describes
1236 * a header match condition
1238 static gboolean matcherprop_criteria_headers(const MatcherProp *matcher)
1240 switch (matcher->criteria) {
1241 case MATCHCRITERIA_HEADER:
1242 case MATCHCRITERIA_NOT_HEADER:
1243 case MATCHCRITERIA_HEADERS_PART:
1244 case MATCHCRITERIA_NOT_HEADERS_PART:
1245 case MATCHCRITERIA_FOUND_IN_ADDRESSBOOK:
1246 case MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK:
1254 *\brief Check if the matcher structure wants the message
1255 * to be matched (just perform an action on any
1258 *\param matcher Matcher structure
1260 *\return gboolean TRUE if matcher condition should match
1263 static gboolean matcherprop_criteria_message(MatcherProp *matcher)
1265 switch (matcher->criteria) {
1266 case MATCHCRITERIA_MESSAGE:
1267 case MATCHCRITERIA_NOT_MESSAGE:
1275 *\brief Check if a list of conditions matches one header in
1278 *\param matchers List of conditions
1279 *\param fp Message file
1281 *\return gboolean TRUE if one of the headers is matched by
1282 * the list of conditions.
1284 static gboolean matcherlist_match_headers(MatcherList *matchers, FILE *fp)
1287 gchar buf[BUFFSIZE];
1289 while (procheader_get_one_field(buf, sizeof(buf), fp, NULL) != -1) {
1290 for (l = matchers->matchers ; l != NULL ; l = g_slist_next(l)) {
1291 MatcherProp *matcher = (MatcherProp *) l->data;
1292 gint match = MATCH_ANY;
1297 /* determine the match range (all, any are our concern here) */
1298 if (matcher->criteria == MATCHCRITERIA_NOT_HEADERS_PART ||
1299 matcher->criteria == MATCHCRITERIA_NOT_MESSAGE) {
1302 } else if (matcher->criteria == MATCHCRITERIA_FOUND_IN_ADDRESSBOOK ||
1303 matcher->criteria == MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK) {
1304 Header *header = NULL;
1306 /* address header is one of the headers we have to match when checking
1307 for any address header or all address headers? */
1308 header = procheader_parse_header(buf);
1310 (procheader_headername_equal(header->name, "From") ||
1311 procheader_headername_equal(header->name, "To") ||
1312 procheader_headername_equal(header->name, "Cc") ||
1313 procheader_headername_equal(header->name, "Reply-To") ||
1314 procheader_headername_equal(header->name, "Sender"))) {
1316 if (strcasecmp(matcher->header, _("Any")) == 0)
1318 else if (strcasecmp(matcher->header, Q_("Filtering Matcher Menu|All")) == 0)
1323 /* further call to matcherprop_match_one_header() can't match
1324 and it irrelevant, so: don't alter the match result */
1329 /* ZERO line must NOT match for the rule to match.
1331 if (match == MATCH_ALL) {
1332 if (matcherprop_match_one_header(matcher, buf)) {
1333 matcher->result = TRUE;
1335 matcher->result = FALSE;
1336 matcher->done = TRUE;
1338 /* else, just one line matching is enough for the rule to match
1340 } else if (matcherprop_criteria_headers(matcher) ||
1341 matcherprop_criteria_message(matcher)) {
1342 if (matcherprop_match_one_header(matcher, buf)) {
1343 matcher->result = TRUE;
1344 matcher->done = TRUE;
1348 /* if the rule matched and the matchers are OR, no need to
1349 * check the others */
1350 if (matcher->result && matcher->done) {
1351 if (!matchers->bool_and)
1361 *\brief Check if a matcher wants to check the message body
1363 *\param matcher Matcher structure
1365 *\return gboolean TRUE if body must be matched.
1367 static gboolean matcherprop_criteria_body(const MatcherProp *matcher)
1369 switch (matcher->criteria) {
1370 case MATCHCRITERIA_BODY_PART:
1371 case MATCHCRITERIA_NOT_BODY_PART:
1379 *\brief Check if a (line) string matches the criteria
1380 * described by a matcher structure
1382 *\param matcher Matcher structure
1385 *\return gboolean TRUE if string matches criteria
1387 static gboolean matcherprop_match_line(MatcherProp *matcher, const gchar *line)
1389 switch (matcher->criteria) {
1390 case MATCHCRITERIA_BODY_PART:
1391 case MATCHCRITERIA_MESSAGE:
1392 return matcherprop_string_decode_match(matcher, line, _("body line"));
1393 case MATCHCRITERIA_NOT_BODY_PART:
1394 case MATCHCRITERIA_NOT_MESSAGE:
1395 return !matcherprop_string_decode_match(matcher, line, _("body line"));
1401 *\brief Check if a line in a message file's body matches
1404 *\param matchers List of conditions
1405 *\param fp Message file
1407 *\return gboolean TRUE if succesful match
1409 static gboolean matcherlist_match_body(MatcherList *matchers, FILE *fp)
1412 gchar buf[BUFFSIZE];
1414 while (fgets(buf, sizeof(buf), fp) != NULL) {
1415 for (l = matchers->matchers ; l != NULL ; l = g_slist_next(l)) {
1416 MatcherProp *matcher = (MatcherProp *) l->data;
1421 /* if the criteria is ~body_part or ~message, ZERO lines
1422 * must NOT match for the rule to match. */
1423 if (matcher->criteria == MATCHCRITERIA_NOT_BODY_PART ||
1424 matcher->criteria == MATCHCRITERIA_NOT_MESSAGE) {
1425 if (matcherprop_match_line(matcher, buf)) {
1426 matcher->result = TRUE;
1428 matcher->result = FALSE;
1429 matcher->done = TRUE;
1431 /* else, just one line has to match */
1432 } else if (matcherprop_criteria_body(matcher) ||
1433 matcherprop_criteria_message(matcher)) {
1434 if (matcherprop_match_line(matcher, buf)) {
1435 matcher->result = TRUE;
1436 matcher->done = TRUE;
1440 /* if the matchers are OR'ed and the rule matched,
1441 * no need to check the others. */
1442 if (matcher->result && matcher->done) {
1443 if (!matchers->bool_and)
1452 *\brief Check if a message file matches criteria
1454 *\param matchers Criteria
1455 *\param info Message info
1456 *\param result Default result
1458 *\return gboolean TRUE if matched
1460 static gboolean matcherlist_match_file(MatcherList *matchers, MsgInfo *info,
1463 gboolean read_headers;
1469 /* file need to be read ? */
1471 read_headers = FALSE;
1473 for (l = matchers->matchers ; l != NULL ; l = g_slist_next(l)) {
1474 MatcherProp *matcher = (MatcherProp *) l->data;
1476 if (matcherprop_criteria_headers(matcher))
1477 read_headers = TRUE;
1478 if (matcherprop_criteria_body(matcher))
1480 if (matcherprop_criteria_message(matcher)) {
1481 read_headers = TRUE;
1484 matcher->result = FALSE;
1485 matcher->done = FALSE;
1488 if (!read_headers && !read_body)
1491 file = procmsg_get_message_file_full(info, read_headers, read_body);
1495 if ((fp = g_fopen(file, "rb")) == NULL) {
1496 FILE_OP_ERROR(file, "fopen");
1501 /* read the headers */
1504 if (matcherlist_match_headers(matchers, fp))
1507 matcherlist_skip_headers(fp);
1512 matcherlist_match_body(matchers, fp);
1515 for (l = matchers->matchers; l != NULL; l = g_slist_next(l)) {
1516 MatcherProp *matcher = (MatcherProp *) l->data;
1518 if (matcherprop_criteria_headers(matcher) ||
1519 matcherprop_criteria_body(matcher) ||
1520 matcherprop_criteria_message(matcher)) {
1521 if (matcher->result) {
1522 if (!matchers->bool_and) {
1528 if (matchers->bool_and) {
1544 *\brief Test list of conditions on a message.
1546 *\param matchers List of conditions
1547 *\param info Message info
1549 *\return gboolean TRUE if matched
1551 gboolean matcherlist_match(MatcherList *matchers, MsgInfo *info)
1559 if (matchers->bool_and)
1564 /* test the cached elements */
1566 for (l = matchers->matchers; l != NULL ;l = g_slist_next(l)) {
1567 MatcherProp *matcher = (MatcherProp *) l->data;
1569 if (debug_filtering_session) {
1570 gchar *buf = matcherprop_to_string(matcher);
1571 log_print(LOG_DEBUG_FILTERING, _("checking if message matches [ %s ]\n"), buf);
1575 switch(matcher->criteria) {
1576 case MATCHCRITERIA_ALL:
1577 case MATCHCRITERIA_UNREAD:
1578 case MATCHCRITERIA_NOT_UNREAD:
1579 case MATCHCRITERIA_NEW:
1580 case MATCHCRITERIA_NOT_NEW:
1581 case MATCHCRITERIA_MARKED:
1582 case MATCHCRITERIA_NOT_MARKED:
1583 case MATCHCRITERIA_DELETED:
1584 case MATCHCRITERIA_NOT_DELETED:
1585 case MATCHCRITERIA_REPLIED:
1586 case MATCHCRITERIA_NOT_REPLIED:
1587 case MATCHCRITERIA_FORWARDED:
1588 case MATCHCRITERIA_NOT_FORWARDED:
1589 case MATCHCRITERIA_LOCKED:
1590 case MATCHCRITERIA_NOT_LOCKED:
1591 case MATCHCRITERIA_SPAM:
1592 case MATCHCRITERIA_NOT_SPAM:
1593 case MATCHCRITERIA_COLORLABEL:
1594 case MATCHCRITERIA_NOT_COLORLABEL:
1595 case MATCHCRITERIA_IGNORE_THREAD:
1596 case MATCHCRITERIA_NOT_IGNORE_THREAD:
1597 case MATCHCRITERIA_SUBJECT:
1598 case MATCHCRITERIA_NOT_SUBJECT:
1599 case MATCHCRITERIA_FROM:
1600 case MATCHCRITERIA_NOT_FROM:
1601 case MATCHCRITERIA_TO:
1602 case MATCHCRITERIA_NOT_TO:
1603 case MATCHCRITERIA_CC:
1604 case MATCHCRITERIA_NOT_CC:
1605 case MATCHCRITERIA_TO_OR_CC:
1606 case MATCHCRITERIA_NOT_TO_AND_NOT_CC:
1607 case MATCHCRITERIA_AGE_GREATER:
1608 case MATCHCRITERIA_AGE_LOWER:
1609 case MATCHCRITERIA_NEWSGROUPS:
1610 case MATCHCRITERIA_NOT_NEWSGROUPS:
1611 case MATCHCRITERIA_INREPLYTO:
1612 case MATCHCRITERIA_NOT_INREPLYTO:
1613 case MATCHCRITERIA_REFERENCES:
1614 case MATCHCRITERIA_NOT_REFERENCES:
1615 case MATCHCRITERIA_SCORE_GREATER:
1616 case MATCHCRITERIA_SCORE_LOWER:
1617 case MATCHCRITERIA_SCORE_EQUAL:
1618 case MATCHCRITERIA_SIZE_GREATER:
1619 case MATCHCRITERIA_SIZE_SMALLER:
1620 case MATCHCRITERIA_SIZE_EQUAL:
1621 case MATCHCRITERIA_TEST:
1622 case MATCHCRITERIA_NOT_TEST:
1623 case MATCHCRITERIA_PARTIAL:
1624 case MATCHCRITERIA_NOT_PARTIAL:
1625 if (matcherprop_match(matcher, info)) {
1626 if (!matchers->bool_and) {
1627 if (debug_filtering_session)
1628 log_status_ok(LOG_DEBUG_FILTERING, _("message matches\n"));
1633 if (matchers->bool_and) {
1634 if (debug_filtering_session)
1635 log_status_nok(LOG_DEBUG_FILTERING, _("message does not match\n"));
1642 /* test the condition on the file */
1644 if (matcherlist_match_file(matchers, info, result)) {
1645 if (!matchers->bool_and) {
1646 if (debug_filtering_session)
1647 log_status_ok(LOG_DEBUG_FILTERING, _("message matches\n"));
1651 if (matchers->bool_and) {
1652 if (debug_filtering_session)
1653 log_status_nok(LOG_DEBUG_FILTERING, _("message does not match\n"));
1658 if (debug_filtering_session) {
1660 log_status_ok(LOG_DEBUG_FILTERING, _("message matches\n"));
1662 log_status_nok(LOG_DEBUG_FILTERING, _("message does not match\n"));
1668 static gint quote_filter_str(gchar * result, guint size,
1678 for(p = path ; * p != '\0' ; p ++) {
1680 if ((* p != '\"') && (* p != '\\')) {
1681 if (remaining > 0) {
1687 result[size - 1] = '\0';
1692 if (remaining >= 2) {
1700 result[size - 1] = '\0';
1705 if (remaining > 0) {
1709 result[size - 1] = '\0';
1717 gchar * matcher_quote_str(const gchar * src)
1722 len = strlen(src) * 2 + 1;
1723 res = g_malloc(len);
1724 quote_filter_str(res, len, src);
1730 *\brief Convert a matcher structure to a string
1732 *\param matcher Matcher structure
1734 *\return gchar * Newly allocated string
1736 gchar *matcherprop_to_string(MatcherProp *matcher)
1738 gchar *matcher_str = NULL;
1739 const gchar *criteria_str;
1740 const gchar *matchtype_str;
1742 gchar * quoted_expr;
1743 gchar * quoted_header;
1745 criteria_str = NULL;
1746 for (i = 0; i < (int) (sizeof(matchparser_tab) / sizeof(MatchParser)); i++) {
1747 if (matchparser_tab[i].id == matcher->criteria)
1748 criteria_str = matchparser_tab[i].str;
1750 if (criteria_str == NULL)
1753 switch (matcher->criteria) {
1754 case MATCHCRITERIA_AGE_GREATER:
1755 case MATCHCRITERIA_AGE_LOWER:
1756 case MATCHCRITERIA_SCORE_GREATER:
1757 case MATCHCRITERIA_SCORE_LOWER:
1758 case MATCHCRITERIA_SCORE_EQUAL:
1759 case MATCHCRITERIA_SIZE_GREATER:
1760 case MATCHCRITERIA_SIZE_SMALLER:
1761 case MATCHCRITERIA_SIZE_EQUAL:
1762 case MATCHCRITERIA_COLORLABEL:
1763 case MATCHCRITERIA_NOT_COLORLABEL:
1764 return g_strdup_printf("%s %i", criteria_str, matcher->value);
1765 case MATCHCRITERIA_ALL:
1766 case MATCHCRITERIA_UNREAD:
1767 case MATCHCRITERIA_NOT_UNREAD:
1768 case MATCHCRITERIA_NEW:
1769 case MATCHCRITERIA_NOT_NEW:
1770 case MATCHCRITERIA_MARKED:
1771 case MATCHCRITERIA_NOT_MARKED:
1772 case MATCHCRITERIA_DELETED:
1773 case MATCHCRITERIA_NOT_DELETED:
1774 case MATCHCRITERIA_REPLIED:
1775 case MATCHCRITERIA_NOT_REPLIED:
1776 case MATCHCRITERIA_FORWARDED:
1777 case MATCHCRITERIA_NOT_FORWARDED:
1778 case MATCHCRITERIA_LOCKED:
1779 case MATCHCRITERIA_NOT_LOCKED:
1780 case MATCHCRITERIA_SPAM:
1781 case MATCHCRITERIA_NOT_SPAM:
1782 case MATCHCRITERIA_PARTIAL:
1783 case MATCHCRITERIA_NOT_PARTIAL:
1784 case MATCHCRITERIA_IGNORE_THREAD:
1785 case MATCHCRITERIA_NOT_IGNORE_THREAD:
1786 return g_strdup(criteria_str);
1787 case MATCHCRITERIA_TEST:
1788 case MATCHCRITERIA_NOT_TEST:
1789 quoted_expr = matcher_quote_str(matcher->expr);
1790 matcher_str = g_strdup_printf("%s \"%s\"",
1791 criteria_str, quoted_expr);
1792 g_free(quoted_expr);
1794 case MATCHCRITERIA_FOUND_IN_ADDRESSBOOK:
1795 case MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK:
1796 quoted_header = matcher_quote_str(matcher->header);
1797 quoted_expr = matcher_quote_str(matcher->expr);
1798 matcher_str = g_strdup_printf("%s \"%s\" in \"%s\"",
1799 criteria_str, quoted_header, quoted_expr);
1800 g_free(quoted_header);
1801 g_free(quoted_expr);
1805 matchtype_str = NULL;
1806 for (i = 0; i < sizeof matchparser_tab / sizeof matchparser_tab[0]; i++) {
1807 if (matchparser_tab[i].id == matcher->matchtype)
1808 matchtype_str = matchparser_tab[i].str;
1811 if (matchtype_str == NULL)
1814 switch (matcher->matchtype) {
1815 case MATCHTYPE_MATCH:
1816 case MATCHTYPE_MATCHCASE:
1817 case MATCHTYPE_REGEXP:
1818 case MATCHTYPE_REGEXPCASE:
1819 quoted_expr = matcher_quote_str(matcher->expr);
1820 if (matcher->header) {
1821 quoted_header = matcher_quote_str(matcher->header);
1822 matcher_str = g_strdup_printf
1823 ("%s \"%s\" %s \"%s\"",
1824 criteria_str, quoted_header,
1825 matchtype_str, quoted_expr);
1826 g_free(quoted_header);
1829 matcher_str = g_strdup_printf
1830 ("%s %s \"%s\"", criteria_str,
1831 matchtype_str, quoted_expr);
1832 g_free(quoted_expr);
1840 *\brief Convert a list of conditions to a string
1842 *\param matchers List of conditions
1844 *\return gchar * Newly allocated string
1846 gchar *matcherlist_to_string(const MatcherList *matchers)
1852 gchar *result = NULL;
1854 count = g_slist_length(matchers->matchers);
1855 vstr = g_new(gchar *, count + 1);
1857 for (l = matchers->matchers, cur_str = vstr; l != NULL;
1858 l = g_slist_next(l), cur_str ++) {
1859 *cur_str = matcherprop_to_string((MatcherProp *) l->data);
1860 if (*cur_str == NULL)
1865 if (matchers->bool_and)
1866 result = g_strjoinv(" & ", vstr);
1868 result = g_strjoinv(" | ", vstr);
1870 for (cur_str = vstr ; *cur_str != NULL ; cur_str ++)
1878 #define STRLEN_ZERO(s) ((s) ? strlen(s) : 0)
1879 #define STRLEN_DEFAULT(s,d) ((s) ? strlen(s) : STRLEN_ZERO(d))
1881 static void add_str_default(gchar ** dest,
1882 const gchar * s, const gchar * d)
1884 gchar quoted_str[4096];
1892 quote_cmd_argument(quoted_str, sizeof(quoted_str), str);
1893 strcpy(* dest, quoted_str);
1895 (* dest) += strlen(* dest);
1898 /* matching_build_command() - preferably cmd should be unescaped */
1900 *\brief Build the command line to execute
1902 *\param cmd String with command line specifiers
1903 *\param info Message info to use for command
1905 *\return gchar * Newly allocated string
1907 gchar *matching_build_command(const gchar *cmd, MsgInfo *info)
1909 const gchar *s = cmd;
1910 gchar *filename = NULL;
1911 gchar *processed_cmd;
1915 const gchar *const no_subject = _("(none)") ;
1916 const gchar *const no_from = _("(none)") ;
1917 const gchar *const no_to = _("(none)") ;
1918 const gchar *const no_cc = _("(none)") ;
1919 const gchar *const no_date = _("(none)") ;
1920 const gchar *const no_msgid = _("(none)") ;
1921 const gchar *const no_newsgroups = _("(none)") ;
1922 const gchar *const no_references = _("(none)") ;
1924 size = STRLEN_ZERO(cmd) + 1;
1925 while (*s != '\0') {
1932 case 's': /* subject */
1933 size += STRLEN_DEFAULT(info->subject, no_subject) - 2;
1935 case 'f': /* from */
1936 size += STRLEN_DEFAULT(info->from, no_from) - 2;
1939 size += STRLEN_DEFAULT(info->to, no_to) - 2;
1942 size += STRLEN_DEFAULT(info->cc, no_cc) - 2;
1944 case 'd': /* date */
1945 size += STRLEN_DEFAULT(info->date, no_date) - 2;
1947 case 'i': /* message-id */
1948 size += STRLEN_DEFAULT(info->msgid, no_msgid) - 2;
1950 case 'n': /* newsgroups */
1951 size += STRLEN_DEFAULT(info->newsgroups, no_newsgroups) - 2;
1953 case 'r': /* references */
1954 /* FIXME: using the inreplyto header for reference */
1955 size += STRLEN_DEFAULT(info->inreplyto, no_references) - 2;
1957 case 'F': /* file */
1958 if (filename == NULL)
1959 filename = folder_item_fetch_msg(info->folder, info->msgnum);
1961 if (filename == NULL) {
1962 g_warning("filename is not set");
1966 size += strlen(filename) - 2;
1975 /* as the string can be quoted, we double the result */
1978 processed_cmd = g_new0(gchar, size);
1982 while (*s != '\0') {
1990 case 's': /* subject */
1991 add_str_default(&p, info->subject,
1994 case 'f': /* from */
1995 add_str_default(&p, info->from,
1999 add_str_default(&p, info->to,
2003 add_str_default(&p, info->cc,
2006 case 'd': /* date */
2007 add_str_default(&p, info->date,
2010 case 'i': /* message-id */
2011 add_str_default(&p, info->msgid,
2014 case 'n': /* newsgroups */
2015 add_str_default(&p, info->newsgroups,
2018 case 'r': /* references */
2019 /* FIXME: using the inreplyto header for references */
2020 add_str_default(&p, info->inreplyto, no_references);
2022 case 'F': /* file */
2023 if (filename != NULL)
2024 add_str_default(&p, filename, NULL);
2043 return processed_cmd;
2045 #undef STRLEN_DEFAULT
2048 /* ************************************************************ */
2052 *\brief Write filtering list to file
2055 *\param prefs_filtering List of filtering conditions
2057 static void prefs_filtering_write(FILE *fp, GSList *prefs_filtering)
2061 for (cur = prefs_filtering; cur != NULL; cur = cur->next) {
2062 gchar *filtering_str = NULL;
2063 gchar *tmp_name = NULL;
2064 FilteringProp *prop = NULL;
2066 if (NULL == (prop = (FilteringProp *) cur->data))
2069 if (NULL == (filtering_str = filteringprop_to_string(prop)))
2072 if (prop->enabled) {
2073 if (fputs("enabled ", fp) == EOF) {
2074 FILE_OP_ERROR("filtering config", "fputs");
2078 if (fputs("disabled ", fp) == EOF) {
2079 FILE_OP_ERROR("filtering config", "fputs");
2084 if (fputs("rulename \"", fp) == EOF) {
2085 FILE_OP_ERROR("filtering config", "fputs");
2086 g_free(filtering_str);
2089 tmp_name = prop->name;
2090 while (tmp_name && *tmp_name != '\0') {
2091 if (*tmp_name != '"') {
2092 if (fputc(*tmp_name, fp) == EOF) {
2093 FILE_OP_ERROR("filtering config", "fputs || fputc");
2094 g_free(filtering_str);
2097 } else if (*tmp_name == '"') {
2098 if (fputc('\\', fp) == EOF ||
2099 fputc('"', fp) == EOF) {
2100 FILE_OP_ERROR("filtering config", "fputs || fputc");
2101 g_free(filtering_str);
2107 if (fputs("\" ", fp) == EOF) {
2108 FILE_OP_ERROR("filtering config", "fputs");
2109 g_free(filtering_str);
2113 if (prop->account_id != 0) {
2116 tmp = g_strdup_printf("account %d ", prop->account_id);
2117 if (fputs(tmp, fp) == EOF) {
2118 FILE_OP_ERROR("filtering config", "fputs");
2125 if(fputs(filtering_str, fp) == EOF ||
2126 fputc('\n', fp) == EOF) {
2127 FILE_OP_ERROR("filtering config", "fputs || fputc");
2128 g_free(filtering_str);
2131 g_free(filtering_str);
2136 *\brief Write matchers from a folder item
2138 *\param node Node with folder info
2139 *\param data File pointer
2141 *\return gboolean FALSE
2143 static gboolean prefs_matcher_write_func(GNode *node, gpointer data)
2148 GSList *prefs_filtering;
2151 /* prevent warning */
2152 if (item->path == NULL)
2154 id = folder_item_get_identifier(item);
2157 prefs_filtering = item->prefs->processing;
2159 if (prefs_filtering != NULL) {
2160 fprintf(fp, "[%s]\n", id);
2161 prefs_filtering_write(fp, prefs_filtering);
2171 *\brief Save matchers from folder items
2175 static void prefs_matcher_save(FILE *fp)
2179 for (cur = folder_get_list() ; cur != NULL ; cur = g_list_next(cur)) {
2182 folder = (Folder *) cur->data;
2183 g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
2184 prefs_matcher_write_func, fp);
2187 /* pre global rules */
2188 fprintf(fp, "[preglobal]\n");
2189 prefs_filtering_write(fp, pre_global_processing);
2192 /* post global rules */
2193 fprintf(fp, "[postglobal]\n");
2194 prefs_filtering_write(fp, post_global_processing);
2197 /* filtering rules */
2198 fprintf(fp, "[filtering]\n");
2199 prefs_filtering_write(fp, filtering_rules);
2204 *\brief Write filtering / matcher configuration file
2206 void prefs_matcher_write_config(void)
2211 debug_print("Writing matcher configuration...\n");
2213 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
2216 if ((pfile = prefs_write_open(rcpath)) == NULL) {
2217 g_warning("failed to write configuration to file\n");
2223 prefs_matcher_save(pfile->fp);
2227 if (prefs_file_close(pfile) < 0) {
2228 g_warning("failed to write configuration to file\n");
2233 /* ******************************************************************* */
2235 static void matcher_add_rulenames(const gchar *rcpath)
2237 gchar *newpath = g_strconcat(rcpath, ".new", NULL);
2238 FILE *src = g_fopen(rcpath, "rb");
2239 FILE *dst = g_fopen(newpath, "wb");
2240 gchar buf[BUFFSIZE];
2248 while (fgets (buf, sizeof(buf), src) != NULL) {
2249 if (strlen(buf) > 2 && buf[0] != '['
2250 && strncmp(buf, "rulename \"", 10)
2251 && strncmp(buf, "enabled rulename \"", 18)
2252 && strncmp(buf, "disabled rulename \"", 18)) {
2253 fwrite("enabled rulename \"\" ",
2254 strlen("enabled rulename \"\" "), 1, dst);
2256 fwrite(buf, strlen(buf), 1, dst);
2260 move_file(newpath, rcpath, TRUE);
2265 *\brief Read matcher configuration
2267 void prefs_matcher_read_config(void)
2270 gchar *rc_old_format;
2273 create_matchparser_hashtab();
2274 prefs_filtering_clear();
2276 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MATCHER_RC, NULL);
2277 rc_old_format = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MATCHER_RC,
2278 ".pre_names", NULL);
2280 if (!is_file_exist(rc_old_format) && is_file_exist(rcpath)) {
2281 /* backup file with no rules names, in case
2282 * anything goes wrong */
2283 copy_file(rcpath, rc_old_format, FALSE);
2284 /* now hack the file in order to have it to the new format */
2285 matcher_add_rulenames(rcpath);
2288 g_free(rc_old_format);
2290 f = g_fopen(rcpath, "rb");
2294 matcher_parser_start_parsing(f);
2295 fclose(matcher_parserin);
2298 /* previous version compatibility */
2300 /* printf("reading filtering\n"); */
2301 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
2302 FILTERING_RC, NULL);
2303 f = g_fopen(rcpath, "rb");
2307 matcher_parser_start_parsing(f);
2308 fclose(matcher_parserin);
2311 /* printf("reading scoring\n"); */
2312 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
2314 f = g_fopen(rcpath, "rb");
2318 matcher_parser_start_parsing(f);
2319 fclose(matcher_parserin);