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 static gboolean filtering_match_condition(FilteringProp *filtering, MsgInfo *info)
344 return matcherlist_match(filtering->matchers, info);
347 static gboolean filtering_apply_rule(FilteringProp *filtering, MsgInfo *info,
355 for (tmp = filtering->action_list ; tmp != NULL ; tmp = tmp->next) {
356 FilteringAction * action;
360 if (FALSE == (result = filteringaction_apply(action, info))) {
361 g_warning("action %s could not be applied",
362 filteringaction_to_string(buf, sizeof buf, action));
365 if (filtering_is_final_action(action)) {
373 static gboolean filtering_is_final_action(FilteringAction *filtering_action)
375 switch(filtering_action->type) {
376 case MATCHACTION_MOVE:
377 case MATCHACTION_DELETE:
378 return TRUE; /* MsgInfo invalid for message */
379 case MATCHACTION_EXECUTE:
380 case MATCHACTION_COPY:
381 case MATCHACTION_MARK:
382 case MATCHACTION_UNMARK:
383 case MATCHACTION_LOCK:
384 case MATCHACTION_UNLOCK:
385 case MATCHACTION_MARK_AS_READ:
386 case MATCHACTION_MARK_AS_UNREAD:
387 case MATCHACTION_FORWARD:
388 case MATCHACTION_FORWARD_AS_ATTACHMENT:
389 case MATCHACTION_REDIRECT:
390 return FALSE; /* MsgInfo still valid for message */
396 static gboolean filter_msginfo(GSList * filtering_list, MsgInfo * info)
402 g_return_val_if_fail(info != NULL, TRUE);
404 for (l = filtering_list, final = FALSE, applied = FALSE; l != NULL; l = g_slist_next(l)) {
405 FilteringProp * filtering = (FilteringProp *) l->data;
407 if (filtering_match_condition(filtering, info)) {
408 applied = filtering_apply_rule(filtering, info, &final);
410 if (TRUE == (final = filtering_is_final_action(filtering)))
418 /* put in inbox if a final rule could not be applied, or
419 * the last rule was not a final one. */
420 if ((final && !applied) || !final) {
427 gboolean filter_message_by_msginfo(GSList *flist, MsgInfo *info)
429 return filter_msginfo(flist, info);
432 gchar *filteringaction_to_string(gchar *dest, gint destlen, FilteringAction *action)
434 const gchar *command_str;
436 command_str = get_matchparser_tab_str(action->type);
438 if (command_str == NULL)
441 switch(action->type) {
442 case MATCHACTION_MOVE:
443 case MATCHACTION_COPY:
444 case MATCHACTION_EXECUTE:
445 g_snprintf(dest, destlen, "%s \"%s\"", command_str, action->destination);
448 case MATCHACTION_DELETE:
449 case MATCHACTION_MARK:
450 case MATCHACTION_UNMARK:
451 case MATCHACTION_LOCK:
452 case MATCHACTION_UNLOCK:
453 case MATCHACTION_MARK_AS_READ:
454 case MATCHACTION_MARK_AS_UNREAD:
455 g_snprintf(dest, destlen, "%s", command_str);
458 case MATCHACTION_REDIRECT:
459 case MATCHACTION_FORWARD:
460 case MATCHACTION_FORWARD_AS_ATTACHMENT:
461 g_snprintf(dest, destlen, "%s %d \"%s\"", command_str, action->account_id, action->destination);
464 case MATCHACTION_COLOR:
465 g_snprintf(dest, destlen, "%s %d", command_str, action->labelcolor);
472 gchar * filteringaction_list_to_string(GSList * action_list)
474 gchar *action_list_str;
479 action_list_str = NULL;
480 for (tmp = action_list ; tmp != NULL ; tmp = tmp->next) {
482 FilteringAction * action;
486 action_str = filteringaction_to_string(buf,
489 if (action_list_str != NULL) {
490 list_str = g_strconcat(action_list_str, " ", action_str, NULL);
491 g_free(action_list_str);
494 list_str = g_strdup(action_str);
496 action_list_str = list_str;
499 return action_list_str;
502 gchar * filteringprop_to_string(FilteringProp * prop)
505 gchar *action_list_str;
506 gchar *filtering_str;
509 action_list_str = filteringaction_list_to_string(prop->action_list);
511 if (action_list_str == NULL)
514 list_str = matcherlist_to_string(prop->matchers);
516 if (list_str == NULL) {
517 g_free(action_list_str);
521 filtering_str = g_strconcat(list_str, " ", action_list_str, NULL);
522 g_free(action_list_str);
525 return filtering_str;
528 void prefs_filtering_free(GSList * prefs_filtering)
530 while (prefs_filtering != NULL) {
531 FilteringProp * filtering = (FilteringProp *)
532 prefs_filtering->data;
533 filteringprop_free(filtering);
534 prefs_filtering = g_slist_remove(prefs_filtering, filtering);
538 static gboolean prefs_filtering_free_func(GNode *node, gpointer data)
540 FolderItem *item = node->data;
542 g_return_val_if_fail(item, FALSE);
543 g_return_val_if_fail(item->prefs, FALSE);
545 prefs_filtering_free(item->prefs->processing);
546 item->prefs->processing = NULL;
551 void prefs_filtering_clear(void)
555 for (cur = folder_get_list() ; cur != NULL ; cur = g_list_next(cur)) {
558 folder = (Folder *) cur->data;
559 g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
560 prefs_filtering_free_func, NULL);
563 prefs_filtering_free(global_processing);
564 global_processing = NULL;
567 void prefs_filtering_clear_folder(Folder *folder)
569 g_return_if_fail(folder);
570 g_return_if_fail(folder->node);
572 g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
573 prefs_filtering_free_func, NULL);
574 /* FIXME: Note folder settings were changed, where the updates? */