inital gtk2 patch
[claws.git] / src / procmime.c
index 7b82cf316b004dba446f79d17e047133177a8886..13d12bc5d84585697d0b9c65459105075051ddeb 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2002 Hiroyuki Yamamoto
+ * Copyright (C) 1999-2003 Hiroyuki Yamamoto
  *
  * 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
@@ -33,6 +33,7 @@
 #include "procmime.h"
 #include "procheader.h"
 #include "base64.h"
+#include "quoted-printable.h"
 #include "uuencode.h"
 #include "unmime.h"
 #include "html.h"
@@ -45,7 +46,7 @@
 #  include "rfc2015.h"
 #endif
 
-#include "prefs.h"
+#include "prefs_gtk.h"
 
 static GHashTable *procmime_get_mime_type_table        (void);
 
@@ -60,28 +61,6 @@ MimeInfo *procmime_mimeinfo_new(void)
        return mimeinfo;
 }
 
-void procmime_mimeinfo_free(MimeInfo *mimeinfo)
-{
-       if (!mimeinfo) return;
-
-       g_free(mimeinfo->encoding);
-       g_free(mimeinfo->content_type);
-       g_free(mimeinfo->charset);
-       g_free(mimeinfo->name);
-       g_free(mimeinfo->boundary);
-       g_free(mimeinfo->content_disposition);
-       g_free(mimeinfo->filename);
-#if USE_GPGME
-       g_free(mimeinfo->plaintextfile);
-       g_free(mimeinfo->sigstatus);
-       g_free(mimeinfo->sigstatus_full);
-#endif
-
-       procmime_mimeinfo_free(mimeinfo->sub);
-
-       g_free(mimeinfo);
-}
-
 void procmime_mimeinfo_free_all(MimeInfo *mimeinfo)
 {
        while (mimeinfo != NULL) {
@@ -94,6 +73,7 @@ void procmime_mimeinfo_free_all(MimeInfo *mimeinfo)
                g_free(mimeinfo->boundary);
                g_free(mimeinfo->content_disposition);
                g_free(mimeinfo->filename);
+               g_free(mimeinfo->description);
 #if USE_GPGME
                g_free(mimeinfo->plaintextfile);
                g_free(mimeinfo->sigstatus);
@@ -188,6 +168,22 @@ MimeInfo *procmime_mimeinfo_next(MimeInfo *mimeinfo)
        return NULL;
 }
 
+#if 0
+void procmime_dump_mimeinfo(MimeInfo *mimeinfo)
+{
+       gint i;
+
+       g_print("\n");
+
+       for (; mimeinfo != NULL; mimeinfo = procmime_mimeinfo_next(mimeinfo)) {
+               for (i = 0; i < mimeinfo->level; i++)
+                       g_print("  ");
+               g_print("%s%s\n", mimeinfo->main ? "sub: " : "",
+                       mimeinfo->content_type);
+       }
+}
+#endif
+
 MimeInfo *procmime_scan_message(MsgInfo *msginfo)
 {
        FILE *fp;
@@ -195,22 +191,21 @@ MimeInfo *procmime_scan_message(MsgInfo *msginfo)
 
        g_return_val_if_fail(msginfo != NULL, NULL);
 
+#if USE_GPGME
+       if ((fp = procmsg_open_message_decrypted(msginfo, &mimeinfo)) == NULL)
+               return NULL;
+#else
        if ((fp = procmsg_open_message(msginfo)) == NULL) return NULL;
        mimeinfo = procmime_scan_mime_header(fp);
+#endif
 
        if (mimeinfo) {
-               if (mimeinfo->mime_type != MIME_MULTIPART) {
-                       if (fseek(fp, mimeinfo->fpos, SEEK_SET) < 0)
-                               perror("fseek");
-               }
-               if (mimeinfo->mime_type != MIME_TEXT)
+               mimeinfo->size = get_left_file_size(fp);
+               if (mimeinfo->mime_type == MIME_MULTIPART ||
+                   mimeinfo->mime_type == MIME_MESSAGE_RFC822)
                        procmime_scan_multipart_message(mimeinfo, fp);
        }
 
-#if USE_GPGME
-        if (prefs_common.auto_check_signatures)
-               rfc2015_check_signature(mimeinfo, fp);
-#endif
        fclose(fp);
 
        return mimeinfo;
@@ -223,10 +218,10 @@ void procmime_scan_multipart_message(MimeInfo *mimeinfo, FILE *fp)
        gint boundary_len = 0;
        gchar buf[BUFFSIZE];
        glong fpos, prev_fpos;
-       gint npart;
 
        g_return_if_fail(mimeinfo != NULL);
-       g_return_if_fail(mimeinfo->mime_type != MIME_TEXT);
+       g_return_if_fail(mimeinfo->mime_type == MIME_MULTIPART ||
+                        mimeinfo->mime_type == MIME_MESSAGE_RFC822);
 
        if (mimeinfo->mime_type == MIME_MULTIPART) {
                g_return_if_fail(mimeinfo->boundary != NULL);
@@ -243,6 +238,9 @@ void procmime_scan_multipart_message(MimeInfo *mimeinfo, FILE *fp)
                while ((p = fgets(buf, sizeof(buf), fp)) != NULL)
                        if (IS_BOUNDARY(buf, boundary, boundary_len)) break;
                if (!p) return;
+       } else if (mimeinfo->parent && mimeinfo->parent->boundary) {
+               boundary = mimeinfo->parent->boundary;
+               boundary_len = strlen(boundary);
        }
 
        if ((fpos = ftell(fp)) < 0) {
@@ -250,40 +248,37 @@ void procmime_scan_multipart_message(MimeInfo *mimeinfo, FILE *fp)
                return;
        }
 
-       for (npart = 0;; npart++) {
+       for (;;) {
                MimeInfo *partinfo;
                gboolean eom = FALSE;
+               gint len;
 
                prev_fpos = fpos;
                debug_print("prev_fpos: %ld\n", fpos);
 
-               partinfo = procmime_scan_mime_header(fp);
-               if (!partinfo) break;
-               procmime_mimeinfo_insert(mimeinfo, partinfo);
-
-               if (partinfo->mime_type == MIME_MULTIPART) {
-                       if (partinfo->level < 8)
-                               procmime_scan_multipart_message(partinfo, fp);
-               } else if (partinfo->mime_type == MIME_MESSAGE_RFC822) {
+               if (mimeinfo->mime_type == MIME_MESSAGE_RFC822) {
                        MimeInfo *sub;
 
-                       partinfo->sub = sub = procmime_scan_mime_header(fp);
+                       mimeinfo->sub = sub = procmime_scan_mime_header(fp);
                        if (!sub) break;
 
-                       sub->level = partinfo->level + 1;
-                       sub->parent = partinfo;
-                       sub->main = partinfo;
-
-                       if (sub->level < 8) {
-                               if (sub->mime_type == MIME_MULTIPART) {
-                                       procmime_scan_multipart_message
-                                               (sub, fp);
-                               } else if (sub->mime_type == MIME_MESSAGE_RFC822) {
-                                       fseek(fp, sub->fpos, SEEK_SET);
-                                       procmime_scan_multipart_message
-                                               (sub, fp);
-                               }
-                       }
+                       sub->level = mimeinfo->level + 1;
+                       sub->parent = mimeinfo->parent;
+                       sub->main = mimeinfo;
+
+                       partinfo = sub;
+               } else {
+                       partinfo = procmime_scan_mime_header(fp);
+                       if (!partinfo) break;
+                       procmime_mimeinfo_insert(mimeinfo, partinfo);
+                       debug_print("content-type: %s\n",
+                                   partinfo->content_type);
+               }
+
+               if (partinfo->mime_type == MIME_MULTIPART ||
+                   partinfo->mime_type == MIME_MESSAGE_RFC822) {
+                       if (partinfo->level < 8)
+                               procmime_scan_multipart_message(partinfo, fp);
                }
 
                /* look for next boundary */
@@ -301,18 +296,27 @@ void procmime_scan_multipart_message(MimeInfo *mimeinfo, FILE *fp)
                        buf[0] = '\0';
                        eom = TRUE;
                }
+               debug_print("boundary: %s\n", buf);
+
                fpos = ftell(fp);
                debug_print("fpos: %ld\n", fpos);
 
-               partinfo->size = fpos - prev_fpos - strlen(buf);
+               len = strlen(buf);
+               partinfo->size = fpos - prev_fpos - len;
                debug_print("partinfo->size: %d\n", partinfo->size);
                if (partinfo->sub && !partinfo->sub->sub &&
                    !partinfo->sub->children) {
-                       partinfo->sub->size = fpos - partinfo->sub->fpos - strlen(buf);
+                       partinfo->sub->size =
+                               fpos - partinfo->sub->fpos - strlen(buf);
                        debug_print("partinfo->sub->size: %d\n",
                                    partinfo->sub->size);
                }
-               debug_print("boundary: %s\n", buf);
+
+               if (mimeinfo->mime_type == MIME_MESSAGE_RFC822) {
+                       if (len > 0 && fseek(fp, fpos - len, SEEK_SET) < 0)
+                               perror("fseek");
+                       break;
+               }
 
                if (eom) break;
        }
@@ -503,12 +507,10 @@ void procmime_scan_content_description(MimeInfo *mimeinfo,
        blen = strlen(buf) + 1;
        Xalloca(tmp, blen, return);
        conv_unmime_header(tmp, blen, buf, NULL);
-       g_free(mimeinfo->name);
-       mimeinfo->name = NULL;
        /*pgp signatures should NOT have a name */
        if (mimeinfo->content_type 
        &&  strcasecmp(mimeinfo->content_type, "application/pgp-signature"))
-               mimeinfo->name = g_strdup(tmp);
+               mimeinfo->description = g_strdup(tmp);
 }
 
 void procmime_scan_subject(MimeInfo *mimeinfo,
@@ -590,14 +592,6 @@ MimeInfo *procmime_scan_mime_header(FILE *fp)
                }
        }
 
-       if (mimeinfo->mime_type == MIME_APPLICATION_OCTET_STREAM &&
-           mimeinfo->name) {
-               const gchar *type;
-               type = procmime_get_mime_type(mimeinfo->name);
-               if (type)
-                       mimeinfo->mime_type = procmime_scan_mime_type(type);
-       }
-
        if (!mimeinfo->content_type)
                        mimeinfo->content_type = g_strdup("text/plain");
 
@@ -633,7 +627,7 @@ FILE *procmime_decode_content(FILE *outfp, FILE *infp, MimeInfo *mimeinfo)
                       (!boundary ||
                        !IS_BOUNDARY(buf, boundary, boundary_len))) {
                        gint len;
-                       len = unmime_quoted_printable_line(buf);
+                       len = qp_decode_line(buf);
                        fwrite(buf, len, 1, outfp);
                }
        } else if (mimeinfo->encoding_type == ENC_BASE64) {
@@ -800,7 +794,7 @@ void renderer_write_config(void)
        rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, RENDERER_RC, NULL);
        
        if ((pfile = prefs_write_open(rcpath)) == NULL) {
-               g_warning(_("failed to write configuration to file\n"));
+               g_warning("failed to write configuration to file\n");
                g_free(rcpath);
                return;
        }
