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) {
435 /* WRONG: can not update the mark, because the message has
436 * been moved. info pertains to original location.
437 * folder_item_move_msg() already updated the mark for the
438 * destination folder.
440 filteringaction_update_mark(info);
443 val = GPOINTER_TO_INT(g_hash_table_lookup
444 (folder_table, dest_folder));
446 folder_item_scan(dest_folder);
447 g_hash_table_insert(folder_table, dest_folder,
450 val = GPOINTER_TO_INT(g_hash_table_lookup
451 (folder_table, info->folder));
453 folder_item_scan(info->folder);
454 g_hash_table_insert(folder_table, info->folder,
460 case MATCHACTION_COPY:
462 folder_find_item_from_identifier(action->destination);
467 /* NOTE: the following call *will* update the mark file for
468 * the destination folder. but the original message will
469 * still be there in the inbox. */
471 if (folder_item_copy_msg(dest_folder, info) == -1)
475 val = GPOINTER_TO_INT(g_hash_table_lookup
476 (folder_table, dest_folder));
478 folder_item_scan(dest_folder);
479 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)) {
609 if (FALSE == (result = filteringaction_apply(filtering->action, info,
611 action_str = filteringaction_to_string(buf, sizeof buf, filtering->action);
612 g_warning(_("action %s could not be applied"), action_str);
615 switch(filtering->action->type) {
616 case MATCHACTION_MOVE:
617 case MATCHACTION_DELETE:
618 return TRUE; /* MsgInfo invalid for message */
619 case MATCHACTION_EXECUTE:
620 case MATCHACTION_COPY:
621 case MATCHACTION_MARK:
622 case MATCHACTION_MARK_AS_READ:
623 case MATCHACTION_UNMARK:
624 case MATCHACTION_MARK_AS_UNREAD:
625 case MATCHACTION_FORWARD:
626 case MATCHACTION_FORWARD_AS_ATTACHMENT:
627 return FALSE; /* MsgInfo still valid for message */
636 void filter_msginfo(GSList * filtering_list, MsgInfo * info,
637 GHashTable *folder_table)
642 g_warning(_("msginfo is not set"));
646 for(l = filtering_list ; l != NULL ; l = g_slist_next(l)) {
647 FilteringProp * filtering = (FilteringProp *) l->data;
649 if (filteringprop_apply(filtering, info, folder_table))
654 void filter_msginfo_move_or_delete(GSList * filtering_list, MsgInfo * info,
655 GHashTable *folder_table)
660 g_warning(_("msginfo is not set"));
664 for(l = filtering_list ; l != NULL ; l = g_slist_next(l)) {
665 FilteringProp * filtering = (FilteringProp *) l->data;
667 switch (filtering->action->type) {
668 case MATCHACTION_MOVE:
669 case MATCHACTION_DELETE:
670 if (filteringprop_apply(filtering, info, folder_table))
676 void filter_message(GSList * filtering_list, FolderItem * item,
677 gint msgnum, GHashTable *folder_table)
681 MsgFlags msgflags = { 0, 0 };
684 g_warning(_("folderitem not set"));
688 filename = folder_item_fetch_msg(item, msgnum);
690 if (filename == NULL) {
691 g_warning(_("filename is not set"));
695 msginfo = procheader_parse(filename, msgflags, TRUE);
699 if (msginfo == NULL) {
700 g_warning(_("could not get info for %s"), filename);
704 msginfo->folder = item;
705 msginfo->msgnum = msgnum;
707 filter_msginfo(filtering_list, msginfo, folder_table);
711 /******************************************************************************/
713 /* revised filtering system.
715 * 07/18/01 alfons initial revisement
720 * the revised filtering system does not rely on a message being "registered"
721 * in one of sylpheed's folders. it now uses the file sylpheed created on
722 * incorporation of mail.
723 * i do use MsgInfo data; that's needed by the matcher, but for the rest
724 * the MsgInfo is not really used. */
727 /* add_mark() - adds a mark for a file */
728 static void add_mark(FolderItem *folder, gint msgnum, MsgPermFlags flags)
733 if (folder->folder->type == F_MH) {
734 dest_path = folder_item_get_path(folder);
735 if (!is_dir_exist(dest_path))
736 make_dir_hier(dest_path);
738 if (dest_path == NULL) {
739 g_warning(_("Can't open mark file.\n"));
743 if ((fp = procmsg_open_mark_file(dest_path, TRUE))
745 g_warning(_("Can't open mark file.\n"));
749 /* TODO: straight from procmsg.c:procmsg_write_flags()
750 * should update this when mark file version changes */
751 #if MARK_VERSION == 2
752 WRITE_CACHE_DATA_INT(msgnum, fp);
753 WRITE_CACHE_DATA_INT(flags, fp);
755 #error should rewrite the above for new mark version
756 /* paste the source code of procmsg.c:procmsg_write_flags() */
764 /* prepare_destination() - prepares destination folder by registering it
765 * in the global folder table (if not already there). it returns TRUE if
766 * successful. it also returns the FolderItem for the destination */
767 static gboolean prepare_destination(const gchar *destination, FolderItem **item,
768 GHashTable *folder_table)
773 result = folder_find_item_from_identifier(destination);
778 val = GPOINTER_TO_INT(g_hash_table_lookup(folder_table, *item));
780 folder_item_scan(*item);
781 g_hash_table_insert(folder_table, *item, GINT_TO_POINTER(1));
787 /* filter_incoming_perform_actions() - performs actions on incoming
788 * message. this function handles updating marks a little bit smarter;
789 * remember that marks can only be appended. */
790 static gboolean filter_incoming_perform_actions(FolderItem *default_folder,
792 GHashTable *folder_table)
794 /* matching actions for msginfo */
795 struct matching_action {
796 FilteringAction *action;
797 struct matching_action *next;
800 struct matching_action ma_head = { NULL, NULL };
801 struct matching_action *ma_tail = &ma_head;
803 /* need this for the forwarding stuff */
804 PrefsAccount *account;
811 MsgFlags markflags = { 0, 0 };
813 /* use the global prefs_filtering list */
814 GSList * list = global_processing;
816 /* things we get after having added a message to a folder */
817 FolderItem *dest_folder;
820 /* after performing certain action, we may have to copy
821 * the message to inbox too */
822 gboolean copy_to_inbox_too = TRUE;
824 /* filename is only put in msginfo->folder if MSG_FILTERING is set. */
825 const gchar *filename = (gchar *) msginfo->folder;
831 /* build list of matching actions */
832 for (; list != NULL; list = g_slist_next(list)) {
833 FilteringProp *prop = (FilteringProp *) list->data;
835 if (!matcherlist_match(prop->matchers, msginfo))
838 if (NULL == (ma_tail->next = alloca(sizeof(struct matching_action)))) {
839 debug_print(_("action allocation error\n"));
843 debug_print("*** action %d\n", prop->action->type);
845 ma_tail->action = prop->action;
846 ma_tail->next->action = NULL;
847 ma_tail->next->next = NULL;
848 ma_tail = ma_tail->next;
851 debug_print("*** collecting marks\n");
853 /* collect all marks */
854 for (ma_tail = &ma_head, stop = FALSE; ma_tail->action && !stop; ma_tail = ma_tail->next) {
855 switch (ma_tail->action->type) {
857 case MATCHACTION_MARK:
858 MSG_SET_PERM_FLAGS(markflags, MSG_MARKED);
861 case MATCHACTION_UNMARK:
862 MSG_UNSET_PERM_FLAGS(markflags, MSG_MARKED);
865 case MATCHACTION_MARK_AS_READ:
866 MSG_UNSET_PERM_FLAGS(markflags, MSG_UNREAD);
869 case MATCHACTION_MARK_AS_UNREAD:
870 MSG_SET_PERM_FLAGS(markflags, MSG_UNREAD);
873 case MATCHACTION_COLOR:
874 MSG_SET_COLORLABEL_VALUE(markflags, ma_tail->action->labelcolor);
878 case MATCHACTION_FORWARD:
879 case MATCHACTION_FORWARD_AS_ATTACHMENT:
880 case MATCHACTION_MOVE:
881 case MATCHACTION_DELETE:
891 /* msgnum & dest_folder should have valid values after a succesful
892 * drop; in this case there is a MsgInfo available */
896 debug_print("*** performing actions\n");
898 for (ma_tail = &ma_head ; ma_tail->action; ma_tail = ma_tail->next) {
900 /* there are variables you have to use when defining new actions:
902 * copy_to_inbox_too - if the original message should be copied to the inbox
903 * (default_folder) too.
905 * also note that after dropping it to a folder (folder_item_add_msg()) you have
906 * to mark the message, just to make sure any defined mark actions are applied. */
908 #define ACTION (ma_tail->action->type)
910 /* C O N T I N U A B L E */
912 if (MATCHACTION_COPY == ACTION) {
913 debug_print("*** performing copy\n");
914 copy_to_inbox_too = TRUE;
915 if (!prepare_destination(ma_tail->action->destination, &dest_folder, folder_table)) {
916 debug_print("Rule failed: unknown destination %s\n", ma_tail->action->destination);
917 continue; /* try next action */
919 if (0 > (msgnum = folder_item_add_msg(dest_folder, filename, FALSE))) {
920 debug_print(_("Rule failed: could not copy to folder %s\n"),
921 ma_tail->action->destination);
922 continue; /* try next action */
924 flags = msginfo->flags.perm_flags | markflags.perm_flags;
925 add_mark(dest_folder, msgnum, flags);
927 else if (MATCHACTION_EXECUTE == ACTION) {
928 debug_print("*** performing exec\n");
929 copy_to_inbox_too = TRUE;
931 /* matching_build_command() knows about filtering */
932 cmd = matching_build_command(ma_tail->action->destination, msginfo);
938 debug_print(_("Rule failed: no command line\n"));
941 /* U N C O N T I N U A B L E */
943 else if (MATCHACTION_FORWARD == ACTION
944 || MATCHACTION_FORWARD_AS_ATTACHMENT == ACTION) {
945 debug_print("*** performing forward\n");
947 /* forwarding messages is complicated because there's currently no
948 * way to forward a message using "filenames"; you can only forward
949 * a message if you have its MsgInfo. this means we have to drop
950 * the message first */
951 if (0 > (msgnum = folder_item_add_msg(default_folder, filename, FALSE))) {
952 debug_print(_("Rule failed: could not forward\n"));
953 copy_to_inbox_too = TRUE;
957 flags = msginfo->flags.perm_flags | markflags.perm_flags;
958 flags |= MSG_FORWARDED;
959 add_mark(default_folder, msgnum, flags);
961 /* grab the dropped message */
962 fwd_msg_name = folder_item_fetch_msg(default_folder, msgnum);
964 tmp.perm_flags = tmp.tmp_flags = 0;
965 fwd_msg = procheader_parse(fwd_msg_name, tmp, TRUE);
967 fwd_msg->folder = default_folder;
968 fwd_msg->msgnum = msgnum;
970 /* do the compose_XXX stuff */
971 account = account_find_from_id(ma_tail->action->account_id);
972 compose = compose_forward(account, fwd_msg, ACTION == MATCHACTION_FORWARD ? FALSE : TRUE);
973 if (compose->account->protocol == A_NNTP)
974 compose_entry_append(compose, ma_tail->action->destination,
977 compose_entry_append(compose, ma_tail->action->destination,
980 compose_send(compose);
982 procmsg_msginfo_free(fwd_msg);
984 gtk_widget_destroy(compose->window);
987 else if (MATCHACTION_DELETE == ACTION) {
988 debug_print("*** performing delete\n");
989 copy_to_inbox_too = FALSE;
992 dest_folder = folder_get_default_trash();
993 msgnum = folder_item_add_msg(dest_folder, filename, FALSE);
995 debug_print(_("Rule failed: could not move to trash"));
996 copy_to_inbox_too = TRUE;
999 flags = msginfo->flags.perm_flags | markflags.perm_flags;
1000 add_mark(dest_folder, msgnum, flags);
1004 else if (MATCHACTION_MOVE == ACTION) {
1005 debug_print("*** performing move\n");
1006 copy_to_inbox_too = FALSE;
1008 if (!prepare_destination(ma_tail->action->destination, &dest_folder, folder_table)) {
1009 copy_to_inbox_too = TRUE;
1013 msgnum = folder_item_add_msg(dest_folder, filename, FALSE);
1015 debug_print(_("Rule failed: could not move to folder %s\n"),
1016 ma_tail->action->destination);
1017 copy_to_inbox_too = TRUE;
1021 flags = msginfo->flags.perm_flags | markflags.perm_flags;
1022 add_mark(dest_folder, msgnum, flags);
1027 /* may need to copy it to inbox too */
1028 if (copy_to_inbox_too) {
1029 debug_print("*** performing inbox copy\n");
1030 msgnum = folder_item_add_msg(default_folder, filename, TRUE);
1032 debug_print(_("error copying incoming file %s to inbox\n"),
1036 flags = msginfo->flags.perm_flags | markflags.perm_flags;
1037 add_mark(default_folder, msgnum, flags);
1040 debug_print("*** unlinking\n");
1041 if (unlink(filename) < 0)
1042 debug_print(_("error deleting incoming message file\n"));
1050 static void filter_incoming_msginfo(FolderItem *default_folder, MsgInfo *msginfo,
1051 GHashTable *folder_table)
1053 filter_incoming_perform_actions(default_folder, msginfo, folder_table);
1056 /* filter_incoming_message() - tries to apply a filter on one incoming message.
1057 * it also handles the case when there's no filter match */
1058 void filter_incoming_message(FolderItem *default_folder, const gchar *file_name,
1059 GHashTable *folder_table)
1062 MsgFlags msgflags = { 0, 0 };
1064 /* make an "uncomplete" msginfo. it's incomplete because it doesn't
1065 * have a message number / folder yet. */
1066 if (NULL == (msginfo = procheader_parse(file_name, msgflags, TRUE))) {
1067 g_warning(_("error filtering incoming message %s\n"),
1072 /* let matcher know that this is a message that has no
1073 * valid body data yet. */
1074 MSG_SET_TMP_FLAGS(msginfo->flags, MSG_FILTERING);
1075 msginfo->folder = (FolderItem *) g_strdup(file_name); /* actually storing a pointer to a string */
1076 msginfo->msgnum = 0;
1078 filter_incoming_msginfo(default_folder, msginfo, folder_table);
1080 g_free(msginfo->folder);
1081 procmsg_msginfo_free(msginfo);
1084 /****************************************************************************/
1089 void prefs_filtering_read_config(void)
1094 gchar buf[PREFSBUFSIZE];
1096 debug_print(_("Reading filtering configuration...\n"));
1098 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
1099 FILTERING_RC, NULL);
1100 if ((fp = fopen(rcpath, "r")) == NULL) {
1101 if (ENOENT != errno) FILE_OP_ERROR(rcpath, "fopen");
1103 prefs_filtering = NULL;
1108 /* remove all filtering */
1110 while (prefs_filtering != NULL) {
1111 FilteringProp * filtering =
1112 (FilteringProp *) prefs_filtering->data;
1113 filteringprop_free(filtering);
1114 prefs_filtering = g_slist_remove(prefs_filtering, filtering);
1117 while (fgets(buf, sizeof(buf), fp) != NULL) {
1118 FilteringProp * filtering;
1123 if ((*buf != '#') && (*buf != '\0')) {
1125 filtering = filteringprop_parse(&tmp);
1128 g_slist_append(prefs_filtering,
1135 g_warning(_("syntax error : %s\n"), buf);
1144 gchar *filteringaction_to_string(gchar *dest, gint destlen, FilteringAction *action)
1148 command_str = get_matchparser_tab_str(action->type);
1150 if (command_str == NULL)
1153 switch(action->type) {
1154 case MATCHACTION_MOVE:
1155 case MATCHACTION_COPY:
1156 case MATCHACTION_EXECUTE:
1157 g_snprintf(dest, destlen, "%s \"%s\"", command_str, action->destination);
1160 case MATCHACTION_DELETE:
1161 case MATCHACTION_MARK:
1162 case MATCHACTION_UNMARK:
1163 case MATCHACTION_MARK_AS_READ:
1164 case MATCHACTION_MARK_AS_UNREAD:
1165 g_snprintf(dest, destlen, "%s", command_str);
1168 case MATCHACTION_FORWARD:
1169 case MATCHACTION_FORWARD_AS_ATTACHMENT:
1170 g_snprintf(dest, destlen, "%s %d \"%s\"", command_str, action->account_id, action->destination);
1173 case MATCHACTION_COLOR:
1174 g_snprintf(dest, destlen, "%s %d", command_str, action->labelcolor);
1182 gchar * filteringprop_to_string(FilteringProp * prop)
1186 gchar *filtering_str;
1189 action_str = filteringaction_to_string(buf, sizeof buf, prop->action);
1191 if (action_str == NULL)
1194 list_str = matcherlist_to_string(prop->matchers);
1196 if (list_str == NULL)
1199 filtering_str = g_strconcat(list_str, " ", action_str, NULL);
1202 return filtering_str;
1205 void prefs_filtering_write_config(void)
1211 FilteringProp * prop;
1213 debug_print(_("Writing filtering configuration...\n"));
1215 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, FILTERING_RC, NULL);
1217 if ((pfile = prefs_write_open(rcpath)) == NULL) {
1218 g_warning(_("failed to write configuration to file\n"));
1223 for (cur = global_processing; cur != NULL; cur = cur->next) {
1224 gchar *filtering_str;
1226 prop = (FilteringProp *) cur->data;
1227 filtering_str = filteringprop_to_string(prop);
1229 if (fputs(filtering_str, pfile->fp) == EOF ||
1230 fputc('\n', pfile->fp) == EOF) {
1231 FILE_OP_ERROR(rcpath, "fputs || fputc");
1232 prefs_write_close_revert(pfile);
1234 g_free(filtering_str);
1237 g_free(filtering_str);
1242 if (prefs_write_close(pfile) < 0) {
1243 g_warning(_("failed to write configuration to file\n"));
1249 void prefs_filtering_free(GSList * prefs_filtering)
1251 while (prefs_filtering != NULL) {
1252 FilteringProp * filtering = (FilteringProp *)
1253 prefs_filtering->data;
1254 filteringprop_free(filtering);
1255 prefs_filtering = g_slist_remove(prefs_filtering, filtering);
1259 static gboolean prefs_filtering_free_func(GNode *node, gpointer data)
1261 FolderItem *item = node->data;
1266 prefs_filtering_free(item->prefs->processing);
1267 item->prefs->processing = NULL;
1272 void prefs_filtering_clear()
1276 for (cur = folder_get_list() ; cur != NULL ; cur = g_list_next(cur)) {
1279 folder = (Folder *) cur->data;
1280 g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
1281 prefs_filtering_free_func, NULL);
1284 prefs_filtering_free(global_processing);
1285 global_processing = NULL;