2 * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2016 Hiroyuki Yamamoto & The Claws Mail Team
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "claws-features.h"
29 #include <glib/gi18n.h>
35 #include <sys/types.h>
41 #include "procheader.h"
42 #include "quoted-printable.h"
49 #include "prefs_common.h"
50 #include "prefs_gtk.h"
51 #include "alertpanel.h"
57 static GHashTable *procmime_get_mime_type_table (void);
58 static MimeInfo *procmime_scan_file_short(const gchar *filename);
59 static MimeInfo *procmime_scan_queue_file_short(const gchar *filename);
60 static MimeInfo *procmime_scan_queue_file_full(const gchar *filename, gboolean short_scan);
62 MimeInfo *procmime_mimeinfo_new(void)
66 mimeinfo = g_new0(MimeInfo, 1);
68 mimeinfo->content = MIMECONTENT_EMPTY;
69 mimeinfo->data.filename = NULL;
71 mimeinfo->type = MIMETYPE_UNKNOWN;
72 mimeinfo->encoding_type = ENC_UNKNOWN;
73 mimeinfo->typeparameters = g_hash_table_new(g_str_hash, g_str_equal);
75 mimeinfo->disposition = DISPOSITIONTYPE_UNKNOWN;
76 mimeinfo->dispositionparameters
77 = g_hash_table_new(g_str_hash, g_str_equal);
79 mimeinfo->node = g_node_new(mimeinfo);
84 static gboolean procmime_mimeinfo_parameters_destroy(gpointer key, gpointer value, gpointer user_data)
92 static gchar *forced_charset = NULL;
94 void procmime_force_charset(const gchar *str)
96 g_free(forced_charset);
97 forced_charset = NULL;
99 forced_charset = g_strdup(str);
102 static EncodingType forced_encoding = 0;
104 void procmime_force_encoding(EncodingType encoding)
106 forced_encoding = encoding;
109 static gboolean free_func(GNode *node, gpointer data)
111 MimeInfo *mimeinfo = (MimeInfo *) node->data;
113 switch (mimeinfo->content) {
114 case MIMECONTENT_FILE:
116 claws_unlink(mimeinfo->data.filename);
117 g_free(mimeinfo->data.filename);
120 case MIMECONTENT_MEM:
122 g_free(mimeinfo->data.mem);
127 g_free(mimeinfo->subtype);
128 g_free(mimeinfo->description);
129 g_free(mimeinfo->id);
130 g_free(mimeinfo->location);
132 g_hash_table_foreach_remove(mimeinfo->typeparameters,
133 procmime_mimeinfo_parameters_destroy, NULL);
134 g_hash_table_destroy(mimeinfo->typeparameters);
135 g_hash_table_foreach_remove(mimeinfo->dispositionparameters,
136 procmime_mimeinfo_parameters_destroy, NULL);
137 g_hash_table_destroy(mimeinfo->dispositionparameters);
139 if (mimeinfo->privacy)
140 privacy_free_privacydata(mimeinfo->privacy);
147 void procmime_mimeinfo_free_all(MimeInfo **mimeinfo_ptr)
149 MimeInfo *mimeinfo = *mimeinfo_ptr;
155 node = mimeinfo->node;
156 g_node_traverse(node, G_IN_ORDER, G_TRAVERSE_ALL, -1, free_func, NULL);
158 g_node_destroy(node);
160 *mimeinfo_ptr = NULL;
163 MimeInfo *procmime_mimeinfo_parent(MimeInfo *mimeinfo)
165 cm_return_val_if_fail(mimeinfo != NULL, NULL);
166 cm_return_val_if_fail(mimeinfo->node != NULL, NULL);
168 if (mimeinfo->node->parent == NULL)
170 return (MimeInfo *) mimeinfo->node->parent->data;
173 MimeInfo *procmime_mimeinfo_next(MimeInfo *mimeinfo)
175 cm_return_val_if_fail(mimeinfo != NULL, NULL);
176 cm_return_val_if_fail(mimeinfo->node != NULL, NULL);
178 if (mimeinfo->node->children)
179 return (MimeInfo *) mimeinfo->node->children->data;
180 if (mimeinfo->node->next)
181 return (MimeInfo *) mimeinfo->node->next->data;
183 if (mimeinfo->node->parent == NULL)
186 while (mimeinfo->node->parent != NULL) {
187 mimeinfo = (MimeInfo *) mimeinfo->node->parent->data;
188 if (mimeinfo->node->next)
189 return (MimeInfo *) mimeinfo->node->next->data;
195 MimeInfo *procmime_scan_message(MsgInfo *msginfo)
200 filename = procmsg_get_message_file_path(msginfo);
201 if (!filename || !is_file_exist(filename)) {
206 if (!folder_has_parent_of_type(msginfo->folder, F_QUEUE) &&
207 !folder_has_parent_of_type(msginfo->folder, F_DRAFT))
208 mimeinfo = procmime_scan_file(filename);
210 mimeinfo = procmime_scan_queue_file(filename);
216 MimeInfo *procmime_scan_message_short(MsgInfo *msginfo)
221 filename = procmsg_get_message_file_path(msginfo);
222 if (!filename || !is_file_exist(filename)) {
227 if (!folder_has_parent_of_type(msginfo->folder, F_QUEUE) &&
228 !folder_has_parent_of_type(msginfo->folder, F_DRAFT))
229 mimeinfo = procmime_scan_file_short(filename);
231 mimeinfo = procmime_scan_queue_file_short(filename);
239 H_CONTENT_TRANSFER_ENCODING = 0,
241 H_CONTENT_DISPOSITION = 2,
242 H_CONTENT_DESCRIPTION = 3,
246 const gchar *procmime_mimeinfo_get_parameter(MimeInfo *mimeinfo, const gchar *name)
250 cm_return_val_if_fail(mimeinfo != NULL, NULL);
251 cm_return_val_if_fail(name != NULL, NULL);
253 value = g_hash_table_lookup(mimeinfo->dispositionparameters, name);
255 value = g_hash_table_lookup(mimeinfo->typeparameters, name);
260 #define FLUSH_LASTLINE() { \
261 if (*lastline != '\0') { \
263 strretchomp(lastline); \
264 llen = strlen(lastline); \
265 if (lastline[llen-1] == ' ' && !account_signatures_matchlist_str_found(lastline, "%s") && \
266 !(llen == 2 && lastline[1] == ' ' && strchr(prefs_common.quote_chars, lastline[0]))) { \
267 /* this is flowed */ \
269 lastline[llen-1] = '\0'; \
270 if (claws_fputs(lastline, outfp) == EOF) \
273 if (claws_fputs(lastline, outfp) == EOF) \
275 if (claws_fputs("\n", outfp) == EOF) \
279 strcpy(lastline, buf); \
282 gboolean procmime_decode_content(MimeInfo *mimeinfo)
289 gboolean tmp_file = FALSE;
290 gboolean flowed = FALSE;
291 gboolean delsp = FALSE;
292 gboolean err = FALSE;
296 cm_return_val_if_fail(mimeinfo != NULL, FALSE);
298 EncodingType encoding = forced_encoding
300 : mimeinfo->encoding_type;
301 gchar lastline[BUFFSIZE];
302 memset(lastline, 0, BUFFSIZE);
304 if (prefs_common.respect_flowed_format &&
305 mimeinfo->type == MIMETYPE_TEXT &&
306 !strcasecmp(mimeinfo->subtype, "plain")) {
307 if (procmime_mimeinfo_get_parameter(mimeinfo, "format") != NULL &&
308 !strcasecmp(procmime_mimeinfo_get_parameter(mimeinfo, "format"),"flowed"))
311 procmime_mimeinfo_get_parameter(mimeinfo, "delsp") != NULL &&
312 !strcasecmp(procmime_mimeinfo_get_parameter(mimeinfo, "delsp"),"yes"))
317 encoding == ENC_UNKNOWN ||
318 encoding == ENC_BINARY ||
319 encoding == ENC_7BIT ||
324 if (mimeinfo->type == MIMETYPE_MULTIPART || mimeinfo->type == MIMETYPE_MESSAGE)
327 if (mimeinfo->data.filename == NULL)
330 infp = claws_fopen(mimeinfo->data.filename, "rb");
332 FILE_OP_ERROR(mimeinfo->data.filename, "claws_fopen");
335 if (fseek(infp, mimeinfo->offset, SEEK_SET) < 0) {
336 FILE_OP_ERROR(mimeinfo->data.filename, "fseek");
341 outfp = get_tmpfile_in_dir(get_mime_tmp_dir(), &tmpfilename);
349 readend = mimeinfo->offset + mimeinfo->length;
351 account_signatures_matchlist_create(); /* FLUSH_LASTLINE will use it */
354 if (encoding == ENC_QUOTED_PRINTABLE) {
355 while ((ftell(infp) < readend) && (claws_fgets(buf, sizeof(buf), infp) != NULL)) {
357 len = qp_decode_line(buf);
360 if (claws_fwrite(buf, 1, len, outfp) < len)
368 } else if (encoding == ENC_BASE64) {
369 gchar outbuf[BUFFSIZE];
370 gint len, inlen, inread;
371 gboolean got_error = FALSE;
372 gboolean uncanonicalize = FALSE;
374 gboolean null_bytes = FALSE;
375 gboolean starting = TRUE;
377 if (mimeinfo->type == MIMETYPE_TEXT ||
378 mimeinfo->type == MIMETYPE_MESSAGE) {
379 uncanonicalize = TRUE;
380 tmpfp = my_tmpfile();
382 perror("my_tmpfile");
391 while ((inlen = MIN(readend - ftell(infp), sizeof(buf))) > 0 && !err) {
392 inread = claws_fread(buf, 1, inlen, infp);
393 len = g_base64_decode_step(buf, inlen, outbuf, &state, &save);
394 if (uncanonicalize == TRUE && strlen(outbuf) < len && starting) {
395 uncanonicalize = FALSE;
399 if (((inread != inlen) || len < 0) && !got_error) {
400 g_warning("Bad BASE64 content.");
401 if (claws_fwrite(_("[Error decoding BASE64]\n"),
403 strlen(_("[Error decoding BASE64]\n")),
404 tmpfp) < strlen(_("[Error decoding BASE64]\n")))
405 g_warning("error decoding BASE64");
408 } else if (len >= 0) {
409 /* print out the error message only once
412 /* we won't uncanonicalize, output to outfp directly */
413 if (claws_fwrite(outbuf, sizeof(gchar), len, outfp) < len)
416 if (claws_fwrite(outbuf, sizeof(gchar), len, tmpfp) < len)
423 if (uncanonicalize) {
425 while (claws_fgets(buf, sizeof(buf), tmpfp) != NULL) {
427 if (claws_fputs(buf, outfp) == EOF)
433 } else if (encoding == ENC_X_UUENCODE) {
434 gchar outbuf[BUFFSIZE];
436 gboolean flag = FALSE;
438 while ((ftell(infp) < readend) && (claws_fgets(buf, sizeof(buf), infp) != NULL)) {
439 if (!flag && strncmp(buf,"begin ", 6)) continue;
442 len = fromuutobits(outbuf, buf);
445 g_warning("Bad UUENCODE content (%d)", len);
448 if (claws_fwrite(outbuf, sizeof(gchar), len, outfp) < len)
454 while ((ftell(infp) < readend) && (claws_fgets(buf, sizeof(buf), infp) != NULL)) {
456 if (claws_fputs(buf, outfp) == EOF)
465 g_warning("write error");
471 account_signatures_matchlist_delete();
477 if (g_stat(tmpfilename, &statbuf) < 0) {
478 FILE_OP_ERROR(tmpfilename, "stat");
483 claws_unlink(mimeinfo->data.filename);
484 g_free(mimeinfo->data.filename);
485 mimeinfo->data.filename = tmpfilename;
486 mimeinfo->tmp = TRUE;
487 mimeinfo->offset = 0;
488 mimeinfo->length = statbuf.st_size;
489 mimeinfo->encoding_type = ENC_BINARY;
494 #define B64_LINE_SIZE 57
495 #define B64_BUFFSIZE 77
497 gboolean procmime_encode_content(MimeInfo *mimeinfo, EncodingType encoding)
499 FILE *infp = NULL, *outfp;
503 gboolean err = FALSE;
505 if (mimeinfo->content == MIMECONTENT_EMPTY)
508 if (mimeinfo->encoding_type != ENC_UNKNOWN &&
509 mimeinfo->encoding_type != ENC_BINARY &&
510 mimeinfo->encoding_type != ENC_7BIT &&
511 mimeinfo->encoding_type != ENC_8BIT)
512 if(!procmime_decode_content(mimeinfo))
515 outfp = get_tmpfile_in_dir(get_mime_tmp_dir(), &tmpfilename);
521 if (mimeinfo->content == MIMECONTENT_FILE && mimeinfo->data.filename) {
522 if ((infp = claws_fopen(mimeinfo->data.filename, "rb")) == NULL) {
523 g_warning("Can't open file %s", mimeinfo->data.filename);
527 } else if (mimeinfo->content == MIMECONTENT_MEM) {
528 infp = str_open_as_stream(mimeinfo->data.mem);
535 g_warning("Unknown mimeinfo");
539 if (encoding == ENC_BASE64) {
540 gchar inbuf[B64_LINE_SIZE], *out;
542 gchar *tmp_file = NULL;
544 if (mimeinfo->type == MIMETYPE_TEXT ||
545 mimeinfo->type == MIMETYPE_MESSAGE) {
546 if (mimeinfo->content == MIMECONTENT_FILE) {
547 tmp_file = get_tmp_file();
548 if (canonicalize_file(mimeinfo->data.filename, tmp_file) < 0) {
554 if ((tmp_fp = claws_fopen(tmp_file, "rb")) == NULL) {
555 FILE_OP_ERROR(tmp_file, "claws_fopen");
556 claws_unlink(tmp_file);
563 gchar *out = canonicalize_str(mimeinfo->data.mem);
565 infp = str_open_as_stream(out);
575 while ((len = claws_fread(inbuf, sizeof(gchar),
576 B64_LINE_SIZE, tmp_fp))
578 out = g_base64_encode(inbuf, B64_LINE_SIZE);
579 if (claws_fputs(out, outfp) == EOF)
582 if (claws_fputc('\n', outfp) == EOF)
585 if (len > 0 && claws_feof(tmp_fp)) {
586 out = g_base64_encode(inbuf, len);
587 if (claws_fputs(out, outfp) == EOF)
590 if (claws_fputc('\n', outfp) == EOF)
595 claws_fclose(tmp_fp);
596 claws_unlink(tmp_file);
599 } else if (encoding == ENC_QUOTED_PRINTABLE) {
600 gchar inbuf[BUFFSIZE], outbuf[BUFFSIZE * 4];
602 while (claws_fgets(inbuf, sizeof(inbuf), infp) != NULL) {
603 qp_encode_line(outbuf, inbuf);
605 if (!strncmp("From ", outbuf, sizeof("From ")-1)) {
606 gchar *tmpbuf = outbuf;
608 tmpbuf += sizeof("From ")-1;
610 if (claws_fputs("=46rom ", outfp) == EOF)
612 if (claws_fputs(tmpbuf, outfp) == EOF)
615 if (claws_fputs(outbuf, outfp) == EOF)
622 while (claws_fgets(buf, sizeof(buf), infp) != NULL) {
624 if (claws_fputs(buf, outfp) == EOF)
635 if (mimeinfo->content == MIMECONTENT_FILE) {
636 if (mimeinfo->tmp && (mimeinfo->data.filename != NULL))
637 claws_unlink(mimeinfo->data.filename);
638 g_free(mimeinfo->data.filename);
639 } else if (mimeinfo->content == MIMECONTENT_MEM) {
640 if (mimeinfo->tmp && (mimeinfo->data.mem != NULL))
641 g_free(mimeinfo->data.mem);
644 if (g_stat(tmpfilename, &statbuf) < 0) {
645 FILE_OP_ERROR(tmpfilename, "stat");
648 mimeinfo->content = MIMECONTENT_FILE;
649 mimeinfo->data.filename = tmpfilename;
650 mimeinfo->tmp = TRUE;
651 mimeinfo->offset = 0;
652 mimeinfo->length = statbuf.st_size;
653 mimeinfo->encoding_type = encoding;
658 static gint procmime_get_part_to_stream(FILE *outfp, MimeInfo *mimeinfo)
662 gint restlength, readlength;
663 gint saved_errno = 0;
665 cm_return_val_if_fail(outfp != NULL, -1);
666 cm_return_val_if_fail(mimeinfo != NULL, -1);
668 if (mimeinfo->encoding_type != ENC_BINARY && !procmime_decode_content(mimeinfo))
671 if ((infp = claws_fopen(mimeinfo->data.filename, "rb")) == NULL) {
673 FILE_OP_ERROR(mimeinfo->data.filename, "claws_fopen");
674 return -(saved_errno);
676 if (fseek(infp, mimeinfo->offset, SEEK_SET) < 0) {
678 FILE_OP_ERROR(mimeinfo->data.filename, "fseek");
680 return -(saved_errno);
683 restlength = mimeinfo->length;
685 while ((restlength > 0) && ((readlength = claws_fread(buf, 1, restlength > BUFFSIZE ? BUFFSIZE : restlength, infp)) > 0)) {
686 if (claws_fwrite(buf, 1, readlength, outfp) != readlength) {
689 return -(saved_errno);
691 restlength -= readlength;
700 gint procmime_get_part(const gchar *outfile, MimeInfo *mimeinfo)
704 gint saved_errno = 0;
706 cm_return_val_if_fail(outfile != NULL, -1);
708 if ((outfp = claws_fopen(outfile, "wb")) == NULL) {
710 FILE_OP_ERROR(outfile, "claws_fopen");
711 return -(saved_errno);
714 result = procmime_get_part_to_stream(outfp, mimeinfo);
716 if (claws_fclose(outfp) == EOF) {
718 FILE_OP_ERROR(outfile, "claws_fclose");
719 claws_unlink(outfile);
720 return -(saved_errno);
726 gboolean procmime_scan_text_content(MimeInfo *mimeinfo,
727 gboolean (*scan_callback)(const gchar *str, gpointer cb_data),
731 const gchar *src_codeset;
732 gboolean conv_fail = FALSE;
735 gboolean scan_ret = FALSE;
736 gchar *tmpfile = NULL;
739 cm_return_val_if_fail(mimeinfo != NULL, TRUE);
740 cm_return_val_if_fail(scan_callback != NULL, TRUE);
742 if (!procmime_decode_content(mimeinfo))
746 tmpfp = fmemopen(NULL, mimeinfo->length * 2, "w+");
748 tmpfile = procmime_get_tmp_file_name(mimeinfo);
749 if (tmpfile == NULL) {
750 g_warning("no filename\n");
754 tmpfp = claws_fopen(tmpfile, "w+");
759 FILE_OP_ERROR(tmpfile, "open");
763 if ((r = procmime_get_part_to_stream(tmpfp, mimeinfo)) < 0) {
764 g_warning("procmime_get_part_to_stream error %d\n", r);
769 src_codeset = forced_charset
771 procmime_mimeinfo_get_parameter(mimeinfo, "charset");
773 /* use supersets transparently when possible */
774 if (!forced_charset && src_codeset && !strcasecmp(src_codeset, CS_ISO_8859_1))
775 src_codeset = CS_WINDOWS_1252;
776 else if (!forced_charset && src_codeset && !strcasecmp(src_codeset, CS_X_GBK))
777 src_codeset = CS_GB18030;
778 else if (!forced_charset && src_codeset && !strcasecmp(src_codeset, CS_GBK))
779 src_codeset = CS_GB18030;
780 else if (!forced_charset && src_codeset && !strcasecmp(src_codeset, CS_GB2312))
781 src_codeset = CS_GB18030;
782 else if (!forced_charset && src_codeset && !strcasecmp(src_codeset, CS_X_VIET_VPS))
783 src_codeset = CS_WINDOWS_874;
785 if (mimeinfo->type == MIMETYPE_TEXT && !g_ascii_strcasecmp(mimeinfo->subtype, "html")) {
786 SC_HTMLParser *parser;
789 conv = conv_code_converter_new(src_codeset);
790 parser = sc_html_parser_new(tmpfp, conv);
791 while ((str = sc_html_parse(parser)) != NULL) {
792 if ((scan_ret = scan_callback(str, cb_data)) == TRUE)
795 sc_html_parser_destroy(parser);
796 conv_code_converter_destroy(conv);
797 } else if (mimeinfo->type == MIMETYPE_TEXT && !g_ascii_strcasecmp(mimeinfo->subtype, "enriched")) {
801 conv = conv_code_converter_new(src_codeset);
802 parser = ertf_parser_new(tmpfp, conv);
803 while ((str = ertf_parse(parser)) != NULL) {
804 if ((scan_ret = scan_callback(str, cb_data)) == TRUE)
807 ertf_parser_destroy(parser);
808 conv_code_converter_destroy(conv);
809 } else if (mimeinfo->type == MIMETYPE_TEXT && mimeinfo->disposition != DISPOSITIONTYPE_ATTACHMENT) {
810 while (claws_fgets(buf, sizeof(buf), tmpfp) != NULL) {
811 str = conv_codeset_strdup(buf, src_codeset, CS_UTF_8);
813 if ((scan_ret = scan_callback(str, cb_data)) == TRUE) {
820 if ((scan_ret = scan_callback(buf, cb_data)) == TRUE)
827 g_warning("procmime_get_text_content(): Code conversion failed.");
832 claws_unlink(tmpfile);
839 static gboolean scan_fputs_cb(const gchar *str, gpointer fp)
841 if (claws_fputs(str, (FILE *)fp) == EOF)
847 FILE *procmime_get_text_content(MimeInfo *mimeinfo)
852 if ((outfp = my_tmpfile()) == NULL) {
853 perror("my_tmpfile");
857 err = procmime_scan_text_content(mimeinfo, scan_fputs_cb, outfp);
868 FILE *procmime_get_binary_content(MimeInfo *mimeinfo)
872 gchar *tmpfile = NULL;
875 cm_return_val_if_fail(mimeinfo != NULL, NULL);
877 if (!procmime_decode_content(mimeinfo))
881 outfp = fmemopen(NULL, mimeinfo->length * 2, "w+");
883 tmpfile = procmime_get_tmp_file_name(mimeinfo);
884 if (tmpfile == NULL) {
885 g_warning("no filename\n");
889 outfp = procmime_fopen(tmpfile, "w+");
895 if (procmime_get_part_to_stream(outfp, mimeinfo) < 0) {
902 /* search the first text part of (multipart) MIME message,
903 decode, convert it and output to outfp. */
904 FILE *procmime_get_first_text_content(MsgInfo *msginfo)
907 MimeInfo *mimeinfo, *partinfo;
908 gboolean empty_ok = FALSE, short_scan = TRUE;
910 cm_return_val_if_fail(msginfo != NULL, NULL);
912 /* first we try to short-scan (for speed), refusing empty parts */
915 mimeinfo = procmime_scan_message_short(msginfo);
917 mimeinfo = procmime_scan_message(msginfo);
918 if (!mimeinfo) return NULL;
921 while (partinfo && (partinfo->type != MIMETYPE_TEXT ||
922 (partinfo->length == 0 && !empty_ok))) {
923 partinfo = procmime_mimeinfo_next(partinfo);
926 outfp = procmime_get_text_content(partinfo);
927 else if (!empty_ok && short_scan) {
928 /* if short scan didn't find a non-empty part, rescan
929 * fully for non-empty parts
932 procmime_mimeinfo_free_all(&mimeinfo);
934 } else if (!empty_ok && !short_scan) {
935 /* if full scan didn't find a non-empty part, rescan
936 * accepting empty parts
939 procmime_mimeinfo_free_all(&mimeinfo);
942 procmime_mimeinfo_free_all(&mimeinfo);
948 static gboolean find_encrypted_func(GNode *node, gpointer data)
950 MimeInfo *mimeinfo = (MimeInfo *) node->data;
951 MimeInfo **encinfo = (MimeInfo **) data;
953 if (privacy_mimeinfo_is_encrypted(mimeinfo)) {
961 static MimeInfo *find_encrypted_part(MimeInfo *rootinfo)
963 MimeInfo *encinfo = NULL;
965 g_node_traverse(rootinfo->node, G_IN_ORDER, G_TRAVERSE_ALL, -1,
966 find_encrypted_func, &encinfo);
971 /* search the first encrypted text part of (multipart) MIME message,
972 decode, convert it and output to outfp. */
973 FILE *procmime_get_first_encrypted_text_content(MsgInfo *msginfo)
976 MimeInfo *mimeinfo, *partinfo, *encinfo;
978 cm_return_val_if_fail(msginfo != NULL, NULL);
980 mimeinfo = procmime_scan_message(msginfo);
986 if ((encinfo = find_encrypted_part(partinfo)) != NULL) {
987 debug_print("decrypting message part\n");
988 if (privacy_mimeinfo_decrypt(encinfo) < 0) {
989 alertpanel_error(_("Couldn't decrypt: %s"),
990 privacy_get_error());
995 while (partinfo && partinfo->type != MIMETYPE_TEXT) {
996 partinfo = procmime_mimeinfo_next(partinfo);
997 if (privacy_mimeinfo_is_signed(partinfo))
998 procmsg_msginfo_set_flags(msginfo, 0, MSG_SIGNED);
1002 outfp = procmime_get_text_content(partinfo);
1004 procmime_mimeinfo_free_all(&mimeinfo);
1009 gboolean procmime_msginfo_is_encrypted(MsgInfo *msginfo)
1011 MimeInfo *mimeinfo, *partinfo;
1012 gboolean result = FALSE;
1014 cm_return_val_if_fail(msginfo != NULL, FALSE);
1016 mimeinfo = procmime_scan_message(msginfo);
1021 partinfo = mimeinfo;
1022 result = (find_encrypted_part(partinfo) != NULL);
1023 procmime_mimeinfo_free_all(&mimeinfo);
1028 gchar *procmime_get_tmp_file_name(MimeInfo *mimeinfo)
1030 static guint32 id = 0;
1035 cm_return_val_if_fail(mimeinfo != NULL, NULL);
1037 g_snprintf(f_prefix, sizeof(f_prefix), "%08x.", id++);
1039 if ((mimeinfo->type == MIMETYPE_TEXT) && !g_ascii_strcasecmp(mimeinfo->subtype, "html"))
1040 base = g_strdup("mimetmp.html");
1042 const gchar *basetmp;
1044 basetmp = procmime_mimeinfo_get_parameter(mimeinfo, "filename");
1045 if (basetmp == NULL)
1046 basetmp = procmime_mimeinfo_get_parameter(mimeinfo, "name");
1047 if (basetmp == NULL)
1048 basetmp = "mimetmp";
1049 basetmp = g_path_get_basename(basetmp);
1050 if (*basetmp == '\0')
1051 basetmp = g_strdup("mimetmp");
1052 base = conv_filename_from_utf8(basetmp);
1053 g_free((gchar*)basetmp);
1054 subst_for_shellsafe_filename(base);
1057 filename = g_strconcat(get_mime_tmp_dir(), G_DIR_SEPARATOR_S,
1058 f_prefix, base, NULL);
1065 static GList *mime_type_list = NULL;
1067 gchar *procmime_get_mime_type(const gchar *filename)
1073 static GHashTable *mime_type_table = NULL;
1074 MimeType *mime_type;
1076 if (!mime_type_table) {
1077 mime_type_table = procmime_get_mime_type_table();
1078 if (!mime_type_table) return NULL;
1082 if (filename == NULL)
1085 base = g_path_get_basename(filename);
1086 if ((p = strrchr(base, '.')) != NULL)
1087 ext = g_utf8_strdown(p + 1, -1);
1089 ext = g_utf8_strdown(base, -1);
1093 mime_type = g_hash_table_lookup(mime_type_table, ext);
1097 str = g_strconcat(mime_type->type, "/", mime_type->sub_type,
1099 debug_print("got type %s for %s\n", str, ext);
1106 gchar *str = get_content_type_from_registry_with_ext(ext);
1113 static guint procmime_str_hash(gconstpointer gptr)
1115 guint hash_result = 0;
1118 for (str = gptr; str && *str; str++) {
1119 if (isupper(*str)) hash_result += (*str + ' ');
1120 else hash_result += *str;
1126 static gint procmime_str_equal(gconstpointer gptr1, gconstpointer gptr2)
1128 const char *str1 = gptr1;
1129 const char *str2 = gptr2;
1131 return !g_utf8_collate(str1, str2);
1134 static GHashTable *procmime_get_mime_type_table(void)
1136 GHashTable *table = NULL;
1138 MimeType *mime_type;
1141 if (!mime_type_list) {
1142 mime_type_list = procmime_get_mime_type_list();
1143 if (!mime_type_list) return NULL;
1146 table = g_hash_table_new(procmime_str_hash, procmime_str_equal);
1148 for (cur = mime_type_list; cur != NULL; cur = cur->next) {
1152 mime_type = (MimeType *)cur->data;
1154 if (!mime_type->extension) continue;
1156 exts = g_strsplit(mime_type->extension, " ", 16);
1157 for (i = 0; exts[i] != NULL; i++) {
1158 /* Don't overwrite previously inserted extension */
1159 if (!g_hash_table_lookup(table, exts[i])) {
1160 key = g_strdup(exts[i]);
1161 g_hash_table_insert(table, key, mime_type);
1170 GList *procmime_get_mime_type_list(void)
1174 gchar buf[BUFFSIZE];
1177 MimeType *mime_type;
1178 gboolean fp_is_glob_file = TRUE;
1181 return mime_type_list;
1183 #if defined(__NetBSD__) || defined(__OpenBSD__) || defined(__FreeBSD__)
1184 if ((fp = claws_fopen(DATAROOTDIR "/mime/globs", "rb")) == NULL)
1186 if ((fp = claws_fopen("/usr/share/mime/globs", "rb")) == NULL)
1189 fp_is_glob_file = FALSE;
1190 if ((fp = claws_fopen("/etc/mime.types", "rb")) == NULL) {
1191 if ((fp = claws_fopen(SYSCONFDIR "/mime.types", "rb"))
1193 FILE_OP_ERROR(SYSCONFDIR "/mime.types",
1200 while (claws_fgets(buf, sizeof(buf), fp) != NULL) {
1201 p = strchr(buf, '#');
1207 if (fp_is_glob_file) {
1208 while (*p && !g_ascii_isspace(*p) && (*p!=':')) p++;
1210 while (*p && !g_ascii_isspace(*p)) p++;
1217 delim = strchr(buf, '/');
1218 if (delim == NULL) continue;
1221 mime_type = g_new(MimeType, 1);
1222 mime_type->type = g_strdup(buf);
1223 mime_type->sub_type = g_strdup(delim + 1);
1225 if (fp_is_glob_file) {
1226 while (*p && (g_ascii_isspace(*p)||(*p=='*')||(*p=='.'))) p++;
1228 while (*p && g_ascii_isspace(*p)) p++;
1232 mime_type->extension = g_utf8_strdown(p, -1);
1234 mime_type->extension = NULL;
1236 list = g_list_append(list, mime_type);
1242 g_warning("Can't read mime.types");
1247 EncodingType procmime_get_encoding_for_charset(const gchar *charset)
1251 else if (!g_ascii_strncasecmp(charset, "ISO-2022-", 9) ||
1252 !g_ascii_strcasecmp(charset, "US-ASCII"))
1254 else if (!g_ascii_strcasecmp(charset, "ISO-8859-5") ||
1255 !g_ascii_strncasecmp(charset, "KOI8-", 5) ||
1256 !g_ascii_strcasecmp(charset, "X-MAC-CYRILLIC") ||
1257 !g_ascii_strcasecmp(charset, "MAC-CYRILLIC") ||
1258 !g_ascii_strcasecmp(charset, "Windows-1251"))
1260 else if (!g_ascii_strncasecmp(charset, "ISO-8859-", 9))
1261 return ENC_QUOTED_PRINTABLE;
1262 else if (!g_ascii_strncasecmp(charset, "UTF-8", 5))
1263 return ENC_QUOTED_PRINTABLE;
1268 EncodingType procmime_get_encoding_for_text_file(const gchar *file, gboolean *has_binary)
1271 guchar buf[BUFFSIZE];
1273 size_t octet_chars = 0;
1274 size_t total_len = 0;
1275 gfloat octet_percentage;
1276 gboolean force_b64 = FALSE;
1278 if ((fp = claws_fopen(file, "rb")) == NULL) {
1279 FILE_OP_ERROR(file, "claws_fopen");
1283 while ((len = claws_fread(buf, sizeof(guchar), sizeof(buf), fp)) > 0) {
1287 for (p = buf, i = 0; i < len; ++p, ++i) {
1301 octet_percentage = (gfloat)octet_chars / (gfloat)total_len;
1303 octet_percentage = 0.0;
1305 debug_print("procmime_get_encoding_for_text_file(): "
1306 "8bit chars: %zd / %zd (%f%%)\n", octet_chars, total_len,
1307 100.0 * octet_percentage);
1309 if (octet_percentage > 0.20 || force_b64) {
1310 debug_print("using BASE64\n");
1312 } else if (octet_chars > 0) {
1313 debug_print("using quoted-printable\n");
1314 return ENC_QUOTED_PRINTABLE;
1316 debug_print("using 7bit\n");
1321 struct EncodingTable
1324 EncodingType enc_type;
1327 struct EncodingTable encoding_table[] = {
1330 {"binary", ENC_BINARY},
1331 {"quoted-printable", ENC_QUOTED_PRINTABLE},
1332 {"base64", ENC_BASE64},
1333 {"x-uuencode", ENC_UNKNOWN},
1334 {NULL, ENC_UNKNOWN},
1337 const gchar *procmime_get_encoding_str(EncodingType encoding)
1339 struct EncodingTable *enc_table;
1341 for (enc_table = encoding_table; enc_table->str != NULL; enc_table++) {
1342 if (enc_table->enc_type == encoding)
1343 return enc_table->str;
1348 /* --- NEW MIME STUFF --- */
1355 static struct TypeTable mime_type_table[] = {
1356 {"text", MIMETYPE_TEXT},
1357 {"image", MIMETYPE_IMAGE},
1358 {"audio", MIMETYPE_AUDIO},
1359 {"video", MIMETYPE_VIDEO},
1360 {"application", MIMETYPE_APPLICATION},
1361 {"message", MIMETYPE_MESSAGE},
1362 {"multipart", MIMETYPE_MULTIPART},
1366 const gchar *procmime_get_media_type_str(MimeMediaType type)
1368 struct TypeTable *type_table;
1370 for (type_table = mime_type_table; type_table->str != NULL; type_table++) {
1371 if (type_table->type == type)
1372 return type_table->str;
1377 MimeMediaType procmime_get_media_type(const gchar *str)
1379 struct TypeTable *typetablearray;
1381 for (typetablearray = mime_type_table; typetablearray->str != NULL; typetablearray++)
1382 if (g_ascii_strncasecmp(str, typetablearray->str, strlen(typetablearray->str)) == 0)
1383 return typetablearray->type;
1385 return MIMETYPE_UNKNOWN;
1389 *\brief Safe wrapper for content type string.
1391 *\return const gchar * Pointer to content type string.
1393 gchar *procmime_get_content_type_str(MimeMediaType type,
1394 const char *subtype)
1396 const gchar *type_str = NULL;
1398 if (subtype == NULL || !(type_str = procmime_get_media_type_str(type)))
1399 return g_strdup("unknown");
1400 return g_strdup_printf("%s/%s", type_str, subtype);
1403 static int procmime_parse_mimepart(MimeInfo *parent,
1404 gchar *content_type,
1405 gchar *content_encoding,
1406 gchar *content_description,
1408 gchar *content_disposition,
1409 gchar *content_location,
1410 const gchar *original_msgid,
1411 const gchar *disposition_notification_hdr,
1412 const gchar *filename,
1415 gboolean short_scan);
1417 static void procmime_parse_message_rfc822(MimeInfo *mimeinfo, gboolean short_scan)
1419 HeaderEntry hentry[] = {{"Content-Type:", NULL, TRUE},
1420 {"Content-Transfer-Encoding:",
1422 {"Content-Description:",
1426 {"Content-Disposition:",
1428 {"Content-Location:",
1432 {"Original-Message-ID:",
1436 {NULL, NULL, FALSE}};
1437 guint content_start, i;
1442 procmime_decode_content(mimeinfo);
1444 fp = claws_fopen(mimeinfo->data.filename, "rb");
1446 FILE_OP_ERROR(mimeinfo->data.filename, "claws_fopen");
1449 if (fseek(fp, mimeinfo->offset, SEEK_SET) < 0) {
1450 FILE_OP_ERROR(mimeinfo->data.filename, "fseek");
1454 procheader_get_header_fields(fp, hentry);
1455 if (hentry[0].body != NULL) {
1456 tmp = conv_unmime_header(hentry[0].body, NULL, FALSE);
1457 g_free(hentry[0].body);
1458 hentry[0].body = tmp;
1460 if (hentry[2].body != NULL) {
1461 tmp = conv_unmime_header(hentry[2].body, NULL, FALSE);
1462 g_free(hentry[2].body);
1463 hentry[2].body = tmp;
1465 if (hentry[4].body != NULL) {
1466 tmp = conv_unmime_header(hentry[4].body, NULL, FALSE);
1467 g_free(hentry[4].body);
1468 hentry[4].body = tmp;
1470 if (hentry[5].body != NULL) {
1471 tmp = conv_unmime_header(hentry[5].body, NULL, FALSE);
1472 g_free(hentry[5].body);
1473 hentry[5].body = tmp;
1475 if (hentry[7].body != NULL) {
1476 tmp = conv_unmime_header(hentry[7].body, NULL, FALSE);
1477 g_free(hentry[7].body);
1478 hentry[7].body = tmp;
1480 if (hentry[8].body != NULL) {
1481 tmp = conv_unmime_header(hentry[8].body, NULL, FALSE);
1482 g_free(hentry[8].body);
1483 hentry[8].body = tmp;
1486 content_start = ftell(fp);
1489 len = mimeinfo->length - (content_start - mimeinfo->offset);
1492 procmime_parse_mimepart(mimeinfo,
1493 hentry[0].body, hentry[1].body,
1494 hentry[2].body, hentry[3].body,
1495 hentry[4].body, hentry[5].body,
1496 hentry[7].body, hentry[8].body,
1497 mimeinfo->data.filename, content_start,
1500 for (i = 0; i < (sizeof hentry / sizeof hentry[0]); i++) {
1501 g_free(hentry[i].body);
1502 hentry[i].body = NULL;
1506 static void procmime_parse_disposition_notification(MimeInfo *mimeinfo,
1507 const gchar *original_msgid, const gchar *disposition_notification_hdr,
1508 gboolean short_scan)
1510 HeaderEntry hentry[] = {{"Original-Message-ID:", NULL, TRUE},
1511 {"Disposition:", NULL, TRUE},
1512 {NULL, NULL, FALSE}};
1515 gchar *orig_msg_id = NULL;
1518 procmime_decode_content(mimeinfo);
1520 debug_print("parse disposition notification\n");
1521 fp = claws_fopen(mimeinfo->data.filename, "rb");
1523 FILE_OP_ERROR(mimeinfo->data.filename, "claws_fopen");
1526 if (fseek(fp, mimeinfo->offset, SEEK_SET) < 0) {
1527 FILE_OP_ERROR(mimeinfo->data.filename, "fseek");
1532 if (original_msgid && disposition_notification_hdr) {
1533 hentry[0].body = g_strdup(original_msgid);
1534 hentry[1].body = g_strdup(disposition_notification_hdr);
1536 procheader_get_header_fields(fp, hentry);
1541 if (!hentry[0].body || !hentry[1].body) {
1542 debug_print("MsgId %s, Disp %s\n",
1543 hentry[0].body ? hentry[0].body:"(nil)",
1544 hentry[1].body ? hentry[1].body:"(nil)");
1548 orig_msg_id = g_strdup(hentry[0].body);
1549 disp = g_strdup(hentry[1].body);
1551 extract_parenthesis(orig_msg_id, '<', '>');
1552 remove_space(orig_msg_id);
1554 if (strstr(disp, "displayed")) {
1555 /* find sent message, if possible */
1556 MsgInfo *info = NULL;
1558 debug_print("%s has been displayed.\n", orig_msg_id);
1559 for (flist = folder_get_list(); flist != NULL; flist = g_list_next(flist)) {
1560 FolderItem *outbox = ((Folder *)(flist->data))->outbox;
1562 debug_print("skipping folder with no outbox...\n");
1565 info = folder_item_get_msginfo_by_msgid(outbox, orig_msg_id);
1566 debug_print("%s %s in %s\n", info?"found":"didn't find", orig_msg_id, outbox->path);
1568 procmsg_msginfo_set_flags(info, MSG_RETRCPT_GOT, 0);
1569 procmsg_msginfo_free(&info);
1573 g_free(orig_msg_id);
1576 for (i = 0; i < (sizeof hentry / sizeof hentry[0]); i++) {
1577 g_free(hentry[i].body);
1578 hentry[i].body = NULL;
1582 #define GET_HEADERS() { \
1583 procheader_get_header_fields(fp, hentry); \
1584 if (hentry[0].body != NULL) { \
1585 tmp = conv_unmime_header(hentry[0].body, NULL, FALSE); \
1586 g_free(hentry[0].body); \
1587 hentry[0].body = tmp; \
1589 if (hentry[2].body != NULL) { \
1590 tmp = conv_unmime_header(hentry[2].body, NULL, FALSE); \
1591 g_free(hentry[2].body); \
1592 hentry[2].body = tmp; \
1594 if (hentry[4].body != NULL) { \
1595 tmp = conv_unmime_header(hentry[4].body, NULL, FALSE); \
1596 g_free(hentry[4].body); \
1597 hentry[4].body = tmp; \
1599 if (hentry[5].body != NULL) { \
1600 tmp = conv_unmime_header(hentry[5].body, NULL, FALSE); \
1601 g_free(hentry[5].body); \
1602 hentry[5].body = tmp; \
1604 if (hentry[6].body != NULL) { \
1605 tmp = conv_unmime_header(hentry[6].body, NULL, FALSE); \
1606 g_free(hentry[6].body); \
1607 hentry[6].body = tmp; \
1609 if (hentry[7].body != NULL) { \
1610 tmp = conv_unmime_header(hentry[7].body, NULL, FALSE); \
1611 g_free(hentry[7].body); \
1612 hentry[7].body = tmp; \
1616 static void procmime_parse_multipart(MimeInfo *mimeinfo, gboolean short_scan)
1618 HeaderEntry hentry[] = {{"Content-Type:", NULL, TRUE},
1619 {"Content-Transfer-Encoding:",
1621 {"Content-Description:",
1625 {"Content-Disposition:",
1627 {"Content-Location:",
1629 {"Original-Message-ID:",
1633 {NULL, NULL, FALSE}};
1636 gint boundary_len = 0, lastoffset = -1, i;
1637 gchar buf[BUFFSIZE];
1640 gboolean start_found = FALSE;
1641 gboolean end_found = FALSE;
1643 boundary = g_hash_table_lookup(mimeinfo->typeparameters, "boundary");
1646 boundary_len = strlen(boundary);
1648 procmime_decode_content(mimeinfo);
1650 fp = claws_fopen(mimeinfo->data.filename, "rb");
1652 FILE_OP_ERROR(mimeinfo->data.filename, "claws_fopen");
1656 if (fseek(fp, mimeinfo->offset, SEEK_SET) < 0) {
1657 FILE_OP_ERROR(mimeinfo->data.filename, "fseek");
1662 while (claws_fgets(buf, sizeof(buf), fp) != NULL && result == 0) {
1663 if (ftell(fp) - 1 > (mimeinfo->offset + mimeinfo->length))
1666 if (IS_BOUNDARY(buf, boundary, boundary_len)) {
1669 if (lastoffset != -1) {
1670 gint len = (ftell(fp) - strlen(buf)) - lastoffset - 1;
1673 result = procmime_parse_mimepart(mimeinfo,
1674 hentry[0].body, hentry[1].body,
1675 hentry[2].body, hentry[3].body,
1676 hentry[4].body, hentry[5].body,
1677 hentry[6].body, hentry[7].body,
1678 mimeinfo->data.filename, lastoffset,
1680 if (result == 1 && short_scan)
1685 if (buf[2 + boundary_len] == '-' &&
1686 buf[2 + boundary_len + 1] == '-') {
1690 for (i = 0; i < (sizeof hentry / sizeof hentry[0]) ; i++) {
1691 g_free(hentry[i].body);
1692 hentry[i].body = NULL;
1695 lastoffset = ftell(fp);
1699 if (start_found && !end_found && lastoffset != -1) {
1700 gint len = (ftell(fp) - strlen(buf)) - lastoffset - 1;
1703 result = procmime_parse_mimepart(mimeinfo,
1704 hentry[0].body, hentry[1].body,
1705 hentry[2].body, hentry[3].body,
1706 hentry[4].body, hentry[5].body,
1707 hentry[6].body, hentry[7].body,
1708 mimeinfo->data.filename, lastoffset,
1711 mimeinfo->broken = TRUE;
1714 for (i = 0; i < (sizeof hentry / sizeof hentry[0]); i++) {
1715 g_free(hentry[i].body);
1716 hentry[i].body = NULL;
1721 static void parse_parameters(const gchar *parameters, GHashTable *table)
1723 gchar *params, *param, *next;
1724 GSList *convlist = NULL, *concatlist = NULL, *cur;
1726 params = g_strdup(parameters);
1729 for (; next != NULL; param = next) {
1730 gchar *attribute, *value, *tmp, *down_attr, *orig_down_attr;
1732 gboolean convert = FALSE;
1734 next = strchr_with_skip_quote(param, '"', ';');
1743 value = strchr(attribute, '=');
1749 while (value[0] != '\0' && value[0] == ' ')
1752 down_attr = g_utf8_strdown(attribute, -1);
1753 orig_down_attr = down_attr;
1755 len = down_attr ? strlen(down_attr):0;
1756 if (len > 0 && down_attr[len - 1] == '*') {
1757 gchar *srcpos, *dstpos, *endpos;
1760 down_attr[len - 1] = '\0';
1764 endpos = value + strlen(value);
1765 while (srcpos < endpos) {
1771 if (!get_hex_value(&dstvalue, srcpos[1], srcpos[2]))
1781 if (value[0] == '"')
1782 extract_quote(value, '"');
1784 if (value[0] == '"')
1785 extract_quote(value, '"');
1786 else if ((tmp = strchr(value, ' ')) != NULL)
1791 while (down_attr[0] == ' ')
1793 while (down_attr[strlen(down_attr)-1] == ' ')
1794 down_attr[strlen(down_attr)-1] = '\0';
1797 while (value[0] != '\0' && value[0] == ' ')
1799 while (value[strlen(value)-1] == ' ')
1800 value[strlen(value)-1] = '\0';
1802 if (down_attr && strrchr(down_attr, '*') != NULL) {
1805 tmpattr = g_strdup(down_attr);
1806 tmp = strrchr(tmpattr, '*');
1809 if ((tmp[1] == '0') && (tmp[2] == '\0') &&
1810 (g_slist_find_custom(concatlist, down_attr, (GCompareFunc)g_strcmp0) == NULL))
1811 concatlist = g_slist_prepend(concatlist, g_strdup(tmpattr));
1813 if (convert && (g_slist_find_custom(convlist, tmpattr, (GCompareFunc)g_strcmp0) == NULL))
1814 convlist = g_slist_prepend(convlist, g_strdup(tmpattr));
1817 } else if (convert) {
1818 if (g_slist_find_custom(convlist, down_attr, (GCompareFunc)g_strcmp0) == NULL)
1819 convlist = g_slist_prepend(convlist, g_strdup(down_attr));
1822 if (g_hash_table_lookup(table, down_attr) == NULL)
1823 g_hash_table_insert(table, g_strdup(down_attr), g_strdup(value));
1824 g_free(orig_down_attr);
1827 for (cur = concatlist; cur != NULL; cur = g_slist_next(cur)) {
1828 gchar *attribute, *attrwnum, *partvalue;
1832 attribute = (gchar *) cur->data;
1833 value = g_string_sized_new(64);
1835 attrwnum = g_strdup_printf("%s*%d", attribute, n);
1836 while ((partvalue = g_hash_table_lookup(table, attrwnum)) != NULL) {
1837 g_string_append(value, partvalue);
1839 g_hash_table_remove(table, attrwnum);
1842 attrwnum = g_strdup_printf("%s*%d", attribute, n);
1846 g_hash_table_insert(table, g_strdup(attribute), g_strdup(value->str));
1847 g_string_free(value, TRUE);
1849 slist_free_strings_full(concatlist);
1851 for (cur = convlist; cur != NULL; cur = g_slist_next(cur)) {
1852 gchar *attribute, *key, *value;
1853 gchar *charset, *lang, *oldvalue, *newvalue;
1855 attribute = (gchar *) cur->data;
1856 if (!g_hash_table_lookup_extended(
1857 table, attribute, (gpointer *)(gchar *) &key, (gpointer *)(gchar *) &value))
1861 if (charset == NULL)
1863 lang = strchr(charset, '\'');
1868 oldvalue = strchr(lang, '\'');
1869 if (oldvalue == NULL)
1874 newvalue = conv_codeset_strdup(oldvalue, charset, CS_UTF_8);
1876 g_hash_table_remove(table, attribute);
1880 g_hash_table_insert(table, g_strdup(attribute), newvalue);
1882 slist_free_strings_full(convlist);
1887 static void procmime_parse_content_type(const gchar *content_type, MimeInfo *mimeinfo)
1889 cm_return_if_fail(content_type != NULL);
1890 cm_return_if_fail(mimeinfo != NULL);
1892 /* RFC 2045, page 13 says that the mime subtype is MANDATORY;
1893 * if it's not available we use the default Content-Type */
1894 if ((content_type[0] == '\0') || (strchr(content_type, '/') == NULL)) {
1895 mimeinfo->type = MIMETYPE_TEXT;
1896 mimeinfo->subtype = g_strdup("plain");
1897 if (g_hash_table_lookup(mimeinfo->typeparameters,
1898 "charset") == NULL) {
1899 g_hash_table_insert(mimeinfo->typeparameters,
1900 g_strdup("charset"),
1902 conv_get_locale_charset_str_no_utf8()));
1905 gchar *type, *subtype, *params;
1907 type = g_strdup(content_type);
1908 subtype = strchr(type, '/') + 1;
1909 *(subtype - 1) = '\0';
1910 if ((params = strchr(subtype, ';')) != NULL) {
1915 mimeinfo->type = procmime_get_media_type(type);
1916 mimeinfo->subtype = g_strstrip(g_strdup(subtype));
1918 /* Get mimeinfo->typeparameters */
1920 parse_parameters(params, mimeinfo->typeparameters);
1926 static void procmime_parse_content_disposition(const gchar *content_disposition, MimeInfo *mimeinfo)
1928 gchar *tmp, *params;
1930 cm_return_if_fail(content_disposition != NULL);
1931 cm_return_if_fail(mimeinfo != NULL);
1933 tmp = g_strdup(content_disposition);
1934 if ((params = strchr(tmp, ';')) != NULL) {
1940 if (!g_ascii_strcasecmp(tmp, "inline"))
1941 mimeinfo->disposition = DISPOSITIONTYPE_INLINE;
1942 else if (!g_ascii_strcasecmp(tmp, "attachment"))
1943 mimeinfo->disposition = DISPOSITIONTYPE_ATTACHMENT;
1945 mimeinfo->disposition = DISPOSITIONTYPE_ATTACHMENT;
1948 parse_parameters(params, mimeinfo->dispositionparameters);
1954 static void procmime_parse_content_encoding(const gchar *content_encoding, MimeInfo *mimeinfo)
1956 struct EncodingTable *enc_table;
1958 for (enc_table = encoding_table; enc_table->str != NULL; enc_table++) {
1959 if (g_ascii_strcasecmp(enc_table->str, content_encoding) == 0) {
1960 mimeinfo->encoding_type = enc_table->enc_type;
1964 mimeinfo->encoding_type = ENC_UNKNOWN;
1968 static GSList *registered_parsers = NULL;
1970 static MimeParser *procmime_get_mimeparser_for_type(MimeMediaType type, const gchar *sub_type)
1973 for (cur = registered_parsers; cur; cur = cur->next) {
1974 MimeParser *parser = (MimeParser *)cur->data;
1975 if (parser->type == type && !strcmp2(parser->sub_type, sub_type))
1981 void procmime_mimeparser_register(MimeParser *parser)
1983 if (!procmime_get_mimeparser_for_type(parser->type, parser->sub_type))
1984 registered_parsers = g_slist_append(registered_parsers, parser);
1988 void procmime_mimeparser_unregister(MimeParser *parser)
1990 registered_parsers = g_slist_remove(registered_parsers, parser);
1993 static gboolean procmime_mimeparser_parse(MimeParser *parser, MimeInfo *mimeinfo)
1995 cm_return_val_if_fail(parser->parse != NULL, FALSE);
1996 return parser->parse(parser, mimeinfo);
1999 static int procmime_parse_mimepart(MimeInfo *parent,
2000 gchar *content_type,
2001 gchar *content_encoding,
2002 gchar *content_description,
2004 gchar *content_disposition,
2005 gchar *content_location,
2006 const gchar *original_msgid,
2007 const gchar *disposition_notification_hdr,
2008 const gchar *filename,
2011 gboolean short_scan)
2014 MimeParser *parser = NULL;
2015 gboolean parsed = FALSE;
2018 /* Create MimeInfo */
2019 mimeinfo = procmime_mimeinfo_new();
2020 mimeinfo->content = MIMECONTENT_FILE;
2022 if (parent != NULL) {
2023 if (g_node_depth(parent->node) > 32) {
2024 /* 32 is an arbitrary value
2025 * this avoids DOSsing ourselves
2026 * with enormous messages
2028 procmime_mimeinfo_free_all(&mimeinfo);
2031 g_node_append(parent->node, mimeinfo->node);
2033 mimeinfo->data.filename = g_strdup(filename);
2034 mimeinfo->offset = offset;
2035 mimeinfo->length = length;
2037 if (content_type != NULL) {
2038 g_strchomp(content_type);
2039 procmime_parse_content_type(content_type, mimeinfo);
2041 mimeinfo->type = MIMETYPE_TEXT;
2042 mimeinfo->subtype = g_strdup("plain");
2043 if (g_hash_table_lookup(mimeinfo->typeparameters,
2044 "charset") == NULL) {
2045 g_hash_table_insert(mimeinfo->typeparameters,
2046 g_strdup("charset"),
2048 conv_get_locale_charset_str_no_utf8()));
2052 if (content_encoding != NULL) {
2053 g_strchomp(content_encoding);
2054 procmime_parse_content_encoding(content_encoding, mimeinfo);
2056 mimeinfo->encoding_type = ENC_UNKNOWN;
2059 if (content_description != NULL)
2060 mimeinfo->description = g_strdup(content_description);
2062 mimeinfo->description = NULL;
2064 if (content_id != NULL)
2065 mimeinfo->id = g_strdup(content_id);
2067 mimeinfo->id = NULL;
2069 if (content_location != NULL)
2070 mimeinfo->location = g_strdup(content_location);
2072 mimeinfo->location = NULL;
2074 if (content_disposition != NULL) {
2075 g_strchomp(content_disposition);
2076 procmime_parse_content_disposition(content_disposition, mimeinfo);
2078 mimeinfo->disposition = DISPOSITIONTYPE_UNKNOWN;
2080 /* Call parser for mime type */
2081 if ((parser = procmime_get_mimeparser_for_type(mimeinfo->type, mimeinfo->subtype)) != NULL) {
2082 parsed = procmime_mimeparser_parse(parser, mimeinfo);
2085 switch (mimeinfo->type) {
2087 if (g_ascii_strcasecmp(mimeinfo->subtype, "plain") == 0 && short_scan) {
2092 case MIMETYPE_MESSAGE:
2093 if (g_ascii_strcasecmp(mimeinfo->subtype, "rfc822") == 0) {
2094 procmime_parse_message_rfc822(mimeinfo, short_scan);
2096 if (g_ascii_strcasecmp(mimeinfo->subtype, "disposition-notification") == 0) {
2097 procmime_parse_disposition_notification(mimeinfo,
2098 original_msgid, disposition_notification_hdr, short_scan);
2102 case MIMETYPE_MULTIPART:
2103 procmime_parse_multipart(mimeinfo, short_scan);
2106 case MIMETYPE_APPLICATION:
2107 if (g_ascii_strcasecmp(mimeinfo->subtype, "octet-stream") == 0
2108 && original_msgid && *original_msgid
2109 && disposition_notification_hdr && *disposition_notification_hdr) {
2110 procmime_parse_disposition_notification(mimeinfo,
2111 original_msgid, disposition_notification_hdr, short_scan);
2122 static gchar *typenames[] = {
2133 static gboolean output_func(GNode *node, gpointer data)
2136 MimeInfo *mimeinfo = (MimeInfo *) node->data;
2138 depth = g_node_depth(node);
2139 for (i = 0; i < depth; i++)
2141 g_print("%s/%s (offset:%d length:%d encoding: %d)\n", typenames[mimeinfo->type], mimeinfo->subtype, mimeinfo->offset, mimeinfo->length, mimeinfo->encoding_type);
2146 static void output_mime_structure(MimeInfo *mimeinfo, int indent)
2148 g_node_traverse(mimeinfo->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1, output_func, NULL);
2151 static MimeInfo *procmime_scan_file_with_offset(const gchar *filename, int offset, gboolean short_scan)
2156 if (g_stat(filename, &buf) < 0) {
2157 FILE_OP_ERROR(filename, "stat");
2161 mimeinfo = procmime_mimeinfo_new();
2162 mimeinfo->content = MIMECONTENT_FILE;
2163 mimeinfo->encoding_type = ENC_UNKNOWN;
2164 mimeinfo->type = MIMETYPE_MESSAGE;
2165 mimeinfo->subtype = g_strdup("rfc822");
2166 mimeinfo->data.filename = g_strdup(filename);
2167 mimeinfo->offset = offset;
2168 mimeinfo->length = buf.st_size - offset;
2170 procmime_parse_message_rfc822(mimeinfo, short_scan);
2171 if (debug_get_mode())
2172 output_mime_structure(mimeinfo, 0);
2177 static MimeInfo *procmime_scan_file_full(const gchar *filename, gboolean short_scan)
2181 cm_return_val_if_fail(filename != NULL, NULL);
2183 mimeinfo = procmime_scan_file_with_offset(filename, 0, short_scan);
2188 MimeInfo *procmime_scan_file(const gchar *filename)
2190 return procmime_scan_file_full(filename, FALSE);
2193 static MimeInfo *procmime_scan_file_short(const gchar *filename)
2195 return procmime_scan_file_full(filename, TRUE);
2198 static MimeInfo *procmime_scan_queue_file_full(const gchar *filename, gboolean short_scan)
2202 gchar buf[BUFFSIZE];
2205 cm_return_val_if_fail(filename != NULL, NULL);
2208 if ((fp = claws_fopen(filename, "rb")) == NULL)
2210 /* Skip queue header */
2211 while (claws_fgets(buf, sizeof(buf), fp) != NULL) {
2213 if ((!strncmp(buf, "X-Claws-End-Special-Headers: 1",
2214 strlen("X-Claws-End-Special-Headers:"))) ||
2215 (!strncmp(buf, "X-Sylpheed-End-Special-Headers: 1",
2216 strlen("X-Sylpheed-End-Special-Headers:"))))
2219 if (buf[0] == '\r' || buf[0] == '\n') break;
2220 /* from other mailers */
2221 if (!strncmp(buf, "Date: ", 6)
2222 || !strncmp(buf, "To: ", 4)
2223 || !strncmp(buf, "From: ", 6)
2224 || !strncmp(buf, "Subject: ", 9)) {
2232 mimeinfo = procmime_scan_file_with_offset(filename, offset, short_scan);
2237 MimeInfo *procmime_scan_queue_file(const gchar *filename)
2239 return procmime_scan_queue_file_full(filename, FALSE);
2242 static MimeInfo *procmime_scan_queue_file_short(const gchar *filename)
2244 return procmime_scan_queue_file_full(filename, TRUE);
2249 ENC_AS_QUOTED_STRING,
2254 typedef struct _ParametersData {
2260 static void write_parameters(gpointer key, gpointer value, gpointer user_data)
2263 gchar *val = value, *valpos, *tmp;
2264 ParametersData *pdata = (ParametersData *)user_data;
2265 GString *buf = g_string_new("");
2268 EncodeAs encas = ENC_AS_TOKEN;
2270 for (valpos = val; *valpos != 0; valpos++) {
2271 if (!IS_ASCII(*valpos)) {
2272 encas = ENC_AS_ENCWORD;
2277 if (((*valpos >= 0) && (*valpos < 037)) || (*valpos == 0177)) {
2278 encas = ENC_AS_QUOTED_STRING;
2282 /* tspecials + SPACE */
2300 encas = ENC_AS_QUOTED_STRING;
2307 g_string_append_printf(buf, "%s=%s", param, val);
2310 case ENC_AS_QUOTED_STRING:
2311 g_string_append_printf(buf, "%s=\"%s\"", param, val);
2314 #if 0 /* we don't use that for now */
2315 case ENC_AS_EXTENDED:
2316 if (!g_utf8_validate(val, -1, NULL))
2317 g_string_append_printf(buf, "%s*=%s''", param,
2318 conv_get_locale_charset_str());
2320 g_string_append_printf(buf, "%s*=%s''", param,
2322 for (valpos = val; *valpos != '\0'; valpos++) {
2323 if (IS_ASCII(*valpos) && isalnum(*valpos)) {
2324 g_string_append_printf(buf, "%c", *valpos);
2326 gchar hexstr[3] = "XX";
2327 get_hex_str(hexstr, *valpos);
2328 g_string_append_printf(buf, "%%%s", hexstr);
2333 case ENC_AS_EXTENDED:
2334 debug_print("Unhandled ENC_AS_EXTENDED.\n");
2337 case ENC_AS_ENCWORD:
2338 len = MAX(strlen(val)*6, 512);
2339 tmp = g_malloc(len+1);
2340 codeconv_set_strict(TRUE);
2341 conv_encode_header_full(tmp, len, val, pdata->len + strlen(param) + 4 , FALSE,
2342 prefs_common.outgoing_charset);
2343 codeconv_set_strict(FALSE);
2344 if (!tmp || !*tmp) {
2345 codeconv_set_strict(TRUE);
2346 conv_encode_header_full(tmp, len, val, pdata->len + strlen(param) + 4 , FALSE,
2347 conv_get_outgoing_charset_str());
2348 codeconv_set_strict(FALSE);
2350 if (!tmp || !*tmp) {
2351 codeconv_set_strict(TRUE);
2352 conv_encode_header_full(tmp, len, val, pdata->len + strlen(param) + 4 , FALSE,
2354 codeconv_set_strict(FALSE);
2356 if (!tmp || !*tmp) {
2357 conv_encode_header_full(tmp, len, val, pdata->len + strlen(param) + 4 , FALSE,
2360 g_string_append_printf(buf, "%s=\"%s\"", param, tmp);
2366 if (buf->str && strlen(buf->str)) {
2367 tmp = strstr(buf->str, "\n");
2369 len = (tmp - buf->str);
2371 len = strlen(buf->str);
2372 if (pdata->len + len > 76) {
2373 if (fprintf(pdata->fp, ";\n %s", buf->str) < 0)
2374 pdata->error = TRUE;
2375 pdata->len = strlen(buf->str) + 1;
2377 if (fprintf(pdata->fp, "; %s", buf->str) < 0)
2378 pdata->error = TRUE;
2379 pdata->len += strlen(buf->str) + 2;
2382 g_string_free(buf, TRUE);
2385 #define TRY(func) { \
2391 int procmime_write_mime_header(MimeInfo *mimeinfo, FILE *fp)
2393 struct TypeTable *type_table;
2394 ParametersData *pdata = g_new0(ParametersData, 1);
2395 debug_print("procmime_write_mime_header\n");
2398 pdata->error = FALSE;
2399 for (type_table = mime_type_table; type_table->str != NULL; type_table++)
2400 if (mimeinfo->type == type_table->type) {
2401 gchar *buf = g_strdup_printf(
2402 "Content-Type: %s/%s", type_table->str, mimeinfo->subtype);
2403 if (fprintf(fp, "%s", buf) < 0) {
2408 pdata->len = strlen(buf);
2412 g_hash_table_foreach(mimeinfo->typeparameters, write_parameters, pdata);
2413 if (pdata->error == TRUE) {
2419 TRY(fprintf(fp, "\n") >= 0);
2421 if (mimeinfo->encoding_type != ENC_UNKNOWN)
2422 TRY(fprintf(fp, "Content-Transfer-Encoding: %s\n", procmime_get_encoding_str(mimeinfo->encoding_type)) >= 0);
2424 if (mimeinfo->description != NULL)
2425 TRY(fprintf(fp, "Content-Description: %s\n", mimeinfo->description) >= 0);
2427 if (mimeinfo->id != NULL)
2428 TRY(fprintf(fp, "Content-ID: %s\n", mimeinfo->id) >= 0);
2430 if (mimeinfo->location != NULL)
2431 TRY(fprintf(fp, "Content-Location: %s\n", mimeinfo->location) >= 0);
2433 if (mimeinfo->disposition != DISPOSITIONTYPE_UNKNOWN) {
2434 ParametersData *pdata = g_new0(ParametersData, 1);
2436 if (mimeinfo->disposition == DISPOSITIONTYPE_INLINE)
2437 buf = g_strdup("Content-Disposition: inline");
2438 else if (mimeinfo->disposition == DISPOSITIONTYPE_ATTACHMENT)
2439 buf = g_strdup("Content-Disposition: attachment");
2441 buf = g_strdup("Content-Disposition: unknown");
2443 if (fprintf(fp, "%s", buf) < 0) {
2448 pdata->len = strlen(buf);
2452 pdata->error = FALSE;
2453 g_hash_table_foreach(mimeinfo->dispositionparameters, write_parameters, pdata);
2454 if (pdata->error == TRUE) {
2459 TRY(fprintf(fp, "\n") >= 0);
2462 TRY(fprintf(fp, "\n") >= 0);
2467 static gint procmime_write_message_rfc822(MimeInfo *mimeinfo, FILE *fp)
2472 gchar buf[BUFFSIZE];
2473 gboolean skip = FALSE;;
2476 debug_print("procmime_write_message_rfc822\n");
2479 switch (mimeinfo->content) {
2480 case MIMECONTENT_FILE:
2481 if ((infp = claws_fopen(mimeinfo->data.filename, "rb")) == NULL) {
2482 FILE_OP_ERROR(mimeinfo->data.filename, "claws_fopen");
2485 if (fseek(infp, mimeinfo->offset, SEEK_SET) < 0) {
2486 FILE_OP_ERROR(mimeinfo->data.filename, "fseek");
2490 while (claws_fgets(buf, sizeof(buf), infp) == buf) {
2492 if (buf[0] == '\n' && buf[1] == '\0')
2494 if (skip && (buf[0] == ' ' || buf[0] == '\t'))
2496 if (g_ascii_strncasecmp(buf, "MIME-Version:", 13) == 0 ||
2497 g_ascii_strncasecmp(buf, "Content-Type:", 13) == 0 ||
2498 g_ascii_strncasecmp(buf, "Content-Transfer-Encoding:", 26) == 0 ||
2499 g_ascii_strncasecmp(buf, "Content-Description:", 20) == 0 ||
2500 g_ascii_strncasecmp(buf, "Content-ID:", 11) == 0 ||
2501 g_ascii_strncasecmp(buf, "Content-Location:", 17) == 0 ||
2502 g_ascii_strncasecmp(buf, "Content-Disposition:", 20) == 0) {
2507 if (claws_fwrite(buf, sizeof(gchar), len, fp) < len) {
2508 g_warning("failed to dump %zd bytes from file", len);
2517 case MIMECONTENT_MEM:
2518 len = strlen(mimeinfo->data.mem);
2519 if (claws_fwrite(mimeinfo->data.mem, sizeof(gchar), len, fp) < len) {
2520 g_warning("failed to dump %zd bytes from mem", len);
2529 childnode = mimeinfo->node->children;
2530 if (childnode == NULL)
2533 child = (MimeInfo *) childnode->data;
2534 if (fprintf(fp, "MIME-Version: 1.0\n") < 0) {
2535 g_warning("failed to write mime version");
2538 if (procmime_write_mime_header(child, fp) < 0)
2540 return procmime_write_mimeinfo(child, fp);
2543 static gint procmime_write_multipart(MimeInfo *mimeinfo, FILE *fp)
2547 gchar *boundary, *str, *str2;
2548 gchar buf[BUFFSIZE];
2549 gboolean firstboundary;
2552 debug_print("procmime_write_multipart\n");
2554 boundary = g_hash_table_lookup(mimeinfo->typeparameters, "boundary");
2556 switch (mimeinfo->content) {
2557 case MIMECONTENT_FILE:
2558 if ((infp = claws_fopen(mimeinfo->data.filename, "rb")) == NULL) {
2559 FILE_OP_ERROR(mimeinfo->data.filename, "claws_fopen");
2562 if (fseek(infp, mimeinfo->offset, SEEK_SET) < 0) {
2563 FILE_OP_ERROR(mimeinfo->data.filename, "fseek");
2567 while (claws_fgets(buf, sizeof(buf), infp) == buf) {
2568 if (IS_BOUNDARY(buf, boundary, strlen(boundary)))
2571 if (claws_fwrite(buf, sizeof(gchar), len, fp) < len) {
2572 g_warning("failed to write %zd", len);
2580 case MIMECONTENT_MEM:
2581 str = g_strdup(mimeinfo->data.mem);
2582 if (((str2 = strstr(str, boundary)) != NULL) && ((str2 - str) >= 2) &&
2583 (*(str2 - 1) == '-') && (*(str2 - 2) == '-'))
2586 if (claws_fwrite(str, sizeof(gchar), len, fp) < len) {
2587 g_warning("failed to write %zd from mem", len);
2598 childnode = mimeinfo->node->children;
2599 firstboundary = TRUE;
2600 while (childnode != NULL) {
2601 MimeInfo *child = childnode->data;
2604 firstboundary = FALSE;
2606 TRY(fprintf(fp, "\n") >= 0);
2608 TRY(fprintf(fp, "--%s\n", boundary) >= 0);
2610 if (procmime_write_mime_header(child, fp) < 0)
2612 if (procmime_write_mimeinfo(child, fp) < 0)
2615 childnode = g_node_next_sibling(childnode);
2617 TRY(fprintf(fp, "\n--%s--\n", boundary) >= 0);
2622 gint procmime_write_mimeinfo(MimeInfo *mimeinfo, FILE *fp)
2626 debug_print("procmime_write_mimeinfo\n");
2628 if (G_NODE_IS_LEAF(mimeinfo->node)) {
2629 switch (mimeinfo->content) {
2630 case MIMECONTENT_FILE:
2631 if ((infp = claws_fopen(mimeinfo->data.filename, "rb")) == NULL) {
2632 FILE_OP_ERROR(mimeinfo->data.filename, "claws_fopen");
2635 copy_file_part_to_fp(infp, mimeinfo->offset, mimeinfo->length, fp);
2639 case MIMECONTENT_MEM:
2640 len = strlen(mimeinfo->data.mem);
2641 if (claws_fwrite(mimeinfo->data.mem, sizeof(gchar), len, fp) < len)
2649 /* Call writer for mime type */
2650 switch (mimeinfo->type) {
2651 case MIMETYPE_MESSAGE:
2652 if (g_ascii_strcasecmp(mimeinfo->subtype, "rfc822") == 0) {
2653 return procmime_write_message_rfc822(mimeinfo, fp);
2657 case MIMETYPE_MULTIPART:
2658 return procmime_write_multipart(mimeinfo, fp);
2670 gchar *procmime_get_part_file_name(MimeInfo *mimeinfo)
2674 if ((mimeinfo->type == MIMETYPE_TEXT) && !g_ascii_strcasecmp(mimeinfo->subtype, "html"))
2675 base = g_strdup("mimetmp.html");
2677 const gchar *basetmp;
2680 basetmp = procmime_mimeinfo_get_parameter(mimeinfo, "filename");
2681 if (basetmp == NULL)
2682 basetmp = procmime_mimeinfo_get_parameter(mimeinfo, "name");
2683 if (basetmp == NULL)
2684 basetmp = "mimetmp";
2685 basename = g_path_get_basename(basetmp);
2686 if (*basename == '\0') {
2688 basename = g_strdup("mimetmp");
2690 base = conv_filename_from_utf8(basename);
2692 subst_for_shellsafe_filename(base);