2004-11-05 [colin] 0.9.12cvs139.1
[claws.git] / src / procmime.c
index 6f07f6b1923d1458fb85a29209f57799993e8e16..02b7f2b09563f31c7f4039ca9c5f11236f8a7496 100644 (file)
@@ -372,9 +372,9 @@ gboolean procmime_encode_content(MimeInfo *mimeinfo, EncodingType encoding)
        gchar *tmpfilename;
        struct stat statbuf;
 
-       if (mimeinfo->encoding_type != ENC_UNKNOWN ||
-           mimeinfo->encoding_type != ENC_BINARY ||
-           mimeinfo->encoding_type != ENC_7BIT ||
+       if (mimeinfo->encoding_type != ENC_UNKNOWN &&
+           mimeinfo->encoding_type != ENC_BINARY &&
+           mimeinfo->encoding_type != ENC_7BIT &&
            mimeinfo->encoding_type != ENC_8BIT)
                if(!procmime_decode_content(mimeinfo))
                        return FALSE;
@@ -385,9 +385,15 @@ gboolean procmime_encode_content(MimeInfo *mimeinfo, EncodingType encoding)
                return FALSE;
        }
 
-       if ((infp = fopen(mimeinfo->data.filename, "rb")) == NULL) {
-               g_warning("Can't open file %s\n", mimeinfo->data.filename);
-               return FALSE;
+       if (mimeinfo->content == MIMECONTENT_FILE) {
+               if ((infp = fopen(mimeinfo->data.filename, "rb")) == NULL) {
+                       g_warning("Can't open file %s\n", mimeinfo->data.filename);
+                       return FALSE;
+               }
+       } else if (mimeinfo->content == MIMECONTENT_MEM) {
+               infp = str_open_as_stream(mimeinfo->data.mem);
+               if (infp == NULL)
+                       return FALSE;
        }
 
        if (encoding == ENC_BASE64) {
@@ -424,10 +430,17 @@ gboolean procmime_encode_content(MimeInfo *mimeinfo, EncodingType encoding)
        fclose(outfp);
        fclose(infp);
 
+       if (mimeinfo->content == MIMECONTENT_FILE) {
+               if (mimeinfo->tmp && (mimeinfo->data.filename != NULL))
+                       unlink(mimeinfo->data.filename);
+               g_free(mimeinfo->data.filename);
+       } else if (mimeinfo->content == MIMECONTENT_MEM) {
+               if (mimeinfo->tmp && (mimeinfo->data.mem != NULL))
+                       g_free(mimeinfo->data.mem);
+       }
+
        stat(tmpfilename, &statbuf);
-       if (mimeinfo->tmp && (mimeinfo->data.filename != NULL))
-               unlink(mimeinfo->data.filename);
-       g_free(mimeinfo->data.filename);
+       mimeinfo->content = MIMECONTENT_FILE;
        mimeinfo->data.filename = tmpfilename;
        mimeinfo->tmp = TRUE;
        mimeinfo->offset = 0;
@@ -1632,8 +1645,6 @@ static void write_parameters(gpointer key, gpointer value, gpointer user_data)
        FILE *fp = user_data;
        EncodeAs encas = ENC_AS_TOKEN;
 
-       fprintf(fp, "; %s=", param);
-
        for (valpos = val; *valpos != 0; valpos++) {
                if (!IS_ASCII(*valpos)) {
                        encas = ENC_AS_EXTENDED;
@@ -1671,16 +1682,27 @@ static void write_parameters(gpointer key, gpointer value, gpointer user_data)
 
        switch (encas) {
        case ENC_AS_TOKEN:
+               fprintf(fp, "; %s=", param);
                fprintf(fp, "%s", val);
                break;
 
        case ENC_AS_QUOTED_STRING:
+               fprintf(fp, "; %s=", param);
                fprintf(fp, "\"%s\"", val);
                break;
 
        case ENC_AS_EXTENDED:
-               /* FIXME: not yet handled */
-               fprintf(fp, "%s", val);
+               fprintf(fp, "; %s*=", param);
+               fprintf(fp, "%s''", conv_get_current_charset_str());
+               for (valpos = val; *valpos != '\0'; valpos++) {
+                       if (IS_ASCII(*valpos) && isalnum(*valpos))
+                               fprintf(fp, "%c", *valpos);
+                       else {
+                               gchar hexstr[3] = "XX";
+                               get_hex_str(hexstr, *valpos);
+                               fprintf(fp, "%%%s", hexstr);
+                       }
+               }
                break;
        }
 }