2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2012 Hiroyuki Yamamoto & The Claws Mail Team
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "claws-features.h"
31 #include <glib/gi18n.h>
37 #include <sys/types.h>
43 #include "procheader.h"
45 #include "quoted-printable.h"
52 #include "prefs_common.h"
53 #include "prefs_gtk.h"
54 #include "alertpanel.h"
58 static GHashTable *procmime_get_mime_type_table (void);
59 static MimeInfo *procmime_scan_file_short(const gchar *filename);
60 static MimeInfo *procmime_scan_queue_file_short(const gchar *filename);
61 static MimeInfo *procmime_scan_queue_file_full(const gchar *filename, gboolean short_scan);
63 MimeInfo *procmime_mimeinfo_new(void)
67 mimeinfo = g_new0(MimeInfo, 1);
68 mimeinfo->content = MIMECONTENT_EMPTY;
69 mimeinfo->data.filename = NULL;
71 mimeinfo->type = MIMETYPE_UNKNOWN;
72 mimeinfo->encoding_type = ENC_UNKNOWN;
73 mimeinfo->typeparameters = g_hash_table_new(g_str_hash, g_str_equal);
75 mimeinfo->disposition = DISPOSITIONTYPE_UNKNOWN;
76 mimeinfo->dispositionparameters
77 = g_hash_table_new(g_str_hash, g_str_equal);
79 mimeinfo->node = g_node_new(mimeinfo);
84 static gboolean procmime_mimeinfo_parameters_destroy(gpointer key, gpointer value, gpointer user_data)
92 static gchar *forced_charset = NULL;
94 void procmime_force_charset(const gchar *str)
96 g_free(forced_charset);
97 forced_charset = NULL;
99 forced_charset = g_strdup(str);
102 static EncodingType forced_encoding = 0;
104 void procmime_force_encoding(EncodingType encoding)
106 forced_encoding = encoding;
109 static gboolean free_func(GNode *node, gpointer data)
111 MimeInfo *mimeinfo = (MimeInfo *) node->data;
113 switch (mimeinfo->content) {
114 case MIMECONTENT_FILE:
116 claws_unlink(mimeinfo->data.filename);
117 g_free(mimeinfo->data.filename);
120 case MIMECONTENT_MEM:
122 g_free(mimeinfo->data.mem);
127 g_free(mimeinfo->subtype);
128 g_free(mimeinfo->description);
129 g_free(mimeinfo->id);
130 g_free(mimeinfo->location);
132 g_hash_table_foreach_remove(mimeinfo->typeparameters,
133 procmime_mimeinfo_parameters_destroy, NULL);
134 g_hash_table_destroy(mimeinfo->typeparameters);
135 g_hash_table_foreach_remove(mimeinfo->dispositionparameters,
136 procmime_mimeinfo_parameters_destroy, NULL);
137 g_hash_table_destroy(mimeinfo->dispositionparameters);
139 if (mimeinfo->privacy)
140 privacy_free_privacydata(mimeinfo->privacy);
147 void procmime_mimeinfo_free_all(MimeInfo *mimeinfo)
154 node = mimeinfo->node;
155 g_node_traverse(node, G_IN_ORDER, G_TRAVERSE_ALL, -1, free_func, NULL);
157 g_node_destroy(node);
160 MimeInfo *procmime_mimeinfo_parent(MimeInfo *mimeinfo)
162 cm_return_val_if_fail(mimeinfo != NULL, NULL);
163 cm_return_val_if_fail(mimeinfo->node != NULL, NULL);
165 if (mimeinfo->node->parent == NULL)
167 return (MimeInfo *) mimeinfo->node->parent->data;
170 MimeInfo *procmime_mimeinfo_next(MimeInfo *mimeinfo)
172 cm_return_val_if_fail(mimeinfo != NULL, NULL);
173 cm_return_val_if_fail(mimeinfo->node != NULL, NULL);
175 if (mimeinfo->node->children)
176 return (MimeInfo *) mimeinfo->node->children->data;
177 if (mimeinfo->node->next)
178 return (MimeInfo *) mimeinfo->node->next->data;
180 if (mimeinfo->node->parent == NULL)
183 while (mimeinfo->node->parent != NULL) {
184 mimeinfo = (MimeInfo *) mimeinfo->node->parent->data;
185 if (mimeinfo->node->next)
186 return (MimeInfo *) mimeinfo->node->next->data;
192 MimeInfo *procmime_scan_message(MsgInfo *msginfo)
197 filename = procmsg_get_message_file_path(msginfo);
198 if (!filename || !is_file_exist(filename)) {
203 if (!folder_has_parent_of_type(msginfo->folder, F_QUEUE) &&
204 !folder_has_parent_of_type(msginfo->folder, F_DRAFT))
205 mimeinfo = procmime_scan_file(filename);
207 mimeinfo = procmime_scan_queue_file(filename);
213 MimeInfo *procmime_scan_message_short(MsgInfo *msginfo)
218 filename = procmsg_get_message_file_path(msginfo);
219 if (!filename || !is_file_exist(filename)) {
224 if (!folder_has_parent_of_type(msginfo->folder, F_QUEUE) &&
225 !folder_has_parent_of_type(msginfo->folder, F_DRAFT))
226 mimeinfo = procmime_scan_file_short(filename);
228 mimeinfo = procmime_scan_queue_file_short(filename);
236 H_CONTENT_TRANSFER_ENCODING = 0,
238 H_CONTENT_DISPOSITION = 2,
239 H_CONTENT_DESCRIPTION = 3,
243 const gchar *procmime_mimeinfo_get_parameter(MimeInfo *mimeinfo, const gchar *name)
247 cm_return_val_if_fail(mimeinfo != NULL, NULL);
248 cm_return_val_if_fail(name != NULL, NULL);
250 value = g_hash_table_lookup(mimeinfo->dispositionparameters, name);
252 value = g_hash_table_lookup(mimeinfo->typeparameters, name);
257 #ifdef HAVE_FGETS_UNLOCKED
258 #define SC_FGETS fgets_unlocked
259 #define SC_FPUTS fputs_unlocked
260 #define SC_FPUTC fputc_unlocked
261 #define SC_FREAD fread_unlocked
262 #define SC_FWRITE fwrite_unlocked
263 #define SC_FEOF feof_unlocked
264 #define SC_FERROR ferror_unlocked
266 static FILE *procmime_fopen(const gchar *file, const gchar *mode)
268 FILE *fp = g_fopen(file, mode);
274 static int procmime_fclose(FILE *fp)
280 #define SC_FGETS fgets
281 #define SC_FPUTS fputs
282 #define SC_FPUTC fputc
283 #define SC_FREAD fread
284 #define SC_FWRITE fwrite
286 #define SC_FERROR ferror
287 #define procmime_fopen g_fopen
288 #define procmime_fclose fclose
291 #define FLUSH_LASTLINE() { \
292 if (*lastline != '\0') { \
294 strretchomp(lastline); \
295 llen = strlen(lastline); \
296 if (lastline[llen-1] == ' ' && strcmp(lastline,"-- ")) { \
297 /* this is flowed */ \
299 lastline[llen-1] = '\0'; \
300 if (SC_FPUTS(lastline, outfp) == EOF) \
303 if (SC_FPUTS(lastline, outfp) == EOF) \
305 if (SC_FPUTS("\n", outfp) == EOF) \
309 strcpy(lastline, buf); \
312 gboolean procmime_decode_content(MimeInfo *mimeinfo)
319 gboolean tmp_file = FALSE;
320 gboolean flowed = FALSE;
321 gboolean delsp = FALSE;
322 gboolean err = FALSE;
324 EncodingType encoding = forced_encoding
326 : mimeinfo->encoding_type;
327 gchar lastline[BUFFSIZE];
328 memset(lastline, 0, BUFFSIZE);
330 cm_return_val_if_fail(mimeinfo != NULL, FALSE);
332 if (prefs_common.respect_flowed_format &&
333 mimeinfo->type == MIMETYPE_TEXT &&
334 !strcasecmp(mimeinfo->subtype, "plain")) {
335 if (procmime_mimeinfo_get_parameter(mimeinfo, "format") != NULL &&
336 !strcasecmp(procmime_mimeinfo_get_parameter(mimeinfo, "format"),"flowed"))
339 procmime_mimeinfo_get_parameter(mimeinfo, "delsp") != NULL &&
340 !strcasecmp(procmime_mimeinfo_get_parameter(mimeinfo, "delsp"),"yes"))
345 encoding == ENC_UNKNOWN ||
346 encoding == ENC_BINARY ||
347 encoding == ENC_7BIT ||
352 if (mimeinfo->type == MIMETYPE_MULTIPART &&
353 !strcasecmp(mimeinfo->subtype, "signed"))
356 infp = procmime_fopen(mimeinfo->data.filename, "rb");
361 fseek(infp, mimeinfo->offset, SEEK_SET);
363 outfp = get_tmpfile_in_dir(get_mime_tmp_dir(), &tmpfilename);
366 procmime_fclose(infp);
370 readend = mimeinfo->offset + mimeinfo->length;
372 if (encoding == ENC_QUOTED_PRINTABLE) {
373 while ((ftell(infp) < readend) && (SC_FGETS(buf, sizeof(buf), infp) != NULL)) {
375 len = qp_decode_line(buf);
378 if (SC_FWRITE(buf, 1, len, outfp) < len)
386 } else if (encoding == ENC_BASE64) {
387 gchar outbuf[BUFFSIZE];
388 gint len, inlen, inread;
389 Base64Decoder *decoder;
390 gboolean got_error = FALSE;
391 gboolean uncanonicalize = FALSE;
393 gboolean null_bytes = FALSE;
394 gboolean starting = TRUE;
396 if (mimeinfo->type == MIMETYPE_TEXT ||
397 mimeinfo->type == MIMETYPE_MESSAGE) {
398 uncanonicalize = TRUE;
399 tmpfp = my_tmpfile();
403 procmime_fclose(outfp);
404 procmime_fclose(infp);
407 #ifdef HAVE_FGETS_UNLOCKED
412 decoder = base64_decoder_new();
413 while ((inlen = MIN(readend - ftell(infp), sizeof(buf))) > 0 && !err) {
414 inread = SC_FREAD(buf, 1, inlen, infp);
415 len = base64_decoder_decode(decoder, buf, outbuf, inread);
416 if (uncanonicalize == TRUE && strlen(outbuf) < len && starting) {
417 uncanonicalize = FALSE;
421 if (((inread != inlen) || len < 0) && !got_error) {
422 g_warning("Bad BASE64 content.\n");
423 if (SC_FWRITE(_("[Error decoding BASE64]\n"),
425 strlen(_("[Error decoding BASE64]\n")),
426 tmpfp) < strlen(_("[Error decoding BASE64]\n")))
427 g_warning("error decoding BASE64");
430 } else if (len >= 0) {
431 /* print out the error message only once
434 /* we won't uncanonicalize, output to outfp directly */
435 if (SC_FWRITE(outbuf, sizeof(gchar), len, outfp) < len)
438 if (SC_FWRITE(outbuf, sizeof(gchar), len, tmpfp) < len)
444 base64_decoder_free(decoder);
446 if (uncanonicalize) {
448 while (SC_FGETS(buf, sizeof(buf), tmpfp) != NULL) {
450 if (SC_FPUTS(buf, outfp) == EOF)
453 procmime_fclose(tmpfp);
455 } else if (encoding == ENC_X_UUENCODE) {
456 gchar outbuf[BUFFSIZE];
458 gboolean flag = FALSE;
460 while ((ftell(infp) < readend) && (SC_FGETS(buf, sizeof(buf), infp) != NULL)) {
461 if (!flag && strncmp(buf,"begin ", 6)) continue;
464 len = fromuutobits(outbuf, buf);
467 g_warning("Bad UUENCODE content(%d)\n", len);
470 if (SC_FWRITE(outbuf, sizeof(gchar), len, outfp) < len)
476 while ((ftell(infp) < readend) && (SC_FGETS(buf, sizeof(buf), infp) != NULL)) {
478 if (SC_FPUTS(buf, outfp) == EOF)
487 g_warning("write error");
490 procmime_fclose(outfp);
491 procmime_fclose(infp);
497 g_stat(tmpfilename, &statbuf);
498 if (mimeinfo->tmp && (mimeinfo->data.filename != NULL))
499 claws_unlink(mimeinfo->data.filename);
500 g_free(mimeinfo->data.filename);
501 mimeinfo->data.filename = tmpfilename;
502 mimeinfo->tmp = TRUE;
503 mimeinfo->offset = 0;
504 mimeinfo->length = statbuf.st_size;
505 mimeinfo->encoding_type = ENC_BINARY;
510 #define B64_LINE_SIZE 57
511 #define B64_BUFFSIZE 77
513 gboolean procmime_encode_content(MimeInfo *mimeinfo, EncodingType encoding)
515 FILE *infp = NULL, *outfp;
519 gboolean err = FALSE;
521 if (mimeinfo->content == MIMECONTENT_EMPTY)
524 if (mimeinfo->encoding_type != ENC_UNKNOWN &&
525 mimeinfo->encoding_type != ENC_BINARY &&
526 mimeinfo->encoding_type != ENC_7BIT &&
527 mimeinfo->encoding_type != ENC_8BIT)
528 if(!procmime_decode_content(mimeinfo))
531 outfp = get_tmpfile_in_dir(get_mime_tmp_dir(), &tmpfilename);
536 #ifdef HAVE_FGETS_UNLOCKED
540 if (mimeinfo->content == MIMECONTENT_FILE && mimeinfo->data.filename) {
541 if ((infp = procmime_fopen(mimeinfo->data.filename, "rb")) == NULL) {
542 g_warning("Can't open file %s\n", mimeinfo->data.filename);
543 procmime_fclose(outfp);
546 } else if (mimeinfo->content == MIMECONTENT_MEM) {
547 infp = str_open_as_stream(mimeinfo->data.mem);
549 procmime_fclose(outfp);
552 #ifdef HAVE_FGETS_UNLOCKED
558 if (encoding == ENC_BASE64) {
559 gchar inbuf[B64_LINE_SIZE], outbuf[B64_BUFFSIZE];
561 gchar *tmp_file = NULL;
563 if (mimeinfo->type == MIMETYPE_TEXT ||
564 mimeinfo->type == MIMETYPE_MESSAGE) {
565 if (mimeinfo->content == MIMECONTENT_FILE) {
566 tmp_file = get_tmp_file();
567 if (canonicalize_file(mimeinfo->data.filename, tmp_file) < 0) {
569 procmime_fclose(infp);
570 procmime_fclose(outfp);
573 if ((tmp_fp = procmime_fopen(tmp_file, "rb")) == NULL) {
574 FILE_OP_ERROR(tmp_file, "fopen");
575 claws_unlink(tmp_file);
577 procmime_fclose(infp);
578 procmime_fclose(outfp);
582 gchar *out = canonicalize_str(mimeinfo->data.mem);
583 procmime_fclose(infp);
584 infp = str_open_as_stream(out);
588 procmime_fclose(outfp);
591 #ifdef HAVE_FGETS_UNLOCKED
597 while ((len = SC_FREAD(inbuf, sizeof(gchar),
598 B64_LINE_SIZE, tmp_fp))
600 base64_encode(outbuf, inbuf, B64_LINE_SIZE);
601 if (SC_FPUTS(outbuf, outfp) == EOF)
603 if (SC_FPUTC('\n', outfp) == EOF)
606 if (len > 0 && SC_FEOF(tmp_fp)) {
607 base64_encode(outbuf, inbuf, len);
608 if (SC_FPUTS(outbuf, outfp) == EOF)
610 if (SC_FPUTC('\n', outfp) == EOF)
615 procmime_fclose(tmp_fp);
616 claws_unlink(tmp_file);
619 } else if (encoding == ENC_QUOTED_PRINTABLE) {
620 gchar inbuf[BUFFSIZE], outbuf[BUFFSIZE * 4];
622 while (SC_FGETS(inbuf, sizeof(inbuf), infp) != NULL) {
623 qp_encode_line(outbuf, inbuf);
625 if (!strncmp("From ", outbuf, sizeof("From ")-1)) {
626 gchar *tmpbuf = outbuf;
628 tmpbuf += sizeof("From ")-1;
630 if (SC_FPUTS("=46rom ", outfp) == EOF)
632 if (SC_FPUTS(tmpbuf, outfp) == EOF)
635 if (SC_FPUTS(outbuf, outfp) == EOF)
642 while (SC_FGETS(buf, sizeof(buf), infp) != NULL) {
644 if (SC_FPUTS(buf, outfp) == EOF)
649 procmime_fclose(outfp);
650 procmime_fclose(infp);
655 if (mimeinfo->content == MIMECONTENT_FILE) {
656 if (mimeinfo->tmp && (mimeinfo->data.filename != NULL))
657 claws_unlink(mimeinfo->data.filename);
658 g_free(mimeinfo->data.filename);
659 } else if (mimeinfo->content == MIMECONTENT_MEM) {
660 if (mimeinfo->tmp && (mimeinfo->data.mem != NULL))
661 g_free(mimeinfo->data.mem);
664 g_stat(tmpfilename, &statbuf);
665 mimeinfo->content = MIMECONTENT_FILE;
666 mimeinfo->data.filename = tmpfilename;
667 mimeinfo->tmp = TRUE;
668 mimeinfo->offset = 0;
669 mimeinfo->length = statbuf.st_size;
670 mimeinfo->encoding_type = encoding;
675 gint procmime_get_part(const gchar *outfile, MimeInfo *mimeinfo)
679 gint restlength, readlength;
680 gint saved_errno = 0;
682 cm_return_val_if_fail(outfile != NULL, -1);
683 cm_return_val_if_fail(mimeinfo != NULL, -1);
685 if (mimeinfo->encoding_type != ENC_BINARY && !procmime_decode_content(mimeinfo))
688 if ((infp = procmime_fopen(mimeinfo->data.filename, "rb")) == NULL) {
690 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
691 return -(saved_errno);
693 if (fseek(infp, mimeinfo->offset, SEEK_SET) < 0) {
695 FILE_OP_ERROR(mimeinfo->data.filename, "fseek");
696 procmime_fclose(infp);
697 return -(saved_errno);
699 if ((outfp = procmime_fopen(outfile, "wb")) == NULL) {
701 FILE_OP_ERROR(outfile, "fopen");
702 procmime_fclose(infp);
703 return -(saved_errno);
706 restlength = mimeinfo->length;
708 while ((restlength > 0) && ((readlength = SC_FREAD(buf, 1, restlength > BUFFSIZE ? BUFFSIZE : restlength, infp)) > 0)) {
709 if (SC_FWRITE(buf, 1, readlength, outfp) != readlength) {
711 procmime_fclose(infp);
712 procmime_fclose(outfp);
713 return -(saved_errno);
715 restlength -= readlength;
718 procmime_fclose(infp);
719 if (procmime_fclose(outfp) == EOF) {
721 FILE_OP_ERROR(outfile, "fclose");
722 claws_unlink(outfile);
723 return -(saved_errno);
729 gboolean procmime_scan_text_content(MimeInfo *mimeinfo,
730 gboolean (*scan_callback)(const gchar *str, gpointer cb_data),
734 const gchar *src_codeset;
735 gboolean conv_fail = FALSE;
739 gboolean scan_ret = FALSE;
741 cm_return_val_if_fail(mimeinfo != NULL, TRUE);
742 cm_return_val_if_fail(scan_callback != NULL, TRUE);
744 if (!procmime_decode_content(mimeinfo))
747 tmpfile = procmime_get_tmp_file_name(mimeinfo);
751 if (procmime_get_part(tmpfile, mimeinfo) < 0) {
756 tmpfp = procmime_fopen(tmpfile, "rb");
762 src_codeset = forced_charset
764 procmime_mimeinfo_get_parameter(mimeinfo, "charset");
766 /* use supersets transparently when possible */
767 if (!forced_charset && src_codeset && !strcasecmp(src_codeset, CS_ISO_8859_1))
768 src_codeset = CS_WINDOWS_1252;
769 else if (!forced_charset && src_codeset && !strcasecmp(src_codeset, CS_X_GBK))
770 src_codeset = CS_GB18030;
771 else if (!forced_charset && src_codeset && !strcasecmp(src_codeset, CS_GBK))
772 src_codeset = CS_GB18030;
773 else if (!forced_charset && src_codeset && !strcasecmp(src_codeset, CS_GB2312))
774 src_codeset = CS_GB18030;
775 else if (!forced_charset && src_codeset && !strcasecmp(src_codeset, CS_X_VIET_VPS))
776 src_codeset = CS_WINDOWS_874;
778 if (mimeinfo->type == MIMETYPE_TEXT && !g_ascii_strcasecmp(mimeinfo->subtype, "html")) {
779 SC_HTMLParser *parser;
782 conv = conv_code_converter_new(src_codeset);
783 parser = sc_html_parser_new(tmpfp, conv);
784 while ((str = sc_html_parse(parser)) != NULL) {
785 if ((scan_ret = scan_callback(str, cb_data)) == TRUE)
788 sc_html_parser_destroy(parser);
789 conv_code_converter_destroy(conv);
790 } else if (mimeinfo->type == MIMETYPE_TEXT && !g_ascii_strcasecmp(mimeinfo->subtype, "enriched")) {
794 conv = conv_code_converter_new(src_codeset);
795 parser = ertf_parser_new(tmpfp, conv);
796 while ((str = ertf_parse(parser)) != NULL) {
797 if ((scan_ret = scan_callback(str, cb_data)) == TRUE)
800 ertf_parser_destroy(parser);
801 conv_code_converter_destroy(conv);
802 } else if (mimeinfo->type == MIMETYPE_TEXT) {
803 while (SC_FGETS(buf, sizeof(buf), tmpfp) != NULL) {
804 str = conv_codeset_strdup(buf, src_codeset, CS_UTF_8);
806 if ((scan_ret = scan_callback(str, cb_data)) == TRUE) {
813 if ((scan_ret = scan_callback(buf, cb_data)) == TRUE)
820 g_warning("procmime_get_text_content(): Code conversion failed.\n");
822 procmime_fclose(tmpfp);
823 claws_unlink(tmpfile);
829 static gboolean scan_fputs_cb(const gchar *str, gpointer fp)
831 if (SC_FPUTS(str, (FILE *)fp) == EOF)
837 FILE *procmime_get_text_content(MimeInfo *mimeinfo)
842 if ((outfp = my_tmpfile()) == NULL) {
846 #ifdef HAVE_FGETS_UNLOCKED
850 err = procmime_scan_text_content(mimeinfo, scan_fputs_cb, outfp);
854 procmime_fclose(outfp);
857 #ifdef HAVE_FGETS_UNLOCKED
864 FILE *procmime_get_binary_content(MimeInfo *mimeinfo)
869 cm_return_val_if_fail(mimeinfo != NULL, NULL);
871 if (!procmime_decode_content(mimeinfo))
874 tmpfile = procmime_get_tmp_file_name(mimeinfo);
878 if (procmime_get_part(tmpfile, mimeinfo) < 0) {
883 outfp = procmime_fopen(tmpfile, "rb");
893 #ifdef HAVE_FGETS_UNLOCKED
899 /* search the first text part of (multipart) MIME message,
900 decode, convert it and output to outfp. */
901 FILE *procmime_get_first_text_content(MsgInfo *msginfo)
904 MimeInfo *mimeinfo, *partinfo;
905 gboolean empty_ok = FALSE, short_scan = TRUE;
907 cm_return_val_if_fail(msginfo != NULL, NULL);
909 /* first we try to short-scan (for speed), refusing empty parts */
912 mimeinfo = procmime_scan_message_short(msginfo);
914 mimeinfo = procmime_scan_message(msginfo);
915 if (!mimeinfo) return NULL;
918 while (partinfo && (partinfo->type != MIMETYPE_TEXT ||
919 (partinfo->length == 0 && !empty_ok))) {
920 partinfo = procmime_mimeinfo_next(partinfo);
923 outfp = procmime_get_text_content(partinfo);
924 else if (!empty_ok && short_scan) {
925 /* if short scan didn't find a non-empty part, rescan
926 * fully for non-empty parts
929 procmime_mimeinfo_free_all(mimeinfo);
931 } else if (!empty_ok && !short_scan) {
932 /* if full scan didn't find a non-empty part, rescan
933 * accepting empty parts
936 procmime_mimeinfo_free_all(mimeinfo);
939 procmime_mimeinfo_free_all(mimeinfo);
941 /* outfp already unlocked at this time */
946 static gboolean find_encrypted_func(GNode *node, gpointer data)
948 MimeInfo *mimeinfo = (MimeInfo *) node->data;
949 MimeInfo **encinfo = (MimeInfo **) data;
951 if (privacy_mimeinfo_is_encrypted(mimeinfo)) {
959 static MimeInfo *find_encrypted_part(MimeInfo *rootinfo)
961 MimeInfo *encinfo = NULL;
963 g_node_traverse(rootinfo->node, G_IN_ORDER, G_TRAVERSE_ALL, -1,
964 find_encrypted_func, &encinfo);
969 /* search the first encrypted text part of (multipart) MIME message,
970 decode, convert it and output to outfp. */
971 FILE *procmime_get_first_encrypted_text_content(MsgInfo *msginfo)
974 MimeInfo *mimeinfo, *partinfo, *encinfo;
976 cm_return_val_if_fail(msginfo != NULL, NULL);
978 mimeinfo = procmime_scan_message(msginfo);
984 if ((encinfo = find_encrypted_part(partinfo)) != NULL) {
985 debug_print("decrypting message part\n");
986 if (privacy_mimeinfo_decrypt(encinfo) < 0) {
987 alertpanel_error(_("Couldn't decrypt: %s"),
988 privacy_get_error());
993 while (partinfo && partinfo->type != MIMETYPE_TEXT) {
994 partinfo = procmime_mimeinfo_next(partinfo);
995 if (privacy_mimeinfo_is_signed(partinfo))
996 procmsg_msginfo_set_flags(msginfo, 0, MSG_SIGNED);
1000 outfp = procmime_get_text_content(partinfo);
1002 procmime_mimeinfo_free_all(mimeinfo);
1004 /* outfp already unlocked at this time */
1008 gboolean procmime_msginfo_is_encrypted(MsgInfo *msginfo)
1010 MimeInfo *mimeinfo, *partinfo;
1011 gboolean result = FALSE;
1013 cm_return_val_if_fail(msginfo != NULL, FALSE);
1015 mimeinfo = procmime_scan_message(msginfo);
1020 partinfo = mimeinfo;
1021 result = (find_encrypted_part(partinfo) != NULL);
1022 procmime_mimeinfo_free_all(mimeinfo);
1027 gchar *procmime_get_tmp_file_name(MimeInfo *mimeinfo)
1029 static guint32 id = 0;
1034 cm_return_val_if_fail(mimeinfo != NULL, NULL);
1036 g_snprintf(f_prefix, sizeof(f_prefix), "%08x.", id++);
1038 if ((mimeinfo->type == MIMETYPE_TEXT) && !g_ascii_strcasecmp(mimeinfo->subtype, "html"))
1039 base = g_strdup("mimetmp.html");
1041 const gchar *basetmp;
1043 basetmp = procmime_mimeinfo_get_parameter(mimeinfo, "filename");
1044 if (basetmp == NULL)
1045 basetmp = procmime_mimeinfo_get_parameter(mimeinfo, "name");
1046 if (basetmp == NULL)
1047 basetmp = "mimetmp";
1048 basetmp = g_path_get_basename(basetmp);
1049 if (*basetmp == '\0')
1050 basetmp = g_strdup("mimetmp");
1051 base = conv_filename_from_utf8(basetmp);
1052 g_free((gchar*)basetmp);
1053 subst_for_shellsafe_filename(base);
1056 filename = g_strconcat(get_mime_tmp_dir(), G_DIR_SEPARATOR_S,
1057 f_prefix, base, NULL);
1064 static GList *mime_type_list = NULL;
1066 gchar *procmime_get_mime_type(const gchar *filename)
1072 static GHashTable *mime_type_table = NULL;
1073 MimeType *mime_type;
1075 if (!mime_type_table) {
1076 mime_type_table = procmime_get_mime_type_table();
1077 if (!mime_type_table) return NULL;
1081 if (filename == NULL)
1084 base = g_path_get_basename(filename);
1085 if ((p = strrchr(base, '.')) != NULL)
1086 ext = g_utf8_strdown(p + 1, -1);
1090 mime_type = g_hash_table_lookup(mime_type_table, ext);
1094 str = g_strconcat(mime_type->type, "/", mime_type->sub_type,
1096 debug_print("got type %s for %s\n", str, ext);
1102 gchar *str = get_content_type_from_registry_with_ext(ext);
1109 static guint procmime_str_hash(gconstpointer gptr)
1111 guint hash_result = 0;
1114 for (str = gptr; str && *str; str++) {
1115 if (isupper(*str)) hash_result += (*str + ' ');
1116 else hash_result += *str;
1122 static gint procmime_str_equal(gconstpointer gptr1, gconstpointer gptr2)
1124 const char *str1 = gptr1;
1125 const char *str2 = gptr2;
1127 return !g_utf8_collate(str1, str2);
1130 static GHashTable *procmime_get_mime_type_table(void)
1132 GHashTable *table = NULL;
1134 MimeType *mime_type;
1137 if (!mime_type_list) {
1138 mime_type_list = procmime_get_mime_type_list();
1139 if (!mime_type_list) return NULL;
1142 table = g_hash_table_new(procmime_str_hash, procmime_str_equal);
1144 for (cur = mime_type_list; cur != NULL; cur = cur->next) {
1148 mime_type = (MimeType *)cur->data;
1150 if (!mime_type->extension) continue;
1152 exts = g_strsplit(mime_type->extension, " ", 16);
1153 for (i = 0; exts[i] != NULL; i++) {
1154 /* use previously dup'd key on overwriting */
1155 if (g_hash_table_lookup(table, exts[i]))
1158 key = g_strdup(exts[i]);
1159 g_hash_table_insert(table, key, mime_type);
1167 GList *procmime_get_mime_type_list(void)
1171 gchar buf[BUFFSIZE];
1174 MimeType *mime_type;
1175 gboolean fp_is_glob_file = TRUE;
1178 return mime_type_list;
1180 #if defined(__NetBSD__) || defined(__OpenBSD__) || defined(__FreeBSD__)
1181 if ((fp = procmime_fopen(DATAROOTDIR "/mime/globs", "rb")) == NULL)
1183 if ((fp = procmime_fopen("/usr/share/mime/globs", "rb")) == NULL)
1186 fp_is_glob_file = FALSE;
1187 if ((fp = procmime_fopen("/etc/mime.types", "rb")) == NULL) {
1188 if ((fp = procmime_fopen(SYSCONFDIR "/mime.types", "rb"))
1190 FILE_OP_ERROR(SYSCONFDIR "/mime.types",
1197 while (SC_FGETS(buf, sizeof(buf), fp) != NULL) {
1198 p = strchr(buf, '#');
1204 if (fp_is_glob_file) {
1205 while (*p && !g_ascii_isspace(*p) && (*p!=':')) p++;
1207 while (*p && !g_ascii_isspace(*p)) p++;
1214 delim = strchr(buf, '/');
1215 if (delim == NULL) continue;
1218 mime_type = g_new(MimeType, 1);
1219 mime_type->type = g_strdup(buf);
1220 mime_type->sub_type = g_strdup(delim + 1);
1222 if (fp_is_glob_file) {
1223 while (*p && (g_ascii_isspace(*p)||(*p=='*')||(*p=='.'))) p++;
1225 while (*p && g_ascii_isspace(*p)) p++;
1229 mime_type->extension = g_utf8_strdown(p, -1);
1231 mime_type->extension = NULL;
1233 list = g_list_append(list, mime_type);
1236 procmime_fclose(fp);
1239 g_warning("Can't read mime.types\n");
1244 EncodingType procmime_get_encoding_for_charset(const gchar *charset)
1248 else if (!g_ascii_strncasecmp(charset, "ISO-2022-", 9) ||
1249 !g_ascii_strcasecmp(charset, "US-ASCII"))
1251 else if (!g_ascii_strcasecmp(charset, "ISO-8859-5") ||
1252 !g_ascii_strncasecmp(charset, "KOI8-", 5) ||
1253 !g_ascii_strcasecmp(charset, "Windows-1251"))
1255 else if (!g_ascii_strncasecmp(charset, "ISO-8859-", 9))
1256 return ENC_QUOTED_PRINTABLE;
1257 else if (!g_ascii_strncasecmp(charset, "UTF-8", 5))
1258 return ENC_QUOTED_PRINTABLE;
1263 EncodingType procmime_get_encoding_for_text_file(const gchar *file, gboolean *has_binary)
1266 guchar buf[BUFFSIZE];
1268 size_t octet_chars = 0;
1269 size_t total_len = 0;
1270 gfloat octet_percentage;
1271 gboolean force_b64 = FALSE;
1273 if ((fp = procmime_fopen(file, "rb")) == NULL) {
1274 FILE_OP_ERROR(file, "fopen");
1278 while ((len = SC_FREAD(buf, sizeof(guchar), sizeof(buf), fp)) > 0) {
1282 for (p = buf, i = 0; i < len; ++p, ++i) {
1293 procmime_fclose(fp);
1296 octet_percentage = (gfloat)octet_chars / (gfloat)total_len;
1298 octet_percentage = 0.0;
1300 debug_print("procmime_get_encoding_for_text_file(): "
1301 "8bit chars: %zd / %zd (%f%%)\n", octet_chars, total_len,
1302 100.0 * octet_percentage);
1304 if (octet_percentage > 0.20 || force_b64) {
1305 debug_print("using BASE64\n");
1307 } else if (octet_chars > 0) {
1308 debug_print("using quoted-printable\n");
1309 return ENC_QUOTED_PRINTABLE;
1311 debug_print("using 7bit\n");
1316 struct EncodingTable
1319 EncodingType enc_type;
1322 struct EncodingTable encoding_table[] = {
1325 {"binary", ENC_BINARY},
1326 {"quoted-printable", ENC_QUOTED_PRINTABLE},
1327 {"base64", ENC_BASE64},
1328 {"x-uuencode", ENC_UNKNOWN},
1329 {NULL, ENC_UNKNOWN},
1332 const gchar *procmime_get_encoding_str(EncodingType encoding)
1334 struct EncodingTable *enc_table;
1336 for (enc_table = encoding_table; enc_table->str != NULL; enc_table++) {
1337 if (enc_table->enc_type == encoding)
1338 return enc_table->str;
1343 /* --- NEW MIME STUFF --- */
1350 static struct TypeTable mime_type_table[] = {
1351 {"text", MIMETYPE_TEXT},
1352 {"image", MIMETYPE_IMAGE},
1353 {"audio", MIMETYPE_AUDIO},
1354 {"video", MIMETYPE_VIDEO},
1355 {"application", MIMETYPE_APPLICATION},
1356 {"message", MIMETYPE_MESSAGE},
1357 {"multipart", MIMETYPE_MULTIPART},
1361 const gchar *procmime_get_media_type_str(MimeMediaType type)
1363 struct TypeTable *type_table;
1365 for (type_table = mime_type_table; type_table->str != NULL; type_table++) {
1366 if (type_table->type == type)
1367 return type_table->str;
1372 MimeMediaType procmime_get_media_type(const gchar *str)
1374 struct TypeTable *typetablearray;
1376 for (typetablearray = mime_type_table; typetablearray->str != NULL; typetablearray++)
1377 if (g_ascii_strncasecmp(str, typetablearray->str, strlen(typetablearray->str)) == 0)
1378 return typetablearray->type;
1380 return MIMETYPE_UNKNOWN;
1384 *\brief Safe wrapper for content type string.
1386 *\return const gchar * Pointer to content type string.
1388 gchar *procmime_get_content_type_str(MimeMediaType type,
1389 const char *subtype)
1391 const gchar *type_str = NULL;
1393 if (subtype == NULL || !(type_str = procmime_get_media_type_str(type)))
1394 return g_strdup("unknown");
1395 return g_strdup_printf("%s/%s", type_str, subtype);
1398 static int procmime_parse_mimepart(MimeInfo *parent,
1399 gchar *content_type,
1400 gchar *content_encoding,
1401 gchar *content_description,
1403 gchar *content_disposition,
1404 gchar *content_location,
1405 const gchar *original_msgid,
1406 const gchar *disposition_notification_hdr,
1407 const gchar *filename,
1410 gboolean short_scan);
1412 static void procmime_parse_message_rfc822(MimeInfo *mimeinfo, gboolean short_scan)
1414 HeaderEntry hentry[] = {{"Content-Type:", NULL, TRUE},
1415 {"Content-Transfer-Encoding:",
1417 {"Content-Description:",
1421 {"Content-Disposition:",
1423 {"Content-Location:",
1427 {"Original-Message-ID:",
1431 {NULL, NULL, FALSE}};
1432 guint content_start, i;
1437 procmime_decode_content(mimeinfo);
1439 fp = procmime_fopen(mimeinfo->data.filename, "rb");
1441 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
1444 fseek(fp, mimeinfo->offset, SEEK_SET);
1445 procheader_get_header_fields(fp, hentry);
1446 if (hentry[0].body != NULL) {
1447 tmp = conv_unmime_header(hentry[0].body, NULL, FALSE);
1448 g_free(hentry[0].body);
1449 hentry[0].body = tmp;
1451 if (hentry[2].body != NULL) {
1452 tmp = conv_unmime_header(hentry[2].body, NULL, FALSE);
1453 g_free(hentry[2].body);
1454 hentry[2].body = tmp;
1456 if (hentry[4].body != NULL) {
1457 tmp = conv_unmime_header(hentry[4].body, NULL, FALSE);
1458 g_free(hentry[4].body);
1459 hentry[4].body = tmp;
1461 if (hentry[5].body != NULL) {
1462 tmp = conv_unmime_header(hentry[5].body, NULL, FALSE);
1463 g_free(hentry[5].body);
1464 hentry[5].body = tmp;
1466 if (hentry[7].body != NULL) {
1467 tmp = conv_unmime_header(hentry[7].body, NULL, FALSE);
1468 g_free(hentry[7].body);
1469 hentry[7].body = tmp;
1471 if (hentry[8].body != NULL) {
1472 tmp = conv_unmime_header(hentry[8].body, NULL, FALSE);
1473 g_free(hentry[8].body);
1474 hentry[8].body = tmp;
1477 content_start = ftell(fp);
1478 procmime_fclose(fp);
1480 len = mimeinfo->length - (content_start - mimeinfo->offset);
1483 procmime_parse_mimepart(mimeinfo,
1484 hentry[0].body, hentry[1].body,
1485 hentry[2].body, hentry[3].body,
1486 hentry[4].body, hentry[5].body,
1487 hentry[7].body, hentry[8].body,
1488 mimeinfo->data.filename, content_start,
1491 for (i = 0; i < (sizeof hentry / sizeof hentry[0]); i++) {
1492 g_free(hentry[i].body);
1493 hentry[i].body = NULL;
1497 static void procmime_parse_disposition_notification(MimeInfo *mimeinfo,
1498 const gchar *original_msgid, const gchar *disposition_notification_hdr,
1499 gboolean short_scan)
1501 HeaderEntry hentry[] = {{"Original-Message-ID:", NULL, TRUE},
1502 {"Disposition:", NULL, TRUE},
1503 {NULL, NULL, FALSE}};
1506 gchar *orig_msg_id = NULL;
1509 procmime_decode_content(mimeinfo);
1511 debug_print("parse disposition notification\n");
1512 fp = procmime_fopen(mimeinfo->data.filename, "rb");
1514 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
1517 fseek(fp, mimeinfo->offset, SEEK_SET);
1519 if (original_msgid && disposition_notification_hdr) {
1520 hentry[0].body = g_strdup(original_msgid);
1521 hentry[1].body = g_strdup(disposition_notification_hdr);
1523 procheader_get_header_fields(fp, hentry);
1526 procmime_fclose(fp);
1528 if (!hentry[0].body || !hentry[1].body) {
1529 debug_print("MsgId %s, Disp %s\n",
1530 hentry[0].body ? hentry[0].body:"(nil)",
1531 hentry[1].body ? hentry[1].body:"(nil)");
1535 orig_msg_id = g_strdup(hentry[0].body);
1536 disp = g_strdup(hentry[1].body);
1538 extract_parenthesis(orig_msg_id, '<', '>');
1539 remove_space(orig_msg_id);
1541 if (strstr(disp, "displayed")) {
1542 /* find sent message, if possible */
1543 MsgInfo *info = NULL;
1545 debug_print("%s has been displayed.\n", orig_msg_id);
1546 for (flist = folder_get_list(); flist != NULL; flist = g_list_next(flist)) {
1547 FolderItem *outbox = ((Folder *)(flist->data))->outbox;
1549 debug_print("skipping folder with no outbox...\n");
1552 info = folder_item_get_msginfo_by_msgid(outbox, orig_msg_id);
1553 debug_print("%s %s in %s\n", info?"found":"didn't find", orig_msg_id, outbox->path);
1555 procmsg_msginfo_set_flags(info, MSG_RETRCPT_GOT, 0);
1556 procmsg_msginfo_free(info);
1560 g_free(orig_msg_id);
1563 for (i = 0; i < (sizeof hentry / sizeof hentry[0]); i++) {
1564 g_free(hentry[i].body);
1565 hentry[i].body = NULL;
1569 #define GET_HEADERS() { \
1570 procheader_get_header_fields(fp, hentry); \
1571 if (hentry[0].body != NULL) { \
1572 tmp = conv_unmime_header(hentry[0].body, NULL, FALSE); \
1573 g_free(hentry[0].body); \
1574 hentry[0].body = tmp; \
1576 if (hentry[2].body != NULL) { \
1577 tmp = conv_unmime_header(hentry[2].body, NULL, FALSE); \
1578 g_free(hentry[2].body); \
1579 hentry[2].body = tmp; \
1581 if (hentry[4].body != NULL) { \
1582 tmp = conv_unmime_header(hentry[4].body, NULL, FALSE); \
1583 g_free(hentry[4].body); \
1584 hentry[4].body = tmp; \
1586 if (hentry[5].body != NULL) { \
1587 tmp = conv_unmime_header(hentry[5].body, NULL, FALSE); \
1588 g_free(hentry[5].body); \
1589 hentry[5].body = tmp; \
1591 if (hentry[6].body != NULL) { \
1592 tmp = conv_unmime_header(hentry[6].body, NULL, FALSE); \
1593 g_free(hentry[6].body); \
1594 hentry[6].body = tmp; \
1596 if (hentry[7].body != NULL) { \
1597 tmp = conv_unmime_header(hentry[7].body, NULL, FALSE); \
1598 g_free(hentry[7].body); \
1599 hentry[7].body = tmp; \
1603 static void procmime_parse_multipart(MimeInfo *mimeinfo, gboolean short_scan)
1605 HeaderEntry hentry[] = {{"Content-Type:", NULL, TRUE},
1606 {"Content-Transfer-Encoding:",
1608 {"Content-Description:",
1612 {"Content-Disposition:",
1614 {"Content-Location:",
1616 {"Original-Message-ID:",
1620 {NULL, NULL, FALSE}};
1623 gint boundary_len = 0, lastoffset = -1, i;
1624 gchar buf[BUFFSIZE];
1627 gboolean start_found = FALSE;
1628 gboolean end_found = FALSE;
1630 boundary = g_hash_table_lookup(mimeinfo->typeparameters, "boundary");
1633 boundary_len = strlen(boundary);
1635 procmime_decode_content(mimeinfo);
1637 fp = procmime_fopen(mimeinfo->data.filename, "rb");
1639 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
1643 fseek(fp, mimeinfo->offset, SEEK_SET);
1644 while ((p = SC_FGETS(buf, sizeof(buf), fp)) != NULL && result == 0) {
1645 if (ftell(fp) - 1 > (mimeinfo->offset + mimeinfo->length))
1648 if (IS_BOUNDARY(buf, boundary, boundary_len)) {
1651 if (lastoffset != -1) {
1652 gint len = (ftell(fp) - strlen(buf)) - lastoffset - 1;
1655 result = procmime_parse_mimepart(mimeinfo,
1656 hentry[0].body, hentry[1].body,
1657 hentry[2].body, hentry[3].body,
1658 hentry[4].body, hentry[5].body,
1659 hentry[6].body, hentry[7].body,
1660 mimeinfo->data.filename, lastoffset,
1662 if (result == 1 && short_scan)
1667 if (buf[2 + boundary_len] == '-' &&
1668 buf[2 + boundary_len + 1] == '-') {
1672 for (i = 0; i < (sizeof hentry / sizeof hentry[0]) ; i++) {
1673 g_free(hentry[i].body);
1674 hentry[i].body = NULL;
1677 lastoffset = ftell(fp);
1681 if (start_found && !end_found && lastoffset != -1) {
1682 gint len = (ftell(fp) - strlen(buf)) - lastoffset - 1;
1685 result = procmime_parse_mimepart(mimeinfo,
1686 hentry[0].body, hentry[1].body,
1687 hentry[2].body, hentry[3].body,
1688 hentry[4].body, hentry[5].body,
1689 hentry[6].body, hentry[7].body,
1690 mimeinfo->data.filename, lastoffset,
1693 mimeinfo->broken = TRUE;
1696 for (i = 0; i < (sizeof hentry / sizeof hentry[0]); i++) {
1697 g_free(hentry[i].body);
1698 hentry[i].body = NULL;
1700 procmime_fclose(fp);
1703 static void parse_parameters(const gchar *parameters, GHashTable *table)
1705 gchar *params, *param, *next;
1706 GSList *convlist = NULL, *concatlist = NULL, *cur;
1708 params = g_strdup(parameters);
1711 for (; next != NULL; param = next) {
1712 gchar *attribute, *value, *tmp, *down_attr, *orig_down_attr;
1714 gboolean convert = FALSE;
1716 next = strchr_with_skip_quote(param, '"', ';');
1725 value = strchr(attribute, '=');
1731 while (value[0] == ' ')
1734 down_attr = g_utf8_strdown(attribute, -1);
1735 orig_down_attr = down_attr;
1737 len = down_attr ? strlen(down_attr):0;
1738 if (len > 0 && down_attr[len - 1] == '*') {
1739 gchar *srcpos, *dstpos, *endpos;
1742 down_attr[len - 1] = '\0';
1746 endpos = value + strlen(value);
1747 while (srcpos < endpos) {
1753 if (!get_hex_value(&dstvalue, srcpos[1], srcpos[2]))
1763 if (value[0] == '"')
1764 extract_quote(value, '"');
1766 if (value[0] == '"')
1767 extract_quote(value, '"');
1768 else if ((tmp = strchr(value, ' ')) != NULL)
1773 while (down_attr[0] == ' ')
1775 while (down_attr[strlen(down_attr)-1] == ' ')
1776 down_attr[strlen(down_attr)-1] = '\0';
1779 while (value[0] == ' ')
1781 while (value[strlen(value)-1] == ' ')
1782 value[strlen(value)-1] = '\0';
1784 if (down_attr && strrchr(down_attr, '*') != NULL) {
1787 tmpattr = g_strdup(down_attr);
1788 tmp = strrchr(tmpattr, '*');
1791 if ((tmp[1] == '0') && (tmp[2] == '\0') &&
1792 (g_slist_find_custom(concatlist, down_attr, (GCompareFunc)g_strcmp0) == NULL))
1793 concatlist = g_slist_prepend(concatlist, g_strdup(tmpattr));
1795 if (convert && (g_slist_find_custom(convlist, tmpattr, (GCompareFunc)g_strcmp0) == NULL))
1796 convlist = g_slist_prepend(convlist, g_strdup(tmpattr));
1799 } else if (convert) {
1800 if (g_slist_find_custom(convlist, down_attr, (GCompareFunc)g_strcmp0) == NULL)
1801 convlist = g_slist_prepend(convlist, g_strdup(down_attr));
1804 if (g_hash_table_lookup(table, down_attr) == NULL)
1805 g_hash_table_insert(table, g_strdup(down_attr), g_strdup(value));
1806 g_free(orig_down_attr);
1809 for (cur = concatlist; cur != NULL; cur = g_slist_next(cur)) {
1810 gchar *attribute, *attrwnum, *partvalue;
1814 attribute = (gchar *) cur->data;
1815 value = g_string_sized_new(64);
1817 attrwnum = g_strdup_printf("%s*%d", attribute, n);
1818 while ((partvalue = g_hash_table_lookup(table, attrwnum)) != NULL) {
1819 g_string_append(value, partvalue);
1821 g_hash_table_remove(table, attrwnum);
1824 attrwnum = g_strdup_printf("%s*%d", attribute, n);
1828 g_hash_table_insert(table, g_strdup(attribute), g_strdup(value->str));
1829 g_string_free(value, TRUE);
1831 slist_free_strings_full(concatlist);
1833 for (cur = convlist; cur != NULL; cur = g_slist_next(cur)) {
1834 gchar *attribute, *key, *value;
1835 gchar *charset, *lang, *oldvalue, *newvalue;
1837 attribute = (gchar *) cur->data;
1838 if (!g_hash_table_lookup_extended(
1839 table, attribute, (gpointer *)(gchar *) &key, (gpointer *)(gchar *) &value))
1843 if (charset == NULL)
1845 lang = strchr(charset, '\'');
1850 oldvalue = strchr(lang, '\'');
1851 if (oldvalue == NULL)
1856 newvalue = conv_codeset_strdup(oldvalue, charset, CS_UTF_8);
1858 g_hash_table_remove(table, attribute);
1862 g_hash_table_insert(table, g_strdup(attribute), newvalue);
1864 slist_free_strings_full(convlist);
1869 static void procmime_parse_content_type(const gchar *content_type, MimeInfo *mimeinfo)
1871 cm_return_if_fail(content_type != NULL);
1872 cm_return_if_fail(mimeinfo != NULL);
1874 /* RFC 2045, page 13 says that the mime subtype is MANDATORY;
1875 * if it's not available we use the default Content-Type */
1876 if ((content_type[0] == '\0') || (strchr(content_type, '/') == NULL)) {
1877 mimeinfo->type = MIMETYPE_TEXT;
1878 mimeinfo->subtype = g_strdup("plain");
1879 if (g_hash_table_lookup(mimeinfo->typeparameters,
1880 "charset") == NULL) {
1881 g_hash_table_insert(mimeinfo->typeparameters,
1882 g_strdup("charset"),
1884 conv_get_locale_charset_str_no_utf8()));
1887 gchar *type, *subtype, *params;
1889 type = g_strdup(content_type);
1890 subtype = strchr(type, '/') + 1;
1891 *(subtype - 1) = '\0';
1892 if ((params = strchr(subtype, ';')) != NULL) {
1897 mimeinfo->type = procmime_get_media_type(type);
1898 mimeinfo->subtype = g_strstrip(g_strdup(subtype));
1900 /* Get mimeinfo->typeparameters */
1902 parse_parameters(params, mimeinfo->typeparameters);
1908 static void procmime_parse_content_disposition(const gchar *content_disposition, MimeInfo *mimeinfo)
1910 gchar *tmp, *params;
1912 cm_return_if_fail(content_disposition != NULL);
1913 cm_return_if_fail(mimeinfo != NULL);
1915 tmp = g_strdup(content_disposition);
1916 if ((params = strchr(tmp, ';')) != NULL) {
1922 if (!g_ascii_strcasecmp(tmp, "inline"))
1923 mimeinfo->disposition = DISPOSITIONTYPE_INLINE;
1924 else if (!g_ascii_strcasecmp(tmp, "attachment"))
1925 mimeinfo->disposition = DISPOSITIONTYPE_ATTACHMENT;
1927 mimeinfo->disposition = DISPOSITIONTYPE_ATTACHMENT;
1930 parse_parameters(params, mimeinfo->dispositionparameters);
1936 static void procmime_parse_content_encoding(const gchar *content_encoding, MimeInfo *mimeinfo)
1938 struct EncodingTable *enc_table;
1940 for (enc_table = encoding_table; enc_table->str != NULL; enc_table++) {
1941 if (g_ascii_strcasecmp(enc_table->str, content_encoding) == 0) {
1942 mimeinfo->encoding_type = enc_table->enc_type;
1946 mimeinfo->encoding_type = ENC_UNKNOWN;
1950 static GSList *registered_parsers = NULL;
1952 static MimeParser *procmime_get_mimeparser_for_type(MimeMediaType type, const gchar *sub_type)
1955 for (cur = registered_parsers; cur; cur = cur->next) {
1956 MimeParser *parser = (MimeParser *)cur->data;
1957 if (parser->type == type && !strcmp2(parser->sub_type, sub_type))
1963 void procmime_mimeparser_register(MimeParser *parser)
1965 if (!procmime_get_mimeparser_for_type(parser->type, parser->sub_type))
1966 registered_parsers = g_slist_append(registered_parsers, parser);
1970 void procmime_mimeparser_unregister(MimeParser *parser)
1972 registered_parsers = g_slist_remove(registered_parsers, parser);
1975 static gboolean procmime_mimeparser_parse(MimeParser *parser, MimeInfo *mimeinfo)
1977 cm_return_val_if_fail(parser->parse != NULL, FALSE);
1978 return parser->parse(parser, mimeinfo);
1981 static int procmime_parse_mimepart(MimeInfo *parent,
1982 gchar *content_type,
1983 gchar *content_encoding,
1984 gchar *content_description,
1986 gchar *content_disposition,
1987 gchar *content_location,
1988 const gchar *original_msgid,
1989 const gchar *disposition_notification_hdr,
1990 const gchar *filename,
1993 gboolean short_scan)
1996 MimeParser *parser = NULL;
1997 gboolean parsed = FALSE;
2000 /* Create MimeInfo */
2001 mimeinfo = procmime_mimeinfo_new();
2002 mimeinfo->content = MIMECONTENT_FILE;
2004 if (parent != NULL) {
2005 if (g_node_depth(parent->node) > 32) {
2006 /* 32 is an arbitrary value
2007 * this avoids DOSsing ourselves
2008 * with enormous messages
2010 procmime_mimeinfo_free_all(mimeinfo);
2013 g_node_append(parent->node, mimeinfo->node);
2015 mimeinfo->data.filename = g_strdup(filename);
2016 mimeinfo->offset = offset;
2017 mimeinfo->length = length;
2019 if (content_type != NULL) {
2020 procmime_parse_content_type(content_type, mimeinfo);
2022 mimeinfo->type = MIMETYPE_TEXT;
2023 mimeinfo->subtype = g_strdup("plain");
2024 if (g_hash_table_lookup(mimeinfo->typeparameters,
2025 "charset") == NULL) {
2026 g_hash_table_insert(mimeinfo->typeparameters,
2027 g_strdup("charset"),
2029 conv_get_locale_charset_str_no_utf8()));
2033 if (content_encoding != NULL) {
2034 procmime_parse_content_encoding(content_encoding, mimeinfo);
2036 mimeinfo->encoding_type = ENC_UNKNOWN;
2039 if (content_description != NULL)
2040 mimeinfo->description = g_strdup(content_description);
2042 mimeinfo->description = NULL;
2044 if (content_id != NULL)
2045 mimeinfo->id = g_strdup(content_id);
2047 mimeinfo->id = NULL;
2049 if (content_location != NULL)
2050 mimeinfo->location = g_strdup(content_location);
2052 mimeinfo->location = NULL;
2054 if (content_disposition != NULL)
2055 procmime_parse_content_disposition(content_disposition, mimeinfo);
2057 mimeinfo->disposition = DISPOSITIONTYPE_UNKNOWN;
2059 /* Call parser for mime type */
2060 if ((parser = procmime_get_mimeparser_for_type(mimeinfo->type, mimeinfo->subtype)) != NULL) {
2061 parsed = procmime_mimeparser_parse(parser, mimeinfo);
2064 switch (mimeinfo->type) {
2066 if (g_ascii_strcasecmp(mimeinfo->subtype, "plain") == 0 && short_scan) {
2071 case MIMETYPE_MESSAGE:
2072 if (g_ascii_strcasecmp(mimeinfo->subtype, "rfc822") == 0) {
2073 procmime_parse_message_rfc822(mimeinfo, short_scan);
2075 if (g_ascii_strcasecmp(mimeinfo->subtype, "disposition-notification") == 0) {
2076 procmime_parse_disposition_notification(mimeinfo,
2077 original_msgid, disposition_notification_hdr, short_scan);
2081 case MIMETYPE_MULTIPART:
2082 procmime_parse_multipart(mimeinfo, short_scan);
2085 case MIMETYPE_APPLICATION:
2086 if (g_ascii_strcasecmp(mimeinfo->subtype, "octet-stream") == 0
2087 && original_msgid && *original_msgid
2088 && disposition_notification_hdr && *disposition_notification_hdr) {
2089 procmime_parse_disposition_notification(mimeinfo,
2090 original_msgid, disposition_notification_hdr, short_scan);
2101 static gchar *typenames[] = {
2112 static gboolean output_func(GNode *node, gpointer data)
2115 MimeInfo *mimeinfo = (MimeInfo *) node->data;
2117 depth = g_node_depth(node);
2118 for (i = 0; i < depth; i++)
2120 g_print("%s/%s (offset:%d length:%d encoding: %d)\n", typenames[mimeinfo->type], mimeinfo->subtype, mimeinfo->offset, mimeinfo->length, mimeinfo->encoding_type);
2125 static void output_mime_structure(MimeInfo *mimeinfo, int indent)
2127 g_node_traverse(mimeinfo->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1, output_func, NULL);
2130 static MimeInfo *procmime_scan_file_with_offset(const gchar *filename, int offset, gboolean short_scan)
2135 g_stat(filename, &buf);
2137 mimeinfo = procmime_mimeinfo_new();
2138 mimeinfo->content = MIMECONTENT_FILE;
2139 mimeinfo->encoding_type = ENC_UNKNOWN;
2140 mimeinfo->type = MIMETYPE_MESSAGE;
2141 mimeinfo->subtype = g_strdup("rfc822");
2142 mimeinfo->data.filename = g_strdup(filename);
2143 mimeinfo->offset = offset;
2144 mimeinfo->length = buf.st_size - offset;
2146 procmime_parse_message_rfc822(mimeinfo, short_scan);
2147 if (debug_get_mode())
2148 output_mime_structure(mimeinfo, 0);
2153 static MimeInfo *procmime_scan_file_full(const gchar *filename, gboolean short_scan)
2157 cm_return_val_if_fail(filename != NULL, NULL);
2159 mimeinfo = procmime_scan_file_with_offset(filename, 0, short_scan);
2164 MimeInfo *procmime_scan_file(const gchar *filename)
2166 return procmime_scan_file_full(filename, FALSE);
2169 static MimeInfo *procmime_scan_file_short(const gchar *filename)
2171 return procmime_scan_file_full(filename, TRUE);
2174 static MimeInfo *procmime_scan_queue_file_full(const gchar *filename, gboolean short_scan)
2178 gchar buf[BUFFSIZE];
2181 cm_return_val_if_fail(filename != NULL, NULL);
2184 if ((fp = procmime_fopen(filename, "rb")) == NULL)
2186 /* Skip queue header */
2187 while (SC_FGETS(buf, sizeof(buf), fp) != NULL) {
2189 if ((!strncmp(buf, "X-Claws-End-Special-Headers: 1",
2190 strlen("X-Claws-End-Special-Headers:"))) ||
2191 (!strncmp(buf, "X-Sylpheed-End-Special-Headers: 1",
2192 strlen("X-Sylpheed-End-Special-Headers:"))))
2195 if (buf[0] == '\r' || buf[0] == '\n') break;
2196 /* from other mailers */
2197 if (!strncmp(buf, "Date: ", 6)
2198 || !strncmp(buf, "To: ", 4)
2199 || !strncmp(buf, "From: ", 6)
2200 || !strncmp(buf, "Subject: ", 9)) {
2206 procmime_fclose(fp);
2208 mimeinfo = procmime_scan_file_with_offset(filename, offset, short_scan);
2213 MimeInfo *procmime_scan_queue_file(const gchar *filename)
2215 return procmime_scan_queue_file_full(filename, FALSE);
2218 static MimeInfo *procmime_scan_queue_file_short(const gchar *filename)
2220 return procmime_scan_queue_file_full(filename, TRUE);
2225 ENC_AS_QUOTED_STRING,
2230 typedef struct _ParametersData {
2236 static void write_parameters(gpointer key, gpointer value, gpointer user_data)
2239 gchar *val = value, *valpos, *tmp;
2240 ParametersData *pdata = (ParametersData *)user_data;
2241 GString *buf = g_string_new("");
2244 EncodeAs encas = ENC_AS_TOKEN;
2246 for (valpos = val; *valpos != 0; valpos++) {
2247 if (!IS_ASCII(*valpos)) {
2248 encas = ENC_AS_ENCWORD;
2253 if (((*valpos >= 0) && (*valpos < 037)) || (*valpos == 0177)) {
2254 encas = ENC_AS_QUOTED_STRING;
2258 /* tspecials + SPACE */
2276 encas = ENC_AS_QUOTED_STRING;
2283 g_string_append_printf(buf, "%s=%s", param, val);
2286 case ENC_AS_QUOTED_STRING:
2287 g_string_append_printf(buf, "%s=\"%s\"", param, val);
2290 #if 0 /* we don't use that for now */
2291 case ENC_AS_EXTENDED:
2292 if (!g_utf8_validate(val, -1, NULL))
2293 g_string_append_printf(buf, "%s*=%s''", param,
2294 conv_get_locale_charset_str());
2296 g_string_append_printf(buf, "%s*=%s''", param,
2298 for (valpos = val; *valpos != '\0'; valpos++) {
2299 if (IS_ASCII(*valpos) && isalnum(*valpos)) {
2300 g_string_append_printf(buf, "%c", *valpos);
2302 gchar hexstr[3] = "XX";
2303 get_hex_str(hexstr, *valpos);
2304 g_string_append_printf(buf, "%%%s", hexstr);
2309 case ENC_AS_EXTENDED:
2310 debug_print("Unhandled ENC_AS_EXTENDED.");
2313 case ENC_AS_ENCWORD:
2314 len = MAX(strlen(val)*6, 512);
2315 tmp = g_malloc(len+1);
2316 codeconv_set_strict(TRUE);
2317 conv_encode_header_full(tmp, len, val, pdata->len + strlen(param) + 4 , FALSE,
2318 prefs_common.outgoing_charset);
2319 codeconv_set_strict(FALSE);
2320 if (!tmp || !*tmp) {
2321 codeconv_set_strict(TRUE);
2322 conv_encode_header_full(tmp, len, val, pdata->len + strlen(param) + 4 , FALSE,
2323 conv_get_outgoing_charset_str());
2324 codeconv_set_strict(FALSE);
2326 if (!tmp || !*tmp) {
2327 codeconv_set_strict(TRUE);
2328 conv_encode_header_full(tmp, len, val, pdata->len + strlen(param) + 4 , FALSE,
2330 codeconv_set_strict(FALSE);
2332 if (!tmp || !*tmp) {
2333 conv_encode_header_full(tmp, len, val, pdata->len + strlen(param) + 4 , FALSE,
2336 g_string_append_printf(buf, "%s=\"%s\"", param, tmp);
2342 if (buf->str && strlen(buf->str)) {
2343 tmp = strstr(buf->str, "\n");
2345 len = (tmp - buf->str);
2347 len = strlen(buf->str);
2348 if (pdata->len + len > 76) {
2349 if (fprintf(pdata->fp, ";\n %s", buf->str) < 0)
2350 pdata->error = TRUE;
2351 pdata->len = strlen(buf->str) + 1;
2353 if (fprintf(pdata->fp, "; %s", buf->str) < 0)
2354 pdata->error = TRUE;
2355 pdata->len += strlen(buf->str) + 2;
2358 g_string_free(buf, TRUE);
2361 #define TRY(func) { \
2367 int procmime_write_mime_header(MimeInfo *mimeinfo, FILE *fp)
2369 struct TypeTable *type_table;
2370 ParametersData *pdata = g_new0(ParametersData, 1);
2371 debug_print("procmime_write_mime_header\n");
2374 pdata->error = FALSE;
2375 for (type_table = mime_type_table; type_table->str != NULL; type_table++)
2376 if (mimeinfo->type == type_table->type) {
2377 gchar *buf = g_strdup_printf(
2378 "Content-Type: %s/%s", type_table->str, mimeinfo->subtype);
2379 if (fprintf(fp, "%s", buf) < 0) {
2384 pdata->len = strlen(buf);
2388 g_hash_table_foreach(mimeinfo->typeparameters, write_parameters, pdata);
2389 if (pdata->error == TRUE) {
2395 TRY(fprintf(fp, "\n") >= 0);
2397 if (mimeinfo->encoding_type != ENC_UNKNOWN)
2398 TRY(fprintf(fp, "Content-Transfer-Encoding: %s\n", procmime_get_encoding_str(mimeinfo->encoding_type)) >= 0);
2400 if (mimeinfo->description != NULL)
2401 TRY(fprintf(fp, "Content-Description: %s\n", mimeinfo->description) >= 0);
2403 if (mimeinfo->id != NULL)
2404 TRY(fprintf(fp, "Content-ID: %s\n", mimeinfo->id) >= 0);
2406 if (mimeinfo->location != NULL)
2407 TRY(fprintf(fp, "Content-Location: %s\n", mimeinfo->location) >= 0);
2409 if (mimeinfo->disposition != DISPOSITIONTYPE_UNKNOWN) {
2410 ParametersData *pdata = g_new0(ParametersData, 1);
2412 if (mimeinfo->disposition == DISPOSITIONTYPE_INLINE)
2413 buf = g_strdup("Content-Disposition: inline");
2414 else if (mimeinfo->disposition == DISPOSITIONTYPE_ATTACHMENT)
2415 buf = g_strdup("Content-Disposition: attachment");
2417 buf = g_strdup("Content-Disposition: unknown");
2419 if (fprintf(fp, "%s", buf) < 0) {
2424 pdata->len = strlen(buf);
2428 pdata->error = FALSE;
2429 g_hash_table_foreach(mimeinfo->dispositionparameters, write_parameters, pdata);
2430 if (pdata->error == TRUE) {
2435 TRY(fprintf(fp, "\n") >= 0);
2438 TRY(fprintf(fp, "\n") >= 0);
2443 static gint procmime_write_message_rfc822(MimeInfo *mimeinfo, FILE *fp)
2448 gchar buf[BUFFSIZE];
2449 gboolean skip = FALSE;;
2452 debug_print("procmime_write_message_rfc822\n");
2455 switch (mimeinfo->content) {
2456 case MIMECONTENT_FILE:
2457 if ((infp = procmime_fopen(mimeinfo->data.filename, "rb")) == NULL) {
2458 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
2461 fseek(infp, mimeinfo->offset, SEEK_SET);
2462 while (SC_FGETS(buf, sizeof(buf), infp) == buf) {
2464 if (buf[0] == '\n' && buf[1] == '\0')
2466 if (skip && (buf[0] == ' ' || buf[0] == '\t'))
2468 if (g_ascii_strncasecmp(buf, "Mime-Version:", 13) == 0 ||
2469 g_ascii_strncasecmp(buf, "Content-Type:", 13) == 0 ||
2470 g_ascii_strncasecmp(buf, "Content-Transfer-Encoding:", 26) == 0 ||
2471 g_ascii_strncasecmp(buf, "Content-Description:", 20) == 0 ||
2472 g_ascii_strncasecmp(buf, "Content-ID:", 11) == 0 ||
2473 g_ascii_strncasecmp(buf, "Content-Location:", 17) == 0 ||
2474 g_ascii_strncasecmp(buf, "Content-Disposition:", 20) == 0) {
2479 if (SC_FWRITE(buf, sizeof(gchar), len, fp) < len) {
2480 g_warning("failed to dump %zd bytes from file", len);
2481 procmime_fclose(infp);
2486 procmime_fclose(infp);
2489 case MIMECONTENT_MEM:
2490 len = strlen(mimeinfo->data.mem);
2491 if (SC_FWRITE(mimeinfo->data.mem, sizeof(gchar), len, fp) < len) {
2492 g_warning("failed to dump %zd bytes from mem", len);
2501 childnode = mimeinfo->node->children;
2502 if (childnode == NULL)
2505 child = (MimeInfo *) childnode->data;
2506 if (fprintf(fp, "Mime-Version: 1.0\n") < 0) {
2507 g_warning("failed to write mime version");
2510 if (procmime_write_mime_header(child, fp) < 0)
2512 return procmime_write_mimeinfo(child, fp);
2515 static gint procmime_write_multipart(MimeInfo *mimeinfo, FILE *fp)
2519 gchar *boundary, *str, *str2;
2520 gchar buf[BUFFSIZE];
2521 gboolean firstboundary;
2524 debug_print("procmime_write_multipart\n");
2526 boundary = g_hash_table_lookup(mimeinfo->typeparameters, "boundary");
2528 switch (mimeinfo->content) {
2529 case MIMECONTENT_FILE:
2530 if ((infp = procmime_fopen(mimeinfo->data.filename, "rb")) == NULL) {
2531 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
2534 fseek(infp, mimeinfo->offset, SEEK_SET);
2535 while (SC_FGETS(buf, sizeof(buf), infp) == buf) {
2536 if (IS_BOUNDARY(buf, boundary, strlen(boundary)))
2539 if (SC_FWRITE(buf, sizeof(gchar), len, fp) < len) {
2540 g_warning("failed to write %zd", len);
2541 procmime_fclose(infp);
2545 procmime_fclose(infp);
2548 case MIMECONTENT_MEM:
2549 str = g_strdup(mimeinfo->data.mem);
2550 if (((str2 = strstr(str, boundary)) != NULL) && ((str2 - str) >= 2) &&
2551 (*(str2 - 1) == '-') && (*(str2 - 2) == '-'))
2554 if (SC_FWRITE(str, sizeof(gchar), len, fp) < len) {
2555 g_warning("failed to write %zd from mem", len);
2566 childnode = mimeinfo->node->children;
2567 firstboundary = TRUE;
2568 while (childnode != NULL) {
2569 MimeInfo *child = childnode->data;
2572 firstboundary = FALSE;
2574 TRY(fprintf(fp, "\n") >= 0);
2576 TRY(fprintf(fp, "--%s\n", boundary) >= 0);
2578 if (procmime_write_mime_header(child, fp) < 0)
2580 if (procmime_write_mimeinfo(child, fp) < 0)
2583 childnode = g_node_next_sibling(childnode);
2585 TRY(fprintf(fp, "\n--%s--\n", boundary) >= 0);
2590 gint procmime_write_mimeinfo(MimeInfo *mimeinfo, FILE *fp)
2594 debug_print("procmime_write_mimeinfo\n");
2596 if (G_NODE_IS_LEAF(mimeinfo->node)) {
2597 switch (mimeinfo->content) {
2598 case MIMECONTENT_FILE:
2599 if ((infp = procmime_fopen(mimeinfo->data.filename, "rb")) == NULL) {
2600 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
2603 copy_file_part_to_fp(infp, mimeinfo->offset, mimeinfo->length, fp);
2604 procmime_fclose(infp);
2607 case MIMECONTENT_MEM:
2608 len = strlen(mimeinfo->data.mem);
2609 if (SC_FWRITE(mimeinfo->data.mem, sizeof(gchar), len, fp) < len)
2617 /* Call writer for mime type */
2618 switch (mimeinfo->type) {
2619 case MIMETYPE_MESSAGE:
2620 if (g_ascii_strcasecmp(mimeinfo->subtype, "rfc822") == 0) {
2621 return procmime_write_message_rfc822(mimeinfo, fp);
2625 case MIMETYPE_MULTIPART:
2626 return procmime_write_multipart(mimeinfo, fp);
2638 gchar *procmime_get_part_file_name(MimeInfo *mimeinfo)
2642 if ((mimeinfo->type == MIMETYPE_TEXT) && !g_ascii_strcasecmp(mimeinfo->subtype, "html"))
2643 base = g_strdup("mimetmp.html");
2645 const gchar *basetmp;
2648 basetmp = procmime_mimeinfo_get_parameter(mimeinfo, "filename");
2649 if (basetmp == NULL)
2650 basetmp = procmime_mimeinfo_get_parameter(mimeinfo, "name");
2651 if (basetmp == NULL)
2652 basetmp = "mimetmp";
2653 basename = g_path_get_basename(basetmp);
2654 if (*basename == '\0') {
2656 basename = g_strdup("mimetmp");
2658 base = conv_filename_from_utf8(basename);
2660 subst_for_shellsafe_filename(base);