2006-02-06 [colin] 2.0.0cvs21
[claws.git] / src / procmsg.c
index 48e949f684e2d23ab808b7c868290c422ccf3d96..0ee9100e3dc838d2b656b1d88e32ca47ff8869ab 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2005 Hiroyuki Yamamoto
+ * Copyright (C) 1999-2006 Hiroyuki Yamamoto and 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
@@ -14,7 +14,7 @@
  *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  */
 
 #include "defs.h"
@@ -332,13 +332,14 @@ GNode *procmsg_get_thread_tree(GSList *mlist)
        return root;
 }
 
-void procmsg_move_messages(GSList *mlist)
+gint procmsg_move_messages(GSList *mlist)
 {
        GSList *cur, *movelist = NULL;
        MsgInfo *msginfo;
        FolderItem *dest = NULL;
+       gint retval = 0;
 
-       if (!mlist) return;
+       if (!mlist) return 0;
 
        folder_item_update_freeze();
 
@@ -360,11 +361,12 @@ void procmsg_move_messages(GSList *mlist)
        }
 
        if (movelist) {
-               folder_item_move_msgs(dest, movelist);
+               retval = folder_item_move_msgs(dest, movelist);
                g_slist_free(movelist);
        }
 
        folder_item_update_thaw();
+       return retval;
 }
 
 void procmsg_copy_messages(GSList *mlist)
@@ -547,6 +549,8 @@ void procmsg_get_filter_keyword(MsgInfo *msginfo, gchar **header, gchar **key,
                                       {"X-Mailing-list:", NULL, TRUE},
                                       {"List-Id:",        NULL, TRUE},
                                       {"X-Sequence:",     NULL, TRUE},
+                                      {"Sender:",         NULL, TRUE},
+                                      {"List-Post:",      NULL, TRUE},
                                       {NULL,              NULL, FALSE}};
        enum
        {
@@ -555,7 +559,9 @@ void procmsg_get_filter_keyword(MsgInfo *msginfo, gchar **header, gchar **key,
                H_X_LIST         = 2,
                H_X_MAILING_LIST = 3,
                H_LIST_ID        = 4,
-               H_X_SEQUENCE     = 5
+               H_X_SEQUENCE     = 5,
+               H_SENDER         = 6,
+               H_LIST_POST      = 7
        };
 
        FILE *fp;
@@ -608,6 +614,13 @@ void procmsg_get_filter_keyword(MsgInfo *msginfo, gchar **header, gchar **key,
                                }
                        }
                        g_strstrip(*key);
+               } else if (hentry[H_SENDER].body != NULL) {
+                       SET_FILTER_KEY("header \"Sender\"", H_SENDER);
+               } else if (hentry[H_LIST_POST].body != NULL) {
+                       SET_FILTER_KEY("header \"Sender\"", H_LIST_POST);
+               } else if (msginfo->to) {
+                       *header = g_strdup("to");
+                       *key = g_strdup(msginfo->to);
                } else if (msginfo->subject) {
                        *header = g_strdup("subject");
                        *key = g_strdup(msginfo->subject);
@@ -625,6 +638,10 @@ void procmsg_get_filter_keyword(MsgInfo *msginfo, gchar **header, gchar **key,
                hentry[H_X_MAILING_LIST].body = NULL;
                g_free(hentry[H_LIST_ID].body);
                hentry[H_LIST_ID].body = NULL;
+               g_free(hentry[H_SENDER].body);
+               hentry[H_SENDER].body = NULL;
+               g_free(hentry[H_LIST_POST].body);
+               hentry[H_LIST_POST].body = NULL;
 
                break;
        case FILTER_BY_FROM:
@@ -646,6 +663,13 @@ void procmsg_get_filter_keyword(MsgInfo *msginfo, gchar **header, gchar **key,
 
 void procmsg_empty_trash(FolderItem *trash)
 {
+       GNode *node, *next;
+
+       if (!trash || 
+           (trash->stype != F_TRASH && 
+            !folder_has_parent_of_type(trash, F_TRASH)))
+               return;
+
        if (trash && trash->total_msgs > 0) {
                GSList *mlist = folder_item_get_msg_list(trash);
                GSList *cur;
@@ -662,6 +686,16 @@ void procmsg_empty_trash(FolderItem *trash)
                g_slist_free(mlist);
                folder_item_remove_all_msg(trash);
        }
+
+       if (!trash->node || !trash->node->children)
+               return;
+
+       node = trash->node->children;
+       while (node != NULL) {
+               next = node->next;
+               procmsg_empty_trash(FOLDER_ITEM(node->data));
+               node = next;
+       }
 }
 
 void procmsg_empty_all_trash(void)
@@ -746,9 +780,10 @@ parse_again:
        cur = orig;
        while (cur) {
                gchar *file = NULL;
-               PrefsAccount *ac = procmsg_get_account_from_file(file);
+               PrefsAccount *ac = NULL;
                msg = (MsgInfo *)cur->data;
                file = folder_item_fetch_msg(queue, msg->msgnum);
+               ac = procmsg_get_account_from_file(file);
                g_free(file);
 
                if (last_account == NULL || (ac != NULL && ac == last_account)) {
@@ -821,6 +856,7 @@ gint procmsg_send_queue(FolderItem *queue, gboolean save_msgs)
        gint sent = 0, err = 0;
        GSList *list, *elem;
        GSList *sorted_list = NULL;
+       GNode *node, *next;
 
        if (!queue)
                queue = folder_get_default_queue();
@@ -869,6 +905,21 @@ gint procmsg_send_queue(FolderItem *queue, gboolean save_msgs)
        }
 
        g_slist_free(sorted_list);
+       folder_item_scan(queue);
+
+       if (queue->node && queue->node->children) {
+               node = queue->node->children;
+               while (node != NULL) {
+                       int res = 0;
+                       next = node->next;
+                       res = procmsg_send_queue(FOLDER_ITEM(node->data), save_msgs);
+                       if (res < 0) 
+                               err = -res;
+                       else
+                               sent += res;
+                       node = next;
+               }
+       }
 
        return (err != 0 ? -err : sent);
 }
@@ -892,6 +943,19 @@ gboolean procmsg_queue_is_empty(FolderItem *queue)
        list = folder_item_get_msg_list(queue);
        res = (list == NULL);
        procmsg_msg_list_free(list);
+
+       if (res == TRUE) {
+               GNode *node, *next;
+               if (queue->node && queue->node->children) {
+                       node = queue->node->children;
+                       while (node != NULL) {
+                               next = node->next;
+                               if (!procmsg_queue_is_empty(FOLDER_ITEM(node->data)))
+                                       return FALSE;
+                               node = next;
+                       }
+               }
+       }
        return res;
 }
 
@@ -916,8 +980,54 @@ gint procmsg_remove_special_headers(const gchar *in, const gchar *out)
        fclose(outfp);
        fclose(fp);
        return 0;
+}
+#if 0
+gchar *procmsg_add_special_headers(const gchar *in, FolderItem *item)
+{
+       gchar *out = get_tmp_file();
+       FILE *fp = NULL;
+       PrefsAccount *account = NULL;
+       if (out == NULL)
+               return NULL;
+
+       fp = fopen(out, "wb");
+       if (fp == NULL) {
+               g_free(out);
+               return NULL;
+       }
+
+       if (item && item->prefs && item->prefs->enable_default_account)
+               account = account_find_from_id(item->prefs->default_account);
+
+       if (!account) account = cur_account;
+
+       if (!account) {
+               fclose(fp);
+               g_free(out);
+               return NULL;
+       }
+
+       fprintf(fp, "X-Sylpheed-Account-Id:%d\n", account->account_id);
+       fprintf(fp, "S:%s\n", account->address);
+       if (item && item->prefs && item->prefs->save_copy_to_folder) {
+               gchar *folderidentifier;
+
+               gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(compose->savemsg_checkbtn), prefs_common.savemsg);
+               folderidentifier = folder_item_get_identifier(item);
+               fprintf(fp, "SCF:%s\n", folderidentifier);
+               g_free(folderidentifier);
+       } else if (account_get_special_folder(account, F_OUTBOX)) {
+               gchar *folderidentifier = folder_item_get_identifier(account_get_special_folder
+                                 (compose->account, F_OUTBOX));
+               fprintf(fp, "SCF:%s\n", folderidentifier);
+               g_free(folderidentifier);
+       }
 
+       fprintf(fp, "\n");
+       fclose(fp);
+       return out;
 }
