2006-02-13 [colin] 2.0.0cvs48
[claws.git] / src / procmime.c
index 4f7e38452c70ebaa11913d84c7a75defee189768..767356baba15f399d0465f5808bb3552dc4742d7 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2004 Hiroyuki Yamamoto & The Sylpheed-Claws Team
+ * Copyright (C) 1999-2006 Hiroyuki Yamamoto & The Sylpheed-Claws 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
@@ -14,7 +14,7 @@
  *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  */
 
 #ifdef HAVE_CONFIG_H
@@ -28,7 +28,9 @@
 #include <glib/gi18n.h>
 #include <stdio.h>
 #include <string.h>
-#include <locale.h>
+#if HAVE_LOCALE_H
+#  include <locale.h>
+#endif
 #include <ctype.h>
 #include <sys/types.h>
 #include <sys/stat.h>
@@ -299,6 +301,7 @@ gboolean procmime_decode_content(MimeInfo *mimeinfo)
        outfp = get_tmpfile_in_dir(get_mime_tmp_dir(), &tmpfilename);
        if (!outfp) {
                perror("tmpfile");
+               fclose(infp);
                return FALSE;
        }
        tmp_file = TRUE;
@@ -325,6 +328,7 @@ gboolean procmime_decode_content(MimeInfo *mimeinfo)
                        if (!tmpfp) {
                                perror("tmpfile");
                                if (tmp_file) fclose(outfp);
+                               fclose(infp);
                                return FALSE;
                        }
                }
@@ -409,6 +413,9 @@ gboolean procmime_encode_content(MimeInfo *mimeinfo, EncodingType encoding)
        gchar *tmpfilename;
        struct stat statbuf;
 
+       if (mimeinfo->content == MIMECONTENT_EMPTY)
+               return TRUE;
+
        if (mimeinfo->encoding_type != ENC_UNKNOWN &&
            mimeinfo->encoding_type != ENC_BINARY &&
            mimeinfo->encoding_type != ENC_7BIT &&
@@ -733,7 +740,7 @@ FILE *procmime_get_text_content(MimeInfo *mimeinfo)
                oldout = dup(1);
                
                dup2(fileno(outfp), 1);
-               
+
                p = popen(renderer->renderer, "w");
                if (p != NULL) {
                        size_t count;
@@ -748,15 +755,15 @@ FILE *procmime_get_text_content(MimeInfo *mimeinfo)
                dup2(oldout, 1);
 /* CodeConverter seems to have no effect here */
        } else if (mimeinfo->type == MIMETYPE_TEXT && !g_ascii_strcasecmp(mimeinfo->subtype, "html")) {
-               HTMLParser *parser;
+               SC_HTMLParser *parser;
                CodeConverter *conv;
 
                conv = conv_code_converter_new(src_codeset);
-               parser = html_parser_new(tmpfp, conv);
-               while ((str = html_parse(parser)) != NULL) {
+               parser = sc_html_parser_new(tmpfp, conv);
+               while ((str = sc_html_parse(parser)) != NULL) {
                        fputs(str, outfp);
                }
-               html_parser_destroy(parser);
+               sc_html_parser_destroy(parser);
                conv_code_converter_destroy(conv);
        } else if (mimeinfo->type == MIMETYPE_TEXT && !g_ascii_strcasecmp(mimeinfo->subtype, "enriched")) {
                ERTFParser *parser;
@@ -977,8 +984,10 @@ gchar *procmime_get_tmp_file_name(MimeInfo *mimeinfo)
                if (basetmp == NULL)
                        basetmp = "mimetmp";
                basetmp = g_path_get_basename(basetmp);
-               if (*basetmp == '\0') basetmp = g_strdup("mimetmp");
+               if (*basetmp == '\0') 
+                       basetmp = g_strdup("mimetmp");
                base = conv_filename_from_utf8(basetmp);
+               g_free((gchar*)basetmp);
                subst_for_shellsafe_filename(base);
        }
 
@@ -1173,7 +1182,7 @@ EncodingType procmime_get_encoding_for_charset(const gchar *charset)
                return ENC_8BIT;
 }
 
-EncodingType procmime_get_encoding_for_text_file(const gchar *file)
+EncodingType procmime_get_encoding_for_text_file(const gchar *file, gboolean *has_binary)
 {
        FILE *fp;
        guchar buf[BUFFSIZE];
@@ -1181,6 +1190,7 @@ EncodingType procmime_get_encoding_for_text_file(const gchar *file)
        size_t octet_chars = 0;
        size_t total_len = 0;
        gfloat octet_percentage;
+       gboolean force_b64 = FALSE;
 
        if ((fp = g_fopen(file, "rb")) == NULL) {
                FILE_OP_ERROR(file, "fopen");
@@ -1194,6 +1204,10 @@ EncodingType procmime_get_encoding_for_text_file(const gchar *file)
                for (p = buf, i = 0; i < len; ++p, ++i) {
                        if (*p & 0x80)
                                ++octet_chars;
+                       if (*p == '\0') {
+                               force_b64 = TRUE;
+                               *has_binary = TRUE;
+                       }
                }
                total_len += len;
        }
@@ -1209,7 +1223,7 @@ EncodingType procmime_get_encoding_for_text_file(const gchar *file)
                    "8bit chars: %d / %d (%f%%)\n", octet_chars, total_len,
                    100.0 * octet_percentage);
 
-       if (octet_percentage > 0.20) {
+       if (octet_percentage > 0.20 || force_b64) {
                debug_print("using BASE64\n");
                return ENC_BASE64;
        } else if (octet_chars > 0) {
@@ -1479,6 +1493,8 @@ static void parse_parameters(const gchar *parameters, GHashTable *table)
 
                value[0] = '\0';
                value++;
+               while (value[0] == ' ')
+                       value++;
 
                g_strdown(attribute);
 
@@ -1515,6 +1531,18 @@ static void parse_parameters(const gchar *parameters, GHashTable *table)
                                *tmp = '\0';
                }
 
+               if (attribute) {
+                       while (attribute[0] == ' ')
+                               attribute++;
+                       while (attribute[strlen(attribute)-1] == ' ') 
+                               attribute[strlen(attribute)-1] = '\0';
+               } 
+               if (value) {
+                       while (value[0] == ' ')
+                               value++;
+                       while (value[strlen(value)-1] == ' ') 
+                               value[strlen(value)-1] = '\0';
+               }               
                if (strrchr(attribute, '*') != NULL) {
                        gchar *tmpattr;
 
@@ -1568,7 +1596,8 @@ static void parse_parameters(const gchar *parameters, GHashTable *table)
                gchar *charset, *lang, *oldvalue, *newvalue;
 
                attribute = (gchar *) cur->data;
-               if (!g_hash_table_lookup_extended(table, attribute, (gpointer *) &key, (gpointer *) &value))
+               if (!g_hash_table_lookup_extended(
+                       table, attribute, (gpointer *)(gchar *) &key, (gpointer *)(gchar *) &value))
                        continue;
 
                charset = value;
@@ -1608,10 +1637,12 @@ static void procmime_parse_content_type(const gchar *content_type, MimeInfo *mim
                mimeinfo->type = MIMETYPE_TEXT;
                mimeinfo->subtype = g_strdup("plain");
                if (g_hash_table_lookup(mimeinfo->typeparameters,
-                                      "charset") == NULL)
+                                      "charset") == NULL) {
                        g_hash_table_insert(mimeinfo->typeparameters,
-                                           g_strdup("charset"),
-                                           g_strdup("us-ascii"));
+                                   g_strdup("charset"),
+                                   g_strdup(
+                                       conv_get_locale_charset_str_no_utf8()));
+               }
        } else {
                gchar *type, *subtype, *params;
 
@@ -1712,8 +1743,12 @@ int procmime_parse_mimepart(MimeInfo *parent,
                mimeinfo->type = MIMETYPE_TEXT;
                mimeinfo->subtype = g_strdup("plain");
                if (g_hash_table_lookup(mimeinfo->typeparameters,
-                                      "charset") == NULL)
-                       g_hash_table_insert(mimeinfo->typeparameters, g_strdup("charset"), g_strdup("us-ascii"));
+                                      "charset") == NULL) {
+                       g_hash_table_insert(mimeinfo->typeparameters,
+                                   g_strdup("charset"),
+                                   g_strdup(
+                                       conv_get_locale_charset_str_no_utf8()));
+               }
        }
 
        if (content_encoding != NULL) {
@@ -2163,8 +2198,9 @@ gint procmime_write_mimeinfo(MimeInfo *mimeinfo, FILE *fp)
                /* Call writer for mime type */
                switch (mimeinfo->type) {
                case MIMETYPE_MESSAGE:
-                       if (g_ascii_strcasecmp(mimeinfo->subtype, "rfc822") == 0)
+                       if (g_ascii_strcasecmp(mimeinfo->subtype, "rfc822") == 0) {
                                return procmime_write_message_rfc822(mimeinfo, fp);
+                       }
                        break;
                        
                case MIMETYPE_MULTIPART: