2006-08-16 [colin] 2.4.0cvs54
[claws.git] / src / procmime.c
index b6b33d0b33a3cc56642e66c4b75c11b1b50ab686..3e64f262826ab66637079255898096191d8c3f8c 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
 
 #include <stdio.h>
 #include <glib.h>
+#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>
 #include <unistd.h>
 
-#include "intl.h"
 #include "procmime.h"
 #include "procheader.h"
 #include "base64.h"
@@ -46,6 +48,7 @@
 #include "utils.h"
 #include "prefs_common.h"
 #include "prefs_gtk.h"
+#include "alertpanel.h"
 
 static GHashTable *procmime_get_mime_type_table        (void);
 
@@ -102,7 +105,7 @@ static gboolean free_func(GNode *node, gpointer data)
        switch (mimeinfo->content) {
        case MIMECONTENT_FILE:
                if (mimeinfo->tmp)
-                       unlink(mimeinfo->data.filename);
+                       g_unlink(mimeinfo->data.filename);
                g_free(mimeinfo->data.filename);
                break;
 
@@ -116,6 +119,7 @@ static gboolean free_func(GNode *node, gpointer data)
        g_free(mimeinfo->subtype);
        g_free(mimeinfo->description);
        g_free(mimeinfo->id);
+       g_free(mimeinfo->location);
 
        g_hash_table_foreach_remove(mimeinfo->typeparameters,
                procmime_mimeinfo_parameters_destroy, NULL);
@@ -145,53 +149,6 @@ void procmime_mimeinfo_free_all(MimeInfo *mimeinfo)
        g_node_destroy(node);
 }
 
-#if 0 /* UNUSED */
-MimeInfo *procmime_mimeinfo_insert(MimeInfo *parent, MimeInfo *mimeinfo)
-{
-       MimeInfo *child = parent->children;
-
-       if (!child)
-               parent->children = mimeinfo;
-       else {
-               while (child->next != NULL)
-                       child = child->next;
-
-               child->next = mimeinfo;
-       }
-
-       mimeinfo->parent = parent;
-       mimeinfo->level = parent->level + 1;
-
-       return mimeinfo;
-}
-
-void procmime_mimeinfo_replace(MimeInfo *old, MimeInfo *new)
-{
-       MimeInfo *parent = old->parent;
-       MimeInfo *child;
-
-       g_return_if_fail(parent != NULL);
-       g_return_if_fail(new->next == NULL);
-
-       for (child = parent->children; child && child != old;
-            child = child->next)
-               ;
-       if (!child) {
-               g_warning("oops: parent can't find it's own child");
-               return;
-       }
-       procmime_mimeinfo_free_all(old);
-
-       if (child == parent->children) {
-               new->next = parent->children->next;
-               parent->children = new;
-       } else {
-               new->next = child->next;
-               child = new;
-       }
-}
-#endif
-
 MimeInfo *procmime_mimeinfo_parent(MimeInfo *mimeinfo)
 {
        g_return_val_if_fail(mimeinfo != NULL, NULL);
@@ -237,8 +194,8 @@ MimeInfo *procmime_scan_message(MsgInfo *msginfo)
        if (!filename || !is_file_exist(filename)) 
                return NULL;
 
-       if (msginfo->folder->stype != F_QUEUE && 
-           msginfo->folder->stype != F_DRAFT)
+       if (!folder_has_parent_of_type(msginfo->folder, F_QUEUE) &&
+           !folder_has_parent_of_type(msginfo->folder, F_DRAFT))
                mimeinfo = procmime_scan_file(filename);
        else
                mimeinfo = procmime_scan_queue_file(filename);
@@ -286,10 +243,12 @@ gboolean procmime_decode_content(MimeInfo *mimeinfo)
        g_return_val_if_fail(mimeinfo != NULL, FALSE);
 
        if (encoding == ENC_UNKNOWN ||
-           encoding == ENC_BINARY)
+           encoding == ENC_BINARY ||
+           encoding == ENC_7BIT ||
+           encoding == ENC_8BIT)
                return TRUE;
 
-       infp = fopen(mimeinfo->data.filename, "rb");
+       infp = g_fopen(mimeinfo->data.filename, "rb");
        if (!infp) {
                perror("fopen");
                return FALSE;
@@ -299,6 +258,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;
@@ -308,12 +268,13 @@ gboolean procmime_decode_content(MimeInfo *mimeinfo)
                while ((ftell(infp) < readend) && (fgets(buf, sizeof(buf), infp) != NULL)) {
                        gint len;
                        len = qp_decode_line(buf);
-                       fwrite(buf, len, 1, outfp);
+                       fwrite(buf, 1, len, outfp);
                }
        } else if (encoding == ENC_BASE64) {
                gchar outbuf[BUFFSIZE];
                gint len;
                Base64Decoder *decoder;
+               gboolean got_error = FALSE;
                gboolean uncanonicalize = FALSE;
                FILE *tmpfp = outfp;
 
@@ -324,6 +285,7 @@ gboolean procmime_decode_content(MimeInfo *mimeinfo)
                        if (!tmpfp) {
                                perror("tmpfile");
                                if (tmp_file) fclose(outfp);
+                               fclose(infp);
                                return FALSE;
                        }
                }
