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);
503 /* FIXME body search is a hack. */
504 static gboolean matcherprop_string_decode_match(MatcherProp *prop, const gchar *str,
505 const gchar *debug_context)
509 gboolean res = FALSE;
514 /* we try to decode QP first, because it's faster than base64 */
515 qp_decode_const(tmp, BUFFSIZE-1, str);
516 if (!g_utf8_validate(tmp, -1, NULL)) {
517 utf = conv_codeset_strdup
518 (tmp, conv_get_locale_charset_str_no_utf8(),
520 res = matcherprop_string_match(prop, utf, debug_context);
523 res = matcherprop_string_match(prop, tmp, debug_context);
526 if (res == FALSE && (strchr(prop->expr, '=') || strchr(prop->expr, '_')
527 || strchr(str, '=') || strchr(str, '_'))) {
528 /* if searching for something with an equal char, maybe
529 * we should try to match the non-decoded string.
530 * In case it was not qp-encoded. */
531 if (!g_utf8_validate(str, -1, NULL)) {
532 utf = conv_codeset_strdup
533 (str, conv_get_locale_charset_str_no_utf8(),
535 res = matcherprop_string_match(prop, utf, debug_context);
538 res = matcherprop_string_match(prop, str, debug_context);
542 /* FIXME base64 decoding is too slow, especially since text can
543 * easily be handled as base64. Don't even try now. */
549 typedef struct _thread_data {
556 static void *matcher_test_thread(void *data)
558 thread_data *td = (thread_data *)data;
561 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
562 pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
564 result = system(td->cmd);
565 if (result) perror("system");
566 td->done = TRUE; /* let the caller thread join() */
567 return GINT_TO_POINTER(result);
572 *\brief Execute a command defined in the matcher structure
574 *\param prop Pointer to matcher structure
575 *\param info Pointer to message info structure
577 *\return gboolean TRUE if command was executed succesfully
579 static gboolean matcherprop_match_test(const MatcherProp *prop,
587 thread_data *td = g_new0(thread_data, 1);
589 time_t start_time = time(NULL);
592 file = procmsg_get_message_file(info);
597 cmd = matching_build_command(prop->expr, info);
601 #if (defined USE_PTHREAD && defined __GLIBC__ && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 3)))
603 if (debug_filtering_session
604 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
605 log_print(LOG_DEBUG_FILTERING,
606 "starting threaded command [ %s ]\n",
612 if (pthread_create(&pt, PTHREAD_CREATE_JOINABLE,
613 matcher_test_thread, td) != 0)
614 retval = system(cmd);
616 printf("waiting for test thread\n");
618 /* don't let the interface freeze while waiting */
620 if (time(NULL) - start_time > 30) {
626 pthread_join(pt, &res);
627 retval = GPOINTER_TO_INT(res);
628 printf(" test thread returned %d\n", retval);
633 if (debug_filtering_session
634 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
635 log_print(LOG_DEBUG_FILTERING,
636 "starting synchronous command [ %s ]\n",
640 retval = system(cmd);
642 debug_print("Command exit code: %d\n", retval);
645 if (debug_filtering_session
646 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
647 log_print(LOG_DEBUG_FILTERING,
648 "command returned [ %d ]\n",
653 return (retval == 0);
657 *\brief Check if a message matches the condition in a matcher
660 *\param prop Pointer to matcher structure
661 *\param info Pointer to message info
663 *\return gboolean TRUE if a match
665 gboolean matcherprop_match(MatcherProp *prop,
670 switch(prop->criteria) {
671 case MATCHCRITERIA_ALL:
673 case MATCHCRITERIA_UNREAD:
674 return MSG_IS_UNREAD(info->flags);
675 case MATCHCRITERIA_NOT_UNREAD:
676 return !MSG_IS_UNREAD(info->flags);
677 case MATCHCRITERIA_NEW:
678 return MSG_IS_NEW(info->flags);
679 case MATCHCRITERIA_NOT_NEW:
680 return !MSG_IS_NEW(info->flags);
681 case MATCHCRITERIA_MARKED:
682 return MSG_IS_MARKED(info->flags);
683 case MATCHCRITERIA_NOT_MARKED:
684 return !MSG_IS_MARKED(info->flags);
685 case MATCHCRITERIA_DELETED:
686 return MSG_IS_DELETED(info->flags);
687 case MATCHCRITERIA_NOT_DELETED:
688 return !MSG_IS_DELETED(info->flags);
689 case MATCHCRITERIA_REPLIED:
690 return MSG_IS_REPLIED(info->flags);
691 case MATCHCRITERIA_NOT_REPLIED:
692 return !MSG_IS_REPLIED(info->flags);
693 case MATCHCRITERIA_FORWARDED:
694 return MSG_IS_FORWARDED(info->flags);
695 case MATCHCRITERIA_NOT_FORWARDED:
696 return !MSG_IS_FORWARDED(info->flags);
697 case MATCHCRITERIA_LOCKED:
698 return MSG_IS_LOCKED(info->flags);
699 case MATCHCRITERIA_NOT_LOCKED:
700 return !MSG_IS_LOCKED(info->flags);
701 case MATCHCRITERIA_SPAM:
702 return MSG_IS_SPAM(info->flags);
703 case MATCHCRITERIA_NOT_SPAM:
704 return !MSG_IS_SPAM(info->flags);
705 case MATCHCRITERIA_COLORLABEL:
707 gint color = MSG_GET_COLORLABEL_VALUE(info->flags);
708 gboolean ret = (color == prop->value);
711 if (debug_filtering_session
712 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
714 log_print(LOG_DEBUG_FILTERING,
715 "message color value [ %d ] matches color value [ %d ]\n",
718 log_print(LOG_DEBUG_FILTERING,
719 "message color value [ %d ] doesn't matches color value [ %d ]\n",
725 case MATCHCRITERIA_NOT_COLORLABEL:
727 gint color = MSG_GET_COLORLABEL_VALUE(info->flags);
728 gboolean ret = (color != prop->value);
731 if (debug_filtering_session
732 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
734 log_print(LOG_DEBUG_FILTERING,
735 "message color value [ %d ] matches color value [ %d ]\n",
738 log_print(LOG_DEBUG_FILTERING,
739 "message color value [ %d ] doesn't matches color value [ %d ]\n",
745 case MATCHCRITERIA_IGNORE_THREAD:
746 return MSG_IS_IGNORE_THREAD(info->flags);
747 case MATCHCRITERIA_NOT_IGNORE_THREAD:
748 return !MSG_IS_IGNORE_THREAD(info->flags);
749 case MATCHCRITERIA_SUBJECT:
750 return matcherprop_string_match(prop, info->subject, _("subject"));
751 case MATCHCRITERIA_NOT_SUBJECT:
752 return !matcherprop_string_match(prop, info->subject, _("subject"));
753 case MATCHCRITERIA_FROM:
754 return matcherprop_string_match(prop, info->from, _("From: header"));
755 case MATCHCRITERIA_NOT_FROM:
756 return !matcherprop_string_match(prop, info->from, _("From: header"));
757 case MATCHCRITERIA_TO:
758 return matcherprop_string_match(prop, info->to, _("To: header"));
759 case MATCHCRITERIA_NOT_TO:
760 return !matcherprop_string_match(prop, info->to, _("To: header"));
761 case MATCHCRITERIA_CC:
762 return matcherprop_string_match(prop, info->cc, _("Cc: header"));
763 case MATCHCRITERIA_NOT_CC:
764 return !matcherprop_string_match(prop, info->cc, _("Cc: header"));
765 case MATCHCRITERIA_TO_OR_CC:
766 return matcherprop_string_match(prop, info->to, _("To: header"))
767 || matcherprop_string_match(prop, info->cc, _("Cc: header"));
768 case MATCHCRITERIA_NOT_TO_AND_NOT_CC:
769 return !(matcherprop_string_match(prop, info->to, _("To:"))
770 || matcherprop_string_match(prop, info->cc, _("Cc:")));
771 case MATCHCRITERIA_AGE_GREATER:
777 age = ((t - info->date_t) / (60 * 60 * 24));
778 ret = (age > prop->value);
781 if (debug_filtering_session
782 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
784 log_print(LOG_DEBUG_FILTERING,
785 "message age [ %d ] is greater than [ %d ]\n",
788 log_print(LOG_DEBUG_FILTERING,
789 "message age [ %d ] is not greater than [ %d ]\n",
795 case MATCHCRITERIA_AGE_LOWER:
801 age = ((t - info->date_t) / (60 * 60 * 24));
802 ret = (age < prop->value);
805 if (debug_filtering_session
806 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
808 log_print(LOG_DEBUG_FILTERING,
809 "message age [ %d ] is lower than [ %d ]\n",
812 log_print(LOG_DEBUG_FILTERING,
813 "message age [ %d ] is not lower than [ %d ]\n",
819 case MATCHCRITERIA_SCORE_GREATER:
821 gboolean ret = (info->score > prop->value);
824 if (debug_filtering_session
825 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
827 log_print(LOG_DEBUG_FILTERING,
828 "message score [ %d ] is greater than [ %d ]\n",
829 info->score, prop->value);
831 log_print(LOG_DEBUG_FILTERING,
832 "message score [ %d ] is not greater than [ %d ]\n",
833 info->score, prop->value);
838 case MATCHCRITERIA_SCORE_LOWER:
840 gboolean ret = (info->score < prop->value);
843 if (debug_filtering_session
844 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
846 log_print(LOG_DEBUG_FILTERING,
847 "message score [ %d ] is lower than [ %d ]\n",
848 info->score, prop->value);
850 log_print(LOG_DEBUG_FILTERING,
851 "message score [ %d ] is not lower than [ %d ]\n",
852 info->score, prop->value);
857 case MATCHCRITERIA_SCORE_EQUAL:
859 gboolean ret = (info->score == prop->value);
862 if (debug_filtering_session
863 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
865 log_print(LOG_DEBUG_FILTERING,
866 "message score [ %d ] is equal to [ %d ]\n",
867 info->score, prop->value);
869 log_print(LOG_DEBUG_FILTERING,
870 "message score [ %d ] is not equal to [ %d ]\n",
871 info->score, prop->value);
876 case MATCHCRITERIA_SIZE_GREATER:
878 /* FIXME: info->size is an off_t */
879 gboolean ret = (info->size > (off_t) prop->value);
882 if (debug_filtering_session
883 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
885 log_print(LOG_DEBUG_FILTERING,
886 "message size [ %ld ] is greater than [ %d ]\n",
887 info->size, prop->value);
889 log_print(LOG_DEBUG_FILTERING,
890 "message size [ %ld ] is not greater than [ %d ]\n",
891 info->size, prop->value);
896 case MATCHCRITERIA_SIZE_SMALLER:
898 /* FIXME: info->size is an off_t */
899 gboolean ret = (info->size < (off_t) prop->value);
902 if (debug_filtering_session
903 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
905 log_print(LOG_DEBUG_FILTERING,
906 "message size [ %ld ] is smaller than [ %d ]\n",
907 info->size, prop->value);
909 log_print(LOG_DEBUG_FILTERING,
910 "message size [ %ld ] is not smaller than [ %d ]\n",
911 info->size, prop->value);
916 case MATCHCRITERIA_SIZE_EQUAL:
918 /* FIXME: info->size is an off_t */
919 gboolean ret = (info->size == (off_t) prop->value);
922 if (debug_filtering_session
923 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
925 log_print(LOG_DEBUG_FILTERING,
926 "message size [ %ld ] is equal to [ %d ]\n",
927 info->size, prop->value);
929 log_print(LOG_DEBUG_FILTERING,
930 "message size [ %ld ] is not equal to [ %d ]\n",
931 info->size, prop->value);
936 case MATCHCRITERIA_PARTIAL:
938 /* FIXME: info->size is an off_t */
939 gboolean ret = (info->total_size != 0 && info->size != (off_t)info->total_size);
942 if (debug_filtering_session
943 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
945 log_print(LOG_DEBUG_FILTERING,
946 "message is partially downloaded, size [ %ld ] is less than total size [ %d ])\n",
947 info->size, info->total_size);
949 log_print(LOG_DEBUG_FILTERING,
950 "message is not partially downloaded\n");
955 case MATCHCRITERIA_NOT_PARTIAL:
957 /* FIXME: info->size is an off_t */
958 gboolean ret = (info->total_size == 0 || info->size == (off_t)info->total_size);
961 if (debug_filtering_session
962 && prefs_common.filtering_debug_level >= FILTERING_DEBUG_LEVEL_HIGH) {
964 log_print(LOG_DEBUG_FILTERING,
965 "message is not partially downloaded\n");
967 log_print(LOG_DEBUG_FILTERING,
968 "message is partially downloaded, size [ %ld ] is less than total size [ %d ])\n",
969 info->size, info->total_size);
974 case MATCHCRITERIA_NEWSGROUPS:
975 return matcherprop_string_match(prop, info->newsgroups, _("Newsgroups: header"));
976 case MATCHCRITERIA_NOT_NEWSGROUPS:
977 return !matcherprop_string_match(prop, info->newsgroups, _("Newsgroups: header"));
978 case MATCHCRITERIA_INREPLYTO:
979 return matcherprop_string_match(prop, info->inreplyto, _("InReplyTo: header"));
980 case MATCHCRITERIA_NOT_INREPLYTO:
981 return !matcherprop_string_match(prop, info->inreplyto, _("InReplyTo: header"));
982 /* FIXME: Using inreplyto, but matching the (newly implemented)
983 * list of references is better */
984 case MATCHCRITERIA_REFERENCES:
985 return matcherprop_string_match(prop, info->inreplyto, _("InReplyTo: header (references)"));
986 case MATCHCRITERIA_NOT_REFERENCES:
987 return !matcherprop_string_match(prop, info->inreplyto, _("InReplyTo: header (references)"));
988 case MATCHCRITERIA_TEST:
989 return matcherprop_match_test(prop, info);
990 case MATCHCRITERIA_NOT_TEST:
991 return !matcherprop_match_test(prop, info);
997 /* ********************* MatcherList *************************** */
1000 *\brief Create a new list of matchers
1002 *\param matchers List of matcher structures
1003 *\param bool_and Operator
1005 *\return MatcherList * New list
1007 MatcherList *matcherlist_new(GSList *matchers, gboolean bool_and)
1011 cond = g_new0(MatcherList, 1);
1013 cond->matchers = matchers;
1014 cond->bool_and = bool_and;
1020 *\brief Frees a list of matchers
1022 *\param cond List of matchers
1024 void matcherlist_free(MatcherList *cond)
1028 g_return_if_fail(cond);
1029 for (l = cond->matchers ; l != NULL ; l = g_slist_next(l)) {
1030 matcherprop_free((MatcherProp *) l->data);
1036 *\brief Skip all headers in a message file
1038 *\param fp Message file
1040 static void matcherlist_skip_headers(FILE *fp)
1042 gchar buf[BUFFSIZE];
1044 while (procheader_get_one_field(buf, sizeof(buf), fp, NULL) != -1)
1049 *\brief Check if a header matches a matcher condition
1051 *\param matcher Matcher structure to check header for
1052 *\param buf Header name
1054 *\return boolean TRUE if matching header
1056 static gboolean matcherprop_match_one_header(MatcherProp *matcher,
1059 gboolean result = FALSE;
1060 Header *header = NULL;
1062 switch (matcher->criteria) {
1063 case MATCHCRITERIA_HEADER:
1064 case MATCHCRITERIA_NOT_HEADER:
1065 header = procheader_parse_header(buf);
1068 if (procheader_headername_equal(header->name,
1070 if (matcher->criteria == MATCHCRITERIA_HEADER)
1071 result = matcherprop_string_match(matcher, header->body, _("header"));
1073 result = !matcherprop_string_match(matcher, header->body, _("header"));
1074 procheader_header_free(header);
1078 procheader_header_free(header);
1081 case MATCHCRITERIA_HEADERS_PART:
1082 return matcherprop_string_match(matcher, buf, _("header line"));
1083 case MATCHCRITERIA_NOT_HEADERS_PART:
1084 return !matcherprop_string_match(matcher, buf, _("headers line"));
1085 case MATCHCRITERIA_MESSAGE:
1086 return matcherprop_string_decode_match(matcher, buf, _("message line"));
1087 case MATCHCRITERIA_NOT_MESSAGE:
1088 return !matcherprop_string_decode_match(matcher, buf, _("message line"));
1089 case MATCHCRITERIA_FOUND_IN_ADDRESSBOOK:
1090 case MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK:
1092 GSList *address_list = NULL;
1093 gint match = MATCH_ONE;
1094 gboolean found = FALSE;
1096 /* how many address headers are me trying to mach? */
1097 if (strcasecmp(matcher->header, _("Any")) == 0)
1099 else if (strcasecmp(matcher->header, Q_("Filtering Matcher Menu|All")) == 0)
1102 if (match == MATCH_ONE) {
1103 /* matching one address header exactly, is that the right one? */
1104 header = procheader_parse_header(buf);
1106 !procheader_headername_equal(header->name, matcher->header))
1108 address_list = address_list_append(address_list, header->body);
1109 if (address_list == NULL)
1113 header = procheader_parse_header(buf);
1116 /* address header is one of the headers we have to match when checking
1117 for any address header or all address headers? */
1118 if (procheader_headername_equal(header->name, "From") ||
1119 procheader_headername_equal(header->name, "To") ||
1120 procheader_headername_equal(header->name, "Cc") ||
1121 procheader_headername_equal(header->name, "Reply-To") ||
1122 procheader_headername_equal(header->name, "Sender"))
1123 address_list = address_list_append(address_list, header->body);
1124 if (address_list == NULL)
1128 found = match_with_addresses_in_addressbook
1129 (matcher, address_list, matcher->criteria,
1130 matcher->expr, match);
1131 g_slist_free(address_list);
1133 if (matcher->criteria == MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK)
1144 *\brief Check if the matcher structure wants headers to
1147 *\param matcher Matcher structure
1149 *\return gboolean TRUE if the matcher structure describes
1150 * a header match condition
1152 static gboolean matcherprop_criteria_headers(const MatcherProp *matcher)
1154 switch (matcher->criteria) {
1155 case MATCHCRITERIA_HEADER:
1156 case MATCHCRITERIA_NOT_HEADER:
1157 case MATCHCRITERIA_HEADERS_PART:
1158 case MATCHCRITERIA_NOT_HEADERS_PART:
1159 case MATCHCRITERIA_FOUND_IN_ADDRESSBOOK:
1160 case MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK:
1168 *\brief Check if the matcher structure wants the message
1169 * to be matched (just perform an action on any
1172 *\param matcher Matcher structure
1174 *\return gboolean TRUE if matcher condition should match
1177 static gboolean matcherprop_criteria_message(MatcherProp *matcher)
1179 switch (matcher->criteria) {
1180 case MATCHCRITERIA_MESSAGE:
1181 case MATCHCRITERIA_NOT_MESSAGE:
1189 *\brief Check if a list of conditions matches one header in
1192 *\param matchers List of conditions
1193 *\param fp Message file
1195 *\return gboolean TRUE if one of the headers is matched by
1196 * the list of conditions.
1198 static gboolean matcherlist_match_headers(MatcherList *matchers, FILE *fp)
1201 gchar buf[BUFFSIZE];
1203 while (procheader_get_one_field(buf, sizeof(buf), fp, NULL) != -1) {
1204 for (l = matchers->matchers ; l != NULL ; l = g_slist_next(l)) {
1205 MatcherProp *matcher = (MatcherProp *) l->data;
1206 gint match = MATCH_ANY;
1211 /* determine the match range (all, any are our concern here) */
1212 if (matcher->criteria == MATCHCRITERIA_NOT_HEADERS_PART ||
1213 matcher->criteria == MATCHCRITERIA_NOT_MESSAGE) {
1216 } else if (matcher->criteria == MATCHCRITERIA_FOUND_IN_ADDRESSBOOK ||
1217 matcher->criteria == MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK) {
1218 Header *header = NULL;
1220 /* address header is one of the headers we have to match when checking
1221 for any address header or all address headers? */
1222 header = procheader_parse_header(buf);
1224 (procheader_headername_equal(header->name, "From") ||
1225 procheader_headername_equal(header->name, "To") ||
1226 procheader_headername_equal(header->name, "Cc") ||
1227 procheader_headername_equal(header->name, "Reply-To") ||
1228 procheader_headername_equal(header->name, "Sender"))) {
1230 if (strcasecmp(matcher->header, _("Any")) == 0)
1232 else if (strcasecmp(matcher->header, Q_("Filtering Matcher Menu|All")) == 0)
1237 /* further call to matcherprop_match_one_header() can't match
1238 and it irrelevant, so: don't alter the match result */
1243 /* ZERO line must NOT match for the rule to match.
1245 if (match == MATCH_ALL) {
1246 if (matcherprop_match_one_header(matcher, buf)) {
1247 matcher->result = TRUE;
1249 matcher->result = FALSE;
1250 matcher->done = TRUE;
1252 /* else, just one line matching is enough for the rule to match
1254 } else if (matcherprop_criteria_headers(matcher) ||
1255 matcherprop_criteria_message(matcher)) {
1256 if (matcherprop_match_one_header(matcher, buf)) {
1257 matcher->result = TRUE;
1258 matcher->done = TRUE;
1262 /* if the rule matched and the matchers are OR, no need to
1263 * check the others */
1264 if (matcher->result && matcher->done) {
1265 if (!matchers->bool_and)
1275 *\brief Check if a matcher wants to check the message body
1277 *\param matcher Matcher structure
1279 *\return gboolean TRUE if body must be matched.
1281 static gboolean matcherprop_criteria_body(const MatcherProp *matcher)
1283 switch (matcher->criteria) {
1284 case MATCHCRITERIA_BODY_PART:
1285 case MATCHCRITERIA_NOT_BODY_PART:
1293 *\brief Check if a (line) string matches the criteria
1294 * described by a matcher structure
1296 *\param matcher Matcher structure
1299 *\return gboolean TRUE if string matches criteria
1301 static gboolean matcherprop_match_line(MatcherProp *matcher, const gchar *line)
1303 switch (matcher->criteria) {
1304 case MATCHCRITERIA_BODY_PART:
1305 case MATCHCRITERIA_MESSAGE:
1306 return matcherprop_string_decode_match(matcher, line, _("body line"));
1307 case MATCHCRITERIA_NOT_BODY_PART:
1308 case MATCHCRITERIA_NOT_MESSAGE:
1309 return !matcherprop_string_decode_match(matcher, line, _("body line"));
1315 *\brief Check if a line in a message file's body matches
1318 *\param matchers List of conditions
1319 *\param fp Message file
1321 *\return gboolean TRUE if succesful match
1323 static gboolean matcherlist_match_body(MatcherList *matchers, FILE *fp)
1326 gchar buf[BUFFSIZE];
1328 while (fgets(buf, sizeof(buf), fp) != NULL) {
1329 for (l = matchers->matchers ; l != NULL ; l = g_slist_next(l)) {
1330 MatcherProp *matcher = (MatcherProp *) l->data;
1335 /* if the criteria is ~body_part or ~message, ZERO lines
1336 * must NOT match for the rule to match. */
1337 if (matcher->criteria == MATCHCRITERIA_NOT_BODY_PART ||
1338 matcher->criteria == MATCHCRITERIA_NOT_MESSAGE) {
1339 if (matcherprop_match_line(matcher, buf)) {
1340 matcher->result = TRUE;
1342 matcher->result = FALSE;
1343 matcher->done = TRUE;
1345 /* else, just one line has to match */
1346 } else if (matcherprop_criteria_body(matcher) ||
1347 matcherprop_criteria_message(matcher)) {
1348 if (matcherprop_match_line(matcher, buf)) {
1349 matcher->result = TRUE;
1350 matcher->done = TRUE;
1354 /* if the matchers are OR'ed and the rule matched,
1355 * no need to check the others. */
1356 if (matcher->result && matcher->done) {
1357 if (!matchers->bool_and)
1366 *\brief Check if a message file matches criteria
1368 *\param matchers Criteria
1369 *\param info Message info
1370 *\param result Default result
1372 *\return gboolean TRUE if matched
1374 static gboolean matcherlist_match_file(MatcherList *matchers, MsgInfo *info,
1377 gboolean read_headers;
1383 /* file need to be read ? */
1385 read_headers = FALSE;
1387 for (l = matchers->matchers ; l != NULL ; l = g_slist_next(l)) {
1388 MatcherProp *matcher = (MatcherProp *) l->data;
1390 if (matcherprop_criteria_headers(matcher))
1391 read_headers = TRUE;
1392 if (matcherprop_criteria_body(matcher))
1394 if (matcherprop_criteria_message(matcher)) {
1395 read_headers = TRUE;
1398 matcher->result = FALSE;
1399 matcher->done = FALSE;
1402 if (!read_headers && !read_body)
1405 file = procmsg_get_message_file_full(info, read_headers, read_body);
1409 if ((fp = g_fopen(file, "rb")) == NULL) {
1410 FILE_OP_ERROR(file, "fopen");
1415 /* read the headers */
1418 if (matcherlist_match_headers(matchers, fp))
1421 matcherlist_skip_headers(fp);
1426 matcherlist_match_body(matchers, fp);
1429 for (l = matchers->matchers; l != NULL; l = g_slist_next(l)) {
1430 MatcherProp *matcher = (MatcherProp *) l->data;
1432 if (matcherprop_criteria_headers(matcher) ||
1433 matcherprop_criteria_body(matcher) ||
1434 matcherprop_criteria_message(matcher)) {
1435 if (matcher->result) {
1436 if (!matchers->bool_and) {
1442 if (matchers->bool_and) {
1458 *\brief Test list of conditions on a message.
1460 *\param matchers List of conditions
1461 *\param info Message info
1463 *\return gboolean TRUE if matched
1465 gboolean matcherlist_match(MatcherList *matchers, MsgInfo *info)
1473 if (matchers->bool_and)
1478 /* test the cached elements */
1480 for (l = matchers->matchers; l != NULL ;l = g_slist_next(l)) {
1481 MatcherProp *matcher = (MatcherProp *) l->data;
1483 if (debug_filtering_session) {
1484 gchar *buf = matcherprop_to_string(matcher);
1485 log_print(LOG_DEBUG_FILTERING, _("checking if message matches [ %s ]\n"), buf);
1489 switch(matcher->criteria) {
1490 case MATCHCRITERIA_ALL:
1491 case MATCHCRITERIA_UNREAD:
1492 case MATCHCRITERIA_NOT_UNREAD:
1493 case MATCHCRITERIA_NEW:
1494 case MATCHCRITERIA_NOT_NEW:
1495 case MATCHCRITERIA_MARKED:
1496 case MATCHCRITERIA_NOT_MARKED:
1497 case MATCHCRITERIA_DELETED:
1498 case MATCHCRITERIA_NOT_DELETED:
1499 case MATCHCRITERIA_REPLIED:
1500 case MATCHCRITERIA_NOT_REPLIED:
1501 case MATCHCRITERIA_FORWARDED:
1502 case MATCHCRITERIA_NOT_FORWARDED:
1503 case MATCHCRITERIA_LOCKED:
1504 case MATCHCRITERIA_NOT_LOCKED:
1505 case MATCHCRITERIA_SPAM:
1506 case MATCHCRITERIA_NOT_SPAM:
1507 case MATCHCRITERIA_COLORLABEL:
1508 case MATCHCRITERIA_NOT_COLORLABEL:
1509 case MATCHCRITERIA_IGNORE_THREAD:
1510 case MATCHCRITERIA_NOT_IGNORE_THREAD:
1511 case MATCHCRITERIA_SUBJECT:
1512 case MATCHCRITERIA_NOT_SUBJECT:
1513 case MATCHCRITERIA_FROM:
1514 case MATCHCRITERIA_NOT_FROM:
1515 case MATCHCRITERIA_TO:
1516 case MATCHCRITERIA_NOT_TO:
1517 case MATCHCRITERIA_CC:
1518 case MATCHCRITERIA_NOT_CC:
1519 case MATCHCRITERIA_TO_OR_CC:
1520 case MATCHCRITERIA_NOT_TO_AND_NOT_CC:
1521 case MATCHCRITERIA_AGE_GREATER:
1522 case MATCHCRITERIA_AGE_LOWER:
1523 case MATCHCRITERIA_NEWSGROUPS:
1524 case MATCHCRITERIA_NOT_NEWSGROUPS:
1525 case MATCHCRITERIA_INREPLYTO:
1526 case MATCHCRITERIA_NOT_INREPLYTO:
1527 case MATCHCRITERIA_REFERENCES:
1528 case MATCHCRITERIA_NOT_REFERENCES:
1529 case MATCHCRITERIA_SCORE_GREATER:
1530 case MATCHCRITERIA_SCORE_LOWER:
1531 case MATCHCRITERIA_SCORE_EQUAL:
1532 case MATCHCRITERIA_SIZE_GREATER:
1533 case MATCHCRITERIA_SIZE_SMALLER:
1534 case MATCHCRITERIA_SIZE_EQUAL:
1535 case MATCHCRITERIA_TEST:
1536 case MATCHCRITERIA_NOT_TEST:
1537 case MATCHCRITERIA_PARTIAL:
1538 case MATCHCRITERIA_NOT_PARTIAL:
1539 if (matcherprop_match(matcher, info)) {
1540 if (!matchers->bool_and) {
1541 if (debug_filtering_session)
1542 log_status_ok(LOG_DEBUG_FILTERING, _("message matches\n"));
1547 if (matchers->bool_and) {
1548 if (debug_filtering_session)
1549 log_status_nok(LOG_DEBUG_FILTERING, _("message does not match\n"));
1556 /* test the condition on the file */
1558 if (matcherlist_match_file(matchers, info, result)) {
1559 if (!matchers->bool_and) {
1560 if (debug_filtering_session)
1561 log_status_ok(LOG_DEBUG_FILTERING, _("message matches\n"));
1565 if (matchers->bool_and) {
1566 if (debug_filtering_session)
1567 log_status_nok(LOG_DEBUG_FILTERING, _("message does not match\n"));
1572 if (debug_filtering_session) {
1574 log_status_ok(LOG_DEBUG_FILTERING, _("message matches\n"));
1576 log_status_nok(LOG_DEBUG_FILTERING, _("message does not match\n"));
1582 static gint quote_filter_str(gchar * result, guint size,
1592 for(p = path ; * p != '\0' ; p ++) {
1594 if ((* p != '\"') && (* p != '\\')) {
1595 if (remaining > 0) {
1601 result[size - 1] = '\0';
1606 if (remaining >= 2) {
1614 result[size - 1] = '\0';
1619 if (remaining > 0) {
1623 result[size - 1] = '\0';
1631 gchar * matcher_quote_str(const gchar * src)
1636 len = strlen(src) * 2 + 1;
1637 res = g_malloc(len);
1638 quote_filter_str(res, len, src);
1644 *\brief Convert a matcher structure to a string
1646 *\param matcher Matcher structure
1648 *\return gchar * Newly allocated string
1650 gchar *matcherprop_to_string(MatcherProp *matcher)
1652 gchar *matcher_str = NULL;
1653 const gchar *criteria_str;
1654 const gchar *matchtype_str;
1656 gchar * quoted_expr;
1657 gchar * quoted_header;
1659 criteria_str = NULL;
1660 for (i = 0; i < (int) (sizeof(matchparser_tab) / sizeof(MatchParser)); i++) {
1661 if (matchparser_tab[i].id == matcher->criteria)
1662 criteria_str = matchparser_tab[i].str;
1664 if (criteria_str == NULL)
1667 switch (matcher->criteria) {
1668 case MATCHCRITERIA_AGE_GREATER:
1669 case MATCHCRITERIA_AGE_LOWER:
1670 case MATCHCRITERIA_SCORE_GREATER:
1671 case MATCHCRITERIA_SCORE_LOWER:
1672 case MATCHCRITERIA_SCORE_EQUAL:
1673 case MATCHCRITERIA_SIZE_GREATER:
1674 case MATCHCRITERIA_SIZE_SMALLER:
1675 case MATCHCRITERIA_SIZE_EQUAL:
1676 case MATCHCRITERIA_COLORLABEL:
1677 case MATCHCRITERIA_NOT_COLORLABEL:
1678 return g_strdup_printf("%s %i", criteria_str, matcher->value);
1679 case MATCHCRITERIA_ALL:
1680 case MATCHCRITERIA_UNREAD:
1681 case MATCHCRITERIA_NOT_UNREAD:
1682 case MATCHCRITERIA_NEW:
1683 case MATCHCRITERIA_NOT_NEW:
1684 case MATCHCRITERIA_MARKED:
1685 case MATCHCRITERIA_NOT_MARKED:
1686 case MATCHCRITERIA_DELETED:
1687 case MATCHCRITERIA_NOT_DELETED:
1688 case MATCHCRITERIA_REPLIED:
1689 case MATCHCRITERIA_NOT_REPLIED:
1690 case MATCHCRITERIA_FORWARDED:
1691 case MATCHCRITERIA_NOT_FORWARDED:
1692 case MATCHCRITERIA_LOCKED:
1693 case MATCHCRITERIA_NOT_LOCKED:
1694 case MATCHCRITERIA_SPAM:
1695 case MATCHCRITERIA_NOT_SPAM:
1696 case MATCHCRITERIA_PARTIAL:
1697 case MATCHCRITERIA_NOT_PARTIAL:
1698 case MATCHCRITERIA_IGNORE_THREAD:
1699 case MATCHCRITERIA_NOT_IGNORE_THREAD:
1700 return g_strdup(criteria_str);
1701 case MATCHCRITERIA_TEST:
1702 case MATCHCRITERIA_NOT_TEST:
1703 quoted_expr = matcher_quote_str(matcher->expr);
1704 matcher_str = g_strdup_printf("%s \"%s\"",
1705 criteria_str, quoted_expr);
1706 g_free(quoted_expr);
1708 case MATCHCRITERIA_FOUND_IN_ADDRESSBOOK:
1709 case MATCHCRITERIA_NOT_FOUND_IN_ADDRESSBOOK:
1710 quoted_header = matcher_quote_str(matcher->header);
1711 quoted_expr = matcher_quote_str(matcher->expr);
1712 matcher_str = g_strdup_printf("%s \"%s\" in \"%s\"",
1713 criteria_str, quoted_header, quoted_expr);
1714 g_free(quoted_header);
1715 g_free(quoted_expr);
1719 matchtype_str = NULL;
1720 for (i = 0; i < sizeof matchparser_tab / sizeof matchparser_tab[0]; i++) {
1721 if (matchparser_tab[i].id == matcher->matchtype)
1722 matchtype_str = matchparser_tab[i].str;
1725 if (matchtype_str == NULL)
1728 switch (matcher->matchtype) {
1729 case MATCHTYPE_MATCH:
1730 case MATCHTYPE_MATCHCASE:
1731 case MATCHTYPE_REGEXP:
1732 case MATCHTYPE_REGEXPCASE:
1733 quoted_expr = matcher_quote_str(matcher->expr);
1734 if (matcher->header) {
1735 quoted_header = matcher_quote_str(matcher->header);
1736 matcher_str = g_strdup_printf
1737 ("%s \"%s\" %s \"%s\"",
1738 criteria_str, quoted_header,
1739 matchtype_str, quoted_expr);
1740 g_free(quoted_header);
1743 matcher_str = g_strdup_printf
1744 ("%s %s \"%s\"", criteria_str,
1745 matchtype_str, quoted_expr);
1746 g_free(quoted_expr);
1754 *\brief Convert a list of conditions to a string
1756 *\param matchers List of conditions
1758 *\return gchar * Newly allocated string
1760 gchar *matcherlist_to_string(const MatcherList *matchers)
1766 gchar *result = NULL;
1768 count = g_slist_length(matchers->matchers);
1769 vstr = g_new(gchar *, count + 1);
1771 for (l = matchers->matchers, cur_str = vstr; l != NULL;
1772 l = g_slist_next(l), cur_str ++) {
1773 *cur_str = matcherprop_to_string((MatcherProp *) l->data);
1774 if (*cur_str == NULL)
1779 if (matchers->bool_and)
1780 result = g_strjoinv(" & ", vstr);
1782 result = g_strjoinv(" | ", vstr);
1784 for (cur_str = vstr ; *cur_str != NULL ; cur_str ++)
1792 #define STRLEN_ZERO(s) ((s) ? strlen(s) : 0)
1793 #define STRLEN_DEFAULT(s,d) ((s) ? strlen(s) : STRLEN_ZERO(d))
1795 static void add_str_default(gchar ** dest,
1796 const gchar * s, const gchar * d)
1798 gchar quoted_str[4096];
1806 quote_cmd_argument(quoted_str, sizeof(quoted_str), str);
1807 strcpy(* dest, quoted_str);
1809 (* dest) += strlen(* dest);
1812 /* matching_build_command() - preferably cmd should be unescaped */
1814 *\brief Build the command line to execute
1816 *\param cmd String with command line specifiers
1817 *\param info Message info to use for command
1819 *\return gchar * Newly allocated string
1821 gchar *matching_build_command(const gchar *cmd, MsgInfo *info)
1823 const gchar *s = cmd;
1824 gchar *filename = NULL;
1825 gchar *processed_cmd;
1829 const gchar *const no_subject = _("(none)") ;
1830 const gchar *const no_from = _("(none)") ;
1831 const gchar *const no_to = _("(none)") ;
1832 const gchar *const no_cc = _("(none)") ;
1833 const gchar *const no_date = _("(none)") ;
1834 const gchar *const no_msgid = _("(none)") ;
1835 const gchar *const no_newsgroups = _("(none)") ;
1836 const gchar *const no_references = _("(none)") ;
1838 size = STRLEN_ZERO(cmd) + 1;
1839 while (*s != '\0') {
1846 case 's': /* subject */
1847 size += STRLEN_DEFAULT(info->subject, no_subject) - 2;
1849 case 'f': /* from */
1850 size += STRLEN_DEFAULT(info->from, no_from) - 2;
1853 size += STRLEN_DEFAULT(info->to, no_to) - 2;
1856 size += STRLEN_DEFAULT(info->cc, no_cc) - 2;
1858 case 'd': /* date */
1859 size += STRLEN_DEFAULT(info->date, no_date) - 2;
1861 case 'i': /* message-id */
1862 size += STRLEN_DEFAULT(info->msgid, no_msgid) - 2;
1864 case 'n': /* newsgroups */
1865 size += STRLEN_DEFAULT(info->newsgroups, no_newsgroups) - 2;
1867 case 'r': /* references */
1868 /* FIXME: using the inreplyto header for reference */
1869 size += STRLEN_DEFAULT(info->inreplyto, no_references) - 2;
1871 case 'F': /* file */
1872 if (filename == NULL)
1873 filename = folder_item_fetch_msg(info->folder, info->msgnum);
1875 if (filename == NULL) {
1876 g_warning("filename is not set");
1880 size += strlen(filename) - 2;
1889 /* as the string can be quoted, we double the result */
1892 processed_cmd = g_new0(gchar, size);
1896 while (*s != '\0') {
1904 case 's': /* subject */
1905 add_str_default(&p, info->subject,
1908 case 'f': /* from */
1909 add_str_default(&p, info->from,
1913 add_str_default(&p, info->to,
1917 add_str_default(&p, info->cc,
1920 case 'd': /* date */
1921 add_str_default(&p, info->date,
1924 case 'i': /* message-id */
1925 add_str_default(&p, info->msgid,
1928 case 'n': /* newsgroups */
1929 add_str_default(&p, info->newsgroups,
1932 case 'r': /* references */
1933 /* FIXME: using the inreplyto header for references */
1934 add_str_default(&p, info->inreplyto, no_references);
1936 case 'F': /* file */
1937 if (filename != NULL)
1938 add_str_default(&p, filename, NULL);
1957 return processed_cmd;
1959 #undef STRLEN_DEFAULT
1962 /* ************************************************************ */
1966 *\brief Write filtering list to file
1969 *\param prefs_filtering List of filtering conditions
1971 static void prefs_filtering_write(FILE *fp, GSList *prefs_filtering)
1975 for (cur = prefs_filtering; cur != NULL; cur = cur->next) {
1976 gchar *filtering_str = NULL;
1977 gchar *tmp_name = NULL;
1978 FilteringProp *prop = NULL;
1980 if (NULL == (prop = (FilteringProp *) cur->data))
1983 if (NULL == (filtering_str = filteringprop_to_string(prop)))
1986 if (prop->enabled) {
1987 if (fputs("enabled ", fp) == EOF) {
1988 FILE_OP_ERROR("filtering config", "fputs");
1992 if (fputs("disabled ", fp) == EOF) {
1993 FILE_OP_ERROR("filtering config", "fputs");
1998 if (fputs("rulename \"", fp) == EOF) {
1999 FILE_OP_ERROR("filtering config", "fputs");
2000 g_free(filtering_str);
2003 tmp_name = prop->name;
2004 while (tmp_name && *tmp_name != '\0') {
2005 if (*tmp_name != '"') {
2006 if (fputc(*tmp_name, fp) == EOF) {
2007 FILE_OP_ERROR("filtering config", "fputs || fputc");
2008 g_free(filtering_str);
2011 } else if (*tmp_name == '"') {
2012 if (fputc('\\', fp) == EOF ||
2013 fputc('"', fp) == EOF) {
2014 FILE_OP_ERROR("filtering config", "fputs || fputc");
2015 g_free(filtering_str);
2021 if (fputs("\" ", fp) == EOF) {
2022 FILE_OP_ERROR("filtering config", "fputs");
2023 g_free(filtering_str);
2027 if (prop->account_id != 0) {
2030 tmp = g_strdup_printf("account %d ", prop->account_id);
2031 if (fputs(tmp, fp) == EOF) {
2032 FILE_OP_ERROR("filtering config", "fputs");
2039 if(fputs(filtering_str, fp) == EOF ||
2040 fputc('\n', fp) == EOF) {
2041 FILE_OP_ERROR("filtering config", "fputs || fputc");
2042 g_free(filtering_str);
2045 g_free(filtering_str);
2050 *\brief Write matchers from a folder item
2052 *\param node Node with folder info
2053 *\param data File pointer
2055 *\return gboolean FALSE
2057 static gboolean prefs_matcher_write_func(GNode *node, gpointer data)
2062 GSList *prefs_filtering;
2065 /* prevent warning */
2066 if (item->path == NULL)
2068 id = folder_item_get_identifier(item);
2071 prefs_filtering = item->prefs->processing;
2073 if (prefs_filtering != NULL) {
2074 fprintf(fp, "[%s]\n", id);
2075 prefs_filtering_write(fp, prefs_filtering);
2085 *\brief Save matchers from folder items
2089 static void prefs_matcher_save(FILE *fp)
2093 for (cur = folder_get_list() ; cur != NULL ; cur = g_list_next(cur)) {
2096 folder = (Folder *) cur->data;
2097 g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
2098 prefs_matcher_write_func, fp);
2101 /* pre global rules */
2102 fprintf(fp, "[preglobal]\n");
2103 prefs_filtering_write(fp, pre_global_processing);
2106 /* post global rules */
2107 fprintf(fp, "[postglobal]\n");
2108 prefs_filtering_write(fp, post_global_processing);
2111 /* filtering rules */
2112 fprintf(fp, "[filtering]\n");
2113 prefs_filtering_write(fp, filtering_rules);
2118 *\brief Write filtering / matcher configuration file
2120 void prefs_matcher_write_config(void)
2125 debug_print("Writing matcher configuration...\n");
2127 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
2130 if ((pfile = prefs_write_open(rcpath)) == NULL) {
2131 g_warning("failed to write configuration to file\n");
2137 prefs_matcher_save(pfile->fp);
2141 if (prefs_file_close(pfile) < 0) {
2142 g_warning("failed to write configuration to file\n");
2147 /* ******************************************************************* */
2149 static void matcher_add_rulenames(const gchar *rcpath)
2151 gchar *newpath = g_strconcat(rcpath, ".new", NULL);
2152 FILE *src = g_fopen(rcpath, "rb");
2153 FILE *dst = g_fopen(newpath, "wb");
2154 gchar buf[BUFFSIZE];
2162 while (fgets (buf, sizeof(buf), src) != NULL) {
2163 if (strlen(buf) > 2 && buf[0] != '['
2164 && strncmp(buf, "rulename \"", 10)
2165 && strncmp(buf, "enabled rulename \"", 18)
2166 && strncmp(buf, "disabled rulename \"", 18)) {
2167 fwrite("enabled rulename \"\" ",
2168 strlen("enabled rulename \"\" "), 1, dst);
2170 fwrite(buf, strlen(buf), 1, dst);
2174 move_file(newpath, rcpath, TRUE);
2179 *\brief Read matcher configuration
2181 void prefs_matcher_read_config(void)
2184 gchar *rc_old_format;
2187 create_matchparser_hashtab();
2188 prefs_filtering_clear();
2190 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MATCHER_RC, NULL);
2191 rc_old_format = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MATCHER_RC,
2192 ".pre_names", NULL);
2194 if (!is_file_exist(rc_old_format) && is_file_exist(rcpath)) {
2195 /* backup file with no rules names, in case
2196 * anything goes wrong */
2197 copy_file(rcpath, rc_old_format, FALSE);
2198 /* now hack the file in order to have it to the new format */
2199 matcher_add_rulenames(rcpath);
2202 g_free(rc_old_format);
2204 f = g_fopen(rcpath, "rb");
2208 matcher_parser_start_parsing(f);
2209 fclose(matcher_parserin);
2212 /* previous version compatibility */
2214 /* printf("reading filtering\n"); */
2215 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
2216 FILTERING_RC, NULL);
2217 f = g_fopen(rcpath, "rb");
2221 matcher_parser_start_parsing(f);
2222 fclose(matcher_parserin);
2225 /* printf("reading scoring\n"); */
2226 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
2228 f = g_fopen(rcpath, "rb");
2232 matcher_parser_start_parsing(f);
2233 fclose(matcher_parserin);