1 /*
2  * LibSylph -- E-Mail client library
3  * Copyright (C) 1999-2010 Hiroyuki Yamamoto
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library 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 GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18  */
19 
20 #ifndef __FILTER_H__
21 #define __FILTER_H__
22 
23 #include <glib.h>
24 
25 #include "folder.h"
26 #include "procmsg.h"
27 #include "utils.h"
28 
29 typedef struct _FilterCond	FilterCond;
30 typedef struct _FilterAction	FilterAction;
31 typedef struct _FilterRule	FilterRule;
32 typedef struct _FilterInfo	FilterInfo;
33 
34 typedef enum
35 {
36 	FLT_TIMING_ANY,
37 	FLT_TIMING_ON_RECEIVE,
38 	FLT_TIMING_MANUAL
39 } FilterTiming;
40 
41 typedef enum
42 {
43 	FLT_COND_HEADER,
44 	FLT_COND_ANY_HEADER,
45 	FLT_COND_TO_OR_CC,
46 	FLT_COND_BODY,
47 	FLT_COND_CMD_TEST,
48 	FLT_COND_SIZE_GREATER,
49 	FLT_COND_AGE_GREATER,
50 	FLT_COND_UNREAD,
51 	FLT_COND_MARK,
52 	FLT_COND_COLOR_LABEL,
53 	FLT_COND_MIME,
54 	FLT_COND_ACCOUNT
55 } FilterCondType;
56 
57 typedef enum
58 {
59 	FLT_CONTAIN,
60 	FLT_EQUAL,
61 	FLT_REGEX,
62 	FLT_IN_ADDRESSBOOK
63 } FilterMatchType;
64 
65 typedef enum
66 {
67 	FLT_NOT_MATCH	= 1 << 0,
68 	FLT_CASE_SENS	= 1 << 1
69 } FilterMatchFlag;
70 
71 typedef enum
72 {
73 	FLT_OR,
74 	FLT_AND
75 } FilterBoolOp;
76 
77 typedef enum
78 {
79 	FLT_ACTION_MOVE,
80 	FLT_ACTION_COPY,
81 	FLT_ACTION_NOT_RECEIVE,
82 	FLT_ACTION_DELETE,
83 	FLT_ACTION_EXEC,
84 	FLT_ACTION_EXEC_ASYNC,
85 	FLT_ACTION_MARK,
86 	FLT_ACTION_COLOR_LABEL,
87 	FLT_ACTION_MARK_READ,
88 	FLT_ACTION_FORWARD,
89 	FLT_ACTION_FORWARD_AS_ATTACHMENT,
90 	FLT_ACTION_REDIRECT,
91 	FLT_ACTION_STOP_EVAL,
92 	FLT_ACTION_NONE
93 } FilterActionType;
94 
95 typedef enum
96 {
97 	FLT_BY_NONE,
98 	FLT_BY_AUTO,
99 	FLT_BY_FROM,
100 	FLT_BY_TO,
101 	FLT_BY_SUBJECT
102 } FilterCreateType;
103 
104 typedef enum
105 {
106 	FLT_ERROR_OK,
107 	FLT_ERROR_ERROR,
108 	FLT_ERROR_EXEC_FAILED
109 } FilterErrorValue;
110 
111 #define FLT_IS_NOT_MATCH(flag)	((flag & FLT_NOT_MATCH) != 0)
112 #define FLT_IS_CASE_SENS(flag)	((flag & FLT_CASE_SENS) != 0)
113 
114 typedef gboolean (*FilterInAddressBookFunc)	(const gchar	*address);
115 
116 struct _FilterCond
117 {
118 	FilterCondType type;
119 
120 	gchar *header_name;
121 
122 	gchar *str_value;
123 	gint int_value;
124 
125 	FilterMatchType match_type;
126 	FilterMatchFlag match_flag;
127 
128 	StrFindFunc match_func;
129 };
130 
131 struct _FilterAction
132 {
133 	FilterActionType type;
134 
135 	gchar *str_value;
136 	gint int_value;
137 };
138 
139 struct _FilterRule
140 {
141 	gchar *name;
142 
143 	FilterBoolOp bool_op;
144 
145 	GSList *cond_list;
146 	GSList *action_list;
147 
148 	FilterTiming timing;
149 	gboolean enabled;
150 
151 	gchar *target_folder;
152 	gboolean recursive;
153 };
154 
155 struct _FilterInfo
156 {
157 	PrefsAccount *account;
158 	MsgFlags flags;
159 
160 	gboolean actions[FLT_ACTION_NONE];
161 	GSList *dest_list;
162 	FolderItem *move_dest;
163 	gboolean drop_done;
164 
165 	FilterErrorValue error;
166 	gint last_exec_exit_status;
167 };
168 
169 gint filter_apply			(GSList			*fltlist,
170 					 const gchar		*file,
171 					 FilterInfo		*fltinfo);
172 gint filter_apply_msginfo		(GSList			*fltlist,
173 					 MsgInfo		*msginfo,
174 					 FilterInfo		*fltinfo);
175 
176 gint filter_action_exec			(FilterRule		*rule,
177 					 MsgInfo		*msginfo,
178 					 const gchar		*file,
179 					 FilterInfo		*fltinfo);
180 
181 gboolean filter_match_rule		(FilterRule		*rule,
182 					 MsgInfo		*msginfo,
183 					 GSList			*hlist,
184 					 FilterInfo		*fltinfo);
185 
186 gboolean filter_rule_requires_full_headers	(FilterRule	*rule);
187 
188 /* read / write config */
189 GSList *filter_xml_node_to_filter_list	(GNode			*node);
190 GSList *filter_read_file		(const gchar		*file);
191 void filter_read_config			(void);
192 void filter_write_file			(GSList			*list,
193 					 const gchar		*file);
194 void filter_write_config		(void);
195 
196 /* for old filterrc */
197 gchar *filter_get_str			(FilterRule		*rule);
198 FilterRule *filter_read_str		(const gchar		*str);
199 
200 void filter_set_addressbook_func	(FilterInAddressBookFunc func);
201 FilterInAddressBookFunc filter_get_addressbook_func
202 					(void);
203 
204 FilterRule *filter_rule_new		(const gchar		*name,
205 					 FilterBoolOp		 bool_op,
206 					 GSList			*cond_list,
207 					 GSList			*action_list);
208 FilterCond *filter_cond_new		(FilterCondType		 type,
209 					 FilterMatchType	 match_type,
210 					 FilterMatchFlag	 match_flag,
211 					 const gchar		*header,
212 					 const gchar		*value);
213 FilterAction *filter_action_new		(FilterActionType	 type,
214 					 const gchar		*str);
215 FilterInfo *filter_info_new		(void);
216 
217 FilterRule *filter_junk_rule_create	(PrefsAccount		*account,
218 					 FolderItem		*default_junk,
219 					 gboolean		 is_manual);
220 
221 void filter_rule_rename_dest_path	(FilterRule		*rule,
222 					 const gchar		*old_path,
223 					 const gchar		*new_path);
224 void filter_rule_delete_action_by_dest_path
225 					(FilterRule		*rule,
226 					 const gchar		*path);
227 
228 void filter_list_rename_path		(const gchar		*old_path,
229 					 const gchar		*new_path);
230 void filter_list_delete_path		(const gchar		*path);
231 
232 void filter_rule_match_type_str_to_enum	(const gchar		*type_str,
233 					 FilterMatchType	*type,
234 					 FilterMatchFlag	*flag);
235 
236 void filter_get_keyword_from_msg	(MsgInfo		*msginfo,
237 					 gchar		       **header,
238 					 gchar		       **key,
239 					 FilterCreateType	 type);
240 
241 void filter_rule_list_free		(GSList			*fltlist);
242 void filter_rule_free			(FilterRule		*rule);
243 void filter_cond_list_free		(GSList			*cond_list);
244 void filter_action_list_free		(GSList			*action_list);
245 void filter_info_free			(FilterInfo		*info);
246 
247 #endif /* __FILTER_H__ */
248