Statistics
| Revision:

root / src / filter.c @ 365

History | View | Annotate | Download (30 kB)

1 1 hiro
/*
2 1 hiro
 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 1 hiro
 * Copyright (C) 1999-2004 Hiroyuki Yamamoto
4 1 hiro
 *
5 1 hiro
 * This program is free software; you can redistribute it and/or modify
6 1 hiro
 * it under the terms of the GNU General Public License as published by
7 1 hiro
 * the Free Software Foundation; either version 2 of the License, or
8 1 hiro
 * (at your option) any later version.
9 1 hiro
 *
10 1 hiro
 * This program is distributed in the hope that it will be useful,
11 1 hiro
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 1 hiro
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 1 hiro
 * GNU General Public License for more details.
14 1 hiro
 *
15 1 hiro
 * You should have received a copy of the GNU General Public License
16 1 hiro
 * along with this program; if not, write to the Free Software
17 1 hiro
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 1 hiro
 */
19 1 hiro
20 1 hiro
#ifdef HAVE_CONFIG_H
21 1 hiro
#  include "config.h"
22 1 hiro
#endif
23 1 hiro
24 1 hiro
#include "defs.h"
25 1 hiro
26 1 hiro
#include <glib.h>
27 92 hiro
#include <glib/gi18n.h>
28 1 hiro
#include <string.h>
29 1 hiro
#include <strings.h>
30 1 hiro
#include <stdlib.h>
31 1 hiro
#include <sys/types.h>
32 1 hiro
#include <regex.h>
33 1 hiro
#include <time.h>
34 1 hiro
35 1 hiro
#include "procheader.h"
36 1 hiro
#include "filter.h"
37 1 hiro
#include "folder.h"
38 1 hiro
#include "utils.h"
39 1 hiro
#include "xml.h"
40 1 hiro
#include "prefs.h"
41 1 hiro
#include "prefs_account.h"
42 1 hiro
43 1 hiro
typedef enum
44 1 hiro
{
45 1 hiro
        FLT_O_CONTAIN        = 1 << 0,
46 1 hiro
        FLT_O_CASE_SENS        = 1 << 1,
47 1 hiro
        FLT_O_REGEX        = 1 << 2
48 1 hiro
} FilterOldFlag;
49 1 hiro
50 1 hiro
static gboolean filter_match_cond        (FilterCond        *cond,
51 1 hiro
                                         MsgInfo        *msginfo,
52 1 hiro
                                         GSList                *hlist,
53 1 hiro
                                         FilterInfo        *fltinfo);
54 1 hiro
static gboolean filter_match_header_cond(FilterCond        *cond,
55 1 hiro
                                         GSList                *hlist);
56 1 hiro
57 1 hiro
static void filter_cond_free                (FilterCond        *cond);
58 1 hiro
static void filter_action_free                (FilterAction        *action);
59 1 hiro
60 1 hiro
61 1 hiro
gint filter_apply(GSList *fltlist, const gchar *file, FilterInfo *fltinfo)
62 1 hiro
{
63 1 hiro
        MsgInfo *msginfo;
64 1 hiro
        gint ret = 0;
65 1 hiro
66 1 hiro
        g_return_val_if_fail(file != NULL, -1);
67 1 hiro
        g_return_val_if_fail(fltinfo != NULL, -1);
68 1 hiro
69 1 hiro
        if (!fltlist) return 0;
70 1 hiro
71 1 hiro
        msginfo = procheader_parse_file(file, fltinfo->flags, FALSE);
72 1 hiro
        if (!msginfo) return 0;
73 1 hiro
        msginfo->file_path = g_strdup(file);
74 1 hiro
75 1 hiro
        ret = filter_apply_msginfo(fltlist, msginfo, fltinfo);
76 1 hiro
77 1 hiro
        procmsg_msginfo_free(msginfo);
78 1 hiro
79 1 hiro
        return ret;
80 1 hiro
}
81 1 hiro
82 1 hiro
gint filter_apply_msginfo(GSList *fltlist, MsgInfo *msginfo,
83 1 hiro
                          FilterInfo *fltinfo)
84 1 hiro
{
85 1 hiro
        gchar *file;
86 1 hiro
        GSList *hlist, *cur;
87 1 hiro
        FilterRule *rule;
88 1 hiro
        gint ret = 0;
89 1 hiro
90 1 hiro
        g_return_val_if_fail(msginfo != NULL, -1);
91 1 hiro
        g_return_val_if_fail(fltinfo != NULL, -1);
92 1 hiro
93 1 hiro
        if (!fltlist) return 0;
94 1 hiro
95 1 hiro
        file = procmsg_get_message_file(msginfo);
96 1 hiro
        hlist = procheader_get_header_list_from_file(file);
97 1 hiro
        if (!hlist) {
98 1 hiro
                g_free(file);
99 1 hiro
                return 0;
100 1 hiro
        }
101 1 hiro
102 1 hiro
        for (cur = fltlist; cur != NULL; cur = cur->next) {
103 1 hiro
                rule = (FilterRule *)cur->data;
104 1 hiro
                if (!rule->enabled) continue;
105 1 hiro
                if (filter_match_rule(rule, msginfo, hlist, fltinfo)) {
106 1 hiro
                        ret = filter_action_exec(rule, msginfo, file, fltinfo);
107 1 hiro
                        if (ret < 0) {
108 1 hiro
                                g_warning("filter_action_exec() returned error\n");
109 1 hiro
                                break;
110 1 hiro
                        }
111 1 hiro
                        if (fltinfo->drop_done == TRUE ||
112 1 hiro
                            fltinfo->actions[FLT_ACTION_STOP_EVAL] == TRUE)
113 1 hiro
                                break;
114 1 hiro
                }
115 1 hiro
        }
116 1 hiro
117 1 hiro
        procheader_header_list_destroy(hlist);
118 1 hiro
        g_free(file);
119 1 hiro
120 1 hiro
        return ret;
121 1 hiro
}
122 1 hiro
123 1 hiro
gint filter_action_exec(FilterRule *rule, MsgInfo *msginfo, const gchar *file,
124 1 hiro
                        FilterInfo *fltinfo)
125 1 hiro
{
126 1 hiro
        FolderItem *dest_folder = NULL;
127 1 hiro
        FilterAction *action;
128 1 hiro
        GSList *cur;
129 1 hiro
        gchar *cmdline;
130 1 hiro
        gboolean copy_to_self = FALSE;
131 1 hiro
132 1 hiro
        g_return_val_if_fail(rule != NULL, -1);
133 1 hiro
        g_return_val_if_fail(msginfo != NULL, -1);
134 1 hiro
        g_return_val_if_fail(file != NULL, -1);
135 1 hiro
        g_return_val_if_fail(fltinfo != NULL, -1);
136 1 hiro
137 1 hiro
        for (cur = rule->action_list; cur != NULL; cur = cur->next) {
138 1 hiro
                action = (FilterAction *)cur->data;
139 1 hiro
140 1 hiro
                switch (action->type) {
141 1 hiro
                case FLT_ACTION_MARK:
142 1 hiro
                        debug_print("filter_action_exec(): mark\n");
143 1 hiro
                        MSG_SET_PERM_FLAGS(fltinfo->flags, MSG_MARKED);
144 1 hiro
                        fltinfo->actions[action->type] = TRUE;
145 1 hiro
                        break;
146 1 hiro
                case FLT_ACTION_COLOR_LABEL:
147 1 hiro
                        debug_print("filter_action_exec(): color label: %d\n",
148 1 hiro
                                    action->int_value);
149 1 hiro
                        MSG_UNSET_PERM_FLAGS(fltinfo->flags,
150 1 hiro
                                             MSG_CLABEL_FLAG_MASK);
151 1 hiro
                        MSG_SET_COLORLABEL_VALUE(fltinfo->flags,
152 1 hiro
                                                 action->int_value);
153 1 hiro
                        fltinfo->actions[action->type] = TRUE;
154 1 hiro
                        break;
155 1 hiro
                case FLT_ACTION_MARK_READ:
156 1 hiro
                        debug_print("filter_action_exec(): mark as read\n");
157 1 hiro
                        if (msginfo->folder) {
158 1 hiro
                                if (MSG_IS_NEW(fltinfo->flags))
159 1 hiro
                                        msginfo->folder->new--;
160 1 hiro
                                if (MSG_IS_UNREAD(fltinfo->flags))
161 1 hiro
                                        msginfo->folder->unread--;
162 1 hiro
                        }
163 1 hiro
                        MSG_UNSET_PERM_FLAGS(fltinfo->flags, MSG_NEW|MSG_UNREAD);
164 1 hiro
                        fltinfo->actions[action->type] = TRUE;
165 1 hiro
                        break;
166 1 hiro
                case FLT_ACTION_EXEC:
167 1 hiro
                        cmdline = g_strconcat(action->str_value, " ", file,
168 1 hiro
                                              NULL);
169 1 hiro
                        execute_command_line(cmdline, FALSE);
170 1 hiro
                        g_free(cmdline);
171 1 hiro
                        fltinfo->actions[action->type] = TRUE;
172 1 hiro
                        break;
173 1 hiro
                case FLT_ACTION_EXEC_ASYNC:
174 1 hiro
                        cmdline = g_strconcat(action->str_value, " ", file,
175 1 hiro
                                              NULL);
176 1 hiro
                        execute_command_line(cmdline, TRUE);
177 1 hiro
                        g_free(cmdline);
178 1 hiro
                        fltinfo->actions[action->type] = TRUE;
179 1 hiro
                        break;
180 1 hiro
                default:
181 1 hiro
                        break;
182 1 hiro
                }
183 1 hiro
        }
184 1 hiro
185 1 hiro
        for (cur = rule->action_list; cur != NULL; cur = cur->next) {
186 1 hiro
                action = (FilterAction *)cur->data;
187 1 hiro
188 1 hiro
                switch (action->type) {
189 1 hiro
                case FLT_ACTION_MOVE:
190 1 hiro
                case FLT_ACTION_COPY:
191 1 hiro
                        dest_folder = folder_find_item_from_identifier
192 1 hiro
                                (action->str_value);
193 1 hiro
                        if (!dest_folder) {
194 1 hiro
                                g_warning("dest folder '%s' not found\n",
195 1 hiro
                                          action->str_value);
196 1 hiro
                                return -1;
197 1 hiro
                        }
198 1 hiro
                        debug_print("filter_action_exec(): %s: dest_folder = %s\n",
199 1 hiro
                                    action->type == FLT_ACTION_COPY ?
200 1 hiro
                                    "copy" : "move", action->str_value);
201 1 hiro
202 1 hiro
                        if (msginfo->folder) {
203 1 hiro
                                gint val;
204 1 hiro
205 1 hiro
                                /* local filtering */
206 1 hiro
                                if (msginfo->folder == dest_folder)
207 1 hiro
                                        copy_to_self = TRUE;
208 1 hiro
                                else {
209 1 hiro
                                        if (action->type == FLT_ACTION_COPY) {
210 1 hiro
                                                val = folder_item_copy_msg
211 1 hiro
                                                        (dest_folder, msginfo);
212 1 hiro
                                                if (val == -1)
213 1 hiro
                                                        return -1;
214 1 hiro
                                        }
215 1 hiro
                                        fltinfo->actions[action->type] = TRUE;
216 1 hiro
                                }
217 1 hiro
                        } else {
218 1 hiro
                                if (folder_item_add_msg(dest_folder, file,
219 1 hiro
                                                        &fltinfo->flags,
220 1 hiro
                                                        FALSE) < 0)
221 1 hiro
                                        return -1;
222 1 hiro
                                fltinfo->actions[action->type] = TRUE;
223 1 hiro
                        }
224 1 hiro
225 1 hiro
                        fltinfo->dest_list = g_slist_append(fltinfo->dest_list,
226 1 hiro
                                                            dest_folder);
227 1 hiro
                        if (action->type == FLT_ACTION_MOVE) {
228 1 hiro
                                fltinfo->move_dest = dest_folder;
229 1 hiro
                                fltinfo->drop_done = TRUE;
230 1 hiro
                        }
231 1 hiro
                        break;
232 1 hiro
                default:
233 1 hiro
                        break;
234 1 hiro
                }
235 1 hiro
        }
236 1 hiro
237 1 hiro
        if (fltinfo->drop_done == TRUE)
238 1 hiro
                return 0;
239 1 hiro
240 1 hiro
        for (cur = rule->action_list; cur != NULL; cur = cur->next) {
241 1 hiro
                action = (FilterAction *)cur->data;
242 1 hiro
243 1 hiro
                switch (action->type) {
244 1 hiro
                case FLT_ACTION_NOT_RECEIVE:
245 1 hiro
                        debug_print("filter_action_exec(): don't receive\n");
246 1 hiro
                        fltinfo->drop_done = TRUE;
247 1 hiro
                        fltinfo->actions[action->type] = TRUE;
248 1 hiro
                        return 0;
249 1 hiro
                case FLT_ACTION_DELETE:
250 1 hiro
                        debug_print("filter_action_exec(): delete\n");
251 1 hiro
                        if (msginfo->folder) {
252 1 hiro
                                /* local filtering */
253 1 hiro
                                if (copy_to_self == FALSE)
254 1 hiro
                                        fltinfo->actions[action->type] = TRUE;
255 1 hiro
                        } else
256 1 hiro
                                fltinfo->actions[action->type] = TRUE;
257 1 hiro
258 1 hiro
                        fltinfo->drop_done = TRUE;
259 1 hiro
                        return 0;
260 1 hiro
                case FLT_ACTION_STOP_EVAL:
261 1 hiro
                        debug_print("filter_action_exec(): stop evaluation\n");
262 1 hiro
                        fltinfo->actions[action->type] = TRUE;
263 1 hiro
                        return 0;
264 1 hiro
                default:
265 1 hiro
                        break;
266 1 hiro
                }
267 1 hiro
        }
268 1 hiro
269 1 hiro
        return 0;
270 1 hiro
}
271 1 hiro
272 1 hiro
static gboolean strmatch_regex(const gchar *haystack, const gchar *needle)
273 1 hiro
{
274 1 hiro
        gint ret = 0;
275 1 hiro
        regex_t preg;
276 1 hiro
        regmatch_t pmatch[1];
277 1 hiro
278 1 hiro
        ret = regcomp(&preg, needle, REG_EXTENDED|REG_ICASE);
279 1 hiro
        if (ret != 0) return FALSE;
280 1 hiro
281 1 hiro
        ret = regexec(&preg, haystack, 1, pmatch, 0);
282 1 hiro
        regfree(&preg);
283 1 hiro
284 1 hiro
        if (ret == REG_NOMATCH) return FALSE;
285 1 hiro
286 1 hiro
        if (pmatch[0].rm_so != -1)
287 1 hiro
                return TRUE;
288 1 hiro
        else
289 1 hiro
                return FALSE;
290 1 hiro
}
291 1 hiro
292 1 hiro
gboolean filter_match_rule(FilterRule *rule, MsgInfo *msginfo, GSList *hlist,
293 1 hiro
                           FilterInfo *fltinfo)
