2005-10-25 [colin] 1.9.15cvs107
[claws.git] / src / procmime.c
index 9a98522795f47668d84e0e05ee47f3f9765455fc..f281ebf1209c531dd1d7d1193526f597066d8f8b 100644 (file)
@@ -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
@@ -409,6 +409,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 +736,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;
@@ -977,8 +980,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(basetmp);
                subst_for_shellsafe_filename(base);
        }
 
@@ -1479,6 +1484,8 @@ static void parse_parameters(const gchar *parameters, GHashTable *table)
 
                value[0] = '\0';
                value++;
+               while (value[0] == ' ')
+                       value++;
 
                g_strdown(attribute);
 
@@ -1515,6 +1522,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 +1587,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 +1628,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 +1734,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) {
@@ -1846,24 +1872,26 @@ typedef enum {
     ENC_AS_TOKEN,
     ENC_AS_QUOTED_STRING,
     ENC_AS_EXTENDED,
+    ENC_TO_ASCII,
 } EncodeAs;
 
 typedef struct _ParametersData {
        FILE *fp;
        guint len;
+       guint ascii_only;
 } ParametersData;
 
 static void write_parameters(gpointer key, gpointer value, gpointer user_data)
 {
        gchar *param = key;
-       gchar *val = value, *valpos;
+       gchar *val = value, *valpos, *tmp;
        ParametersData *pdata = (ParametersData *)user_data;
        GString *buf = g_string_new("");
 
        EncodeAs encas = ENC_AS_TOKEN;
 
        for (valpos = val; *valpos != 0; valpos++) {
-               if (!IS_ASCII(*valpos)) {
+               if (!IS_ASCII(*valpos) || *valpos == '"') {
                        encas = ENC_AS_EXTENDED;
                        break;
                }
@@ -1886,7 +1914,7 @@ static void write_parameters(gpointer key, gpointer value, gpointer user_data)
                case ';':
                case ':':
                case '\\':
-               case '"':
+               case '\'':
                case '/':
                case '[':
                case ']':
@@ -1896,12 +1924,26 @@ static void write_parameters(gpointer key, gpointer value, gpointer user_data)
                        continue;
                }
        }
+       
+       if (encas == ENC_AS_EXTENDED && pdata->ascii_only == TRUE) 
+               encas = ENC_TO_ASCII;
 
        switch (encas) {
        case ENC_AS_TOKEN:
                g_string_append_printf(buf, "%s=%s", param, val);
                break;
 
+       case ENC_TO_ASCII:
+               tmp = g_strdup(val);
+               g_strcanon(tmp, 
+                       " ()<>@,';:\\/[]?=.0123456789"
+                       "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
+                       "abcdefghijklmnopqrstuvwxyz",
+                       '_');
+               g_string_append_printf(buf, "%s=\"%s\"", param, tmp);
+               g_free(tmp);
+               break;
+
        case ENC_AS_QUOTED_STRING:
                g_string_append_printf(buf, "%s=\"%s\"", param, val);
                break;
@@ -1922,7 +1964,7 @@ static void write_parameters(gpointer key, gpointer value, gpointer user_data)
                                g_string_append_printf(buf, "%%%s", hexstr);
                        }
                }
-               break;
+               break;          
        }
        
        if (buf->str && strlen(buf->str)) {
@@ -1944,6 +1986,7 @@ void procmime_write_mime_header(MimeInfo *mimeinfo, FILE *fp)
        debug_print("procmime_write_mime_header\n");
        
        pdata->fp = fp;
+       pdata->ascii_only = FALSE;
 
        for (type_table = mime_type_table; type_table->str != NULL; type_table++)
                if (mimeinfo->type == type_table->type) {
@@ -1951,6 +1994,7 @@ void procmime_write_mime_header(MimeInfo *mimeinfo, FILE *fp)
                                "Content-Type: %s/%s", type_table->str, mimeinfo->subtype);
                        fprintf(fp, "%s", buf);
                        pdata->len = strlen(buf);
+                       pdata->ascii_only = TRUE;
                        g_free(buf);
                        break;
                }
@@ -1983,6 +2027,8 @@ void procmime_write_mime_header(MimeInfo *mimeinfo, FILE *fp)
                g_free(buf);
 
                pdata->fp = fp;
+               pdata->ascii_only = FALSE;
+
                g_hash_table_foreach(mimeinfo->dispositionparameters, write_parameters, pdata);
                g_free(pdata);
                fprintf(fp, "\n");
@@ -2143,8 +2189,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: