don't get a fright when encountering an unknown Content-Type
[claws.git] / src / procmime.c
index f33eff8aae113f484234d4ea3445a34f5039e8fc..0a40426877ac66f833eef8972914fec2ac1dcf05 100644 (file)
@@ -1,6 +1,6 @@
 /*
- * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2012 Hiroyuki Yamamoto & The Claws Mail Team
+ * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
+ * Copyright (C) 1999-2020 the Claws Mail Team and Hiroyuki Yamamoto
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -14,7 +14,6 @@
  *
  * You should have received a copy of the GNU General Public License
  * along with this program. If not, see <http://www.gnu.org/licenses/>.
- * 
  */
 
 #ifdef HAVE_CONFIG_H
@@ -22,7 +21,6 @@
 #include "claws-features.h"
 #endif
 
-#define _GNU_SOURCE
 #include <stdio.h>
 
 #include "defs.h"
@@ -41,7 +39,6 @@
 
 #include "procmime.h"
 #include "procheader.h"
-#include "base64.h"
 #include "quoted-printable.h"
 #include "uuencode.h"
 #include "unmime.h"
@@ -54,6 +51,8 @@
 #include "alertpanel.h"
 #include "timing.h"
 #include "privacy.h"
+#include "account.h"
+#include "file-utils.h"
 
 static GHashTable *procmime_get_mime_type_table        (void);
 static MimeInfo *procmime_scan_file_short(const gchar *filename);
@@ -65,6 +64,7 @@ MimeInfo *procmime_mimeinfo_new(void)
        MimeInfo *mimeinfo;
 
        mimeinfo = g_new0(MimeInfo, 1);
+
        mimeinfo->content        = MIMECONTENT_EMPTY;
        mimeinfo->data.filename  = NULL;
 
@@ -144,8 +144,9 @@ static gboolean free_func(GNode *node, gpointer data)
        return FALSE;
 }
 
-void procmime_mimeinfo_free_all(MimeInfo *mimeinfo)
+void procmime_mimeinfo_free_all(MimeInfo **mimeinfo_ptr)
 {
+       MimeInfo *mimeinfo = *mimeinfo_ptr;
        GNode *node;
 
        if (!mimeinfo)
@@ -155,6 +156,8 @@ void procmime_mimeinfo_free_all(MimeInfo *mimeinfo)
        g_node_traverse(node, G_IN_ORDER, G_TRAVERSE_ALL, -1, free_func, NULL);
 
        g_node_destroy(node);
+
+       *mimeinfo_ptr = NULL;
 }
 
 MimeInfo *procmime_mimeinfo_parent(MimeInfo *mimeinfo)
@@ -254,55 +257,22 @@ const gchar *procmime_mimeinfo_get_parameter(MimeInfo *mimeinfo, const gchar *na
        return value;
 }
 
-#ifdef HAVE_FGETS_UNLOCKED
-#define SC_FGETS fgets_unlocked
-#define SC_FPUTS fputs_unlocked
-#define SC_FPUTC fputc_unlocked
-#define SC_FREAD fread_unlocked
-#define SC_FWRITE fwrite_unlocked
-#define SC_FEOF feof_unlocked
-#define SC_FERROR ferror_unlocked
-
-static FILE *procmime_fopen(const gchar *file, const gchar *mode)
-{
-       FILE *fp = g_fopen(file, mode);
-       if (!fp)
-               return NULL;
-       flockfile(fp);
-       return fp;
-}
-static int procmime_fclose(FILE *fp)
-{
-       funlockfile(fp);
-       return fclose(fp);
-}
-#else
-#define SC_FGETS fgets
-#define SC_FPUTS fputs
-#define SC_FPUTC fputc
-#define SC_FREAD fread
-#define SC_FWRITE fwrite
-#define SC_FEOF feof
-#define SC_FERROR ferror
-#define procmime_fopen g_fopen
-#define procmime_fclose fclose
-#endif
-
 #define FLUSH_LASTLINE() {                                                     \
        if (*lastline != '\0') {                                                \
                gint llen = 0;                                                  \
                strretchomp(lastline);                                          \
                llen = strlen(lastline);                                        \
-               if (lastline[llen-1] == ' ' && strcmp(lastline,"-- ")) {        \
+               if (lastline[llen-1] == ' ' && !account_sigsep_matchlist_str_found(lastline, "%s") &&   \
+                   !(llen == 2 && lastline[1] == ' ' && strchr(prefs_common.quote_chars, lastline[0]))) {                                      \
                        /* this is flowed */                                    \
                        if (delsp)                                              \
                                lastline[llen-1] = '\0';                        \
-                       if (SC_FPUTS(lastline, outfp) == EOF)                   \
+                       if (claws_fputs(lastline, outfp) == EOF)                        \
                                err = TRUE;                                     \
                } else {                                                        \
-                       if (SC_FPUTS(lastline, outfp) == EOF)                   \
+                       if (claws_fputs(lastline, outfp) == EOF)                        \
                                err = TRUE;                                     \
-                       if (SC_FPUTS("\n", outfp) == EOF)                               \
+                       if (claws_fputs("\n", outfp) == EOF)                            \
                                err = TRUE;                                     \
                }                                                               \
        }                                                                       \
@@ -315,19 +285,21 @@ gboolean procmime_decode_content(MimeInfo *mimeinfo)
        gint readend;
        gchar *tmpfilename;
        FILE *outfp, *infp;
-       struct stat statbuf;
+       GStatBuf statbuf;
        gboolean tmp_file = FALSE;
        gboolean flowed = FALSE;
        gboolean delsp = FALSE; 
        gboolean err = FALSE;
+       gint state = 0;
+       guint save = 0;
+
+       cm_return_val_if_fail(mimeinfo != NULL, FALSE);
 
        EncodingType encoding = forced_encoding 
                                ? forced_encoding
                                : mimeinfo->encoding_type;
        gchar lastline[BUFFSIZE];
        memset(lastline, 0, BUFFSIZE);
-                  
-       cm_return_val_if_fail(mimeinfo != NULL, FALSE);
 
        if (prefs_common.respect_flowed_format &&
            mimeinfo->type == MIMETYPE_TEXT && 
@@ -349,29 +321,43 @@ gboolean procmime_decode_content(MimeInfo *mimeinfo)
            ))
                return TRUE;
 
-       infp = procmime_fopen(mimeinfo->data.filename, "rb");
+       if (mimeinfo->type == MIMETYPE_MULTIPART || mimeinfo->type == MIMETYPE_MESSAGE)
+               return TRUE;
+
+       if (mimeinfo->data.filename == NULL)
+               return FALSE;
+
+       infp = claws_fopen(mimeinfo->data.filename, "rb");
        if (!infp) {
-               perror("fopen");
+               FILE_OP_ERROR(mimeinfo->data.filename, "claws_fopen");
+               return FALSE;
+       }
+       if (fseek(infp, mimeinfo->offset, SEEK_SET) < 0) {
+               FILE_OP_ERROR(mimeinfo->data.filename, "fseek");
+               claws_fclose(infp);
                return FALSE;
        }
-       fseek(infp, mimeinfo->offset, SEEK_SET);
 
        outfp = get_tmpfile_in_dir(get_mime_tmp_dir(), &tmpfilename);
        if (!outfp) {
                perror("tmpfile");
-               procmime_fclose(infp);
+               claws_fclose(infp);
                return FALSE;
        }
+
        tmp_file = TRUE;
        readend = mimeinfo->offset + mimeinfo->length;
 
