2008-09-18 [colin] 3.5.0cvs113
[claws.git] / src / procmime.c
index 4f7e38452c70ebaa11913d84c7a75defee189768..bfdf08427af391ffe4fd1bf8ec2a0634d4a99fd0 100644 (file)
@@ -1,10 +1,10 @@
 /*
  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2004 Hiroyuki Yamamoto & The Sylpheed-Claws Team
+ * Copyright (C) 1999-2007 Hiroyuki Yamamoto & The Claws Mail 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
- * the Free Software Foundation; either version 2 of the License, or
+ * the Free Software Foundation; either version 3 of the License, or
  * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
@@ -13,8 +13,8 @@
  * GNU General Public License for more details.
  *
  * 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.
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ * 
  */
 
 #ifdef HAVE_CONFIG_H
 #include <glib/gi18n.h>
 #include <stdio.h>
 #include <string.h>
-#include <locale.h>
+#if HAVE_LOCALE_H
+#  include <locale.h>
+#endif
 #include <ctype.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <unistd.h>
+#include <errno.h>
 
 #include "procmime.h"
 #include "procheader.h"
 #include "utils.h"
 #include "prefs_common.h"
 #include "prefs_gtk.h"
+#include "alertpanel.h"
+#include "timing.h"
 
 static GHashTable *procmime_get_mime_type_table        (void);
+static MimeInfo *procmime_scan_file_short(const gchar *filename);
+static MimeInfo *procmime_scan_queue_file_short(const gchar *filename);
+static MimeInfo *procmime_scan_queue_file_full(const gchar *filename, gboolean short_scan);
 
 MimeInfo *procmime_mimeinfo_new(void)
 {
@@ -102,7 +110,7 @@ static gboolean free_func(GNode *node, gpointer data)
        switch (mimeinfo->content) {
        case MIMECONTENT_FILE:
                if (mimeinfo->tmp)
-                       g_unlink(mimeinfo->data.filename);
+                       claws_unlink(mimeinfo->data.filename);
                g_free(mimeinfo->data.filename);
                break;
 
@@ -116,6 +124,7 @@ static gboolean free_func(GNode *node, gpointer data)
        g_free(mimeinfo->subtype);
        g_free(mimeinfo->description);
        g_free(mimeinfo->id);
+       g_free(mimeinfo->location);
 
        g_hash_table_foreach_remove(mimeinfo->typeparameters,
                procmime_mimeinfo_parameters_destroy, NULL);
@@ -145,53 +154,6 @@ void procmime_mimeinfo_free_all(MimeInfo *mimeinfo)
        g_node_destroy(node);
 }
 
-#if 0 /* UNUSED */
-MimeInfo *procmime_mimeinfo_insert(MimeInfo *parent, MimeInfo *mimeinfo)
-{
-       MimeInfo *child = parent->children;
-
-       if (!child)
-               parent->children = mimeinfo;
-       else {
-               while (child->next != NULL)
-                       child = child->next;
-
-               child->next = mimeinfo;
-       }
-
-       mimeinfo->parent = parent;
-       mimeinfo->level = parent->level + 1;
-
-       return mimeinfo;
-}
-
-void procmime_mimeinfo_replace(MimeInfo *old, MimeInfo *new)
-{
-       MimeInfo *parent = old->parent;
-       MimeInfo *child;
-
-       g_return_if_fail(parent != NULL);
-       g_return_if_fail(new->next == NULL);
-
-       for (child = parent->children; child && child != old;
-            child = child->next)
-               ;
-       if (!child) {
-               g_warning("oops: parent can't find it's own child");
-               return;
-       }
-       procmime_mimeinfo_free_all(old);
-
-       if (child == parent->children) {
-               new->next = parent->children->next;
-               parent->children = new;
-       } else {
-               new->next = child->next;
-               child = new;
-       }
-}
-#endif
-
 MimeInfo *procmime_mimeinfo_parent(MimeInfo *mimeinfo)
 {
        g_return_val_if_fail(mimeinfo != NULL, NULL);
@@ -228,14 +190,12 @@ MimeInfo *procmime_scan_message(MsgInfo *msginfo)
 {
        gchar *filename;
        MimeInfo *mimeinfo;
-
+       START_TIMING("");
        filename = procmsg_get_message_file_path(msginfo);
        if (!filename || !is_file_exist(filename)) {
                g_free(filename);
-               filename = procmsg_get_message_file(msginfo);
-       }
-       if (!filename || !is_file_exist(filename)) 
                return NULL;
+       }
 
        if (!folder_has_parent_of_type(msginfo->folder, F_QUEUE) &&
            !folder_has_parent_of_type(msginfo->folder, F_DRAFT))
@@ -244,6 +204,28 @@ MimeInfo *procmime_scan_message(MsgInfo *msginfo)
                mimeinfo = procmime_scan_queue_file(filename);
        g_free(filename);
 
+       END_TIMING();
+       return mimeinfo;
+}
+
+MimeInfo *procmime_scan_message_short(MsgInfo *msginfo)
+{
+       gchar *filename;
+       MimeInfo *mimeinfo;
+
+       filename = procmsg_get_message_file_path(msginfo);
+       if (!filename || !is_file_exist(filename)) {
+               g_free(filename);
+               return NULL;
+       }
+
+       if (!folder_has_parent_of_type(msginfo->folder, F_QUEUE) &&
+           !folder_has_parent_of_type(msginfo->folder, F_DRAFT))
+               mimeinfo = procmime_scan_file_short(filename);
+       else
+               mimeinfo = procmime_scan_queue_file_short(filename);
+       g_free(filename);
+
        return mimeinfo;
 }
 
@@ -270,6 +252,27 @@ const gchar *procmime_mimeinfo_get_parameter(MimeInfo *mimeinfo, const gchar *na
        return value;
 }
 
+#define FLUSH_LASTLINE() {                                                     \
+       if (*lastline != '\0') {                                                \
+               gint llen = 0;                                                  \
+               strretchomp(lastline);                                          \
+               llen = strlen(lastline);                                        \
+               if (lastline[llen-1] == ' ' && strcmp(lastline,"-- ")) {        \
+                       /* this is flowed */                                    \
+                       if (delsp)                                              \
+                               lastline[llen-1] = '\0';                        \
+                       if (fputs(lastline, outfp) == EOF)                      \
+                               err = TRUE;                                     \
+               } else {                                                        \
+                       if (fputs(lastline, outfp) == EOF)                      \
+                               err = TRUE;                                     \
+                       if (fputs("\n", outfp) == EOF)                          \
+                               err = TRUE;                                     \
+               }                                                               \
+       }                                                                       \
+       strcpy(lastline, buf);                                                  \
+}
+
 gboolean procmime_decode_content(MimeInfo *mimeinfo)
 {
        gchar buf[BUFFSIZE];
@@ -278,15 +281,36 @@ gboolean procmime_decode_content(MimeInfo *mimeinfo)
        FILE *outfp, *infp;
        struct stat statbuf;
        gboolean tmp_file = FALSE;
+       gboolean flowed = FALSE;
+       gboolean delsp = FALSE; 
+       gboolean err = FALSE;
 
        EncodingType encoding = forced_encoding 
                                ? forced_encoding
                                : mimeinfo->encoding_type;
+       gchar lastline[BUFFSIZE];
+       memset(lastline, 0, BUFFSIZE);
                   
        g_return_val_if_fail(mimeinfo != NULL, FALSE);
 
-       if (encoding == ENC_UNKNOWN ||
-           encoding == ENC_BINARY)
+       if (prefs_common.respect_flowed_format &&
+           mimeinfo->type == MIMETYPE_TEXT && 
+           !strcasecmp(mimeinfo->subtype, "plain")) {
+               if (procmime_mimeinfo_get_parameter(mimeinfo, "format") != NULL &&
+                   !strcasecmp(procmime_mimeinfo_get_parameter(mimeinfo, "format"),"flowed"))
+                       flowed = TRUE;
+               if (flowed &&
+                   procmime_mimeinfo_get_parameter(mimeinfo, "delsp") != NULL &&
+                   !strcasecmp(procmime_mimeinfo_get_parameter(mimeinfo, "delsp"),"yes"))
+                       delsp = TRUE;
+       }
+       
+       if (!flowed && (
+            encoding == ENC_UNKNOWN ||
+            encoding == ENC_BINARY ||
+            encoding == ENC_7BIT ||
+            encoding == ENC_8BIT
+           ))
                return TRUE;
 
        infp = g_fopen(mimeinfo->data.filename, "rb");
@@ -299,6 +323,7 @@ gboolean procmime_decode_content(MimeInfo *mimeinfo)
        outfp = get_tmpfile_in_dir(get_mime_tmp_dir(), &tmpfilename);
        if (!outfp) {
                perror("tmpfile");
+               fclose(infp);
                return FALSE;
        }
        tmp_file = TRUE;
@@ -308,8 +333,16 @@ gboolean procmime_decode_content(MimeInfo *mimeinfo)
                while ((ftell(infp) < readend) && (fgets(buf, sizeof(buf), infp) != NULL)) {
                        gint len;
                        len = qp_decode_line(buf);
-                       fwrite(buf, 1, len, outfp);
+                       buf[len]='\0';
+                       if (!flowed) {
+                               if (fwrite(buf, 1, len, outfp) < len)
+                                       err = TRUE;
+                       } else {
+                               FLUSH_LASTLINE();
+                       }
                }
+               if (flowed)
+                       FLUSH_LASTLINE();
        } else if (encoding == ENC_BASE64) {
                gchar outbuf[BUFFSIZE];
                gint len;
@@ -325,6 +358,7 @@ gboolean procmime_decode_content(MimeInfo *mimeinfo)
                        if (!tmpfp) {
                                perror("tmpfile");
                                if (tmp_file) fclose(outfp);
+                               fclose(infp);
                                return FALSE;
                        }
                }