+#endif
 gint procmsg_save_to_outbox(FolderItem *outbox, const gchar *file,
                            gboolean is_queued)
 {
@@ -1088,6 +1198,7 @@ MsgInfo *procmsg_msginfo_copy(MsgInfo *msginfo)
        MEMBCOPY(folder);
        MEMBCOPY(to_folder);
 
+       MEMBDUP(face);
        MEMBDUP(xface);
        MEMBDUP(dispositionnotificationto);
        MEMBDUP(returnreceiptto);
@@ -1100,7 +1211,6 @@ MsgInfo *procmsg_msginfo_copy(MsgInfo *msginfo)
         newmsginfo->references = g_slist_reverse(newmsginfo->references);
 
        MEMBCOPY(score);
-       MEMBCOPY(threadscore);
        MEMBDUP(plaintext_file);
 
        return newmsginfo;
@@ -1131,6 +1241,8 @@ MsgInfo *procmsg_msginfo_get_full_info(MsgInfo *msginfo)
         * procheader.c::procheader_get_headernames() */
        if (!msginfo->xface)
                msginfo->xface = g_strdup(full_msginfo->xface);
+       if (!msginfo->face)
+               msginfo->face = g_strdup(full_msginfo->face);
        if (!msginfo->dispositionnotificationto)
                msginfo->dispositionnotificationto = 
                        g_strdup(full_msginfo->dispositionnotificationto);
@@ -1170,6 +1282,7 @@ void procmsg_msginfo_free(MsgInfo *msginfo)
        g_free(msginfo->returnreceiptto);
        g_free(msginfo->dispositionnotificationto);
        g_free(msginfo->xface);
+       g_free(msginfo->face);
 
        g_free(msginfo->fromname);
 
@@ -1221,6 +1334,8 @@ guint procmsg_msginfo_memusage(MsgInfo *msginfo)
                memusage += strlen(msginfo->inreplyto);
        if (msginfo->xface)
                memusage += strlen(msginfo->xface);
+       if (msginfo->face)
+               memusage += strlen(msginfo->face);
        if (msginfo->dispositionnotificationto)
                memusage += strlen(msginfo->dispositionnotificationto);
        if (msginfo->returnreceiptto)
@@ -1897,16 +2012,19 @@ gboolean procmsg_msginfo_filter(MsgInfo *msginfo)
        MailFilteringData mail_filtering_data;
                        
        mail_filtering_data.msginfo = msginfo;                  
-       if (hooks_invoke(MAIL_FILTERING_HOOKLIST, &mail_filtering_data))
+       if (hooks_invoke(MAIL_FILTERING_HOOKLIST, &mail_filtering_data)) {
+               hooks_invoke(MAIL_POSTFILTERING_HOOKLIST, msginfo);
                return TRUE;
+       }
 
        /* filter if enabled in prefs or move to inbox if not */
        if((filtering_rules != NULL) &&
-          filter_message_by_msginfo(filtering_rules, msginfo))
+          filter_message_by_msginfo(filtering_rules, msginfo)) {
+               hooks_invoke(MAIL_POSTFILTERING_HOOKLIST, msginfo);
                return TRUE;
+       }
                
        hooks_invoke(MAIL_POSTFILTERING_HOOKLIST, msginfo);
-
        return FALSE;
 }
 
@@ -1937,7 +2055,8 @@ MsgInfo *procmsg_msginfo_new_from_mimeinfo(MsgInfo *src_msginfo, MimeInfo *mimei
                fclose(fp);
 
        if (tmp_msginfo != NULL) {
-               tmp_msginfo->folder = src_msginfo->folder;
+               if (src_msginfo)
+                       tmp_msginfo->folder = src_msginfo->folder;
                tmp_msginfo->plaintext_file = g_strdup(tmpfile);
        } else {
                g_warning("procmsg_msginfo_new_from_mimeinfo(): Can't generate new msginfo");