2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 2002-2004 by the Sylpheed Claws 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"
47 *\brief Keyword lookup element
50 gint id; /*!< keyword id */
51 gchar *str; /*!< keyword */
53 typedef struct _MatchParser MatchParser;
56 *\brief Table with strings and ids used by the lexer and
57 * the parser. New keywords can be added here.
59 static const MatchParser matchparser_tab[] = {
61 {MATCHCRITERIA_ALL, "all"},
62 {MATCHCRITERIA_UNREAD, "unread"},
63 {MATCHCRITERIA_NOT_UNREAD, "~unread"},
64 {MATCHCRITERIA_NEW, "new"},
65 {MATCHCRITERIA_NOT_NEW, "~new"},
66 {MATCHCRITERIA_MARKED, "marked"},
67 {MATCHCRITERIA_NOT_MARKED, "~marked"},
68 {MATCHCRITERIA_DELETED, "deleted"},
69 {MATCHCRITERIA_NOT_DELETED, "~deleted"},
70 {MATCHCRITERIA_REPLIED, "replied"},
71 {MATCHCRITERIA_NOT_REPLIED, "~replied"},
72 {MATCHCRITERIA_FORWARDED, "forwarded"},
73 {MATCHCRITERIA_NOT_FORWARDED, "~forwarded"},
74 {MATCHCRITERIA_LOCKED, "locked"},
75 {MATCHCRITERIA_NOT_LOCKED, "~locked"},
76 {MATCHCRITERIA_COLORLABEL, "colorlabel"},
77 {MATCHCRITERIA_NOT_COLORLABEL, "~colorlabel"},
78 {MATCHCRITERIA_IGNORE_THREAD, "ignore_thread"},
79 {MATCHCRITERIA_NOT_IGNORE_THREAD, "~ignore_thread"},
82 {MATCHCRITERIA_SUBJECT, "subject"},
83 {MATCHCRITERIA_NOT_SUBJECT, "~subject"},
84 {MATCHCRITERIA_FROM, "from"},
85 {MATCHCRITERIA_NOT_FROM, "~from"},
86 {MATCHCRITERIA_TO, "to"},
87 {MATCHCRITERIA_NOT_TO, "~to"},
88 {MATCHCRITERIA_CC, "cc"},
89 {MATCHCRITERIA_NOT_CC, "~cc"},
90 {MATCHCRITERIA_TO_OR_CC, "to_or_cc"},
91 {MATCHCRITERIA_NOT_TO_AND_NOT_CC, "~to_or_cc"},
92 {MATCHCRITERIA_AGE_GREATER, "age_greater"},
93 {MATCHCRITERIA_AGE_LOWER, "age_lower"},
94 {MATCHCRITERIA_NEWSGROUPS, "newsgroups"},
95 {MATCHCRITERIA_NOT_NEWSGROUPS, "~newsgroups"},
96 {MATCHCRITERIA_INREPLYTO, "inreplyto"},
97 {MATCHCRITERIA_NOT_INREPLYTO, "~inreplyto"},
98 {MATCHCRITERIA_REFERENCES, "references"},
99 {MATCHCRITERIA_NOT_REFERENCES, "~references"},
100 {MATCHCRITERIA_SCORE_GREATER, "score_greater"},
101 {MATCHCRITERIA_SCORE_LOWER, "score_lower"},
102 {MATCHCRITERIA_SCORE_EQUAL, "score_equal"},
103 {MATCHCRITERIA_PARTIAL, "partial"},
104 {MATCHCRITERIA_NOT_PARTIAL, "~partial"},
106 {MATCHCRITERIA_SIZE_GREATER, "size_greater"},
107 {MATCHCRITERIA_SIZE_SMALLER, "size_smaller"},
108 {MATCHCRITERIA_SIZE_EQUAL, "size_equal"},
110 /* content have to be read */
111 {MATCHCRITERIA_HEADER, "header"},
112 {MATCHCRITERIA_NOT_HEADER, "~header"},
113 {MATCHCRITERIA_HEADERS_PART, "headers_part"},
114 {MATCHCRITERIA_NOT_HEADERS_PART, "~headers_part"},
115 {MATCHCRITERIA_MESSAGE, "message"},
116 {MATCHCRITERIA_NOT_MESSAGE, "~message"},
117 {MATCHCRITERIA_BODY_PART, "body_part"},
118 {MATCHCRITERIA_NOT_BODY_PART, "~body_part"},
119 {MATCHCRITERIA_TEST, "test"},
120 {MATCHCRITERIA_NOT_TEST, "~test"},
123 {MATCHTYPE_MATCHCASE, "matchcase"},
124 {MATCHTYPE_MATCH, "match"},
125 {MATCHTYPE_REGEXPCASE, "regexpcase"},
126 {MATCHTYPE_REGEXP, "regexp"},
127 {MATCHTYPE_ANY_IN_ADDRESSBOOK, "any_in_addressbook"},
128 {MATCHTYPE_ALL_IN_ADDRESSBOOK, "all_in_addressbook"},
131 {MATCHACTION_SCORE, "score"}, /* for backward compatibility */
132 {MATCHACTION_MOVE, "move"},
133 {MATCHACTION_COPY, "copy"},
134 {MATCHACTION_DELETE, "delete"},
135 {MATCHACTION_MARK, "mark"},
136 {MATCHACTION_UNMARK, "unmark"},
137 {MATCHACTION_LOCK, "lock"},
138 {MATCHACTION_UNLOCK, "unlock"},
139 {MATCHACTION_MARK_AS_READ, "mark_as_read"},
140 {MATCHACTION_MARK_AS_UNREAD, "mark_as_unread"},
141 {MATCHACTION_FORWARD, "forward"},
142 {MATCHACTION_FORWARD_AS_ATTACHMENT, "forward_as_attachment"},
143 {MATCHACTION_EXECUTE, "execute"},
144 {MATCHACTION_COLOR, "color"},
145 {MATCHACTION_REDIRECT, "redirect"},
146 {MATCHACTION_CHANGE_SCORE, "change_score"},
147 {MATCHACTION_SET_SCORE, "set_score"},
148 {MATCHACTION_STOP, "stop"},
149 {MATCHACTION_HIDE, "hide"},
150 {MATCHACTION_IGNORE, "ignore"},
154 *\brief Look up table with keywords defined in \sa matchparser_tab
156 static GHashTable *matchparser_hashtab;
159 *\brief Translate keyword id to keyword string
161 *\param id Id of keyword
163 *\return const gchar * Keyword
165 const gchar *get_matchparser_tab_str(gint id)
169 for (i = 0; i < sizeof matchparser_tab / sizeof matchparser_tab[0]; i++) {
170 if (matchparser_tab[i].id == id)
171 return matchparser_tab[i].str;
177 *\brief Create keyword lookup table
179 static void create_matchparser_hashtab(void)
183 if (matchparser_hashtab) return;
184 matchparser_hashtab = g_hash_table_new(g_str_hash, g_str_equal);
185 for (i = 0; i < sizeof matchparser_tab / sizeof matchparser_tab[0]; i++)
186 g_hash_table_insert(matchparser_hashtab,
187 matchparser_tab[i].str,
188 (gpointer) &matchparser_tab[i]);
192 *\brief Return a keyword id from a keyword string
194 *\param str Keyword string
196 *\return gint Keyword id
198 gint get_matchparser_tab_id(const gchar *str)
202 if (NULL != (res = g_hash_table_lookup(matchparser_hashtab, str))) {
208 /* **************** data structure allocation **************** */
211 *\brief Allocate a structure for a filtering / scoring
212 * "condition" (a matcher structure)
214 *\param criteria Criteria ID (MATCHCRITERIA_XXXX)
215 *\param header Header string (if criteria is MATCHCRITERIA_HEADER)
216 *\param matchtype Type of action (MATCHTYPE_XXX)
217 *\param expr String value or expression to check
218 *\param value Integer value to check
220 *\return MatcherProp * Pointer to newly allocated structure
222 MatcherProp *matcherprop_new(gint criteria, const gchar *header,
223 gint matchtype, const gchar *expr,
228 prop = g_new0(MatcherProp, 1);
229 prop->criteria = criteria;
230 prop->header = header != NULL ? g_strdup(header) : NULL;
231 prop->expr = expr != NULL ? g_strdup(expr) : NULL;
232 prop->matchtype = matchtype;
241 *\brief Free a matcher structure
243 *\param prop Pointer to matcher structure allocated with
246 void matcherprop_free(MatcherProp *prop)
249 g_free(prop->header);
250 if (prop->preg != NULL) {
258 *\brief Copy a matcher structure
260 *\param src Matcher structure to copy
262 *\return MatcherProp * Pointer to newly allocated matcher structure
264 MatcherProp *matcherprop_copy(const MatcherProp *src)
266 MatcherProp *prop = g_new0(MatcherProp, 1);
268 prop->criteria = src->criteria;
269 prop->header = src->header ? g_strdup(src->header) : NULL;
270 prop->expr = src->expr ? g_strdup(src->expr) : NULL;
271 prop->matchtype = src->matchtype;
273 prop->preg = NULL; /* will be re-evaluated */
274 prop->value = src->value;
275 prop->error = src->error;
279 /* ************** match ******************************/
281 static gboolean match_with_addresses_in_addressbook
282 (MatcherProp *prop, const gchar *str, gint type)
284 GSList *address_list = NULL;
286 gboolean res = FALSE;
288 if (str == NULL || *str == 0)
291 /* XXX: perhaps complete with comments too */
292 address_list = address_list_append(address_list, str);
296 start_address_completion();
298 for (walk = address_list; walk != NULL; walk = walk->next) {
299 gboolean found = complete_address(walk->data) ? TRUE : FALSE;
302 if (!found && type == MATCHTYPE_ALL_IN_ADDRESSBOOK) {
309 g_slist_free(address_list);
311 end_address_completion();
317 *\brief Find out if a string matches a condition
319 *\param prop Matcher structure
320 *\param str String to check
322 *\return gboolean TRUE if str matches the condition in the
325 static gboolean matcherprop_string_match(MatcherProp *prop, const gchar *str)
333 switch (prop->matchtype) {
334 case MATCHTYPE_REGEXPCASE:
335 case MATCHTYPE_REGEXP:
336 if (!prop->preg && (prop->error == 0)) {
337 prop->preg = g_new0(regex_t, 1);
338 /* if regexp then don't use the escaped string */
339 if (regcomp(prop->preg, prop->expr,
340 REG_NOSUB | REG_EXTENDED
341 | ((prop->matchtype == MATCHTYPE_REGEXPCASE)
342 ? REG_ICASE : 0)) != 0) {
348 if (prop->preg == NULL)
351 if (regexec(prop->preg, str, 0, NULL, 0) == 0)
356 case MATCHTYPE_ALL_IN_ADDRESSBOOK:
357 case MATCHTYPE_ANY_IN_ADDRESSBOOK:
358 return match_with_addresses_in_addressbook
359 (prop, str, prop->matchtype);
361 case MATCHTYPE_MATCH:
362 return (strstr(str, prop->expr) != NULL);
364 /* FIXME: put upper in unesc_str */
365 case MATCHTYPE_MATCHCASE:
366 str2 = alloca(strlen(prop->expr) + 1);
367 strcpy(str2, prop->expr);
369 str1 = alloca(strlen(str) + 1);
372 return (strstr(str1, str2) != NULL);
379 /* FIXME body search is a hack. */
380 static gboolean matcherprop_string_decode_match(MatcherProp *prop, const gchar *str)
384 gboolean res = FALSE;
389 /* we try to decode QP first, because it's faster than base64 */
390 qp_decode_const(tmp, BUFFSIZE-1, str);
391 if (!g_utf8_validate(tmp, -1, NULL)) {
392 utf = conv_codeset_strdup
393 (tmp, conv_get_locale_charset_str_no_utf8(),
395 res = matcherprop_string_match(prop, utf);
398 res = matcherprop_string_match(prop, tmp);
401 if (res == FALSE && (strchr(prop->expr, '=') || strchr(prop->expr, '_')) ) {
402 /* if searching for something with an equal char, maybe
403 * we should try to match the non-decoded string.
404 * In case it was not qp-encoded. */
405 if (!g_utf8_validate(str, -1, NULL)) {
406 utf = conv_codeset_strdup
407 (str, conv_get_locale_charset_str_no_utf8(),
409 res = matcherprop_string_match(prop, utf);
411 res = matcherprop_string_match(prop, str);
415 /* FIXME base64 decoding is too slow, especially since text can
416 * easily be handled as base64. Don't even try now. */
422 typedef struct _thread_data {
429 void *matcher_test_thread(void *data)
431 thread_data *td = (thread_data *)data;
434 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
435 pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
437 result = system(td->cmd);
438 if (result) perror("system");
439 td->done = TRUE; /* let the caller thread join() */
440 return GINT_TO_POINTER(result);
445 *\brief Execute a command defined in the matcher structure
447 *\param prop Pointer to matcher structure
448 *\param info Pointer to message info structure
450 *\return gboolean TRUE if command was executed succesfully
452 static gboolean matcherprop_match_test(const MatcherProp *prop,
460 thread_data *td = g_new0(thread_data, 1);
462 time_t start_time = time(NULL);
465 file = procmsg_get_message_file(info);
470 cmd = matching_build_command(prop->expr, info);
474 #if (defined USE_PTHREAD && defined __GLIBC__ && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 3)))
477 if (pthread_create(&pt, PTHREAD_CREATE_JOINABLE,
478 matcher_test_thread, td) != 0)
479 retval = system(cmd);
481 printf("waiting for test thread\n");
483 /* don't let the interface freeze while waiting */
485 if (time(NULL) - start_time > 30) {
491 pthread_join(pt, &res);
492 retval = GPOINTER_TO_INT(res);
493 printf(" test thread returned %d\n", retval);
497 retval = system(cmd);
499 debug_print("Command exit code: %d\n", retval);
502 return (retval == 0);
506 *\brief Check if a message matches the condition in a matcher
509 *\param prop Pointer to matcher structure
510 *\param info Pointer to message info
512 *\return gboolean TRUE if a match
514 gboolean matcherprop_match(MatcherProp *prop,
519 switch(prop->criteria) {
520 case MATCHCRITERIA_ALL:
522 case MATCHCRITERIA_UNREAD:
523 return MSG_IS_UNREAD(info->flags);
524 case MATCHCRITERIA_NOT_UNREAD:
525 return !MSG_IS_UNREAD(info->flags);
526 case MATCHCRITERIA_NEW:
527 return MSG_IS_NEW(info->flags);
528 case MATCHCRITERIA_NOT_NEW:
529 return !MSG_IS_NEW(info->flags);
530 case MATCHCRITERIA_MARKED:
531 return MSG_IS_MARKED(info->flags);
532 case MATCHCRITERIA_NOT_MARKED:
533 return !MSG_IS_MARKED(info->flags);
534 case MATCHCRITERIA_DELETED:
535 return MSG_IS_DELETED(info->flags);
536 case MATCHCRITERIA_NOT_DELETED:
537 return !MSG_IS_DELETED(info->flags);
538 case MATCHCRITERIA_REPLIED:
539 return MSG_IS_REPLIED(info->flags);
540 case MATCHCRITERIA_NOT_REPLIED:
541 return !MSG_IS_REPLIED(info->flags);
542 case MATCHCRITERIA_FORWARDED:
543 return MSG_IS_FORWARDED(info->flags);
544 case MATCHCRITERIA_NOT_FORWARDED:
545 return !MSG_IS_FORWARDED(info->flags);
546 case MATCHCRITERIA_LOCKED:
547 return MSG_IS_LOCKED(info->flags);
548 case MATCHCRITERIA_NOT_LOCKED:
549 return !MSG_IS_LOCKED(info->flags);
550 case MATCHCRITERIA_COLORLABEL:
551 return MSG_GET_COLORLABEL_VALUE(info->flags) == prop->value;
552 case MATCHCRITERIA_NOT_COLORLABEL:
553 return MSG_GET_COLORLABEL_VALUE(info->flags) != prop->value;
554 case MATCHCRITERIA_IGNORE_THREAD:
555 return MSG_IS_IGNORE_THREAD(info->flags);
556 case MATCHCRITERIA_NOT_IGNORE_THREAD:
557 return !MSG_IS_IGNORE_THREAD(info->flags);
558 case MATCHCRITERIA_SUBJECT:
559 return matcherprop_string_match(prop, info->subject);
560 case MATCHCRITERIA_NOT_SUBJECT:
561 return !matcherprop_string_match(prop, info->subject);
562 case MATCHCRITERIA_FROM:
563 return matcherprop_string_match(prop, info->from);
564 case MATCHCRITERIA_NOT_FROM:
565 return !matcherprop_string_match(prop, info->from);
566 case MATCHCRITERIA_TO:
567 return matcherprop_string_match(prop, info->to);
568 case MATCHCRITERIA_NOT_TO:
569 return !matcherprop_string_match(prop, info->to);
570 case MATCHCRITERIA_CC:
571 return matcherprop_string_match(prop, info->cc);
572 case MATCHCRITERIA_NOT_CC:
573 return !matcherprop_string_match(prop, info->cc);
574 case MATCHCRITERIA_TO_OR_CC:
575 return matcherprop_string_match(prop, info->to)
576 || matcherprop_string_match(prop, info->cc);
577 case MATCHCRITERIA_NOT_TO_AND_NOT_CC:
578 return !(matcherprop_string_match(prop, info->to)
579 || matcherprop_string_match(prop, info->cc));
580 case MATCHCRITERIA_AGE_GREATER:
582 return ((t - info->date_t) / (60 * 60 * 24)) > prop->value;
583 case MATCHCRITERIA_AGE_LOWER:
585 return ((t - info->date_t) / (60 * 60 * 24)) < prop->value;
586 case MATCHCRITERIA_SCORE_GREATER:
587 return info->score > prop->value;
588 case MATCHCRITERIA_SCORE_LOWER:
589 return info->score < prop->value;
590 case MATCHCRITERIA_SCORE_EQUAL:
591 return info->score == prop->value;
592 case MATCHCRITERIA_SIZE_GREATER:
593 /* FIXME: info->size is an off_t */
594 return info->size > (off_t) prop->value;
595 case MATCHCRITERIA_SIZE_EQUAL:
596 /* FIXME: info->size is an off_t */
597 return info->size == (off_t) prop->value;
598 case MATCHCRITERIA_SIZE_SMALLER:
599 /* FIXME: info->size is an off_t */
600 return info->size < (off_t) prop->value;
601 case MATCHCRITERIA_PARTIAL:
602 /* FIXME: info->size is an off_t */
603 return (info->total_size != 0 && info->size != (off_t)info->total_size);
604 case MATCHCRITERIA_NOT_PARTIAL:
605 /* FIXME: info->size is an off_t */
606 return (info->total_size == 0 || info->size == (off_t)info->total_size);
607 case MATCHCRITERIA_NEWSGROUPS:
608 return matcherprop_string_match(prop, info->newsgroups);
609 case MATCHCRITERIA_NOT_NEWSGROUPS:
610 return !matcherprop_string_match(prop, info->newsgroups);
611 case MATCHCRITERIA_INREPLYTO:
612 return matcherprop_string_match(prop, info->inreplyto);
613 case MATCHCRITERIA_NOT_INREPLYTO:
614 return !matcherprop_string_match(prop, info->inreplyto);
615 /* FIXME: Using inreplyto, but matching the (newly implemented)
616 * list of references is better */
617 case MATCHCRITERIA_REFERENCES:
618 return matcherprop_string_match(prop, info->inreplyto);
619 case MATCHCRITERIA_NOT_REFERENCES:
620 return !matcherprop_string_match(prop, info->inreplyto);
621 case MATCHCRITERIA_TEST:
622 return matcherprop_match_test(prop, info);
623 case MATCHCRITERIA_NOT_TEST:
624 return !matcherprop_match_test(prop, info);
630 /* ********************* MatcherList *************************** */
633 *\brief Create a new list of matchers
635 *\param matchers List of matcher structures
636 *\param bool_and Operator
638 *\return MatcherList * New list
640 MatcherList *matcherlist_new(GSList *matchers, gboolean bool_and)
644 cond = g_new0(MatcherList, 1);
646 cond->matchers = matchers;
647 cond->bool_and = bool_and;
653 *\brief Frees a list of matchers
655 *\param cond List of matchers
657 void matcherlist_free(MatcherList *cond)
661 g_return_if_fail(cond);
662 for (l = cond->matchers ; l != NULL ; l = g_slist_next(l)) {
663 matcherprop_free((MatcherProp *) l->data);
669 *\brief Skip all headers in a message file
671 *\param fp Message file
673 static void matcherlist_skip_headers(FILE *fp)
677 while (procheader_get_one_field(buf, sizeof(buf), fp, NULL) != -1)
682 *\brief Check if a header matches a matcher condition
684 *\param matcher Matcher structure to check header for
685 *\param buf Header name
687 *\return boolean TRUE if matching header
689 static gboolean matcherprop_match_one_header(MatcherProp *matcher,
695 switch (matcher->criteria) {
696 case MATCHCRITERIA_HEADER:
697 case MATCHCRITERIA_NOT_HEADER:
698 header = procheader_parse_header(buf);
701 if (procheader_headername_equal(header->name,
703 if (matcher->criteria == MATCHCRITERIA_HEADER)
704 result = matcherprop_string_match(matcher, header->body);
706 result = !matcherprop_string_match(matcher, header->body);
707 procheader_header_free(header);
711 procheader_header_free(header);
714 case MATCHCRITERIA_HEADERS_PART:
715 return matcherprop_string_match(matcher, buf);
716 case MATCHCRITERIA_MESSAGE:
717 return matcherprop_string_decode_match(matcher, buf);
718 case MATCHCRITERIA_NOT_MESSAGE:
719 return !matcherprop_string_decode_match(matcher, buf);
720 case MATCHCRITERIA_NOT_HEADERS_PART:
721 return !matcherprop_string_match(matcher, buf);
727 *\brief Check if the matcher structure wants headers to
730 *\param matcher Matcher structure
732 *\return gboolean TRUE if the matcher structure describes
733 * a header match condition
735 static gboolean matcherprop_criteria_headers(const MatcherProp *matcher)
737 switch (matcher->criteria) {
738 case MATCHCRITERIA_HEADER:
739 case MATCHCRITERIA_NOT_HEADER:
740 case MATCHCRITERIA_HEADERS_PART:
741 case MATCHCRITERIA_NOT_HEADERS_PART:
749 *\brief Check if the matcher structure wants the message
750 * to be matched (just perform an action on any
753 *\param matcher Matcher structure
755 *\return gboolean TRUE if matcher condition should match
758 static gboolean matcherprop_criteria_message(MatcherProp *matcher)
760 switch (matcher->criteria) {
761 case MATCHCRITERIA_MESSAGE:
762 case MATCHCRITERIA_NOT_MESSAGE:
770 *\brief Check if a list of conditions matches one header in
773 *\param matchers List of conditions
774 *\param fp Message file
776 *\return gboolean TRUE if one of the headers is matched by
777 * the list of conditions.
779 static gboolean matcherlist_match_headers(MatcherList *matchers, FILE *fp)
784 while (procheader_get_one_field(buf, sizeof(buf), fp, NULL) != -1) {
785 for (l = matchers->matchers ; l != NULL ; l = g_slist_next(l)) {
786 MatcherProp *matcher = (MatcherProp *) l->data;
791 /* if the criteria is ~headers_part or ~message, ZERO lines
792 * must NOT match for the rule to match. */
793 if (matcher->criteria == MATCHCRITERIA_NOT_HEADERS_PART ||
794 matcher->criteria == MATCHCRITERIA_NOT_MESSAGE) {
795 if (matcherprop_match_one_header(matcher, buf)) {
796 matcher->result = TRUE;
798 matcher->result = FALSE;
799 matcher->done = TRUE;
801 /* else, just one line matching is enough for the rule to match
803 } else if (matcherprop_criteria_headers(matcher) ||
804 matcherprop_criteria_message(matcher)){
805 if (matcherprop_match_one_header(matcher, buf)) {
806 matcher->result = TRUE;
807 matcher->done = TRUE;
811 /* if the rule matched and the matchers are OR, no need to
812 * check the others */
813 if (matcher->result && matcher->done) {
814 if (!matchers->bool_and)
823 *\brief Check if a matcher wants to check the message body
825 *\param matcher Matcher structure
827 *\return gboolean TRUE if body must be matched.
829 static gboolean matcherprop_criteria_body(const MatcherProp *matcher)
831 switch (matcher->criteria) {
832 case MATCHCRITERIA_BODY_PART:
833 case MATCHCRITERIA_NOT_BODY_PART:
841 *\brief Check if a (line) string matches the criteria
842 * described by a matcher structure
844 *\param matcher Matcher structure
847 *\return gboolean TRUE if string matches criteria
849 static gboolean matcherprop_match_line(MatcherProp *matcher, const gchar *line)
851 switch (matcher->criteria) {
852 case MATCHCRITERIA_BODY_PART:
853 case MATCHCRITERIA_MESSAGE:
854 return matcherprop_string_decode_match(matcher, line);
855 case MATCHCRITERIA_NOT_BODY_PART:
856 case MATCHCRITERIA_NOT_MESSAGE:
857 return !matcherprop_string_decode_match(matcher, line);
863 *\brief Check if a line in a message file's body matches
866 *\param matchers List of conditions
867 *\param fp Message file
869 *\return gboolean TRUE if succesful match
871 static gboolean matcherlist_match_body(MatcherList *matchers, FILE *fp)
876 while (fgets(buf, sizeof(buf), fp) != NULL) {
877 for (l = matchers->matchers ; l != NULL ; l = g_slist_next(l)) {
878 MatcherProp *matcher = (MatcherProp *) l->data;
883 /* if the criteria is ~body_part or ~message, ZERO lines
884 * must NOT match for the rule to match. */
885 if (matcher->criteria == MATCHCRITERIA_NOT_BODY_PART ||
886 matcher->criteria == MATCHCRITERIA_NOT_MESSAGE) {
887 if (matcherprop_match_line(matcher, buf)) {
888 matcher->result = TRUE;
890 matcher->result = FALSE;
891 matcher->done = TRUE;
893 /* else, just one line has to match */
894 } else if (matcherprop_criteria_body(matcher) ||
895 matcherprop_criteria_message(matcher)) {
896 if (matcherprop_match_line(matcher, buf)) {
897 matcher->result = TRUE;
898 matcher->done = TRUE;
902 /* if the matchers are OR'ed and the rule matched,
903 * no need to check the others. */
904 if (matcher->result && matcher->done) {
905 if (!matchers->bool_and)
914 *\brief Check if a message file matches criteria
916 *\param matchers Criteria
917 *\param info Message info
918 *\param result Default result
920 *\return gboolean TRUE if matched
922 gboolean matcherlist_match_file(MatcherList *matchers, MsgInfo *info,
925 gboolean read_headers;
931 /* file need to be read ? */
933 read_headers = FALSE;
935 for (l = matchers->matchers ; l != NULL ; l = g_slist_next(l)) {
936 MatcherProp *matcher = (MatcherProp *) l->data;
938 if (matcherprop_criteria_headers(matcher))
940 if (matcherprop_criteria_body(matcher))
942 if (matcherprop_criteria_message(matcher)) {
946 matcher->result = FALSE;
947 matcher->done = FALSE;
950 if (!read_headers && !read_body)
953 file = procmsg_get_message_file_full(info, read_headers, read_body);
957 if ((fp = g_fopen(file, "rb")) == NULL) {
958 FILE_OP_ERROR(file, "fopen");
963 /* read the headers */
966 if (matcherlist_match_headers(matchers, fp))
969 matcherlist_skip_headers(fp);
974 matcherlist_match_body(matchers, fp);
977 for (l = matchers->matchers; l != NULL; l = g_slist_next(l)) {
978 MatcherProp *matcher = (MatcherProp *) l->data;
980 if (matcherprop_criteria_headers(matcher) ||
981 matcherprop_criteria_body(matcher) ||
982 matcherprop_criteria_message(matcher)) {
983 if (matcher->result) {
984 if (!matchers->bool_and) {
990 if (matchers->bool_and) {
1006 *\brief Test list of conditions on a message.
1008 *\param matchers List of conditions
1009 *\param info Message info
1011 *\return gboolean TRUE if matched
1013 gboolean matcherlist_match(MatcherList *matchers, MsgInfo *info)
1021 if (matchers->bool_and)
1026 /* test the cached elements */
1028 for (l = matchers->matchers; l != NULL ;l = g_slist_next(l)) {
1029 MatcherProp *matcher = (MatcherProp *) l->data;
1031 switch(matcher->criteria) {
1032 case MATCHCRITERIA_ALL:
1033 case MATCHCRITERIA_UNREAD:
1034 case MATCHCRITERIA_NOT_UNREAD:
1035 case MATCHCRITERIA_NEW:
1036 case MATCHCRITERIA_NOT_NEW:
1037 case MATCHCRITERIA_MARKED:
1038 case MATCHCRITERIA_NOT_MARKED:
1039 case MATCHCRITERIA_DELETED:
1040 case MATCHCRITERIA_NOT_DELETED:
1041 case MATCHCRITERIA_REPLIED:
1042 case MATCHCRITERIA_NOT_REPLIED:
1043 case MATCHCRITERIA_FORWARDED:
1044 case MATCHCRITERIA_NOT_FORWARDED:
1045 case MATCHCRITERIA_LOCKED:
1046 case MATCHCRITERIA_NOT_LOCKED:
1047 case MATCHCRITERIA_COLORLABEL:
1048 case MATCHCRITERIA_NOT_COLORLABEL:
1049 case MATCHCRITERIA_IGNORE_THREAD:
1050 case MATCHCRITERIA_NOT_IGNORE_THREAD:
1051 case MATCHCRITERIA_SUBJECT:
1052 case MATCHCRITERIA_NOT_SUBJECT:
1053 case MATCHCRITERIA_FROM:
1054 case MATCHCRITERIA_NOT_FROM:
1055 case MATCHCRITERIA_TO:
1056 case MATCHCRITERIA_NOT_TO:
1057 case MATCHCRITERIA_CC:
1058 case MATCHCRITERIA_NOT_CC:
1059 case MATCHCRITERIA_TO_OR_CC:
1060 case MATCHCRITERIA_NOT_TO_AND_NOT_CC:
1061 case MATCHCRITERIA_AGE_GREATER:
1062 case MATCHCRITERIA_AGE_LOWER:
1063 case MATCHCRITERIA_NEWSGROUPS:
1064 case MATCHCRITERIA_NOT_NEWSGROUPS:
1065 case MATCHCRITERIA_INREPLYTO:
1066 case MATCHCRITERIA_NOT_INREPLYTO:
1067 case MATCHCRITERIA_REFERENCES:
1068 case MATCHCRITERIA_NOT_REFERENCES:
1069 case MATCHCRITERIA_SCORE_GREATER:
1070 case MATCHCRITERIA_SCORE_LOWER:
1071 case MATCHCRITERIA_SCORE_EQUAL:
1072 case MATCHCRITERIA_SIZE_GREATER:
1073 case MATCHCRITERIA_SIZE_SMALLER:
1074 case MATCHCRITERIA_SIZE_EQUAL:
1075 case MATCHCRITERIA_TEST:
1076 case MATCHCRITERIA_NOT_TEST:
1077 case MATCHCRITERIA_PARTIAL:
1078 case MATCHCRITERIA_NOT_PARTIAL:
1079 if (matcherprop_match(matcher, info)) {
1080 if (!matchers->bool_and) {
1085 if (matchers->bool_and) {
1092 /* test the condition on the file */
1094 if (matcherlist_match_file(matchers, info, result)) {
1095 if (!matchers->bool_and)
1099 if (matchers->bool_and)
1107 static gint quote_filter_str(gchar * result, guint size,
1117 for(p = path ; * p != '\0' ; p ++) {
1119 if ((* p != '\"') && (* p != '\\')) {
1120 if (remaining > 0) {
1126 result[size - 1] = '\0';
1131 if (remaining >= 2) {
1139 result[size - 1] = '\0';
1144 if (remaining > 0) {
1148 result[size - 1] = '\0';
1156 gchar * matcher_quote_str(const gchar * src)
1161 len = strlen(src) * 2 + 1;
1162 res = g_malloc(len);
1163 quote_filter_str(res, len, src);
1169 *\brief Convert a matcher structure to a string
1171 *\param matcher Matcher structure
1173 *\return gchar * Newly allocated string
1175 gchar *matcherprop_to_string(MatcherProp *matcher)
1177 gchar *matcher_str = NULL;
1178 const gchar *criteria_str;
1179 const gchar *matchtype_str;
1181 gchar * quoted_expr;
1183 criteria_str = NULL;
1184 for (i = 0; i < (int) (sizeof(matchparser_tab) / sizeof(MatchParser)); i++) {
1185 if (matchparser_tab[i].id == matcher->criteria)
1186 criteria_str = matchparser_tab[i].str;
1188 if (criteria_str == NULL)
1191 switch (matcher->criteria) {
1192 case MATCHCRITERIA_AGE_GREATER:
1193 case MATCHCRITERIA_AGE_LOWER:
1194 case MATCHCRITERIA_SCORE_GREATER:
1195 case MATCHCRITERIA_SCORE_LOWER:
1196 case MATCHCRITERIA_SCORE_EQUAL:
1197 case MATCHCRITERIA_SIZE_GREATER:
1198 case MATCHCRITERIA_SIZE_SMALLER:
1199 case MATCHCRITERIA_SIZE_EQUAL:
1200 case MATCHCRITERIA_COLORLABEL:
1201 case MATCHCRITERIA_NOT_COLORLABEL:
1202 return g_strdup_printf("%s %i", criteria_str, matcher->value);
1203 case MATCHCRITERIA_ALL:
1204 case MATCHCRITERIA_UNREAD:
1205 case MATCHCRITERIA_NOT_UNREAD:
1206 case MATCHCRITERIA_NEW:
1207 case MATCHCRITERIA_NOT_NEW:
1208 case MATCHCRITERIA_MARKED:
1209 case MATCHCRITERIA_NOT_MARKED:
1210 case MATCHCRITERIA_DELETED:
1211 case MATCHCRITERIA_NOT_DELETED:
1212 case MATCHCRITERIA_REPLIED:
1213 case MATCHCRITERIA_NOT_REPLIED:
1214 case MATCHCRITERIA_FORWARDED:
1215 case MATCHCRITERIA_NOT_FORWARDED:
1216 case MATCHCRITERIA_LOCKED:
1217 case MATCHCRITERIA_NOT_LOCKED:
1218 case MATCHCRITERIA_PARTIAL:
1219 case MATCHCRITERIA_NOT_PARTIAL:
1220 case MATCHCRITERIA_IGNORE_THREAD:
1221 case MATCHCRITERIA_NOT_IGNORE_THREAD:
1222 return g_strdup(criteria_str);
1223 case MATCHCRITERIA_TEST:
1224 case MATCHCRITERIA_NOT_TEST:
1225 quoted_expr = matcher_quote_str(matcher->expr);
1226 matcher_str = g_strdup_printf("%s \"%s\"",
1227 criteria_str, quoted_expr);
1228 g_free(quoted_expr);
1232 matchtype_str = NULL;
1233 for (i = 0; i < sizeof matchparser_tab / sizeof matchparser_tab[0]; i++) {
1234 if (matchparser_tab[i].id == matcher->matchtype)
1235 matchtype_str = matchparser_tab[i].str;
1238 if (matchtype_str == NULL)
1241 switch (matcher->matchtype) {
1242 case MATCHTYPE_MATCH:
1243 case MATCHTYPE_MATCHCASE:
1244 case MATCHTYPE_REGEXP:
1245 case MATCHTYPE_REGEXPCASE:
1246 case MATCHTYPE_ALL_IN_ADDRESSBOOK:
1247 case MATCHTYPE_ANY_IN_ADDRESSBOOK:
1248 quoted_expr = matcher_quote_str(matcher->expr);
1249 if (matcher->header) {
1250 gchar * quoted_header;
1252 quoted_header = matcher_quote_str(matcher->header);
1253 matcher_str = g_strdup_printf
1254 ("%s \"%s\" %s \"%s\"",
1255 criteria_str, quoted_header,
1256 matchtype_str, quoted_expr);
1257 g_free(quoted_header);
1260 matcher_str = g_strdup_printf
1261 ("%s %s \"%s\"", criteria_str,
1262 matchtype_str, quoted_expr);
1263 g_free(quoted_expr);
1271 *\brief Convert a list of conditions to a string
1273 *\param matchers List of conditions
1275 *\return gchar * Newly allocated string
1277 gchar *matcherlist_to_string(const MatcherList *matchers)
1283 gchar *result = NULL;
1285 count = g_slist_length(matchers->matchers);
1286 vstr = g_new(gchar *, count + 1);
1288 for (l = matchers->matchers, cur_str = vstr; l != NULL;
1289 l = g_slist_next(l), cur_str ++) {
1290 *cur_str = matcherprop_to_string((MatcherProp *) l->data);
1291 if (*cur_str == NULL)
1296 if (matchers->bool_and)
1297 result = g_strjoinv(" & ", vstr);
1299 result = g_strjoinv(" | ", vstr);
1301 for (cur_str = vstr ; *cur_str != NULL ; cur_str ++)
1309 #define STRLEN_ZERO(s) ((s) ? strlen(s) : 0)
1310 #define STRLEN_DEFAULT(s,d) ((s) ? strlen(s) : STRLEN_ZERO(d))
1312 static void add_str_default(gchar ** dest,
1313 const gchar * s, const gchar * d)
1315 gchar quoted_str[4096];
1323 quote_cmd_argument(quoted_str, sizeof(quoted_str), str);
1324 strcpy(* dest, quoted_str);
1326 (* dest) += strlen(* dest);
1329 /* matching_build_command() - preferably cmd should be unescaped */
1331 *\brief Build the command line to execute
1333 *\param cmd String with command line specifiers
1334 *\param info Message info to use for command
1336 *\return gchar * Newly allocated string
1338 gchar *matching_build_command(const gchar *cmd, MsgInfo *info)
1340 const gchar *s = cmd;
1341 gchar *filename = NULL;
1342 gchar *processed_cmd;
1346 const gchar *const no_subject = _("(none)") ;
1347 const gchar *const no_from = _("(none)") ;
1348 const gchar *const no_to = _("(none)") ;
1349 const gchar *const no_cc = _("(none)") ;
1350 const gchar *const no_date = _("(none)") ;
1351 const gchar *const no_msgid = _("(none)") ;
1352 const gchar *const no_newsgroups = _("(none)") ;
1353 const gchar *const no_references = _("(none)") ;
1355 size = STRLEN_ZERO(cmd) + 1;
1356 while (*s != '\0') {
1363 case 's': /* subject */
1364 size += STRLEN_DEFAULT(info->subject, no_subject) - 2;
1366 case 'f': /* from */
1367 size += STRLEN_DEFAULT(info->from, no_from) - 2;
1370 size += STRLEN_DEFAULT(info->to, no_to) - 2;
1373 size += STRLEN_DEFAULT(info->cc, no_cc) - 2;
1375 case 'd': /* date */
1376 size += STRLEN_DEFAULT(info->date, no_date) - 2;
1378 case 'i': /* message-id */
1379 size += STRLEN_DEFAULT(info->msgid, no_msgid) - 2;
1381 case 'n': /* newsgroups */
1382 size += STRLEN_DEFAULT(info->newsgroups, no_newsgroups) - 2;
1384 case 'r': /* references */
1385 /* FIXME: using the inreplyto header for reference */
1386 size += STRLEN_DEFAULT(info->inreplyto, no_references) - 2;
1388 case 'F': /* file */
1389 if (filename == NULL)
1390 filename = folder_item_fetch_msg(info->folder, info->msgnum);
1392 if (filename == NULL) {
1393 g_warning("filename is not set");
1397 size += strlen(filename) - 2;
1406 /* as the string can be quoted, we double the result */
1409 processed_cmd = g_new0(gchar, size);
1413 while (*s != '\0') {
1421 case 's': /* subject */
1422 add_str_default(&p, info->subject,
1425 case 'f': /* from */
1426 add_str_default(&p, info->from,
1430 add_str_default(&p, info->to,
1434 add_str_default(&p, info->cc,
1437 case 'd': /* date */
1438 add_str_default(&p, info->date,
1441 case 'i': /* message-id */
1442 add_str_default(&p, info->msgid,
1445 case 'n': /* newsgroups */
1446 add_str_default(&p, info->newsgroups,
1449 case 'r': /* references */
1450 /* FIXME: using the inreplyto header for references */
1451 add_str_default(&p, info->inreplyto, no_references);
1453 case 'F': /* file */
1454 if (filename != NULL)
1455 add_str_default(&p, filename, NULL);
1474 return processed_cmd;
1476 #undef STRLEN_DEFAULT
1479 /* ************************************************************ */
1483 *\brief Write filtering list to file
1486 *\param prefs_filtering List of filtering conditions
1488 static void prefs_filtering_write(FILE *fp, GSList *prefs_filtering)
1492 for (cur = prefs_filtering; cur != NULL; cur = cur->next) {
1493 gchar *filtering_str;
1494 gchar *tmp_name = NULL;
1495 FilteringProp *prop;
1497 if (NULL == (prop = (FilteringProp *) cur->data))
1500 if (NULL == (filtering_str = filteringprop_to_string(prop)))
1503 if (prop->enabled) {
1504 if (fputs("enabled ", fp) == EOF) {
1505 FILE_OP_ERROR("filtering config", "fputs || fputc");
1509 if (fputs("disabled ", fp) == EOF) {
1510 FILE_OP_ERROR("filtering config", "fputs || fputc");
1515 if (fputs("rulename \"", fp) == EOF) {
1516 FILE_OP_ERROR("filtering config", "fputs || fputc");
1517 g_free(filtering_str);
1520 tmp_name = prop->name;
1521 while (tmp_name && *tmp_name != '\0') {
1522 if (*tmp_name != '"') {
1523 if (fputc(*tmp_name, fp) == EOF) {
1524 FILE_OP_ERROR("filtering config", "fputc");
1525 g_free(filtering_str);
1528 } else if (*tmp_name == '"') {
1529 if (fputc('\\', fp) == EOF ||
1530 fputc('"', fp) == EOF) {
1531 FILE_OP_ERROR("filtering config", "fputc");
1532 g_free(filtering_str);
1538 if(fputs("\" ", fp) == EOF ||
1539 fputs(filtering_str, fp) == EOF ||
1540 fputc('\n', fp) == EOF) {
1541 FILE_OP_ERROR("filtering config", "fputs || fputc");
1542 g_free(filtering_str);
1545 g_free(filtering_str);
1550 *\brief Write matchers from a folder item
1552 *\param node Node with folder info
1553 *\param data File pointer
1555 *\return gboolean FALSE
1557 static gboolean prefs_matcher_write_func(GNode *node, gpointer data)
1562 GSList *prefs_filtering;
1565 /* prevent warning */
1566 if (item->path == NULL)
1568 id = folder_item_get_identifier(item);
1571 prefs_filtering = item->prefs->processing;
1573 if (prefs_filtering != NULL) {
1574 fprintf(fp, "[%s]\n", id);
1575 prefs_filtering_write(fp, prefs_filtering);
1585 *\brief Save matchers from folder items
1589 static void prefs_matcher_save(FILE *fp)
1593 for (cur = folder_get_list() ; cur != NULL ; cur = g_list_next(cur)) {
1596 folder = (Folder *) cur->data;
1597 g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
1598 prefs_matcher_write_func, fp);
1601 /* pre global rules */
1602 fprintf(fp, "[preglobal]\n");
1603 prefs_filtering_write(fp, pre_global_processing);
1606 /* post global rules */
1607 fprintf(fp, "[postglobal]\n");
1608 prefs_filtering_write(fp, post_global_processing);
1611 /* filtering rules */
1612 fprintf(fp, "[filtering]\n");
1613 prefs_filtering_write(fp, filtering_rules);
1618 *\brief Write filtering / matcher configuration file
1620 void prefs_matcher_write_config(void)
1625 debug_print("Writing matcher configuration...\n");
1627 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
1630 if ((pfile = prefs_write_open(rcpath)) == NULL) {
1631 g_warning("failed to write configuration to file\n");
1637 prefs_matcher_save(pfile->fp);
1641 if (prefs_file_close(pfile) < 0) {
1642 g_warning("failed to write configuration to file\n");
1647 /* ******************************************************************* */
1649 void matcher_add_rulenames(const gchar *rcpath)
1651 gchar *newpath = g_strconcat(rcpath, ".new", NULL);
1652 FILE *src = g_fopen(rcpath, "rb");
1653 FILE *dst = g_fopen(newpath, "wb");
1654 gchar buf[BUFFSIZE];
1662 while (fgets (buf, sizeof(buf), src) != NULL) {
1663 if (strlen(buf) > 2 && buf[0] != '['
1664 && strncmp(buf, "rulename \"", 10)) {
1665 fwrite("rulename \"\" ",
1666 strlen("rulename \"\" "), 1, dst);
1668 fwrite(buf, strlen(buf), 1, dst);
1672 move_file(newpath, rcpath, TRUE);
1677 *\brief Read matcher configuration
1679 void prefs_matcher_read_config(void)
1682 gchar *rc_old_format;
1685 create_matchparser_hashtab();
1686 prefs_filtering_clear();
1688 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MATCHER_RC, NULL);
1689 rc_old_format = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MATCHER_RC,
1690 ".pre_names", NULL);
1692 if (!is_file_exist(rc_old_format) && is_file_exist(rcpath)) {
1693 /* backup file with no rules names, in case
1694 * anything goes wrong */
1695 copy_file(rcpath, rc_old_format, FALSE);
1696 /* now hack the file in order to have it to the new format */
1697 matcher_add_rulenames(rcpath);
1700 g_free(rc_old_format);
1702 f = g_fopen(rcpath, "rb");
1706 matcher_parser_start_parsing(f);
1707 fclose(matcher_parserin);
1710 /* previous version compatibily */
1712 /* printf("reading filtering\n"); */
1713 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
1714 FILTERING_RC, NULL);
1715 f = g_fopen(rcpath, "rb");
1719 matcher_parser_start_parsing(f);
1720 fclose(matcher_parserin);
1723 /* printf("reading scoring\n"); */
1724 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
1726 f = g_fopen(rcpath, "rb");
1730 matcher_parser_start_parsing(f);
1731 fclose(matcher_parserin);