+       account_sigsep_matchlist_create(); /* FLUSH_LASTLINE will use it */
+
+       *buf = '\0';
        if (encoding == ENC_QUOTED_PRINTABLE) {
-               while ((ftell(infp) < readend) && (SC_FGETS(buf, sizeof(buf), infp) != NULL)) {
+               while ((ftell(infp) < readend) && (claws_fgets(buf, sizeof(buf), infp) != NULL)) {
                        gint len;
                        len = qp_decode_line(buf);
-                       buf[len]='\0';
+                       buf[len] = '\0';
                        if (!flowed) {
-                               if (SC_FWRITE(buf, 1, len, outfp) < len)
+                               if (claws_fwrite(buf, 1, len, outfp) < len)
                                        err = TRUE;
                        } else {
                                FLUSH_LASTLINE();
@@ -380,12 +366,11 @@ gboolean procmime_decode_content(MimeInfo *mimeinfo)
                if (flowed)
                        FLUSH_LASTLINE();
        } else if (encoding == ENC_BASE64) {
-               gchar outbuf[BUFFSIZE];
+               gchar outbuf[BUFFSIZE + 1];
                gint len, inlen, inread;
-               Base64Decoder *decoder;
                gboolean got_error = FALSE;
                gboolean uncanonicalize = FALSE;
-               FILE *tmpfp = outfp;
+               FILE *tmpfp = NULL;
                gboolean null_bytes = FALSE;
                gboolean starting = TRUE;
 
@@ -394,29 +379,27 @@ gboolean procmime_decode_content(MimeInfo *mimeinfo)
                        uncanonicalize = TRUE;
                        tmpfp = my_tmpfile();
                        if (!tmpfp) {
-                               perror("tmpfile");
+                               perror("my_tmpfile");
                                if (tmp_file) 
-                                       procmime_fclose(outfp);
-                               procmime_fclose(infp);
+                                       claws_fclose(outfp);
+                               claws_fclose(infp);
                                return FALSE;
                        }
-#ifdef HAVE_FGETS_UNLOCKED
-                       flockfile(tmpfp);
-#endif
-               }
+               } else
+                       tmpfp = outfp;
 
-               decoder = base64_decoder_new();
                while ((inlen = MIN(readend - ftell(infp), sizeof(buf))) > 0 && !err) {
-                       inread = SC_FREAD(buf, 1, inlen, infp);
-                       len = base64_decoder_decode(decoder, buf, outbuf, inread);
+                       inread = claws_fread(buf, 1, inlen, infp);
+                       memset(outbuf, 0, sizeof(buf));
+                       len = g_base64_decode_step(buf, inlen, outbuf, &state, &save);
                        if (uncanonicalize == TRUE && strlen(outbuf) < len && starting) {
                                uncanonicalize = FALSE;
                                null_bytes = TRUE;
                        }
                        starting = FALSE;
                        if (((inread != inlen) || len < 0) && !got_error) {
-                               g_warning("Bad BASE64 content.\n");
-                               if (SC_FWRITE(_("[Error decoding BASE64]\n"),
+                               g_warning("Bad BASE64 content.");
+                               if (claws_fwrite(_("[Error decoding BASE64]\n"),
                                        sizeof(gchar),
                                        strlen(_("[Error decoding BASE64]\n")),
                                        tmpfp) < strlen(_("[Error decoding BASE64]\n")))
@@ -428,50 +411,51 @@ gboolean procmime_decode_content(MimeInfo *mimeinfo)
                                 * per block */
                                if (null_bytes) {
                                        /* we won't uncanonicalize, output to outfp directly */
-                                       if (SC_FWRITE(outbuf, sizeof(gchar), len, outfp) < len)
+                                       if (claws_fwrite(outbuf, sizeof(gchar), len, outfp) < len)
                                                err = TRUE;
                                } else {
-                                       if (SC_FWRITE(outbuf, sizeof(gchar), len, tmpfp) < len)
+                                       if (claws_fwrite(outbuf, sizeof(gchar), len, tmpfp) < len)
                                                err = TRUE;
                                }
                                got_error = FALSE;
                        }
                }
-               base64_decoder_free(decoder);
 
                if (uncanonicalize) {
                        rewind(tmpfp);
-                       while (SC_FGETS(buf, sizeof(buf), tmpfp) != NULL) {
+                       while (claws_fgets(buf, sizeof(buf), tmpfp) != NULL) {
                                strcrchomp(buf);
-                               if (SC_FPUTS(buf, outfp) == EOF)
+                               if (claws_fputs(buf, outfp) == EOF)
                                        err = TRUE;
                        }
-                       procmime_fclose(tmpfp);
+               }
+               if (tmpfp != outfp) {
+                       claws_fclose(tmpfp);
                }
        } else if (encoding == ENC_X_UUENCODE) {
                gchar outbuf[BUFFSIZE];
                gint len;
                gboolean flag = FALSE;
 
-               while ((ftell(infp) < readend) && (SC_FGETS(buf, sizeof(buf), infp) != NULL)) {
+               while ((ftell(infp) < readend) && (claws_fgets(buf, sizeof(buf), infp) != NULL)) {
                        if (!flag && strncmp(buf,"begin ", 6)) continue;
 
                        if (flag) {
                                len = fromuutobits(outbuf, buf);
                                if (len <= 0) {
                                        if (len < 0) 
-                                               g_warning("Bad UUENCODE content(%d)\n", len);
+                                               g_warning("Bad UUENCODE content (%d)", len);
                                        break;
                                }
-                               if (SC_FWRITE(outbuf, sizeof(gchar), len, outfp) < len)
+                               if (claws_fwrite(outbuf, sizeof(gchar), len, outfp) < len)
                                        err = TRUE;
                        } else
                                flag = TRUE;
                }
        } else {
-               while ((ftell(infp) < readend) && (SC_FGETS(buf, sizeof(buf), infp) != NULL)) {
+               while ((ftell(infp) < readend) && (claws_fgets(buf, sizeof(buf), infp) != NULL)) {
                        if (!flowed) {
-                               if (SC_FPUTS(buf, outfp) == EOF)
+                               if (claws_fputs(buf, outfp) == EOF)
                                        err = TRUE;
                        } else {
                                FLUSH_LASTLINE();
@@ -483,15 +467,21 @@ gboolean procmime_decode_content(MimeInfo *mimeinfo)
                        g_warning("write error");
        }
 
-       procmime_fclose(outfp);
-       procmime_fclose(infp);
+       claws_fclose(outfp);
+       claws_fclose(infp);
+
+       account_sigsep_matchlist_delete();
 
        if (err == TRUE) {
                return FALSE;
        }
 
-       g_stat(tmpfilename, &statbuf);
-       if (mimeinfo->tmp && (mimeinfo->data.filename != NULL))
+       if (g_stat(tmpfilename, &statbuf) < 0) {
+               FILE_OP_ERROR(tmpfilename, "stat");
+               return FALSE;
+       }
+
+       if (mimeinfo->tmp)
                claws_unlink(mimeinfo->data.filename);
        g_free(mimeinfo->data.filename);
        mimeinfo->data.filename = tmpfilename;
@@ -511,7 +501,7 @@ gboolean procmime_encode_content(MimeInfo *mimeinfo, EncodingType encoding)
        FILE *infp = NULL, *outfp;
        gint len;
        gchar *tmpfilename;
-       struct stat statbuf;
+       GStatBuf statbuf;
        gboolean err = FALSE;
 
        if (mimeinfo->content == MIMECONTENT_EMPTY)
@@ -529,30 +519,27 @@ gboolean procmime_encode_content(MimeInfo *mimeinfo, EncodingType encoding)
                perror("tmpfile");
                return FALSE;
        }
-#ifdef HAVE_FGETS_UNLOCKED
-       flockfile(outfp);
-#endif
 
        if (mimeinfo->content == MIMECONTENT_FILE && mimeinfo->data.filename) {
-               if ((infp = procmime_fopen(mimeinfo->data.filename, "rb")) == NULL) {
-                       g_warning("Can't open file %s\n", mimeinfo->data.filename);
-                       procmime_fclose(outfp);
+               if ((infp = claws_fopen(mimeinfo->data.filename, "rb")) == NULL) {
+                       g_warning("Can't open file %s", mimeinfo->data.filename);
+                       claws_fclose(outfp);
                        return FALSE;
                }
        } else if (mimeinfo->content == MIMECONTENT_MEM) {
                infp = str_open_as_stream(mimeinfo->data.mem);
                if (infp == NULL) {
-                       procmime_fclose(outfp);
+                       claws_fclose(outfp);
                        return FALSE;
                }
-#ifdef HAVE_FGETS_UNLOCKED
-               flockfile(infp);
-#endif
-
+       } else {
+               claws_fclose(outfp);
+               g_warning("Unknown mimeinfo");
+               return FALSE;
        }
 
        if (encoding == ENC_BASE64) {
-               gchar inbuf[B64_LINE_SIZE], outbuf[B64_BUFFSIZE];
+               gchar inbuf[B64_LINE_SIZE], *out;
                FILE *tmp_fp = infp;
                gchar *tmp_file = NULL;
 
@@ -562,60 +549,59 @@ gboolean procmime_encode_content(MimeInfo *mimeinfo, EncodingType encoding)
                                tmp_file = get_tmp_file();
                                if (canonicalize_file(mimeinfo->data.filename, tmp_file) < 0) {
                                        g_free(tmp_file);
-                                       procmime_fclose(infp);
-                                       procmime_fclose(outfp);
+                                       claws_fclose(infp);
+                                       claws_fclose(outfp);
                                        return FALSE;
                                }
-                               if ((tmp_fp = procmime_fopen(tmp_file, "rb")) == NULL) {
-                                       FILE_OP_ERROR(tmp_file, "fopen");
+                               if ((tmp_fp = claws_fopen(tmp_file, "rb")) == NULL) {
+                                       FILE_OP_ERROR(tmp_file, "claws_fopen");
                                        claws_unlink(tmp_file);
                                        g_free(tmp_file);
-                                       procmime_fclose(infp);
-                                       procmime_fclose(outfp);
+                                       claws_fclose(infp);
+                                       claws_fclose(outfp);
                                        return FALSE;
                                }
                        } else {
                                gchar *out = canonicalize_str(mimeinfo->data.mem);
-                               procmime_fclose(infp);
+                               claws_fclose(infp);
                                infp = str_open_as_stream(out);
                                tmp_fp = infp;
                                g_free(out);
                                if (infp == NULL) {
-                                       procmime_fclose(outfp);
+                                       claws_fclose(outfp);
                                        return FALSE;
                                }
-#ifdef HAVE_FGETS_UNLOCKED
-                               flockfile(infp);
-#endif
                        }
                }
 
-               while ((len = SC_FREAD(inbuf, sizeof(gchar),
+               while ((len = claws_fread(inbuf, sizeof(gchar),
                                    B64_LINE_SIZE, tmp_fp))
                       == B64_LINE_SIZE) {
-                       base64_encode(outbuf, inbuf, B64_LINE_SIZE);
-                       if (SC_FPUTS(outbuf, outfp) == EOF)
+                       out = g_base64_encode(inbuf, B64_LINE_SIZE);
+                       if (claws_fputs(out, outfp) == EOF)
                                err = TRUE;
-                       if (SC_FPUTC('\n', outfp) == EOF)
+                       g_free(out);
+                       if (claws_fputc('\n', outfp) == EOF)
                                err = TRUE;
                }
-               if (len > 0 && SC_FEOF(tmp_fp)) {
-                       base64_encode(outbuf, inbuf, len);
-                       if (SC_FPUTS(outbuf, outfp) == EOF)
+               if (len > 0 && claws_feof(tmp_fp)) {
+                       out = g_base64_encode(inbuf, len);
+                       if (claws_fputs(out, outfp) == EOF)
                                err = TRUE;
-                       if (SC_FPUTC('\n', outfp) == EOF)
+                       g_free(out);
+                       if (claws_fputc('\n', outfp) == EOF)
                                err = TRUE;
                }
 
                if (tmp_file) {
-                       procmime_fclose(tmp_fp);
+                       claws_fclose(tmp_fp);
                        claws_unlink(tmp_file);
                        g_free(tmp_file);
                }
        } else if (encoding == ENC_QUOTED_PRINTABLE) {
                gchar inbuf[BUFFSIZE], outbuf[BUFFSIZE * 4];
 
-               while (SC_FGETS(inbuf, sizeof(inbuf), infp) != NULL) {
+               while (claws_fgets(inbuf, sizeof(inbuf), infp) != NULL) {
                        qp_encode_line(outbuf, inbuf);
 
                        if (!strncmp("From ", outbuf, sizeof("From ")-1)) {
@@ -623,27 +609,27 @@ gboolean procmime_encode_content(MimeInfo *mimeinfo, EncodingType encoding)
                                
                                tmpbuf += sizeof("From ")-1;
                                
-                               if (SC_FPUTS("=46rom ", outfp) == EOF)
+                               if (claws_fputs("=46rom ", outfp) == EOF)
                                        err = TRUE;
-                               if (SC_FPUTS(tmpbuf, outfp) == EOF)
+                               if (claws_fputs(tmpbuf, outfp) == EOF)
                                        err = TRUE;
                        } else {
-                               if (SC_FPUTS(outbuf, outfp) == EOF)
+                               if (claws_fputs(outbuf, outfp) == EOF)
                                        err = TRUE;
                        }
                }
        } else {
                gchar buf[BUFFSIZE];
 
-               while (SC_FGETS(buf, sizeof(buf), infp) != NULL) {
+               while (claws_fgets(buf, sizeof(buf), infp) != NULL) {
                        strcrchomp(buf);
-                       if (SC_FPUTS(buf, outfp) == EOF)
+                       if (claws_fputs(buf, outfp) == EOF)
                                err = TRUE;
                }
        }
 
-       procmime_fclose(outfp);
-       procmime_fclose(infp);
+       claws_fclose(outfp);
+       claws_fclose(infp);
 
        if (err == TRUE)
                return FALSE;
@@ -657,7 +643,10 @@ gboolean procmime_encode_content(MimeInfo *mimeinfo, EncodingType encoding)
                        g_free(mimeinfo->data.mem);
        }
 
-       g_stat(tmpfilename, &statbuf);
+       if (g_stat(tmpfilename, &statbuf) < 0) {
+               FILE_OP_ERROR(tmpfilename, "stat");
+               return FALSE;
+       }
        mimeinfo->content = MIMECONTENT_FILE;
        mimeinfo->data.filename = tmpfilename;
        mimeinfo->tmp = TRUE;
@@ -668,58 +657,72 @@ gboolean procmime_encode_content(MimeInfo *mimeinfo, EncodingType encoding)
        return TRUE;
 }
 
-gint procmime_get_part(const gchar *outfile, MimeInfo *mimeinfo)
+static gint procmime_get_part_to_stream(FILE *outfp, MimeInfo *mimeinfo)
 {
-       FILE *infp, *outfp;
+       FILE *infp;
        gchar buf[BUFFSIZE];
        gint restlength, readlength;
        gint saved_errno = 0;
 
-       cm_return_val_if_fail(outfile != NULL, -1);
+       cm_return_val_if_fail(outfp != NULL, -1);
        cm_return_val_if_fail(mimeinfo != NULL, -1);
 
        if (mimeinfo->encoding_type != ENC_BINARY && !procmime_decode_content(mimeinfo))
                return -EINVAL;
 
-       if ((infp = procmime_fopen(mimeinfo->data.filename, "rb")) == NULL) {
+       if ((infp = claws_fopen(mimeinfo->data.filename, "rb")) == NULL) {
                saved_errno = errno;
-               FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
+               FILE_OP_ERROR(mimeinfo->data.filename, "claws_fopen");
                return -(saved_errno);
        }
        if (fseek(infp, mimeinfo->offset, SEEK_SET) < 0) {
                saved_errno = errno;
                FILE_OP_ERROR(mimeinfo->data.filename, "fseek");
-               procmime_fclose(infp);
-               return -(saved_errno);
-       }
-       if ((outfp = procmime_fopen(outfile, "wb")) == NULL) {
-               saved_errno = errno;
-               FILE_OP_ERROR(outfile, "fopen");
-               procmime_fclose(infp);
+               claws_fclose(infp);
                return -(saved_errno);
        }
 
        restlength = mimeinfo->length;
 
-       while ((restlength > 0) && ((readlength = SC_FREAD(buf, 1, restlength > BUFFSIZE ? BUFFSIZE : restlength, infp)) > 0)) {
-               if (SC_FWRITE(buf, 1, readlength, outfp) != readlength) {
+       while ((restlength > 0) && ((readlength = claws_fread(buf, 1, restlength > BUFFSIZE ? BUFFSIZE : restlength, infp)) > 0)) {
+               if (claws_fwrite(buf, 1, readlength, outfp) != readlength) {
                        saved_errno = errno;
-                       procmime_fclose(infp);
-                       procmime_fclose(outfp);
+                       claws_fclose(infp);
                        return -(saved_errno);
                }
                restlength -= readlength;
        }
 
-       procmime_fclose(infp);
-       if (procmime_fclose(outfp) == EOF) {
+       claws_fclose(infp);
+       rewind(outfp);
+
+       return 0;
+}
+
+gint procmime_get_part(const gchar *outfile, MimeInfo *mimeinfo)
+{
+       FILE *outfp;
+       gint result;
+       gint saved_errno = 0;
+
+       cm_return_val_if_fail(outfile != NULL, -1);
+
+       if ((outfp = claws_fopen(outfile, "wb")) == NULL) {
                saved_errno = errno;
-               FILE_OP_ERROR(outfile, "fclose");
+               FILE_OP_ERROR(outfile, "claws_fopen");
+               return -(saved_errno);
+       }
+
+       result = procmime_get_part_to_stream(outfp, mimeinfo);
+
+       if (claws_fclose(outfp) == EOF) {
+               saved_errno = errno;
+               FILE_OP_ERROR(outfile, "claws_fclose");
                claws_unlink(outfile);
                return -(saved_errno);
        }
 
-       return 0;
+       return result;
 }
 
 gboolean procmime_scan_text_content(MimeInfo *mimeinfo,
@@ -731,8 +734,8 @@ gboolean procmime_scan_text_content(MimeInfo *mimeinfo,
        gboolean conv_fail = FALSE;
        gchar buf[BUFFSIZE];
        gchar *str;
-       gchar *tmpfile;
        gboolean scan_ret = FALSE;
+       int r;
 
        cm_return_val_if_fail(mimeinfo != NULL, TRUE);
        cm_return_val_if_fail(scan_callback != NULL, TRUE);
@@ -740,18 +743,15 @@ gboolean procmime_scan_text_content(MimeInfo *mimeinfo,
        if (!procmime_decode_content(mimeinfo))
                return TRUE;
 
-       tmpfile = procmime_get_tmp_file_name(mimeinfo);
-       if (tmpfile == NULL)
-               return TRUE;
+       tmpfp = my_tmpfile();
 
-       if (procmime_get_part(tmpfile, mimeinfo) < 0) {
-               g_free(tmpfile);
+       if (tmpfp == NULL) {
+               FILE_OP_ERROR("tmpfile", "open");
                return TRUE;
        }
 
-       tmpfp = procmime_fopen(tmpfile, "rb");
-       if (tmpfp == NULL) {
-               g_free(tmpfile);
+       if ((r = procmime_get_part_to_stream(tmpfp, mimeinfo)) < 0) {
+               g_warning("procmime_get_part_to_stream error %d\n", r);
                return TRUE;
        }
 
@@ -768,6 +768,8 @@ gboolean procmime_scan_text_content(MimeInfo *mimeinfo,
                src_codeset = CS_GB18030;
        else if (!forced_charset && src_codeset && !strcasecmp(src_codeset, CS_GB2312))
                src_codeset = CS_GB18030;
+       else if (!forced_charset && src_codeset && !strcasecmp(src_codeset, CS_X_VIET_VPS))
+               src_codeset = CS_WINDOWS_874;
 
        if (mimeinfo->type == MIMETYPE_TEXT && !g_ascii_strcasecmp(mimeinfo->subtype, "html")) {
                SC_HTMLParser *parser;
@@ -793,8 +795,8 @@ gboolean procmime_scan_text_content(MimeInfo *mimeinfo,
                }
                ertf_parser_destroy(parser);
                conv_code_converter_destroy(conv);
-       } else if (mimeinfo->type == MIMETYPE_TEXT) {
-               while (SC_FGETS(buf, sizeof(buf), tmpfp) != NULL) {
+       } else if (mimeinfo->type == MIMETYPE_TEXT && mimeinfo->disposition != DISPOSITIONTYPE_ATTACHMENT) {
+               while (claws_fgets(buf, sizeof(buf), tmpfp) != NULL) {
                        str = conv_codeset_strdup(buf, src_codeset, CS_UTF_8);
                        if (str) {
                                if ((scan_ret = scan_callback(str, cb_data)) == TRUE) {
@@ -804,25 +806,23 @@ gboolean procmime_scan_text_content(MimeInfo *mimeinfo,
                                g_free(str);
                        } else {
                                conv_fail = TRUE;
-                               if ((scan_ret = scan_callback(str, cb_data)) == TRUE)
+                               if ((scan_ret = scan_callback(buf, cb_data)) == TRUE)
                                        break;
                        }
                }
        }
 
        if (conv_fail)
-               g_warning("procmime_get_text_content(): Code conversion failed.\n");
+               g_warning("procmime_get_text_content(): Code conversion failed.");
 
-       procmime_fclose(tmpfp);
-       claws_unlink(tmpfile);
-       g_free(tmpfile);
+       claws_fclose(tmpfp);
 
        return scan_ret;
 }
 
 static gboolean scan_fputs_cb(const gchar *str, gpointer fp)
 {
-       if (SC_FPUTS(str, (FILE *)fp) == EOF)
+       if (claws_fputs(str, (FILE *)fp) == EOF)
                return TRUE;
        
        return FALSE;
@@ -834,23 +834,17 @@ FILE *procmime_get_text_content(MimeInfo *mimeinfo)
        gboolean err;
 
        if ((outfp = my_tmpfile()) == NULL) {
-               perror("tmpfile");
+               perror("my_tmpfile");
                return NULL;
        }
-#ifdef HAVE_FGETS_UNLOCKED
-       flockfile(outfp);
-#endif
 
        err = procmime_scan_text_content(mimeinfo, scan_fputs_cb, outfp);
 
        rewind(outfp);
        if (err == TRUE) {
-               procmime_fclose(outfp);
+               claws_fclose(outfp);
                return NULL;
        }
-#ifdef HAVE_FGETS_UNLOCKED
-       funlockfile(outfp);
-#endif
        return outfp;
 
 }
@@ -858,35 +852,18 @@ FILE *procmime_get_text_content(MimeInfo *mimeinfo)
 FILE *procmime_get_binary_content(MimeInfo *mimeinfo)
 {
        FILE *outfp;
-       gchar *tmpfile;
 
        cm_return_val_if_fail(mimeinfo != NULL, NULL);
 
        if (!procmime_decode_content(mimeinfo))
                return NULL;
 
-       tmpfile = procmime_get_tmp_file_name(mimeinfo);
-       if (tmpfile == NULL)
-               return NULL;
-
-       if (procmime_get_part(tmpfile, mimeinfo) < 0) {
-               g_free(tmpfile);
-               return NULL;
-       }
+       outfp = my_tmpfile();
 
-       outfp = procmime_fopen(tmpfile, "rb");
-       if (outfp == NULL) {
-               g_unlink(tmpfile);
-               g_free(tmpfile);
+       if (procmime_get_part_to_stream(outfp, mimeinfo) < 0) {
                return NULL;
        }
 
-       g_unlink(tmpfile);
-       g_free(tmpfile);
-
-#ifdef HAVE_FGETS_UNLOCKED
-       funlockfile(outfp);
-#endif
        return outfp;
 }
 
@@ -920,19 +897,18 @@ scan_again:
                 * fully for non-empty parts
                 */
                short_scan = FALSE;
-               procmime_mimeinfo_free_all(mimeinfo);
+               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);
+               procmime_mimeinfo_free_all(&mimeinfo);
                goto scan_again;
        }
-       procmime_mimeinfo_free_all(mimeinfo);
+       procmime_mimeinfo_free_all(&mimeinfo);
 
-       /* outfp already unlocked at this time */
        return outfp;
 }
 
@@ -993,9 +969,8 @@ FILE *procmime_get_first_encrypted_text_content(MsgInfo *msginfo)
        if (partinfo)
                outfp = procmime_get_text_content(partinfo);
 
-       procmime_mimeinfo_free_all(mimeinfo);
+       procmime_mimeinfo_free_all(&mimeinfo);
 
-       /* outfp already unlocked at this time */
        return outfp;
 }
 
@@ -1013,7 +988,7 @@ gboolean procmime_msginfo_is_encrypted(MsgInfo *msginfo)
 
        partinfo = mimeinfo;
        result = (find_encrypted_part(partinfo) != NULL);
-       procmime_mimeinfo_free_all(mimeinfo);
+       procmime_mimeinfo_free_all(&mimeinfo);
 
        return result;
 }
@@ -1078,6 +1053,8 @@ gchar *procmime_get_mime_type(const gchar *filename)
        base = g_path_get_basename(filename);
        if ((p = strrchr(base, '.')) != NULL)
                ext = g_utf8_strdown(p + 1, -1);
+       else
+               ext = g_utf8_strdown(base, -1);
        g_free(base);
 
 #ifndef G_OS_WIN32
@@ -1090,7 +1067,8 @@ gchar *procmime_get_mime_type(const gchar *filename)
                debug_print("got type %s for %s\n", str, ext);
                g_free(ext);
                return str;
-       } 
+       }
+       g_free(ext);
        return NULL;
 #else
        gchar *str = get_content_type_from_registry_with_ext(ext);
@@ -1145,12 +1123,11 @@ static GHashTable *procmime_get_mime_type_table(void)
 
                exts = g_strsplit(mime_type->extension, " ", 16);
                for (i = 0; exts[i] != NULL; i++) {
-                       /* use previously dup'd key on overwriting */
-                       if (g_hash_table_lookup(table, exts[i]))
-                               key = exts[i];
-                       else
+                       /* Don't overwrite previously inserted extension */
+                       if (!g_hash_table_lookup(table, exts[i])) {
                                key = g_strdup(exts[i]);
-                       g_hash_table_insert(table, key, mime_type);
+                               g_hash_table_insert(table, key, mime_type);
+                       }
                }
                g_strfreev(exts);
        }
@@ -1172,23 +1149,23 @@ GList *procmime_get_mime_type_list(void)
                return mime_type_list;
        
 #if defined(__NetBSD__) || defined(__OpenBSD__) || defined(__FreeBSD__)
-       if ((fp = procmime_fopen(DATAROOTDIR "/mime/globs", "rb")) == NULL) 
+       if ((fp = claws_fopen(DATAROOTDIR "/mime/globs", "rb")) == NULL) 
 #else
-       if ((fp = procmime_fopen("/usr/share/mime/globs", "rb")) == NULL) 
+       if ((fp = claws_fopen("/usr/share/mime/globs", "rb")) == NULL) 
 #endif
        {
                fp_is_glob_file = FALSE;
-               if ((fp = procmime_fopen("/etc/mime.types", "rb")) == NULL) {
-                       if ((fp = procmime_fopen(SYSCONFDIR "/mime.types", "rb")) 
+               if ((fp = claws_fopen("/etc/mime.types", "rb")) == NULL) {
+                       if ((fp = claws_fopen(SYSCONFDIR "/mime.types", "rb")) 
                                == NULL) {
                                FILE_OP_ERROR(SYSCONFDIR "/mime.types", 
-                                       "fopen");
+                                       "claws_fopen");
                                return NULL;
                        }
                }
        }
 
-       while (SC_FGETS(buf, sizeof(buf), fp) != NULL) {
+       while (claws_fgets(buf, sizeof(buf), fp) != NULL) {
                p = strchr(buf, '#');
                if (p) *p = '\0';
                g_strstrip(buf);
@@ -1227,10 +1204,10 @@ GList *procmime_get_mime_type_list(void)
                list = g_list_append(list, mime_type);
        }
 
-       procmime_fclose(fp);
+       claws_fclose(fp);
 
        if (!list)
-               g_warning("Can't read mime.types\n");
+               g_warning("Can't read mime.types");
 
        return list;
 }
@@ -1244,6 +1221,8 @@ EncodingType procmime_get_encoding_for_charset(const gchar *charset)
                return ENC_7BIT;
        else if (!g_ascii_strcasecmp(charset, "ISO-8859-5") ||
                 !g_ascii_strncasecmp(charset, "KOI8-", 5) ||
+                !g_ascii_strcasecmp(charset, "X-MAC-CYRILLIC") ||
+                !g_ascii_strcasecmp(charset, "MAC-CYRILLIC") ||
                 !g_ascii_strcasecmp(charset, "Windows-1251"))
                return ENC_8BIT;
        else if (!g_ascii_strncasecmp(charset, "ISO-8859-", 9))
@@ -1264,12 +1243,12 @@ EncodingType procmime_get_encoding_for_text_file(const gchar *file, gboolean *ha
        gfloat octet_percentage;
        gboolean force_b64 = FALSE;
 
-       if ((fp = procmime_fopen(file, "rb")) == NULL) {
-               FILE_OP_ERROR(file, "fopen");
+       if ((fp = claws_fopen(file, "rb")) == NULL) {
+               FILE_OP_ERROR(file, "claws_fopen");
                return ENC_UNKNOWN;
        }
 
-       while ((len = SC_FREAD(buf, sizeof(guchar), sizeof(buf), fp)) > 0) {
+       while ((len = claws_fread(buf, sizeof(guchar), sizeof(buf), fp)) > 0) {
                guchar *p;
                gint i;
 
@@ -1284,7 +1263,7 @@ EncodingType procmime_get_encoding_for_text_file(const gchar *file, gboolean *ha
                total_len += len;
        }
 
-       procmime_fclose(fp);
+       claws_fclose(fp);
        
        if (total_len > 0)
                octet_percentage = (gfloat)octet_chars / (gfloat)total_len;
@@ -1292,7 +1271,7 @@ EncodingType procmime_get_encoding_for_text_file(const gchar *file, gboolean *ha
                octet_percentage = 0.0;
 
        debug_print("procmime_get_encoding_for_text_file(): "
-                   "8bit chars: %zd / %zd (%f%%)\n", octet_chars, total_len,
+                   "8bit chars: %"G_GSIZE_FORMAT" / %"G_GSIZE_FORMAT" (%f%%)\n", octet_chars, total_len,
                    100.0 * octet_percentage);
 
        if (octet_percentage > 0.20 || force_b64) {
@@ -1346,6 +1325,7 @@ static struct TypeTable mime_type_table[] = {
        {"image", MIMETYPE_IMAGE},
        {"audio", MIMETYPE_AUDIO},
        {"video", MIMETYPE_VIDEO},
+       {"model", MIMETYPE_MODEL},
        {"application", MIMETYPE_APPLICATION},
        {"message", MIMETYPE_MESSAGE},
        {"multipart", MIMETYPE_MULTIPART},
@@ -1430,12 +1410,16 @@ static void procmime_parse_message_rfc822(MimeInfo *mimeinfo, gboolean short_sca
 
        procmime_decode_content(mimeinfo);
 
-       fp = procmime_fopen(mimeinfo->data.filename, "rb");
+       fp = claws_fopen(mimeinfo->data.filename, "rb");
        if (fp == NULL) {
-               FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
+               FILE_OP_ERROR(mimeinfo->data.filename, "claws_fopen");
+               return;
+       }
+       if (fseek(fp, mimeinfo->offset, SEEK_SET) < 0) {
+               FILE_OP_ERROR(mimeinfo->data.filename, "fseek");
+               claws_fclose(fp);
                return;
        }
-       fseek(fp, mimeinfo->offset, SEEK_SET);
        procheader_get_header_fields(fp, hentry);
        if (hentry[0].body != NULL) {
                tmp = conv_unmime_header(hentry[0].body, NULL, FALSE);
@@ -1469,7 +1453,7 @@ static void procmime_parse_message_rfc822(MimeInfo *mimeinfo, gboolean short_sca
         }
   
        content_start = ftell(fp);
-       procmime_fclose(fp);
+       claws_fclose(fp);
        
        len = mimeinfo->length - (content_start - mimeinfo->offset);
        if (len < 0)
@@ -1503,12 +1487,16 @@ static void procmime_parse_disposition_notification(MimeInfo *mimeinfo,
        procmime_decode_content(mimeinfo);
 
        debug_print("parse disposition notification\n");
-       fp = procmime_fopen(mimeinfo->data.filename, "rb");
+       fp = claws_fopen(mimeinfo->data.filename, "rb");
        if (fp == NULL) {
-               FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
+               FILE_OP_ERROR(mimeinfo->data.filename, "claws_fopen");
+               return;
+       }
+       if (fseek(fp, mimeinfo->offset, SEEK_SET) < 0) {
+               FILE_OP_ERROR(mimeinfo->data.filename, "fseek");
+               claws_fclose(fp);
                return;
        }
-       fseek(fp, mimeinfo->offset, SEEK_SET);
 
        if (original_msgid && disposition_notification_hdr) {
                hentry[0].body = g_strdup(original_msgid);
@@ -1517,7 +1505,7 @@ static void procmime_parse_disposition_notification(MimeInfo *mimeinfo,
                procheader_get_header_fields(fp, hentry);
        }
     
-        procmime_fclose(fp);
+        claws_fclose(fp);
 
        if (!hentry[0].body || !hentry[1].body) {
                debug_print("MsgId %s, Disp %s\n",
@@ -1547,7 +1535,7 @@ static void procmime_parse_disposition_notification(MimeInfo *mimeinfo,
                        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);
+                               procmsg_msginfo_free(&info);
                        }
                }
        }
@@ -1612,7 +1600,7 @@ static void procmime_parse_multipart(MimeInfo *mimeinfo, gboolean short_scan)
                                {"Disposition:",
                                                   NULL, TRUE},
                                {NULL,             NULL, FALSE}};
-       gchar *p, *tmp;
+       gchar *tmp;
        gchar *boundary;
        gint boundary_len = 0, lastoffset = -1, i;
        gchar buf[BUFFSIZE];
@@ -1628,14 +1616,19 @@ static void procmime_parse_multipart(MimeInfo *mimeinfo, gboolean short_scan)
 
        procmime_decode_content(mimeinfo);
 
-       fp = procmime_fopen(mimeinfo->data.filename, "rb");
+       fp = claws_fopen(mimeinfo->data.filename, "rb");
        if (fp == NULL) {
-               FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
+               FILE_OP_ERROR(mimeinfo->data.filename, "claws_fopen");
                return;
        }
 
-       fseek(fp, mimeinfo->offset, SEEK_SET);
-       while ((p = SC_FGETS(buf, sizeof(buf), fp)) != NULL && result == 0) {
+       if (fseek(fp, mimeinfo->offset, SEEK_SET) < 0) {
+               FILE_OP_ERROR(mimeinfo->data.filename, "fseek");
+               claws_fclose(fp);
+               return;
+       }
+
+       while (claws_fgets(buf, sizeof(buf), fp) != NULL && result == 0) {
                if (ftell(fp) - 1 > (mimeinfo->offset + mimeinfo->length))
                        break;
 
@@ -1691,7 +1684,7 @@ static void procmime_parse_multipart(MimeInfo *mimeinfo, gboolean short_scan)
                g_free(hentry[i].body);
                hentry[i].body = NULL;
        }
-       procmime_fclose(fp);
+       claws_fclose(fp);
 }
 
 static void parse_parameters(const gchar *parameters, GHashTable *table)
@@ -1722,7 +1715,7 @@ static void parse_parameters(const gchar *parameters, GHashTable *table)
 
                value[0] = '\0';
                value++;
-               while (value[0] == ' ')
+               while (value[0] != '\0' && value[0] == ' ')
                        value++;
 
                down_attr = g_utf8_strdown(attribute, -1);
@@ -1769,12 +1762,12 @@ static void parse_parameters(const gchar *parameters, GHashTable *table)
                        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';
-               }               
+
+               while (value[0] != '\0' && value[0] == ' ')
+                       value++;
+               while (value[strlen(value)-1] == ' ') 
+                       value[strlen(value)-1] = '\0';
+
                if (down_attr && strrchr(down_attr, '*') != NULL) {
                        gchar *tmpattr;
 
@@ -1822,8 +1815,7 @@ static void parse_parameters(const gchar *parameters, GHashTable *table)
                g_hash_table_insert(table, g_strdup(attribute), g_strdup(value->str));
                g_string_free(value, TRUE);
        }
-       slist_free_strings(concatlist);
-       g_slist_free(concatlist);
+       slist_free_strings_full(concatlist);
 
        for (cur = convlist; cur != NULL; cur = g_slist_next(cur)) {
                gchar *attribute, *key, *value;
@@ -1835,6 +1827,8 @@ static void parse_parameters(const gchar *parameters, GHashTable *table)
                        continue;
 
                charset = value;
+               if (charset == NULL)
+                       continue;
                lang = strchr(charset, '\'');
                if (lang == NULL)
                        continue;
@@ -1854,8 +1848,7 @@ static void parse_parameters(const gchar *parameters, GHashTable *table)
 
                g_hash_table_insert(table, g_strdup(attribute), newvalue);
        }
-       slist_free_strings(convlist);
-       g_slist_free(convlist);
+       slist_free_strings_full(convlist);
 
        g_free(params);
 }      
@@ -1948,7 +1941,7 @@ static MimeParser *procmime_get_mimeparser_for_type(MimeMediaType type, const gc
        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))
+               if (parser->type == type && !g_strcmp0(parser->sub_type, sub_type))
                        return parser;
        }
        return NULL;
@@ -2001,7 +1994,7 @@ static int procmime_parse_mimepart(MimeInfo *parent,
                         * this avoids DOSsing ourselves 
                         * with enormous messages
                         */
-                       procmime_mimeinfo_free_all(mimeinfo);
+                       procmime_mimeinfo_free_all(&mimeinfo);
                        return -1;                      
                }
                g_node_append(parent->node, mimeinfo->node);
@@ -2011,6 +2004,7 @@ static int procmime_parse_mimepart(MimeInfo *parent,
        mimeinfo->length = length;
 
        if (content_type != NULL) {
+               g_strchomp(content_type);
                procmime_parse_content_type(content_type, mimeinfo);
        } else {
                mimeinfo->type = MIMETYPE_TEXT;
@@ -2025,6 +2019,7 @@ static int procmime_parse_mimepart(MimeInfo *parent,
        }
 
        if (content_encoding != NULL) {
+               g_strchomp(content_encoding);
                procmime_parse_content_encoding(content_encoding, mimeinfo);
        } else {
                mimeinfo->encoding_type = ENC_UNKNOWN;
@@ -2045,9 +2040,10 @@ static int procmime_parse_mimepart(MimeInfo *parent,
        else
                mimeinfo->location = NULL;
 
-       if (content_disposition != NULL) 
+       if (content_disposition != NULL) {
+               g_strchomp(content_disposition);
                procmime_parse_content_disposition(content_disposition, mimeinfo);
-       else
+       else
                mimeinfo->disposition = DISPOSITIONTYPE_UNKNOWN;
 
        /* Call parser for mime type */
@@ -2111,7 +2107,9 @@ static gboolean output_func(GNode *node, gpointer data)
        depth = g_node_depth(node);
        for (i = 0; i < depth; i++)
                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);
+       g_print("%s/%s (offset:%d length:%d encoding: %d)\n", 
+               (mimeinfo->type < 8)? typenames[mimeinfo->type] : "unknown", 
+               mimeinfo->subtype, mimeinfo->offset, mimeinfo->length, mimeinfo->encoding_type);
 
        return FALSE;
 }
@@ -2124,9 +2122,12 @@ static void output_mime_structure(MimeInfo *mimeinfo, int indent)
 static MimeInfo *procmime_scan_file_with_offset(const gchar *filename, int offset, gboolean short_scan)
 {
        MimeInfo *mimeinfo;
-       struct stat buf;
+       GStatBuf buf;
 
-       g_stat(filename, &buf);
+       if (g_stat(filename, &buf) < 0) {
+               FILE_OP_ERROR(filename, "stat");
+               return NULL;
+       }
 
        mimeinfo = procmime_mimeinfo_new();
        mimeinfo->content = MIMECONTENT_FILE;
@@ -2175,10 +2176,10 @@ static MimeInfo *procmime_scan_queue_file_full(const gchar *filename, gboolean s
        cm_return_val_if_fail(filename != NULL, NULL);
 
        /* Open file */
-       if ((fp = procmime_fopen(filename, "rb")) == NULL)
+       if ((fp = claws_fopen(filename, "rb")) == NULL)
                return NULL;
        /* Skip queue header */
-       while (SC_FGETS(buf, sizeof(buf), fp) != NULL) {
+       while (claws_fgets(buf, sizeof(buf), fp) != NULL) {
                /* new way */
                if ((!strncmp(buf, "X-Claws-End-Special-Headers: 1",
                        strlen("X-Claws-End-Special-Headers:"))) ||
@@ -2197,7 +2198,7 @@ static MimeInfo *procmime_scan_queue_file_full(const gchar *filename, gboolean s
                }
        }
        offset = ftell(fp);
-       procmime_fclose(fp);
+       claws_fclose(fp);
 
        mimeinfo = procmime_scan_file_with_offset(filename, offset, short_scan);
 
@@ -2301,7 +2302,7 @@ static void write_parameters(gpointer key, gpointer value, gpointer user_data)
                break;
 #else
        case ENC_AS_EXTENDED:
-               debug_print("Unhandled ENC_AS_EXTENDED.");
+               debug_print("Unhandled ENC_AS_EXTENDED.\n");
                break;
 #endif
        case ENC_AS_ENCWORD:
@@ -2448,18 +2449,22 @@ static gint procmime_write_message_rfc822(MimeInfo *mimeinfo, FILE *fp)
        /* write header */
        switch (mimeinfo->content) {
        case MIMECONTENT_FILE:
-               if ((infp = procmime_fopen(mimeinfo->data.filename, "rb")) == NULL) {
-                       FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
+               if ((infp = claws_fopen(mimeinfo->data.filename, "rb")) == NULL) {
+                       FILE_OP_ERROR(mimeinfo->data.filename, "claws_fopen");
                        return -1;
                }
-               fseek(infp, mimeinfo->offset, SEEK_SET);
-               while (SC_FGETS(buf, sizeof(buf), infp) == buf) {
+               if (fseek(infp, mimeinfo->offset, SEEK_SET) < 0) {
+                       FILE_OP_ERROR(mimeinfo->data.filename, "fseek");
+                       claws_fclose(infp);
+                       return -1;
+               }
+               while (claws_fgets(buf, sizeof(buf), infp) == buf) {
                        strcrchomp(buf);
                        if (buf[0] == '\n' && buf[1] == '\0')
                                break;
                        if (skip && (buf[0] == ' ' || buf[0] == '\t'))
                                continue;
-                       if (g_ascii_strncasecmp(buf, "Mime-Version:", 13) == 0 ||
+                       if (g_ascii_strncasecmp(buf, "MIME-Version:", 13) == 0 ||
                            g_ascii_strncasecmp(buf, "Content-Type:", 13) == 0 ||
                            g_ascii_strncasecmp(buf, "Content-Transfer-Encoding:", 26) == 0 ||
                            g_ascii_strncasecmp(buf, "Content-Description:", 20) == 0 ||
@@ -2470,20 +2475,20 @@ static gint procmime_write_message_rfc822(MimeInfo *mimeinfo, FILE *fp)
                                continue;
                        }
                        len = strlen(buf);
-                       if (SC_FWRITE(buf, sizeof(gchar), len, fp) < len) {
-                               g_warning("failed to dump %zd bytes from file", len);
-                               procmime_fclose(infp);
+                       if (claws_fwrite(buf, sizeof(gchar), len, fp) < len) {
+                               g_warning("failed to dump %"G_GSIZE_FORMAT" bytes from file", len);
+                               claws_fclose(infp);
                                return -1;
                        }
                        skip = FALSE;
                }
-               procmime_fclose(infp);
+               claws_fclose(infp);
                break;
 
        case MIMECONTENT_MEM:
                len = strlen(mimeinfo->data.mem);
-               if (SC_FWRITE(mimeinfo->data.mem, sizeof(gchar), len, fp) < len) {
-                       g_warning("failed to dump %zd bytes from mem", len);
+               if (claws_fwrite(mimeinfo->data.mem, sizeof(gchar), len, fp) < len) {
+                       g_warning("failed to dump %"G_GSIZE_FORMAT" bytes from mem", len);
                        return -1;
                }
                break;
@@ -2497,7 +2502,7 @@ static gint procmime_write_message_rfc822(MimeInfo *mimeinfo, FILE *fp)
                return -1;
 
        child = (MimeInfo *) childnode->data;
-       if (fprintf(fp, "Mime-Version: 1.0\n") < 0) {
+       if (fprintf(fp, "MIME-Version: 1.0\n") < 0) {
                g_warning("failed to write mime version");
                return -1;
        }
@@ -2521,22 +2526,26 @@ static gint procmime_write_multipart(MimeInfo *mimeinfo, FILE *fp)
 
        switch (mimeinfo->content) {
        case MIMECONTENT_FILE:
-               if ((infp = procmime_fopen(mimeinfo->data.filename, "rb")) == NULL) {
-                       FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
+               if ((infp = claws_fopen(mimeinfo->data.filename, "rb")) == NULL) {
+                       FILE_OP_ERROR(mimeinfo->data.filename, "claws_fopen");
                        return -1;
                }
-               fseek(infp, mimeinfo->offset, SEEK_SET);
-               while (SC_FGETS(buf, sizeof(buf), infp) == buf) {
+               if (fseek(infp, mimeinfo->offset, SEEK_SET) < 0) {
+                       FILE_OP_ERROR(mimeinfo->data.filename, "fseek");
+                       claws_fclose(infp);
+                       return -1;
+               }
+               while (claws_fgets(buf, sizeof(buf), infp) == buf) {
                        if (IS_BOUNDARY(buf, boundary, strlen(boundary)))
                                break;
                        len = strlen(buf);
-                       if (SC_FWRITE(buf, sizeof(gchar), len, fp) < len) {
-                               g_warning("failed to write %zd", len);
-                               procmime_fclose(infp);
+                       if (claws_fwrite(buf, sizeof(gchar), len, fp) < len) {
+                               g_warning("failed to write %"G_GSIZE_FORMAT, len);
+                               claws_fclose(infp);
                                return -1;
                        }
                }
-               procmime_fclose(infp);
+               claws_fclose(infp);
                break;
 
        case MIMECONTENT_MEM:
@@ -2545,8 +2554,8 @@ static gint procmime_write_multipart(MimeInfo *mimeinfo, FILE *fp)
                    (*(str2 - 1) == '-') && (*(str2 - 2) == '-'))
                        *(str2 - 2) = '\0';
                len = strlen(str);
-               if (SC_FWRITE(str, sizeof(gchar), len, fp) < len) {
-                       g_warning("failed to write %zd from mem", len);
+               if (claws_fwrite(str, sizeof(gchar), len, fp) < len) {
+                       g_warning("failed to write %"G_GSIZE_FORMAT" from mem", len);
                        g_free(str);
                        return -1;
                }
@@ -2590,17 +2599,17 @@ gint procmime_write_mimeinfo(MimeInfo *mimeinfo, FILE *fp)
        if (G_NODE_IS_LEAF(mimeinfo->node)) {
                switch (mimeinfo->content) {
                case MIMECONTENT_FILE:
-                       if ((infp = procmime_fopen(mimeinfo->data.filename, "rb")) == NULL) {
-                               FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
+                       if ((infp = claws_fopen(mimeinfo->data.filename, "rb")) == NULL) {
+                               FILE_OP_ERROR(mimeinfo->data.filename, "claws_fopen");
                                return -1;
                        }
                        copy_file_part_to_fp(infp, mimeinfo->offset, mimeinfo->length, fp);
-                       procmime_fclose(infp);
+                       claws_fclose(infp);
                        return 0;
 
                case MIMECONTENT_MEM:
                        len = strlen(mimeinfo->data.mem);
-                       if (SC_FWRITE(mimeinfo->data.mem, sizeof(gchar), len, fp) < len)
+                       if (claws_fwrite(mimeinfo->data.mem, sizeof(gchar), len, fp) < len)
                                return -1;
                        return 0;
 
@@ -2657,3 +2666,104 @@ gchar *procmime_get_part_file_name(MimeInfo *mimeinfo)
        return base;
 }
 
+void *procmime_get_part_as_string(MimeInfo *mimeinfo,
+               gboolean null_terminate)
+{
+       FILE *infp;
+       gchar *data;
+       gint length, readlength;
+
+       cm_return_val_if_fail(mimeinfo != NULL, NULL);
+
+       if (mimeinfo->encoding_type != ENC_BINARY &&
+                       !procmime_decode_content(mimeinfo))
+               return NULL;
+
+       if (mimeinfo->content == MIMECONTENT_MEM)
+               return g_strdup(mimeinfo->data.mem);
+
+       if ((infp = claws_fopen(mimeinfo->data.filename, "rb")) == NULL) {
+               FILE_OP_ERROR(mimeinfo->data.filename, "claws_fopen");
+               return NULL;
+       }
+
+       if (fseek(infp, mimeinfo->offset, SEEK_SET) < 0) {
+               FILE_OP_ERROR(mimeinfo->data.filename, "fseek");
+               claws_fclose(infp);
+               return NULL;
+       }
+
+       length = mimeinfo->length;
+
+       data = g_malloc(null_terminate ? length + 1 : length);
+       if (data == NULL) {
+               g_warning("Could not allocate %d bytes for procmime_get_part_as_string.\n",
+                               (null_terminate ? length + 1 : length));
+               claws_fclose(infp);
+               return NULL;
+       }
+
+       readlength = claws_fread(data, length, 1, infp);
+       if (readlength <= 0) {
+               FILE_OP_ERROR(mimeinfo->data.filename, "fread");
+               g_free(data);
+               claws_fclose(infp);
+               return NULL;
+       }
+
+       claws_fclose(infp);
+
+       if (null_terminate)
+               data[length] = '\0';
+
+       return data;
+}
+
+/* Returns an open GInputStream. The caller should just
+ * read mimeinfo->length bytes from it and then release it. */
+GInputStream *procmime_get_part_as_inputstream(MimeInfo *mimeinfo)
+{
+       cm_return_val_if_fail(mimeinfo != NULL, NULL);
+
+       if (mimeinfo->encoding_type != ENC_BINARY &&
+                       !procmime_decode_content(mimeinfo)) {
+               g_warning("could not decode part");
+               return NULL;
+       }
+       if (mimeinfo->content == MIMECONTENT_MEM) {
+               /* NULL for destroy func, since we're not copying
+                * the data for the stream. */
+               return g_memory_input_stream_new_from_data(
+                               mimeinfo->data.mem,
+                               mimeinfo->length, NULL);
+       } else {
+               return g_memory_input_stream_new_from_data(
+                               procmime_get_part_as_string(mimeinfo, FALSE),
+                               mimeinfo->length, g_free);
+       }
+}
+
+GdkPixbuf *procmime_get_part_as_pixbuf(MimeInfo *mimeinfo, GError **error)
+{
+       GdkPixbuf *pixbuf;
+       GInputStream *stream;
+
+       if (error)
+               *error = NULL;
+
+       stream = procmime_get_part_as_inputstream(mimeinfo);
+       if (stream == NULL) {
+               if (error)
+                       *error = g_error_new_literal(G_FILE_ERROR, -1, _("Could not decode part"));
+               return NULL;
+       }
+
+       pixbuf = gdk_pixbuf_new_from_stream(stream, NULL, error);
+       g_object_unref(stream);
+
+       if (error && *error != NULL)
+               return NULL;
+
+       return pixbuf;
+}
+