2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2001 Hiroyuki Yamamoto & The Sylpheed Claws Team
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 * 07/18/01 Alfons when we want a file name from a MsgInfo, get that
24 * from MsgInfo->folder if the message is being filtered
25 * from incorporation. also some more safe string checking.
34 #include "procheader.h"
43 typedef struct _MatchParser MatchParser;
45 static MatchParser matchparser_tab[] = {
47 {MATCHING_ALL, "all"},
48 {MATCHING_UNREAD, "unread"},
49 {MATCHING_NOT_UNREAD, "~unread"},
50 {MATCHING_NEW, "new"},
51 {MATCHING_NOT_NEW, "~new"},
52 {MATCHING_MARKED, "marked"},
53 {MATCHING_NOT_MARKED, "~marked"},
54 {MATCHING_DELETED, "deleted"},
55 {MATCHING_NOT_DELETED, "~deleted"},
56 {MATCHING_REPLIED, "replied"},
57 {MATCHING_NOT_REPLIED, "~replied"},
58 {MATCHING_FORWARDED, "forwarded"},
59 {MATCHING_NOT_FORWARDED, "~forwarded"},
62 {MATCHING_SUBJECT, "subject"},
63 {MATCHING_NOT_SUBJECT, "~subject"},
64 {MATCHING_FROM, "from"},
65 {MATCHING_NOT_FROM, "~from"},
67 {MATCHING_NOT_TO, "~to"},
69 {MATCHING_NOT_CC, "~cc"},
70 {MATCHING_TO_OR_CC, "to_or_cc"},
71 {MATCHING_NOT_TO_AND_NOT_CC, "~to_or_cc"},
72 {MATCHING_AGE_GREATER, "age_greater"},
73 {MATCHING_AGE_LOWER, "age_lower"},
74 {MATCHING_NEWSGROUPS, "newsgroups"},
75 {MATCHING_NOT_NEWSGROUPS, "~newsgroups"},
76 {MATCHING_INREPLYTO, "inreplyto"},
77 {MATCHING_NOT_INREPLYTO, "~inreplyto"},
78 {MATCHING_REFERENCES, "references"},
79 {MATCHING_NOT_REFERENCES, "~references"},
80 {MATCHING_SCORE_GREATER, "score_greater"},
81 {MATCHING_SCORE_LOWER, "score_lower"},
83 /* content have to be read */
84 {MATCHING_HEADER, "header"},
85 {MATCHING_NOT_HEADER, "~header"},
86 {MATCHING_HEADERS_PART, "headers_part"},
87 {MATCHING_NOT_HEADERS_PART, "~headers_part"},
88 {MATCHING_MESSAGE, "message"},
89 {MATCHING_NOT_MESSAGE, "~message"},
90 {MATCHING_BODY_PART, "body_part"},
91 {MATCHING_NOT_BODY_PART, "~body_part"},
92 {MATCHING_EXECUTE, "execute"},
93 {MATCHING_NOT_EXECUTE, "~execute"},
96 {MATCHING_MATCHCASE, "matchcase"},
97 {MATCHING_MATCH, "match"},
98 {MATCHING_REGEXPCASE, "regexpcase"},
99 {MATCHING_REGEXP, "regexp"},
102 {MATCHING_SCORE, "score"},
105 {MATCHING_ACTION_MOVE, "move"},
106 {MATCHING_ACTION_COPY, "copy"},
107 {MATCHING_ACTION_DELETE, "delete"},
108 {MATCHING_ACTION_MARK, "mark"},
109 {MATCHING_ACTION_UNMARK, "unmark"},
110 {MATCHING_ACTION_MARK_AS_READ, "mark_as_read"},
111 {MATCHING_ACTION_MARK_AS_UNREAD, "mark_as_unread"},
112 {MATCHING_ACTION_FORWARD, "forward"},
113 {MATCHING_ACTION_FORWARD_AS_ATTACHMENT, "forward_as_attachment"},
114 /* {MATCHING_EXECUTE, "execute"}, */
117 gchar * get_matchparser_tab_str(gint id)
121 for(i = 0 ; i < (int) (sizeof(matchparser_tab) / sizeof(MatchParser)) ;
123 if (matchparser_tab[i].id == id)
124 return matchparser_tab[i].str;
134 header "x-mailing" match "toto"
135 subject match "regexp" & to regexp "regexp"
136 subject match "regexp" | to regexpcase "regexp" | age_sup 5
139 static gboolean matcher_is_blank(gchar ch);
141 /* ******************* parser *********************** */
143 static gboolean matcher_is_blank(gchar ch)
145 return (ch == ' ') || (ch == '\t');
148 /* parse for one condition */
150 MatcherProp * matcherprop_parse(gchar ** str)
158 gchar * header = NULL;
161 key = matcher_parse_keyword(&tmp);
168 case MATCHING_AGE_LOWER:
169 case MATCHING_AGE_GREATER:
170 case MATCHING_SCORE_LOWER:
171 case MATCHING_SCORE_GREATER:
172 value = matcher_parse_number(&tmp);
179 prop = matcherprop_new(key, NULL, 0, NULL, value);
184 case MATCHING_UNREAD:
185 case MATCHING_NOT_UNREAD:
187 case MATCHING_NOT_NEW:
188 case MATCHING_MARKED:
189 case MATCHING_NOT_MARKED:
190 case MATCHING_DELETED:
191 case MATCHING_NOT_DELETED:
192 case MATCHING_REPLIED:
193 case MATCHING_NOT_REPLIED:
194 case MATCHING_FORWARDED:
195 case MATCHING_NOT_FORWARDED:
196 prop = matcherprop_new(key, NULL, 0, NULL, 0);
201 case MATCHING_SUBJECT:
202 case MATCHING_NOT_SUBJECT:
204 case MATCHING_NOT_FROM:
206 case MATCHING_NOT_TO:
208 case MATCHING_NOT_CC:
209 case MATCHING_TO_OR_CC:
210 case MATCHING_NOT_TO_AND_NOT_CC:
211 case MATCHING_NEWSGROUPS:
212 case MATCHING_NOT_NEWSGROUPS:
213 case MATCHING_INREPLYTO:
214 case MATCHING_NOT_REFERENCES:
215 case MATCHING_REFERENCES:
216 case MATCHING_NOT_INREPLYTO:
217 case MATCHING_MESSAGE:
218 case MATCHING_NOT_MESSAGE:
219 case MATCHING_EXECUTE:
220 case MATCHING_NOT_EXECUTE:
221 case MATCHING_HEADERS_PART:
222 case MATCHING_NOT_HEADERS_PART:
223 case MATCHING_BODY_PART:
224 case MATCHING_NOT_BODY_PART:
225 case MATCHING_HEADER:
226 case MATCHING_NOT_HEADER:
227 if ((key == MATCHING_HEADER) || (key == MATCHING_NOT_HEADER)) {
228 header = matcher_parse_str(&tmp);
235 match = matcher_parse_keyword(&tmp);
244 case MATCHING_REGEXP:
245 case MATCHING_REGEXPCASE:
246 expr = matcher_parse_regexp(&tmp);
254 prop = matcherprop_new(key, header, match, expr, 0);
259 case MATCHING_MATCHCASE:
260 expr = matcher_parse_str(&tmp);
268 prop = matcherprop_new(key, header, match, expr, 0);
284 gint matcher_parse_keyword(gchar ** str)
292 dup = alloca(strlen(* str) + 1);
296 while (matcher_is_blank(*p))
301 while (!matcher_is_blank(*p) && (*p != '\0'))
305 for(i = 0 ; i < (int) (sizeof(matchparser_tab) / sizeof(MatchParser)) ;
307 if ((strlen(matchparser_tab[i].str) == p - start) &&
308 (strncasecmp(matchparser_tab[i].str, start,
323 return matchparser_tab[match].id;
326 gint matcher_parse_number(gchar ** str)
332 dup = alloca(strlen(* str) + 1);
336 while (matcher_is_blank(*p))
341 if (!isdigit(*p) && *p != '-' && *p != '+') {
345 if (*p == '-' || *p == '+')
356 gboolean matcher_parse_boolean_op(gchar ** str)
362 while (matcher_is_blank(*p))
366 *str += p - * str + 1;
369 else if (*p == '&') {
370 *str += p - * str + 1;
379 gchar * matcher_parse_regexp(gchar ** str)
385 dup = alloca(strlen(* str) + 1);
389 while (matcher_is_blank(*p))
407 return g_strdup(start);
410 /* matcher_parse_str() - parses a string until it hits a
411 * terminating \". to unescape characters use \. The string
412 * should not be used directly: matcher_unescape_str()
413 * returns a string that can be used directly. */
414 gchar * matcher_parse_str(gchar ** str)
421 dup = alloca(strlen(* str) + 1);
425 while (matcher_is_blank(*p))
437 for ( ; *p && *p != '\"'; p++, dest++) {
447 *str += dest - dup + 2;
448 return g_strdup(start);
451 gchar *matcher_unescape_str(gchar *str)
453 gchar *tmp = alloca(strlen(str) + 1);
454 register gchar *src = tmp;
455 register gchar *dst = str;
459 for ( ; *src; src++) {
466 else if (*src == 'n')
468 else if (*src == 'r')
470 else if (*src == '\'' || *src == '\"')
482 /* **************** data structure allocation **************** */
485 MatcherProp * matcherprop_new(gint criteria, gchar * header,
486 gint matchtype, gchar * expr,
491 prop = g_new0(MatcherProp, 1);
492 prop->criteria = criteria;
494 prop->header = g_strdup(header);
498 prop->expr = g_strdup(expr);
501 prop->matchtype = matchtype;
509 void matcherprop_free(MatcherProp * prop)
512 if (prop->preg != NULL) {
520 /* ************** match ******************************/
523 /* match the given string */
525 static gboolean matcherprop_string_match(MatcherProp * prop, gchar * str)
533 switch(prop->matchtype) {
534 case MATCHING_REGEXPCASE:
535 case MATCHING_REGEXP:
536 if (!prop->preg && (prop->error == 0)) {
537 prop->preg = g_new0(regex_t, 1);
538 if (regcomp(prop->preg, prop->expr,
539 REG_NOSUB | REG_EXTENDED
540 | ((prop->matchtype == MATCHING_REGEXPCASE)
541 ? REG_ICASE : 0)) != 0) {
546 if (prop->preg == NULL)
549 if (regexec(prop->preg, str, 0, NULL, 0) == 0)
555 return (strstr(str, prop->expr) != NULL);
557 case MATCHING_MATCHCASE:
558 str2 = alloca(strlen(prop->expr) + 1);
559 strcpy(str2, prop->expr);
561 str1 = alloca(strlen(str) + 1);
564 return (strstr(str1, str2) != NULL);
571 gboolean matcherprop_match_execute(MatcherProp * prop, MsgInfo * info)
575 cmd = matching_build_command(prop->expr, info);
579 return (system(cmd) == 0);
582 /* match a message and his headers, hlist can be NULL if you don't
583 want to use headers */
585 gboolean matcherprop_match(MatcherProp * prop, MsgInfo * info)
589 switch(prop->criteria) {
592 case MATCHING_UNREAD:
593 return MSG_IS_UNREAD(info->flags);
594 case MATCHING_NOT_UNREAD:
595 return !MSG_IS_UNREAD(info->flags);
597 return MSG_IS_NEW(info->flags);
598 case MATCHING_NOT_NEW:
599 return !MSG_IS_NEW(info->flags);
600 case MATCHING_MARKED:
601 return MSG_IS_MARKED(info->flags);
602 case MATCHING_NOT_MARKED:
603 return !MSG_IS_MARKED(info->flags);
604 case MATCHING_DELETED:
605 return MSG_IS_DELETED(info->flags);
606 case MATCHING_NOT_DELETED:
607 return !MSG_IS_DELETED(info->flags);
608 case MATCHING_REPLIED:
609 return MSG_IS_REPLIED(info->flags);
610 case MATCHING_NOT_REPLIED:
611 return !MSG_IS_REPLIED(info->flags);
612 case MATCHING_FORWARDED:
613 return MSG_IS_FORWARDED(info->flags);
614 case MATCHING_NOT_FORWARDED:
615 return !MSG_IS_FORWARDED(info->flags);
616 case MATCHING_SUBJECT:
617 return matcherprop_string_match(prop, info->subject);
618 case MATCHING_NOT_SUBJECT:
619 return !matcherprop_string_match(prop, info->subject);
621 return matcherprop_string_match(prop, info->from);
622 case MATCHING_NOT_FROM:
623 return !matcherprop_string_match(prop, info->from);
625 return matcherprop_string_match(prop, info->to);
626 case MATCHING_NOT_TO:
627 return !matcherprop_string_match(prop, info->to);
629 return matcherprop_string_match(prop, info->cc);
630 case MATCHING_NOT_CC:
631 return !matcherprop_string_match(prop, info->cc);
632 case MATCHING_TO_OR_CC:
633 return matcherprop_string_match(prop, info->to)
634 || matcherprop_string_match(prop, info->cc);
635 case MATCHING_NOT_TO_AND_NOT_CC:
636 return !(matcherprop_string_match(prop, info->to)
637 || matcherprop_string_match(prop, info->cc));
638 case MATCHING_AGE_GREATER:
640 return ((t - info->date_t) / (60 * 60 * 24)) >= prop->value;
641 case MATCHING_AGE_LOWER:
643 return ((t - info->date_t) / (60 * 60 * 24)) <= prop->value;
644 case MATCHING_SCORE_GREATER:
645 return info->score >= prop->value;
646 case MATCHING_SCORE_LOWER:
647 return info->score <= prop->value;
648 case MATCHING_NEWSGROUPS:
649 return matcherprop_string_match(prop, info->newsgroups);
650 case MATCHING_NOT_NEWSGROUPS:
651 return !matcherprop_string_match(prop, info->newsgroups);
652 case MATCHING_INREPLYTO:
653 return matcherprop_string_match(prop, info->inreplyto);
654 case MATCHING_NOT_INREPLYTO:
655 return !matcherprop_string_match(prop, info->inreplyto);
656 case MATCHING_REFERENCES:
657 return matcherprop_string_match(prop, info->references);
658 case MATCHING_NOT_REFERENCES:
659 return !matcherprop_string_match(prop, info->references);
660 case MATCHING_EXECUTE:
661 return matcherprop_match_execute(prop, info);
662 case MATCHING_NOT_EXECUTE:
663 return !matcherprop_match_execute(prop, info);
669 /* ********************* MatcherList *************************** */
672 /* parse for a list of conditions */
674 MatcherList * matcherlist_parse(gchar ** str)
677 MatcherProp * matcher;
678 GSList * matchers_list = NULL;
679 gboolean bool_and = TRUE;
682 gboolean main_bool_and = TRUE;
687 matcher = matcherprop_parse(&tmp);
693 matchers_list = g_slist_append(matchers_list, matcher);
696 bool_and = matcher_parse_boolean_op(&tmp);
702 main_bool_and = bool_and;
703 matcher = matcherprop_parse(&tmp);
706 g_slist_append(matchers_list, matcher);
709 for(l = matchers_list ; l != NULL ;
711 matcherprop_free((MatcherProp *)
713 g_slist_free(matchers_list);
720 cond = matcherlist_new(matchers_list, main_bool_and);
727 MatcherList * matcherlist_new(GSList * matchers, gboolean bool_and)
731 cond = g_new0(MatcherList, 1);
733 cond->matchers = matchers;
734 cond->bool_and = bool_and;
739 void matcherlist_free(MatcherList * cond)
743 for(l = cond->matchers ; l != NULL ; l = g_slist_next(l)) {
744 matcherprop_free((MatcherProp *) l->data);
753 static void matcherlist_skip_headers(FILE *fp)
757 while (procheader_get_one_field(buf, sizeof(buf), fp, NULL) != -1) {
762 matcherprop_match_one_header
763 returns TRUE if buf matchs the MatchersProp criteria
766 static gboolean matcherprop_match_one_header(MatcherProp * matcher,
772 switch(matcher->criteria) {
773 case MATCHING_HEADER:
774 case MATCHING_NOT_HEADER:
775 header = procheader_parse_header(buf);
778 if (procheader_headername_equal(header->name,
780 if (matcher->criteria == MATCHING_HEADER)
781 result = matcherprop_string_match(matcher, header->body);
783 result = !matcherprop_string_match(matcher, header->body);
784 procheader_header_free(header);
788 procheader_header_free(header);
791 case MATCHING_HEADERS_PART:
792 case MATCHING_MESSAGE:
793 return matcherprop_string_match(matcher, buf);
794 case MATCHING_NOT_MESSAGE:
795 case MATCHING_NOT_HEADERS_PART:
796 return !matcherprop_string_match(matcher, buf);
802 matcherprop_criteria_header
803 returns TRUE if the headers must be matched
806 static gboolean matcherprop_criteria_headers(MatcherProp * matcher)
808 switch(matcher->criteria) {
809 case MATCHING_HEADER:
810 case MATCHING_NOT_HEADER:
811 case MATCHING_HEADERS_PART:
812 case MATCHING_NOT_HEADERS_PART:
819 static gboolean matcherprop_criteria_message(MatcherProp * matcher)
821 switch(matcher->criteria) {
822 case MATCHING_MESSAGE:
823 case MATCHING_NOT_MESSAGE:
831 matcherlist_match_one_header
832 returns TRUE if match should stop
835 static gboolean matcherlist_match_one_header(MatcherList * matchers,
840 for(l = matchers->matchers ; l != NULL ; l = g_slist_next(l)) {
841 MatcherProp * matcher = (MatcherProp *) l->data;
843 if (matcherprop_criteria_headers(matcher) ||
844 matcherprop_criteria_message(matcher)) {
845 if (matcherprop_match_one_header(matcher, buf)) {
846 matcher->result = TRUE;
850 if (matcherprop_criteria_headers(matcher)) {
851 if (matcher->result) {
852 if (!matchers->bool_and)
862 matcherlist_match_headers
863 returns TRUE if one of the headers matchs the MatcherList criteria
866 static gboolean matcherlist_match_headers(MatcherList * matchers, FILE * fp)
870 while (procheader_get_one_field(buf, sizeof(buf), fp, NULL) != -1)
871 if (matcherlist_match_one_header(matchers, buf))
878 matcherprop_criteria_body
879 returns TRUE if the body must be matched
882 static gboolean matcherprop_criteria_body(MatcherProp * matcher)
884 switch(matcher->criteria) {
885 case MATCHING_BODY_PART:
886 case MATCHING_NOT_BODY_PART:
894 matcherprop_match_line
895 returns TRUE if the string matchs the MatcherProp criteria
898 static gboolean matcherprop_match_line(MatcherProp * matcher, gchar * line)
900 switch(matcher->criteria) {
901 case MATCHING_BODY_PART:
902 case MATCHING_MESSAGE:
903 return matcherprop_string_match(matcher, line);
904 case MATCHING_NOT_BODY_PART:
905 case MATCHING_NOT_MESSAGE:
906 return !matcherprop_string_match(matcher, line);
912 matcherlist_match_line
913 returns TRUE if the string matchs the MatcherList criteria
916 static gboolean matcherlist_match_line(MatcherList * matchers, gchar * line)
920 for(l = matchers->matchers ; l != NULL ; l = g_slist_next(l)) {
921 MatcherProp * matcher = (MatcherProp *) l->data;
923 if (matcherprop_criteria_body(matcher) ||
924 matcherprop_criteria_message(matcher)) {
925 if (matcherprop_match_line(matcher, line)) {
926 matcher->result = TRUE;
930 if (matcher->result) {
931 if (!matchers->bool_and)
939 matcherlist_match_body
940 returns TRUE if one line of the body matchs the MatcherList criteria
943 static gboolean matcherlist_match_body(MatcherList * matchers, FILE * fp)
947 while (fgets(buf, sizeof(buf), fp) != NULL)
948 if (matcherlist_match_line(matchers, buf))
954 gboolean matcherlist_match_file(MatcherList * matchers, MsgInfo * info,
957 gboolean read_headers;
962 gboolean is_incorporating = MSG_IS_FILTERING(info->flags);
964 /* check which things we need to do */
965 read_headers = FALSE;
968 for(l = matchers->matchers ; l != NULL ; l = g_slist_next(l)) {
969 MatcherProp * matcher = (MatcherProp *) l->data;
971 if (matcherprop_criteria_headers(matcher))
973 if (matcherprop_criteria_body(matcher))
975 if (matcherprop_criteria_message(matcher)) {
979 matcher->result = FALSE;
982 if (!read_headers && !read_body)
985 file = is_incorporating ? g_strdup(info->folder)
986 : procmsg_get_message_file(info);
991 if ((fp = fopen(file, "r")) == NULL) {
992 FILE_OP_ERROR(file, "fopen");
997 /* read the headers */
1000 if (matcherlist_match_headers(matchers, fp))
1004 matcherlist_skip_headers(fp);
1009 matcherlist_match_body(matchers, fp);
1012 for(l = matchers->matchers ; l != NULL ; l = g_slist_next(l)) {
1013 MatcherProp * matcher = (MatcherProp *) l->data;
1015 if (matcherprop_criteria_headers(matcher) ||
1016 matcherprop_criteria_body(matcher) ||
1017 matcherprop_criteria_message(matcher))
1018 if (matcher->result) {
1019 if (!matchers->bool_and) {
1025 if (matchers->bool_and) {
1039 /* test a list of condition */
1041 gboolean matcherlist_match(MatcherList * matchers, MsgInfo * info)
1046 if (matchers->bool_and)
1051 /* test the cached elements */
1053 for(l = matchers->matchers ; l != NULL ; l = g_slist_next(l)) {
1054 MatcherProp * matcher = (MatcherProp *) l->data;
1056 switch(matcher->criteria) {
1058 case MATCHING_UNREAD:
1059 case MATCHING_NOT_UNREAD:
1061 case MATCHING_NOT_NEW:
1062 case MATCHING_MARKED:
1063 case MATCHING_NOT_MARKED:
1064 case MATCHING_DELETED:
1065 case MATCHING_NOT_DELETED:
1066 case MATCHING_REPLIED:
1067 case MATCHING_NOT_REPLIED:
1068 case MATCHING_FORWARDED:
1069 case MATCHING_NOT_FORWARDED:
1070 case MATCHING_SUBJECT:
1071 case MATCHING_NOT_SUBJECT:
1073 case MATCHING_NOT_FROM:
1075 case MATCHING_NOT_TO:
1077 case MATCHING_NOT_CC:
1078 case MATCHING_TO_OR_CC:
1079 case MATCHING_NOT_TO_AND_NOT_CC:
1080 case MATCHING_AGE_GREATER:
1081 case MATCHING_AGE_LOWER:
1082 case MATCHING_NEWSGROUPS:
1083 case MATCHING_NOT_NEWSGROUPS:
1084 case MATCHING_INREPLYTO:
1085 case MATCHING_NOT_INREPLYTO:
1086 case MATCHING_REFERENCES:
1087 case MATCHING_NOT_REFERENCES:
1088 case MATCHING_SCORE_GREATER:
1089 case MATCHING_SCORE_LOWER:
1090 case MATCHING_EXECUTE:
1091 case MATCHING_NOT_EXECUTE:
1092 if (matcherprop_match(matcher, info)) {
1093 if (!matchers->bool_and) {
1098 if (matchers->bool_and) {
1105 /* test the condition on the file */
1107 if (matcherlist_match_file(matchers, info, result)) {
1108 if (!matchers->bool_and)
1112 if (matchers->bool_and)
1120 static void matcherprop_print(MatcherProp * matcher)
1124 if (matcher == NULL) {
1125 printf("no matcher\n");
1129 switch (matcher->matchtype) {
1130 case MATCHING_MATCH:
1133 case MATCHING_REGEXP:
1136 case MATCHING_MATCHCASE:
1137 printf("matchcase\n");
1139 case MATCHING_REGEXPCASE:
1140 printf("regexpcase\n");
1144 for(i = 0 ; i < (int) (sizeof(matchparser_tab) / sizeof(MatchParser)) ;
1146 if (matchparser_tab[i].id == matcher->criteria)
1147 printf("%s\n", matchparser_tab[i].str);
1151 printf("expr : %s\n", matcher->expr);
1153 printf("age: %i\n", matcher->value;
1155 printf("compiled : %s\n", matcher->preg != NULL ? "yes" : "no");
1156 printf("error: %i\n", matcher->error);
1160 gchar * matcherprop_to_string(MatcherProp * matcher)
1162 gchar * matcher_str = NULL;
1163 gchar * criteria_str;
1164 gchar * matchtype_str;
1171 criteria_str = NULL;
1172 for(i = 0 ; i < (int) (sizeof(matchparser_tab) / sizeof(MatchParser)) ;
1174 if (matchparser_tab[i].id == matcher->criteria)
1175 criteria_str = matchparser_tab[i].str;
1177 if (criteria_str == NULL)
1180 switch(matcher->criteria) {
1181 case MATCHING_AGE_GREATER:
1182 case MATCHING_AGE_LOWER:
1183 case MATCHING_SCORE_GREATER:
1184 case MATCHING_SCORE_LOWER:
1185 return g_strdup_printf("%s %i", criteria_str, matcher->value);
1188 case MATCHING_UNREAD:
1189 case MATCHING_NOT_UNREAD:
1191 case MATCHING_NOT_NEW:
1192 case MATCHING_MARKED:
1193 case MATCHING_NOT_MARKED:
1194 case MATCHING_DELETED:
1195 case MATCHING_NOT_DELETED:
1196 case MATCHING_REPLIED:
1197 case MATCHING_NOT_REPLIED:
1198 case MATCHING_FORWARDED:
1199 case MATCHING_NOT_FORWARDED:
1200 return g_strdup(criteria_str);
1203 matchtype_str = NULL;
1204 for(i = 0 ; i < (int) (sizeof(matchparser_tab) / sizeof(MatchParser)) ;
1206 if (matchparser_tab[i].id == matcher->matchtype)
1207 matchtype_str = matchparser_tab[i].str;
1210 if (matchtype_str == NULL)
1213 switch (matcher->matchtype) {
1214 case MATCHING_MATCH:
1215 case MATCHING_MATCHCASE:
1217 for(p = matcher->expr; *p != 0 ; p++)
1218 if (*p == '\"') count ++;
1220 expr_str = g_new(char, strlen(matcher->expr) + count + 1);
1222 for(p = matcher->expr, out = expr_str ; *p != 0 ; p++, out++) {
1232 if (matcher->header)
1234 g_strdup_printf("%s \"%s\" %s \"%s\"",
1235 criteria_str, matcher->header,
1236 matchtype_str, expr_str);
1239 g_strdup_printf("%s %s \"%s\"", criteria_str,
1240 matchtype_str, expr_str);
1246 case MATCHING_REGEXP:
1247 case MATCHING_REGEXPCASE:
1249 if (matcher->header)
1251 g_strdup_printf("%s \"%s\" %s /%s/",
1252 criteria_str, matcher->header,
1253 matchtype_str, matcher->expr);
1256 g_strdup_printf("%s %s /%s/", criteria_str,
1257 matchtype_str, matcher->expr);
1265 gchar * matcherlist_to_string(MatcherList * matchers)
1273 count = g_slist_length(matchers->matchers);
1274 vstr = g_new(gchar *, count + 1);
1276 for (l = matchers->matchers, cur_str = vstr ; l != NULL ;
1277 l = g_slist_next(l), cur_str ++) {
1278 *cur_str = matcherprop_to_string((MatcherProp *) l->data);
1279 if (*cur_str == NULL)
1284 if (matchers->bool_and)
1285 result = g_strjoinv(" & ", vstr);
1287 result = g_strjoinv(" | ", vstr);
1289 for(cur_str = vstr ; *cur_str != NULL ; cur_str ++)
1296 static inline gint strlen_with_check(const gchar *expr, gint fline, const gchar *str)
1301 debug_print("%s(%d) - invalid string %s\n", __FILE__, fline, expr);
1306 #define STRLEN_WITH_CHECK(expr) \
1307 strlen_with_check(#expr, __LINE__, expr)
1309 gchar * matching_build_command(gchar * cmd, MsgInfo * info)
1312 gchar * filename = NULL;
1313 gchar * processed_cmd;
1317 matcher_unescape_str(cmd);
1319 size = strlen(cmd) + 1;
1320 while (*s != '\0') {
1327 case 's': /* subject */
1328 size += STRLEN_WITH_CHECK(info->subject) - 2;
1330 case 'f': /* from */
1331 size += STRLEN_WITH_CHECK(info->from) - 2;
1334 size += STRLEN_WITH_CHECK(info->to) - 2;
1337 size += STRLEN_WITH_CHECK(info->cc) - 2;
1339 case 'd': /* date */
1340 size += STRLEN_WITH_CHECK(info->date) - 2;
1342 case 'i': /* message-id */
1343 size += STRLEN_WITH_CHECK(info->msgid) - 2;
1345 case 'n': /* newsgroups */
1346 size += STRLEN_WITH_CHECK(info->newsgroups) - 2;
1348 case 'r': /* references */
1349 size += STRLEN_WITH_CHECK(info->references) - 2;
1351 case 'F': /* file */
1352 if (MSG_IS_FILTERING(info->flags))
1353 filename = g_strdup(info->folder);
1355 filename = folder_item_fetch_msg(info->folder, info->msgnum);
1357 if (filename == NULL) {
1358 debug_print(_("%s(%d) - filename is not set"), __FILE__, __LINE__);
1362 size += strlen(filename) - 2;
1370 processed_cmd = g_new0(gchar, size);
1374 while (*s != '\0') {
1382 case 's': /* subject */
1383 if (info->subject != NULL)
1384 strcpy(p, info->subject);
1386 strcpy(p, "(none)");
1389 case 'f': /* from */
1390 if (info->from != NULL)
1391 strcpy(p, info->from);
1393 strcpy(p, "(none)");
1397 if (info->to != NULL)
1398 strcpy(p, info->to);
1400 strcpy(p, "(none)");
1404 if (info->cc != NULL)
1405 strcpy(p, info->cc);
1407 strcpy(p, "(none)");
1410 case 'd': /* date */
1411 if (info->date != NULL)
1412 strcpy(p, info->date);
1414 strcpy(p, "(none)");
1417 case 'i': /* message-id */
1418 if (info->msgid != NULL)
1419 strcpy(p, info->msgid);
1421 strcpy(p, "(none)");
1424 case 'n': /* newsgroups */
1425 if (info->newsgroups != NULL)
1426 strcpy(p, info->newsgroups);
1428 strcpy(p, "(none)");
1431 case 'r': /* references */
1432 if (info->references != NULL)
1433 strcpy(p, info->references);
1435 strcpy(p, "(none)");
1438 case 'F': /* file */
1439 strcpy(p, filename);
1458 debug_print("*** exec string \"%s\"\n", processed_cmd);
1461 return processed_cmd;
1465 /* ************************************************************ */
1468 static void matcher_parse (gchar * str)
1470 matcher_parser_scan_string(str);
1471 matcher_parserparse();