2008-09-18 [colin] 3.5.0cvs113
[claws.git] / src / procmime.c
index 6ac265917533090c0be4fce372498111890afe6c..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;
 
@@ -1280,6 +1303,8 @@ static int procmime_parse_mimepart(MimeInfo *parent,
                             gchar *content_id,
                             gchar *content_disposition,
                             gchar *content_location,
+                            const gchar *original_msgid,
+                            const gchar *disposition_notification_hdr,
                             const gchar *filename,
                             guint offset,
                             guint length,
@@ -1300,6 +1325,10 @@ static void procmime_parse_message_rfc822(MimeInfo *mimeinfo, gboolean short_sca
                                                   NULL, TRUE},
                                {"MIME-Version:",
                                                   NULL, TRUE},
+                               {"Original-Message-ID:",
+                                                  NULL, TRUE},
+                               {"Disposition:",
+                                                  NULL, TRUE},
                                {NULL,             NULL, FALSE}};
        guint content_start, i;
        FILE *fp;
@@ -1335,6 +1364,17 @@ static void procmime_parse_message_rfc822(MimeInfo *mimeinfo, gboolean short_sca
                 g_free(hentry[5].body);
                 hentry[5].body = tmp;
         }                
+       if (hentry[7].body != NULL) {
+                tmp = conv_unmime_header(hentry[7].body, NULL);
+                g_free(hentry[7].body);
+                hentry[7].body = tmp;
+        }
+       if (hentry[8].body != NULL) {
+                tmp = conv_unmime_header(hentry[8].body, NULL);
+                g_free(hentry[8].body);
+                hentry[8].body = tmp;
+        }
+  
        content_start = ftell(fp);
        fclose(fp);
        
@@ -1345,6 +1385,7 @@ static void procmime_parse_message_rfc822(MimeInfo *mimeinfo, gboolean short_sca
                                hentry[0].body, hentry[1].body,
                                hentry[2].body, hentry[3].body,
                                hentry[4].body, hentry[5].body,
+                               hentry[7].body, hentry[8].body, 
                                mimeinfo->data.filename, content_start,
                                len, short_scan);
        
@@ -1354,6 +1395,110 @@ static void procmime_parse_message_rfc822(MimeInfo *mimeinfo, gboolean short_sca
        }
 }
 
+static void procmime_parse_disposition_notification(MimeInfo *mimeinfo, 
+               const gchar *original_msgid, const gchar *disposition_notification_hdr,
+               gboolean short_scan)
+{
+       HeaderEntry hentry[] = {{"Original-Message-ID:",  NULL, TRUE},
+                               {"Disposition:",          NULL, TRUE},
+                               {NULL,                    NULL, FALSE}};
+       guint i;
+       FILE *fp;
+       gchar *orig_msg_id = NULL;
+       gchar *disp = NULL;
+
+       procmime_decode_content(mimeinfo);
+
+       debug_print("parse disposition notification\n");
+       fp = g_fopen(mimeinfo->data.filename, "rb");
+       if (fp == NULL) {
+               FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
+               return;
+       }
+       fseek(fp, mimeinfo->offset, SEEK_SET);
+
+       if (original_msgid && disposition_notification_hdr) {
+               hentry[0].body = g_strdup(original_msgid);
+               hentry[1].body = g_strdup(disposition_notification_hdr);
+       } else {
+               procheader_get_header_fields(fp, hentry);
+       }
+
+       if (!hentry[0].body || !hentry[1].body) {
+               debug_print("MsgId %s, Disp %s\n",
+                       hentry[0].body ? hentry[0].body:"(nil)",
+                       hentry[1].body ? hentry[1].body:"(nil)");
+               goto bail;
+       }
+
+       orig_msg_id = g_strdup(hentry[0].body);
+       disp = g_strdup(hentry[1].body);
+
+       extract_parenthesis(orig_msg_id, '<', '>');
+       remove_space(orig_msg_id);
+       
+       if (strstr(disp, "displayed")) {
+               /* find sent message, if possible */
+               MsgInfo *info = NULL;
+               GList *flist;
+               debug_print("%s has been displayed.\n", orig_msg_id);
+               for (flist = folder_get_list(); flist != NULL; flist = g_list_next(flist)) {
+                       FolderItem *outbox = ((Folder *)(flist->data))->outbox;
+                       if (!outbox) {
+                               debug_print("skipping folder with no outbox...\n");
+                               continue;
+                       }
+                       info = folder_item_get_msginfo_by_msgid(outbox, orig_msg_id);
+                       debug_print("%s %s in %s\n", info?"found":"didn't find", orig_msg_id, outbox->path);
+                       if (info) {
+                               procmsg_msginfo_set_flags(info, MSG_RETRCPT_GOT, 0);
+                               procmsg_msginfo_free(info);
+                       }
+               }
+       }
+       g_free(orig_msg_id);
+       g_free(disp);
+bail:
+       for (i = 0; i < (sizeof hentry / sizeof hentry[0]); i++) {
+               g_free(hentry[i].body);
+               hentry[i].body = NULL;
+       }
+}
+
+#define GET_HEADERS() {                                                \
+       procheader_get_header_fields(fp, hentry);               \
+        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;                          \
+        }                                                      \
+       if (hentry[6].body != NULL) {                           \
+                tmp = conv_unmime_header(hentry[6].body, NULL);        \
+                g_free(hentry[6].body);                                \
+                hentry[6].body = tmp;                          \
+        }                                                      \
+       if (hentry[7].body != NULL) {                           \
+                tmp = conv_unmime_header(hentry[7].body, NULL);        \
+                g_free(hentry[7].body);                                \
+                hentry[7].body = tmp;                          \
+        }                                                      \
+}
+
 static void procmime_parse_multipart(MimeInfo *mimeinfo, gboolean short_scan)
 {
        HeaderEntry hentry[] = {{"Content-Type:",  NULL, TRUE},
@@ -1367,6 +1512,10 @@ static void procmime_parse_multipart(MimeInfo *mimeinfo, gboolean short_scan)
                                                   NULL, TRUE},
                                {"Content-Location:",
                                                   NULL, TRUE},
+                               {"Original-Message-ID:",
+                                                  NULL, TRUE},
+                               {"Disposition:",
+                                                  NULL, TRUE},
                                {NULL,             NULL, FALSE}};
        gchar *p, *tmp;
        gchar *boundary;
