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,
56 gint labelcolor, gint score)
58 FilteringAction * action;
60 action = g_new0(FilteringAction, 1);
63 action->account_id = account_id;
65 action->destination = g_strdup(destination);
66 action->unesc_destination = matcher_unescape_str(g_strdup(destination));
68 action->destination = NULL;
69 action->unesc_destination = NULL;
71 action->labelcolor = labelcolor;
72 action->score = score;
76 void filteringaction_free(FilteringAction * action)
78 g_return_if_fail(action);
79 if (action->destination)
80 g_free(action->destination);
81 if (action->unesc_destination)
82 g_free(action->unesc_destination);
86 FilteringProp * filteringprop_new(MatcherList * matchers,
89 FilteringProp * filtering;
91 filtering = g_new0(FilteringProp, 1);
92 filtering->matchers = matchers;
93 filtering->action_list = action_list;
98 static FilteringAction * filteringaction_copy(FilteringAction * src)
100 FilteringAction * new;
102 new = g_new0(FilteringAction, 1);
104 new->type = src->type;
105 new->account_id = src->account_id;
106 if (src->destination)
107 new->destination = g_strdup(src->destination);
109 new->destination = NULL;
110 if (src->unesc_destination)
111 new->unesc_destination = g_strdup(src->unesc_destination);
113 new->unesc_destination = NULL;
114 new->labelcolor = src->labelcolor;
119 FilteringProp * filteringprop_copy(FilteringProp *src)
124 new = g_new0(FilteringProp, 1);
125 new->matchers = g_new0(MatcherList, 1);
128 new->action = g_new0(FilteringAction, 1);
131 for (tmp = src->matchers->matchers; tmp != NULL && tmp->data != NULL;) {
132 MatcherProp *matcher = (MatcherProp *)tmp->data;
134 new->matchers->matchers = g_slist_append(new->matchers->matchers,
135 matcherprop_copy(matcher));
139 new->matchers->bool_and = src->matchers->bool_and;
142 new->action->type = src->action->type;
143 new->action->account_id = src->action->account_id;
144 if (src->action->destination)
145 new->action->destination = g_strdup(src->action->destination);
147 new->action->destination = NULL;
148 if (src->action->unesc_destination)
149 new->action->unesc_destination = g_strdup(src->action->unesc_destination);
151 new->action->unesc_destination = NULL;
152 new->action->labelcolor = src->action->labelcolor;
154 new->action_list = NULL;
156 for (tmp = src->action_list ; tmp != NULL ; tmp = tmp->next) {
157 FilteringAction *filtering_action;
159 filtering_action = tmp->data;
161 new->action_list = g_slist_append(new->action_list,
162 filteringaction_copy(filtering_action));
168 void filteringprop_free(FilteringProp * prop)
172 g_return_if_fail(prop);
173 matcherlist_free(prop->matchers);
175 for (tmp = prop->action_list ; tmp != NULL ; tmp = tmp->next) {
176 filteringaction_free(tmp->data);
182 fitleringaction_apply
183 runs the action on one MsgInfo
184 return value : return TRUE if the action could be applied
187 static gboolean filteringaction_apply(FilteringAction * action, MsgInfo * info)
189 FolderItem * dest_folder;
192 PrefsAccount * account;
195 switch(action->type) {
196 case MATCHACTION_MOVE:
198 folder_find_item_from_identifier(action->destination);
202 if (folder_item_move_msg(dest_folder, info) == -1) {
203 debug_print("*** could not move message\n");
209 case MATCHACTION_COPY:
211 folder_find_item_from_identifier(action->destination);
216 if (folder_item_copy_msg(dest_folder, info) == -1)
221 case MATCHACTION_DELETE:
222 if (folder_item_remove_msg(info->folder, info->msgnum) == -1)
226 case MATCHACTION_MARK:
227 procmsg_msginfo_set_flags(info, MSG_MARKED, 0);
230 case MATCHACTION_UNMARK:
231 procmsg_msginfo_unset_flags(info, MSG_MARKED, 0);
234 case MATCHACTION_LOCK:
235 procmsg_msginfo_set_flags(info, MSG_LOCKED, 0);
238 case MATCHACTION_UNLOCK:
239 procmsg_msginfo_unset_flags(info, MSG_LOCKED, 0);
242 case MATCHACTION_MARK_AS_READ:
243 procmsg_msginfo_unset_flags(info, MSG_UNREAD | MSG_NEW, 0);
246 case MATCHACTION_MARK_AS_UNREAD:
247 procmsg_msginfo_set_flags(info, MSG_UNREAD | MSG_NEW, 0);
250 case MATCHACTION_COLOR:
251 procmsg_msginfo_unset_flags(info, MSG_CLABEL_FLAG_MASK, 0);
252 procmsg_msginfo_set_flags(info, MSG_COLORLABEL_TO_FLAGS(action->labelcolor), 0);
255 case MATCHACTION_FORWARD:
256 case MATCHACTION_FORWARD_AS_ATTACHMENT:
257 account = account_find_from_id(action->account_id);
258 compose = compose_forward(account, info,
259 action->type == MATCHACTION_FORWARD ? FALSE : TRUE,
261 compose_entry_append(compose, action->destination,
262 compose->account->protocol == A_NNTP
266 val = compose_send(compose);
267 gtk_widget_destroy(compose->window);
269 return val == 0 ? TRUE : FALSE;
271 case MATCHACTION_REDIRECT:
272 account = account_find_from_id(action->account_id);
273 compose = compose_redirect(account, info);
274 if (compose->account->protocol == A_NNTP)
277 compose_entry_append(compose, action->destination,
280 val = compose_send(compose);
281 gtk_widget_destroy(compose->window);
283 return val == 0 ? TRUE : FALSE;
285 case MATCHACTION_EXECUTE:
286 cmd = matching_build_command(action->unesc_destination, info);
295 case MATCHACTION_SET_SCORE:
296 info->score = action->score;
299 case MATCHACTION_ADD_SCORE:
300 info->score += action->score;
303 case MATCHACTION_STOP:
312 gboolean filteringaction_apply_action_list(GSList *action_list, MsgInfo *info)
315 g_return_val_if_fail(action_list, FALSE);
316 g_return_val_if_fail(info, FALSE);
317 for (p = action_list; p && p->data; p = g_slist_next(p)) {
318 FilteringAction *a = (FilteringAction *) p->data;
319 if (filteringaction_apply(a, info)) {
320 if (filtering_is_final_action(a))
329 static gboolean filtering_match_condition(FilteringProp *filtering, MsgInfo *info)
331 return matcherlist_match(filtering->matchers, info);
334 static gboolean filtering_apply_rule(FilteringProp *filtering, MsgInfo *info,
342 for (tmp = filtering->action_list ; tmp != NULL ; tmp = tmp->next) {
343 FilteringAction * action;
347 if (FALSE == (result = filteringaction_apply(action, info))) {
348 g_warning("action %s could not be applied",
349 filteringaction_to_string(buf, sizeof buf, action));
352 if (filtering_is_final_action(action)) {
360 static gboolean filtering_is_final_action(FilteringAction *filtering_action)
362 switch(filtering_action->type) {
363 case MATCHACTION_MOVE:
364 case MATCHACTION_DELETE:
365 case MATCHACTION_STOP:
366 return TRUE; /* MsgInfo invalid for message */
372 static gboolean filter_msginfo(GSList * filtering_list, MsgInfo * info)
378 g_return_val_if_fail(info != NULL, TRUE);
380 for (l = filtering_list, final = FALSE, applied = FALSE; l != NULL; l = g_slist_next(l)) {
381 FilteringProp * filtering = (FilteringProp *) l->data;
383 if (filtering_match_condition(filtering, info)) {
384 applied = filtering_apply_rule(filtering, info, &final);
386 if (TRUE == (final = filtering_is_final_action(filtering)))
394 /* put in inbox if a final rule could not be applied, or
395 * the last rule was not a final one. */
396 if ((final && !applied) || !final) {
403 gboolean filter_message_by_msginfo(GSList *flist, MsgInfo *info)
405 return filter_msginfo(flist, info);
408 gchar *filteringaction_to_string(gchar *dest, gint destlen, FilteringAction *action)
410 const gchar *command_str;
412 command_str = get_matchparser_tab_str(action->type);
414 if (command_str == NULL)
417 switch(action->type) {
418 case MATCHACTION_MOVE:
419 case MATCHACTION_COPY:
420 case MATCHACTION_EXECUTE:
421 g_snprintf(dest, destlen, "%s \"%s\"", command_str, action->destination);
424 case MATCHACTION_DELETE:
425 case MATCHACTION_MARK:
426 case MATCHACTION_UNMARK:
427 case MATCHACTION_LOCK:
428 case MATCHACTION_UNLOCK:
429 case MATCHACTION_MARK_AS_READ:
430 case MATCHACTION_MARK_AS_UNREAD:
431 case MATCHACTION_STOP:
432 g_snprintf(dest, destlen, "%s", command_str);
435 case MATCHACTION_REDIRECT:
436 case MATCHACTION_FORWARD:
437 case MATCHACTION_FORWARD_AS_ATTACHMENT:
438 g_snprintf(dest, destlen, "%s %d \"%s\"", command_str, action->account_id, action->destination);
441 case MATCHACTION_COLOR:
442 g_snprintf(dest, destlen, "%s %d", command_str, action->labelcolor);
445 case MATCHACTION_ADD_SCORE:
446 case MATCHACTION_SET_SCORE:
447 g_snprintf(dest, destlen, "%s %d", command_str, action->score);
455 gchar * filteringaction_list_to_string(GSList * action_list)
457 gchar *action_list_str;
462 action_list_str = NULL;
463 for (tmp = action_list ; tmp != NULL ; tmp = tmp->next) {
465 FilteringAction * action;
469 action_str = filteringaction_to_string(buf,
472 if (action_list_str != NULL) {
473 list_str = g_strconcat(action_list_str, " ", action_str, NULL);
474 g_free(action_list_str);
477 list_str = g_strdup(action_str);
479 action_list_str = list_str;
482 return action_list_str;
485 gchar * filteringprop_to_string(FilteringProp * prop)
488 gchar *action_list_str;
489 gchar *filtering_str;
492 action_list_str = filteringaction_list_to_string(prop->action_list);
494 if (action_list_str == NULL)
497 list_str = matcherlist_to_string(prop->matchers);
499 if (list_str == NULL) {
500 g_free(action_list_str);
504 filtering_str = g_strconcat(list_str, " ", action_list_str, NULL);
505 g_free(action_list_str);
508 return filtering_str;
511 void prefs_filtering_free(GSList * prefs_filtering)
513 while (prefs_filtering != NULL) {
514 FilteringProp * filtering = (FilteringProp *)
515 prefs_filtering->data;
516 filteringprop_free(filtering);
517 prefs_filtering = g_slist_remove(prefs_filtering, filtering);
521 static gboolean prefs_filtering_free_func(GNode *node, gpointer data)
523 FolderItem *item = node->data;
525 g_return_val_if_fail(item, FALSE);
526 g_return_val_if_fail(item->prefs, FALSE);
528 prefs_filtering_free(item->prefs->processing);
529 item->prefs->processing = NULL;
534 void prefs_filtering_clear(void)
538 for (cur = folder_get_list() ; cur != NULL ; cur = g_list_next(cur)) {
541 folder = (Folder *) cur->data;
542 g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
543 prefs_filtering_free_func, NULL);
546 prefs_filtering_free(global_processing);
547 global_processing = NULL;
550 void prefs_filtering_clear_folder(Folder *folder)
552 g_return_if_fail(folder);
553 g_return_if_fail(folder->node);
555 g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
556 prefs_filtering_free_func, NULL);
557 /* FIXME: Note folder settings were changed, where the updates? */