2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2002 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 #include "procheader.h"
31 #include "filtering.h"
32 #include "prefs_gtk.h"
35 #define PREFSBUFSIZE 1024
37 GSList * global_processing = NULL;
39 static gboolean filtering_is_final_action(FilteringAction *filtering_action);
41 #define STRLEN_WITH_CHECK(expr) \
42 strlen_with_check(#expr, __LINE__, expr)
44 static inline gint strlen_with_check(const gchar *expr, gint fline, const gchar *str)
49 debug_print("%s(%d) - invalid string %s\n", __FILE__, fline, expr);
54 FilteringAction * filteringaction_new(int type, int account_id,
58 FilteringAction * action;
60 action = g_new0(FilteringAction, 1);
63 * if type is MATCHACTION_CHANGE_SCORE, account_id = (-1, 0, 1) and
64 * labelcolor = the score value change
68 action->account_id = account_id;
70 action->destination = g_strdup(destination);
71 action->unesc_destination = matcher_unescape_str(g_strdup(destination));
73 action->destination = NULL;
74 action->unesc_destination = NULL;
76 action->labelcolor = labelcolor;
80 void filteringaction_free(FilteringAction * action)
82 g_return_if_fail(action);
83 if (action->destination)
84 g_free(action->destination);
85 if (action->unesc_destination)
86 g_free(action->unesc_destination);
90 FilteringProp * filteringprop_new(MatcherList * matchers,
93 FilteringProp * filtering;
95 filtering = g_new0(FilteringProp, 1);
96 filtering->matchers = matchers;
97 filtering->action_list = action_list;
102 static FilteringAction * filteringaction_copy(FilteringAction * src)
104 FilteringAction * new;
106 new = g_new0(FilteringAction, 1);
108 new->type = src->type;
109 new->account_id = src->account_id;
110 if (src->destination)
111 new->destination = g_strdup(src->destination);
113 new->destination = NULL;
114 if (src->unesc_destination)
115 new->unesc_destination = g_strdup(src->unesc_destination);
117 new->unesc_destination = NULL;
118 new->labelcolor = src->labelcolor;
123 FilteringProp * filteringprop_copy(FilteringProp *src)
128 new = g_new0(FilteringProp, 1);
129 new->matchers = g_new0(MatcherList, 1);
132 new->action = g_new0(FilteringAction, 1);
135 for (tmp = src->matchers->matchers; tmp != NULL && tmp->data != NULL;) {
136 MatcherProp *matcher = (MatcherProp *)tmp->data;
138 new->matchers->matchers = g_slist_append(new->matchers->matchers,
139 matcherprop_copy(matcher));
143 new->matchers->bool_and = src->matchers->bool_and;
146 new->action->type = src->action->type;
147 new->action->account_id = src->action->account_id;
148 if (src->action->destination)
149 new->action->destination = g_strdup(src->action->destination);
151 new->action->destination = NULL;
152 if (src->action->unesc_destination)
153 new->action->unesc_destination = g_strdup(src->action->unesc_destination);
155 new->action->unesc_destination = NULL;
156 new->action->labelcolor = src->action->labelcolor;
158 new->action_list = NULL;
160 for (tmp = src->action_list ; tmp != NULL ; tmp = tmp->next) {
161 FilteringAction *filtering_action;
163 filtering_action = tmp->data;
165 new->action_list = g_slist_append(new->action_list,
166 filteringaction_copy(filtering_action));
172 void filteringprop_free(FilteringProp * prop)
176 g_return_if_fail(prop);
177 matcherlist_free(prop->matchers);
179 for (tmp = prop->action_list ; tmp != NULL ; tmp = tmp->next) {
180 filteringaction_free(tmp->data);
186 fitleringaction_apply
187 runs the action on one MsgInfo
188 return value : return TRUE if the action could be applied
191 static gboolean filteringaction_apply(FilteringAction * action, MsgInfo * info)
193 FolderItem * dest_folder;
196 PrefsAccount * account;
199 switch(action->type) {
200 case MATCHACTION_MOVE:
202 folder_find_item_from_identifier(action->destination);
206 if (folder_item_move_msg(dest_folder, info) == -1) {
207 debug_print("*** could not move message\n");
213 case MATCHACTION_COPY:
215 folder_find_item_from_identifier(action->destination);
220 if (folder_item_copy_msg(dest_folder, info) == -1)
225 case MATCHACTION_DELETE:
226 if (folder_item_remove_msg(info->folder, info->msgnum) == -1)
230 case MATCHACTION_MARK:
231 procmsg_msginfo_set_flags(info, MSG_MARKED, 0);
234 case MATCHACTION_UNMARK:
235 procmsg_msginfo_unset_flags(info, MSG_MARKED, 0);
238 case MATCHACTION_LOCK:
239 procmsg_msginfo_set_flags(info, MSG_LOCKED, 0);
242 case MATCHACTION_UNLOCK:
243 procmsg_msginfo_unset_flags(info, MSG_LOCKED, 0);
246 case MATCHACTION_MARK_AS_READ:
247 procmsg_msginfo_unset_flags(info, MSG_UNREAD | MSG_NEW, 0);
250 case MATCHACTION_MARK_AS_UNREAD:
251 procmsg_msginfo_set_flags(info, MSG_UNREAD | MSG_NEW, 0);
254 case MATCHACTION_COLOR:
255 procmsg_msginfo_unset_flags(info, MSG_CLABEL_FLAG_MASK, 0);
256 procmsg_msginfo_set_flags(info, MSG_COLORLABEL_TO_FLAGS(action->labelcolor), 0);
259 case MATCHACTION_FORWARD:
260 account = account_find_from_id(action->account_id);
261 compose = compose_forward(account, info, FALSE, NULL);
262 if (compose->account->protocol == A_NNTP)
263 compose_entry_append(compose, action->destination,
266 compose_entry_append(compose, action->destination,
269 val = compose_send(compose);
271 gtk_widget_destroy(compose->window);
275 gtk_widget_destroy(compose->window);
278 case MATCHACTION_FORWARD_AS_ATTACHMENT:
280 account = account_find_from_id(action->account_id);
281 compose = compose_forward(account, info, TRUE, NULL);
282 if (compose->account->protocol == A_NNTP)
283 compose_entry_append(compose, action->destination,
286 compose_entry_append(compose, action->destination,
289 val = compose_send(compose);
291 gtk_widget_destroy(compose->window);
294 gtk_widget_destroy(compose->window);
297 case MATCHACTION_REDIRECT:
298 account = account_find_from_id(action->account_id);
299 compose = compose_redirect(account, info);
300 if (compose->account->protocol == A_NNTP)
303 compose_entry_append(compose, action->destination,
306 val = compose_send(compose);
308 gtk_widget_destroy(compose->window);
312 gtk_widget_destroy(compose->window);
315 case MATCHACTION_EXECUTE:
316 cmd = matching_build_command(action->unesc_destination, info);
325 case MATCHACTION_CHANGE_SCORE:
327 * action->account_id is 0 if just assignment, -1 if decrement
328 * and 1 if increment by action->labelcolor
329 * action->labelcolor has the score value change
331 info->score = action->account_id == 1 ? info->score + action->labelcolor
332 : action->account_id == -1 ? info->score - action->labelcolor
333 : action->labelcolor;
342 gboolean filteringaction_apply_action_list(GSList *action_list, MsgInfo *info)
345 g_return_val_if_fail(action_list, FALSE);
346 g_return_val_if_fail(info, FALSE);
347 for (p = action_list; p && p->data; p = g_slist_next(p)) {
348 FilteringAction *a = (FilteringAction *) p->data;
349 if (filteringaction_apply(a, info)) {
350 if (filtering_is_final_action(a))
359 static gboolean filtering_match_condition(FilteringProp *filtering, MsgInfo *info)
361 return matcherlist_match(filtering->matchers, info);
364 static gboolean filtering_apply_rule(FilteringProp *filtering, MsgInfo *info,
372 for (tmp = filtering->action_list ; tmp != NULL ; tmp = tmp->next) {
373 FilteringAction * action;
377 if (FALSE == (result = filteringaction_apply(action, info))) {
378 g_warning("action %s could not be applied",
379 filteringaction_to_string(buf, sizeof buf, action));
382 if (filtering_is_final_action(action)) {
390 static gboolean filtering_is_final_action(FilteringAction *filtering_action)
392 switch(filtering_action->type) {
393 case MATCHACTION_MOVE:
394 case MATCHACTION_DELETE:
395 return TRUE; /* MsgInfo invalid for message */
396 case MATCHACTION_EXECUTE:
397 case MATCHACTION_COPY:
398 case MATCHACTION_MARK:
399 case MATCHACTION_UNMARK:
400 case MATCHACTION_LOCK:
401 case MATCHACTION_UNLOCK:
402 case MATCHACTION_MARK_AS_READ:
403 case MATCHACTION_MARK_AS_UNREAD:
404 case MATCHACTION_FORWARD:
405 case MATCHACTION_FORWARD_AS_ATTACHMENT:
406 case MATCHACTION_REDIRECT:
407 return FALSE; /* MsgInfo still valid for message */
413 static gboolean filter_msginfo(GSList * filtering_list, MsgInfo * info)
419 g_return_val_if_fail(info != NULL, TRUE);
421 for (l = filtering_list, final = FALSE, applied = FALSE; l != NULL; l = g_slist_next(l)) {
422 FilteringProp * filtering = (FilteringProp *) l->data;
424 if (filtering_match_condition(filtering, info)) {
425 applied = filtering_apply_rule(filtering, info, &final);
427 if (TRUE == (final = filtering_is_final_action(filtering)))
435 /* put in inbox if a final rule could not be applied, or
436 * the last rule was not a final one. */
437 if ((final && !applied) || !final) {
444 gboolean filter_message_by_msginfo(GSList *flist, MsgInfo *info)
446 return filter_msginfo(flist, info);
449 gchar *filteringaction_to_string(gchar *dest, gint destlen, FilteringAction *action)
451 const gchar *command_str;
453 command_str = get_matchparser_tab_str(action->type);
455 if (command_str == NULL)
458 switch(action->type) {
459 case MATCHACTION_MOVE:
460 case MATCHACTION_COPY:
461 case MATCHACTION_EXECUTE:
462 g_snprintf(dest, destlen, "%s \"%s\"", command_str, action->destination);
465 case MATCHACTION_DELETE:
466 case MATCHACTION_MARK:
467 case MATCHACTION_UNMARK:
468 case MATCHACTION_LOCK:
469 case MATCHACTION_UNLOCK:
470 case MATCHACTION_MARK_AS_READ:
471 case MATCHACTION_MARK_AS_UNREAD:
472 g_snprintf(dest, destlen, "%s", command_str);
475 case MATCHACTION_REDIRECT:
476 case MATCHACTION_FORWARD:
477 case MATCHACTION_FORWARD_AS_ATTACHMENT:
478 g_snprintf(dest, destlen, "%s %d \"%s\"", command_str, action->account_id, action->destination);
481 case MATCHACTION_COLOR:
482 g_snprintf(dest, destlen, "%s %d", command_str, action->labelcolor);
489 gchar * filteringaction_list_to_string(GSList * action_list)
491 gchar *action_list_str;
496 action_list_str = NULL;
497 for (tmp = action_list ; tmp != NULL ; tmp = tmp->next) {
499 FilteringAction * action;
503 action_str = filteringaction_to_string(buf,
506 if (action_list_str != NULL) {
507 list_str = g_strconcat(action_list_str, " ", action_str, NULL);
508 g_free(action_list_str);
511 list_str = g_strdup(action_str);
513 action_list_str = list_str;
516 return action_list_str;
519 gchar * filteringprop_to_string(FilteringProp * prop)
522 gchar *action_list_str;
523 gchar *filtering_str;
526 action_list_str = filteringaction_list_to_string(prop->action_list);
528 if (action_list_str == NULL)
531 list_str = matcherlist_to_string(prop->matchers);
533 if (list_str == NULL) {
534 g_free(action_list_str);
538 filtering_str = g_strconcat(list_str, " ", action_list_str, NULL);
539 g_free(action_list_str);
542 return filtering_str;
545 void prefs_filtering_free(GSList * prefs_filtering)
547 while (prefs_filtering != NULL) {
548 FilteringProp * filtering = (FilteringProp *)
549 prefs_filtering->data;
550 filteringprop_free(filtering);
551 prefs_filtering = g_slist_remove(prefs_filtering, filtering);
555 static gboolean prefs_filtering_free_func(GNode *node, gpointer data)
557 FolderItem *item = node->data;
559 g_return_val_if_fail(item, FALSE);
560 g_return_val_if_fail(item->prefs, FALSE);
562 prefs_filtering_free(item->prefs->processing);
563 item->prefs->processing = NULL;
568 void prefs_filtering_clear(void)
572 for (cur = folder_get_list() ; cur != NULL ; cur = g_list_next(cur)) {
575 folder = (Folder *) cur->data;
576 g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
577 prefs_filtering_free_func, NULL);
580 prefs_filtering_free(global_processing);
581 global_processing = NULL;
584 void prefs_filtering_clear_folder(Folder *folder)
586 g_return_if_fail(folder);
587 g_return_if_fail(folder->node);
589 g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
590 prefs_filtering_free_func, NULL);
591 /* FIXME: Note folder settings were changed, where the updates? */