2007-10-05 [colin] 3.0.2cvs11
[claws.git] / src / procmime.c
index ec2c1bc591c8c5f66eca1314bb406e1ba06a93a2..deab098aa5be8b6fa5eb2471ceeae867d39c95e2 100644 (file)
@@ -4,7 +4,7 @@
  *
  * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ * 
  */
 
 #ifdef HAVE_CONFIG_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)
 {
@@ -186,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))
@@ -202,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;
 }
 
@@ -237,10 +261,13 @@ const gchar *procmime_mimeinfo_get_parameter(MimeInfo *mimeinfo, const gchar *na
                        /* this is flowed */                                    \
                        if (delsp)                                              \
                                lastline[llen-1] = '\0';                        \
-                       fputs(lastline, outfp);                                 \
+                       if (fputs(lastline, outfp) == EOF)                      \
+                               err = TRUE;                                     \
                } else {                                                        \
-                       fputs(lastline, outfp);                                 \
-                       fputs("\n", outfp);                                     \
+                       if (fputs(lastline, outfp) == EOF)                      \
+                               err = TRUE;                                     \
+                       if (fputs("\n", outfp) == EOF)                          \
+                               err = TRUE;                                     \
                }                                                               \
        }                                                                       \
        strcpy(lastline, buf);                                                  \
@@ -256,6 +283,8 @@ gboolean procmime_decode_content(MimeInfo *mimeinfo)
        gboolean tmp_file = FALSE;
        gboolean flowed = FALSE;
        gboolean delsp = FALSE; 
+       gboolean err = FALSE;
+
        EncodingType encoding = forced_encoding 
                                ? forced_encoding
                                : mimeinfo->encoding_type;
@@ -306,7 +335,8 @@ gboolean procmime_decode_content(MimeInfo *mimeinfo)
                        len = qp_decode_line(buf);
                        buf[len]='\0';
                        if (!flowed) {
-                               fwrite(buf, 1, len, outfp);
+                               if (fwrite(buf, 1, len, outfp) < len)
+                                       err = TRUE;
                        } else {
                                FLUSH_LASTLINE();
                        }
@@ -338,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;
                        }
                }
@@ -357,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);
                }
@@ -376,25 +409,33 @@ 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)) {
                        if (!flowed) {
-                               fputs(buf, outfp);
+                               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);
@@ -417,6 +458,7 @@ 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;
@@ -481,13 +523,17 @@ 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) {
@@ -506,23 +552,31 @@ 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);
@@ -666,7 +720,7 @@ void renderer_write_config(void)
        gchar * rcpath;
        PrefFile *pfile;
        GList * cur;
-
+       int err = 0;
        rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, RENDERER_RC, NULL);
        
        if ((pfile = prefs_write_open(rcpath)) == NULL) {
@@ -680,13 +734,20 @@ void renderer_write_config(void)
        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 (fprintf(pfile->fp, "%s %s\n", renderer->content_type,
+                       renderer->renderer) < 0) {
+                       err = TRUE;
+                       break;
+               }
        }
 
-       if (prefs_file_close(pfile) < 0) {
-               g_warning("failed to write configuration to file\n");
-               return;
+       if (!err) {
+               if (prefs_file_close(pfile) < 0) {
+                       g_warning("failed to write configuration to file\n");
+                       return;
+               }
+       } else {
+               prefs_file_close_revert(pfile);
        }
 }
 
@@ -700,7 +761,8 @@ FILE *procmime_get_text_content(MimeInfo *mimeinfo)
        struct ContentRenderer * renderer;
        GList * cur;
        gchar *tmpfile, *content_type;
-    
+       gboolean err = FALSE;
+
        g_return_val_if_fail(mimeinfo != NULL, NULL);
 
        if (!procmime_decode_content(mimeinfo))
@@ -761,8 +823,10 @@ FILE *procmime_get_text_content(MimeInfo *mimeinfo)
                        
                        while ((count =
                                fread(buf, sizeof(char), sizeof(buf),
-                                     tmpfp)) > 0)
-                               fwrite(buf, sizeof(char), count, p);
+                                     tmpfp)) > 0) {
+                               if (fwrite(buf, sizeof(char), count, p) < count)
+                                       err = TRUE;
+                       }
                        pclose(p);
                }
                
@@ -775,7 +839,8 @@ FILE *procmime_get_text_content(MimeInfo *mimeinfo)
                conv = conv_code_converter_new(src_codeset);
                parser = sc_html_parser_new(tmpfp, conv);
                while ((str = sc_html_parse(parser)) != NULL) {
-                       fputs(str, outfp);
+                       if (fputs(str, outfp) == EOF)
+                               err = TRUE;
                }
                sc_html_parser_destroy(parser);
                conv_code_converter_destroy(conv);
@@ -786,7 +851,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);
@@ -794,11 +860,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;
                        }
                }
        }
@@ -811,6 +879,11 @@ FILE *procmime_get_text_content(MimeInfo *mimeinfo)
        g_unlink(tmpfile);
        g_free(tmpfile);
 
+       if (err == TRUE) {
+               fclose(outfp);
+               return NULL;
+       }
+
        return outfp;
 }
 