294 1 hiro
{
295 1 hiro
        FilterCond *cond;
296 1 hiro
        GSList *cur;
297 1 hiro
        gboolean matched;
298 1 hiro
299 1 hiro
        g_return_val_if_fail(rule->cond_list != NULL, FALSE);
300 1 hiro
        g_return_val_if_fail(rule->action_list != NULL, FALSE);
301 1 hiro
302 1 hiro
        switch (rule->timing) {
303 1 hiro
        case FLT_TIMING_ANY:
304 1 hiro
                break;
305 1 hiro
        case FLT_TIMING_ON_RECEIVE:
306 1 hiro
                if (msginfo->folder != NULL)
307 1 hiro
                        return FALSE;
308 1 hiro
                break;
309 1 hiro
        case FLT_TIMING_MANUAL:
310 1 hiro
                if (msginfo->folder == NULL)
311 1 hiro
                        return FALSE;
312 1 hiro
                break;
313 1 hiro
        default:
314 1 hiro
                break;
315 1 hiro
        }
316 1 hiro
317 1 hiro
        if (rule->bool_op == FLT_AND) {
318 1 hiro
                for (cur = rule->cond_list; cur != NULL; cur = cur->next) {
319 1 hiro
                        cond = (FilterCond *)cur->data;
320 1 hiro
                        matched = filter_match_cond(cond, msginfo, hlist,
321 1 hiro
                                                    fltinfo);
322 1 hiro
                        if (matched == FALSE)
323 1 hiro
                                return FALSE;
324 1 hiro
                }
325 1 hiro
326 1 hiro
                return TRUE;
327 1 hiro
        } else if (rule->bool_op == FLT_OR) {
328 1 hiro
                for (cur = rule->cond_list; cur != NULL; cur = cur->next) {
329 1 hiro
                        cond = (FilterCond *)cur->data;
330 1 hiro
                        matched = filter_match_cond(cond, msginfo, hlist,
331 1 hiro
                                                    fltinfo);
332 1 hiro
                        if (matched == TRUE)
333 1 hiro
                                return TRUE;
334 1 hiro
                }
335 1 hiro
336 1 hiro
                return FALSE;
337 1 hiro
        }
338 1 hiro
339 1 hiro
        return FALSE;
340 1 hiro
}
341 1 hiro
342 1 hiro
static gboolean filter_match_cond(FilterCond *cond, MsgInfo *msginfo,
343 1 hiro
                                  GSList *hlist, FilterInfo *fltinfo)
344 1 hiro
{
345 1 hiro
        gboolean matched = FALSE;
346 1 hiro
        gchar *file;
347 1 hiro
        gchar *cmdline;
348 1 hiro
        PrefsAccount *cond_ac;
349 1 hiro
350 1 hiro
        switch (cond->type) {
351 1 hiro
        case FLT_COND_HEADER:
352 1 hiro
        case FLT_COND_ANY_HEADER:
353 1 hiro
        case FLT_COND_TO_OR_CC:
354 1 hiro
                return filter_match_header_cond(cond, hlist);
355 1 hiro
        case FLT_COND_BODY:
356 1 hiro
                matched = procmime_find_string(msginfo, cond->str_value,
357 1 hiro
                                               cond->match_func);
358 1 hiro
                break;
359 1 hiro
        case FLT_COND_CMD_TEST:
360 1 hiro
                file = procmsg_get_message_file(msginfo);
361 1 hiro
                cmdline = g_strconcat(cond->str_value, " ", file, NULL);
362 1 hiro
                matched = (execute_command_line(cmdline, FALSE) == 0);
363 1 hiro
                g_free(cmdline);
364 1 hiro
                g_free(file);
365 1 hiro
                break;
366 1 hiro
        case FLT_COND_SIZE_GREATER:
367 1 hiro
                matched = (msginfo->size > cond->int_value * 1024);
368 1 hiro
                break;
369 1 hiro
        case FLT_COND_AGE_GREATER:
370 1 hiro
                matched = (time(NULL) - msginfo->date_t >
371 1 hiro
                                cond->int_value * 24 * 60 * 60);
372 1 hiro
                break;
373 1 hiro
        case FLT_COND_ACCOUNT:
374 1 hiro
                cond_ac = account_find_from_id(cond->int_value);
375 1 hiro
                matched = (cond_ac != NULL && cond_ac == fltinfo->account);
376 1 hiro
                break;
377 1 hiro
        default:
378 1 hiro
                g_warning("filter_match_cond(): unknown condition: %d\n",
379 1 hiro
                          cond->type);
380 1 hiro
                return FALSE;
381 1 hiro
        }
382 1 hiro
383 1 hiro
        if (FLT_IS_NOT_MATCH(cond->match_flag))
384 1 hiro
                matched = !matched;
385 1 hiro
386 1 hiro
        return matched;
387 1 hiro
}
388 1 hiro
389 1 hiro
static gboolean filter_match_header_cond(FilterCond *cond, GSList *hlist)
390 1 hiro
{
391 1 hiro
        gboolean matched = FALSE;
392 1 hiro
        GSList *cur;
393 1 hiro
        Header *header;
394 1 hiro
395 1 hiro
        for (cur = hlist; cur != NULL; cur = cur->next) {
396 1 hiro
                header = (Header *)cur->data;
397 1 hiro
398 1 hiro
                switch (cond->type) {
399 1 hiro
                case FLT_COND_HEADER:
400 333 hiro
                        if (!g_ascii_strcasecmp
401 333 hiro
                                (header->name, cond->header_name)) {
402 1 hiro
                                if (!cond->str_value ||
403 1 hiro
                                    cond->match_func(header->body,
404 1 hiro
                                                     cond->str_value))
405 1 hiro
                                        matched = TRUE;
406 1 hiro
                        }
407 1 hiro
                        break;
408 1 hiro
                case FLT_COND_ANY_HEADER:
409 1 hiro
                        if (!cond->str_value ||
410 1 hiro
                            cond->match_func(header->body, cond->str_value))
411 1 hiro
                                matched = TRUE;
412 1 hiro
                        break;
413 1 hiro
                case FLT_COND_TO_OR_CC:
414 333 hiro
                        if (!g_ascii_strcasecmp(header->name, "To") ||
415 333 hiro
                            !g_ascii_strcasecmp(header->name, "Cc")) {
416 1 hiro
                                if (!cond->str_value ||
417 1 hiro
                                    cond->match_func(header->body,
418 1 hiro
                                                     cond->str_value))
419 1 hiro
                                        matched = TRUE;
420 1 hiro
                        }
421 1 hiro
                        break;
422 1 hiro
                default:
423 1 hiro
                        break;
424 1 hiro
                }
425 1 hiro
426 1 hiro
                if (matched == TRUE)
427 1 hiro
                        break;
428 1 hiro
        }
429 1 hiro
430 1 hiro
        if (FLT_IS_NOT_MATCH(cond->match_flag))
431 1 hiro
                matched = !matched;
432 1 hiro
433 1 hiro
        return matched;
434 1 hiro
}
435 1 hiro
436 1 hiro
#define RETURN_IF_TAG_NOT_MATCH(tag_name)                        \
437 1 hiro
        if (strcmp2(xmlnode->tag->tag, tag_name) != 0) {        \
438 1 hiro
                g_warning("tag name != \"" tag_name "\"\n");        \
439 1 hiro
                filter_cond_list_free(cond_list);                \
440 1 hiro
                filter_action_list_free(cond_list);                \
441 1 hiro
                return FALSE;                                        \
442 1 hiro
        }                                                        \
443 1 hiro
444 1 hiro
#define STR_SWITCH(sw)                { const gchar *sw_str = sw;
445 1 hiro
#define STR_CASE_BEGIN(str)        if (!strcmp(sw_str, str)) {
446 1 hiro
#define STR_CASE(str)                } else if (!strcmp(sw_str, str)) {
447 1 hiro
#define STR_CASE_END                } }
448 1 hiro
449 1 hiro
static gboolean filter_xml_node_func(GNode *node, gpointer data)
450 1 hiro
{
451 1 hiro
        XMLNode *xmlnode;
452 1 hiro
        GList *list;
453 1 hiro
        const gchar *name = NULL;
454 1 hiro
        FilterTiming timing = FLT_TIMING_ANY;
455 1 hiro
        gboolean enabled = TRUE;
456 1 hiro
        FilterRule *rule;
457 1 hiro
        FilterBoolOp bool_op = FLT_OR;
458 1 hiro
        GSList *cond_list = NULL;
459 1 hiro
        GSList *action_list = NULL;
460 1 hiro
        GNode *child, *cond_child, *action_child;
461 1 hiro
        GSList **fltlist = (GSList **)data;
462 1 hiro
463 1 hiro
        if (g_node_depth(node) != 2) return FALSE;
464 1 hiro
        g_return_val_if_fail(node->data != NULL, FALSE);
465 1 hiro
466 1 hiro
        xmlnode = node->data;
467 1 hiro
        RETURN_IF_TAG_NOT_MATCH("rule");
468 1 hiro
469 1 hiro
        for (list = xmlnode->tag->attr; list != NULL; list = list->next) {
470 1 hiro
                XMLAttr *attr = (XMLAttr *)list->data;
471 1 hiro
472 1 hiro
                if (!attr || !attr->name || !attr->value) continue;
473 1 hiro
                if (!strcmp(attr->name, "name"))
474 1 hiro
                        name = attr->value;
475 1 hiro
                else if (!strcmp(attr->name, "timing")) {
476 1 hiro
                        if (!strcmp(attr->value, "any"))
477 1 hiro
                                timing = FLT_TIMING_ANY;
478 1 hiro
                        else if (!strcmp(attr->value, "receive"))
479 1 hiro
                                timing = FLT_TIMING_ON_RECEIVE;
480 1 hiro
                        else if (!strcmp(attr->value, "manual"))
481 1 hiro
                                timing = FLT_TIMING_MANUAL;
482 1 hiro
                } else if (!strcmp(attr->name, "enabled")) {
483 1 hiro
                        if (!strcmp(attr->value, "true"))
484 1 hiro
                                enabled = TRUE;
485 1 hiro
                        else
486 1 hiro
                                enabled = FALSE;
487 1 hiro
                }
488 1 hiro
        }
489 1 hiro
490 1 hiro
        /* condition list */
491 1 hiro
        child = node->children;
492 1 hiro
        if (!child) {
493 1 hiro
                g_warning("<rule> must have children\n");
494 1 hiro
                return FALSE;
495 1 hiro
        }
496 1 hiro
        xmlnode = child->data;
497 1 hiro
        RETURN_IF_TAG_NOT_MATCH("condition-list");
498 1 hiro
        for (list = xmlnode->tag->attr; list != NULL; list = list->next) {
499 1 hiro
                XMLAttr *attr = (XMLAttr *)list->data;
500 1 hiro
501 1 hiro
                if (attr && attr->name && attr->value &&
502 1 hiro
                    !strcmp(attr->name, "bool")) {
503 1 hiro
                        if (!strcmp(attr->value, "or"))
504 1 hiro
                                bool_op = FLT_OR;
505 1 hiro
                        else
506 1 hiro
                                bool_op = FLT_AND;
507 1 hiro
                }
508 1 hiro
        }
509 1 hiro
510 1 hiro
        for (cond_child = child->children; cond_child != NULL;
511 1 hiro
             cond_child = cond_child->next) {
512 1 hiro
                const gchar *type = NULL;
513 1 hiro
                const gchar *name = NULL;
514 1 hiro
                const gchar *value = NULL;
515 1 hiro
                FilterCond *cond;
516 1 hiro
                FilterCondType cond_type = FLT_COND_HEADER;
517 1 hiro
                FilterMatchType match_type = FLT_CONTAIN;
518 1 hiro
                FilterMatchFlag match_flag = 0;
519 1 hiro
520 1 hiro
                xmlnode = cond_child->data;
521 1 hiro
                if (!xmlnode || !xmlnode->tag || !xmlnode->tag->tag) continue;
522 1 hiro
523 1 hiro
                for (list = xmlnode->tag->attr; list != NULL; list = list->next) {
524 1 hiro
                        XMLAttr *attr = (XMLAttr *)list->data;
525 1 hiro
526 1 hiro
                        if (!attr || !attr->name || !attr->value) continue;
527 1 hiro
                        if (!strcmp(attr->name, "type"))
528 1 hiro
                                type = attr->value;
529 1 hiro
                        else if (!strcmp(attr->name, "name"))
530 1 hiro
                                name = attr->value;
531 1 hiro
                }
532 1 hiro
533 1 hiro
                if (type) {
534 1 hiro
                        filter_rule_match_type_str_to_enum
535 1 hiro
                                (type, &match_type, &match_flag);
536 1 hiro
                }
537 1 hiro
                value = xmlnode->element;
538 1 hiro
539 1 hiro
                STR_SWITCH(xmlnode->tag->tag)
540 1 hiro
                STR_CASE_BEGIN("match-header")
541 1 hiro
                        cond_type = FLT_COND_HEADER;
542 1 hiro
                STR_CASE("match-any-header")
543 1 hiro
                        cond_type = FLT_COND_ANY_HEADER;
544 1 hiro
                STR_CASE("match-to-or-cc")
545 1 hiro
                        cond_type = FLT_COND_TO_OR_CC;
546 1 hiro
                STR_CASE("match-body-text")
547 1 hiro
                        cond_type = FLT_COND_BODY;
548 1 hiro
                STR_CASE("command-test")
549 1 hiro
                        cond_type = FLT_COND_CMD_TEST;
550 1 hiro
                STR_CASE("size")
551 1 hiro
                        cond_type = FLT_COND_SIZE_GREATER;
552 1 hiro
                STR_CASE("age")
553 1 hiro
                        cond_type = FLT_COND_AGE_GREATER;
554 1 hiro
                STR_CASE("account-id")
555 1 hiro
                        cond_type = FLT_COND_ACCOUNT;
556 1 hiro
                STR_CASE_END
557 1 hiro
558 1 hiro
                cond = filter_cond_new(cond_type, match_type, match_flag,
559 1 hiro
                                       name, value);
560 1 hiro
                cond_list = g_slist_append(cond_list, cond);
561 1 hiro
        }
562 1 hiro
563 1 hiro
        /* action list */
564 1 hiro
        child = child->next;
565 1 hiro
        if (!child) {
566 1 hiro
                g_warning("<rule> must have multiple children\n");
567 1 hiro
                filter_cond_list_free(cond_list);
568 1 hiro
                return FALSE;
569 1 hiro
        }
570 1 hiro
        xmlnode = child->data;
571 1 hiro
        RETURN_IF_TAG_NOT_MATCH("action-list");
572 1 hiro
573 1 hiro
        for (action_child = child->children; action_child != NULL;
574 1 hiro
             action_child = action_child->next) {
575 1 hiro
                FilterAction *action;
576 1 hiro
                FilterActionType action_type = FLT_ACTION_NONE;
577 1 hiro
578 1 hiro
                xmlnode = action_child->data;
579 1 hiro
                if (!xmlnode || !xmlnode->tag || !xmlnode->tag->tag) continue;
580 1 hiro
581 1 hiro
                STR_SWITCH(xmlnode->tag->tag)
582 1 hiro
                STR_CASE_BEGIN("move")
583 1 hiro
                        action_type = FLT_ACTION_MOVE;
584 1 hiro
                STR_CASE("copy")
585 1 hiro
                        action_type = FLT_ACTION_COPY;
586 1 hiro
                STR_CASE("not-receive")
587 1 hiro
                        action_type = FLT_ACTION_NOT_RECEIVE;
588 1 hiro
                STR_CASE("delete")
589 1 hiro
                        action_type = FLT_ACTION_DELETE;
590 1 hiro
                STR_CASE("exec")
591 1 hiro
                        action_type = FLT_ACTION_EXEC;
592 1 hiro
                STR_CASE("exec-async")
593 1 hiro
                        action_type = FLT_ACTION_EXEC_ASYNC;
594 1 hiro
                STR_CASE("mark")
595 1 hiro
                        action_type = FLT_ACTION_MARK;
596 1 hiro
                STR_CASE("color-label")
597 1 hiro
                        action_type = FLT_ACTION_COLOR_LABEL;
598 1 hiro
                STR_CASE("mark-as-read")
599 1 hiro
                        action_type = FLT_ACTION_MARK_READ;
600 1 hiro
                STR_CASE("forward")
601 1 hiro
                        action_type = FLT_ACTION_FORWARD;
602 1 hiro
                STR_CASE("forward-as-attachment")
603 1 hiro
                        action_type = FLT_ACTION_FORWARD_AS_ATTACHMENT;
604 1 hiro
                STR_CASE("redirect")
605 1 hiro
                        action_type = FLT_ACTION_REDIRECT;
606 1 hiro
                STR_CASE("stop-eval")
607 1 hiro
                        action_type = FLT_ACTION_STOP_EVAL;
608 1 hiro
                STR_CASE_END
609 1 hiro
610 1 hiro
                action = filter_action_new(action_type, xmlnode->element);
611 1 hiro
                action_list = g_slist_append(action_list, action);
612 1 hiro
        }
613 1 hiro
614 1 hiro
        if (name && cond_list && action_list) {
615 1 hiro
                rule = filter_rule_new(name, bool_op, cond_list, action_list);
616 1 hiro
                rule->timing = timing;
617 1 hiro
                rule->enabled = enabled;
618 1 hiro
                *fltlist = g_slist_prepend(*fltlist, rule);
619 1 hiro
        }
620 1 hiro
621 1 hiro
        return FALSE;
622 1 hiro
}
623 1 hiro
624 1 hiro
#undef RETURN_IF_TAG_NOT_MATCH
625 1 hiro
#undef STR_SWITCH
626 1 hiro
#undef STR_CASE_BEGIN
627 1 hiro
#undef STR_CASE
628 1 hiro
#undef STR_CASE_END
629 1 hiro
630 1 hiro
GSList *filter_xml_node_to_filter_list(GNode *node)
631 1 hiro
{
632 1 hiro
        GSList *fltlist = NULL;
633 1 hiro
634 1 hiro
        g_return_val_if_fail(node != NULL, NULL);
635 1 hiro
636 1 hiro
        g_node_traverse(node, G_PRE_ORDER, G_TRAVERSE_ALL, 2,
637 1 hiro
                        filter_xml_node_func, &fltlist);
638 1 hiro
        fltlist = g_slist_reverse(fltlist);
639 1 hiro
640 1 hiro
        return fltlist;
641 1 hiro
}
642 1 hiro
643 1 hiro
#define NODE_NEW(tag, text) \
644 1 hiro
        node = xml_node_new(xml_tag_new(tag), text)
