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 * prefs_filtering = NULL;
55 FilteringAction * filteringaction_new(int type, int account_id,
58 FilteringAction * action;
60 action = g_new0(FilteringAction, 1);
63 action->account_id = account_id;
65 action->destination = g_strdup(destination);
70 void filteringaction_free(FilteringAction * action)
72 if (action->destination)
73 g_free(action->destination);
77 FilteringAction * filteringaction_parse(gchar ** str)
79 FilteringAction * action;
81 gchar * destination = NULL;
87 key = matcher_parse_keyword(&tmp);
90 case MATCHING_ACTION_MOVE:
91 case MATCHING_ACTION_COPY:
92 case MATCHING_EXECUTE:
93 destination = matcher_parse_str(&tmp);
99 case MATCHING_ACTION_DELETE:
101 case MATCHING_ACTION_MARK:
103 case MATCHING_ACTION_MARK_AS_READ:
105 case MATCHING_ACTION_UNMARK:
107 case MATCHING_ACTION_MARK_AS_UNREAD:
109 case MATCHING_ACTION_FORWARD:
110 case MATCHING_ACTION_FORWARD_AS_ATTACHMENT:
111 account_id = matcher_parse_number(&tmp);
117 destination = matcher_parse_str(&tmp);
130 action = filteringaction_new(key, account_id, destination);
135 FilteringProp * filteringprop_parse(gchar ** str)
139 FilteringProp * filtering;
140 MatcherList * matchers;
141 FilteringAction * action;
145 matchers = matcherlist_parse(&tmp);
152 matcherlist_free(matchers);
157 action = filteringaction_parse(&tmp);
159 matcherlist_free(matchers);
164 filtering = filteringprop_new(matchers, action);
171 FilteringProp * filteringprop_new(MatcherList * matchers,
172 FilteringAction * action)
174 FilteringProp * filtering;
176 filtering = g_new0(FilteringProp, 1);
177 filtering->matchers = matchers;
178 filtering->action = action;
183 void filteringprop_free(FilteringProp * prop)
185 matcherlist_free(prop->matchers);
186 filteringaction_free(prop->action);
190 /* filteringaction_update_mark() - updates a mark for a message. note that
191 * the message should not have been moved or copied. remember that the
192 * procmsg_open_mark_file(PATH, TRUE) actually _appends_ a new record.
194 static gboolean filteringaction_update_mark(MsgInfo * info)
199 if (info->folder->folder->type == F_MH) {
200 dest_path = folder_item_get_path(info->folder);
201 if (!is_dir_exist(dest_path))
202 make_dir_hier(dest_path);
204 if (dest_path == NULL) {
205 g_warning(_("Can't open mark file.\n"));
209 if ((fp = procmsg_open_mark_file(dest_path, TRUE))
211 g_warning(_("Can't open mark file.\n"));
215 procmsg_write_flags(info, fp);
222 static inline gint strlen_with_check(const gchar *expr, gint fline, const gchar *str)
227 debug_print("%s(%d) - invalid string %s\n", __FILE__, fline, expr);
232 #define STRLEN_WITH_CHECK(expr) \
233 strlen_with_check(#expr, __LINE__, expr)
235 static gchar * filteringaction_execute_command(gchar * cmd, MsgInfo * info)
238 gchar * filename = NULL;
239 gchar * processed_cmd;
243 size = strlen(cmd) + 1;
251 case 's': /* subject */
252 size += STRLEN_WITH_CHECK(info->subject) - 2;
255 size += STRLEN_WITH_CHECK(info->from) - 2;
258 size += STRLEN_WITH_CHECK(info->to) - 2;
261 size += STRLEN_WITH_CHECK(info->cc) - 2;
264 size += STRLEN_WITH_CHECK(info->date) - 2;
266 case 'i': /* message-id */
267 size += STRLEN_WITH_CHECK(info->msgid) - 2;
269 case 'n': /* newsgroups */
270 size += STRLEN_WITH_CHECK(info->newsgroups) - 2;
272 case 'r': /* references */
273 size += STRLEN_WITH_CHECK(info->references) - 2;
276 if (MSG_IS_FILTERING(info->flags))
277 filename = g_strdup(info->folder);
279 filename = folder_item_fetch_msg(info->folder, info->msgnum);
281 if (filename == NULL) {
282 g_warning(_("filename is not set"));
286 size += strlen(filename) - 2;
295 processed_cmd = g_new0(gchar, size);
307 case 's': /* subject */
308 if (info->subject != NULL)
309 strcpy(p, info->subject);
315 if (info->from != NULL)
316 strcpy(p, info->from);
322 if (info->to != NULL)
329 if (info->cc != NULL)
336 if (info->date != NULL)
337 strcpy(p, info->date);
342 case 'i': /* message-id */
343 if (info->msgid != NULL)
344 strcpy(p, info->msgid);
349 case 'n': /* newsgroups */
350 if (info->newsgroups != NULL)
351 strcpy(p, info->newsgroups);
356 case 'r': /* references */
357 if (info->references != NULL)
358 strcpy(p, info->references);
384 return processed_cmd;
388 fitleringaction_apply
389 runs the action on one MsgInfo
390 return value : return TRUE if the action could be applied
393 #define CHANGE_FLAGS(msginfo) \
395 if (msginfo->folder->folder->change_flags != NULL) \
396 msginfo->folder->folder->change_flags(msginfo->folder->folder, \
401 static gboolean filteringaction_apply(FilteringAction * action, MsgInfo * info,
402 GHashTable *folder_table)
404 FolderItem * dest_folder;
407 PrefsAccount * account;
410 switch(action->type) {
411 case MATCHING_ACTION_MOVE:
413 folder_find_item_from_identifier(action->destination);
417 if (folder_item_move_msg(dest_folder, info) == -1)
420 /* WRONG: can not update the mark, because the message has
421 * been moved. info pertains to original location.
422 * folder_item_move_msg() already updated the mark for the
423 * destination folder.
425 filteringaction_update_mark(info);
428 val = GPOINTER_TO_INT(g_hash_table_lookup
429 (folder_table, dest_folder));
431 folder_item_scan(dest_folder);
432 g_hash_table_insert(folder_table, dest_folder,
435 val = GPOINTER_TO_INT(g_hash_table_lookup
436 (folder_table, info->folder));
438 folder_item_scan(info->folder);
439 g_hash_table_insert(folder_table, info->folder,
445 case MATCHING_ACTION_COPY:
447 folder_find_item_from_identifier(action->destination);
452 /* NOTE: the following call *will* update the mark file for
453 * the destination folder. but the original message will
454 * still be there in the inbox. */
456 if (folder_item_copy_msg(dest_folder, info) == -1)
460 val = GPOINTER_TO_INT(g_hash_table_lookup
461 (folder_table, dest_folder));
463 folder_item_scan(dest_folder);
464 g_hash_table_insert(folder_table, dest_folder,
471 case MATCHING_ACTION_DELETE:
472 if (folder_item_remove_msg(info->folder, info->msgnum) == -1)
475 /* WRONG: can not update the mark. this would actually add
476 * a bogus record to the mark file for the message's original
479 filteringaction_update_mark(info);
484 case MATCHING_ACTION_MARK:
485 MSG_SET_PERM_FLAGS(info->flags, MSG_MARKED);
486 debug_print("*** MARKING message %d, in folder %s, \"%s\"\n",
487 info->msgnum, info->folder->name,
488 info->subject ? info->subject : "<none>");
489 filteringaction_update_mark(info);
495 case MATCHING_ACTION_UNMARK:
496 MSG_UNSET_PERM_FLAGS(info->flags, MSG_MARKED);
497 filteringaction_update_mark(info);
503 case MATCHING_ACTION_MARK_AS_READ:
504 MSG_UNSET_PERM_FLAGS(info->flags, MSG_UNREAD | MSG_NEW);
505 filteringaction_update_mark(info);
511 case MATCHING_ACTION_MARK_AS_UNREAD:
512 MSG_SET_PERM_FLAGS(info->flags, MSG_UNREAD | MSG_NEW);
513 filteringaction_update_mark(info);
519 case MATCHING_ACTION_FORWARD:
521 account = account_find_from_id(action->account_id);
522 compose = compose_forward(account, info, FALSE);
523 if (compose->account->protocol == A_NNTP)
524 compose_entry_append(compose, action->destination,
527 compose_entry_append(compose, action->destination,
530 val = compose_send(compose);
532 gtk_widget_destroy(compose->window);
536 gtk_widget_destroy(compose->window);
539 case MATCHING_ACTION_FORWARD_AS_ATTACHMENT:
541 account = account_find_from_id(action->account_id);
542 compose = compose_forward(account, info, TRUE);
543 if (compose->account->protocol == A_NNTP)
544 compose_entry_append(compose, action->destination,
547 compose_entry_append(compose, action->destination,
550 val = compose_send(compose);
552 gtk_widget_destroy(compose->window);
556 gtk_widget_destroy(compose->window);
559 case MATCHING_EXECUTE:
561 cmd = matching_build_command(action->destination, info);
576 /* filteringprop_apply() - runs the action on one MsgInfo if it matches the
577 * criterium. certain actions can be followed by other actions. in this
578 * case the function returns FALSE. if an action can not be followed
579 * by others, the function returns TRUE.
581 * remember that this is because of the fact that msg flags are always
582 * _appended_ to mark files. currently sylpheed does not insert messages
583 * at a certain index.
584 * now, after having performed a certain action, the MsgInfo is still
585 * valid for the message. in *this* case the function returns FALSE.
587 static gboolean filteringprop_apply(FilteringProp * filtering, MsgInfo * info,
588 GHashTable *folder_table)
590 if (matcherlist_match(filtering->matchers, info)) {
596 result = filteringaction_apply(filtering->action, info,
599 filteringaction_to_string(filtering->action);
601 g_warning(_("action %s could not be applied"),
605 debug_print(_("message %i %s..."),
606 info->msgnum, action_str);
611 switch(filtering->action->type) {
612 case MATCHING_ACTION_MOVE:
613 case MATCHING_ACTION_DELETE:
614 return TRUE; /* MsgInfo invalid for message */
615 case MATCHING_EXECUTE:
616 case MATCHING_ACTION_COPY:
617 case MATCHING_ACTION_MARK:
618 case MATCHING_ACTION_MARK_AS_READ:
619 case MATCHING_ACTION_UNMARK:
620 case MATCHING_ACTION_MARK_AS_UNREAD:
621 case MATCHING_ACTION_FORWARD:
622 case MATCHING_ACTION_FORWARD_AS_ATTACHMENT:
623 return FALSE; /* MsgInfo still valid for message */
632 void filter_msginfo(GSList * filtering_list, MsgInfo * info,
633 GHashTable *folder_table)
638 g_warning(_("msginfo is not set"));
642 for(l = filtering_list ; l != NULL ; l = g_slist_next(l)) {
643 FilteringProp * filtering = (FilteringProp *) l->data;
645 if (filteringprop_apply(filtering, info, folder_table))
650 void filter_msginfo_move_or_delete(GSList * filtering_list, MsgInfo * info,
651 GHashTable *folder_table)
656 g_warning(_("msginfo is not set"));
660 for(l = filtering_list ; l != NULL ; l = g_slist_next(l)) {
661 FilteringProp * filtering = (FilteringProp *) l->data;
663 switch (filtering->action->type) {
664 case MATCHING_ACTION_MOVE:
665 case MATCHING_ACTION_DELETE:
666 if (filteringprop_apply(filtering, info, folder_table))
672 void filter_message(GSList * filtering_list, FolderItem * item,
673 gint msgnum, GHashTable *folder_table)
677 MsgFlags msgflags = { 0, 0 };
680 g_warning(_("folderitem not set"));
684 filename = folder_item_fetch_msg(item, msgnum);
686 if (filename == NULL) {
687 g_warning(_("filename is not set"));
691 msginfo = procheader_parse(filename, msgflags, TRUE);
695 if (msginfo == NULL) {
696 g_warning(_("could not get info for %s"), filename);
700 msginfo->folder = item;
701 msginfo->msgnum = msgnum;
703 filter_msginfo(filtering_list, msginfo, folder_table);
707 /******************************************************************************/
709 /* revised filtering system.
711 * 07/18/01 alfons initial revisement
716 * the revised filtering system does not rely on a message being "registered"
717 * in one of sylpheed's folders. it now uses the file sylpheed created on
718 * incorporation of mail.
719 * i do use MsgInfo data; that's needed by the matcher, but for the rest
720 * the MsgInfo is not really used. */
723 /* add_mark() - adds a mark for a file */
724 static void add_mark(FolderItem *folder, gint msgnum, MsgPermFlags flags)
729 if (folder->folder->type == F_MH) {
730 dest_path = folder_item_get_path(folder);
731 if (!is_dir_exist(dest_path))
732 make_dir_hier(dest_path);
734 if (dest_path == NULL) {
735 g_warning(_("Can't open mark file.\n"));
739 if ((fp = procmsg_open_mark_file(dest_path, TRUE))
741 g_warning(_("Can't open mark file.\n"));
745 /* TODO: straight from procmsg.c:procmsg_write_flags()
746 * should update this when mark file version changes */
747 #if MARK_VERSION == 2
748 WRITE_CACHE_DATA_INT(msgnum, fp);
749 WRITE_CACHE_DATA_INT(flags, fp);
751 #error should rewrite the above for new mark version
752 /* paste the source code of procmsg.c:procmsg_write_flags() */
760 /* prepare_destination() - prepares destination folder by registering it
761 * in the global folder table (if not already there). it returns TRUE if
762 * successful. it also returns the FolderItem for the destination */
763 static gboolean prepare_destination(const gchar *destination, FolderItem **item,
764 GHashTable *folder_table)
769 result = folder_find_item_from_identifier(destination);
774 val = GPOINTER_TO_INT(g_hash_table_lookup(folder_table, *item));
776 folder_item_scan(*item);
777 g_hash_table_insert(folder_table, *item, GINT_TO_POINTER(1));
783 /* filter_incoming_perform_actions() - performs actions on incoming
784 * message. this function handles updating marks a little bit smarter;
785 * remember that marks can only be appended. */
786 static gboolean filter_incoming_perform_actions(FolderItem *default_folder,
788 GHashTable *folder_table)
790 /* matching actions for msginfo */
791 struct matching_action {
792 FilteringAction *action;
793 struct matching_action *next;
796 struct matching_action ma_head = { NULL, NULL };
797 struct matching_action *ma_tail = &ma_head;
799 /* need this for the forwarding stuff */
800 PrefsAccount *account;
807 MsgFlags markflags = { 0, 0 };
809 /* use the global prefs_filtering list */
810 GSList * list = prefs_filtering;
812 /* things we get after having added a message to a folder */
813 FolderItem *dest_folder;
816 /* after performing certain action, we may have to copy
817 * the message to inbox too */
818 gboolean copy_to_inbox_too = TRUE;
820 /* filename is only put in msginfo->folder if MSG_FILTERING is set. */
821 const gchar *filename = (gchar *) msginfo->folder;
827 /* build list of matching actions */
828 for (; list != NULL; list = g_slist_next(list)) {
829 FilteringProp *prop = (FilteringProp *) list->data;
831 if (!matcherlist_match(prop->matchers, msginfo))
834 if (NULL == (ma_tail->next = alloca(sizeof(struct matching_action)))) {
835 debug_print(_("action allocation error\n"));
839 debug_print("*** action %d\n", prop->action->type);
841 ma_tail->action = prop->action;
842 ma_tail->next->action = NULL;
843 ma_tail->next->next = NULL;
844 ma_tail = ma_tail->next;
847 debug_print("*** collecting marks\n");
849 /* collect all marks */
850 for (ma_tail = &ma_head, stop = FALSE; ma_tail->action && !stop; ma_tail = ma_tail->next) {
851 switch (ma_tail->action->type) {
853 case MATCHING_ACTION_MARK:
854 MSG_SET_PERM_FLAGS(markflags, MSG_MARKED);
857 case MATCHING_ACTION_UNMARK:
858 MSG_UNSET_PERM_FLAGS(markflags, MSG_MARKED);
861 case MATCHING_ACTION_MARK_AS_READ:
862 MSG_UNSET_PERM_FLAGS(markflags, MSG_UNREAD);
865 case MATCHING_ACTION_MARK_AS_UNREAD:
866 MSG_SET_PERM_FLAGS(markflags, MSG_UNREAD);
870 case MATCHING_ACTION_FORWARD:
871 case MATCHING_ACTION_FORWARD_AS_ATTACHMENT:
872 case MATCHING_ACTION_MOVE:
873 case MATCHING_ACTION_DELETE:
883 /* msgnum & dest_folder should have valid values after a succesful
884 * drop; in this case there is a MsgInfo available */
888 debug_print("*** performing actions\n");
890 for (ma_tail = &ma_head ; ma_tail->action; ma_tail = ma_tail->next) {
892 /* there are variables you have to use when defining new actions:
894 * copy_to_inbox_too - if the original message should be copied to the inbox
895 * (default_folder) too.
897 * also note that after dropping it to a folder (folder_item_add_msg()) you have
898 * to mark the message, just to make sure any defined mark actions are applied. */
900 #define ACTION (ma_tail->action->type)
902 /* C O N T I N U A B L E */
904 if (MATCHING_ACTION_COPY == ACTION) {
905 debug_print("*** performing copy\n");
906 copy_to_inbox_too = TRUE;
907 if (!prepare_destination(ma_tail->action->destination, &dest_folder, folder_table)) {
908 debug_print("Rule failed: unknown destination %s\n", ma_tail->action->destination);
909 continue; /* try next action */
911 if (0 > (msgnum = folder_item_add_msg(dest_folder, filename, FALSE))) {
912 debug_print(_("Rule failed: could not copy to folder %s\n"),
913 ma_tail->action->destination);
914 continue; /* try next action */
916 flags = msginfo->flags.perm_flags | markflags.perm_flags;
917 add_mark(dest_folder, msgnum, flags);
919 else if (MATCHING_EXECUTE == ACTION) {
920 debug_print("*** performing exec\n");
921 copy_to_inbox_too = TRUE;
923 /* matching_build_command() knows about filtering */
924 cmd = matching_build_command(ma_tail->action->destination, msginfo);
930 debug_print(_("Rule failed: no command line\n"));
933 /* U N C O N T I N U A B L E */
935 else if (MATCHING_ACTION_FORWARD == ACTION
936 || MATCHING_ACTION_FORWARD_AS_ATTACHMENT == ACTION) {
937 debug_print("*** performing forward\n");
939 /* forwarding messages is complicated because there's currently no
940 * way to forward a message using "filenames"; you can only forward
941 * a message if you have its MsgInfo. this means we have to drop
942 * the message first */
943 if (0 > (msgnum = folder_item_add_msg(default_folder, filename, FALSE))) {
944 debug_print(_("Rule failed: could not forward\n"));
945 copy_to_inbox_too = TRUE;
949 flags = msginfo->flags.perm_flags | markflags.perm_flags;
950 add_mark(default_folder, msgnum, flags);
952 /* grab the dropped message */
953 fwd_msg_name = folder_item_fetch_msg(default_folder, msgnum);
955 tmp.perm_flags = tmp.tmp_flags = 0;
956 fwd_msg = procheader_parse(fwd_msg_name, tmp, TRUE);
958 /* do the compose_XXX stuff */
959 account = account_find_from_id(ma_tail->action->account_id);
960 compose = compose_forward(account, fwd_msg, ma_tail->action->type == ACTION ? FALSE : TRUE);
961 if (compose->account->protocol == A_NNTP)
962 compose_entry_append(compose, ma_tail->action->destination,
965 compose_entry_append(compose, ma_tail->action->destination,
968 compose_send(compose);
970 procmsg_msginfo_free(fwd_msg);
972 gtk_widget_destroy(compose->window);
975 else if (MATCHING_ACTION_DELETE == ACTION) {
976 debug_print("*** performing delete\n");
977 copy_to_inbox_too = FALSE;
978 if (unlink(filename) < 0)
979 debug_print(_("Rule failed: could not delete message\n"));
982 else if (MATCHING_ACTION_MOVE == ACTION) {
983 debug_print("*** performing move\n");
984 copy_to_inbox_too = FALSE;
986 if (!prepare_destination(ma_tail->action->destination, &dest_folder, folder_table)) {
987 copy_to_inbox_too = TRUE;
991 msgnum = folder_item_add_msg(dest_folder, filename, FALSE);
993 debug_print(_("Rule failed: could not move to folder %s\n"),
994 ma_tail->action->destination);
995 copy_to_inbox_too = TRUE;
999 flags = msginfo->flags.perm_flags | markflags.perm_flags;
1000 add_mark(dest_folder, msgnum, flags);
1005 /* may need to copy it to inbox too */
1006 if (copy_to_inbox_too) {
1007 debug_print("*** performing inbox copy\n");
1008 msgnum = folder_item_add_msg(default_folder, filename, TRUE);
1010 debug_print(_("error copying incoming file %s to inbox\n"),
1014 flags = msginfo->flags.perm_flags | markflags.perm_flags;
1015 add_mark(default_folder, msgnum, flags);
1018 debug_print("*** unlinking\n");
1019 if (unlink(filename) < 0)
1020 debug_print(_("error deleting incoming message file\n"));
1028 static void filter_incoming_msginfo(FolderItem *default_folder, MsgInfo *msginfo,
1029 GHashTable *folder_table)
1031 filter_incoming_perform_actions(default_folder, msginfo, folder_table);
1034 /* filter_incoming_message() - tries to apply a filter on one incoming message.
1035 * it also handles the case when there's no filter match */
1036 void filter_incoming_message(FolderItem *default_folder, const gchar *file_name,
1037 GHashTable *folder_table)
1040 MsgFlags msgflags = { 0, 0 };
1042 /* make an "uncomplete" msginfo. it's incomplete because it doesn't
1043 * have a message number / folder yet. */
1044 if (NULL == (msginfo = procheader_parse(file_name, msgflags, TRUE))) {
1045 g_warning(_("error filtering incoming message %s\n"),
1050 /* let matcher know that this is a message that has no
1051 * valid body data yet. */
1052 MSG_SET_TMP_FLAGS(msginfo->flags, MSG_FILTERING);
1053 msginfo->folder = g_strdup(file_name);
1054 msginfo->msgnum = 0;
1056 filter_incoming_msginfo(default_folder, msginfo, folder_table);
1058 g_free(msginfo->folder);
1059 procmsg_msginfo_free(msginfo);
1062 /******************************************************************************/
1064 void prefs_filtering_read_config(void)
1068 gchar buf[PREFSBUFSIZE];
1070 debug_print(_("Reading filtering configuration...\n"));
1072 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
1073 FILTERING_RC, NULL);
1074 if ((fp = fopen(rcpath, "r")) == NULL) {
1075 if (ENOENT != errno) FILE_OP_ERROR(rcpath, "fopen");
1077 prefs_filtering = NULL;
1082 /* remove all filtering */
1083 while (prefs_filtering != NULL) {
1084 FilteringProp * filtering =
1085 (FilteringProp *) prefs_filtering->data;
1086 filteringprop_free(filtering);
1087 prefs_filtering = g_slist_remove(prefs_filtering, filtering);
1090 while (fgets(buf, sizeof(buf), fp) != NULL) {
1091 FilteringProp * filtering;
1096 if ((*buf != '#') && (*buf != '\0')) {
1098 filtering = filteringprop_parse(&tmp);
1101 g_slist_append(prefs_filtering,
1106 g_warning(_("syntax error : %s\n"), buf);
1114 gchar * filteringaction_to_string(FilteringAction * action)
1116 gchar * command_str;
1118 gchar * account_id_str;
1121 command_str = get_matchparser_tab_str(action->type);
1123 if (command_str == NULL)
1126 switch(action->type) {
1127 case MATCHING_ACTION_MOVE:
1128 case MATCHING_ACTION_COPY:
1129 case MATCHING_EXECUTE:
1130 return g_strconcat(command_str, " \"", action->destination,
1133 case MATCHING_ACTION_DELETE:
1134 case MATCHING_ACTION_MARK:
1135 case MATCHING_ACTION_UNMARK:
1136 case MATCHING_ACTION_MARK_AS_READ:
1137 case MATCHING_ACTION_MARK_AS_UNREAD:
1138 return g_strdup(command_str);
1141 case MATCHING_ACTION_FORWARD:
1142 case MATCHING_ACTION_FORWARD_AS_ATTACHMENT:
1143 account_id_str = itos(action->account_id);
1144 return g_strconcat(command_str, " ", account_id_str,
1145 " \"", action->destination, "\"", NULL);
1152 gchar * filteringprop_to_string(FilteringProp * prop)
1156 gchar * filtering_str;
1158 action_str = filteringaction_to_string(prop->action);
1160 if (action_str == NULL)
1163 list_str = matcherlist_to_string(prop->matchers);
1165 if (list_str == NULL) {
1170 filtering_str = g_strconcat(list_str, " ", action_str, NULL);
1174 return filtering_str;
1177 void prefs_filtering_write_config(void)
1182 FilteringProp * prop;
1184 debug_print(_("Writing filtering configuration...\n"));
1186 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, FILTERING_RC, NULL);
1188 if ((pfile = prefs_write_open(rcpath)) == NULL) {
1189 g_warning(_("failed to write configuration to file\n"));
1194 for (cur = prefs_filtering; cur != NULL; cur = cur->next) {
1195 gchar *filtering_str;
1197 prop = (FilteringProp *) cur->data;
1198 filtering_str = filteringprop_to_string(prop);
1199 if (fputs(filtering_str, pfile->fp) == EOF ||
1200 fputc('\n', pfile->fp) == EOF) {
1201 FILE_OP_ERROR(rcpath, "fputs || fputc");
1202 prefs_write_close_revert(pfile);
1204 g_free(filtering_str);
1207 g_free(filtering_str);
1212 if (prefs_write_close(pfile) < 0) {
1213 g_warning(_("failed to write configuration to file\n"));