@@ -334,16 +368,18 @@ gboolean procmime_decode_content(MimeInfo *mimeinfo)
                        len = base64_decoder_decode(decoder, buf, outbuf);
                        if (len < 0 && !got_error) {
                                g_warning("Bad BASE64 content.\n");
-                               fwrite(_("[Error decoding BASE64]\n"),
+                               if (fwrite(_("[Error decoding BASE64]\n"),
                                        sizeof(gchar),
                                        strlen(_("[Error decoding BASE64]\n")),
-                                       tmpfp);
+                                       tmpfp) < strlen(_("[Error decoding BASE64]\n")))
+                                       g_warning("error decoding BASE64");
                                got_error = TRUE;
                                continue;
                        } else if (len >= 0) {
                                /* print out the error message only once 
                                 * per block */
-                               fwrite(outbuf, sizeof(gchar), len, tmpfp);
+                               if (fwrite(outbuf, sizeof(gchar), len, tmpfp) < len)
+                                       err = TRUE;
                                got_error = FALSE;
                        }
                }
@@ -353,7 +389,8 @@ gboolean procmime_decode_content(MimeInfo *mimeinfo)
                        rewind(tmpfp);
                        while (fgets(buf, sizeof(buf), tmpfp) != NULL) {
                                strcrchomp(buf);
-                               fputs(buf, outfp);
+                               if (fputs(buf, outfp) == EOF)
+                                       err = TRUE;
                        }
                        fclose(tmpfp);
                }
@@ -372,24 +409,37 @@ gboolean procmime_decode_content(MimeInfo *mimeinfo)
                                                g_warning("Bad UUENCODE content(%d)\n", len);
                                        break;
                                }
-                               fwrite(outbuf, sizeof(gchar), len, outfp);
+                               if (fwrite(outbuf, sizeof(gchar), len, outfp) < len)
+                                       err = TRUE;
                        } else
                                flag = TRUE;
                }
        } else {
                while ((ftell(infp) < readend) && (fgets(buf, sizeof(buf), infp) != NULL)) {
-                       fputs(buf, outfp);
+                       if (!flowed) {
+                               if (fputs(buf, outfp) == EOF)
+                                       err = TRUE;
+                       } else {
+                               FLUSH_LASTLINE();
+                       }
                }
+               if (flowed)
+                       FLUSH_LASTLINE();
+               if (err == TRUE)
+                       g_warning("write error");
        }
 
        fclose(outfp);
        fclose(infp);
 
+       if (err == TRUE) {
+               return FALSE;
+       }
+
        stat(tmpfilename, &statbuf);
        if (mimeinfo->tmp && (mimeinfo->data.filename != NULL))
-               g_unlink(mimeinfo->data.filename);
-       if (mimeinfo->data.filename != NULL)
-               g_free(mimeinfo->data.filename);
+               claws_unlink(mimeinfo->data.filename);
+       g_free(mimeinfo->data.filename);
        mimeinfo->data.filename = tmpfilename;
        mimeinfo->tmp = TRUE;
        mimeinfo->offset = 0;
@@ -408,6 +458,10 @@ gboolean procmime_encode_content(MimeInfo *mimeinfo, EncodingType encoding)
        gint len;
        gchar *tmpfilename;
        struct stat statbuf;
+       gboolean err = FALSE;
+
+       if (mimeinfo->content == MIMECONTENT_EMPTY)
+               return TRUE;
 
        if (mimeinfo->encoding_type != ENC_UNKNOWN &&
            mimeinfo->encoding_type != ENC_BINARY &&
@@ -449,7 +503,7 @@ gboolean procmime_encode_content(MimeInfo *mimeinfo, EncodingType encoding)
                                }
                                if ((tmp_fp = g_fopen(tmp_file, "rb")) == NULL) {
                                        FILE_OP_ERROR(tmp_file, "fopen");
-                                       g_unlink(tmp_file);
+                                       claws_unlink(tmp_file);
                                        g_free(tmp_file);
                                        fclose(infp);
                                        return FALSE;
@@ -469,18 +523,22 @@ gboolean procmime_encode_content(MimeInfo *mimeinfo, EncodingType encoding)
                                    B64_LINE_SIZE, tmp_fp))
                       == B64_LINE_SIZE) {
                        base64_encode(outbuf, inbuf, B64_LINE_SIZE);
-                       fputs(outbuf, outfp);
-                       fputc('\n', outfp);
+                       if (fputs(outbuf, outfp) == EOF)
+                               err = TRUE;
+                       if (fputc('\n', outfp) == EOF)
+                               err = TRUE;
                }
                if (len > 0 && feof(tmp_fp)) {
                        base64_encode(outbuf, inbuf, len);
-                       fputs(outbuf, outfp);
-                       fputc('\n', outfp);
+                       if (fputs(outbuf, outfp) == EOF)
+                               err = TRUE;
+                       if (fputc('\n', outfp) == EOF)
+                               err = TRUE;
                }
 
                if (tmp_file) {
                        fclose(tmp_fp);
-                       g_unlink(tmp_file);
+                       claws_unlink(tmp_file);
                        g_free(tmp_file);
                }
        } else if (encoding == ENC_QUOTED_PRINTABLE) {
@@ -494,26 +552,34 @@ gboolean procmime_encode_content(MimeInfo *mimeinfo, EncodingType encoding)
                                
                                tmpbuf += sizeof("From ")-1;
                                
-                               fputs("=46rom ", outfp);
-                               fputs(tmpbuf, outfp);
-                       } else 
-                               fputs(outbuf, outfp);
+                               if (fputs("=46rom ", outfp) == EOF)
+                                       err = TRUE;
+                               if (fputs(tmpbuf, outfp) == EOF)
+                                       err = TRUE;
+                       } else {
+                               if (fputs(outbuf, outfp) == EOF)
+                                       err = TRUE;
+                       }
                }
        } else {
                gchar buf[BUFFSIZE];
 
                while (fgets(buf, sizeof(buf), infp) != NULL) {
                        strcrchomp(buf);
-                       fputs(buf, outfp);
+                       if (fputs(buf, outfp) == EOF)
+                               err = TRUE;
                }
        }
 
        fclose(outfp);
        fclose(infp);
 
+       if (err == TRUE)
+               return FALSE;
+
        if (mimeinfo->content == MIMECONTENT_FILE) {
                if (mimeinfo->tmp && (mimeinfo->data.filename != NULL))
-                       g_unlink(mimeinfo->data.filename);
+                       claws_unlink(mimeinfo->data.filename);
                g_free(mimeinfo->data.filename);
        } else if (mimeinfo->content == MIMECONTENT_MEM) {
                if (mimeinfo->tmp && (mimeinfo->data.mem != NULL))
@@ -536,150 +602,64 @@ gint procmime_get_part(const gchar *outfile, MimeInfo *mimeinfo)
        FILE *infp, *outfp;
        gchar buf[BUFFSIZE];
        gint restlength, readlength;
+       gint saved_errno = 0;
 
        g_return_val_if_fail(outfile != NULL, -1);
        g_return_val_if_fail(mimeinfo != NULL, -1);
 
        if (mimeinfo->encoding_type != ENC_BINARY && !procmime_decode_content(mimeinfo))
-               return -1;
+               return -EINVAL;
 
        if ((infp = g_fopen(mimeinfo->data.filename, "rb")) == NULL) {
+               saved_errno = errno;
                FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
-               return -1;
+               return -(saved_errno);
        }
        if (fseek(infp, mimeinfo->offset, SEEK_SET) < 0) {
+               saved_errno = errno;
                FILE_OP_ERROR(mimeinfo->data.filename, "fseek");
                fclose(infp);
-               return -1;
+               return -(saved_errno);
        }
        if ((outfp = g_fopen(outfile, "wb")) == NULL) {
+               saved_errno = errno;
                FILE_OP_ERROR(outfile, "fopen");
                fclose(infp);
-               return -1;
+               return -(saved_errno);
        }
 
        restlength = mimeinfo->length;
 
        while ((restlength > 0) && ((readlength = fread(buf, 1, restlength > BUFFSIZE ? BUFFSIZE : restlength, infp)) > 0)) {
-               fwrite(buf, 1, readlength, outfp);
+               if (fwrite(buf, 1, readlength, outfp) != readlength) {
+                       saved_errno = errno;
+                       fclose(outfp);
+                       return -(saved_errno);
+               }
                restlength -= readlength;
        }
 
        fclose(infp);
        if (fclose(outfp) == EOF) {
+               saved_errno = errno;
                FILE_OP_ERROR(outfile, "fclose");
-               g_unlink(outfile);
-               return -1;
+               claws_unlink(outfile);
+               return -(saved_errno);
        }
 
        return 0;
 }
 
-struct ContentRenderer {
-       char * content_type;
-       char * renderer;
-};
-
-static GList * renderer_list = NULL;
-
-static struct ContentRenderer *
-content_renderer_new(char * content_type, char * renderer)
-{
-       struct ContentRenderer * cr;
-
-       cr = g_new(struct ContentRenderer, 1);
-       if (cr == NULL)
-               return NULL;
-
-       cr->content_type = g_strdup(content_type);
-       cr->renderer = g_strdup(renderer);
-
-       return cr;
-}
-
-static void content_renderer_free(struct ContentRenderer * cr)
-{
-       g_free(cr->content_type);
-       g_free(cr->renderer);
-       g_free(cr);
-}
-
-void renderer_read_config(void)
-{
-       gchar buf[BUFFSIZE];
-       FILE * f;
-       gchar * rcpath;
-
-       g_list_foreach(renderer_list, (GFunc) content_renderer_free, NULL);
-       renderer_list = NULL;
-
-       rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, RENDERER_RC, NULL);
-       f = g_fopen(rcpath, "rb");
-       g_free(rcpath);
-       
-       if (f == NULL)
-               return;
-
-       while (fgets(buf, BUFFSIZE, f)) {
-               char * p;
-               struct ContentRenderer * cr;
-
-               strretchomp(buf);
-               p = strchr(buf, ' ');
-               if (p == NULL)
-                       continue;
-               * p = 0;
-
-               cr = content_renderer_new(buf, p + 1);
-               if (cr == NULL)
-                       continue;
-
-               renderer_list = g_list_append(renderer_list, cr);
-       }
-
-       fclose(f);
-}
-
-void renderer_write_config(void)
-{
-       gchar * rcpath;
-       PrefFile *pfile;
-       GList * cur;
-
-       rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, RENDERER_RC, NULL);
-       
-       if ((pfile = prefs_write_open(rcpath)) == NULL) {
-               g_warning("failed to write configuration to file\n");
-               g_free(rcpath);
-               return;
-       }
-
-       g_free(rcpath);
-
-       for (cur = renderer_list ; cur != NULL ; cur = cur->next) {
-               struct ContentRenderer * renderer;
-               renderer = cur->data;
-               fprintf(pfile->fp, "%s %s\n", renderer->content_type,
-                       renderer->renderer);
-       }
-
-       if (prefs_file_close(pfile) < 0) {
-               g_warning("failed to write configuration to file\n");
-               return;
-       }
-}
-
-FILE *procmime_get_text_content(MimeInfo *mimeinfo)
+static FILE *procmime_get_text_content(MimeInfo *mimeinfo)
 {
        FILE *tmpfp, *outfp;
        const gchar *src_codeset;
        gboolean conv_fail = FALSE;
        gchar buf[BUFFSIZE];
        gchar *str;
-       struct ContentRenderer * renderer;
-       GList * cur;
-       gchar *tmpfile, *content_type;
-    
+       gchar *tmpfile;
+       gboolean err = FALSE;
+
        g_return_val_if_fail(mimeinfo != NULL, NULL);
 
        if (!procmime_decode_content(mimeinfo))
@@ -711,52 +691,20 @@ FILE *procmime_get_text_content(MimeInfo *mimeinfo)
                      ? forced_charset : 
                      procmime_mimeinfo_get_parameter(mimeinfo, "charset");
 
-       renderer = NULL;
+       if (!forced_charset && !strcasecmp(src_codeset, CS_ISO_8859_1))
+               src_codeset = CS_WINDOWS_1252;
 
-       content_type = procmime_get_content_type_str(mimeinfo->type,
-                                                    mimeinfo->subtype);
-       for (cur = renderer_list ; cur != NULL ; cur = cur->next) {
-               struct ContentRenderer * cr;
-
-               cr = cur->data;
-               if (g_ascii_strcasecmp(cr->content_type, content_type) == 0) {
-                       renderer = cr;
-                       break;
-               }
-       }
-       g_free(content_type);
-
-       if (renderer != NULL) {
-               FILE * p;
-               int oldout;
-               
-               oldout = dup(1);
-               
-               dup2(fileno(outfp), 1);
-               
-               p = popen(renderer->renderer, "w");
-               if (p != NULL) {
-                       size_t count;
-                       
-                       while ((count =
-                               fread(buf, sizeof(char), sizeof(buf),
-                                     tmpfp)) > 0)
-                               fwrite(buf, sizeof(char), count, p);
-                       pclose(p);
-               }
-               
-               dup2(oldout, 1);
-/* CodeConverter seems to have no effect here */
-       } else if (mimeinfo->type == MIMETYPE_TEXT && !g_ascii_strcasecmp(mimeinfo->subtype, "html")) {
-               HTMLParser *parser;
+       if (mimeinfo->type == MIMETYPE_TEXT && !g_ascii_strcasecmp(mimeinfo->subtype, "html")) {
+               SC_HTMLParser *parser;
                CodeConverter *conv;
 
                conv = conv_code_converter_new(src_codeset);
-               parser = html_parser_new(tmpfp, conv);
-               while ((str = html_parse(parser)) != NULL) {
-                       fputs(str, outfp);
+               parser = sc_html_parser_new(tmpfp, conv);
+               while ((str = sc_html_parse(parser)) != NULL) {
+                       if (fputs(str, outfp) == EOF)
+                               err = TRUE;
                }
-               html_parser_destroy(parser);
+               sc_html_parser_destroy(parser);
                conv_code_converter_destroy(conv);
        } else if (mimeinfo->type == MIMETYPE_TEXT && !g_ascii_strcasecmp(mimeinfo->subtype, "enriched")) {
                ERTFParser *parser;
@@ -765,7 +713,8 @@ FILE *procmime_get_text_content(MimeInfo *mimeinfo)
                conv = conv_code_converter_new(src_codeset);
                parser = ertf_parser_new(tmpfp, conv);
                while ((str = ertf_parse(parser)) != NULL) {
-                       fputs(str, outfp);
+                       if (fputs(str, outfp) == EOF)
+                               err = TRUE;
                }
                ertf_parser_destroy(parser);
                conv_code_converter_destroy(conv);
@@ -773,11 +722,13 @@ FILE *procmime_get_text_content(MimeInfo *mimeinfo)
                while (fgets(buf, sizeof(buf), tmpfp) != NULL) {
                        str = conv_codeset_strdup(buf, src_codeset, CS_UTF_8);
                        if (str) {
-                               fputs(str, outfp);
+                               if (fputs(str, outfp) == EOF)
+                                       err = TRUE;
                                g_free(str);
                        } else {
                                conv_fail = TRUE;
-                               fputs(buf, outfp);
+                               if (fputs(buf, outfp) == EOF)
+                                       err = TRUE;
                        }
                }
        }
@@ -787,9 +738,14 @@ FILE *procmime_get_text_content(MimeInfo *mimeinfo)
 
        fclose(tmpfp);
        rewind(outfp);
-       g_unlink(tmpfile);
+       claws_unlink(tmpfile);
        g_free(tmpfile);
 
+       if (err == TRUE) {
+               fclose(outfp);
+               return NULL;
+       }
+
        return outfp;
 }
 
@@ -799,21 +755,42 @@ FILE *procmime_get_first_text_content(MsgInfo *msginfo)
 {
        FILE *outfp = NULL;
        MimeInfo *mimeinfo, *partinfo;
-
+       gboolean empty_ok = FALSE, short_scan = TRUE;
+       START_TIMING("");
        g_return_val_if_fail(msginfo != NULL, NULL);
 
-       mimeinfo = procmime_scan_message(msginfo);
+       /* first we try to short-scan (for speed), refusing empty parts */
+scan_again:
+       if (short_scan)
+               mimeinfo = procmime_scan_message_short(msginfo);
+       else
+               mimeinfo = procmime_scan_message(msginfo);
        if (!mimeinfo) return NULL;
 
        partinfo = mimeinfo;
-       while (partinfo && partinfo->type != MIMETYPE_TEXT) {
+       while (partinfo && (partinfo->type != MIMETYPE_TEXT ||
+              (partinfo->length == 0 && !empty_ok))) {
                partinfo = procmime_mimeinfo_next(partinfo);
        }
        if (partinfo)
                outfp = procmime_get_text_content(partinfo);
-
+       else if (!empty_ok && short_scan) {
+               /* if short scan didn't find a non-empty part, rescan
+                * fully for non-empty parts
+                */
+               short_scan = FALSE;
+               procmime_mimeinfo_free_all(mimeinfo);
+               goto scan_again;
+       } else if (!empty_ok && !short_scan) {
+               /* if full scan didn't find a non-empty part, rescan
+                * accepting empty parts 
+                */
+               empty_ok = TRUE;
+               procmime_mimeinfo_free_all(mimeinfo);
+               goto scan_again;
+       }
        procmime_mimeinfo_free_all(mimeinfo);
-
+       END_TIMING();
        return outfp;
 }
 
@@ -858,12 +835,17 @@ FILE *procmime_get_first_encrypted_text_content(MsgInfo *msginfo)
        partinfo = mimeinfo;
        if ((encinfo = find_encrypted_part(partinfo)) != NULL) {
                debug_print("decrypting message part\n");
-               if (privacy_mimeinfo_decrypt(encinfo) < 0)
+               if (privacy_mimeinfo_decrypt(encinfo) < 0) {
+                       alertpanel_error(_("Couldn't decrypt: %s"),
+                               privacy_get_error());
                        return NULL;
+               }
        }
        partinfo = mimeinfo;
        while (partinfo && partinfo->type != MIMETYPE_TEXT) {
                partinfo = procmime_mimeinfo_next(partinfo);
+               if (privacy_mimeinfo_is_signed(partinfo))
+                       procmsg_msginfo_set_flags(msginfo, 0, MSG_SIGNED);
        }
 
        if (partinfo)
@@ -893,7 +875,7 @@ gboolean procmime_msginfo_is_encrypted(MsgInfo *msginfo)
        return result;
 }
 
-gboolean procmime_find_string_part(MimeInfo *mimeinfo, const gchar *filename,
+static gboolean procmime_find_string_part(MimeInfo *mimeinfo, const gchar *filename,
                                   const gchar *str, StrFindFunc find_func)
 {
        FILE *outfp;
@@ -977,8 +959,10 @@ gchar *procmime_get_tmp_file_name(MimeInfo *mimeinfo)
                if (basetmp == NULL)
                        basetmp = "mimetmp";
                basetmp = g_path_get_basename(basetmp);
-               if (*basetmp == '\0') basetmp = g_strdup("mimetmp");
+               if (*basetmp == '\0') 
+                       basetmp = g_strdup("mimetmp");
                base = conv_filename_from_utf8(basetmp);
+               g_free((gchar*)basetmp);
                subst_for_shellsafe_filename(base);
        }
 
@@ -1007,12 +991,11 @@ gchar *procmime_get_mime_type(const gchar *filename)
 
        base = g_path_get_basename(filename);
        if ((p = strrchr(base, '.')) != NULL)
-               Xstrdup_a(ext, p + 1, p = NULL );
+               ext = g_utf8_strdown(p + 1, -1);
        g_free(base);
-       if (!p) return NULL;
 
-       g_strdown(ext);
        mime_type = g_hash_table_lookup(mime_type_table, ext);
+       g_free(ext);
        if (mime_type) {
                gchar *str;
 
@@ -1069,8 +1052,6 @@ static GHashTable *procmime_get_mime_type_table(void)
 
                exts = g_strsplit(mime_type->extension, " ", 16);
                for (i = 0; exts[i] != NULL; i++) {
-                       /* make the key case insensitive */
-                       g_strdown(exts[i]);
                        /* use previously dup'd key on overwriting */
                        if (g_hash_table_lookup(table, exts[i]))
                                key = exts[i];
@@ -1097,7 +1078,12 @@ GList *procmime_get_mime_type_list(void)
        if (mime_type_list) 
                return mime_type_list;
        
-       if ((fp = g_fopen("/usr/share/mime/globs", "rb")) == NULL) {
+#if defined(__NetBSD__) || defined(__OpenBSD__) || defined(__FreeBSD__)
+       if ((fp = g_fopen(DATAROOTDIR "/mime/globs", "rb")) == NULL) 
+#else
+       if ((fp = g_fopen("/usr/share/mime/globs", "rb")) == NULL) 
+#endif
+       {
                fp_is_glob_file = FALSE;
                if ((fp = g_fopen("/etc/mime.types", "rb")) == NULL) {
                        if ((fp = g_fopen(SYSCONFDIR "/mime.types", "rb")) 
@@ -1141,7 +1127,7 @@ GList *procmime_get_mime_type_list(void)
                }
 
                if (*p)
-                       mime_type->extension = g_strdup(p);
+                       mime_type->extension = g_utf8_strdown(p, -1);
                else
                        mime_type->extension = NULL;
 
@@ -1169,11 +1155,13 @@ EncodingType procmime_get_encoding_for_charset(const gchar *charset)
                return ENC_8BIT;
        else if (!g_ascii_strncasecmp(charset, "ISO-8859-", 9))
                return ENC_QUOTED_PRINTABLE;
-       else
+       else if (!g_ascii_strncasecmp(charset, "UTF-8", 5))
+               return ENC_QUOTED_PRINTABLE;
+       else 
                return ENC_8BIT;
 }
 
-EncodingType procmime_get_encoding_for_text_file(const gchar *file)
+EncodingType procmime_get_encoding_for_text_file(const gchar *file, gboolean *has_binary)
 {
        FILE *fp;
        guchar buf[BUFFSIZE];
@@ -1181,6 +1169,7 @@ EncodingType procmime_get_encoding_for_text_file(const gchar *file)
        size_t octet_chars = 0;
        size_t total_len = 0;
        gfloat octet_percentage;
+       gboolean force_b64 = FALSE;
 
        if ((fp = g_fopen(file, "rb")) == NULL) {
                FILE_OP_ERROR(file, "fopen");
@@ -1194,6 +1183,10 @@ EncodingType procmime_get_encoding_for_text_file(const gchar *file)
                for (p = buf, i = 0; i < len; ++p, ++i) {
                        if (*p & 0x80)
                                ++octet_chars;
+                       if (*p == '\0') {
+                               force_b64 = TRUE;
+                               *has_binary = TRUE;
+                       }
                }
                total_len += len;
        }
@@ -1206,10 +1199,10 @@ EncodingType procmime_get_encoding_for_text_file(const gchar *file)
                octet_percentage = 0.0;
 
        debug_print("procmime_get_encoding_for_text_file(): "
-                   "8bit chars: %d / %d (%f%%)\n", octet_chars, total_len,
+                   "8bit chars: %zd / %zd (%f%%)\n", octet_chars, total_len,
                    100.0 * octet_percentage);
 
-       if (octet_percentage > 0.20) {
+       if (octet_percentage > 0.20 || force_b64) {
                debug_print("using BASE64\n");
                return ENC_BASE64;
        } else if (octet_chars > 0) {
@@ -1303,17 +1296,21 @@ gchar *procmime_get_content_type_str(MimeMediaType type,
        return g_strdup_printf("%s/%s", type_str, subtype);
 }
 
-int procmime_parse_mimepart(MimeInfo *parent,
+static int procmime_parse_mimepart(MimeInfo *parent,
                             gchar *content_type,
                             gchar *content_encoding,
                             gchar *content_description,
                             gchar *content_id,
                             gchar *content_disposition,
+                            gchar *content_location,
+                            const gchar *original_msgid,
+                            const gchar *disposition_notification_hdr,
                             const gchar *filename,
                             guint offset,
-                            guint length);
+                            guint length,
+                            gboolean short_scan);
 
-void procmime_parse_message_rfc822(MimeInfo *mimeinfo)
+static void procmime_parse_message_rfc822(MimeInfo *mimeinfo, gboolean short_scan)
 {
        HeaderEntry hentry[] = {{"Content-Type:",  NULL, TRUE},
                                {"Content-Transfer-Encoding:",
@@ -1324,12 +1321,19 @@ void procmime_parse_message_rfc822(MimeInfo *mimeinfo)
                                                   NULL, TRUE},
                                {"Content-Disposition:",
                                                   NULL, TRUE},
+                               {"Content-Location:",
+                                                  NULL, TRUE},
                                {"MIME-Version:",
                                                   NULL, TRUE},
+                               {"Original-Message-ID:",
+                                                  NULL, TRUE},
+                               {"Disposition:",
+                                                  NULL, TRUE},
                                {NULL,             NULL, FALSE}};
        guint content_start, i;
        FILE *fp;
         gchar *tmp;
+       gint len = 0;
 
        procmime_decode_content(mimeinfo);
 
@@ -1355,23 +1359,147 @@ void procmime_parse_message_rfc822(MimeInfo *mimeinfo)
                 g_free(hentry[4].body);
                 hentry[4].body = tmp;
         }                
+       if (hentry[5].body != NULL) {
+                tmp = conv_unmime_header(hentry[5].body, NULL);
+                g_free(hentry[5].body);
+                hentry[5].body = tmp;
+        }                
+       if (hentry[7].body != NULL) {
+                tmp = conv_unmime_header(hentry[7].body, NULL);
+                g_free(hentry[7].body);
+                hentry[7].body = tmp;
+        }
+       if (hentry[8].body != NULL) {
+                tmp = conv_unmime_header(hentry[8].body, NULL);
+                g_free(hentry[8].body);
+                hentry[8].body = tmp;
+        }
+  
        content_start = ftell(fp);
        fclose(fp);
        
+       len = mimeinfo->length - (content_start - mimeinfo->offset);
+       if (len < 0)
+               len = 0;
        procmime_parse_mimepart(mimeinfo,
                                hentry[0].body, hentry[1].body,
                                hentry[2].body, hentry[3].body,
-                               hentry[4].body,
+                               hentry[4].body, hentry[5].body,
+                               hentry[7].body, hentry[8].body, 
                                mimeinfo->data.filename, content_start,
-                               mimeinfo->length - (content_start - mimeinfo->offset));
+                               len, short_scan);
+       
+       for (i = 0; i < (sizeof hentry / sizeof hentry[0]); i++) {
+               g_free(hentry[i].body);
+               hentry[i].body = NULL;
+       }
+}
+
+static void procmime_parse_disposition_notification(MimeInfo *mimeinfo, 
+               const gchar *original_msgid, const gchar *disposition_notification_hdr,
+               gboolean short_scan)
+{
+       HeaderEntry hentry[] = {{"Original-Message-ID:",  NULL, TRUE},
+                               {"Disposition:",          NULL, TRUE},
+                               {NULL,                    NULL, FALSE}};
+       guint i;
+       FILE *fp;
+       gchar *orig_msg_id = NULL;
+       gchar *disp = NULL;
+
+       procmime_decode_content(mimeinfo);
+
+       debug_print("parse disposition notification\n");
+       fp = g_fopen(mimeinfo->data.filename, "rb");
+       if (fp == NULL) {
+               FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
+               return;
+       }
+       fseek(fp, mimeinfo->offset, SEEK_SET);
+
+       if (original_msgid && disposition_notification_hdr) {
+               hentry[0].body = g_strdup(original_msgid);
+               hentry[1].body = g_strdup(disposition_notification_hdr);
+       } else {
+               procheader_get_header_fields(fp, hentry);
+       }
+
+       if (!hentry[0].body || !hentry[1].body) {
+               debug_print("MsgId %s, Disp %s\n",
+                       hentry[0].body ? hentry[0].body:"(nil)",
+                       hentry[1].body ? hentry[1].body:"(nil)");
+               goto bail;
+       }
+
+       orig_msg_id = g_strdup(hentry[0].body);
+       disp = g_strdup(hentry[1].body);
+
+       extract_parenthesis(orig_msg_id, '<', '>');
+       remove_space(orig_msg_id);
        
+       if (strstr(disp, "displayed")) {
+               /* find sent message, if possible */
+               MsgInfo *info = NULL;
+               GList *flist;
+               debug_print("%s has been displayed.\n", orig_msg_id);
+               for (flist = folder_get_list(); flist != NULL; flist = g_list_next(flist)) {
+                       FolderItem *outbox = ((Folder *)(flist->data))->outbox;
+                       if (!outbox) {
+                               debug_print("skipping folder with no outbox...\n");
+                               continue;
+                       }
+                       info = folder_item_get_msginfo_by_msgid(outbox, orig_msg_id);
+                       debug_print("%s %s in %s\n", info?"found":"didn't find", orig_msg_id, outbox->path);
+                       if (info) {
+                               procmsg_msginfo_set_flags(info, MSG_RETRCPT_GOT, 0);
+                               procmsg_msginfo_free(info);
+                       }
+               }
+       }
+       g_free(orig_msg_id);
+       g_free(disp);
+bail:
        for (i = 0; i < (sizeof hentry / sizeof hentry[0]); i++) {
                g_free(hentry[i].body);
                hentry[i].body = NULL;
        }
 }
 
-void procmime_parse_multipart(MimeInfo *mimeinfo)
+#define GET_HEADERS() {                                                \
+       procheader_get_header_fields(fp, hentry);               \
+        if (hentry[0].body != NULL) {                          \
+                tmp = conv_unmime_header(hentry[0].body, NULL);        \
+                g_free(hentry[0].body);                                \
+                hentry[0].body = tmp;                          \
+        }                                                      \
+        if (hentry[2].body != NULL) {                          \
+                tmp = conv_unmime_header(hentry[2].body, NULL);        \
+                g_free(hentry[2].body);                                \
+                hentry[2].body = tmp;                          \
+        }                                                      \
+        if (hentry[4].body != NULL) {                          \
+                tmp = conv_unmime_header(hentry[4].body, NULL);        \
+                g_free(hentry[4].body);                                \
+                hentry[4].body = tmp;                          \
+        }                                                      \
+        if (hentry[5].body != NULL) {                          \
+                tmp = conv_unmime_header(hentry[5].body, NULL);        \
+                g_free(hentry[5].body);                                \
+                hentry[5].body = tmp;                          \
+        }                                                      \
+       if (hentry[6].body != NULL) {                           \
+                tmp = conv_unmime_header(hentry[6].body, NULL);        \
+                g_free(hentry[6].body);                                \
+                hentry[6].body = tmp;                          \
+        }                                                      \
+       if (hentry[7].body != NULL) {                           \
+                tmp = conv_unmime_header(hentry[7].body, NULL);        \
+                g_free(hentry[7].body);                                \
+                hentry[7].body = tmp;                          \
+        }                                                      \
+}
+
+static void procmime_parse_multipart(MimeInfo *mimeinfo, gboolean short_scan)
 {
        HeaderEntry hentry[] = {{"Content-Type:",  NULL, TRUE},
                                {"Content-Transfer-Encoding:",
@@ -1382,6 +1510,12 @@ void procmime_parse_multipart(MimeInfo *mimeinfo)
                                                   NULL, TRUE},
                                {"Content-Disposition:",
                                                   NULL, TRUE},
+                               {"Content-Location:",
+                                                  NULL, TRUE},
+                               {"Original-Message-ID:",
+                                                  NULL, TRUE},
+                               {"Disposition:",
+                                                  NULL, TRUE},
                                {NULL,             NULL, FALSE}};
        gchar *p, *tmp;
        gchar *boundary;
@@ -1389,6 +1523,7 @@ void procmime_parse_multipart(MimeInfo *mimeinfo)
        gchar buf[BUFFSIZE];
        FILE *fp;
        int result = 0;
+       gboolean done = FALSE;
 
        boundary = g_hash_table_lookup(mimeinfo->typeparameters, "boundary");
        if (!boundary)
@@ -1404,43 +1539,36 @@ void procmime_parse_multipart(MimeInfo *mimeinfo)
        }
        fseek(fp, mimeinfo->offset, SEEK_SET);
        while ((p = fgets(buf, sizeof(buf), fp)) != NULL && result == 0) {
-               if (ftell(fp) > (mimeinfo->offset + mimeinfo->length))
+               if (ftell(fp) - 1 > (mimeinfo->offset + mimeinfo->length))
                        break;
 
                if (IS_BOUNDARY(buf, boundary, boundary_len)) {
                        if (lastoffset != -1) {
+                               gint len = (ftell(fp) - strlen(buf)) - lastoffset - 1;
+                               if (len < 0)
+                                       len = 0;
                                result = procmime_parse_mimepart(mimeinfo,
                                                        hentry[0].body, hentry[1].body,
                                                        hentry[2].body, hentry[3].body, 
-                                                       hentry[4].body, 
+                                                       hentry[4].body, hentry[5].body,
+                                                       hentry[6].body, hentry[7].body,
                                                        mimeinfo->data.filename, lastoffset,
-                                                       (ftell(fp) - strlen(buf)) - lastoffset - 1);
-                       }
+                                                       len, short_scan);
+                               if (result == 1 && short_scan) {
+                                       done = TRUE;
+                                       break;
+                               }
+                       } 
                        
                        if (buf[2 + boundary_len]     == '-' &&
-                           buf[2 + boundary_len + 1] == '-')
+                           buf[2 + boundary_len + 1] == '-') {
                                break;
-
+                       }
                        for (i = 0; i < (sizeof hentry / sizeof hentry[0]) ; i++) {
                                g_free(hentry[i].body);
                                hentry[i].body = NULL;
                        }
-                       procheader_get_header_fields(fp, hentry);
-                        if (hentry[0].body != NULL) {
-                                tmp = conv_unmime_header(hentry[0].body, NULL);
-                                g_free(hentry[0].body);
-                                hentry[0].body = tmp;
-                        }                
-                        if (hentry[2].body != NULL) {
-                                tmp = conv_unmime_header(hentry[2].body, NULL);
-                                g_free(hentry[2].body);
-                                hentry[2].body = tmp;
-                        }                
-                        if (hentry[4].body != NULL) {
-                                tmp = conv_unmime_header(hentry[4].body, NULL);
-                                g_free(hentry[4].body);
-                                hentry[4].body = tmp;
-                        }                
+                       GET_HEADERS();
                        lastoffset = ftell(fp);
                }
        }
@@ -1460,7 +1588,7 @@ static void parse_parameters(const gchar *parameters, GHashTable *table)
        param = params;
        next = params;
        for (; next != NULL; param = next) {
-               gchar *attribute, *value, *tmp;
+               gchar *attribute, *value, *tmp, *down_attr, *orig_down_attr;
                gint len;
                gboolean convert = FALSE;
 
@@ -1479,15 +1607,18 @@ static void parse_parameters(const gchar *parameters, GHashTable *table)
 
                value[0] = '\0';
                value++;
+               while (value[0] == ' ')
+                       value++;
 
-               g_strdown(attribute);
-
-               len = strlen(attribute);
-               if (attribute[len - 1] == '*') {
+               down_attr = g_utf8_strdown(attribute, -1);
+               orig_down_attr = down_attr;
+       
+               len = strlen(down_attr);
+               if (down_attr[len - 1] == '*') {
                        gchar *srcpos, *dstpos, *endpos;
 
                        convert = TRUE;
-                       attribute[len - 1] = '\0';
+                       down_attr[len - 1] = '\0';
 
                        srcpos = value;
                        dstpos = value;
@@ -1515,28 +1646,41 @@ static void parse_parameters(const gchar *parameters, GHashTable *table)
                                *tmp = '\0';
                }
 
-               if (strrchr(attribute, '*') != NULL) {
+               if (down_attr) {
+                       while (down_attr[0] == ' ')
+                               down_attr++;
+                       while (down_attr[strlen(down_attr)-1] == ' ') 
+                               down_attr[strlen(down_attr)-1] = '\0';
+               } 
+               if (value) {
+                       while (value[0] == ' ')
+                               value++;
+                       while (value[strlen(value)-1] == ' ') 
+                               value[strlen(value)-1] = '\0';
+               }               
+               if (strrchr(down_attr, '*') != NULL) {
                        gchar *tmpattr;
 
-                       tmpattr = g_strdup(attribute);
+                       tmpattr = g_strdup(down_attr);
                        tmp = strrchr(tmpattr, '*');
                        tmp[0] = '\0';
 
                        if ((tmp[1] == '0') && (tmp[2] == '\0') && 
-                           (g_slist_find_custom(concatlist, attribute, g_str_equal) == NULL))
+                           (g_slist_find_custom(concatlist, down_attr, g_str_equal) == NULL))
                                concatlist = g_slist_prepend(concatlist, g_strdup(tmpattr));
 
-                       if (convert && (g_slist_find_custom(convlist, attribute, g_str_equal) == NULL))
+                       if (convert && (g_slist_find_custom(convlist, down_attr, g_str_equal) == NULL))
                                convlist = g_slist_prepend(convlist, g_strdup(tmpattr));
 
                        g_free(tmpattr);
                } else if (convert) {
-                       if (g_slist_find_custom(convlist, attribute, g_str_equal) == NULL)
-                               convlist = g_slist_prepend(convlist, g_strdup(attribute));
+                       if (g_slist_find_custom(convlist, down_attr, g_str_equal) == NULL)
+                               convlist = g_slist_prepend(convlist, g_strdup(down_attr));
                }
 
-               if (g_hash_table_lookup(table, attribute) == NULL)
-                       g_hash_table_insert(table, g_strdup(attribute), g_strdup(value));
+               if (g_hash_table_lookup(table, down_attr) == NULL)
+                       g_hash_table_insert(table, g_strdup(down_attr), g_strdup(value));
+               g_free(orig_down_attr);
        }
 
        for (cur = concatlist; cur != NULL; cur = g_slist_next(cur)) {
@@ -1568,7 +1712,8 @@ static void parse_parameters(const gchar *parameters, GHashTable *table)
                gchar *charset, *lang, *oldvalue, *newvalue;
 
                attribute = (gchar *) cur->data;
-               if (!g_hash_table_lookup_extended(table, attribute, (gpointer *) &key, (gpointer *) &value))
+               if (!g_hash_table_lookup_extended(
+                       table, attribute, (gpointer *)(gchar *) &key, (gpointer *)(gchar *) &value))
                        continue;
 
                charset = value;
@@ -1608,10 +1753,12 @@ static void procmime_parse_content_type(const gchar *content_type, MimeInfo *mim
                mimeinfo->type = MIMETYPE_TEXT;
                mimeinfo->subtype = g_strdup("plain");
                if (g_hash_table_lookup(mimeinfo->typeparameters,
-                                      "charset") == NULL)
+                                      "charset") == NULL) {
                        g_hash_table_insert(mimeinfo->typeparameters,
-                                           g_strdup("charset"),
-                                           g_strdup("us-ascii"));
+                                   g_strdup("charset"),
+                                   g_strdup(
+                                       conv_get_locale_charset_str_no_utf8()));
+               }
        } else {
                gchar *type, *subtype, *params;
 
@@ -1624,7 +1771,7 @@ static void procmime_parse_content_type(const gchar *content_type, MimeInfo *mim
                }
 
                mimeinfo->type = procmime_get_media_type(type);
-               mimeinfo->subtype = g_strdup(subtype);
+               mimeinfo->subtype = g_strstrip(g_strdup(subtype));
 
                /* Get mimeinfo->typeparameters */
                if (params != NULL)
@@ -1676,17 +1823,55 @@ static void procmime_parse_content_encoding(const gchar *content_encoding, MimeI
        return;
 }
 
-int procmime_parse_mimepart(MimeInfo *parent,
+static GSList *registered_parsers = NULL;
+
+static MimeParser *procmime_get_mimeparser_for_type(MimeMediaType type, const gchar *sub_type)
+{
+       GSList *cur;
+       for (cur = registered_parsers; cur; cur = cur->next) {
+               MimeParser *parser = (MimeParser *)cur->data;
+               if (parser->type == type && !strcmp2(parser->sub_type, sub_type))
+                       return parser;
+       }
+       return NULL;
+}
+
+void procmime_mimeparser_register(MimeParser *parser)
+{
+       if (!procmime_get_mimeparser_for_type(parser->type, parser->sub_type))
+               registered_parsers = g_slist_append(registered_parsers, parser);
+}
+
+
+void procmime_mimeparser_unregister(MimeParser *parser) 
+{
+       registered_parsers = g_slist_remove(registered_parsers, parser);
+}
+
+static gboolean procmime_mimeparser_parse(MimeParser *parser, MimeInfo *mimeinfo)
+{
+       g_return_val_if_fail(parser->parse != NULL, FALSE);
+       return parser->parse(parser, mimeinfo); 
+}
+
+static int procmime_parse_mimepart(MimeInfo *parent,
                             gchar *content_type,
                             gchar *content_encoding,
                             gchar *content_description,
                             gchar *content_id,
                             gchar *content_disposition,
+                            gchar *content_location,
+                            const gchar *original_msgid,
+                            const gchar *disposition_notification_hdr,
                             const gchar *filename,
                             guint offset,
-                            guint length)
+                            guint length,
+                            gboolean short_scan)
 {
        MimeInfo *mimeinfo;
+       MimeParser *parser = NULL;
+       gboolean parsed = FALSE;
+       int result = 0;
 
        /* Create MimeInfo */
        mimeinfo = procmime_mimeinfo_new();
@@ -1712,8 +1897,12 @@ int procmime_parse_mimepart(MimeInfo *parent,
                mimeinfo->type = MIMETYPE_TEXT;
                mimeinfo->subtype = g_strdup("plain");
                if (g_hash_table_lookup(mimeinfo->typeparameters,
-                                      "charset") == NULL)
-                       g_hash_table_insert(mimeinfo->typeparameters, g_strdup("charset"), g_strdup("us-ascii"));
+                                      "charset") == NULL) {
+                       g_hash_table_insert(mimeinfo->typeparameters,
+                                   g_strdup("charset"),
+                                   g_strdup(
+                                       conv_get_locale_charset_str_no_utf8()));
+               }
        }
 
        if (content_encoding != NULL) {
@@ -1732,28 +1921,56 @@ int procmime_parse_mimepart(MimeInfo *parent,
        else
                mimeinfo->id = NULL;
 
+       if (content_location != NULL)
+               mimeinfo->location = g_strdup(content_location);
+       else
+               mimeinfo->location = NULL;
+
        if (content_disposition != NULL) 
                procmime_parse_content_disposition(content_disposition, mimeinfo);
        else
                mimeinfo->disposition = DISPOSITIONTYPE_UNKNOWN;
 
        /* Call parser for mime type */
-       switch (mimeinfo->type) {
+       if ((parser = procmime_get_mimeparser_for_type(mimeinfo->type, mimeinfo->subtype)) != NULL) {
+               parsed = procmime_mimeparser_parse(parser, mimeinfo);
+       } 
+       if (!parsed) {
+               switch (mimeinfo->type) {
+               case MIMETYPE_TEXT:
+                       if (g_ascii_strcasecmp(mimeinfo->subtype, "plain") == 0 && short_scan) {
+                               return 1;
+                       }
+                       break;
+
                case MIMETYPE_MESSAGE:
                        if (g_ascii_strcasecmp(mimeinfo->subtype, "rfc822") == 0) {
-                               procmime_parse_message_rfc822(mimeinfo);
+                               procmime_parse_message_rfc822(mimeinfo, short_scan);
+                       }
+                       if (g_ascii_strcasecmp(mimeinfo->subtype, "disposition-notification") == 0) {
+                               procmime_parse_disposition_notification(mimeinfo, 
+                                       original_msgid, disposition_notification_hdr, short_scan);
                        }
                        break;
                        
                case MIMETYPE_MULTIPART:
-                       procmime_parse_multipart(mimeinfo);
+                       procmime_parse_multipart(mimeinfo, short_scan);
+                       break;
+               
+               case MIMETYPE_APPLICATION:
+                       if (g_ascii_strcasecmp(mimeinfo->subtype, "octet-stream") == 0
+                       && original_msgid && *original_msgid 
+                       && disposition_notification_hdr && *disposition_notification_hdr) {
+                               procmime_parse_disposition_notification(mimeinfo, 
+                                       original_msgid, disposition_notification_hdr, short_scan);
+                       }
                        break;
-                       
                default:
                        break;
+               }
        }
 
-       return 0;
+       return result;
 }
 
 static gchar *typenames[] = {
@@ -1774,8 +1991,8 @@ static gboolean output_func(GNode *node, gpointer data)
 
        depth = g_node_depth(node);
        for (i = 0; i < depth; i++)
-               printf("    ");
-       printf("%s/%s (offset:%d length:%d encoding: %d)\n", typenames[mimeinfo->type], mimeinfo->subtype, mimeinfo->offset, mimeinfo->length, mimeinfo->encoding_type);
+               g_print("    ");
+       g_print("%s/%s (offset:%d length:%d encoding: %d)\n", typenames[mimeinfo->type], mimeinfo->subtype, mimeinfo->offset, mimeinfo->length, mimeinfo->encoding_type);
 
        return FALSE;
 }
@@ -1785,7 +2002,7 @@ static void output_mime_structure(MimeInfo *mimeinfo, int indent)
        g_node_traverse(mimeinfo->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1, output_func, NULL);
 }
 
-MimeInfo *procmime_scan_file_with_offset(const gchar *filename, int offset)
+static MimeInfo *procmime_scan_file_with_offset(const gchar *filename, int offset, gboolean short_scan)
 {
        MimeInfo *mimeinfo;
        struct stat buf;
@@ -1801,25 +2018,35 @@ MimeInfo *procmime_scan_file_with_offset(const gchar *filename, int offset)
        mimeinfo->offset = offset;
        mimeinfo->length = buf.st_size - offset;
 
-       procmime_parse_message_rfc822(mimeinfo);
+       procmime_parse_message_rfc822(mimeinfo, short_scan);
        if (debug_get_mode())
                output_mime_structure(mimeinfo, 0);
 
        return mimeinfo;
 }
 
-MimeInfo *procmime_scan_file(const gchar *filename)
+static MimeInfo *procmime_scan_file_full(const gchar *filename, gboolean short_scan)
 {
        MimeInfo *mimeinfo;
 
        g_return_val_if_fail(filename != NULL, NULL);
 
-       mimeinfo = procmime_scan_file_with_offset(filename, 0);
+       mimeinfo = procmime_scan_file_with_offset(filename, 0, short_scan);
 
        return mimeinfo;
 }
 
-MimeInfo *procmime_scan_queue_file(const gchar *filename)
+MimeInfo *procmime_scan_file(const gchar *filename)
+{
+       return procmime_scan_file_full(filename, FALSE);
+}
+
+static MimeInfo *procmime_scan_file_short(const gchar *filename)
+{
+       return procmime_scan_file_full(filename, TRUE);
+}
+
+static MimeInfo *procmime_scan_queue_file_full(const gchar *filename, gboolean short_scan)
 {
        FILE *fp;
        MimeInfo *mimeinfo;
@@ -1832,16 +2059,42 @@ MimeInfo *procmime_scan_queue_file(const gchar *filename)
        if ((fp = g_fopen(filename, "rb")) == NULL)
                return NULL;
        /* Skip queue header */
-       while (fgets(buf, sizeof(buf), fp) != NULL)
+       while (fgets(buf, sizeof(buf), fp) != NULL) {
+               /* new way */
+               if ((!strncmp(buf, "X-Claws-End-Special-Headers: 1",
+                       strlen("X-Claws-End-Special-Headers:"))) ||
+                  (!strncmp(buf, "X-Sylpheed-End-Special-Headers: 1",
+                       strlen("X-Sylpheed-End-Special-Headers:"))))
+                       break;
+               /* old way */
                if (buf[0] == '\r' || buf[0] == '\n') break;
+               /* from other mailers */
+               if (!strncmp(buf, "Date: ", 6)
+               ||  !strncmp(buf, "To: ", 4)
+               ||  !strncmp(buf, "From: ", 6)
+               ||  !strncmp(buf, "Subject: ", 9)) {
+                       rewind(fp);
+                       break;
+               }
+       }
        offset = ftell(fp);
        fclose(fp);
 
-       mimeinfo = procmime_scan_file_with_offset(filename, offset);
+       mimeinfo = procmime_scan_file_with_offset(filename, offset, short_scan);
 
        return mimeinfo;
 }
 
+MimeInfo *procmime_scan_queue_file(const gchar *filename)
+{
+       return procmime_scan_queue_file_full(filename, FALSE);
+}
+
+static MimeInfo *procmime_scan_queue_file_short(const gchar *filename)
+{
+       return procmime_scan_queue_file_full(filename, TRUE);
+}
+
 typedef enum {
     ENC_AS_TOKEN,
     ENC_AS_QUOTED_STRING,
@@ -1853,6 +2106,7 @@ typedef struct _ParametersData {
        FILE *fp;
        guint len;
        guint ascii_only;
+       gint error;
 } ParametersData;
 
 static void write_parameters(gpointer key, gpointer value, gpointer user_data)
@@ -1943,17 +2197,25 @@ static void write_parameters(gpointer key, gpointer value, gpointer user_data)
        
        if (buf->str && strlen(buf->str)) {
                if (pdata->len + strlen(buf->str) + 2 > 76) {
-                       fprintf(pdata->fp, ";\n %s", buf->str);
+                       if (fprintf(pdata->fp, ";\n %s", buf->str) < 0)
+                               pdata->error = TRUE;
                        pdata->len = strlen(buf->str) + 1;
                } else {
-                       fprintf(pdata->fp, "; %s", buf->str);
+                       if (fprintf(pdata->fp, "; %s", buf->str) < 0)
+                               pdata->error = TRUE;
                        pdata->len += strlen(buf->str) + 2;
                }
        }
        g_string_free(buf, TRUE);
 }
 
-void procmime_write_mime_header(MimeInfo *mimeinfo, FILE *fp)
+#define TRY(func) { \
+       if (!(func)) { \
+               return -1; \
+       } \
+}
+
+int procmime_write_mime_header(MimeInfo *mimeinfo, FILE *fp)
 {
        struct TypeTable *type_table;
        ParametersData *pdata = g_new0(ParametersData, 1);
@@ -1961,30 +2223,41 @@ void procmime_write_mime_header(MimeInfo *mimeinfo, FILE *fp)
        
        pdata->fp = fp;
        pdata->ascii_only = FALSE;
-
+       pdata->error = FALSE;
        for (type_table = mime_type_table; type_table->str != NULL; type_table++)
                if (mimeinfo->type == type_table->type) {
                        gchar *buf = g_strdup_printf(
                                "Content-Type: %s/%s", type_table->str, mimeinfo->subtype);
-                       fprintf(fp, "%s", buf);
+                       if (fprintf(fp, "%s", buf) < 0) {
+                               g_free(buf);
+                               g_free(pdata);
+                               return -1;
+                       }
                        pdata->len = strlen(buf);
                        pdata->ascii_only = TRUE;
                        g_free(buf);
                        break;
                }
        g_hash_table_foreach(mimeinfo->typeparameters, write_parameters, pdata);
+       if (pdata->error == TRUE) {
+               g_free(pdata);
+               return -1;
+       }
        g_free(pdata);
 
-       fprintf(fp, "\n");
+       TRY(fprintf(fp, "\n") >= 0);
 
        if (mimeinfo->encoding_type != ENC_UNKNOWN)
-               fprintf(fp, "Content-Transfer-Encoding: %s\n", procmime_get_encoding_str(mimeinfo->encoding_type));
+               TRY(fprintf(fp, "Content-Transfer-Encoding: %s\n", procmime_get_encoding_str(mimeinfo->encoding_type)) >= 0);
 
        if (mimeinfo->description != NULL)
-               fprintf(fp, "Content-Description: %s\n", mimeinfo->description);
+               TRY(fprintf(fp, "Content-Description: %s\n", mimeinfo->description) >= 0);
 
        if (mimeinfo->id != NULL)
-               fprintf(fp, "Content-ID: %s\n", mimeinfo->id);
+               TRY(fprintf(fp, "Content-ID: %s\n", mimeinfo->id) >= 0);
+
+       if (mimeinfo->location != NULL)
+               TRY(fprintf(fp, "Content-Location: %s\n", mimeinfo->location) >= 0);
 
        if (mimeinfo->disposition != DISPOSITIONTYPE_UNKNOWN) {
                ParametersData *pdata = g_new0(ParametersData, 1);
@@ -1996,28 +2269,39 @@ void procmime_write_mime_header(MimeInfo *mimeinfo, FILE *fp)
                else
                        buf = g_strdup("Content-Disposition: unknown");
 
-               fprintf(fp, "%s", buf);
+               if (fprintf(fp, "%s", buf) < 0) {
+                       g_free(buf);
+                       g_free(pdata);
+                       return -1;
+               }
                pdata->len = strlen(buf);
                g_free(buf);
 
                pdata->fp = fp;
                pdata->ascii_only = FALSE;
-
+               pdata->error = FALSE;
                g_hash_table_foreach(mimeinfo->dispositionparameters, write_parameters, pdata);
+               if (pdata->error == TRUE) {
+                       g_free(pdata);
+                       return -1;
+               }
                g_free(pdata);
-               fprintf(fp, "\n");
+               TRY(fprintf(fp, "\n") >= 0);
        }
 
-       fprintf(fp, "\n");
+       TRY(fprintf(fp, "\n") >= 0);
+       
+       return 0;
 }
 
-gint procmime_write_message_rfc822(MimeInfo *mimeinfo, FILE *fp)
+static gint procmime_write_message_rfc822(MimeInfo *mimeinfo, FILE *fp)
 {
        FILE *infp;
        GNode *childnode;
        MimeInfo *child;
        gchar buf[BUFFSIZE];
        gboolean skip = FALSE;;
+       size_t len;
 
        debug_print("procmime_write_message_rfc822\n");
 
@@ -2030,6 +2314,7 @@ gint procmime_write_message_rfc822(MimeInfo *mimeinfo, FILE *fp)
                }
                fseek(infp, mimeinfo->offset, SEEK_SET);
                while (fgets(buf, sizeof(buf), infp) == buf) {
+                       strcrchomp(buf);
                        if (buf[0] == '\n' && buf[1] == '\0')
                                break;
                        if (skip && (buf[0] == ' ' || buf[0] == '\t'))
@@ -2039,21 +2324,28 @@ gint procmime_write_message_rfc822(MimeInfo *mimeinfo, FILE *fp)
                            g_ascii_strncasecmp(buf, "Content-Transfer-Encoding:", 26) == 0 ||
                            g_ascii_strncasecmp(buf, "Content-Description:", 20) == 0 ||
                            g_ascii_strncasecmp(buf, "Content-ID:", 11) == 0 ||
+                           g_ascii_strncasecmp(buf, "Content-Location:", 17) == 0 ||
                            g_ascii_strncasecmp(buf, "Content-Disposition:", 20) == 0) {
                                skip = TRUE;
                                continue;
                        }
-                       fwrite(buf, sizeof(gchar), strlen(buf), fp);
+                       len = strlen(buf);
+                       if (fwrite(buf, sizeof(gchar), len, fp) < len) {
+                               g_warning("failed to dump %zd bytes from file", len);
+                               fclose(infp);
+                               return -1;
+                       }
                        skip = FALSE;
                }
                fclose(infp);
                break;
 
        case MIMECONTENT_MEM:
-               fwrite(mimeinfo->data.mem, 
-                               sizeof(gchar), 
-                               strlen(mimeinfo->data.mem), 
-                               fp);
+               len = strlen(mimeinfo->data.mem);
+               if (fwrite(mimeinfo->data.mem, sizeof(gchar), len, fp) < len) {
+                       g_warning("failed to dump %zd bytes from mem", len);
+                       return -1;
+               }
                break;
 
        default:
@@ -2065,18 +2357,23 @@ gint procmime_write_message_rfc822(MimeInfo *mimeinfo, FILE *fp)
                return -1;
 
        child = (MimeInfo *) childnode->data;
-       fprintf(fp, "Mime-Version: 1.0\n");
-       procmime_write_mime_header(child, fp);
+       if (fprintf(fp, "Mime-Version: 1.0\n") < 0) {
+               g_warning("failed to write mime version");
+               return -1;
+       }
+       if (procmime_write_mime_header(child, fp) < 0)
+               return -1;
        return procmime_write_mimeinfo(child, fp);
 }
 
-gint procmime_write_multipart(MimeInfo *mimeinfo, FILE *fp)
+static gint procmime_write_multipart(MimeInfo *mimeinfo, FILE *fp)
 {
        FILE *infp;
        GNode *childnode;
        gchar *boundary, *str, *str2;
        gchar buf[BUFFSIZE];
        gboolean firstboundary;
+       size_t len;
 
        debug_print("procmime_write_multipart\n");
 
@@ -2092,7 +2389,12 @@ gint procmime_write_multipart(MimeInfo *mimeinfo, FILE *fp)
                while (fgets(buf, sizeof(buf), infp) == buf) {
                        if (IS_BOUNDARY(buf, boundary, strlen(boundary)))
                                break;
-                       fwrite(buf, sizeof(gchar), strlen(buf), fp);
+                       len = strlen(buf);
+                       if (fwrite(buf, sizeof(gchar), len, fp) < len) {
+                               g_warning("failed to write %zd", len);
+                               fclose(infp);
+                               return -1;
+                       }
                }
                fclose(infp);
                break;
@@ -2102,7 +2404,12 @@ gint procmime_write_multipart(MimeInfo *mimeinfo, FILE *fp)
                if (((str2 = strstr(str, boundary)) != NULL) && ((str2 - str) >= 2) &&
                    (*(str2 - 1) == '-') && (*(str2 - 2) == '-'))
                        *(str2 - 2) = '\0';
-               fwrite(str, sizeof(gchar), strlen(str), fp);
+               len = strlen(str);
+               if (fwrite(str, sizeof(gchar), len, fp) < len) {
+                       g_warning("failed to write %zd from mem", len);
+                       g_free(str);
+                       return -1;
+               }
                g_free(str);
                break;
 
@@ -2118,16 +2425,18 @@ gint procmime_write_multipart(MimeInfo *mimeinfo, FILE *fp)
                if (firstboundary)
                        firstboundary = FALSE;
                else
-                       fprintf(fp, "\n");
-               fprintf(fp, "--%s\n", boundary);
+                       TRY(fprintf(fp, "\n") >= 0);
+                       
+               TRY(fprintf(fp, "--%s\n", boundary) >= 0);
 
-               procmime_write_mime_header(child, fp);
+               if (procmime_write_mime_header(child, fp) < 0)
+                       return -1;
                if (procmime_write_mimeinfo(child, fp) < 0)
                        return -1;
 
                childnode = g_node_next_sibling(childnode);
        }       
-       fprintf(fp, "\n--%s--\n", boundary);
+       TRY(fprintf(fp, "\n--%s--\n", boundary) >= 0);
 
        return 0;
 }
