2012-10-12 [mones] 3.8.1cvs98
[claws.git] / src / matcher.h
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 2002-2012 by the Claws Mail Team and Hiroyuki Yamamoto
4  *
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 3 of the License, or
8  * (at your option) any later version.
9  *
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.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  * 
18  */
19
20 #ifndef MATCHER_H
21 #define MATCHER_H
22
23 #include <sys/types.h>
24 #include <regex.h>
25 #include <glib.h>
26 #include "proctypes.h"
27 #include "matchertypes.h"
28
29 /* constants generated by yacc */
30 #include "matcher_parser_lex.h"
31 #include "matcher_parser_parse.h"
32
33 struct _MatcherProp {
34         int matchtype;
35         int criteria;
36         gchar *header;
37         gchar *expr;
38         int value;
39         regex_t *preg;
40         int error;
41         gboolean result;
42         gboolean done;
43 };
44
45 struct _MatcherList {
46         GSList *matchers;
47         gboolean bool_and;
48 };
49
50
51 /* map MATCHCRITERIA_ to yacc's MATCHER_ */
52 #define MC_(name) \
53         MATCHCRITERIA_ ## name = MATCHER_ ## name
54
55 /* map MATCHTYPE_ to yacc's MATCHER_ */
56 #define MT_(name) \
57         MATCHTYPE_ ## name = MATCHER_ ## name
58
59 /* map MATCHACTION_ to yacc's MATCHER_ */
60 #define MA_(name) \
61         MATCHACTION_ ## name = MATCHER_ ## name
62
63 /* map MATCHBOOL_ to yacc's MATCHER_ */
64 #define MB_(name) \
65         MATCHERBOOL_ ## name = MATCHER_ ## name
66
67 enum {
68         /* match */
69         MC_(ALL),
70         MC_(UNREAD), MC_(NOT_UNREAD),
71         MC_(NEW), MC_(NOT_NEW),
72         MC_(MARKED), MC_(NOT_MARKED),
73         MC_(DELETED), MC_(NOT_DELETED),
74         MC_(REPLIED), MC_(NOT_REPLIED),
75         MC_(FORWARDED), MC_(NOT_FORWARDED),
76         MC_(LOCKED), MC_(NOT_LOCKED),
77         MC_(SPAM),MC_(NOT_SPAM),
78         MC_(HAS_ATTACHMENT), MC_(HAS_NO_ATTACHMENT),
79         MC_(SIGNED), MC_(NOT_SIGNED),
80         MC_(PARTIAL), MC_(NOT_PARTIAL),
81         MC_(COLORLABEL), MC_(NOT_COLORLABEL),
82         MC_(IGNORE_THREAD), MC_(NOT_IGNORE_THREAD),
83         MC_(WATCH_THREAD), MC_(NOT_WATCH_THREAD),
84         MC_(SUBJECT), MC_(NOT_SUBJECT),
85         MC_(FROM), MC_(NOT_FROM),
86         MC_(TO), MC_(NOT_TO),
87         MC_(CC), MC_(NOT_CC),
88         MC_(TO_OR_CC), MC_(NOT_TO_AND_NOT_CC),
89         MC_(AGE_GREATER), MC_(AGE_LOWER),
90         MC_(NEWSGROUPS), MC_(NOT_NEWSGROUPS),
91         MC_(INREPLYTO), MC_(NOT_INREPLYTO),
92         MC_(REFERENCES), MC_(NOT_REFERENCES),
93         MC_(SCORE_GREATER), MC_(SCORE_LOWER),
94         MC_(HEADER), MC_(NOT_HEADER),
95         MC_(HEADERS_PART), MC_(NOT_HEADERS_PART),
96         MC_(MESSAGE), MC_(NOT_MESSAGE),
97         MC_(BODY_PART), MC_(NOT_BODY_PART),
98         MC_(TEST), MC_(NOT_TEST),
99         MC_(SCORE_EQUAL),
100         MC_(SIZE_GREATER), 
101         MC_(SIZE_SMALLER),
102         MC_(SIZE_EQUAL),
103         MC_(FOUND_IN_ADDRESSBOOK),MC_(NOT_FOUND_IN_ADDRESSBOOK),
104         MC_(TAG),MC_(NOT_TAG),
105         MC_(TAGGED),MC_(NOT_TAGGED),
106
107         /* match type */
108         MT_(MATCHCASE),
109         MT_(MATCH),
110         MT_(REGEXPCASE),
111         MT_(REGEXP),
112         /* actions */
113         MA_(SCORE),
114         MA_(EXECUTE),
115         MA_(MOVE),
116         MA_(COPY),
117         MA_(DELETE),
118         MA_(MARK),
119         MA_(UNMARK),
120         MA_(LOCK),
121         MA_(UNLOCK),
122         MA_(MARK_AS_READ),
123         MA_(MARK_AS_UNREAD),
124         MA_(MARK_AS_SPAM),
125         MA_(MARK_AS_HAM),
126         MA_(FORWARD),
127         MA_(FORWARD_AS_ATTACHMENT),
128         MA_(COLOR),
129         MA_(REDIRECT),
130         MA_(CHANGE_SCORE),
131         MA_(SET_SCORE),
132         MA_(STOP),
133         MA_(HIDE),
134         MA_(IGNORE),
135         MA_(WATCH),
136         MA_(ADD_TO_ADDRESSBOOK),
137         MA_(SET_TAG),
138         MA_(UNSET_TAG),
139         MA_(CLEAR_TAGS),
140         /* boolean operations */
141         MB_(OR),
142         MB_(AND)
143 };
144
145 void matcher_init(void);
146 void matcher_done(void);
147
148 const gchar *get_matchparser_tab_str    (gint id);
149 gint get_matchparser_tab_id             (const gchar *str); 
150
151 MatcherProp *matcherprop_new            (gint            criteria, 
152                                          const gchar    *header,
153                                          gint            matchtype, 
154                                          const gchar    *expr,
155                                          int             value);
156 void matcherprop_free                   (MatcherProp *prop);
157
158 MatcherProp *matcherprop_parse          (gchar  **str);
159
160 MatcherProp *matcherprop_copy           (const MatcherProp *src);
161
162 MatcherList * matcherlist_new           (GSList         *matchers, 
163                                          gboolean       bool_and);
164 void matcherlist_free                   (MatcherList    *cond);
165
166 MatcherList *matcherlist_parse          (gchar          **str);
167
168 gboolean matcherlist_match              (MatcherList    *cond, 
169                                          MsgInfo        *info);
170
171 gint matcher_parse_keyword              (gchar          **str);
172 gint matcher_parse_number               (gchar          **str);
173 gboolean matcher_parse_boolean_op       (gchar          **str);
174 gchar *matcher_parse_regexp             (gchar          **str);
175 gchar *matcher_parse_str                (gchar          **str);
176 gchar *matcherprop_to_string            (MatcherProp    *matcher);
177 gchar *matcherlist_to_string            (const MatcherList      *matchers);
178 gchar *matching_build_command           (const gchar    *cmd, 
179                                          MsgInfo        *info);
180
181 void prefs_matcher_read_config          (void);
182 void prefs_matcher_write_config         (void);
183
184 gchar * matcher_quote_str(const gchar * src);
185
186 #endif