2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2007 Hiroyuki Yamamoto & The Claws Mail 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 #include <glib/gi18n.h>
31 #include "procheader.h"
33 #include "filtering.h"
34 #include "prefs_gtk.h"
36 #include "prefs_common.h"
38 #define PREFSBUFSIZE 1024
40 GSList * pre_global_processing = NULL;
41 GSList * post_global_processing = NULL;
42 GSList * filtering_rules = NULL;
44 static gboolean filtering_is_final_action(FilteringAction *filtering_action);
46 #define STRLEN_WITH_CHECK(expr) \
47 strlen_with_check(#expr, __LINE__, expr)
49 static inline gint strlen_with_check(const gchar *expr, gint fline, const gchar *str)
54 debug_print("%s(%d) - invalid string %s\n", __FILE__, fline, expr);
59 FilteringAction * filteringaction_new(int type, int account_id,
61 gint labelcolor, gint score)
63 FilteringAction * action;
65 action = g_new0(FilteringAction, 1);
68 action->account_id = account_id;
70 action->destination = g_strdup(destination);
72 action->destination = NULL;
74 action->labelcolor = labelcolor;
75 action->score = score;
79 void filteringaction_free(FilteringAction * action)
81 g_return_if_fail(action);
82 g_free(action->destination);
86 FilteringProp * filteringprop_new(gboolean enabled,
89 MatcherList * matchers,
92 FilteringProp * filtering;
94 filtering = g_new0(FilteringProp, 1);
95 filtering->enabled = enabled;
96 filtering->name = name ? g_strdup(name): NULL;
97 filtering->account_id = account_id;
98 filtering->matchers = matchers;
99 filtering->action_list = action_list;
104 static FilteringAction * filteringaction_copy(FilteringAction * src)
106 FilteringAction * new;
108 new = g_new0(FilteringAction, 1);
110 new->type = src->type;
111 new->account_id = src->account_id;
112 if (src->destination)
113 new->destination = g_strdup(src->destination);
115 new->destination = NULL;
116 new->labelcolor = src->labelcolor;
117 new->score = src->score;
122 FilteringProp * filteringprop_copy(FilteringProp *src)
127 new = g_new0(FilteringProp, 1);
128 new->matchers = g_new0(MatcherList, 1);
130 for (tmp = src->matchers->matchers; tmp != NULL && tmp->data != NULL;) {
131 MatcherProp *matcher = (MatcherProp *)tmp->data;
133 new->matchers->matchers = g_slist_append(new->matchers->matchers,
134 matcherprop_copy(matcher));
138 new->matchers->bool_and = src->matchers->bool_and;
140 new->action_list = NULL;
142 for (tmp = src->action_list ; tmp != NULL ; tmp = tmp->next) {
143 FilteringAction *filtering_action;
145 filtering_action = tmp->data;
147 new->action_list = g_slist_append(new->action_list,
148 filteringaction_copy(filtering_action));
151 new->enabled = src->enabled;
152 new->name = g_strdup(src->name);
157 void filteringprop_free(FilteringProp * prop)
161 g_return_if_fail(prop);
162 matcherlist_free(prop->matchers);
164 for (tmp = prop->action_list ; tmp != NULL ; tmp = tmp->next) {
165 filteringaction_free(tmp->data);
171 void filtering_move_and_copy_msg(MsgInfo *msginfo)
173 GSList *list = g_slist_append(NULL, msginfo);
174 filtering_move_and_copy_msgs(list);
178 /* move and copy messages by batches to be faster on IMAP */
179 void filtering_move_and_copy_msgs(GSList *msgs)
181 GSList *messages = g_slist_copy(msgs);
182 FolderItem *last_item = NULL;
183 gboolean is_copy = FALSE, is_move = FALSE;
184 debug_print("checking %d messages\n", g_slist_length(msgs));
186 GSList *batch = NULL, *cur;
188 for (cur = messages; cur; cur = cur->next) {
189 MsgInfo *info = (MsgInfo *)cur->data;
190 if (last_item == NULL) {
191 last_item = info->to_filter_folder;
193 if (last_item == NULL)
195 if (!is_copy && !is_move) {
198 else if (info->is_move)
202 if (info->to_filter_folder == last_item
203 && info->is_copy == is_copy
204 && info->is_move == is_move) {
205 batch = g_slist_prepend(batch, info);
209 debug_print("no more messages to move/copy\n");
212 debug_print("%d messages to %s in %s\n", found,
213 is_copy ? "copy":"move", last_item->name ? last_item->name:"(noname)");
215 for (cur = batch; cur; cur = cur->next) {
216 MsgInfo *info = (MsgInfo *)cur->data;
217 messages = g_slist_remove(messages, info);
219 batch = g_slist_reverse(batch);
220 if (g_slist_length(batch)) {
221 MsgInfo *info = (MsgInfo *)batch->data;
222 if (is_copy && last_item != info->folder) {
223 folder_item_copy_msgs(last_item, batch);
224 } else if (is_move && last_item != info->folder) {
225 if (folder_item_move_msgs(last_item, batch) < 0)
226 folder_item_move_msgs(
227 folder_get_default_inbox(),
230 /* we don't reference the msginfos, because caller will do */
238 /* we don't reference the msginfos, because caller will do */
239 g_slist_free(messages);
243 fitleringaction_apply
244 runs the action on one MsgInfo
245 return value : return TRUE if the action could be applied
248 static gboolean filteringaction_apply(FilteringAction * action, MsgInfo * info)
250 FolderItem * dest_folder;
253 PrefsAccount * account;
256 switch(action->type) {
257 case MATCHACTION_MOVE:
259 folder_find_item_from_identifier(action->destination);
261 debug_print("*** folder not found '%s'\n",
262 action->destination ?action->destination :"");
266 /* check if mail is set to copy already,
267 * in which case we have to do it */
268 if (info->is_copy && info->to_filter_folder) {
269 debug_print("should cp and mv !\n");
270 folder_item_copy_msg(info->to_filter_folder, info);
271 info->is_copy = FALSE;
273 /* mark message to be moved */
274 info->is_move = TRUE;
275 info->to_filter_folder = dest_folder;
278 case MATCHACTION_COPY:
280 folder_find_item_from_identifier(action->destination);
283 debug_print("*** folder not found '%s'\n",
284 action->destination ?action->destination :"");
288 /* check if mail is set to copy already,
289 * in which case we have to do it */
290 if (info->is_copy && info->to_filter_folder) {
291 debug_print("should cp and mv !\n");
292 folder_item_copy_msg(info->to_filter_folder, info);
293 info->is_copy = FALSE;
295 /* mark message to be copied */
296 info->is_copy = TRUE;
297 info->to_filter_folder = dest_folder;
300 case MATCHACTION_DELETE:
301 if (folder_item_remove_msg(info->folder, info->msgnum) == -1)
305 case MATCHACTION_MARK:
306 procmsg_msginfo_set_flags(info, MSG_MARKED, 0);
309 case MATCHACTION_UNMARK:
310 procmsg_msginfo_unset_flags(info, MSG_MARKED, 0);
313 case MATCHACTION_LOCK:
314 procmsg_msginfo_set_flags(info, MSG_LOCKED, 0);
317 case MATCHACTION_UNLOCK:
318 procmsg_msginfo_unset_flags(info, MSG_LOCKED, 0);
321 case MATCHACTION_MARK_AS_READ:
322 procmsg_msginfo_unset_flags(info, MSG_UNREAD | MSG_NEW, 0);
325 case MATCHACTION_MARK_AS_UNREAD:
326 procmsg_msginfo_set_flags(info, MSG_UNREAD | MSG_NEW, 0);
329 case MATCHACTION_COLOR:
330 procmsg_msginfo_unset_flags(info, MSG_CLABEL_FLAG_MASK, 0);
331 procmsg_msginfo_set_flags(info, MSG_COLORLABEL_TO_FLAGS(action->labelcolor), 0);
334 case MATCHACTION_FORWARD:
335 case MATCHACTION_FORWARD_AS_ATTACHMENT:
336 account = account_find_from_id(action->account_id);
337 compose = compose_forward(account, info,
338 action->type == MATCHACTION_FORWARD ? FALSE : TRUE,
340 compose_entry_append(compose, action->destination,
341 compose->account->protocol == A_NNTP
345 val = compose_send(compose);
347 return val == 0 ? TRUE : FALSE;
349 case MATCHACTION_REDIRECT:
350 account = account_find_from_id(action->account_id);
351 compose = compose_redirect(account, info, TRUE);
352 if (compose->account->protocol == A_NNTP)
355 compose_entry_append(compose, action->destination,
358 val = compose_send(compose);
360 return val == 0 ? TRUE : FALSE;
362 case MATCHACTION_EXECUTE:
363 cmd = matching_build_command(action->destination, info);
372 case MATCHACTION_SET_SCORE:
373 info->score = action->score;
376 case MATCHACTION_CHANGE_SCORE:
377 info->score += action->score;
380 case MATCHACTION_STOP:
383 case MATCHACTION_HIDE:
387 case MATCHACTION_IGNORE:
388 procmsg_msginfo_set_flags(info, MSG_IGNORE_THREAD, 0);
397 gboolean filteringaction_apply_action_list(GSList *action_list, MsgInfo *info)
400 g_return_val_if_fail(action_list, FALSE);
401 g_return_val_if_fail(info, FALSE);
402 for (p = action_list; p && p->data; p = g_slist_next(p)) {
403 FilteringAction *a = (FilteringAction *) p->data;
404 if (filteringaction_apply(a, info)) {
405 if (filtering_is_final_action(a))
414 static gboolean filtering_match_condition(FilteringProp *filtering, MsgInfo *info,
415 PrefsAccount *ac_prefs)
417 gboolean matches = FALSE;
419 if (ac_prefs != NULL) {
420 matches = ((filtering->account_id == 0)
421 || (filtering->account_id == ac_prefs->account_id));
423 switch (prefs_common.apply_per_account_filtering_rules) {
424 case FILTERING_ACCOUNT_RULES_FORCE:
425 /* apply filtering rules regardless to the account info */
428 case FILTERING_ACCOUNT_RULES_SKIP:
429 /* don't apply filtering rules that belong to an account */
430 matches = (filtering->account_id == 0);
432 case FILTERING_ACCOUNT_RULES_USE_CURRENT:
433 matches = ((filtering->account_id == 0)
434 || (filtering->account_id == cur_account->account_id));
439 return matches && matcherlist_match(filtering->matchers, info);
443 *\brief Apply a rule on message.
445 *\param filtering List of filtering rules.
446 *\param info Message to apply rules on.
447 *\param final Variable returning TRUE or FALSE if one of the
448 * encountered actions was final.
449 * See also \ref filtering_is_final_action.
451 *\return gboolean TRUE to continue applying rules.
453 static gboolean filtering_apply_rule(FilteringProp *filtering, MsgInfo *info,
456 gboolean result = TRUE;
461 for (tmp = filtering->action_list ; tmp != NULL ; tmp = tmp->next) {
462 FilteringAction * action;
466 if (FALSE == (result = filteringaction_apply(action, info))) {
467 g_warning("No further processing after rule %s\n",
468 filteringaction_to_string(buf, sizeof buf, action));
471 if (filtering_is_final_action(action)) {
480 *\brief Check if an action is "final", i.e. should break further
483 *\param filtering_action Action to check.
485 *\return gboolean TRUE if \a filtering_action is final.
487 static gboolean filtering_is_final_action(FilteringAction *filtering_action)
489 switch(filtering_action->type) {
490 case MATCHACTION_MOVE:
491 case MATCHACTION_DELETE:
492 case MATCHACTION_STOP:
493 return TRUE; /* MsgInfo invalid for message */
499 static gboolean filter_msginfo(GSList * filtering_list, MsgInfo * info, PrefsAccount* ac_prefs)
505 g_return_val_if_fail(info != NULL, TRUE);
507 for (l = filtering_list, final = FALSE, apply_next = FALSE; l != NULL; l = g_slist_next(l)) {
508 FilteringProp * filtering = (FilteringProp *) l->data;
510 if (filtering->enabled && filtering_match_condition(filtering, info, ac_prefs)) {
511 apply_next = filtering_apply_rule(filtering, info, &final);
517 /* put in inbox if a final rule could not be applied, or
518 * the last rule was not a final one. */
519 if ((final && !apply_next) || !final) {
527 *\brief Filter a message against a list of rules.
529 *\param flist List of filter rules.
530 *\param info Message.
532 *\return gboolean TRUE if filter rules handled the message.
534 *\note Returning FALSE means the message was not handled,
535 * and that the calling code should do the default
536 * processing. E.g. \ref inc.c::inc_start moves the
537 * message to the inbox.
539 gboolean filter_message_by_msginfo(GSList *flist, MsgInfo *info, PrefsAccount* ac_prefs)
541 return filter_msginfo(flist, info, ac_prefs);
544 gchar *filteringaction_to_string(gchar *dest, gint destlen, FilteringAction *action)
546 const gchar *command_str;
549 command_str = get_matchparser_tab_str(action->type);
551 if (command_str == NULL)
554 switch(action->type) {
555 case MATCHACTION_MOVE:
556 case MATCHACTION_COPY:
557 case MATCHACTION_EXECUTE:
558 quoted_dest = matcher_quote_str(action->destination);
559 g_snprintf(dest, destlen, "%s \"%s\"", command_str, quoted_dest);
563 case MATCHACTION_DELETE:
564 case MATCHACTION_MARK:
565 case MATCHACTION_UNMARK:
566 case MATCHACTION_LOCK:
567 case MATCHACTION_UNLOCK:
568 case MATCHACTION_MARK_AS_READ:
569 case MATCHACTION_MARK_AS_UNREAD:
570 case MATCHACTION_STOP:
571 case MATCHACTION_HIDE:
572 case MATCHACTION_IGNORE:
573 g_snprintf(dest, destlen, "%s", command_str);
576 case MATCHACTION_REDIRECT:
577 case MATCHACTION_FORWARD:
578 case MATCHACTION_FORWARD_AS_ATTACHMENT:
579 quoted_dest = matcher_quote_str(action->destination);
580 g_snprintf(dest, destlen, "%s %d \"%s\"", command_str, action->account_id, quoted_dest);
584 case MATCHACTION_COLOR:
585 g_snprintf(dest, destlen, "%s %d", command_str, action->labelcolor);
588 case MATCHACTION_CHANGE_SCORE:
589 case MATCHACTION_SET_SCORE:
590 g_snprintf(dest, destlen, "%s %d", command_str, action->score);
598 gchar * filteringaction_list_to_string(GSList * action_list)
600 gchar *action_list_str;
605 action_list_str = NULL;
606 for (tmp = action_list ; tmp != NULL ; tmp = tmp->next) {
608 FilteringAction * action;
612 action_str = filteringaction_to_string(buf,
615 if (action_list_str != NULL) {
616 list_str = g_strconcat(action_list_str, " ", action_str, NULL);
617 g_free(action_list_str);
620 list_str = g_strdup(action_str);
622 action_list_str = list_str;
625 return action_list_str;
628 gchar * filteringprop_to_string(FilteringProp * prop)
631 gchar *action_list_str;
632 gchar *filtering_str;
634 action_list_str = filteringaction_list_to_string(prop->action_list);
636 if (action_list_str == NULL)
639 list_str = matcherlist_to_string(prop->matchers);
641 if (list_str == NULL) {
642 g_free(action_list_str);
646 filtering_str = g_strconcat(list_str, " ", action_list_str, NULL);
647 g_free(action_list_str);
650 return filtering_str;
653 void prefs_filtering_free(GSList * prefs_filtering)
655 while (prefs_filtering != NULL) {
656 FilteringProp * filtering = (FilteringProp *)
657 prefs_filtering->data;
658 filteringprop_free(filtering);
659 prefs_filtering = g_slist_remove(prefs_filtering, filtering);
663 static gboolean prefs_filtering_free_func(GNode *node, gpointer data)
665 FolderItem *item = node->data;
667 g_return_val_if_fail(item, FALSE);
668 g_return_val_if_fail(item->prefs, FALSE);
670 prefs_filtering_free(item->prefs->processing);
671 item->prefs->processing = NULL;
676 void prefs_filtering_clear(void)
680 for (cur = folder_get_list() ; cur != NULL ; cur = g_list_next(cur)) {
683 folder = (Folder *) cur->data;
684 g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
685 prefs_filtering_free_func, NULL);
688 prefs_filtering_free(filtering_rules);
689 filtering_rules = NULL;
690 prefs_filtering_free(pre_global_processing);
691 pre_global_processing = NULL;
692 prefs_filtering_free(post_global_processing);
693 post_global_processing = NULL;
696 void prefs_filtering_clear_folder(Folder *folder)
698 g_return_if_fail(folder);
699 g_return_if_fail(folder->node);
701 g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
702 prefs_filtering_free_func, NULL);
703 /* FIXME: Note folder settings were changed, where the updates? */
706 gboolean filtering_peek_per_account_rules(GSList *filtering_list)
707 /* return TRUE if there's at least one per-account filtering rule */
711 for (l = filtering_list; l != NULL; l = g_slist_next(l)) {
712 FilteringProp * filtering = (FilteringProp *) l->data;
714 if (filtering->enabled && (filtering->account_id != 0)) {