@@ -814,8 +808,8 @@ void renderer_write_config(void)
                        renderer->renderer);
        }
 
-       if (prefs_write_close(pfile) < 0) {
-               g_warning(_("failed to write configuration to file\n"));
+       if (prefs_file_close(pfile) < 0) {
+               g_warning("failed to write configuration to file\n");
                return;
        }
 }
@@ -891,7 +885,8 @@ FILE *procmime_get_text_content(MimeInfo *mimeinfo, FILE *infp)
                dup2(oldout, 1);
        } else if (mimeinfo->mime_type == MIME_TEXT) {
                while (fgets(buf, sizeof(buf), tmpfp) != NULL) {
-                       str = conv_codeset_strdup(buf, src_codeset, NULL);
+#warning FIXME_GTK2
+                       str = conv_codeset_strdup(buf, src_codeset, CS_UTF_8);
                        if (str) {
                                fputs(str, outfp);
                                g_free(str);
@@ -925,7 +920,7 @@ FILE *procmime_get_text_content(MimeInfo *mimeinfo, FILE *infp)
        }
 
        if (conv_fail)
-               g_warning(_("procmime_get_text_content(): Code conversion failed.\n"));
+               g_warning("procmime_get_text_content(): Code conversion failed.\n");
 
        fclose(tmpfp);
        rewind(outfp);
@@ -1049,7 +1044,7 @@ gboolean procmime_find_string(MsgInfo *msginfo, const gchar *str,
 gchar *procmime_get_tmp_file_name(MimeInfo *mimeinfo)
 {
        static guint32 id = 0;
-       gchar *base;
+       const gchar *base;
        gchar *filename;
        gchar f_prefix[10];
 
@@ -1060,12 +1055,14 @@ gchar *procmime_get_tmp_file_name(MimeInfo *mimeinfo)
        if (MIME_TEXT_HTML == mimeinfo->mime_type)
                base = "mimetmp.html";
        else {
+               gchar *tmp;
                base = mimeinfo->filename ? mimeinfo->filename
                        : mimeinfo->name ? mimeinfo->name : "mimetmp";
                base = g_basename(base);
                if (*base == '\0') base = "mimetmp";
-               Xstrdup_a(base, base, return NULL);
-               subst_for_filename(base);
+               Xstrdup_a(tmp, base, return NULL);
+               subst_for_filename(tmp);
+               base = tmp;
        }
 
        filename = g_strconcat(get_mime_tmp_dir(), G_DIR_SEPARATOR_S,
@@ -1259,10 +1256,14 @@ EncodingType procmime_get_encoding_for_charset(const gchar *charset)
        else if (!strncasecmp(charset, "ISO-2022-", 9) ||
                 !strcasecmp(charset, "US-ASCII"))
                return ENC_7BIT;
+       else if (!strcasecmp(charset, "ISO-8859-5") ||
+                !strncasecmp(charset, "KOI8-", 5) ||
+                !strcasecmp(charset, "Windows-1251"))
+               return ENC_8BIT;
+       else if (!strncasecmp(charset, "ISO-8859-", 9))
+               return ENC_QUOTED_PRINTABLE;
        else
                return ENC_8BIT;
-               /* return ENC_BASE64; */
-               /* return ENC_QUOTED_PRINTABLE; */
 }
 
 EncodingType procmime_get_encoding_for_file(const gchar *file)