2 * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2016 Hiroyuki Yamamoto & The Claws Mail Team
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "claws-features.h"
30 #include <glib/gi18n.h>
36 #include <sys/types.h>
42 #include "procheader.h"
43 #include "quoted-printable.h"
50 #include "prefs_common.h"
51 #include "prefs_gtk.h"
52 #include "alertpanel.h"
56 static GHashTable
*procmime_get_mime_type_table (void);
57 static MimeInfo
*procmime_scan_file_short(const gchar
*filename
);
58 static MimeInfo
*procmime_scan_queue_file_short(const gchar
*filename
);
59 static MimeInfo
*procmime_scan_queue_file_full(const gchar
*filename
, gboolean short_scan
);
61 MimeInfo
*procmime_mimeinfo_new(void)
65 mimeinfo
= g_new0(MimeInfo
, 1);
66 mimeinfo
->content
= MIMECONTENT_EMPTY
;
67 mimeinfo
->data
.filename
= NULL
;
69 mimeinfo
->type
= MIMETYPE_UNKNOWN
;
70 mimeinfo
->encoding_type
= ENC_UNKNOWN
;
71 mimeinfo
->typeparameters
= g_hash_table_new(g_str_hash
, g_str_equal
);
73 mimeinfo
->disposition
= DISPOSITIONTYPE_UNKNOWN
;
74 mimeinfo
->dispositionparameters
75 = g_hash_table_new(g_str_hash
, g_str_equal
);
77 mimeinfo
->node
= g_node_new(mimeinfo
);
82 static gboolean
procmime_mimeinfo_parameters_destroy(gpointer key
, gpointer value
, gpointer user_data
)
90 static gchar
*forced_charset
= NULL
;
92 void procmime_force_charset(const gchar
*str
)
94 g_free(forced_charset
);
95 forced_charset
= NULL
;
97 forced_charset
= g_strdup(str
);
100 static EncodingType forced_encoding
= 0;
102 void procmime_force_encoding(EncodingType encoding
)
104 forced_encoding
= encoding
;
107 static gboolean
free_func(GNode
*node
, gpointer data
)
109 MimeInfo
*mimeinfo
= (MimeInfo
*) node
->data
;
111 switch (mimeinfo
->content
) {
112 case MIMECONTENT_FILE
:
114 claws_unlink(mimeinfo
->data
.filename
);
115 g_free(mimeinfo
->data
.filename
);
118 case MIMECONTENT_MEM
:
120 g_free(mimeinfo
->data
.mem
);
125 g_free(mimeinfo
->subtype
);
126 g_free(mimeinfo
->description
);
127 g_free(mimeinfo
->id
);
128 g_free(mimeinfo
->location
);
130 g_hash_table_foreach_remove(mimeinfo
->typeparameters
,
131 procmime_mimeinfo_parameters_destroy
, NULL
);
132 g_hash_table_destroy(mimeinfo
->typeparameters
);
133 g_hash_table_foreach_remove(mimeinfo
->dispositionparameters
,
134 procmime_mimeinfo_parameters_destroy
, NULL
);
135 g_hash_table_destroy(mimeinfo
->dispositionparameters
);
137 if (mimeinfo
->privacy
)
138 privacy_free_privacydata(mimeinfo
->privacy
);
143 void procmime_mimeinfo_free_all(MimeInfo
**mimeinfo_ptr
)
145 MimeInfo
*mimeinfo
= *mimeinfo_ptr
;
151 node
= mimeinfo
->node
;
152 g_node_traverse(node
, G_IN_ORDER
, G_TRAVERSE_ALL
, -1, free_func
, NULL
);
154 g_node_destroy(node
);
157 *mimeinfo_ptr
= NULL
;
160 MimeInfo
*procmime_mimeinfo_parent(MimeInfo
*mimeinfo
)
162 cm_return_val_if_fail(mimeinfo
!= NULL
, NULL
);
163 cm_return_val_if_fail(mimeinfo
->node
!= NULL
, NULL
);
165 if (mimeinfo
->node
->parent
== NULL
)
167 return (MimeInfo
*) mimeinfo
->node
->parent
->data
;
170 MimeInfo
*procmime_mimeinfo_next(MimeInfo
*mimeinfo
)
172 cm_return_val_if_fail(mimeinfo
!= NULL
, NULL
);
173 cm_return_val_if_fail(mimeinfo
->node
!= NULL
, NULL
);
175 if (mimeinfo
->node
->children
)
176 return (MimeInfo
*) mimeinfo
->node
->children
->data
;
177 if (mimeinfo
->node
->next
)
178 return (MimeInfo
*) mimeinfo
->node
->next
->data
;
180 if (mimeinfo
->node
->parent
== NULL
)
183 while (mimeinfo
->node
->parent
!= NULL
) {
184 mimeinfo
= (MimeInfo
*) mimeinfo
->node
->parent
->data
;
185 if (mimeinfo
->node
->next
)
186 return (MimeInfo
*) mimeinfo
->node
->next
->data
;
192 MimeInfo
*procmime_scan_message(MsgInfo
*msginfo
)
197 filename
= procmsg_get_message_file_path(msginfo
);
198 if (!filename
|| !is_file_exist(filename
)) {
203 if (!folder_has_parent_of_type(msginfo
->folder
, F_QUEUE
) &&
204 !folder_has_parent_of_type(msginfo
->folder
, F_DRAFT
))
205 mimeinfo
= procmime_scan_file(filename
);
207 mimeinfo
= procmime_scan_queue_file(filename
);
213 MimeInfo
*procmime_scan_message_short(MsgInfo
*msginfo
)
218 filename
= procmsg_get_message_file_path(msginfo
);
219 if (!filename
|| !is_file_exist(filename
)) {
224 if (!folder_has_parent_of_type(msginfo
->folder
, F_QUEUE
) &&
225 !folder_has_parent_of_type(msginfo
->folder
, F_DRAFT
))
226 mimeinfo
= procmime_scan_file_short(filename
);
228 mimeinfo
= procmime_scan_queue_file_short(filename
);
236 H_CONTENT_TRANSFER_ENCODING
= 0,
238 H_CONTENT_DISPOSITION
= 2,
239 H_CONTENT_DESCRIPTION
= 3,
243 const gchar
*procmime_mimeinfo_get_parameter(MimeInfo
*mimeinfo
, const gchar
*name
)
247 cm_return_val_if_fail(mimeinfo
!= NULL
, NULL
);
248 cm_return_val_if_fail(name
!= NULL
, NULL
);
250 value
= g_hash_table_lookup(mimeinfo
->dispositionparameters
, name
);
252 value
= g_hash_table_lookup(mimeinfo
->typeparameters
, name
);
257 #ifdef HAVE_FGETS_UNLOCKED
258 #define SC_FGETS fgets_unlocked
259 #define SC_FPUTS fputs_unlocked
260 #define SC_FPUTC fputc_unlocked
261 #define SC_FREAD fread_unlocked
262 #define SC_FWRITE fwrite_unlocked
263 #define SC_FEOF feof_unlocked
264 #define SC_FERROR ferror_unlocked
266 static FILE *procmime_fopen(const gchar
*file
, const gchar
*mode
)
268 FILE *fp
= g_fopen(file
, mode
);
274 static int procmime_fclose(FILE *fp
)
280 #define SC_FGETS fgets
281 #define SC_FPUTS fputs
282 #define SC_FPUTC fputc
283 #define SC_FREAD fread
284 #define SC_FWRITE fwrite
286 #define SC_FERROR ferror
287 #define procmime_fopen g_fopen
288 #define procmime_fclose fclose
291 #define FLUSH_LASTLINE() { \
292 if (*lastline != '\0') { \
294 strretchomp(lastline); \
295 llen = strlen(lastline); \
296 if (lastline[llen-1] == ' ' && strcmp(lastline,"-- ")) { \
297 /* this is flowed */ \
299 lastline[llen-1] = '\0'; \
300 if (SC_FPUTS(lastline, outfp) == EOF) \
303 if (SC_FPUTS(lastline, outfp) == EOF) \
305 if (SC_FPUTS("\n", outfp) == EOF) \
309 strcpy(lastline, buf); \
312 gboolean
procmime_decode_content(MimeInfo
*mimeinfo
)
319 gboolean tmp_file
= FALSE
;
320 gboolean flowed
= FALSE
;
321 gboolean delsp
= FALSE
;
322 gboolean err
= FALSE
;
326 cm_return_val_if_fail(mimeinfo
!= NULL
, FALSE
);
328 EncodingType encoding
= forced_encoding
330 : mimeinfo
->encoding_type
;
331 gchar lastline
[BUFFSIZE
];
332 memset(lastline
, 0, BUFFSIZE
);
334 if (prefs_common
.respect_flowed_format
&&
335 mimeinfo
->type
== MIMETYPE_TEXT
&&
336 !strcasecmp(mimeinfo
->subtype
, "plain")) {
337 if (procmime_mimeinfo_get_parameter(mimeinfo
, "format") != NULL
&&
338 !strcasecmp(procmime_mimeinfo_get_parameter(mimeinfo
, "format"),"flowed"))
341 procmime_mimeinfo_get_parameter(mimeinfo
, "delsp") != NULL
&&
342 !strcasecmp(procmime_mimeinfo_get_parameter(mimeinfo
, "delsp"),"yes"))
347 encoding
== ENC_UNKNOWN
||
348 encoding
== ENC_BINARY
||
349 encoding
== ENC_7BIT
||
354 if (mimeinfo
->type
== MIMETYPE_MULTIPART
|| mimeinfo
->type
== MIMETYPE_MESSAGE
)
357 if (mimeinfo
->data
.filename
== NULL
)
360 infp
= procmime_fopen(mimeinfo
->data
.filename
, "rb");
362 FILE_OP_ERROR(mimeinfo
->data
.filename
, "fopen");
365 if (fseek(infp
, mimeinfo
->offset
, SEEK_SET
) < 0) {
366 FILE_OP_ERROR(mimeinfo
->data
.filename
, "fseek");
367 procmime_fclose(infp
);
371 outfp
= get_tmpfile_in_dir(get_mime_tmp_dir(), &tmpfilename
);
374 procmime_fclose(infp
);
377 #ifdef HAVE_FGETS_UNLOCKED
381 readend
= mimeinfo
->offset
+ mimeinfo
->length
;
383 if (encoding
== ENC_QUOTED_PRINTABLE
) {
384 while ((ftell(infp
) < readend
) && (SC_FGETS(buf
, sizeof(buf
), infp
) != NULL
)) {
386 len
= qp_decode_line(buf
);
389 if (SC_FWRITE(buf
, 1, len
, outfp
) < len
)
397 } else if (encoding
== ENC_BASE64
) {
398 gchar outbuf
[BUFFSIZE
];
399 gint len
, inlen
, inread
;
400 gboolean got_error
= FALSE
;
401 gboolean uncanonicalize
= FALSE
;
403 gboolean null_bytes
= FALSE
;
404 gboolean starting
= TRUE
;
406 if (mimeinfo
->type
== MIMETYPE_TEXT
||
407 mimeinfo
->type
== MIMETYPE_MESSAGE
) {
408 uncanonicalize
= TRUE
;
409 tmpfp
= my_tmpfile();
411 perror("my_tmpfile");
413 procmime_fclose(outfp
);
414 procmime_fclose(infp
);
417 #ifdef HAVE_FGETS_UNLOCKED
423 while ((inlen
= MIN(readend
- ftell(infp
), sizeof(buf
))) > 0 && !err
) {
424 inread
= SC_FREAD(buf
, 1, inlen
, infp
);
425 len
= g_base64_decode_step(buf
, inlen
, outbuf
, &state
, &save
);
426 if (uncanonicalize
== TRUE
&& strlen(outbuf
) < len
&& starting
) {
427 uncanonicalize
= FALSE
;
431 if (((inread
!= inlen
) || len
< 0) && !got_error
) {
432 g_warning("Bad BASE64 content.");
433 if (SC_FWRITE(_("[Error decoding BASE64]\n"),
435 strlen(_("[Error decoding BASE64]\n")),
436 tmpfp
) < strlen(_("[Error decoding BASE64]\n")))
437 g_warning("error decoding BASE64");
440 } else if (len
>= 0) {
441 /* print out the error message only once
444 /* we won't uncanonicalize, output to outfp directly */
445 if (SC_FWRITE(outbuf
, sizeof(gchar
), len
, outfp
) < len
)
448 if (SC_FWRITE(outbuf
, sizeof(gchar
), len
, tmpfp
) < len
)
455 if (uncanonicalize
) {
457 while (SC_FGETS(buf
, sizeof(buf
), tmpfp
) != NULL
) {
459 if (SC_FPUTS(buf
, outfp
) == EOF
)
464 procmime_fclose(tmpfp
);
465 } else if (encoding
== ENC_X_UUENCODE
) {
466 gchar outbuf
[BUFFSIZE
];
468 gboolean flag
= FALSE
;
470 while ((ftell(infp
) < readend
) && (SC_FGETS(buf
, sizeof(buf
), infp
) != NULL
)) {
471 if (!flag
&& strncmp(buf
,"begin ", 6)) continue;
474 len
= fromuutobits(outbuf
, buf
);
477 g_warning("Bad UUENCODE content (%d)", len
);
480 if (SC_FWRITE(outbuf
, sizeof(gchar
), len
, outfp
) < len
)
486 while ((ftell(infp
) < readend
) && (SC_FGETS(buf
, sizeof(buf
), infp
) != NULL
)) {
488 if (SC_FPUTS(buf
, outfp
) == EOF
)
497 g_warning("write error");
500 procmime_fclose(outfp
);
501 procmime_fclose(infp
);
507 if (g_stat(tmpfilename
, &statbuf
) < 0) {
508 FILE_OP_ERROR(tmpfilename
, "stat");
513 claws_unlink(mimeinfo
->data
.filename
);
514 g_free(mimeinfo
->data
.filename
);
515 mimeinfo
->data
.filename
= tmpfilename
;
516 mimeinfo
->tmp
= TRUE
;
517 mimeinfo
->offset
= 0;
518 mimeinfo
->length
= statbuf
.st_size
;
519 mimeinfo
->encoding_type
= ENC_BINARY
;
524 #define B64_LINE_SIZE 57
525 #define B64_BUFFSIZE 77
527 gboolean
procmime_encode_content(MimeInfo
*mimeinfo
, EncodingType encoding
)
529 FILE *infp
= NULL
, *outfp
;
533 gboolean err
= FALSE
;
535 if (mimeinfo
->content
== MIMECONTENT_EMPTY
)
538 if (mimeinfo
->encoding_type
!= ENC_UNKNOWN
&&
539 mimeinfo
->encoding_type
!= ENC_BINARY
&&
540 mimeinfo
->encoding_type
!= ENC_7BIT
&&
541 mimeinfo
->encoding_type
!= ENC_8BIT
)
542 if(!procmime_decode_content(mimeinfo
))
545 outfp
= get_tmpfile_in_dir(get_mime_tmp_dir(), &tmpfilename
);
550 #ifdef HAVE_FGETS_UNLOCKED
554 if (mimeinfo
->content
== MIMECONTENT_FILE
&& mimeinfo
->data
.filename
) {
555 if ((infp
= procmime_fopen(mimeinfo
->data
.filename
, "rb")) == NULL
) {
556 g_warning("Can't open file %s", mimeinfo
->data
.filename
);
557 procmime_fclose(outfp
);
560 } else if (mimeinfo
->content
== MIMECONTENT_MEM
) {
561 infp
= str_open_as_stream(mimeinfo
->data
.mem
);
563 procmime_fclose(outfp
);
566 #ifdef HAVE_FGETS_UNLOCKED
570 procmime_fclose(outfp
);
571 g_warning("Unknown mimeinfo");
575 if (encoding
== ENC_BASE64
) {
576 gchar inbuf
[B64_LINE_SIZE
], *out
;
578 gchar
*tmp_file
= NULL
;
580 if (mimeinfo
->type
== MIMETYPE_TEXT
||
581 mimeinfo
->type
== MIMETYPE_MESSAGE
) {
582 if (mimeinfo
->content
== MIMECONTENT_FILE
) {
583 tmp_file
= get_tmp_file();
584 if (canonicalize_file(mimeinfo
->data
.filename
, tmp_file
) < 0) {
586 procmime_fclose(infp
);
587 procmime_fclose(outfp
);
590 if ((tmp_fp
= procmime_fopen(tmp_file
, "rb")) == NULL
) {
591 FILE_OP_ERROR(tmp_file
, "fopen");
592 claws_unlink(tmp_file
);
594 procmime_fclose(infp
);
595 procmime_fclose(outfp
);
599 gchar
*out
= canonicalize_str(mimeinfo
->data
.mem
);
600 procmime_fclose(infp
);
601 infp
= str_open_as_stream(out
);
605 procmime_fclose(outfp
);
608 #ifdef HAVE_FGETS_UNLOCKED
614 while ((len
= SC_FREAD(inbuf
, sizeof(gchar
),
615 B64_LINE_SIZE
, tmp_fp
))
617 out
= g_base64_encode(inbuf
, B64_LINE_SIZE
);
618 if (SC_FPUTS(out
, outfp
) == EOF
)
621 if (SC_FPUTC('\n', outfp
) == EOF
)
624 if (len
> 0 && SC_FEOF(tmp_fp
)) {
625 out
= g_base64_encode(inbuf
, len
);
626 if (SC_FPUTS(out
, outfp
) == EOF
)
629 if (SC_FPUTC('\n', outfp
) == EOF
)
634 procmime_fclose(tmp_fp
);
635 claws_unlink(tmp_file
);
638 } else if (encoding
== ENC_QUOTED_PRINTABLE
) {
639 gchar inbuf
[BUFFSIZE
], outbuf
[BUFFSIZE
* 4];
641 while (SC_FGETS(inbuf
, sizeof(inbuf
), infp
) != NULL
) {
642 qp_encode_line(outbuf
, inbuf
);
644 if (!strncmp("From ", outbuf
, sizeof("From ")-1)) {
645 gchar
*tmpbuf
= outbuf
;
647 tmpbuf
+= sizeof("From ")-1;
649 if (SC_FPUTS("=46rom ", outfp
) == EOF
)
651 if (SC_FPUTS(tmpbuf
, outfp
) == EOF
)
654 if (SC_FPUTS(outbuf
, outfp
) == EOF
)
661 while (SC_FGETS(buf
, sizeof(buf
), infp
) != NULL
) {
663 if (SC_FPUTS(buf
, outfp
) == EOF
)
668 procmime_fclose(outfp
);
669 procmime_fclose(infp
);
674 if (mimeinfo
->content
== MIMECONTENT_FILE
) {
675 if (mimeinfo
->tmp
&& (mimeinfo
->data
.filename
!= NULL
))
676 claws_unlink(mimeinfo
->data
.filename
);
677 g_free(mimeinfo
->data
.filename
);
678 } else if (mimeinfo
->content
== MIMECONTENT_MEM
) {
679 if (mimeinfo
->tmp
&& (mimeinfo
->data
.mem
!= NULL
))
680 g_free(mimeinfo
->data
.mem
);
683 if (g_stat(tmpfilename
, &statbuf
) < 0) {
684 FILE_OP_ERROR(tmpfilename
, "stat");
687 mimeinfo
->content
= MIMECONTENT_FILE
;
688 mimeinfo
->data
.filename
= tmpfilename
;
689 mimeinfo
->tmp
= TRUE
;
690 mimeinfo
->offset
= 0;
691 mimeinfo
->length
= statbuf
.st_size
;
692 mimeinfo
->encoding_type
= encoding
;
697 gint
procmime_get_part(const gchar
*outfile
, MimeInfo
*mimeinfo
)
701 gint restlength
, readlength
;
702 gint saved_errno
= 0;
704 cm_return_val_if_fail(outfile
!= NULL
, -1);
705 cm_return_val_if_fail(mimeinfo
!= NULL
, -1);
707 if (mimeinfo
->encoding_type
!= ENC_BINARY
&& !procmime_decode_content(mimeinfo
))
710 if ((infp
= procmime_fopen(mimeinfo
->data
.filename
, "rb")) == NULL
) {
712 FILE_OP_ERROR(mimeinfo
->data
.filename
, "fopen");
713 return -(saved_errno
);
715 if (fseek(infp
, mimeinfo
->offset
, SEEK_SET
) < 0) {
717 FILE_OP_ERROR(mimeinfo
->data
.filename
, "fseek");
718 procmime_fclose(infp
);
719 return -(saved_errno
);
721 if ((outfp
= procmime_fopen(outfile
, "wb")) == NULL
) {
723 FILE_OP_ERROR(outfile
, "fopen");
724 procmime_fclose(infp
);
725 return -(saved_errno
);
728 restlength
= mimeinfo
->length
;
730 while ((restlength
> 0) && ((readlength
= SC_FREAD(buf
, 1, restlength
> BUFFSIZE
? BUFFSIZE
: restlength
, infp
)) > 0)) {
731 if (SC_FWRITE(buf
, 1, readlength
, outfp
) != readlength
) {
733 procmime_fclose(infp
);
734 procmime_fclose(outfp
);
735 return -(saved_errno
);
737 restlength
-= readlength
;
740 procmime_fclose(infp
);
741 if (procmime_fclose(outfp
) == EOF
) {
743 FILE_OP_ERROR(outfile
, "fclose");
744 claws_unlink(outfile
);
745 return -(saved_errno
);
751 gboolean
procmime_scan_text_content(MimeInfo
*mimeinfo
,
752 gboolean (*scan_callback
)(const gchar
*str
, gpointer cb_data
),
756 const gchar
*src_codeset
;
757 gboolean conv_fail
= FALSE
;
761 gboolean scan_ret
= FALSE
;
763 cm_return_val_if_fail(mimeinfo
!= NULL
, TRUE
);
764 cm_return_val_if_fail(scan_callback
!= NULL
, TRUE
);
766 if (!procmime_decode_content(mimeinfo
))
769 tmpfile
= procmime_get_tmp_file_name(mimeinfo
);
773 if (procmime_get_part(tmpfile
, mimeinfo
) < 0) {
778 tmpfp
= procmime_fopen(tmpfile
, "rb");
784 src_codeset
= forced_charset
786 procmime_mimeinfo_get_parameter(mimeinfo
, "charset");
788 /* use supersets transparently when possible */
789 if (!forced_charset
&& src_codeset
&& !strcasecmp(src_codeset
, CS_ISO_8859_1
))
790 src_codeset
= CS_WINDOWS_1252
;
791 else if (!forced_charset
&& src_codeset
&& !strcasecmp(src_codeset
, CS_X_GBK
))
792 src_codeset
= CS_GB18030
;
793 else if (!forced_charset
&& src_codeset
&& !strcasecmp(src_codeset
, CS_GBK
))
794 src_codeset
= CS_GB18030
;
795 else if (!forced_charset
&& src_codeset
&& !strcasecmp(src_codeset
, CS_GB2312
))
796 src_codeset
= CS_GB18030
;
797 else if (!forced_charset
&& src_codeset
&& !strcasecmp(src_codeset
, CS_X_VIET_VPS
))
798 src_codeset
= CS_WINDOWS_874
;
800 if (mimeinfo
->type
== MIMETYPE_TEXT
&& !g_ascii_strcasecmp(mimeinfo
->subtype
, "html")) {
801 SC_HTMLParser
*parser
;
804 conv
= conv_code_converter_new(src_codeset
);
805 parser
= sc_html_parser_new(tmpfp
, conv
);
806 while ((str
= sc_html_parse(parser
)) != NULL
) {
807 if ((scan_ret
= scan_callback(str
, cb_data
)) == TRUE
)
810 sc_html_parser_destroy(parser
);
811 conv_code_converter_destroy(conv
);
812 } else if (mimeinfo
->type
== MIMETYPE_TEXT
&& !g_ascii_strcasecmp(mimeinfo
->subtype
, "enriched")) {
816 conv
= conv_code_converter_new(src_codeset
);
817 parser
= ertf_parser_new(tmpfp
, conv
);
818 while ((str
= ertf_parse(parser
)) != NULL
) {
819 if ((scan_ret
= scan_callback(str
, cb_data
)) == TRUE
)
822 ertf_parser_destroy(parser
);
823 conv_code_converter_destroy(conv
);
824 } else if (mimeinfo
->type
== MIMETYPE_TEXT
&& mimeinfo
->disposition
!= DISPOSITIONTYPE_ATTACHMENT
) {
825 while (SC_FGETS(buf
, sizeof(buf
), tmpfp
) != NULL
) {
826 str
= conv_codeset_strdup(buf
, src_codeset
, CS_UTF_8
);
828 if ((scan_ret
= scan_callback(str
, cb_data
)) == TRUE
) {
835 if ((scan_ret
= scan_callback(buf
, cb_data
)) == TRUE
)
842 g_warning("procmime_get_text_content(): Code conversion failed.");
844 procmime_fclose(tmpfp
);
845 claws_unlink(tmpfile
);
851 static gboolean
scan_fputs_cb(const gchar
*str
, gpointer fp
)
853 if (SC_FPUTS(str
, (FILE *)fp
) == EOF
)
859 FILE *procmime_get_text_content(MimeInfo
*mimeinfo
)
864 if ((outfp
= my_tmpfile()) == NULL
) {
865 perror("my_tmpfile");
868 #ifdef HAVE_FGETS_UNLOCKED
872 err
= procmime_scan_text_content(mimeinfo
, scan_fputs_cb
, outfp
);
876 procmime_fclose(outfp
);
879 #ifdef HAVE_FGETS_UNLOCKED
886 FILE *procmime_get_binary_content(MimeInfo
*mimeinfo
)
891 cm_return_val_if_fail(mimeinfo
!= NULL
, NULL
);
893 if (!procmime_decode_content(mimeinfo
))
896 tmpfile
= procmime_get_tmp_file_name(mimeinfo
);
900 if (procmime_get_part(tmpfile
, mimeinfo
) < 0) {
905 outfp
= procmime_fopen(tmpfile
, "rb");
915 #ifdef HAVE_FGETS_UNLOCKED
921 /* search the first text part of (multipart) MIME message,
922 decode, convert it and output to outfp. */
923 FILE *procmime_get_first_text_content(MsgInfo
*msginfo
)
926 MimeInfo
*mimeinfo
, *partinfo
;
927 gboolean empty_ok
= FALSE
, short_scan
= TRUE
;
929 cm_return_val_if_fail(msginfo
!= NULL
, NULL
);
931 /* first we try to short-scan (for speed), refusing empty parts */
934 mimeinfo
= procmime_scan_message_short(msginfo
);
936 mimeinfo
= procmime_scan_message(msginfo
);
937 if (!mimeinfo
) return NULL
;
940 while (partinfo
&& (partinfo
->type
!= MIMETYPE_TEXT
||
941 (partinfo
->length
== 0 && !empty_ok
))) {
942 partinfo
= procmime_mimeinfo_next(partinfo
);
945 outfp
= procmime_get_text_content(partinfo
);
946 else if (!empty_ok
&& short_scan
) {
947 /* if short scan didn't find a non-empty part, rescan
948 * fully for non-empty parts
951 procmime_mimeinfo_free_all(&mimeinfo
);
953 } else if (!empty_ok
&& !short_scan
) {
954 /* if full scan didn't find a non-empty part, rescan
955 * accepting empty parts
958 procmime_mimeinfo_free_all(&mimeinfo
);
961 procmime_mimeinfo_free_all(&mimeinfo
);
963 /* outfp already unlocked at this time */
968 static gboolean
find_encrypted_func(GNode
*node
, gpointer data
)
970 MimeInfo
*mimeinfo
= (MimeInfo
*) node
->data
;
971 MimeInfo
**encinfo
= (MimeInfo
**) data
;
973 if (privacy_mimeinfo_is_encrypted(mimeinfo
)) {
981 static MimeInfo
*find_encrypted_part(MimeInfo
*rootinfo
)
983 MimeInfo
*encinfo
= NULL
;
985 g_node_traverse(rootinfo
->node
, G_IN_ORDER
, G_TRAVERSE_ALL
, -1,
986 find_encrypted_func
, &encinfo
);
991 /* search the first encrypted text part of (multipart) MIME message,
992 decode, convert it and output to outfp. */
993 FILE *procmime_get_first_encrypted_text_content(MsgInfo
*msginfo
)
996 MimeInfo
*mimeinfo
, *partinfo
, *encinfo
;
998 cm_return_val_if_fail(msginfo
!= NULL
, NULL
);
1000 mimeinfo
= procmime_scan_message(msginfo
);
1005 partinfo
= mimeinfo
;
1006 if ((encinfo
= find_encrypted_part(partinfo
)) != NULL
) {
1007 debug_print("decrypting message part\n");
1008 if (privacy_mimeinfo_decrypt(encinfo
) < 0) {
1009 alertpanel_error(_("Couldn't decrypt: %s"),
1010 privacy_get_error());
1014 partinfo
= mimeinfo
;
1015 while (partinfo
&& partinfo
->type
!= MIMETYPE_TEXT
) {
1016 partinfo
= procmime_mimeinfo_next(partinfo
);
1017 if (privacy_mimeinfo_is_signed(partinfo
))
1018 procmsg_msginfo_set_flags(msginfo
, 0, MSG_SIGNED
);
1022 outfp
= procmime_get_text_content(partinfo
);
1024 procmime_mimeinfo_free_all(&mimeinfo
);
1026 /* outfp already unlocked at this time */
1030 gboolean
procmime_msginfo_is_encrypted(MsgInfo
*msginfo
)
1032 MimeInfo
*mimeinfo
, *partinfo
;
1033 gboolean result
= FALSE
;
1035 cm_return_val_if_fail(msginfo
!= NULL
, FALSE
);
1037 mimeinfo
= procmime_scan_message(msginfo
);
1042 partinfo
= mimeinfo
;
1043 result
= (find_encrypted_part(partinfo
) != NULL
);
1044 procmime_mimeinfo_free_all(&mimeinfo
);
1049 gchar
*procmime_get_tmp_file_name(MimeInfo
*mimeinfo
)
1051 static guint32 id
= 0;
1056 cm_return_val_if_fail(mimeinfo
!= NULL
, NULL
);
1058 g_snprintf(f_prefix
, sizeof(f_prefix
), "%08x.", id
++);
1060 if ((mimeinfo
->type
== MIMETYPE_TEXT
) && !g_ascii_strcasecmp(mimeinfo
->subtype
, "html"))
1061 base
= g_strdup("mimetmp.html");
1063 const gchar
*basetmp
;
1065 basetmp
= procmime_mimeinfo_get_parameter(mimeinfo
, "filename");
1066 if (basetmp
== NULL
)
1067 basetmp
= procmime_mimeinfo_get_parameter(mimeinfo
, "name");
1068 if (basetmp
== NULL
)
1069 basetmp
= "mimetmp";
1070 basetmp
= g_path_get_basename(basetmp
);
1071 if (*basetmp
== '\0')
1072 basetmp
= g_strdup("mimetmp");
1073 base
= conv_filename_from_utf8(basetmp
);
1074 g_free((gchar
*)basetmp
);
1075 subst_for_shellsafe_filename(base
);
1078 filename
= g_strconcat(get_mime_tmp_dir(), G_DIR_SEPARATOR_S
,
1079 f_prefix
, base
, NULL
);
1086 static GList
*mime_type_list
= NULL
;
1088 gchar
*procmime_get_mime_type(const gchar
*filename
)
1094 static GHashTable
*mime_type_table
= NULL
;
1095 MimeType
*mime_type
;
1097 if (!mime_type_table
) {
1098 mime_type_table
= procmime_get_mime_type_table();
1099 if (!mime_type_table
) return NULL
;
1103 if (filename
== NULL
)
1106 base
= g_path_get_basename(filename
);
1107 if ((p
= strrchr(base
, '.')) != NULL
)
1108 ext
= g_utf8_strdown(p
+ 1, -1);
1110 ext
= g_utf8_strdown(base
, -1);
1114 mime_type
= g_hash_table_lookup(mime_type_table
, ext
);
1118 str
= g_strconcat(mime_type
->type
, "/", mime_type
->sub_type
,
1120 debug_print("got type %s for %s\n", str
, ext
);
1127 gchar
*str
= get_content_type_from_registry_with_ext(ext
);
1134 static guint
procmime_str_hash(gconstpointer gptr
)
1136 guint hash_result
= 0;
1139 for (str
= gptr
; str
&& *str
; str
++) {
1140 if (isupper(*str
)) hash_result
+= (*str
+ ' ');
1141 else hash_result
+= *str
;
1147 static gint
procmime_str_equal(gconstpointer gptr1
, gconstpointer gptr2
)
1149 const char *str1
= gptr1
;
1150 const char *str2
= gptr2
;
1152 return !g_utf8_collate(str1
, str2
);
1155 static GHashTable
*procmime_get_mime_type_table(void)
1157 GHashTable
*table
= NULL
;
1159 MimeType
*mime_type
;
1162 if (!mime_type_list
) {
1163 mime_type_list
= procmime_get_mime_type_list();
1164 if (!mime_type_list
) return NULL
;
1167 table
= g_hash_table_new(procmime_str_hash
, procmime_str_equal
);
1169 for (cur
= mime_type_list
; cur
!= NULL
; cur
= cur
->next
) {
1173 mime_type
= (MimeType
*)cur
->data
;
1175 if (!mime_type
->extension
) continue;
1177 exts
= g_strsplit(mime_type
->extension
, " ", 16);
1178 for (i
= 0; exts
[i
] != NULL
; i
++) {
1179 /* Don't overwrite previously inserted extension */
1180 if (!g_hash_table_lookup(table
, exts
[i
])) {
1181 key
= g_strdup(exts
[i
]);
1182 g_hash_table_insert(table
, key
, mime_type
);
1191 GList
*procmime_get_mime_type_list(void)
1195 gchar buf
[BUFFSIZE
];
1198 MimeType
*mime_type
;
1199 gboolean fp_is_glob_file
= TRUE
;
1202 return mime_type_list
;
1204 #if defined(__NetBSD__) || defined(__OpenBSD__) || defined(__FreeBSD__)
1205 if ((fp
= procmime_fopen(DATAROOTDIR
"/mime/globs", "rb")) == NULL
)
1207 if ((fp
= procmime_fopen("/usr/share/mime/globs", "rb")) == NULL
)
1210 fp_is_glob_file
= FALSE
;
1211 if ((fp
= procmime_fopen("/etc/mime.types", "rb")) == NULL
) {
1212 if ((fp
= procmime_fopen(SYSCONFDIR
"/mime.types", "rb"))
1214 FILE_OP_ERROR(SYSCONFDIR
"/mime.types",
1221 while (SC_FGETS(buf
, sizeof(buf
), fp
) != NULL
) {
1222 p
= strchr(buf
, '#');
1228 if (fp_is_glob_file
) {
1229 while (*p
&& !g_ascii_isspace(*p
) && (*p
!=':')) p
++;
1231 while (*p
&& !g_ascii_isspace(*p
)) p
++;
1238 delim
= strchr(buf
, '/');
1239 if (delim
== NULL
) continue;
1242 mime_type
= g_new(MimeType
, 1);
1243 mime_type
->type
= g_strdup(buf
);
1244 mime_type
->sub_type
= g_strdup(delim
+ 1);
1246 if (fp_is_glob_file
) {
1247 while (*p
&& (g_ascii_isspace(*p
)||(*p
=='*')||(*p
=='.'))) p
++;
1249 while (*p
&& g_ascii_isspace(*p
)) p
++;
1253 mime_type
->extension
= g_utf8_strdown(p
, -1);
1255 mime_type
->extension
= NULL
;
1257 list
= g_list_append(list
, mime_type
);
1260 procmime_fclose(fp
);
1263 g_warning("Can't read mime.types");
1268 EncodingType
procmime_get_encoding_for_charset(const gchar
*charset
)
1272 else if (!g_ascii_strncasecmp(charset
, "ISO-2022-", 9) ||
1273 !g_ascii_strcasecmp(charset
, "US-ASCII"))
1275 else if (!g_ascii_strcasecmp(charset
, "ISO-8859-5") ||
1276 !g_ascii_strncasecmp(charset
, "KOI8-", 5) ||
1277 !g_ascii_strcasecmp(charset
, "X-MAC-CYRILLIC") ||
1278 !g_ascii_strcasecmp(charset
, "MAC-CYRILLIC") ||
1279 !g_ascii_strcasecmp(charset
, "Windows-1251"))
1281 else if (!g_ascii_strncasecmp(charset
, "ISO-8859-", 9))
1282 return ENC_QUOTED_PRINTABLE
;
1283 else if (!g_ascii_strncasecmp(charset
, "UTF-8", 5))
1284 return ENC_QUOTED_PRINTABLE
;
1289 EncodingType
procmime_get_encoding_for_text_file(const gchar
*file
, gboolean
*has_binary
)
1292 guchar buf
[BUFFSIZE
];
1294 size_t octet_chars
= 0;
1295 size_t total_len
= 0;
1296 gfloat octet_percentage
;
1297 gboolean force_b64
= FALSE
;
1299 if ((fp
= procmime_fopen(file
, "rb")) == NULL
) {
1300 FILE_OP_ERROR(file
, "fopen");
1304 while ((len
= SC_FREAD(buf
, sizeof(guchar
), sizeof(buf
), fp
)) > 0) {
1308 for (p
= buf
, i
= 0; i
< len
; ++p
, ++i
) {
1319 procmime_fclose(fp
);
1322 octet_percentage
= (gfloat
)octet_chars
/ (gfloat
)total_len
;
1324 octet_percentage
= 0.0;
1326 debug_print("procmime_get_encoding_for_text_file(): "
1327 "8bit chars: %zd / %zd (%f%%)\n", octet_chars
, total_len
,
1328 100.0 * octet_percentage
);
1330 if (octet_percentage
> 0.20 || force_b64
) {
1331 debug_print("using BASE64\n");
1333 } else if (octet_chars
> 0) {
1334 debug_print("using quoted-printable\n");
1335 return ENC_QUOTED_PRINTABLE
;
1337 debug_print("using 7bit\n");
1342 struct EncodingTable
1345 EncodingType enc_type
;
1348 struct EncodingTable encoding_table
[] = {
1351 {"binary", ENC_BINARY
},
1352 {"quoted-printable", ENC_QUOTED_PRINTABLE
},
1353 {"base64", ENC_BASE64
},
1354 {"x-uuencode", ENC_UNKNOWN
},
1355 {NULL
, ENC_UNKNOWN
},
1358 const gchar
*procmime_get_encoding_str(EncodingType encoding
)
1360 struct EncodingTable
*enc_table
;
1362 for (enc_table
= encoding_table
; enc_table
->str
!= NULL
; enc_table
++) {
1363 if (enc_table
->enc_type
== encoding
)
1364 return enc_table
->str
;
1369 /* --- NEW MIME STUFF --- */
1376 static struct TypeTable mime_type_table
[] = {
1377 {"text", MIMETYPE_TEXT
},
1378 {"image", MIMETYPE_IMAGE
},
1379 {"audio", MIMETYPE_AUDIO
},
1380 {"video", MIMETYPE_VIDEO
},
1381 {"application", MIMETYPE_APPLICATION
},
1382 {"message", MIMETYPE_MESSAGE
},
1383 {"multipart", MIMETYPE_MULTIPART
},
1387 const gchar
*procmime_get_media_type_str(MimeMediaType type
)
1389 struct TypeTable
*type_table
;
1391 for (type_table
= mime_type_table
; type_table
->str
!= NULL
; type_table
++) {
1392 if (type_table
->type
== type
)
1393 return type_table
->str
;
1398 MimeMediaType
procmime_get_media_type(const gchar
*str
)
1400 struct TypeTable
*typetablearray
;
1402 for (typetablearray
= mime_type_table
; typetablearray
->str
!= NULL
; typetablearray
++)
1403 if (g_ascii_strncasecmp(str
, typetablearray
->str
, strlen(typetablearray
->str
)) == 0)
1404 return typetablearray
->type
;
1406 return MIMETYPE_UNKNOWN
;
1410 *\brief Safe wrapper for content type string.
1412 *\return const gchar * Pointer to content type string.
1414 gchar
*procmime_get_content_type_str(MimeMediaType type
,
1415 const char *subtype
)
1417 const gchar
*type_str
= NULL
;
1419 if (subtype
== NULL
|| !(type_str
= procmime_get_media_type_str(type
)))
1420 return g_strdup("unknown");
1421 return g_strdup_printf("%s/%s", type_str
, subtype
);
1424 static int procmime_parse_mimepart(MimeInfo
*parent
,
1425 gchar
*content_type
,
1426 gchar
*content_encoding
,
1427 gchar
*content_description
,
1429 gchar
*content_disposition
,
1430 gchar
*content_location
,
1431 const gchar
*original_msgid
,
1432 const gchar
*disposition_notification_hdr
,
1433 const gchar
*filename
,
1436 gboolean short_scan
);
1438 static void procmime_parse_message_rfc822(MimeInfo
*mimeinfo
, gboolean short_scan
)
1440 HeaderEntry hentry
[] = {{"Content-Type:", NULL
, TRUE
},
1441 {"Content-Transfer-Encoding:",
1443 {"Content-Description:",
1447 {"Content-Disposition:",
1449 {"Content-Location:",
1453 {"Original-Message-ID:",
1457 {NULL
, NULL
, FALSE
}};
1458 guint content_start
, i
;
1463 procmime_decode_content(mimeinfo
);
1465 fp
= procmime_fopen(mimeinfo
->data
.filename
, "rb");
1467 FILE_OP_ERROR(mimeinfo
->data
.filename
, "fopen");
1470 if (fseek(fp
, mimeinfo
->offset
, SEEK_SET
) < 0) {
1471 FILE_OP_ERROR(mimeinfo
->data
.filename
, "fseek");
1472 procmime_fclose(fp
);
1475 procheader_get_header_fields(fp
, hentry
);
1476 if (hentry
[0].body
!= NULL
) {
1477 tmp
= conv_unmime_header(hentry
[0].body
, NULL
, FALSE
);
1478 g_free(hentry
[0].body
);
1479 hentry
[0].body
= tmp
;
1481 if (hentry
[2].body
!= NULL
) {
1482 tmp
= conv_unmime_header(hentry
[2].body
, NULL
, FALSE
);
1483 g_free(hentry
[2].body
);
1484 hentry
[2].body
= tmp
;
1486 if (hentry
[4].body
!= NULL
) {
1487 tmp
= conv_unmime_header(hentry
[4].body
, NULL
, FALSE
);
1488 g_free(hentry
[4].body
);
1489 hentry
[4].body
= tmp
;
1491 if (hentry
[5].body
!= NULL
) {
1492 tmp
= conv_unmime_header(hentry
[5].body
, NULL
, FALSE
);
1493 g_free(hentry
[5].body
);
1494 hentry
[5].body
= tmp
;
1496 if (hentry
[7].body
!= NULL
) {
1497 tmp
= conv_unmime_header(hentry
[7].body
, NULL
, FALSE
);
1498 g_free(hentry
[7].body
);
1499 hentry
[7].body
= tmp
;
1501 if (hentry
[8].body
!= NULL
) {
1502 tmp
= conv_unmime_header(hentry
[8].body
, NULL
, FALSE
);
1503 g_free(hentry
[8].body
);
1504 hentry
[8].body
= tmp
;
1507 content_start
= ftell(fp
);
1508 procmime_fclose(fp
);
1510 len
= mimeinfo
->length
- (content_start
- mimeinfo
->offset
);
1513 procmime_parse_mimepart(mimeinfo
,
1514 hentry
[0].body
, hentry
[1].body
,
1515 hentry
[2].body
, hentry
[3].body
,
1516 hentry
[4].body
, hentry
[5].body
,
1517 hentry
[7].body
, hentry
[8].body
,
1518 mimeinfo
->data
.filename
, content_start
,
1521 for (i
= 0; i
< (sizeof hentry
/ sizeof hentry
[0]); i
++) {
1522 g_free(hentry
[i
].body
);
1523 hentry
[i
].body
= NULL
;
1527 static void procmime_parse_disposition_notification(MimeInfo
*mimeinfo
,
1528 const gchar
*original_msgid
, const gchar
*disposition_notification_hdr
,
1529 gboolean short_scan
)
1531 HeaderEntry hentry
[] = {{"Original-Message-ID:", NULL
, TRUE
},
1532 {"Disposition:", NULL
, TRUE
},
1533 {NULL
, NULL
, FALSE
}};
1536 gchar
*orig_msg_id
= NULL
;
1539 procmime_decode_content(mimeinfo
);
1541 debug_print("parse disposition notification\n");
1542 fp
= procmime_fopen(mimeinfo
->data
.filename
, "rb");
1544 FILE_OP_ERROR(mimeinfo
->data
.filename
, "fopen");
1547 if (fseek(fp
, mimeinfo
->offset
, SEEK_SET
) < 0) {
1548 FILE_OP_ERROR(mimeinfo
->data
.filename
, "fseek");
1549 procmime_fclose(fp
);
1553 if (original_msgid
&& disposition_notification_hdr
) {
1554 hentry
[0].body
= g_strdup(original_msgid
);
1555 hentry
[1].body
= g_strdup(disposition_notification_hdr
);
1557 procheader_get_header_fields(fp
, hentry
);
1560 procmime_fclose(fp
);
1562 if (!hentry
[0].body
|| !hentry
[1].body
) {
1563 debug_print("MsgId %s, Disp %s\n",
1564 hentry
[0].body
? hentry
[0].body
:"(nil)",
1565 hentry
[1].body
? hentry
[1].body
:"(nil)");
1569 orig_msg_id
= g_strdup(hentry
[0].body
);
1570 disp
= g_strdup(hentry
[1].body
);
1572 extract_parenthesis(orig_msg_id
, '<', '>');
1573 remove_space(orig_msg_id
);
1575 if (strstr(disp
, "displayed")) {
1576 /* find sent message, if possible */
1577 MsgInfo
*info
= NULL
;
1579 debug_print("%s has been displayed.\n", orig_msg_id
);
1580 for (flist
= folder_get_list(); flist
!= NULL
; flist
= g_list_next(flist
)) {
1581 FolderItem
*outbox
= ((Folder
*)(flist
->data
))->outbox
;
1583 debug_print("skipping folder with no outbox...\n");
1586 info
= folder_item_get_msginfo_by_msgid(outbox
, orig_msg_id
);
1587 debug_print("%s %s in %s\n", info
?"found":"didn't find", orig_msg_id
, outbox
->path
);
1589 procmsg_msginfo_set_flags(info
, MSG_RETRCPT_GOT
, 0);
1590 procmsg_msginfo_free(&info
);
1594 g_free(orig_msg_id
);
1597 for (i
= 0; i
< (sizeof hentry
/ sizeof hentry
[0]); i
++) {
1598 g_free(hentry
[i
].body
);
1599 hentry
[i
].body
= NULL
;
1603 #define GET_HEADERS() { \
1604 procheader_get_header_fields(fp, hentry); \
1605 if (hentry[0].body != NULL) { \
1606 tmp = conv_unmime_header(hentry[0].body, NULL, FALSE); \
1607 g_free(hentry[0].body); \
1608 hentry[0].body = tmp; \
1610 if (hentry[2].body != NULL) { \
1611 tmp = conv_unmime_header(hentry[2].body, NULL, FALSE); \
1612 g_free(hentry[2].body); \
1613 hentry[2].body = tmp; \
1615 if (hentry[4].body != NULL) { \
1616 tmp = conv_unmime_header(hentry[4].body, NULL, FALSE); \
1617 g_free(hentry[4].body); \
1618 hentry[4].body = tmp; \
1620 if (hentry[5].body != NULL) { \
1621 tmp = conv_unmime_header(hentry[5].body, NULL, FALSE); \
1622 g_free(hentry[5].body); \
1623 hentry[5].body = tmp; \
1625 if (hentry[6].body != NULL) { \
1626 tmp = conv_unmime_header(hentry[6].body, NULL, FALSE); \
1627 g_free(hentry[6].body); \
1628 hentry[6].body = tmp; \
1630 if (hentry[7].body != NULL) { \
1631 tmp = conv_unmime_header(hentry[7].body, NULL, FALSE); \
1632 g_free(hentry[7].body); \
1633 hentry[7].body = tmp; \
1637 static void procmime_parse_multipart(MimeInfo
*mimeinfo
, gboolean short_scan
)
1639 HeaderEntry hentry
[] = {{"Content-Type:", NULL
, TRUE
},
1640 {"Content-Transfer-Encoding:",
1642 {"Content-Description:",
1646 {"Content-Disposition:",
1648 {"Content-Location:",
1650 {"Original-Message-ID:",
1654 {NULL
, NULL
, FALSE
}};
1657 gint boundary_len
= 0, lastoffset
= -1, i
;
1658 gchar buf
[BUFFSIZE
];
1661 gboolean start_found
= FALSE
;
1662 gboolean end_found
= FALSE
;
1664 boundary
= g_hash_table_lookup(mimeinfo
->typeparameters
, "boundary");
1667 boundary_len
= strlen(boundary
);
1669 procmime_decode_content(mimeinfo
);
1671 fp
= procmime_fopen(mimeinfo
->data
.filename
, "rb");
1673 FILE_OP_ERROR(mimeinfo
->data
.filename
, "fopen");
1677 if (fseek(fp
, mimeinfo
->offset
, SEEK_SET
) < 0) {
1678 FILE_OP_ERROR(mimeinfo
->data
.filename
, "fseek");
1679 procmime_fclose(fp
);
1683 while (SC_FGETS(buf
, sizeof(buf
), fp
) != NULL
&& result
== 0) {
1684 if (ftell(fp
) - 1 > (mimeinfo
->offset
+ mimeinfo
->length
))
1687 if (IS_BOUNDARY(buf
, boundary
, boundary_len
)) {
1690 if (lastoffset
!= -1) {
1691 gint len
= (ftell(fp
) - strlen(buf
)) - lastoffset
- 1;
1694 result
= procmime_parse_mimepart(mimeinfo
,
1695 hentry
[0].body
, hentry
[1].body
,
1696 hentry
[2].body
, hentry
[3].body
,
1697 hentry
[4].body
, hentry
[5].body
,
1698 hentry
[6].body
, hentry
[7].body
,
1699 mimeinfo
->data
.filename
, lastoffset
,
1701 if (result
== 1 && short_scan
)
1706 if (buf
[2 + boundary_len
] == '-' &&
1707 buf
[2 + boundary_len
+ 1] == '-') {
1711 for (i
= 0; i
< (sizeof hentry
/ sizeof hentry
[0]) ; i
++) {
1712 g_free(hentry
[i
].body
);
1713 hentry
[i
].body
= NULL
;
1716 lastoffset
= ftell(fp
);
1720 if (start_found
&& !end_found
&& lastoffset
!= -1) {
1721 gint len
= (ftell(fp
) - strlen(buf
)) - lastoffset
- 1;
1724 result
= procmime_parse_mimepart(mimeinfo
,
1725 hentry
[0].body
, hentry
[1].body
,
1726 hentry
[2].body
, hentry
[3].body
,
1727 hentry
[4].body
, hentry
[5].body
,
1728 hentry
[6].body
, hentry
[7].body
,
1729 mimeinfo
->data
.filename
, lastoffset
,
1732 mimeinfo
->broken
= TRUE
;
1735 for (i
= 0; i
< (sizeof hentry
/ sizeof hentry
[0]); i
++) {
1736 g_free(hentry
[i
].body
);
1737 hentry
[i
].body
= NULL
;
1739 procmime_fclose(fp
);
1742 static void parse_parameters(const gchar
*parameters
, GHashTable
*table
)
1744 gchar
*params
, *param
, *next
;
1745 GSList
*convlist
= NULL
, *concatlist
= NULL
, *cur
;
1747 params
= g_strdup(parameters
);
1750 for (; next
!= NULL
; param
= next
) {
1751 gchar
*attribute
, *value
, *tmp
, *down_attr
, *orig_down_attr
;
1753 gboolean convert
= FALSE
;
1755 next
= strchr_with_skip_quote(param
, '"', ';');
1764 value
= strchr(attribute
, '=');
1770 while (value
[0] != '\0' && value
[0] == ' ')
1773 down_attr
= g_utf8_strdown(attribute
, -1);
1774 orig_down_attr
= down_attr
;
1776 len
= down_attr
? strlen(down_attr
):0;
1777 if (len
> 0 && down_attr
[len
- 1] == '*') {
1778 gchar
*srcpos
, *dstpos
, *endpos
;
1781 down_attr
[len
- 1] = '\0';
1785 endpos
= value
+ strlen(value
);
1786 while (srcpos
< endpos
) {
1792 if (!get_hex_value(&dstvalue
, srcpos
[1], srcpos
[2]))
1802 if (value
[0] == '"')
1803 extract_quote(value
, '"');
1805 if (value
[0] == '"')
1806 extract_quote(value
, '"');
1807 else if ((tmp
= strchr(value
, ' ')) != NULL
)
1812 while (down_attr
[0] == ' ')
1814 while (down_attr
[strlen(down_attr
)-1] == ' ')
1815 down_attr
[strlen(down_attr
)-1] = '\0';
1818 while (value
[0] != '\0' && value
[0] == ' ')
1820 while (value
[strlen(value
)-1] == ' ')
1821 value
[strlen(value
)-1] = '\0';
1823 if (down_attr
&& strrchr(down_attr
, '*') != NULL
) {
1826 tmpattr
= g_strdup(down_attr
);
1827 tmp
= strrchr(tmpattr
, '*');
1830 if ((tmp
[1] == '0') && (tmp
[2] == '\0') &&
1831 (g_slist_find_custom(concatlist
, down_attr
, (GCompareFunc
)g_strcmp0
) == NULL
))
1832 concatlist
= g_slist_prepend(concatlist
, g_strdup(tmpattr
));
1834 if (convert
&& (g_slist_find_custom(convlist
, tmpattr
, (GCompareFunc
)g_strcmp0
) == NULL
))
1835 convlist
= g_slist_prepend(convlist
, g_strdup(tmpattr
));
1838 } else if (convert
) {
1839 if (g_slist_find_custom(convlist
, down_attr
, (GCompareFunc
)g_strcmp0
) == NULL
)
1840 convlist
= g_slist_prepend(convlist
, g_strdup(down_attr
));
1843 if (g_hash_table_lookup(table
, down_attr
) == NULL
)
1844 g_hash_table_insert(table
, g_strdup(down_attr
), g_strdup(value
));
1845 g_free(orig_down_attr
);
1848 for (cur
= concatlist
; cur
!= NULL
; cur
= g_slist_next(cur
)) {
1849 gchar
*attribute
, *attrwnum
, *partvalue
;
1853 attribute
= (gchar
*) cur
->data
;
1854 value
= g_string_sized_new(64);
1856 attrwnum
= g_strdup_printf("%s*%d", attribute
, n
);
1857 while ((partvalue
= g_hash_table_lookup(table
, attrwnum
)) != NULL
) {
1858 g_string_append(value
, partvalue
);
1860 g_hash_table_remove(table
, attrwnum
);
1863 attrwnum
= g_strdup_printf("%s*%d", attribute
, n
);
1867 g_hash_table_insert(table
, g_strdup(attribute
), g_strdup(value
->str
));
1868 g_string_free(value
, TRUE
);
1870 slist_free_strings_full(concatlist
);
1872 for (cur
= convlist
; cur
!= NULL
; cur
= g_slist_next(cur
)) {
1873 gchar
*attribute
, *key
, *value
;
1874 gchar
*charset
, *lang
, *oldvalue
, *newvalue
;
1876 attribute
= (gchar
*) cur
->data
;
1877 if (!g_hash_table_lookup_extended(
1878 table
, attribute
, (gpointer
*)(gchar
*) &key
, (gpointer
*)(gchar
*) &value
))
1882 if (charset
== NULL
)
1884 lang
= strchr(charset
, '\'');
1889 oldvalue
= strchr(lang
, '\'');
1890 if (oldvalue
== NULL
)
1895 newvalue
= conv_codeset_strdup(oldvalue
, charset
, CS_UTF_8
);
1897 g_hash_table_remove(table
, attribute
);
1901 g_hash_table_insert(table
, g_strdup(attribute
), newvalue
);
1903 slist_free_strings_full(convlist
);
1908 static void procmime_parse_content_type(const gchar
*content_type
, MimeInfo
*mimeinfo
)
1910 cm_return_if_fail(content_type
!= NULL
);
1911 cm_return_if_fail(mimeinfo
!= NULL
);
1913 /* RFC 2045, page 13 says that the mime subtype is MANDATORY;
1914 * if it's not available we use the default Content-Type */
1915 if ((content_type
[0] == '\0') || (strchr(content_type
, '/') == NULL
)) {
1916 mimeinfo
->type
= MIMETYPE_TEXT
;
1917 mimeinfo
->subtype
= g_strdup("plain");
1918 if (g_hash_table_lookup(mimeinfo
->typeparameters
,
1919 "charset") == NULL
) {
1920 g_hash_table_insert(mimeinfo
->typeparameters
,
1921 g_strdup("charset"),
1923 conv_get_locale_charset_str_no_utf8()));
1926 gchar
*type
, *subtype
, *params
;
1928 type
= g_strdup(content_type
);
1929 subtype
= strchr(type
, '/') + 1;
1930 *(subtype
- 1) = '\0';
1931 if ((params
= strchr(subtype
, ';')) != NULL
) {
1936 mimeinfo
->type
= procmime_get_media_type(type
);
1937 mimeinfo
->subtype
= g_strstrip(g_strdup(subtype
));
1939 /* Get mimeinfo->typeparameters */
1941 parse_parameters(params
, mimeinfo
->typeparameters
);
1947 static void procmime_parse_content_disposition(const gchar
*content_disposition
, MimeInfo
*mimeinfo
)
1949 gchar
*tmp
, *params
;
1951 cm_return_if_fail(content_disposition
!= NULL
);
1952 cm_return_if_fail(mimeinfo
!= NULL
);
1954 tmp
= g_strdup(content_disposition
);
1955 if ((params
= strchr(tmp
, ';')) != NULL
) {
1961 if (!g_ascii_strcasecmp(tmp
, "inline"))
1962 mimeinfo
->disposition
= DISPOSITIONTYPE_INLINE
;
1963 else if (!g_ascii_strcasecmp(tmp
, "attachment"))
1964 mimeinfo
->disposition
= DISPOSITIONTYPE_ATTACHMENT
;
1966 mimeinfo
->disposition
= DISPOSITIONTYPE_ATTACHMENT
;
1969 parse_parameters(params
, mimeinfo
->dispositionparameters
);
1975 static void procmime_parse_content_encoding(const gchar
*content_encoding
, MimeInfo
*mimeinfo
)
1977 struct EncodingTable
*enc_table
;
1979 for (enc_table
= encoding_table
; enc_table
->str
!= NULL
; enc_table
++) {
1980 if (g_ascii_strcasecmp(enc_table
->str
, content_encoding
) == 0) {
1981 mimeinfo
->encoding_type
= enc_table
->enc_type
;
1985 mimeinfo
->encoding_type
= ENC_UNKNOWN
;
1989 static GSList
*registered_parsers
= NULL
;
1991 static MimeParser
*procmime_get_mimeparser_for_type(MimeMediaType type
, const gchar
*sub_type
)
1994 for (cur
= registered_parsers
; cur
; cur
= cur
->next
) {
1995 MimeParser
*parser
= (MimeParser
*)cur
->data
;
1996 if (parser
->type
== type
&& !strcmp2(parser
->sub_type
, sub_type
))
2002 void procmime_mimeparser_register(MimeParser
*parser
)
2004 if (!procmime_get_mimeparser_for_type(parser
->type
, parser
->sub_type
))
2005 registered_parsers
= g_slist_append(registered_parsers
, parser
);
2009 void procmime_mimeparser_unregister(MimeParser
*parser
)
2011 registered_parsers
= g_slist_remove(registered_parsers
, parser
);
2014 static gboolean
procmime_mimeparser_parse(MimeParser
*parser
, MimeInfo
*mimeinfo
)
2016 cm_return_val_if_fail(parser
->parse
!= NULL
, FALSE
);
2017 return parser
->parse(parser
, mimeinfo
);
2020 static int procmime_parse_mimepart(MimeInfo
*parent
,
2021 gchar
*content_type
,
2022 gchar
*content_encoding
,
2023 gchar
*content_description
,
2025 gchar
*content_disposition
,
2026 gchar
*content_location
,
2027 const gchar
*original_msgid
,
2028 const gchar
*disposition_notification_hdr
,
2029 const gchar
*filename
,
2032 gboolean short_scan
)
2035 MimeParser
*parser
= NULL
;
2036 gboolean parsed
= FALSE
;
2039 /* Create MimeInfo */
2040 mimeinfo
= procmime_mimeinfo_new();
2041 mimeinfo
->content
= MIMECONTENT_FILE
;
2043 if (parent
!= NULL
) {
2044 if (g_node_depth(parent
->node
) > 32) {
2045 /* 32 is an arbitrary value
2046 * this avoids DOSsing ourselves
2047 * with enormous messages
2049 procmime_mimeinfo_free_all(&mimeinfo
);
2052 g_node_append(parent
->node
, mimeinfo
->node
);
2054 mimeinfo
->data
.filename
= g_strdup(filename
);
2055 mimeinfo
->offset
= offset
;
2056 mimeinfo
->length
= length
;
2058 if (content_type
!= NULL
) {
2059 g_strchomp(content_type
);
2060 procmime_parse_content_type(content_type
, mimeinfo
);
2062 mimeinfo
->type
= MIMETYPE_TEXT
;
2063 mimeinfo
->subtype
= g_strdup("plain");
2064 if (g_hash_table_lookup(mimeinfo
->typeparameters
,
2065 "charset") == NULL
) {
2066 g_hash_table_insert(mimeinfo
->typeparameters
,
2067 g_strdup("charset"),
2069 conv_get_locale_charset_str_no_utf8()));
2073 if (content_encoding
!= NULL
) {
2074 g_strchomp(content_encoding
);
2075 procmime_parse_content_encoding(content_encoding
, mimeinfo
);
2077 mimeinfo
->encoding_type
= ENC_UNKNOWN
;
2080 if (content_description
!= NULL
)
2081 mimeinfo
->description
= g_strdup(content_description
);
2083 mimeinfo
->description
= NULL
;
2085 if (content_id
!= NULL
)
2086 mimeinfo
->id
= g_strdup(content_id
);
2088 mimeinfo
->id
= NULL
;
2090 if (content_location
!= NULL
)
2091 mimeinfo
->location
= g_strdup(content_location
);
2093 mimeinfo
->location
= NULL
;
2095 if (content_disposition
!= NULL
) {
2096 g_strchomp(content_disposition
);
2097 procmime_parse_content_disposition(content_disposition
, mimeinfo
);
2099 mimeinfo
->disposition
= DISPOSITIONTYPE_UNKNOWN
;
2101 /* Call parser for mime type */
2102 if ((parser
= procmime_get_mimeparser_for_type(mimeinfo
->type
, mimeinfo
->subtype
)) != NULL
) {
2103 parsed
= procmime_mimeparser_parse(parser
, mimeinfo
);
2106 switch (mimeinfo
->type
) {
2108 if (g_ascii_strcasecmp(mimeinfo
->subtype
, "plain") == 0 && short_scan
) {
2113 case MIMETYPE_MESSAGE
:
2114 if (g_ascii_strcasecmp(mimeinfo
->subtype
, "rfc822") == 0) {
2115 procmime_parse_message_rfc822(mimeinfo
, short_scan
);
2117 if (g_ascii_strcasecmp(mimeinfo
->subtype
, "disposition-notification") == 0) {
2118 procmime_parse_disposition_notification(mimeinfo
,
2119 original_msgid
, disposition_notification_hdr
, short_scan
);
2123 case MIMETYPE_MULTIPART
:
2124 procmime_parse_multipart(mimeinfo
, short_scan
);
2127 case MIMETYPE_APPLICATION
:
2128 if (g_ascii_strcasecmp(mimeinfo
->subtype
, "octet-stream") == 0
2129 && original_msgid
&& *original_msgid
2130 && disposition_notification_hdr
&& *disposition_notification_hdr
) {
2131 procmime_parse_disposition_notification(mimeinfo
,
2132 original_msgid
, disposition_notification_hdr
, short_scan
);
2143 static gchar
*typenames
[] = {
2154 static gboolean
output_func(GNode
*node
, gpointer data
)
2157 MimeInfo
*mimeinfo
= (MimeInfo
*) node
->data
;
2159 depth
= g_node_depth(node
);
2160 for (i
= 0; i
< depth
; i
++)
2162 g_print("%s/%s (offset:%d length:%d encoding: %d)\n", typenames
[mimeinfo
->type
], mimeinfo
->subtype
, mimeinfo
->offset
, mimeinfo
->length
, mimeinfo
->encoding_type
);
2167 static void output_mime_structure(MimeInfo
*mimeinfo
, int indent
)
2169 g_node_traverse(mimeinfo
->node
, G_PRE_ORDER
, G_TRAVERSE_ALL
, -1, output_func
, NULL
);
2172 static MimeInfo
*procmime_scan_file_with_offset(const gchar
*filename
, int offset
, gboolean short_scan
)
2177 if (g_stat(filename
, &buf
) < 0) {
2178 FILE_OP_ERROR(filename
, "stat");
2182 mimeinfo
= procmime_mimeinfo_new();
2183 mimeinfo
->content
= MIMECONTENT_FILE
;
2184 mimeinfo
->encoding_type
= ENC_UNKNOWN
;
2185 mimeinfo
->type
= MIMETYPE_MESSAGE
;
2186 mimeinfo
->subtype
= g_strdup("rfc822");
2187 mimeinfo
->data
.filename
= g_strdup(filename
);
2188 mimeinfo
->offset
= offset
;
2189 mimeinfo
->length
= buf
.st_size
- offset
;
2191 procmime_parse_message_rfc822(mimeinfo
, short_scan
);
2192 if (debug_get_mode())
2193 output_mime_structure(mimeinfo
, 0);
2198 static MimeInfo
*procmime_scan_file_full(const gchar
*filename
, gboolean short_scan
)
2202 cm_return_val_if_fail(filename
!= NULL
, NULL
);
2204 mimeinfo
= procmime_scan_file_with_offset(filename
, 0, short_scan
);
2209 MimeInfo
*procmime_scan_file(const gchar
*filename
)
2211 return procmime_scan_file_full(filename
, FALSE
);
2214 static MimeInfo
*procmime_scan_file_short(const gchar
*filename
)
2216 return procmime_scan_file_full(filename
, TRUE
);
2219 static MimeInfo
*procmime_scan_queue_file_full(const gchar
*filename
, gboolean short_scan
)
2223 gchar buf
[BUFFSIZE
];
2226 cm_return_val_if_fail(filename
!= NULL
, NULL
);
2229 if ((fp
= procmime_fopen(filename
, "rb")) == NULL
)
2231 /* Skip queue header */
2232 while (SC_FGETS(buf
, sizeof(buf
), fp
) != NULL
) {
2234 if ((!strncmp(buf
, "X-Claws-End-Special-Headers: 1",
2235 strlen("X-Claws-End-Special-Headers:"))) ||
2236 (!strncmp(buf
, "X-Sylpheed-End-Special-Headers: 1",
2237 strlen("X-Sylpheed-End-Special-Headers:"))))
2240 if (buf
[0] == '\r' || buf
[0] == '\n') break;
2241 /* from other mailers */
2242 if (!strncmp(buf
, "Date: ", 6)
2243 || !strncmp(buf
, "To: ", 4)
2244 || !strncmp(buf
, "From: ", 6)
2245 || !strncmp(buf
, "Subject: ", 9)) {
2251 procmime_fclose(fp
);
2253 mimeinfo
= procmime_scan_file_with_offset(filename
, offset
, short_scan
);
2258 MimeInfo
*procmime_scan_queue_file(const gchar
*filename
)
2260 return procmime_scan_queue_file_full(filename
, FALSE
);
2263 static MimeInfo
*procmime_scan_queue_file_short(const gchar
*filename
)
2265 return procmime_scan_queue_file_full(filename
, TRUE
);
2270 ENC_AS_QUOTED_STRING
,
2275 typedef struct _ParametersData
{
2281 static void write_parameters(gpointer key
, gpointer value
, gpointer user_data
)
2284 gchar
*val
= value
, *valpos
, *tmp
;
2285 ParametersData
*pdata
= (ParametersData
*)user_data
;
2286 GString
*buf
= g_string_new("");
2289 EncodeAs encas
= ENC_AS_TOKEN
;
2291 for (valpos
= val
; *valpos
!= 0; valpos
++) {
2292 if (!IS_ASCII(*valpos
)) {
2293 encas
= ENC_AS_ENCWORD
;
2298 if (((*valpos
>= 0) && (*valpos
< 037)) || (*valpos
== 0177)) {
2299 encas
= ENC_AS_QUOTED_STRING
;
2303 /* tspecials + SPACE */
2321 encas
= ENC_AS_QUOTED_STRING
;
2328 g_string_append_printf(buf
, "%s=%s", param
, val
);
2331 case ENC_AS_QUOTED_STRING
:
2332 g_string_append_printf(buf
, "%s=\"%s\"", param
, val
);
2335 #if 0 /* we don't use that for now */
2336 case ENC_AS_EXTENDED
:
2337 if (!g_utf8_validate(val
, -1, NULL
))
2338 g_string_append_printf(buf
, "%s*=%s''", param
,
2339 conv_get_locale_charset_str());
2341 g_string_append_printf(buf
, "%s*=%s''", param
,
2343 for (valpos
= val
; *valpos
!= '\0'; valpos
++) {
2344 if (IS_ASCII(*valpos
) && isalnum(*valpos
)) {
2345 g_string_append_printf(buf
, "%c", *valpos
);
2347 gchar hexstr
[3] = "XX";
2348 get_hex_str(hexstr
, *valpos
);
2349 g_string_append_printf(buf
, "%%%s", hexstr
);
2354 case ENC_AS_EXTENDED
:
2355 debug_print("Unhandled ENC_AS_EXTENDED.\n");
2358 case ENC_AS_ENCWORD
:
2359 len
= MAX(strlen(val
)*6, 512);
2360 tmp
= g_malloc(len
+1);
2361 codeconv_set_strict(TRUE
);
2362 conv_encode_header_full(tmp
, len
, val
, pdata
->len
+ strlen(param
) + 4 , FALSE
,
2363 prefs_common
.outgoing_charset
);
2364 codeconv_set_strict(FALSE
);
2365 if (!tmp
|| !*tmp
) {
2366 codeconv_set_strict(TRUE
);
2367 conv_encode_header_full(tmp
, len
, val
, pdata
->len
+ strlen(param
) + 4 , FALSE
,
2368 conv_get_outgoing_charset_str());
2369 codeconv_set_strict(FALSE
);
2371 if (!tmp
|| !*tmp
) {
2372 codeconv_set_strict(TRUE
);
2373 conv_encode_header_full(tmp
, len
, val
, pdata
->len
+ strlen(param
) + 4 , FALSE
,
2375 codeconv_set_strict(FALSE
);
2377 if (!tmp
|| !*tmp
) {
2378 conv_encode_header_full(tmp
, len
, val
, pdata
->len
+ strlen(param
) + 4 , FALSE
,
2381 g_string_append_printf(buf
, "%s=\"%s\"", param
, tmp
);
2387 if (buf
->str
&& strlen(buf
->str
)) {
2388 tmp
= strstr(buf
->str
, "\n");
2390 len
= (tmp
- buf
->str
);
2392 len
= strlen(buf
->str
);
2393 if (pdata
->len
+ len
> 76) {
2394 if (fprintf(pdata
->fp
, ";\n %s", buf
->str
) < 0)
2395 pdata
->error
= TRUE
;
2396 pdata
->len
= strlen(buf
->str
) + 1;
2398 if (fprintf(pdata
->fp
, "; %s", buf
->str
) < 0)
2399 pdata
->error
= TRUE
;
2400 pdata
->len
+= strlen(buf
->str
) + 2;
2403 g_string_free(buf
, TRUE
);
2406 #define TRY(func) { \
2412 int procmime_write_mime_header(MimeInfo
*mimeinfo
, FILE *fp
)
2414 struct TypeTable
*type_table
;
2415 ParametersData
*pdata
= g_new0(ParametersData
, 1);
2416 debug_print("procmime_write_mime_header\n");
2419 pdata
->error
= FALSE
;
2420 for (type_table
= mime_type_table
; type_table
->str
!= NULL
; type_table
++)
2421 if (mimeinfo
->type
== type_table
->type
) {
2422 gchar
*buf
= g_strdup_printf(
2423 "Content-Type: %s/%s", type_table
->str
, mimeinfo
->subtype
);
2424 if (fprintf(fp
, "%s", buf
) < 0) {
2429 pdata
->len
= strlen(buf
);
2433 g_hash_table_foreach(mimeinfo
->typeparameters
, write_parameters
, pdata
);
2434 if (pdata
->error
== TRUE
) {
2440 TRY(fprintf(fp
, "\n") >= 0);
2442 if (mimeinfo
->encoding_type
!= ENC_UNKNOWN
)
2443 TRY(fprintf(fp
, "Content-Transfer-Encoding: %s\n", procmime_get_encoding_str(mimeinfo
->encoding_type
)) >= 0);
2445 if (mimeinfo
->description
!= NULL
)
2446 TRY(fprintf(fp
, "Content-Description: %s\n", mimeinfo
->description
) >= 0);
2448 if (mimeinfo
->id
!= NULL
)
2449 TRY(fprintf(fp
, "Content-ID: %s\n", mimeinfo
->id
) >= 0);
2451 if (mimeinfo
->location
!= NULL
)
2452 TRY(fprintf(fp
, "Content-Location: %s\n", mimeinfo
->location
) >= 0);
2454 if (mimeinfo
->disposition
!= DISPOSITIONTYPE_UNKNOWN
) {
2455 ParametersData
*pdata
= g_new0(ParametersData
, 1);
2457 if (mimeinfo
->disposition
== DISPOSITIONTYPE_INLINE
)
2458 buf
= g_strdup("Content-Disposition: inline");
2459 else if (mimeinfo
->disposition
== DISPOSITIONTYPE_ATTACHMENT
)
2460 buf
= g_strdup("Content-Disposition: attachment");
2462 buf
= g_strdup("Content-Disposition: unknown");
2464 if (fprintf(fp
, "%s", buf
) < 0) {
2469 pdata
->len
= strlen(buf
);
2473 pdata
->error
= FALSE
;
2474 g_hash_table_foreach(mimeinfo
->dispositionparameters
, write_parameters
, pdata
);
2475 if (pdata
->error
== TRUE
) {
2480 TRY(fprintf(fp
, "\n") >= 0);
2483 TRY(fprintf(fp
, "\n") >= 0);
2488 static gint
procmime_write_message_rfc822(MimeInfo
*mimeinfo
, FILE *fp
)
2493 gchar buf
[BUFFSIZE
];
2494 gboolean skip
= FALSE
;;
2497 debug_print("procmime_write_message_rfc822\n");
2500 switch (mimeinfo
->content
) {
2501 case MIMECONTENT_FILE
:
2502 if ((infp
= procmime_fopen(mimeinfo
->data
.filename
, "rb")) == NULL
) {
2503 FILE_OP_ERROR(mimeinfo
->data
.filename
, "fopen");
2506 if (fseek(infp
, mimeinfo
->offset
, SEEK_SET
) < 0) {
2507 FILE_OP_ERROR(mimeinfo
->data
.filename
, "fseek");
2508 procmime_fclose(infp
);
2511 while (SC_FGETS(buf
, sizeof(buf
), infp
) == buf
) {
2513 if (buf
[0] == '\n' && buf
[1] == '\0')
2515 if (skip
&& (buf
[0] == ' ' || buf
[0] == '\t'))
2517 if (g_ascii_strncasecmp(buf
, "MIME-Version:", 13) == 0 ||
2518 g_ascii_strncasecmp(buf
, "Content-Type:", 13) == 0 ||
2519 g_ascii_strncasecmp(buf
, "Content-Transfer-Encoding:", 26) == 0 ||
2520 g_ascii_strncasecmp(buf
, "Content-Description:", 20) == 0 ||
2521 g_ascii_strncasecmp(buf
, "Content-ID:", 11) == 0 ||
2522 g_ascii_strncasecmp(buf
, "Content-Location:", 17) == 0 ||
2523 g_ascii_strncasecmp(buf
, "Content-Disposition:", 20) == 0) {
2528 if (SC_FWRITE(buf
, sizeof(gchar
), len
, fp
) < len
) {
2529 g_warning("failed to dump %zd bytes from file", len
);
2530 procmime_fclose(infp
);
2535 procmime_fclose(infp
);
2538 case MIMECONTENT_MEM
:
2539 len
= strlen(mimeinfo
->data
.mem
);
2540 if (SC_FWRITE(mimeinfo
->data
.mem
, sizeof(gchar
), len
, fp
) < len
) {
2541 g_warning("failed to dump %zd bytes from mem", len
);
2550 childnode
= mimeinfo
->node
->children
;
2551 if (childnode
== NULL
)
2554 child
= (MimeInfo
*) childnode
->data
;
2555 if (fprintf(fp
, "MIME-Version: 1.0\n") < 0) {
2556 g_warning("failed to write mime version");
2559 if (procmime_write_mime_header(child
, fp
) < 0)
2561 return procmime_write_mimeinfo(child
, fp
);
2564 static gint
procmime_write_multipart(MimeInfo
*mimeinfo
, FILE *fp
)
2568 gchar
*boundary
, *str
, *str2
;
2569 gchar buf
[BUFFSIZE
];
2570 gboolean firstboundary
;
2573 debug_print("procmime_write_multipart\n");
2575 boundary
= g_hash_table_lookup(mimeinfo
->typeparameters
, "boundary");
2577 switch (mimeinfo
->content
) {
2578 case MIMECONTENT_FILE
:
2579 if ((infp
= procmime_fopen(mimeinfo
->data
.filename
, "rb")) == NULL
) {
2580 FILE_OP_ERROR(mimeinfo
->data
.filename
, "fopen");
2583 if (fseek(infp
, mimeinfo
->offset
, SEEK_SET
) < 0) {
2584 FILE_OP_ERROR(mimeinfo
->data
.filename
, "fseek");
2585 procmime_fclose(infp
);
2588 while (SC_FGETS(buf
, sizeof(buf
), infp
) == buf
) {
2589 if (IS_BOUNDARY(buf
, boundary
, strlen(boundary
)))
2592 if (SC_FWRITE(buf
, sizeof(gchar
), len
, fp
) < len
) {
2593 g_warning("failed to write %zd", len
);
2594 procmime_fclose(infp
);
2598 procmime_fclose(infp
);
2601 case MIMECONTENT_MEM
:
2602 str
= g_strdup(mimeinfo
->data
.mem
);
2603 if (((str2
= strstr(str
, boundary
)) != NULL
) && ((str2
- str
) >= 2) &&
2604 (*(str2
- 1) == '-') && (*(str2
- 2) == '-'))
2607 if (SC_FWRITE(str
, sizeof(gchar
), len
, fp
) < len
) {
2608 g_warning("failed to write %zd from mem", len
);
2619 childnode
= mimeinfo
->node
->children
;
2620 firstboundary
= TRUE
;
2621 while (childnode
!= NULL
) {
2622 MimeInfo
*child
= childnode
->data
;
2625 firstboundary
= FALSE
;
2627 TRY(fprintf(fp
, "\n") >= 0);
2629 TRY(fprintf(fp
, "--%s\n", boundary
) >= 0);
2631 if (procmime_write_mime_header(child
, fp
) < 0)
2633 if (procmime_write_mimeinfo(child
, fp
) < 0)
2636 childnode
= g_node_next_sibling(childnode
);
2638 TRY(fprintf(fp
, "\n--%s--\n", boundary
) >= 0);
2643 gint
procmime_write_mimeinfo(MimeInfo
*mimeinfo
, FILE *fp
)
2647 debug_print("procmime_write_mimeinfo\n");
2649 if (G_NODE_IS_LEAF(mimeinfo
->node
)) {
2650 switch (mimeinfo
->content
) {
2651 case MIMECONTENT_FILE
:
2652 if ((infp
= procmime_fopen(mimeinfo
->data
.filename
, "rb")) == NULL
) {
2653 FILE_OP_ERROR(mimeinfo
->data
.filename
, "fopen");
2656 copy_file_part_to_fp(infp
, mimeinfo
->offset
, mimeinfo
->length
, fp
);
2657 procmime_fclose(infp
);
2660 case MIMECONTENT_MEM
:
2661 len
= strlen(mimeinfo
->data
.mem
);
2662 if (SC_FWRITE(mimeinfo
->data
.mem
, sizeof(gchar
), len
, fp
) < len
)
2670 /* Call writer for mime type */
2671 switch (mimeinfo
->type
) {
2672 case MIMETYPE_MESSAGE
:
2673 if (g_ascii_strcasecmp(mimeinfo
->subtype
, "rfc822") == 0) {
2674 return procmime_write_message_rfc822(mimeinfo
, fp
);
2678 case MIMETYPE_MULTIPART
:
2679 return procmime_write_multipart(mimeinfo
, fp
);
2691 gchar
*procmime_get_part_file_name(MimeInfo
*mimeinfo
)
2695 if ((mimeinfo
->type
== MIMETYPE_TEXT
) && !g_ascii_strcasecmp(mimeinfo
->subtype
, "html"))
2696 base
= g_strdup("mimetmp.html");
2698 const gchar
*basetmp
;
2701 basetmp
= procmime_mimeinfo_get_parameter(mimeinfo
, "filename");
2702 if (basetmp
== NULL
)
2703 basetmp
= procmime_mimeinfo_get_parameter(mimeinfo
, "name");
2704 if (basetmp
== NULL
)
2705 basetmp
= "mimetmp";
2706 basename
= g_path_get_basename(basetmp
);
2707 if (*basename
== '\0') {
2709 basename
= g_strdup("mimetmp");
2711 base
= conv_filename_from_utf8(basename
);
2713 subst_for_shellsafe_filename(base
);