2011-04-08 [wwp] 3.7.8cvs74
[claws.git] / src / procmime.c
index 5c6de3040b50e81e0aa5ae46cdbbd98147f78852..e44627eade36ab0182444fb2406268d5714e1eb2 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2009 Hiroyuki Yamamoto & The Claws Mail Team
+ * Copyright (C) 1999-2011 Hiroyuki Yamamoto & The Claws Mail Team
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -190,8 +190,8 @@ MimeInfo *procmime_scan_message(MsgInfo *msginfo)
 {
        gchar *filename;
        MimeInfo *mimeinfo;
-       START_TIMING("");
-       filename = procmsg_get_message_file_path(msginfo);
+
+       filename = procmsg_get_message_file_path(msginfo);
        if (!filename || !is_file_exist(filename)) {
                g_free(filename);
                return NULL;
@@ -204,7 +204,6 @@ MimeInfo *procmime_scan_message(MsgInfo *msginfo)
                mimeinfo = procmime_scan_queue_file(filename);
        g_free(filename);
 
-       END_TIMING();
        return mimeinfo;
 }
 
@@ -345,11 +344,13 @@ gboolean procmime_decode_content(MimeInfo *mimeinfo)
                        FLUSH_LASTLINE();
        } else if (encoding == ENC_BASE64) {
                gchar outbuf[BUFFSIZE];
-               gint len;
+               gint len, inlen, inread;
                Base64Decoder *decoder;
                gboolean got_error = FALSE;
                gboolean uncanonicalize = FALSE;
                FILE *tmpfp = outfp;
+               gboolean null_bytes = FALSE;
+               gboolean starting = TRUE;
 
                if (mimeinfo->type == MIMETYPE_TEXT ||
                    mimeinfo->type == MIMETYPE_MESSAGE) {
@@ -364,9 +365,15 @@ gboolean procmime_decode_content(MimeInfo *mimeinfo)
                }
 
                decoder = base64_decoder_new();
-               while ((ftell(infp) < readend) && (fgets(buf, sizeof(buf), infp) != NULL)) {
-                       len = base64_decoder_decode(decoder, buf, outbuf);
-                       if (len < 0 && !got_error) {
+               while ((inlen = MIN(readend - ftell(infp), sizeof(buf))) > 0 && !err) {
+                       inread = fread(buf, 1, inlen, infp);
+                       len = base64_decoder_decode(decoder, buf, outbuf, inread);
+                       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 (fwrite(_("[Error decoding BASE64]\n"),
                                        sizeof(gchar),
@@ -378,8 +385,14 @@ gboolean procmime_decode_content(MimeInfo *mimeinfo)
                        } else if (len >= 0) {
                                /* print out the error message only once 
                                 * per block */
-                               if (fwrite(outbuf, sizeof(gchar), len, tmpfp) < len)
-                                       err = TRUE;
+                               if (null_bytes) {
+                                       /* we won't uncanonicalize, output to outfp directly */
+                                       if (fwrite(outbuf, sizeof(gchar), len, outfp) < len)
+                                               err = TRUE;
+                               } else {
+                                       if (fwrite(outbuf, sizeof(gchar), len, tmpfp) < len)
+                                               err = TRUE;
+                               }
                                got_error = FALSE;
                        }
                }
@@ -640,6 +653,7 @@ gint procmime_get_part(const gchar *outfile, MimeInfo *mimeinfo)
        while ((restlength > 0) && ((readlength = fread(buf, 1, restlength > BUFFSIZE ? BUFFSIZE : restlength, infp)) > 0)) {
                if (fwrite(buf, 1, readlength, outfp) != readlength) {
                        saved_errno = errno;
+                       fclose(infp);
                        fclose(outfp);
                        return -(saved_errno);
                }
@@ -657,7 +671,7 @@ gint procmime_get_part(const gchar *outfile, MimeInfo *mimeinfo)
        return 0;
 }
 
-static FILE *procmime_get_text_content(MimeInfo *mimeinfo)
+FILE *procmime_get_text_content(MimeInfo *mimeinfo)
 {
        FILE *tmpfp, *outfp;
        const gchar *src_codeset;
@@ -698,8 +712,15 @@ static FILE *procmime_get_text_content(MimeInfo *mimeinfo)
                      ? forced_charset : 
                      procmime_mimeinfo_get_parameter(mimeinfo, "charset");
 
+       /* use supersets transparently when possible */
        if (!forced_charset && src_codeset && !strcasecmp(src_codeset, CS_ISO_8859_1))
                src_codeset = CS_WINDOWS_1252;
+       else if (!forced_charset && src_codeset && !strcasecmp(src_codeset, CS_X_GBK))
+               src_codeset = CS_GB18030;
+       else if (!forced_charset && src_codeset && !strcasecmp(src_codeset, CS_GBK))
+               src_codeset = CS_GB18030;
+       else if (!forced_charset && src_codeset && !strcasecmp(src_codeset, CS_GB2312))
+               src_codeset = CS_GB18030;
 
        if (mimeinfo->type == MIMETYPE_TEXT && !g_ascii_strcasecmp(mimeinfo->subtype, "html")) {
                SC_HTMLParser *parser;
@@ -756,6 +777,38 @@ static FILE *procmime_get_text_content(MimeInfo *mimeinfo)
        return outfp;
 }
 
+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 = g_fopen(tmpfile, "rb");
+       if (outfp == NULL) {
+               g_unlink(tmpfile);
+               g_free(tmpfile);
+               return NULL;
+       }
+
+       g_unlink(tmpfile);
+       g_free(tmpfile);
+
+       return outfp;
+}
+
 /* search the first text part of (multipart) MIME message,
    decode, convert it and output to outfp. */
 FILE *procmime_get_first_text_content(MsgInfo *msginfo)
@@ -763,8 +816,8 @@ FILE *procmime_get_first_text_content(MsgInfo *msginfo)
        FILE *outfp = NULL;
        MimeInfo *mimeinfo, *partinfo;
        gboolean empty_ok = FALSE, short_scan = TRUE;
-       START_TIMING("");
-       cm_return_val_if_fail(msginfo != NULL, NULL);
+
+       cm_return_val_if_fail(msginfo != NULL, NULL);
 
        /* first we try to short-scan (for speed), refusing empty parts */
 scan_again:
@@ -797,7 +850,6 @@ scan_again:
                goto scan_again;
        }
        procmime_mimeinfo_free_all(mimeinfo);
-       END_TIMING();
        return outfp;
 }
 
@@ -882,68 +934,6 @@ gboolean procmime_msginfo_is_encrypted(MsgInfo *msginfo)
        return result;
 }
 
-static gboolean procmime_find_string_part(MimeInfo *mimeinfo, const gchar *filename,
-                                  const gchar *str, StrFindFunc find_func)
-{
-       FILE *outfp;
-       gchar buf[BUFFSIZE];
-
-       cm_return_val_if_fail(mimeinfo != NULL, FALSE);
-       cm_return_val_if_fail(mimeinfo->type == MIMETYPE_TEXT, FALSE);
-       cm_return_val_if_fail(str != NULL, FALSE);
-       cm_return_val_if_fail(find_func != NULL, FALSE);
-
-       outfp = procmime_get_text_content(mimeinfo);
-
-       if (!outfp)
-               return FALSE;
-
-       while (fgets(buf, sizeof(buf), outfp) != NULL) {
-               strretchomp(buf);
-               if (find_func(buf, str)) {
-                       fclose(outfp);
-                       return TRUE;
-               }
-       }
-
-       fclose(outfp);
-
-       return FALSE;
-}
-
-gboolean procmime_find_string(MsgInfo *msginfo, const gchar *str,
-                             StrFindFunc find_func)
-{
-       MimeInfo *mimeinfo;
-       MimeInfo *partinfo;
-       gchar *filename;
-       gboolean found = FALSE;
-
-       cm_return_val_if_fail(msginfo != NULL, FALSE);
-       cm_return_val_if_fail(str != NULL, FALSE);
-       cm_return_val_if_fail(find_func != NULL, FALSE);
-
-       filename = procmsg_get_message_file(msginfo);
-       if (!filename) return FALSE;
-       mimeinfo = procmime_scan_message(msginfo);
-
-       for (partinfo = mimeinfo; partinfo != NULL;
-            partinfo = procmime_mimeinfo_next(partinfo)) {
-               if (partinfo->type == MIMETYPE_TEXT) {
-                       if (procmime_find_string_part
-                               (partinfo, filename, str, find_func) == TRUE) {
-                               found = TRUE;
-                               break;
-                       }
-               }
-       }
-
-       procmime_mimeinfo_free_all(mimeinfo);
-       g_free(filename);
-
-       return found;
-}
-
 gchar *procmime_get_tmp_file_name(MimeInfo *mimeinfo)
 {
        static guint32 id = 0;
@@ -1364,32 +1354,32 @@ static void procmime_parse_message_rfc822(MimeInfo *mimeinfo, gboolean short_sca
        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);
+               tmp = conv_unmime_header(hentry[0].body, NULL, FALSE);
                 g_free(hentry[0].body);
                 hentry[0].body = tmp;
         }                
        if (hentry[2].body != NULL) {
-                tmp = conv_unmime_header(hentry[2].body, NULL);
+               tmp = conv_unmime_header(hentry[2].body, NULL, FALSE);
                 g_free(hentry[2].body);
                 hentry[2].body = tmp;
         }                
        if (hentry[4].body != NULL) {
-                tmp = conv_unmime_header(hentry[4].body, NULL);
+               tmp = conv_unmime_header(hentry[4].body, NULL, FALSE);
                 g_free(hentry[4].body);
                 hentry[4].body = tmp;
         }                
        if (hentry[5].body != NULL) {
-                tmp = conv_unmime_header(hentry[5].body, NULL);
+               tmp = conv_unmime_header(hentry[5].body, NULL, FALSE);
                 g_free(hentry[5].body);
                 hentry[5].body = tmp;
         }                
        if (hentry[7].body != NULL) {
-                tmp = conv_unmime_header(hentry[7].body, NULL);
+               tmp = conv_unmime_header(hentry[7].body, NULL, FALSE);
                 g_free(hentry[7].body);
                 hentry[7].body = tmp;
         }
        if (hentry[8].body != NULL) {
-                tmp = conv_unmime_header(hentry[8].body, NULL);
+               tmp = conv_unmime_header(hentry[8].body, NULL, FALSE);
                 g_free(hentry[8].body);
                 hentry[8].body = tmp;
         }
@@ -1442,8 +1432,10 @@ static void procmime_parse_disposition_notification(MimeInfo *mimeinfo,
        } else {
                procheader_get_header_fields(fp, hentry);
        }
+    
+        fclose(fp);
 
-       if (!hentry[0].body || !hentry[1].body) {
+       if (!hentry[0].body || !hentry[1].body) {
                debug_print("MsgId %s, Disp %s\n",
                        hentry[0].body ? hentry[0].body:"(nil)",
                        hentry[1].body ? hentry[1].body:"(nil)");
@@ -1487,32 +1479,32 @@ bail:
 #define GET_HEADERS() {                                                \
        procheader_get_header_fields(fp, hentry);               \
         if (hentry[0].body != NULL) {                          \
-                tmp = conv_unmime_header(hentry[0].body, NULL);        \
+               tmp = conv_unmime_header(hentry[0].body, NULL, FALSE);  \
                 g_free(hentry[0].body);                                \
                 hentry[0].body = tmp;                          \
         }                                                      \
         if (hentry[2].body != NULL) {                          \
-                tmp = conv_unmime_header(hentry[2].body, NULL);        \
+               tmp = conv_unmime_header(hentry[2].body, NULL, FALSE);  \
                 g_free(hentry[2].body);                                \
                 hentry[2].body = tmp;                          \
         }                                                      \
         if (hentry[4].body != NULL) {                          \
-                tmp = conv_unmime_header(hentry[4].body, NULL);        \
+               tmp = conv_unmime_header(hentry[4].body, NULL, FALSE);  \
                 g_free(hentry[4].body);                                \
                 hentry[4].body = tmp;                          \
         }                                                      \
         if (hentry[5].body != NULL) {                          \
-                tmp = conv_unmime_header(hentry[5].body, NULL);        \
+               tmp = conv_unmime_header(hentry[5].body, NULL, FALSE);  \
                 g_free(hentry[5].body);                                \
                 hentry[5].body = tmp;                          \
         }                                                      \
        if (hentry[6].body != NULL) {                           \
-                tmp = conv_unmime_header(hentry[6].body, NULL);        \
+               tmp = conv_unmime_header(hentry[6].body, NULL, FALSE);  \
                 g_free(hentry[6].body);                                \
                 hentry[6].body = tmp;                          \
         }                                                      \
        if (hentry[7].body != NULL) {                           \
-                tmp = conv_unmime_header(hentry[7].body, NULL);        \
+               tmp = conv_unmime_header(hentry[7].body, NULL, FALSE);  \
                 g_free(hentry[7].body);                                \
                 hentry[7].body = tmp;                          \
         }                                                      \
@@ -1680,6 +1672,8 @@ static void parse_parameters(const gchar *parameters, GHashTable *table)
                                dstpos++;
                        }
                        *dstpos = '\0';
+                       if (value[0] == '"')
+                               extract_quote(value, '"');
                } else {
                        if (value[0] == '"')
                                extract_quote(value, '"');
@@ -2222,7 +2216,11 @@ static void write_parameters(gpointer key, gpointer value, gpointer user_data)
                                g_string_append_printf(buf, "%%%s", hexstr);
                        }
                }
-               break;          
+               break;
+#else
+       case ENC_AS_EXTENDED:
+               debug_print("Unhandled ENC_AS_EXTENDED.");
+               break;
 #endif
        case ENC_AS_ENCWORD:
                len = MAX(strlen(val)*6, 512);