2 * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2016 Hiroyuki Yamamoto & The Claws Mail Team
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "claws-features.h"
30 #include <glib/gi18n.h>
36 #include <sys/types.h>
42 #include "procheader.h"
43 #include "quoted-printable.h"
50 #include "prefs_common.h"
51 #include "prefs_gtk.h"
52 #include "alertpanel.h"
57 static GHashTable *procmime_get_mime_type_table (void);
58 static MimeInfo *procmime_scan_file_short(const gchar *filename);
59 static MimeInfo *procmime_scan_queue_file_short(const gchar *filename);
60 static MimeInfo *procmime_scan_queue_file_full(const gchar *filename, gboolean short_scan);
62 MimeInfo *procmime_mimeinfo_new(void)
66 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_ptr)
149 MimeInfo *mimeinfo = *mimeinfo_ptr;
155 node = mimeinfo->node;
156 g_node_traverse(node, G_IN_ORDER, G_TRAVERSE_ALL, -1, free_func, NULL);
158 g_node_destroy(node);
160 *mimeinfo_ptr = NULL;
163 MimeInfo *procmime_mimeinfo_parent(MimeInfo *mimeinfo)
165 cm_return_val_if_fail(mimeinfo != NULL, NULL);
166 cm_return_val_if_fail(mimeinfo->node != NULL, NULL);
168 if (mimeinfo->node->parent == NULL)
170 return (MimeInfo *) mimeinfo->node->parent->data;
173 MimeInfo *procmime_mimeinfo_next(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->children)
179 return (MimeInfo *) mimeinfo->node->children->data;
180 if (mimeinfo->node->next)
181 return (MimeInfo *) mimeinfo->node->next->data;
183 if (mimeinfo->node->parent == NULL)
186 while (mimeinfo->node->parent != NULL) {
187 mimeinfo = (MimeInfo *) mimeinfo->node->parent->data;
188 if (mimeinfo->node->next)
189 return (MimeInfo *) mimeinfo->node->next->data;
195 MimeInfo *procmime_scan_message(MsgInfo *msginfo)
200 filename = procmsg_get_message_file_path(msginfo);
201 if (!filename || !is_file_exist(filename)) {
206 if (!folder_has_parent_of_type(msginfo->folder, F_QUEUE) &&
207 !folder_has_parent_of_type(msginfo->folder, F_DRAFT))
208 mimeinfo = procmime_scan_file(filename);
210 mimeinfo = procmime_scan_queue_file(filename);
216 MimeInfo *procmime_scan_message_short(MsgInfo *msginfo)
221 filename = procmsg_get_message_file_path(msginfo);
222 if (!filename || !is_file_exist(filename)) {
227 if (!folder_has_parent_of_type(msginfo->folder, F_QUEUE) &&
228 !folder_has_parent_of_type(msginfo->folder, F_DRAFT))
229 mimeinfo = procmime_scan_file_short(filename);
231 mimeinfo = procmime_scan_queue_file_short(filename);
239 H_CONTENT_TRANSFER_ENCODING = 0,
241 H_CONTENT_DISPOSITION = 2,
242 H_CONTENT_DESCRIPTION = 3,
246 const gchar *procmime_mimeinfo_get_parameter(MimeInfo *mimeinfo, const gchar *name)
250 cm_return_val_if_fail(mimeinfo != NULL, NULL);
251 cm_return_val_if_fail(name != NULL, NULL);
253 value = g_hash_table_lookup(mimeinfo->dispositionparameters, name);
255 value = g_hash_table_lookup(mimeinfo->typeparameters, name);
260 #ifdef HAVE_FGETS_UNLOCKED
261 #define SC_FGETS fgets_unlocked
262 #define SC_FPUTS fputs_unlocked
263 #define SC_FPUTC fputc_unlocked
264 #define SC_FREAD fread_unlocked
265 #define SC_FWRITE fwrite_unlocked
266 #define SC_FEOF feof_unlocked
267 #define SC_FERROR ferror_unlocked
269 static FILE *procmime_fopen(const gchar *file, const gchar *mode)
271 FILE *fp = g_fopen(file, mode);
277 static int procmime_fclose(FILE *fp)
283 #define SC_FGETS fgets
284 #define SC_FPUTS fputs
285 #define SC_FPUTC fputc
286 #define SC_FREAD fread
287 #define SC_FWRITE fwrite
289 #define SC_FERROR ferror
290 #define procmime_fopen g_fopen
291 #define procmime_fclose fclose
294 #define FLUSH_LASTLINE() { \
295 if (*lastline != '\0') { \
297 strretchomp(lastline); \
298 llen = strlen(lastline); \
299 if (lastline[llen-1] == ' ' && !account_signatures_matchlist_str_found(lastline, "%s") && \
300 !(llen == 2 && lastline[1] == ' ' && strchr(prefs_common.quote_chars, lastline[0]))) { \
301 /* this is flowed */ \
303 lastline[llen-1] = '\0'; \
304 if (SC_FPUTS(lastline, outfp) == EOF) \
307 if (SC_FPUTS(lastline, outfp) == EOF) \
309 if (SC_FPUTS("\n", outfp) == EOF) \
313 strcpy(lastline, buf); \
316 gboolean procmime_decode_content(MimeInfo *mimeinfo)
323 gboolean tmp_file = FALSE;
324 gboolean flowed = FALSE;
325 gboolean delsp = FALSE;
326 gboolean err = FALSE;
330 cm_return_val_if_fail(mimeinfo != NULL, FALSE);
332 EncodingType encoding = forced_encoding
334 : mimeinfo->encoding_type;
335 gchar lastline[BUFFSIZE];
336 memset(lastline, 0, BUFFSIZE);
338 if (prefs_common.respect_flowed_format &&
339 mimeinfo->type == MIMETYPE_TEXT &&
340 !strcasecmp(mimeinfo->subtype, "plain")) {
341 if (procmime_mimeinfo_get_parameter(mimeinfo, "format") != NULL &&
342 !strcasecmp(procmime_mimeinfo_get_parameter(mimeinfo, "format"),"flowed"))
345 procmime_mimeinfo_get_parameter(mimeinfo, "delsp") != NULL &&
346 !strcasecmp(procmime_mimeinfo_get_parameter(mimeinfo, "delsp"),"yes"))
351 encoding == ENC_UNKNOWN ||
352 encoding == ENC_BINARY ||
353 encoding == ENC_7BIT ||
358 if (mimeinfo->type == MIMETYPE_MULTIPART || mimeinfo->type == MIMETYPE_MESSAGE)
361 if (mimeinfo->data.filename == NULL)
364 infp = procmime_fopen(mimeinfo->data.filename, "rb");
366 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
369 if (fseek(infp, mimeinfo->offset, SEEK_SET) < 0) {
370 FILE_OP_ERROR(mimeinfo->data.filename, "fseek");
371 procmime_fclose(infp);
375 outfp = get_tmpfile_in_dir(get_mime_tmp_dir(), &tmpfilename);
378 procmime_fclose(infp);
381 #ifdef HAVE_FGETS_UNLOCKED
385 readend = mimeinfo->offset + mimeinfo->length;
387 account_signatures_matchlist_create(); /* FLUSH_LASTLINE will use it */
390 if (encoding == ENC_QUOTED_PRINTABLE) {
391 while ((ftell(infp) < readend) && (SC_FGETS(buf, sizeof(buf), infp) != NULL)) {
393 len = qp_decode_line(buf);
396 if (SC_FWRITE(buf, 1, len, outfp) < len)
404 } else if (encoding == ENC_BASE64) {
405 gchar outbuf[BUFFSIZE];
406 gint len, inlen, inread;
407 gboolean got_error = FALSE;
408 gboolean uncanonicalize = FALSE;
410 gboolean null_bytes = FALSE;
411 gboolean starting = TRUE;
413 if (mimeinfo->type == MIMETYPE_TEXT ||
414 mimeinfo->type == MIMETYPE_MESSAGE) {
415 uncanonicalize = TRUE;
416 tmpfp = my_tmpfile();
418 perror("my_tmpfile");
420 procmime_fclose(outfp);
421 procmime_fclose(infp);
424 #ifdef HAVE_FGETS_UNLOCKED
430 while ((inlen = MIN(readend - ftell(infp), sizeof(buf))) > 0 && !err) {
431 inread = SC_FREAD(buf, 1, inlen, infp);
432 len = g_base64_decode_step(buf, inlen, outbuf, &state, &save);
433 if (uncanonicalize == TRUE && strlen(outbuf) < len && starting) {
434 uncanonicalize = FALSE;
438 if (((inread != inlen) || len < 0) && !got_error) {
439 g_warning("Bad BASE64 content.");
440 if (SC_FWRITE(_("[Error decoding BASE64]\n"),
442 strlen(_("[Error decoding BASE64]\n")),
443 tmpfp) < strlen(_("[Error decoding BASE64]\n")))
444 g_warning("error decoding BASE64");
447 } else if (len >= 0) {
448 /* print out the error message only once
451 /* we won't uncanonicalize, output to outfp directly */
452 if (SC_FWRITE(outbuf, sizeof(gchar), len, outfp) < len)
455 if (SC_FWRITE(outbuf, sizeof(gchar), len, tmpfp) < len)
462 if (uncanonicalize) {
464 while (SC_FGETS(buf, sizeof(buf), tmpfp) != NULL) {
466 if (SC_FPUTS(buf, outfp) == EOF)
471 procmime_fclose(tmpfp);
472 } else if (encoding == ENC_X_UUENCODE) {
473 gchar outbuf[BUFFSIZE];
475 gboolean flag = FALSE;
477 while ((ftell(infp) < readend) && (SC_FGETS(buf, sizeof(buf), infp) != NULL)) {
478 if (!flag && strncmp(buf,"begin ", 6)) continue;
481 len = fromuutobits(outbuf, buf);
484 g_warning("Bad UUENCODE content (%d)", len);
487 if (SC_FWRITE(outbuf, sizeof(gchar), len, outfp) < len)
493 while ((ftell(infp) < readend) && (SC_FGETS(buf, sizeof(buf), infp) != NULL)) {
495 if (SC_FPUTS(buf, outfp) == EOF)
504 g_warning("write error");
507 procmime_fclose(outfp);
508 procmime_fclose(infp);
510 account_signatures_matchlist_delete();
516 if (g_stat(tmpfilename, &statbuf) < 0) {
517 FILE_OP_ERROR(tmpfilename, "stat");
522 claws_unlink(mimeinfo->data.filename);
523 g_free(mimeinfo->data.filename);
524 mimeinfo->data.filename = tmpfilename;
525 mimeinfo->tmp = TRUE;
526 mimeinfo->offset = 0;
527 mimeinfo->length = statbuf.st_size;
528 mimeinfo->encoding_type = ENC_BINARY;
533 #define B64_LINE_SIZE 57
534 #define B64_BUFFSIZE 77
536 gboolean procmime_encode_content(MimeInfo *mimeinfo, EncodingType encoding)
538 FILE *infp = NULL, *outfp;
542 gboolean err = FALSE;
544 if (mimeinfo->content == MIMECONTENT_EMPTY)
547 if (mimeinfo->encoding_type != ENC_UNKNOWN &&
548 mimeinfo->encoding_type != ENC_BINARY &&
549 mimeinfo->encoding_type != ENC_7BIT &&
550 mimeinfo->encoding_type != ENC_8BIT)
551 if(!procmime_decode_content(mimeinfo))
554 outfp = get_tmpfile_in_dir(get_mime_tmp_dir(), &tmpfilename);
559 #ifdef HAVE_FGETS_UNLOCKED
563 if (mimeinfo->content == MIMECONTENT_FILE && mimeinfo->data.filename) {
564 if ((infp = procmime_fopen(mimeinfo->data.filename, "rb")) == NULL) {
565 g_warning("Can't open file %s", mimeinfo->data.filename);
566 procmime_fclose(outfp);
569 } else if (mimeinfo->content == MIMECONTENT_MEM) {
570 infp = str_open_as_stream(mimeinfo->data.mem);
572 procmime_fclose(outfp);
575 #ifdef HAVE_FGETS_UNLOCKED
579 procmime_fclose(outfp);
580 g_warning("Unknown mimeinfo");
584 if (encoding == ENC_BASE64) {
585 gchar inbuf[B64_LINE_SIZE], *out;
587 gchar *tmp_file = NULL;
589 if (mimeinfo->type == MIMETYPE_TEXT ||
590 mimeinfo->type == MIMETYPE_MESSAGE) {
591 if (mimeinfo->content == MIMECONTENT_FILE) {
592 tmp_file = get_tmp_file();
593 if (canonicalize_file(mimeinfo->data.filename, tmp_file) < 0) {
595 procmime_fclose(infp);
596 procmime_fclose(outfp);
599 if ((tmp_fp = procmime_fopen(tmp_file, "rb")) == NULL) {
600 FILE_OP_ERROR(tmp_file, "fopen");
601 claws_unlink(tmp_file);
603 procmime_fclose(infp);
604 procmime_fclose(outfp);
608 gchar *out = canonicalize_str(mimeinfo->data.mem);
609 procmime_fclose(infp);
610 infp = str_open_as_stream(out);
614 procmime_fclose(outfp);
617 #ifdef HAVE_FGETS_UNLOCKED
623 while ((len = SC_FREAD(inbuf, sizeof(gchar),
624 B64_LINE_SIZE, tmp_fp))
626 out = g_base64_encode(inbuf, B64_LINE_SIZE);
627 if (SC_FPUTS(out, outfp) == EOF)
630 if (SC_FPUTC('\n', outfp) == EOF)
633 if (len > 0 && SC_FEOF(tmp_fp)) {
634 out = g_base64_encode(inbuf, len);
635 if (SC_FPUTS(out, outfp) == EOF)
638 if (SC_FPUTC('\n', outfp) == EOF)
643 procmime_fclose(tmp_fp);
644 claws_unlink(tmp_file);
647 } else if (encoding == ENC_QUOTED_PRINTABLE) {
648 gchar inbuf[BUFFSIZE], outbuf[BUFFSIZE * 4];
650 while (SC_FGETS(inbuf, sizeof(inbuf), infp) != NULL) {
651 qp_encode_line(outbuf, inbuf);
653 if (!strncmp("From ", outbuf, sizeof("From ")-1)) {
654 gchar *tmpbuf = outbuf;
656 tmpbuf += sizeof("From ")-1;
658 if (SC_FPUTS("=46rom ", outfp) == EOF)
660 if (SC_FPUTS(tmpbuf, outfp) == EOF)
663 if (SC_FPUTS(outbuf, outfp) == EOF)
670 while (SC_FGETS(buf, sizeof(buf), infp) != NULL) {
672 if (SC_FPUTS(buf, outfp) == EOF)
677 procmime_fclose(outfp);
678 procmime_fclose(infp);
683 if (mimeinfo->content == MIMECONTENT_FILE) {
684 if (mimeinfo->tmp && (mimeinfo->data.filename != NULL))
685 claws_unlink(mimeinfo->data.filename);
686 g_free(mimeinfo->data.filename);
687 } else if (mimeinfo->content == MIMECONTENT_MEM) {
688 if (mimeinfo->tmp && (mimeinfo->data.mem != NULL))
689 g_free(mimeinfo->data.mem);
692 if (g_stat(tmpfilename, &statbuf) < 0) {
693 FILE_OP_ERROR(tmpfilename, "stat");
696 mimeinfo->content = MIMECONTENT_FILE;
697 mimeinfo->data.filename = tmpfilename;
698 mimeinfo->tmp = TRUE;
699 mimeinfo->offset = 0;
700 mimeinfo->length = statbuf.st_size;
701 mimeinfo->encoding_type = encoding;
706 static gint procmime_get_part_to_stream(FILE *outfp, MimeInfo *mimeinfo)
710 gint restlength, readlength;
711 gint saved_errno = 0;
713 cm_return_val_if_fail(outfp != NULL, -1);
714 cm_return_val_if_fail(mimeinfo != NULL, -1);
716 if (mimeinfo->encoding_type != ENC_BINARY && !procmime_decode_content(mimeinfo))
719 if ((infp = procmime_fopen(mimeinfo->data.filename, "rb")) == NULL) {
721 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
722 return -(saved_errno);
724 if (fseek(infp, mimeinfo->offset, SEEK_SET) < 0) {
726 FILE_OP_ERROR(mimeinfo->data.filename, "fseek");
727 procmime_fclose(infp);
728 return -(saved_errno);
731 restlength = mimeinfo->length;
733 while ((restlength > 0) && ((readlength = SC_FREAD(buf, 1, restlength > BUFFSIZE ? BUFFSIZE : restlength, infp)) > 0)) {
734 if (SC_FWRITE(buf, 1, readlength, outfp) != readlength) {
736 procmime_fclose(infp);
737 return -(saved_errno);
739 restlength -= readlength;
742 procmime_fclose(infp);
748 gint procmime_get_part(const gchar *outfile, MimeInfo *mimeinfo)
752 gint saved_errno = 0;
754 cm_return_val_if_fail(outfile != NULL, -1);
756 if ((outfp = procmime_fopen(outfile, "wb")) == NULL) {
758 FILE_OP_ERROR(outfile, "fopen");
759 return -(saved_errno);
762 result = procmime_get_part_to_stream(outfp, mimeinfo);
764 if (procmime_fclose(outfp) == EOF) {
766 FILE_OP_ERROR(outfile, "fclose");
767 claws_unlink(outfile);
768 return -(saved_errno);
774 gboolean procmime_scan_text_content(MimeInfo *mimeinfo,
775 gboolean (*scan_callback)(const gchar *str, gpointer cb_data),
779 const gchar *src_codeset;
780 gboolean conv_fail = FALSE;
783 gboolean scan_ret = FALSE;
784 gchar *tmpfile = NULL;
787 cm_return_val_if_fail(mimeinfo != NULL, TRUE);
788 cm_return_val_if_fail(scan_callback != NULL, TRUE);
790 if (!procmime_decode_content(mimeinfo))
794 tmpfp = fmemopen(NULL, mimeinfo->length * 2, "w+");
796 tmpfile = procmime_get_tmp_file_name(mimeinfo);
797 if (tmpfile == NULL) {
798 g_warning("no filename\n");
802 tmpfp = procmime_fopen(tmpfile, "w+");
807 FILE_OP_ERROR(tmpfile, "open");
811 if ((r = procmime_get_part_to_stream(tmpfp, mimeinfo)) < 0) {
812 g_warning("procmime_get_part_to_stream error %d\n", r);
817 src_codeset = forced_charset
819 procmime_mimeinfo_get_parameter(mimeinfo, "charset");
821 /* use supersets transparently when possible */
822 if (!forced_charset && src_codeset && !strcasecmp(src_codeset, CS_ISO_8859_1))
823 src_codeset = CS_WINDOWS_1252;
824 else if (!forced_charset && src_codeset && !strcasecmp(src_codeset, CS_X_GBK))
825 src_codeset = CS_GB18030;
826 else if (!forced_charset && src_codeset && !strcasecmp(src_codeset, CS_GBK))
827 src_codeset = CS_GB18030;
828 else if (!forced_charset && src_codeset && !strcasecmp(src_codeset, CS_GB2312))
829 src_codeset = CS_GB18030;
830 else if (!forced_charset && src_codeset && !strcasecmp(src_codeset, CS_X_VIET_VPS))
831 src_codeset = CS_WINDOWS_874;
833 if (mimeinfo->type == MIMETYPE_TEXT && !g_ascii_strcasecmp(mimeinfo->subtype, "html")) {
834 SC_HTMLParser *parser;
837 conv = conv_code_converter_new(src_codeset);
838 parser = sc_html_parser_new(tmpfp, conv);
839 while ((str = sc_html_parse(parser)) != NULL) {
840 if ((scan_ret = scan_callback(str, cb_data)) == TRUE)
843 sc_html_parser_destroy(parser);
844 conv_code_converter_destroy(conv);
845 } else if (mimeinfo->type == MIMETYPE_TEXT && !g_ascii_strcasecmp(mimeinfo->subtype, "enriched")) {
849 conv = conv_code_converter_new(src_codeset);
850 parser = ertf_parser_new(tmpfp, conv);
851 while ((str = ertf_parse(parser)) != NULL) {
852 if ((scan_ret = scan_callback(str, cb_data)) == TRUE)
855 ertf_parser_destroy(parser);
856 conv_code_converter_destroy(conv);
857 } else if (mimeinfo->type == MIMETYPE_TEXT && mimeinfo->disposition != DISPOSITIONTYPE_ATTACHMENT) {
858 while (SC_FGETS(buf, sizeof(buf), tmpfp) != NULL) {
859 str = conv_codeset_strdup(buf, src_codeset, CS_UTF_8);
861 if ((scan_ret = scan_callback(str, cb_data)) == TRUE) {
868 if ((scan_ret = scan_callback(buf, cb_data)) == TRUE)
875 g_warning("procmime_get_text_content(): Code conversion failed.");
877 procmime_fclose(tmpfp);
880 claws_unlink(tmpfile);
887 static gboolean scan_fputs_cb(const gchar *str, gpointer fp)
889 if (SC_FPUTS(str, (FILE *)fp) == EOF)
895 FILE *procmime_get_text_content(MimeInfo *mimeinfo)
900 if ((outfp = my_tmpfile()) == NULL) {
901 perror("my_tmpfile");
904 #ifdef HAVE_FGETS_UNLOCKED
908 err = procmime_scan_text_content(mimeinfo, scan_fputs_cb, outfp);
912 procmime_fclose(outfp);
915 #ifdef HAVE_FGETS_UNLOCKED
922 FILE *procmime_get_binary_content(MimeInfo *mimeinfo)
925 gchar *tmpfile = NULL;
927 cm_return_val_if_fail(mimeinfo != NULL, NULL);
929 if (!procmime_decode_content(mimeinfo))
933 outfp = fmemopen(NULL, mimeinfo->length * 2, "w+");
935 tmpfile = procmime_get_tmp_file_name(mimeinfo);
936 if (tmpfile == NULL) {
937 g_warning("no filename\n");
941 outfp = procmime_fopen(tmpfile, "w+");
944 if (tmpfile != NULL) {
949 if (procmime_get_part_to_stream(outfp, mimeinfo) < 0) {
953 #ifdef HAVE_FGETS_UNLOCKED
959 /* search the first text part of (multipart) MIME message,
960 decode, convert it and output to outfp. */
961 FILE *procmime_get_first_text_content(MsgInfo *msginfo)
964 MimeInfo *mimeinfo, *partinfo;
965 gboolean empty_ok = FALSE, short_scan = TRUE;
967 cm_return_val_if_fail(msginfo != NULL, NULL);
969 /* first we try to short-scan (for speed), refusing empty parts */
972 mimeinfo = procmime_scan_message_short(msginfo);
974 mimeinfo = procmime_scan_message(msginfo);
975 if (!mimeinfo) return NULL;
978 while (partinfo && (partinfo->type != MIMETYPE_TEXT ||
979 (partinfo->length == 0 && !empty_ok))) {
980 partinfo = procmime_mimeinfo_next(partinfo);
983 outfp = procmime_get_text_content(partinfo);
984 else if (!empty_ok && short_scan) {
985 /* if short scan didn't find a non-empty part, rescan
986 * fully for non-empty parts
989 procmime_mimeinfo_free_all(&mimeinfo);
991 } else if (!empty_ok && !short_scan) {
992 /* if full scan didn't find a non-empty part, rescan
993 * accepting empty parts
996 procmime_mimeinfo_free_all(&mimeinfo);
999 procmime_mimeinfo_free_all(&mimeinfo);
1001 /* outfp already unlocked at this time */
1006 static gboolean find_encrypted_func(GNode *node, gpointer data)
1008 MimeInfo *mimeinfo = (MimeInfo *) node->data;
1009 MimeInfo **encinfo = (MimeInfo **) data;
1011 if (privacy_mimeinfo_is_encrypted(mimeinfo)) {
1012 *encinfo = mimeinfo;
1019 static MimeInfo *find_encrypted_part(MimeInfo *rootinfo)
1021 MimeInfo *encinfo = NULL;
1023 g_node_traverse(rootinfo->node, G_IN_ORDER, G_TRAVERSE_ALL, -1,
1024 find_encrypted_func, &encinfo);
1029 /* search the first encrypted text part of (multipart) MIME message,
1030 decode, convert it and output to outfp. */
1031 FILE *procmime_get_first_encrypted_text_content(MsgInfo *msginfo)
1034 MimeInfo *mimeinfo, *partinfo, *encinfo;
1036 cm_return_val_if_fail(msginfo != NULL, NULL);
1038 mimeinfo = procmime_scan_message(msginfo);
1043 partinfo = mimeinfo;
1044 if ((encinfo = find_encrypted_part(partinfo)) != NULL) {
1045 debug_print("decrypting message part\n");
1046 if (privacy_mimeinfo_decrypt(encinfo) < 0) {
1047 alertpanel_error(_("Couldn't decrypt: %s"),
1048 privacy_get_error());
1052 partinfo = mimeinfo;
1053 while (partinfo && partinfo->type != MIMETYPE_TEXT) {
1054 partinfo = procmime_mimeinfo_next(partinfo);
1055 if (privacy_mimeinfo_is_signed(partinfo))
1056 procmsg_msginfo_set_flags(msginfo, 0, MSG_SIGNED);
1060 outfp = procmime_get_text_content(partinfo);
1062 procmime_mimeinfo_free_all(&mimeinfo);
1064 /* outfp already unlocked at this time */
1068 gboolean procmime_msginfo_is_encrypted(MsgInfo *msginfo)
1070 MimeInfo *mimeinfo, *partinfo;
1071 gboolean result = FALSE;
1073 cm_return_val_if_fail(msginfo != NULL, FALSE);
1075 mimeinfo = procmime_scan_message(msginfo);
1080 partinfo = mimeinfo;
1081 result = (find_encrypted_part(partinfo) != NULL);
1082 procmime_mimeinfo_free_all(&mimeinfo);
1087 gchar *procmime_get_tmp_file_name(MimeInfo *mimeinfo)
1089 static guint32 id = 0;
1094 cm_return_val_if_fail(mimeinfo != NULL, NULL);
1096 g_snprintf(f_prefix, sizeof(f_prefix), "%08x.", id++);
1098 if ((mimeinfo->type == MIMETYPE_TEXT) && !g_ascii_strcasecmp(mimeinfo->subtype, "html"))
1099 base = g_strdup("mimetmp.html");
1101 const gchar *basetmp;
1103 basetmp = procmime_mimeinfo_get_parameter(mimeinfo, "filename");
1104 if (basetmp == NULL)
1105 basetmp = procmime_mimeinfo_get_parameter(mimeinfo, "name");
1106 if (basetmp == NULL)
1107 basetmp = "mimetmp";
1108 basetmp = g_path_get_basename(basetmp);
1109 if (*basetmp == '\0')
1110 basetmp = g_strdup("mimetmp");
1111 base = conv_filename_from_utf8(basetmp);
1112 g_free((gchar*)basetmp);
1113 subst_for_shellsafe_filename(base);
1116 filename = g_strconcat(get_mime_tmp_dir(), G_DIR_SEPARATOR_S,
1117 f_prefix, base, NULL);
1124 static GList *mime_type_list = NULL;
1126 gchar *procmime_get_mime_type(const gchar *filename)
1132 static GHashTable *mime_type_table = NULL;
1133 MimeType *mime_type;
1135 if (!mime_type_table) {
1136 mime_type_table = procmime_get_mime_type_table();
1137 if (!mime_type_table) return NULL;
1141 if (filename == NULL)
1144 base = g_path_get_basename(filename);
1145 if ((p = strrchr(base, '.')) != NULL)
1146 ext = g_utf8_strdown(p + 1, -1);
1148 ext = g_utf8_strdown(base, -1);
1152 mime_type = g_hash_table_lookup(mime_type_table, ext);
1156 str = g_strconcat(mime_type->type, "/", mime_type->sub_type,
1158 debug_print("got type %s for %s\n", str, ext);
1165 gchar *str = get_content_type_from_registry_with_ext(ext);
1172 static guint procmime_str_hash(gconstpointer gptr)
1174 guint hash_result = 0;
1177 for (str = gptr; str && *str; str++) {
1178 if (isupper(*str)) hash_result += (*str + ' ');
1179 else hash_result += *str;
1185 static gint procmime_str_equal(gconstpointer gptr1, gconstpointer gptr2)
1187 const char *str1 = gptr1;
1188 const char *str2 = gptr2;
1190 return !g_utf8_collate(str1, str2);
1193 static GHashTable *procmime_get_mime_type_table(void)
1195 GHashTable *table = NULL;
1197 MimeType *mime_type;
1200 if (!mime_type_list) {
1201 mime_type_list = procmime_get_mime_type_list();
1202 if (!mime_type_list) return NULL;
1205 table = g_hash_table_new(procmime_str_hash, procmime_str_equal);
1207 for (cur = mime_type_list; cur != NULL; cur = cur->next) {
1211 mime_type = (MimeType *)cur->data;
1213 if (!mime_type->extension) continue;
1215 exts = g_strsplit(mime_type->extension, " ", 16);
1216 for (i = 0; exts[i] != NULL; i++) {
1217 /* Don't overwrite previously inserted extension */
1218 if (!g_hash_table_lookup(table, exts[i])) {
1219 key = g_strdup(exts[i]);
1220 g_hash_table_insert(table, key, mime_type);
1229 GList *procmime_get_mime_type_list(void)
1233 gchar buf[BUFFSIZE];
1236 MimeType *mime_type;
1237 gboolean fp_is_glob_file = TRUE;
1240 return mime_type_list;
1242 #if defined(__NetBSD__) || defined(__OpenBSD__) || defined(__FreeBSD__)
1243 if ((fp = procmime_fopen(DATAROOTDIR "/mime/globs", "rb")) == NULL)
1245 if ((fp = procmime_fopen("/usr/share/mime/globs", "rb")) == NULL)
1248 fp_is_glob_file = FALSE;
1249 if ((fp = procmime_fopen("/etc/mime.types", "rb")) == NULL) {
1250 if ((fp = procmime_fopen(SYSCONFDIR "/mime.types", "rb"))
1252 FILE_OP_ERROR(SYSCONFDIR "/mime.types",
1259 while (SC_FGETS(buf, sizeof(buf), fp) != NULL) {
1260 p = strchr(buf, '#');
1266 if (fp_is_glob_file) {
1267 while (*p && !g_ascii_isspace(*p) && (*p!=':')) p++;
1269 while (*p && !g_ascii_isspace(*p)) p++;
1276 delim = strchr(buf, '/');
1277 if (delim == NULL) continue;
1280 mime_type = g_new(MimeType, 1);
1281 mime_type->type = g_strdup(buf);
1282 mime_type->sub_type = g_strdup(delim + 1);
1284 if (fp_is_glob_file) {
1285 while (*p && (g_ascii_isspace(*p)||(*p=='*')||(*p=='.'))) p++;
1287 while (*p && g_ascii_isspace(*p)) p++;
1291 mime_type->extension = g_utf8_strdown(p, -1);
1293 mime_type->extension = NULL;
1295 list = g_list_append(list, mime_type);
1298 procmime_fclose(fp);
1301 g_warning("Can't read mime.types");
1306 EncodingType procmime_get_encoding_for_charset(const gchar *charset)
1310 else if (!g_ascii_strncasecmp(charset, "ISO-2022-", 9) ||
1311 !g_ascii_strcasecmp(charset, "US-ASCII"))
1313 else if (!g_ascii_strcasecmp(charset, "ISO-8859-5") ||
1314 !g_ascii_strncasecmp(charset, "KOI8-", 5) ||
1315 !g_ascii_strcasecmp(charset, "X-MAC-CYRILLIC") ||
1316 !g_ascii_strcasecmp(charset, "MAC-CYRILLIC") ||
1317 !g_ascii_strcasecmp(charset, "Windows-1251"))
1319 else if (!g_ascii_strncasecmp(charset, "ISO-8859-", 9))
1320 return ENC_QUOTED_PRINTABLE;
1321 else if (!g_ascii_strncasecmp(charset, "UTF-8", 5))
1322 return ENC_QUOTED_PRINTABLE;
1327 EncodingType procmime_get_encoding_for_text_file(const gchar *file, gboolean *has_binary)
1330 guchar buf[BUFFSIZE];
1332 size_t octet_chars = 0;
1333 size_t total_len = 0;
1334 gfloat octet_percentage;
1335 gboolean force_b64 = FALSE;
1337 if ((fp = procmime_fopen(file, "rb")) == NULL) {
1338 FILE_OP_ERROR(file, "fopen");
1342 while ((len = SC_FREAD(buf, sizeof(guchar), sizeof(buf), fp)) > 0) {
1346 for (p = buf, i = 0; i < len; ++p, ++i) {
1357 procmime_fclose(fp);
1360 octet_percentage = (gfloat)octet_chars / (gfloat)total_len;
1362 octet_percentage = 0.0;
1364 debug_print("procmime_get_encoding_for_text_file(): "
1365 "8bit chars: %zd / %zd (%f%%)\n", octet_chars, total_len,
1366 100.0 * octet_percentage);
1368 if (octet_percentage > 0.20 || force_b64) {
1369 debug_print("using BASE64\n");
1371 } else if (octet_chars > 0) {
1372 debug_print("using quoted-printable\n");
1373 return ENC_QUOTED_PRINTABLE;
1375 debug_print("using 7bit\n");
1380 struct EncodingTable
1383 EncodingType enc_type;
1386 struct EncodingTable encoding_table[] = {
1389 {"binary", ENC_BINARY},
1390 {"quoted-printable", ENC_QUOTED_PRINTABLE},
1391 {"base64", ENC_BASE64},
1392 {"x-uuencode", ENC_UNKNOWN},
1393 {NULL, ENC_UNKNOWN},
1396 const gchar *procmime_get_encoding_str(EncodingType encoding)
1398 struct EncodingTable *enc_table;
1400 for (enc_table = encoding_table; enc_table->str != NULL; enc_table++) {
1401 if (enc_table->enc_type == encoding)
1402 return enc_table->str;
1407 /* --- NEW MIME STUFF --- */
1414 static struct TypeTable mime_type_table[] = {
1415 {"text", MIMETYPE_TEXT},
1416 {"image", MIMETYPE_IMAGE},
1417 {"audio", MIMETYPE_AUDIO},
1418 {"video", MIMETYPE_VIDEO},
1419 {"application", MIMETYPE_APPLICATION},
1420 {"message", MIMETYPE_MESSAGE},
1421 {"multipart", MIMETYPE_MULTIPART},
1425 const gchar *procmime_get_media_type_str(MimeMediaType type)
1427 struct TypeTable *type_table;
1429 for (type_table = mime_type_table; type_table->str != NULL; type_table++) {
1430 if (type_table->type == type)
1431 return type_table->str;
1436 MimeMediaType procmime_get_media_type(const gchar *str)
1438 struct TypeTable *typetablearray;
1440 for (typetablearray = mime_type_table; typetablearray->str != NULL; typetablearray++)
1441 if (g_ascii_strncasecmp(str, typetablearray->str, strlen(typetablearray->str)) == 0)
1442 return typetablearray->type;
1444 return MIMETYPE_UNKNOWN;
1448 *\brief Safe wrapper for content type string.
1450 *\return const gchar * Pointer to content type string.
1452 gchar *procmime_get_content_type_str(MimeMediaType type,
1453 const char *subtype)
1455 const gchar *type_str = NULL;
1457 if (subtype == NULL || !(type_str = procmime_get_media_type_str(type)))
1458 return g_strdup("unknown");
1459 return g_strdup_printf("%s/%s", type_str, subtype);
1462 static int procmime_parse_mimepart(MimeInfo *parent,
1463 gchar *content_type,
1464 gchar *content_encoding,
1465 gchar *content_description,
1467 gchar *content_disposition,
1468 gchar *content_location,
1469 const gchar *original_msgid,
1470 const gchar *disposition_notification_hdr,
1471 const gchar *filename,
1474 gboolean short_scan);
1476 static void procmime_parse_message_rfc822(MimeInfo *mimeinfo, gboolean short_scan)
1478 HeaderEntry hentry[] = {{"Content-Type:", NULL, TRUE},
1479 {"Content-Transfer-Encoding:",
1481 {"Content-Description:",
1485 {"Content-Disposition:",
1487 {"Content-Location:",
1491 {"Original-Message-ID:",
1495 {NULL, NULL, FALSE}};
1496 guint content_start, i;
1501 procmime_decode_content(mimeinfo);
1503 fp = procmime_fopen(mimeinfo->data.filename, "rb");
1505 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
1508 if (fseek(fp, mimeinfo->offset, SEEK_SET) < 0) {
1509 FILE_OP_ERROR(mimeinfo->data.filename, "fseek");
1510 procmime_fclose(fp);
1513 procheader_get_header_fields(fp, hentry);
1514 if (hentry[0].body != NULL) {
1515 tmp = conv_unmime_header(hentry[0].body, NULL, FALSE);
1516 g_free(hentry[0].body);
1517 hentry[0].body = tmp;
1519 if (hentry[2].body != NULL) {
1520 tmp = conv_unmime_header(hentry[2].body, NULL, FALSE);
1521 g_free(hentry[2].body);
1522 hentry[2].body = tmp;
1524 if (hentry[4].body != NULL) {
1525 tmp = conv_unmime_header(hentry[4].body, NULL, FALSE);
1526 g_free(hentry[4].body);
1527 hentry[4].body = tmp;
1529 if (hentry[5].body != NULL) {
1530 tmp = conv_unmime_header(hentry[5].body, NULL, FALSE);
1531 g_free(hentry[5].body);
1532 hentry[5].body = tmp;
1534 if (hentry[7].body != NULL) {
1535 tmp = conv_unmime_header(hentry[7].body, NULL, FALSE);
1536 g_free(hentry[7].body);
1537 hentry[7].body = tmp;
1539 if (hentry[8].body != NULL) {
1540 tmp = conv_unmime_header(hentry[8].body, NULL, FALSE);
1541 g_free(hentry[8].body);
1542 hentry[8].body = tmp;
1545 content_start = ftell(fp);
1546 procmime_fclose(fp);
1548 len = mimeinfo->length - (content_start - mimeinfo->offset);
1551 procmime_parse_mimepart(mimeinfo,
1552 hentry[0].body, hentry[1].body,
1553 hentry[2].body, hentry[3].body,
1554 hentry[4].body, hentry[5].body,
1555 hentry[7].body, hentry[8].body,
1556 mimeinfo->data.filename, content_start,
1559 for (i = 0; i < (sizeof hentry / sizeof hentry[0]); i++) {
1560 g_free(hentry[i].body);
1561 hentry[i].body = NULL;
1565 static void procmime_parse_disposition_notification(MimeInfo *mimeinfo,
1566 const gchar *original_msgid, const gchar *disposition_notification_hdr,
1567 gboolean short_scan)
1569 HeaderEntry hentry[] = {{"Original-Message-ID:", NULL, TRUE},
1570 {"Disposition:", NULL, TRUE},
1571 {NULL, NULL, FALSE}};
1574 gchar *orig_msg_id = NULL;
1577 procmime_decode_content(mimeinfo);
1579 debug_print("parse disposition notification\n");
1580 fp = procmime_fopen(mimeinfo->data.filename, "rb");
1582 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
1585 if (fseek(fp, mimeinfo->offset, SEEK_SET) < 0) {
1586 FILE_OP_ERROR(mimeinfo->data.filename, "fseek");
1587 procmime_fclose(fp);
1591 if (original_msgid && disposition_notification_hdr) {
1592 hentry[0].body = g_strdup(original_msgid);
1593 hentry[1].body = g_strdup(disposition_notification_hdr);
1595 procheader_get_header_fields(fp, hentry);
1598 procmime_fclose(fp);
1600 if (!hentry[0].body || !hentry[1].body) {
1601 debug_print("MsgId %s, Disp %s\n",
1602 hentry[0].body ? hentry[0].body:"(nil)",
1603 hentry[1].body ? hentry[1].body:"(nil)");
1607 orig_msg_id = g_strdup(hentry[0].body);
1608 disp = g_strdup(hentry[1].body);
1610 extract_parenthesis(orig_msg_id, '<', '>');
1611 remove_space(orig_msg_id);
1613 if (strstr(disp, "displayed")) {
1614 /* find sent message, if possible */
1615 MsgInfo *info = NULL;
1617 debug_print("%s has been displayed.\n", orig_msg_id);
1618 for (flist = folder_get_list(); flist != NULL; flist = g_list_next(flist)) {
1619 FolderItem *outbox = ((Folder *)(flist->data))->outbox;
1621 debug_print("skipping folder with no outbox...\n");
1624 info = folder_item_get_msginfo_by_msgid(outbox, orig_msg_id);
1625 debug_print("%s %s in %s\n", info?"found":"didn't find", orig_msg_id, outbox->path);
1627 procmsg_msginfo_set_flags(info, MSG_RETRCPT_GOT, 0);
1628 procmsg_msginfo_free(&info);
1632 g_free(orig_msg_id);
1635 for (i = 0; i < (sizeof hentry / sizeof hentry[0]); i++) {
1636 g_free(hentry[i].body);
1637 hentry[i].body = NULL;
1641 #define GET_HEADERS() { \
1642 procheader_get_header_fields(fp, hentry); \
1643 if (hentry[0].body != NULL) { \
1644 tmp = conv_unmime_header(hentry[0].body, NULL, FALSE); \
1645 g_free(hentry[0].body); \
1646 hentry[0].body = tmp; \
1648 if (hentry[2].body != NULL) { \
1649 tmp = conv_unmime_header(hentry[2].body, NULL, FALSE); \
1650 g_free(hentry[2].body); \
1651 hentry[2].body = tmp; \
1653 if (hentry[4].body != NULL) { \
1654 tmp = conv_unmime_header(hentry[4].body, NULL, FALSE); \
1655 g_free(hentry[4].body); \
1656 hentry[4].body = tmp; \
1658 if (hentry[5].body != NULL) { \
1659 tmp = conv_unmime_header(hentry[5].body, NULL, FALSE); \
1660 g_free(hentry[5].body); \
1661 hentry[5].body = tmp; \
1663 if (hentry[6].body != NULL) { \
1664 tmp = conv_unmime_header(hentry[6].body, NULL, FALSE); \
1665 g_free(hentry[6].body); \
1666 hentry[6].body = tmp; \
1668 if (hentry[7].body != NULL) { \
1669 tmp = conv_unmime_header(hentry[7].body, NULL, FALSE); \
1670 g_free(hentry[7].body); \
1671 hentry[7].body = tmp; \
1675 static void procmime_parse_multipart(MimeInfo *mimeinfo, gboolean short_scan)
1677 HeaderEntry hentry[] = {{"Content-Type:", NULL, TRUE},
1678 {"Content-Transfer-Encoding:",
1680 {"Content-Description:",
1684 {"Content-Disposition:",
1686 {"Content-Location:",
1688 {"Original-Message-ID:",
1692 {NULL, NULL, FALSE}};
1695 gint boundary_len = 0, lastoffset = -1, i;
1696 gchar buf[BUFFSIZE];
1699 gboolean start_found = FALSE;
1700 gboolean end_found = FALSE;
1702 boundary = g_hash_table_lookup(mimeinfo->typeparameters, "boundary");
1705 boundary_len = strlen(boundary);
1707 procmime_decode_content(mimeinfo);
1709 fp = procmime_fopen(mimeinfo->data.filename, "rb");
1711 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
1715 if (fseek(fp, mimeinfo->offset, SEEK_SET) < 0) {
1716 FILE_OP_ERROR(mimeinfo->data.filename, "fseek");
1717 procmime_fclose(fp);
1721 while (SC_FGETS(buf, sizeof(buf), fp) != NULL && result == 0) {
1722 if (ftell(fp) - 1 > (mimeinfo->offset + mimeinfo->length))
1725 if (IS_BOUNDARY(buf, boundary, boundary_len)) {
1728 if (lastoffset != -1) {
1729 gint len = (ftell(fp) - strlen(buf)) - lastoffset - 1;
1732 result = procmime_parse_mimepart(mimeinfo,
1733 hentry[0].body, hentry[1].body,
1734 hentry[2].body, hentry[3].body,
1735 hentry[4].body, hentry[5].body,
1736 hentry[6].body, hentry[7].body,
1737 mimeinfo->data.filename, lastoffset,
1739 if (result == 1 && short_scan)
1744 if (buf[2 + boundary_len] == '-' &&
1745 buf[2 + boundary_len + 1] == '-') {
1749 for (i = 0; i < (sizeof hentry / sizeof hentry[0]) ; i++) {
1750 g_free(hentry[i].body);
1751 hentry[i].body = NULL;
1754 lastoffset = ftell(fp);
1758 if (start_found && !end_found && lastoffset != -1) {
1759 gint len = (ftell(fp) - strlen(buf)) - lastoffset - 1;
1762 result = procmime_parse_mimepart(mimeinfo,
1763 hentry[0].body, hentry[1].body,
1764 hentry[2].body, hentry[3].body,
1765 hentry[4].body, hentry[5].body,
1766 hentry[6].body, hentry[7].body,
1767 mimeinfo->data.filename, lastoffset,
1770 mimeinfo->broken = TRUE;
1773 for (i = 0; i < (sizeof hentry / sizeof hentry[0]); i++) {
1774 g_free(hentry[i].body);
1775 hentry[i].body = NULL;
1777 procmime_fclose(fp);
1780 static void parse_parameters(const gchar *parameters, GHashTable *table)
1782 gchar *params, *param, *next;
1783 GSList *convlist = NULL, *concatlist = NULL, *cur;
1785 params = g_strdup(parameters);
1788 for (; next != NULL; param = next) {
1789 gchar *attribute, *value, *tmp, *down_attr, *orig_down_attr;
1791 gboolean convert = FALSE;
1793 next = strchr_with_skip_quote(param, '"', ';');
1802 value = strchr(attribute, '=');
1808 while (value[0] != '\0' && value[0] == ' ')
1811 down_attr = g_utf8_strdown(attribute, -1);
1812 orig_down_attr = down_attr;
1814 len = down_attr ? strlen(down_attr):0;
1815 if (len > 0 && down_attr[len - 1] == '*') {
1816 gchar *srcpos, *dstpos, *endpos;
1819 down_attr[len - 1] = '\0';
1823 endpos = value + strlen(value);
1824 while (srcpos < endpos) {
1830 if (!get_hex_value(&dstvalue, srcpos[1], srcpos[2]))
1840 if (value[0] == '"')
1841 extract_quote(value, '"');
1843 if (value[0] == '"')
1844 extract_quote(value, '"');
1845 else if ((tmp = strchr(value, ' ')) != NULL)
1850 while (down_attr[0] == ' ')
1852 while (down_attr[strlen(down_attr)-1] == ' ')
1853 down_attr[strlen(down_attr)-1] = '\0';
1856 while (value[0] != '\0' && value[0] == ' ')
1858 while (value[strlen(value)-1] == ' ')
1859 value[strlen(value)-1] = '\0';
1861 if (down_attr && strrchr(down_attr, '*') != NULL) {
1864 tmpattr = g_strdup(down_attr);
1865 tmp = strrchr(tmpattr, '*');
1868 if ((tmp[1] == '0') && (tmp[2] == '\0') &&
1869 (g_slist_find_custom(concatlist, down_attr, (GCompareFunc)g_strcmp0) == NULL))
1870 concatlist = g_slist_prepend(concatlist, g_strdup(tmpattr));
1872 if (convert && (g_slist_find_custom(convlist, tmpattr, (GCompareFunc)g_strcmp0) == NULL))
1873 convlist = g_slist_prepend(convlist, g_strdup(tmpattr));
1876 } else if (convert) {
1877 if (g_slist_find_custom(convlist, down_attr, (GCompareFunc)g_strcmp0) == NULL)
1878 convlist = g_slist_prepend(convlist, g_strdup(down_attr));
1881 if (g_hash_table_lookup(table, down_attr) == NULL)
1882 g_hash_table_insert(table, g_strdup(down_attr), g_strdup(value));
1883 g_free(orig_down_attr);
1886 for (cur = concatlist; cur != NULL; cur = g_slist_next(cur)) {
1887 gchar *attribute, *attrwnum, *partvalue;
1891 attribute = (gchar *) cur->data;
1892 value = g_string_sized_new(64);
1894 attrwnum = g_strdup_printf("%s*%d", attribute, n);
1895 while ((partvalue = g_hash_table_lookup(table, attrwnum)) != NULL) {
1896 g_string_append(value, partvalue);
1898 g_hash_table_remove(table, attrwnum);
1901 attrwnum = g_strdup_printf("%s*%d", attribute, n);
1905 g_hash_table_insert(table, g_strdup(attribute), g_strdup(value->str));
1906 g_string_free(value, TRUE);
1908 slist_free_strings_full(concatlist);
1910 for (cur = convlist; cur != NULL; cur = g_slist_next(cur)) {
1911 gchar *attribute, *key, *value;
1912 gchar *charset, *lang, *oldvalue, *newvalue;
1914 attribute = (gchar *) cur->data;
1915 if (!g_hash_table_lookup_extended(
1916 table, attribute, (gpointer *)(gchar *) &key, (gpointer *)(gchar *) &value))
1920 if (charset == NULL)
1922 lang = strchr(charset, '\'');
1927 oldvalue = strchr(lang, '\'');
1928 if (oldvalue == NULL)
1933 newvalue = conv_codeset_strdup(oldvalue, charset, CS_UTF_8);
1935 g_hash_table_remove(table, attribute);
1939 g_hash_table_insert(table, g_strdup(attribute), newvalue);
1941 slist_free_strings_full(convlist);
1946 static void procmime_parse_content_type(const gchar *content_type, MimeInfo *mimeinfo)
1948 cm_return_if_fail(content_type != NULL);
1949 cm_return_if_fail(mimeinfo != NULL);
1951 /* RFC 2045, page 13 says that the mime subtype is MANDATORY;
1952 * if it's not available we use the default Content-Type */
1953 if ((content_type[0] == '\0') || (strchr(content_type, '/') == NULL)) {
1954 mimeinfo->type = MIMETYPE_TEXT;
1955 mimeinfo->subtype = g_strdup("plain");
1956 if (g_hash_table_lookup(mimeinfo->typeparameters,
1957 "charset") == NULL) {
1958 g_hash_table_insert(mimeinfo->typeparameters,
1959 g_strdup("charset"),
1961 conv_get_locale_charset_str_no_utf8()));
1964 gchar *type, *subtype, *params;
1966 type = g_strdup(content_type);
1967 subtype = strchr(type, '/') + 1;
1968 *(subtype - 1) = '\0';
1969 if ((params = strchr(subtype, ';')) != NULL) {
1974 mimeinfo->type = procmime_get_media_type(type);
1975 mimeinfo->subtype = g_strstrip(g_strdup(subtype));
1977 /* Get mimeinfo->typeparameters */
1979 parse_parameters(params, mimeinfo->typeparameters);
1985 static void procmime_parse_content_disposition(const gchar *content_disposition, MimeInfo *mimeinfo)
1987 gchar *tmp, *params;
1989 cm_return_if_fail(content_disposition != NULL);
1990 cm_return_if_fail(mimeinfo != NULL);
1992 tmp = g_strdup(content_disposition);
1993 if ((params = strchr(tmp, ';')) != NULL) {
1999 if (!g_ascii_strcasecmp(tmp, "inline"))
2000 mimeinfo->disposition = DISPOSITIONTYPE_INLINE;
2001 else if (!g_ascii_strcasecmp(tmp, "attachment"))
2002 mimeinfo->disposition = DISPOSITIONTYPE_ATTACHMENT;
2004 mimeinfo->disposition = DISPOSITIONTYPE_ATTACHMENT;
2007 parse_parameters(params, mimeinfo->dispositionparameters);
2013 static void procmime_parse_content_encoding(const gchar *content_encoding, MimeInfo *mimeinfo)
2015 struct EncodingTable *enc_table;
2017 for (enc_table = encoding_table; enc_table->str != NULL; enc_table++) {
2018 if (g_ascii_strcasecmp(enc_table->str, content_encoding) == 0) {
2019 mimeinfo->encoding_type = enc_table->enc_type;
2023 mimeinfo->encoding_type = ENC_UNKNOWN;
2027 static GSList *registered_parsers = NULL;
2029 static MimeParser *procmime_get_mimeparser_for_type(MimeMediaType type, const gchar *sub_type)
2032 for (cur = registered_parsers; cur; cur = cur->next) {
2033 MimeParser *parser = (MimeParser *)cur->data;
2034 if (parser->type == type && !strcmp2(parser->sub_type, sub_type))
2040 void procmime_mimeparser_register(MimeParser *parser)
2042 if (!procmime_get_mimeparser_for_type(parser->type, parser->sub_type))
2043 registered_parsers = g_slist_append(registered_parsers, parser);
2047 void procmime_mimeparser_unregister(MimeParser *parser)
2049 registered_parsers = g_slist_remove(registered_parsers, parser);
2052 static gboolean procmime_mimeparser_parse(MimeParser *parser, MimeInfo *mimeinfo)
2054 cm_return_val_if_fail(parser->parse != NULL, FALSE);
2055 return parser->parse(parser, mimeinfo);
2058 static int procmime_parse_mimepart(MimeInfo *parent,
2059 gchar *content_type,
2060 gchar *content_encoding,
2061 gchar *content_description,
2063 gchar *content_disposition,
2064 gchar *content_location,
2065 const gchar *original_msgid,
2066 const gchar *disposition_notification_hdr,
2067 const gchar *filename,
2070 gboolean short_scan)
2073 MimeParser *parser = NULL;
2074 gboolean parsed = FALSE;
2077 /* Create MimeInfo */
2078 mimeinfo = procmime_mimeinfo_new();
2079 mimeinfo->content = MIMECONTENT_FILE;
2081 if (parent != NULL) {
2082 if (g_node_depth(parent->node) > 32) {
2083 /* 32 is an arbitrary value
2084 * this avoids DOSsing ourselves
2085 * with enormous messages
2087 procmime_mimeinfo_free_all(&mimeinfo);
2090 g_node_append(parent->node, mimeinfo->node);
2092 mimeinfo->data.filename = g_strdup(filename);
2093 mimeinfo->offset = offset;
2094 mimeinfo->length = length;
2096 if (content_type != NULL) {
2097 g_strchomp(content_type);
2098 procmime_parse_content_type(content_type, mimeinfo);
2100 mimeinfo->type = MIMETYPE_TEXT;
2101 mimeinfo->subtype = g_strdup("plain");
2102 if (g_hash_table_lookup(mimeinfo->typeparameters,
2103 "charset") == NULL) {
2104 g_hash_table_insert(mimeinfo->typeparameters,
2105 g_strdup("charset"),
2107 conv_get_locale_charset_str_no_utf8()));
2111 if (content_encoding != NULL) {
2112 g_strchomp(content_encoding);
2113 procmime_parse_content_encoding(content_encoding, mimeinfo);
2115 mimeinfo->encoding_type = ENC_UNKNOWN;
2118 if (content_description != NULL)
2119 mimeinfo->description = g_strdup(content_description);
2121 mimeinfo->description = NULL;
2123 if (content_id != NULL)
2124 mimeinfo->id = g_strdup(content_id);
2126 mimeinfo->id = NULL;
2128 if (content_location != NULL)
2129 mimeinfo->location = g_strdup(content_location);
2131 mimeinfo->location = NULL;
2133 if (content_disposition != NULL) {
2134 g_strchomp(content_disposition);
2135 procmime_parse_content_disposition(content_disposition, mimeinfo);
2137 mimeinfo->disposition = DISPOSITIONTYPE_UNKNOWN;
2139 /* Call parser for mime type */
2140 if ((parser = procmime_get_mimeparser_for_type(mimeinfo->type, mimeinfo->subtype)) != NULL) {
2141 parsed = procmime_mimeparser_parse(parser, mimeinfo);
2144 switch (mimeinfo->type) {
2146 if (g_ascii_strcasecmp(mimeinfo->subtype, "plain") == 0 && short_scan) {
2151 case MIMETYPE_MESSAGE:
2152 if (g_ascii_strcasecmp(mimeinfo->subtype, "rfc822") == 0) {
2153 procmime_parse_message_rfc822(mimeinfo, short_scan);
2155 if (g_ascii_strcasecmp(mimeinfo->subtype, "disposition-notification") == 0) {
2156 procmime_parse_disposition_notification(mimeinfo,
2157 original_msgid, disposition_notification_hdr, short_scan);
2161 case MIMETYPE_MULTIPART:
2162 procmime_parse_multipart(mimeinfo, short_scan);
2165 case MIMETYPE_APPLICATION:
2166 if (g_ascii_strcasecmp(mimeinfo->subtype, "octet-stream") == 0
2167 && original_msgid && *original_msgid
2168 && disposition_notification_hdr && *disposition_notification_hdr) {
2169 procmime_parse_disposition_notification(mimeinfo,
2170 original_msgid, disposition_notification_hdr, short_scan);
2181 static gchar *typenames[] = {
2192 static gboolean output_func(GNode *node, gpointer data)
2195 MimeInfo *mimeinfo = (MimeInfo *) node->data;
2197 depth = g_node_depth(node);
2198 for (i = 0; i < depth; i++)
2200 g_print("%s/%s (offset:%d length:%d encoding: %d)\n", typenames[mimeinfo->type], mimeinfo->subtype, mimeinfo->offset, mimeinfo->length, mimeinfo->encoding_type);
2205 static void output_mime_structure(MimeInfo *mimeinfo, int indent)
2207 g_node_traverse(mimeinfo->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1, output_func, NULL);
2210 static MimeInfo *procmime_scan_file_with_offset(const gchar *filename, int offset, gboolean short_scan)
2215 if (g_stat(filename, &buf) < 0) {
2216 FILE_OP_ERROR(filename, "stat");
2220 mimeinfo = procmime_mimeinfo_new();
2221 mimeinfo->content = MIMECONTENT_FILE;
2222 mimeinfo->encoding_type = ENC_UNKNOWN;
2223 mimeinfo->type = MIMETYPE_MESSAGE;
2224 mimeinfo->subtype = g_strdup("rfc822");
2225 mimeinfo->data.filename = g_strdup(filename);
2226 mimeinfo->offset = offset;
2227 mimeinfo->length = buf.st_size - offset;
2229 procmime_parse_message_rfc822(mimeinfo, short_scan);
2230 if (debug_get_mode())
2231 output_mime_structure(mimeinfo, 0);
2236 static MimeInfo *procmime_scan_file_full(const gchar *filename, gboolean short_scan)
2240 cm_return_val_if_fail(filename != NULL, NULL);
2242 mimeinfo = procmime_scan_file_with_offset(filename, 0, short_scan);
2247 MimeInfo *procmime_scan_file(const gchar *filename)
2249 return procmime_scan_file_full(filename, FALSE);
2252 static MimeInfo *procmime_scan_file_short(const gchar *filename)
2254 return procmime_scan_file_full(filename, TRUE);
2257 static MimeInfo *procmime_scan_queue_file_full(const gchar *filename, gboolean short_scan)
2261 gchar buf[BUFFSIZE];
2264 cm_return_val_if_fail(filename != NULL, NULL);
2267 if ((fp = procmime_fopen(filename, "rb")) == NULL)
2269 /* Skip queue header */
2270 while (SC_FGETS(buf, sizeof(buf), fp) != NULL) {
2272 if ((!strncmp(buf, "X-Claws-End-Special-Headers: 1",
2273 strlen("X-Claws-End-Special-Headers:"))) ||
2274 (!strncmp(buf, "X-Sylpheed-End-Special-Headers: 1",
2275 strlen("X-Sylpheed-End-Special-Headers:"))))
2278 if (buf[0] == '\r' || buf[0] == '\n') break;
2279 /* from other mailers */
2280 if (!strncmp(buf, "Date: ", 6)
2281 || !strncmp(buf, "To: ", 4)
2282 || !strncmp(buf, "From: ", 6)
2283 || !strncmp(buf, "Subject: ", 9)) {
2289 procmime_fclose(fp);
2291 mimeinfo = procmime_scan_file_with_offset(filename, offset, short_scan);
2296 MimeInfo *procmime_scan_queue_file(const gchar *filename)
2298 return procmime_scan_queue_file_full(filename, FALSE);
2301 static MimeInfo *procmime_scan_queue_file_short(const gchar *filename)
2303 return procmime_scan_queue_file_full(filename, TRUE);
2308 ENC_AS_QUOTED_STRING,
2313 typedef struct _ParametersData {
2319 static void write_parameters(gpointer key, gpointer value, gpointer user_data)
2322 gchar *val = value, *valpos, *tmp;
2323 ParametersData *pdata = (ParametersData *)user_data;
2324 GString *buf = g_string_new("");
2327 EncodeAs encas = ENC_AS_TOKEN;
2329 for (valpos = val; *valpos != 0; valpos++) {
2330 if (!IS_ASCII(*valpos)) {
2331 encas = ENC_AS_ENCWORD;
2336 if (((*valpos >= 0) && (*valpos < 037)) || (*valpos == 0177)) {
2337 encas = ENC_AS_QUOTED_STRING;
2341 /* tspecials + SPACE */
2359 encas = ENC_AS_QUOTED_STRING;
2366 g_string_append_printf(buf, "%s=%s", param, val);
2369 case ENC_AS_QUOTED_STRING:
2370 g_string_append_printf(buf, "%s=\"%s\"", param, val);
2373 #if 0 /* we don't use that for now */
2374 case ENC_AS_EXTENDED:
2375 if (!g_utf8_validate(val, -1, NULL))
2376 g_string_append_printf(buf, "%s*=%s''", param,
2377 conv_get_locale_charset_str());
2379 g_string_append_printf(buf, "%s*=%s''", param,
2381 for (valpos = val; *valpos != '\0'; valpos++) {
2382 if (IS_ASCII(*valpos) && isalnum(*valpos)) {
2383 g_string_append_printf(buf, "%c", *valpos);
2385 gchar hexstr[3] = "XX";
2386 get_hex_str(hexstr, *valpos);
2387 g_string_append_printf(buf, "%%%s", hexstr);
2392 case ENC_AS_EXTENDED:
2393 debug_print("Unhandled ENC_AS_EXTENDED.\n");
2396 case ENC_AS_ENCWORD:
2397 len = MAX(strlen(val)*6, 512);
2398 tmp = g_malloc(len+1);
2399 codeconv_set_strict(TRUE);
2400 conv_encode_header_full(tmp, len, val, pdata->len + strlen(param) + 4 , FALSE,
2401 prefs_common.outgoing_charset);
2402 codeconv_set_strict(FALSE);
2403 if (!tmp || !*tmp) {
2404 codeconv_set_strict(TRUE);
2405 conv_encode_header_full(tmp, len, val, pdata->len + strlen(param) + 4 , FALSE,
2406 conv_get_outgoing_charset_str());
2407 codeconv_set_strict(FALSE);
2409 if (!tmp || !*tmp) {
2410 codeconv_set_strict(TRUE);
2411 conv_encode_header_full(tmp, len, val, pdata->len + strlen(param) + 4 , FALSE,
2413 codeconv_set_strict(FALSE);
2415 if (!tmp || !*tmp) {
2416 conv_encode_header_full(tmp, len, val, pdata->len + strlen(param) + 4 , FALSE,
2419 g_string_append_printf(buf, "%s=\"%s\"", param, tmp);
2425 if (buf->str && strlen(buf->str)) {
2426 tmp = strstr(buf->str, "\n");
2428 len = (tmp - buf->str);
2430 len = strlen(buf->str);
2431 if (pdata->len + len > 76) {
2432 if (fprintf(pdata->fp, ";\n %s", buf->str) < 0)
2433 pdata->error = TRUE;
2434 pdata->len = strlen(buf->str) + 1;
2436 if (fprintf(pdata->fp, "; %s", buf->str) < 0)
2437 pdata->error = TRUE;
2438 pdata->len += strlen(buf->str) + 2;
2441 g_string_free(buf, TRUE);
2444 #define TRY(func) { \
2450 int procmime_write_mime_header(MimeInfo *mimeinfo, FILE *fp)
2452 struct TypeTable *type_table;
2453 ParametersData *pdata = g_new0(ParametersData, 1);
2454 debug_print("procmime_write_mime_header\n");
2457 pdata->error = FALSE;
2458 for (type_table = mime_type_table; type_table->str != NULL; type_table++)
2459 if (mimeinfo->type == type_table->type) {
2460 gchar *buf = g_strdup_printf(
2461 "Content-Type: %s/%s", type_table->str, mimeinfo->subtype);
2462 if (fprintf(fp, "%s", buf) < 0) {
2467 pdata->len = strlen(buf);
2471 g_hash_table_foreach(mimeinfo->typeparameters, write_parameters, pdata);
2472 if (pdata->error == TRUE) {
2478 TRY(fprintf(fp, "\n") >= 0);
2480 if (mimeinfo->encoding_type != ENC_UNKNOWN)
2481 TRY(fprintf(fp, "Content-Transfer-Encoding: %s\n", procmime_get_encoding_str(mimeinfo->encoding_type)) >= 0);
2483 if (mimeinfo->description != NULL)
2484 TRY(fprintf(fp, "Content-Description: %s\n", mimeinfo->description) >= 0);
2486 if (mimeinfo->id != NULL)
2487 TRY(fprintf(fp, "Content-ID: %s\n", mimeinfo->id) >= 0);
2489 if (mimeinfo->location != NULL)
2490 TRY(fprintf(fp, "Content-Location: %s\n", mimeinfo->location) >= 0);
2492 if (mimeinfo->disposition != DISPOSITIONTYPE_UNKNOWN) {
2493 ParametersData *pdata = g_new0(ParametersData, 1);
2495 if (mimeinfo->disposition == DISPOSITIONTYPE_INLINE)
2496 buf = g_strdup("Content-Disposition: inline");
2497 else if (mimeinfo->disposition == DISPOSITIONTYPE_ATTACHMENT)
2498 buf = g_strdup("Content-Disposition: attachment");
2500 buf = g_strdup("Content-Disposition: unknown");
2502 if (fprintf(fp, "%s", buf) < 0) {
2507 pdata->len = strlen(buf);
2511 pdata->error = FALSE;
2512 g_hash_table_foreach(mimeinfo->dispositionparameters, write_parameters, pdata);
2513 if (pdata->error == TRUE) {
2518 TRY(fprintf(fp, "\n") >= 0);
2521 TRY(fprintf(fp, "\n") >= 0);
2526 static gint procmime_write_message_rfc822(MimeInfo *mimeinfo, FILE *fp)
2531 gchar buf[BUFFSIZE];
2532 gboolean skip = FALSE;;
2535 debug_print("procmime_write_message_rfc822\n");
2538 switch (mimeinfo->content) {
2539 case MIMECONTENT_FILE:
2540 if ((infp = procmime_fopen(mimeinfo->data.filename, "rb")) == NULL) {
2541 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
2544 if (fseek(infp, mimeinfo->offset, SEEK_SET) < 0) {
2545 FILE_OP_ERROR(mimeinfo->data.filename, "fseek");
2546 procmime_fclose(infp);
2549 while (SC_FGETS(buf, sizeof(buf), infp) == buf) {
2551 if (buf[0] == '\n' && buf[1] == '\0')
2553 if (skip && (buf[0] == ' ' || buf[0] == '\t'))
2555 if (g_ascii_strncasecmp(buf, "MIME-Version:", 13) == 0 ||
2556 g_ascii_strncasecmp(buf, "Content-Type:", 13) == 0 ||
2557 g_ascii_strncasecmp(buf, "Content-Transfer-Encoding:", 26) == 0 ||
2558 g_ascii_strncasecmp(buf, "Content-Description:", 20) == 0 ||
2559 g_ascii_strncasecmp(buf, "Content-ID:", 11) == 0 ||
2560 g_ascii_strncasecmp(buf, "Content-Location:", 17) == 0 ||
2561 g_ascii_strncasecmp(buf, "Content-Disposition:", 20) == 0) {
2566 if (SC_FWRITE(buf, sizeof(gchar), len, fp) < len) {
2567 g_warning("failed to dump %zd bytes from file", len);
2568 procmime_fclose(infp);
2573 procmime_fclose(infp);
2576 case MIMECONTENT_MEM:
2577 len = strlen(mimeinfo->data.mem);
2578 if (SC_FWRITE(mimeinfo->data.mem, sizeof(gchar), len, fp) < len) {
2579 g_warning("failed to dump %zd bytes from mem", len);
2588 childnode = mimeinfo->node->children;
2589 if (childnode == NULL)
2592 child = (MimeInfo *) childnode->data;
2593 if (fprintf(fp, "MIME-Version: 1.0\n") < 0) {
2594 g_warning("failed to write mime version");
2597 if (procmime_write_mime_header(child, fp) < 0)
2599 return procmime_write_mimeinfo(child, fp);
2602 static gint procmime_write_multipart(MimeInfo *mimeinfo, FILE *fp)
2606 gchar *boundary, *str, *str2;
2607 gchar buf[BUFFSIZE];
2608 gboolean firstboundary;
2611 debug_print("procmime_write_multipart\n");
2613 boundary = g_hash_table_lookup(mimeinfo->typeparameters, "boundary");
2615 switch (mimeinfo->content) {
2616 case MIMECONTENT_FILE:
2617 if ((infp = procmime_fopen(mimeinfo->data.filename, "rb")) == NULL) {
2618 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
2621 if (fseek(infp, mimeinfo->offset, SEEK_SET) < 0) {
2622 FILE_OP_ERROR(mimeinfo->data.filename, "fseek");
2623 procmime_fclose(infp);
2626 while (SC_FGETS(buf, sizeof(buf), infp) == buf) {
2627 if (IS_BOUNDARY(buf, boundary, strlen(boundary)))
2630 if (SC_FWRITE(buf, sizeof(gchar), len, fp) < len) {
2631 g_warning("failed to write %zd", len);
2632 procmime_fclose(infp);
2636 procmime_fclose(infp);
2639 case MIMECONTENT_MEM:
2640 str = g_strdup(mimeinfo->data.mem);
2641 if (((str2 = strstr(str, boundary)) != NULL) && ((str2 - str) >= 2) &&
2642 (*(str2 - 1) == '-') && (*(str2 - 2) == '-'))
2645 if (SC_FWRITE(str, sizeof(gchar), len, fp) < len) {
2646 g_warning("failed to write %zd from mem", len);
2657 childnode = mimeinfo->node->children;
2658 firstboundary = TRUE;
2659 while (childnode != NULL) {
2660 MimeInfo *child = childnode->data;
2663 firstboundary = FALSE;
2665 TRY(fprintf(fp, "\n") >= 0);
2667 TRY(fprintf(fp, "--%s\n", boundary) >= 0);
2669 if (procmime_write_mime_header(child, fp) < 0)
2671 if (procmime_write_mimeinfo(child, fp) < 0)
2674 childnode = g_node_next_sibling(childnode);
2676 TRY(fprintf(fp, "\n--%s--\n", boundary) >= 0);
2681 gint procmime_write_mimeinfo(MimeInfo *mimeinfo, FILE *fp)
2685 debug_print("procmime_write_mimeinfo\n");
2687 if (G_NODE_IS_LEAF(mimeinfo->node)) {
2688 switch (mimeinfo->content) {
2689 case MIMECONTENT_FILE:
2690 if ((infp = procmime_fopen(mimeinfo->data.filename, "rb")) == NULL) {
2691 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
2694 copy_file_part_to_fp(infp, mimeinfo->offset, mimeinfo->length, fp);
2695 procmime_fclose(infp);
2698 case MIMECONTENT_MEM:
2699 len = strlen(mimeinfo->data.mem);
2700 if (SC_FWRITE(mimeinfo->data.mem, sizeof(gchar), len, fp) < len)
2708 /* Call writer for mime type */
2709 switch (mimeinfo->type) {
2710 case MIMETYPE_MESSAGE:
2711 if (g_ascii_strcasecmp(mimeinfo->subtype, "rfc822") == 0) {
2712 return procmime_write_message_rfc822(mimeinfo, fp);
2716 case MIMETYPE_MULTIPART:
2717 return procmime_write_multipart(mimeinfo, fp);
2729 gchar *procmime_get_part_file_name(MimeInfo *mimeinfo)
2733 if ((mimeinfo->type == MIMETYPE_TEXT) && !g_ascii_strcasecmp(mimeinfo->subtype, "html"))
2734 base = g_strdup("mimetmp.html");
2736 const gchar *basetmp;
2739 basetmp = procmime_mimeinfo_get_parameter(mimeinfo, "filename");
2740 if (basetmp == NULL)
2741 basetmp = procmime_mimeinfo_get_parameter(mimeinfo, "name");
2742 if (basetmp == NULL)
2743 basetmp = "mimetmp";
2744 basename = g_path_get_basename(basetmp);
2745 if (*basename == '\0') {
2747 basename = g_strdup("mimetmp");
2749 base = conv_filename_from_utf8(basename);
2751 subst_for_shellsafe_filename(base);