2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2006 Hiroyuki Yamamoto & The Sylpheed Claws 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"
37 #define PREFSBUFSIZE 1024
39 GSList * pre_global_processing = NULL;
40 GSList * post_global_processing = NULL;
41 GSList * filtering_rules = NULL;
43 static gboolean filtering_is_final_action(FilteringAction *filtering_action);
45 #define STRLEN_WITH_CHECK(expr) \
46 strlen_with_check(#expr, __LINE__, expr)
48 static inline gint strlen_with_check(const gchar *expr, gint fline, const gchar *str)
53 debug_print("%s(%d) - invalid string %s\n", __FILE__, fline, expr);
58 FilteringAction * filteringaction_new(int type, int account_id,
60 gint labelcolor, gint score)
62 FilteringAction * action;
64 action = g_new0(FilteringAction, 1);
67 action->account_id = account_id;
69 action->destination = g_strdup(destination);
71 action->destination = NULL;
73 action->labelcolor = labelcolor;
74 action->score = score;
78 void filteringaction_free(FilteringAction * action)
80 g_return_if_fail(action);
81 g_free(action->destination);
85 FilteringProp * filteringprop_new(gboolean enabled,
87 MatcherList * matchers,
90 FilteringProp * filtering;
92 filtering = g_new0(FilteringProp, 1);
93 filtering->enabled = enabled;
94 filtering->name = name ? g_strdup(name): NULL;
95 filtering->matchers = matchers;
96 filtering->action_list = action_list;
101 static FilteringAction * filteringaction_copy(FilteringAction * src)
103 FilteringAction * new;
105 new = g_new0(FilteringAction, 1);
107 new->type = src->type;
108 new->account_id = src->account_id;
109 if (src->destination)
110 new->destination = g_strdup(src->destination);
112 new->destination = NULL;
113 new->labelcolor = src->labelcolor;
114 new->score = src->score;
119 FilteringProp * filteringprop_copy(FilteringProp *src)
124 new = g_new0(FilteringProp, 1);
125 new->matchers = g_new0(MatcherList, 1);
127 for (tmp = src->matchers->matchers; tmp != NULL && tmp->data != NULL;) {
128 MatcherProp *matcher = (MatcherProp *)tmp->data;
130 new->matchers->matchers = g_slist_append(new->matchers->matchers,
131 matcherprop_copy(matcher));
135 new->matchers->bool_and = src->matchers->bool_and;
137 new->action_list = NULL;
139 for (tmp = src->action_list ; tmp != NULL ; tmp = tmp->next) {
140 FilteringAction *filtering_action;
142 filtering_action = tmp->data;
144 new->action_list = g_slist_append(new->action_list,
145 filteringaction_copy(filtering_action));
148 new->enabled = src->enabled;
149 new->name = g_strdup(src->name);
154 void filteringprop_free(FilteringProp * prop)
158 g_return_if_fail(prop);
159 matcherlist_free(prop->matchers);
161 for (tmp = prop->action_list ; tmp != NULL ; tmp = tmp->next) {
162 filteringaction_free(tmp->data);
168 void filtering_move_and_copy_msg(MsgInfo *msginfo)
170 GSList *list = g_slist_append(NULL, msginfo);
171 filtering_move_and_copy_msgs(list);
175 /* move and copy messages by batches to be faster on IMAP */
176 void filtering_move_and_copy_msgs(GSList *msgs)
178 GSList *messages = g_slist_copy(msgs);
179 FolderItem *last_item = NULL;
180 gboolean is_copy = FALSE, is_move = FALSE;
183 GSList *batch = NULL, *cur;
185 for (cur = messages; cur; cur = cur->next) {
186 MsgInfo *info = (MsgInfo *)cur->data;
187 if (last_item == NULL) {
188 last_item = info->to_filter_folder;
190 if (last_item == NULL)
192 if (!is_copy && !is_move) {
195 else if (info->is_move)
199 if (info->to_filter_folder == last_item
200 && info->is_copy == is_copy
201 && info->is_move == is_move) {
202 batch = g_slist_prepend(batch, info);
206 debug_print("no more messages to move/copy\n");
209 for (cur = batch; cur; cur = cur->next) {
210 MsgInfo *info = (MsgInfo *)cur->data;
211 messages = g_slist_remove(messages, info);
213 batch = g_slist_reverse(batch);
214 if (g_slist_length(batch)) {
215 MsgInfo *info = (MsgInfo *)batch->data;
216 if (is_copy && last_item != info->folder) {
217 folder_item_copy_msgs(last_item, batch);
218 } else if (is_move && last_item != info->folder) {
219 if (folder_item_move_msgs(last_item, batch) < 0)
220 folder_item_move_msgs(
221 folder_get_default_inbox(),
224 /* we don't reference the msginfos, because caller will do */
232 /* we don't reference the msginfos, because caller will do */
233 g_slist_free(messages);
237 fitleringaction_apply
238 runs the action on one MsgInfo
239 return value : return TRUE if the action could be applied
242 static gboolean filteringaction_apply(FilteringAction * action, MsgInfo * info)
244 FolderItem * dest_folder;
247 PrefsAccount * account;
250 switch(action->type) {
251 case MATCHACTION_MOVE:
253 folder_find_item_from_identifier(action->destination);
255 debug_print("*** folder not found '%s'\n",
256 action->destination ?action->destination :"");
260 /* check if mail is set to copy already,
261 * in which case we have to do it */
262 if (info->is_copy && info->to_filter_folder) {
263 debug_print("should cp and mv !\n");
264 folder_item_copy_msg(info->to_filter_folder, info);
265 info->is_copy = FALSE;
267 /* mark message to be moved */
268 info->is_move = TRUE;
269 info->to_filter_folder = dest_folder;
272 case MATCHACTION_COPY:
274 folder_find_item_from_identifier(action->destination);
277 debug_print("*** folder not found '%s'\n",
278 action->destination ?action->destination :"");
282 /* check if mail is set to copy already,
283 * in which case we have to do it */
284 if (info->is_copy && info->to_filter_folder) {
285 debug_print("should cp and mv !\n");
286 folder_item_copy_msg(info->to_filter_folder, info);
287 info->is_copy = FALSE;
289 /* mark message to be copied */
290 info->is_copy = TRUE;
291 info->to_filter_folder = dest_folder;
294 case MATCHACTION_DELETE:
295 if (folder_item_remove_msg(info->folder, info->msgnum) == -1)
299 case MATCHACTION_MARK:
300 procmsg_msginfo_set_flags(info, MSG_MARKED, 0);
303 case MATCHACTION_UNMARK:
304 procmsg_msginfo_unset_flags(info, MSG_MARKED, 0);
307 case MATCHACTION_LOCK:
308 procmsg_msginfo_set_flags(info, MSG_LOCKED, 0);
311 case MATCHACTION_UNLOCK:
312 procmsg_msginfo_unset_flags(info, MSG_LOCKED, 0);
315 case MATCHACTION_MARK_AS_READ:
316 procmsg_msginfo_unset_flags(info, MSG_UNREAD | MSG_NEW, 0);
319 case MATCHACTION_MARK_AS_UNREAD:
320 procmsg_msginfo_set_flags(info, MSG_UNREAD | MSG_NEW, 0);
323 case MATCHACTION_COLOR:
324 procmsg_msginfo_unset_flags(info, MSG_CLABEL_FLAG_MASK, 0);
325 procmsg_msginfo_set_flags(info, MSG_COLORLABEL_TO_FLAGS(action->labelcolor), 0);
328 case MATCHACTION_FORWARD:
329 case MATCHACTION_FORWARD_AS_ATTACHMENT:
330 account = account_find_from_id(action->account_id);
331 compose = compose_forward(account, info,
332 action->type == MATCHACTION_FORWARD ? FALSE : TRUE,
334 compose_entry_append(compose, action->destination,
335 compose->account->protocol == A_NNTP
339 val = compose_send(compose);
341 return val == 0 ? TRUE : FALSE;
343 case MATCHACTION_REDIRECT:
344 account = account_find_from_id(action->account_id);
345 compose = compose_redirect(account, info, TRUE);
346 if (compose->account->protocol == A_NNTP)
349 compose_entry_append(compose, action->destination,
352 val = compose_send(compose);
354 return val == 0 ? TRUE : FALSE;
356 case MATCHACTION_EXECUTE:
357 cmd = matching_build_command(action->destination, info);
366 case MATCHACTION_SET_SCORE:
367 info->score = action->score;
370 case MATCHACTION_CHANGE_SCORE:
371 info->score += action->score;
374 case MATCHACTION_STOP:
377 case MATCHACTION_HIDE:
381 case MATCHACTION_IGNORE:
382 procmsg_msginfo_set_flags(info, MSG_IGNORE_THREAD, 0);
391 gboolean filteringaction_apply_action_list(GSList *action_list, MsgInfo *info)
394 g_return_val_if_fail(action_list, FALSE);
395 g_return_val_if_fail(info, FALSE);
396 for (p = action_list; p && p->data; p = g_slist_next(p)) {
397 FilteringAction *a = (FilteringAction *) p->data;
398 if (filteringaction_apply(a, info)) {
399 if (filtering_is_final_action(a))
408 static gboolean filtering_match_condition(FilteringProp *filtering, MsgInfo *info)
410 return matcherlist_match(filtering->matchers, info);
414 *\brief Apply a rule on message.
416 *\param filtering List of filtering rules.
417 *\param info Message to apply rules on.
418 *\param final Variable returning TRUE or FALSE if one of the
419 * encountered actions was final.
420 * See also \ref filtering_is_final_action.
422 *\return gboolean TRUE to continue applying rules.
424 static gboolean filtering_apply_rule(FilteringProp *filtering, MsgInfo *info,
427 gboolean result = TRUE;
432 for (tmp = filtering->action_list ; tmp != NULL ; tmp = tmp->next) {
433 FilteringAction * action;
437 if (FALSE == (result = filteringaction_apply(action, info))) {
438 g_warning("No further processing after rule %s\n",
439 filteringaction_to_string(buf, sizeof buf, action));
442 if (filtering_is_final_action(action)) {
451 *\brief Check if an action is "final", i.e. should break further
454 *\param filtering_action Action to check.
456 *\return gboolean TRUE if \a filtering_action is final.
458 static gboolean filtering_is_final_action(FilteringAction *filtering_action)
460 switch(filtering_action->type) {
461 case MATCHACTION_MOVE:
462 case MATCHACTION_DELETE:
463 case MATCHACTION_STOP:
464 return TRUE; /* MsgInfo invalid for message */
470 static gboolean filter_msginfo(GSList * filtering_list, MsgInfo * info)
476 g_return_val_if_fail(info != NULL, TRUE);
478 for (l = filtering_list, final = FALSE, apply_next = FALSE; l != NULL; l = g_slist_next(l)) {
479 FilteringProp * filtering = (FilteringProp *) l->data;
481 if (filtering->enabled && filtering_match_condition(filtering, info)) {
482 apply_next = filtering_apply_rule(filtering, info, &final);
488 /* put in inbox if a final rule could not be applied, or
489 * the last rule was not a final one. */
490 if ((final && !apply_next) || !final) {
498 *\brief Filter a message against a list of rules.
500 *\param flist List of filter rules.
501 *\param info Message.
503 *\return gboolean TRUE if filter rules handled the message.
505 *\note Returning FALSE means the message was not handled,
506 * and that the calling code should do the default
507 * processing. E.g. \ref inc.c::inc_start moves the
508 * message to the inbox.
510 gboolean filter_message_by_msginfo(GSList *flist, MsgInfo *info)
512 return filter_msginfo(flist, info);
515 gchar *filteringaction_to_string(gchar *dest, gint destlen, FilteringAction *action)
517 const gchar *command_str;
520 command_str = get_matchparser_tab_str(action->type);
522 if (command_str == NULL)
525 switch(action->type) {
526 case MATCHACTION_MOVE:
527 case MATCHACTION_COPY:
528 case MATCHACTION_EXECUTE:
529 quoted_dest = matcher_quote_str(action->destination);
530 g_snprintf(dest, destlen, "%s \"%s\"", command_str, quoted_dest);
534 case MATCHACTION_DELETE:
535 case MATCHACTION_MARK:
536 case MATCHACTION_UNMARK:
537 case MATCHACTION_LOCK:
538 case MATCHACTION_UNLOCK:
539 case MATCHACTION_MARK_AS_READ:
540 case MATCHACTION_MARK_AS_UNREAD:
541 case MATCHACTION_STOP:
542 case MATCHACTION_HIDE:
543 case MATCHACTION_IGNORE:
544 g_snprintf(dest, destlen, "%s", command_str);
547 case MATCHACTION_REDIRECT:
548 case MATCHACTION_FORWARD:
549 case MATCHACTION_FORWARD_AS_ATTACHMENT:
550 quoted_dest = matcher_quote_str(action->destination);
551 g_snprintf(dest, destlen, "%s %d \"%s\"", command_str, action->account_id, quoted_dest);
555 case MATCHACTION_COLOR:
556 g_snprintf(dest, destlen, "%s %d", command_str, action->labelcolor);
559 case MATCHACTION_CHANGE_SCORE:
560 case MATCHACTION_SET_SCORE:
561 g_snprintf(dest, destlen, "%s %d", command_str, action->score);
569 gchar * filteringaction_list_to_string(GSList * action_list)
571 gchar *action_list_str;
576 action_list_str = NULL;
577 for (tmp = action_list ; tmp != NULL ; tmp = tmp->next) {
579 FilteringAction * action;
583 action_str = filteringaction_to_string(buf,
586 if (action_list_str != NULL) {
587 list_str = g_strconcat(action_list_str, " ", action_str, NULL);
588 g_free(action_list_str);
591 list_str = g_strdup(action_str);
593 action_list_str = list_str;
596 return action_list_str;
599 gchar * filteringprop_to_string(FilteringProp * prop)
602 gchar *action_list_str;
603 gchar *filtering_str;
605 action_list_str = filteringaction_list_to_string(prop->action_list);
607 if (action_list_str == NULL)
610 list_str = matcherlist_to_string(prop->matchers);
612 if (list_str == NULL) {
613 g_free(action_list_str);
617 filtering_str = g_strconcat(list_str, " ", action_list_str, NULL);
618 g_free(action_list_str);
621 return filtering_str;
624 void prefs_filtering_free(GSList * prefs_filtering)
626 while (prefs_filtering != NULL) {
627 FilteringProp * filtering = (FilteringProp *)
628 prefs_filtering->data;
629 filteringprop_free(filtering);
630 prefs_filtering = g_slist_remove(prefs_filtering, filtering);
634 static gboolean prefs_filtering_free_func(GNode *node, gpointer data)
636 FolderItem *item = node->data;
638 g_return_val_if_fail(item, FALSE);
639 g_return_val_if_fail(item->prefs, FALSE);
641 prefs_filtering_free(item->prefs->processing);
642 item->prefs->processing = NULL;
647 void prefs_filtering_clear(void)
651 for (cur = folder_get_list() ; cur != NULL ; cur = g_list_next(cur)) {
654 folder = (Folder *) cur->data;
655 g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
656 prefs_filtering_free_func, NULL);
659 prefs_filtering_free(filtering_rules);
660 filtering_rules = NULL;
661 prefs_filtering_free(pre_global_processing);
662 pre_global_processing = NULL;
663 prefs_filtering_free(post_global_processing);
664 post_global_processing = NULL;
667 void prefs_filtering_clear_folder(Folder *folder)
669 g_return_if_fail(folder);
670 g_return_if_fail(folder->node);
672 g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
673 prefs_filtering_free_func, NULL);
674 /* FIXME: Note folder settings were changed, where the updates? */