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 g_return_if_fail(prop);
133 matcherlist_free(prop->matchers);
134 filteringaction_free(prop->action);
139 fitleringaction_apply
140 runs the action on one MsgInfo
141 return value : return TRUE if the action could be applied
144 static gboolean filteringaction_apply(FilteringAction * action, MsgInfo * info)
146 FolderItem * dest_folder;
149 PrefsAccount * account;
152 switch(action->type) {
153 case MATCHACTION_MOVE:
155 folder_find_item_from_identifier(action->destination);
159 if (folder_item_move_msg(dest_folder, info) == -1) {
160 debug_print("*** could not move message\n");
166 case MATCHACTION_COPY:
168 folder_find_item_from_identifier(action->destination);
173 if (folder_item_copy_msg(dest_folder, info) == -1)
178 case MATCHACTION_DELETE:
179 if (folder_item_remove_msg(info->folder, info->msgnum) == -1)
183 case MATCHACTION_MARK:
184 procmsg_msginfo_set_flags(info, MSG_MARKED, 0);
187 case MATCHACTION_UNMARK:
188 procmsg_msginfo_unset_flags(info, MSG_MARKED, 0);
191 case MATCHACTION_LOCK:
192 procmsg_msginfo_set_flags(info, MSG_LOCKED, 0);
195 case MATCHACTION_UNLOCK:
196 procmsg_msginfo_unset_flags(info, MSG_LOCKED, 0);
199 case MATCHACTION_MARK_AS_READ:
200 procmsg_msginfo_unset_flags(info, MSG_UNREAD | MSG_NEW, 0);
203 case MATCHACTION_MARK_AS_UNREAD:
204 procmsg_msginfo_set_flags(info, MSG_UNREAD | MSG_NEW, 0);
207 case MATCHACTION_COLOR:
208 procmsg_msginfo_unset_flags(info, MSG_CLABEL_FLAG_MASK, 0);
209 procmsg_msginfo_set_flags(info, MSG_COLORLABEL_TO_FLAGS(action->labelcolor), 0);
212 case MATCHACTION_FORWARD:
213 account = account_find_from_id(action->account_id);
214 compose = compose_forward(account, info, FALSE, NULL);
215 if (compose->account->protocol == A_NNTP)
216 compose_entry_append(compose, action->destination,
219 compose_entry_append(compose, action->destination,
222 val = compose_send(compose);
224 gtk_widget_destroy(compose->window);
228 gtk_widget_destroy(compose->window);
231 case MATCHACTION_FORWARD_AS_ATTACHMENT:
233 account = account_find_from_id(action->account_id);
234 compose = compose_forward(account, info, TRUE, NULL);
235 if (compose->account->protocol == A_NNTP)
236 compose_entry_append(compose, action->destination,
239 compose_entry_append(compose, action->destination,
242 val = compose_send(compose);
244 gtk_widget_destroy(compose->window);
247 gtk_widget_destroy(compose->window);
250 case MATCHACTION_REDIRECT:
251 account = account_find_from_id(action->account_id);
252 compose = compose_redirect(account, info);
253 if (compose->account->protocol == A_NNTP)
256 compose_entry_append(compose, action->destination,
259 val = compose_send(compose);
261 gtk_widget_destroy(compose->window);
265 gtk_widget_destroy(compose->window);
268 case MATCHACTION_EXECUTE:
269 cmd = matching_build_command(action->unesc_destination, info);
278 case MATCHACTION_CHANGE_SCORE:
280 * action->account_id is 0 if just assignment, -1 if decrement
281 * and 1 if increment by action->labelcolor
282 * action->labelcolor has the score value change
284 info->score = action->account_id == 1 ? info->score + action->labelcolor
285 : action->account_id == -1 ? info->score - action->labelcolor
286 : action->labelcolor;
295 static gboolean filtering_match_condition(FilteringProp *filtering, MsgInfo *info)
297 return matcherlist_match(filtering->matchers, info);
300 static gboolean filtering_apply_rule(FilteringProp *filtering, MsgInfo *info)
305 if (FALSE == (result = filteringaction_apply(filtering->action, info))) {
306 g_warning("action %s could not be applied",
307 filteringaction_to_string(buf, sizeof buf, filtering->action));
312 static gboolean filtering_is_final_action(FilteringProp *filtering)
314 switch(filtering->action->type) {
315 case MATCHACTION_MOVE:
316 case MATCHACTION_DELETE:
317 return TRUE; /* MsgInfo invalid for message */
318 case MATCHACTION_EXECUTE:
319 case MATCHACTION_COPY:
320 case MATCHACTION_MARK:
321 case MATCHACTION_UNMARK:
322 case MATCHACTION_LOCK:
323 case MATCHACTION_UNLOCK:
324 case MATCHACTION_MARK_AS_READ:
325 case MATCHACTION_MARK_AS_UNREAD:
326 case MATCHACTION_FORWARD:
327 case MATCHACTION_FORWARD_AS_ATTACHMENT:
328 case MATCHACTION_REDIRECT:
329 return FALSE; /* MsgInfo still valid for message */
335 static gboolean filter_msginfo(GSList * filtering_list, MsgInfo * info)
341 g_return_val_if_fail(info != NULL, TRUE);
343 for (l = filtering_list, final = FALSE, applied = FALSE; l != NULL; l = g_slist_next(l)) {
344 FilteringProp * filtering = (FilteringProp *) l->data;
346 if (filtering_match_condition(filtering, info)) {
347 applied = filtering_apply_rule(filtering, info);
348 if (TRUE == (final = filtering_is_final_action(filtering)))
353 /* put in inbox if a final rule could not be applied, or
354 * the last rule was not a final one. */
355 if ((final && !applied) || !final) {
362 gboolean filter_message_by_msginfo(GSList *flist, MsgInfo *info)
364 return filter_msginfo(flist, info);
367 gchar *filteringaction_to_string(gchar *dest, gint destlen, FilteringAction *action)
369 const gchar *command_str;
371 command_str = get_matchparser_tab_str(action->type);
373 if (command_str == NULL)
376 switch(action->type) {
377 case MATCHACTION_MOVE:
378 case MATCHACTION_COPY:
379 case MATCHACTION_EXECUTE:
380 g_snprintf(dest, destlen, "%s \"%s\"", command_str, action->destination);
383 case MATCHACTION_DELETE:
384 case MATCHACTION_MARK:
385 case MATCHACTION_UNMARK:
386 case MATCHACTION_LOCK:
387 case MATCHACTION_UNLOCK:
388 case MATCHACTION_MARK_AS_READ:
389 case MATCHACTION_MARK_AS_UNREAD:
390 g_snprintf(dest, destlen, "%s", command_str);
393 case MATCHACTION_REDIRECT:
394 case MATCHACTION_FORWARD:
395 case MATCHACTION_FORWARD_AS_ATTACHMENT:
396 g_snprintf(dest, destlen, "%s %d \"%s\"", command_str, action->account_id, action->destination);
399 case MATCHACTION_COLOR:
400 g_snprintf(dest, destlen, "%s %d", command_str, action->labelcolor);
407 gchar * filteringprop_to_string(FilteringProp * prop)
411 gchar *filtering_str;
414 action_str = filteringaction_to_string(buf, sizeof buf, prop->action);
416 if (action_str == NULL)
419 list_str = matcherlist_to_string(prop->matchers);
421 if (list_str == NULL)
424 filtering_str = g_strconcat(list_str, " ", action_str, NULL);
427 return filtering_str;
430 void prefs_filtering_free(GSList * prefs_filtering)
432 while (prefs_filtering != NULL) {
433 FilteringProp * filtering = (FilteringProp *)
434 prefs_filtering->data;
435 filteringprop_free(filtering);
436 prefs_filtering = g_slist_remove(prefs_filtering, filtering);
440 static gboolean prefs_filtering_free_func(GNode *node, gpointer data)
442 FolderItem *item = node->data;
444 g_return_val_if_fail(item, FALSE);
445 g_return_val_if_fail(item->prefs, FALSE);
447 prefs_filtering_free(item->prefs->processing);
448 item->prefs->processing = NULL;
453 void prefs_filtering_clear(void)
457 for (cur = folder_get_list() ; cur != NULL ; cur = g_list_next(cur)) {
460 folder = (Folder *) cur->data;
461 g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
462 prefs_filtering_free_func, NULL);
465 prefs_filtering_free(global_processing);
466 global_processing = NULL;
469 void prefs_filtering_clear_folder(Folder *folder)
471 g_return_if_fail(folder);
472 g_return_if_fail(folder->node);
474 g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
475 prefs_filtering_free_func, NULL);
476 /* FIXME: Note folder settings were changed, where the updates? */