645 1 hiro
#define ADD_ATTR(name, value) \
646 1 hiro
        xml_tag_add_attr(node->tag, xml_attr_new(name, value))
647 1 hiro
648 1 hiro
void filter_write_config(GSList *fltlist)
649 1 hiro
{
650 1 hiro
        gchar *rcpath;
651 1 hiro
        PrefFile *pfile;
652 1 hiro
        GSList *cur;
653 1 hiro
654 1 hiro
        debug_print("Writing filter configuration...\n");
655 1 hiro
656 1 hiro
        rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, FILTER_LIST,
657 1 hiro
                             NULL);
658 1 hiro
        if ((pfile = prefs_file_open(rcpath)) == NULL) {
659 1 hiro
                g_warning("failed to write filter configuration to file\n");
660 1 hiro
                g_free(rcpath);
661 1 hiro
                return;
662 1 hiro
        }
663 1 hiro
664 1 hiro
        xml_file_put_xml_decl(pfile->fp);
665 1 hiro
        fputs("\n<filter>\n", pfile->fp);
666 1 hiro
667 1 hiro
        for (cur = fltlist; cur != NULL; cur = cur->next) {
668 1 hiro
                FilterRule *rule = (FilterRule *)cur->data;
669 1 hiro
                GSList *cur_cond;
670 1 hiro
                GSList *cur_action;
671 1 hiro
                gchar match_type[16];
672 1 hiro
673 1 hiro
                fputs("    <rule name=\"", pfile->fp);
674 1 hiro
                xml_file_put_escape_str(pfile->fp, rule->name);
675 1 hiro
                fprintf(pfile->fp, "\" timing=\"%s\"",
676 1 hiro
                        rule->timing == FLT_TIMING_ON_RECEIVE ? "receive" :
677 1 hiro
                        rule->timing == FLT_TIMING_MANUAL ? "manual" : "any");
678 1 hiro
                fprintf(pfile->fp, " enabled=\"%s\">\n",
679 1 hiro
                        rule->enabled ? "true" : "false");
680 1 hiro
681 1 hiro
                fprintf(pfile->fp, "        <condition-list bool=\"%s\">\n",
682 1 hiro
                        rule->bool_op == FLT_OR ? "or" : "and");
683 1 hiro
684 1 hiro
                for (cur_cond = rule->cond_list; cur_cond != NULL;
685 1 hiro
                     cur_cond = cur_cond->next) {
686 1 hiro
                        FilterCond *cond = (FilterCond *)cur_cond->data;
687 1 hiro
                        XMLNode *node = NULL;
688 1 hiro
689 1 hiro
                        switch (cond->match_type) {
690 1 hiro
                        case FLT_CONTAIN:
691 1 hiro
                                strcpy(match_type,
692 1 hiro
                                       FLT_IS_NOT_MATCH(cond->match_flag)
693 1 hiro
                                       ? "not-contain" : "contains");
694 1 hiro
                                break;
695 1 hiro
                        case FLT_EQUAL:
696 1 hiro
                                strcpy(match_type,
697 1 hiro
                                       FLT_IS_NOT_MATCH(cond->match_flag)
698 1 hiro
                                       ? "is-not" : "is");
699 1 hiro
                                break;
700 1 hiro
                        case FLT_REGEX:
701 1 hiro
                                strcpy(match_type,
702 1 hiro
                                       FLT_IS_NOT_MATCH(cond->match_flag)
703 1 hiro
                                       ? "not-regex" : "regex");
704 1 hiro
                                break;
705 1 hiro
                        default:
706 1 hiro
                                match_type[0] = '\0';
707 1 hiro
                                break;
708 1 hiro
                        }
709 1 hiro
710 1 hiro
                        switch (cond->type) {
711 1 hiro
                        case FLT_COND_HEADER:
712 1 hiro
                                NODE_NEW("match-header", cond->str_value);
713 1 hiro
                                ADD_ATTR("type", match_type);
714 1 hiro
                                ADD_ATTR("name", cond->header_name);
715 1 hiro
                                break;
716 1 hiro
                        case FLT_COND_ANY_HEADER:
717 1 hiro
                                NODE_NEW("match-any-header", cond->str_value);
718 1 hiro
                                ADD_ATTR("type", match_type);
719 1 hiro
                                break;
720 1 hiro
                        case FLT_COND_TO_OR_CC:
721 1 hiro
                                NODE_NEW("match-to-or-cc", cond->str_value);
722 1 hiro
                                ADD_ATTR("type", match_type);
723 1 hiro
                                break;
724 1 hiro
                        case FLT_COND_BODY:
725 1 hiro
                                NODE_NEW("match-body-text", cond->str_value);
726 1 hiro
                                ADD_ATTR("type", match_type);
727 1 hiro
                                break;
728 1 hiro
                        case FLT_COND_CMD_TEST:
729 1 hiro
                                NODE_NEW("command-test", cond->str_value);
730 1 hiro
                                break;
731 1 hiro
                        case FLT_COND_SIZE_GREATER:
732 1 hiro
                                NODE_NEW("size", itos(cond->int_value));
733 1 hiro
                                ADD_ATTR("type",
734 1 hiro
                                         FLT_IS_NOT_MATCH(cond->match_flag)
735 1 hiro
                                         ? "lt" : "gt");
736 1 hiro
                                break;
737 1 hiro
                        case FLT_COND_AGE_GREATER:
738 1 hiro
                                NODE_NEW("age", itos(cond->int_value));
739 1 hiro
                                ADD_ATTR("type",
740 1 hiro
                                         FLT_IS_NOT_MATCH(cond->match_flag)
741 1 hiro
                                         ? "lt" : "gt");
742 1 hiro
                                break;
743 1 hiro
                        case FLT_COND_ACCOUNT:
744 1 hiro
                                 NODE_NEW("account-id", itos(cond->int_value));
745 1 hiro
                                 break;
746 1 hiro
                        default:
747 1 hiro
                                 break;
748 1 hiro
                        }
749 1 hiro
750 1 hiro
                        if (node) {
751 1 hiro
                                fputs("            ", pfile->fp);
752 1 hiro
                                xml_file_put_node(pfile->fp, node);
753 1 hiro
                                xml_free_node(node);
754 1 hiro
                        }
755 1 hiro
                }
756 1 hiro
757 1 hiro
                fputs("        </condition-list>\n", pfile->fp);
758 1 hiro
759 1 hiro
                fputs("        <action-list>\n", pfile->fp);
760 1 hiro
761 1 hiro
                for (cur_action = rule->action_list; cur_action != NULL;
762 1 hiro
                     cur_action = cur_action->next) {
763 1 hiro
                        FilterAction *action = (FilterAction *)cur_action->data;
764 1 hiro
                        XMLNode *node = NULL;
765 1 hiro
766 1 hiro
                        switch (action->type) {
767 1 hiro
                        case FLT_ACTION_MOVE:
768 1 hiro
                                NODE_NEW("move", action->str_value);
769 1 hiro
                                break;
770 1 hiro
                        case FLT_ACTION_COPY:
771 1 hiro
                                NODE_NEW("copy", action->str_value);
772 1 hiro
                                break;
773 1 hiro
                        case FLT_ACTION_NOT_RECEIVE:
774 1 hiro
                                NODE_NEW("not-receive", NULL);
775 1 hiro
                                break;
776 1 hiro
                        case FLT_ACTION_DELETE:
777 1 hiro
                                NODE_NEW("delete", NULL);
778 1 hiro
                                break;
779 1 hiro
                        case FLT_ACTION_EXEC:
780 1 hiro
                                NODE_NEW("exec", action->str_value);
781 1 hiro
                                break;
782 1 hiro
                        case FLT_ACTION_EXEC_ASYNC:
783 1 hiro
                                NODE_NEW("exec-async", action->str_value);
784 1 hiro
                                break;
785 1 hiro
                        case FLT_ACTION_MARK:
786 1 hiro
                                NODE_NEW("mark", NULL);
787 1 hiro
                                break;
788 1 hiro
                        case FLT_ACTION_COLOR_LABEL:
789 1 hiro
                                NODE_NEW("color-label", action->str_value);
790 1 hiro
                                break;
791 1 hiro
                        case FLT_ACTION_MARK_READ:
792 1 hiro
                                NODE_NEW("mark-as-read", NULL);
793 1 hiro
                                break;
794 1 hiro
                        case FLT_ACTION_FORWARD:
795 1 hiro
                                NODE_NEW("forward", action->str_value);
796 1 hiro
                                break;
797 1 hiro
                        case FLT_ACTION_FORWARD_AS_ATTACHMENT:
798 1 hiro
                                NODE_NEW("forward-as-attachment",
799 1 hiro
                                         action->str_value);
800 1 hiro
                                break;
801 1 hiro
                        case FLT_ACTION_REDIRECT:
802 1 hiro
                                NODE_NEW("redirect", action->str_value);
803 1 hiro
                                break;
804 1 hiro
                        case FLT_ACTION_STOP_EVAL:
805 1 hiro
                                NODE_NEW("stop-eval", NULL);
806 1 hiro
                                break;
807 1 hiro
                        default:
808 1 hiro
                                break;
809 1 hiro
                        }
810 1 hiro
811 1 hiro
                        if (node) {
812 1 hiro
                                fputs("            ", pfile->fp);
813 1 hiro
                                xml_file_put_node(pfile->fp, node);
814 1 hiro
                                xml_free_node(node);
815 1 hiro
                        }
816 1 hiro
                }
817 1 hiro
818 1 hiro
                fputs("        </action-list>\n", pfile->fp);
819 1 hiro
820 1 hiro
                fputs("    </rule>\n", pfile->fp);
821 1 hiro
        }
822 1 hiro
823 1 hiro
        fputs("</filter>\n", pfile->fp);
824 1 hiro
825 1 hiro
        g_free(rcpath);
826 1 hiro
827 1 hiro
        if (prefs_file_close(pfile) < 0) {
828 1 hiro
                g_warning(_("failed to write configuration to file\n"));
829 1 hiro
                return;
830 1 hiro
        }
831 1 hiro
}
832 1 hiro
833 1 hiro
#undef NODE_NEW
834 1 hiro
#undef ADD_ATTR
835 1 hiro
836 1 hiro
gchar *filter_get_str(FilterRule *rule)
837 1 hiro
{
838 1 hiro
        gchar *str;
839 1 hiro
        FilterCond *cond1, *cond2;
840 1 hiro
        FilterAction *action = NULL;
841 1 hiro
        GSList *cur;
842 1 hiro
        gint flag1 = 0, flag2 = 0;
843 1 hiro
844 1 hiro
        cond1 = (FilterCond *)rule->cond_list->data;
845 1 hiro
        if (rule->cond_list->next)
846 1 hiro
                cond2 = (FilterCond *)rule->cond_list->next->data;
847 1 hiro
        else
848 1 hiro
                cond2 = NULL;
849 1 hiro
850 1 hiro
        /* new -> old flag conversion */
851 1 hiro
        switch (cond1->match_type) {
852 1 hiro
        case FLT_CONTAIN:
853 1 hiro
        case FLT_EQUAL:
854 1 hiro
                flag1 = FLT_IS_NOT_MATCH(cond1->match_flag) ? 0 : FLT_O_CONTAIN;
855 1 hiro
                if (FLT_IS_CASE_SENS(cond1->match_flag))
856 1 hiro
                        flag1 |= FLT_O_CASE_SENS;
857 1 hiro
                break;
858 1 hiro
        case FLT_REGEX:
859 1 hiro
                flag1 = FLT_O_REGEX; break;
860 1 hiro
        default:
861 1 hiro
                break;
862 1 hiro
        }
863 1 hiro
        if (cond2) {
864 1 hiro
                switch (cond2->match_type) {
865 1 hiro
                case FLT_CONTAIN:
866 1 hiro
                case FLT_EQUAL:
867 1 hiro
                        flag2 = FLT_IS_NOT_MATCH(cond2->match_flag) ? 0 : FLT_O_CONTAIN;
868 1 hiro
                        if (FLT_IS_CASE_SENS(cond2->match_flag))
869 1 hiro
                                flag2 |= FLT_O_CASE_SENS;
870 1 hiro
                        break;
871 1 hiro
                case FLT_REGEX:
872 1 hiro
                        flag2 = FLT_O_REGEX; break;
873 1 hiro
                default:
874 1 hiro
                        break;
875 1 hiro
                }
876 1 hiro
        } else
877 1 hiro
                flag2 = FLT_O_CONTAIN;
878 1 hiro
879 1 hiro
        for (cur = rule->action_list; cur != NULL; cur = cur->next) {
880 1 hiro
                action = (FilterAction *)cur->data;
881 1 hiro
                if (action->type == FLT_ACTION_MOVE ||
882 1 hiro
                    action->type == FLT_ACTION_NOT_RECEIVE ||
883 1 hiro
                    action->type == FLT_ACTION_DELETE)
884 1 hiro
                        break;
885 1 hiro
        }
886 1 hiro
887 1 hiro
        str = g_strdup_printf
888 1 hiro
                ("%s:%s:%c:%s:%s:%s:%d:%d:%c",
889 1 hiro
                 cond1->header_name, cond1->str_value ? cond1->str_value : "",
890 1 hiro
                 (cond2 && cond2->header_name) ?
891 1 hiro
                         (rule->bool_op == FLT_AND ? '&' : '|') : ' ',
892 1 hiro
                 (cond2 && cond2->header_name) ? cond2->header_name : "",
893 1 hiro
                 (cond2 && cond2->str_value) ? cond2->str_value : "",
894 1 hiro
                 (action && action->str_value) ? action->str_value : "",
895 1 hiro
                 flag1, flag2,
896 1 hiro
                 (action && action->type == FLT_ACTION_MOVE) ? 'm' :
897 1 hiro
                 (action && action->type == FLT_ACTION_NOT_RECEIVE) ? 'n' :
898 1 hiro
                 (action && action->type == FLT_ACTION_DELETE) ? 'd' : ' ');
899 1 hiro
900 1 hiro
        return str;
901 1 hiro
}
902 1 hiro
903 1 hiro
#define PARSE_ONE_PARAM(p, srcp) \
904 1 hiro
{ \
905 1 hiro
        p = strchr(srcp, '\t'); \
906 1 hiro
        if (!p) return NULL; \
907 1 hiro
        else \
908 1 hiro
                *p++ = '\0'; \
909 1 hiro
}
910 1 hiro
911 1 hiro
FilterRule *filter_read_str(const gchar *str)
912 1 hiro
{
913 1 hiro
        FilterRule *rule;
914 1 hiro
        FilterBoolOp bool_op;
915 1 hiro
        gint flag;
916 1 hiro
        FilterCond *cond;
917 1 hiro
        FilterMatchType match_type;
918 1 hiro
        FilterMatchFlag match_flag;
919 1 hiro
        FilterAction *action;
920 1 hiro
        GSList *cond_list = NULL;
921 1 hiro
        GSList *action_list = NULL;
922 1 hiro
        gchar *tmp;
923 1 hiro
        gchar *rule_name;
924 1 hiro
        gchar *name1, *body1, *op, *name2, *body2, *dest;
925 1 hiro
        gchar *flag1 = NULL, *flag2 = NULL, *action1 = NULL;
926 1 hiro
927 1 hiro
        Xstrdup_a(tmp, str, return NULL);
928 1 hiro
929 1 hiro
        name1 = tmp;
930 1 hiro
        PARSE_ONE_PARAM(body1, name1);
931 1 hiro
        PARSE_ONE_PARAM(op, body1);
932 1 hiro
        PARSE_ONE_PARAM(name2, op);
933 1 hiro
        PARSE_ONE_PARAM(body2, name2);
934 1 hiro
        PARSE_ONE_PARAM(dest, body2);
935 1 hiro
        if (strchr(dest, '\t')) {
936 1 hiro
                gchar *p;
937 1 hiro
938 1 hiro
                PARSE_ONE_PARAM(flag1, dest);
939 1 hiro
                PARSE_ONE_PARAM(flag2, flag1);
940 1 hiro
                PARSE_ONE_PARAM(action1, flag2);
941 1 hiro
                if ((p = strchr(action1, '\t'))) *p = '\0';
942 1 hiro
        }
943 1 hiro
944 1 hiro
        bool_op = (*op == '&') ? FLT_AND : FLT_OR;
945 1 hiro
946 1 hiro
        if (*name1) {
947 1 hiro
                if (flag1)
948 1 hiro
                        flag = (FilterOldFlag)strtoul(flag1, NULL, 10);
949 1 hiro
                else
950 1 hiro
                        flag = FLT_O_CONTAIN;
951 1 hiro
                match_type = FLT_CONTAIN;
952 1 hiro
                match_flag = 0;
953 1 hiro
                if (flag & FLT_O_REGEX)
954 1 hiro
                        match_type = FLT_REGEX;
955 1 hiro
                else if (!(flag & FLT_O_CONTAIN))
956 1 hiro
                        match_flag = FLT_NOT_MATCH;
957 1 hiro
                if (flag & FLT_O_CASE_SENS)
958 1 hiro
                        match_flag |= FLT_CASE_SENS;
959 1 hiro
                cond = filter_cond_new(FLT_COND_HEADER, match_type, match_flag,
960 1 hiro
                                       name1, body1);
961 1 hiro
                cond_list = g_slist_append(cond_list, cond);
962 1 hiro
        }
963 1 hiro
        if (*name2) {
964 1 hiro
                if (flag2)
965 1 hiro
                        flag = (FilterOldFlag)strtoul(flag2, NULL, 10);
966 1 hiro
                else
967 1 hiro
                        flag = FLT_O_CONTAIN;
968 1 hiro
                match_type = FLT_CONTAIN;
969 1 hiro
                match_flag = 0;
970 1 hiro
                if (flag & FLT_O_REGEX)
971 1 hiro
                        match_type = FLT_REGEX;
972 1 hiro
                else if (!(flag & FLT_O_CONTAIN))
973 1 hiro
                        match_flag = FLT_NOT_MATCH;
974 1 hiro
                if (flag & FLT_O_CASE_SENS)
975 1 hiro
                        match_flag |= FLT_CASE_SENS;
976 1 hiro
                cond = filter_cond_new(FLT_COND_HEADER, match_type, match_flag,
977 1 hiro
                                       name2, body2);
978 1 hiro
                cond_list = g_slist_append(cond_list, cond);
979 1 hiro
        }
980 1 hiro
981 1 hiro
        action = filter_action_new(FLT_ACTION_MOVE,
982 1 hiro
                                   *dest ? g_strdup(dest) : NULL);
983 1 hiro
        if (action1) {
984 1 hiro
                switch (*action1) {
985 1 hiro
                case 'm': action->type = FLT_ACTION_MOVE;                break;
986 1 hiro
                case 'n': action->type = FLT_ACTION_NOT_RECEIVE;        break;
987 1 hiro
                case 'd': action->type = FLT_ACTION_DELETE;                break;
988 1 hiro
                default:  g_warning("Invalid action: `%c'\n", *action1);
989 1 hiro
                }
990 1 hiro
        }
991 1 hiro
        action_list = g_slist_append(action_list, action);
992 1 hiro
993 1 hiro
        Xstrdup_a(rule_name, str, return NULL);
994 1 hiro
        subst_char(rule_name, '\t', ':');
995 1 hiro
996 1 hiro
        rule = filter_rule_new(rule_name, bool_op, cond_list, action_list);
997 1 hiro
998 1 hiro
        return rule;
999 1 hiro
}
1000 1 hiro
1001 1 hiro
FilterRule *filter_rule_new(const gchar *name, FilterBoolOp bool_op,
1002 1 hiro
                            GSList *cond_list, GSList *action_list)