@@ -331,11 +293,20 @@ 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) {
-                               g_warning("Bad BASE64 content\n");
-                               break;
+                       if (len < 0 && !got_error) {
+                               g_warning("Bad BASE64 content.\n");
+                               fwrite(_("[Error decoding BASE64]\n"),
+                                       sizeof(gchar),
+                                       strlen(_("[Error decoding BASE64]\n")),
+                                       tmpfp);
+                               got_error = TRUE;
+                               continue;
+                       } else if (len >= 0) {
+                               /* print out the error message only once 
+                                * per block */
+                               fwrite(outbuf, sizeof(gchar), len, tmpfp);
+                               got_error = FALSE;
                        }
-                       fwrite(outbuf, sizeof(gchar), len, tmpfp);
                }
                base64_decoder_free(decoder);
 
@@ -377,9 +348,8 @@ gboolean procmime_decode_content(MimeInfo *mimeinfo)
 
        stat(tmpfilename, &statbuf);
        if (mimeinfo->tmp && (mimeinfo->data.filename != NULL))
-               unlink(mimeinfo->data.filename);
-       if (mimeinfo->data.filename != NULL)
-               g_free(mimeinfo->data.filename);
+               g_unlink(mimeinfo->data.filename);
+       g_free(mimeinfo->data.filename);
        mimeinfo->data.filename = tmpfilename;
        mimeinfo->tmp = TRUE;
        mimeinfo->offset = 0;
