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_MARK_AS_SPAM:
330 procmsg_spam_learner_learn(info, NULL, TRUE);
331 procmsg_msginfo_change_flags(info, MSG_SPAM, 0, MSG_NEW|MSG_UNREAD, 0);
332 if (procmsg_spam_get_folder(info)) {
333 info->is_move = TRUE;
334 info->to_filter_folder = procmsg_spam_get_folder(info);
338 case MATCHACTION_MARK_AS_HAM:
339 procmsg_spam_learner_learn(info, NULL, FALSE);
342 case MATCHACTION_COLOR:
343 procmsg_msginfo_unset_flags(info, MSG_CLABEL_FLAG_MASK, 0);
344 procmsg_msginfo_set_flags(info, MSG_COLORLABEL_TO_FLAGS(action->labelcolor), 0);
347 case MATCHACTION_FORWARD:
348 case MATCHACTION_FORWARD_AS_ATTACHMENT:
349 account = account_find_from_id(action->account_id);
350 compose = compose_forward(account, info,
351 action->type == MATCHACTION_FORWARD ? FALSE : TRUE,
353 compose_entry_append(compose, action->destination,
354 compose->account->protocol == A_NNTP
358 val = compose_send(compose);
360 return val == 0 ? TRUE : FALSE;
362 case MATCHACTION_REDIRECT:
363 account = account_find_from_id(action->account_id);
364 compose = compose_redirect(account, info, TRUE);
365 if (compose->account->protocol == A_NNTP)
368 compose_entry_append(compose, action->destination,
371 val = compose_send(compose);
373 return val == 0 ? TRUE : FALSE;
375 case MATCHACTION_EXECUTE:
376 cmd = matching_build_command(action->destination, info);
385 case MATCHACTION_SET_SCORE:
386 info->score = action->score;
389 case MATCHACTION_CHANGE_SCORE:
390 info->score += action->score;
393 case MATCHACTION_STOP:
396 case MATCHACTION_HIDE:
400 case MATCHACTION_IGNORE:
401 procmsg_msginfo_set_flags(info, MSG_IGNORE_THREAD, 0);
410 gboolean filteringaction_apply_action_list(GSList *action_list, MsgInfo *info)
413 g_return_val_if_fail(action_list, FALSE);
414 g_return_val_if_fail(info, FALSE);
415 for (p = action_list; p && p->data; p = g_slist_next(p)) {
416 FilteringAction *a = (FilteringAction *) p->data;
417 if (filteringaction_apply(a, info)) {
418 if (filtering_is_final_action(a))
427 static gboolean filtering_match_condition(FilteringProp *filtering, MsgInfo *info,
428 PrefsAccount *ac_prefs)
430 gboolean matches = FALSE;
432 if (ac_prefs != NULL) {
433 matches = ((filtering->account_id == 0)
434 || (filtering->account_id == ac_prefs->account_id));
436 switch (prefs_common.apply_per_account_filtering_rules) {
437 case FILTERING_ACCOUNT_RULES_FORCE:
438 /* apply filtering rules regardless to the account info */
441 case FILTERING_ACCOUNT_RULES_SKIP:
442 /* don't apply filtering rules that belong to an account */
443 matches = (filtering->account_id == 0);
445 case FILTERING_ACCOUNT_RULES_USE_CURRENT:
446 matches = ((filtering->account_id == 0)
447 || (filtering->account_id == cur_account->account_id));
452 return matches && matcherlist_match(filtering->matchers, info);
456 *\brief Apply a rule on message.
458 *\param filtering List of filtering rules.
459 *\param info Message to apply rules on.
460 *\param final Variable returning TRUE or FALSE if one of the
461 * encountered actions was final.
462 * See also \ref filtering_is_final_action.
464 *\return gboolean TRUE to continue applying rules.
466 static gboolean filtering_apply_rule(FilteringProp *filtering, MsgInfo *info,
469 gboolean result = TRUE;
474 for (tmp = filtering->action_list ; tmp != NULL ; tmp = tmp->next) {
475 FilteringAction * action;
479 if (FALSE == (result = filteringaction_apply(action, info))) {
480 g_warning("No further processing after rule %s\n",
481 filteringaction_to_string(buf, sizeof buf, action));
484 if (filtering_is_final_action(action)) {
493 *\brief Check if an action is "final", i.e. should break further
496 *\param filtering_action Action to check.
498 *\return gboolean TRUE if \a filtering_action is final.
500 static gboolean filtering_is_final_action(FilteringAction *filtering_action)
502 switch(filtering_action->type) {
503 case MATCHACTION_MOVE:
504 case MATCHACTION_DELETE:
505 case MATCHACTION_STOP:
506 case MATCHACTION_MARK_AS_SPAM:
507 return TRUE; /* MsgInfo invalid for message */
513 static gboolean filter_msginfo(GSList * filtering_list, MsgInfo * info, PrefsAccount* ac_prefs)
519 g_return_val_if_fail(info != NULL, TRUE);
521 for (l = filtering_list, final = FALSE, apply_next = FALSE; l != NULL; l = g_slist_next(l)) {
522 FilteringProp * filtering = (FilteringProp *) l->data;
524 if (filtering->enabled && filtering_match_condition(filtering, info, ac_prefs)) {
525 apply_next = filtering_apply_rule(filtering, info, &final);
531 /* put in inbox if a final rule could not be applied, or
532 * the last rule was not a final one. */
533 if ((final && !apply_next) || !final) {
541 *\brief Filter a message against a list of rules.
543 *\param flist List of filter rules.
544 *\param info Message.
546 *\return gboolean TRUE if filter rules handled the message.
548 *\note Returning FALSE means the message was not handled,
549 * and that the calling code should do the default
550 * processing. E.g. \ref inc.c::inc_start moves the
551 * message to the inbox.
553 gboolean filter_message_by_msginfo(GSList *flist, MsgInfo *info, PrefsAccount* ac_prefs)
555 return filter_msginfo(flist, info, ac_prefs);
558 gchar *filteringaction_to_string(gchar *dest, gint destlen, FilteringAction *action)
560 const gchar *command_str;
563 command_str = get_matchparser_tab_str(action->type);
565 if (command_str == NULL)
568 switch(action->type) {
569 case MATCHACTION_MOVE:
570 case MATCHACTION_COPY:
571 case MATCHACTION_EXECUTE:
572 quoted_dest = matcher_quote_str(action->destination);
573 g_snprintf(dest, destlen, "%s \"%s\"", command_str, quoted_dest);
577 case MATCHACTION_DELETE:
578 case MATCHACTION_MARK:
579 case MATCHACTION_UNMARK:
580 case MATCHACTION_LOCK:
581 case MATCHACTION_UNLOCK:
582 case MATCHACTION_MARK_AS_READ:
583 case MATCHACTION_MARK_AS_UNREAD:
584 case MATCHACTION_MARK_AS_SPAM:
585 case MATCHACTION_MARK_AS_HAM:
586 case MATCHACTION_STOP:
587 case MATCHACTION_HIDE:
588 case MATCHACTION_IGNORE:
589 g_snprintf(dest, destlen, "%s", command_str);
592 case MATCHACTION_REDIRECT:
593 case MATCHACTION_FORWARD:
594 case MATCHACTION_FORWARD_AS_ATTACHMENT:
595 quoted_dest = matcher_quote_str(action->destination);
596 g_snprintf(dest, destlen, "%s %d \"%s\"", command_str, action->account_id, quoted_dest);
600 case MATCHACTION_COLOR:
601 g_snprintf(dest, destlen, "%s %d", command_str, action->labelcolor);
604 case MATCHACTION_CHANGE_SCORE:
605 case MATCHACTION_SET_SCORE:
606 g_snprintf(dest, destlen, "%s %d", command_str, action->score);
614 gchar * filteringaction_list_to_string(GSList * action_list)
616 gchar *action_list_str;
621 action_list_str = NULL;
622 for (tmp = action_list ; tmp != NULL ; tmp = tmp->next) {
624 FilteringAction * action;
628 action_str = filteringaction_to_string(buf,
631 if (action_list_str != NULL) {
632 list_str = g_strconcat(action_list_str, " ", action_str, NULL);
633 g_free(action_list_str);
636 list_str = g_strdup(action_str);
638 action_list_str = list_str;
641 return action_list_str;
644 gchar * filteringprop_to_string(FilteringProp * prop)
647 gchar *action_list_str;
648 gchar *filtering_str;
650 action_list_str = filteringaction_list_to_string(prop->action_list);
652 if (action_list_str == NULL)
655 list_str = matcherlist_to_string(prop->matchers);
657 if (list_str == NULL) {
658 g_free(action_list_str);
662 filtering_str = g_strconcat(list_str, " ", action_list_str, NULL);
663 g_free(action_list_str);
666 return filtering_str;
669 void prefs_filtering_free(GSList * prefs_filtering)
671 while (prefs_filtering != NULL) {
672 FilteringProp * filtering = (FilteringProp *)
673 prefs_filtering->data;
674 filteringprop_free(filtering);
675 prefs_filtering = g_slist_remove(prefs_filtering, filtering);
679 static gboolean prefs_filtering_free_func(GNode *node, gpointer data)
681 FolderItem *item = node->data;
683 g_return_val_if_fail(item, FALSE);
684 g_return_val_if_fail(item->prefs, FALSE);
686 prefs_filtering_free(item->prefs->processing);
687 item->prefs->processing = NULL;
692 void prefs_filtering_clear(void)
696 for (cur = folder_get_list() ; cur != NULL ; cur = g_list_next(cur)) {
699 folder = (Folder *) cur->data;
700 g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
701 prefs_filtering_free_func, NULL);
704 prefs_filtering_free(filtering_rules);
705 filtering_rules = NULL;
706 prefs_filtering_free(pre_global_processing);
707 pre_global_processing = NULL;
708 prefs_filtering_free(post_global_processing);
709 post_global_processing = NULL;
712 void prefs_filtering_clear_folder(Folder *folder)
714 g_return_if_fail(folder);
715 g_return_if_fail(folder->node);
717 g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
718 prefs_filtering_free_func, NULL);
719 /* FIXME: Note folder settings were changed, where the updates? */
722 gboolean filtering_peek_per_account_rules(GSList *filtering_list)
723 /* return TRUE if there's at least one per-account filtering rule */
727 for (l = filtering_list; l != NULL; l = g_slist_next(l)) {
728 FilteringProp * filtering = (FilteringProp *) l->data;
730 if (filtering->enabled && (filtering->account_id != 0)) {