2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2012 Hiroyuki Yamamoto & The Claws Mail Team
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "claws-features.h"
31 #include <glib/gi18n.h>
37 #include <sys/types.h>
43 #include "procheader.h"
45 #include "quoted-printable.h"
52 #include "prefs_common.h"
53 #include "prefs_gtk.h"
54 #include "alertpanel.h"
58 static GHashTable *procmime_get_mime_type_table (void);
59 static MimeInfo *procmime_scan_file_short(const gchar *filename);
60 static MimeInfo *procmime_scan_queue_file_short(const gchar *filename);
61 static MimeInfo *procmime_scan_queue_file_full(const gchar *filename, gboolean short_scan);
63 MimeInfo *procmime_mimeinfo_new(void)
67 mimeinfo = g_new0(MimeInfo, 1);
68 mimeinfo->content = MIMECONTENT_EMPTY;
69 mimeinfo->data.filename = NULL;
71 mimeinfo->type = MIMETYPE_UNKNOWN;
72 mimeinfo->encoding_type = ENC_UNKNOWN;
73 mimeinfo->typeparameters = g_hash_table_new(g_str_hash, g_str_equal);
75 mimeinfo->disposition = DISPOSITIONTYPE_UNKNOWN;
76 mimeinfo->dispositionparameters
77 = g_hash_table_new(g_str_hash, g_str_equal);
79 mimeinfo->node = g_node_new(mimeinfo);
84 static gboolean procmime_mimeinfo_parameters_destroy(gpointer key, gpointer value, gpointer user_data)
92 static gchar *forced_charset = NULL;
94 void procmime_force_charset(const gchar *str)
96 g_free(forced_charset);
97 forced_charset = NULL;
99 forced_charset = g_strdup(str);
102 static EncodingType forced_encoding = 0;
104 void procmime_force_encoding(EncodingType encoding)
106 forced_encoding = encoding;
109 static gboolean free_func(GNode *node, gpointer data)
111 MimeInfo *mimeinfo = (MimeInfo *) node->data;
113 switch (mimeinfo->content) {
114 case MIMECONTENT_FILE:
116 claws_unlink(mimeinfo->data.filename);
117 g_free(mimeinfo->data.filename);
120 case MIMECONTENT_MEM:
122 g_free(mimeinfo->data.mem);
127 g_free(mimeinfo->subtype);
128 g_free(mimeinfo->description);
129 g_free(mimeinfo->id);
130 g_free(mimeinfo->location);
132 g_hash_table_foreach_remove(mimeinfo->typeparameters,
133 procmime_mimeinfo_parameters_destroy, NULL);
134 g_hash_table_destroy(mimeinfo->typeparameters);
135 g_hash_table_foreach_remove(mimeinfo->dispositionparameters,
136 procmime_mimeinfo_parameters_destroy, NULL);
137 g_hash_table_destroy(mimeinfo->dispositionparameters);
139 if (mimeinfo->privacy)
140 privacy_free_privacydata(mimeinfo->privacy);
147 void procmime_mimeinfo_free_all(MimeInfo *mimeinfo)
154 node = mimeinfo->node;
155 g_node_traverse(node, G_IN_ORDER, G_TRAVERSE_ALL, -1, free_func, NULL);
157 g_node_destroy(node);
160 MimeInfo *procmime_mimeinfo_parent(MimeInfo *mimeinfo)
162 cm_return_val_if_fail(mimeinfo != NULL, NULL);
163 cm_return_val_if_fail(mimeinfo->node != NULL, NULL);
165 if (mimeinfo->node->parent == NULL)
167 return (MimeInfo *) mimeinfo->node->parent->data;
170 MimeInfo *procmime_mimeinfo_next(MimeInfo *mimeinfo)
172 cm_return_val_if_fail(mimeinfo != NULL, NULL);
173 cm_return_val_if_fail(mimeinfo->node != NULL, NULL);
175 if (mimeinfo->node->children)
176 return (MimeInfo *) mimeinfo->node->children->data;
177 if (mimeinfo->node->next)
178 return (MimeInfo *) mimeinfo->node->next->data;
180 if (mimeinfo->node->parent == NULL)
183 while (mimeinfo->node->parent != NULL) {
184 mimeinfo = (MimeInfo *) mimeinfo->node->parent->data;
185 if (mimeinfo->node->next)
186 return (MimeInfo *) mimeinfo->node->next->data;
192 MimeInfo *procmime_scan_message(MsgInfo *msginfo)
197 filename = procmsg_get_message_file_path(msginfo);
198 if (!filename || !is_file_exist(filename)) {
203 if (!folder_has_parent_of_type(msginfo->folder, F_QUEUE) &&
204 !folder_has_parent_of_type(msginfo->folder, F_DRAFT))
205 mimeinfo = procmime_scan_file(filename);
207 mimeinfo = procmime_scan_queue_file(filename);
213 MimeInfo *procmime_scan_message_short(MsgInfo *msginfo)
218 filename = procmsg_get_message_file_path(msginfo);
219 if (!filename || !is_file_exist(filename)) {
224 if (!folder_has_parent_of_type(msginfo->folder, F_QUEUE) &&
225 !folder_has_parent_of_type(msginfo->folder, F_DRAFT))
226 mimeinfo = procmime_scan_file_short(filename);
228 mimeinfo = procmime_scan_queue_file_short(filename);
236 H_CONTENT_TRANSFER_ENCODING = 0,
238 H_CONTENT_DISPOSITION = 2,
239 H_CONTENT_DESCRIPTION = 3,
243 const gchar *procmime_mimeinfo_get_parameter(MimeInfo *mimeinfo, const gchar *name)
247 cm_return_val_if_fail(mimeinfo != NULL, NULL);
248 cm_return_val_if_fail(name != NULL, NULL);
250 value = g_hash_table_lookup(mimeinfo->dispositionparameters, name);
252 value = g_hash_table_lookup(mimeinfo->typeparameters, name);
257 #ifdef HAVE_FGETS_UNLOCKED
258 #define SC_FGETS fgets_unlocked
259 #define SC_FPUTS fputs_unlocked
260 #define SC_FPUTC fputc_unlocked
261 #define SC_FREAD fread_unlocked
262 #define SC_FWRITE fwrite_unlocked
263 #define SC_FEOF feof_unlocked
264 #define SC_FERROR ferror_unlocked
266 static FILE *procmime_fopen(const gchar *file, const gchar *mode)
268 FILE *fp = g_fopen(file, mode);
274 static int procmime_fclose(FILE *fp)
280 #define SC_FGETS fgets
281 #define SC_FPUTS fputs
282 #define SC_FPUTC fputc
283 #define SC_FREAD fread
284 #define SC_FWRITE fwrite
286 #define SC_FERROR ferror
287 #define procmime_fopen g_fopen
288 #define procmime_fclose fclose
291 #define FLUSH_LASTLINE() { \
292 if (*lastline != '\0') { \
294 strretchomp(lastline); \
295 llen = strlen(lastline); \
296 if (lastline[llen-1] == ' ' && strcmp(lastline,"-- ")) { \
297 /* this is flowed */ \
299 lastline[llen-1] = '\0'; \
300 if (SC_FPUTS(lastline, outfp) == EOF) \
303 if (SC_FPUTS(lastline, outfp) == EOF) \
305 if (SC_FPUTS("\n", outfp) == EOF) \
309 strcpy(lastline, buf); \
312 gboolean procmime_decode_content(MimeInfo *mimeinfo)
319 gboolean tmp_file = FALSE;
320 gboolean flowed = FALSE;
321 gboolean delsp = FALSE;
322 gboolean err = FALSE;
324 EncodingType encoding = forced_encoding
326 : mimeinfo->encoding_type;
327 gchar lastline[BUFFSIZE];
328 memset(lastline, 0, BUFFSIZE);
330 cm_return_val_if_fail(mimeinfo != NULL, FALSE);
332 if (prefs_common.respect_flowed_format &&
333 mimeinfo->type == MIMETYPE_TEXT &&
334 !strcasecmp(mimeinfo->subtype, "plain")) {
335 if (procmime_mimeinfo_get_parameter(mimeinfo, "format") != NULL &&
336 !strcasecmp(procmime_mimeinfo_get_parameter(mimeinfo, "format"),"flowed"))
339 procmime_mimeinfo_get_parameter(mimeinfo, "delsp") != NULL &&
340 !strcasecmp(procmime_mimeinfo_get_parameter(mimeinfo, "delsp"),"yes"))
345 encoding == ENC_UNKNOWN ||
346 encoding == ENC_BINARY ||
347 encoding == ENC_7BIT ||
352 if (mimeinfo->type == MIMETYPE_MULTIPART || mimeinfo->type == MIMETYPE_MESSAGE)
355 if (mimeinfo->data.filename == NULL)
358 infp = procmime_fopen(mimeinfo->data.filename, "rb");
363 if (fseek(infp, mimeinfo->offset, SEEK_SET) < 0) {
365 procmime_fclose(infp);
369 outfp = get_tmpfile_in_dir(get_mime_tmp_dir(), &tmpfilename);
372 procmime_fclose(infp);
375 #ifdef HAVE_FGETS_UNLOCKED
379 readend = mimeinfo->offset + mimeinfo->length;
381 if (encoding == ENC_QUOTED_PRINTABLE) {
382 while ((ftell(infp) < readend) && (SC_FGETS(buf, sizeof(buf), infp) != NULL)) {
384 len = qp_decode_line(buf);
387 if (SC_FWRITE(buf, 1, len, outfp) < len)
395 } else if (encoding == ENC_BASE64) {
396 gchar outbuf[BUFFSIZE];
397 gint len, inlen, inread;
398 Base64Decoder *decoder;
399 gboolean got_error = FALSE;
400 gboolean uncanonicalize = FALSE;
402 gboolean null_bytes = FALSE;
403 gboolean starting = TRUE;
405 if (mimeinfo->type == MIMETYPE_TEXT ||
406 mimeinfo->type == MIMETYPE_MESSAGE) {
407 uncanonicalize = TRUE;
408 tmpfp = my_tmpfile();
412 procmime_fclose(outfp);
413 procmime_fclose(infp);
416 #ifdef HAVE_FGETS_UNLOCKED
422 decoder = base64_decoder_new();
423 while ((inlen = MIN(readend - ftell(infp), sizeof(buf))) > 0 && !err) {
424 inread = SC_FREAD(buf, 1, inlen, infp);
425 len = base64_decoder_decode(decoder, buf, outbuf, inread);
426 if (uncanonicalize == TRUE && strlen(outbuf) < len && starting) {
427 uncanonicalize = FALSE;
431 if (((inread != inlen) || len < 0) && !got_error) {
432 g_warning("Bad BASE64 content.\n");
433 if (SC_FWRITE(_("[Error decoding BASE64]\n"),
435 strlen(_("[Error decoding BASE64]\n")),
436 tmpfp) < strlen(_("[Error decoding BASE64]\n")))
437 g_warning("error decoding BASE64");
440 } else if (len >= 0) {
441 /* print out the error message only once
444 /* we won't uncanonicalize, output to outfp directly */
445 if (SC_FWRITE(outbuf, sizeof(gchar), len, outfp) < len)
448 if (SC_FWRITE(outbuf, sizeof(gchar), len, tmpfp) < len)
454 base64_decoder_free(decoder);
456 if (uncanonicalize) {
458 while (SC_FGETS(buf, sizeof(buf), tmpfp) != NULL) {
460 if (SC_FPUTS(buf, outfp) == EOF)
463 procmime_fclose(tmpfp);
465 } else if (encoding == ENC_X_UUENCODE) {
466 gchar outbuf[BUFFSIZE];
468 gboolean flag = FALSE;
470 while ((ftell(infp) < readend) && (SC_FGETS(buf, sizeof(buf), infp) != NULL)) {
471 if (!flag && strncmp(buf,"begin ", 6)) continue;
474 len = fromuutobits(outbuf, buf);
477 g_warning("Bad UUENCODE content(%d)\n", len);
480 if (SC_FWRITE(outbuf, sizeof(gchar), len, outfp) < len)
486 while ((ftell(infp) < readend) && (SC_FGETS(buf, sizeof(buf), infp) != NULL)) {
488 if (SC_FPUTS(buf, outfp) == EOF)
497 g_warning("write error");
500 procmime_fclose(outfp);
501 procmime_fclose(infp);
507 if (g_stat(tmpfilename, &statbuf) < 0) {
508 FILE_OP_ERROR(tmpfilename, "stat");
513 claws_unlink(mimeinfo->data.filename);
514 g_free(mimeinfo->data.filename);
515 mimeinfo->data.filename = tmpfilename;
516 mimeinfo->tmp = TRUE;
517 mimeinfo->offset = 0;
518 mimeinfo->length = statbuf.st_size;
519 mimeinfo->encoding_type = ENC_BINARY;
524 #define B64_LINE_SIZE 57
525 #define B64_BUFFSIZE 77
527 gboolean procmime_encode_content(MimeInfo *mimeinfo, EncodingType encoding)
529 FILE *infp = NULL, *outfp;
533 gboolean err = FALSE;
535 if (mimeinfo->content == MIMECONTENT_EMPTY)
538 if (mimeinfo->encoding_type != ENC_UNKNOWN &&
539 mimeinfo->encoding_type != ENC_BINARY &&
540 mimeinfo->encoding_type != ENC_7BIT &&
541 mimeinfo->encoding_type != ENC_8BIT)
542 if(!procmime_decode_content(mimeinfo))
545 outfp = get_tmpfile_in_dir(get_mime_tmp_dir(), &tmpfilename);
550 #ifdef HAVE_FGETS_UNLOCKED
554 if (mimeinfo->content == MIMECONTENT_FILE && mimeinfo->data.filename) {
555 if ((infp = procmime_fopen(mimeinfo->data.filename, "rb")) == NULL) {
556 g_warning("Can't open file %s\n", mimeinfo->data.filename);
557 procmime_fclose(outfp);
560 } else if (mimeinfo->content == MIMECONTENT_MEM) {
561 infp = str_open_as_stream(mimeinfo->data.mem);
563 procmime_fclose(outfp);
566 #ifdef HAVE_FGETS_UNLOCKED
570 procmime_fclose(outfp);
571 g_warning("Unknown mimeinfo");
575 if (encoding == ENC_BASE64) {
576 gchar inbuf[B64_LINE_SIZE], outbuf[B64_BUFFSIZE];
578 gchar *tmp_file = NULL;
580 if (mimeinfo->type == MIMETYPE_TEXT ||
581 mimeinfo->type == MIMETYPE_MESSAGE) {
582 if (mimeinfo->content == MIMECONTENT_FILE) {
583 tmp_file = get_tmp_file();
584 if (canonicalize_file(mimeinfo->data.filename, tmp_file) < 0) {
586 procmime_fclose(infp);
587 procmime_fclose(outfp);
590 if ((tmp_fp = procmime_fopen(tmp_file, "rb")) == NULL) {
591 FILE_OP_ERROR(tmp_file, "fopen");
592 claws_unlink(tmp_file);
594 procmime_fclose(infp);
595 procmime_fclose(outfp);
599 gchar *out = canonicalize_str(mimeinfo->data.mem);
600 procmime_fclose(infp);
601 infp = str_open_as_stream(out);
605 procmime_fclose(outfp);
608 #ifdef HAVE_FGETS_UNLOCKED
614 while ((len = SC_FREAD(inbuf, sizeof(gchar),
615 B64_LINE_SIZE, tmp_fp))
617 base64_encode(outbuf, inbuf, B64_LINE_SIZE);
618 if (SC_FPUTS(outbuf, outfp) == EOF)
620 if (SC_FPUTC('\n', outfp) == EOF)
623 if (len > 0 && SC_FEOF(tmp_fp)) {
624 base64_encode(outbuf, inbuf, len);
625 if (SC_FPUTS(outbuf, outfp) == EOF)
627 if (SC_FPUTC('\n', outfp) == EOF)
632 procmime_fclose(tmp_fp);
633 claws_unlink(tmp_file);
636 } else if (encoding == ENC_QUOTED_PRINTABLE) {
637 gchar inbuf[BUFFSIZE], outbuf[BUFFSIZE * 4];
639 while (SC_FGETS(inbuf, sizeof(inbuf), infp) != NULL) {
640 qp_encode_line(outbuf, inbuf);
642 if (!strncmp("From ", outbuf, sizeof("From ")-1)) {
643 gchar *tmpbuf = outbuf;
645 tmpbuf += sizeof("From ")-1;
647 if (SC_FPUTS("=46rom ", outfp) == EOF)
649 if (SC_FPUTS(tmpbuf, outfp) == EOF)
652 if (SC_FPUTS(outbuf, outfp) == EOF)
659 while (SC_FGETS(buf, sizeof(buf), infp) != NULL) {
661 if (SC_FPUTS(buf, outfp) == EOF)
666 procmime_fclose(outfp);
667 procmime_fclose(infp);
672 if (mimeinfo->content == MIMECONTENT_FILE) {
673 if (mimeinfo->tmp && (mimeinfo->data.filename != NULL))
674 claws_unlink(mimeinfo->data.filename);
675 g_free(mimeinfo->data.filename);
676 } else if (mimeinfo->content == MIMECONTENT_MEM) {
677 if (mimeinfo->tmp && (mimeinfo->data.mem != NULL))
678 g_free(mimeinfo->data.mem);
681 if (g_stat(tmpfilename, &statbuf) < 0) {
682 FILE_OP_ERROR(tmpfilename, "stat");
685 mimeinfo->content = MIMECONTENT_FILE;
686 mimeinfo->data.filename = tmpfilename;
687 mimeinfo->tmp = TRUE;
688 mimeinfo->offset = 0;
689 mimeinfo->length = statbuf.st_size;
690 mimeinfo->encoding_type = encoding;
695 gint procmime_get_part(const gchar *outfile, MimeInfo *mimeinfo)
699 gint restlength, readlength;
700 gint saved_errno = 0;
702 cm_return_val_if_fail(outfile != NULL, -1);
703 cm_return_val_if_fail(mimeinfo != NULL, -1);
705 if (mimeinfo->encoding_type != ENC_BINARY && !procmime_decode_content(mimeinfo))
708 if ((infp = procmime_fopen(mimeinfo->data.filename, "rb")) == NULL) {
710 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
711 return -(saved_errno);
713 if (fseek(infp, mimeinfo->offset, SEEK_SET) < 0) {
715 FILE_OP_ERROR(mimeinfo->data.filename, "fseek");
716 procmime_fclose(infp);
717 return -(saved_errno);
719 if ((outfp = procmime_fopen(outfile, "wb")) == NULL) {
721 FILE_OP_ERROR(outfile, "fopen");
722 procmime_fclose(infp);
723 return -(saved_errno);
726 restlength = mimeinfo->length;
728 while ((restlength > 0) && ((readlength = SC_FREAD(buf, 1, restlength > BUFFSIZE ? BUFFSIZE : restlength, infp)) > 0)) {
729 if (SC_FWRITE(buf, 1, readlength, outfp) != readlength) {
731 procmime_fclose(infp);
732 procmime_fclose(outfp);
733 return -(saved_errno);
735 restlength -= readlength;
738 procmime_fclose(infp);
739 if (procmime_fclose(outfp) == EOF) {
741 FILE_OP_ERROR(outfile, "fclose");
742 claws_unlink(outfile);
743 return -(saved_errno);
749 gboolean procmime_scan_text_content(MimeInfo *mimeinfo,
750 gboolean (*scan_callback)(const gchar *str, gpointer cb_data),
754 const gchar *src_codeset;
755 gboolean conv_fail = FALSE;
759 gboolean scan_ret = FALSE;
761 cm_return_val_if_fail(mimeinfo != NULL, TRUE);
762 cm_return_val_if_fail(scan_callback != NULL, TRUE);
764 if (!procmime_decode_content(mimeinfo))
767 tmpfile = procmime_get_tmp_file_name(mimeinfo);
771 if (procmime_get_part(tmpfile, mimeinfo) < 0) {
776 tmpfp = procmime_fopen(tmpfile, "rb");
782 src_codeset = forced_charset
784 procmime_mimeinfo_get_parameter(mimeinfo, "charset");
786 /* use supersets transparently when possible */
787 if (!forced_charset && src_codeset && !strcasecmp(src_codeset, CS_ISO_8859_1))
788 src_codeset = CS_WINDOWS_1252;
789 else if (!forced_charset && src_codeset && !strcasecmp(src_codeset, CS_X_GBK))
790 src_codeset = CS_GB18030;
791 else if (!forced_charset && src_codeset && !strcasecmp(src_codeset, CS_GBK))
792 src_codeset = CS_GB18030;
793 else if (!forced_charset && src_codeset && !strcasecmp(src_codeset, CS_GB2312))
794 src_codeset = CS_GB18030;
795 else if (!forced_charset && src_codeset && !strcasecmp(src_codeset, CS_X_VIET_VPS))
796 src_codeset = CS_WINDOWS_874;
798 if (mimeinfo->type == MIMETYPE_TEXT && !g_ascii_strcasecmp(mimeinfo->subtype, "html")) {
799 SC_HTMLParser *parser;
802 conv = conv_code_converter_new(src_codeset);
803 parser = sc_html_parser_new(tmpfp, conv);
804 while ((str = sc_html_parse(parser)) != NULL) {
805 if ((scan_ret = scan_callback(str, cb_data)) == TRUE)
808 sc_html_parser_destroy(parser);
809 conv_code_converter_destroy(conv);
810 } else if (mimeinfo->type == MIMETYPE_TEXT && !g_ascii_strcasecmp(mimeinfo->subtype, "enriched")) {
814 conv = conv_code_converter_new(src_codeset);
815 parser = ertf_parser_new(tmpfp, conv);
816 while ((str = ertf_parse(parser)) != NULL) {
817 if ((scan_ret = scan_callback(str, cb_data)) == TRUE)
820 ertf_parser_destroy(parser);
821 conv_code_converter_destroy(conv);
822 } else if (mimeinfo->type == MIMETYPE_TEXT) {
823 while (SC_FGETS(buf, sizeof(buf), tmpfp) != NULL) {
824 str = conv_codeset_strdup(buf, src_codeset, CS_UTF_8);
826 if ((scan_ret = scan_callback(str, cb_data)) == TRUE) {
833 if ((scan_ret = scan_callback(buf, cb_data)) == TRUE)
840 g_warning("procmime_get_text_content(): Code conversion failed.\n");
842 procmime_fclose(tmpfp);
843 claws_unlink(tmpfile);
849 static gboolean scan_fputs_cb(const gchar *str, gpointer fp)
851 if (SC_FPUTS(str, (FILE *)fp) == EOF)
857 FILE *procmime_get_text_content(MimeInfo *mimeinfo)
862 if ((outfp = my_tmpfile()) == NULL) {
866 #ifdef HAVE_FGETS_UNLOCKED
870 err = procmime_scan_text_content(mimeinfo, scan_fputs_cb, outfp);
874 procmime_fclose(outfp);
877 #ifdef HAVE_FGETS_UNLOCKED
884 FILE *procmime_get_binary_content(MimeInfo *mimeinfo)
889 cm_return_val_if_fail(mimeinfo != NULL, NULL);
891 if (!procmime_decode_content(mimeinfo))
894 tmpfile = procmime_get_tmp_file_name(mimeinfo);
898 if (procmime_get_part(tmpfile, mimeinfo) < 0) {
903 outfp = procmime_fopen(tmpfile, "rb");
913 #ifdef HAVE_FGETS_UNLOCKED
919 /* search the first text part of (multipart) MIME message,
920 decode, convert it and output to outfp. */
921 FILE *procmime_get_first_text_content(MsgInfo *msginfo)
924 MimeInfo *mimeinfo, *partinfo;
925 gboolean empty_ok = FALSE, short_scan = TRUE;
927 cm_return_val_if_fail(msginfo != NULL, NULL);
929 /* first we try to short-scan (for speed), refusing empty parts */
932 mimeinfo = procmime_scan_message_short(msginfo);
934 mimeinfo = procmime_scan_message(msginfo);
935 if (!mimeinfo) return NULL;
938 while (partinfo && (partinfo->type != MIMETYPE_TEXT ||
939 (partinfo->length == 0 && !empty_ok))) {
940 partinfo = procmime_mimeinfo_next(partinfo);
943 outfp = procmime_get_text_content(partinfo);
944 else if (!empty_ok && short_scan) {
945 /* if short scan didn't find a non-empty part, rescan
946 * fully for non-empty parts
949 procmime_mimeinfo_free_all(mimeinfo);
951 } else if (!empty_ok && !short_scan) {
952 /* if full scan didn't find a non-empty part, rescan
953 * accepting empty parts
956 procmime_mimeinfo_free_all(mimeinfo);
959 procmime_mimeinfo_free_all(mimeinfo);
961 /* outfp already unlocked at this time */
966 static gboolean find_encrypted_func(GNode *node, gpointer data)
968 MimeInfo *mimeinfo = (MimeInfo *) node->data;
969 MimeInfo **encinfo = (MimeInfo **) data;
971 if (privacy_mimeinfo_is_encrypted(mimeinfo)) {
979 static MimeInfo *find_encrypted_part(MimeInfo *rootinfo)
981 MimeInfo *encinfo = NULL;
983 g_node_traverse(rootinfo->node, G_IN_ORDER, G_TRAVERSE_ALL, -1,
984 find_encrypted_func, &encinfo);
989 /* search the first encrypted text part of (multipart) MIME message,
990 decode, convert it and output to outfp. */
991 FILE *procmime_get_first_encrypted_text_content(MsgInfo *msginfo)
994 MimeInfo *mimeinfo, *partinfo, *encinfo;
996 cm_return_val_if_fail(msginfo != NULL, NULL);
998 mimeinfo = procmime_scan_message(msginfo);
1003 partinfo = mimeinfo;
1004 if ((encinfo = find_encrypted_part(partinfo)) != NULL) {
1005 debug_print("decrypting message part\n");
1006 if (privacy_mimeinfo_decrypt(encinfo) < 0) {
1007 alertpanel_error(_("Couldn't decrypt: %s"),
1008 privacy_get_error());
1012 partinfo = mimeinfo;
1013 while (partinfo && partinfo->type != MIMETYPE_TEXT) {
1014 partinfo = procmime_mimeinfo_next(partinfo);
1015 if (privacy_mimeinfo_is_signed(partinfo))
1016 procmsg_msginfo_set_flags(msginfo, 0, MSG_SIGNED);
1020 outfp = procmime_get_text_content(partinfo);
1022 procmime_mimeinfo_free_all(mimeinfo);
1024 /* outfp already unlocked at this time */
1028 gboolean procmime_msginfo_is_encrypted(MsgInfo *msginfo)
1030 MimeInfo *mimeinfo, *partinfo;
1031 gboolean result = FALSE;
1033 cm_return_val_if_fail(msginfo != NULL, FALSE);
1035 mimeinfo = procmime_scan_message(msginfo);
1040 partinfo = mimeinfo;
1041 result = (find_encrypted_part(partinfo) != NULL);
1042 procmime_mimeinfo_free_all(mimeinfo);
1047 gchar *procmime_get_tmp_file_name(MimeInfo *mimeinfo)
1049 static guint32 id = 0;
1054 cm_return_val_if_fail(mimeinfo != NULL, NULL);
1056 g_snprintf(f_prefix, sizeof(f_prefix), "%08x.", id++);
1058 if ((mimeinfo->type == MIMETYPE_TEXT) && !g_ascii_strcasecmp(mimeinfo->subtype, "html"))
1059 base = g_strdup("mimetmp.html");
1061 const gchar *basetmp;
1063 basetmp = procmime_mimeinfo_get_parameter(mimeinfo, "filename");
1064 if (basetmp == NULL)
1065 basetmp = procmime_mimeinfo_get_parameter(mimeinfo, "name");
1066 if (basetmp == NULL)
1067 basetmp = "mimetmp";
1068 basetmp = g_path_get_basename(basetmp);
1069 if (*basetmp == '\0')
1070 basetmp = g_strdup("mimetmp");
1071 base = conv_filename_from_utf8(basetmp);
1072 g_free((gchar*)basetmp);
1073 subst_for_shellsafe_filename(base);
1076 filename = g_strconcat(get_mime_tmp_dir(), G_DIR_SEPARATOR_S,
1077 f_prefix, base, NULL);
1084 static GList *mime_type_list = NULL;
1086 gchar *procmime_get_mime_type(const gchar *filename)
1092 static GHashTable *mime_type_table = NULL;
1093 MimeType *mime_type;
1095 if (!mime_type_table) {
1096 mime_type_table = procmime_get_mime_type_table();
1097 if (!mime_type_table) return NULL;
1101 if (filename == NULL)
1104 base = g_path_get_basename(filename);
1105 if ((p = strrchr(base, '.')) != NULL)
1106 ext = g_utf8_strdown(p + 1, -1);
1110 mime_type = g_hash_table_lookup(mime_type_table, ext);
1114 str = g_strconcat(mime_type->type, "/", mime_type->sub_type,
1116 debug_print("got type %s for %s\n", str, ext);
1122 gchar *str = get_content_type_from_registry_with_ext(ext);
1129 static guint procmime_str_hash(gconstpointer gptr)
1131 guint hash_result = 0;
1134 for (str = gptr; str && *str; str++) {
1135 if (isupper(*str)) hash_result += (*str + ' ');
1136 else hash_result += *str;
1142 static gint procmime_str_equal(gconstpointer gptr1, gconstpointer gptr2)
1144 const char *str1 = gptr1;
1145 const char *str2 = gptr2;
1147 return !g_utf8_collate(str1, str2);
1150 static GHashTable *procmime_get_mime_type_table(void)
1152 GHashTable *table = NULL;
1154 MimeType *mime_type;
1157 if (!mime_type_list) {
1158 mime_type_list = procmime_get_mime_type_list();
1159 if (!mime_type_list) return NULL;
1162 table = g_hash_table_new(procmime_str_hash, procmime_str_equal);
1164 for (cur = mime_type_list; cur != NULL; cur = cur->next) {
1168 mime_type = (MimeType *)cur->data;
1170 if (!mime_type->extension) continue;
1172 exts = g_strsplit(mime_type->extension, " ", 16);
1173 for (i = 0; exts[i] != NULL; i++) {
1174 /* Don't overwrite previously inserted extension */
1175 if (!g_hash_table_lookup(table, exts[i])) {
1176 key = g_strdup(exts[i]);
1177 g_hash_table_insert(table, key, mime_type);
1186 GList *procmime_get_mime_type_list(void)
1190 gchar buf[BUFFSIZE];
1193 MimeType *mime_type;
1194 gboolean fp_is_glob_file = TRUE;
1197 return mime_type_list;
1199 #if defined(__NetBSD__) || defined(__OpenBSD__) || defined(__FreeBSD__)
1200 if ((fp = procmime_fopen(DATAROOTDIR "/mime/globs", "rb")) == NULL)
1202 if ((fp = procmime_fopen("/usr/share/mime/globs", "rb")) == NULL)
1205 fp_is_glob_file = FALSE;
1206 if ((fp = procmime_fopen("/etc/mime.types", "rb")) == NULL) {
1207 if ((fp = procmime_fopen(SYSCONFDIR "/mime.types", "rb"))
1209 FILE_OP_ERROR(SYSCONFDIR "/mime.types",
1216 while (SC_FGETS(buf, sizeof(buf), fp) != NULL) {
1217 p = strchr(buf, '#');
1223 if (fp_is_glob_file) {
1224 while (*p && !g_ascii_isspace(*p) && (*p!=':')) p++;
1226 while (*p && !g_ascii_isspace(*p)) p++;
1233 delim = strchr(buf, '/');
1234 if (delim == NULL) continue;
1237 mime_type = g_new(MimeType, 1);
1238 mime_type->type = g_strdup(buf);
1239 mime_type->sub_type = g_strdup(delim + 1);
1241 if (fp_is_glob_file) {
1242 while (*p && (g_ascii_isspace(*p)||(*p=='*')||(*p=='.'))) p++;
1244 while (*p && g_ascii_isspace(*p)) p++;
1248 mime_type->extension = g_utf8_strdown(p, -1);
1250 mime_type->extension = NULL;
1252 list = g_list_append(list, mime_type);
1255 procmime_fclose(fp);
1258 g_warning("Can't read mime.types\n");
1263 EncodingType procmime_get_encoding_for_charset(const gchar *charset)
1267 else if (!g_ascii_strncasecmp(charset, "ISO-2022-", 9) ||
1268 !g_ascii_strcasecmp(charset, "US-ASCII"))
1270 else if (!g_ascii_strcasecmp(charset, "ISO-8859-5") ||
1271 !g_ascii_strncasecmp(charset, "KOI8-", 5) ||
1272 !g_ascii_strcasecmp(charset, "Windows-1251"))
1274 else if (!g_ascii_strncasecmp(charset, "ISO-8859-", 9))
1275 return ENC_QUOTED_PRINTABLE;
1276 else if (!g_ascii_strncasecmp(charset, "UTF-8", 5))
1277 return ENC_QUOTED_PRINTABLE;
1282 EncodingType procmime_get_encoding_for_text_file(const gchar *file, gboolean *has_binary)
1285 guchar buf[BUFFSIZE];
1287 size_t octet_chars = 0;
1288 size_t total_len = 0;
1289 gfloat octet_percentage;
1290 gboolean force_b64 = FALSE;
1292 if ((fp = procmime_fopen(file, "rb")) == NULL) {
1293 FILE_OP_ERROR(file, "fopen");
1297 while ((len = SC_FREAD(buf, sizeof(guchar), sizeof(buf), fp)) > 0) {
1301 for (p = buf, i = 0; i < len; ++p, ++i) {
1312 procmime_fclose(fp);
1315 octet_percentage = (gfloat)octet_chars / (gfloat)total_len;
1317 octet_percentage = 0.0;
1319 debug_print("procmime_get_encoding_for_text_file(): "
1320 "8bit chars: %zd / %zd (%f%%)\n", octet_chars, total_len,
1321 100.0 * octet_percentage);
1323 if (octet_percentage > 0.20 || force_b64) {
1324 debug_print("using BASE64\n");
1326 } else if (octet_chars > 0) {
1327 debug_print("using quoted-printable\n");
1328 return ENC_QUOTED_PRINTABLE;
1330 debug_print("using 7bit\n");
1335 struct EncodingTable
1338 EncodingType enc_type;
1341 struct EncodingTable encoding_table[] = {
1344 {"binary", ENC_BINARY},
1345 {"quoted-printable", ENC_QUOTED_PRINTABLE},
1346 {"base64", ENC_BASE64},
1347 {"x-uuencode", ENC_UNKNOWN},
1348 {NULL, ENC_UNKNOWN},
1351 const gchar *procmime_get_encoding_str(EncodingType encoding)
1353 struct EncodingTable *enc_table;
1355 for (enc_table = encoding_table; enc_table->str != NULL; enc_table++) {
1356 if (enc_table->enc_type == encoding)
1357 return enc_table->str;
1362 /* --- NEW MIME STUFF --- */
1369 static struct TypeTable mime_type_table[] = {
1370 {"text", MIMETYPE_TEXT},
1371 {"image", MIMETYPE_IMAGE},
1372 {"audio", MIMETYPE_AUDIO},
1373 {"video", MIMETYPE_VIDEO},
1374 {"application", MIMETYPE_APPLICATION},
1375 {"message", MIMETYPE_MESSAGE},
1376 {"multipart", MIMETYPE_MULTIPART},
1380 const gchar *procmime_get_media_type_str(MimeMediaType type)
1382 struct TypeTable *type_table;
1384 for (type_table = mime_type_table; type_table->str != NULL; type_table++) {
1385 if (type_table->type == type)
1386 return type_table->str;
1391 MimeMediaType procmime_get_media_type(const gchar *str)
1393 struct TypeTable *typetablearray;
1395 for (typetablearray = mime_type_table; typetablearray->str != NULL; typetablearray++)
1396 if (g_ascii_strncasecmp(str, typetablearray->str, strlen(typetablearray->str)) == 0)
1397 return typetablearray->type;
1399 return MIMETYPE_UNKNOWN;
1403 *\brief Safe wrapper for content type string.
1405 *\return const gchar * Pointer to content type string.
1407 gchar *procmime_get_content_type_str(MimeMediaType type,
1408 const char *subtype)
1410 const gchar *type_str = NULL;
1412 if (subtype == NULL || !(type_str = procmime_get_media_type_str(type)))
1413 return g_strdup("unknown");
1414 return g_strdup_printf("%s/%s", type_str, subtype);
1417 static int procmime_parse_mimepart(MimeInfo *parent,
1418 gchar *content_type,
1419 gchar *content_encoding,
1420 gchar *content_description,
1422 gchar *content_disposition,
1423 gchar *content_location,
1424 const gchar *original_msgid,
1425 const gchar *disposition_notification_hdr,
1426 const gchar *filename,
1429 gboolean short_scan);
1431 static void procmime_parse_message_rfc822(MimeInfo *mimeinfo, gboolean short_scan)
1433 HeaderEntry hentry[] = {{"Content-Type:", NULL, TRUE},
1434 {"Content-Transfer-Encoding:",
1436 {"Content-Description:",
1440 {"Content-Disposition:",
1442 {"Content-Location:",
1446 {"Original-Message-ID:",
1450 {NULL, NULL, FALSE}};
1451 guint content_start, i;
1456 procmime_decode_content(mimeinfo);
1458 fp = procmime_fopen(mimeinfo->data.filename, "rb");
1460 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
1463 if (fseek(fp, mimeinfo->offset, SEEK_SET) < 0) {
1464 FILE_OP_ERROR(mimeinfo->data.filename, "fseek");
1465 procmime_fclose(fp);
1468 procheader_get_header_fields(fp, hentry);
1469 if (hentry[0].body != NULL) {
1470 tmp = conv_unmime_header(hentry[0].body, NULL, FALSE);
1471 g_free(hentry[0].body);
1472 hentry[0].body = tmp;
1474 if (hentry[2].body != NULL) {
1475 tmp = conv_unmime_header(hentry[2].body, NULL, FALSE);
1476 g_free(hentry[2].body);
1477 hentry[2].body = tmp;
1479 if (hentry[4].body != NULL) {
1480 tmp = conv_unmime_header(hentry[4].body, NULL, FALSE);
1481 g_free(hentry[4].body);
1482 hentry[4].body = tmp;
1484 if (hentry[5].body != NULL) {
1485 tmp = conv_unmime_header(hentry[5].body, NULL, FALSE);
1486 g_free(hentry[5].body);
1487 hentry[5].body = tmp;
1489 if (hentry[7].body != NULL) {
1490 tmp = conv_unmime_header(hentry[7].body, NULL, FALSE);
1491 g_free(hentry[7].body);
1492 hentry[7].body = tmp;
1494 if (hentry[8].body != NULL) {
1495 tmp = conv_unmime_header(hentry[8].body, NULL, FALSE);
1496 g_free(hentry[8].body);
1497 hentry[8].body = tmp;
1500 content_start = ftell(fp);
1501 procmime_fclose(fp);
1503 len = mimeinfo->length - (content_start - mimeinfo->offset);
1506 procmime_parse_mimepart(mimeinfo,
1507 hentry[0].body, hentry[1].body,
1508 hentry[2].body, hentry[3].body,
1509 hentry[4].body, hentry[5].body,
1510 hentry[7].body, hentry[8].body,
1511 mimeinfo->data.filename, content_start,
1514 for (i = 0; i < (sizeof hentry / sizeof hentry[0]); i++) {
1515 g_free(hentry[i].body);
1516 hentry[i].body = NULL;
1520 static void procmime_parse_disposition_notification(MimeInfo *mimeinfo,
1521 const gchar *original_msgid, const gchar *disposition_notification_hdr,
1522 gboolean short_scan)
1524 HeaderEntry hentry[] = {{"Original-Message-ID:", NULL, TRUE},
1525 {"Disposition:", NULL, TRUE},
1526 {NULL, NULL, FALSE}};
1529 gchar *orig_msg_id = NULL;
1532 procmime_decode_content(mimeinfo);
1534 debug_print("parse disposition notification\n");
1535 fp = procmime_fopen(mimeinfo->data.filename, "rb");
1537 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
1540 if (fseek(fp, mimeinfo->offset, SEEK_SET) < 0) {
1541 FILE_OP_ERROR(mimeinfo->data.filename, "fseek");
1542 procmime_fclose(fp);
1546 if (original_msgid && disposition_notification_hdr) {
1547 hentry[0].body = g_strdup(original_msgid);
1548 hentry[1].body = g_strdup(disposition_notification_hdr);
1550 procheader_get_header_fields(fp, hentry);
1553 procmime_fclose(fp);
1555 if (!hentry[0].body || !hentry[1].body) {
1556 debug_print("MsgId %s, Disp %s\n",
1557 hentry[0].body ? hentry[0].body:"(nil)",
1558 hentry[1].body ? hentry[1].body:"(nil)");
1562 orig_msg_id = g_strdup(hentry[0].body);
1563 disp = g_strdup(hentry[1].body);
1565 extract_parenthesis(orig_msg_id, '<', '>');
1566 remove_space(orig_msg_id);
1568 if (strstr(disp, "displayed")) {
1569 /* find sent message, if possible */
1570 MsgInfo *info = NULL;
1572 debug_print("%s has been displayed.\n", orig_msg_id);
1573 for (flist = folder_get_list(); flist != NULL; flist = g_list_next(flist)) {
1574 FolderItem *outbox = ((Folder *)(flist->data))->outbox;
1576 debug_print("skipping folder with no outbox...\n");
1579 info = folder_item_get_msginfo_by_msgid(outbox, orig_msg_id);
1580 debug_print("%s %s in %s\n", info?"found":"didn't find", orig_msg_id, outbox->path);
1582 procmsg_msginfo_set_flags(info, MSG_RETRCPT_GOT, 0);
1583 procmsg_msginfo_free(info);
1587 g_free(orig_msg_id);
1590 for (i = 0; i < (sizeof hentry / sizeof hentry[0]); i++) {
1591 g_free(hentry[i].body);
1592 hentry[i].body = NULL;
1596 #define GET_HEADERS() { \
1597 procheader_get_header_fields(fp, hentry); \
1598 if (hentry[0].body != NULL) { \
1599 tmp = conv_unmime_header(hentry[0].body, NULL, FALSE); \
1600 g_free(hentry[0].body); \
1601 hentry[0].body = tmp; \
1603 if (hentry[2].body != NULL) { \
1604 tmp = conv_unmime_header(hentry[2].body, NULL, FALSE); \
1605 g_free(hentry[2].body); \
1606 hentry[2].body = tmp; \
1608 if (hentry[4].body != NULL) { \
1609 tmp = conv_unmime_header(hentry[4].body, NULL, FALSE); \
1610 g_free(hentry[4].body); \
1611 hentry[4].body = tmp; \
1613 if (hentry[5].body != NULL) { \
1614 tmp = conv_unmime_header(hentry[5].body, NULL, FALSE); \
1615 g_free(hentry[5].body); \
1616 hentry[5].body = tmp; \
1618 if (hentry[6].body != NULL) { \
1619 tmp = conv_unmime_header(hentry[6].body, NULL, FALSE); \
1620 g_free(hentry[6].body); \
1621 hentry[6].body = tmp; \
1623 if (hentry[7].body != NULL) { \
1624 tmp = conv_unmime_header(hentry[7].body, NULL, FALSE); \
1625 g_free(hentry[7].body); \
1626 hentry[7].body = tmp; \
1630 static void procmime_parse_multipart(MimeInfo *mimeinfo, gboolean short_scan)
1632 HeaderEntry hentry[] = {{"Content-Type:", NULL, TRUE},
1633 {"Content-Transfer-Encoding:",
1635 {"Content-Description:",
1639 {"Content-Disposition:",
1641 {"Content-Location:",
1643 {"Original-Message-ID:",
1647 {NULL, NULL, FALSE}};
1650 gint boundary_len = 0, lastoffset = -1, i;
1651 gchar buf[BUFFSIZE];
1654 gboolean start_found = FALSE;
1655 gboolean end_found = FALSE;
1657 boundary = g_hash_table_lookup(mimeinfo->typeparameters, "boundary");
1660 boundary_len = strlen(boundary);
1662 procmime_decode_content(mimeinfo);
1664 fp = procmime_fopen(mimeinfo->data.filename, "rb");
1666 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
1670 if (fseek(fp, mimeinfo->offset, SEEK_SET) < 0) {
1671 FILE_OP_ERROR(mimeinfo->data.filename, "fseek");
1672 procmime_fclose(fp);
1676 while (SC_FGETS(buf, sizeof(buf), fp) != NULL && result == 0) {
1677 if (ftell(fp) - 1 > (mimeinfo->offset + mimeinfo->length))
1680 if (IS_BOUNDARY(buf, boundary, boundary_len)) {
1683 if (lastoffset != -1) {
1684 gint len = (ftell(fp) - strlen(buf)) - lastoffset - 1;
1687 result = procmime_parse_mimepart(mimeinfo,
1688 hentry[0].body, hentry[1].body,
1689 hentry[2].body, hentry[3].body,
1690 hentry[4].body, hentry[5].body,
1691 hentry[6].body, hentry[7].body,
1692 mimeinfo->data.filename, lastoffset,
1694 if (result == 1 && short_scan)
1699 if (buf[2 + boundary_len] == '-' &&
1700 buf[2 + boundary_len + 1] == '-') {
1704 for (i = 0; i < (sizeof hentry / sizeof hentry[0]) ; i++) {
1705 g_free(hentry[i].body);
1706 hentry[i].body = NULL;
1709 lastoffset = ftell(fp);
1713 if (start_found && !end_found && lastoffset != -1) {
1714 gint len = (ftell(fp) - strlen(buf)) - lastoffset - 1;
1717 result = procmime_parse_mimepart(mimeinfo,
1718 hentry[0].body, hentry[1].body,
1719 hentry[2].body, hentry[3].body,
1720 hentry[4].body, hentry[5].body,
1721 hentry[6].body, hentry[7].body,
1722 mimeinfo->data.filename, lastoffset,
1725 mimeinfo->broken = TRUE;
1728 for (i = 0; i < (sizeof hentry / sizeof hentry[0]); i++) {
1729 g_free(hentry[i].body);
1730 hentry[i].body = NULL;
1732 procmime_fclose(fp);
1735 static void parse_parameters(const gchar *parameters, GHashTable *table)
1737 gchar *params, *param, *next;
1738 GSList *convlist = NULL, *concatlist = NULL, *cur;
1740 params = g_strdup(parameters);
1743 for (; next != NULL; param = next) {
1744 gchar *attribute, *value, *tmp, *down_attr, *orig_down_attr;
1746 gboolean convert = FALSE;
1748 next = strchr_with_skip_quote(param, '"', ';');
1757 value = strchr(attribute, '=');
1763 while (value[0] != '\0' && value[0] == ' ')
1766 down_attr = g_utf8_strdown(attribute, -1);
1767 orig_down_attr = down_attr;
1769 len = down_attr ? strlen(down_attr):0;
1770 if (len > 0 && down_attr[len - 1] == '*') {
1771 gchar *srcpos, *dstpos, *endpos;
1774 down_attr[len - 1] = '\0';
1778 endpos = value + strlen(value);
1779 while (srcpos < endpos) {
1785 if (!get_hex_value(&dstvalue, srcpos[1], srcpos[2]))
1795 if (value[0] == '"')
1796 extract_quote(value, '"');
1798 if (value[0] == '"')
1799 extract_quote(value, '"');
1800 else if ((tmp = strchr(value, ' ')) != NULL)
1805 while (down_attr[0] == ' ')
1807 while (down_attr[strlen(down_attr)-1] == ' ')
1808 down_attr[strlen(down_attr)-1] = '\0';
1811 while (value[0] != '\0' && value[0] == ' ')
1813 while (value[strlen(value)-1] == ' ')
1814 value[strlen(value)-1] = '\0';
1816 if (down_attr && strrchr(down_attr, '*') != NULL) {
1819 tmpattr = g_strdup(down_attr);
1820 tmp = strrchr(tmpattr, '*');
1823 if ((tmp[1] == '0') && (tmp[2] == '\0') &&
1824 (g_slist_find_custom(concatlist, down_attr, (GCompareFunc)g_strcmp0) == NULL))
1825 concatlist = g_slist_prepend(concatlist, g_strdup(tmpattr));
1827 if (convert && (g_slist_find_custom(convlist, tmpattr, (GCompareFunc)g_strcmp0) == NULL))
1828 convlist = g_slist_prepend(convlist, g_strdup(tmpattr));
1831 } else if (convert) {
1832 if (g_slist_find_custom(convlist, down_attr, (GCompareFunc)g_strcmp0) == NULL)
1833 convlist = g_slist_prepend(convlist, g_strdup(down_attr));
1836 if (g_hash_table_lookup(table, down_attr) == NULL)
1837 g_hash_table_insert(table, g_strdup(down_attr), g_strdup(value));
1838 g_free(orig_down_attr);
1841 for (cur = concatlist; cur != NULL; cur = g_slist_next(cur)) {
1842 gchar *attribute, *attrwnum, *partvalue;
1846 attribute = (gchar *) cur->data;
1847 value = g_string_sized_new(64);
1849 attrwnum = g_strdup_printf("%s*%d", attribute, n);
1850 while ((partvalue = g_hash_table_lookup(table, attrwnum)) != NULL) {
1851 g_string_append(value, partvalue);
1853 g_hash_table_remove(table, attrwnum);
1856 attrwnum = g_strdup_printf("%s*%d", attribute, n);
1860 g_hash_table_insert(table, g_strdup(attribute), g_strdup(value->str));
1861 g_string_free(value, TRUE);
1863 slist_free_strings_full(concatlist);
1865 for (cur = convlist; cur != NULL; cur = g_slist_next(cur)) {
1866 gchar *attribute, *key, *value;
1867 gchar *charset, *lang, *oldvalue, *newvalue;
1869 attribute = (gchar *) cur->data;
1870 if (!g_hash_table_lookup_extended(
1871 table, attribute, (gpointer *)(gchar *) &key, (gpointer *)(gchar *) &value))
1875 if (charset == NULL)
1877 lang = strchr(charset, '\'');
1882 oldvalue = strchr(lang, '\'');
1883 if (oldvalue == NULL)
1888 newvalue = conv_codeset_strdup(oldvalue, charset, CS_UTF_8);
1890 g_hash_table_remove(table, attribute);
1894 g_hash_table_insert(table, g_strdup(attribute), newvalue);
1896 slist_free_strings_full(convlist);
1901 static void procmime_parse_content_type(const gchar *content_type, MimeInfo *mimeinfo)
1903 cm_return_if_fail(content_type != NULL);
1904 cm_return_if_fail(mimeinfo != NULL);
1906 /* RFC 2045, page 13 says that the mime subtype is MANDATORY;
1907 * if it's not available we use the default Content-Type */
1908 if ((content_type[0] == '\0') || (strchr(content_type, '/') == NULL)) {
1909 mimeinfo->type = MIMETYPE_TEXT;
1910 mimeinfo->subtype = g_strdup("plain");
1911 if (g_hash_table_lookup(mimeinfo->typeparameters,
1912 "charset") == NULL) {
1913 g_hash_table_insert(mimeinfo->typeparameters,
1914 g_strdup("charset"),
1916 conv_get_locale_charset_str_no_utf8()));
1919 gchar *type, *subtype, *params;
1921 type = g_strdup(content_type);
1922 subtype = strchr(type, '/') + 1;
1923 *(subtype - 1) = '\0';
1924 if ((params = strchr(subtype, ';')) != NULL) {
1929 mimeinfo->type = procmime_get_media_type(type);
1930 mimeinfo->subtype = g_strstrip(g_strdup(subtype));
1932 /* Get mimeinfo->typeparameters */
1934 parse_parameters(params, mimeinfo->typeparameters);
1940 static void procmime_parse_content_disposition(const gchar *content_disposition, MimeInfo *mimeinfo)
1942 gchar *tmp, *params;
1944 cm_return_if_fail(content_disposition != NULL);
1945 cm_return_if_fail(mimeinfo != NULL);
1947 tmp = g_strdup(content_disposition);
1948 if ((params = strchr(tmp, ';')) != NULL) {
1954 if (!g_ascii_strcasecmp(tmp, "inline"))
1955 mimeinfo->disposition = DISPOSITIONTYPE_INLINE;
1956 else if (!g_ascii_strcasecmp(tmp, "attachment"))
1957 mimeinfo->disposition = DISPOSITIONTYPE_ATTACHMENT;
1959 mimeinfo->disposition = DISPOSITIONTYPE_ATTACHMENT;
1962 parse_parameters(params, mimeinfo->dispositionparameters);
1968 static void procmime_parse_content_encoding(const gchar *content_encoding, MimeInfo *mimeinfo)
1970 struct EncodingTable *enc_table;
1972 for (enc_table = encoding_table; enc_table->str != NULL; enc_table++) {
1973 if (g_ascii_strcasecmp(enc_table->str, content_encoding) == 0) {
1974 mimeinfo->encoding_type = enc_table->enc_type;
1978 mimeinfo->encoding_type = ENC_UNKNOWN;
1982 static GSList *registered_parsers = NULL;
1984 static MimeParser *procmime_get_mimeparser_for_type(MimeMediaType type, const gchar *sub_type)
1987 for (cur = registered_parsers; cur; cur = cur->next) {
1988 MimeParser *parser = (MimeParser *)cur->data;
1989 if (parser->type == type && !strcmp2(parser->sub_type, sub_type))
1995 void procmime_mimeparser_register(MimeParser *parser)
1997 if (!procmime_get_mimeparser_for_type(parser->type, parser->sub_type))
1998 registered_parsers = g_slist_append(registered_parsers, parser);
2002 void procmime_mimeparser_unregister(MimeParser *parser)
2004 registered_parsers = g_slist_remove(registered_parsers, parser);
2007 static gboolean procmime_mimeparser_parse(MimeParser *parser, MimeInfo *mimeinfo)
2009 cm_return_val_if_fail(parser->parse != NULL, FALSE);
2010 return parser->parse(parser, mimeinfo);
2013 static int procmime_parse_mimepart(MimeInfo *parent,
2014 gchar *content_type,
2015 gchar *content_encoding,
2016 gchar *content_description,
2018 gchar *content_disposition,
2019 gchar *content_location,
2020 const gchar *original_msgid,
2021 const gchar *disposition_notification_hdr,
2022 const gchar *filename,
2025 gboolean short_scan)
2028 MimeParser *parser = NULL;
2029 gboolean parsed = FALSE;
2032 /* Create MimeInfo */
2033 mimeinfo = procmime_mimeinfo_new();
2034 mimeinfo->content = MIMECONTENT_FILE;
2036 if (parent != NULL) {
2037 if (g_node_depth(parent->node) > 32) {
2038 /* 32 is an arbitrary value
2039 * this avoids DOSsing ourselves
2040 * with enormous messages
2042 procmime_mimeinfo_free_all(mimeinfo);
2045 g_node_append(parent->node, mimeinfo->node);
2047 mimeinfo->data.filename = g_strdup(filename);
2048 mimeinfo->offset = offset;
2049 mimeinfo->length = length;
2051 if (content_type != NULL) {
2052 g_strchomp(content_type);
2053 procmime_parse_content_type(content_type, mimeinfo);
2055 mimeinfo->type = MIMETYPE_TEXT;
2056 mimeinfo->subtype = g_strdup("plain");
2057 if (g_hash_table_lookup(mimeinfo->typeparameters,
2058 "charset") == NULL) {
2059 g_hash_table_insert(mimeinfo->typeparameters,
2060 g_strdup("charset"),
2062 conv_get_locale_charset_str_no_utf8()));
2066 if (content_encoding != NULL) {
2067 g_strchomp(content_encoding);
2068 procmime_parse_content_encoding(content_encoding, mimeinfo);
2070 mimeinfo->encoding_type = ENC_UNKNOWN;
2073 if (content_description != NULL)
2074 mimeinfo->description = g_strdup(content_description);
2076 mimeinfo->description = NULL;
2078 if (content_id != NULL)
2079 mimeinfo->id = g_strdup(content_id);
2081 mimeinfo->id = NULL;
2083 if (content_location != NULL)
2084 mimeinfo->location = g_strdup(content_location);
2086 mimeinfo->location = NULL;
2088 if (content_disposition != NULL) {
2089 g_strchomp(content_disposition);
2090 procmime_parse_content_disposition(content_disposition, mimeinfo);
2092 mimeinfo->disposition = DISPOSITIONTYPE_UNKNOWN;
2094 /* Call parser for mime type */
2095 if ((parser = procmime_get_mimeparser_for_type(mimeinfo->type, mimeinfo->subtype)) != NULL) {
2096 parsed = procmime_mimeparser_parse(parser, mimeinfo);
2099 switch (mimeinfo->type) {
2101 if (g_ascii_strcasecmp(mimeinfo->subtype, "plain") == 0 && short_scan) {
2106 case MIMETYPE_MESSAGE:
2107 if (g_ascii_strcasecmp(mimeinfo->subtype, "rfc822") == 0) {
2108 procmime_parse_message_rfc822(mimeinfo, short_scan);
2110 if (g_ascii_strcasecmp(mimeinfo->subtype, "disposition-notification") == 0) {
2111 procmime_parse_disposition_notification(mimeinfo,
2112 original_msgid, disposition_notification_hdr, short_scan);
2116 case MIMETYPE_MULTIPART:
2117 procmime_parse_multipart(mimeinfo, short_scan);
2120 case MIMETYPE_APPLICATION:
2121 if (g_ascii_strcasecmp(mimeinfo->subtype, "octet-stream") == 0
2122 && original_msgid && *original_msgid
2123 && disposition_notification_hdr && *disposition_notification_hdr) {
2124 procmime_parse_disposition_notification(mimeinfo,
2125 original_msgid, disposition_notification_hdr, short_scan);
2136 static gchar *typenames[] = {
2147 static gboolean output_func(GNode *node, gpointer data)
2150 MimeInfo *mimeinfo = (MimeInfo *) node->data;
2152 depth = g_node_depth(node);
2153 for (i = 0; i < depth; i++)
2155 g_print("%s/%s (offset:%d length:%d encoding: %d)\n", typenames[mimeinfo->type], mimeinfo->subtype, mimeinfo->offset, mimeinfo->length, mimeinfo->encoding_type);
2160 static void output_mime_structure(MimeInfo *mimeinfo, int indent)
2162 g_node_traverse(mimeinfo->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1, output_func, NULL);
2165 static MimeInfo *procmime_scan_file_with_offset(const gchar *filename, int offset, gboolean short_scan)
2170 if (g_stat(filename, &buf) < 0) {
2171 FILE_OP_ERROR(filename, "stat");
2175 mimeinfo = procmime_mimeinfo_new();
2176 mimeinfo->content = MIMECONTENT_FILE;
2177 mimeinfo->encoding_type = ENC_UNKNOWN;
2178 mimeinfo->type = MIMETYPE_MESSAGE;
2179 mimeinfo->subtype = g_strdup("rfc822");
2180 mimeinfo->data.filename = g_strdup(filename);
2181 mimeinfo->offset = offset;
2182 mimeinfo->length = buf.st_size - offset;
2184 procmime_parse_message_rfc822(mimeinfo, short_scan);
2185 if (debug_get_mode())
2186 output_mime_structure(mimeinfo, 0);
2191 static MimeInfo *procmime_scan_file_full(const gchar *filename, gboolean short_scan)
2195 cm_return_val_if_fail(filename != NULL, NULL);
2197 mimeinfo = procmime_scan_file_with_offset(filename, 0, short_scan);
2202 MimeInfo *procmime_scan_file(const gchar *filename)
2204 return procmime_scan_file_full(filename, FALSE);
2207 static MimeInfo *procmime_scan_file_short(const gchar *filename)
2209 return procmime_scan_file_full(filename, TRUE);
2212 static MimeInfo *procmime_scan_queue_file_full(const gchar *filename, gboolean short_scan)
2216 gchar buf[BUFFSIZE];
2219 cm_return_val_if_fail(filename != NULL, NULL);
2222 if ((fp = procmime_fopen(filename, "rb")) == NULL)
2224 /* Skip queue header */
2225 while (SC_FGETS(buf, sizeof(buf), fp) != NULL) {
2227 if ((!strncmp(buf, "X-Claws-End-Special-Headers: 1",
2228 strlen("X-Claws-End-Special-Headers:"))) ||
2229 (!strncmp(buf, "X-Sylpheed-End-Special-Headers: 1",
2230 strlen("X-Sylpheed-End-Special-Headers:"))))
2233 if (buf[0] == '\r' || buf[0] == '\n') break;
2234 /* from other mailers */
2235 if (!strncmp(buf, "Date: ", 6)
2236 || !strncmp(buf, "To: ", 4)
2237 || !strncmp(buf, "From: ", 6)
2238 || !strncmp(buf, "Subject: ", 9)) {
2244 procmime_fclose(fp);
2246 mimeinfo = procmime_scan_file_with_offset(filename, offset, short_scan);
2251 MimeInfo *procmime_scan_queue_file(const gchar *filename)
2253 return procmime_scan_queue_file_full(filename, FALSE);
2256 static MimeInfo *procmime_scan_queue_file_short(const gchar *filename)
2258 return procmime_scan_queue_file_full(filename, TRUE);
2263 ENC_AS_QUOTED_STRING,
2268 typedef struct _ParametersData {
2274 static void write_parameters(gpointer key, gpointer value, gpointer user_data)
2277 gchar *val = value, *valpos, *tmp;
2278 ParametersData *pdata = (ParametersData *)user_data;
2279 GString *buf = g_string_new("");
2282 EncodeAs encas = ENC_AS_TOKEN;
2284 for (valpos = val; *valpos != 0; valpos++) {
2285 if (!IS_ASCII(*valpos)) {
2286 encas = ENC_AS_ENCWORD;
2291 if (((*valpos >= 0) && (*valpos < 037)) || (*valpos == 0177)) {
2292 encas = ENC_AS_QUOTED_STRING;
2296 /* tspecials + SPACE */
2314 encas = ENC_AS_QUOTED_STRING;
2321 g_string_append_printf(buf, "%s=%s", param, val);
2324 case ENC_AS_QUOTED_STRING:
2325 g_string_append_printf(buf, "%s=\"%s\"", param, val);
2328 #if 0 /* we don't use that for now */
2329 case ENC_AS_EXTENDED:
2330 if (!g_utf8_validate(val, -1, NULL))
2331 g_string_append_printf(buf, "%s*=%s''", param,
2332 conv_get_locale_charset_str());
2334 g_string_append_printf(buf, "%s*=%s''", param,
2336 for (valpos = val; *valpos != '\0'; valpos++) {
2337 if (IS_ASCII(*valpos) && isalnum(*valpos)) {
2338 g_string_append_printf(buf, "%c", *valpos);
2340 gchar hexstr[3] = "XX";
2341 get_hex_str(hexstr, *valpos);
2342 g_string_append_printf(buf, "%%%s", hexstr);
2347 case ENC_AS_EXTENDED:
2348 debug_print("Unhandled ENC_AS_EXTENDED.");
2351 case ENC_AS_ENCWORD:
2352 len = MAX(strlen(val)*6, 512);
2353 tmp = g_malloc(len+1);
2354 codeconv_set_strict(TRUE);
2355 conv_encode_header_full(tmp, len, val, pdata->len + strlen(param) + 4 , FALSE,
2356 prefs_common.outgoing_charset);
2357 codeconv_set_strict(FALSE);
2358 if (!tmp || !*tmp) {
2359 codeconv_set_strict(TRUE);
2360 conv_encode_header_full(tmp, len, val, pdata->len + strlen(param) + 4 , FALSE,
2361 conv_get_outgoing_charset_str());
2362 codeconv_set_strict(FALSE);
2364 if (!tmp || !*tmp) {
2365 codeconv_set_strict(TRUE);
2366 conv_encode_header_full(tmp, len, val, pdata->len + strlen(param) + 4 , FALSE,
2368 codeconv_set_strict(FALSE);
2370 if (!tmp || !*tmp) {
2371 conv_encode_header_full(tmp, len, val, pdata->len + strlen(param) + 4 , FALSE,
2374 g_string_append_printf(buf, "%s=\"%s\"", param, tmp);
2380 if (buf->str && strlen(buf->str)) {
2381 tmp = strstr(buf->str, "\n");
2383 len = (tmp - buf->str);
2385 len = strlen(buf->str);
2386 if (pdata->len + len > 76) {
2387 if (fprintf(pdata->fp, ";\n %s", buf->str) < 0)
2388 pdata->error = TRUE;
2389 pdata->len = strlen(buf->str) + 1;
2391 if (fprintf(pdata->fp, "; %s", buf->str) < 0)
2392 pdata->error = TRUE;
2393 pdata->len += strlen(buf->str) + 2;
2396 g_string_free(buf, TRUE);
2399 #define TRY(func) { \
2405 int procmime_write_mime_header(MimeInfo *mimeinfo, FILE *fp)
2407 struct TypeTable *type_table;
2408 ParametersData *pdata = g_new0(ParametersData, 1);
2409 debug_print("procmime_write_mime_header\n");
2412 pdata->error = FALSE;
2413 for (type_table = mime_type_table; type_table->str != NULL; type_table++)
2414 if (mimeinfo->type == type_table->type) {
2415 gchar *buf = g_strdup_printf(
2416 "Content-Type: %s/%s", type_table->str, mimeinfo->subtype);
2417 if (fprintf(fp, "%s", buf) < 0) {
2422 pdata->len = strlen(buf);
2426 g_hash_table_foreach(mimeinfo->typeparameters, write_parameters, pdata);
2427 if (pdata->error == TRUE) {
2433 TRY(fprintf(fp, "\n") >= 0);
2435 if (mimeinfo->encoding_type != ENC_UNKNOWN)
2436 TRY(fprintf(fp, "Content-Transfer-Encoding: %s\n", procmime_get_encoding_str(mimeinfo->encoding_type)) >= 0);
2438 if (mimeinfo->description != NULL)
2439 TRY(fprintf(fp, "Content-Description: %s\n", mimeinfo->description) >= 0);
2441 if (mimeinfo->id != NULL)
2442 TRY(fprintf(fp, "Content-ID: %s\n", mimeinfo->id) >= 0);
2444 if (mimeinfo->location != NULL)
2445 TRY(fprintf(fp, "Content-Location: %s\n", mimeinfo->location) >= 0);
2447 if (mimeinfo->disposition != DISPOSITIONTYPE_UNKNOWN) {
2448 ParametersData *pdata = g_new0(ParametersData, 1);
2450 if (mimeinfo->disposition == DISPOSITIONTYPE_INLINE)
2451 buf = g_strdup("Content-Disposition: inline");
2452 else if (mimeinfo->disposition == DISPOSITIONTYPE_ATTACHMENT)
2453 buf = g_strdup("Content-Disposition: attachment");
2455 buf = g_strdup("Content-Disposition: unknown");
2457 if (fprintf(fp, "%s", buf) < 0) {
2462 pdata->len = strlen(buf);
2466 pdata->error = FALSE;
2467 g_hash_table_foreach(mimeinfo->dispositionparameters, write_parameters, pdata);
2468 if (pdata->error == TRUE) {
2473 TRY(fprintf(fp, "\n") >= 0);
2476 TRY(fprintf(fp, "\n") >= 0);
2481 static gint procmime_write_message_rfc822(MimeInfo *mimeinfo, FILE *fp)
2486 gchar buf[BUFFSIZE];
2487 gboolean skip = FALSE;;
2490 debug_print("procmime_write_message_rfc822\n");
2493 switch (mimeinfo->content) {
2494 case MIMECONTENT_FILE:
2495 if ((infp = procmime_fopen(mimeinfo->data.filename, "rb")) == NULL) {
2496 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
2499 if (fseek(infp, mimeinfo->offset, SEEK_SET) < 0) {
2500 FILE_OP_ERROR(mimeinfo->data.filename, "fseek");
2501 procmime_fclose(infp);
2504 while (SC_FGETS(buf, sizeof(buf), infp) == buf) {
2506 if (buf[0] == '\n' && buf[1] == '\0')
2508 if (skip && (buf[0] == ' ' || buf[0] == '\t'))
2510 if (g_ascii_strncasecmp(buf, "MIME-Version:", 13) == 0 ||
2511 g_ascii_strncasecmp(buf, "Content-Type:", 13) == 0 ||
2512 g_ascii_strncasecmp(buf, "Content-Transfer-Encoding:", 26) == 0 ||
2513 g_ascii_strncasecmp(buf, "Content-Description:", 20) == 0 ||
2514 g_ascii_strncasecmp(buf, "Content-ID:", 11) == 0 ||
2515 g_ascii_strncasecmp(buf, "Content-Location:", 17) == 0 ||
2516 g_ascii_strncasecmp(buf, "Content-Disposition:", 20) == 0) {
2521 if (SC_FWRITE(buf, sizeof(gchar), len, fp) < len) {
2522 g_warning("failed to dump %zd bytes from file", len);
2523 procmime_fclose(infp);
2528 procmime_fclose(infp);
2531 case MIMECONTENT_MEM:
2532 len = strlen(mimeinfo->data.mem);
2533 if (SC_FWRITE(mimeinfo->data.mem, sizeof(gchar), len, fp) < len) {
2534 g_warning("failed to dump %zd bytes from mem", len);
2543 childnode = mimeinfo->node->children;
2544 if (childnode == NULL)
2547 child = (MimeInfo *) childnode->data;
2548 if (fprintf(fp, "MIME-Version: 1.0\n") < 0) {
2549 g_warning("failed to write mime version");
2552 if (procmime_write_mime_header(child, fp) < 0)
2554 return procmime_write_mimeinfo(child, fp);
2557 static gint procmime_write_multipart(MimeInfo *mimeinfo, FILE *fp)
2561 gchar *boundary, *str, *str2;
2562 gchar buf[BUFFSIZE];
2563 gboolean firstboundary;
2566 debug_print("procmime_write_multipart\n");
2568 boundary = g_hash_table_lookup(mimeinfo->typeparameters, "boundary");
2570 switch (mimeinfo->content) {
2571 case MIMECONTENT_FILE:
2572 if ((infp = procmime_fopen(mimeinfo->data.filename, "rb")) == NULL) {
2573 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
2576 if (fseek(infp, mimeinfo->offset, SEEK_SET) < 0) {
2577 FILE_OP_ERROR(mimeinfo->data.filename, "fseek");
2578 procmime_fclose(infp);
2581 while (SC_FGETS(buf, sizeof(buf), infp) == buf) {
2582 if (IS_BOUNDARY(buf, boundary, strlen(boundary)))
2585 if (SC_FWRITE(buf, sizeof(gchar), len, fp) < len) {
2586 g_warning("failed to write %zd", len);
2587 procmime_fclose(infp);
2591 procmime_fclose(infp);
2594 case MIMECONTENT_MEM:
2595 str = g_strdup(mimeinfo->data.mem);
2596 if (((str2 = strstr(str, boundary)) != NULL) && ((str2 - str) >= 2) &&
2597 (*(str2 - 1) == '-') && (*(str2 - 2) == '-'))
2600 if (SC_FWRITE(str, sizeof(gchar), len, fp) < len) {
2601 g_warning("failed to write %zd from mem", len);
2612 childnode = mimeinfo->node->children;
2613 firstboundary = TRUE;
2614 while (childnode != NULL) {
2615 MimeInfo *child = childnode->data;
2618 firstboundary = FALSE;
2620 TRY(fprintf(fp, "\n") >= 0);
2622 TRY(fprintf(fp, "--%s\n", boundary) >= 0);
2624 if (procmime_write_mime_header(child, fp) < 0)
2626 if (procmime_write_mimeinfo(child, fp) < 0)
2629 childnode = g_node_next_sibling(childnode);
2631 TRY(fprintf(fp, "\n--%s--\n", boundary) >= 0);
2636 gint procmime_write_mimeinfo(MimeInfo *mimeinfo, FILE *fp)
2640 debug_print("procmime_write_mimeinfo\n");
2642 if (G_NODE_IS_LEAF(mimeinfo->node)) {
2643 switch (mimeinfo->content) {
2644 case MIMECONTENT_FILE:
2645 if ((infp = procmime_fopen(mimeinfo->data.filename, "rb")) == NULL) {
2646 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
2649 copy_file_part_to_fp(infp, mimeinfo->offset, mimeinfo->length, fp);
2650 procmime_fclose(infp);
2653 case MIMECONTENT_MEM:
2654 len = strlen(mimeinfo->data.mem);
2655 if (SC_FWRITE(mimeinfo->data.mem, sizeof(gchar), len, fp) < len)
2663 /* Call writer for mime type */
2664 switch (mimeinfo->type) {
2665 case MIMETYPE_MESSAGE:
2666 if (g_ascii_strcasecmp(mimeinfo->subtype, "rfc822") == 0) {
2667 return procmime_write_message_rfc822(mimeinfo, fp);
2671 case MIMETYPE_MULTIPART:
2672 return procmime_write_multipart(mimeinfo, fp);
2684 gchar *procmime_get_part_file_name(MimeInfo *mimeinfo)
2688 if ((mimeinfo->type == MIMETYPE_TEXT) && !g_ascii_strcasecmp(mimeinfo->subtype, "html"))
2689 base = g_strdup("mimetmp.html");
2691 const gchar *basetmp;
2694 basetmp = procmime_mimeinfo_get_parameter(mimeinfo, "filename");
2695 if (basetmp == NULL)
2696 basetmp = procmime_mimeinfo_get_parameter(mimeinfo, "name");
2697 if (basetmp == NULL)
2698 basetmp = "mimetmp";
2699 basename = g_path_get_basename(basetmp);
2700 if (*basename == '\0') {
2702 basename = g_strdup("mimetmp");
2704 base = conv_filename_from_utf8(basename);
2706 subst_for_shellsafe_filename(base);