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 #define CHANGE_FLAGS(msginfo) \
110 if (msginfo->folder->folder->change_flags != NULL) \
111 msginfo->folder->folder->change_flags(msginfo->folder->folder, \
116 static gboolean filteringaction_apply(FilteringAction * action, MsgInfo * info,
117 GHashTable *folder_table)
119 FolderItem * dest_folder;
122 PrefsAccount * account;
125 switch(action->type) {
126 case MATCHACTION_MOVE:
128 folder_find_item_from_identifier(action->destination);
132 if (folder_item_move_msg(dest_folder, info) == -1) {
133 debug_print("*** could not move message\n");
138 val = GPOINTER_TO_INT(g_hash_table_lookup
139 (folder_table, dest_folder));
141 folder_item_scan(dest_folder);
142 g_hash_table_insert(folder_table, dest_folder,
148 case MATCHACTION_COPY:
150 folder_find_item_from_identifier(action->destination);
155 if (folder_item_copy_msg(dest_folder, info) == -1)
159 val = GPOINTER_TO_INT(g_hash_table_lookup
160 (folder_table, dest_folder));
162 folder_item_scan(dest_folder);
163 g_hash_table_insert(folder_table, dest_folder,
169 case MATCHACTION_DELETE:
170 if (folder_item_remove_msg(info->folder, info->msgnum) == -1)
174 case MATCHACTION_MARK:
175 MSG_SET_PERM_FLAGS(info->flags, MSG_MARKED);
178 case MATCHACTION_UNMARK:
179 MSG_UNSET_PERM_FLAGS(info->flags, MSG_MARKED);
182 case MATCHACTION_MARK_AS_READ:
183 MSG_UNSET_PERM_FLAGS(info->flags, MSG_UNREAD | MSG_NEW);
186 case MATCHACTION_MARK_AS_UNREAD:
187 debug_print("*** setting unread flags\n");
188 MSG_SET_PERM_FLAGS(info->flags, MSG_UNREAD | MSG_NEW);
191 case MATCHACTION_COLOR:
192 MSG_SET_COLORLABEL_VALUE(info->flags, action->labelcolor);
195 case MATCHACTION_FORWARD:
196 account = account_find_from_id(action->account_id);
197 compose = compose_forward(account, info, FALSE, NULL);
198 if (compose->account->protocol == A_NNTP)
199 compose_entry_append(compose, action->destination,
202 compose_entry_append(compose, action->destination,
205 val = compose_send(compose);
207 gtk_widget_destroy(compose->window);
211 gtk_widget_destroy(compose->window);
214 case MATCHACTION_FORWARD_AS_ATTACHMENT:
216 account = account_find_from_id(action->account_id);
217 compose = compose_forward(account, info, TRUE, NULL);
218 if (compose->account->protocol == A_NNTP)
219 compose_entry_append(compose, action->destination,
222 compose_entry_append(compose, action->destination,
225 val = compose_send(compose);
227 gtk_widget_destroy(compose->window);
230 gtk_widget_destroy(compose->window);
233 case MATCHACTION_BOUNCE:
234 account = account_find_from_id(action->account_id);
235 compose = compose_bounce(account, info);
236 if (compose->account->protocol == A_NNTP)
239 compose_entry_append(compose, action->destination,
242 val = compose_send(compose);
244 gtk_widget_destroy(compose->window);
248 gtk_widget_destroy(compose->window);
251 case MATCHACTION_EXECUTE:
252 cmd = matching_build_command(action->unesc_destination, info);
266 static gboolean filtering_match_condition(FilteringProp *filtering, MsgInfo *info)
268 return matcherlist_match(filtering->matchers, info);
271 static gboolean filtering_apply_rule(FilteringProp *filtering, MsgInfo *info,
272 GHashTable *foldertable)
278 if (FALSE == (result = filteringaction_apply(filtering->action, info, foldertable))) {
279 g_warning(_("action %s could not be applied"),
280 filteringaction_to_string(buf, sizeof buf, filtering->action));
285 static gboolean filtering_is_final_action(FilteringProp *filtering)
287 switch(filtering->action->type) {
288 case MATCHACTION_MOVE:
289 case MATCHACTION_DELETE:
290 return TRUE; /* MsgInfo invalid for message */
291 case MATCHACTION_EXECUTE:
292 case MATCHACTION_COPY:
293 case MATCHACTION_MARK:
294 case MATCHACTION_MARK_AS_READ:
295 case MATCHACTION_UNMARK:
296 case MATCHACTION_MARK_AS_UNREAD:
297 case MATCHACTION_FORWARD:
298 case MATCHACTION_FORWARD_AS_ATTACHMENT:
299 case MATCHACTION_BOUNCE:
300 return FALSE; /* MsgInfo still valid for message */
306 static void filter_msginfo(GSList * filtering_list, FolderItem *inbox,
307 MsgInfo * info, GHashTable *folder_table)
315 g_warning(_("msginfo is not set"));
319 for (l = filtering_list, final = FALSE, applied = FALSE; l != NULL; l = g_slist_next(l)) {
320 FilteringProp * filtering = (FilteringProp *) l->data;
322 if (filtering_match_condition(filtering, info)) {
323 applied = filtering_apply_rule(filtering, info, folder_table);
324 if (TRUE == (final = filtering_is_final_action(filtering)))
329 /* put in inbox if a final rule could not be applied, or
330 * the last rule was not a final one. */
331 if ((final && !applied) || !final) {
332 if (inbox != info->folder) {
333 if (folder_item_move_msg(inbox, info) == -1) {
334 debug_print(_("*** Could not drop message in inbox; check .processing\n"));
338 val = GPOINTER_TO_INT(g_hash_table_lookup
339 (folder_table, inbox));
341 folder_item_scan(inbox);
342 g_hash_table_insert(folder_table, inbox,
351 *\brief filters a message based on its message info data
353 *\param flist filter and actions list
355 *\param ftable table with changed folders after call
357 void filter_message_by_msginfo(GSList *flist, MsgInfo *info, GHashTable *ftable)
361 if (info->folder == NULL) {
362 debug_print("using default inbox as final destination!\n");
363 inbox = folder_get_default_inbox();
365 inbox = info->folder;
368 * message is already in a folder. the filtering code will
369 * handle duplicate moves and copies.
371 filter_msginfo(flist, inbox, info, ftable);
375 *\brief filters a message waiting to be processed in the
376 * .processing folder.
378 *\param filtering_list list of filters and actions
379 *\param inbox default inbox when no filter could be applied
380 *\param msgnum message number in processing folder
381 *\param folder_table table with folders that have been
382 * changed after the call to this function
384 void filter_message(GSList *filtering_list, FolderItem *inbox,
385 gint msgnum, GHashTable *folder_table)
389 MsgFlags msgflags = { 0, 0 };
390 FolderItem *item = folder_get_default_processing();
393 g_warning(_("folderitem not set"));
397 filename = folder_item_fetch_msg(item, msgnum);
399 if (filename == NULL) {
400 g_warning(_("filename is not set"));
404 msginfo = procheader_parse(filename, msgflags, TRUE, FALSE);
408 if (msginfo == NULL) {
409 g_warning(_("could not get info for %s"), filename);
413 msginfo->folder = item;
414 msginfo->msgnum = msgnum;
416 filter_msginfo(filtering_list, inbox, msginfo, folder_table);
418 procmsg_msginfo_free(msginfo);
421 gchar *filteringaction_to_string(gchar *dest, gint destlen, FilteringAction *action)
425 command_str = get_matchparser_tab_str(action->type);
427 if (command_str == NULL)
430 switch(action->type) {
431 case MATCHACTION_MOVE:
432 case MATCHACTION_COPY:
433 case MATCHACTION_EXECUTE:
434 g_snprintf(dest, destlen, "%s \"%s\"", command_str, action->destination);
437 case MATCHACTION_DELETE:
438 case MATCHACTION_MARK:
439 case MATCHACTION_UNMARK:
440 case MATCHACTION_MARK_AS_READ:
441 case MATCHACTION_MARK_AS_UNREAD:
442 g_snprintf(dest, destlen, "%s", command_str);
445 case MATCHACTION_BOUNCE:
446 case MATCHACTION_FORWARD:
447 case MATCHACTION_FORWARD_AS_ATTACHMENT:
448 g_snprintf(dest, destlen, "%s %d \"%s\"", command_str, action->account_id, action->destination);
451 case MATCHACTION_COLOR:
452 g_snprintf(dest, destlen, "%s %d", command_str, action->labelcolor);
454 case MATCHACTION_DELETE_ON_SERVER:
455 g_snprintf(dest, destlen, "%s", command_str);
462 gchar * filteringprop_to_string(FilteringProp * prop)
466 gchar *filtering_str;
469 action_str = filteringaction_to_string(buf, sizeof buf, prop->action);
471 if (action_str == NULL)
474 list_str = matcherlist_to_string(prop->matchers);
476 if (list_str == NULL)
479 filtering_str = g_strconcat(list_str, " ", action_str, NULL);
482 return filtering_str;
485 void prefs_filtering_free(GSList * prefs_filtering)
487 while (prefs_filtering != NULL) {
488 FilteringProp * filtering = (FilteringProp *)
489 prefs_filtering->data;
490 filteringprop_free(filtering);
491 prefs_filtering = g_slist_remove(prefs_filtering, filtering);
495 static gboolean prefs_filtering_free_func(GNode *node, gpointer data)
497 FolderItem *item = node->data;
502 prefs_filtering_free(item->prefs->processing);
503 item->prefs->processing = NULL;
508 void prefs_filtering_clear()
512 for (cur = folder_get_list() ; cur != NULL ; cur = g_list_next(cur)) {
515 folder = (Folder *) cur->data;
516 g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
517 prefs_filtering_free_func, NULL);
520 prefs_filtering_free(global_processing);
521 global_processing = NULL;