2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2012 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/>.
22 #include "claws-features.h"
31 #include <glib/gi18n.h>
37 #include <sys/types.h>
43 #include "procheader.h"
45 #include "quoted-printable.h"
52 #include "prefs_common.h"
53 #include "prefs_gtk.h"
54 #include "alertpanel.h"
58 static GHashTable
*procmime_get_mime_type_table (void);
59 static MimeInfo
*procmime_scan_file_short(const gchar
*filename
);
60 static MimeInfo
*procmime_scan_queue_file_short(const gchar
*filename
);
61 static MimeInfo
*procmime_scan_queue_file_full(const gchar
*filename
, gboolean short_scan
);
63 MimeInfo
*procmime_mimeinfo_new(void)
67 mimeinfo
= g_new0(MimeInfo
, 1);
68 mimeinfo
->content
= MIMECONTENT_EMPTY
;
69 mimeinfo
->data
.filename
= NULL
;
71 mimeinfo
->type
= MIMETYPE_UNKNOWN
;
72 mimeinfo
->encoding_type
= ENC_UNKNOWN
;
73 mimeinfo
->typeparameters
= g_hash_table_new(g_str_hash
, g_str_equal
);
75 mimeinfo
->disposition
= DISPOSITIONTYPE_UNKNOWN
;
76 mimeinfo
->dispositionparameters
77 = g_hash_table_new(g_str_hash
, g_str_equal
);
79 mimeinfo
->node
= g_node_new(mimeinfo
);
84 static gboolean
procmime_mimeinfo_parameters_destroy(gpointer key
, gpointer value
, gpointer user_data
)
92 static gchar
*forced_charset
= NULL
;
94 void procmime_force_charset(const gchar
*str
)
96 g_free(forced_charset
);
97 forced_charset
= NULL
;
99 forced_charset
= g_strdup(str
);
102 static EncodingType forced_encoding
= 0;
104 void procmime_force_encoding(EncodingType encoding
)
106 forced_encoding
= encoding
;
109 static gboolean
free_func(GNode
*node
, gpointer data
)
111 MimeInfo
*mimeinfo
= (MimeInfo
*) node
->data
;
113 switch (mimeinfo
->content
) {
114 case MIMECONTENT_FILE
:
116 claws_unlink(mimeinfo
->data
.filename
);
117 g_free(mimeinfo
->data
.filename
);
120 case MIMECONTENT_MEM
:
122 g_free(mimeinfo
->data
.mem
);
127 g_free(mimeinfo
->subtype
);
128 g_free(mimeinfo
->description
);
129 g_free(mimeinfo
->id
);
130 g_free(mimeinfo
->location
);
132 g_hash_table_foreach_remove(mimeinfo
->typeparameters
,
133 procmime_mimeinfo_parameters_destroy
, NULL
);
134 g_hash_table_destroy(mimeinfo
->typeparameters
);
135 g_hash_table_foreach_remove(mimeinfo
->dispositionparameters
,
136 procmime_mimeinfo_parameters_destroy
, NULL
);
137 g_hash_table_destroy(mimeinfo
->dispositionparameters
);
139 if (mimeinfo
->privacy
)
140 privacy_free_privacydata(mimeinfo
->privacy
);
147 void procmime_mimeinfo_free_all(MimeInfo
*mimeinfo
)
154 node
= mimeinfo
->node
;
155 g_node_traverse(node
, G_IN_ORDER
, G_TRAVERSE_ALL
, -1, free_func
, NULL
);
157 g_node_destroy(node
);
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
;
324 EncodingType encoding
= forced_encoding
326 : mimeinfo
->encoding_type
;
327 gchar lastline
[BUFFSIZE
];
328 memset(lastline
, 0, BUFFSIZE
);
330 cm_return_val_if_fail(mimeinfo
!= NULL
, FALSE
);
332 if (prefs_common
.respect_flowed_format
&&
333 mimeinfo
->type
== MIMETYPE_TEXT
&&
334 !strcasecmp(mimeinfo
->subtype
, "plain")) {
335 if (procmime_mimeinfo_get_parameter(mimeinfo
, "format") != NULL
&&
336 !strcasecmp(procmime_mimeinfo_get_parameter(mimeinfo
, "format"),"flowed"))
339 procmime_mimeinfo_get_parameter(mimeinfo
, "delsp") != NULL
&&
340 !strcasecmp(procmime_mimeinfo_get_parameter(mimeinfo
, "delsp"),"yes"))
345 encoding
== ENC_UNKNOWN
||
346 encoding
== ENC_BINARY
||
347 encoding
== ENC_7BIT
||
352 if (mimeinfo
->type
== MIMETYPE_MULTIPART
|| mimeinfo
->type
== MIMETYPE_MESSAGE
)
355 if (mimeinfo
->data
.filename
== NULL
)
358 infp
= procmime_fopen(mimeinfo
->data
.filename
, "rb");
363 if (fseek(infp
, mimeinfo
->offset
, SEEK_SET
) < 0) {
365 procmime_fclose(infp
);
369 outfp
= get_tmpfile_in_dir(get_mime_tmp_dir(), &tmpfilename
);
372 procmime_fclose(infp
);
376 readend
= mimeinfo
->offset
+ mimeinfo
->length
;
378 if (encoding
== ENC_QUOTED_PRINTABLE
) {
379 while ((ftell(infp
) < readend
) && (SC_FGETS(buf
, sizeof(buf
), infp
) != NULL
)) {
381 len
= qp_decode_line(buf
);
384 if (SC_FWRITE(buf
, 1, len
, outfp
) < len
)
392 } else if (encoding
== ENC_BASE64
) {
393 gchar outbuf
[BUFFSIZE
];
394 gint len
, inlen
, inread
;
395 Base64Decoder
*decoder
;
396 gboolean got_error
= FALSE
;
397 gboolean uncanonicalize
= FALSE
;
399 gboolean null_bytes
= FALSE
;
400 gboolean starting
= TRUE
;
402 if (mimeinfo
->type
== MIMETYPE_TEXT
||
403 mimeinfo
->type
== MIMETYPE_MESSAGE
) {
404 uncanonicalize
= TRUE
;
405 tmpfp
= my_tmpfile();
409 procmime_fclose(outfp
);
410 procmime_fclose(infp
);
413 #ifdef HAVE_FGETS_UNLOCKED
419 decoder
= base64_decoder_new();
420 while ((inlen
= MIN(readend
- ftell(infp
), sizeof(buf
))) > 0 && !err
) {
421 inread
= SC_FREAD(buf
, 1, inlen
, infp
);
422 len
= base64_decoder_decode(decoder
, buf
, outbuf
, inread
);
423 if (uncanonicalize
== TRUE
&& strlen(outbuf
) < len
&& starting
) {
424 uncanonicalize
= FALSE
;
428 if (((inread
!= inlen
) || len
< 0) && !got_error
) {
429 g_warning("Bad BASE64 content.\n");
430 if (SC_FWRITE(_("[Error decoding BASE64]\n"),
432 strlen(_("[Error decoding BASE64]\n")),
433 tmpfp
) < strlen(_("[Error decoding BASE64]\n")))
434 g_warning("error decoding BASE64");
437 } else if (len
>= 0) {
438 /* print out the error message only once
441 /* we won't uncanonicalize, output to outfp directly */
442 if (SC_FWRITE(outbuf
, sizeof(gchar
), len
, outfp
) < len
)
445 if (SC_FWRITE(outbuf
, sizeof(gchar
), len
, tmpfp
) < len
)
451 base64_decoder_free(decoder
);
453 if (uncanonicalize
) {
455 while (SC_FGETS(buf
, sizeof(buf
), tmpfp
) != NULL
) {
457 if (SC_FPUTS(buf
, outfp
) == EOF
)
460 procmime_fclose(tmpfp
);
462 } else if (encoding
== ENC_X_UUENCODE
) {
463 gchar outbuf
[BUFFSIZE
];
465 gboolean flag
= FALSE
;
467 while ((ftell(infp
) < readend
) && (SC_FGETS(buf
, sizeof(buf
), infp
) != NULL
)) {
468 if (!flag
&& strncmp(buf
,"begin ", 6)) continue;
471 len
= fromuutobits(outbuf
, buf
);
474 g_warning("Bad UUENCODE content(%d)\n", len
);
477 if (SC_FWRITE(outbuf
, sizeof(gchar
), len
, outfp
) < len
)
483 while ((ftell(infp
) < readend
) && (SC_FGETS(buf
, sizeof(buf
), infp
) != NULL
)) {
485 if (SC_FPUTS(buf
, outfp
) == EOF
)
494 g_warning("write error");
497 procmime_fclose(outfp
);
498 procmime_fclose(infp
);
504 if (g_stat(tmpfilename
, &statbuf
) < 0) {
505 FILE_OP_ERROR(tmpfilename
, "stat");
510 claws_unlink(mimeinfo
->data
.filename
);
511 g_free(mimeinfo
->data
.filename
);
512 mimeinfo
->data
.filename
= tmpfilename
;
513 mimeinfo
->tmp
= TRUE
;
514 mimeinfo
->offset
= 0;
515 mimeinfo
->length
= statbuf
.st_size
;
516 mimeinfo
->encoding_type
= ENC_BINARY
;
521 #define B64_LINE_SIZE 57
522 #define B64_BUFFSIZE 77
524 gboolean
procmime_encode_content(MimeInfo
*mimeinfo
, EncodingType encoding
)
526 FILE *infp
= NULL
, *outfp
;
530 gboolean err
= FALSE
;
532 if (mimeinfo
->content
== MIMECONTENT_EMPTY
)
535 if (mimeinfo
->encoding_type
!= ENC_UNKNOWN
&&
536 mimeinfo
->encoding_type
!= ENC_BINARY
&&
537 mimeinfo
->encoding_type
!= ENC_7BIT
&&
538 mimeinfo
->encoding_type
!= ENC_8BIT
)
539 if(!procmime_decode_content(mimeinfo
))
542 outfp
= get_tmpfile_in_dir(get_mime_tmp_dir(), &tmpfilename
);
547 #ifdef HAVE_FGETS_UNLOCKED
551 if (mimeinfo
->content
== MIMECONTENT_FILE
&& mimeinfo
->data
.filename
) {
552 if ((infp
= procmime_fopen(mimeinfo
->data
.filename
, "rb")) == NULL
) {
553 g_warning("Can't open file %s\n", mimeinfo
->data
.filename
);
554 procmime_fclose(outfp
);
557 } else if (mimeinfo
->content
== MIMECONTENT_MEM
) {
558 infp
= str_open_as_stream(mimeinfo
->data
.mem
);
560 procmime_fclose(outfp
);
563 #ifdef HAVE_FGETS_UNLOCKED
567 procmime_fclose(outfp
);
568 g_warning("Unknown mimeinfo");
572 if (encoding
== ENC_BASE64
) {
573 gchar inbuf
[B64_LINE_SIZE
], outbuf
[B64_BUFFSIZE
];
575 gchar
*tmp_file
= NULL
;
577 if (mimeinfo
->type
== MIMETYPE_TEXT
||
578 mimeinfo
->type
== MIMETYPE_MESSAGE
) {
579 if (mimeinfo
->content
== MIMECONTENT_FILE
) {
580 tmp_file
= get_tmp_file();
581 if (canonicalize_file(mimeinfo
->data
.filename
, tmp_file
) < 0) {
583 procmime_fclose(infp
);
584 procmime_fclose(outfp
);
587 if ((tmp_fp
= procmime_fopen(tmp_file
, "rb")) == NULL
) {
588 FILE_OP_ERROR(tmp_file
, "fopen");
589 claws_unlink(tmp_file
);
591 procmime_fclose(infp
);
592 procmime_fclose(outfp
);
596 gchar
*out
= canonicalize_str(mimeinfo
->data
.mem
);
597 procmime_fclose(infp
);
598 infp
= str_open_as_stream(out
);
602 procmime_fclose(outfp
);
605 #ifdef HAVE_FGETS_UNLOCKED
611 while ((len
= SC_FREAD(inbuf
, sizeof(gchar
),
612 B64_LINE_SIZE
, tmp_fp
))
614 base64_encode(outbuf
, inbuf
, B64_LINE_SIZE
);
615 if (SC_FPUTS(outbuf
, outfp
) == EOF
)
617 if (SC_FPUTC('\n', outfp
) == EOF
)
620 if (len
> 0 && SC_FEOF(tmp_fp
)) {
621 base64_encode(outbuf
, inbuf
, len
);
622 if (SC_FPUTS(outbuf
, outfp
) == EOF
)
624 if (SC_FPUTC('\n', outfp
) == EOF
)
629 procmime_fclose(tmp_fp
);
630 claws_unlink(tmp_file
);
633 } else if (encoding
== ENC_QUOTED_PRINTABLE
) {
634 gchar inbuf
[BUFFSIZE
], outbuf
[BUFFSIZE
* 4];
636 while (SC_FGETS(inbuf
, sizeof(inbuf
), infp
) != NULL
) {
637 qp_encode_line(outbuf
, inbuf
);
639 if (!strncmp("From ", outbuf
, sizeof("From ")-1)) {
640 gchar
*tmpbuf
= outbuf
;
642 tmpbuf
+= sizeof("From ")-1;
644 if (SC_FPUTS("=46rom ", outfp
) == EOF
)
646 if (SC_FPUTS(tmpbuf
, outfp
) == EOF
)
649 if (SC_FPUTS(outbuf
, outfp
) == EOF
)
656 while (SC_FGETS(buf
, sizeof(buf
), infp
) != NULL
) {
658 if (SC_FPUTS(buf
, outfp
) == EOF
)
663 procmime_fclose(outfp
);
664 procmime_fclose(infp
);
669 if (mimeinfo
->content
== MIMECONTENT_FILE
) {
670 if (mimeinfo
->tmp
&& (mimeinfo
->data
.filename
!= NULL
))
671 claws_unlink(mimeinfo
->data
.filename
);
672 g_free(mimeinfo
->data
.filename
);
673 } else if (mimeinfo
->content
== MIMECONTENT_MEM
) {
674 if (mimeinfo
->tmp
&& (mimeinfo
->data
.mem
!= NULL
))
675 g_free(mimeinfo
->data
.mem
);
678 if (g_stat(tmpfilename
, &statbuf
) < 0) {
679 FILE_OP_ERROR(tmpfilename
, "stat");
682 mimeinfo
->content
= MIMECONTENT_FILE
;
683 mimeinfo
->data
.filename
= tmpfilename
;
684 mimeinfo
->tmp
= TRUE
;
685 mimeinfo
->offset
= 0;
686 mimeinfo
->length
= statbuf
.st_size
;
687 mimeinfo
->encoding_type
= encoding
;
692 gint
procmime_get_part(const gchar
*outfile
, MimeInfo
*mimeinfo
)
696 gint restlength
, readlength
;
697 gint saved_errno
= 0;
699 cm_return_val_if_fail(outfile
!= NULL
, -1);
700 cm_return_val_if_fail(mimeinfo
!= NULL
, -1);
702 if (mimeinfo
->encoding_type
!= ENC_BINARY
&& !procmime_decode_content(mimeinfo
))
705 if ((infp
= procmime_fopen(mimeinfo
->data
.filename
, "rb")) == NULL
) {
707 FILE_OP_ERROR(mimeinfo
->data
.filename
, "fopen");
708 return -(saved_errno
);
710 if (fseek(infp
, mimeinfo
->offset
, SEEK_SET
) < 0) {
712 FILE_OP_ERROR(mimeinfo
->data
.filename
, "fseek");
713 procmime_fclose(infp
);
714 return -(saved_errno
);
716 if ((outfp
= procmime_fopen(outfile
, "wb")) == NULL
) {
718 FILE_OP_ERROR(outfile
, "fopen");
719 procmime_fclose(infp
);
720 return -(saved_errno
);
723 restlength
= mimeinfo
->length
;
725 while ((restlength
> 0) && ((readlength
= SC_FREAD(buf
, 1, restlength
> BUFFSIZE
? BUFFSIZE
: restlength
, infp
)) > 0)) {
726 if (SC_FWRITE(buf
, 1, readlength
, outfp
) != readlength
) {
728 procmime_fclose(infp
);
729 procmime_fclose(outfp
);
730 return -(saved_errno
);
732 restlength
-= readlength
;
735 procmime_fclose(infp
);
736 if (procmime_fclose(outfp
) == EOF
) {
738 FILE_OP_ERROR(outfile
, "fclose");
739 claws_unlink(outfile
);
740 return -(saved_errno
);
746 gboolean
procmime_scan_text_content(MimeInfo
*mimeinfo
,
747 gboolean (*scan_callback
)(const gchar
*str
, gpointer cb_data
),
751 const gchar
*src_codeset
;
752 gboolean conv_fail
= FALSE
;
756 gboolean scan_ret
= FALSE
;
758 cm_return_val_if_fail(mimeinfo
!= NULL
, TRUE
);
759 cm_return_val_if_fail(scan_callback
!= NULL
, TRUE
);
761 if (!procmime_decode_content(mimeinfo
))
764 tmpfile
= procmime_get_tmp_file_name(mimeinfo
);
768 if (procmime_get_part(tmpfile
, mimeinfo
) < 0) {
773 tmpfp
= procmime_fopen(tmpfile
, "rb");
779 src_codeset
= forced_charset
781 procmime_mimeinfo_get_parameter(mimeinfo
, "charset");
783 /* use supersets transparently when possible */
784 if (!forced_charset
&& src_codeset
&& !strcasecmp(src_codeset
, CS_ISO_8859_1
))
785 src_codeset
= CS_WINDOWS_1252
;
786 else if (!forced_charset
&& src_codeset
&& !strcasecmp(src_codeset
, CS_X_GBK
))
787 src_codeset
= CS_GB18030
;
788 else if (!forced_charset
&& src_codeset
&& !strcasecmp(src_codeset
, CS_GBK
))
789 src_codeset
= CS_GB18030
;
790 else if (!forced_charset
&& src_codeset
&& !strcasecmp(src_codeset
, CS_GB2312
))
791 src_codeset
= CS_GB18030
;
792 else if (!forced_charset
&& src_codeset
&& !strcasecmp(src_codeset
, CS_X_VIET_VPS
))
793 src_codeset
= CS_WINDOWS_874
;
795 if (mimeinfo
->type
== MIMETYPE_TEXT
&& !g_ascii_strcasecmp(mimeinfo
->subtype
, "html")) {
796 SC_HTMLParser
*parser
;
799 conv
= conv_code_converter_new(src_codeset
);
800 parser
= sc_html_parser_new(tmpfp
, conv
);
801 while ((str
= sc_html_parse(parser
)) != NULL
) {
802 if ((scan_ret
= scan_callback(str
, cb_data
)) == TRUE
)
805 sc_html_parser_destroy(parser
);
806 conv_code_converter_destroy(conv
);
807 } else if (mimeinfo
->type
== MIMETYPE_TEXT
&& !g_ascii_strcasecmp(mimeinfo
->subtype
, "enriched")) {
811 conv
= conv_code_converter_new(src_codeset
);
812 parser
= ertf_parser_new(tmpfp
, conv
);
813 while ((str
= ertf_parse(parser
)) != NULL
) {
814 if ((scan_ret
= scan_callback(str
, cb_data
)) == TRUE
)
817 ertf_parser_destroy(parser
);
818 conv_code_converter_destroy(conv
);
819 } else if (mimeinfo
->type
== MIMETYPE_TEXT
) {
820 while (SC_FGETS(buf
, sizeof(buf
), tmpfp
) != NULL
) {
821 str
= conv_codeset_strdup(buf
, src_codeset
, CS_UTF_8
);
823 if ((scan_ret
= scan_callback(str
, cb_data
)) == TRUE
) {
830 if ((scan_ret
= scan_callback(buf
, cb_data
)) == TRUE
)
837 g_warning("procmime_get_text_content(): Code conversion failed.\n");
839 procmime_fclose(tmpfp
);
840 claws_unlink(tmpfile
);
846 static gboolean
scan_fputs_cb(const gchar
*str
, gpointer fp
)
848 if (SC_FPUTS(str
, (FILE *)fp
) == EOF
)
854 FILE *procmime_get_text_content(MimeInfo
*mimeinfo
)
859 if ((outfp
= my_tmpfile()) == NULL
) {
863 #ifdef HAVE_FGETS_UNLOCKED
867 err
= procmime_scan_text_content(mimeinfo
, scan_fputs_cb
, outfp
);
871 procmime_fclose(outfp
);
874 #ifdef HAVE_FGETS_UNLOCKED
881 FILE *procmime_get_binary_content(MimeInfo
*mimeinfo
)
886 cm_return_val_if_fail(mimeinfo
!= NULL
, NULL
);
888 if (!procmime_decode_content(mimeinfo
))
891 tmpfile
= procmime_get_tmp_file_name(mimeinfo
);
895 if (procmime_get_part(tmpfile
, mimeinfo
) < 0) {
900 outfp
= procmime_fopen(tmpfile
, "rb");
910 #ifdef HAVE_FGETS_UNLOCKED
916 /* search the first text part of (multipart) MIME message,
917 decode, convert it and output to outfp. */
918 FILE *procmime_get_first_text_content(MsgInfo
*msginfo
)
921 MimeInfo
*mimeinfo
, *partinfo
;
922 gboolean empty_ok
= FALSE
, short_scan
= TRUE
;
924 cm_return_val_if_fail(msginfo
!= NULL
, NULL
);
926 /* first we try to short-scan (for speed), refusing empty parts */
929 mimeinfo
= procmime_scan_message_short(msginfo
);
931 mimeinfo
= procmime_scan_message(msginfo
);
932 if (!mimeinfo
) return NULL
;
935 while (partinfo
&& (partinfo
->type
!= MIMETYPE_TEXT
||
936 (partinfo
->length
== 0 && !empty_ok
))) {
937 partinfo
= procmime_mimeinfo_next(partinfo
);
940 outfp
= procmime_get_text_content(partinfo
);
941 else if (!empty_ok
&& short_scan
) {
942 /* if short scan didn't find a non-empty part, rescan
943 * fully for non-empty parts
946 procmime_mimeinfo_free_all(mimeinfo
);
948 } else if (!empty_ok
&& !short_scan
) {
949 /* if full scan didn't find a non-empty part, rescan
950 * accepting empty parts
953 procmime_mimeinfo_free_all(mimeinfo
);
956 procmime_mimeinfo_free_all(mimeinfo
);
958 /* outfp already unlocked at this time */
963 static gboolean
find_encrypted_func(GNode
*node
, gpointer data
)
965 MimeInfo
*mimeinfo
= (MimeInfo
*) node
->data
;
966 MimeInfo
**encinfo
= (MimeInfo
**) data
;
968 if (privacy_mimeinfo_is_encrypted(mimeinfo
)) {
976 static MimeInfo
*find_encrypted_part(MimeInfo
*rootinfo
)
978 MimeInfo
*encinfo
= NULL
;
980 g_node_traverse(rootinfo
->node
, G_IN_ORDER
, G_TRAVERSE_ALL
, -1,
981 find_encrypted_func
, &encinfo
);
986 /* search the first encrypted text part of (multipart) MIME message,
987 decode, convert it and output to outfp. */
988 FILE *procmime_get_first_encrypted_text_content(MsgInfo
*msginfo
)
991 MimeInfo
*mimeinfo
, *partinfo
, *encinfo
;
993 cm_return_val_if_fail(msginfo
!= NULL
, NULL
);
995 mimeinfo
= procmime_scan_message(msginfo
);
1000 partinfo
= mimeinfo
;
1001 if ((encinfo
= find_encrypted_part(partinfo
)) != NULL
) {
1002 debug_print("decrypting message part\n");
1003 if (privacy_mimeinfo_decrypt(encinfo
) < 0) {
1004 alertpanel_error(_("Couldn't decrypt: %s"),
1005 privacy_get_error());
1009 partinfo
= mimeinfo
;
1010 while (partinfo
&& partinfo
->type
!= MIMETYPE_TEXT
) {
1011 partinfo
= procmime_mimeinfo_next(partinfo
);
1012 if (privacy_mimeinfo_is_signed(partinfo
))
1013 procmsg_msginfo_set_flags(msginfo
, 0, MSG_SIGNED
);
1017 outfp
= procmime_get_text_content(partinfo
);
1019 procmime_mimeinfo_free_all(mimeinfo
);
1021 /* outfp already unlocked at this time */
1025 gboolean
procmime_msginfo_is_encrypted(MsgInfo
*msginfo
)
1027 MimeInfo
*mimeinfo
, *partinfo
;
1028 gboolean result
= FALSE
;
1030 cm_return_val_if_fail(msginfo
!= NULL
, FALSE
);
1032 mimeinfo
= procmime_scan_message(msginfo
);
1037 partinfo
= mimeinfo
;
1038 result
= (find_encrypted_part(partinfo
) != NULL
);
1039 procmime_mimeinfo_free_all(mimeinfo
);
1044 gchar
*procmime_get_tmp_file_name(MimeInfo
*mimeinfo
)
1046 static guint32 id
= 0;
1051 cm_return_val_if_fail(mimeinfo
!= NULL
, NULL
);
1053 g_snprintf(f_prefix
, sizeof(f_prefix
), "%08x.", id
++);
1055 if ((mimeinfo
->type
== MIMETYPE_TEXT
) && !g_ascii_strcasecmp(mimeinfo
->subtype
, "html"))
1056 base
= g_strdup("mimetmp.html");
1058 const gchar
*basetmp
;
1060 basetmp
= procmime_mimeinfo_get_parameter(mimeinfo
, "filename");
1061 if (basetmp
== NULL
)
1062 basetmp
= procmime_mimeinfo_get_parameter(mimeinfo
, "name");
1063 if (basetmp
== NULL
)
1064 basetmp
= "mimetmp";
1065 basetmp
= g_path_get_basename(basetmp
);
1066 if (*basetmp
== '\0')
1067 basetmp
= g_strdup("mimetmp");
1068 base
= conv_filename_from_utf8(basetmp
);
1069 g_free((gchar
*)basetmp
);
1070 subst_for_shellsafe_filename(base
);
1073 filename
= g_strconcat(get_mime_tmp_dir(), G_DIR_SEPARATOR_S
,
1074 f_prefix
, base
, NULL
);
1081 static GList
*mime_type_list
= NULL
;
1083 gchar
*procmime_get_mime_type(const gchar
*filename
)
1089 static GHashTable
*mime_type_table
= NULL
;
1090 MimeType
*mime_type
;
1092 if (!mime_type_table
) {
1093 mime_type_table
= procmime_get_mime_type_table();
1094 if (!mime_type_table
) return NULL
;
1098 if (filename
== NULL
)
1101 base
= g_path_get_basename(filename
);
1102 if ((p
= strrchr(base
, '.')) != NULL
)
1103 ext
= g_utf8_strdown(p
+ 1, -1);
1107 mime_type
= g_hash_table_lookup(mime_type_table
, ext
);
1111 str
= g_strconcat(mime_type
->type
, "/", mime_type
->sub_type
,
1113 debug_print("got type %s for %s\n", str
, ext
);
1119 gchar
*str
= get_content_type_from_registry_with_ext(ext
);
1126 static guint
procmime_str_hash(gconstpointer gptr
)
1128 guint hash_result
= 0;
1131 for (str
= gptr
; str
&& *str
; str
++) {
1132 if (isupper(*str
)) hash_result
+= (*str
+ ' ');
1133 else hash_result
+= *str
;
1139 static gint
procmime_str_equal(gconstpointer gptr1
, gconstpointer gptr2
)
1141 const char *str1
= gptr1
;
1142 const char *str2
= gptr2
;
1144 return !g_utf8_collate(str1
, str2
);
1147 static GHashTable
*procmime_get_mime_type_table(void)
1149 GHashTable
*table
= NULL
;
1151 MimeType
*mime_type
;
1154 if (!mime_type_list
) {
1155 mime_type_list
= procmime_get_mime_type_list();
1156 if (!mime_type_list
) return NULL
;
1159 table
= g_hash_table_new(procmime_str_hash
, procmime_str_equal
);
1161 for (cur
= mime_type_list
; cur
!= NULL
; cur
= cur
->next
) {
1165 mime_type
= (MimeType
*)cur
->data
;
1167 if (!mime_type
->extension
) continue;
1169 exts
= g_strsplit(mime_type
->extension
, " ", 16);
1170 for (i
= 0; exts
[i
] != NULL
; i
++) {
1171 /* Don't overwrite previously inserted extension */
1172 if (!g_hash_table_lookup(table
, exts
[i
])) {
1173 key
= g_strdup(exts
[i
]);
1174 g_hash_table_insert(table
, key
, mime_type
);
1183 GList
*procmime_get_mime_type_list(void)
1187 gchar buf
[BUFFSIZE
];
1190 MimeType
*mime_type
;
1191 gboolean fp_is_glob_file
= TRUE
;
1194 return mime_type_list
;
1196 #if defined(__NetBSD__) || defined(__OpenBSD__) || defined(__FreeBSD__)
1197 if ((fp
= procmime_fopen(DATAROOTDIR
"/mime/globs", "rb")) == NULL
)
1199 if ((fp
= procmime_fopen("/usr/share/mime/globs", "rb")) == NULL
)
1202 fp_is_glob_file
= FALSE
;
1203 if ((fp
= procmime_fopen("/etc/mime.types", "rb")) == NULL
) {
1204 if ((fp
= procmime_fopen(SYSCONFDIR
"/mime.types", "rb"))
1206 FILE_OP_ERROR(SYSCONFDIR
"/mime.types",
1213 while (SC_FGETS(buf
, sizeof(buf
), fp
) != NULL
) {
1214 p
= strchr(buf
, '#');
1220 if (fp_is_glob_file
) {
1221 while (*p
&& !g_ascii_isspace(*p
) && (*p
!=':')) p
++;
1223 while (*p
&& !g_ascii_isspace(*p
)) p
++;
1230 delim
= strchr(buf
, '/');
1231 if (delim
== NULL
) continue;
1234 mime_type
= g_new(MimeType
, 1);
1235 mime_type
->type
= g_strdup(buf
);
1236 mime_type
->sub_type
= g_strdup(delim
+ 1);
1238 if (fp_is_glob_file
) {
1239 while (*p
&& (g_ascii_isspace(*p
)||(*p
=='*')||(*p
=='.'))) p
++;
1241 while (*p
&& g_ascii_isspace(*p
)) p
++;
1245 mime_type
->extension
= g_utf8_strdown(p
, -1);
1247 mime_type
->extension
= NULL
;
1249 list
= g_list_append(list
, mime_type
);
1252 procmime_fclose(fp
);
1255 g_warning("Can't read mime.types\n");
1260 EncodingType
procmime_get_encoding_for_charset(const gchar
*charset
)
1264 else if (!g_ascii_strncasecmp(charset
, "ISO-2022-", 9) ||
1265 !g_ascii_strcasecmp(charset
, "US-ASCII"))
1267 else if (!g_ascii_strcasecmp(charset
, "ISO-8859-5") ||
1268 !g_ascii_strncasecmp(charset
, "KOI8-", 5) ||
1269 !g_ascii_strcasecmp(charset
, "Windows-1251"))
1271 else if (!g_ascii_strncasecmp(charset
, "ISO-8859-", 9))
1272 return ENC_QUOTED_PRINTABLE
;
1273 else if (!g_ascii_strncasecmp(charset
, "UTF-8", 5))
1274 return ENC_QUOTED_PRINTABLE
;
1279 EncodingType
procmime_get_encoding_for_text_file(const gchar
*file
, gboolean
*has_binary
)
1282 guchar buf
[BUFFSIZE
];
1284 size_t octet_chars
= 0;
1285 size_t total_len
= 0;
1286 gfloat octet_percentage
;
1287 gboolean force_b64
= FALSE
;
1289 if ((fp
= procmime_fopen(file
, "rb")) == NULL
) {
1290 FILE_OP_ERROR(file
, "fopen");
1294 while ((len
= SC_FREAD(buf
, sizeof(guchar
), sizeof(buf
), fp
)) > 0) {
1298 for (p
= buf
, i
= 0; i
< len
; ++p
, ++i
) {
1309 procmime_fclose(fp
);
1312 octet_percentage
= (gfloat
)octet_chars
/ (gfloat
)total_len
;
1314 octet_percentage
= 0.0;
1316 debug_print("procmime_get_encoding_for_text_file(): "
1317 "8bit chars: %zd / %zd (%f%%)\n", octet_chars
, total_len
,
1318 100.0 * octet_percentage
);
1320 if (octet_percentage
> 0.20 || force_b64
) {
1321 debug_print("using BASE64\n");
1323 } else if (octet_chars
> 0) {
1324 debug_print("using quoted-printable\n");
1325 return ENC_QUOTED_PRINTABLE
;
1327 debug_print("using 7bit\n");
1332 struct EncodingTable
1335 EncodingType enc_type
;
1338 struct EncodingTable encoding_table
[] = {
1341 {"binary", ENC_BINARY
},
1342 {"quoted-printable", ENC_QUOTED_PRINTABLE
},
1343 {"base64", ENC_BASE64
},
1344 {"x-uuencode", ENC_UNKNOWN
},
1345 {NULL
, ENC_UNKNOWN
},
1348 const gchar
*procmime_get_encoding_str(EncodingType encoding
)
1350 struct EncodingTable
*enc_table
;
1352 for (enc_table
= encoding_table
; enc_table
->str
!= NULL
; enc_table
++) {
1353 if (enc_table
->enc_type
== encoding
)
1354 return enc_table
->str
;
1359 /* --- NEW MIME STUFF --- */
1366 static struct TypeTable mime_type_table
[] = {
1367 {"text", MIMETYPE_TEXT
},
1368 {"image", MIMETYPE_IMAGE
},
1369 {"audio", MIMETYPE_AUDIO
},
1370 {"video", MIMETYPE_VIDEO
},
1371 {"application", MIMETYPE_APPLICATION
},
1372 {"message", MIMETYPE_MESSAGE
},
1373 {"multipart", MIMETYPE_MULTIPART
},
1377 const gchar
*procmime_get_media_type_str(MimeMediaType type
)
1379 struct TypeTable
*type_table
;
1381 for (type_table
= mime_type_table
; type_table
->str
!= NULL
; type_table
++) {
1382 if (type_table
->type
== type
)
1383 return type_table
->str
;
1388 MimeMediaType
procmime_get_media_type(const gchar
*str
)
1390 struct TypeTable
*typetablearray
;
1392 for (typetablearray
= mime_type_table
; typetablearray
->str
!= NULL
; typetablearray
++)
1393 if (g_ascii_strncasecmp(str
, typetablearray
->str
, strlen(typetablearray
->str
)) == 0)
1394 return typetablearray
->type
;
1396 return MIMETYPE_UNKNOWN
;
1400 *\brief Safe wrapper for content type string.
1402 *\return const gchar * Pointer to content type string.
1404 gchar
*procmime_get_content_type_str(MimeMediaType type
,
1405 const char *subtype
)
1407 const gchar
*type_str
= NULL
;
1409 if (subtype
== NULL
|| !(type_str
= procmime_get_media_type_str(type
)))
1410 return g_strdup("unknown");
1411 return g_strdup_printf("%s/%s", type_str
, subtype
);
1414 static int procmime_parse_mimepart(MimeInfo
*parent
,
1415 gchar
*content_type
,
1416 gchar
*content_encoding
,
1417 gchar
*content_description
,
1419 gchar
*content_disposition
,
1420 gchar
*content_location
,
1421 const gchar
*original_msgid
,
1422 const gchar
*disposition_notification_hdr
,
1423 const gchar
*filename
,
1426 gboolean short_scan
);
1428 static void procmime_parse_message_rfc822(MimeInfo
*mimeinfo
, gboolean short_scan
)
1430 HeaderEntry hentry
[] = {{"Content-Type:", NULL
, TRUE
},
1431 {"Content-Transfer-Encoding:",
1433 {"Content-Description:",
1437 {"Content-Disposition:",
1439 {"Content-Location:",
1443 {"Original-Message-ID:",
1447 {NULL
, NULL
, FALSE
}};
1448 guint content_start
, i
;
1453 procmime_decode_content(mimeinfo
);
1455 fp
= procmime_fopen(mimeinfo
->data
.filename
, "rb");
1457 FILE_OP_ERROR(mimeinfo
->data
.filename
, "fopen");
1460 if (fseek(fp
, mimeinfo
->offset
, SEEK_SET
) < 0) {
1461 FILE_OP_ERROR(mimeinfo
->data
.filename
, "fseek");
1462 procmime_fclose(fp
);
1465 procheader_get_header_fields(fp
, hentry
);
1466 if (hentry
[0].body
!= NULL
) {
1467 tmp
= conv_unmime_header(hentry
[0].body
, NULL
, FALSE
);
1468 g_free(hentry
[0].body
);
1469 hentry
[0].body
= tmp
;
1471 if (hentry
[2].body
!= NULL
) {
1472 tmp
= conv_unmime_header(hentry
[2].body
, NULL
, FALSE
);
1473 g_free(hentry
[2].body
);
1474 hentry
[2].body
= tmp
;
1476 if (hentry
[4].body
!= NULL
) {
1477 tmp
= conv_unmime_header(hentry
[4].body
, NULL
, FALSE
);
1478 g_free(hentry
[4].body
);
1479 hentry
[4].body
= tmp
;
1481 if (hentry
[5].body
!= NULL
) {
1482 tmp
= conv_unmime_header(hentry
[5].body
, NULL
, FALSE
);
1483 g_free(hentry
[5].body
);
1484 hentry
[5].body
= tmp
;
1486 if (hentry
[7].body
!= NULL
) {
1487 tmp
= conv_unmime_header(hentry
[7].body
, NULL
, FALSE
);
1488 g_free(hentry
[7].body
);
1489 hentry
[7].body
= tmp
;
1491 if (hentry
[8].body
!= NULL
) {
1492 tmp
= conv_unmime_header(hentry
[8].body
, NULL
, FALSE
);
1493 g_free(hentry
[8].body
);
1494 hentry
[8].body
= tmp
;
1497 content_start
= ftell(fp
);
1498 procmime_fclose(fp
);
1500 len
= mimeinfo
->length
- (content_start
- mimeinfo
->offset
);
1503 procmime_parse_mimepart(mimeinfo
,
1504 hentry
[0].body
, hentry
[1].body
,
1505 hentry
[2].body
, hentry
[3].body
,
1506 hentry
[4].body
, hentry
[5].body
,
1507 hentry
[7].body
, hentry
[8].body
,
1508 mimeinfo
->data
.filename
, content_start
,
1511 for (i
= 0; i
< (sizeof hentry
/ sizeof hentry
[0]); i
++) {
1512 g_free(hentry
[i
].body
);
1513 hentry
[i
].body
= NULL
;
1517 static void procmime_parse_disposition_notification(MimeInfo
*mimeinfo
,
1518 const gchar
*original_msgid
, const gchar
*disposition_notification_hdr
,
1519 gboolean short_scan
)
1521 HeaderEntry hentry
[] = {{"Original-Message-ID:", NULL
, TRUE
},
1522 {"Disposition:", NULL
, TRUE
},
1523 {NULL
, NULL
, FALSE
}};
1526 gchar
*orig_msg_id
= NULL
;
1529 procmime_decode_content(mimeinfo
);
1531 debug_print("parse disposition notification\n");
1532 fp
= procmime_fopen(mimeinfo
->data
.filename
, "rb");
1534 FILE_OP_ERROR(mimeinfo
->data
.filename
, "fopen");
1537 if (fseek(fp
, mimeinfo
->offset
, SEEK_SET
) < 0) {
1538 FILE_OP_ERROR(mimeinfo
->data
.filename
, "fseek");
1539 procmime_fclose(fp
);
1543 if (original_msgid
&& disposition_notification_hdr
) {
1544 hentry
[0].body
= g_strdup(original_msgid
);
1545 hentry
[1].body
= g_strdup(disposition_notification_hdr
);
1547 procheader_get_header_fields(fp
, hentry
);
1550 procmime_fclose(fp
);
1552 if (!hentry
[0].body
|| !hentry
[1].body
) {
1553 debug_print("MsgId %s, Disp %s\n",
1554 hentry
[0].body
? hentry
[0].body
:"(nil)",
1555 hentry
[1].body
? hentry
[1].body
:"(nil)");
1559 orig_msg_id
= g_strdup(hentry
[0].body
);
1560 disp
= g_strdup(hentry
[1].body
);
1562 extract_parenthesis(orig_msg_id
, '<', '>');
1563 remove_space(orig_msg_id
);
1565 if (strstr(disp
, "displayed")) {
1566 /* find sent message, if possible */
1567 MsgInfo
*info
= NULL
;
1569 debug_print("%s has been displayed.\n", orig_msg_id
);
1570 for (flist
= folder_get_list(); flist
!= NULL
; flist
= g_list_next(flist
)) {
1571 FolderItem
*outbox
= ((Folder
*)(flist
->data
))->outbox
;
1573 debug_print("skipping folder with no outbox...\n");
1576 info
= folder_item_get_msginfo_by_msgid(outbox
, orig_msg_id
);
1577 debug_print("%s %s in %s\n", info
?"found":"didn't find", orig_msg_id
, outbox
->path
);
1579 procmsg_msginfo_set_flags(info
, MSG_RETRCPT_GOT
, 0);
1580 procmsg_msginfo_free(info
);
1584 g_free(orig_msg_id
);
1587 for (i
= 0; i
< (sizeof hentry
/ sizeof hentry
[0]); i
++) {
1588 g_free(hentry
[i
].body
);
1589 hentry
[i
].body
= NULL
;
1593 #define GET_HEADERS() { \
1594 procheader_get_header_fields(fp, hentry); \
1595 if (hentry[0].body != NULL) { \
1596 tmp = conv_unmime_header(hentry[0].body, NULL, FALSE); \
1597 g_free(hentry[0].body); \
1598 hentry[0].body = tmp; \
1600 if (hentry[2].body != NULL) { \
1601 tmp = conv_unmime_header(hentry[2].body, NULL, FALSE); \
1602 g_free(hentry[2].body); \
1603 hentry[2].body = tmp; \
1605 if (hentry[4].body != NULL) { \
1606 tmp = conv_unmime_header(hentry[4].body, NULL, FALSE); \
1607 g_free(hentry[4].body); \
1608 hentry[4].body = tmp; \
1610 if (hentry[5].body != NULL) { \
1611 tmp = conv_unmime_header(hentry[5].body, NULL, FALSE); \
1612 g_free(hentry[5].body); \
1613 hentry[5].body = tmp; \
1615 if (hentry[6].body != NULL) { \
1616 tmp = conv_unmime_header(hentry[6].body, NULL, FALSE); \
1617 g_free(hentry[6].body); \
1618 hentry[6].body = tmp; \
1620 if (hentry[7].body != NULL) { \
1621 tmp = conv_unmime_header(hentry[7].body, NULL, FALSE); \
1622 g_free(hentry[7].body); \
1623 hentry[7].body = tmp; \
1627 static void procmime_parse_multipart(MimeInfo
*mimeinfo
, gboolean short_scan
)
1629 HeaderEntry hentry
[] = {{"Content-Type:", NULL
, TRUE
},
1630 {"Content-Transfer-Encoding:",
1632 {"Content-Description:",
1636 {"Content-Disposition:",
1638 {"Content-Location:",
1640 {"Original-Message-ID:",
1644 {NULL
, NULL
, FALSE
}};
1647 gint boundary_len
= 0, lastoffset
= -1, i
;
1648 gchar buf
[BUFFSIZE
];
1651 gboolean start_found
= FALSE
;
1652 gboolean end_found
= FALSE
;
1654 boundary
= g_hash_table_lookup(mimeinfo
->typeparameters
, "boundary");
1657 boundary_len
= strlen(boundary
);
1659 procmime_decode_content(mimeinfo
);
1661 fp
= procmime_fopen(mimeinfo
->data
.filename
, "rb");
1663 FILE_OP_ERROR(mimeinfo
->data
.filename
, "fopen");
1667 if (fseek(fp
, mimeinfo
->offset
, SEEK_SET
) < 0) {
1668 FILE_OP_ERROR(mimeinfo
->data
.filename
, "fseek");
1669 procmime_fclose(fp
);
1673 while (SC_FGETS(buf
, sizeof(buf
), fp
) != NULL
&& result
== 0) {
1674 if (ftell(fp
) - 1 > (mimeinfo
->offset
+ mimeinfo
->length
))
1677 if (IS_BOUNDARY(buf
, boundary
, boundary_len
)) {
1680 if (lastoffset
!= -1) {
1681 gint len
= (ftell(fp
) - strlen(buf
)) - lastoffset
- 1;
1684 result
= procmime_parse_mimepart(mimeinfo
,
1685 hentry
[0].body
, hentry
[1].body
,
1686 hentry
[2].body
, hentry
[3].body
,
1687 hentry
[4].body
, hentry
[5].body
,
1688 hentry
[6].body
, hentry
[7].body
,
1689 mimeinfo
->data
.filename
, lastoffset
,
1691 if (result
== 1 && short_scan
)
1696 if (buf
[2 + boundary_len
] == '-' &&
1697 buf
[2 + boundary_len
+ 1] == '-') {
1701 for (i
= 0; i
< (sizeof hentry
/ sizeof hentry
[0]) ; i
++) {
1702 g_free(hentry
[i
].body
);
1703 hentry
[i
].body
= NULL
;
1706 lastoffset
= ftell(fp
);
1710 if (start_found
&& !end_found
&& lastoffset
!= -1) {
1711 gint len
= (ftell(fp
) - strlen(buf
)) - lastoffset
- 1;
1714 result
= procmime_parse_mimepart(mimeinfo
,
1715 hentry
[0].body
, hentry
[1].body
,
1716 hentry
[2].body
, hentry
[3].body
,
1717 hentry
[4].body
, hentry
[5].body
,
1718 hentry
[6].body
, hentry
[7].body
,
1719 mimeinfo
->data
.filename
, lastoffset
,
1722 mimeinfo
->broken
= TRUE
;
1725 for (i
= 0; i
< (sizeof hentry
/ sizeof hentry
[0]); i
++) {
1726 g_free(hentry
[i
].body
);
1727 hentry
[i
].body
= NULL
;
1729 procmime_fclose(fp
);
1732 static void parse_parameters(const gchar
*parameters
, GHashTable
*table
)
1734 gchar
*params
, *param
, *next
;
1735 GSList
*convlist
= NULL
, *concatlist
= NULL
, *cur
;
1737 params
= g_strdup(parameters
);
1740 for (; next
!= NULL
; param
= next
) {
1741 gchar
*attribute
, *value
, *tmp
, *down_attr
, *orig_down_attr
;
1743 gboolean convert
= FALSE
;
1745 next
= strchr_with_skip_quote(param
, '"', ';');
1754 value
= strchr(attribute
, '=');
1760 while (value
[0] != '\0' && value
[0] == ' ')
1763 down_attr
= g_utf8_strdown(attribute
, -1);
1764 orig_down_attr
= down_attr
;
1766 len
= down_attr
? strlen(down_attr
):0;
1767 if (len
> 0 && down_attr
[len
- 1] == '*') {
1768 gchar
*srcpos
, *dstpos
, *endpos
;
1771 down_attr
[len
- 1] = '\0';
1775 endpos
= value
+ strlen(value
);
1776 while (srcpos
< endpos
) {
1782 if (!get_hex_value(&dstvalue
, srcpos
[1], srcpos
[2]))
1792 if (value
[0] == '"')
1793 extract_quote(value
, '"');
1795 if (value
[0] == '"')
1796 extract_quote(value
, '"');
1797 else if ((tmp
= strchr(value
, ' ')) != NULL
)
1802 while (down_attr
[0] == ' ')
1804 while (down_attr
[strlen(down_attr
)-1] == ' ')
1805 down_attr
[strlen(down_attr
)-1] = '\0';
1808 while (value
[0] != '\0' && value
[0] == ' ')
1810 while (value
[strlen(value
)-1] == ' ')
1811 value
[strlen(value
)-1] = '\0';
1813 if (down_attr
&& strrchr(down_attr
, '*') != NULL
) {
1816 tmpattr
= g_strdup(down_attr
);
1817 tmp
= strrchr(tmpattr
, '*');
1820 if ((tmp
[1] == '0') && (tmp
[2] == '\0') &&
1821 (g_slist_find_custom(concatlist
, down_attr
, (GCompareFunc
)g_strcmp0
) == NULL
))
1822 concatlist
= g_slist_prepend(concatlist
, g_strdup(tmpattr
));
1824 if (convert
&& (g_slist_find_custom(convlist
, tmpattr
, (GCompareFunc
)g_strcmp0
) == NULL
))
1825 convlist
= g_slist_prepend(convlist
, g_strdup(tmpattr
));
1828 } else if (convert
) {
1829 if (g_slist_find_custom(convlist
, down_attr
, (GCompareFunc
)g_strcmp0
) == NULL
)
1830 convlist
= g_slist_prepend(convlist
, g_strdup(down_attr
));
1833 if (g_hash_table_lookup(table
, down_attr
) == NULL
)
1834 g_hash_table_insert(table
, g_strdup(down_attr
), g_strdup(value
));
1835 g_free(orig_down_attr
);
1838 for (cur
= concatlist
; cur
!= NULL
; cur
= g_slist_next(cur
)) {
1839 gchar
*attribute
, *attrwnum
, *partvalue
;
1843 attribute
= (gchar
*) cur
->data
;
1844 value
= g_string_sized_new(64);
1846 attrwnum
= g_strdup_printf("%s*%d", attribute
, n
);
1847 while ((partvalue
= g_hash_table_lookup(table
, attrwnum
)) != NULL
) {
1848 g_string_append(value
, partvalue
);
1850 g_hash_table_remove(table
, attrwnum
);
1853 attrwnum
= g_strdup_printf("%s*%d", attribute
, n
);
1857 g_hash_table_insert(table
, g_strdup(attribute
), g_strdup(value
->str
));
1858 g_string_free(value
, TRUE
);
1860 slist_free_strings_full(concatlist
);
1862 for (cur
= convlist
; cur
!= NULL
; cur
= g_slist_next(cur
)) {
1863 gchar
*attribute
, *key
, *value
;
1864 gchar
*charset
, *lang
, *oldvalue
, *newvalue
;
1866 attribute
= (gchar
*) cur
->data
;
1867 if (!g_hash_table_lookup_extended(
1868 table
, attribute
, (gpointer
*)(gchar
*) &key
, (gpointer
*)(gchar
*) &value
))
1872 if (charset
== NULL
)
1874 lang
= strchr(charset
, '\'');
1879 oldvalue
= strchr(lang
, '\'');
1880 if (oldvalue
== NULL
)
1885 newvalue
= conv_codeset_strdup(oldvalue
, charset
, CS_UTF_8
);
1887 g_hash_table_remove(table
, attribute
);
1891 g_hash_table_insert(table
, g_strdup(attribute
), newvalue
);
1893 slist_free_strings_full(convlist
);
1898 static void procmime_parse_content_type(const gchar
*content_type
, MimeInfo
*mimeinfo
)
1900 cm_return_if_fail(content_type
!= NULL
);
1901 cm_return_if_fail(mimeinfo
!= NULL
);
1903 /* RFC 2045, page 13 says that the mime subtype is MANDATORY;
1904 * if it's not available we use the default Content-Type */
1905 if ((content_type
[0] == '\0') || (strchr(content_type
, '/') == NULL
)) {
1906 mimeinfo
->type
= MIMETYPE_TEXT
;
1907 mimeinfo
->subtype
= g_strdup("plain");
1908 if (g_hash_table_lookup(mimeinfo
->typeparameters
,
1909 "charset") == NULL
) {
1910 g_hash_table_insert(mimeinfo
->typeparameters
,
1911 g_strdup("charset"),
1913 conv_get_locale_charset_str_no_utf8()));
1916 gchar
*type
, *subtype
, *params
;
1918 type
= g_strdup(content_type
);
1919 subtype
= strchr(type
, '/') + 1;
1920 *(subtype
- 1) = '\0';
1921 if ((params
= strchr(subtype
, ';')) != NULL
) {
1926 mimeinfo
->type
= procmime_get_media_type(type
);
1927 mimeinfo
->subtype
= g_strstrip(g_strdup(subtype
));
1929 /* Get mimeinfo->typeparameters */
1931 parse_parameters(params
, mimeinfo
->typeparameters
);
1937 static void procmime_parse_content_disposition(const gchar
*content_disposition
, MimeInfo
*mimeinfo
)
1939 gchar
*tmp
, *params
;
1941 cm_return_if_fail(content_disposition
!= NULL
);
1942 cm_return_if_fail(mimeinfo
!= NULL
);
1944 tmp
= g_strdup(content_disposition
);
1945 if ((params
= strchr(tmp
, ';')) != NULL
) {
1951 if (!g_ascii_strcasecmp(tmp
, "inline"))
1952 mimeinfo
->disposition
= DISPOSITIONTYPE_INLINE
;
1953 else if (!g_ascii_strcasecmp(tmp
, "attachment"))
1954 mimeinfo
->disposition
= DISPOSITIONTYPE_ATTACHMENT
;
1956 mimeinfo
->disposition
= DISPOSITIONTYPE_ATTACHMENT
;
1959 parse_parameters(params
, mimeinfo
->dispositionparameters
);
1965 static void procmime_parse_content_encoding(const gchar
*content_encoding
, MimeInfo
*mimeinfo
)
1967 struct EncodingTable
*enc_table
;
1969 for (enc_table
= encoding_table
; enc_table
->str
!= NULL
; enc_table
++) {
1970 if (g_ascii_strcasecmp(enc_table
->str
, content_encoding
) == 0) {
1971 mimeinfo
->encoding_type
= enc_table
->enc_type
;
1975 mimeinfo
->encoding_type
= ENC_UNKNOWN
;
1979 static GSList
*registered_parsers
= NULL
;
1981 static MimeParser
*procmime_get_mimeparser_for_type(MimeMediaType type
, const gchar
*sub_type
)
1984 for (cur
= registered_parsers
; cur
; cur
= cur
->next
) {
1985 MimeParser
*parser
= (MimeParser
*)cur
->data
;
1986 if (parser
->type
== type
&& !strcmp2(parser
->sub_type
, sub_type
))
1992 void procmime_mimeparser_register(MimeParser
*parser
)
1994 if (!procmime_get_mimeparser_for_type(parser
->type
, parser
->sub_type
))
1995 registered_parsers
= g_slist_append(registered_parsers
, parser
);
1999 void procmime_mimeparser_unregister(MimeParser
*parser
)
2001 registered_parsers
= g_slist_remove(registered_parsers
, parser
);
2004 static gboolean
procmime_mimeparser_parse(MimeParser
*parser
, MimeInfo
*mimeinfo
)
2006 cm_return_val_if_fail(parser
->parse
!= NULL
, FALSE
);
2007 return parser
->parse(parser
, mimeinfo
);
2010 static int procmime_parse_mimepart(MimeInfo
*parent
,
2011 gchar
*content_type
,
2012 gchar
*content_encoding
,
2013 gchar
*content_description
,
2015 gchar
*content_disposition
,
2016 gchar
*content_location
,
2017 const gchar
*original_msgid
,
2018 const gchar
*disposition_notification_hdr
,
2019 const gchar
*filename
,
2022 gboolean short_scan
)
2025 MimeParser
*parser
= NULL
;
2026 gboolean parsed
= FALSE
;
2029 /* Create MimeInfo */
2030 mimeinfo
= procmime_mimeinfo_new();
2031 mimeinfo
->content
= MIMECONTENT_FILE
;
2033 if (parent
!= NULL
) {
2034 if (g_node_depth(parent
->node
) > 32) {
2035 /* 32 is an arbitrary value
2036 * this avoids DOSsing ourselves
2037 * with enormous messages
2039 procmime_mimeinfo_free_all(mimeinfo
);
2042 g_node_append(parent
->node
, mimeinfo
->node
);
2044 mimeinfo
->data
.filename
= g_strdup(filename
);
2045 mimeinfo
->offset
= offset
;
2046 mimeinfo
->length
= length
;
2048 if (content_type
!= NULL
) {
2049 g_strchomp(content_type
);
2050 procmime_parse_content_type(content_type
, mimeinfo
);
2052 mimeinfo
->type
= MIMETYPE_TEXT
;
2053 mimeinfo
->subtype
= g_strdup("plain");
2054 if (g_hash_table_lookup(mimeinfo
->typeparameters
,
2055 "charset") == NULL
) {
2056 g_hash_table_insert(mimeinfo
->typeparameters
,
2057 g_strdup("charset"),
2059 conv_get_locale_charset_str_no_utf8()));
2063 if (content_encoding
!= NULL
) {
2064 g_strchomp(content_encoding
);
2065 procmime_parse_content_encoding(content_encoding
, mimeinfo
);
2067 mimeinfo
->encoding_type
= ENC_UNKNOWN
;
2070 if (content_description
!= NULL
)
2071 mimeinfo
->description
= g_strdup(content_description
);
2073 mimeinfo
->description
= NULL
;
2075 if (content_id
!= NULL
)
2076 mimeinfo
->id
= g_strdup(content_id
);
2078 mimeinfo
->id
= NULL
;
2080 if (content_location
!= NULL
)
2081 mimeinfo
->location
= g_strdup(content_location
);
2083 mimeinfo
->location
= NULL
;
2085 if (content_disposition
!= NULL
) {
2086 g_strchomp(content_disposition
);
2087 procmime_parse_content_disposition(content_disposition
, mimeinfo
);
2089 mimeinfo
->disposition
= DISPOSITIONTYPE_UNKNOWN
;
2091 /* Call parser for mime type */
2092 if ((parser
= procmime_get_mimeparser_for_type(mimeinfo
->type
, mimeinfo
->subtype
)) != NULL
) {
2093 parsed
= procmime_mimeparser_parse(parser
, mimeinfo
);
2096 switch (mimeinfo
->type
) {
2098 if (g_ascii_strcasecmp(mimeinfo
->subtype
, "plain") == 0 && short_scan
) {
2103 case MIMETYPE_MESSAGE
:
2104 if (g_ascii_strcasecmp(mimeinfo
->subtype
, "rfc822") == 0) {
2105 procmime_parse_message_rfc822(mimeinfo
, short_scan
);
2107 if (g_ascii_strcasecmp(mimeinfo
->subtype
, "disposition-notification") == 0) {
2108 procmime_parse_disposition_notification(mimeinfo
,
2109 original_msgid
, disposition_notification_hdr
, short_scan
);
2113 case MIMETYPE_MULTIPART
:
2114 procmime_parse_multipart(mimeinfo
, short_scan
);
2117 case MIMETYPE_APPLICATION
:
2118 if (g_ascii_strcasecmp(mimeinfo
->subtype
, "octet-stream") == 0
2119 && original_msgid
&& *original_msgid
2120 && disposition_notification_hdr
&& *disposition_notification_hdr
) {
2121 procmime_parse_disposition_notification(mimeinfo
,
2122 original_msgid
, disposition_notification_hdr
, short_scan
);
2133 static gchar
*typenames
[] = {
2144 static gboolean
output_func(GNode
*node
, gpointer data
)
2147 MimeInfo
*mimeinfo
= (MimeInfo
*) node
->data
;
2149 depth
= g_node_depth(node
);
2150 for (i
= 0; i
< depth
; i
++)
2152 g_print("%s/%s (offset:%d length:%d encoding: %d)\n", typenames
[mimeinfo
->type
], mimeinfo
->subtype
, mimeinfo
->offset
, mimeinfo
->length
, mimeinfo
->encoding_type
);
2157 static void output_mime_structure(MimeInfo
*mimeinfo
, int indent
)
2159 g_node_traverse(mimeinfo
->node
, G_PRE_ORDER
, G_TRAVERSE_ALL
, -1, output_func
, NULL
);
2162 static MimeInfo
*procmime_scan_file_with_offset(const gchar
*filename
, int offset
, gboolean short_scan
)
2167 if (g_stat(filename
, &buf
) < 0) {
2168 FILE_OP_ERROR(filename
, "stat");
2172 mimeinfo
= procmime_mimeinfo_new();
2173 mimeinfo
->content
= MIMECONTENT_FILE
;
2174 mimeinfo
->encoding_type
= ENC_UNKNOWN
;
2175 mimeinfo
->type
= MIMETYPE_MESSAGE
;
2176 mimeinfo
->subtype
= g_strdup("rfc822");
2177 mimeinfo
->data
.filename
= g_strdup(filename
);
2178 mimeinfo
->offset
= offset
;
2179 mimeinfo
->length
= buf
.st_size
- offset
;
2181 procmime_parse_message_rfc822(mimeinfo
, short_scan
);
2182 if (debug_get_mode())
2183 output_mime_structure(mimeinfo
, 0);
2188 static MimeInfo
*procmime_scan_file_full(const gchar
*filename
, gboolean short_scan
)
2192 cm_return_val_if_fail(filename
!= NULL
, NULL
);
2194 mimeinfo
= procmime_scan_file_with_offset(filename
, 0, short_scan
);
2199 MimeInfo
*procmime_scan_file(const gchar
*filename
)
2201 return procmime_scan_file_full(filename
, FALSE
);
2204 static MimeInfo
*procmime_scan_file_short(const gchar
*filename
)
2206 return procmime_scan_file_full(filename
, TRUE
);
2209 static MimeInfo
*procmime_scan_queue_file_full(const gchar
*filename
, gboolean short_scan
)
2213 gchar buf
[BUFFSIZE
];
2216 cm_return_val_if_fail(filename
!= NULL
, NULL
);
2219 if ((fp
= procmime_fopen(filename
, "rb")) == NULL
)
2221 /* Skip queue header */
2222 while (SC_FGETS(buf
, sizeof(buf
), fp
) != NULL
) {
2224 if ((!strncmp(buf
, "X-Claws-End-Special-Headers: 1",
2225 strlen("X-Claws-End-Special-Headers:"))) ||
2226 (!strncmp(buf
, "X-Sylpheed-End-Special-Headers: 1",
2227 strlen("X-Sylpheed-End-Special-Headers:"))))
2230 if (buf
[0] == '\r' || buf
[0] == '\n') break;
2231 /* from other mailers */
2232 if (!strncmp(buf
, "Date: ", 6)
2233 || !strncmp(buf
, "To: ", 4)
2234 || !strncmp(buf
, "From: ", 6)
2235 || !strncmp(buf
, "Subject: ", 9)) {
2241 procmime_fclose(fp
);
2243 mimeinfo
= procmime_scan_file_with_offset(filename
, offset
, short_scan
);
2248 MimeInfo
*procmime_scan_queue_file(const gchar
*filename
)
2250 return procmime_scan_queue_file_full(filename
, FALSE
);
2253 static MimeInfo
*procmime_scan_queue_file_short(const gchar
*filename
)
2255 return procmime_scan_queue_file_full(filename
, TRUE
);
2260 ENC_AS_QUOTED_STRING
,
2265 typedef struct _ParametersData
{
2271 static void write_parameters(gpointer key
, gpointer value
, gpointer user_data
)
2274 gchar
*val
= value
, *valpos
, *tmp
;
2275 ParametersData
*pdata
= (ParametersData
*)user_data
;
2276 GString
*buf
= g_string_new("");
2279 EncodeAs encas
= ENC_AS_TOKEN
;
2281 for (valpos
= val
; *valpos
!= 0; valpos
++) {
2282 if (!IS_ASCII(*valpos
)) {
2283 encas
= ENC_AS_ENCWORD
;
2288 if (((*valpos
>= 0) && (*valpos
< 037)) || (*valpos
== 0177)) {
2289 encas
= ENC_AS_QUOTED_STRING
;
2293 /* tspecials + SPACE */
2311 encas
= ENC_AS_QUOTED_STRING
;
2318 g_string_append_printf(buf
, "%s=%s", param
, val
);
2321 case ENC_AS_QUOTED_STRING
:
2322 g_string_append_printf(buf
, "%s=\"%s\"", param
, val
);
2325 #if 0 /* we don't use that for now */
2326 case ENC_AS_EXTENDED
:
2327 if (!g_utf8_validate(val
, -1, NULL
))
2328 g_string_append_printf(buf
, "%s*=%s''", param
,
2329 conv_get_locale_charset_str());
2331 g_string_append_printf(buf
, "%s*=%s''", param
,
2333 for (valpos
= val
; *valpos
!= '\0'; valpos
++) {
2334 if (IS_ASCII(*valpos
) && isalnum(*valpos
)) {
2335 g_string_append_printf(buf
, "%c", *valpos
);
2337 gchar hexstr
[3] = "XX";
2338 get_hex_str(hexstr
, *valpos
);
2339 g_string_append_printf(buf
, "%%%s", hexstr
);
2344 case ENC_AS_EXTENDED
:
2345 debug_print("Unhandled ENC_AS_EXTENDED.");
2348 case ENC_AS_ENCWORD
:
2349 len
= MAX(strlen(val
)*6, 512);
2350 tmp
= g_malloc(len
+1);
2351 codeconv_set_strict(TRUE
);
2352 conv_encode_header_full(tmp
, len
, val
, pdata
->len
+ strlen(param
) + 4 , FALSE
,
2353 prefs_common
.outgoing_charset
);
2354 codeconv_set_strict(FALSE
);
2355 if (!tmp
|| !*tmp
) {
2356 codeconv_set_strict(TRUE
);
2357 conv_encode_header_full(tmp
, len
, val
, pdata
->len
+ strlen(param
) + 4 , FALSE
,
2358 conv_get_outgoing_charset_str());
2359 codeconv_set_strict(FALSE
);
2361 if (!tmp
|| !*tmp
) {
2362 codeconv_set_strict(TRUE
);
2363 conv_encode_header_full(tmp
, len
, val
, pdata
->len
+ strlen(param
) + 4 , FALSE
,
2365 codeconv_set_strict(FALSE
);
2367 if (!tmp
|| !*tmp
) {
2368 conv_encode_header_full(tmp
, len
, val
, pdata
->len
+ strlen(param
) + 4 , FALSE
,
2371 g_string_append_printf(buf
, "%s=\"%s\"", param
, tmp
);
2377 if (buf
->str
&& strlen(buf
->str
)) {
2378 tmp
= strstr(buf
->str
, "\n");
2380 len
= (tmp
- buf
->str
);
2382 len
= strlen(buf
->str
);
2383 if (pdata
->len
+ len
> 76) {
2384 if (fprintf(pdata
->fp
, ";\n %s", buf
->str
) < 0)
2385 pdata
->error
= TRUE
;
2386 pdata
->len
= strlen(buf
->str
) + 1;
2388 if (fprintf(pdata
->fp
, "; %s", buf
->str
) < 0)
2389 pdata
->error
= TRUE
;
2390 pdata
->len
+= strlen(buf
->str
) + 2;
2393 g_string_free(buf
, TRUE
);
2396 #define TRY(func) { \
2402 int procmime_write_mime_header(MimeInfo
*mimeinfo
, FILE *fp
)
2404 struct TypeTable
*type_table
;
2405 ParametersData
*pdata
= g_new0(ParametersData
, 1);
2406 debug_print("procmime_write_mime_header\n");
2409 pdata
->error
= FALSE
;
2410 for (type_table
= mime_type_table
; type_table
->str
!= NULL
; type_table
++)
2411 if (mimeinfo
->type
== type_table
->type
) {
2412 gchar
*buf
= g_strdup_printf(
2413 "Content-Type: %s/%s", type_table
->str
, mimeinfo
->subtype
);
2414 if (fprintf(fp
, "%s", buf
) < 0) {
2419 pdata
->len
= strlen(buf
);
2423 g_hash_table_foreach(mimeinfo
->typeparameters
, write_parameters
, pdata
);
2424 if (pdata
->error
== TRUE
) {
2430 TRY(fprintf(fp
, "\n") >= 0);
2432 if (mimeinfo
->encoding_type
!= ENC_UNKNOWN
)
2433 TRY(fprintf(fp
, "Content-Transfer-Encoding: %s\n", procmime_get_encoding_str(mimeinfo
->encoding_type
)) >= 0);
2435 if (mimeinfo
->description
!= NULL
)
2436 TRY(fprintf(fp
, "Content-Description: %s\n", mimeinfo
->description
) >= 0);
2438 if (mimeinfo
->id
!= NULL
)
2439 TRY(fprintf(fp
, "Content-ID: %s\n", mimeinfo
->id
) >= 0);
2441 if (mimeinfo
->location
!= NULL
)
2442 TRY(fprintf(fp
, "Content-Location: %s\n", mimeinfo
->location
) >= 0);
2444 if (mimeinfo
->disposition
!= DISPOSITIONTYPE_UNKNOWN
) {
2445 ParametersData
*pdata
= g_new0(ParametersData
, 1);
2447 if (mimeinfo
->disposition
== DISPOSITIONTYPE_INLINE
)
2448 buf
= g_strdup("Content-Disposition: inline");
2449 else if (mimeinfo
->disposition
== DISPOSITIONTYPE_ATTACHMENT
)
2450 buf
= g_strdup("Content-Disposition: attachment");
2452 buf
= g_strdup("Content-Disposition: unknown");
2454 if (fprintf(fp
, "%s", buf
) < 0) {
2459 pdata
->len
= strlen(buf
);
2463 pdata
->error
= FALSE
;
2464 g_hash_table_foreach(mimeinfo
->dispositionparameters
, write_parameters
, pdata
);
2465 if (pdata
->error
== TRUE
) {
2470 TRY(fprintf(fp
, "\n") >= 0);
2473 TRY(fprintf(fp
, "\n") >= 0);
2478 static gint
procmime_write_message_rfc822(MimeInfo
*mimeinfo
, FILE *fp
)
2483 gchar buf
[BUFFSIZE
];
2484 gboolean skip
= FALSE
;;
2487 debug_print("procmime_write_message_rfc822\n");
2490 switch (mimeinfo
->content
) {
2491 case MIMECONTENT_FILE
:
2492 if ((infp
= procmime_fopen(mimeinfo
->data
.filename
, "rb")) == NULL
) {
2493 FILE_OP_ERROR(mimeinfo
->data
.filename
, "fopen");
2496 if (fseek(infp
, mimeinfo
->offset
, SEEK_SET
) < 0) {
2497 FILE_OP_ERROR(mimeinfo
->data
.filename
, "fseek");
2498 procmime_fclose(infp
);
2501 while (SC_FGETS(buf
, sizeof(buf
), infp
) == buf
) {
2503 if (buf
[0] == '\n' && buf
[1] == '\0')
2505 if (skip
&& (buf
[0] == ' ' || buf
[0] == '\t'))
2507 if (g_ascii_strncasecmp(buf
, "MIME-Version:", 13) == 0 ||
2508 g_ascii_strncasecmp(buf
, "Content-Type:", 13) == 0 ||
2509 g_ascii_strncasecmp(buf
, "Content-Transfer-Encoding:", 26) == 0 ||
2510 g_ascii_strncasecmp(buf
, "Content-Description:", 20) == 0 ||
2511 g_ascii_strncasecmp(buf
, "Content-ID:", 11) == 0 ||
2512 g_ascii_strncasecmp(buf
, "Content-Location:", 17) == 0 ||
2513 g_ascii_strncasecmp(buf
, "Content-Disposition:", 20) == 0) {
2518 if (SC_FWRITE(buf
, sizeof(gchar
), len
, fp
) < len
) {
2519 g_warning("failed to dump %zd bytes from file", len
);
2520 procmime_fclose(infp
);
2525 procmime_fclose(infp
);
2528 case MIMECONTENT_MEM
:
2529 len
= strlen(mimeinfo
->data
.mem
);
2530 if (SC_FWRITE(mimeinfo
->data
.mem
, sizeof(gchar
), len
, fp
) < len
) {
2531 g_warning("failed to dump %zd bytes from mem", len
);
2540 childnode
= mimeinfo
->node
->children
;
2541 if (childnode
== NULL
)
2544 child
= (MimeInfo
*) childnode
->data
;
2545 if (fprintf(fp
, "MIME-Version: 1.0\n") < 0) {
2546 g_warning("failed to write mime version");
2549 if (procmime_write_mime_header(child
, fp
) < 0)
2551 return procmime_write_mimeinfo(child
, fp
);
2554 static gint
procmime_write_multipart(MimeInfo
*mimeinfo
, FILE *fp
)
2558 gchar
*boundary
, *str
, *str2
;
2559 gchar buf
[BUFFSIZE
];
2560 gboolean firstboundary
;
2563 debug_print("procmime_write_multipart\n");
2565 boundary
= g_hash_table_lookup(mimeinfo
->typeparameters
, "boundary");
2567 switch (mimeinfo
->content
) {
2568 case MIMECONTENT_FILE
:
2569 if ((infp
= procmime_fopen(mimeinfo
->data
.filename
, "rb")) == NULL
) {
2570 FILE_OP_ERROR(mimeinfo
->data
.filename
, "fopen");
2573 if (fseek(infp
, mimeinfo
->offset
, SEEK_SET
) < 0) {
2574 FILE_OP_ERROR(mimeinfo
->data
.filename
, "fseek");
2575 procmime_fclose(infp
);
2578 while (SC_FGETS(buf
, sizeof(buf
), infp
) == buf
) {
2579 if (IS_BOUNDARY(buf
, boundary
, strlen(boundary
)))
2582 if (SC_FWRITE(buf
, sizeof(gchar
), len
, fp
) < len
) {
2583 g_warning("failed to write %zd", len
);
2584 procmime_fclose(infp
);
2588 procmime_fclose(infp
);
2591 case MIMECONTENT_MEM
:
2592 str
= g_strdup(mimeinfo
->data
.mem
);
2593 if (((str2
= strstr(str
, boundary
)) != NULL
) && ((str2
- str
) >= 2) &&
2594 (*(str2
- 1) == '-') && (*(str2
- 2) == '-'))
2597 if (SC_FWRITE(str
, sizeof(gchar
), len
, fp
) < len
) {
2598 g_warning("failed to write %zd from mem", len
);
2609 childnode
= mimeinfo
->node
->children
;
2610 firstboundary
= TRUE
;
2611 while (childnode
!= NULL
) {
2612 MimeInfo
*child
= childnode
->data
;
2615 firstboundary
= FALSE
;
2617 TRY(fprintf(fp
, "\n") >= 0);
2619 TRY(fprintf(fp
, "--%s\n", boundary
) >= 0);
2621 if (procmime_write_mime_header(child
, fp
) < 0)
2623 if (procmime_write_mimeinfo(child
, fp
) < 0)
2626 childnode
= g_node_next_sibling(childnode
);
2628 TRY(fprintf(fp
, "\n--%s--\n", boundary
) >= 0);
2633 gint
procmime_write_mimeinfo(MimeInfo
*mimeinfo
, FILE *fp
)
2637 debug_print("procmime_write_mimeinfo\n");
2639 if (G_NODE_IS_LEAF(mimeinfo
->node
)) {
2640 switch (mimeinfo
->content
) {
2641 case MIMECONTENT_FILE
:
2642 if ((infp
= procmime_fopen(mimeinfo
->data
.filename
, "rb")) == NULL
) {
2643 FILE_OP_ERROR(mimeinfo
->data
.filename
, "fopen");
2646 copy_file_part_to_fp(infp
, mimeinfo
->offset
, mimeinfo
->length
, fp
);
2647 procmime_fclose(infp
);
2650 case MIMECONTENT_MEM
:
2651 len
= strlen(mimeinfo
->data
.mem
);
2652 if (SC_FWRITE(mimeinfo
->data
.mem
, sizeof(gchar
), len
, fp
) < len
)
2660 /* Call writer for mime type */
2661 switch (mimeinfo
->type
) {
2662 case MIMETYPE_MESSAGE
:
2663 if (g_ascii_strcasecmp(mimeinfo
->subtype
, "rfc822") == 0) {
2664 return procmime_write_message_rfc822(mimeinfo
, fp
);
2668 case MIMETYPE_MULTIPART
:
2669 return procmime_write_multipart(mimeinfo
, fp
);
2681 gchar
*procmime_get_part_file_name(MimeInfo
*mimeinfo
)
2685 if ((mimeinfo
->type
== MIMETYPE_TEXT
) && !g_ascii_strcasecmp(mimeinfo
->subtype
, "html"))
2686 base
= g_strdup("mimetmp.html");
2688 const gchar
*basetmp
;
2691 basetmp
= procmime_mimeinfo_get_parameter(mimeinfo
, "filename");
2692 if (basetmp
== NULL
)
2693 basetmp
= procmime_mimeinfo_get_parameter(mimeinfo
, "name");
2694 if (basetmp
== NULL
)
2695 basetmp
= "mimetmp";
2696 basename
= g_path_get_basename(basetmp
);
2697 if (*basename
== '\0') {
2699 basename
= g_strdup("mimetmp");
2701 base
= conv_filename_from_utf8(basename
);
2703 subst_for_shellsafe_filename(base
);