2008-09-18 [colin] 3.5.0cvs113
[claws.git] / src / procmime.c
index 79cfeaeaaac302c4df61d8c4dddc8599b3815b63..bfdf08427af391ffe4fd1bf8ec2a0634d4a99fd0 100644 (file)
@@ -110,7 +110,7 @@ static gboolean free_func(GNode *node, gpointer data)
        switch (mimeinfo->content) {
        case MIMECONTENT_FILE:
                if (mimeinfo->tmp)
-                       g_unlink(mimeinfo->data.filename);
+                       claws_unlink(mimeinfo->data.filename);
                g_free(mimeinfo->data.filename);
                break;
 
@@ -438,7 +438,7 @@ gboolean procmime_decode_content(MimeInfo *mimeinfo)
 
        stat(tmpfilename, &statbuf);
        if (mimeinfo->tmp && (mimeinfo->data.filename != NULL))
-               g_unlink(mimeinfo->data.filename);
+               claws_unlink(mimeinfo->data.filename);
        g_free(mimeinfo->data.filename);
        mimeinfo->data.filename = tmpfilename;
        mimeinfo->tmp = TRUE;
@@ -503,7 +503,7 @@ gboolean procmime_encode_content(MimeInfo *mimeinfo, EncodingType encoding)
                                }
                                if ((tmp_fp = g_fopen(tmp_file, "rb")) == NULL) {
                                        FILE_OP_ERROR(tmp_file, "fopen");
-                                       g_unlink(tmp_file);
+                                       claws_unlink(tmp_file);
                                        g_free(tmp_file);
                                        fclose(infp);
                                        return FALSE;
@@ -538,7 +538,7 @@ gboolean procmime_encode_content(MimeInfo *mimeinfo, EncodingType encoding)
 
                if (tmp_file) {
                        fclose(tmp_fp);
-                       g_unlink(tmp_file);
+                       claws_unlink(tmp_file);
                        g_free(tmp_file);
                }
        } else if (encoding == ENC_QUOTED_PRINTABLE) {
@@ -579,7 +579,7 @@ gboolean procmime_encode_content(MimeInfo *mimeinfo, EncodingType encoding)
 
        if (mimeinfo->content == MIMECONTENT_FILE) {
                if (mimeinfo->tmp && (mimeinfo->data.filename != NULL))
-                       g_unlink(mimeinfo->data.filename);
+                       claws_unlink(mimeinfo->data.filename);
                g_free(mimeinfo->data.filename);
        } else if (mimeinfo->content == MIMECONTENT_MEM) {
                if (mimeinfo->tmp && (mimeinfo->data.mem != NULL))
@@ -643,7 +643,7 @@ gint procmime_get_part(const gchar *outfile, MimeInfo *mimeinfo)
        if (fclose(outfp) == EOF) {
                saved_errno = errno;
                FILE_OP_ERROR(outfile, "fclose");
-               g_unlink(outfile);
+               claws_unlink(outfile);
                return -(saved_errno);
        }
 
@@ -691,6 +691,9 @@ static FILE *procmime_get_text_content(MimeInfo *mimeinfo)
                      ? forced_charset : 
                      procmime_mimeinfo_get_parameter(mimeinfo, "charset");
 
+       if (!forced_charset && !strcasecmp(src_codeset, CS_ISO_8859_1))
+               src_codeset = CS_WINDOWS_1252;
+
        if (mimeinfo->type == MIMETYPE_TEXT && !g_ascii_strcasecmp(mimeinfo->subtype, "html")) {
                SC_HTMLParser *parser;
                CodeConverter *conv;
@@ -735,7 +738,7 @@ static FILE *procmime_get_text_content(MimeInfo *mimeinfo)
 
        fclose(tmpfp);
        rewind(outfp);
-       g_unlink(tmpfile);
+       claws_unlink(tmpfile);
        g_free(tmpfile);
 
        if (err == TRUE) {
@@ -752,19 +755,40 @@ FILE *procmime_get_first_text_content(MsgInfo *msginfo)
 {
        FILE *outfp = NULL;
        MimeInfo *mimeinfo, *partinfo;
+       gboolean empty_ok = FALSE, short_scan = TRUE;
        START_TIMING("");
        g_return_val_if_fail(msginfo != NULL, NULL);
 
-       mimeinfo = procmime_scan_message_short(msginfo);
+       /* first we try to short-scan (for speed), refusing empty parts */
+scan_again:
+       if (short_scan)
+               mimeinfo = procmime_scan_message_short(msginfo);
+       else
+               mimeinfo = procmime_scan_message(msginfo);
        if (!mimeinfo) return NULL;
 
        partinfo = mimeinfo;
-       while (partinfo && partinfo->type != MIMETYPE_TEXT) {
+       while (partinfo && (partinfo->type != MIMETYPE_TEXT ||
+              (partinfo->length == 0 && !empty_ok))) {
                partinfo = procmime_mimeinfo_next(partinfo);
        }
        if (partinfo)
                outfp = procmime_get_text_content(partinfo);
-
+       else if (!empty_ok && short_scan) {
+               /* if short scan didn't find a non-empty part, rescan
+                * fully for non-empty parts
+                */
+               short_scan = FALSE;
+               procmime_mimeinfo_free_all(mimeinfo);
+               goto scan_again;
+       } else if (!empty_ok && !short_scan) {
+               /* if full scan didn't find a non-empty part, rescan
+                * accepting empty parts 
+                */
+               empty_ok = TRUE;
+               procmime_mimeinfo_free_all(mimeinfo);
+               goto scan_again;
+       }
        procmime_mimeinfo_free_all(mimeinfo);
        END_TIMING();
        return outfp;
@@ -820,6 +844,8 @@ FILE *procmime_get_first_encrypted_text_content(MsgInfo *msginfo)
        partinfo = mimeinfo;
        while (partinfo && partinfo->type != MIMETYPE_TEXT) {
                partinfo = procmime_mimeinfo_next(partinfo);
+               if (privacy_mimeinfo_is_signed(partinfo))
+                       procmsg_msginfo_set_flags(msginfo, 0, MSG_SIGNED);
        }
 
        if (partinfo)
@@ -965,12 +991,11 @@ gchar *procmime_get_mime_type(const gchar *filename)
 
        base = g_path_get_basename(filename);
        if ((p = strrchr(base, '.')) != NULL)
-               Xstrdup_a(ext, p + 1, p = NULL );
+               ext = g_utf8_strdown(p + 1, -1);
        g_free(base);
-       if (!p) return NULL;
 
-       g_strdown(ext);
        mime_type = g_hash_table_lookup(mime_type_table, ext);
+       g_free(ext);
        if (mime_type) {
                gchar *str;
 
@@ -1027,8 +1052,6 @@ static GHashTable *procmime_get_mime_type_table(void)
 
                exts = g_strsplit(mime_type->extension, " ", 16);
                for (i = 0; exts[i] != NULL; i++) {
-                       /* make the key case insensitive */
-                       g_strdown(exts[i]);
                        /* use previously dup'd key on overwriting */
                        if (g_hash_table_lookup(table, exts[i]))
                                key = exts[i];
@@ -1055,7 +1078,7 @@ GList *procmime_get_mime_type_list(void)
        if (mime_type_list) 
                return mime_type_list;
        
-#if defined(__NetBSD__)
+#if defined(__NetBSD__) || defined(__OpenBSD__) || defined(__FreeBSD__)
        if ((fp = g_fopen(DATAROOTDIR "/mime/globs", "rb")) == NULL) 
 #else
        if ((fp = g_fopen("/usr/share/mime/globs", "rb")) == NULL) 
@@ -1104,7 +1127,7 @@ GList *procmime_get_mime_type_list(void)
                }
 
                if (*p)
-                       mime_type->extension = g_strdup(p);
+                       mime_type->extension = g_utf8_strdown(p, -1);
                else
                        mime_type->extension = NULL;
 
@@ -1565,7 +1588,7 @@ static void parse_parameters(const gchar *parameters, GHashTable *table)
        param = params;
        next = params;
        for (; next != NULL; param = next) {
-               gchar *attribute, *value, *tmp;
+               gchar *attribute, *value, *tmp, *down_attr, *orig_down_attr;
                gint len;
                gboolean convert = FALSE;
 
@@ -1587,14 +1610,15 @@ static void parse_parameters(const gchar *parameters, GHashTable *table)
                while (value[0] == ' ')
                        value++;
 
-               g_strdown(attribute);
-
-               len = strlen(attribute);
-               if (attribute[len - 1] == '*') {
+               down_attr = g_utf8_strdown(attribute, -1);
+               orig_down_attr = down_attr;
+       
+               len = strlen(down_attr);
+               if (down_attr[len - 1] == '*') {
                        gchar *srcpos, *dstpos, *endpos;
 
                        convert = TRUE;
-                       attribute[len - 1] = '\0';
+                       down_attr[len - 1] = '\0';
 
                        srcpos = value;
                        dstpos = value;
@@ -1622,11 +1646,11 @@ 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 (down_attr) {
+                       while (down_attr[0] == ' ')
+                               down_attr++;
+                       while (down_attr[strlen(down_attr)-1] == ' ') 
+                               down_attr[strlen(down_attr)-1] = '\0';
                } 
                if (value) {
                        while (value[0] == ' ')
@@ -1634,28 +1658,29 @@ static void parse_parameters(const gchar *parameters, GHashTable *table)
                        while (value[strlen(value)-1] == ' ') 
                                value[strlen(value)-1] = '\0';
                }               
-               if (strrchr(attribute, '*') != NULL) {
+               if (strrchr(down_attr, '*') != NULL) {
                        gchar *tmpattr;
 
-                       tmpattr = g_strdup(attribute);
+                       tmpattr = g_strdup(down_attr);
                        tmp = strrchr(tmpattr, '*');
                        tmp[0] = '\0';
 
                        if ((tmp[1] == '0') && (tmp[2] == '\0') && 
-                           (g_slist_find_custom(concatlist, attribute, g_str_equal) == NULL))
+                           (g_slist_find_custom(concatlist, down_attr, g_str_equal) == NULL))
                                concatlist = g_slist_prepend(concatlist, g_strdup(tmpattr));
 
-                       if (convert && (g_slist_find_custom(convlist, attribute, g_str_equal) == NULL))
+                       if (convert && (g_slist_find_custom(convlist, down_attr, g_str_equal) == NULL))
                                convlist = g_slist_prepend(convlist, g_strdup(tmpattr));
 
                        g_free(tmpattr);
                } else if (convert) {
-                       if (g_slist_find_custom(convlist, attribute, g_str_equal) == NULL)
-                               convlist = g_slist_prepend(convlist, g_strdup(attribute));
+                       if (g_slist_find_custom(convlist, down_attr, g_str_equal) == NULL)
+                               convlist = g_slist_prepend(convlist, g_strdup(down_attr));
                }
 
-               if (g_hash_table_lookup(table, attribute) == NULL)
-                       g_hash_table_insert(table, g_strdup(attribute), g_strdup(value));
+               if (g_hash_table_lookup(table, down_attr) == NULL)
+                       g_hash_table_insert(table, g_strdup(down_attr), g_strdup(value));
+               g_free(orig_down_attr);
        }
 
        for (cur = concatlist; cur != NULL; cur = g_slist_next(cur)) {
@@ -1746,7 +1771,7 @@ static void procmime_parse_content_type(const gchar *content_type, MimeInfo *mim
                }
 
                mimeinfo->type = procmime_get_media_type(type);
-               mimeinfo->subtype = g_strdup(subtype);
+               mimeinfo->subtype = g_strstrip(g_strdup(subtype));
 
                /* Get mimeinfo->typeparameters */
                if (params != NULL)