2 * Claws Mail -- a GTK based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2023 the Claws Mail team and Hiroyuki Yamamoto
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"
29 #include <glib/gi18n.h>
35 #include <sys/types.h>
41 #include "procheader.h"
42 #include "quoted-printable.h"
49 #include "prefs_common.h"
50 #include "prefs_gtk.h"
51 #include "alertpanel.h"
55 #include "file-utils.h"
59 #define REG_MIME_TYPE_VALUE "Content Type"
63 static GHashTable
*procmime_get_mime_type_table (void);
65 static MimeInfo
*procmime_scan_file_short(const gchar
*filename
);
66 static MimeInfo
*procmime_scan_queue_file_short(const gchar
*filename
);
67 static MimeInfo
*procmime_scan_queue_file_full(const gchar
*filename
, gboolean short_scan
);
69 MimeInfo
*procmime_mimeinfo_new(void)
73 mimeinfo
= g_new0(MimeInfo
, 1);
75 mimeinfo
->content
= MIMECONTENT_EMPTY
;
76 mimeinfo
->data
.filename
= NULL
;
78 mimeinfo
->type
= MIMETYPE_UNKNOWN
;
79 mimeinfo
->encoding_type
= ENC_UNKNOWN
;
80 mimeinfo
->typeparameters
= g_hash_table_new(g_str_hash
, g_str_equal
);
82 mimeinfo
->disposition
= DISPOSITIONTYPE_UNKNOWN
;
83 mimeinfo
->dispositionparameters
84 = g_hash_table_new(g_str_hash
, g_str_equal
);
86 mimeinfo
->node
= g_node_new(mimeinfo
);
91 static gboolean
procmime_mimeinfo_parameters_destroy(gpointer key
, gpointer value
, gpointer user_data
)
99 static gchar
*forced_charset
= NULL
;
101 void procmime_force_charset(const gchar
*str
)
103 g_free(forced_charset
);
104 forced_charset
= NULL
;
106 forced_charset
= g_strdup(str
);
109 static EncodingType forced_encoding
= 0;
111 void procmime_force_encoding(EncodingType encoding
)
113 forced_encoding
= encoding
;
116 static gboolean
free_func(GNode
*node
, gpointer data
)
118 MimeInfo
*mimeinfo
= (MimeInfo
*) node
->data
;
120 switch (mimeinfo
->content
) {
121 case MIMECONTENT_FILE
:
123 claws_unlink(mimeinfo
->data
.filename
);
124 g_free(mimeinfo
->data
.filename
);
127 case MIMECONTENT_MEM
:
129 g_free(mimeinfo
->data
.mem
);
134 g_free(mimeinfo
->subtype
);
135 g_free(mimeinfo
->description
);
136 g_free(mimeinfo
->id
);
137 g_free(mimeinfo
->location
);
139 g_hash_table_foreach_remove(mimeinfo
->typeparameters
,
140 procmime_mimeinfo_parameters_destroy
, NULL
);
141 g_hash_table_destroy(mimeinfo
->typeparameters
);
142 g_hash_table_foreach_remove(mimeinfo
->dispositionparameters
,
143 procmime_mimeinfo_parameters_destroy
, NULL
);
144 g_hash_table_destroy(mimeinfo
->dispositionparameters
);
146 if (mimeinfo
->privacy
)
147 privacy_free_privacydata(mimeinfo
->privacy
);
149 if (mimeinfo
->sig_data
)
150 privacy_free_signature_data(mimeinfo
->sig_data
);
157 void procmime_mimeinfo_free_all(MimeInfo
**mimeinfo_ptr
)
159 MimeInfo
*mimeinfo
= *mimeinfo_ptr
;
165 node
= mimeinfo
->node
;
166 g_node_traverse(node
, G_IN_ORDER
, G_TRAVERSE_ALL
, -1, free_func
, NULL
);
168 g_node_destroy(node
);
170 *mimeinfo_ptr
= NULL
;
173 MimeInfo
*procmime_mimeinfo_parent(MimeInfo
*mimeinfo
)
175 cm_return_val_if_fail(mimeinfo
!= NULL
, NULL
);
176 cm_return_val_if_fail(mimeinfo
->node
!= NULL
, NULL
);
178 if (mimeinfo
->node
->parent
== NULL
)
180 return (MimeInfo
*) mimeinfo
->node
->parent
->data
;
183 MimeInfo
*procmime_mimeinfo_next(MimeInfo
*mimeinfo
)
185 cm_return_val_if_fail(mimeinfo
!= NULL
, NULL
);
186 cm_return_val_if_fail(mimeinfo
->node
!= NULL
, NULL
);
188 if (mimeinfo
->node
->children
)
189 return (MimeInfo
*) mimeinfo
->node
->children
->data
;
190 if (mimeinfo
->node
->next
)
191 return (MimeInfo
*) mimeinfo
->node
->next
->data
;
193 if (mimeinfo
->node
->parent
== NULL
)
196 while (mimeinfo
->node
->parent
!= NULL
) {
197 mimeinfo
= (MimeInfo
*) mimeinfo
->node
->parent
->data
;
198 if (mimeinfo
->node
->next
)
199 return (MimeInfo
*) mimeinfo
->node
->next
->data
;
205 MimeInfo
*procmime_scan_message(MsgInfo
*msginfo
)
210 filename
= procmsg_get_message_file_path(msginfo
);
211 if (!filename
|| !is_file_exist(filename
)) {
216 if (!folder_has_parent_of_type(msginfo
->folder
, F_QUEUE
) &&
217 !folder_has_parent_of_type(msginfo
->folder
, F_DRAFT
))
218 mimeinfo
= procmime_scan_file(filename
);
220 mimeinfo
= procmime_scan_queue_file(filename
);
226 MimeInfo
*procmime_scan_message_short(MsgInfo
*msginfo
)
231 filename
= procmsg_get_message_file_path(msginfo
);
232 if (!filename
|| !is_file_exist(filename
)) {
237 if (!folder_has_parent_of_type(msginfo
->folder
, F_QUEUE
) &&
238 !folder_has_parent_of_type(msginfo
->folder
, F_DRAFT
))
239 mimeinfo
= procmime_scan_file_short(filename
);
241 mimeinfo
= procmime_scan_queue_file_short(filename
);
249 H_CONTENT_TRANSFER_ENCODING
= 0,
251 H_CONTENT_DISPOSITION
= 2,
252 H_CONTENT_DESCRIPTION
= 3,
256 const gchar
*procmime_mimeinfo_get_parameter(MimeInfo
*mimeinfo
, const gchar
*name
)
260 cm_return_val_if_fail(mimeinfo
!= NULL
, NULL
);
261 cm_return_val_if_fail(name
!= NULL
, NULL
);
263 value
= g_hash_table_lookup(mimeinfo
->dispositionparameters
, name
);
265 value
= g_hash_table_lookup(mimeinfo
->typeparameters
, name
);
270 #define FLUSH_LASTLINE() { \
271 if (*lastline != '\0') { \
273 strretchomp(lastline); \
274 llen = strlen(lastline); \
275 if (lastline[llen-1] == ' ' && !account_sigsep_matchlist_str_found(lastline, "%s") && \
276 !(llen == 2 && lastline[1] == ' ' && strchr(prefs_common.quote_chars, lastline[0]))) { \
277 /* this is flowed */ \
279 lastline[llen-1] = '\0'; \
280 if (claws_fputs(lastline, outfp) == EOF) \
283 if (claws_fputs(lastline, outfp) == EOF) \
285 if (claws_fputs("\n", outfp) == EOF) \
289 strcpy(lastline, buf); \
292 gboolean
procmime_decode_content(MimeInfo
*mimeinfo
)
299 gboolean tmp_file
= FALSE
;
300 gboolean flowed
= FALSE
;
301 gboolean delsp
= FALSE
;
302 gboolean err
= FALSE
;
306 cm_return_val_if_fail(mimeinfo
!= NULL
, FALSE
);
308 EncodingType encoding
= forced_encoding
310 : mimeinfo
->encoding_type
;
311 gchar lastline
[BUFFSIZE
];
312 memset(lastline
, 0, BUFFSIZE
);
314 if (prefs_common
.respect_flowed_format
&&
315 mimeinfo
->type
== MIMETYPE_TEXT
&&
316 !strcasecmp(mimeinfo
->subtype
, "plain")) {
317 if (procmime_mimeinfo_get_parameter(mimeinfo
, "format") != NULL
&&
318 !strcasecmp(procmime_mimeinfo_get_parameter(mimeinfo
, "format"),"flowed"))
321 procmime_mimeinfo_get_parameter(mimeinfo
, "delsp") != NULL
&&
322 !strcasecmp(procmime_mimeinfo_get_parameter(mimeinfo
, "delsp"),"yes"))
327 encoding
== ENC_UNKNOWN
||
328 encoding
== ENC_BINARY
||
329 encoding
== ENC_7BIT
||
334 if (mimeinfo
->type
== MIMETYPE_MULTIPART
|| mimeinfo
->type
== MIMETYPE_MESSAGE
)
337 if (mimeinfo
->data
.filename
== NULL
)
340 infp
= claws_fopen(mimeinfo
->data
.filename
, "rb");
342 FILE_OP_ERROR(mimeinfo
->data
.filename
, "claws_fopen");
345 if (fseek(infp
, mimeinfo
->offset
, SEEK_SET
) < 0) {
346 FILE_OP_ERROR(mimeinfo
->data
.filename
, "fseek");
351 outfp
= get_tmpfile_in_dir(get_mime_tmp_dir(), &tmpfilename
);
360 readend
= mimeinfo
->offset
+ mimeinfo
->length
;
362 account_sigsep_matchlist_create(); /* FLUSH_LASTLINE will use it */
365 if (encoding
== ENC_QUOTED_PRINTABLE
) {
366 while ((ftell(infp
) < readend
) && (claws_fgets(buf
, sizeof(buf
), infp
) != NULL
)) {
368 len
= qp_decode_line(buf
);
371 if (claws_fwrite(buf
, 1, len
, outfp
) < len
)
379 } else if (encoding
== ENC_BASE64
) {
380 gchar outbuf
[BUFFSIZE
+ 1];
381 gint len
, inlen
, inread
;
382 gboolean got_error
= FALSE
;
383 gboolean uncanonicalize
= FALSE
;
385 gboolean null_bytes
= FALSE
;
386 gboolean starting
= TRUE
;
388 if (mimeinfo
->type
== MIMETYPE_TEXT
||
389 mimeinfo
->type
== MIMETYPE_MESSAGE
) {
390 uncanonicalize
= TRUE
;
391 tmpfp
= my_tmpfile();
393 perror("my_tmpfile");
403 while ((inlen
= MIN(readend
- ftell(infp
), sizeof(buf
))) > 0 && !err
) {
404 inread
= claws_fread(buf
, 1, inlen
, infp
);
405 memset(outbuf
, 0, sizeof(buf
));
406 len
= g_base64_decode_step(buf
, inlen
, outbuf
, &state
, &save
);
407 if (uncanonicalize
== TRUE
&& strlen(outbuf
) < len
&& starting
) {
408 uncanonicalize
= FALSE
;
412 if (((inread
!= inlen
) || len
< 0) && !got_error
) {
413 g_warning("bad BASE64 content");
414 if (claws_fwrite(_("[Error decoding BASE64]\n"),
416 strlen(_("[Error decoding BASE64]\n")),
417 tmpfp
) < strlen(_("[Error decoding BASE64]\n")))
418 g_warning("error decoding BASE64");
421 } else if (len
>= 0) {
422 /* print out the error message only once
425 /* we won't uncanonicalize, output to outfp directly */
426 if (claws_fwrite(outbuf
, sizeof(gchar
), len
, outfp
) < len
)
429 if (claws_fwrite(outbuf
, sizeof(gchar
), len
, tmpfp
) < len
)
436 if (uncanonicalize
) {
438 while (claws_fgets(buf
, sizeof(buf
), tmpfp
) != NULL
) {
440 if (claws_fputs(buf
, outfp
) == EOF
)
444 if (tmpfp
!= outfp
) {
445 ftruncate(fileno(tmpfp
), ftell(tmpfp
));
448 } else if (encoding
== ENC_X_UUENCODE
) {
449 gchar outbuf
[BUFFSIZE
];
451 gboolean flag
= FALSE
;
453 while ((ftell(infp
) < readend
) && (claws_fgets(buf
, sizeof(buf
), infp
) != NULL
)) {
454 if (!flag
&& strncmp(buf
,"begin ", 6)) continue;
457 len
= fromuutobits(outbuf
, buf
);
460 g_warning("bad UUENCODE content (%d)", len
);
463 if (claws_fwrite(outbuf
, sizeof(gchar
), len
, outfp
) < len
)
469 while ((ftell(infp
) < readend
) && (claws_fgets(buf
, sizeof(buf
), infp
) != NULL
)) {
471 if (claws_fputs(buf
, outfp
) == EOF
)
480 g_warning("write error");
483 ftruncate(fileno(outfp
), ftell(outfp
));
487 account_sigsep_matchlist_delete();
494 if (g_stat(tmpfilename
, &statbuf
) < 0) {
495 FILE_OP_ERROR(tmpfilename
, "stat");
501 claws_unlink(mimeinfo
->data
.filename
);
502 g_free(mimeinfo
->data
.filename
);
503 mimeinfo
->data
.filename
= tmpfilename
;
504 mimeinfo
->tmp
= TRUE
;
505 mimeinfo
->offset
= 0;
506 mimeinfo
->length
= statbuf
.st_size
;
507 mimeinfo
->encoding_type
= ENC_BINARY
;
512 #define B64_LINE_SIZE 57
513 #define B64_BUFFSIZE 77
515 gboolean
procmime_encode_content(MimeInfo
*mimeinfo
, EncodingType encoding
)
517 FILE *infp
= NULL
, *outfp
;
521 gboolean err
= FALSE
;
523 if (mimeinfo
->content
== MIMECONTENT_EMPTY
)
526 if (mimeinfo
->encoding_type
!= ENC_UNKNOWN
&&
527 mimeinfo
->encoding_type
!= ENC_BINARY
&&
528 mimeinfo
->encoding_type
!= ENC_7BIT
&&
529 mimeinfo
->encoding_type
!= ENC_8BIT
)
530 if(!procmime_decode_content(mimeinfo
))
533 outfp
= get_tmpfile_in_dir(get_mime_tmp_dir(), &tmpfilename
);
540 if (mimeinfo
->content
== MIMECONTENT_FILE
&& mimeinfo
->data
.filename
) {
541 if ((infp
= claws_fopen(mimeinfo
->data
.filename
, "rb")) == NULL
) {
542 g_warning("can't open file %s", mimeinfo
->data
.filename
);
547 } else if (mimeinfo
->content
== MIMECONTENT_MEM
) {
548 infp
= str_open_as_stream(mimeinfo
->data
.mem
);
557 g_warning("unknown mimeinfo");
561 if (encoding
== ENC_BASE64
) {
562 gchar inbuf
[B64_LINE_SIZE
], *out
;
564 gchar
*tmp_file
= NULL
;
566 if (mimeinfo
->type
== MIMETYPE_TEXT
||
567 mimeinfo
->type
== MIMETYPE_MESSAGE
) {
568 if (mimeinfo
->content
== MIMECONTENT_FILE
) {
569 tmp_file
= get_tmp_file();
570 if (canonicalize_file(mimeinfo
->data
.filename
, tmp_file
) < 0) {
577 if ((tmp_fp
= claws_fopen(tmp_file
, "rb")) == NULL
) {
578 FILE_OP_ERROR(tmp_file
, "claws_fopen");
579 claws_unlink(tmp_file
);
587 gchar
*out
= canonicalize_str(mimeinfo
->data
.mem
);
589 infp
= str_open_as_stream(out
);
600 while ((len
= claws_fread(inbuf
, sizeof(gchar
),
601 B64_LINE_SIZE
, tmp_fp
))
603 out
= g_base64_encode(inbuf
, B64_LINE_SIZE
);
604 if (claws_fputs(out
, outfp
) == EOF
)
607 if (claws_fputc('\n', outfp
) == EOF
)
610 if (len
> 0 && claws_feof(tmp_fp
)) {
611 out
= g_base64_encode(inbuf
, len
);
612 if (claws_fputs(out
, outfp
) == EOF
)
615 if (claws_fputc('\n', outfp
) == EOF
)
620 claws_fclose(tmp_fp
);
621 claws_unlink(tmp_file
);
624 } else if (encoding
== ENC_QUOTED_PRINTABLE
) {
625 gchar inbuf
[BUFFSIZE
], outbuf
[BUFFSIZE
* 4];
627 while (claws_fgets(inbuf
, sizeof(inbuf
), infp
) != NULL
) {
628 qp_encode_line(outbuf
, inbuf
);
630 if (!strncmp("From ", outbuf
, sizeof("From ")-1)) {
631 gchar
*tmpbuf
= outbuf
;
633 tmpbuf
+= sizeof("From ")-1;
635 if (claws_fputs("=46rom ", outfp
) == EOF
)
637 if (claws_fputs(tmpbuf
, outfp
) == EOF
)
640 if (claws_fputs(outbuf
, outfp
) == EOF
)
647 while (claws_fgets(buf
, sizeof(buf
), infp
) != NULL
) {
649 if (claws_fputs(buf
, outfp
) == EOF
)
662 if (mimeinfo
->content
== MIMECONTENT_FILE
) {
663 if (mimeinfo
->tmp
&& (mimeinfo
->data
.filename
!= NULL
))
664 claws_unlink(mimeinfo
->data
.filename
);
665 g_free(mimeinfo
->data
.filename
);
666 } else if (mimeinfo
->content
== MIMECONTENT_MEM
) {
667 if (mimeinfo
->tmp
&& (mimeinfo
->data
.mem
!= NULL
))
668 g_free(mimeinfo
->data
.mem
);
671 if (g_stat(tmpfilename
, &statbuf
) < 0) {
672 FILE_OP_ERROR(tmpfilename
, "stat");
676 mimeinfo
->content
= MIMECONTENT_FILE
;
677 mimeinfo
->data
.filename
= tmpfilename
;
678 mimeinfo
->tmp
= TRUE
;
679 mimeinfo
->offset
= 0;
680 mimeinfo
->length
= statbuf
.st_size
;
681 mimeinfo
->encoding_type
= encoding
;
686 static gint
procmime_get_part_to_stream(FILE *outfp
, MimeInfo
*mimeinfo
)
690 gint restlength
, readlength
;
691 gint saved_errno
= 0;
693 cm_return_val_if_fail(outfp
!= NULL
, -1);
694 cm_return_val_if_fail(mimeinfo
!= NULL
, -1);
696 if (mimeinfo
->encoding_type
!= ENC_BINARY
&& !procmime_decode_content(mimeinfo
))
699 if ((infp
= claws_fopen(mimeinfo
->data
.filename
, "rb")) == NULL
) {
701 FILE_OP_ERROR(mimeinfo
->data
.filename
, "claws_fopen");
702 return -(saved_errno
);
704 if (fseek(infp
, mimeinfo
->offset
, SEEK_SET
) < 0) {
706 FILE_OP_ERROR(mimeinfo
->data
.filename
, "fseek");
708 return -(saved_errno
);
711 restlength
= mimeinfo
->length
;
713 while ((restlength
> 0) && ((readlength
= claws_fread(buf
, 1, restlength
> BUFFSIZE
? BUFFSIZE
: restlength
, infp
)) > 0)) {
714 if (claws_fwrite(buf
, 1, readlength
, outfp
) != readlength
) {
717 return -(saved_errno
);
719 restlength
-= readlength
;
728 gint
procmime_get_part(const gchar
*outfile
, MimeInfo
*mimeinfo
)
732 gint saved_errno
= 0;
734 cm_return_val_if_fail(outfile
!= NULL
, -1);
736 if ((outfp
= claws_fopen(outfile
, "wb")) == NULL
) {
738 FILE_OP_ERROR(outfile
, "claws_fopen");
739 return -(saved_errno
);
742 if (change_file_mode_rw(outfp
, outfile
) < 0) {
743 FILE_OP_ERROR(outfile
, "chmod");
744 g_warning("can't change file mode: %s", outfile
);
747 result
= procmime_get_part_to_stream(outfp
, mimeinfo
);
749 if (claws_fclose(outfp
) == EOF
) {
751 FILE_OP_ERROR(outfile
, "claws_fclose");
752 if (claws_unlink(outfile
) < 0)
753 FILE_OP_ERROR(outfile
, "claws_unlink");
754 return -(saved_errno
);
760 gboolean
procmime_scan_text_content(MimeInfo
*mimeinfo
,
761 gboolean (*scan_callback
)(const gchar
*str
, gpointer cb_data
),
765 const gchar
*src_codeset
;
766 gboolean conv_fail
= FALSE
;
769 gboolean scan_ret
= FALSE
;
770 gchar
*tmpfile
= NULL
;
773 cm_return_val_if_fail(mimeinfo
!= NULL
, TRUE
);
774 cm_return_val_if_fail(scan_callback
!= NULL
, TRUE
);
776 if (!procmime_decode_content(mimeinfo
))
780 tmpfp
= fmemopen(NULL
, mimeinfo
->length
* 2, "w+");
782 tmpfile
= procmime_get_tmp_file_name(mimeinfo
);
783 if (tmpfile
== NULL
) {
784 g_warning("no filename");
788 tmpfp
= claws_fopen(tmpfile
, "w+");
793 FILE_OP_ERROR(tmpfile
, "open");
797 if ((r
= procmime_get_part_to_stream(tmpfp
, mimeinfo
)) < 0) {
798 g_warning("procmime_get_part_to_stream error %d", r
);
803 src_codeset
= forced_charset
805 procmime_mimeinfo_get_parameter(mimeinfo
, "charset");
807 /* use supersets transparently when possible */
808 if (!forced_charset
&& src_codeset
&& !strcasecmp(src_codeset
, CS_ISO_8859_1
))
809 src_codeset
= CS_WINDOWS_1252
;
810 else if (!forced_charset
&& src_codeset
&& !strcasecmp(src_codeset
, CS_X_GBK
))
811 src_codeset
= CS_GB18030
;
812 else if (!forced_charset
&& src_codeset
&& !strcasecmp(src_codeset
, CS_GBK
))
813 src_codeset
= CS_GB18030
;
814 else if (!forced_charset
&& src_codeset
&& !strcasecmp(src_codeset
, CS_GB2312
))
815 src_codeset
= CS_GB18030
;
816 else if (!forced_charset
&& src_codeset
&& !strcasecmp(src_codeset
, CS_X_VIET_VPS
))
817 src_codeset
= CS_WINDOWS_874
;
819 if (mimeinfo
->type
== MIMETYPE_TEXT
&& !g_ascii_strcasecmp(mimeinfo
->subtype
, "html")) {
820 SC_HTMLParser
*parser
;
823 conv
= conv_code_converter_new(src_codeset
);
824 parser
= sc_html_parser_new(tmpfp
, conv
);
825 while ((str
= sc_html_parse(parser
)) != NULL
) {
826 if ((scan_ret
= scan_callback(str
, cb_data
)) == TRUE
)
829 sc_html_parser_destroy(parser
);
830 conv_code_converter_destroy(conv
);
831 } else if (mimeinfo
->type
== MIMETYPE_TEXT
&& !g_ascii_strcasecmp(mimeinfo
->subtype
, "enriched")) {
835 conv
= conv_code_converter_new(src_codeset
);
836 parser
= ertf_parser_new(tmpfp
, conv
);
837 while ((str
= ertf_parse(parser
)) != NULL
) {
838 if ((scan_ret
= scan_callback(str
, cb_data
)) == TRUE
)
841 ertf_parser_destroy(parser
);
842 conv_code_converter_destroy(conv
);
843 } else if (mimeinfo
->type
== MIMETYPE_TEXT
&& mimeinfo
->disposition
!= DISPOSITIONTYPE_ATTACHMENT
) {
844 while (claws_fgets(buf
, sizeof(buf
), tmpfp
) != NULL
) {
845 str
= conv_codeset_strdup(buf
, src_codeset
, CS_UTF_8
);
847 if ((scan_ret
= scan_callback(str
, cb_data
)) == TRUE
) {
854 if ((scan_ret
= scan_callback(buf
, cb_data
)) == TRUE
)
861 g_warning("procmime_get_text_content(): code conversion failed");
866 claws_unlink(tmpfile
);
873 static gboolean
scan_fputs_cb(const gchar
*str
, gpointer fp
)
875 if (claws_fputs(str
, (FILE *)fp
) == EOF
)
881 FILE *procmime_get_text_content(MimeInfo
*mimeinfo
)
886 if ((outfp
= my_tmpfile()) == NULL
) {
887 perror("my_tmpfile");
891 err
= procmime_scan_text_content(mimeinfo
, scan_fputs_cb
, outfp
);
893 ftruncate(fileno(outfp
), ftell(outfp
));
903 FILE *procmime_get_binary_content(MimeInfo
*mimeinfo
)
907 gchar
*tmpfile
= NULL
;
910 cm_return_val_if_fail(mimeinfo
!= NULL
, NULL
);
912 if (!procmime_decode_content(mimeinfo
))
916 outfp
= fmemopen(NULL
, mimeinfo
->length
* 2, "w+");
918 tmpfile
= procmime_get_tmp_file_name(mimeinfo
);
919 if (tmpfile
== NULL
) {
920 g_warning("no filename");
924 outfp
= claws_fopen(tmpfile
, "w+");
926 if (tmpfile
!= NULL
) {
932 if (procmime_get_part_to_stream(outfp
, mimeinfo
) < 0) {
935 ftruncate(fileno(outfp
), ftell(outfp
));
939 /* search the first text part of (multipart) MIME message,
940 decode, convert it and output to outfp. */
941 FILE *procmime_get_first_text_content(MsgInfo
*msginfo
)
944 MimeInfo
*mimeinfo
, *partinfo
;
945 gboolean empty_ok
= FALSE
, short_scan
= TRUE
;
947 cm_return_val_if_fail(msginfo
!= NULL
, NULL
);
949 /* first we try to short-scan (for speed), refusing empty parts */
952 mimeinfo
= procmime_scan_message_short(msginfo
);
954 mimeinfo
= procmime_scan_message(msginfo
);
955 if (!mimeinfo
) return NULL
;
958 while (partinfo
&& (partinfo
->type
!= MIMETYPE_TEXT
||
959 (partinfo
->length
== 0 && !empty_ok
))) {
960 partinfo
= procmime_mimeinfo_next(partinfo
);
963 outfp
= procmime_get_text_content(partinfo
);
964 else if (!empty_ok
&& short_scan
) {
965 /* if short scan didn't find a non-empty part, rescan
966 * fully for non-empty parts
969 procmime_mimeinfo_free_all(&mimeinfo
);
971 } else if (!empty_ok
&& !short_scan
) {
972 /* if full scan didn't find a non-empty part, rescan
973 * accepting empty parts
976 procmime_mimeinfo_free_all(&mimeinfo
);
979 procmime_mimeinfo_free_all(&mimeinfo
);
985 static gboolean
find_encrypted_func(GNode
*node
, gpointer data
)
987 MimeInfo
*mimeinfo
= (MimeInfo
*) node
->data
;
988 MimeInfo
**encinfo
= (MimeInfo
**) data
;
990 if (privacy_mimeinfo_is_encrypted(mimeinfo
)) {
998 static MimeInfo
*find_encrypted_part(MimeInfo
*rootinfo
)
1000 MimeInfo
*encinfo
= NULL
;
1002 g_node_traverse(rootinfo
->node
, G_IN_ORDER
, G_TRAVERSE_ALL
, -1,
1003 find_encrypted_func
, &encinfo
);
1008 /* search the first encrypted text part of (multipart) MIME message,
1009 decode, convert it and output to outfp. */
1010 FILE *procmime_get_first_encrypted_text_content(MsgInfo
*msginfo
)
1013 MimeInfo
*mimeinfo
, *partinfo
, *encinfo
;
1015 cm_return_val_if_fail(msginfo
!= NULL
, NULL
);
1017 mimeinfo
= procmime_scan_message(msginfo
);
1022 partinfo
= mimeinfo
;
1023 if ((encinfo
= find_encrypted_part(partinfo
)) != NULL
) {
1024 debug_print("decrypting message part\n");
1025 if (privacy_mimeinfo_decrypt(encinfo
) < 0) {
1026 alertpanel_error(_("Couldn't decrypt: %s"),
1027 privacy_get_error());
1031 partinfo
= mimeinfo
;
1032 while (partinfo
&& partinfo
->type
!= MIMETYPE_TEXT
) {
1033 partinfo
= procmime_mimeinfo_next(partinfo
);
1034 if (privacy_mimeinfo_is_signed(partinfo
))
1035 procmsg_msginfo_set_flags(msginfo
, 0, MSG_SIGNED
);
1039 outfp
= procmime_get_text_content(partinfo
);
1041 procmime_mimeinfo_free_all(&mimeinfo
);
1046 gboolean
procmime_msginfo_is_encrypted(MsgInfo
*msginfo
)
1048 MimeInfo
*mimeinfo
, *partinfo
;
1049 gboolean result
= FALSE
;
1051 cm_return_val_if_fail(msginfo
!= NULL
, FALSE
);
1053 mimeinfo
= procmime_scan_message(msginfo
);
1058 partinfo
= mimeinfo
;
1059 result
= (find_encrypted_part(partinfo
) != NULL
);
1060 procmime_mimeinfo_free_all(&mimeinfo
);
1065 gchar
*procmime_get_tmp_file_name(MimeInfo
*mimeinfo
)
1067 static guint32 id
= 0;
1072 cm_return_val_if_fail(mimeinfo
!= NULL
, NULL
);
1074 g_snprintf(f_prefix
, sizeof(f_prefix
), "%08x.", id
++);
1076 if ((mimeinfo
->type
== MIMETYPE_TEXT
) && !g_ascii_strcasecmp(mimeinfo
->subtype
, "html"))
1077 base
= g_strdup("mimetmp.html");
1079 const gchar
*basetmp1
;
1082 basetmp1
= procmime_mimeinfo_get_parameter(mimeinfo
, "filename");
1083 if (basetmp1
== NULL
)
1084 basetmp1
= procmime_mimeinfo_get_parameter(mimeinfo
, "name");
1085 if (basetmp1
== NULL
)
1086 basetmp1
= "mimetmp";
1087 basetmp2
= g_path_get_basename(basetmp1
);
1088 if (*basetmp2
== '\0') {
1090 basetmp2
= g_strdup("mimetmp");
1092 base
= conv_filename_from_utf8(basetmp2
);
1094 subst_for_shellsafe_filename(base
);
1097 filename
= g_strconcat(get_mime_tmp_dir(), G_DIR_SEPARATOR_S
,
1098 f_prefix
, base
, NULL
);
1105 static GList
*mime_type_list
= NULL
;
1107 gchar
*procmime_get_mime_type(const gchar
*filename
)
1114 static GHashTable
*mime_type_table
= NULL
;
1115 MimeType
*mime_type
;
1117 if (!mime_type_table
) {
1118 mime_type_table
= procmime_get_mime_type_table();
1119 if (!mime_type_table
) return NULL
;
1123 if (filename
== NULL
)
1126 base
= g_path_get_basename(filename
);
1127 if ((p
= strrchr(base
, '.')) != NULL
)
1129 ext
= g_utf8_strdown(p
+ 1, -1);
1131 ext
= g_utf8_strdown(p
, -1);
1134 ext
= g_utf8_strdown(base
, -1);
1138 mime_type
= g_hash_table_lookup(mime_type_table
, ext
);
1141 str
= g_strconcat(mime_type
->type
, "/", mime_type
->sub_type
,
1143 debug_print("got type %s for %s\n", str
, ext
);
1150 str
= read_w32_registry_string(HKEY_CLASSES_ROOT
, ext
, REG_MIME_TYPE_VALUE
);
1151 debug_print("got type %s for %s\n", str
, ext
);
1158 static guint
procmime_str_hash(gconstpointer gptr
)
1160 guint hash_result
= 0;
1163 for (str
= gptr
; str
&& *str
; str
++) {
1164 if (isupper(*str
)) hash_result
+= (*str
+ ' ');
1165 else hash_result
+= *str
;
1171 static gint
procmime_str_equal(gconstpointer gptr1
, gconstpointer gptr2
)
1173 const char *str1
= gptr1
;
1174 const char *str2
= gptr2
;
1176 return !g_utf8_collate(str1
, str2
);
1179 static GHashTable
*procmime_get_mime_type_table(void)
1181 GHashTable
*table
= NULL
;
1183 MimeType
*mime_type
;
1186 if (!mime_type_list
) {
1187 mime_type_list
= procmime_get_mime_type_list();
1188 if (!mime_type_list
) return NULL
;
1191 table
= g_hash_table_new(procmime_str_hash
, procmime_str_equal
);
1193 for (cur
= mime_type_list
; cur
!= NULL
; cur
= cur
->next
) {
1197 mime_type
= (MimeType
*)cur
->data
;
1199 if (!mime_type
->extension
) continue;
1201 exts
= g_strsplit(mime_type
->extension
, " ", 16);
1202 for (i
= 0; exts
[i
] != NULL
; i
++) {
1203 /* Don't overwrite previously inserted extension */
1204 if (!g_hash_table_lookup(table
, exts
[i
])) {
1205 key
= g_strdup(exts
[i
]);
1206 g_hash_table_insert(table
, key
, mime_type
);
1216 GList
*procmime_get_mime_type_list(void)
1220 gchar buf
[BUFFSIZE
];
1223 MimeType
*mime_type
;
1224 gboolean fp_is_glob_file
= TRUE
;
1227 return mime_type_list
;
1229 #if defined(__NetBSD__) || defined(__OpenBSD__) || defined(__FreeBSD__)
1230 if ((fp
= claws_fopen(DATAROOTDIR
"/mime/globs", "rb")) == NULL
)
1232 if ((fp
= claws_fopen("/usr/share/mime/globs", "rb")) == NULL
)
1235 fp_is_glob_file
= FALSE
;
1236 if ((fp
= claws_fopen("/etc/mime.types", "rb")) == NULL
) {
1237 if ((fp
= claws_fopen(SYSCONFDIR
"/mime.types", "rb"))
1239 FILE_OP_ERROR(SYSCONFDIR
"/mime.types",
1246 while (claws_fgets(buf
, sizeof(buf
), fp
) != NULL
) {
1247 p
= strchr(buf
, '#');
1253 if (fp_is_glob_file
) {
1254 while (*p
&& !g_ascii_isspace(*p
) && (*p
!=':')) p
++;
1256 while (*p
&& !g_ascii_isspace(*p
)) p
++;
1263 delim
= strchr(buf
, '/');
1264 if (delim
== NULL
) continue;
1267 mime_type
= g_new(MimeType
, 1);
1268 mime_type
->type
= g_strdup(buf
);
1269 mime_type
->sub_type
= g_strdup(delim
+ 1);
1271 if (fp_is_glob_file
) {
1272 while (*p
&& (g_ascii_isspace(*p
)||(*p
=='*')||(*p
=='.'))) p
++;
1274 while (*p
&& g_ascii_isspace(*p
)) p
++;
1278 mime_type
->extension
= g_utf8_strdown(p
, -1);
1280 mime_type
->extension
= NULL
;
1282 list
= g_list_append(list
, mime_type
);
1288 g_warning("can't read mime.types");
1293 EncodingType
procmime_get_encoding_for_charset(const gchar
*charset
)
1297 else if (!g_ascii_strncasecmp(charset
, "ISO-2022-", 9) ||
1298 !g_ascii_strcasecmp(charset
, "US-ASCII"))
1300 else if (!g_ascii_strcasecmp(charset
, "ISO-8859-5") ||
1301 !g_ascii_strncasecmp(charset
, "KOI8-", 5) ||
1302 !g_ascii_strcasecmp(charset
, "X-MAC-CYRILLIC") ||
1303 !g_ascii_strcasecmp(charset
, "MAC-CYRILLIC") ||
1304 !g_ascii_strcasecmp(charset
, "Windows-1251"))
1306 else if (!g_ascii_strncasecmp(charset
, "ISO-8859-", 9))
1307 return ENC_QUOTED_PRINTABLE
;
1308 else if (!g_ascii_strncasecmp(charset
, "UTF-8", 5))
1309 return ENC_QUOTED_PRINTABLE
;
1314 EncodingType
procmime_get_encoding_for_text_file(const gchar
*file
, gboolean
*has_binary
)
1317 guchar buf
[BUFFSIZE
];
1319 size_t octet_chars
= 0;
1320 size_t total_len
= 0;
1321 gfloat octet_percentage
;
1322 gboolean force_b64
= FALSE
;
1324 if ((fp
= claws_fopen(file
, "rb")) == NULL
) {
1325 FILE_OP_ERROR(file
, "claws_fopen");
1329 while ((len
= claws_fread(buf
, sizeof(guchar
), sizeof(buf
), fp
)) > 0) {
1333 for (p
= buf
, i
= 0; i
< len
; ++p
, ++i
) {
1347 octet_percentage
= (gfloat
)octet_chars
/ (gfloat
)total_len
;
1349 octet_percentage
= 0.0;
1351 debug_print("procmime_get_encoding_for_text_file(): "
1352 "8bit chars: %"G_GSIZE_FORMAT
" / %"G_GSIZE_FORMAT
" (%f%%)\n", octet_chars
, total_len
,
1353 100.0 * octet_percentage
);
1355 if (octet_percentage
> 0.20 || force_b64
) {
1356 debug_print("using BASE64\n");
1358 } else if (octet_chars
> 0) {
1359 debug_print("using quoted-printable\n");
1360 return ENC_QUOTED_PRINTABLE
;
1362 debug_print("using 7bit\n");
1367 struct EncodingTable
1370 EncodingType enc_type
;
1373 struct EncodingTable encoding_table
[] = {
1376 {"binary", ENC_BINARY
},
1377 {"quoted-printable", ENC_QUOTED_PRINTABLE
},
1378 {"base64", ENC_BASE64
},
1379 {"x-uuencode", ENC_UNKNOWN
},
1380 {NULL
, ENC_UNKNOWN
},
1383 const gchar
*procmime_get_encoding_str(EncodingType encoding
)
1385 struct EncodingTable
*enc_table
;
1387 for (enc_table
= encoding_table
; enc_table
->str
!= NULL
; enc_table
++) {
1388 if (enc_table
->enc_type
== encoding
)
1389 return enc_table
->str
;
1394 /* --- NEW MIME STUFF --- */
1401 static struct TypeTable mime_type_table
[] = {
1402 {"text", MIMETYPE_TEXT
},
1403 {"image", MIMETYPE_IMAGE
},
1404 {"audio", MIMETYPE_AUDIO
},
1405 {"video", MIMETYPE_VIDEO
},
1406 {"font", MIMETYPE_FONT
},
1407 {"model", MIMETYPE_MODEL
},
1408 {"chemical", MIMETYPE_CHEMICAL
},
1409 {"application", MIMETYPE_APPLICATION
},
1410 {"message", MIMETYPE_MESSAGE
},
1411 {"multipart", MIMETYPE_MULTIPART
},
1415 const gchar
*procmime_get_media_type_str(MimeMediaType type
)
1417 struct TypeTable
*type_table
;
1419 for (type_table
= mime_type_table
; type_table
->str
!= NULL
; type_table
++) {
1420 if (type_table
->type
== type
)
1421 return type_table
->str
;
1426 MimeMediaType
procmime_get_media_type(const gchar
*str
)
1428 struct TypeTable
*typetablearray
;
1430 for (typetablearray
= mime_type_table
; typetablearray
->str
!= NULL
; typetablearray
++)
1431 if (g_ascii_strncasecmp(str
, typetablearray
->str
, strlen(typetablearray
->str
)) == 0)
1432 return typetablearray
->type
;
1434 return MIMETYPE_UNKNOWN
;
1438 *\brief Safe wrapper for content type string.
1440 *\return const gchar * Pointer to content type string.
1442 gchar
*procmime_get_content_type_str(MimeMediaType type
,
1443 const char *subtype
)
1445 const gchar
*type_str
= NULL
;
1447 if (subtype
== NULL
|| !(type_str
= procmime_get_media_type_str(type
)))
1448 return g_strdup("unknown");
1449 return g_strdup_printf("%s/%s", type_str
, subtype
);
1452 static int procmime_parse_mimepart(MimeInfo
*parent
,
1453 gchar
*content_type
,
1454 gchar
*content_encoding
,
1455 gchar
*content_description
,
1457 gchar
*content_disposition
,
1458 gchar
*content_location
,
1459 const gchar
*original_msgid
,
1460 const gchar
*disposition_notification_hdr
,
1461 const gchar
*filename
,
1464 gboolean short_scan
);
1466 static void procmime_parse_message_rfc822(MimeInfo
*mimeinfo
, gboolean short_scan
)
1468 HeaderEntry hentry
[] = {{"Content-Type:", NULL
, TRUE
},
1469 {"Content-Transfer-Encoding:",
1471 {"Content-Description:",
1475 {"Content-Disposition:",
1477 {"Content-Location:",
1481 {"Original-Message-ID:",
1485 {NULL
, NULL
, FALSE
}};
1486 guint content_start
, i
;
1491 procmime_decode_content(mimeinfo
);
1493 fp
= claws_fopen(mimeinfo
->data
.filename
, "rb");
1495 FILE_OP_ERROR(mimeinfo
->data
.filename
, "claws_fopen");
1498 if (fseek(fp
, mimeinfo
->offset
, SEEK_SET
) < 0) {
1499 FILE_OP_ERROR(mimeinfo
->data
.filename
, "fseek");
1503 procheader_get_header_fields(fp
, hentry
);
1504 if (hentry
[0].body
!= NULL
) {
1505 tmp
= conv_unmime_header(hentry
[0].body
, NULL
, FALSE
);
1506 g_free(hentry
[0].body
);
1507 hentry
[0].body
= tmp
;
1509 if (hentry
[2].body
!= NULL
) {
1510 tmp
= conv_unmime_header(hentry
[2].body
, NULL
, FALSE
);
1511 g_free(hentry
[2].body
);
1512 hentry
[2].body
= tmp
;
1514 if (hentry
[4].body
!= NULL
) {
1515 tmp
= conv_unmime_header(hentry
[4].body
, NULL
, FALSE
);
1516 g_free(hentry
[4].body
);
1517 hentry
[4].body
= tmp
;
1519 if (hentry
[5].body
!= NULL
) {
1520 tmp
= conv_unmime_header(hentry
[5].body
, NULL
, FALSE
);
1521 g_free(hentry
[5].body
);
1522 hentry
[5].body
= tmp
;
1524 if (hentry
[7].body
!= NULL
) {
1525 tmp
= conv_unmime_header(hentry
[7].body
, NULL
, FALSE
);
1526 g_free(hentry
[7].body
);
1527 hentry
[7].body
= tmp
;
1529 if (hentry
[8].body
!= NULL
) {
1530 tmp
= conv_unmime_header(hentry
[8].body
, NULL
, FALSE
);
1531 g_free(hentry
[8].body
);
1532 hentry
[8].body
= tmp
;
1535 content_start
= ftell(fp
);
1538 len
= mimeinfo
->length
- (content_start
- mimeinfo
->offset
);
1541 procmime_parse_mimepart(mimeinfo
,
1542 hentry
[0].body
, hentry
[1].body
,
1543 hentry
[2].body
, hentry
[3].body
,
1544 hentry
[4].body
, hentry
[5].body
,
1545 hentry
[7].body
, hentry
[8].body
,
1546 mimeinfo
->data
.filename
, content_start
,
1549 for (i
= 0; i
< (sizeof hentry
/ sizeof hentry
[0]); i
++) {
1550 g_free(hentry
[i
].body
);
1551 hentry
[i
].body
= NULL
;
1555 static void procmime_parse_disposition_notification(MimeInfo
*mimeinfo
,
1556 const gchar
*original_msgid
, const gchar
*disposition_notification_hdr
,
1557 gboolean short_scan
)
1559 HeaderEntry hentry
[] = {{"Original-Message-ID:", NULL
, TRUE
},
1560 {"Disposition:", NULL
, TRUE
},
1561 {NULL
, NULL
, FALSE
}};
1564 gchar
*orig_msg_id
= NULL
;
1567 procmime_decode_content(mimeinfo
);
1569 debug_print("parse disposition notification\n");
1570 fp
= claws_fopen(mimeinfo
->data
.filename
, "rb");
1572 FILE_OP_ERROR(mimeinfo
->data
.filename
, "claws_fopen");
1575 if (fseek(fp
, mimeinfo
->offset
, SEEK_SET
) < 0) {
1576 FILE_OP_ERROR(mimeinfo
->data
.filename
, "fseek");
1581 if (original_msgid
&& disposition_notification_hdr
) {
1582 hentry
[0].body
= g_strdup(original_msgid
);
1583 hentry
[1].body
= g_strdup(disposition_notification_hdr
);
1585 procheader_get_header_fields(fp
, hentry
);
1590 if (!hentry
[0].body
|| !hentry
[1].body
) {
1591 debug_print("MsgId %s, Disp %s\n",
1592 hentry
[0].body
? hentry
[0].body
:"(nil)",
1593 hentry
[1].body
? hentry
[1].body
:"(nil)");
1597 orig_msg_id
= g_strdup(hentry
[0].body
);
1598 disp
= g_strdup(hentry
[1].body
);
1600 extract_parenthesis(orig_msg_id
, '<', '>');
1601 remove_space(orig_msg_id
);
1603 if (strstr(disp
, "displayed")) {
1604 /* find sent message, if possible */
1605 MsgInfo
*info
= NULL
;
1607 debug_print("%s has been displayed.\n", orig_msg_id
);
1608 for (flist
= folder_get_list(); flist
!= NULL
; flist
= g_list_next(flist
)) {
1609 FolderItem
*outbox
= ((Folder
*)(flist
->data
))->outbox
;
1611 debug_print("skipping folder with no outbox...\n");
1614 info
= folder_item_get_msginfo_by_msgid(outbox
, orig_msg_id
);
1615 debug_print("%s %s in %s\n", info
?"found":"didn't find", orig_msg_id
, outbox
->path
);
1617 procmsg_msginfo_set_flags(info
, MSG_RETRCPT_GOT
, 0);
1618 procmsg_msginfo_free(&info
);
1622 g_free(orig_msg_id
);
1625 for (i
= 0; i
< (sizeof hentry
/ sizeof hentry
[0]); i
++) {
1626 g_free(hentry
[i
].body
);
1627 hentry
[i
].body
= NULL
;
1631 #define GET_HEADERS() { \
1632 procheader_get_header_fields(fp, hentry); \
1633 if (hentry[0].body != NULL) { \
1634 tmp = conv_unmime_header(hentry[0].body, NULL, FALSE); \
1635 g_free(hentry[0].body); \
1636 hentry[0].body = tmp; \
1638 if (hentry[2].body != NULL) { \
1639 tmp = conv_unmime_header(hentry[2].body, NULL, FALSE); \
1640 g_free(hentry[2].body); \
1641 hentry[2].body = tmp; \
1643 if (hentry[4].body != NULL) { \
1644 tmp = conv_unmime_header(hentry[4].body, NULL, FALSE); \
1645 g_free(hentry[4].body); \
1646 hentry[4].body = tmp; \
1648 if (hentry[5].body != NULL) { \
1649 tmp = conv_unmime_header(hentry[5].body, NULL, FALSE); \
1650 g_free(hentry[5].body); \
1651 hentry[5].body = tmp; \
1653 if (hentry[6].body != NULL) { \
1654 tmp = conv_unmime_header(hentry[6].body, NULL, FALSE); \
1655 g_free(hentry[6].body); \
1656 hentry[6].body = tmp; \
1658 if (hentry[7].body != NULL) { \
1659 tmp = conv_unmime_header(hentry[7].body, NULL, FALSE); \
1660 g_free(hentry[7].body); \
1661 hentry[7].body = tmp; \
1665 static void procmime_parse_multipart(MimeInfo
*mimeinfo
, gboolean short_scan
)
1667 HeaderEntry hentry
[] = {{"Content-Type:", NULL
, TRUE
},
1668 {"Content-Transfer-Encoding:",
1670 {"Content-Description:",
1674 {"Content-Disposition:",
1676 {"Content-Location:",
1678 {"Original-Message-ID:",
1682 {NULL
, NULL
, FALSE
}};
1685 gint boundary_len
= 0, lastoffset
= -1, i
;
1686 gchar buf
[BUFFSIZE
];
1689 gboolean start_found
= FALSE
;
1690 gboolean end_found
= FALSE
;
1692 boundary
= g_hash_table_lookup(mimeinfo
->typeparameters
, "boundary");
1695 boundary_len
= strlen(boundary
);
1697 procmime_decode_content(mimeinfo
);
1699 fp
= claws_fopen(mimeinfo
->data
.filename
, "rb");
1701 FILE_OP_ERROR(mimeinfo
->data
.filename
, "claws_fopen");
1705 if (fseek(fp
, mimeinfo
->offset
, SEEK_SET
) < 0) {
1706 FILE_OP_ERROR(mimeinfo
->data
.filename
, "fseek");
1711 while (claws_fgets(buf
, sizeof(buf
), fp
) != NULL
&& result
== 0) {
1712 if (ftell(fp
) - 1 > (mimeinfo
->offset
+ mimeinfo
->length
))
1715 if (IS_BOUNDARY(buf
, boundary
, boundary_len
)) {
1718 if (lastoffset
!= -1) {
1719 gint len
= (ftell(fp
) - strlen(buf
)) - lastoffset
- 1;
1722 result
= procmime_parse_mimepart(mimeinfo
,
1723 hentry
[0].body
, hentry
[1].body
,
1724 hentry
[2].body
, hentry
[3].body
,
1725 hentry
[4].body
, hentry
[5].body
,
1726 hentry
[6].body
, hentry
[7].body
,
1727 mimeinfo
->data
.filename
, lastoffset
,
1729 if (result
== 1 && short_scan
)
1734 if (buf
[2 + boundary_len
] == '-' &&
1735 buf
[2 + boundary_len
+ 1] == '-') {
1739 for (i
= 0; i
< (sizeof hentry
/ sizeof hentry
[0]) ; i
++) {
1740 g_free(hentry
[i
].body
);
1741 hentry
[i
].body
= NULL
;
1744 lastoffset
= ftell(fp
);
1748 if (start_found
&& !end_found
&& lastoffset
!= -1) {
1749 gint len
= (ftell(fp
) - strlen(buf
)) - lastoffset
- 1;
1752 result
= procmime_parse_mimepart(mimeinfo
,
1753 hentry
[0].body
, hentry
[1].body
,
1754 hentry
[2].body
, hentry
[3].body
,
1755 hentry
[4].body
, hentry
[5].body
,
1756 hentry
[6].body
, hentry
[7].body
,
1757 mimeinfo
->data
.filename
, lastoffset
,
1760 mimeinfo
->broken
= TRUE
;
1763 for (i
= 0; i
< (sizeof hentry
/ sizeof hentry
[0]); i
++) {
1764 g_free(hentry
[i
].body
);
1765 hentry
[i
].body
= NULL
;
1770 static void parse_parameters(const gchar
*parameters
, GHashTable
*table
)
1772 gchar
*params
, *param
, *next
;
1773 GSList
*convlist
= NULL
, *concatlist
= NULL
, *cur
;
1775 params
= g_strdup(parameters
);
1778 for (; next
!= NULL
; param
= next
) {
1779 gchar
*attribute
, *value
, *tmp
, *down_attr
, *orig_down_attr
;
1781 gboolean convert
= FALSE
;
1783 next
= strchr_with_skip_quote(param
, '"', ';');
1792 value
= strchr(attribute
, '=');
1798 while (value
[0] != '\0' && value
[0] == ' ')
1801 down_attr
= g_utf8_strdown(attribute
, -1);
1802 orig_down_attr
= down_attr
;
1804 len
= down_attr
? strlen(down_attr
):0;
1805 if (len
> 0 && down_attr
[len
- 1] == '*') {
1806 gchar
*srcpos
, *dstpos
, *endpos
;
1809 down_attr
[len
- 1] = '\0';
1813 endpos
= value
+ strlen(value
);
1814 while (srcpos
< endpos
) {
1820 if (!get_hex_value(&dstvalue
, srcpos
[1], srcpos
[2]))
1830 if (value
[0] == '"')
1831 extract_quote(value
, '"');
1833 if (value
[0] == '"')
1834 extract_quote(value
, '"');
1835 else if ((tmp
= strchr(value
, ' ')) != NULL
)
1840 while (down_attr
[0] == ' ')
1842 while (down_attr
[strlen(down_attr
)-1] == ' ')
1843 down_attr
[strlen(down_attr
)-1] = '\0';
1846 while (value
[0] != '\0' && value
[0] == ' ')
1848 while (value
[strlen(value
)-1] == ' ')
1849 value
[strlen(value
)-1] = '\0';
1851 if (down_attr
&& strrchr(down_attr
, '*') != NULL
) {
1854 tmpattr
= g_strdup(down_attr
);
1855 tmp
= strrchr(tmpattr
, '*');
1858 if ((tmp
[1] == '0') && (tmp
[2] == '\0') &&
1859 (g_slist_find_custom(concatlist
, down_attr
, (GCompareFunc
)g_strcmp0
) == NULL
))
1860 concatlist
= g_slist_prepend(concatlist
, g_strdup(tmpattr
));
1862 if (convert
&& (g_slist_find_custom(convlist
, tmpattr
, (GCompareFunc
)g_strcmp0
) == NULL
))
1863 convlist
= g_slist_prepend(convlist
, g_strdup(tmpattr
));
1866 } else if (convert
) {
1867 if (g_slist_find_custom(convlist
, down_attr
, (GCompareFunc
)g_strcmp0
) == NULL
)
1868 convlist
= g_slist_prepend(convlist
, g_strdup(down_attr
));
1871 if (g_hash_table_lookup(table
, down_attr
) == NULL
)
1872 g_hash_table_insert(table
, g_strdup(down_attr
), g_strdup(value
));
1873 g_free(orig_down_attr
);
1876 for (cur
= concatlist
; cur
!= NULL
; cur
= g_slist_next(cur
)) {
1877 gchar
*attribute
, *attrwnum
, *partvalue
;
1881 attribute
= (gchar
*) cur
->data
;
1882 value
= g_string_sized_new(64);
1884 attrwnum
= g_strdup_printf("%s*%d", attribute
, n
);
1885 while ((partvalue
= g_hash_table_lookup(table
, attrwnum
)) != NULL
) {
1886 g_string_append(value
, partvalue
);
1888 g_hash_table_remove(table
, attrwnum
);
1891 attrwnum
= g_strdup_printf("%s*%d", attribute
, n
);
1895 g_hash_table_insert(table
, g_strdup(attribute
), g_strdup(value
->str
));
1896 g_string_free(value
, TRUE
);
1898 slist_free_strings_full(concatlist
);
1900 for (cur
= convlist
; cur
!= NULL
; cur
= g_slist_next(cur
)) {
1901 gchar
*attribute
, *key
, *value
;
1902 gchar
*charset
, *lang
, *oldvalue
, *newvalue
;
1904 attribute
= (gchar
*) cur
->data
;
1905 if (!g_hash_table_lookup_extended(
1906 table
, attribute
, (gpointer
*)(gchar
*) &key
, (gpointer
*)(gchar
*) &value
))
1910 if (charset
== NULL
)
1912 lang
= strchr(charset
, '\'');
1917 oldvalue
= strchr(lang
, '\'');
1918 if (oldvalue
== NULL
)
1923 newvalue
= conv_codeset_strdup(oldvalue
, charset
, CS_UTF_8
);
1925 g_hash_table_remove(table
, attribute
);
1929 g_hash_table_insert(table
, g_strdup(attribute
), newvalue
);
1931 slist_free_strings_full(convlist
);
1936 static void procmime_parse_content_type(const gchar
*content_type
, MimeInfo
*mimeinfo
)
1938 cm_return_if_fail(content_type
!= NULL
);
1939 cm_return_if_fail(mimeinfo
!= NULL
);
1941 /* RFC 2045, page 13 says that the mime subtype is MANDATORY;
1942 * if it's not available we use the default Content-Type */
1943 if ((content_type
[0] == '\0') || (strchr(content_type
, '/') == NULL
)) {
1944 mimeinfo
->type
= MIMETYPE_TEXT
;
1945 mimeinfo
->subtype
= g_strdup("plain");
1946 if (g_hash_table_lookup(mimeinfo
->typeparameters
,
1947 "charset") == NULL
) {
1948 g_hash_table_insert(mimeinfo
->typeparameters
,
1949 g_strdup("charset"),
1951 conv_get_locale_charset_str_no_utf8()));
1954 gchar
*type
, *subtype
, *params
;
1956 type
= g_strdup(content_type
);
1957 subtype
= strchr(type
, '/') + 1;
1958 *(subtype
- 1) = '\0';
1959 if ((params
= strchr(subtype
, ';')) != NULL
) {
1964 mimeinfo
->type
= procmime_get_media_type(type
);
1965 mimeinfo
->subtype
= g_strstrip(g_strdup(subtype
));
1967 /* Get mimeinfo->typeparameters */
1969 parse_parameters(params
, mimeinfo
->typeparameters
);
1975 static void procmime_parse_content_disposition(const gchar
*content_disposition
, MimeInfo
*mimeinfo
)
1977 gchar
*tmp
, *params
;
1979 cm_return_if_fail(content_disposition
!= NULL
);
1980 cm_return_if_fail(mimeinfo
!= NULL
);
1982 tmp
= g_strdup(content_disposition
);
1983 if ((params
= strchr(tmp
, ';')) != NULL
) {
1989 if (!g_ascii_strcasecmp(tmp
, "inline"))
1990 mimeinfo
->disposition
= DISPOSITIONTYPE_INLINE
;
1991 else if (!g_ascii_strcasecmp(tmp
, "attachment"))
1992 mimeinfo
->disposition
= DISPOSITIONTYPE_ATTACHMENT
;
1994 mimeinfo
->disposition
= DISPOSITIONTYPE_ATTACHMENT
;
1997 parse_parameters(params
, mimeinfo
->dispositionparameters
);
2003 static void procmime_parse_content_encoding(const gchar
*content_encoding
, MimeInfo
*mimeinfo
)
2005 struct EncodingTable
*enc_table
;
2007 for (enc_table
= encoding_table
; enc_table
->str
!= NULL
; enc_table
++) {
2008 if (g_ascii_strcasecmp(enc_table
->str
, content_encoding
) == 0) {
2009 mimeinfo
->encoding_type
= enc_table
->enc_type
;
2013 mimeinfo
->encoding_type
= ENC_UNKNOWN
;
2017 static GSList
*registered_parsers
= NULL
;
2019 static MimeParser
*procmime_get_mimeparser_for_type(MimeMediaType type
, const gchar
*sub_type
)
2022 for (cur
= registered_parsers
; cur
; cur
= cur
->next
) {
2023 MimeParser
*parser
= (MimeParser
*)cur
->data
;
2024 if (parser
->type
== type
&& !g_strcmp0(parser
->sub_type
, sub_type
))
2030 void procmime_mimeparser_register(MimeParser
*parser
)
2032 if (!procmime_get_mimeparser_for_type(parser
->type
, parser
->sub_type
))
2033 registered_parsers
= g_slist_append(registered_parsers
, parser
);
2037 void procmime_mimeparser_unregister(MimeParser
*parser
)
2039 registered_parsers
= g_slist_remove(registered_parsers
, parser
);
2042 static gboolean
procmime_mimeparser_parse(MimeParser
*parser
, MimeInfo
*mimeinfo
)
2044 cm_return_val_if_fail(parser
->parse
!= NULL
, FALSE
);
2045 return parser
->parse(parser
, mimeinfo
);
2048 static int procmime_parse_mimepart(MimeInfo
*parent
,
2049 gchar
*content_type
,
2050 gchar
*content_encoding
,
2051 gchar
*content_description
,
2053 gchar
*content_disposition
,
2054 gchar
*content_location
,
2055 const gchar
*original_msgid
,
2056 const gchar
*disposition_notification_hdr
,
2057 const gchar
*filename
,
2060 gboolean short_scan
)
2063 MimeParser
*parser
= NULL
;
2064 gboolean parsed
= FALSE
;
2067 /* Create MimeInfo */
2068 mimeinfo
= procmime_mimeinfo_new();
2069 mimeinfo
->content
= MIMECONTENT_FILE
;
2071 if (parent
!= NULL
) {
2072 if (g_node_depth(parent
->node
) > 32) {
2073 /* 32 is an arbitrary value
2074 * this avoids DOSsing ourselves
2075 * with enormous messages
2077 procmime_mimeinfo_free_all(&mimeinfo
);
2080 g_node_append(parent
->node
, mimeinfo
->node
);
2082 mimeinfo
->data
.filename
= g_strdup(filename
);
2083 mimeinfo
->offset
= offset
;
2084 mimeinfo
->length
= length
;
2086 if (content_type
!= NULL
) {
2087 g_strchomp(content_type
);
2088 procmime_parse_content_type(content_type
, mimeinfo
);
2090 mimeinfo
->type
= MIMETYPE_TEXT
;
2091 mimeinfo
->subtype
= g_strdup("plain");
2092 if (g_hash_table_lookup(mimeinfo
->typeparameters
,
2093 "charset") == NULL
) {
2094 g_hash_table_insert(mimeinfo
->typeparameters
,
2095 g_strdup("charset"),
2097 conv_get_locale_charset_str_no_utf8()));
2101 if (content_encoding
!= NULL
) {
2102 g_strchomp(content_encoding
);
2103 procmime_parse_content_encoding(content_encoding
, mimeinfo
);
2105 mimeinfo
->encoding_type
= ENC_UNKNOWN
;
2108 if (content_description
!= NULL
)
2109 mimeinfo
->description
= g_strdup(content_description
);
2111 mimeinfo
->description
= NULL
;
2113 if (content_id
!= NULL
)
2114 mimeinfo
->id
= g_strdup(content_id
);
2116 mimeinfo
->id
= NULL
;
2118 if (content_location
!= NULL
)
2119 mimeinfo
->location
= g_strdup(content_location
);
2121 mimeinfo
->location
= NULL
;
2123 if (content_disposition
!= NULL
) {
2124 g_strchomp(content_disposition
);
2125 procmime_parse_content_disposition(content_disposition
, mimeinfo
);
2127 mimeinfo
->disposition
= DISPOSITIONTYPE_UNKNOWN
;
2129 /* Call parser for mime type */
2130 if ((parser
= procmime_get_mimeparser_for_type(mimeinfo
->type
, mimeinfo
->subtype
)) != NULL
) {
2131 parsed
= procmime_mimeparser_parse(parser
, mimeinfo
);
2134 switch (mimeinfo
->type
) {
2136 if (g_ascii_strcasecmp(mimeinfo
->subtype
, "plain") == 0 && short_scan
) {
2141 case MIMETYPE_MESSAGE
:
2142 if (g_ascii_strcasecmp(mimeinfo
->subtype
, "rfc822") == 0) {
2143 procmime_parse_message_rfc822(mimeinfo
, short_scan
);
2145 if (g_ascii_strcasecmp(mimeinfo
->subtype
, "disposition-notification") == 0) {
2146 procmime_parse_disposition_notification(mimeinfo
,
2147 original_msgid
, disposition_notification_hdr
, short_scan
);
2151 case MIMETYPE_MULTIPART
:
2152 procmime_parse_multipart(mimeinfo
, short_scan
);
2155 case MIMETYPE_APPLICATION
:
2156 if (g_ascii_strcasecmp(mimeinfo
->subtype
, "octet-stream") == 0
2157 && original_msgid
&& *original_msgid
2158 && disposition_notification_hdr
&& *disposition_notification_hdr
) {
2159 procmime_parse_disposition_notification(mimeinfo
,
2160 original_msgid
, disposition_notification_hdr
, short_scan
);
2171 static gchar
*typenames
[] = {
2185 static gboolean
output_func(GNode
*node
, gpointer data
)
2188 MimeInfo
*mimeinfo
= (MimeInfo
*) node
->data
;
2190 depth
= g_node_depth(node
);
2191 for (i
= 0; i
< depth
; i
++)
2193 g_print("%s/%s (offset:%d length:%d encoding: %d)\n",
2194 (mimeinfo
->type
<= MIMETYPE_UNKNOWN
)? typenames
[mimeinfo
->type
] : "unknown",
2195 mimeinfo
->subtype
, mimeinfo
->offset
, mimeinfo
->length
, mimeinfo
->encoding_type
);
2200 static void output_mime_structure(MimeInfo
*mimeinfo
, int indent
)
2202 g_node_traverse(mimeinfo
->node
, G_PRE_ORDER
, G_TRAVERSE_ALL
, -1, output_func
, NULL
);
2205 static MimeInfo
*procmime_scan_file_with_offset(const gchar
*filename
, int offset
, gboolean short_scan
)
2210 if (g_stat(filename
, &buf
) < 0) {
2211 FILE_OP_ERROR(filename
, "stat");
2215 mimeinfo
= procmime_mimeinfo_new();
2216 mimeinfo
->content
= MIMECONTENT_FILE
;
2217 mimeinfo
->encoding_type
= ENC_UNKNOWN
;
2218 mimeinfo
->type
= MIMETYPE_MESSAGE
;
2219 mimeinfo
->subtype
= g_strdup("rfc822");
2220 mimeinfo
->data
.filename
= g_strdup(filename
);
2221 mimeinfo
->offset
= offset
;
2222 mimeinfo
->length
= buf
.st_size
- offset
;
2224 procmime_parse_message_rfc822(mimeinfo
, short_scan
);
2225 if (debug_get_mode())
2226 output_mime_structure(mimeinfo
, 0);
2231 static MimeInfo
*procmime_scan_file_full(const gchar
*filename
, gboolean short_scan
)
2235 cm_return_val_if_fail(filename
!= NULL
, NULL
);
2237 mimeinfo
= procmime_scan_file_with_offset(filename
, 0, short_scan
);
2242 MimeInfo
*procmime_scan_file(const gchar
*filename
)
2244 return procmime_scan_file_full(filename
, FALSE
);
2247 static MimeInfo
*procmime_scan_file_short(const gchar
*filename
)
2249 return procmime_scan_file_full(filename
, TRUE
);
2252 static MimeInfo
*procmime_scan_queue_file_full(const gchar
*filename
, gboolean short_scan
)
2256 gchar buf
[BUFFSIZE
];
2259 cm_return_val_if_fail(filename
!= NULL
, NULL
);
2262 if ((fp
= claws_fopen(filename
, "rb")) == NULL
)
2264 /* Skip queue header */
2265 while (claws_fgets(buf
, sizeof(buf
), fp
) != NULL
) {
2267 if ((!strncmp(buf
, "X-Claws-End-Special-Headers: 1",
2268 strlen("X-Claws-End-Special-Headers:"))) ||
2269 (!strncmp(buf
, "X-Sylpheed-End-Special-Headers: 1",
2270 strlen("X-Sylpheed-End-Special-Headers:"))))
2273 if (buf
[0] == '\r' || buf
[0] == '\n') break;
2274 /* from other mailers */
2275 if (!strncmp(buf
, "Date: ", 6)
2276 || !strncmp(buf
, "To: ", 4)
2277 || !strncmp(buf
, "From: ", 6)
2278 || !strncmp(buf
, "Subject: ", 9)) {
2286 mimeinfo
= procmime_scan_file_with_offset(filename
, offset
, short_scan
);
2291 MimeInfo
*procmime_scan_queue_file(const gchar
*filename
)
2293 return procmime_scan_queue_file_full(filename
, FALSE
);
2296 static MimeInfo
*procmime_scan_queue_file_short(const gchar
*filename
)
2298 return procmime_scan_queue_file_full(filename
, TRUE
);
2303 ENC_AS_QUOTED_STRING
,
2308 typedef struct _ParametersData
{
2314 static void write_parameters(gpointer key
, gpointer value
, gpointer user_data
)
2317 gchar
*val
= value
, *valpos
, *tmp
;
2318 ParametersData
*pdata
= (ParametersData
*)user_data
;
2319 GString
*buf
= g_string_new("");
2322 EncodeAs encas
= ENC_AS_TOKEN
;
2324 for (valpos
= val
; *valpos
!= 0; valpos
++) {
2325 if (!IS_ASCII(*valpos
)) {
2326 encas
= ENC_AS_ENCWORD
;
2331 if (((*valpos
>= 0) && (*valpos
< 037)) || (*valpos
== 0177)) {
2332 encas
= ENC_AS_QUOTED_STRING
;
2336 /* tspecials + SPACE */
2354 encas
= ENC_AS_QUOTED_STRING
;
2361 g_string_append_printf(buf
, "%s=%s", param
, val
);
2364 case ENC_AS_QUOTED_STRING
:
2365 g_string_append_printf(buf
, "%s=\"%s\"", param
, val
);
2368 #if 0 /* we don't use that for now */
2369 case ENC_AS_EXTENDED
:
2370 if (!g_utf8_validate(val
, -1, NULL
))
2371 g_string_append_printf(buf
, "%s*=%s''", param
,
2372 conv_get_locale_charset_str());
2374 g_string_append_printf(buf
, "%s*=%s''", param
,
2376 for (valpos
= val
; *valpos
!= '\0'; valpos
++) {
2377 if (IS_ASCII(*valpos
) && isalnum(*valpos
)) {
2378 g_string_append_printf(buf
, "%c", *valpos
);
2380 gchar hexstr
[3] = "XX";
2381 get_hex_str(hexstr
, *valpos
);
2382 g_string_append_printf(buf
, "%%%s", hexstr
);
2387 case ENC_AS_EXTENDED
:
2388 debug_print("Unhandled ENC_AS_EXTENDED.\n");
2391 case ENC_AS_ENCWORD
:
2392 len
= MAX(strlen(val
)*6, 512);
2393 tmp
= g_malloc(len
+1);
2394 codeconv_set_strict(TRUE
);
2395 conv_encode_header_full(tmp
, len
, val
, pdata
->len
+ strlen(param
) + 4 , FALSE
,
2396 prefs_common
.outgoing_charset
);
2397 codeconv_set_strict(FALSE
);
2398 if (!tmp
|| !*tmp
) {
2399 codeconv_set_strict(TRUE
);
2400 conv_encode_header_full(tmp
, len
, val
, pdata
->len
+ strlen(param
) + 4 , FALSE
,
2401 conv_get_outgoing_charset_str());
2402 codeconv_set_strict(FALSE
);
2404 if (!tmp
|| !*tmp
) {
2405 codeconv_set_strict(TRUE
);
2406 conv_encode_header_full(tmp
, len
, val
, pdata
->len
+ strlen(param
) + 4 , FALSE
,
2408 codeconv_set_strict(FALSE
);
2410 if (!tmp
|| !*tmp
) {
2411 conv_encode_header_full(tmp
, len
, val
, pdata
->len
+ strlen(param
) + 4 , FALSE
,
2414 g_string_append_printf(buf
, "%s=\"%s\"", param
, tmp
);
2420 if (buf
->str
&& strlen(buf
->str
)) {
2421 tmp
= strstr(buf
->str
, "\n");
2423 len
= (tmp
- buf
->str
);
2425 len
= strlen(buf
->str
);
2426 if (pdata
->len
+ len
> 76) {
2427 if (fprintf(pdata
->fp
, ";\n %s", buf
->str
) < 0)
2428 pdata
->error
= TRUE
;
2429 pdata
->len
= strlen(buf
->str
) + 1;
2431 if (fprintf(pdata
->fp
, "; %s", buf
->str
) < 0)
2432 pdata
->error
= TRUE
;
2433 pdata
->len
+= strlen(buf
->str
) + 2;
2436 g_string_free(buf
, TRUE
);
2439 #define TRY(func) { \
2445 int procmime_write_mime_header(MimeInfo
*mimeinfo
, FILE *fp
)
2447 struct TypeTable
*type_table
;
2448 ParametersData
*pdata
= g_new0(ParametersData
, 1);
2449 debug_print("procmime_write_mime_header\n");
2452 pdata
->error
= FALSE
;
2453 for (type_table
= mime_type_table
; type_table
->str
!= NULL
; type_table
++)
2454 if (mimeinfo
->type
== type_table
->type
) {
2455 gchar
*buf
= g_strdup_printf(
2456 "Content-Type: %s/%s", type_table
->str
, mimeinfo
->subtype
);
2457 if (fprintf(fp
, "%s", buf
) < 0) {
2462 pdata
->len
= strlen(buf
);
2466 g_hash_table_foreach(mimeinfo
->typeparameters
, write_parameters
, pdata
);
2467 if (pdata
->error
== TRUE
) {
2473 TRY(fprintf(fp
, "\n") >= 0);
2475 if (mimeinfo
->encoding_type
!= ENC_UNKNOWN
)
2476 TRY(fprintf(fp
, "Content-Transfer-Encoding: %s\n", procmime_get_encoding_str(mimeinfo
->encoding_type
)) >= 0);
2478 if (mimeinfo
->description
!= NULL
)
2479 TRY(fprintf(fp
, "Content-Description: %s\n", mimeinfo
->description
) >= 0);
2481 if (mimeinfo
->id
!= NULL
)
2482 TRY(fprintf(fp
, "Content-ID: %s\n", mimeinfo
->id
) >= 0);
2484 if (mimeinfo
->location
!= NULL
)
2485 TRY(fprintf(fp
, "Content-Location: %s\n", mimeinfo
->location
) >= 0);
2487 if (mimeinfo
->disposition
!= DISPOSITIONTYPE_UNKNOWN
) {
2488 ParametersData
*pdata
= g_new0(ParametersData
, 1);
2490 if (mimeinfo
->disposition
== DISPOSITIONTYPE_INLINE
)
2491 buf
= g_strdup("Content-Disposition: inline");
2492 else if (mimeinfo
->disposition
== DISPOSITIONTYPE_ATTACHMENT
)
2493 buf
= g_strdup("Content-Disposition: attachment");
2495 buf
= g_strdup("Content-Disposition: unknown");
2497 if (fprintf(fp
, "%s", buf
) < 0) {
2502 pdata
->len
= strlen(buf
);
2506 pdata
->error
= FALSE
;
2507 g_hash_table_foreach(mimeinfo
->dispositionparameters
, write_parameters
, pdata
);
2508 if (pdata
->error
== TRUE
) {
2513 TRY(fprintf(fp
, "\n") >= 0);
2516 TRY(fprintf(fp
, "\n") >= 0);
2521 static gint
procmime_write_message_rfc822(MimeInfo
*mimeinfo
, FILE *fp
)
2526 gchar buf
[BUFFSIZE
];
2527 gboolean skip
= FALSE
;
2530 debug_print("procmime_write_message_rfc822\n");
2533 switch (mimeinfo
->content
) {
2534 case MIMECONTENT_FILE
:
2535 if ((infp
= claws_fopen(mimeinfo
->data
.filename
, "rb")) == NULL
) {
2536 FILE_OP_ERROR(mimeinfo
->data
.filename
, "claws_fopen");
2539 if (fseek(infp
, mimeinfo
->offset
, SEEK_SET
) < 0) {
2540 FILE_OP_ERROR(mimeinfo
->data
.filename
, "fseek");
2544 while (claws_fgets(buf
, sizeof(buf
), infp
) == buf
) {
2546 if (buf
[0] == '\n' && buf
[1] == '\0')
2548 if (skip
&& (buf
[0] == ' ' || buf
[0] == '\t'))
2550 if (g_ascii_strncasecmp(buf
, "MIME-Version:", 13) == 0 ||
2551 g_ascii_strncasecmp(buf
, "Content-Type:", 13) == 0 ||
2552 g_ascii_strncasecmp(buf
, "Content-Transfer-Encoding:", 26) == 0 ||
2553 g_ascii_strncasecmp(buf
, "Content-Description:", 20) == 0 ||
2554 g_ascii_strncasecmp(buf
, "Content-ID:", 11) == 0 ||
2555 g_ascii_strncasecmp(buf
, "Content-Location:", 17) == 0 ||
2556 g_ascii_strncasecmp(buf
, "Content-Disposition:", 20) == 0) {
2561 if (claws_fwrite(buf
, sizeof(gchar
), len
, fp
) < len
) {
2562 g_warning("failed to dump %"G_GSIZE_FORMAT
" bytes from file", len
);
2571 case MIMECONTENT_MEM
:
2572 len
= strlen(mimeinfo
->data
.mem
);
2573 if (claws_fwrite(mimeinfo
->data
.mem
, sizeof(gchar
), len
, fp
) < len
) {
2574 g_warning("failed to dump %"G_GSIZE_FORMAT
" bytes from mem", len
);
2583 childnode
= mimeinfo
->node
->children
;
2584 if (childnode
== NULL
)
2587 child
= (MimeInfo
*) childnode
->data
;
2588 if (fprintf(fp
, "MIME-Version: 1.0\n") < 0) {
2589 g_warning("failed to write mime version");
2592 if (procmime_write_mime_header(child
, fp
) < 0)
2594 return procmime_write_mimeinfo(child
, fp
);
2597 static gint
procmime_write_multipart(MimeInfo
*mimeinfo
, FILE *fp
)
2601 gchar
*boundary
, *str
, *str2
;
2602 gchar buf
[BUFFSIZE
];
2603 gboolean firstboundary
;
2606 debug_print("procmime_write_multipart\n");
2608 boundary
= g_hash_table_lookup(mimeinfo
->typeparameters
, "boundary");
2610 switch (mimeinfo
->content
) {
2611 case MIMECONTENT_FILE
:
2612 if ((infp
= claws_fopen(mimeinfo
->data
.filename
, "rb")) == NULL
) {
2613 FILE_OP_ERROR(mimeinfo
->data
.filename
, "claws_fopen");
2616 if (fseek(infp
, mimeinfo
->offset
, SEEK_SET
) < 0) {
2617 FILE_OP_ERROR(mimeinfo
->data
.filename
, "fseek");
2621 while (claws_fgets(buf
, sizeof(buf
), infp
) == buf
) {
2622 if (IS_BOUNDARY(buf
, boundary
, strlen(boundary
)))
2625 if (claws_fwrite(buf
, sizeof(gchar
), len
, fp
) < len
) {
2626 g_warning("failed to write %"G_GSIZE_FORMAT
, len
);
2634 case MIMECONTENT_MEM
:
2635 str
= g_strdup(mimeinfo
->data
.mem
);
2636 if (((str2
= strstr(str
, boundary
)) != NULL
) && ((str2
- str
) >= 2) &&
2637 (*(str2
- 1) == '-') && (*(str2
- 2) == '-'))
2640 if (claws_fwrite(str
, sizeof(gchar
), len
, fp
) < len
) {
2641 g_warning("failed to write %"G_GSIZE_FORMAT
" from mem", len
);
2652 childnode
= mimeinfo
->node
->children
;
2653 firstboundary
= TRUE
;
2654 while (childnode
!= NULL
) {
2655 MimeInfo
*child
= childnode
->data
;
2658 firstboundary
= FALSE
;
2660 TRY(fprintf(fp
, "\n") >= 0);
2662 TRY(fprintf(fp
, "--%s\n", boundary
) >= 0);
2664 if (procmime_write_mime_header(child
, fp
) < 0)
2666 if (procmime_write_mimeinfo(child
, fp
) < 0)
2669 childnode
= g_node_next_sibling(childnode
);
2671 TRY(fprintf(fp
, "\n--%s--\n", boundary
) >= 0);
2676 gint
procmime_write_mimeinfo(MimeInfo
*mimeinfo
, FILE *fp
)
2680 debug_print("procmime_write_mimeinfo\n");
2682 if (G_NODE_IS_LEAF(mimeinfo
->node
)) {
2683 switch (mimeinfo
->content
) {
2684 case MIMECONTENT_FILE
:
2685 if ((infp
= claws_fopen(mimeinfo
->data
.filename
, "rb")) == NULL
) {
2686 FILE_OP_ERROR(mimeinfo
->data
.filename
, "claws_fopen");
2689 copy_file_part_to_fp(infp
, mimeinfo
->offset
, mimeinfo
->length
, fp
);
2693 case MIMECONTENT_MEM
:
2694 len
= strlen(mimeinfo
->data
.mem
);
2695 if (claws_fwrite(mimeinfo
->data
.mem
, sizeof(gchar
), len
, fp
) < len
)
2703 /* Call writer for mime type */
2704 switch (mimeinfo
->type
) {
2705 case MIMETYPE_MESSAGE
:
2706 if (g_ascii_strcasecmp(mimeinfo
->subtype
, "rfc822") == 0) {
2707 return procmime_write_message_rfc822(mimeinfo
, fp
);
2711 case MIMETYPE_MULTIPART
:
2712 return procmime_write_multipart(mimeinfo
, fp
);
2724 gchar
*procmime_get_part_file_name(MimeInfo
*mimeinfo
)
2728 if ((mimeinfo
->type
== MIMETYPE_TEXT
) && !g_ascii_strcasecmp(mimeinfo
->subtype
, "html"))
2729 base
= g_strdup("mimetmp.html");
2731 const gchar
*basetmp
;
2734 basetmp
= procmime_mimeinfo_get_parameter(mimeinfo
, "filename");
2735 if (basetmp
== NULL
)
2736 basetmp
= procmime_mimeinfo_get_parameter(mimeinfo
, "name");
2737 if (basetmp
== NULL
)
2738 basetmp
= "mimetmp";
2739 basename
= g_path_get_basename(basetmp
);
2740 if (*basename
== '\0') {
2742 basename
= g_strdup("mimetmp");
2744 base
= conv_filename_from_utf8(basename
);
2746 subst_for_shellsafe_filename(base
);
2752 void *procmime_get_part_as_string(MimeInfo
*mimeinfo
,
2753 gboolean null_terminate
)
2757 gint length
, readlength
;
2759 cm_return_val_if_fail(mimeinfo
!= NULL
, NULL
);
2761 if (mimeinfo
->encoding_type
!= ENC_BINARY
&&
2762 !procmime_decode_content(mimeinfo
))
2765 if (mimeinfo
->content
== MIMECONTENT_MEM
)
2766 return g_strdup(mimeinfo
->data
.mem
);
2768 if ((infp
= claws_fopen(mimeinfo
->data
.filename
, "rb")) == NULL
) {
2769 FILE_OP_ERROR(mimeinfo
->data
.filename
, "claws_fopen");
2773 if (fseek(infp
, mimeinfo
->offset
, SEEK_SET
) < 0) {
2774 FILE_OP_ERROR(mimeinfo
->data
.filename
, "fseek");
2779 length
= mimeinfo
->length
;
2781 data
= g_malloc(null_terminate
? length
+ 1 : length
);
2783 g_warning("could not allocate %d bytes for procmime_get_part_as_string",
2784 (null_terminate
? length
+ 1 : length
));
2789 readlength
= claws_fread(data
, length
, 1, infp
);
2790 if (readlength
<= 0) {
2791 FILE_OP_ERROR(mimeinfo
->data
.filename
, "fread");
2800 data
[length
] = '\0';
2805 /* Returns an open GInputStream. The caller should just
2806 * read mimeinfo->length bytes from it and then release it. */
2807 GInputStream
*procmime_get_part_as_inputstream(MimeInfo
*mimeinfo
)
2809 cm_return_val_if_fail(mimeinfo
!= NULL
, NULL
);
2811 if (mimeinfo
->encoding_type
!= ENC_BINARY
&&
2812 !procmime_decode_content(mimeinfo
)) {
2813 g_warning("could not decode part");
2816 if (mimeinfo
->content
== MIMECONTENT_MEM
) {
2817 /* NULL for destroy func, since we're not copying
2818 * the data for the stream. */
2819 return g_memory_input_stream_new_from_data(
2821 mimeinfo
->length
, NULL
);
2823 return g_memory_input_stream_new_from_data(
2824 procmime_get_part_as_string(mimeinfo
, FALSE
),
2825 mimeinfo
->length
, g_free
);
2829 GdkPixbuf
*procmime_get_part_as_pixbuf(MimeInfo
*mimeinfo
, GError
**error
)
2832 GInputStream
*stream
;
2837 stream
= procmime_get_part_as_inputstream(mimeinfo
);
2838 if (stream
== NULL
) {
2840 *error
= g_error_new_literal(G_FILE_ERROR
, -1, _("Could not decode part"));
2844 pixbuf
= gdk_pixbuf_new_from_stream(stream
, NULL
, error
);
2845 g_object_unref(stream
);
2847 if (error
&& *error
!= NULL
)