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"
44 #include "quoted-printable.h"
51 #include "prefs_common.h"
52 #include "prefs_gtk.h"
53 #include "alertpanel.h"
57 static GHashTable *procmime_get_mime_type_table (void);
58 static MimeInfo *procmime_scan_file_short(const gchar *filename);
59 static MimeInfo *procmime_scan_queue_file_short(const gchar *filename);
60 static MimeInfo *procmime_scan_queue_file_full(const gchar *filename, gboolean short_scan);
62 MimeInfo *procmime_mimeinfo_new(void)
66 mimeinfo = g_new0(MimeInfo, 1);
67 mimeinfo->content = MIMECONTENT_EMPTY;
68 mimeinfo->data.filename = NULL;
70 mimeinfo->type = MIMETYPE_UNKNOWN;
71 mimeinfo->encoding_type = ENC_UNKNOWN;
72 mimeinfo->typeparameters = g_hash_table_new(g_str_hash, g_str_equal);
74 mimeinfo->disposition = DISPOSITIONTYPE_UNKNOWN;
75 mimeinfo->dispositionparameters
76 = g_hash_table_new(g_str_hash, g_str_equal);
78 mimeinfo->node = g_node_new(mimeinfo);
83 static gboolean procmime_mimeinfo_parameters_destroy(gpointer key, gpointer value, gpointer user_data)
91 static gchar *forced_charset = NULL;
93 void procmime_force_charset(const gchar *str)
95 g_free(forced_charset);
96 forced_charset = NULL;
98 forced_charset = g_strdup(str);
101 static EncodingType forced_encoding = 0;
103 void procmime_force_encoding(EncodingType encoding)
105 forced_encoding = encoding;
108 static gboolean free_func(GNode *node, gpointer data)
110 MimeInfo *mimeinfo = (MimeInfo *) node->data;
112 switch (mimeinfo->content) {
113 case MIMECONTENT_FILE:
115 claws_unlink(mimeinfo->data.filename);
116 g_free(mimeinfo->data.filename);
119 case MIMECONTENT_MEM:
121 g_free(mimeinfo->data.mem);
126 g_free(mimeinfo->subtype);
127 g_free(mimeinfo->description);
128 g_free(mimeinfo->id);
129 g_free(mimeinfo->location);
131 g_hash_table_foreach_remove(mimeinfo->typeparameters,
132 procmime_mimeinfo_parameters_destroy, NULL);
133 g_hash_table_destroy(mimeinfo->typeparameters);
134 g_hash_table_foreach_remove(mimeinfo->dispositionparameters,
135 procmime_mimeinfo_parameters_destroy, NULL);
136 g_hash_table_destroy(mimeinfo->dispositionparameters);
138 if (mimeinfo->privacy)
139 privacy_free_privacydata(mimeinfo->privacy);
146 void procmime_mimeinfo_free_all(MimeInfo *mimeinfo)
153 node = mimeinfo->node;
154 g_node_traverse(node, G_IN_ORDER, G_TRAVERSE_ALL, -1, free_func, NULL);
156 g_node_destroy(node);
159 MimeInfo *procmime_mimeinfo_parent(MimeInfo *mimeinfo)
161 cm_return_val_if_fail(mimeinfo != NULL, NULL);
162 cm_return_val_if_fail(mimeinfo->node != NULL, NULL);
164 if (mimeinfo->node->parent == NULL)
166 return (MimeInfo *) mimeinfo->node->parent->data;
169 MimeInfo *procmime_mimeinfo_next(MimeInfo *mimeinfo)
171 cm_return_val_if_fail(mimeinfo != NULL, NULL);
172 cm_return_val_if_fail(mimeinfo->node != NULL, NULL);
174 if (mimeinfo->node->children)
175 return (MimeInfo *) mimeinfo->node->children->data;
176 if (mimeinfo->node->next)
177 return (MimeInfo *) mimeinfo->node->next->data;
179 if (mimeinfo->node->parent == NULL)
182 while (mimeinfo->node->parent != NULL) {
183 mimeinfo = (MimeInfo *) mimeinfo->node->parent->data;
184 if (mimeinfo->node->next)
185 return (MimeInfo *) mimeinfo->node->next->data;
191 MimeInfo *procmime_scan_message(MsgInfo *msginfo)
196 filename = procmsg_get_message_file_path(msginfo);
197 if (!filename || !is_file_exist(filename)) {
202 if (!folder_has_parent_of_type(msginfo->folder, F_QUEUE) &&
203 !folder_has_parent_of_type(msginfo->folder, F_DRAFT))
204 mimeinfo = procmime_scan_file(filename);
206 mimeinfo = procmime_scan_queue_file(filename);
212 MimeInfo *procmime_scan_message_short(MsgInfo *msginfo)
217 filename = procmsg_get_message_file_path(msginfo);
218 if (!filename || !is_file_exist(filename)) {
223 if (!folder_has_parent_of_type(msginfo->folder, F_QUEUE) &&
224 !folder_has_parent_of_type(msginfo->folder, F_DRAFT))
225 mimeinfo = procmime_scan_file_short(filename);
227 mimeinfo = procmime_scan_queue_file_short(filename);
235 H_CONTENT_TRANSFER_ENCODING = 0,
237 H_CONTENT_DISPOSITION = 2,
238 H_CONTENT_DESCRIPTION = 3,
242 const gchar *procmime_mimeinfo_get_parameter(MimeInfo *mimeinfo, const gchar *name)
246 cm_return_val_if_fail(mimeinfo != NULL, NULL);
247 cm_return_val_if_fail(name != NULL, NULL);
249 value = g_hash_table_lookup(mimeinfo->dispositionparameters, name);
251 value = g_hash_table_lookup(mimeinfo->typeparameters, name);
256 #ifdef HAVE_FGETS_UNLOCKED
257 #define SC_FGETS fgets_unlocked
258 #define SC_FPUTS fputs_unlocked
259 #define SC_FPUTC fputc_unlocked
260 #define SC_FREAD fread_unlocked
261 #define SC_FWRITE fwrite_unlocked
262 #define SC_FEOF feof_unlocked
263 #define SC_FERROR ferror_unlocked
265 static FILE *procmime_fopen(const gchar *file, const gchar *mode)
267 FILE *fp = g_fopen(file, mode);
273 static int procmime_fclose(FILE *fp)
279 #define SC_FGETS fgets
280 #define SC_FPUTS fputs
281 #define SC_FPUTC fputc
282 #define SC_FREAD fread
283 #define SC_FWRITE fwrite
285 #define SC_FERROR ferror
286 #define procmime_fopen g_fopen
287 #define procmime_fclose fclose
290 #define FLUSH_LASTLINE() { \
291 if (*lastline != '\0') { \
293 strretchomp(lastline); \
294 llen = strlen(lastline); \
295 if (lastline[llen-1] == ' ' && strcmp(lastline,"-- ")) { \
296 /* this is flowed */ \
298 lastline[llen-1] = '\0'; \
299 if (SC_FPUTS(lastline, outfp) == EOF) \
302 if (SC_FPUTS(lastline, outfp) == EOF) \
304 if (SC_FPUTS("\n", outfp) == EOF) \
308 strcpy(lastline, buf); \
311 gboolean procmime_decode_content(MimeInfo *mimeinfo)
318 gboolean tmp_file = FALSE;
319 gboolean flowed = FALSE;
320 gboolean delsp = FALSE;
321 gboolean err = FALSE;
325 EncodingType encoding = forced_encoding
327 : mimeinfo->encoding_type;
328 gchar lastline[BUFFSIZE];
329 memset(lastline, 0, BUFFSIZE);
331 cm_return_val_if_fail(mimeinfo != NULL, FALSE);
333 if (prefs_common.respect_flowed_format &&
334 mimeinfo->type == MIMETYPE_TEXT &&
335 !strcasecmp(mimeinfo->subtype, "plain")) {
336 if (procmime_mimeinfo_get_parameter(mimeinfo, "format") != NULL &&
337 !strcasecmp(procmime_mimeinfo_get_parameter(mimeinfo, "format"),"flowed"))
340 procmime_mimeinfo_get_parameter(mimeinfo, "delsp") != NULL &&
341 !strcasecmp(procmime_mimeinfo_get_parameter(mimeinfo, "delsp"),"yes"))
346 encoding == ENC_UNKNOWN ||
347 encoding == ENC_BINARY ||
348 encoding == ENC_7BIT ||
353 if (mimeinfo->type == MIMETYPE_MULTIPART || mimeinfo->type == MIMETYPE_MESSAGE)
356 if (mimeinfo->data.filename == NULL)
359 infp = procmime_fopen(mimeinfo->data.filename, "rb");
364 if (fseek(infp, mimeinfo->offset, SEEK_SET) < 0) {
366 procmime_fclose(infp);
370 outfp = get_tmpfile_in_dir(get_mime_tmp_dir(), &tmpfilename);
373 procmime_fclose(infp);
376 #ifdef HAVE_FGETS_UNLOCKED
380 readend = mimeinfo->offset + mimeinfo->length;
382 if (encoding == ENC_QUOTED_PRINTABLE) {
383 while ((ftell(infp) < readend) && (SC_FGETS(buf, sizeof(buf), infp) != NULL)) {
385 len = qp_decode_line(buf);
388 if (SC_FWRITE(buf, 1, len, outfp) < len)
396 } else if (encoding == ENC_BASE64) {
397 gchar outbuf[BUFFSIZE];
398 gint len, inlen, inread;
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 while ((inlen = MIN(readend - ftell(infp), sizeof(buf))) > 0 && !err) {
423 inread = SC_FREAD(buf, 1, inlen, infp);
424 len = g_base64_decode_step(buf, inlen, outbuf, &state, &save);
425 if (uncanonicalize == TRUE && strlen(outbuf) < len && starting) {
426 uncanonicalize = FALSE;
430 if (((inread != inlen) || len < 0) && !got_error) {
431 g_warning("Bad BASE64 content.");
432 if (SC_FWRITE(_("[Error decoding BASE64]\n"),
434 strlen(_("[Error decoding BASE64]\n")),
435 tmpfp) < strlen(_("[Error decoding BASE64]\n")))
436 g_warning("error decoding BASE64");
439 } else if (len >= 0) {
440 /* print out the error message only once
443 /* we won't uncanonicalize, output to outfp directly */
444 if (SC_FWRITE(outbuf, sizeof(gchar), len, outfp) < len)
447 if (SC_FWRITE(outbuf, sizeof(gchar), len, tmpfp) < len)
454 if (uncanonicalize) {
456 while (SC_FGETS(buf, sizeof(buf), tmpfp) != NULL) {
458 if (SC_FPUTS(buf, outfp) == EOF)
461 procmime_fclose(tmpfp);
463 } else if (encoding == ENC_X_UUENCODE) {
464 gchar outbuf[BUFFSIZE];
466 gboolean flag = FALSE;
468 while ((ftell(infp) < readend) && (SC_FGETS(buf, sizeof(buf), infp) != NULL)) {
469 if (!flag && strncmp(buf,"begin ", 6)) continue;
472 len = fromuutobits(outbuf, buf);
475 g_warning("Bad UUENCODE content (%d)", len);
478 if (SC_FWRITE(outbuf, sizeof(gchar), len, outfp) < len)
484 while ((ftell(infp) < readend) && (SC_FGETS(buf, sizeof(buf), infp) != NULL)) {
486 if (SC_FPUTS(buf, outfp) == EOF)
495 g_warning("write error");
498 procmime_fclose(outfp);
499 procmime_fclose(infp);
505 if (g_stat(tmpfilename, &statbuf) < 0) {
506 FILE_OP_ERROR(tmpfilename, "stat");
511 claws_unlink(mimeinfo->data.filename);
512 g_free(mimeinfo->data.filename);
513 mimeinfo->data.filename = tmpfilename;
514 mimeinfo->tmp = TRUE;
515 mimeinfo->offset = 0;
516 mimeinfo->length = statbuf.st_size;
517 mimeinfo->encoding_type = ENC_BINARY;
522 #define B64_LINE_SIZE 57
523 #define B64_BUFFSIZE 77
525 gboolean procmime_encode_content(MimeInfo *mimeinfo, EncodingType encoding)
527 FILE *infp = NULL, *outfp;
531 gboolean err = FALSE;
533 if (mimeinfo->content == MIMECONTENT_EMPTY)
536 if (mimeinfo->encoding_type != ENC_UNKNOWN &&
537 mimeinfo->encoding_type != ENC_BINARY &&
538 mimeinfo->encoding_type != ENC_7BIT &&
539 mimeinfo->encoding_type != ENC_8BIT)
540 if(!procmime_decode_content(mimeinfo))
543 outfp = get_tmpfile_in_dir(get_mime_tmp_dir(), &tmpfilename);
548 #ifdef HAVE_FGETS_UNLOCKED
552 if (mimeinfo->content == MIMECONTENT_FILE && mimeinfo->data.filename) {
553 if ((infp = procmime_fopen(mimeinfo->data.filename, "rb")) == NULL) {
554 g_warning("Can't open file %s", mimeinfo->data.filename);
555 procmime_fclose(outfp);
558 } else if (mimeinfo->content == MIMECONTENT_MEM) {
559 infp = str_open_as_stream(mimeinfo->data.mem);
561 procmime_fclose(outfp);
564 #ifdef HAVE_FGETS_UNLOCKED
568 procmime_fclose(outfp);
569 g_warning("Unknown mimeinfo");
573 if (encoding == ENC_BASE64) {
574 gchar inbuf[B64_LINE_SIZE], *out;
576 gchar *tmp_file = NULL;
578 if (mimeinfo->type == MIMETYPE_TEXT ||
579 mimeinfo->type == MIMETYPE_MESSAGE) {
580 if (mimeinfo->content == MIMECONTENT_FILE) {
581 tmp_file = get_tmp_file();
582 if (canonicalize_file(mimeinfo->data.filename, tmp_file) < 0) {
584 procmime_fclose(infp);
585 procmime_fclose(outfp);
588 if ((tmp_fp = procmime_fopen(tmp_file, "rb")) == NULL) {
589 FILE_OP_ERROR(tmp_file, "fopen");
590 claws_unlink(tmp_file);
592 procmime_fclose(infp);
593 procmime_fclose(outfp);
597 gchar *out = canonicalize_str(mimeinfo->data.mem);
598 procmime_fclose(infp);
599 infp = str_open_as_stream(out);
603 procmime_fclose(outfp);
606 #ifdef HAVE_FGETS_UNLOCKED
612 while ((len = SC_FREAD(inbuf, sizeof(gchar),
613 B64_LINE_SIZE, tmp_fp))
615 out = g_base64_encode(inbuf, B64_LINE_SIZE);
616 if (SC_FPUTS(out, outfp) == EOF)
619 if (SC_FPUTC('\n', outfp) == EOF)
622 if (len > 0 && SC_FEOF(tmp_fp)) {
623 out = g_base64_encode(inbuf, len);
624 if (SC_FPUTS(out, 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 && mimeinfo->disposition != DISPOSITIONTYPE_ATTACHMENT) {
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.");
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);
1108 ext = g_utf8_strdown(base, -1);
1112 mime_type = g_hash_table_lookup(mime_type_table, ext);
1116 str = g_strconcat(mime_type->type, "/", mime_type->sub_type,
1118 debug_print("got type %s for %s\n", str, ext);
1124 gchar *str = get_content_type_from_registry_with_ext(ext);
1131 static guint procmime_str_hash(gconstpointer gptr)
1133 guint hash_result = 0;
1136 for (str = gptr; str && *str; str++) {
1137 if (isupper(*str)) hash_result += (*str + ' ');
1138 else hash_result += *str;
1144 static gint procmime_str_equal(gconstpointer gptr1, gconstpointer gptr2)
1146 const char *str1 = gptr1;
1147 const char *str2 = gptr2;
1149 return !g_utf8_collate(str1, str2);
1152 static GHashTable *procmime_get_mime_type_table(void)
1154 GHashTable *table = NULL;
1156 MimeType *mime_type;
1159 if (!mime_type_list) {
1160 mime_type_list = procmime_get_mime_type_list();
1161 if (!mime_type_list) return NULL;
1164 table = g_hash_table_new(procmime_str_hash, procmime_str_equal);
1166 for (cur = mime_type_list; cur != NULL; cur = cur->next) {
1170 mime_type = (MimeType *)cur->data;
1172 if (!mime_type->extension) continue;
1174 exts = g_strsplit(mime_type->extension, " ", 16);
1175 for (i = 0; exts[i] != NULL; i++) {
1176 /* Don't overwrite previously inserted extension */
1177 if (!g_hash_table_lookup(table, exts[i])) {
1178 key = g_strdup(exts[i]);
1179 g_hash_table_insert(table, key, mime_type);
1188 GList *procmime_get_mime_type_list(void)
1192 gchar buf[BUFFSIZE];
1195 MimeType *mime_type;
1196 gboolean fp_is_glob_file = TRUE;
1199 return mime_type_list;
1201 #if defined(__NetBSD__) || defined(__OpenBSD__) || defined(__FreeBSD__)
1202 if ((fp = procmime_fopen(DATAROOTDIR "/mime/globs", "rb")) == NULL)
1204 if ((fp = procmime_fopen("/usr/share/mime/globs", "rb")) == NULL)
1207 fp_is_glob_file = FALSE;
1208 if ((fp = procmime_fopen("/etc/mime.types", "rb")) == NULL) {
1209 if ((fp = procmime_fopen(SYSCONFDIR "/mime.types", "rb"))
1211 FILE_OP_ERROR(SYSCONFDIR "/mime.types",
1218 while (SC_FGETS(buf, sizeof(buf), fp) != NULL) {
1219 p = strchr(buf, '#');
1225 if (fp_is_glob_file) {
1226 while (*p && !g_ascii_isspace(*p) && (*p!=':')) p++;
1228 while (*p && !g_ascii_isspace(*p)) p++;
1235 delim = strchr(buf, '/');
1236 if (delim == NULL) continue;
1239 mime_type = g_new(MimeType, 1);
1240 mime_type->type = g_strdup(buf);
1241 mime_type->sub_type = g_strdup(delim + 1);
1243 if (fp_is_glob_file) {
1244 while (*p && (g_ascii_isspace(*p)||(*p=='*')||(*p=='.'))) p++;
1246 while (*p && g_ascii_isspace(*p)) p++;
1250 mime_type->extension = g_utf8_strdown(p, -1);
1252 mime_type->extension = NULL;
1254 list = g_list_append(list, mime_type);
1257 procmime_fclose(fp);
1260 g_warning("Can't read mime.types");
1265 EncodingType procmime_get_encoding_for_charset(const gchar *charset)
1269 else if (!g_ascii_strncasecmp(charset, "ISO-2022-", 9) ||
1270 !g_ascii_strcasecmp(charset, "US-ASCII"))
1272 else if (!g_ascii_strcasecmp(charset, "ISO-8859-5") ||
1273 !g_ascii_strncasecmp(charset, "KOI8-", 5) ||
1274 !g_ascii_strcasecmp(charset, "X-MAC-CYRILLIC") ||
1275 !g_ascii_strcasecmp(charset, "MAC-CYRILLIC") ||
1276 !g_ascii_strcasecmp(charset, "Windows-1251"))
1278 else if (!g_ascii_strncasecmp(charset, "ISO-8859-", 9))
1279 return ENC_QUOTED_PRINTABLE;
1280 else if (!g_ascii_strncasecmp(charset, "UTF-8", 5))
1281 return ENC_QUOTED_PRINTABLE;
1286 EncodingType procmime_get_encoding_for_text_file(const gchar *file, gboolean *has_binary)
1289 guchar buf[BUFFSIZE];
1291 size_t octet_chars = 0;
1292 size_t total_len = 0;
1293 gfloat octet_percentage;
1294 gboolean force_b64 = FALSE;
1296 if ((fp = procmime_fopen(file, "rb")) == NULL) {
1297 FILE_OP_ERROR(file, "fopen");
1301 while ((len = SC_FREAD(buf, sizeof(guchar), sizeof(buf), fp)) > 0) {
1305 for (p = buf, i = 0; i < len; ++p, ++i) {
1316 procmime_fclose(fp);
1319 octet_percentage = (gfloat)octet_chars / (gfloat)total_len;
1321 octet_percentage = 0.0;
1323 debug_print("procmime_get_encoding_for_text_file(): "
1324 "8bit chars: %zd / %zd (%f%%)\n", octet_chars, total_len,
1325 100.0 * octet_percentage);
1327 if (octet_percentage > 0.20 || force_b64) {
1328 debug_print("using BASE64\n");
1330 } else if (octet_chars > 0) {
1331 debug_print("using quoted-printable\n");
1332 return ENC_QUOTED_PRINTABLE;
1334 debug_print("using 7bit\n");
1339 struct EncodingTable
1342 EncodingType enc_type;
1345 struct EncodingTable encoding_table[] = {
1348 {"binary", ENC_BINARY},
1349 {"quoted-printable", ENC_QUOTED_PRINTABLE},
1350 {"base64", ENC_BASE64},
1351 {"x-uuencode", ENC_UNKNOWN},
1352 {NULL, ENC_UNKNOWN},
1355 const gchar *procmime_get_encoding_str(EncodingType encoding)
1357 struct EncodingTable *enc_table;
1359 for (enc_table = encoding_table; enc_table->str != NULL; enc_table++) {
1360 if (enc_table->enc_type == encoding)
1361 return enc_table->str;
1366 /* --- NEW MIME STUFF --- */
1373 static struct TypeTable mime_type_table[] = {
1374 {"text", MIMETYPE_TEXT},
1375 {"image", MIMETYPE_IMAGE},
1376 {"audio", MIMETYPE_AUDIO},
1377 {"video", MIMETYPE_VIDEO},
1378 {"application", MIMETYPE_APPLICATION},
1379 {"message", MIMETYPE_MESSAGE},
1380 {"multipart", MIMETYPE_MULTIPART},
1384 const gchar *procmime_get_media_type_str(MimeMediaType type)
1386 struct TypeTable *type_table;
1388 for (type_table = mime_type_table; type_table->str != NULL; type_table++) {
1389 if (type_table->type == type)
1390 return type_table->str;
1395 MimeMediaType procmime_get_media_type(const gchar *str)
1397 struct TypeTable *typetablearray;
1399 for (typetablearray = mime_type_table; typetablearray->str != NULL; typetablearray++)
1400 if (g_ascii_strncasecmp(str, typetablearray->str, strlen(typetablearray->str)) == 0)
1401 return typetablearray->type;
1403 return MIMETYPE_UNKNOWN;
1407 *\brief Safe wrapper for content type string.
1409 *\return const gchar * Pointer to content type string.
1411 gchar *procmime_get_content_type_str(MimeMediaType type,
1412 const char *subtype)
1414 const gchar *type_str = NULL;
1416 if (subtype == NULL || !(type_str = procmime_get_media_type_str(type)))
1417 return g_strdup("unknown");
1418 return g_strdup_printf("%s/%s", type_str, subtype);
1421 static int procmime_parse_mimepart(MimeInfo *parent,
1422 gchar *content_type,
1423 gchar *content_encoding,
1424 gchar *content_description,
1426 gchar *content_disposition,
1427 gchar *content_location,
1428 const gchar *original_msgid,
1429 const gchar *disposition_notification_hdr,
1430 const gchar *filename,
1433 gboolean short_scan);
1435 static void procmime_parse_message_rfc822(MimeInfo *mimeinfo, gboolean short_scan)
1437 HeaderEntry hentry[] = {{"Content-Type:", NULL, TRUE},
1438 {"Content-Transfer-Encoding:",
1440 {"Content-Description:",
1444 {"Content-Disposition:",
1446 {"Content-Location:",
1450 {"Original-Message-ID:",
1454 {NULL, NULL, FALSE}};
1455 guint content_start, i;
1460 procmime_decode_content(mimeinfo);
1462 fp = procmime_fopen(mimeinfo->data.filename, "rb");
1464 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
1467 if (fseek(fp, mimeinfo->offset, SEEK_SET) < 0) {
1468 FILE_OP_ERROR(mimeinfo->data.filename, "fseek");
1469 procmime_fclose(fp);
1472 procheader_get_header_fields(fp, hentry);
1473 if (hentry[0].body != NULL) {
1474 tmp = conv_unmime_header(hentry[0].body, NULL, FALSE);
1475 g_free(hentry[0].body);
1476 hentry[0].body = tmp;
1478 if (hentry[2].body != NULL) {
1479 tmp = conv_unmime_header(hentry[2].body, NULL, FALSE);
1480 g_free(hentry[2].body);
1481 hentry[2].body = tmp;
1483 if (hentry[4].body != NULL) {
1484 tmp = conv_unmime_header(hentry[4].body, NULL, FALSE);
1485 g_free(hentry[4].body);
1486 hentry[4].body = tmp;
1488 if (hentry[5].body != NULL) {
1489 tmp = conv_unmime_header(hentry[5].body, NULL, FALSE);
1490 g_free(hentry[5].body);
1491 hentry[5].body = tmp;
1493 if (hentry[7].body != NULL) {
1494 tmp = conv_unmime_header(hentry[7].body, NULL, FALSE);
1495 g_free(hentry[7].body);
1496 hentry[7].body = tmp;
1498 if (hentry[8].body != NULL) {
1499 tmp = conv_unmime_header(hentry[8].body, NULL, FALSE);
1500 g_free(hentry[8].body);
1501 hentry[8].body = tmp;
1504 content_start = ftell(fp);
1505 procmime_fclose(fp);
1507 len = mimeinfo->length - (content_start - mimeinfo->offset);
1510 procmime_parse_mimepart(mimeinfo,
1511 hentry[0].body, hentry[1].body,
1512 hentry[2].body, hentry[3].body,
1513 hentry[4].body, hentry[5].body,
1514 hentry[7].body, hentry[8].body,
1515 mimeinfo->data.filename, content_start,
1518 for (i = 0; i < (sizeof hentry / sizeof hentry[0]); i++) {
1519 g_free(hentry[i].body);
1520 hentry[i].body = NULL;
1524 static void procmime_parse_disposition_notification(MimeInfo *mimeinfo,
1525 const gchar *original_msgid, const gchar *disposition_notification_hdr,
1526 gboolean short_scan)
1528 HeaderEntry hentry[] = {{"Original-Message-ID:", NULL, TRUE},
1529 {"Disposition:", NULL, TRUE},
1530 {NULL, NULL, FALSE}};
1533 gchar *orig_msg_id = NULL;
1536 procmime_decode_content(mimeinfo);
1538 debug_print("parse disposition notification\n");
1539 fp = procmime_fopen(mimeinfo->data.filename, "rb");
1541 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
1544 if (fseek(fp, mimeinfo->offset, SEEK_SET) < 0) {
1545 FILE_OP_ERROR(mimeinfo->data.filename, "fseek");
1546 procmime_fclose(fp);
1550 if (original_msgid && disposition_notification_hdr) {
1551 hentry[0].body = g_strdup(original_msgid);
1552 hentry[1].body = g_strdup(disposition_notification_hdr);
1554 procheader_get_header_fields(fp, hentry);
1557 procmime_fclose(fp);
1559 if (!hentry[0].body || !hentry[1].body) {
1560 debug_print("MsgId %s, Disp %s\n",
1561 hentry[0].body ? hentry[0].body:"(nil)",
1562 hentry[1].body ? hentry[1].body:"(nil)");
1566 orig_msg_id = g_strdup(hentry[0].body);
1567 disp = g_strdup(hentry[1].body);
1569 extract_parenthesis(orig_msg_id, '<', '>');
1570 remove_space(orig_msg_id);
1572 if (strstr(disp, "displayed")) {
1573 /* find sent message, if possible */
1574 MsgInfo *info = NULL;
1576 debug_print("%s has been displayed.\n", orig_msg_id);
1577 for (flist = folder_get_list(); flist != NULL; flist = g_list_next(flist)) {
1578 FolderItem *outbox = ((Folder *)(flist->data))->outbox;
1580 debug_print("skipping folder with no outbox...\n");
1583 info = folder_item_get_msginfo_by_msgid(outbox, orig_msg_id);
1584 debug_print("%s %s in %s\n", info?"found":"didn't find", orig_msg_id, outbox->path);
1586 procmsg_msginfo_set_flags(info, MSG_RETRCPT_GOT, 0);
1587 procmsg_msginfo_free(info);
1591 g_free(orig_msg_id);
1594 for (i = 0; i < (sizeof hentry / sizeof hentry[0]); i++) {
1595 g_free(hentry[i].body);
1596 hentry[i].body = NULL;
1600 #define GET_HEADERS() { \
1601 procheader_get_header_fields(fp, hentry); \
1602 if (hentry[0].body != NULL) { \
1603 tmp = conv_unmime_header(hentry[0].body, NULL, FALSE); \
1604 g_free(hentry[0].body); \
1605 hentry[0].body = tmp; \
1607 if (hentry[2].body != NULL) { \
1608 tmp = conv_unmime_header(hentry[2].body, NULL, FALSE); \
1609 g_free(hentry[2].body); \
1610 hentry[2].body = tmp; \
1612 if (hentry[4].body != NULL) { \
1613 tmp = conv_unmime_header(hentry[4].body, NULL, FALSE); \
1614 g_free(hentry[4].body); \
1615 hentry[4].body = tmp; \
1617 if (hentry[5].body != NULL) { \
1618 tmp = conv_unmime_header(hentry[5].body, NULL, FALSE); \
1619 g_free(hentry[5].body); \
1620 hentry[5].body = tmp; \
1622 if (hentry[6].body != NULL) { \
1623 tmp = conv_unmime_header(hentry[6].body, NULL, FALSE); \
1624 g_free(hentry[6].body); \
1625 hentry[6].body = tmp; \
1627 if (hentry[7].body != NULL) { \
1628 tmp = conv_unmime_header(hentry[7].body, NULL, FALSE); \
1629 g_free(hentry[7].body); \
1630 hentry[7].body = tmp; \
1634 static void procmime_parse_multipart(MimeInfo *mimeinfo, gboolean short_scan)
1636 HeaderEntry hentry[] = {{"Content-Type:", NULL, TRUE},
1637 {"Content-Transfer-Encoding:",
1639 {"Content-Description:",
1643 {"Content-Disposition:",
1645 {"Content-Location:",
1647 {"Original-Message-ID:",
1651 {NULL, NULL, FALSE}};
1654 gint boundary_len = 0, lastoffset = -1, i;
1655 gchar buf[BUFFSIZE];
1658 gboolean start_found = FALSE;
1659 gboolean end_found = FALSE;
1661 boundary = g_hash_table_lookup(mimeinfo->typeparameters, "boundary");
1664 boundary_len = strlen(boundary);
1666 procmime_decode_content(mimeinfo);
1668 fp = procmime_fopen(mimeinfo->data.filename, "rb");
1670 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
1674 if (fseek(fp, mimeinfo->offset, SEEK_SET) < 0) {
1675 FILE_OP_ERROR(mimeinfo->data.filename, "fseek");
1676 procmime_fclose(fp);
1680 while (SC_FGETS(buf, sizeof(buf), fp) != NULL && result == 0) {
1681 if (ftell(fp) - 1 > (mimeinfo->offset + mimeinfo->length))
1684 if (IS_BOUNDARY(buf, boundary, boundary_len)) {
1687 if (lastoffset != -1) {
1688 gint len = (ftell(fp) - strlen(buf)) - lastoffset - 1;
1691 result = procmime_parse_mimepart(mimeinfo,
1692 hentry[0].body, hentry[1].body,
1693 hentry[2].body, hentry[3].body,
1694 hentry[4].body, hentry[5].body,
1695 hentry[6].body, hentry[7].body,
1696 mimeinfo->data.filename, lastoffset,
1698 if (result == 1 && short_scan)
1703 if (buf[2 + boundary_len] == '-' &&
1704 buf[2 + boundary_len + 1] == '-') {
1708 for (i = 0; i < (sizeof hentry / sizeof hentry[0]) ; i++) {
1709 g_free(hentry[i].body);
1710 hentry[i].body = NULL;
1713 lastoffset = ftell(fp);
1717 if (start_found && !end_found && lastoffset != -1) {
1718 gint len = (ftell(fp) - strlen(buf)) - lastoffset - 1;
1721 result = procmime_parse_mimepart(mimeinfo,
1722 hentry[0].body, hentry[1].body,
1723 hentry[2].body, hentry[3].body,
1724 hentry[4].body, hentry[5].body,
1725 hentry[6].body, hentry[7].body,
1726 mimeinfo->data.filename, lastoffset,
1729 mimeinfo->broken = TRUE;
1732 for (i = 0; i < (sizeof hentry / sizeof hentry[0]); i++) {
1733 g_free(hentry[i].body);
1734 hentry[i].body = NULL;
1736 procmime_fclose(fp);
1739 static void parse_parameters(const gchar *parameters, GHashTable *table)
1741 gchar *params, *param, *next;
1742 GSList *convlist = NULL, *concatlist = NULL, *cur;
1744 params = g_strdup(parameters);
1747 for (; next != NULL; param = next) {
1748 gchar *attribute, *value, *tmp, *down_attr, *orig_down_attr;
1750 gboolean convert = FALSE;
1752 next = strchr_with_skip_quote(param, '"', ';');
1761 value = strchr(attribute, '=');
1767 while (value[0] != '\0' && value[0] == ' ')
1770 down_attr = g_utf8_strdown(attribute, -1);
1771 orig_down_attr = down_attr;
1773 len = down_attr ? strlen(down_attr):0;
1774 if (len > 0 && down_attr[len - 1] == '*') {
1775 gchar *srcpos, *dstpos, *endpos;
1778 down_attr[len - 1] = '\0';
1782 endpos = value + strlen(value);
1783 while (srcpos < endpos) {
1789 if (!get_hex_value(&dstvalue, srcpos[1], srcpos[2]))
1799 if (value[0] == '"')
1800 extract_quote(value, '"');
1802 if (value[0] == '"')
1803 extract_quote(value, '"');
1804 else if ((tmp = strchr(value, ' ')) != NULL)
1809 while (down_attr[0] == ' ')
1811 while (down_attr[strlen(down_attr)-1] == ' ')
1812 down_attr[strlen(down_attr)-1] = '\0';
1815 while (value[0] != '\0' && value[0] == ' ')
1817 while (value[strlen(value)-1] == ' ')
1818 value[strlen(value)-1] = '\0';
1820 if (down_attr && strrchr(down_attr, '*') != NULL) {
1823 tmpattr = g_strdup(down_attr);
1824 tmp = strrchr(tmpattr, '*');
1827 if ((tmp[1] == '0') && (tmp[2] == '\0') &&
1828 (g_slist_find_custom(concatlist, down_attr, (GCompareFunc)g_strcmp0) == NULL))
1829 concatlist = g_slist_prepend(concatlist, g_strdup(tmpattr));
1831 if (convert && (g_slist_find_custom(convlist, tmpattr, (GCompareFunc)g_strcmp0) == NULL))
1832 convlist = g_slist_prepend(convlist, g_strdup(tmpattr));
1835 } else if (convert) {
1836 if (g_slist_find_custom(convlist, down_attr, (GCompareFunc)g_strcmp0) == NULL)
1837 convlist = g_slist_prepend(convlist, g_strdup(down_attr));
1840 if (g_hash_table_lookup(table, down_attr) == NULL)
1841 g_hash_table_insert(table, g_strdup(down_attr), g_strdup(value));
1842 g_free(orig_down_attr);
1845 for (cur = concatlist; cur != NULL; cur = g_slist_next(cur)) {
1846 gchar *attribute, *attrwnum, *partvalue;
1850 attribute = (gchar *) cur->data;
1851 value = g_string_sized_new(64);
1853 attrwnum = g_strdup_printf("%s*%d", attribute, n);
1854 while ((partvalue = g_hash_table_lookup(table, attrwnum)) != NULL) {
1855 g_string_append(value, partvalue);
1857 g_hash_table_remove(table, attrwnum);
1860 attrwnum = g_strdup_printf("%s*%d", attribute, n);
1864 g_hash_table_insert(table, g_strdup(attribute), g_strdup(value->str));
1865 g_string_free(value, TRUE);
1867 slist_free_strings_full(concatlist);
1869 for (cur = convlist; cur != NULL; cur = g_slist_next(cur)) {
1870 gchar *attribute, *key, *value;
1871 gchar *charset, *lang, *oldvalue, *newvalue;
1873 attribute = (gchar *) cur->data;
1874 if (!g_hash_table_lookup_extended(
1875 table, attribute, (gpointer *)(gchar *) &key, (gpointer *)(gchar *) &value))
1879 if (charset == NULL)
1881 lang = strchr(charset, '\'');
1886 oldvalue = strchr(lang, '\'');
1887 if (oldvalue == NULL)
1892 newvalue = conv_codeset_strdup(oldvalue, charset, CS_UTF_8);
1894 g_hash_table_remove(table, attribute);
1898 g_hash_table_insert(table, g_strdup(attribute), newvalue);
1900 slist_free_strings_full(convlist);
1905 static void procmime_parse_content_type(const gchar *content_type, MimeInfo *mimeinfo)
1907 cm_return_if_fail(content_type != NULL);
1908 cm_return_if_fail(mimeinfo != NULL);
1910 /* RFC 2045, page 13 says that the mime subtype is MANDATORY;
1911 * if it's not available we use the default Content-Type */
1912 if ((content_type[0] == '\0') || (strchr(content_type, '/') == NULL)) {
1913 mimeinfo->type = MIMETYPE_TEXT;
1914 mimeinfo->subtype = g_strdup("plain");
1915 if (g_hash_table_lookup(mimeinfo->typeparameters,
1916 "charset") == NULL) {
1917 g_hash_table_insert(mimeinfo->typeparameters,
1918 g_strdup("charset"),
1920 conv_get_locale_charset_str_no_utf8()));
1923 gchar *type, *subtype, *params;
1925 type = g_strdup(content_type);
1926 subtype = strchr(type, '/') + 1;
1927 *(subtype - 1) = '\0';
1928 if ((params = strchr(subtype, ';')) != NULL) {
1933 mimeinfo->type = procmime_get_media_type(type);
1934 mimeinfo->subtype = g_strstrip(g_strdup(subtype));
1936 /* Get mimeinfo->typeparameters */
1938 parse_parameters(params, mimeinfo->typeparameters);
1944 static void procmime_parse_content_disposition(const gchar *content_disposition, MimeInfo *mimeinfo)
1946 gchar *tmp, *params;
1948 cm_return_if_fail(content_disposition != NULL);
1949 cm_return_if_fail(mimeinfo != NULL);
1951 tmp = g_strdup(content_disposition);
1952 if ((params = strchr(tmp, ';')) != NULL) {
1958 if (!g_ascii_strcasecmp(tmp, "inline"))
1959 mimeinfo->disposition = DISPOSITIONTYPE_INLINE;
1960 else if (!g_ascii_strcasecmp(tmp, "attachment"))
1961 mimeinfo->disposition = DISPOSITIONTYPE_ATTACHMENT;
1963 mimeinfo->disposition = DISPOSITIONTYPE_ATTACHMENT;
1966 parse_parameters(params, mimeinfo->dispositionparameters);
1972 static void procmime_parse_content_encoding(const gchar *content_encoding, MimeInfo *mimeinfo)
1974 struct EncodingTable *enc_table;
1976 for (enc_table = encoding_table; enc_table->str != NULL; enc_table++) {
1977 if (g_ascii_strcasecmp(enc_table->str, content_encoding) == 0) {
1978 mimeinfo->encoding_type = enc_table->enc_type;
1982 mimeinfo->encoding_type = ENC_UNKNOWN;
1986 static GSList *registered_parsers = NULL;
1988 static MimeParser *procmime_get_mimeparser_for_type(MimeMediaType type, const gchar *sub_type)
1991 for (cur = registered_parsers; cur; cur = cur->next) {
1992 MimeParser *parser = (MimeParser *)cur->data;
1993 if (parser->type == type && !strcmp2(parser->sub_type, sub_type))
1999 void procmime_mimeparser_register(MimeParser *parser)
2001 if (!procmime_get_mimeparser_for_type(parser->type, parser->sub_type))
2002 registered_parsers = g_slist_append(registered_parsers, parser);
2006 void procmime_mimeparser_unregister(MimeParser *parser)
2008 registered_parsers = g_slist_remove(registered_parsers, parser);
2011 static gboolean procmime_mimeparser_parse(MimeParser *parser, MimeInfo *mimeinfo)
2013 cm_return_val_if_fail(parser->parse != NULL, FALSE);
2014 return parser->parse(parser, mimeinfo);
2017 static int procmime_parse_mimepart(MimeInfo *parent,
2018 gchar *content_type,
2019 gchar *content_encoding,
2020 gchar *content_description,
2022 gchar *content_disposition,
2023 gchar *content_location,
2024 const gchar *original_msgid,
2025 const gchar *disposition_notification_hdr,
2026 const gchar *filename,
2029 gboolean short_scan)
2032 MimeParser *parser = NULL;
2033 gboolean parsed = FALSE;
2036 /* Create MimeInfo */
2037 mimeinfo = procmime_mimeinfo_new();
2038 mimeinfo->content = MIMECONTENT_FILE;
2040 if (parent != NULL) {
2041 if (g_node_depth(parent->node) > 32) {
2042 /* 32 is an arbitrary value
2043 * this avoids DOSsing ourselves
2044 * with enormous messages
2046 procmime_mimeinfo_free_all(mimeinfo);
2049 g_node_append(parent->node, mimeinfo->node);
2051 mimeinfo->data.filename = g_strdup(filename);
2052 mimeinfo->offset = offset;
2053 mimeinfo->length = length;
2055 if (content_type != NULL) {
2056 g_strchomp(content_type);
2057 procmime_parse_content_type(content_type, mimeinfo);
2059 mimeinfo->type = MIMETYPE_TEXT;
2060 mimeinfo->subtype = g_strdup("plain");
2061 if (g_hash_table_lookup(mimeinfo->typeparameters,
2062 "charset") == NULL) {
2063 g_hash_table_insert(mimeinfo->typeparameters,
2064 g_strdup("charset"),
2066 conv_get_locale_charset_str_no_utf8()));
2070 if (content_encoding != NULL) {
2071 g_strchomp(content_encoding);
2072 procmime_parse_content_encoding(content_encoding, mimeinfo);
2074 mimeinfo->encoding_type = ENC_UNKNOWN;
2077 if (content_description != NULL)
2078 mimeinfo->description = g_strdup(content_description);
2080 mimeinfo->description = NULL;
2082 if (content_id != NULL)
2083 mimeinfo->id = g_strdup(content_id);
2085 mimeinfo->id = NULL;
2087 if (content_location != NULL)
2088 mimeinfo->location = g_strdup(content_location);
2090 mimeinfo->location = NULL;
2092 if (content_disposition != NULL) {
2093 g_strchomp(content_disposition);
2094 procmime_parse_content_disposition(content_disposition, mimeinfo);
2096 mimeinfo->disposition = DISPOSITIONTYPE_UNKNOWN;
2098 /* Call parser for mime type */
2099 if ((parser = procmime_get_mimeparser_for_type(mimeinfo->type, mimeinfo->subtype)) != NULL) {
2100 parsed = procmime_mimeparser_parse(parser, mimeinfo);
2103 switch (mimeinfo->type) {
2105 if (g_ascii_strcasecmp(mimeinfo->subtype, "plain") == 0 && short_scan) {
2110 case MIMETYPE_MESSAGE:
2111 if (g_ascii_strcasecmp(mimeinfo->subtype, "rfc822") == 0) {
2112 procmime_parse_message_rfc822(mimeinfo, short_scan);
2114 if (g_ascii_strcasecmp(mimeinfo->subtype, "disposition-notification") == 0) {
2115 procmime_parse_disposition_notification(mimeinfo,
2116 original_msgid, disposition_notification_hdr, short_scan);
2120 case MIMETYPE_MULTIPART:
2121 procmime_parse_multipart(mimeinfo, short_scan);
2124 case MIMETYPE_APPLICATION:
2125 if (g_ascii_strcasecmp(mimeinfo->subtype, "octet-stream") == 0
2126 && original_msgid && *original_msgid
2127 && disposition_notification_hdr && *disposition_notification_hdr) {
2128 procmime_parse_disposition_notification(mimeinfo,
2129 original_msgid, disposition_notification_hdr, short_scan);
2140 static gchar *typenames[] = {
2151 static gboolean output_func(GNode *node, gpointer data)
2154 MimeInfo *mimeinfo = (MimeInfo *) node->data;
2156 depth = g_node_depth(node);
2157 for (i = 0; i < depth; i++)
2159 g_print("%s/%s (offset:%d length:%d encoding: %d)\n", typenames[mimeinfo->type], mimeinfo->subtype, mimeinfo->offset, mimeinfo->length, mimeinfo->encoding_type);
2164 static void output_mime_structure(MimeInfo *mimeinfo, int indent)
2166 g_node_traverse(mimeinfo->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1, output_func, NULL);
2169 static MimeInfo *procmime_scan_file_with_offset(const gchar *filename, int offset, gboolean short_scan)
2174 if (g_stat(filename, &buf) < 0) {
2175 FILE_OP_ERROR(filename, "stat");
2179 mimeinfo = procmime_mimeinfo_new();
2180 mimeinfo->content = MIMECONTENT_FILE;
2181 mimeinfo->encoding_type = ENC_UNKNOWN;
2182 mimeinfo->type = MIMETYPE_MESSAGE;
2183 mimeinfo->subtype = g_strdup("rfc822");
2184 mimeinfo->data.filename = g_strdup(filename);
2185 mimeinfo->offset = offset;
2186 mimeinfo->length = buf.st_size - offset;
2188 procmime_parse_message_rfc822(mimeinfo, short_scan);
2189 if (debug_get_mode())
2190 output_mime_structure(mimeinfo, 0);
2195 static MimeInfo *procmime_scan_file_full(const gchar *filename, gboolean short_scan)
2199 cm_return_val_if_fail(filename != NULL, NULL);
2201 mimeinfo = procmime_scan_file_with_offset(filename, 0, short_scan);
2206 MimeInfo *procmime_scan_file(const gchar *filename)
2208 return procmime_scan_file_full(filename, FALSE);
2211 static MimeInfo *procmime_scan_file_short(const gchar *filename)
2213 return procmime_scan_file_full(filename, TRUE);
2216 static MimeInfo *procmime_scan_queue_file_full(const gchar *filename, gboolean short_scan)
2220 gchar buf[BUFFSIZE];
2223 cm_return_val_if_fail(filename != NULL, NULL);
2226 if ((fp = procmime_fopen(filename, "rb")) == NULL)
2228 /* Skip queue header */
2229 while (SC_FGETS(buf, sizeof(buf), fp) != NULL) {
2231 if ((!strncmp(buf, "X-Claws-End-Special-Headers: 1",
2232 strlen("X-Claws-End-Special-Headers:"))) ||
2233 (!strncmp(buf, "X-Sylpheed-End-Special-Headers: 1",
2234 strlen("X-Sylpheed-End-Special-Headers:"))))
2237 if (buf[0] == '\r' || buf[0] == '\n') break;
2238 /* from other mailers */
2239 if (!strncmp(buf, "Date: ", 6)
2240 || !strncmp(buf, "To: ", 4)
2241 || !strncmp(buf, "From: ", 6)
2242 || !strncmp(buf, "Subject: ", 9)) {
2248 procmime_fclose(fp);
2250 mimeinfo = procmime_scan_file_with_offset(filename, offset, short_scan);
2255 MimeInfo *procmime_scan_queue_file(const gchar *filename)
2257 return procmime_scan_queue_file_full(filename, FALSE);
2260 static MimeInfo *procmime_scan_queue_file_short(const gchar *filename)
2262 return procmime_scan_queue_file_full(filename, TRUE);
2267 ENC_AS_QUOTED_STRING,
2272 typedef struct _ParametersData {
2278 static void write_parameters(gpointer key, gpointer value, gpointer user_data)
2281 gchar *val = value, *valpos, *tmp;
2282 ParametersData *pdata = (ParametersData *)user_data;
2283 GString *buf = g_string_new("");
2286 EncodeAs encas = ENC_AS_TOKEN;
2288 for (valpos = val; *valpos != 0; valpos++) {
2289 if (!IS_ASCII(*valpos)) {
2290 encas = ENC_AS_ENCWORD;
2295 if (((*valpos >= 0) && (*valpos < 037)) || (*valpos == 0177)) {
2296 encas = ENC_AS_QUOTED_STRING;
2300 /* tspecials + SPACE */
2318 encas = ENC_AS_QUOTED_STRING;
2325 g_string_append_printf(buf, "%s=%s", param, val);
2328 case ENC_AS_QUOTED_STRING:
2329 g_string_append_printf(buf, "%s=\"%s\"", param, val);
2332 #if 0 /* we don't use that for now */
2333 case ENC_AS_EXTENDED:
2334 if (!g_utf8_validate(val, -1, NULL))
2335 g_string_append_printf(buf, "%s*=%s''", param,
2336 conv_get_locale_charset_str());
2338 g_string_append_printf(buf, "%s*=%s''", param,
2340 for (valpos = val; *valpos != '\0'; valpos++) {
2341 if (IS_ASCII(*valpos) && isalnum(*valpos)) {
2342 g_string_append_printf(buf, "%c", *valpos);
2344 gchar hexstr[3] = "XX";
2345 get_hex_str(hexstr, *valpos);
2346 g_string_append_printf(buf, "%%%s", hexstr);
2351 case ENC_AS_EXTENDED:
2352 debug_print("Unhandled ENC_AS_EXTENDED.");
2355 case ENC_AS_ENCWORD:
2356 len = MAX(strlen(val)*6, 512);
2357 tmp = g_malloc(len+1);
2358 codeconv_set_strict(TRUE);
2359 conv_encode_header_full(tmp, len, val, pdata->len + strlen(param) + 4 , FALSE,
2360 prefs_common.outgoing_charset);
2361 codeconv_set_strict(FALSE);
2362 if (!tmp || !*tmp) {
2363 codeconv_set_strict(TRUE);
2364 conv_encode_header_full(tmp, len, val, pdata->len + strlen(param) + 4 , FALSE,
2365 conv_get_outgoing_charset_str());
2366 codeconv_set_strict(FALSE);
2368 if (!tmp || !*tmp) {
2369 codeconv_set_strict(TRUE);
2370 conv_encode_header_full(tmp, len, val, pdata->len + strlen(param) + 4 , FALSE,
2372 codeconv_set_strict(FALSE);
2374 if (!tmp || !*tmp) {
2375 conv_encode_header_full(tmp, len, val, pdata->len + strlen(param) + 4 , FALSE,
2378 g_string_append_printf(buf, "%s=\"%s\"", param, tmp);
2384 if (buf->str && strlen(buf->str)) {
2385 tmp = strstr(buf->str, "\n");
2387 len = (tmp - buf->str);
2389 len = strlen(buf->str);
2390 if (pdata->len + len > 76) {
2391 if (fprintf(pdata->fp, ";\n %s", buf->str) < 0)
2392 pdata->error = TRUE;
2393 pdata->len = strlen(buf->str) + 1;
2395 if (fprintf(pdata->fp, "; %s", buf->str) < 0)
2396 pdata->error = TRUE;
2397 pdata->len += strlen(buf->str) + 2;
2400 g_string_free(buf, TRUE);
2403 #define TRY(func) { \
2409 int procmime_write_mime_header(MimeInfo *mimeinfo, FILE *fp)
2411 struct TypeTable *type_table;
2412 ParametersData *pdata = g_new0(ParametersData, 1);
2413 debug_print("procmime_write_mime_header\n");
2416 pdata->error = FALSE;
2417 for (type_table = mime_type_table; type_table->str != NULL; type_table++)
2418 if (mimeinfo->type == type_table->type) {
2419 gchar *buf = g_strdup_printf(
2420 "Content-Type: %s/%s", type_table->str, mimeinfo->subtype);
2421 if (fprintf(fp, "%s", buf) < 0) {
2426 pdata->len = strlen(buf);
2430 g_hash_table_foreach(mimeinfo->typeparameters, write_parameters, pdata);
2431 if (pdata->error == TRUE) {
2437 TRY(fprintf(fp, "\n") >= 0);
2439 if (mimeinfo->encoding_type != ENC_UNKNOWN)
2440 TRY(fprintf(fp, "Content-Transfer-Encoding: %s\n", procmime_get_encoding_str(mimeinfo->encoding_type)) >= 0);
2442 if (mimeinfo->description != NULL)
2443 TRY(fprintf(fp, "Content-Description: %s\n", mimeinfo->description) >= 0);
2445 if (mimeinfo->id != NULL)
2446 TRY(fprintf(fp, "Content-ID: %s\n", mimeinfo->id) >= 0);
2448 if (mimeinfo->location != NULL)
2449 TRY(fprintf(fp, "Content-Location: %s\n", mimeinfo->location) >= 0);
2451 if (mimeinfo->disposition != DISPOSITIONTYPE_UNKNOWN) {
2452 ParametersData *pdata = g_new0(ParametersData, 1);
2454 if (mimeinfo->disposition == DISPOSITIONTYPE_INLINE)
2455 buf = g_strdup("Content-Disposition: inline");
2456 else if (mimeinfo->disposition == DISPOSITIONTYPE_ATTACHMENT)
2457 buf = g_strdup("Content-Disposition: attachment");
2459 buf = g_strdup("Content-Disposition: unknown");
2461 if (fprintf(fp, "%s", buf) < 0) {
2466 pdata->len = strlen(buf);
2470 pdata->error = FALSE;
2471 g_hash_table_foreach(mimeinfo->dispositionparameters, write_parameters, pdata);
2472 if (pdata->error == TRUE) {
2477 TRY(fprintf(fp, "\n") >= 0);
2480 TRY(fprintf(fp, "\n") >= 0);
2485 static gint procmime_write_message_rfc822(MimeInfo *mimeinfo, FILE *fp)
2490 gchar buf[BUFFSIZE];
2491 gboolean skip = FALSE;;
2494 debug_print("procmime_write_message_rfc822\n");
2497 switch (mimeinfo->content) {
2498 case MIMECONTENT_FILE:
2499 if ((infp = procmime_fopen(mimeinfo->data.filename, "rb")) == NULL) {
2500 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
2503 if (fseek(infp, mimeinfo->offset, SEEK_SET) < 0) {
2504 FILE_OP_ERROR(mimeinfo->data.filename, "fseek");
2505 procmime_fclose(infp);
2508 while (SC_FGETS(buf, sizeof(buf), infp) == buf) {
2510 if (buf[0] == '\n' && buf[1] == '\0')
2512 if (skip && (buf[0] == ' ' || buf[0] == '\t'))
2514 if (g_ascii_strncasecmp(buf, "MIME-Version:", 13) == 0 ||
2515 g_ascii_strncasecmp(buf, "Content-Type:", 13) == 0 ||
2516 g_ascii_strncasecmp(buf, "Content-Transfer-Encoding:", 26) == 0 ||
2517 g_ascii_strncasecmp(buf, "Content-Description:", 20) == 0 ||
2518 g_ascii_strncasecmp(buf, "Content-ID:", 11) == 0 ||
2519 g_ascii_strncasecmp(buf, "Content-Location:", 17) == 0 ||
2520 g_ascii_strncasecmp(buf, "Content-Disposition:", 20) == 0) {
2525 if (SC_FWRITE(buf, sizeof(gchar), len, fp) < len) {
2526 g_warning("failed to dump %zd bytes from file", len);
2527 procmime_fclose(infp);
2532 procmime_fclose(infp);
2535 case MIMECONTENT_MEM:
2536 len = strlen(mimeinfo->data.mem);
2537 if (SC_FWRITE(mimeinfo->data.mem, sizeof(gchar), len, fp) < len) {
2538 g_warning("failed to dump %zd bytes from mem", len);
2547 childnode = mimeinfo->node->children;
2548 if (childnode == NULL)
2551 child = (MimeInfo *) childnode->data;
2552 if (fprintf(fp, "MIME-Version: 1.0\n") < 0) {
2553 g_warning("failed to write mime version");
2556 if (procmime_write_mime_header(child, fp) < 0)
2558 return procmime_write_mimeinfo(child, fp);
2561 static gint procmime_write_multipart(MimeInfo *mimeinfo, FILE *fp)
2565 gchar *boundary, *str, *str2;
2566 gchar buf[BUFFSIZE];
2567 gboolean firstboundary;
2570 debug_print("procmime_write_multipart\n");
2572 boundary = g_hash_table_lookup(mimeinfo->typeparameters, "boundary");
2574 switch (mimeinfo->content) {
2575 case MIMECONTENT_FILE:
2576 if ((infp = procmime_fopen(mimeinfo->data.filename, "rb")) == NULL) {
2577 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
2580 if (fseek(infp, mimeinfo->offset, SEEK_SET) < 0) {
2581 FILE_OP_ERROR(mimeinfo->data.filename, "fseek");
2582 procmime_fclose(infp);
2585 while (SC_FGETS(buf, sizeof(buf), infp) == buf) {
2586 if (IS_BOUNDARY(buf, boundary, strlen(boundary)))
2589 if (SC_FWRITE(buf, sizeof(gchar), len, fp) < len) {
2590 g_warning("failed to write %zd", len);
2591 procmime_fclose(infp);
2595 procmime_fclose(infp);
2598 case MIMECONTENT_MEM:
2599 str = g_strdup(mimeinfo->data.mem);
2600 if (((str2 = strstr(str, boundary)) != NULL) && ((str2 - str) >= 2) &&
2601 (*(str2 - 1) == '-') && (*(str2 - 2) == '-'))
2604 if (SC_FWRITE(str, sizeof(gchar), len, fp) < len) {
2605 g_warning("failed to write %zd from mem", len);
2616 childnode = mimeinfo->node->children;
2617 firstboundary = TRUE;
2618 while (childnode != NULL) {
2619 MimeInfo *child = childnode->data;
2622 firstboundary = FALSE;
2624 TRY(fprintf(fp, "\n") >= 0);
2626 TRY(fprintf(fp, "--%s\n", boundary) >= 0);
2628 if (procmime_write_mime_header(child, fp) < 0)
2630 if (procmime_write_mimeinfo(child, fp) < 0)
2633 childnode = g_node_next_sibling(childnode);
2635 TRY(fprintf(fp, "\n--%s--\n", boundary) >= 0);
2640 gint procmime_write_mimeinfo(MimeInfo *mimeinfo, FILE *fp)
2644 debug_print("procmime_write_mimeinfo\n");
2646 if (G_NODE_IS_LEAF(mimeinfo->node)) {
2647 switch (mimeinfo->content) {
2648 case MIMECONTENT_FILE:
2649 if ((infp = procmime_fopen(mimeinfo->data.filename, "rb")) == NULL) {
2650 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
2653 copy_file_part_to_fp(infp, mimeinfo->offset, mimeinfo->length, fp);
2654 procmime_fclose(infp);
2657 case MIMECONTENT_MEM:
2658 len = strlen(mimeinfo->data.mem);
2659 if (SC_FWRITE(mimeinfo->data.mem, sizeof(gchar), len, fp) < len)
2667 /* Call writer for mime type */
2668 switch (mimeinfo->type) {
2669 case MIMETYPE_MESSAGE:
2670 if (g_ascii_strcasecmp(mimeinfo->subtype, "rfc822") == 0) {
2671 return procmime_write_message_rfc822(mimeinfo, fp);
2675 case MIMETYPE_MULTIPART:
2676 return procmime_write_multipart(mimeinfo, fp);
2688 gchar *procmime_get_part_file_name(MimeInfo *mimeinfo)
2692 if ((mimeinfo->type == MIMETYPE_TEXT) && !g_ascii_strcasecmp(mimeinfo->subtype, "html"))
2693 base = g_strdup("mimetmp.html");
2695 const gchar *basetmp;
2698 basetmp = procmime_mimeinfo_get_parameter(mimeinfo, "filename");
2699 if (basetmp == NULL)
2700 basetmp = procmime_mimeinfo_get_parameter(mimeinfo, "name");
2701 if (basetmp == NULL)
2702 basetmp = "mimetmp";
2703 basename = g_path_get_basename(basetmp);
2704 if (*basename == '\0') {
2706 basename = g_strdup("mimetmp");
2708 base = conv_filename_from_utf8(basename);
2710 subst_for_shellsafe_filename(base);