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 void filteringprop_free(FilteringProp * prop)
97 matcherlist_free(prop->matchers);
98 filteringaction_free(prop->action);
103 fitleringaction_apply
104 runs the action on one MsgInfo
105 return value : return TRUE if the action could be applied
108 static gboolean filteringaction_apply(FilteringAction * action, MsgInfo * info)
110 FolderItem * dest_folder;
113 PrefsAccount * account;
116 switch(action->type) {
117 case MATCHACTION_MOVE:
119 folder_find_item_from_identifier(action->destination);
123 if (folder_item_move_msg(dest_folder, info) == -1) {
124 debug_print("*** could not move message\n");
130 case MATCHACTION_COPY:
132 folder_find_item_from_identifier(action->destination);
137 if (folder_item_copy_msg(dest_folder, info) == -1)
142 case MATCHACTION_DELETE:
143 if (folder_item_remove_msg(info->folder, info->msgnum) == -1)
147 case MATCHACTION_MARK:
148 procmsg_msginfo_set_flags(info, MSG_MARKED, 0);
151 case MATCHACTION_UNMARK:
152 procmsg_msginfo_unset_flags(info, MSG_MARKED, 0);
155 case MATCHACTION_MARK_AS_READ:
156 procmsg_msginfo_unset_flags(info, MSG_UNREAD | MSG_NEW, 0);
159 case MATCHACTION_MARK_AS_UNREAD:
160 debug_print("*** setting unread flags\n");
161 procmsg_msginfo_set_flags(info, MSG_UNREAD | MSG_NEW, 0);
164 case MATCHACTION_COLOR:
165 procmsg_msginfo_unset_flags(info, MSG_CLABEL_FLAG_MASK, 0);
166 procmsg_msginfo_set_flags(info, MSG_COLORLABEL_TO_FLAGS(action->labelcolor), 0);
169 case MATCHACTION_FORWARD:
170 account = account_find_from_id(action->account_id);
171 compose = compose_forward(account, info, FALSE, NULL);
172 if (compose->account->protocol == A_NNTP)
173 compose_entry_append(compose, action->destination,
176 compose_entry_append(compose, action->destination,
179 val = compose_send(compose);
181 gtk_widget_destroy(compose->window);
185 gtk_widget_destroy(compose->window);
188 case MATCHACTION_FORWARD_AS_ATTACHMENT:
190 account = account_find_from_id(action->account_id);
191 compose = compose_forward(account, info, TRUE, NULL);
192 if (compose->account->protocol == A_NNTP)
193 compose_entry_append(compose, action->destination,
196 compose_entry_append(compose, action->destination,
199 val = compose_send(compose);
201 gtk_widget_destroy(compose->window);
204 gtk_widget_destroy(compose->window);
207 case MATCHACTION_REDIRECT:
208 account = account_find_from_id(action->account_id);
209 compose = compose_redirect(account, info);
210 if (compose->account->protocol == A_NNTP)
213 compose_entry_append(compose, action->destination,
216 val = compose_send(compose);
218 gtk_widget_destroy(compose->window);
222 gtk_widget_destroy(compose->window);
225 case MATCHACTION_EXECUTE:
226 cmd = matching_build_command(action->unesc_destination, info);
241 static gboolean filtering_match_condition(FilteringProp *filtering, MsgInfo *info)
243 return matcherlist_match(filtering->matchers, info);
246 static gboolean filtering_apply_rule(FilteringProp *filtering, MsgInfo *info)
251 if (FALSE == (result = filteringaction_apply(filtering->action, info))) {
252 g_warning(_("action %s could not be applied"),
253 filteringaction_to_string(buf, sizeof buf, filtering->action));
258 static gboolean filtering_is_final_action(FilteringProp *filtering)
260 switch(filtering->action->type) {
261 case MATCHACTION_MOVE:
262 case MATCHACTION_DELETE:
263 return TRUE; /* MsgInfo invalid for message */
264 case MATCHACTION_EXECUTE:
265 case MATCHACTION_COPY:
266 case MATCHACTION_MARK:
267 case MATCHACTION_MARK_AS_READ:
268 case MATCHACTION_UNMARK:
269 case MATCHACTION_MARK_AS_UNREAD:
270 case MATCHACTION_FORWARD:
271 case MATCHACTION_FORWARD_AS_ATTACHMENT:
272 case MATCHACTION_REDIRECT:
273 return FALSE; /* MsgInfo still valid for message */
279 static void filter_msginfo(GSList * filtering_list, FolderItem *inbox,
288 g_warning(_("msginfo is not set"));
292 for (l = filtering_list, final = FALSE, applied = FALSE; l != NULL; l = g_slist_next(l)) {
293 FilteringProp * filtering = (FilteringProp *) l->data;
295 if (filtering_match_condition(filtering, info)) {
296 applied = filtering_apply_rule(filtering, info);
297 if (TRUE == (final = filtering_is_final_action(filtering)))
302 /* put in inbox if a final rule could not be applied, or
303 * the last rule was not a final one. */
304 if ((final && !applied) || !final) {
305 if (inbox != info->folder) {
306 if (folder_item_move_msg(inbox, info) == -1) {
307 debug_print("*** Could not drop message in inbox; check .processing\n");
315 *\brief filters a message based on its message info data
317 *\param flist filter and actions list
320 void filter_message_by_msginfo_with_inbox(GSList *flist, MsgInfo *info, FolderItem *def_inbox)
324 if ((def_inbox == NULL)) {
325 debug_print("using default inbox as final destination!\n");
326 inbox = folder_get_default_inbox();
331 * message is already in a folder. the filtering code will
332 * handle duplicate moves and copies.
334 filter_msginfo(flist, inbox, info);
337 void filter_message_by_msginfo(GSList *flist, MsgInfo *info)
339 filter_message_by_msginfo_with_inbox(flist, info, info->folder);
343 *\brief filters a message waiting to be processed in the
344 * .processing folder.
346 *\param filtering_list list of filters and actions
347 *\param inbox default inbox when no filter could be applied
348 *\param msgnum message number in processing folder
349 * changed after the call to this function
351 void filter_message(GSList *filtering_list, FolderItem *inbox,
356 MsgFlags msgflags = { 0, 0 };
357 FolderItem *item = folder_get_default_processing();
360 g_warning(_("folderitem not set"));
364 filename = folder_item_fetch_msg(item, msgnum);
366 if (filename == NULL) {
367 g_warning(_("filename is not set"));
371 msginfo = procheader_parse_file(filename, msgflags, TRUE, FALSE);
375 if (msginfo == NULL) {
376 g_warning(_("could not get info for %s"), filename);
380 msginfo->folder = item;
381 msginfo->msgnum = msgnum;
383 filter_msginfo(filtering_list, inbox, msginfo);
385 procmsg_msginfo_free(msginfo);
388 gchar *filteringaction_to_string(gchar *dest, gint destlen, FilteringAction *action)
392 command_str = get_matchparser_tab_str(action->type);
394 if (command_str == NULL)
397 switch(action->type) {
398 case MATCHACTION_MOVE:
399 case MATCHACTION_COPY:
400 case MATCHACTION_EXECUTE:
401 g_snprintf(dest, destlen, "%s \"%s\"", command_str, action->destination);
404 case MATCHACTION_DELETE:
405 case MATCHACTION_MARK:
406 case MATCHACTION_UNMARK:
407 case MATCHACTION_MARK_AS_READ:
408 case MATCHACTION_MARK_AS_UNREAD:
409 g_snprintf(dest, destlen, "%s", command_str);
412 case MATCHACTION_REDIRECT:
413 case MATCHACTION_FORWARD:
414 case MATCHACTION_FORWARD_AS_ATTACHMENT:
415 g_snprintf(dest, destlen, "%s %d \"%s\"", command_str, action->account_id, action->destination);
418 case MATCHACTION_COLOR:
419 g_snprintf(dest, destlen, "%s %d", command_str, action->labelcolor);
421 case MATCHACTION_DELETE_ON_SERVER:
422 g_snprintf(dest, destlen, "%s", command_str);
429 gchar * filteringprop_to_string(FilteringProp * prop)
433 gchar *filtering_str;
436 action_str = filteringaction_to_string(buf, sizeof buf, prop->action);
438 if (action_str == NULL)
441 list_str = matcherlist_to_string(prop->matchers);
443 if (list_str == NULL)
446 filtering_str = g_strconcat(list_str, " ", action_str, NULL);
449 return filtering_str;
452 void prefs_filtering_free(GSList * prefs_filtering)
454 while (prefs_filtering != NULL) {
455 FilteringProp * filtering = (FilteringProp *)
456 prefs_filtering->data;
457 filteringprop_free(filtering);
458 prefs_filtering = g_slist_remove(prefs_filtering, filtering);
462 static gboolean prefs_filtering_free_func(GNode *node, gpointer data)
464 FolderItem *item = node->data;
469 prefs_filtering_free(item->prefs->processing);
470 item->prefs->processing = NULL;
475 void prefs_filtering_clear()
479 for (cur = folder_get_list() ; cur != NULL ; cur = g_list_next(cur)) {
482 folder = (Folder *) cur->data;
483 g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
484 prefs_filtering_free_func, NULL);
487 prefs_filtering_free(global_processing);
488 global_processing = NULL;