remove selective download
[claws.git] / src / filtering.c
index 5491c4ce927eb0671af247d710901146400f0a80..5640ad6f5017505e42865d27d1f83b7e396aaf01 100644 (file)
@@ -29,7 +29,7 @@
 #include "procheader.h"
 #include "matcher.h"
 #include "filtering.h"
-#include "prefs.h"
+#include "prefs_gtk.h"
 #include "compose.h"
 
 #define PREFSBUFSIZE           1024
@@ -57,6 +57,11 @@ FilteringAction * filteringaction_new(int type, int account_id,
 
        action = g_new0(FilteringAction, 1);
 
+       /* NOTE:
+        * if type is MATCHACTION_CHANGE_SCORE, account_id = (-1, 0, 1) and
+        * labelcolor = the score value change
+        */
+
        action->type = type;
        action->account_id = account_id;
        if (destination) {
@@ -92,6 +97,36 @@ FilteringProp * filteringprop_new(MatcherList * matchers,
        return filtering;
 }
 
+FilteringProp * filteringprop_copy(FilteringProp *src)
+{
+       FilteringProp * new;
+       GSList *tmp;
+       
+       new = g_new0(FilteringProp, 1);
+       new->matchers = g_new0(MatcherList, 1);
+       new->action = g_new0(FilteringAction, 1);
+       for (tmp = src->matchers->matchers; tmp != NULL && tmp->data != NULL;) {
+               MatcherProp *matcher = (MatcherProp *)tmp->data;
+               
+               new->matchers->matchers = g_slist_append(new->matchers->matchers,
+                                                  matcherprop_copy(matcher));
+               tmp = tmp->next;
+       }
+       new->matchers->bool_and = src->matchers->bool_and;
+       new->action->type = src->action->type;
+       new->action->account_id = src->action->account_id;
+       if (src->action->destination)
+               new->action->destination = g_strdup(src->action->destination);
+       else 
+               new->action->destination = NULL;
+       if (src->action->unesc_destination)
+               new->action->unesc_destination = g_strdup(src->action->unesc_destination);
+       else
+               new->action->unesc_destination = NULL;
+       new->action->labelcolor = src->action->labelcolor;
+       return new;
+}
+
 void filteringprop_free(FilteringProp * prop)
 {
        matcherlist_free(prop->matchers);
@@ -105,16 +140,7 @@ void filteringprop_free(FilteringProp * prop)
   return value : return TRUE if the action could be applied
 */
 
-#define CHANGE_FLAGS(msginfo) \
-{ \
-if (msginfo->folder->folder->change_flags != NULL) \
-msginfo->folder->folder->change_flags(msginfo->folder->folder, \
-                                     msginfo->folder, \
-                                     msginfo); \
-}
-
-static gboolean filteringaction_apply(FilteringAction * action, MsgInfo * info,
-                                     GHashTable *folder_table)
+static gboolean filteringaction_apply(FilteringAction * action, MsgInfo * info)
 {
        FolderItem * dest_folder;
        gint val;
@@ -134,15 +160,6 @@ static gboolean filteringaction_apply(FilteringAction * action, MsgInfo * info,
                        return FALSE;
                }       
 
-               if (folder_table) {
-                       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));
-                       }
-               }
                return TRUE;
 
        case MATCHACTION_COPY:
@@ -155,15 +172,6 @@ static gboolean filteringaction_apply(FilteringAction * action, MsgInfo * info,
                if (folder_item_copy_msg(dest_folder, info) == -1)
                        return FALSE;
 
-               if (folder_table) {
-                       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));
-                       }
-               }
                return TRUE;
 
        case MATCHACTION_DELETE:
@@ -172,23 +180,25 @@ 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:
@@ -229,9 +239,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
@@ -257,9 +267,21 @@ static gboolean filteringaction_apply(FilteringAction * action, MsgInfo * info,
                }
                return TRUE;
 
+       case MATCHACTION_CHANGE_SCORE:
+               /* NOTE:
+                * action->account_id is 0 if just assignment, -1 if decrement
+                * and 1 if increment by action->labelcolor 
+                * action->labelcolor has the score value change
+                */
+               info->score = action->account_id ==  1 ? info->score + action->labelcolor
+                           : action->account_id == -1 ? info->score - action->labelcolor
+                           : action->labelcolor; 
+               return TRUE;
+
        default:
-               return FALSE;
+               break;
        }
+       return FALSE;
 }
 
 static gboolean filtering_match_condition(FilteringProp *filtering, MsgInfo *info)
