2 * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2016 Hiroyuki Yamamoto & The Claws Mail Team
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "claws-features.h"
30 #include <glib/gi18n.h>
36 #include <sys/types.h>
42 #include "procheader.h"
43 #include "quoted-printable.h"
50 #include "prefs_common.h"
51 #include "prefs_gtk.h"
52 #include "alertpanel.h"
56 static GHashTable *procmime_get_mime_type_table (void);
57 static MimeInfo *procmime_scan_file_short(const gchar *filename);
58 static MimeInfo *procmime_scan_queue_file_short(const gchar *filename);
59 static MimeInfo *procmime_scan_queue_file_full(const gchar *filename, gboolean short_scan);
61 MimeInfo *procmime_mimeinfo_new(void)
65 mimeinfo = g_new0(MimeInfo, 1);
66 mimeinfo->content = MIMECONTENT_EMPTY;
67 mimeinfo->data.filename = NULL;
69 mimeinfo->type = MIMETYPE_UNKNOWN;
70 mimeinfo->encoding_type = ENC_UNKNOWN;
71 mimeinfo->typeparameters = g_hash_table_new(g_str_hash, g_str_equal);
73 mimeinfo->disposition = DISPOSITIONTYPE_UNKNOWN;
74 mimeinfo->dispositionparameters
75 = g_hash_table_new(g_str_hash, g_str_equal);
77 mimeinfo->node = g_node_new(mimeinfo);
82 static gboolean procmime_mimeinfo_parameters_destroy(gpointer key, gpointer value, gpointer user_data)
90 static gchar *forced_charset = NULL;
92 void procmime_force_charset(const gchar *str)
94 g_free(forced_charset);
95 forced_charset = NULL;
97 forced_charset = g_strdup(str);
100 static EncodingType forced_encoding = 0;
102 void procmime_force_encoding(EncodingType encoding)
104 forced_encoding = encoding;
107 static gboolean free_func(GNode *node, gpointer data)
109 MimeInfo *mimeinfo = (MimeInfo *) node->data;
111 switch (mimeinfo->content) {
112 case MIMECONTENT_FILE:
114 claws_unlink(mimeinfo->data.filename);
115 g_free(mimeinfo->data.filename);
118 case MIMECONTENT_MEM:
120 g_free(mimeinfo->data.mem);
125 g_free(mimeinfo->subtype);
126 g_free(mimeinfo->description);
127 g_free(mimeinfo->id);
128 g_free(mimeinfo->location);
130 g_hash_table_foreach_remove(mimeinfo->typeparameters,
131 procmime_mimeinfo_parameters_destroy, NULL);
132 g_hash_table_destroy(mimeinfo->typeparameters);
133 g_hash_table_foreach_remove(mimeinfo->dispositionparameters,
134 procmime_mimeinfo_parameters_destroy, NULL);
135 g_hash_table_destroy(mimeinfo->dispositionparameters);
137 if (mimeinfo->privacy)
138 privacy_free_privacydata(mimeinfo->privacy);
143 void procmime_mimeinfo_free_all(MimeInfo **mimeinfo_ptr)
145 MimeInfo *mimeinfo = *mimeinfo_ptr;
151 node = mimeinfo->node;
152 g_node_traverse(node, G_IN_ORDER, G_TRAVERSE_ALL, -1, free_func, NULL);
154 g_node_destroy(node);
157 *mimeinfo_ptr = NULL;
160 MimeInfo *procmime_mimeinfo_parent(MimeInfo *mimeinfo)
162 cm_return_val_if_fail(mimeinfo != NULL, NULL);
163 cm_return_val_if_fail(mimeinfo->node != NULL, NULL);
165 if (mimeinfo->node->parent == NULL)
167 return (MimeInfo *) mimeinfo->node->parent->data;
170 MimeInfo *procmime_mimeinfo_next(MimeInfo *mimeinfo)
172 cm_return_val_if_fail(mimeinfo != NULL, NULL);
173 cm_return_val_if_fail(mimeinfo->node != NULL, NULL);
175 if (mimeinfo->node->children)
176 return (MimeInfo *) mimeinfo->node->children->data;
177 if (mimeinfo->node->next)
178 return (MimeInfo *) mimeinfo->node->next->data;
180 if (mimeinfo->node->parent == NULL)
183 while (mimeinfo->node->parent != NULL) {
184 mimeinfo = (MimeInfo *) mimeinfo->node->parent->data;
185 if (mimeinfo->node->next)
186 return (MimeInfo *) mimeinfo->node->next->data;
192 MimeInfo *procmime_scan_message(MsgInfo *msginfo)
197 filename = procmsg_get_message_file_path(msginfo);
198 if (!filename || !is_file_exist(filename)) {
203 if (!folder_has_parent_of_type(msginfo->folder, F_QUEUE) &&
204 !folder_has_parent_of_type(msginfo->folder, F_DRAFT))
205 mimeinfo = procmime_scan_file(filename);
207 mimeinfo = procmime_scan_queue_file(filename);
213 MimeInfo *procmime_scan_message_short(MsgInfo *msginfo)
218 filename = procmsg_get_message_file_path(msginfo);
219 if (!filename || !is_file_exist(filename)) {
224 if (!folder_has_parent_of_type(msginfo->folder, F_QUEUE) &&
225 !folder_has_parent_of_type(msginfo->folder, F_DRAFT))
226 mimeinfo = procmime_scan_file_short(filename);
228 mimeinfo = procmime_scan_queue_file_short(filename);
236 H_CONTENT_TRANSFER_ENCODING = 0,
238 H_CONTENT_DISPOSITION = 2,
239 H_CONTENT_DESCRIPTION = 3,
243 const gchar *procmime_mimeinfo_get_parameter(MimeInfo *mimeinfo, const gchar *name)
247 cm_return_val_if_fail(mimeinfo != NULL, NULL);
248 cm_return_val_if_fail(name != NULL, NULL);
250 value = g_hash_table_lookup(mimeinfo->dispositionparameters, name);
252 value = g_hash_table_lookup(mimeinfo->typeparameters, name);
257 #ifdef HAVE_FGETS_UNLOCKED
258 #define SC_FGETS fgets_unlocked
259 #define SC_FPUTS fputs_unlocked
260 #define SC_FPUTC fputc_unlocked
261 #define SC_FREAD fread_unlocked
262 #define SC_FWRITE fwrite_unlocked
263 #define SC_FEOF feof_unlocked
264 #define SC_FERROR ferror_unlocked
266 static FILE *procmime_fopen(const gchar *file, const gchar *mode)
268 FILE *fp = g_fopen(file, mode);
274 static int procmime_fclose(FILE *fp)
280 #define SC_FGETS fgets
281 #define SC_FPUTS fputs
282 #define SC_FPUTC fputc
283 #define SC_FREAD fread
284 #define SC_FWRITE fwrite
286 #define SC_FERROR ferror
287 #define procmime_fopen g_fopen
288 #define procmime_fclose fclose
291 #define FLUSH_LASTLINE() { \
292 if (*lastline != '\0') { \
294 strretchomp(lastline); \
295 llen = strlen(lastline); \
296 if (lastline[llen-1] == ' ' && strcmp(lastline,"-- ")) { \
297 /* this is flowed */ \
299 lastline[llen-1] = '\0'; \
300 if (SC_FPUTS(lastline, outfp) == EOF) \
303 if (SC_FPUTS(lastline, outfp) == EOF) \
305 if (SC_FPUTS("\n", outfp) == EOF) \
309 strcpy(lastline, buf); \
312 gboolean procmime_decode_content(MimeInfo *mimeinfo)
319 gboolean tmp_file = FALSE;
320 gboolean flowed = FALSE;
321 gboolean delsp = FALSE;
322 gboolean err = FALSE;
326 cm_return_val_if_fail(mimeinfo != NULL, FALSE);
328 EncodingType encoding = forced_encoding
330 : mimeinfo->encoding_type;
331 gchar lastline[BUFFSIZE];
332 memset(lastline, 0, BUFFSIZE);
334 if (prefs_common.respect_flowed_format &&
335 mimeinfo->type == MIMETYPE_TEXT &&
336 !strcasecmp(mimeinfo->subtype, "plain")) {
337 if (procmime_mimeinfo_get_parameter(mimeinfo, "format") != NULL &&
338 !strcasecmp(procmime_mimeinfo_get_parameter(mimeinfo, "format"),"flowed"))
341 procmime_mimeinfo_get_parameter(mimeinfo, "delsp") != NULL &&
342 !strcasecmp(procmime_mimeinfo_get_parameter(mimeinfo, "delsp"),"yes"))
347 encoding == ENC_UNKNOWN ||
348 encoding == ENC_BINARY ||
349 encoding == ENC_7BIT ||
354 if (mimeinfo->type == MIMETYPE_MULTIPART || mimeinfo->type == MIMETYPE_MESSAGE)
357 if (mimeinfo->data.filename == NULL)
360 infp = procmime_fopen(mimeinfo->data.filename, "rb");
362 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
365 if (fseek(infp, mimeinfo->offset, SEEK_SET) < 0) {
366 FILE_OP_ERROR(mimeinfo->data.filename, "fseek");
367 procmime_fclose(infp);
371 outfp = get_tmpfile_in_dir(get_mime_tmp_dir(), &tmpfilename);
374 procmime_fclose(infp);
377 #ifdef HAVE_FGETS_UNLOCKED
381 readend = mimeinfo->offset + mimeinfo->length;
384 if (encoding == ENC_QUOTED_PRINTABLE) {
385 while ((ftell(infp) < readend) && (SC_FGETS(buf, sizeof(buf), infp) != NULL)) {
387 len = qp_decode_line(buf);
390 if (SC_FWRITE(buf, 1, len, outfp) < len)
398 } else if (encoding == ENC_BASE64) {
399 gchar outbuf[BUFFSIZE];
400 gint len, inlen, inread;
401 gboolean got_error = FALSE;
402 gboolean uncanonicalize = FALSE;
404 gboolean null_bytes = FALSE;
405 gboolean starting = TRUE;
407 if (mimeinfo->type == MIMETYPE_TEXT ||
408 mimeinfo->type == MIMETYPE_MESSAGE) {
409 uncanonicalize = TRUE;
410 tmpfp = my_tmpfile();
412 perror("my_tmpfile");
414 procmime_fclose(outfp);
415 procmime_fclose(infp);
418 #ifdef HAVE_FGETS_UNLOCKED
424 while ((inlen = MIN(readend - ftell(infp), sizeof(buf))) > 0 && !err) {
425 inread = SC_FREAD(buf, 1, inlen, infp);
426 len = g_base64_decode_step(buf, inlen, outbuf, &state, &save);
427 if (uncanonicalize == TRUE && strlen(outbuf) < len && starting) {
428 uncanonicalize = FALSE;
432 if (((inread != inlen) || len < 0) && !got_error) {
433 g_warning("Bad BASE64 content.");
434 if (SC_FWRITE(_("[Error decoding BASE64]\n"),
436 strlen(_("[Error decoding BASE64]\n")),
437 tmpfp) < strlen(_("[Error decoding BASE64]\n")))
438 g_warning("error decoding BASE64");
441 } else if (len >= 0) {
442 /* print out the error message only once
445 /* we won't uncanonicalize, output to outfp directly */
446 if (SC_FWRITE(outbuf, sizeof(gchar), len, outfp) < len)
449 if (SC_FWRITE(outbuf, sizeof(gchar), len, tmpfp) < len)
456 if (uncanonicalize) {
458 while (SC_FGETS(buf, sizeof(buf), tmpfp) != NULL) {
460 if (SC_FPUTS(buf, outfp) == EOF)
465 procmime_fclose(tmpfp);
466 } else if (encoding == ENC_X_UUENCODE) {
467 gchar outbuf[BUFFSIZE];
469 gboolean flag = FALSE;
471 while ((ftell(infp) < readend) && (SC_FGETS(buf, sizeof(buf), infp) != NULL)) {
472 if (!flag && strncmp(buf,"begin ", 6)) continue;
475 len = fromuutobits(outbuf, buf);
478 g_warning("Bad UUENCODE content (%d)", len);
481 if (SC_FWRITE(outbuf, sizeof(gchar), len, outfp) < len)
487 while ((ftell(infp) < readend) && (SC_FGETS(buf, sizeof(buf), infp) != NULL)) {
489 if (SC_FPUTS(buf, outfp) == EOF)
498 g_warning("write error");
501 procmime_fclose(outfp);
502 procmime_fclose(infp);
508 if (g_stat(tmpfilename, &statbuf) < 0) {
509 FILE_OP_ERROR(tmpfilename, "stat");
514 claws_unlink(mimeinfo->data.filename);
515 g_free(mimeinfo->data.filename);
516 mimeinfo->data.filename = tmpfilename;
517 mimeinfo->tmp = TRUE;
518 mimeinfo->offset = 0;
519 mimeinfo->length = statbuf.st_size;
520 mimeinfo->encoding_type = ENC_BINARY;
525 #define B64_LINE_SIZE 57
526 #define B64_BUFFSIZE 77
528 gboolean procmime_encode_content(MimeInfo *mimeinfo, EncodingType encoding)
530 FILE *infp = NULL, *outfp;
534 gboolean err = FALSE;
536 if (mimeinfo->content == MIMECONTENT_EMPTY)
539 if (mimeinfo->encoding_type != ENC_UNKNOWN &&
540 mimeinfo->encoding_type != ENC_BINARY &&
541 mimeinfo->encoding_type != ENC_7BIT &&
542 mimeinfo->encoding_type != ENC_8BIT)
543 if(!procmime_decode_content(mimeinfo))
546 outfp = get_tmpfile_in_dir(get_mime_tmp_dir(), &tmpfilename);
551 #ifdef HAVE_FGETS_UNLOCKED
555 if (mimeinfo->content == MIMECONTENT_FILE && mimeinfo->data.filename) {
556 if ((infp = procmime_fopen(mimeinfo->data.filename, "rb")) == NULL) {
557 g_warning("Can't open file %s", mimeinfo->data.filename);
558 procmime_fclose(outfp);
561 } else if (mimeinfo->content == MIMECONTENT_MEM) {
562 infp = str_open_as_stream(mimeinfo->data.mem);
564 procmime_fclose(outfp);
567 #ifdef HAVE_FGETS_UNLOCKED
571 procmime_fclose(outfp);
572 g_warning("Unknown mimeinfo");
576 if (encoding == ENC_BASE64) {
577 gchar inbuf[B64_LINE_SIZE], *out;
579 gchar *tmp_file = NULL;
581 if (mimeinfo->type == MIMETYPE_TEXT ||
582 mimeinfo->type == MIMETYPE_MESSAGE) {
583 if (mimeinfo->content == MIMECONTENT_FILE) {
584 tmp_file = get_tmp_file();
585 if (canonicalize_file(mimeinfo->data.filename, tmp_file) < 0) {
587 procmime_fclose(infp);
588 procmime_fclose(outfp);
591 if ((tmp_fp = procmime_fopen(tmp_file, "rb")) == NULL) {
592 FILE_OP_ERROR(tmp_file, "fopen");
593 claws_unlink(tmp_file);
595 procmime_fclose(infp);
596 procmime_fclose(outfp);
600 gchar *out = canonicalize_str(mimeinfo->data.mem);
601 procmime_fclose(infp);
602 infp = str_open_as_stream(out);
606 procmime_fclose(outfp);
609 #ifdef HAVE_FGETS_UNLOCKED
615 while ((len = SC_FREAD(inbuf, sizeof(gchar),
616 B64_LINE_SIZE, tmp_fp))
618 out = g_base64_encode(inbuf, B64_LINE_SIZE);
619 if (SC_FPUTS(out, outfp) == EOF)
622 if (SC_FPUTC('\n', outfp) == EOF)
625 if (len > 0 && SC_FEOF(tmp_fp)) {
626 out = g_base64_encode(inbuf, len);
627 if (SC_FPUTS(out, outfp) == EOF)
630 if (SC_FPUTC('\n', outfp) == EOF)
635 procmime_fclose(tmp_fp);
636 claws_unlink(tmp_file);
639 } else if (encoding == ENC_QUOTED_PRINTABLE) {
640 gchar inbuf[BUFFSIZE], outbuf[BUFFSIZE * 4];
642 while (SC_FGETS(inbuf, sizeof(inbuf), infp) != NULL) {
643 qp_encode_line(outbuf, inbuf);
645 if (!strncmp("From ", outbuf, sizeof("From ")-1)) {
646 gchar *tmpbuf = outbuf;
648 tmpbuf += sizeof("From ")-1;
650 if (SC_FPUTS("=46rom ", outfp) == EOF)
652 if (SC_FPUTS(tmpbuf, outfp) == EOF)
655 if (SC_FPUTS(outbuf, outfp) == EOF)
662 while (SC_FGETS(buf, sizeof(buf), infp) != NULL) {
664 if (SC_FPUTS(buf, outfp) == EOF)
669 procmime_fclose(outfp);
670 procmime_fclose(infp);
675 if (mimeinfo->content == MIMECONTENT_FILE) {
676 if (mimeinfo->tmp && (mimeinfo->data.filename != NULL))
677 claws_unlink(mimeinfo->data.filename);
678 g_free(mimeinfo->data.filename);
679 } else if (mimeinfo->content == MIMECONTENT_MEM) {
680 if (mimeinfo->tmp && (mimeinfo->data.mem != NULL))
681 g_free(mimeinfo->data.mem);
684 if (g_stat(tmpfilename, &statbuf) < 0) {
685 FILE_OP_ERROR(tmpfilename, "stat");
688 mimeinfo->content = MIMECONTENT_FILE;
689 mimeinfo->data.filename = tmpfilename;
690 mimeinfo->tmp = TRUE;
691 mimeinfo->offset = 0;
692 mimeinfo->length = statbuf.st_size;
693 mimeinfo->encoding_type = encoding;
698 gint procmime_get_part(const gchar *outfile, MimeInfo *mimeinfo)
702 gint restlength, readlength;
703 gint saved_errno = 0;
705 cm_return_val_if_fail(outfile != NULL, -1);
706 cm_return_val_if_fail(mimeinfo != NULL, -1);
708 if (mimeinfo->encoding_type != ENC_BINARY && !procmime_decode_content(mimeinfo))
711 if ((infp = procmime_fopen(mimeinfo->data.filename, "rb")) == NULL) {
713 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
714 return -(saved_errno);
716 if (fseek(infp, mimeinfo->offset, SEEK_SET) < 0) {
718 FILE_OP_ERROR(mimeinfo->data.filename, "fseek");
719 procmime_fclose(infp);
720 return -(saved_errno);
722 if ((outfp = procmime_fopen(outfile, "wb")) == NULL) {
724 FILE_OP_ERROR(outfile, "fopen");
725 procmime_fclose(infp);
726 return -(saved_errno);
729 restlength = mimeinfo->length;
731 while ((restlength > 0) && ((readlength = SC_FREAD(buf, 1, restlength > BUFFSIZE ? BUFFSIZE : restlength, infp)) > 0)) {
732 if (SC_FWRITE(buf, 1, readlength, outfp) != readlength) {
734 procmime_fclose(infp);
735 procmime_fclose(outfp);
736 return -(saved_errno);
738 restlength -= readlength;
741 procmime_fclose(infp);
742 if (procmime_fclose(outfp) == EOF) {
744 FILE_OP_ERROR(outfile, "fclose");
745 claws_unlink(outfile);
746 return -(saved_errno);
752 gboolean procmime_scan_text_content(MimeInfo *mimeinfo,
753 gboolean (*scan_callback)(const gchar *str, gpointer cb_data),
757 const gchar *src_codeset;
758 gboolean conv_fail = FALSE;
762 gboolean scan_ret = FALSE;
764 cm_return_val_if_fail(mimeinfo != NULL, TRUE);
765 cm_return_val_if_fail(scan_callback != NULL, TRUE);
767 if (!procmime_decode_content(mimeinfo))
770 tmpfile = procmime_get_tmp_file_name(mimeinfo);
774 if (procmime_get_part(tmpfile, mimeinfo) < 0) {
779 tmpfp = procmime_fopen(tmpfile, "rb");
785 src_codeset = forced_charset
787 procmime_mimeinfo_get_parameter(mimeinfo, "charset");
789 /* use supersets transparently when possible */
790 if (!forced_charset && src_codeset && !strcasecmp(src_codeset, CS_ISO_8859_1))
791 src_codeset = CS_WINDOWS_1252;
792 else if (!forced_charset && src_codeset && !strcasecmp(src_codeset, CS_X_GBK))
793 src_codeset = CS_GB18030;
794 else if (!forced_charset && src_codeset && !strcasecmp(src_codeset, CS_GBK))
795 src_codeset = CS_GB18030;
796 else if (!forced_charset && src_codeset && !strcasecmp(src_codeset, CS_GB2312))
797 src_codeset = CS_GB18030;
798 else if (!forced_charset && src_codeset && !strcasecmp(src_codeset, CS_X_VIET_VPS))
799 src_codeset = CS_WINDOWS_874;
801 if (mimeinfo->type == MIMETYPE_TEXT && !g_ascii_strcasecmp(mimeinfo->subtype, "html")) {
802 SC_HTMLParser *parser;
805 conv = conv_code_converter_new(src_codeset);
806 parser = sc_html_parser_new(tmpfp, conv);
807 while ((str = sc_html_parse(parser)) != NULL) {
808 if ((scan_ret = scan_callback(str, cb_data)) == TRUE)
811 sc_html_parser_destroy(parser);
812 conv_code_converter_destroy(conv);
813 } else if (mimeinfo->type == MIMETYPE_TEXT && !g_ascii_strcasecmp(mimeinfo->subtype, "enriched")) {
817 conv = conv_code_converter_new(src_codeset);
818 parser = ertf_parser_new(tmpfp, conv);
819 while ((str = ertf_parse(parser)) != NULL) {
820 if ((scan_ret = scan_callback(str, cb_data)) == TRUE)
823 ertf_parser_destroy(parser);
824 conv_code_converter_destroy(conv);
825 } else if (mimeinfo->type == MIMETYPE_TEXT && mimeinfo->disposition != DISPOSITIONTYPE_ATTACHMENT) {
826 while (SC_FGETS(buf, sizeof(buf), tmpfp) != NULL) {
827 str = conv_codeset_strdup(buf, src_codeset, CS_UTF_8);
829 if ((scan_ret = scan_callback(str, cb_data)) == TRUE) {
836 if ((scan_ret = scan_callback(buf, cb_data)) == TRUE)
843 g_warning("procmime_get_text_content(): Code conversion failed.");
845 procmime_fclose(tmpfp);
846 claws_unlink(tmpfile);
852 static gboolean scan_fputs_cb(const gchar *str, gpointer fp)
854 if (SC_FPUTS(str, (FILE *)fp) == EOF)
860 FILE *procmime_get_text_content(MimeInfo *mimeinfo)
865 if ((outfp = my_tmpfile()) == NULL) {
866 perror("my_tmpfile");
869 #ifdef HAVE_FGETS_UNLOCKED
873 err = procmime_scan_text_content(mimeinfo, scan_fputs_cb, outfp);
877 procmime_fclose(outfp);
880 #ifdef HAVE_FGETS_UNLOCKED
887 FILE *procmime_get_binary_content(MimeInfo *mimeinfo)
892 cm_return_val_if_fail(mimeinfo != NULL, NULL);
894 if (!procmime_decode_content(mimeinfo))
897 tmpfile = procmime_get_tmp_file_name(mimeinfo);
901 if (procmime_get_part(tmpfile, mimeinfo) < 0) {
906 outfp = procmime_fopen(tmpfile, "rb");
916 #ifdef HAVE_FGETS_UNLOCKED
922 /* search the first text part of (multipart) MIME message,
923 decode, convert it and output to outfp. */
924 FILE *procmime_get_first_text_content(MsgInfo *msginfo)
927 MimeInfo *mimeinfo, *partinfo;
928 gboolean empty_ok = FALSE, short_scan = TRUE;
930 cm_return_val_if_fail(msginfo != NULL, NULL);
932 /* first we try to short-scan (for speed), refusing empty parts */
935 mimeinfo = procmime_scan_message_short(msginfo);
937 mimeinfo = procmime_scan_message(msginfo);
938 if (!mimeinfo) return NULL;
941 while (partinfo && (partinfo->type != MIMETYPE_TEXT ||
942 (partinfo->length == 0 && !empty_ok))) {
943 partinfo = procmime_mimeinfo_next(partinfo);
946 outfp = procmime_get_text_content(partinfo);
947 else if (!empty_ok && short_scan) {
948 /* if short scan didn't find a non-empty part, rescan
949 * fully for non-empty parts
952 procmime_mimeinfo_free_all(&mimeinfo);
954 } else if (!empty_ok && !short_scan) {
955 /* if full scan didn't find a non-empty part, rescan
956 * accepting empty parts
959 procmime_mimeinfo_free_all(&mimeinfo);
962 procmime_mimeinfo_free_all(&mimeinfo);
964 /* outfp already unlocked at this time */
969 static gboolean find_encrypted_func(GNode *node, gpointer data)
971 MimeInfo *mimeinfo = (MimeInfo *) node->data;
972 MimeInfo **encinfo = (MimeInfo **) data;
974 if (privacy_mimeinfo_is_encrypted(mimeinfo)) {
982 static MimeInfo *find_encrypted_part(MimeInfo *rootinfo)
984 MimeInfo *encinfo = NULL;
986 g_node_traverse(rootinfo->node, G_IN_ORDER, G_TRAVERSE_ALL, -1,
987 find_encrypted_func, &encinfo);
992 /* search the first encrypted text part of (multipart) MIME message,
993 decode, convert it and output to outfp. */
994 FILE *procmime_get_first_encrypted_text_content(MsgInfo *msginfo)
997 MimeInfo *mimeinfo, *partinfo, *encinfo;
999 cm_return_val_if_fail(msginfo != NULL, NULL);
1001 mimeinfo = procmime_scan_message(msginfo);
1006 partinfo = mimeinfo;
1007 if ((encinfo = find_encrypted_part(partinfo)) != NULL) {
1008 debug_print("decrypting message part\n");
1009 if (privacy_mimeinfo_decrypt(encinfo) < 0) {
1010 alertpanel_error(_("Couldn't decrypt: %s"),
1011 privacy_get_error());
1015 partinfo = mimeinfo;
1016 while (partinfo && partinfo->type != MIMETYPE_TEXT) {
1017 partinfo = procmime_mimeinfo_next(partinfo);
1018 if (privacy_mimeinfo_is_signed(partinfo))
1019 procmsg_msginfo_set_flags(msginfo, 0, MSG_SIGNED);
1023 outfp = procmime_get_text_content(partinfo);
1025 procmime_mimeinfo_free_all(&mimeinfo);
1027 /* outfp already unlocked at this time */
1031 gboolean procmime_msginfo_is_encrypted(MsgInfo *msginfo)
1033 MimeInfo *mimeinfo, *partinfo;
1034 gboolean result = FALSE;
1036 cm_return_val_if_fail(msginfo != NULL, FALSE);
1038 mimeinfo = procmime_scan_message(msginfo);
1043 partinfo = mimeinfo;
1044 result = (find_encrypted_part(partinfo) != NULL);
1045 procmime_mimeinfo_free_all(&mimeinfo);
1050 gchar *procmime_get_tmp_file_name(MimeInfo *mimeinfo)
1052 static guint32 id = 0;
1057 cm_return_val_if_fail(mimeinfo != NULL, NULL);
1059 g_snprintf(f_prefix, sizeof(f_prefix), "%08x.", id++);
1061 if ((mimeinfo->type == MIMETYPE_TEXT) && !g_ascii_strcasecmp(mimeinfo->subtype, "html"))
1062 base = g_strdup("mimetmp.html");
1064 const gchar *basetmp;
1066 basetmp = procmime_mimeinfo_get_parameter(mimeinfo, "filename");
1067 if (basetmp == NULL)
1068 basetmp = procmime_mimeinfo_get_parameter(mimeinfo, "name");
1069 if (basetmp == NULL)
1070 basetmp = "mimetmp";
1071 basetmp = g_path_get_basename(basetmp);
1072 if (*basetmp == '\0')
1073 basetmp = g_strdup("mimetmp");
1074 base = conv_filename_from_utf8(basetmp);
1075 g_free((gchar*)basetmp);
1076 subst_for_shellsafe_filename(base);
1079 filename = g_strconcat(get_mime_tmp_dir(), G_DIR_SEPARATOR_S,
1080 f_prefix, base, NULL);
1087 static GList *mime_type_list = NULL;
1089 gchar *procmime_get_mime_type(const gchar *filename)
1095 static GHashTable *mime_type_table = NULL;
1096 MimeType *mime_type;
1098 if (!mime_type_table) {
1099 mime_type_table = procmime_get_mime_type_table();
1100 if (!mime_type_table) return NULL;
1104 if (filename == NULL)
1107 base = g_path_get_basename(filename);
1108 if ((p = strrchr(base, '.')) != NULL)
1109 ext = g_utf8_strdown(p + 1, -1);
1111 ext = g_utf8_strdown(base, -1);
1115 mime_type = g_hash_table_lookup(mime_type_table, ext);
1119 str = g_strconcat(mime_type->type, "/", mime_type->sub_type,
1121 debug_print("got type %s for %s\n", str, ext);
1128 gchar *str = get_content_type_from_registry_with_ext(ext);
1135 static guint procmime_str_hash(gconstpointer gptr)
1137 guint hash_result = 0;
1140 for (str = gptr; str && *str; str++) {
1141 if (isupper(*str)) hash_result += (*str + ' ');
1142 else hash_result += *str;
1148 static gint procmime_str_equal(gconstpointer gptr1, gconstpointer gptr2)
1150 const char *str1 = gptr1;
1151 const char *str2 = gptr2;
1153 return !g_utf8_collate(str1, str2);
1156 static GHashTable *procmime_get_mime_type_table(void)
1158 GHashTable *table = NULL;
1160 MimeType *mime_type;
1163 if (!mime_type_list) {
1164 mime_type_list = procmime_get_mime_type_list();
1165 if (!mime_type_list) return NULL;
1168 table = g_hash_table_new(procmime_str_hash, procmime_str_equal);
1170 for (cur = mime_type_list; cur != NULL; cur = cur->next) {
1174 mime_type = (MimeType *)cur->data;
1176 if (!mime_type->extension) continue;
1178 exts = g_strsplit(mime_type->extension, " ", 16);
1179 for (i = 0; exts[i] != NULL; i++) {
1180 /* Don't overwrite previously inserted extension */
1181 if (!g_hash_table_lookup(table, exts[i])) {
1182 key = g_strdup(exts[i]);
1183 g_hash_table_insert(table, key, mime_type);
1192 GList *procmime_get_mime_type_list(void)
1196 gchar buf[BUFFSIZE];
1199 MimeType *mime_type;
1200 gboolean fp_is_glob_file = TRUE;
1203 return mime_type_list;
1205 #if defined(__NetBSD__) || defined(__OpenBSD__) || defined(__FreeBSD__)
1206 if ((fp = procmime_fopen(DATAROOTDIR "/mime/globs", "rb")) == NULL)
1208 if ((fp = procmime_fopen("/usr/share/mime/globs", "rb")) == NULL)
1211 fp_is_glob_file = FALSE;
1212 if ((fp = procmime_fopen("/etc/mime.types", "rb")) == NULL) {
1213 if ((fp = procmime_fopen(SYSCONFDIR "/mime.types", "rb"))
1215 FILE_OP_ERROR(SYSCONFDIR "/mime.types",
1222 while (SC_FGETS(buf, sizeof(buf), fp) != NULL) {
1223 p = strchr(buf, '#');
1229 if (fp_is_glob_file) {
1230 while (*p && !g_ascii_isspace(*p) && (*p!=':')) p++;
1232 while (*p && !g_ascii_isspace(*p)) p++;
1239 delim = strchr(buf, '/');
1240 if (delim == NULL) continue;
1243 mime_type = g_new(MimeType, 1);
1244 mime_type->type = g_strdup(buf);
1245 mime_type->sub_type = g_strdup(delim + 1);
1247 if (fp_is_glob_file) {
1248 while (*p && (g_ascii_isspace(*p)||(*p=='*')||(*p=='.'))) p++;
1250 while (*p && g_ascii_isspace(*p)) p++;
1254 mime_type->extension = g_utf8_strdown(p, -1);
1256 mime_type->extension = NULL;
1258 list = g_list_append(list, mime_type);
1261 procmime_fclose(fp);
1264 g_warning("Can't read mime.types");
1269 EncodingType procmime_get_encoding_for_charset(const gchar *charset)
1273 else if (!g_ascii_strncasecmp(charset, "ISO-2022-", 9) ||
1274 !g_ascii_strcasecmp(charset, "US-ASCII"))
1276 else if (!g_ascii_strcasecmp(charset, "ISO-8859-5") ||
1277 !g_ascii_strncasecmp(charset, "KOI8-", 5) ||
1278 !g_ascii_strcasecmp(charset, "X-MAC-CYRILLIC") ||
1279 !g_ascii_strcasecmp(charset, "MAC-CYRILLIC") ||
1280 !g_ascii_strcasecmp(charset, "Windows-1251"))
1282 else if (!g_ascii_strncasecmp(charset, "ISO-8859-", 9))
1283 return ENC_QUOTED_PRINTABLE;
1284 else if (!g_ascii_strncasecmp(charset, "UTF-8", 5))
1285 return ENC_QUOTED_PRINTABLE;
1290 EncodingType procmime_get_encoding_for_text_file(const gchar *file, gboolean *has_binary)
1293 guchar buf[BUFFSIZE];
1295 size_t octet_chars = 0;
1296 size_t total_len = 0;
1297 gfloat octet_percentage;
1298 gboolean force_b64 = FALSE;
1300 if ((fp = procmime_fopen(file, "rb")) == NULL) {
1301 FILE_OP_ERROR(file, "fopen");
1305 while ((len = SC_FREAD(buf, sizeof(guchar), sizeof(buf), fp)) > 0) {
1309 for (p = buf, i = 0; i < len; ++p, ++i) {
1320 procmime_fclose(fp);
1323 octet_percentage = (gfloat)octet_chars / (gfloat)total_len;
1325 octet_percentage = 0.0;
1327 debug_print("procmime_get_encoding_for_text_file(): "
1328 "8bit chars: %zd / %zd (%f%%)\n", octet_chars, total_len,
1329 100.0 * octet_percentage);
1331 if (octet_percentage > 0.20 || force_b64) {
1332 debug_print("using BASE64\n");
1334 } else if (octet_chars > 0) {
1335 debug_print("using quoted-printable\n");
1336 return ENC_QUOTED_PRINTABLE;
1338 debug_print("using 7bit\n");
1343 struct EncodingTable
1346 EncodingType enc_type;
1349 struct EncodingTable encoding_table[] = {
1352 {"binary", ENC_BINARY},
1353 {"quoted-printable", ENC_QUOTED_PRINTABLE},
1354 {"base64", ENC_BASE64},
1355 {"x-uuencode", ENC_UNKNOWN},
1356 {NULL, ENC_UNKNOWN},
1359 const gchar *procmime_get_encoding_str(EncodingType encoding)
1361 struct EncodingTable *enc_table;
1363 for (enc_table = encoding_table; enc_table->str != NULL; enc_table++) {
1364 if (enc_table->enc_type == encoding)
1365 return enc_table->str;
1370 /* --- NEW MIME STUFF --- */
1377 static struct TypeTable mime_type_table[] = {
1378 {"text", MIMETYPE_TEXT},
1379 {"image", MIMETYPE_IMAGE},
1380 {"audio", MIMETYPE_AUDIO},
1381 {"video", MIMETYPE_VIDEO},
1382 {"application", MIMETYPE_APPLICATION},
1383 {"message", MIMETYPE_MESSAGE},
1384 {"multipart", MIMETYPE_MULTIPART},
1388 const gchar *procmime_get_media_type_str(MimeMediaType type)
1390 struct TypeTable *type_table;
1392 for (type_table = mime_type_table; type_table->str != NULL; type_table++) {
1393 if (type_table->type == type)
1394 return type_table->str;
1399 MimeMediaType procmime_get_media_type(const gchar *str)
1401 struct TypeTable *typetablearray;
1403 for (typetablearray = mime_type_table; typetablearray->str != NULL; typetablearray++)
1404 if (g_ascii_strncasecmp(str, typetablearray->str, strlen(typetablearray->str)) == 0)
1405 return typetablearray->type;
1407 return MIMETYPE_UNKNOWN;
1411 *\brief Safe wrapper for content type string.
1413 *\return const gchar * Pointer to content type string.
1415 gchar *procmime_get_content_type_str(MimeMediaType type,
1416 const char *subtype)
1418 const gchar *type_str = NULL;
1420 if (subtype == NULL || !(type_str = procmime_get_media_type_str(type)))
1421 return g_strdup("unknown");
1422 return g_strdup_printf("%s/%s", type_str, subtype);
1425 static int procmime_parse_mimepart(MimeInfo *parent,
1426 gchar *content_type,
1427 gchar *content_encoding,
1428 gchar *content_description,
1430 gchar *content_disposition,
1431 gchar *content_location,
1432 const gchar *original_msgid,
1433 const gchar *disposition_notification_hdr,
1434 const gchar *filename,
1437 gboolean short_scan);
1439 static void procmime_parse_message_rfc822(MimeInfo *mimeinfo, gboolean short_scan)
1441 HeaderEntry hentry[] = {{"Content-Type:", NULL, TRUE},
1442 {"Content-Transfer-Encoding:",
1444 {"Content-Description:",
1448 {"Content-Disposition:",
1450 {"Content-Location:",
1454 {"Original-Message-ID:",
1458 {NULL, NULL, FALSE}};
1459 guint content_start, i;
1464 procmime_decode_content(mimeinfo);
1466 fp = procmime_fopen(mimeinfo->data.filename, "rb");
1468 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
1471 if (fseek(fp, mimeinfo->offset, SEEK_SET) < 0) {
1472 FILE_OP_ERROR(mimeinfo->data.filename, "fseek");
1473 procmime_fclose(fp);
1476 procheader_get_header_fields(fp, hentry);
1477 if (hentry[0].body != NULL) {
1478 tmp = conv_unmime_header(hentry[0].body, NULL, FALSE);
1479 g_free(hentry[0].body);
1480 hentry[0].body = tmp;
1482 if (hentry[2].body != NULL) {
1483 tmp = conv_unmime_header(hentry[2].body, NULL, FALSE);
1484 g_free(hentry[2].body);
1485 hentry[2].body = tmp;
1487 if (hentry[4].body != NULL) {
1488 tmp = conv_unmime_header(hentry[4].body, NULL, FALSE);
1489 g_free(hentry[4].body);
1490 hentry[4].body = tmp;
1492 if (hentry[5].body != NULL) {
1493 tmp = conv_unmime_header(hentry[5].body, NULL, FALSE);
1494 g_free(hentry[5].body);
1495 hentry[5].body = tmp;
1497 if (hentry[7].body != NULL) {
1498 tmp = conv_unmime_header(hentry[7].body, NULL, FALSE);
1499 g_free(hentry[7].body);
1500 hentry[7].body = tmp;
1502 if (hentry[8].body != NULL) {
1503 tmp = conv_unmime_header(hentry[8].body, NULL, FALSE);
1504 g_free(hentry[8].body);
1505 hentry[8].body = tmp;
1508 content_start = ftell(fp);
1509 procmime_fclose(fp);
1511 len = mimeinfo->length - (content_start - mimeinfo->offset);
1514 procmime_parse_mimepart(mimeinfo,
1515 hentry[0].body, hentry[1].body,
1516 hentry[2].body, hentry[3].body,
1517 hentry[4].body, hentry[5].body,
1518 hentry[7].body, hentry[8].body,
1519 mimeinfo->data.filename, content_start,
1522 for (i = 0; i < (sizeof hentry / sizeof hentry[0]); i++) {
1523 g_free(hentry[i].body);
1524 hentry[i].body = NULL;
1528 static void procmime_parse_disposition_notification(MimeInfo *mimeinfo,
1529 const gchar *original_msgid, const gchar *disposition_notification_hdr,
1530 gboolean short_scan)
1532 HeaderEntry hentry[] = {{"Original-Message-ID:", NULL, TRUE},
1533 {"Disposition:", NULL, TRUE},
1534 {NULL, NULL, FALSE}};
1537 gchar *orig_msg_id = NULL;
1540 procmime_decode_content(mimeinfo);
1542 debug_print("parse disposition notification\n");
1543 fp = procmime_fopen(mimeinfo->data.filename, "rb");
1545 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
1548 if (fseek(fp, mimeinfo->offset, SEEK_SET) < 0) {
1549 FILE_OP_ERROR(mimeinfo->data.filename, "fseek");
1550 procmime_fclose(fp);
1554 if (original_msgid && disposition_notification_hdr) {
1555 hentry[0].body = g_strdup(original_msgid);
1556 hentry[1].body = g_strdup(disposition_notification_hdr);
1558 procheader_get_header_fields(fp, hentry);
1561 procmime_fclose(fp);
1563 if (!hentry[0].body || !hentry[1].body) {
1564 debug_print("MsgId %s, Disp %s\n",
1565 hentry[0].body ? hentry[0].body:"(nil)",
1566 hentry[1].body ? hentry[1].body:"(nil)");
1570 orig_msg_id = g_strdup(hentry[0].body);
1571 disp = g_strdup(hentry[1].body);
1573 extract_parenthesis(orig_msg_id, '<', '>');
1574 remove_space(orig_msg_id);
1576 if (strstr(disp, "displayed")) {
1577 /* find sent message, if possible */
1578 MsgInfo *info = NULL;
1580 debug_print("%s has been displayed.\n", orig_msg_id);
1581 for (flist = folder_get_list(); flist != NULL; flist = g_list_next(flist)) {
1582 FolderItem *outbox = ((Folder *)(flist->data))->outbox;
1584 debug_print("skipping folder with no outbox...\n");
1587 info = folder_item_get_msginfo_by_msgid(outbox, orig_msg_id);
1588 debug_print("%s %s in %s\n", info?"found":"didn't find", orig_msg_id, outbox->path);
1590 procmsg_msginfo_set_flags(info, MSG_RETRCPT_GOT, 0);
1591 procmsg_msginfo_free(&info);
1595 g_free(orig_msg_id);
1598 for (i = 0; i < (sizeof hentry / sizeof hentry[0]); i++) {
1599 g_free(hentry[i].body);
1600 hentry[i].body = NULL;
1604 #define GET_HEADERS() { \
1605 procheader_get_header_fields(fp, hentry); \
1606 if (hentry[0].body != NULL) { \
1607 tmp = conv_unmime_header(hentry[0].body, NULL, FALSE); \
1608 g_free(hentry[0].body); \
1609 hentry[0].body = tmp; \
1611 if (hentry[2].body != NULL) { \
1612 tmp = conv_unmime_header(hentry[2].body, NULL, FALSE); \
1613 g_free(hentry[2].body); \
1614 hentry[2].body = tmp; \
1616 if (hentry[4].body != NULL) { \
1617 tmp = conv_unmime_header(hentry[4].body, NULL, FALSE); \
1618 g_free(hentry[4].body); \
1619 hentry[4].body = tmp; \
1621 if (hentry[5].body != NULL) { \
1622 tmp = conv_unmime_header(hentry[5].body, NULL, FALSE); \
1623 g_free(hentry[5].body); \
1624 hentry[5].body = tmp; \
1626 if (hentry[6].body != NULL) { \
1627 tmp = conv_unmime_header(hentry[6].body, NULL, FALSE); \
1628 g_free(hentry[6].body); \
1629 hentry[6].body = tmp; \
1631 if (hentry[7].body != NULL) { \
1632 tmp = conv_unmime_header(hentry[7].body, NULL, FALSE); \
1633 g_free(hentry[7].body); \
1634 hentry[7].body = tmp; \
1638 static void procmime_parse_multipart(MimeInfo *mimeinfo, gboolean short_scan)
1640 HeaderEntry hentry[] = {{"Content-Type:", NULL, TRUE},
1641 {"Content-Transfer-Encoding:",
1643 {"Content-Description:",
1647 {"Content-Disposition:",
1649 {"Content-Location:",
1651 {"Original-Message-ID:",
1655 {NULL, NULL, FALSE}};
1658 gint boundary_len = 0, lastoffset = -1, i;
1659 gchar buf[BUFFSIZE];
1662 gboolean start_found = FALSE;
1663 gboolean end_found = FALSE;
1665 boundary = g_hash_table_lookup(mimeinfo->typeparameters, "boundary");
1668 boundary_len = strlen(boundary);
1670 procmime_decode_content(mimeinfo);
1672 fp = procmime_fopen(mimeinfo->data.filename, "rb");
1674 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
1678 if (fseek(fp, mimeinfo->offset, SEEK_SET) < 0) {
1679 FILE_OP_ERROR(mimeinfo->data.filename, "fseek");
1680 procmime_fclose(fp);
1684 while (SC_FGETS(buf, sizeof(buf), fp) != NULL && result == 0) {
1685 if (ftell(fp) - 1 > (mimeinfo->offset + mimeinfo->length))
1688 if (IS_BOUNDARY(buf, boundary, boundary_len)) {
1691 if (lastoffset != -1) {
1692 gint len = (ftell(fp) - strlen(buf)) - lastoffset - 1;
1695 result = procmime_parse_mimepart(mimeinfo,
1696 hentry[0].body, hentry[1].body,
1697 hentry[2].body, hentry[3].body,
1698 hentry[4].body, hentry[5].body,
1699 hentry[6].body, hentry[7].body,
1700 mimeinfo->data.filename, lastoffset,
1702 if (result == 1 && short_scan)
1707 if (buf[2 + boundary_len] == '-' &&
1708 buf[2 + boundary_len + 1] == '-') {
1712 for (i = 0; i < (sizeof hentry / sizeof hentry[0]) ; i++) {
1713 g_free(hentry[i].body);
1714 hentry[i].body = NULL;
1717 lastoffset = ftell(fp);
1721 if (start_found && !end_found && lastoffset != -1) {
1722 gint len = (ftell(fp) - strlen(buf)) - lastoffset - 1;
1725 result = procmime_parse_mimepart(mimeinfo,
1726 hentry[0].body, hentry[1].body,
1727 hentry[2].body, hentry[3].body,
1728 hentry[4].body, hentry[5].body,
1729 hentry[6].body, hentry[7].body,
1730 mimeinfo->data.filename, lastoffset,
1733 mimeinfo->broken = TRUE;
1736 for (i = 0; i < (sizeof hentry / sizeof hentry[0]); i++) {
1737 g_free(hentry[i].body);
1738 hentry[i].body = NULL;
1740 procmime_fclose(fp);
1743 static void parse_parameters(const gchar *parameters, GHashTable *table)
1745 gchar *params, *param, *next;
1746 GSList *convlist = NULL, *concatlist = NULL, *cur;
1748 params = g_strdup(parameters);
1751 for (; next != NULL; param = next) {
1752 gchar *attribute, *value, *tmp, *down_attr, *orig_down_attr;
1754 gboolean convert = FALSE;
1756 next = strchr_with_skip_quote(param, '"', ';');
1765 value = strchr(attribute, '=');
1771 while (value[0] != '\0' && value[0] == ' ')
1774 down_attr = g_utf8_strdown(attribute, -1);
1775 orig_down_attr = down_attr;
1777 len = down_attr ? strlen(down_attr):0;
1778 if (len > 0 && down_attr[len - 1] == '*') {
1779 gchar *srcpos, *dstpos, *endpos;
1782 down_attr[len - 1] = '\0';
1786 endpos = value + strlen(value);
1787 while (srcpos < endpos) {
1793 if (!get_hex_value(&dstvalue, srcpos[1], srcpos[2]))
1803 if (value[0] == '"')
1804 extract_quote(value, '"');
1806 if (value[0] == '"')
1807 extract_quote(value, '"');
1808 else if ((tmp = strchr(value, ' ')) != NULL)
1813 while (down_attr[0] == ' ')
1815 while (down_attr[strlen(down_attr)-1] == ' ')
1816 down_attr[strlen(down_attr)-1] = '\0';
1819 while (value[0] != '\0' && value[0] == ' ')
1821 while (value[strlen(value)-1] == ' ')
1822 value[strlen(value)-1] = '\0';
1824 if (down_attr && strrchr(down_attr, '*') != NULL) {
1827 tmpattr = g_strdup(down_attr);
1828 tmp = strrchr(tmpattr, '*');
1831 if ((tmp[1] == '0') && (tmp[2] == '\0') &&
1832 (g_slist_find_custom(concatlist, down_attr, (GCompareFunc)g_strcmp0) == NULL))
1833 concatlist = g_slist_prepend(concatlist, g_strdup(tmpattr));
1835 if (convert && (g_slist_find_custom(convlist, tmpattr, (GCompareFunc)g_strcmp0) == NULL))
1836 convlist = g_slist_prepend(convlist, g_strdup(tmpattr));
1839 } else if (convert) {
1840 if (g_slist_find_custom(convlist, down_attr, (GCompareFunc)g_strcmp0) == NULL)
1841 convlist = g_slist_prepend(convlist, g_strdup(down_attr));
1844 if (g_hash_table_lookup(table, down_attr) == NULL)
1845 g_hash_table_insert(table, g_strdup(down_attr), g_strdup(value));
1846 g_free(orig_down_attr);
1849 for (cur = concatlist; cur != NULL; cur = g_slist_next(cur)) {
1850 gchar *attribute, *attrwnum, *partvalue;
1854 attribute = (gchar *) cur->data;
1855 value = g_string_sized_new(64);
1857 attrwnum = g_strdup_printf("%s*%d", attribute, n);
1858 while ((partvalue = g_hash_table_lookup(table, attrwnum)) != NULL) {
1859 g_string_append(value, partvalue);
1861 g_hash_table_remove(table, attrwnum);
1864 attrwnum = g_strdup_printf("%s*%d", attribute, n);
1868 g_hash_table_insert(table, g_strdup(attribute), g_strdup(value->str));
1869 g_string_free(value, TRUE);
1871 slist_free_strings_full(concatlist);
1873 for (cur = convlist; cur != NULL; cur = g_slist_next(cur)) {
1874 gchar *attribute, *key, *value;
1875 gchar *charset, *lang, *oldvalue, *newvalue;
1877 attribute = (gchar *) cur->data;
1878 if (!g_hash_table_lookup_extended(
1879 table, attribute, (gpointer *)(gchar *) &key, (gpointer *)(gchar *) &value))
1883 if (charset == NULL)
1885 lang = strchr(charset, '\'');
1890 oldvalue = strchr(lang, '\'');
1891 if (oldvalue == NULL)
1896 newvalue = conv_codeset_strdup(oldvalue, charset, CS_UTF_8);
1898 g_hash_table_remove(table, attribute);
1902 g_hash_table_insert(table, g_strdup(attribute), newvalue);
1904 slist_free_strings_full(convlist);
1909 static void procmime_parse_content_type(const gchar *content_type, MimeInfo *mimeinfo)
1911 cm_return_if_fail(content_type != NULL);
1912 cm_return_if_fail(mimeinfo != NULL);
1914 /* RFC 2045, page 13 says that the mime subtype is MANDATORY;
1915 * if it's not available we use the default Content-Type */
1916 if ((content_type[0] == '\0') || (strchr(content_type, '/') == NULL)) {
1917 mimeinfo->type = MIMETYPE_TEXT;
1918 mimeinfo->subtype = g_strdup("plain");
1919 if (g_hash_table_lookup(mimeinfo->typeparameters,
1920 "charset") == NULL) {
1921 g_hash_table_insert(mimeinfo->typeparameters,
1922 g_strdup("charset"),
1924 conv_get_locale_charset_str_no_utf8()));
1927 gchar *type, *subtype, *params;
1929 type = g_strdup(content_type);
1930 subtype = strchr(type, '/') + 1;
1931 *(subtype - 1) = '\0';
1932 if ((params = strchr(subtype, ';')) != NULL) {
1937 mimeinfo->type = procmime_get_media_type(type);
1938 mimeinfo->subtype = g_strstrip(g_strdup(subtype));
1940 /* Get mimeinfo->typeparameters */
1942 parse_parameters(params, mimeinfo->typeparameters);
1948 static void procmime_parse_content_disposition(const gchar *content_disposition, MimeInfo *mimeinfo)
1950 gchar *tmp, *params;
1952 cm_return_if_fail(content_disposition != NULL);
1953 cm_return_if_fail(mimeinfo != NULL);
1955 tmp = g_strdup(content_disposition);
1956 if ((params = strchr(tmp, ';')) != NULL) {
1962 if (!g_ascii_strcasecmp(tmp, "inline"))
1963 mimeinfo->disposition = DISPOSITIONTYPE_INLINE;
1964 else if (!g_ascii_strcasecmp(tmp, "attachment"))
1965 mimeinfo->disposition = DISPOSITIONTYPE_ATTACHMENT;
1967 mimeinfo->disposition = DISPOSITIONTYPE_ATTACHMENT;
1970 parse_parameters(params, mimeinfo->dispositionparameters);
1976 static void procmime_parse_content_encoding(const gchar *content_encoding, MimeInfo *mimeinfo)
1978 struct EncodingTable *enc_table;
1980 for (enc_table = encoding_table; enc_table->str != NULL; enc_table++) {
1981 if (g_ascii_strcasecmp(enc_table->str, content_encoding) == 0) {
1982 mimeinfo->encoding_type = enc_table->enc_type;
1986 mimeinfo->encoding_type = ENC_UNKNOWN;
1990 static GSList *registered_parsers = NULL;
1992 static MimeParser *procmime_get_mimeparser_for_type(MimeMediaType type, const gchar *sub_type)
1995 for (cur = registered_parsers; cur; cur = cur->next) {
1996 MimeParser *parser = (MimeParser *)cur->data;
1997 if (parser->type == type && !strcmp2(parser->sub_type, sub_type))
2003 void procmime_mimeparser_register(MimeParser *parser)
2005 if (!procmime_get_mimeparser_for_type(parser->type, parser->sub_type))
2006 registered_parsers = g_slist_append(registered_parsers, parser);
2010 void procmime_mimeparser_unregister(MimeParser *parser)
2012 registered_parsers = g_slist_remove(registered_parsers, parser);
2015 static gboolean procmime_mimeparser_parse(MimeParser *parser, MimeInfo *mimeinfo)
2017 cm_return_val_if_fail(parser->parse != NULL, FALSE);
2018 return parser->parse(parser, mimeinfo);
2021 static int procmime_parse_mimepart(MimeInfo *parent,
2022 gchar *content_type,
2023 gchar *content_encoding,
2024 gchar *content_description,
2026 gchar *content_disposition,
2027 gchar *content_location,
2028 const gchar *original_msgid,
2029 const gchar *disposition_notification_hdr,
2030 const gchar *filename,
2033 gboolean short_scan)
2036 MimeParser *parser = NULL;
2037 gboolean parsed = FALSE;
2040 /* Create MimeInfo */
2041 mimeinfo = procmime_mimeinfo_new();
2042 mimeinfo->content = MIMECONTENT_FILE;
2044 if (parent != NULL) {
2045 if (g_node_depth(parent->node) > 32) {
2046 /* 32 is an arbitrary value
2047 * this avoids DOSsing ourselves
2048 * with enormous messages
2050 procmime_mimeinfo_free_all(&mimeinfo);
2053 g_node_append(parent->node, mimeinfo->node);
2055 mimeinfo->data.filename = g_strdup(filename);
2056 mimeinfo->offset = offset;
2057 mimeinfo->length = length;
2059 if (content_type != NULL) {
2060 g_strchomp(content_type);
2061 procmime_parse_content_type(content_type, mimeinfo);
2063 mimeinfo->type = MIMETYPE_TEXT;
2064 mimeinfo->subtype = g_strdup("plain");
2065 if (g_hash_table_lookup(mimeinfo->typeparameters,
2066 "charset") == NULL) {
2067 g_hash_table_insert(mimeinfo->typeparameters,
2068 g_strdup("charset"),
2070 conv_get_locale_charset_str_no_utf8()));
2074 if (content_encoding != NULL) {
2075 g_strchomp(content_encoding);
2076 procmime_parse_content_encoding(content_encoding, mimeinfo);
2078 mimeinfo->encoding_type = ENC_UNKNOWN;
2081 if (content_description != NULL)
2082 mimeinfo->description = g_strdup(content_description);
2084 mimeinfo->description = NULL;
2086 if (content_id != NULL)
2087 mimeinfo->id = g_strdup(content_id);
2089 mimeinfo->id = NULL;
2091 if (content_location != NULL)
2092 mimeinfo->location = g_strdup(content_location);
2094 mimeinfo->location = NULL;
2096 if (content_disposition != NULL) {
2097 g_strchomp(content_disposition);
2098 procmime_parse_content_disposition(content_disposition, mimeinfo);
2100 mimeinfo->disposition = DISPOSITIONTYPE_UNKNOWN;
2102 /* Call parser for mime type */
2103 if ((parser = procmime_get_mimeparser_for_type(mimeinfo->type, mimeinfo->subtype)) != NULL) {
2104 parsed = procmime_mimeparser_parse(parser, mimeinfo);
2107 switch (mimeinfo->type) {
2109 if (g_ascii_strcasecmp(mimeinfo->subtype, "plain") == 0 && short_scan) {
2114 case MIMETYPE_MESSAGE:
2115 if (g_ascii_strcasecmp(mimeinfo->subtype, "rfc822") == 0) {
2116 procmime_parse_message_rfc822(mimeinfo, short_scan);
2118 if (g_ascii_strcasecmp(mimeinfo->subtype, "disposition-notification") == 0) {
2119 procmime_parse_disposition_notification(mimeinfo,
2120 original_msgid, disposition_notification_hdr, short_scan);
2124 case MIMETYPE_MULTIPART:
2125 procmime_parse_multipart(mimeinfo, short_scan);
2128 case MIMETYPE_APPLICATION:
2129 if (g_ascii_strcasecmp(mimeinfo->subtype, "octet-stream") == 0
2130 && original_msgid && *original_msgid
2131 && disposition_notification_hdr && *disposition_notification_hdr) {
2132 procmime_parse_disposition_notification(mimeinfo,
2133 original_msgid, disposition_notification_hdr, short_scan);
2144 static gchar *typenames[] = {
2155 static gboolean output_func(GNode *node, gpointer data)
2158 MimeInfo *mimeinfo = (MimeInfo *) node->data;
2160 depth = g_node_depth(node);
2161 for (i = 0; i < depth; i++)
2163 g_print("%s/%s (offset:%d length:%d encoding: %d)\n", typenames[mimeinfo->type], mimeinfo->subtype, mimeinfo->offset, mimeinfo->length, mimeinfo->encoding_type);
2168 static void output_mime_structure(MimeInfo *mimeinfo, int indent)
2170 g_node_traverse(mimeinfo->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1, output_func, NULL);
2173 static MimeInfo *procmime_scan_file_with_offset(const gchar *filename, int offset, gboolean short_scan)
2178 if (g_stat(filename, &buf) < 0) {
2179 FILE_OP_ERROR(filename, "stat");
2183 mimeinfo = procmime_mimeinfo_new();
2184 mimeinfo->content = MIMECONTENT_FILE;
2185 mimeinfo->encoding_type = ENC_UNKNOWN;
2186 mimeinfo->type = MIMETYPE_MESSAGE;
2187 mimeinfo->subtype = g_strdup("rfc822");
2188 mimeinfo->data.filename = g_strdup(filename);
2189 mimeinfo->offset = offset;
2190 mimeinfo->length = buf.st_size - offset;
2192 procmime_parse_message_rfc822(mimeinfo, short_scan);
2193 if (debug_get_mode())
2194 output_mime_structure(mimeinfo, 0);
2199 static MimeInfo *procmime_scan_file_full(const gchar *filename, gboolean short_scan)
2203 cm_return_val_if_fail(filename != NULL, NULL);
2205 mimeinfo = procmime_scan_file_with_offset(filename, 0, short_scan);
2210 MimeInfo *procmime_scan_file(const gchar *filename)
2212 return procmime_scan_file_full(filename, FALSE);
2215 static MimeInfo *procmime_scan_file_short(const gchar *filename)
2217 return procmime_scan_file_full(filename, TRUE);
2220 static MimeInfo *procmime_scan_queue_file_full(const gchar *filename, gboolean short_scan)
2224 gchar buf[BUFFSIZE];
2227 cm_return_val_if_fail(filename != NULL, NULL);
2230 if ((fp = procmime_fopen(filename, "rb")) == NULL)
2232 /* Skip queue header */
2233 while (SC_FGETS(buf, sizeof(buf), fp) != NULL) {
2235 if ((!strncmp(buf, "X-Claws-End-Special-Headers: 1",
2236 strlen("X-Claws-End-Special-Headers:"))) ||
2237 (!strncmp(buf, "X-Sylpheed-End-Special-Headers: 1",
2238 strlen("X-Sylpheed-End-Special-Headers:"))))
2241 if (buf[0] == '\r' || buf[0] == '\n') break;
2242 /* from other mailers */
2243 if (!strncmp(buf, "Date: ", 6)
2244 || !strncmp(buf, "To: ", 4)
2245 || !strncmp(buf, "From: ", 6)
2246 || !strncmp(buf, "Subject: ", 9)) {
2252 procmime_fclose(fp);
2254 mimeinfo = procmime_scan_file_with_offset(filename, offset, short_scan);
2259 MimeInfo *procmime_scan_queue_file(const gchar *filename)
2261 return procmime_scan_queue_file_full(filename, FALSE);
2264 static MimeInfo *procmime_scan_queue_file_short(const gchar *filename)
2266 return procmime_scan_queue_file_full(filename, TRUE);
2271 ENC_AS_QUOTED_STRING,
2276 typedef struct _ParametersData {
2282 static void write_parameters(gpointer key, gpointer value, gpointer user_data)
2285 gchar *val = value, *valpos, *tmp;
2286 ParametersData *pdata = (ParametersData *)user_data;
2287 GString *buf = g_string_new("");
2290 EncodeAs encas = ENC_AS_TOKEN;
2292 for (valpos = val; *valpos != 0; valpos++) {
2293 if (!IS_ASCII(*valpos)) {
2294 encas = ENC_AS_ENCWORD;
2299 if (((*valpos >= 0) && (*valpos < 037)) || (*valpos == 0177)) {
2300 encas = ENC_AS_QUOTED_STRING;
2304 /* tspecials + SPACE */
2322 encas = ENC_AS_QUOTED_STRING;
2329 g_string_append_printf(buf, "%s=%s", param, val);
2332 case ENC_AS_QUOTED_STRING:
2333 g_string_append_printf(buf, "%s=\"%s\"", param, val);
2336 #if 0 /* we don't use that for now */
2337 case ENC_AS_EXTENDED:
2338 if (!g_utf8_validate(val, -1, NULL))
2339 g_string_append_printf(buf, "%s*=%s''", param,
2340 conv_get_locale_charset_str());
2342 g_string_append_printf(buf, "%s*=%s''", param,
2344 for (valpos = val; *valpos != '\0'; valpos++) {
2345 if (IS_ASCII(*valpos) && isalnum(*valpos)) {
2346 g_string_append_printf(buf, "%c", *valpos);
2348 gchar hexstr[3] = "XX";
2349 get_hex_str(hexstr, *valpos);
2350 g_string_append_printf(buf, "%%%s", hexstr);
2355 case ENC_AS_EXTENDED:
2356 debug_print("Unhandled ENC_AS_EXTENDED.\n");
2359 case ENC_AS_ENCWORD:
2360 len = MAX(strlen(val)*6, 512);
2361 tmp = g_malloc(len+1);
2362 codeconv_set_strict(TRUE);
2363 conv_encode_header_full(tmp, len, val, pdata->len + strlen(param) + 4 , FALSE,
2364 prefs_common.outgoing_charset);
2365 codeconv_set_strict(FALSE);
2366 if (!tmp || !*tmp) {
2367 codeconv_set_strict(TRUE);
2368 conv_encode_header_full(tmp, len, val, pdata->len + strlen(param) + 4 , FALSE,
2369 conv_get_outgoing_charset_str());
2370 codeconv_set_strict(FALSE);
2372 if (!tmp || !*tmp) {
2373 codeconv_set_strict(TRUE);
2374 conv_encode_header_full(tmp, len, val, pdata->len + strlen(param) + 4 , FALSE,
2376 codeconv_set_strict(FALSE);
2378 if (!tmp || !*tmp) {
2379 conv_encode_header_full(tmp, len, val, pdata->len + strlen(param) + 4 , FALSE,
2382 g_string_append_printf(buf, "%s=\"%s\"", param, tmp);
2388 if (buf->str && strlen(buf->str)) {
2389 tmp = strstr(buf->str, "\n");
2391 len = (tmp - buf->str);
2393 len = strlen(buf->str);
2394 if (pdata->len + len > 76) {
2395 if (fprintf(pdata->fp, ";\n %s", buf->str) < 0)
2396 pdata->error = TRUE;
2397 pdata->len = strlen(buf->str) + 1;
2399 if (fprintf(pdata->fp, "; %s", buf->str) < 0)
2400 pdata->error = TRUE;
2401 pdata->len += strlen(buf->str) + 2;
2404 g_string_free(buf, TRUE);
2407 #define TRY(func) { \
2413 int procmime_write_mime_header(MimeInfo *mimeinfo, FILE *fp)
2415 struct TypeTable *type_table;
2416 ParametersData *pdata = g_new0(ParametersData, 1);
2417 debug_print("procmime_write_mime_header\n");
2420 pdata->error = FALSE;
2421 for (type_table = mime_type_table; type_table->str != NULL; type_table++)
2422 if (mimeinfo->type == type_table->type) {
2423 gchar *buf = g_strdup_printf(
2424 "Content-Type: %s/%s", type_table->str, mimeinfo->subtype);
2425 if (fprintf(fp, "%s", buf) < 0) {
2430 pdata->len = strlen(buf);
2434 g_hash_table_foreach(mimeinfo->typeparameters, write_parameters, pdata);
2435 if (pdata->error == TRUE) {
2441 TRY(fprintf(fp, "\n") >= 0);
2443 if (mimeinfo->encoding_type != ENC_UNKNOWN)
2444 TRY(fprintf(fp, "Content-Transfer-Encoding: %s\n", procmime_get_encoding_str(mimeinfo->encoding_type)) >= 0);
2446 if (mimeinfo->description != NULL)
2447 TRY(fprintf(fp, "Content-Description: %s\n", mimeinfo->description) >= 0);
2449 if (mimeinfo->id != NULL)
2450 TRY(fprintf(fp, "Content-ID: %s\n", mimeinfo->id) >= 0);
2452 if (mimeinfo->location != NULL)
2453 TRY(fprintf(fp, "Content-Location: %s\n", mimeinfo->location) >= 0);
2455 if (mimeinfo->disposition != DISPOSITIONTYPE_UNKNOWN) {
2456 ParametersData *pdata = g_new0(ParametersData, 1);
2458 if (mimeinfo->disposition == DISPOSITIONTYPE_INLINE)
2459 buf = g_strdup("Content-Disposition: inline");
2460 else if (mimeinfo->disposition == DISPOSITIONTYPE_ATTACHMENT)
2461 buf = g_strdup("Content-Disposition: attachment");
2463 buf = g_strdup("Content-Disposition: unknown");
2465 if (fprintf(fp, "%s", buf) < 0) {
2470 pdata->len = strlen(buf);
2474 pdata->error = FALSE;
2475 g_hash_table_foreach(mimeinfo->dispositionparameters, write_parameters, pdata);
2476 if (pdata->error == TRUE) {
2481 TRY(fprintf(fp, "\n") >= 0);
2484 TRY(fprintf(fp, "\n") >= 0);
2489 static gint procmime_write_message_rfc822(MimeInfo *mimeinfo, FILE *fp)
2494 gchar buf[BUFFSIZE];
2495 gboolean skip = FALSE;;
2498 debug_print("procmime_write_message_rfc822\n");
2501 switch (mimeinfo->content) {
2502 case MIMECONTENT_FILE:
2503 if ((infp = procmime_fopen(mimeinfo->data.filename, "rb")) == NULL) {
2504 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
2507 if (fseek(infp, mimeinfo->offset, SEEK_SET) < 0) {
2508 FILE_OP_ERROR(mimeinfo->data.filename, "fseek");
2509 procmime_fclose(infp);
2512 while (SC_FGETS(buf, sizeof(buf), infp) == buf) {
2514 if (buf[0] == '\n' && buf[1] == '\0')
2516 if (skip && (buf[0] == ' ' || buf[0] == '\t'))
2518 if (g_ascii_strncasecmp(buf, "MIME-Version:", 13) == 0 ||
2519 g_ascii_strncasecmp(buf, "Content-Type:", 13) == 0 ||
2520 g_ascii_strncasecmp(buf, "Content-Transfer-Encoding:", 26) == 0 ||
2521 g_ascii_strncasecmp(buf, "Content-Description:", 20) == 0 ||
2522 g_ascii_strncasecmp(buf, "Content-ID:", 11) == 0 ||
2523 g_ascii_strncasecmp(buf, "Content-Location:", 17) == 0 ||
2524 g_ascii_strncasecmp(buf, "Content-Disposition:", 20) == 0) {
2529 if (SC_FWRITE(buf, sizeof(gchar), len, fp) < len) {
2530 g_warning("failed to dump %zd bytes from file", len);
2531 procmime_fclose(infp);
2536 procmime_fclose(infp);
2539 case MIMECONTENT_MEM:
2540 len = strlen(mimeinfo->data.mem);
2541 if (SC_FWRITE(mimeinfo->data.mem, sizeof(gchar), len, fp) < len) {
2542 g_warning("failed to dump %zd bytes from mem", len);
2551 childnode = mimeinfo->node->children;
2552 if (childnode == NULL)
2555 child = (MimeInfo *) childnode->data;
2556 if (fprintf(fp, "MIME-Version: 1.0\n") < 0) {
2557 g_warning("failed to write mime version");
2560 if (procmime_write_mime_header(child, fp) < 0)
2562 return procmime_write_mimeinfo(child, fp);
2565 static gint procmime_write_multipart(MimeInfo *mimeinfo, FILE *fp)
2569 gchar *boundary, *str, *str2;
2570 gchar buf[BUFFSIZE];
2571 gboolean firstboundary;
2574 debug_print("procmime_write_multipart\n");
2576 boundary = g_hash_table_lookup(mimeinfo->typeparameters, "boundary");
2578 switch (mimeinfo->content) {
2579 case MIMECONTENT_FILE:
2580 if ((infp = procmime_fopen(mimeinfo->data.filename, "rb")) == NULL) {
2581 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
2584 if (fseek(infp, mimeinfo->offset, SEEK_SET) < 0) {
2585 FILE_OP_ERROR(mimeinfo->data.filename, "fseek");
2586 procmime_fclose(infp);
2589 while (SC_FGETS(buf, sizeof(buf), infp) == buf) {
2590 if (IS_BOUNDARY(buf, boundary, strlen(boundary)))
2593 if (SC_FWRITE(buf, sizeof(gchar), len, fp) < len) {
2594 g_warning("failed to write %zd", len);
2595 procmime_fclose(infp);
2599 procmime_fclose(infp);
2602 case MIMECONTENT_MEM:
2603 str = g_strdup(mimeinfo->data.mem);
2604 if (((str2 = strstr(str, boundary)) != NULL) && ((str2 - str) >= 2) &&
2605 (*(str2 - 1) == '-') && (*(str2 - 2) == '-'))
2608 if (SC_FWRITE(str, sizeof(gchar), len, fp) < len) {
2609 g_warning("failed to write %zd from mem", len);
2620 childnode = mimeinfo->node->children;
2621 firstboundary = TRUE;
2622 while (childnode != NULL) {
2623 MimeInfo *child = childnode->data;
2626 firstboundary = FALSE;
2628 TRY(fprintf(fp, "\n") >= 0);
2630 TRY(fprintf(fp, "--%s\n", boundary) >= 0);
2632 if (procmime_write_mime_header(child, fp) < 0)
2634 if (procmime_write_mimeinfo(child, fp) < 0)
2637 childnode = g_node_next_sibling(childnode);
2639 TRY(fprintf(fp, "\n--%s--\n", boundary) >= 0);
2644 gint procmime_write_mimeinfo(MimeInfo *mimeinfo, FILE *fp)
2648 debug_print("procmime_write_mimeinfo\n");
2650 if (G_NODE_IS_LEAF(mimeinfo->node)) {
2651 switch (mimeinfo->content) {
2652 case MIMECONTENT_FILE:
2653 if ((infp = procmime_fopen(mimeinfo->data.filename, "rb")) == NULL) {
2654 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
2657 copy_file_part_to_fp(infp, mimeinfo->offset, mimeinfo->length, fp);
2658 procmime_fclose(infp);
2661 case MIMECONTENT_MEM:
2662 len = strlen(mimeinfo->data.mem);
2663 if (SC_FWRITE(mimeinfo->data.mem, sizeof(gchar), len, fp) < len)
2671 /* Call writer for mime type */
2672 switch (mimeinfo->type) {
2673 case MIMETYPE_MESSAGE:
2674 if (g_ascii_strcasecmp(mimeinfo->subtype, "rfc822") == 0) {
2675 return procmime_write_message_rfc822(mimeinfo, fp);
2679 case MIMETYPE_MULTIPART:
2680 return procmime_write_multipart(mimeinfo, fp);
2692 gchar *procmime_get_part_file_name(MimeInfo *mimeinfo)
2696 if ((mimeinfo->type == MIMETYPE_TEXT) && !g_ascii_strcasecmp(mimeinfo->subtype, "html"))
2697 base = g_strdup("mimetmp.html");
2699 const gchar *basetmp;
2702 basetmp = procmime_mimeinfo_get_parameter(mimeinfo, "filename");
2703 if (basetmp == NULL)
2704 basetmp = procmime_mimeinfo_get_parameter(mimeinfo, "name");
2705 if (basetmp == NULL)
2706 basetmp = "mimetmp";
2707 basename = g_path_get_basename(basetmp);
2708 if (*basename == '\0') {
2710 basename = g_strdup("mimetmp");
2712 base = conv_filename_from_utf8(basename);
2714 subst_for_shellsafe_filename(base);