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"
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 action->account_id = account_id;
63 action->destination = g_strdup(destination);
64 action->unesc_destination = matcher_unescape_str(g_strdup(destination));
66 action->destination = NULL;
67 action->unesc_destination = NULL;
69 action->labelcolor = labelcolor;
73 void filteringaction_free(FilteringAction * action)
75 g_return_if_fail(action);
76 if (action->destination)
77 g_free(action->destination);
78 if (action->unesc_destination)
79 g_free(action->unesc_destination);
83 FilteringProp * filteringprop_new(MatcherList * matchers,
84 FilteringAction * action)
86 FilteringProp * filtering;
88 filtering = g_new0(FilteringProp, 1);
89 filtering->matchers = matchers;
90 filtering->action = action;
95 FilteringProp * filteringprop_copy(FilteringProp *src)
100 new = g_new0(FilteringProp, 1);
101 new->matchers = g_new0(MatcherList, 1);
102 new->action = g_new0(FilteringAction, 1);
103 for (tmp = src->matchers->matchers; tmp != NULL && tmp->data != NULL;) {
104 MatcherProp *matcher = (MatcherProp *)tmp->data;
106 new->matchers->matchers = g_slist_append(new->matchers->matchers,
107 matcherprop_copy(matcher));
110 new->matchers->bool_and = src->matchers->bool_and;
111 new->action->type = src->action->type;
112 new->action->account_id = src->action->account_id;
113 if (src->action->destination)
114 new->action->destination = g_strdup(src->action->destination);
116 new->action->destination = NULL;
117 if (src->action->unesc_destination)
118 new->action->unesc_destination = g_strdup(src->action->unesc_destination);
120 new->action->unesc_destination = NULL;
121 new->action->labelcolor = src->action->labelcolor;
125 void filteringprop_free(FilteringProp * prop)
127 matcherlist_free(prop->matchers);
128 filteringaction_free(prop->action);
133 fitleringaction_apply
134 runs the action on one MsgInfo
135 return value : return TRUE if the action could be applied
138 static gboolean filteringaction_apply(FilteringAction * action, MsgInfo * info)
140 FolderItem * dest_folder;
143 PrefsAccount * account;
146 switch(action->type) {
147 case MATCHACTION_MOVE:
149 folder_find_item_from_identifier(action->destination);
153 if (folder_item_move_msg(dest_folder, info) == -1) {
154 debug_print("*** could not move message\n");
160 case MATCHACTION_COPY:
162 folder_find_item_from_identifier(action->destination);
167 if (folder_item_copy_msg(dest_folder, info) == -1)
172 case MATCHACTION_DELETE:
173 if (folder_item_remove_msg(info->folder, info->msgnum) == -1)
177 case MATCHACTION_MARK:
178 procmsg_msginfo_set_flags(info, MSG_MARKED, 0);
181 case MATCHACTION_UNMARK:
182 procmsg_msginfo_unset_flags(info, MSG_MARKED, 0);
185 case MATCHACTION_MARK_AS_READ:
186 procmsg_msginfo_unset_flags(info, MSG_UNREAD | MSG_NEW, 0);
189 case MATCHACTION_MARK_AS_UNREAD:
190 debug_print("*** setting unread flags\n");
191 procmsg_msginfo_set_flags(info, MSG_UNREAD | MSG_NEW, 0);
194 case MATCHACTION_COLOR:
195 procmsg_msginfo_unset_flags(info, MSG_CLABEL_FLAG_MASK, 0);
196 procmsg_msginfo_set_flags(info, MSG_COLORLABEL_TO_FLAGS(action->labelcolor), 0);
199 case MATCHACTION_FORWARD:
200 account = account_find_from_id(action->account_id);
201 compose = compose_forward(account, info, FALSE, NULL);
202 if (compose->account->protocol == A_NNTP)
203 compose_entry_append(compose, action->destination,
206 compose_entry_append(compose, action->destination,
209 val = compose_send(compose);
211 gtk_widget_destroy(compose->window);
215 gtk_widget_destroy(compose->window);
218 case MATCHACTION_FORWARD_AS_ATTACHMENT:
220 account = account_find_from_id(action->account_id);
221 compose = compose_forward(account, info, TRUE, NULL);
222 if (compose->account->protocol == A_NNTP)
223 compose_entry_append(compose, action->destination,
226 compose_entry_append(compose, action->destination,
229 val = compose_send(compose);
231 gtk_widget_destroy(compose->window);
234 gtk_widget_destroy(compose->window);
237 case MATCHACTION_REDIRECT:
238 account = account_find_from_id(action->account_id);
239 compose = compose_redirect(account, info);
240 if (compose->account->protocol == A_NNTP)
243 compose_entry_append(compose, action->destination,
246 val = compose_send(compose);
248 gtk_widget_destroy(compose->window);
252 gtk_widget_destroy(compose->window);
255 case MATCHACTION_EXECUTE:
256 cmd = matching_build_command(action->unesc_destination, info);
271 static gboolean filtering_match_condition(FilteringProp *filtering, MsgInfo *info)
273 return matcherlist_match(filtering->matchers, info);
276 static gboolean filtering_apply_rule(FilteringProp *filtering, MsgInfo *info)
281 if (FALSE == (result = filteringaction_apply(filtering->action, info))) {
282 g_warning("action %s could not be applied",
283 filteringaction_to_string(buf, sizeof buf, filtering->action));
288 static gboolean filtering_is_final_action(FilteringProp *filtering)
290 switch(filtering->action->type) {
291 case MATCHACTION_MOVE:
292 case MATCHACTION_DELETE:
293 return TRUE; /* MsgInfo invalid for message */
294 case MATCHACTION_EXECUTE:
295 case MATCHACTION_COPY:
296 case MATCHACTION_MARK:
297 case MATCHACTION_MARK_AS_READ:
298 case MATCHACTION_UNMARK:
299 case MATCHACTION_MARK_AS_UNREAD:
300 case MATCHACTION_FORWARD:
301 case MATCHACTION_FORWARD_AS_ATTACHMENT:
302 case MATCHACTION_REDIRECT:
303 return FALSE; /* MsgInfo still valid for message */
309 static void filter_msginfo(GSList * filtering_list, FolderItem *inbox,
317 g_warning("msginfo is not set");
321 for (l = filtering_list, final = FALSE, applied = FALSE; l != NULL; l = g_slist_next(l)) {
322 FilteringProp * filtering = (FilteringProp *) l->data;
324 if (filtering_match_condition(filtering, info)) {
325 applied = filtering_apply_rule(filtering, info);
326 if (TRUE == (final = filtering_is_final_action(filtering)))
331 /* put in inbox if a final rule could not be applied, or
332 * the last rule was not a final one. */
333 if ((final && !applied) || !final) {
334 if (inbox != info->folder) {
335 if (folder_item_move_msg(inbox, info) == -1) {
336 debug_print("*** Could not drop message in inbox; check .processing\n");
344 *\brief filters a message based on its message info data
346 *\param flist filter and actions list
349 void filter_message_by_msginfo_with_inbox(GSList *flist, MsgInfo *info, FolderItem *def_inbox)
353 if ((def_inbox == NULL)) {
354 debug_print("using default inbox as final destination!\n");
355 inbox = folder_get_default_inbox();
360 * message is already in a folder. the filtering code will
361 * handle duplicate moves and copies.
363 filter_msginfo(flist, inbox, info);
366 void filter_message_by_msginfo(GSList *flist, MsgInfo *info)
368 filter_message_by_msginfo_with_inbox(flist, info, info->folder);
372 *\brief filters a message waiting to be processed in the
373 * .processing folder.
375 *\param filtering_list list of filters and actions
376 *\param inbox default inbox when no filter could be applied
377 *\param msgnum message number in processing folder
378 * changed after the call to this function
380 void filter_message(GSList *filtering_list, FolderItem *inbox,
385 MsgFlags msgflags = { 0, 0 };
386 FolderItem *item = folder_get_default_processing();
389 g_warning("folderitem not set");
393 filename = folder_item_fetch_msg(item, msgnum);
395 if (filename == NULL) {
396 g_warning("filename is not set");
400 msginfo = procheader_parse_file(filename, msgflags, TRUE, FALSE);
404 if (msginfo == NULL) {
405 g_warning("could not get info for %s", filename);
409 msginfo->folder = item;
410 msginfo->msgnum = msgnum;
412 filter_msginfo(filtering_list, inbox, msginfo);
414 procmsg_msginfo_free(msginfo);
417 gchar *filteringaction_to_string(gchar *dest, gint destlen, FilteringAction *action)
421 command_str = get_matchparser_tab_str(action->type);
423 if (command_str == NULL)
426 switch(action->type) {
427 case MATCHACTION_MOVE:
428 case MATCHACTION_COPY:
429 case MATCHACTION_EXECUTE:
430 g_snprintf(dest, destlen, "%s \"%s\"", command_str, action->destination);
433 case MATCHACTION_DELETE:
434 case MATCHACTION_MARK:
435 case MATCHACTION_UNMARK:
436 case MATCHACTION_MARK_AS_READ:
437 case MATCHACTION_MARK_AS_UNREAD:
438 g_snprintf(dest, destlen, "%s", command_str);
441 case MATCHACTION_REDIRECT:
442 case MATCHACTION_FORWARD:
443 case MATCHACTION_FORWARD_AS_ATTACHMENT:
444 g_snprintf(dest, destlen, "%s %d \"%s\"", command_str, action->account_id, action->destination);
447 case MATCHACTION_COLOR:
448 g_snprintf(dest, destlen, "%s %d", command_str, action->labelcolor);
450 case MATCHACTION_DELETE_ON_SERVER:
451 g_snprintf(dest, destlen, "%s", command_str);
458 gchar * filteringprop_to_string(FilteringProp * prop)
462 gchar *filtering_str;
465 action_str = filteringaction_to_string(buf, sizeof buf, prop->action);
467 if (action_str == NULL)
470 list_str = matcherlist_to_string(prop->matchers);
472 if (list_str == NULL)
475 filtering_str = g_strconcat(list_str, " ", action_str, NULL);
478 return filtering_str;
481 void prefs_filtering_free(GSList * prefs_filtering)
483 while (prefs_filtering != NULL) {
484 FilteringProp * filtering = (FilteringProp *)
485 prefs_filtering->data;
486 filteringprop_free(filtering);
487 prefs_filtering = g_slist_remove(prefs_filtering, filtering);
491 static gboolean prefs_filtering_free_func(GNode *node, gpointer data)
493 FolderItem *item = node->data;
498 prefs_filtering_free(item->prefs->processing);
499 item->prefs->processing = NULL;
504 void prefs_filtering_clear()
508 for (cur = folder_get_list() ; cur != NULL ; cur = g_list_next(cur)) {
511 folder = (Folder *) cur->data;
512 g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
513 prefs_filtering_free_func, NULL);
516 prefs_filtering_free(global_processing);
517 global_processing = NULL;