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 #define STRLEN_WITH_CHECK(expr) \
40 strlen_with_check(#expr, __LINE__, expr)
42 static inline gint strlen_with_check(const gchar *expr, gint fline, const gchar *str)
47 debug_print("%s(%d) - invalid string %s\n", __FILE__, fline, expr);
52 FilteringAction * filteringaction_new(int type, int account_id,
56 FilteringAction * action;
58 action = g_new0(FilteringAction, 1);
61 * if type is MATCHACTION_CHANGE_SCORE, account_id = (-1, 0, 1) and
62 * labelcolor = the score value change
66 action->account_id = account_id;
68 action->destination = g_strdup(destination);
69 action->unesc_destination = matcher_unescape_str(g_strdup(destination));
71 action->destination = NULL;
72 action->unesc_destination = NULL;
74 action->labelcolor = labelcolor;
78 void filteringaction_free(FilteringAction * action)
80 g_return_if_fail(action);
81 if (action->destination)
82 g_free(action->destination);
83 if (action->unesc_destination)
84 g_free(action->unesc_destination);
88 FilteringProp * filteringprop_new(MatcherList * matchers,
89 FilteringAction * action)
91 FilteringProp * filtering;
93 filtering = g_new0(FilteringProp, 1);
94 filtering->matchers = matchers;
95 filtering->action = action;
100 FilteringProp * filteringprop_copy(FilteringProp *src)
105 new = g_new0(FilteringProp, 1);
106 new->matchers = g_new0(MatcherList, 1);
107 new->action = g_new0(FilteringAction, 1);
108 for (tmp = src->matchers->matchers; tmp != NULL && tmp->data != NULL;) {
109 MatcherProp *matcher = (MatcherProp *)tmp->data;
111 new->matchers->matchers = g_slist_append(new->matchers->matchers,
112 matcherprop_copy(matcher));
115 new->matchers->bool_and = src->matchers->bool_and;
116 new->action->type = src->action->type;
117 new->action->account_id = src->action->account_id;
118 if (src->action->destination)
119 new->action->destination = g_strdup(src->action->destination);
121 new->action->destination = NULL;
122 if (src->action->unesc_destination)
123 new->action->unesc_destination = g_strdup(src->action->unesc_destination);
125 new->action->unesc_destination = NULL;
126 new->action->labelcolor = src->action->labelcolor;
130 void filteringprop_free(FilteringProp * prop)
132 matcherlist_free(prop->matchers);
133 filteringaction_free(prop->action);
138 fitleringaction_apply
139 runs the action on one MsgInfo
140 return value : return TRUE if the action could be applied
143 static gboolean filteringaction_apply(FilteringAction * action, MsgInfo * info)
145 FolderItem * dest_folder;
148 PrefsAccount * account;
151 switch(action->type) {
152 case MATCHACTION_MOVE:
154 folder_find_item_from_identifier(action->destination);
158 if (folder_item_move_msg(dest_folder, info) == -1) {
159 debug_print("*** could not move message\n");
165 case MATCHACTION_COPY:
167 folder_find_item_from_identifier(action->destination);
172 if (folder_item_copy_msg(dest_folder, info) == -1)
177 case MATCHACTION_DELETE:
178 if (folder_item_remove_msg(info->folder, info->msgnum) == -1)
182 case MATCHACTION_MARK:
183 procmsg_msginfo_set_flags(info, MSG_MARKED, 0);
186 case MATCHACTION_UNMARK:
187 procmsg_msginfo_unset_flags(info, MSG_MARKED, 0);
190 case MATCHACTION_MARK_AS_READ:
191 procmsg_msginfo_unset_flags(info, MSG_UNREAD | MSG_NEW, 0);
194 case MATCHACTION_MARK_AS_UNREAD:
195 debug_print("*** setting unread flags\n");
196 procmsg_msginfo_set_flags(info, MSG_UNREAD | MSG_NEW, 0);
199 case MATCHACTION_COLOR:
200 procmsg_msginfo_unset_flags(info, MSG_CLABEL_FLAG_MASK, 0);
201 procmsg_msginfo_set_flags(info, MSG_COLORLABEL_TO_FLAGS(action->labelcolor), 0);
204 case MATCHACTION_FORWARD:
205 account = account_find_from_id(action->account_id);
206 compose = compose_forward(account, info, FALSE, NULL);
207 if (compose->account->protocol == A_NNTP)
208 compose_entry_append(compose, action->destination,
211 compose_entry_append(compose, action->destination,
214 val = compose_send(compose);
216 gtk_widget_destroy(compose->window);
220 gtk_widget_destroy(compose->window);
223 case MATCHACTION_FORWARD_AS_ATTACHMENT:
225 account = account_find_from_id(action->account_id);
226 compose = compose_forward(account, info, TRUE, NULL);
227 if (compose->account->protocol == A_NNTP)
228 compose_entry_append(compose, action->destination,
231 compose_entry_append(compose, action->destination,
234 val = compose_send(compose);
236 gtk_widget_destroy(compose->window);
239 gtk_widget_destroy(compose->window);
242 case MATCHACTION_REDIRECT:
243 account = account_find_from_id(action->account_id);
244 compose = compose_redirect(account, info);
245 if (compose->account->protocol == A_NNTP)
248 compose_entry_append(compose, action->destination,
251 val = compose_send(compose);
253 gtk_widget_destroy(compose->window);
257 gtk_widget_destroy(compose->window);
260 case MATCHACTION_EXECUTE:
261 cmd = matching_build_command(action->unesc_destination, info);
270 case MATCHACTION_CHANGE_SCORE:
272 * action->account_id is 0 if just assignment, -1 if decrement
273 * and 1 if increment by action->labelcolor
274 * action->labelcolor has the score value change
276 info->score = action->account_id == 1 ? info->score + action->labelcolor
277 : action->account_id == -1 ? info->score - action->labelcolor
278 : action->labelcolor;
287 static gboolean filtering_match_condition(FilteringProp *filtering, MsgInfo *info)
289 return matcherlist_match(filtering->matchers, info);
292 static gboolean filtering_apply_rule(FilteringProp *filtering, MsgInfo *info)
297 if (FALSE == (result = filteringaction_apply(filtering->action, info))) {
298 g_warning("action %s could not be applied",
299 filteringaction_to_string(buf, sizeof buf, filtering->action));
304 static gboolean filtering_is_final_action(FilteringProp *filtering)
306 switch(filtering->action->type) {
307 case MATCHACTION_MOVE:
308 case MATCHACTION_DELETE:
309 return TRUE; /* MsgInfo invalid for message */
310 case MATCHACTION_EXECUTE:
311 case MATCHACTION_COPY:
312 case MATCHACTION_MARK:
313 case MATCHACTION_MARK_AS_READ:
314 case MATCHACTION_UNMARK:
315 case MATCHACTION_MARK_AS_UNREAD:
316 case MATCHACTION_FORWARD:
317 case MATCHACTION_FORWARD_AS_ATTACHMENT:
318 case MATCHACTION_REDIRECT:
319 return FALSE; /* MsgInfo still valid for message */
325 static gboolean filter_msginfo(GSList * filtering_list, MsgInfo * info)
331 g_return_val_if_fail(info != NULL, TRUE);
333 for (l = filtering_list, final = FALSE, applied = FALSE; l != NULL; l = g_slist_next(l)) {
334 FilteringProp * filtering = (FilteringProp *) l->data;
336 if (filtering_match_condition(filtering, info)) {
337 applied = filtering_apply_rule(filtering, info);
338 if (TRUE == (final = filtering_is_final_action(filtering)))
343 /* put in inbox if a final rule could not be applied, or
344 * the last rule was not a final one. */
345 if ((final && !applied) || !final) {
352 gboolean filter_message_by_msginfo(GSList *flist, MsgInfo *info)
354 return filter_msginfo(flist, info);
357 gchar *filteringaction_to_string(gchar *dest, gint destlen, FilteringAction *action)
359 const gchar *command_str;
361 command_str = get_matchparser_tab_str(action->type);
363 if (command_str == NULL)
366 switch(action->type) {
367 case MATCHACTION_MOVE:
368 case MATCHACTION_COPY:
369 case MATCHACTION_EXECUTE:
370 g_snprintf(dest, destlen, "%s \"%s\"", command_str, action->destination);
373 case MATCHACTION_DELETE:
374 case MATCHACTION_MARK:
375 case MATCHACTION_UNMARK:
376 case MATCHACTION_MARK_AS_READ:
377 case MATCHACTION_MARK_AS_UNREAD:
378 g_snprintf(dest, destlen, "%s", command_str);
381 case MATCHACTION_REDIRECT:
382 case MATCHACTION_FORWARD:
383 case MATCHACTION_FORWARD_AS_ATTACHMENT:
384 g_snprintf(dest, destlen, "%s %d \"%s\"", command_str, action->account_id, action->destination);
387 case MATCHACTION_COLOR:
388 g_snprintf(dest, destlen, "%s %d", command_str, action->labelcolor);
395 gchar * filteringprop_to_string(FilteringProp * prop)
399 gchar *filtering_str;
402 action_str = filteringaction_to_string(buf, sizeof buf, prop->action);
404 if (action_str == NULL)
407 list_str = matcherlist_to_string(prop->matchers);
409 if (list_str == NULL)
412 filtering_str = g_strconcat(list_str, " ", action_str, NULL);
415 return filtering_str;
418 void prefs_filtering_free(GSList * prefs_filtering)
420 while (prefs_filtering != NULL) {
421 FilteringProp * filtering = (FilteringProp *)
422 prefs_filtering->data;
423 filteringprop_free(filtering);
424 prefs_filtering = g_slist_remove(prefs_filtering, filtering);
428 static gboolean prefs_filtering_free_func(GNode *node, gpointer data)
430 FolderItem *item = node->data;
435 prefs_filtering_free(item->prefs->processing);
436 item->prefs->processing = NULL;
441 void prefs_filtering_clear(void)
445 for (cur = folder_get_list() ; cur != NULL ; cur = g_list_next(cur)) {
448 folder = (Folder *) cur->data;
449 g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
450 prefs_filtering_free_func, NULL);
453 prefs_filtering_free(global_processing);
454 global_processing = NULL;