1003 1 hiro
{
1004 1 hiro
        FilterRule *rule;
1005 1 hiro
1006 1 hiro
        rule = g_new0(FilterRule, 1);
1007 1 hiro
        rule->name = g_strdup(name);
1008 1 hiro
        rule->bool_op = bool_op;
1009 1 hiro
        rule->cond_list = cond_list;
1010 1 hiro
        rule->action_list = action_list;
1011 1 hiro
        rule->timing = FLT_TIMING_ANY;
1012 1 hiro
        rule->enabled = TRUE;
1013 1 hiro
1014 1 hiro
        return rule;
1015 1 hiro
}
1016 1 hiro
1017 1 hiro
FilterCond *filter_cond_new(FilterCondType type,
1018 1 hiro
                            FilterMatchType match_type,
1019 1 hiro
                            FilterMatchFlag match_flag,
1020 1 hiro
                            const gchar *header, const gchar *value)
1021 1 hiro
{
1022 1 hiro
        FilterCond *cond;
1023 1 hiro
1024 1 hiro
        cond = g_new0(FilterCond, 1);
1025 1 hiro
        cond->type = type;
1026 1 hiro
        cond->match_type = match_type;
1027 1 hiro
        cond->match_flag = match_flag;
1028 1 hiro
1029 1 hiro
        if (type == FLT_COND_HEADER)
1030 1 hiro
                cond->header_name =
1031 1 hiro
                        (header && *header) ? g_strdup(header) : NULL;
1032 1 hiro
        else
1033 1 hiro
                cond->header_name = NULL;
1034 1 hiro
1035 1 hiro
        cond->str_value = (value && *value) ? g_strdup(value) : NULL;
1036 1 hiro
        if (type == FLT_COND_SIZE_GREATER || type == FLT_COND_AGE_GREATER)
1037 1 hiro
                cond->int_value = atoi(value);
1038 1 hiro
        else
1039 1 hiro
                cond->int_value = 0;
1040 1 hiro
1041 1 hiro
        if (match_type == FLT_REGEX)
1042 1 hiro
                cond->match_func = strmatch_regex;
1043 1 hiro
        else if (match_type == FLT_EQUAL) {
1044 1 hiro
                if (FLT_IS_CASE_SENS(match_flag))
1045 1 hiro
                        cond->match_func = str_find_equal;
1046 1 hiro
                else
1047 1 hiro
                        cond->match_func = str_case_find_equal;
1048 1 hiro
        } else {
1049 1 hiro
                if (FLT_IS_CASE_SENS(match_flag))
1050 1 hiro
                        cond->match_func = str_find;
1051 1 hiro
                else
1052 1 hiro
                        cond->match_func = str_case_find;
1053 1 hiro
        }
1054 1 hiro
1055 1 hiro
        return cond;
1056 1 hiro
}
1057 1 hiro
1058 1 hiro
FilterAction *filter_action_new(FilterActionType type, const gchar *str)
1059 1 hiro
{
1060 1 hiro
        FilterAction *action;
1061 1 hiro
1062 1 hiro
        action = g_new0(FilterAction, 1);
1063 1 hiro
        action->type = type;
1064 1 hiro
1065 1 hiro
        action->str_value = (str && *str) ? g_strdup(str) : NULL;
1066 1 hiro
        if (type == FLT_ACTION_COLOR_LABEL && str)
1067 1 hiro
                action->int_value = atoi(str);
1068 1 hiro
        else
1069 1 hiro
                action->int_value = 0;
1070 1 hiro
1071 1 hiro
        return action;
1072 1 hiro
}
1073 1 hiro
1074 1 hiro
FilterInfo *filter_info_new(void)
1075 1 hiro
{
1076 1 hiro
        FilterInfo *fltinfo;
1077 1 hiro
1078 1 hiro
        fltinfo = g_new0(FilterInfo, 1);
1079 1 hiro
        fltinfo->dest_list = NULL;
1080 1 hiro
        fltinfo->move_dest = NULL;
1081 1 hiro
        fltinfo->drop_done = FALSE;
1082 1 hiro
1083 1 hiro
        return fltinfo;
1084 1 hiro
}
1085 1 hiro
1086 1 hiro
void filter_rule_rename_dest_path(FilterRule *rule, const gchar *old_path,
1087 1 hiro
                                  const gchar *new_path)
