* src/filtering.c
[claws.git] / src / filtering.c
index 4dd3cb9914cf1fa2fd1ef9a82ca19748ec259d97..01a03f7a274f6b6f818ba8557b90663b8e358a4a 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2001 Hiroyuki Yamamoto & The Sylpheed Claws Team
+ * Copyright (C) 1999-2002 Hiroyuki Yamamoto & The Sylpheed Claws Team
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -138,7 +138,6 @@ static gboolean filteringaction_apply(FilteringAction * action, MsgInfo * info,
                        val = GPOINTER_TO_INT(g_hash_table_lookup
                                              (folder_table, dest_folder));
                        if (val == 0) {
-                               folder_item_scan(dest_folder);
                                g_hash_table_insert(folder_table, dest_folder,
                                                    GINT_TO_POINTER(1));
                        }
@@ -159,7 +158,6 @@ static gboolean filteringaction_apply(FilteringAction * action, MsgInfo * info,
                        val = GPOINTER_TO_INT(g_hash_table_lookup
                                              (folder_table, dest_folder));
                        if (val == 0) {
-                               folder_item_scan(dest_folder);
                                g_hash_table_insert(folder_table, dest_folder,
                                                    GINT_TO_POINTER(1));
                        }
@@ -172,28 +170,30 @@ static gboolean filteringaction_apply(FilteringAction * action, MsgInfo * info,
                return TRUE;
 
        case MATCHACTION_MARK:
-               MSG_SET_PERM_FLAGS(info->flags, MSG_MARKED);
+               procmsg_msginfo_set_flags(info, MSG_MARKED, 0);
                return TRUE;
 
        case MATCHACTION_UNMARK:
-               MSG_UNSET_PERM_FLAGS(info->flags, MSG_MARKED);
+               procmsg_msginfo_unset_flags(info, MSG_MARKED, 0);
                return TRUE;
                
        case MATCHACTION_MARK_AS_READ:
-               MSG_UNSET_PERM_FLAGS(info->flags, MSG_UNREAD | MSG_NEW);
+               procmsg_msginfo_unset_flags(info, MSG_UNREAD | MSG_NEW, 0);
                return TRUE;
 
        case MATCHACTION_MARK_AS_UNREAD:
-               MSG_SET_PERM_FLAGS(info->flags, MSG_UNREAD | MSG_NEW);
+               debug_print("*** setting unread flags\n");
+               procmsg_msginfo_set_flags(info, MSG_UNREAD | MSG_NEW, 0);
                return TRUE;
        
        case MATCHACTION_COLOR:
-               MSG_SET_COLORLABEL_VALUE(info->flags, action->labelcolor);
+               procmsg_msginfo_unset_flags(info, MSG_CLABEL_FLAG_MASK, 0); 
+               procmsg_msginfo_set_flags(info, MSG_COLORLABEL_TO_FLAGS(action->labelcolor), 0);
                return TRUE;
 
        case MATCHACTION_FORWARD:
                account = account_find_from_id(action->account_id);
-               compose = compose_forward(account, info, FALSE);
+               compose = compose_forward(account, info, FALSE, NULL);
                if (compose->account->protocol == A_NNTP)
                        compose_entry_append(compose, action->destination,
                                             COMPOSE_NEWSGROUPS);
@@ -213,7 +213,7 @@ static gboolean filteringaction_apply(FilteringAction * action, MsgInfo * info,
        case MATCHACTION_FORWARD_AS_ATTACHMENT:
 
                account = account_find_from_id(action->account_id);
-               compose = compose_forward(account, info, TRUE);
+               compose = compose_forward(account, info, TRUE, NULL);
                if (compose->account->protocol == A_NNTP)
                        compose_entry_append(compose, action->destination,
                                             COMPOSE_NEWSGROUPS);
@@ -229,9 +229,9 @@ static gboolean filteringaction_apply(FilteringAction * action, MsgInfo * info,
                gtk_widget_destroy(compose->window);
                return FALSE;
 
-       case MATCHACTION_BOUNCE:
+       case MATCHACTION_REDIRECT:
                account = account_find_from_id(action->account_id);
-               compose = compose_bounce(account, info);
+               compose = compose_redirect(account, info);
                if (compose->account->protocol == A_NNTP)
                        break;
                else
@@ -262,106 +262,129 @@ static gboolean filteringaction_apply(FilteringAction * action, MsgInfo * info,
        }
 }
 
-/* filteringprop_apply() - runs the action on one MsgInfo if it matches the 
- * criterium. certain actions can be followed by other actions. in this
- * case the function returns FALSE. if an action can not be followed
- * by others, the function returns TRUE. */
-static gboolean filteringprop_apply(FilteringProp * filtering, MsgInfo * info,
-                                   GHashTable *folder_table)
+static gboolean filtering_match_condition(FilteringProp *filtering, MsgInfo *info)
 {
-       if (matcherlist_match(filtering->matchers, info)) {
-               gboolean result;
-               gchar   *action_str;
-               gchar    buf[256]; 
-
-               if (FALSE == (result = filteringaction_apply(filtering->action, info,
-                                              folder_table))) {
-                       action_str = filteringaction_to_string(buf, sizeof buf, filtering->action);
-                       g_warning(_("action %s could not be applied"), action_str);
-               }
+       return matcherlist_match(filtering->matchers, info);
+}
 
-               switch(filtering->action->type) {
-               case MATCHACTION_MOVE:
-               case MATCHACTION_DELETE:
-                       return TRUE; /* MsgInfo invalid for message */
-               case MATCHACTION_EXECUTE:
-               case MATCHACTION_COPY:
-               case MATCHACTION_MARK:
-               case MATCHACTION_MARK_AS_READ:
-               case MATCHACTION_UNMARK:
-               case MATCHACTION_MARK_AS_UNREAD:
-               case MATCHACTION_FORWARD:
-               case MATCHACTION_FORWARD_AS_ATTACHMENT:
-               case MATCHACTION_BOUNCE:
-                       return FALSE; /* MsgInfo still valid for message */
-               default:
-                       return FALSE;
-               }
+static gboolean filtering_apply_rule(FilteringProp *filtering, MsgInfo *info, 
+                                    GHashTable *foldertable)
+{
+       gboolean result;
+       gchar    actionstr;
+       gchar    buf[50];
+
+       if (FALSE == (result = filteringaction_apply(filtering->action, info, foldertable))) {
+               g_warning(_("action %s could not be applied"), 
+               filteringaction_to_string(buf, sizeof buf, filtering->action));
        }
-       else
+       return result;
+}
+
+static gboolean filtering_is_final_action(FilteringProp *filtering)
+{
+       switch(filtering->action->type) {
+       case MATCHACTION_MOVE:
+       case MATCHACTION_DELETE:
+               return TRUE; /* MsgInfo invalid for message */
+       case MATCHACTION_EXECUTE:
+       case MATCHACTION_COPY:
+       case MATCHACTION_MARK:
+       case MATCHACTION_MARK_AS_READ:
+       case MATCHACTION_UNMARK:
+       case MATCHACTION_MARK_AS_UNREAD:
+       case MATCHACTION_FORWARD:
+       case MATCHACTION_FORWARD_AS_ATTACHMENT:
+       case MATCHACTION_REDIRECT:
+               return FALSE; /* MsgInfo still valid for message */
+       default:
                return FALSE;
+       }
 }
 
 static void filter_msginfo(GSList * filtering_list, FolderItem *inbox,
                           MsgInfo * info, GHashTable *folder_table)
 {
-       GSList          *l;
-       gboolean         result;
+       GSList  *l;
+       gboolean final;
+       gboolean applied;
+       gint val;
        
        if (info == NULL) {
                g_warning(_("msginfo is not set"));
                return;
        }
        
-       for(l = filtering_list ; l != NULL ; l = g_slist_next(l)) {
+       for (l = filtering_list, final = FALSE, applied = FALSE; l != NULL; l = g_slist_next(l)) {
                FilteringProp * filtering = (FilteringProp *) l->data;
-               if (TRUE == (result = filteringprop_apply(filtering, info, folder_table))) 
-                       break;
-       }
 
-       /* drop in inbox too */
-       if (!result) {
-               gint val;
-
-               if (folder_item_move_msg(inbox, info) == -1) {
-                       debug_print(_("*** Could not drop message in inbox; still in .processing\n"));
-                       return;
-               }       
+               if (filtering_match_condition(filtering, info)) {
+                       applied = filtering_apply_rule(filtering, info, folder_table);
+                       if (TRUE == (final = filtering_is_final_action(filtering)))
+                               break;
+               }               
+       }
 
-               if (folder_table) {
-                       val = GPOINTER_TO_INT(g_hash_table_lookup
-                                             (folder_table, inbox));
-                       if (val == 0) {
-                               folder_item_scan(inbox);
-                               g_hash_table_insert(folder_table, inbox,
-                                                   GINT_TO_POINTER(1));
+       /* put in inbox if a final rule could not be applied, or
+        * the last rule was not a final one. */
+       if ((final && !applied) || !final) {
+               if (inbox != info->folder) {
+                       if (folder_item_move_msg(inbox, info) == -1) {
+                               debug_print("*** Could not drop message in inbox; check .processing\n");
+                               return;
+                       }       
+                       if (folder_table) {
+                               val = GPOINTER_TO_INT(g_hash_table_lookup
+                                                     (folder_table, inbox));
+                               if (val == 0) {
+                                       folder_item_scan(inbox);
+                                       g_hash_table_insert(folder_table, inbox,
+                                                           GINT_TO_POINTER(1));
+                               }
                        }
-               }
+               }       
        }
 }
 
-void filter_msginfo_move_or_delete(GSList * filtering_list, MsgInfo * info,
-                                  GHashTable *folder_table)
+/*!
+ *\brief       filters a message based on its message info data
+ *
+ *\param       flist filter and actions list
+ *\param       info message
+ *\param       ftable table with changed folders after call
+ */
+void filter_message_by_msginfo_with_inbox(GSList *flist, MsgInfo *info, GHashTable *ftable, FolderItem *def_inbox)
 {
-       GSList * l;
-
-       if (info == NULL) {
-               g_warning(_("msginfo is not set"));
-               return;
-       }
-       
-       for(l = filtering_list ; l != NULL ; l = g_slist_next(l)) {
-               FilteringProp * filtering = (FilteringProp *) l->data;
+       FolderItem *inbox;
+
+       if ((def_inbox == NULL)) {
+               debug_print("using default inbox as final destination!\n");
+               inbox = folder_get_default_inbox();
+       } else
+               inbox = def_inbox;
+
+       /*
+        * message is already in a folder. the filtering code will
+        * handle duplicate moves and copies.
+        */
+       filter_msginfo(flist, inbox, info, ftable);
+}
 
-               switch (filtering->action->type) {
-               case MATCHACTION_MOVE:
-               case MATCHACTION_DELETE:
-                       if (filteringprop_apply(filtering, info, folder_table))
-                               return;
-               }
-       }
+void filter_message_by_msginfo(GSList *flist, MsgInfo *info, GHashTable *ftable)
+{
+       filter_message_by_msginfo_with_inbox(flist, info, ftable, info->folder);
 }
 
+/*!
+ *\brief       filters a message waiting to be processed in the
+ *             .processing folder. 
+ *
+  *\param      filtering_list list of filters and actions
+  *\param      inbox default inbox when no filter could be applied
+  *\param      msgnum message number in processing folder
+  *\param      folder_table table with folders that have been
+  *            changed after the call to this function
+  */
 void filter_message(GSList *filtering_list, FolderItem *inbox,
                    gint msgnum, GHashTable *folder_table)
 {
@@ -382,7 +405,7 @@ void filter_message(GSList *filtering_list, FolderItem *inbox,
                return;
        }
 
-       msginfo = procheader_parse(filename, msgflags, TRUE);
+       msginfo = procheader_parse_file(filename, msgflags, TRUE, FALSE);
        
        g_free(filename);
 
@@ -395,6 +418,8 @@ void filter_message(GSList *filtering_list, FolderItem *inbox,
        msginfo->msgnum = msgnum;
 
        filter_msginfo(filtering_list, inbox, msginfo, folder_table);
+
+       procmsg_msginfo_free(msginfo);
 }
 
 gchar *filteringaction_to_string(gchar *dest, gint destlen, FilteringAction *action)
@@ -421,7 +446,7 @@ gchar *filteringaction_to_string(gchar *dest, gint destlen, FilteringAction *act
                g_snprintf(dest, destlen, "%s", command_str);
                return dest;
 
-       case MATCHACTION_BOUNCE:
+       case MATCHACTION_REDIRECT:
        case MATCHACTION_FORWARD:
        case MATCHACTION_FORWARD_AS_ATTACHMENT:
                g_snprintf(dest, destlen, "%s %d \"%s\"", command_str, action->account_id, action->destination); 
@@ -430,7 +455,9 @@ gchar *filteringaction_to_string(gchar *dest, gint destlen, FilteringAction *act
        case MATCHACTION_COLOR:
                g_snprintf(dest, destlen, "%s %d", command_str, action->labelcolor);
                return dest;  
-
+       case MATCHACTION_DELETE_ON_SERVER:
+               g_snprintf(dest, destlen, "%s", command_str);
+               return dest;
        default:
                return NULL;
        }