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 * pre_global_processing = NULL;
38 GSList * post_global_processing = NULL;
39 GSList * filtering_rules = NULL;
41 static gboolean filtering_is_final_action(FilteringAction *filtering_action);
43 #define STRLEN_WITH_CHECK(expr) \
44 strlen_with_check(#expr, __LINE__, expr)
46 static inline gint strlen_with_check(const gchar *expr, gint fline, const gchar *str)
51 debug_print("%s(%d) - invalid string %s\n", __FILE__, fline, expr);
56 FilteringAction * filteringaction_new(int type, int account_id,
58 gint labelcolor, gint score)
60 FilteringAction * action;
62 action = g_new0(FilteringAction, 1);
65 action->account_id = account_id;
67 action->destination = g_strdup(destination);
69 action->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);
84 FilteringProp * filteringprop_new(MatcherList * matchers,
87 FilteringProp * filtering;
89 filtering = g_new0(FilteringProp, 1);
90 filtering->matchers = matchers;
91 filtering->action_list = action_list;
96 static FilteringAction * filteringaction_copy(FilteringAction * src)
98 FilteringAction * new;
100 new = g_new0(FilteringAction, 1);
102 new->type = src->type;
103 new->account_id = src->account_id;
104 if (src->destination)
105 new->destination = g_strdup(src->destination);
107 new->destination = NULL;
108 new->labelcolor = src->labelcolor;
113 FilteringProp * filteringprop_copy(FilteringProp *src)
118 new = g_new0(FilteringProp, 1);
119 new->matchers = g_new0(MatcherList, 1);
121 for (tmp = src->matchers->matchers; tmp != NULL && tmp->data != NULL;) {
122 MatcherProp *matcher = (MatcherProp *)tmp->data;
124 new->matchers->matchers = g_slist_append(new->matchers->matchers,
125 matcherprop_copy(matcher));
129 new->matchers->bool_and = src->matchers->bool_and;
131 new->action_list = NULL;
133 for (tmp = src->action_list ; tmp != NULL ; tmp = tmp->next) {
134 FilteringAction *filtering_action;
136 filtering_action = tmp->data;
138 new->action_list = g_slist_append(new->action_list,
139 filteringaction_copy(filtering_action));
145 void filteringprop_free(FilteringProp * prop)
149 g_return_if_fail(prop);
150 matcherlist_free(prop->matchers);
152 for (tmp = prop->action_list ; tmp != NULL ; tmp = tmp->next) {
153 filteringaction_free(tmp->data);
159 fitleringaction_apply
160 runs the action on one MsgInfo
161 return value : return TRUE if the action could be applied
164 static gboolean filteringaction_apply(FilteringAction * action, MsgInfo * info)
166 FolderItem * dest_folder;
169 PrefsAccount * account;
172 switch(action->type) {
173 case MATCHACTION_MOVE:
175 folder_find_item_from_identifier(action->destination);
177 debug_print("*** folder not found '%s'\n",
178 action->destination ?action->destination :"");
182 if (folder_item_move_msg(dest_folder, info) == -1) {
183 debug_print("*** could not move message\n");
189 case MATCHACTION_COPY:
191 folder_find_item_from_identifier(action->destination);
194 debug_print("*** folder not found '%s'\n",
195 action->destination ?action->destination :"");
199 if (folder_item_copy_msg(dest_folder, info) == -1)
204 case MATCHACTION_DELETE:
205 if (folder_item_remove_msg(info->folder, info->msgnum) == -1)
209 case MATCHACTION_MARK:
210 procmsg_msginfo_set_flags(info, MSG_MARKED, 0);
213 case MATCHACTION_UNMARK:
214 procmsg_msginfo_unset_flags(info, MSG_MARKED, 0);
217 case MATCHACTION_LOCK:
218 procmsg_msginfo_set_flags(info, MSG_LOCKED, 0);
221 case MATCHACTION_UNLOCK:
222 procmsg_msginfo_unset_flags(info, MSG_LOCKED, 0);
225 case MATCHACTION_MARK_AS_READ:
226 procmsg_msginfo_unset_flags(info, MSG_UNREAD | MSG_NEW, 0);
229 case MATCHACTION_MARK_AS_UNREAD:
230 procmsg_msginfo_set_flags(info, MSG_UNREAD | MSG_NEW, 0);
233 case MATCHACTION_COLOR:
234 procmsg_msginfo_unset_flags(info, MSG_CLABEL_FLAG_MASK, 0);
235 procmsg_msginfo_set_flags(info, MSG_COLORLABEL_TO_FLAGS(action->labelcolor), 0);
238 case MATCHACTION_FORWARD:
239 case MATCHACTION_FORWARD_AS_ATTACHMENT:
240 account = account_find_from_id(action->account_id);
241 compose = compose_forward(account, info,
242 action->type == MATCHACTION_FORWARD ? FALSE : TRUE,
244 compose_entry_append(compose, action->destination,
245 compose->account->protocol == A_NNTP
249 val = compose_send(compose);
250 gtk_widget_destroy(compose->window);
252 return val == 0 ? TRUE : FALSE;
254 case MATCHACTION_REDIRECT:
255 account = account_find_from_id(action->account_id);
256 compose = compose_redirect(account, info);
257 if (compose->account->protocol == A_NNTP)
260 compose_entry_append(compose, action->destination,
263 val = compose_send(compose);
264 gtk_widget_destroy(compose->window);
266 return val == 0 ? TRUE : FALSE;
268 case MATCHACTION_EXECUTE:
269 cmd = matching_build_command(action->destination, info);
278 case MATCHACTION_SET_SCORE:
279 info->score = action->score;
282 case MATCHACTION_CHANGE_SCORE:
283 info->score += action->score;
286 case MATCHACTION_STOP:
289 case MATCHACTION_HIDE:
299 gboolean filteringaction_apply_action_list(GSList *action_list, MsgInfo *info)
302 g_return_val_if_fail(action_list, FALSE);
303 g_return_val_if_fail(info, FALSE);
304 for (p = action_list; p && p->data; p = g_slist_next(p)) {
305 FilteringAction *a = (FilteringAction *) p->data;
306 if (filteringaction_apply(a, info)) {
307 if (filtering_is_final_action(a))
316 static gboolean filtering_match_condition(FilteringProp *filtering, MsgInfo *info)
318 return matcherlist_match(filtering->matchers, info);
321 static gboolean filtering_apply_rule(FilteringProp *filtering, MsgInfo *info,
329 for (tmp = filtering->action_list ; tmp != NULL ; tmp = tmp->next) {
330 FilteringAction * action;
334 if (FALSE == (result = filteringaction_apply(action, info))) {
335 g_warning("action %s could not be applied",
336 filteringaction_to_string(buf, sizeof buf, action));
339 if (filtering_is_final_action(action)) {
347 static gboolean filtering_is_final_action(FilteringAction *filtering_action)
349 switch(filtering_action->type) {
350 case MATCHACTION_MOVE:
351 case MATCHACTION_DELETE:
352 case MATCHACTION_STOP:
353 return TRUE; /* MsgInfo invalid for message */
359 static gboolean filter_msginfo(GSList * filtering_list, MsgInfo * info)
365 g_return_val_if_fail(info != NULL, TRUE);
367 for (l = filtering_list, final = FALSE, applied = FALSE; l != NULL; l = g_slist_next(l)) {
368 FilteringProp * filtering = (FilteringProp *) l->data;
370 if (filtering_match_condition(filtering, info)) {
371 applied = filtering_apply_rule(filtering, info, &final);
377 /* put in inbox if a final rule could not be applied, or
378 * the last rule was not a final one. */
379 if ((final && !applied) || !final) {
386 gboolean filter_message_by_msginfo(GSList *flist, MsgInfo *info)
388 return filter_msginfo(flist, info);
391 gchar *filteringaction_to_string(gchar *dest, gint destlen, FilteringAction *action)
393 const gchar *command_str;
396 command_str = get_matchparser_tab_str(action->type);
398 if (command_str == NULL)
401 switch(action->type) {
402 case MATCHACTION_MOVE:
403 case MATCHACTION_COPY:
404 case MATCHACTION_EXECUTE:
405 quoted_dest = matcher_quote_str(action->destination);
406 g_snprintf(dest, destlen, "%s \"%s\"", command_str, quoted_dest);
410 case MATCHACTION_DELETE:
411 case MATCHACTION_MARK:
412 case MATCHACTION_UNMARK:
413 case MATCHACTION_LOCK:
414 case MATCHACTION_UNLOCK:
415 case MATCHACTION_MARK_AS_READ:
416 case MATCHACTION_MARK_AS_UNREAD:
417 case MATCHACTION_STOP:
418 case MATCHACTION_HIDE:
419 g_snprintf(dest, destlen, "%s", command_str);
422 case MATCHACTION_REDIRECT:
423 case MATCHACTION_FORWARD:
424 case MATCHACTION_FORWARD_AS_ATTACHMENT:
425 quoted_dest = matcher_quote_str(action->destination);
426 g_snprintf(dest, destlen, "%s %d \"%s\"", command_str, action->account_id, quoted_dest);
430 case MATCHACTION_COLOR:
431 g_snprintf(dest, destlen, "%s %d", command_str, action->labelcolor);
434 case MATCHACTION_CHANGE_SCORE:
435 case MATCHACTION_SET_SCORE:
436 g_snprintf(dest, destlen, "%s %d", command_str, action->score);
444 gchar * filteringaction_list_to_string(GSList * action_list)
446 gchar *action_list_str;
451 action_list_str = NULL;
452 for (tmp = action_list ; tmp != NULL ; tmp = tmp->next) {
454 FilteringAction * action;
458 action_str = filteringaction_to_string(buf,
461 if (action_list_str != NULL) {
462 list_str = g_strconcat(action_list_str, " ", action_str, NULL);
463 g_free(action_list_str);
466 list_str = g_strdup(action_str);
468 action_list_str = list_str;
471 return action_list_str;
474 gchar * filteringprop_to_string(FilteringProp * prop)
477 gchar *action_list_str;
478 gchar *filtering_str;
480 action_list_str = filteringaction_list_to_string(prop->action_list);
482 if (action_list_str == NULL)
485 list_str = matcherlist_to_string(prop->matchers);
487 if (list_str == NULL) {
488 g_free(action_list_str);
492 filtering_str = g_strconcat(list_str, " ", action_list_str, NULL);
493 g_free(action_list_str);
496 return filtering_str;
499 void prefs_filtering_free(GSList * prefs_filtering)
501 while (prefs_filtering != NULL) {
502 FilteringProp * filtering = (FilteringProp *)
503 prefs_filtering->data;
504 filteringprop_free(filtering);
505 prefs_filtering = g_slist_remove(prefs_filtering, filtering);
509 static gboolean prefs_filtering_free_func(GNode *node, gpointer data)
511 FolderItem *item = node->data;
513 g_return_val_if_fail(item, FALSE);
514 g_return_val_if_fail(item->prefs, FALSE);
516 prefs_filtering_free(item->prefs->processing);
517 item->prefs->processing = NULL;
522 void prefs_filtering_clear(void)
526 for (cur = folder_get_list() ; cur != NULL ; cur = g_list_next(cur)) {
529 folder = (Folder *) cur->data;
530 g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
531 prefs_filtering_free_func, NULL);
534 prefs_filtering_free(filtering_rules);
535 filtering_rules = NULL;
536 prefs_filtering_free(pre_global_processing);
537 pre_global_processing = NULL;
538 prefs_filtering_free(post_global_processing);
539 post_global_processing = NULL;
542 void prefs_filtering_clear_folder(Folder *folder)
544 g_return_if_fail(folder);
545 g_return_if_fail(folder->node);
547 g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
548 prefs_filtering_free_func, NULL);
549 /* FIXME: Note folder settings were changed, where the updates? */