2007-01-16 [paul] 2.7.1cvs6
[claws.git] / src / procmime.c
index 86fde560894c0d2eeaa563e0852d714c8a44fa21..0ab8d1d91c733ed300992f310b5a0757cb0addca 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-2007 Hiroyuki Yamamoto & The Claws Mail 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
@@ -28,7 +28,9 @@
 #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>
@@ -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);
 
@@ -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);
@@ -270,6 +227,24 @@ const gchar *procmime_mimeinfo_get_parameter(MimeInfo *mimeinfo, const gchar *na
        return value;
 }
 
+#define FLUSH_LASTLINE() {                                                     \
+       if (*lastline != '\0') {                                                \
+               gint llen = 0;                                                  \
+               strretchomp(lastline);                                          \
+               llen = strlen(lastline);                                        \
+               if (lastline[llen-1] == ' ' && strcmp(lastline,"-- ")) {        \
+                       /* this is flowed */                                    \
+                       if (delsp)                                              \
+                               lastline[llen-1] = '\0';                        \
+                       fputs(lastline, outfp);                                 \
+               } else {                                                        \
+                       fputs(lastline, outfp);                                 \
+                       fputs("\n", outfp);                                     \
+               }                                                               \
+       }                                                                       \
+       strcpy(lastline, buf);                                                  \
+}
+
 gboolean procmime_decode_content(MimeInfo *mimeinfo)
 {
        gchar buf[BUFFSIZE];
@@ -278,15 +253,34 @@ gboolean procmime_decode_content(MimeInfo *mimeinfo)
        FILE *outfp, *infp;
        struct stat statbuf;
        gboolean tmp_file = FALSE;
-
+       gboolean flowed = FALSE;
+       gboolean delsp = FALSE; 
        EncodingType encoding = forced_encoding 
                                ? forced_encoding
                                : mimeinfo->encoding_type;
+       gchar lastline[BUFFSIZE];
+       memset(lastline, 0, BUFFSIZE);
                   
        g_return_val_if_fail(mimeinfo != NULL, FALSE);
 
-       if (encoding == ENC_UNKNOWN ||
-           encoding == ENC_BINARY)
+       if (prefs_common.respect_flowed_format &&
+           mimeinfo->type == MIMETYPE_TEXT && 
+           !strcasecmp(mimeinfo->subtype, "plain")) {
+               if (procmime_mimeinfo_get_parameter(mimeinfo, "format") != NULL &&
+                   !strcasecmp(procmime_mimeinfo_get_parameter(mimeinfo, "format"),"flowed"))
+                       flowed = TRUE;
+               if (flowed &&
+                   procmime_mimeinfo_get_parameter(mimeinfo, "delsp") != NULL &&
+                   !strcasecmp(procmime_mimeinfo_get_parameter(mimeinfo, "delsp"),"yes"))
+                       delsp = TRUE;
+       }
+       
+       if (!flowed && (
+            encoding == ENC_UNKNOWN ||
+            encoding == ENC_BINARY ||
+            encoding == ENC_7BIT ||
+            encoding == ENC_8BIT
+           ))
                return TRUE;
 
        infp = g_fopen(mimeinfo->data.filename, "rb");
@@ -299,6 +293,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,8 +303,15 @@ 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, 1, len, outfp);
+                       buf[len]='\0';
+                       if (!flowed) {
+                               fwrite(buf, 1, len, outfp);
+                       } else {
+                               FLUSH_LASTLINE();
+                       }
                }
+               if (flowed)
+                       FLUSH_LASTLINE();
        } else if (encoding == ENC_BASE64) {
                gchar outbuf[BUFFSIZE];
                gint len;
@@ -325,6 +327,7 @@ gboolean procmime_decode_content(MimeInfo *mimeinfo)
                        if (!tmpfp) {
                                perror("tmpfile");
                                if (tmp_file) fclose(outfp);
+                               fclose(infp);
                                return FALSE;
                        }
                }
@@ -378,8 +381,14 @@ gboolean procmime_decode_content(MimeInfo *mimeinfo)
                }
        } else {
                while ((ftell(infp) < readend) && (fgets(buf, sizeof(buf), infp) != NULL)) {
-                       fputs(buf, outfp);
+                       if (!flowed) {
+                               fputs(buf, outfp);
+                       } else {
+                               FLUSH_LASTLINE();
+                       }
                }
+               if (flowed)
+                       FLUSH_LASTLINE();
        }
 
        fclose(outfp);
@@ -388,8 +397,7 @@ gboolean procmime_decode_content(MimeInfo *mimeinfo)
        stat(tmpfilename, &statbuf);
        if (mimeinfo->tmp && (mimeinfo->data.filename != NULL))
                g_unlink(mimeinfo->data.filename);
-       if (mimeinfo->data.filename != NULL)
-               g_free(mimeinfo->data.filename);
+       g_free(mimeinfo->data.filename);
        mimeinfo->data.filename = tmpfilename;
        mimeinfo->tmp = TRUE;
        mimeinfo->offset = 0;
@@ -539,40 +547,49 @@ gint procmime_get_part(const gchar *outfile, MimeInfo *mimeinfo)
        FILE *infp, *outfp;
        gchar buf[BUFFSIZE];
        gint restlength, readlength;
+       gint saved_errno = 0;
 
        g_return_val_if_fail(outfile != NULL, -1);
        g_return_val_if_fail(mimeinfo != NULL, -1);
 
        if (mimeinfo->encoding_type != ENC_BINARY && !procmime_decode_content(mimeinfo))
-               return -1;
+               return -EINVAL;
 
        if ((infp = g_fopen(mimeinfo->data.filename, "rb")) == NULL) {
+               saved_errno = errno;
                FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
-               return -1;
+               return -(saved_errno);
        }
        if (fseek(infp, mimeinfo->offset, SEEK_SET) < 0) {
+               saved_errno = errno;
                FILE_OP_ERROR(mimeinfo->data.filename, "fseek");
                fclose(infp);
-               return -1;
+               return -(saved_errno);
        }
        if ((outfp = g_fopen(outfile, "wb")) == NULL) {
+               saved_errno = errno;
                FILE_OP_ERROR(outfile, "fopen");
                fclose(infp);
-               return -1;
+               return -(saved_errno);
        }
 
        restlength = mimeinfo->length;
 
        while ((restlength > 0) && ((readlength = fread(buf, 1, restlength > BUFFSIZE ? BUFFSIZE : restlength, infp)) > 0)) {
-               fwrite(buf, 1, readlength, outfp);
+               if (fwrite(buf, 1, readlength, outfp) != readlength) {
+                       saved_errno = errno;
+                       fclose(outfp);
+                       return -(saved_errno);
+               }
                restlength -= readlength;
        }
 
        fclose(infp);
        if (fclose(outfp) == EOF) {
+               saved_errno = errno;
                FILE_OP_ERROR(outfile, "fclose");
                g_unlink(outfile);
-               return -1;
+               return -(saved_errno);
        }
 
        return 0;
@@ -751,15 +768,15 @@ FILE *procmime_get_text_content(MimeInfo *mimeinfo)
                dup2(oldout, 1);
 /* 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;
@@ -861,8 +878,11 @@ FILE *procmime_get_first_encrypted_text_content(MsgInfo *msginfo)
        partinfo = mimeinfo;
        if ((encinfo = find_encrypted_part(partinfo)) != NULL) {
                debug_print("decrypting message part\n");
-               if (privacy_mimeinfo_decrypt(encinfo) < 0)
+               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) {
@@ -980,8 +1000,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((gchar*)basetmp);
                subst_for_shellsafe_filename(base);
        }
 
@@ -1172,11 +1194,13 @@ EncodingType procmime_get_encoding_for_charset(const gchar *charset)
                return ENC_8BIT;
        else if (!g_ascii_strncasecmp(charset, "ISO-8859-", 9))
                return ENC_QUOTED_PRINTABLE;
-       else
+       else if (!g_ascii_strncasecmp(charset, "UTF-8", 5))
+               return ENC_QUOTED_PRINTABLE;
+       else 
                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];
@@ -1184,6 +1208,7 @@ 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 = g_fopen(file, "rb")) == NULL) {
                FILE_OP_ERROR(file, "fopen");
@@ -1197,6 +1222,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;
        }
@@ -1212,7 +1241,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) {
@@ -1306,12 +1335,13 @@ gchar *procmime_get_content_type_str(MimeMediaType type,
        return g_strdup_printf("%s/%s", type_str, subtype);
 }
 
-int 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);
@@ -1327,6 +1357,8 @@ void procmime_parse_message_rfc822(MimeInfo *mimeinfo)
                                                   NULL, TRUE},
                                {"Content-Disposition:",
                                                   NULL, TRUE},
+                               {"Content-Location:",
+                                                  NULL, TRUE},
                                {"MIME-Version:",
                                                   NULL, TRUE},
                                {NULL,             NULL, FALSE}};
@@ -1358,13 +1390,18 @@ void procmime_parse_message_rfc822(MimeInfo *mimeinfo)
                 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);
        
        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, content_start,
                                mimeinfo->length - (content_start - mimeinfo->offset));
        
@@ -1385,6 +1422,8 @@ void procmime_parse_multipart(MimeInfo *mimeinfo)
                                                   NULL, TRUE},
                                {"Content-Disposition:",
                                                   NULL, TRUE},
+                               {"Content-Location:",
+                                                  NULL, TRUE},
                                {NULL,             NULL, FALSE}};
        gchar *p, *tmp;
        gchar *boundary;
@@ -1407,7 +1446,7 @@ void procmime_parse_multipart(MimeInfo *mimeinfo)
        }
        fseek(fp, mimeinfo->offset, SEEK_SET);
        while ((p = fgets(buf, sizeof(buf), fp)) != NULL && result == 0) {
-               if (ftell(fp) > (mimeinfo->offset + mimeinfo->length))
+               if (ftell(fp) - 1 > (mimeinfo->offset + mimeinfo->length))
                        break;
 
                if (IS_BOUNDARY(buf, boundary, boundary_len)) {
@@ -1415,7 +1454,7 @@ void procmime_parse_multipart(MimeInfo *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);
                        }
@@ -1444,6 +1483,11 @@ void procmime_parse_multipart(MimeInfo *mimeinfo)
                                 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);
                }
        }
@@ -1696,12 +1740,13 @@ static void procmime_parse_content_encoding(const gchar *content_encoding, MimeI
        return;
 }
 
-int 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)
@@ -1756,6 +1801,11 @@ int 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
@@ -1856,8 +1906,24 @@ MimeInfo *procmime_scan_queue_file(const gchar *filename)
        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-Claws-End-Special-Headers: 1",
+                       strlen("X-Claws-End-Special-Headers:"))) ||
+                  (!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);
 
@@ -2010,6 +2076,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;
@@ -2054,6 +2123,7 @@ gint procmime_write_message_rfc822(MimeInfo *mimeinfo, FILE *fp)
                }
                fseek(infp, mimeinfo->offset, SEEK_SET);
                while (fgets(buf, sizeof(buf), infp) == buf) {
+                       strcrchomp(buf);
                        if (buf[0] == '\n' && buf[1] == '\0')
                                break;
                        if (skip && (buf[0] == ' ' || buf[0] == '\t'))
@@ -2063,6 +2133,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;