2005-05-20 [colin] 1.9.11cvs2
[claws.git] / src / procmsg.c
index dd7ad649c18f39c035559c9d9dcd9c3bbe6346f3..9f5817d0d26c980228aa1dfe9df074c3d6213a31 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2004 Hiroyuki Yamamoto
+ * Copyright (C) 1999-2005 Hiroyuki Yamamoto
  *
  * 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
 #include "defs.h"
 
 #include <glib.h>
+#include <glib/gi18n.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <ctype.h>
 
-#include "intl.h"
 #include "main.h"
 #include "utils.h"
 #include "procmsg.h"
 #include "msgcache.h"
 #include "partial_download.h"
 
+static gint procmsg_send_message_queue_full(const gchar *file, gboolean keep_session);
+
+enum
+{
+       Q_SENDER           = 0,
+       Q_SMTPSERVER       = 1,
+       Q_RECIPIENTS       = 2,
+       Q_NEWSGROUPS       = 3,
+       Q_MAIL_ACCOUNT_ID  = 4,
+       Q_NEWS_ACCOUNT_ID  = 5,
+       Q_SAVE_COPY_FOLDER = 6,
+       Q_REPLY_MESSAGE_ID = 7,
+       Q_FWD_MESSAGE_ID   = 8,
+       Q_PRIVACY_SYSTEM   = 9,
+       Q_ENCRYPT          = 10,
+       Q_ENCRYPT_DATA     = 11,
+};
+
 GHashTable *procmsg_msg_hash_table_create(GSList *mlist)
 {
        GHashTable *msg_table;
@@ -223,6 +241,7 @@ GNode *procmsg_get_thread_tree(GSList *mlist)
        GRelation *subject_relation;
        MsgInfo *msginfo;
        const gchar *msgid;
+        GSList *reflist;
 
        root = g_node_new(NULL);
        msgid_table = g_hash_table_new(g_str_hash, g_str_equal);
@@ -253,20 +272,31 @@ GNode *procmsg_get_thread_tree(GSList *mlist)
 
        /* complete the unfinished threads */
        for (node = root->children; node != NULL; ) {
-               parent = NULL;
                next = node->next;
                msginfo = (MsgInfo *)node->data;
-               if (msginfo->inreplyto) { 
+               parent = NULL;
+               
+                if (msginfo->inreplyto)
                        parent = g_hash_table_lookup(msgid_table, msginfo->inreplyto);
-                       /* node should not be the parent, and node should not 
-                          be an ancestor of parent (circular reference) */
-                       if (parent && parent != node && 
-                           !g_node_is_ancestor(node, parent)) {
-                               g_node_unlink(node);
-                               g_node_insert_before
-                                       (parent, parent->children, node);
-                       }                               
+
+               /* try looking for the indirect parent */
+               if (!parent && msginfo->references) {
+                       for (reflist = msginfo->references;
+                            reflist != NULL; reflist = reflist->next)
+                               if ((parent = g_hash_table_lookup
+                                       (msgid_table, reflist->data)) != NULL)
+                                       break;
+                }                                        
+              
+               /* node should not be the parent, and node should not
+                  be an ancestor of parent (circular reference) */
+               if (parent && parent != node &&
+                   !g_node_is_ancestor(node, parent)) {
+                       g_node_unlink(node);
+                       g_node_insert_before
+                               (parent, parent->children, node);
                }
+               
                node = next;
        }
 
@@ -602,8 +632,6 @@ void procmsg_get_filter_keyword(MsgInfo *msginfo, gchar **header, gchar **key,
 
 void procmsg_empty_trash(FolderItem *trash)
 {
-       FILE *fp;
-
        if (trash && trash->total_msgs > 0) {
                GSList *mlist = folder_item_get_msg_list(trash);
                GSList *cur;
@@ -628,6 +656,134 @@ void procmsg_empty_all_trash(void)
        }
 }
 
+static PrefsAccount *procmsg_get_account_from_file(const gchar *file)
+{
+       PrefsAccount *mailac = NULL;
+       FILE *fp;
+       int hnum;
+       gchar buf[BUFFSIZE];
+       static HeaderEntry qentry[] = {{"S:",    NULL, FALSE},
+                                      {"SSV:",  NULL, FALSE},
+                                      {"R:",    NULL, FALSE},
+                                      {"NG:",   NULL, FALSE},
+                                      {"MAID:", NULL, FALSE},
+                                      {"NAID:", NULL, FALSE},
+                                      {"SCF:",  NULL, FALSE},
+                                      {"RMID:", NULL, FALSE},
+                                      {"FMID:", NULL, FALSE},
+                                      {"X-Sylpheed-Privacy-System:", NULL, FALSE},
+                                      {"X-Sylpheed-Encrypt:", NULL, FALSE},
+                                      {"X-Sylpheed-Encrypt-Data:", NULL, FALSE},
+                                      {NULL,    NULL, FALSE}};
+       
+       g_return_val_if_fail(file != NULL, NULL);
+
+       if ((fp = fopen(file, "rb")) == NULL) {
+               FILE_OP_ERROR(file, "fopen");
+               return NULL;
+       }
+
+       while ((hnum = procheader_get_one_field(buf, sizeof(buf), fp, qentry))
+              != -1) {
+               gchar *p = buf + strlen(qentry[hnum].name);
+
+               if (hnum == Q_MAIL_ACCOUNT_ID) {
+                       mailac = account_find_from_id(atoi(p));
+                       break;
+               }
+       }
+       fclose(fp);
+       return mailac;
+}
+
+static GSList *procmsg_list_sort_by_account(FolderItem *queue, GSList *list)
+{
+       GSList *result = NULL;
+       GSList *orig = NULL;
+       PrefsAccount *last_account = NULL;
+       MsgInfo *msg = NULL;
+       GSList *cur = NULL;
+       gboolean nothing_to_sort = TRUE;
+
+       if (!list)
+               return NULL;
+
+       orig = g_slist_copy(list);
+       
+       msg = (MsgInfo *)orig->data;
+       
+       for (cur = orig; cur; cur = cur->next)
+               debug_print("sort before %s\n", ((MsgInfo *)cur->data)->from);
+       
+       debug_print("\n");
+
+parse_again:   
+       nothing_to_sort = TRUE;
+       cur = orig;
+       while (cur) {
+               gchar *file;
+               PrefsAccount *ac = procmsg_get_account_from_file(file);
+               msg = (MsgInfo *)cur->data;
+               file = folder_item_fetch_msg(queue, msg->msgnum);
+               g_free(file);
+
+               if (last_account == NULL || (ac != NULL && ac == last_account)) {
+                       result = g_slist_append(result, msg);
+                       orig = g_slist_remove(orig, msg);
+                       last_account = ac;
+                       nothing_to_sort = FALSE;
+                       goto parse_again;
+               }
+               cur = cur->next;
+       }
+       
+       if (orig || g_slist_length(orig)) {
+               if (!last_account && nothing_to_sort) {
+                       /* can't find an account for the rest of the list */
+                       cur = orig;
+                       while (cur) {
+                               result = g_slist_append(result, cur->data);
+                               cur = cur->next;
+                       }
+               } else {
+                       last_account = NULL;
+                       goto parse_again;
+               }
+       }
+       
+       g_slist_free(orig);
+       
+       for (cur = result; cur; cur = cur->next)
+               debug_print("sort after  %s\n", ((MsgInfo *)cur->data)->from);
+
+       debug_print("\n");
+
+       return result;
+}
+
+static gboolean procmsg_is_last_for_account(FolderItem *queue, MsgInfo *msginfo, GSList *elem)
+{
+       gchar *file = folder_item_fetch_msg(queue, msginfo->msgnum);
+       PrefsAccount *ac = procmsg_get_account_from_file(file);
+       GSList *cur = elem;
+       g_free(file);
+       for (cur = elem; cur; cur = cur->next) {
+               MsgInfo *cur_msginfo = (MsgInfo *)cur->data;
+               PrefsAccount *cur_ac = NULL;
+               file = folder_item_fetch_msg(queue, cur_msginfo->msgnum);
+               
+               if (cur_msginfo != msginfo && !MSG_IS_LOCKED(cur_msginfo->flags)) {
+                       if (procmsg_get_account_from_file(file) == ac) {
+                               g_free(file);
+                               return FALSE;
+                       }
+               }
+               
+               g_free(file);
+       }
+       return TRUE;
+}
+
 /*!
  *\brief       Send messages in queue
  *
@@ -641,6 +797,7 @@ gint procmsg_send_queue(FolderItem *queue, gboolean save_msgs)
 {
        gint sent = 0, err = 0;
        GSList *list, *elem;
+       GSList *sorted_list = NULL;
 
        if (!queue)
                queue = folder_get_default_queue();
@@ -649,15 +806,19 @@ gint procmsg_send_queue(FolderItem *queue, gboolean save_msgs)
        folder_item_scan(queue);
        list = folder_item_get_msg_list(queue);
 
-       for (elem = list; elem != NULL; elem = elem->next) {
+       /* sort the list per sender account; this helps reusing the same SMTP server */
+       sorted_list = procmsg_list_sort_by_account(queue, list);
+       
+       for (elem = sorted_list; elem != NULL; elem = elem->next) {
                gchar *file;
                MsgInfo *msginfo;
-               
+                       
                msginfo = (MsgInfo *)(elem->data);
                if (!MSG_IS_LOCKED(msginfo->flags)) {
                        file = folder_item_fetch_msg(queue, msginfo->msgnum);
                        if (file) {
-                               if (procmsg_send_message_queue(file) < 0) {
+                               if (procmsg_send_message_queue_full(file, 
+                                               !procmsg_is_last_for_account(queue, msginfo, elem)) < 0) {
                                        g_warning("Sending queued message %d failed.\n", 
                                                  msginfo->msgnum);
                                        err++;
@@ -684,6 +845,8 @@ gint procmsg_send_queue(FolderItem *queue, gboolean save_msgs)
                procmsg_msginfo_free(msginfo);
        }
 
+       g_slist_free(sorted_list);
+
        return (err != 0 ? -err : sent);
 }
 
@@ -842,6 +1005,7 @@ MsgInfo *procmsg_msginfo_new(void)
 MsgInfo *procmsg_msginfo_copy(MsgInfo *msginfo)
 {
        MsgInfo *newmsginfo;
+        GSList *refs;
 
        if (msginfo == NULL) return NULL;
 
@@ -878,7 +1042,13 @@ MsgInfo *procmsg_msginfo_copy(MsgInfo *msginfo)
        MEMBDUP(xface);
        MEMBDUP(dispositionnotificationto);
        MEMBDUP(returnreceiptto);
-       MEMBDUP(references);
+
+        refs = msginfo->references;
+        for (refs = msginfo->references; refs != NULL; refs = refs->next) {
+                newmsginfo->references = g_slist_prepend
+                        (newmsginfo->references, g_strdup(refs->data)); 
+        }
+        newmsginfo->references = g_slist_reverse(newmsginfo->references);
 
        MEMBCOPY(score);
        MEMBCOPY(threadscore);
@@ -894,8 +1064,12 @@ MsgInfo *procmsg_msginfo_get_full_info(MsgInfo *msginfo)
 
        if (msginfo == NULL) return NULL;
 
-       file = procmsg_get_message_file(msginfo);
-       if (!file) {
+       file = procmsg_get_message_file_path(msginfo);
+       if (!file || !is_file_exist(file)) {
+               g_free(file);
+               file = procmsg_get_message_file(msginfo);
+       }
+       if (!file || !is_file_exist(file)) {
                g_warning("procmsg_msginfo_get_full_info(): can't get message file.\n");
                return NULL;
        }
@@ -944,7 +1118,6 @@ void procmsg_msginfo_free(MsgInfo *msginfo)
        }
 
        g_free(msginfo->fromspace);
-       g_free(msginfo->references);
        g_free(msginfo->returnreceiptto);
        g_free(msginfo->dispositionnotificationto);
        g_free(msginfo->xface);
@@ -965,6 +1138,9 @@ void procmsg_msginfo_free(MsgInfo *msginfo)
        g_free(msginfo->account_server);
        g_free(msginfo->account_login);
        
+       slist_free_strings(msginfo->references);
+       g_slist_free(msginfo->references);
+
        g_free(msginfo->plaintext_file);
 
        g_free(msginfo);
@@ -999,8 +1175,11 @@ guint procmsg_msginfo_memusage(MsgInfo *msginfo)
                memusage += strlen(msginfo->dispositionnotificationto);
        if (msginfo->returnreceiptto)
                memusage += strlen(msginfo->returnreceiptto);
+#warning FIXME: Calculate size of references list                
+#if 0
        if (msginfo->references)
                memusage += strlen(msginfo->references);
+#endif                
        if (msginfo->fromspace)
                memusage += strlen(msginfo->fromspace);
 
@@ -1020,23 +1199,7 @@ gint procmsg_cmp_msgnum_for_sort(gconstpointer a, gconstpointer b)
        return msginfo1->msgnum - msginfo2->msgnum;
 }
 
-enum
-{
-       Q_SENDER           = 0,
-       Q_SMTPSERVER       = 1,
-       Q_RECIPIENTS       = 2,
-       Q_NEWSGROUPS       = 3,
-       Q_MAIL_ACCOUNT_ID  = 4,
-       Q_NEWS_ACCOUNT_ID  = 5,
-       Q_SAVE_COPY_FOLDER = 6,
-       Q_REPLY_MESSAGE_ID = 7,
-       Q_FWD_MESSAGE_ID   = 8,
-       Q_PRIVACY_SYSTEM   = 9,
-       Q_ENCRYPT          = 10,
-       Q_ENCRYPT_DATA     = 11,
-};
-
-gint procmsg_send_message_queue(const gchar *file)
+static gint procmsg_send_message_queue_full(const gchar *file, gboolean keep_session)
 {
        static HeaderEntry qentry[] = {{"S:",    NULL, FALSE},
                                       {"SSV:",  NULL, FALSE},
@@ -1171,7 +1334,6 @@ gint procmsg_send_message_queue(const gchar *file)
                                content = file_read_stream_to_str(fp);
                                rewind(fp);
 
-                               get_tmpfile_in_dir(get_mime_tmp_dir(), &tmp_enc_file);
                                str_write_to_file(content, tmp_enc_file);
                                g_free(content);
                        } else {
@@ -1204,7 +1366,7 @@ gint procmsg_send_message_queue(const gchar *file)
                        }
 
                        if (mailac)
-                               mailval = send_message_smtp(mailac, to_list, fp);
+                               mailval = send_message_smtp_full(mailac, to_list, fp, keep_session);
                        else {
                                PrefsAccount tmp_ac;
 
@@ -1279,11 +1441,15 @@ gint procmsg_send_message_queue(const gchar *file)
                        procmsg_save_to_outbox(outbox, file, TRUE);
                } else {
                        procmsg_save_to_outbox(outbox, tmp_enc_file, FALSE);
-                       unlink(tmp_enc_file);
-                       free(tmp_enc_file);
                }
        }
 
+       if (tmp_enc_file != NULL) {
+               unlink(tmp_enc_file);
+               free(tmp_enc_file);
+               tmp_enc_file = NULL;
+       }
+
        if (replymessageid != NULL || fwdmessageid != NULL) {
                gchar **tokens;
                FolderItem *item;
@@ -1341,6 +1507,11 @@ gint procmsg_send_message_queue(const gchar *file)
        return (newsval != 0 ? newsval : mailval);
 }
 
+gint procmsg_send_message_queue(const gchar *file)
+{
+       return procmsg_send_message_queue_full(file, FALSE);
+}
+
 static void update_folder_msg_counts(FolderItem *item, MsgInfo *msginfo, MsgPermFlags old_flags)
 {
        MsgPermFlags new_flags = msginfo->flags.perm_flags;
@@ -1642,3 +1813,37 @@ gboolean procmsg_msginfo_filter(MsgInfo *msginfo)
 
        return FALSE;
 }
+
+MsgInfo *procmsg_msginfo_new_from_mimeinfo(MsgInfo *src_msginfo, MimeInfo *mimeinfo)
+{
+       MsgInfo *tmp_msginfo = NULL;
+       MsgFlags flags = {0, 0};
+       
+       
+       if (!mimeinfo || mimeinfo->type != MIMETYPE_MESSAGE ||
+           g_ascii_strcasecmp(mimeinfo->subtype, "rfc822")) {
+               g_warning("procmsg_msginfo_new_from_mimeinfo(): unsuitable mimeinfo");
+               return NULL;
+       }
+                   
+       if (mimeinfo->content == MIMECONTENT_MEM) {
+               gchar *tmpfile = get_tmp_file();
+               str_write_to_file(mimeinfo->data.mem, tmpfile);
+               g_free(mimeinfo->data.mem);
+               mimeinfo->content == MIMECONTENT_FILE;
+               mimeinfo->data.filename = g_strdup(tmpfile);
+               g_free(tmpfile);
+       }
+
+       tmp_msginfo = procheader_parse_file(mimeinfo->data.filename,
+                               flags, TRUE, FALSE);
+
+       if (tmp_msginfo != NULL) {
+               tmp_msginfo->folder = src_msginfo->folder;
+               tmp_msginfo->plaintext_file = g_strdup(mimeinfo->data.filename);
+       } else {
+               g_warning("procmsg_msginfo_new_from_mimeinfo(): Can't generate new msginfo");
+       }
+       
+       return tmp_msginfo;
+}