1088 1 hiro
{
1089 1 hiro
        FilterAction *action;
1090 1 hiro
        GSList *cur;
1091 1 hiro
        gchar *base;
1092 1 hiro
        gchar *dest_path;
1093 1 hiro
        gint oldpathlen;
1094 1 hiro
1095 1 hiro
        for (cur = rule->action_list; cur != NULL; cur = cur->next) {
1096 1 hiro
                action = (FilterAction *)cur->data;
1097 1 hiro
1098 1 hiro
                if (action->type != FLT_ACTION_MOVE &&
1099 1 hiro
                    action->type != FLT_ACTION_COPY)
1100 1 hiro
                        continue;
1101 1 hiro
1102 1 hiro
                oldpathlen = strlen(old_path);
1103 1 hiro
                if (action->str_value &&
1104 1 hiro
                    !strncmp(old_path, action->str_value, oldpathlen)) {
1105 1 hiro
                        base = action->str_value + oldpathlen;
1106 1 hiro
                        while (*base == G_DIR_SEPARATOR) base++;
1107 1 hiro
                        if (*base == '\0')
1108 1 hiro
                                dest_path = g_strdup(new_path);
1109 1 hiro
                        else
1110 1 hiro
                                dest_path = g_strconcat(new_path,
1111 1 hiro
                                                        G_DIR_SEPARATOR_S,
1112 1 hiro
                                                        base, NULL);
1113 1 hiro
                        g_free(action->str_value);
1114 1 hiro
                        action->str_value = dest_path;
1115 1 hiro
                }
1116 1 hiro
        }
1117 1 hiro
}
1118 1 hiro
1119 1 hiro
void filter_rule_delete_action_by_dest_path(FilterRule *rule, const gchar *path)
1120 1 hiro
{
1121 1 hiro
        FilterAction *action;
1122 1 hiro
        GSList *cur;
1123 1 hiro
        GSList *next;
1124 1 hiro
1125 1 hiro
        for (cur = rule->action_list; cur != NULL; cur = next) {
1126 1 hiro
                action = (FilterAction *)cur->data;
1127 1 hiro
                next = cur->next;
1128 1 hiro
1129 1 hiro
                if (action->type != FLT_ACTION_MOVE &&
1130 1 hiro
                    action->type != FLT_ACTION_COPY)
1131 1 hiro
                        continue;
1132 1 hiro
1133 1 hiro
                if (action->str_value &&
1134 1 hiro
                    !strncmp(path, action->str_value, strlen(path))) {
1135 1 hiro
                        rule->action_list = g_slist_remove
1136 1 hiro
                                (rule->action_list, action);
1137 1 hiro
                        filter_action_free(action);
1138 1 hiro
                }
1139 1 hiro
        }
1140 1 hiro
}
1141 1 hiro
1142 1 hiro
void filter_rule_match_type_str_to_enum(const gchar *match_type,
1143 1 hiro
                                        FilterMatchType *type,
1144 1 hiro
                                        FilterMatchFlag *flag)
1145 1 hiro
{
1146 1 hiro
        g_return_if_fail(match_type != NULL);
1147 1 hiro
1148 1 hiro
        *type = FLT_CONTAIN;
1149 1 hiro
        *flag = 0;
1150 1 hiro
1151 1 hiro
        if (!strcmp(match_type, "contains")) {
1152 1 hiro
                *type = FLT_CONTAIN;
1153 1 hiro
        } else if (!strcmp(match_type, "not-contain")) {
1154 1 hiro
                *type = FLT_CONTAIN;
1155 1 hiro
                *flag = FLT_NOT_MATCH;
1156 1 hiro
        } else if (!strcmp(match_type, "is")) {
1157 1 hiro
                *type = FLT_EQUAL;
1158 1 hiro
        } else if (!strcmp(match_type, "is-not")) {
1159 1 hiro
                *type = FLT_EQUAL;
1160 1 hiro
                *flag = FLT_NOT_MATCH;
1161 1 hiro
        } else if (!strcmp(match_type, "regex")) {
1162 1 hiro
                *type = FLT_REGEX;
1163 1 hiro
        } else if (!strcmp(match_type, "not-regex")) {
1164 1 hiro
                *type = FLT_REGEX;
1165 1 hiro
                *flag = FLT_NOT_MATCH;
1166 1 hiro
        } else if (!strcmp(match_type, "gt")) {
1167 1 hiro
        } else if (!strcmp(match_type, "lt")) {
1168 1 hiro
                *flag = FLT_NOT_MATCH;
1169 1 hiro
        }
1170 1 hiro
}
1171 1 hiro
1172 336 hiro
void filter_rule_list_free(GSList *fltlist)
1173 336 hiro
{
1174 336 hiro
        GSList *cur;
1175 336 hiro
1176 336 hiro
        for (cur = fltlist; cur != NULL; cur = cur->next)
1177 336 hiro
                filter_rule_free((FilterRule *)cur->data);
1178 336 hiro
        g_slist_free(fltlist);
1179 336 hiro
}
1180 336 hiro
1181 1 hiro
void filter_rule_free(FilterRule *rule)
1182 1 hiro
{
1183 1 hiro
        if (!rule) return;
1184 1 hiro
1185 1 hiro
        g_free(rule->name);
1186 1 hiro
1187 1 hiro
        filter_cond_list_free(rule->cond_list);
1188 1 hiro
        filter_action_list_free(rule->action_list);
1189 1 hiro
1190 1 hiro
        g_free(rule);
1191 1 hiro
}
1192 1 hiro
1193 1 hiro
void filter_cond_list_free(GSList *cond_list)
1194 1 hiro
{
1195 1 hiro
        GSList *cur;
1196 1 hiro
1197 1 hiro
        for (cur = cond_list; cur != NULL; cur = cur->next)
1198 1 hiro
                filter_cond_free((FilterCond *)cur->data);
1199 1 hiro
        g_slist_free(cond_list);
1200 1 hiro
}
1201 1 hiro
1202 1 hiro
void filter_action_list_free(GSList *action_list)
1203 1 hiro
{
1204 1 hiro
        GSList *cur;
1205 1 hiro
1206 1 hiro
        for (cur = action_list; cur != NULL; cur = cur->next)
1207 1 hiro
                filter_action_free((FilterAction *)cur->data);
1208 1 hiro
        g_slist_free(action_list);
1209 1 hiro
}
1210 1 hiro
1211 1 hiro
static void filter_cond_free(FilterCond *cond)
1212 1 hiro
{
1213 1 hiro
        g_free(cond->header_name);
1214 1 hiro
        g_free(cond->str_value);
1215 1 hiro
        g_free(cond);
1216 1 hiro
}
1217 1 hiro
1218 1 hiro
static void filter_action_free(FilterAction *action)
1219 1 hiro
{
1220 1 hiro
        g_free(action->str_value);
1221 1 hiro
        g_free(action);
1222 1 hiro
}
1223 1 hiro
1224 1 hiro
void filter_info_free(FilterInfo *fltinfo)
1225 1 hiro
{
1226 1 hiro
        g_slist_free(fltinfo->dest_list);
1227 1 hiro
        g_free(fltinfo);
1228 1 hiro
}