@@ -394,11 +364,14 @@ gboolean procmime_decode_content(MimeInfo *mimeinfo)
 
 gboolean procmime_encode_content(MimeInfo *mimeinfo, EncodingType encoding)
 {
-       FILE *infp, *outfp;
+       FILE *infp = NULL, *outfp;
        gint len;
        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 &&
@@ -413,7 +386,7 @@ gboolean procmime_encode_content(MimeInfo *mimeinfo, EncodingType encoding)
        }
 
        if (mimeinfo->content == MIMECONTENT_FILE) {
-               if ((infp = fopen(mimeinfo->data.filename, "rb")) == NULL) {
+               if ((infp = g_fopen(mimeinfo->data.filename, "rb")) == NULL) {
                        g_warning("Can't open file %s\n", mimeinfo->data.filename);
                        return FALSE;
                }
@@ -437,9 +410,9 @@ gboolean procmime_encode_content(MimeInfo *mimeinfo, EncodingType encoding)
                                        fclose(infp);
                                        return FALSE;
                                }
-                               if ((tmp_fp = fopen(tmp_file, "rb")) == NULL) {
+                               if ((tmp_fp = g_fopen(tmp_file, "rb")) == NULL) {
                                        FILE_OP_ERROR(tmp_file, "fopen");
-                                       unlink(tmp_file);
+                                       g_unlink(tmp_file);
                                        g_free(tmp_file);
                                        fclose(infp);
                                        return FALSE;
@@ -448,6 +421,7 @@ gboolean procmime_encode_content(MimeInfo *mimeinfo, EncodingType encoding)
                                gchar *out = canonicalize_str(mimeinfo->data.mem);
                                fclose(infp);
                                infp = str_open_as_stream(out);
+                               tmp_fp = infp;
                                g_free(out);
                                if (infp == NULL)
                                        return FALSE;
@@ -469,7 +443,7 @@ gboolean procmime_encode_content(MimeInfo *mimeinfo, EncodingType encoding)
 
                if (tmp_file) {
                        fclose(tmp_fp);
-                       unlink(tmp_file);
+                       g_unlink(tmp_file);
                        g_free(tmp_file);
                }
        } else if (encoding == ENC_QUOTED_PRINTABLE) {
@@ -477,7 +451,16 @@ gboolean procmime_encode_content(MimeInfo *mimeinfo, EncodingType encoding)
 
                while (fgets(inbuf, sizeof(inbuf), infp) != NULL) {
                        qp_encode_line(outbuf, inbuf);
-                       fputs(outbuf, outfp);
+
+                       if (!strncmp("From ", outbuf, sizeof("From ")-1)) {
+                               gchar *tmpbuf = outbuf;
+                               
+                               tmpbuf += sizeof("From ")-1;
+                               
+                               fputs("=46rom ", outfp);
+                               fputs(tmpbuf, outfp);
+                       } else 
+                               fputs(outbuf, outfp);
                }
        } else {
                gchar buf[BUFFSIZE];
@@ -493,7 +476,7 @@ gboolean procmime_encode_content(MimeInfo *mimeinfo, EncodingType encoding)
 
        if (mimeinfo->content == MIMECONTENT_FILE) {
                if (mimeinfo->tmp && (mimeinfo->data.filename != NULL))
-                       unlink(mimeinfo->data.filename);
+                       g_unlink(mimeinfo->data.filename);
                g_free(mimeinfo->data.filename);
        } else if (mimeinfo->content == MIMECONTENT_MEM) {
                if (mimeinfo->tmp && (mimeinfo->data.mem != NULL))
@@ -523,7 +506,7 @@ gint procmime_get_part(const gchar *outfile, MimeInfo *mimeinfo)
        if (mimeinfo->encoding_type != ENC_BINARY && !procmime_decode_content(mimeinfo))
                return -1;
 
-       if ((infp = fopen(mimeinfo->data.filename, "rb")) == NULL) {
+       if ((infp = g_fopen(mimeinfo->data.filename, "rb")) == NULL) {
                FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
                return -1;
        }
@@ -532,7 +515,7 @@ gint procmime_get_part(const gchar *outfile, MimeInfo *mimeinfo)
                fclose(infp);
                return -1;
        }
-       if ((outfp = fopen(outfile, "wb")) == NULL) {
+       if ((outfp = g_fopen(outfile, "wb")) == NULL) {
                FILE_OP_ERROR(outfile, "fopen");
                fclose(infp);
                return -1;
@@ -548,7 +531,7 @@ gint procmime_get_part(const gchar *outfile, MimeInfo *mimeinfo)
        fclose(infp);
        if (fclose(outfp) == EOF) {
                FILE_OP_ERROR(outfile, "fclose");
-               unlink(outfile);
+               g_unlink(outfile);
                return -1;
        }
 
@@ -594,7 +577,7 @@ void renderer_read_config(void)
        renderer_list = NULL;
 
        rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, RENDERER_RC, NULL);
-       f = fopen(rcpath, "rb");
+       f = g_fopen(rcpath, "rb");
        g_free(rcpath);
        
        if (f == NULL)
@@ -674,7 +657,7 @@ FILE *procmime_get_text_content(MimeInfo *mimeinfo)
                return NULL;
        }
 
-       tmpfp = fopen(tmpfile, "rb");
+       tmpfp = g_fopen(tmpfile, "rb");
        if (tmpfp == NULL) {
                g_free(tmpfile);
                return NULL;
@@ -713,7 +696,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;
@@ -726,18 +709,17 @@ FILE *procmime_get_text_content(MimeInfo *mimeinfo)
                }
                
                dup2(oldout, 1);
-#warning FIXME_GTK2 HTML/RTF not yet utf8
 /* 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;
@@ -768,7 +750,7 @@ FILE *procmime_get_text_content(MimeInfo *mimeinfo)
 
        fclose(tmpfp);
        rewind(outfp);
-       unlink(tmpfile);
+       g_unlink(tmpfile);
        g_free(tmpfile);
 
        return outfp;
@@ -787,8 +769,68 @@ FILE *procmime_get_first_text_content(MsgInfo *msginfo)
        if (!mimeinfo) return NULL;
 
        partinfo = mimeinfo;
-       while (partinfo && partinfo->type != MIMETYPE_TEXT)
+       while (partinfo && partinfo->type != MIMETYPE_TEXT) {
                partinfo = procmime_mimeinfo_next(partinfo);
+       }
+       if (partinfo)
+               outfp = procmime_get_text_content(partinfo);
+
+       procmime_mimeinfo_free_all(mimeinfo);
+
+       return outfp;
+}
+
+
+static gboolean find_encrypted_func(GNode *node, gpointer data)
+{
+       MimeInfo *mimeinfo = (MimeInfo *) node->data;
+       MimeInfo **encinfo = (MimeInfo **) data;
+       
+       if (privacy_mimeinfo_is_encrypted(mimeinfo)) {
+               *encinfo = mimeinfo;
+               return TRUE;
+       }
+       
+       return FALSE;
+}
+
+static MimeInfo *find_encrypted_part(MimeInfo *rootinfo)
+{
+       MimeInfo *encinfo = NULL;
+
+       g_node_traverse(rootinfo->node, G_IN_ORDER, G_TRAVERSE_ALL, -1,
+               find_encrypted_func, &encinfo);
+       
+       return encinfo;
+}
+
+/* search the first encrypted text part of (multipart) MIME message,
+   decode, convert it and output to outfp. */
+FILE *procmime_get_first_encrypted_text_content(MsgInfo *msginfo)
+{
+       FILE *outfp = NULL;
+       MimeInfo *mimeinfo, *partinfo, *encinfo;
+
+       g_return_val_if_fail(msginfo != NULL, NULL);
+
+       mimeinfo = procmime_scan_message(msginfo);
+       if (!mimeinfo) {
+               return NULL;
+       }
+
+       partinfo = mimeinfo;
+       if ((encinfo = find_encrypted_part(partinfo)) != NULL) {
+               debug_print("decrypting message part\n");
+               if (privacy_mimeinfo_decrypt(encinfo) < 0) {
+                       alertpanel_error(_("Couldn't decrypt: %s"),
+                               privacy_get_error());
+                       return NULL;
+               }
+       }
+       partinfo = mimeinfo;
+       while (partinfo && partinfo->type != MIMETYPE_TEXT) {
+               partinfo = procmime_mimeinfo_next(partinfo);
+       }
 
        if (partinfo)
                outfp = procmime_get_text_content(partinfo);
@@ -798,6 +840,25 @@ FILE *procmime_get_first_text_content(MsgInfo *msginfo)
        return outfp;
 }
 
+gboolean procmime_msginfo_is_encrypted(MsgInfo *msginfo)
+{
+       MimeInfo *mimeinfo, *partinfo;
+       gboolean result = FALSE;
+
+       g_return_val_if_fail(msginfo != NULL, FALSE);
+
+       mimeinfo = procmime_scan_message(msginfo);
+       if (!mimeinfo) {
+               return FALSE;
+       }
+
+       partinfo = mimeinfo;
+       result = (find_encrypted_part(partinfo) != NULL);
+       procmime_mimeinfo_free_all(mimeinfo);
+
+       return result;
+}
+
 gboolean procmime_find_string_part(MimeInfo *mimeinfo, const gchar *filename,
                                   const gchar *str, StrFindFunc find_func)
 {
@@ -881,8 +942,11 @@ gchar *procmime_get_tmp_file_name(MimeInfo *mimeinfo)
                        basetmp = procmime_mimeinfo_get_parameter(mimeinfo, "name");
                if (basetmp == NULL)
                        basetmp = "mimetmp";
-               base = g_path_get_basename(basetmp);
-               if (*base == '\0') base = g_strdup("mimetmp");
+               basetmp = g_path_get_basename(basetmp);
+               if (*basetmp == '\0') 
+                       basetmp = g_strdup("mimetmp");
+               base = conv_filename_from_utf8(basetmp);
+               g_free((gchar*)basetmp);
                subst_for_shellsafe_filename(base);
        }
 
@@ -890,6 +954,7 @@ gchar *procmime_get_tmp_file_name(MimeInfo *mimeinfo)
                               f_prefix, base, NULL);
 
        g_free(base);
+       
        return filename;
 }
 
@@ -900,7 +965,7 @@ gchar *procmime_get_mime_type(const gchar *filename)
        static GHashTable *mime_type_table = NULL;
        MimeType *mime_type;
        const gchar *p;
-       gchar *ext;
+       gchar *ext = NULL;
        gchar *base;
 
        if (!mime_type_table) {
@@ -909,11 +974,11 @@ gchar *procmime_get_mime_type(const gchar *filename)
        }
 
        base = g_path_get_basename(filename);
-       p = strrchr(base, '.');
+       if ((p = strrchr(base, '.')) != NULL)
+               Xstrdup_a(ext, p + 1, p = NULL );
        g_free(base);
        if (!p) return NULL;
 
-       Xstrdup_a(ext, p + 1, return NULL);
        g_strdown(ext);
        mime_type = g_hash_table_lookup(mime_type_table, ext);
        if (mime_type) {
@@ -992,17 +1057,23 @@ GList *procmime_get_mime_type_list(void)
        GList *list = NULL;
        FILE *fp;
        gchar buf[BUFFSIZE];
-       guchar *p;
+       gchar *p;
        gchar *delim;
        MimeType *mime_type;
+       gboolean fp_is_glob_file = TRUE;
 
        if (mime_type_list) 
                return mime_type_list;
-
-       if ((fp = fopen("/etc/mime.types", "rb")) == NULL) {
-               if ((fp = fopen(SYSCONFDIR "/mime.types", "rb")) == NULL) {
-                       FILE_OP_ERROR(SYSCONFDIR "/mime.types", "fopen");
-                       return NULL;
+       
+       if ((fp = g_fopen("/usr/share/mime/globs", "rb")) == NULL) {
+               fp_is_glob_file = FALSE;
+               if ((fp = g_fopen("/etc/mime.types", "rb")) == NULL) {
+                       if ((fp = g_fopen(SYSCONFDIR "/mime.types", "rb")) 
+                               == NULL) {
+                               FILE_OP_ERROR(SYSCONFDIR "/mime.types", 
+                                       "fopen");
+                               return NULL;
+                       }
                }
        }
 
@@ -1012,7 +1083,13 @@ GList *procmime_get_mime_type_list(void)
                g_strstrip(buf);
 
                p = buf;
-               while (*p && !isspace(*p)) p++;
+               
+               if (fp_is_glob_file) {
+                       while (*p && !g_ascii_isspace(*p) && (*p!=':')) p++;
+               } else {
+                       while (*p && !g_ascii_isspace(*p)) p++;
+               }
+
                if (*p) {
                        *p = '\0';
                        p++;
@@ -1025,7 +1102,12 @@ GList *procmime_get_mime_type_list(void)
                mime_type->type = g_strdup(buf);
                mime_type->sub_type = g_strdup(delim + 1);
 
-               while (*p && isspace(*p)) p++;
+               if (fp_is_glob_file) {
+                       while (*p && (g_ascii_isspace(*p)||(*p=='*')||(*p=='.'))) p++;
+               } else {
+                       while (*p && g_ascii_isspace(*p)) p++;
+               }
+
                if (*p)
                        mime_type->extension = g_strdup(p);
                else
@@ -1059,7 +1141,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];
@@ -1067,8 +1149,9 @@ 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 = fopen(file, "rb")) == NULL) {
+       if ((fp = g_fopen(file, "rb")) == NULL) {
                FILE_OP_ERROR(file, "fopen");
                return ENC_UNKNOWN;
        }
@@ -1080,6 +1163,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;
        }
@@ -1095,7 +1182,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) {
@@ -1189,12 +1276,13 @@ gchar *procmime_get_content_type_str(MimeMediaType type,
        return g_strdup_printf("%s/%s", type_str, subtype);
 }
 
-void procmime_parse_mimepart(MimeInfo *parent,
+static int procmime_parse_mimepart(MimeInfo *parent,
                             gchar *content_type,
                             gchar *content_encoding,
                             gchar *content_description,
                             gchar *content_id,
                             gchar *content_disposition,
+                            gchar *content_location,
                             const gchar *filename,
                             guint offset,
                             guint length);
@@ -1210,54 +1298,54 @@ void procmime_parse_message_rfc822(MimeInfo *mimeinfo)
                                                   NULL, TRUE},
                                {"Content-Disposition:",
                                                   NULL, TRUE},
+                               {"Content-Location:",
+                                                  NULL, TRUE},
                                {"MIME-Version:",
                                                   NULL, TRUE},
                                {NULL,             NULL, FALSE}};
        guint content_start, i;
        FILE *fp;
-       gint mime_major, mime_minor;
+        gchar *tmp;
 
        procmime_decode_content(mimeinfo);
 
-       fp = fopen(mimeinfo->data.filename, "rb");
+       fp = g_fopen(mimeinfo->data.filename, "rb");
        if (fp == NULL) {
                FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
                return;
        }
        fseek(fp, mimeinfo->offset, SEEK_SET);
        procheader_get_header_fields(fp, hentry);
-       if (hentry[0].body != NULL)
-               conv_unmime_header_overwrite(hentry[0].body);
-       if (hentry[2].body != NULL)
-               conv_unmime_header_overwrite(hentry[2].body);
-       if (hentry[4].body != NULL)
-               conv_unmime_header_overwrite(hentry[4].body);
+       if (hentry[0].body != NULL) {
+                tmp = conv_unmime_header(hentry[0].body, NULL);
+                g_free(hentry[0].body);
+                hentry[0].body = tmp;
+        }                
+       if (hentry[2].body != NULL) {
+                tmp = conv_unmime_header(hentry[2].body, NULL);
+                g_free(hentry[2].body);
+                hentry[2].body = tmp;
+        }                
+       if (hentry[4].body != NULL) {
+                tmp = conv_unmime_header(hentry[4].body, NULL);
+                g_free(hentry[4].body);
+                hentry[4].body = tmp;
+        }                
+       if (hentry[5].body != NULL) {
+                tmp = conv_unmime_header(hentry[5].body, NULL);
+                g_free(hentry[5].body);
+                hentry[5].body = tmp;
+        }                
        content_start = ftell(fp);
        fclose(fp);
-
-       if ((hentry[5].body != NULL) &&
-           (sscanf(hentry[5].body, "%d.%d", &mime_major, &mime_minor) == 2) &&
-           (mime_major == 1) && (mime_minor == 0)) {
-               procmime_parse_mimepart(mimeinfo,
-                                       hentry[0].body, hentry[1].body,
-                                       hentry[2].body, hentry[3].body, 
-                                       hentry[4].body, 
-                                       mimeinfo->data.filename, content_start,
-                                       mimeinfo->length - (content_start - mimeinfo->offset));
-       } else {
-               MimeInfo *subinfo;
-
-               subinfo = procmime_mimeinfo_new();
-               subinfo->content = MIMECONTENT_FILE;
-               subinfo->encoding_type = ENC_UNKNOWN;
-               subinfo->type = MIMETYPE_TEXT;
-               subinfo->subtype = g_strdup("plain");
-               subinfo->data.filename = g_strdup(mimeinfo->data.filename);
-               subinfo->offset = content_start;
-               subinfo->length = mimeinfo->length - (content_start - mimeinfo->offset);
-
-               g_node_append(mimeinfo->node, subinfo->node);
-       }
+       
+       procmime_parse_mimepart(mimeinfo,
+                               hentry[0].body, hentry[1].body,
+                               hentry[2].body, hentry[3].body,
+                               hentry[4].body, hentry[5].body,
+                               mimeinfo->data.filename, content_start,
+                               mimeinfo->length - (content_start - mimeinfo->offset));
+       
        for (i = 0; i < (sizeof hentry / sizeof hentry[0]); i++) {
                g_free(hentry[i].body);
                hentry[i].body = NULL;
@@ -1275,12 +1363,15 @@ void procmime_parse_multipart(MimeInfo *mimeinfo)
                                                   NULL, TRUE},
                                {"Content-Disposition:",
                                                   NULL, TRUE},
+                               {"Content-Location:",
+                                                  NULL, TRUE},
                                {NULL,             NULL, FALSE}};
-       gchar *p;
+       gchar *p, *tmp;
        gchar *boundary;
        gint boundary_len = 0, lastoffset = -1, i;
        gchar buf[BUFFSIZE];
        FILE *fp;
+       int result = 0;
 
        boundary = g_hash_table_lookup(mimeinfo->typeparameters, "boundary");
        if (!boundary)
@@ -1289,22 +1380,22 @@ void procmime_parse_multipart(MimeInfo *mimeinfo)
 
        procmime_decode_content(mimeinfo);
 
-       fp = fopen(mimeinfo->data.filename, "rb");
+       fp = g_fopen(mimeinfo->data.filename, "rb");
        if (fp == NULL) {
                FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
                return;
        }
        fseek(fp, mimeinfo->offset, SEEK_SET);
-       while ((p = fgets(buf, sizeof(buf), fp)) != NULL) {
-               if (ftell(fp) > (mimeinfo->offset + mimeinfo->length))
+       while ((p = fgets(buf, sizeof(buf), fp)) != NULL && result == 0) {
+               if (ftell(fp) - 1 > (mimeinfo->offset + mimeinfo->length))
                        break;
 
                if (IS_BOUNDARY(buf, boundary, boundary_len)) {
                        if (lastoffset != -1) {
-                               procmime_parse_mimepart(mimeinfo,
+                               result = procmime_parse_mimepart(mimeinfo,
                                                        hentry[0].body, hentry[1].body,
                                                        hentry[2].body, hentry[3].body, 
-                                                       hentry[4].body, 
+                                                       hentry[4].body, hentry[5].body,
                                                        mimeinfo->data.filename, lastoffset,
                                                        (ftell(fp) - strlen(buf)) - lastoffset - 1);
                        }
@@ -1318,12 +1409,26 @@ void procmime_parse_multipart(MimeInfo *mimeinfo)
                                hentry[i].body = NULL;
                        }
                        procheader_get_header_fields(fp, hentry);
-                       if (hentry[0].body != NULL)
-                               conv_unmime_header_overwrite(hentry[0].body);
-                       if (hentry[2].body != NULL)
-                               conv_unmime_header_overwrite(hentry[2].body);
-                       if (hentry[4].body != NULL)
-                               conv_unmime_header_overwrite(hentry[4].body);
+                        if (hentry[0].body != NULL) {
+                                tmp = conv_unmime_header(hentry[0].body, NULL);
+                                g_free(hentry[0].body);
+                                hentry[0].body = tmp;
+                        }                
+                        if (hentry[2].body != NULL) {
+                                tmp = conv_unmime_header(hentry[2].body, NULL);
+                                g_free(hentry[2].body);
+                                hentry[2].body = tmp;
+                        }                
+                        if (hentry[4].body != NULL) {
+                                tmp = conv_unmime_header(hentry[4].body, NULL);
+                                g_free(hentry[4].body);
+                                hentry[4].body = tmp;
+                        }                
+                        if (hentry[5].body != NULL) {
+                                tmp = conv_unmime_header(hentry[5].body, NULL);
+                                g_free(hentry[5].body);
+                                hentry[5].body = tmp;
+                        }                
                        lastoffset = ftell(fp);
                }
        }
@@ -1362,6 +1467,8 @@ static void parse_parameters(const gchar *parameters, GHashTable *table)
 
                value[0] = '\0';
                value++;
+               while (value[0] == ' ')
+                       value++;
 
                g_strdown(attribute);
 
@@ -1398,6 +1505,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;
 
@@ -1451,7 +1570,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;
@@ -1491,10 +1611,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;
 
@@ -1559,12 +1681,13 @@ static void procmime_parse_content_encoding(const gchar *content_encoding, MimeI
        return;
 }
 
-void procmime_parse_mimepart(MimeInfo *parent,
+static int procmime_parse_mimepart(MimeInfo *parent,
                             gchar *content_type,
                             gchar *content_encoding,
                             gchar *content_description,
                             gchar *content_id,
                             gchar *content_disposition,
+                            gchar *content_location,
                             const gchar *filename,
                             guint offset,
                             guint length)
@@ -1574,8 +1697,17 @@ void procmime_parse_mimepart(MimeInfo *parent,
        /* Create MimeInfo */
        mimeinfo = procmime_mimeinfo_new();
        mimeinfo->content = MIMECONTENT_FILE;
-       if (parent != NULL)
+       if (parent != NULL) {
+               if (g_node_depth(parent->node) > 32) {
+                       /* 32 is an arbitrary value
+                        * this avoids DOSsing ourselves 
+                        * with enormous messages
+                        */
+                       procmime_mimeinfo_free_all(mimeinfo);
+                       return -1;                      
+               }
                g_node_append(parent->node, mimeinfo->node);
+       }
        mimeinfo->data.filename = g_strdup(filename);
        mimeinfo->offset = offset;
        mimeinfo->length = length;
@@ -1586,8 +1718,12 @@ void 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) {
@@ -1606,6 +1742,11 @@ void procmime_parse_mimepart(MimeInfo *parent,
        else
                mimeinfo->id = NULL;
 
+       if (content_location != NULL)
+               mimeinfo->location = g_strdup(content_location);
+       else
+               mimeinfo->location = NULL;
+
        if (content_disposition != NULL) 
                procmime_parse_content_disposition(content_disposition, mimeinfo);
        else
@@ -1626,6 +1767,8 @@ void procmime_parse_mimepart(MimeInfo *parent,
                default:
                        break;
        }
+
+       return 0;
 }
 
 static gchar *typenames[] = {
@@ -1701,11 +1844,25 @@ MimeInfo *procmime_scan_queue_file(const gchar *filename)
        g_return_val_if_fail(filename != NULL, NULL);
 
        /* Open file */
-       if ((fp = fopen(filename, "rb")) == NULL)
+       if ((fp = g_fopen(filename, "rb")) == NULL)
                return NULL;
        /* Skip queue header */
-       while (fgets(buf, sizeof(buf), fp) != NULL)
+       while (fgets(buf, sizeof(buf), fp) != NULL) {
+               /* new way */
+               if (!strncmp(buf, "X-Sylpheed-End-Special-Headers: 1",
+                       strlen("X-Sylpheed-End-Special-Headers:")))
+                       break;
+               /* old way */
                if (buf[0] == '\r' || buf[0] == '\n') break;
+               /* from other mailers */
+               if (!strncmp(buf, "Date: ", 6)
+               ||  !strncmp(buf, "To: ", 4)
+               ||  !strncmp(buf, "From: ", 6)
+               ||  !strncmp(buf, "Subject: ", 9)) {
+                       rewind(fp);
+                       break;
+               }
+       }
        offset = ftell(fp);
        fclose(fp);
 
@@ -1718,24 +1875,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;
                }
@@ -1758,7 +1917,7 @@ static void write_parameters(gpointer key, gpointer value, gpointer user_data)
                case ';':
                case ':':
                case '\\':
-               case '"':
+               case '\'':
                case '/':
                case '[':
                case ']':
@@ -1768,19 +1927,37 @@ 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;
 
        case ENC_AS_EXTENDED:
-               g_string_append_printf(buf, "%s*=%s''", param,
-                       conv_get_current_charset_str());
+               if (!g_utf8_validate(val, -1, NULL))
+                       g_string_append_printf(buf, "%s*=%s''", param,
+                               conv_get_locale_charset_str());
+               else
+                       g_string_append_printf(buf, "%s*=%s''", param,
+                               CS_INTERNAL);
                for (valpos = val; *valpos != '\0'; valpos++) {
                        if (IS_ASCII(*valpos) && isalnum(*valpos)) {
                                g_string_append_printf(buf, "%c", *valpos);
@@ -1790,7 +1967,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)) {
@@ -1812,6 +1989,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) {
@@ -1819,6 +1997,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;
                }
@@ -1836,6 +2015,9 @@ void procmime_write_mime_header(MimeInfo *mimeinfo, FILE *fp)
        if (mimeinfo->id != NULL)
                fprintf(fp, "Content-ID: %s\n", mimeinfo->id);
 
+       if (mimeinfo->location != NULL)
+               fprintf(fp, "Content-Location: %s\n", mimeinfo->location);
+
        if (mimeinfo->disposition != DISPOSITIONTYPE_UNKNOWN) {
                ParametersData *pdata = g_new0(ParametersData, 1);
                gchar *buf = NULL;
@@ -1851,6 +2033,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");
@@ -1872,7 +2056,7 @@ gint procmime_write_message_rfc822(MimeInfo *mimeinfo, FILE *fp)
        /* write header */
        switch (mimeinfo->content) {
        case MIMECONTENT_FILE:
-               if ((infp = fopen(mimeinfo->data.filename, "rb")) == NULL) {
+               if ((infp = g_fopen(mimeinfo->data.filename, "rb")) == NULL) {
                        FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
                        return -1;
                }
@@ -1887,6 +2071,7 @@ gint procmime_write_message_rfc822(MimeInfo *mimeinfo, FILE *fp)
                            g_ascii_strncasecmp(buf, "Content-Transfer-Encoding:", 26) == 0 ||
                            g_ascii_strncasecmp(buf, "Content-Description:", 20) == 0 ||
                            g_ascii_strncasecmp(buf, "Content-ID:", 11) == 0 ||
+                           g_ascii_strncasecmp(buf, "Content-Location:", 17) == 0 ||
                            g_ascii_strncasecmp(buf, "Content-Disposition:", 20) == 0) {
                                skip = TRUE;
                                continue;
@@ -1898,7 +2083,10 @@ gint procmime_write_message_rfc822(MimeInfo *mimeinfo, FILE *fp)
                break;
 
        case MIMECONTENT_MEM:
-               fwrite(mimeinfo->data.mem, strlen(mimeinfo->data.mem), sizeof(gchar), fp);
+               fwrite(mimeinfo->data.mem, 
+                               sizeof(gchar), 
+                               strlen(mimeinfo->data.mem), 
+                               fp);
                break;
 
        default:
@@ -1929,7 +2117,7 @@ gint procmime_write_multipart(MimeInfo *mimeinfo, FILE *fp)
 
        switch (mimeinfo->content) {
        case MIMECONTENT_FILE:
-               if ((infp = fopen(mimeinfo->data.filename, "rb")) == NULL) {
+               if ((infp = g_fopen(mimeinfo->data.filename, "rb")) == NULL) {
                        FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
                        return -1;
                }
@@ -1947,7 +2135,7 @@ gint procmime_write_multipart(MimeInfo *mimeinfo, FILE *fp)
                if (((str2 = strstr(str, boundary)) != NULL) && ((str2 - str) >= 2) &&
                    (*(str2 - 1) == '-') && (*(str2 - 2) == '-'))
                        *(str2 - 2) = '\0';
-               fwrite(str, strlen(str), sizeof(gchar), fp);
+               fwrite(str, sizeof(gchar), strlen(str), fp);
                g_free(str);
                break;
 
@@ -1986,7 +2174,7 @@ gint procmime_write_mimeinfo(MimeInfo *mimeinfo, FILE *fp)
        if (G_NODE_IS_LEAF(mimeinfo->node)) {
                switch (mimeinfo->content) {
                case MIMECONTENT_FILE:
-                       if ((infp = fopen(mimeinfo->data.filename, "rb")) == NULL) {
+                       if ((infp = g_fopen(mimeinfo->data.filename, "rb")) == NULL) {
                                FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
                                return -1;
                        }
@@ -1995,7 +2183,10 @@ gint procmime_write_mimeinfo(MimeInfo *mimeinfo, FILE *fp)
                        return 0;
 
                case MIMECONTENT_MEM:
-                       fwrite(mimeinfo->data.mem, strlen(mimeinfo->data.mem), sizeof(gchar), fp);
+                       fwrite(mimeinfo->data.mem, 
+                                       sizeof(gchar), 
+                                       strlen(mimeinfo->data.mem), 
+                                       fp);
                        return 0;
 
                default:
@@ -2005,8 +2196,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:
@@ -2021,3 +2213,32 @@ gint procmime_write_mimeinfo(MimeInfo *mimeinfo, FILE *fp)
 
        return 0;
 }
+
+gchar *procmime_get_part_file_name(MimeInfo *mimeinfo)
+{
+       gchar *base;
+
+       if ((mimeinfo->type == MIMETYPE_TEXT) && !g_ascii_strcasecmp(mimeinfo->subtype, "html"))
+               base = g_strdup("mimetmp.html");
+       else {
+               const gchar *basetmp;
+               gchar *basename;
+
+               basetmp = procmime_mimeinfo_get_parameter(mimeinfo, "filename");
+               if (basetmp == NULL)
+                       basetmp = procmime_mimeinfo_get_parameter(mimeinfo, "name");
+               if (basetmp == NULL)
+                       basetmp = "mimetmp";
+               basename = g_path_get_basename(basetmp);
+               if (*basename == '\0') {
+                       g_free(basename);
+                       basename = g_strdup("mimetmp");
+               }
+               base = conv_filename_from_utf8(basename);
+               g_free(basename);
+               subst_for_shellsafe_filename(base);
+       }
+       
+       return base;
+}
+