@@ -820,10 +893,10 @@ FILE *procmime_get_first_text_content(MsgInfo *msginfo)
 {
        FILE *outfp = NULL;
        MimeInfo *mimeinfo, *partinfo;
-
+       START_TIMING("");
        g_return_val_if_fail(msginfo != NULL, NULL);
 
-       mimeinfo = procmime_scan_message(msginfo);
+       mimeinfo = procmime_scan_message_short(msginfo);
        if (!mimeinfo) return NULL;
 
        partinfo = mimeinfo;
@@ -834,7 +907,7 @@ FILE *procmime_get_first_text_content(MsgInfo *msginfo)
                outfp = procmime_get_text_content(partinfo);
 
        procmime_mimeinfo_free_all(mimeinfo);
-
+       END_TIMING();
        return outfp;
 }
 
@@ -1244,7 +1317,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: %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 || force_b64) {
@@ -1350,9 +1423,10 @@ static int procmime_parse_mimepart(MimeInfo *parent,
                             gchar *content_location,
                             const gchar *filename,
                             guint offset,
-                            guint length);
+                            guint length,
+                            gboolean short_scan);
 
-static 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:",
@@ -1413,7 +1487,7 @@ static void procmime_parse_message_rfc822(MimeInfo *mimeinfo)
                                hentry[2].body, hentry[3].body,
                                hentry[4].body, hentry[5].body,
                                mimeinfo->data.filename, content_start,
-                               len);
+                               len, short_scan);
        
        for (i = 0; i < (sizeof hentry / sizeof hentry[0]); i++) {
                g_free(hentry[i].body);
@@ -1421,7 +1495,7 @@ static void procmime_parse_message_rfc822(MimeInfo *mimeinfo)
        }
 }
 
-static void procmime_parse_multipart(MimeInfo *mimeinfo)
+static void procmime_parse_multipart(MimeInfo *mimeinfo, gboolean short_scan)
 {
        HeaderEntry hentry[] = {{"Content-Type:",  NULL, TRUE},
                                {"Content-Transfer-Encoding:",
@@ -1441,6 +1515,7 @@ static 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)
@@ -1469,7 +1544,11 @@ static void procmime_parse_multipart(MimeInfo *mimeinfo)
                                                        hentry[2].body, hentry[3].body, 
                                                        hentry[4].body, hentry[5].body,
                                                        mimeinfo->data.filename, lastoffset,
-                                                       len);
+                                                       len, short_scan);
+                               if (result == 1 && short_scan) {
+                                       done = TRUE;
+                                       break;
+                               }
                        }
                        
                        if (buf[2 + boundary_len]     == '-' &&
@@ -1753,6 +1832,37 @@ static void procmime_parse_content_encoding(const gchar *content_encoding, MimeI
        return;
 }
 
+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,
@@ -1762,9 +1872,13 @@ static int procmime_parse_mimepart(MimeInfo *parent,
                             gchar *content_location,
                             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();
@@ -1825,22 +1939,33 @@ static int procmime_parse_mimepart(MimeInfo *parent,
                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);
                        }
                        break;
                        
                case MIMETYPE_MULTIPART:
-                       procmime_parse_multipart(mimeinfo);
+                       procmime_parse_multipart(mimeinfo, short_scan);
                        break;
                        
                default:
                        break;
+               }
        }
 
-       return 0;
+       return result;
 }
 
 static gchar *typenames[] = {
@@ -1861,8 +1986,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;
 }
@@ -1872,7 +1997,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);
 }
 
-static 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;
@@ -1888,25 +2013,35 @@ static MimeInfo *procmime_scan_file_with_offset(const gchar *filename, int offse
        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;
@@ -1940,11 +2075,21 @@ MimeInfo *procmime_scan_queue_file(const gchar *filename)
        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,
@@ -1956,6 +2101,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)
@@ -2046,17 +2192,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);
@@ -2064,33 +2218,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)
-               fprintf(fp, "Content-Location: %s\n", mimeinfo->location);
+               TRY(fprintf(fp, "Content-Location: %s\n", mimeinfo->location) >= 0);
 
        if (mimeinfo->disposition != DISPOSITIONTYPE_UNKNOWN) {
                ParametersData *pdata = g_new0(ParametersData, 1);
@@ -2102,19 +2264,29 @@ 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;
 }
 
 static gint procmime_write_message_rfc822(MimeInfo *mimeinfo, FILE *fp)
@@ -2124,6 +2296,7 @@ static gint procmime_write_message_rfc822(MimeInfo *mimeinfo, FILE *fp)
        MimeInfo *child;
        gchar buf[BUFFSIZE];
        gboolean skip = FALSE;;
+       size_t len;
 
        debug_print("procmime_write_message_rfc822\n");
 
@@ -2151,17 +2324,23 @@ static gint procmime_write_message_rfc822(MimeInfo *mimeinfo, FILE *fp)
                                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:
@@ -2173,8 +2352,12 @@ static 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);
 }
 
@@ -2185,6 +2368,7 @@ static gint procmime_write_multipart(MimeInfo *mimeinfo, FILE *fp)
        gchar *boundary, *str, *str2;
        gchar buf[BUFFSIZE];
        gboolean firstboundary;
+       size_t len;
 
        debug_print("procmime_write_multipart\n");
 
@@ -2200,7 +2384,12 @@ static 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;
@@ -2210,7 +2399,12 @@ static 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;
 
@@ -2226,16 +2420,18 @@ static 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;
 }
@@ -2243,7 +2439,7 @@ static 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)) {
@@ -2258,10 +2454,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: