2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2001 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.
21 * initital Hoa initial
22 * 07/16/01 Alfons fix bugs
23 * 07/18/01 Alfons rewrite things
28 * filter_message() is no longer the entry point for filtering. it's
29 * replaced with filter_incoming_message() which does not trust
30 * on MsgInfo's from a folder. instead it directly works with
32 * updating the mark file is a lot easier now, because we can do that
33 * right after a call to folder_item_add_msg().
45 #include "procheader.h"
47 #include "filtering.h"
51 #define PREFSBUFSIZE 1024
53 GSList * global_processing = NULL;
55 FilteringAction * filteringaction_new(int type, int account_id,
59 FilteringAction * action;
61 action = g_new0(FilteringAction, 1);
64 action->account_id = account_id;
66 action->destination = g_strdup(destination);
67 action->labelcolor = labelcolor;
71 void filteringaction_free(FilteringAction * action)
73 if (action->destination)
74 g_free(action->destination);
79 FilteringAction * filteringaction_parse(gchar ** str)
81 FilteringAction * action;
83 gchar * destination = NULL;
90 key = matcher_parse_keyword(&tmp);
93 case MATCHACTION_MOVE:
94 case MATCHACTION_COPY:
95 case MATCHACTION_EXECUTE:
96 destination = matcher_parse_str(&tmp);
102 case MATCHACTION_DELETE:
104 case MATCHACTION_MARK:
106 case MATCHACTION_MARK_AS_READ:
108 case MATCHACTION_UNMARK:
110 case MATCHACTION_MARK_AS_UNREAD:
112 case MATCHACTION_FORWARD:
113 case MATCHACTION_FORWARD_AS_ATTACHMENT:
114 account_id = matcher_parse_number(&tmp);
120 destination = matcher_parse_str(&tmp);
127 case MATCHACTION_COLOR:
128 labelcolor = matcher_parse_number(&tmp);
142 action = filteringaction_new(key, account_id, destination, labelcolor);
147 FilteringProp * filteringprop_parse(gchar ** str)
151 FilteringProp * filtering;
152 MatcherList * matchers;
153 FilteringAction * action;
157 matchers = matcherlist_parse(&tmp);
164 matcherlist_free(matchers);
169 action = filteringaction_parse(&tmp);
171 matcherlist_free(matchers);
176 filtering = filteringprop_new(matchers, action);
183 FilteringProp * filteringprop_new(MatcherList * matchers,
184 FilteringAction * action)
186 FilteringProp * filtering;
188 filtering = g_new0(FilteringProp, 1);
189 filtering->matchers = matchers;
190 filtering->action = action;
195 void filteringprop_free(FilteringProp * prop)
197 matcherlist_free(prop->matchers);
198 filteringaction_free(prop->action);
202 /* filteringaction_update_mark() - updates a mark for a message. note that
203 * the message should not have been moved or copied. remember that the
204 * procmsg_open_mark_file(PATH, TRUE) actually _appends_ a new record.
206 static gboolean filteringaction_update_mark(MsgInfo * info)
211 if (info->folder->folder->type == F_MH) {
212 dest_path = folder_item_get_path(info->folder);
213 if (!is_dir_exist(dest_path))
214 make_dir_hier(dest_path);
216 if (dest_path == NULL) {
217 g_warning(_("Can't open mark file.\n"));
221 if ((fp = procmsg_open_mark_file(dest_path, TRUE))
223 g_warning(_("Can't open mark file.\n"));
227 procmsg_write_flags(info, fp);
234 static inline gint strlen_with_check(const gchar *expr, gint fline, const gchar *str)
239 debug_print("%s(%d) - invalid string %s\n", __FILE__, fline, expr);
244 #define STRLEN_WITH_CHECK(expr) \
245 strlen_with_check(#expr, __LINE__, expr)
247 static gchar * filteringaction_execute_command(gchar * cmd, MsgInfo * info)
250 gchar * filename = NULL;
251 gchar * processed_cmd;
255 matcher_unescape_str(cmd);
257 size = strlen(cmd) + 1;
265 case 's': /* subject */
266 size += STRLEN_WITH_CHECK(info->subject) - 2;
269 size += STRLEN_WITH_CHECK(info->from) - 2;
272 size += STRLEN_WITH_CHECK(info->to) - 2;
275 size += STRLEN_WITH_CHECK(info->cc) - 2;
278 size += STRLEN_WITH_CHECK(info->date) - 2;
280 case 'i': /* message-id */
281 size += STRLEN_WITH_CHECK(info->msgid) - 2;
283 case 'n': /* newsgroups */
284 size += STRLEN_WITH_CHECK(info->newsgroups) - 2;
286 case 'r': /* references */
287 size += STRLEN_WITH_CHECK(info->references) - 2;
290 if (MSG_IS_FILTERING(info->flags))
291 filename = g_strdup((gchar *)info->folder);
293 filename = folder_item_fetch_msg(info->folder, info->msgnum);
295 if (filename == NULL) {
296 g_warning(_("filename is not set"));
300 size += strlen(filename) - 2;
309 processed_cmd = g_new0(gchar, size);
321 case 's': /* subject */
322 if (info->subject != NULL)
323 strcpy(p, info->subject);
329 if (info->from != NULL)
330 strcpy(p, info->from);
336 if (info->to != NULL)
343 if (info->cc != NULL)
350 if (info->date != NULL)
351 strcpy(p, info->date);
356 case 'i': /* message-id */
357 if (info->msgid != NULL)
358 strcpy(p, info->msgid);
363 case 'n': /* newsgroups */
364 if (info->newsgroups != NULL)
365 strcpy(p, info->newsgroups);
370 case 'r': /* references */
371 if (info->references != NULL)
372 strcpy(p, info->references);
398 return processed_cmd;
402 fitleringaction_apply
403 runs the action on one MsgInfo
404 return value : return TRUE if the action could be applied
407 #define CHANGE_FLAGS(msginfo) \
409 if (msginfo->folder->folder->change_flags != NULL) \
410 msginfo->folder->folder->change_flags(msginfo->folder->folder, \
415 static gboolean filteringaction_apply(FilteringAction * action, MsgInfo * info,
416 GHashTable *folder_table)
418 FolderItem * dest_folder;
421 PrefsAccount * account;
424 switch(action->type) {
425 case MATCHACTION_MOVE:
427 folder_find_item_from_identifier(action->destination);
431 if (folder_item_move_msg(dest_folder, info) == -1)
434 /* WRONG: can not update the mark, because the message has
435 * been moved. info pertains to original location.
436 * folder_item_move_msg() already updated the mark for the
437 * destination folder.
439 filteringaction_update_mark(info);
442 val = GPOINTER_TO_INT(g_hash_table_lookup
443 (folder_table, dest_folder));
445 folder_item_scan(dest_folder);
446 g_hash_table_insert(folder_table, dest_folder,
449 val = GPOINTER_TO_INT(g_hash_table_lookup
450 (folder_table, info->folder));
452 folder_item_scan(info->folder);
453 g_hash_table_insert(folder_table, info->folder,
459 case MATCHACTION_COPY:
461 folder_find_item_from_identifier(action->destination);
466 /* NOTE: the following call *will* update the mark file for
467 * the destination folder. but the original message will
468 * still be there in the inbox. */
470 if (folder_item_copy_msg(dest_folder, info) == -1)
474 val = GPOINTER_TO_INT(g_hash_table_lookup
475 (folder_table, dest_folder));
477 folder_item_scan(dest_folder);
478 g_hash_table_insert(folder_table, dest_folder,
485 case MATCHACTION_DELETE:
486 if (folder_item_remove_msg(info->folder, info->msgnum) == -1)
489 /* WRONG: can not update the mark. this would actually add
490 * a bogus record to the mark file for the message's original
493 filteringaction_update_mark(info);
498 case MATCHACTION_MARK:
499 MSG_SET_PERM_FLAGS(info->flags, MSG_MARKED);
500 debug_print("*** MARKING message %d, in folder %s, \"%s\"\n",
501 info->msgnum, info->folder->name,
502 info->subject ? info->subject : "<none>");
503 filteringaction_update_mark(info);
509 case MATCHACTION_UNMARK:
510 MSG_UNSET_PERM_FLAGS(info->flags, MSG_MARKED);
511 filteringaction_update_mark(info);
517 case MATCHACTION_MARK_AS_READ:
518 MSG_UNSET_PERM_FLAGS(info->flags, MSG_UNREAD | MSG_NEW);
519 filteringaction_update_mark(info);
525 case MATCHACTION_MARK_AS_UNREAD:
526 MSG_SET_PERM_FLAGS(info->flags, MSG_UNREAD | MSG_NEW);
527 filteringaction_update_mark(info);
533 case MATCHACTION_FORWARD:
535 account = account_find_from_id(action->account_id);
536 compose = compose_forward(account, info, FALSE);
537 if (compose->account->protocol == A_NNTP)
538 compose_entry_append(compose, action->destination,
541 compose_entry_append(compose, action->destination,
544 val = compose_send(compose);
546 gtk_widget_destroy(compose->window);
550 gtk_widget_destroy(compose->window);
553 case MATCHACTION_FORWARD_AS_ATTACHMENT:
555 account = account_find_from_id(action->account_id);
556 compose = compose_forward(account, info, TRUE);
557 if (compose->account->protocol == A_NNTP)
558 compose_entry_append(compose, action->destination,
561 compose_entry_append(compose, action->destination,
564 val = compose_send(compose);
566 gtk_widget_destroy(compose->window);
570 gtk_widget_destroy(compose->window);
573 case MATCHACTION_EXECUTE:
575 cmd = matching_build_command(action->destination, info);
590 /* filteringprop_apply() - runs the action on one MsgInfo if it matches the
591 * criterium. certain actions can be followed by other actions. in this
592 * case the function returns FALSE. if an action can not be followed
593 * by others, the function returns TRUE.
595 * remember that this is because of the fact that msg flags are always
596 * _appended_ to mark files. currently sylpheed does not insert messages
597 * at a certain index.
598 * now, after having performed a certain action, the MsgInfo is still
599 * valid for the message. in *this* case the function returns FALSE.
601 static gboolean filteringprop_apply(FilteringProp * filtering, MsgInfo * info,
602 GHashTable *folder_table)
604 if (matcherlist_match(filtering->matchers, info)) {
610 result = filteringaction_apply(filtering->action, info,
613 filteringaction_to_string(filtering->action);
615 g_warning(_("action %s could not be applied"),
619 debug_print(_("message %i %s..."),
620 info->msgnum, action_str);
625 switch(filtering->action->type) {
626 case MATCHACTION_MOVE:
627 case MATCHACTION_DELETE:
628 return TRUE; /* MsgInfo invalid for message */
629 case MATCHACTION_EXECUTE:
630 case MATCHACTION_COPY:
631 case MATCHACTION_MARK:
632 case MATCHACTION_MARK_AS_READ:
633 case MATCHACTION_UNMARK:
634 case MATCHACTION_MARK_AS_UNREAD:
635 case MATCHACTION_FORWARD:
636 case MATCHACTION_FORWARD_AS_ATTACHMENT:
637 return FALSE; /* MsgInfo still valid for message */
646 void filter_msginfo(GSList * filtering_list, MsgInfo * info,
647 GHashTable *folder_table)
652 g_warning(_("msginfo is not set"));
656 for(l = filtering_list ; l != NULL ; l = g_slist_next(l)) {
657 FilteringProp * filtering = (FilteringProp *) l->data;
659 if (filteringprop_apply(filtering, info, folder_table))
664 void filter_msginfo_move_or_delete(GSList * filtering_list, MsgInfo * info,
665 GHashTable *folder_table)
670 g_warning(_("msginfo is not set"));
674 for(l = filtering_list ; l != NULL ; l = g_slist_next(l)) {
675 FilteringProp * filtering = (FilteringProp *) l->data;
677 switch (filtering->action->type) {
678 case MATCHACTION_MOVE:
679 case MATCHACTION_DELETE:
680 if (filteringprop_apply(filtering, info, folder_table))
686 void filter_message(GSList * filtering_list, FolderItem * item,
687 gint msgnum, GHashTable *folder_table)
691 MsgFlags msgflags = { 0, 0 };
694 g_warning(_("folderitem not set"));
698 filename = folder_item_fetch_msg(item, msgnum);
700 if (filename == NULL) {
701 g_warning(_("filename is not set"));
705 msginfo = procheader_parse(filename, msgflags, TRUE);
709 if (msginfo == NULL) {
710 g_warning(_("could not get info for %s"), filename);
714 msginfo->folder = item;
715 msginfo->msgnum = msgnum;
717 filter_msginfo(filtering_list, msginfo, folder_table);
721 /******************************************************************************/
723 /* revised filtering system.
725 * 07/18/01 alfons initial revisement
730 * the revised filtering system does not rely on a message being "registered"
731 * in one of sylpheed's folders. it now uses the file sylpheed created on
732 * incorporation of mail.
733 * i do use MsgInfo data; that's needed by the matcher, but for the rest
734 * the MsgInfo is not really used. */
737 /* add_mark() - adds a mark for a file */
738 static void add_mark(FolderItem *folder, gint msgnum, MsgPermFlags flags)
743 if (folder->folder->type == F_MH) {
744 dest_path = folder_item_get_path(folder);
745 if (!is_dir_exist(dest_path))
746 make_dir_hier(dest_path);
748 if (dest_path == NULL) {
749 g_warning(_("Can't open mark file.\n"));
753 if ((fp = procmsg_open_mark_file(dest_path, TRUE))
755 g_warning(_("Can't open mark file.\n"));
759 /* TODO: straight from procmsg.c:procmsg_write_flags()
760 * should update this when mark file version changes */
761 #if MARK_VERSION == 2
762 WRITE_CACHE_DATA_INT(msgnum, fp);
763 WRITE_CACHE_DATA_INT(flags, fp);
765 #error should rewrite the above for new mark version
766 /* paste the source code of procmsg.c:procmsg_write_flags() */
774 /* prepare_destination() - prepares destination folder by registering it
775 * in the global folder table (if not already there). it returns TRUE if
776 * successful. it also returns the FolderItem for the destination */
777 static gboolean prepare_destination(const gchar *destination, FolderItem **item,
778 GHashTable *folder_table)
783 result = folder_find_item_from_identifier(destination);
788 val = GPOINTER_TO_INT(g_hash_table_lookup(folder_table, *item));
790 folder_item_scan(*item);
791 g_hash_table_insert(folder_table, *item, GINT_TO_POINTER(1));
797 /* filter_incoming_perform_actions() - performs actions on incoming
798 * message. this function handles updating marks a little bit smarter;
799 * remember that marks can only be appended. */
800 static gboolean filter_incoming_perform_actions(FolderItem *default_folder,
802 GHashTable *folder_table)
804 /* matching actions for msginfo */
805 struct matching_action {
806 FilteringAction *action;
807 struct matching_action *next;
810 struct matching_action ma_head = { NULL, NULL };
811 struct matching_action *ma_tail = &ma_head;
813 /* need this for the forwarding stuff */
814 PrefsAccount *account;
821 MsgFlags markflags = { 0, 0 };
823 /* use the global prefs_filtering list */
824 GSList * list = global_processing;
826 /* things we get after having added a message to a folder */
827 FolderItem *dest_folder;
830 /* after performing certain action, we may have to copy
831 * the message to inbox too */
832 gboolean copy_to_inbox_too = TRUE;
834 /* filename is only put in msginfo->folder if MSG_FILTERING is set. */
835 const gchar *filename = (gchar *) msginfo->folder;
841 /* build list of matching actions */
842 for (; list != NULL; list = g_slist_next(list)) {
843 FilteringProp *prop = (FilteringProp *) list->data;
845 if (!matcherlist_match(prop->matchers, msginfo))
848 if (NULL == (ma_tail->next = alloca(sizeof(struct matching_action)))) {
849 debug_print(_("action allocation error\n"));
853 debug_print("*** action %d\n", prop->action->type);
855 ma_tail->action = prop->action;
856 ma_tail->next->action = NULL;
857 ma_tail->next->next = NULL;
858 ma_tail = ma_tail->next;
861 debug_print("*** collecting marks\n");
863 /* collect all marks */
864 for (ma_tail = &ma_head, stop = FALSE; ma_tail->action && !stop; ma_tail = ma_tail->next) {
865 switch (ma_tail->action->type) {
867 case MATCHACTION_MARK:
868 MSG_SET_PERM_FLAGS(markflags, MSG_MARKED);
871 case MATCHACTION_UNMARK:
872 MSG_UNSET_PERM_FLAGS(markflags, MSG_MARKED);
875 case MATCHACTION_MARK_AS_READ:
876 MSG_UNSET_PERM_FLAGS(markflags, MSG_UNREAD);
879 case MATCHACTION_MARK_AS_UNREAD:
880 MSG_SET_PERM_FLAGS(markflags, MSG_UNREAD);
883 case MATCHACTION_COLOR:
884 MSG_SET_COLORLABEL_VALUE(markflags, ma_tail->action->labelcolor);
888 case MATCHACTION_FORWARD:
889 case MATCHACTION_FORWARD_AS_ATTACHMENT:
890 case MATCHACTION_MOVE:
891 case MATCHACTION_DELETE:
901 /* msgnum & dest_folder should have valid values after a succesful
902 * drop; in this case there is a MsgInfo available */
906 debug_print("*** performing actions\n");
908 for (ma_tail = &ma_head ; ma_tail->action; ma_tail = ma_tail->next) {
910 /* there are variables you have to use when defining new actions:
912 * copy_to_inbox_too - if the original message should be copied to the inbox
913 * (default_folder) too.
915 * also note that after dropping it to a folder (folder_item_add_msg()) you have
916 * to mark the message, just to make sure any defined mark actions are applied. */
918 #define ACTION (ma_tail->action->type)
920 /* C O N T I N U A B L E */
922 if (MATCHACTION_COPY == ACTION) {
923 debug_print("*** performing copy\n");
924 copy_to_inbox_too = TRUE;
925 if (!prepare_destination(ma_tail->action->destination, &dest_folder, folder_table)) {
926 debug_print("Rule failed: unknown destination %s\n", ma_tail->action->destination);
927 continue; /* try next action */
929 if (0 > (msgnum = folder_item_add_msg(dest_folder, filename, FALSE))) {
930 debug_print(_("Rule failed: could not copy to folder %s\n"),
931 ma_tail->action->destination);
932 continue; /* try next action */
934 flags = msginfo->flags.perm_flags | markflags.perm_flags;
935 add_mark(dest_folder, msgnum, flags);
937 else if (MATCHACTION_EXECUTE == ACTION) {
938 debug_print("*** performing exec\n");
939 copy_to_inbox_too = TRUE;
941 /* matching_build_command() knows about filtering */
942 cmd = matching_build_command(ma_tail->action->destination, msginfo);
948 debug_print(_("Rule failed: no command line\n"));
951 /* U N C O N T I N U A B L E */
953 else if (MATCHACTION_FORWARD == ACTION
954 || MATCHACTION_FORWARD_AS_ATTACHMENT == ACTION) {
955 debug_print("*** performing forward\n");
957 /* forwarding messages is complicated because there's currently no
958 * way to forward a message using "filenames"; you can only forward
959 * a message if you have its MsgInfo. this means we have to drop
960 * the message first */
961 if (0 > (msgnum = folder_item_add_msg(default_folder, filename, FALSE))) {
962 debug_print(_("Rule failed: could not forward\n"));
963 copy_to_inbox_too = TRUE;
967 flags = msginfo->flags.perm_flags | markflags.perm_flags;
968 flags |= MSG_FORWARDED;
969 add_mark(default_folder, msgnum, flags);
971 /* grab the dropped message */
972 fwd_msg_name = folder_item_fetch_msg(default_folder, msgnum);
974 tmp.perm_flags = tmp.tmp_flags = 0;
975 fwd_msg = procheader_parse(fwd_msg_name, tmp, TRUE);
977 fwd_msg->folder = default_folder;
978 fwd_msg->msgnum = msgnum;
980 /* do the compose_XXX stuff */
981 account = account_find_from_id(ma_tail->action->account_id);
982 compose = compose_forward(account, fwd_msg, ACTION == MATCHACTION_FORWARD ? FALSE : TRUE);
983 if (compose->account->protocol == A_NNTP)
984 compose_entry_append(compose, ma_tail->action->destination,
987 compose_entry_append(compose, ma_tail->action->destination,
990 compose_send(compose);
992 procmsg_msginfo_free(fwd_msg);
994 gtk_widget_destroy(compose->window);
997 else if (MATCHACTION_DELETE == ACTION) {
998 debug_print("*** performing delete\n");
999 copy_to_inbox_too = FALSE;
1002 dest_folder = folder_get_default_trash();
1003 msgnum = folder_item_add_msg(dest_folder, filename, FALSE);
1005 debug_print(_("Rule failed: could not move to trash"));
1006 copy_to_inbox_too = TRUE;
1009 flags = msginfo->flags.perm_flags | markflags.perm_flags;
1010 add_mark(dest_folder, msgnum, flags);
1014 else if (MATCHACTION_MOVE == ACTION) {
1015 debug_print("*** performing move\n");
1016 copy_to_inbox_too = FALSE;
1018 if (!prepare_destination(ma_tail->action->destination, &dest_folder, folder_table)) {
1019 copy_to_inbox_too = TRUE;
1023 msgnum = folder_item_add_msg(dest_folder, filename, FALSE);
1025 debug_print(_("Rule failed: could not move to folder %s\n"),
1026 ma_tail->action->destination);
1027 copy_to_inbox_too = TRUE;
1031 flags = msginfo->flags.perm_flags | markflags.perm_flags;
1032 add_mark(dest_folder, msgnum, flags);
1037 /* may need to copy it to inbox too */
1038 if (copy_to_inbox_too) {
1039 debug_print("*** performing inbox copy\n");
1040 msgnum = folder_item_add_msg(default_folder, filename, TRUE);
1042 debug_print(_("error copying incoming file %s to inbox\n"),
1046 flags = msginfo->flags.perm_flags | markflags.perm_flags;
1047 add_mark(default_folder, msgnum, flags);
1050 debug_print("*** unlinking\n");
1051 if (unlink(filename) < 0)
1052 debug_print(_("error deleting incoming message file\n"));
1060 static void filter_incoming_msginfo(FolderItem *default_folder, MsgInfo *msginfo,
1061 GHashTable *folder_table)
1063 filter_incoming_perform_actions(default_folder, msginfo, folder_table);
1066 /* filter_incoming_message() - tries to apply a filter on one incoming message.
1067 * it also handles the case when there's no filter match */
1068 void filter_incoming_message(FolderItem *default_folder, const gchar *file_name,
1069 GHashTable *folder_table)
1072 MsgFlags msgflags = { 0, 0 };
1074 /* make an "uncomplete" msginfo. it's incomplete because it doesn't
1075 * have a message number / folder yet. */
1076 if (NULL == (msginfo = procheader_parse(file_name, msgflags, TRUE))) {
1077 g_warning(_("error filtering incoming message %s\n"),
1082 /* let matcher know that this is a message that has no
1083 * valid body data yet. */
1084 MSG_SET_TMP_FLAGS(msginfo->flags, MSG_FILTERING);
1085 msginfo->folder = (FolderItem *) g_strdup(file_name); /* actually storing a pointer to a string */
1086 msginfo->msgnum = 0;
1088 filter_incoming_msginfo(default_folder, msginfo, folder_table);
1090 g_free(msginfo->folder);
1091 procmsg_msginfo_free(msginfo);
1094 /****************************************************************************/
1099 void prefs_filtering_read_config(void)
1104 gchar buf[PREFSBUFSIZE];
1106 debug_print(_("Reading filtering configuration...\n"));
1108 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
1109 FILTERING_RC, NULL);
1110 if ((fp = fopen(rcpath, "r")) == NULL) {
1111 if (ENOENT != errno) FILE_OP_ERROR(rcpath, "fopen");
1113 prefs_filtering = NULL;
1118 /* remove all filtering */
1120 while (prefs_filtering != NULL) {
1121 FilteringProp * filtering =
1122 (FilteringProp *) prefs_filtering->data;
1123 filteringprop_free(filtering);
1124 prefs_filtering = g_slist_remove(prefs_filtering, filtering);
1127 while (fgets(buf, sizeof(buf), fp) != NULL) {
1128 FilteringProp * filtering;
1133 if ((*buf != '#') && (*buf != '\0')) {
1135 filtering = filteringprop_parse(&tmp);
1138 g_slist_append(prefs_filtering,
1145 g_warning(_("syntax error : %s\n"), buf);
1154 gchar * filteringaction_to_string(FilteringAction * action)
1156 gchar * command_str;
1158 gchar * account_id_str;
1159 gchar * labelcolor_str;
1161 /* FIXME: use g_sprintf() throughout */
1164 command_str = get_matchparser_tab_str(action->type);
1166 if (command_str == NULL)
1169 switch(action->type) {
1170 case MATCHACTION_MOVE:
1171 case MATCHACTION_COPY:
1172 case MATCHACTION_EXECUTE:
1173 return g_strconcat(command_str, " \"", action->destination,
1176 case MATCHACTION_DELETE:
1177 case MATCHACTION_MARK:
1178 case MATCHACTION_UNMARK:
1179 case MATCHACTION_MARK_AS_READ:
1180 case MATCHACTION_MARK_AS_UNREAD:
1181 return g_strdup(command_str);
1184 case MATCHACTION_FORWARD:
1185 case MATCHACTION_FORWARD_AS_ATTACHMENT:
1186 account_id_str = itos(action->account_id);
1187 return g_strconcat(command_str, " ", account_id_str,
1188 " \"", action->destination, "\"", NULL);
1190 case MATCHACTION_COLOR:
1191 labelcolor_str = itos(action->labelcolor);
1192 return g_strconcat(command_str, " ", labelcolor_str, NULL);
1199 gchar * filteringprop_to_string(FilteringProp * prop)
1203 gchar * filtering_str;
1205 action_str = filteringaction_to_string(prop->action);
1207 if (action_str == NULL)
1210 list_str = matcherlist_to_string(prop->matchers);
1212 if (list_str == NULL) {
1217 filtering_str = g_strconcat(list_str, " ", action_str, NULL);
1221 return filtering_str;
1224 void prefs_filtering_write_config(void)
1230 FilteringProp * prop;
1232 debug_print(_("Writing filtering configuration...\n"));
1234 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, FILTERING_RC, NULL);
1236 if ((pfile = prefs_write_open(rcpath)) == NULL) {
1237 g_warning(_("failed to write configuration to file\n"));
1242 for (cur = global_processing; cur != NULL; cur = cur->next) {
1243 gchar *filtering_str;
1245 prop = (FilteringProp *) cur->data;
1246 filtering_str = filteringprop_to_string(prop);
1248 if (fputs(filtering_str, pfile->fp) == EOF ||
1249 fputc('\n', pfile->fp) == EOF) {
1250 FILE_OP_ERROR(rcpath, "fputs || fputc");
1251 prefs_write_close_revert(pfile);
1253 g_free(filtering_str);
1256 g_free(filtering_str);
1261 if (prefs_write_close(pfile) < 0) {
1262 g_warning(_("failed to write configuration to file\n"));
1268 void prefs_filtering_free(GSList * prefs_filtering)
1270 while (prefs_filtering != NULL) {
1271 FilteringProp * filtering = (FilteringProp *)
1272 prefs_filtering->data;
1273 filteringprop_free(filtering);
1274 prefs_filtering = g_slist_remove(prefs_filtering, filtering);
1278 static gboolean prefs_filtering_free_func(GNode *node, gpointer data)
1280 FolderItem *item = node->data;
1285 prefs_filtering_free(item->prefs->processing);
1286 item->prefs->processing = NULL;
1291 void prefs_filtering_clear()
1295 for (cur = folder_get_list() ; cur != NULL ; cur = g_list_next(cur)) {
1298 folder = (Folder *) cur->data;
1299 g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
1300 prefs_filtering_free_func, NULL);
1303 prefs_filtering_free(global_processing);
1304 global_processing = NULL;