fix crash when using Forward in Filtering when external editor option is set
[claws.git] / src / compose.c
index 729469a2b9d754f2ea14bd72bb50fcf2c34389ac..e0d81adabee32e3247a68ab110a994a453b6f3e2 100644 (file)
@@ -62,6 +62,7 @@
 #include <sys/wait.h>
 #include <signal.h>
 #include <errno.h>
+#include <libgen.h>
 
 #if (HAVE_WCTYPE_H && HAVE_WCHAR_H)
 #  include <wchar.h>
@@ -149,6 +150,14 @@ typedef enum
        PRIORITY_LOWEST
 } PriorityLevel;
 
+typedef enum
+{
+       COMPOSE_INSERT_SUCCESS,
+       COMPOSE_INSERT_READ_ERROR,
+       COMPOSE_INSERT_INVALID_CHARACTER,
+       COMPOSE_INSERT_NO_FILE
+} ComposeInsertResult;
+
 #define B64_LINE_SIZE          57
 #define B64_BUFFSIZE           77
 
@@ -199,7 +208,7 @@ static void compose_reedit_set_entry                (Compose        *compose,
 static void compose_insert_sig                 (Compose        *compose,
                                                 gboolean        replace);
 static gchar *compose_get_signature_str                (Compose        *compose);
-static void compose_insert_file                        (Compose        *compose,
+static ComposeInsertResult compose_insert_file (Compose        *compose,
                                                 const gchar    *file);
 static void compose_attach_append              (Compose        *compose,
                                                 const gchar    *file,
@@ -415,6 +424,8 @@ static void activate_gnupg_mode     (Compose *compose,
 #endif
 static void compose_toggle_return_receipt_cb(gpointer data, guint action,
                                             GtkWidget *widget);
+static void compose_toggle_remove_refs_cb(gpointer data, guint action,
+                                            GtkWidget *widget);
 static void compose_set_priority_cb    (gpointer        data,
                                         guint           action,
                                         GtkWidget      *widget);
@@ -482,6 +493,7 @@ static void compose_check_forwards_go          (Compose *compose);
 #endif
 
 static gboolean compose_send_control_enter     (Compose        *compose);
+static gint compose_defer_auto_save_draft      (Compose        *compose);
 
 static GtkItemFactoryEntry compose_popup_entries[] =
 {
@@ -657,6 +669,7 @@ static GtkItemFactoryEntry compose_entries[] =
        {N_("/_Message/Priority/_Lowest"),  NULL, compose_set_priority_cb, PRIORITY_LOWEST, "/Message/Priority/Highest"},
        {N_("/_Message/---"),           NULL,           NULL,   0, "<Separator>"},
        {N_("/_Message/_Request Return Receipt"),       NULL, compose_toggle_return_receipt_cb, 0, "<ToggleItem>"},
+       {N_("/_Message/Remo_ve references"),    NULL, compose_toggle_remove_refs_cb, 0, "<ToggleItem>"},
        {N_("/_Tools"),                 NULL, NULL, 0, "<Branch>"},
        {N_("/_Tools/Show _ruler"),     NULL, compose_toggle_ruler_cb, 0, "<ToggleItem>"},
        {N_("/_Tools/_Address book"),   "<shift><control>A", compose_address_cb , 0, NULL},
@@ -705,6 +718,13 @@ Compose *compose_generic_new(PrefsAccount *account, const gchar *mailto, FolderI
        text = GTK_STEXT(compose->text);
        gtk_stext_freeze(text);
 
+#ifdef USE_ASPELL
+       if (item && item->prefs && item->prefs->enable_default_dictionary &&
+           compose->gtkaspell) 
+               gtkaspell_change_dict(compose->gtkaspell, 
+                   item->prefs->default_dictionary);
+#endif
+
        if (account->auto_sig)
                compose_insert_sig(compose, FALSE);
        gtk_editable_set_position(GTK_EDITABLE(text), 0);
@@ -862,7 +882,7 @@ void compose_reply_mode(ComposeMode mode, GSList *msginfo_list, gchar *body)
        case COMPOSE_FORWARD_INLINE:
                /* check if we reply to more than one Message */
                if (list_len == 1) {
-                       compose_forward(NULL, msginfo, FALSE, body);
+                       compose_forward(NULL, msginfo, FALSE, body, FALSE);
                        break;
                } 
                /* more messages FALL THROUGH */
@@ -900,6 +920,7 @@ static void compose_generic_reply(MsgInfo *msginfo, gboolean quote,
                                  gboolean followup_and_reply_to,
                                  const gchar *body)
 {
+       GtkItemFactory *ifactory;
        Compose *compose;
        PrefsAccount *account = NULL;
        PrefsAccount *reply_account;
@@ -924,17 +945,17 @@ static void compose_generic_reply(MsgInfo *msginfo, gboolean quote,
                reply_account = account;
 
        compose = compose_create(account, COMPOSE_REPLY);
+       ifactory = gtk_item_factory_from_widget(compose->menubar);
+
+       menu_set_toggle(ifactory, "/Message/Remove references", FALSE);
+       menu_set_sensitive(ifactory, "/Message/Remove references", TRUE);
 
        compose->replyinfo = procmsg_msginfo_get_full_info(msginfo);
        if (!compose->replyinfo)
                compose->replyinfo = procmsg_msginfo_copy(msginfo);
 
-       if (msginfo->folder && msginfo->folder->ret_rcpt) {
-               GtkItemFactory *ifactory;
-       
-               ifactory = gtk_item_factory_from_widget(compose->menubar);
+       if (msginfo->folder && msginfo->folder->ret_rcpt)
                menu_set_toggle(ifactory, "/Message/Request Return Receipt", TRUE);
-       }
 
        /* Set save folder */
        if (msginfo->folder && msginfo->folder->prefs && msginfo->folder->prefs->save_copy_to_folder) {
@@ -954,18 +975,26 @@ static void compose_generic_reply(MsgInfo *msginfo, gboolean quote,
        text = GTK_STEXT(compose->text);
        gtk_stext_freeze(text);
 
+#ifdef USE_ASPELL
+       if (msginfo->folder && msginfo->folder->prefs && 
+           msginfo->folder->prefs && 
+           msginfo->folder->prefs->enable_default_dictionary &&
+           compose->gtkaspell)
+               gtkaspell_change_dict(compose->gtkaspell, 
+                   msginfo->folder->prefs->default_dictionary);
+#endif
+
        if (quote) {
                gchar *qmark;
-               gchar *quote_str;
 
                if (prefs_common.quotemark && *prefs_common.quotemark)
                        qmark = prefs_common.quotemark;
                else
                        qmark = "> ";
 
-               quote_str = compose_quote_fmt(compose, compose->replyinfo,
-                                             prefs_common.quotefmt,
-                                             qmark, body);
+               compose_quote_fmt(compose, compose->replyinfo,
+                                 prefs_common.quotefmt,
+                                 qmark, body);
        }
 
        if (account->auto_sig)
@@ -992,7 +1021,8 @@ if (msginfo->var && *msginfo->var) { \
 }
 
 Compose *compose_forward(PrefsAccount *account, MsgInfo *msginfo,
-                        gboolean as_attach, const gchar *body)
+                        gboolean as_attach, const gchar *body,
+                        gboolean no_extedit)
 {
        Compose *compose;
        GtkSText *text;
@@ -1013,7 +1043,7 @@ Compose *compose_forward(PrefsAccount *account, MsgInfo *msginfo,
 
        if (!account && prefs_common.forward_account_autosel) {
                gchar cc[BUFFSIZE];
-               if (!get_header_from_msginfo(msginfo,cc,sizeof(cc),"CC:")){ /* Found a CC header */
+               if (!procheader_get_header_from_msginfo(msginfo,cc,sizeof(cc),"CC:")){ /* Found a CC header */
                        extract_address(cc);
                        account = account_find_from_address(cc);
                 }
@@ -1035,48 +1065,54 @@ Compose *compose_forward(PrefsAccount *account, MsgInfo *msginfo,
                compose->fwdinfo = procmsg_msginfo_copy(msginfo);
 
        if (msginfo->subject && *msginfo->subject) {
-               gtk_entry_set_text(GTK_ENTRY(compose->subject_entry), "Fw: ");
-               gtk_entry_append_text(GTK_ENTRY(compose->subject_entry),
-                                     msginfo->subject);
+               gchar *buf, *buf2, *p;
+
+               buf = p = g_strdup(msginfo->subject);
+               p += subject_get_prefix_length(p);
+               memmove(buf, p, strlen(p) + 1);
+
+               buf2 = g_strdup_printf("Fw: %s", buf);
+               gtk_entry_set_text(GTK_ENTRY(compose->subject_entry), buf2);
+               
+               g_free(buf);
+               g_free(buf2);
        }
 
        text = GTK_STEXT(compose->text);
        gtk_stext_freeze(text);
 
+       if (as_attach) {
+               gchar *msgfile;
 
-               if (as_attach) {
-                       gchar *msgfile;
-
-                       msgfile = procmsg_get_message_file_path(msginfo);
-                       if (!is_file_exist(msgfile))
-                               g_warning("%s: file not exist\n", msgfile);
-                       else
-                               compose_attach_append(compose, msgfile, msgfile,
-                                                     "message/rfc822");
+               msgfile = procmsg_get_message_file_path(msginfo);
+               if (!is_file_exist(msgfile))
+                       g_warning("%s: file not exist\n", msgfile);
+               else
+                       compose_attach_append(compose, msgfile, msgfile,
+                                             "message/rfc822");
 
-                       g_free(msgfile);
-               } else {
-                       gchar *qmark;
-                       gchar *quote_str;
-                       MsgInfo *full_msginfo;
+               g_free(msgfile);
+       } else {
+               gchar *qmark;
+               MsgInfo *full_msginfo;
 
-                       full_msginfo = procmsg_msginfo_get_full_info(msginfo);
-                       if (!full_msginfo)
-                               full_msginfo = procmsg_msginfo_copy(msginfo);
+               full_msginfo = procmsg_msginfo_get_full_info(msginfo);
+               if (!full_msginfo)
+                       full_msginfo = procmsg_msginfo_copy(msginfo);
 
-                       if (prefs_common.fw_quotemark &&
-                           *prefs_common.fw_quotemark)
-                               qmark = prefs_common.fw_quotemark;
-                       else
-                               qmark = "> ";
+               if (prefs_common.fw_quotemark &&
+                   *prefs_common.fw_quotemark)
+                       qmark = prefs_common.fw_quotemark;
+               else
+                       qmark = "> ";
 
-                       quote_str = compose_quote_fmt(compose, full_msginfo,
-                                                     prefs_common.fw_quotefmt,
-                                                     qmark, body);
-                       compose_attach_parts(compose, msginfo);
+               compose_quote_fmt(compose, full_msginfo,
+                                 prefs_common.fw_quotefmt,
+                                 qmark, body);
+               compose_attach_parts(compose, msginfo);
 
-                       procmsg_msginfo_free(full_msginfo);
-               }
+               procmsg_msginfo_free(full_msginfo);
+       }
 
        if (account->auto_sig)
                compose_insert_sig(compose, FALSE);
@@ -1096,7 +1132,7 @@ Compose *compose_forward(PrefsAccount *account, MsgInfo *msginfo,
 #endif
        gtk_widget_grab_focus(compose->header_last->entry);
 
-       if (prefs_common.auto_exteditor)
+       if (!no_extedit && prefs_common.auto_exteditor)
                compose_exec_ext_editor(compose);
        
        /*save folder*/
@@ -1195,22 +1231,22 @@ void compose_reedit(MsgInfo *msginfo)
                gint id;
 
                /* Select Account from queue headers */
-               if (!get_header_from_msginfo(msginfo, queueheader_buf, 
+               if (!procheader_get_header_from_msginfo(msginfo, queueheader_buf, 
                                             sizeof(queueheader_buf), "X-Sylpheed-Account-Id:")) {
                        id = atoi(&queueheader_buf[22]);
                        account = account_find_from_id(id);
                }
-               if (!account && !get_header_from_msginfo(msginfo, queueheader_buf, 
+               if (!account && !procheader_get_header_from_msginfo(msginfo, queueheader_buf, 
                                             sizeof(queueheader_buf), "NAID:")) {
                        id = atoi(&queueheader_buf[5]);
                        account = account_find_from_id(id);
                }
-               if (!account && !get_header_from_msginfo(msginfo, queueheader_buf, 
+               if (!account && !procheader_get_header_from_msginfo(msginfo, queueheader_buf, 
                                                    sizeof(queueheader_buf), "MAID:")) {
                        id = atoi(&queueheader_buf[5]);
                        account = account_find_from_id(id);
                }
-               if (!account && !get_header_from_msginfo(msginfo, queueheader_buf, 
+               if (!account && !procheader_get_header_from_msginfo(msginfo, queueheader_buf, 
                                                                sizeof(queueheader_buf), "S:")) {
                        account = account_find_from_address(queueheader_buf);
                }
@@ -1219,7 +1255,7 @@ void compose_reedit(MsgInfo *msginfo)
 
        if (!account && prefs_common.reedit_account_autosel) {
                        gchar from[BUFFSIZE];
-               if (!get_header_from_msginfo(msginfo, from, sizeof(from), "FROM:")){
+               if (!procheader_get_header_from_msginfo(msginfo, from, sizeof(from), "FROM:")){
                        extract_address(from);
                        account = account_find_from_address(from);
                 }
@@ -1235,7 +1271,7 @@ void compose_reedit(MsgInfo *msginfo)
                gchar queueheader_buf[BUFFSIZE];
 
                /* Set message save folder */
-               if (!get_header_from_msginfo(msginfo, queueheader_buf, sizeof(queueheader_buf), "SCF:")) {
+               if (!procheader_get_header_from_msginfo(msginfo, queueheader_buf, sizeof(queueheader_buf), "SCF:")) {
                        gint startpos = 0;
 
                        gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(compose->savemsg_checkbtn), TRUE);
@@ -1273,11 +1309,13 @@ Compose *compose_redirect(PrefsAccount *account, MsgInfo *msginfo)
        Compose *compose;
        gchar *filename;
        GtkItemFactory *ifactory;
+       FolderItem *item;
 
        g_return_val_if_fail(msginfo != NULL, NULL);
 
        if (!account)
-               account = cur_account;
+               account = account_get_reply_account(msginfo,
+                                       prefs_common.reply_account_autosel);
        g_return_val_if_fail(account != NULL, NULL);
 
        compose = compose_create(account, COMPOSE_REDIRECT);
@@ -1296,6 +1334,17 @@ Compose *compose_redirect(PrefsAccount *account, MsgInfo *msginfo)
 
        compose->redirect_filename = filename;
        
+       /* Set save folder */
+       item = msginfo->folder;
+       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);
+               gtk_entry_set_text(GTK_ENTRY(compose->savemsg_entry), folderidentifier);
+               g_free(folderidentifier);
+       }
+
        compose_attach_parts(compose, msginfo);
 
        if (msginfo->subject)
@@ -1673,7 +1722,7 @@ static gchar *compose_quote_fmt(Compose *compose, MsgInfo *msginfo,
        gchar *buf;
        gchar *p, *lastp;
        gint len;
-       gchar *trimmed_body = body;
+       const gchar *trimmed_body = body;
        
        if (!msginfo)
                msginfo = &dummyinfo;
@@ -1790,11 +1839,12 @@ static void compose_reply_set_entry(Compose *compose, MsgInfo *msginfo,
                gchar *buf, *buf2, *p;
 
                buf = p = g_strdup(msginfo->subject);
-               p += subject_get_reply_prefix_length(p);
+               p += subject_get_prefix_length(p);
                memmove(buf, p, strlen(p) + 1);
 
                buf2 = g_strdup_printf("Re: %s", buf);
                gtk_entry_set_text(GTK_ENTRY(compose->subject_entry), buf2);
+
                g_free(buf2);
                g_free(buf);
        } else
@@ -1814,6 +1864,10 @@ static void compose_reply_set_entry(Compose *compose, MsgInfo *msginfo,
 
        if (replyto && from)
                cc_list = address_list_append_with_comments(cc_list, from);
+       if (to_all && msginfo->folder && 
+           msginfo->folder->prefs->enable_default_reply_to)
+               cc_list = address_list_append_with_comments(cc_list,
+                               msginfo->folder->prefs->default_reply_to);
        cc_list = address_list_append_with_comments(cc_list, msginfo->to);
        cc_list = address_list_append_with_comments(cc_list, compose->cc);
 
@@ -1849,6 +1903,7 @@ static void compose_reply_set_entry(Compose *compose, MsgInfo *msginfo,
                slist_free_strings(cc_list);
                g_slist_free(cc_list);
        }
+
 }
 
 #define SET_ENTRY(entry, str) \
@@ -1890,6 +1945,7 @@ static void compose_insert_sig(Compose *compose, gboolean replace)
 {
        GtkSText *text = GTK_STEXT(compose->text);
        gint cur_pos;
+       gint len;
 
        g_return_if_fail(compose->account != NULL);
 
@@ -1897,9 +1953,13 @@ static void compose_insert_sig(Compose *compose, gboolean replace)
 
        gtk_stext_freeze(text);
 
+       if (replace) {
+               len = gtk_stext_get_length(text);
+               gtk_stext_set_point(text, len);
+       }
+
        if (replace && compose->sig_str) {
                gint pos;
-               gint len;
 
                if (compose->sig_str[0] == '\0')
                        pos = -1;
@@ -1911,16 +1971,9 @@ static void compose_insert_sig(Compose *compose, gboolean replace)
                        if (len >= 0) {
                                gtk_stext_set_point(text, pos);
                                gtk_stext_forward_delete(text, len);
-                       } else {
-                               len = gtk_stext_get_length(text);
-                               gtk_stext_set_point(text, len);
                        }
-               } else {
-                       len = gtk_stext_get_length(text);
-                       gtk_stext_set_point(text, len);
                }
-       } else
-               gtk_stext_insert(text, NULL, NULL, NULL, "\n\n", 2);
+       }
 
        g_free(compose->sig_str);
        compose->sig_str = compose_get_signature_str(compose);
@@ -1968,28 +2021,30 @@ static gchar *compose_get_signature_str(Compose *compose)
                g_free(tmp);
        }
 
-       if (compose->account->sig_sep) {
-               sig_str = g_strconcat(compose->account->sig_sep, "\n", sig_body,
+       if (compose->account->sig_sep)
+               sig_str = g_strconcat("\n\n", compose->account->sig_sep, "\n", sig_body,
                                      NULL);
-               g_free(sig_body);
-       } else
-               sig_str = sig_body;
+       else
+               sig_str = g_strconcat("\n\n", sig_body, NULL);
 
+       g_free(sig_body);
+       
        return sig_str;
 }
 
-static void compose_insert_file(Compose *compose, const gchar *file)
+static ComposeInsertResult compose_insert_file(Compose *compose, const gchar *file)
 {
        GtkSText *text = GTK_STEXT(compose->text);
        gchar buf[BUFFSIZE];
        gint len;
        FILE *fp;
+       gboolean badtxt = FALSE;
 
-       g_return_if_fail(file != NULL);
+       g_return_val_if_fail(file != NULL, COMPOSE_INSERT_NO_FILE);
 
        if ((fp = fopen(file, "rb")) == NULL) {
                FILE_OP_ERROR(file, "fopen");
-               return;
+               return COMPOSE_INSERT_READ_ERROR;
        }
 
        gtk_stext_freeze(text);
@@ -2003,12 +2058,19 @@ static void compose_insert_file(Compose *compose, const gchar *file)
                        while (--len >= 0)
                                if (buf[len] == '\r') buf[len] = '\n';
                }
+               if (mbstowcs(NULL, buf, 0) == -1)
+                       badtxt = TRUE;
                gtk_stext_insert(text, NULL, NULL, NULL, buf, -1);
        }
 
        gtk_stext_thaw(text);
 
        fclose(fp);
+
+       if (badtxt)
+               return COMPOSE_INSERT_INVALID_CHARACTER;
+       else 
+               return COMPOSE_INSERT_SUCCESS;
 }
 
 static void compose_attach_append(Compose *compose, const gchar *file,
@@ -2057,13 +2119,24 @@ static void compose_attach_append(Compose *compose, const gchar *file,
        if (content_type) {
                ainfo->content_type = g_strdup(content_type);
                if (!strcasecmp(content_type, "message/rfc822")) {
+                       MsgInfo *msginfo;
+                       MsgFlags flags = {0, 0};
+                       gchar *name;
+
                        if (procmime_get_encoding_for_file(file) == ENC_7BIT)
                                ainfo->encoding = ENC_7BIT;
                        else
                                ainfo->encoding = ENC_8BIT;
-                       ainfo->name = g_strdup_printf
-                               (_("Message: %s"),
-                                g_basename(filename ? filename : file));
+
+                       msginfo = procheader_parse_file(file, flags, FALSE, FALSE);
+                       if (msginfo && msginfo->subject)
+                               name = msginfo->subject;
+                       else
+                               name = g_basename(filename ? filename : file);
+
+                       ainfo->name = g_strdup_printf(_("Message: %s"), name);
+
+                       procmsg_msginfo_free(msginfo);
                } else {
                        if (!g_strncasecmp(content_type, "text", 4))
                                ainfo->encoding =
@@ -2096,14 +2169,11 @@ static void compose_attach_append(Compose *compose, const gchar *file,
 }
 
 #define IS_FIRST_PART_TEXT(info) \
-       ((info->mime_type == MIME_TEXT || info->mime_type == MIME_TEXT_HTML || \
-         info->mime_type == MIME_TEXT_ENRICHED) || \
-        (info->mime_type == MIME_MULTIPART && info->content_type && \
-         !strcasecmp(info->content_type, "multipart/alternative") && \
-         (info->children && \
-          (info->children->mime_type == MIME_TEXT || \
-           info->children->mime_type == MIME_TEXT_HTML || \
-           info->children->mime_type == MIME_TEXT_ENRICHED))))
+       ((info->type == MIMETYPE_TEXT) || \
+        (info->type == MIMETYPE_MULTIPART && info->subtype && \
+         !strcasecmp(info->subtype, "alternative") && \
+         (info->node->children && \
+          (((MimeInfo *) info->node->children->data)->type == MIMETYPE_TEXT))))
 
 static void compose_attach_parts(Compose *compose, MsgInfo *msginfo)
 {
@@ -2116,39 +2186,46 @@ static void compose_attach_parts(Compose *compose, MsgInfo *msginfo)
        if (!mimeinfo) return;
 
        /* skip first text (presumably message body) */
-       child = mimeinfo->children;
+       child = (MimeInfo *) mimeinfo->node->children->data;
        if (!child || IS_FIRST_PART_TEXT(mimeinfo)) {
                procmime_mimeinfo_free_all(mimeinfo);
                return;
        }
 
        if (IS_FIRST_PART_TEXT(child))
-               child = child->next;
+               child = (MimeInfo *) child->node->next;
 
        infile = procmsg_get_message_file_path(msginfo);
 
        while (child != NULL) {
-               if (child->children || child->mime_type == MIME_MULTIPART) {
+               if (child->node->children || child->type == MIMETYPE_MULTIPART) {
                        child = procmime_mimeinfo_next(child);
                        continue;
                }
-               if(child->parent && child->parent->parent
-               && !strcasecmp(child->parent->parent->content_type, "multipart/signed")
-               && child->mime_type == MIME_TEXT) {
+               if (child->node->parent && child->node->parent->parent
+               && (((MimeInfo *) child->node->parent->parent->data)->type == MIMETYPE_MULTIPART)
+               && !strcasecmp(((MimeInfo *) child->node->parent->parent->data)->subtype, "signed")
+               && child->type == MIMETYPE_TEXT) {
                        /* this is the main text part of a signed message */
                        child = procmime_mimeinfo_next(child);
                        continue;
                }
                outfile = procmime_get_tmp_file_name(child);
-               if (procmime_get_part(outfile, infile, child) < 0)
+               if (procmime_get_part(outfile, child) < 0)
                        g_warning("Can't get the part of multipart message.");
-               else if (compose->mode != COMPOSE_REEDIT || strcmp(child->content_type, "application/pgp-signature"))
+               else if (compose->mode != COMPOSE_REEDIT || 
+                   !((child->type == MIMETYPE_APPLICATION) && !strcmp(child->subtype, "pgp-signature"))) {
+                       gchar *content_type;
+
+                       content_type = g_strdup_printf("%s/%s", procmime_get_type_str(child->type), child->subtype);
                        compose_attach_append
                                (compose, outfile,
                                 child->filename ? child->filename : child->name,
-                                child->content_type);
+                                content_type);
+                       g_free(content_type);
+               }
 
-               child = child->next;
+               child = child->node->next != NULL ? (MimeInfo *) child->node->next->data : NULL;
        }
 
        g_free(infile);
@@ -2376,6 +2453,10 @@ static guint get_indent_length(GtkSText *text, guint start_pos, guint text_len)
        gboolean is_space;
        gboolean is_indent;
 
+       if (prefs_common.quote_chars == NULL) {
+               return 0 ;
+       }
+
        for (i = start_pos; i < text_len; i++) {
                GET_CHAR(i, cbuf, ch_len);
                if (ch_len > 1)
@@ -2876,7 +2957,7 @@ static void compose_select_account(Compose *compose, PrefsAccount *account,
        activate_gnupg_mode(compose, account);          
 #endif /* USE_GPGME */
 
-       if (!init)
+       if (!init && compose->mode != COMPOSE_REDIRECT)
                compose_insert_sig(compose, TRUE);
 }
 
@@ -2972,6 +3053,8 @@ gint compose_send(Compose *compose)
        g_free(msgpath);
 
        folder_item_remove_msg(folder, msgnum);
+       
+       folder_item_scan(folder);
 
        return val;
 }
@@ -3091,8 +3174,9 @@ gint compose_send(Compose *compose)
 }
 #endif
 
-static gboolean compose_use_attach(Compose *compose) {
-    return(gtk_clist_get_row_data(GTK_CLIST(compose->attach_clist), 0) != NULL);
+static gboolean compose_use_attach(Compose *compose) 
+{
+       return gtk_clist_get_row_data(GTK_CLIST(compose->attach_clist), 0) != NULL;
 }
 
 static gint compose_redirect_write_headers_from_headerlist(Compose *compose, 
@@ -3218,7 +3302,7 @@ static gint compose_redirect_write_to_file(Compose *compose, const gchar *file)
                g_warning("can't change file mode\n");
        }
 
-       while (procheader_get_unfolded_line(buf, sizeof(buf), fp)) {
+       while (procheader_get_one_field(buf, sizeof(buf), fp, NULL) != -1) {
                /* should filter returnpath, delivered-to */
                if (g_strncasecmp(buf, "Return-Path:",
                                   strlen("Return-Path:")) == 0 ||
@@ -3476,6 +3560,10 @@ static gint compose_write_to_file(Compose *compose, const gchar *file,
                fprintf(fp, "\n--%s\n", compose->boundary);
                fprintf(fp, "Content-Type: text/plain; charset=%s\n",
                        out_codeset);
+#if USE_GPGME
+               if (compose->use_signing && !compose->gnupg_mode)
+                       fprintf(fp, "Content-Disposition: inline\n");
+#endif
                fprintf(fp, "Content-Transfer-Encoding: %s\n",
                        procmime_get_encoding_str(encoding));
                fputc('\n', fp);
@@ -3838,7 +3926,7 @@ static gint compose_queue_sub(Compose *compose, gint *msgnum, FolderItem **item,
                return -1;
        }
        folder_item_scan(queue);
-       if ((num = folder_item_add_msg(queue, tmp, TRUE)) < 0) {
+       if ((num = folder_item_add_msg(queue, tmp, NULL, TRUE)) < 0) {
                g_warning("can't queue the message\n");
                unlink(tmp);
                g_free(tmp);
@@ -3969,14 +4057,15 @@ static void compose_write_attach(Compose *compose, FILE *fp)
 
 static gint compose_write_headers_from_headerlist(Compose *compose, 
                                                  FILE *fp, 
-                                                 gchar *header)
+                                                 const gchar *header,
+                                                 const gchar *seperator)
 {
-       gchar buf[BUFFSIZE];
        gchar *str, *header_w_colon, *trans_hdr;
-       gboolean first_address;
+       gboolean write_header = FALSE;
        GSList *list;
        ComposeHeaderEntry *headerentry;
        gchar * headerentryname;
+       GString *headerstr;
 
        if (IS_IN_CUSTOM_HEADER(header)) {
                return 0;
@@ -3984,10 +4073,11 @@ static gint compose_write_headers_from_headerlist(Compose *compose,
 
        debug_print("Writing %s-header\n", header);
 
+       headerstr = g_string_sized_new(64);
+
        header_w_colon = g_strconcat(header, ":", NULL);
        trans_hdr = (prefs_common.trans_hdr ? gettext(header_w_colon) : header_w_colon);
 
-       first_address = TRUE;
        for (list = compose->header_list; list; list = list->next) {
                headerentry = ((ComposeHeaderEntry *)list->data);
                headerentryname = gtk_entry_get_text(GTK_ENTRY(GTK_COMBO(headerentry->combo)->entry));
@@ -3997,24 +4087,26 @@ static gint compose_write_headers_from_headerlist(Compose *compose,
                        Xstrdup_a(str, str, return -1);
                        g_strstrip(str);
                        if (str[0] != '\0') {
-                               compose_convert_header
-                                       (buf, sizeof(buf), str,
-                                       strlen(header) + 2, TRUE);
-                               if (first_address) {
-                                       fprintf(fp, "%s: ", header);
-                                       first_address = FALSE;
-                               } else {
-                                       fprintf(fp, ",");
-                               }
-                               fprintf(fp, "%s", buf);
+                               if (write_header)
+                                       g_string_append(headerstr, seperator);
+                               g_string_append(headerstr, str);
+                               write_header = TRUE;
                        }
                }
        }
-       if (!first_address) {
-               fprintf(fp, "\n");
+       if (write_header) {
+               gchar *buf;
+
+               buf = g_new0(gchar, headerstr->len * 4 + 256);
+               compose_convert_header
+                       (buf, headerstr->len * 4  + 256, headerstr->str,
+                       strlen(header) + 2, TRUE);
+               fprintf(fp, "%s: %s\n", header, buf);
+               g_free(buf);
        }
 
        g_free(header_w_colon);
+       g_string_free(headerstr, TRUE);
 
        return(0);
 }
@@ -4070,7 +4162,7 @@ static gint compose_write_headers(Compose *compose, FILE *fp,
        }
        
        /* To */
-       compose_write_headers_from_headerlist(compose, fp, "To");
+       compose_write_headers_from_headerlist(compose, fp, "To", ", ");
 #if 0 /* NEW COMPOSE GUI */
        if (compose->use_to) {
                str = gtk_entry_get_text(GTK_ENTRY(compose->to_entry));
@@ -4079,7 +4171,7 @@ static gint compose_write_headers(Compose *compose, FILE *fp,
 #endif
 
        /* Newsgroups */
-       compose_write_headers_from_headerlist(compose, fp, "Newsgroups");
+       compose_write_headers_from_headerlist(compose, fp, "Newsgroups", ",");
 #if 0 /* NEW COMPOSE GUI */
        if (compose->use_newsgroups) {
                str = gtk_entry_get_text(GTK_ENTRY(compose->newsgroups_entry));
@@ -4100,7 +4192,7 @@ static gint compose_write_headers(Compose *compose, FILE *fp,
        }
 #endif
        /* Cc */
-       compose_write_headers_from_headerlist(compose, fp, "Cc");
+       compose_write_headers_from_headerlist(compose, fp, "Cc", ", ");
 #if 0 /* NEW COMPOSE GUI */
        if (compose->use_cc) {
                str = gtk_entry_get_text(GTK_ENTRY(compose->cc_entry));
@@ -4108,7 +4200,7 @@ static gint compose_write_headers(Compose *compose, FILE *fp,
        }
 #endif
        /* Bcc */
-       compose_write_headers_from_headerlist(compose, fp, "Bcc");
+       compose_write_headers_from_headerlist(compose, fp, "Bcc", ", ");
 #if 0 /* NEW COMPOSE GUI */
        if (compose->use_bcc) {
                str = gtk_entry_get_text(GTK_ENTRY(compose->bcc_entry));
@@ -4135,16 +4227,18 @@ static gint compose_write_headers(Compose *compose, FILE *fp,
                compose->msgid = g_strdup(buf);
        }
 
-       /* In-Reply-To */
-       if (compose->inreplyto && compose->to_list)
-               fprintf(fp, "In-Reply-To: <%s>\n", compose->inreplyto);
-
-       /* References */
-       if (compose->references)
-               fprintf(fp, "References: %s\n", compose->references);
+       if (compose->remove_references == FALSE) {
+               /* In-Reply-To */
+               if (compose->inreplyto && compose->to_list)
+                       fprintf(fp, "In-Reply-To: <%s>\n", compose->inreplyto);
+       
+               /* References */
+               if (compose->references)
+                       fprintf(fp, "References: %s\n", compose->references);
+       }
 
        /* Followup-To */
-       compose_write_headers_from_headerlist(compose, fp, "Followup-To");
+       compose_write_headers_from_headerlist(compose, fp, "Followup-To", ",");
 #if 0 /* NEW COMPOSE GUI */
        if (compose->use_followupto && !IS_IN_CUSTOM_HEADER("Followup-To")) {
                str = gtk_entry_get_text(GTK_ENTRY(compose->followup_entry));
@@ -4162,7 +4256,7 @@ static gint compose_write_headers(Compose *compose, FILE *fp,
        }
 #endif
        /* Reply-To */
-       compose_write_headers_from_headerlist(compose, fp, "Reply-To");
+       compose_write_headers_from_headerlist(compose, fp, "Reply-To", ", ");
 #if 0 /* NEW COMPOSE GUI */
        if (compose->use_replyto && !IS_IN_CUSTOM_HEADER("Reply-To")) {
                str = gtk_entry_get_text(GTK_ENTRY(compose->reply_entry));
@@ -4236,16 +4330,16 @@ static gint compose_write_headers(Compose *compose, FILE *fp,
        /* MIME */
        fprintf(fp, "Mime-Version: 1.0\n");
        if (compose_use_attach(compose)) {
-               get_rfc822_date(buf, sizeof(buf));
-               subst_char(buf, ' ', '_');
-               subst_char(buf, ',', '_');
-               compose->boundary = g_strdup_printf("Multipart_%s_%08x",
-                                                   buf, (guint)compose);
+               compose->boundary = generate_mime_boundary(NULL);
                fprintf(fp,
                        "Content-Type: multipart/mixed;\n"
                        " boundary=\"%s\"\n", compose->boundary);
        } else {
                fprintf(fp, "Content-Type: text/plain; charset=%s\n", charset);
+#if USE_GPGME
+               if (compose->use_signing && !compose->gnupg_mode)
+                       fprintf(fp, "Content-Disposition: inline\n");
+#endif
                fprintf(fp, "Content-Transfer-Encoding: %s\n",
                        procmime_get_encoding_str(encoding));
        }
@@ -4298,6 +4392,11 @@ static gint compose_write_headers(Compose *compose, FILE *fp,
                headerentry = ((ComposeHeaderEntry *)list->data);
                
                tmp = g_strdup(gtk_entry_get_text(GTK_ENTRY(GTK_COMBO(headerentry->combo)->entry)));
+               if (strchr(tmp, ' ') != NULL || strchr(tmp, '\r') != NULL || strchr(tmp, '\n') != NULL) {
+                       g_free(tmp);
+                       continue;
+               }
+
                if (!strstr(tmp, ":")) {
                        headername_wcolon = g_strconcat(tmp, ":", NULL);
                        headername = g_strdup(tmp);
@@ -4308,6 +4407,8 @@ static gint compose_write_headers(Compose *compose, FILE *fp,
                g_free(tmp);
                
                headervalue = gtk_entry_get_text(GTK_ENTRY(headerentry->entry));
+               subst_char(headervalue, '\r', ' ');
+               subst_char(headervalue, '\n', ' ');
                string = std_headers;
                while (*string != NULL) {
                        headername_trans = prefs_common.trans_hdr ? gettext(*string) : *string;
@@ -4319,8 +4420,7 @@ static gint compose_write_headers(Compose *compose, FILE *fp,
                        fprintf(fp, "%s %s\n", headername_wcolon, headervalue);
                                
                g_free(headername);
-               g_free(headername_wcolon);
-               
+               g_free(headername_wcolon);              
        }
 
        /* separator between header and body */
@@ -4339,6 +4439,8 @@ static void compose_convert_header(gchar *dest, gint len, gchar *src,
 
        if (len < 1) return;
 
+       subst_char(src, '\n', ' ');
+       subst_char(src, '\r', ' ');
        g_strchomp(src);
 
        conv_encode_header(dest, len, src, header_len, addr_field);
@@ -4970,6 +5072,7 @@ static Compose *compose_create(PrefsAccount *account, ComposeMode mode)
        ifactory = gtk_item_factory_from_widget(menubar);
        menu_set_sensitive(ifactory, "/Edit/Undo", FALSE);
        menu_set_sensitive(ifactory, "/Edit/Redo", FALSE);
+       menu_set_sensitive(ifactory, "/Message/Remove references", FALSE);
 
        tmpl_menu = gtk_item_factory_get_item(ifactory, "/Tools/Template");
 #if 0 /* NEW COMPOSE GUI */
@@ -5081,22 +5184,27 @@ static Compose *compose_create(PrefsAccount *account, ComposeMode mode)
                                                  prefs_common.use_alternate,
                                                  GTK_STEXT(text));
                        if (!gtkaspell) {
-                               alertpanel_error(_("Spell checker could not be started.\n%s"), gtkaspellcheckers->error_message);
+                               alertpanel_error(_("Spell checker could not "
+                                               "be started.\n%s"),
+                                               gtkaspell_checkers_strerror());
                                gtkaspell_checkers_reset_error();
                        } else {
 
                                GtkWidget *menuitem;
 
-                               if (!gtkaspell_set_sug_mode(gtkaspell, prefs_common.aspell_sugmode)) {
-                                       debug_print("Aspell: could not set suggestion mode %s\n",
-                                       gtkaspellcheckers->error_message);
+                               if (!gtkaspell_set_sug_mode(gtkaspell,
+                                               prefs_common.aspell_sugmode)) {
+                                       debug_print("Aspell: could not set "
+                                                   "suggestion mode %s\n",
+                                                   gtkaspell_checkers_strerror());
                                        gtkaspell_checkers_reset_error();
                                }
 
-                               menuitem = gtk_item_factory_get_item(ifactory, "/Spelling/Spelling Configuration");
+                               menuitem = gtk_item_factory_get_item(ifactory,
+                                       "/Spelling/Spelling Configuration");
                                gtkaspell_populate_submenu(gtkaspell, menuitem);
                                menu_set_sensitive(ifactory, "/Spelling", TRUE);
-                               }
+                       }
                }
        }
 #endif
@@ -6171,7 +6279,9 @@ static void compose_allow_user_actions (Compose *compose, gboolean allow)
        toolbar_comp_set_sensitive(compose, allow);
        menu_set_sensitive(ifactory, "/File", allow);
        menu_set_sensitive(ifactory, "/Edit", allow);
+#if USE_ASPELL
        menu_set_sensitive(ifactory, "/Spelling", allow);
+#endif 
        menu_set_sensitive(ifactory, "/Message", allow);
        menu_set_sensitive(ifactory, "/Tools", allow);
        menu_set_sensitive(ifactory, "/Help", allow);
@@ -6218,6 +6328,7 @@ static void compose_draft_cb(gpointer data, guint action, GtkWidget *widget)
        FolderItem *draft;
        gchar *tmp;
        gint msgnum;
+       MsgFlags flag = {0, 0};
        static gboolean lock = FALSE;
        MsgInfo *newmsginfo;
        
@@ -6237,7 +6348,8 @@ static void compose_draft_cb(gpointer data, guint action, GtkWidget *widget)
                return;
        }
 
-       if ((msgnum = folder_item_add_msg(draft, tmp, TRUE)) < 0) {
+       folder_item_scan(draft);
+       if ((msgnum = folder_item_add_msg(draft, tmp, &flag, TRUE)) < 0) {
                unlink(tmp);
                g_free(tmp);
                lock = FALSE;
@@ -6257,6 +6369,8 @@ static void compose_draft_cb(gpointer data, guint action, GtkWidget *widget)
                procmsg_msginfo_free(newmsginfo);
        }
        
+       folder_item_scan(draft);
+       
        lock = FALSE;
 
        /* 0: quit editing  1: keep editing  2: keep editing (autosave) */
@@ -6326,7 +6440,19 @@ static void compose_insert_file_cb(gpointer data, guint action,
 
                for ( tmp = file_list; tmp; tmp = tmp->next) {
                        gchar *file = (gchar *) tmp->data;
-                       compose_insert_file(compose, file);
+                       gchar *filedup = g_strdup(file);
+                       gchar *shortfile;
+                       ComposeInsertResult res;
+
+                       res = compose_insert_file(compose, file);
+                       shortfile = basename(filedup);
+                       if (res == COMPOSE_INSERT_READ_ERROR) {
+                               alertpanel_error(_("File '%s' could not be read."), shortfile);
+                       } else if (res == COMPOSE_INSERT_INVALID_CHARACTER) {
+                               alertpanel_error(_("File '%s' contained invalid characters\n"
+                                                  "for the current encoding, insertion may be incorrect."), shortfile);
+                       }
+                       g_free(filedup);
                        g_free(file);
                }
                g_list_free(file_list);
@@ -6859,6 +6985,17 @@ static void compose_toggle_return_receipt_cb(gpointer data, guint action,
                compose->return_receipt = FALSE;
 }
 
+static void compose_toggle_remove_refs_cb(gpointer data, guint action,
+                                            GtkWidget *widget)
+{
+       Compose *compose = (Compose *)data;
+
+       if (GTK_CHECK_MENU_ITEM(widget)->active)
+               compose->remove_references = TRUE;
+       else
+               compose->remove_references = FALSE;
+}
+
 void compose_headerentry_key_press_event_cb(GtkWidget *entry,
                                            GdkEventKey *event,
                                            ComposeHeaderEntry *headerentry)
@@ -6964,10 +7101,15 @@ static void text_inserted(GtkWidget *widget, const gchar *text,
                                           compose);
        gtk_signal_emit_stop_by_name(GTK_OBJECT(editable), "insert_text");
 
-       
        if (prefs_common.autosave && 
            gtk_stext_get_length(GTK_STEXT(widget)) % prefs_common.autosave_length == 0)
-               compose_draft_cb((gpointer)compose, 2, NULL);
+               gtk_timeout_add(500, (GtkFunction) compose_defer_auto_save_draft, compose);
+}
+
+static gint compose_defer_auto_save_draft(Compose *compose)
+{
+       compose_draft_cb((gpointer)compose, 2, NULL);
+       return FALSE;
 }
 
 static gboolean compose_send_control_enter(Compose *compose)