@@ -1402,43 +1551,24 @@ static void procmime_parse_multipart(MimeInfo *mimeinfo, gboolean short_scan)
                                                        hentry[0].body, hentry[1].body,
                                                        hentry[2].body, hentry[3].body, 
                                                        hentry[4].body, hentry[5].body,
+                                                       hentry[6].body, hentry[7].body,
                                                        mimeinfo->data.filename, lastoffset,
                                                        len, short_scan);
                                if (result == 1 && short_scan) {
                                        done = TRUE;
                                        break;
                                }
-                       }
+                       } 
                        
                        if (buf[2 + boundary_len]     == '-' &&
-                           buf[2 + boundary_len + 1] == '-')
+                           buf[2 + boundary_len + 1] == '-') {
                                break;
-
+                       }
                        for (i = 0; i < (sizeof hentry / sizeof hentry[0]) ; i++) {
                                g_free(hentry[i].body);
                                hentry[i].body = NULL;
                        }
-                       procheader_get_header_fields(fp, hentry);
-                        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;
-                        }                
+                       GET_HEADERS();
                        lastoffset = ftell(fp);
                }
        }
@@ -1458,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;
 
@@ -1480,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;
@@ -1515,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] == ' ')
@@ -1527,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)) {
@@ -1639,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)
@@ -1729,6 +1861,8 @@ static int procmime_parse_mimepart(MimeInfo *parent,
                             gchar *content_id,
                             gchar *content_disposition,
                             gchar *content_location,
+                            const gchar *original_msgid,
+                            const gchar *disposition_notification_hdr,
                             const gchar *filename,
                             guint offset,
                             guint length,
@@ -1813,12 +1947,24 @@ static int procmime_parse_mimepart(MimeInfo *parent,
                        if (g_ascii_strcasecmp(mimeinfo->subtype, "rfc822") == 0) {
                                procmime_parse_message_rfc822(mimeinfo, short_scan);
                        }
+                       if (g_ascii_strcasecmp(mimeinfo->subtype, "disposition-notification") == 0) {
+                               procmime_parse_disposition_notification(mimeinfo, 
+                                       original_msgid, disposition_notification_hdr, short_scan);
+                       }
                        break;
                        
                case MIMETYPE_MULTIPART:
                        procmime_parse_multipart(mimeinfo, short_scan);
                        break;
-                       
+               
+               case MIMETYPE_APPLICATION:
+                       if (g_ascii_strcasecmp(mimeinfo->subtype, "octet-stream") == 0
+                       && original_msgid && *original_msgid 
+                       && disposition_notification_hdr && *disposition_notification_hdr) {
+                               procmime_parse_disposition_notification(mimeinfo, 
+                                       original_msgid, disposition_notification_hdr, short_scan);
+                       }
+                       break;
                default:
                        break;
                }