2010-11-04 [wwp] 3.7.6cvs62
[claws.git] / src / filtering.c
index 24c7644df11d5f34187bc59ecad253be5c1cf2d1..790be5772b4db5e574d5a7969704b415ccb19eab 100644 (file)
@@ -87,7 +87,7 @@ FilteringAction * filteringaction_new(int type, int account_id,
 
 void filteringaction_free(FilteringAction * action)
 {
-       g_return_if_fail(action);
+       cm_return_if_fail(action);
        g_free(action->header);
        g_free(action->destination);
        g_free(action);
@@ -168,7 +168,7 @@ void filteringprop_free(FilteringProp * prop)
 {
         GSList * tmp;
 
-       g_return_if_fail(prop);
+       cm_return_if_fail(prop);
        matcherlist_free(prop->matchers);
         
         for (tmp = prop->action_list ; tmp != NULL ; tmp = tmp->next) {
@@ -400,7 +400,7 @@ static gboolean filteringaction_apply(FilteringAction * action, MsgInfo * info)
                compose_entry_append(compose, action->destination,
                                     compose->account->protocol == A_NNTP
                                            ? COMPOSE_NEWSGROUPS
-                                           : COMPOSE_TO);
+                                           : COMPOSE_TO, PREF_NONE);
 
                val = compose_send(compose);
 
@@ -413,7 +413,7 @@ static gboolean filteringaction_apply(FilteringAction * action, MsgInfo * info)
                        break;
                else
                        compose_entry_append(compose, action->destination,
-                                            COMPOSE_TO);
+                                            COMPOSE_TO, PREF_NONE);
 
                val = compose_send(compose);
                
@@ -424,7 +424,8 @@ static gboolean filteringaction_apply(FilteringAction * action, MsgInfo * info)
                if (cmd == NULL)
                        return FALSE;
                else {
-                       system(cmd);
+                       if (system(cmd) == -1)
+                               g_warning("couldn't run %s", cmd);
                        g_free(cmd);
                }
                return TRUE;
@@ -531,8 +532,8 @@ static gboolean filteringaction_apply(FilteringAction * action, MsgInfo * info)
 gboolean filteringaction_apply_action_list(GSList *action_list, MsgInfo *info)
 {
        GSList *p;
-       g_return_val_if_fail(action_list, FALSE);
-       g_return_val_if_fail(info, FALSE);
+       cm_return_val_if_fail(action_list, FALSE);
+       cm_return_val_if_fail(info, FALSE);
        for (p = action_list; p && p->data; p = g_slist_next(p)) {
                FilteringAction *a = (FilteringAction *) p->data;
                if (filteringaction_apply(a, info)) {
@@ -719,7 +720,7 @@ static gboolean filtering_apply_rule(FilteringProp *filtering, MsgInfo *info,
     gboolean * final)
 {
        gboolean result = TRUE;
-       gchar    buf[256];
+       gchar    *buf;
         GSList * tmp;
         
         * final = FALSE;
@@ -727,9 +728,9 @@ static gboolean filtering_apply_rule(FilteringProp *filtering, MsgInfo *info,
                 FilteringAction * action;
 
                 action = tmp->data;
-                               filteringaction_to_string(buf, sizeof buf, action);
-                               if (debug_filtering_session)
-                                       log_print(LOG_DEBUG_FILTERING, _("applying action [ %s ]\n"), buf);
+               buf = filteringaction_to_string(action);
+               if (debug_filtering_session)
+                       log_print(LOG_DEBUG_FILTERING, _("applying action [ %s ]\n"), buf);
 
                 if (FALSE == (result = filteringaction_apply(action, info))) {
                                        if (debug_filtering_session) {
@@ -740,11 +741,12 @@ static gboolean filtering_apply_rule(FilteringProp *filtering, MsgInfo *info,
                                        } else
                                                g_warning("No further processing after rule %s\n", buf);
                 }
-                
+                g_free(buf);
                 if (filtering_is_final_action(action)) {
                         * final = TRUE;
                         break;
                 }
+               
         }
        return result;
 }
@@ -776,7 +778,7 @@ static gboolean filter_msginfo(GSList * filtering_list, MsgInfo * info, PrefsAcc
        gboolean final;
        gboolean apply_next;
        
-       g_return_val_if_fail(info != NULL, TRUE);
+       cm_return_val_if_fail(info != NULL, TRUE);
        
        for (l = filtering_list, final = FALSE, apply_next = FALSE; l != NULL; l = g_slist_next(l)) {
                FilteringProp * filtering = (FilteringProp *) l->data;
@@ -821,9 +823,10 @@ static gboolean filter_msginfo(GSList * filtering_list, MsgInfo * info, PrefsAcc
                }
        }
 
-       /* put in inbox if a final rule could not be applied, or
-        * the last rule was not a final one. */
-       if ((final && !apply_next) || !final) {
+    /* put in inbox if the last rule was not a final one, or
+     * a final rule could not be applied.
+     * Either of these cases is likely. */
+    if (!final || !apply_next) {
                return FALSE;
        }
 
@@ -911,12 +914,14 @@ gboolean filter_message_by_msginfo(GSList *flist, MsgInfo *info, PrefsAccount* a
        return ret;
 }
 
-gchar *filteringaction_to_string(gchar *dest, gint destlen, FilteringAction *action)
+gchar *filteringaction_to_string(FilteringAction *action)
 {
        const gchar *command_str;
        gchar * quoted_dest;
        gchar * quoted_header;
-       
+       GString *dest = g_string_new("");
+       gchar *deststr = NULL;
+
        command_str = get_matchparser_tab_str(action->type);
 
        if (command_str == NULL)
@@ -929,9 +934,9 @@ gchar *filteringaction_to_string(gchar *dest, gint destlen, FilteringAction *act
        case MATCHACTION_SET_TAG:
        case MATCHACTION_UNSET_TAG:
                quoted_dest = matcher_quote_str(action->destination);
-               g_snprintf(dest, destlen, "%s \"%s\"", command_str, quoted_dest);
+               g_string_append_printf(dest, "%s \"%s\"", command_str, quoted_dest);
                g_free(quoted_dest);
-               return dest;
+               break;
 
        case MATCHACTION_DELETE:
        case MATCHACTION_MARK:
@@ -947,43 +952,45 @@ gchar *filteringaction_to_string(gchar *dest, gint destlen, FilteringAction *act
        case MATCHACTION_IGNORE:
        case MATCHACTION_WATCH:
        case MATCHACTION_CLEAR_TAGS:
-               g_snprintf(dest, destlen, "%s", command_str);
-               return dest;
+               g_string_append_printf(dest, "%s", command_str);
+               break;
 
        case MATCHACTION_REDIRECT:
        case MATCHACTION_FORWARD:
        case MATCHACTION_FORWARD_AS_ATTACHMENT:
                quoted_dest = matcher_quote_str(action->destination);
-               g_snprintf(dest, destlen, "%s %d \"%s\"", command_str, action->account_id, quoted_dest);
+               g_string_append_printf(dest, "%s %d \"%s\"", command_str, action->account_id, quoted_dest);
                g_free(quoted_dest);
-               return dest; 
+               break;
 
        case MATCHACTION_COLOR:
-               g_snprintf(dest, destlen, "%s %d", command_str, action->labelcolor);
-               return dest;  
+               g_string_append_printf(dest, "%s %d", command_str, action->labelcolor);
+               break;
 
        case MATCHACTION_CHANGE_SCORE:
        case MATCHACTION_SET_SCORE:
-               g_snprintf(dest, destlen, "%s %d", command_str, action->score);
-               return dest;  
+               g_string_append_printf(dest, "%s %d", command_str, action->score);
+               break;
 
        case MATCHACTION_ADD_TO_ADDRESSBOOK:
                quoted_header = matcher_quote_str(action->header);
                quoted_dest = matcher_quote_str(action->destination);
-               g_snprintf(dest, destlen, "%s \"%s\" \"%s\"", command_str, quoted_header, quoted_dest);
+               g_string_append_printf(dest, "%s \"%s\" \"%s\"", command_str, quoted_header, quoted_dest);
                g_free(quoted_dest);
                g_free(quoted_header);
-               return dest;
+               break;
 
        default:
                return NULL;
        }
+       deststr = dest->str;
+       g_string_free(dest, FALSE);
+       return deststr;
 }
 
 gchar * filteringaction_list_to_string(GSList * action_list)
 {
        gchar *action_list_str;
-       gchar  buf[256];
         GSList * tmp;
        gchar *list_str;
 
@@ -994,8 +1001,7 @@ gchar * filteringaction_list_to_string(GSList * action_list)
                 
                 action = tmp->data;
                 
-                action_str = filteringaction_to_string(buf,
-                    sizeof buf, action);
+                action_str = filteringaction_to_string(action);
                 
                 if (action_list_str != NULL) {
                         list_str = g_strconcat(action_list_str, " ", action_str, NULL);
@@ -1004,6 +1010,7 @@ gchar * filteringaction_list_to_string(GSList * action_list)
                 else {
                         list_str = g_strdup(action_str);
                 }
+               g_free(action_str);
                 action_list_str = list_str;
         }
 
@@ -1016,6 +1023,9 @@ gchar * filteringprop_to_string(FilteringProp * prop)
        gchar *action_list_str;
        gchar *filtering_str;
 
+       if (prop == NULL)
+               return NULL;
+
         action_list_str = filteringaction_list_to_string(prop->action_list);
 
        if (action_list_str == NULL)
@@ -1049,8 +1059,8 @@ static gboolean prefs_filtering_free_func(GNode *node, gpointer data)
 {
        FolderItem *item = node->data;
 
-       g_return_val_if_fail(item, FALSE);
-       g_return_val_if_fail(item->prefs, FALSE);
+       cm_return_val_if_fail(item, FALSE);
+       cm_return_val_if_fail(item->prefs, FALSE);
 
        prefs_filtering_free(item->prefs->processing);
        item->prefs->processing = NULL;
@@ -1080,8 +1090,8 @@ void prefs_filtering_clear(void)
 
 void prefs_filtering_clear_folder(Folder *folder)
 {
-       g_return_if_fail(folder);
-       g_return_if_fail(folder->node);
+       cm_return_if_fail(folder);
+       cm_return_if_fail(folder->node);
 
        g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
                        prefs_filtering_free_func, NULL);