@@ -2135,7 +2444,7 @@ gint procmime_write_multipart(MimeInfo *mimeinfo, FILE *fp)
 gint procmime_write_mimeinfo(MimeInfo *mimeinfo, FILE *fp)
 {
        FILE *infp;
-
+       size_t len;
        debug_print("procmime_write_mimeinfo\n");
 
        if (G_NODE_IS_LEAF(mimeinfo->node)) {
@@ -2150,10 +2459,9 @@ gint procmime_write_mimeinfo(MimeInfo *mimeinfo, FILE *fp)
                        return 0;
 
                case MIMECONTENT_MEM:
-                       fwrite(mimeinfo->data.mem, 
-                                       sizeof(gchar), 
-                                       strlen(mimeinfo->data.mem), 
-                                       fp);
+                       len = strlen(mimeinfo->data.mem);
+                       if (fwrite(mimeinfo->data.mem, sizeof(gchar), len, fp) < len)
+                               return -1;
                        return 0;
 
                default:
@@ -2163,8 +2471,9 @@ gint procmime_write_mimeinfo(MimeInfo *mimeinfo, FILE *fp)
                /* Call writer for mime type */
                switch (mimeinfo->type) {
                case MIMETYPE_MESSAGE:
-                       if (g_ascii_strcasecmp(mimeinfo->subtype, "rfc822") == 0)
+                       if (g_ascii_strcasecmp(mimeinfo->subtype, "rfc822") == 0) {
                                return procmime_write_message_rfc822(mimeinfo, fp);
+                       }
                        break;
                        
                case MIMETYPE_MULTIPART: