176114bfbf2af1de1b2160a3997d6c087572517d
[claws.git] / src / filter.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2001 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 2 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, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include <glib.h>
25 #include <string.h>
26 #include <strings.h>
27 #include <stdlib.h>
28 #include <regex.h>
29
30 #include "intl.h"
31 #include "procheader.h"
32 #include "filter.h"
33 #include "folder.h"
34 #include "utils.h"
35
36 FolderItem *filter_get_dest_folder(GSList *fltlist, const gchar *file)
37 {
38         static FolderItem *dummy = NULL;
39         FolderItem *dest_folder = NULL;
40         GSList *hlist, *cur;
41         Filter *filter;
42
43         g_return_val_if_fail(file != NULL, NULL);
44         if (!fltlist) return NULL;
45
46         hlist = procheader_get_header_list_from_file(file);
47         if (!hlist) return NULL;
48
49         for (cur = fltlist; cur != NULL; cur = cur->next) {
50                 filter = (Filter *)cur->data;
51                 if (filter_match_condition(filter, hlist)) {
52                         if (filter->action == FLT_NOTRECV) {
53                                 if (!dummy) {
54                                         dummy = folder_item_new(NULL, NULL);
55                                         dummy->path = g_strdup(FILTER_NOT_RECEIVE);
56                                 }
57                                 dest_folder = dummy;
58                         } else
59                                 dest_folder = folder_find_item_from_path
60                                         (filter->dest);
61                         break;
62                 }
63         }
64
65         procheader_header_list_destroy(hlist);
66
67         return dest_folder;
68 }
69
70 static gboolean strfind(const gchar *haystack, const gchar *needle)
71 {
72         return strstr(haystack, needle) != NULL ? TRUE : FALSE;
73 }
74
75 static gboolean strnotfind(const gchar *haystack, const gchar *needle)
76 {
77         return strstr(haystack, needle) != NULL ? FALSE : TRUE;
78 }
79
80 static gboolean strcasefind(const gchar *haystack, const gchar *needle)
81 {
82         return strcasestr(haystack, needle) != NULL ? TRUE : FALSE;
83 }
84
85 static gboolean strcasenotfind(const gchar *haystack, const gchar *needle)
86 {
87         return strcasestr(haystack, needle) != NULL ? FALSE : TRUE;
88 }
89
90 static gboolean strmatch_regex(const gchar *haystack, const gchar *needle)
91 {
92         gint ret = 0;
93         regex_t preg;
94         regmatch_t pmatch[1];
95
96         ret = regcomp(&preg, needle, 0);
97         if (ret != 0) return FALSE;
98
99         ret = regexec(&preg, haystack, 1, pmatch, 0);
100         regfree(&preg);
101
102         if (ret == REG_NOMATCH) return FALSE;
103
104         if (pmatch[0].rm_so != -1)
105                 return TRUE;
106         else
107                 return FALSE;
108 }
109
110 gboolean filter_match_condition(Filter *filter, GSList *hlist)
111 {
112         Header *header;
113         gboolean (*StrFind1)    (const gchar *hs, const gchar *nd);
114         gboolean (*StrFind2)    (const gchar *hs, const gchar *nd);
115
116         g_return_val_if_fail(filter->name1 != NULL, FALSE);
117
118         if (FLT_IS_REGEX(filter->flag1))
119                 StrFind1 = strmatch_regex;
120         else if (FLT_IS_CASE_SENS(filter->flag1))
121                 StrFind1 = FLT_IS_CONTAIN(filter->flag1)
122                         ? strfind : strnotfind;
123         else
124                 StrFind1 = FLT_IS_CONTAIN(filter->flag1)
125                         ? strcasefind : strcasenotfind;
126
127         if (FLT_IS_REGEX(filter->flag2))
128                 StrFind2 = strmatch_regex;
129         if (FLT_IS_CASE_SENS(filter->flag2))
130                 StrFind2 = FLT_IS_CONTAIN(filter->flag2)
131                         ? strfind : strnotfind;
132         else
133                 StrFind2 = FLT_IS_CONTAIN(filter->flag2)
134                         ? strcasefind : strcasenotfind;
135
136         if (filter->cond == FLT_AND) {
137                 gboolean match1 = FALSE, match2 = FALSE;
138
139                 /* ignore second condition if not set */
140                 if (!filter->name2) match2 = TRUE;
141
142                 for (; hlist != NULL; hlist = hlist->next) {
143                         header = hlist->data;
144
145                         if (!match1 &&
146                             procheader_headername_equal(header->name,
147                                                         filter->name1)) {
148                                 if (!filter->body1 ||
149                                     StrFind1(header->body, filter->body1))
150                                         match1 = TRUE;
151                         }
152                         if (!match2 &&
153                             procheader_headername_equal(header->name,
154                                                          filter->name2)) {
155                                 if (!filter->body2 ||
156                                     StrFind2(header->body, filter->body2))
157                                         match2 = TRUE;
158                         }
159
160                         if (match1 && match2) return TRUE;
161                 }
162         } else if (filter->cond == FLT_OR) {
163                 for (; hlist != NULL; hlist = hlist->next) {
164                         header = hlist->data;
165
166                         if (procheader_headername_equal(header->name,
167                                                         filter->name1))
168                                 if (!filter->body1 ||
169                                     StrFind1(header->body, filter->body1))
170                                         return TRUE;
171                         if (filter->name2 &&
172                             procheader_headername_equal(header->name,
173                                                         filter->name2))
174                                 if (!filter->body2 ||
175                                     StrFind2(header->body, filter->body2))
176                                         return TRUE;
177                 }
178         }
179
180         return FALSE;
181 }
182
183 gchar *filter_get_str(Filter *filter)
184 {
185         gchar *str;
186
187         str = g_strdup_printf
188                 ("%s\t%s\t%c\t%s\t%s\t%s\t%d\t%d\t%c",
189                  filter->name1, filter->body1 ? filter->body1 : "",
190                  filter->name2 ? (filter->cond == FLT_AND ? '&' : '|') : ' ',
191                  filter->name2 ? filter->name2 : "",
192                  filter->body2 ? filter->body2 : "",
193                  filter->dest ? filter->dest : "",
194                  (guint)filter->flag1, (guint)filter->flag2,
195                  filter->action == FLT_MOVE    ? 'm' :
196                  filter->action == FLT_NOTRECV ? 'n' :
197                  filter->action == FLT_DELETE  ? 'd' : ' ');
198
199         return str;
200 }
201
202 #define PARSE_ONE_PARAM(p, srcp) \
203 { \
204         p = strchr(srcp, '\t'); \
205         if (!p) return NULL; \
206         else \
207                 *p++ = '\0'; \
208 }
209
210 Filter *filter_read_str(const gchar *str)
211 {
212         Filter *filter;
213         gchar *tmp;
214         gchar *name1, *body1, *op, *name2, *body2, *dest;
215         gchar *flag1 = NULL, *flag2 = NULL, *action = NULL;
216
217         Xstrdup_a(tmp, str, return NULL);
218
219         name1 = tmp;
220         PARSE_ONE_PARAM(body1, name1);
221         PARSE_ONE_PARAM(op, body1);
222         PARSE_ONE_PARAM(name2, op);
223         PARSE_ONE_PARAM(body2, name2);
224         PARSE_ONE_PARAM(dest, body2);
225         if (strchr(dest, '\t')) {
226                 gchar *p;
227
228                 PARSE_ONE_PARAM(flag1, dest);
229                 PARSE_ONE_PARAM(flag2, flag1);
230                 PARSE_ONE_PARAM(action, flag2);
231                 if ((p = strchr(action, '\t'))) *p = '\0';
232         }
233
234         filter = g_new0(Filter, 1);
235         filter->name1 = *name1 ? g_strdup(name1) : NULL;
236         filter->body1 = *body1 ? g_strdup(body1) : NULL;
237         filter->name2 = *name2 ? g_strdup(name2) : NULL;
238         filter->body2 = *body2 ? g_strdup(body2) : NULL;
239         filter->cond = (*op == '|') ? FLT_OR : FLT_AND;
240         filter->dest = *dest ? g_strdup(dest) : NULL;
241
242         filter->flag1 = FLT_CONTAIN;
243         filter->flag2 = FLT_CONTAIN;
244         if (flag1) filter->flag1 = (FilterFlag)strtoul(flag1, NULL, 10);
245         if (flag2) filter->flag2 = (FilterFlag)strtoul(flag2, NULL, 10);
246
247         if (!strcmp2(dest, FILTER_NOT_RECEIVE))
248                 filter->action = FLT_NOTRECV;
249         else
250                 filter->action = FLT_MOVE;
251         if (action) {
252                 switch (*action) {
253                 case 'm': filter->action = FLT_MOVE;    break;
254                 case 'n': filter->action = FLT_NOTRECV; break;
255                 case 'd': filter->action = FLT_DELETE;  break;
256                 default:  g_warning("Invalid action: `%c'\n", *action);
257                 }
258         }
259
260         return filter;
261 }
262
263 void filter_free(Filter *filter)
264 {
265         if (!filter) return;
266
267         g_free(filter->name1);
268         g_free(filter->body1);
269
270         g_free(filter->name2);
271         g_free(filter->body2);
272
273         g_free(filter->dest);
274
275         g_free(filter);
276 }