@@ -267,16 +289,14 @@ static gboolean filtering_match_condition(FilteringProp *filtering, MsgInfo *inf
        return matcherlist_match(filtering->matchers, info);
 }
 
-static gboolean filtering_apply_rule(FilteringProp *filtering, MsgInfo *info, 
-                                    GHashTable *foldertable)
+static gboolean filtering_apply_rule(FilteringProp *filtering, MsgInfo *info)
 {
        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));
+       if (FALSE == (result = filteringaction_apply(filtering->action, info))) {
+               g_warning("action %s could not be applied"
+               filteringaction_to_string(buf, sizeof buf, filtering->action));
        }
        return result;
 }
@@ -295,7 +315,7 @@ static gboolean filtering_is_final_action(FilteringProp *filtering)
        case MATCHACTION_MARK_AS_UNREAD:
        case MATCHACTION_FORWARD:
        case MATCHACTION_FORWARD_AS_ATTACHMENT:
-       case MATCHACTION_BOUNCE:
+       case MATCHACTION_REDIRECT:
                return FALSE; /* MsgInfo still valid for message */
        default:
                return FALSE;
@@ -303,15 +323,14 @@ static gboolean filtering_is_final_action(FilteringProp *filtering)
 }
 
 static void filter_msginfo(GSList * filtering_list, FolderItem *inbox,
-                          MsgInfo * info, GHashTable *folder_table)
+                          MsgInfo * info)
 {
        GSList  *l;
        gboolean final;
        gboolean applied;
-       gint val;
        
        if (info == NULL) {
-               g_warning(_("msginfo is not set"));
+               g_warning("msginfo is not set");
                return;
        }
        
@@ -319,7 +338,7 @@ static void filter_msginfo(GSList * filtering_list, FolderItem *inbox,
                FilteringProp * filtering = (FilteringProp *) l->data;
 
                if (filtering_match_condition(filtering, info)) {
-                       applied = filtering_apply_rule(filtering, info, folder_table);
+                       applied = filtering_apply_rule(filtering, info);
                        if (TRUE == (final = filtering_is_final_action(filtering)))
                                break;
                }               
@@ -328,47 +347,54 @@ static void filter_msginfo(GSList * filtering_list, FolderItem *inbox,
        /* 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 (folder_item_move_msg(inbox, info) == -1) {
-                       debug_print(_("*** Could not drop message in inbox; still in .processing\n"));
-                       return;
+               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
+ */
+void filter_message_by_msginfo_with_inbox(GSList *flist, MsgInfo *info, 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);
+}
 
-               switch (filtering->action->type) {
-               case MATCHACTION_MOVE:
-               case MATCHACTION_DELETE:
-                       if (filtering_match_condition(filtering, info) &&
-                           filtering_apply_rule(filtering, info, folder_table))
-                               return;
-               }
-       }
+void filter_message_by_msginfo(GSList *flist, MsgInfo *info)
+{
+       filter_message_by_msginfo_with_inbox(flist, info, 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
+  *            changed after the call to this function
+  */
 void filter_message(GSList *filtering_list, FolderItem *inbox,
-                   gint msgnum, GHashTable *folder_table)
+                   gint msgnum)
 {
        MsgInfo *msginfo;
        gchar *filename;
@@ -376,37 +402,37 @@ void filter_message(GSList *filtering_list, FolderItem *inbox,
        FolderItem *item = folder_get_default_processing();
 
        if (item == NULL) {
-               g_warning(_("folderitem not set"));
+               g_warning("folderitem not set");
                return;
        }
 
        filename = folder_item_fetch_msg(item, msgnum);
 
        if (filename == NULL) {
-               g_warning(_("filename is not set"));
+               g_warning("filename is not set");
                return;
        }
 
-       msginfo = procheader_parse(filename, msgflags, TRUE, FALSE);
+       msginfo = procheader_parse_file(filename, msgflags, TRUE, FALSE);
        
        g_free(filename);
 
        if (msginfo == NULL) {
-               g_warning(_("could not get info for %s"), filename);
+               g_warning("could not get info for %s", filename);
                return;
        }
 
        msginfo->folder = item;
        msginfo->msgnum = msgnum;
 
-       filter_msginfo(filtering_list, inbox, msginfo, folder_table);
+       filter_msginfo(filtering_list, inbox, msginfo);
 
        procmsg_msginfo_free(msginfo);
 }
 
 gchar *filteringaction_to_string(gchar *dest, gint destlen, FilteringAction *action)
 {
-       gchar *command_str;
+       const gchar *command_str;
 
        command_str = get_matchparser_tab_str(action->type);
 
@@ -428,7 +454,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); 
@@ -437,9 +463,6 @@ 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;
        }