2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2012 Hiroyuki Yamamoto & The Claws Mail Team
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "claws-features.h"
31 #include <glib/gi18n.h>
37 #include <sys/types.h>
43 #include "procheader.h"
45 #include "quoted-printable.h"
52 #include "prefs_common.h"
53 #include "prefs_gtk.h"
54 #include "alertpanel.h"
58 static GHashTable *procmime_get_mime_type_table (void);
59 static MimeInfo *procmime_scan_file_short(const gchar *filename);
60 static MimeInfo *procmime_scan_queue_file_short(const gchar *filename);
61 static MimeInfo *procmime_scan_queue_file_full(const gchar *filename, gboolean short_scan);
63 MimeInfo *procmime_mimeinfo_new(void)
67 mimeinfo = g_new0(MimeInfo, 1);
68 mimeinfo->content = MIMECONTENT_EMPTY;
69 mimeinfo->data.filename = NULL;
71 mimeinfo->type = MIMETYPE_UNKNOWN;
72 mimeinfo->encoding_type = ENC_UNKNOWN;
73 mimeinfo->typeparameters = g_hash_table_new(g_str_hash, g_str_equal);
75 mimeinfo->disposition = DISPOSITIONTYPE_UNKNOWN;
76 mimeinfo->dispositionparameters
77 = g_hash_table_new(g_str_hash, g_str_equal);
79 mimeinfo->node = g_node_new(mimeinfo);
84 static gboolean procmime_mimeinfo_parameters_destroy(gpointer key, gpointer value, gpointer user_data)
92 static gchar *forced_charset = NULL;
94 void procmime_force_charset(const gchar *str)
96 g_free(forced_charset);
97 forced_charset = NULL;
99 forced_charset = g_strdup(str);
102 static EncodingType forced_encoding = 0;
104 void procmime_force_encoding(EncodingType encoding)
106 forced_encoding = encoding;
109 static gboolean free_func(GNode *node, gpointer data)
111 MimeInfo *mimeinfo = (MimeInfo *) node->data;
113 switch (mimeinfo->content) {
114 case MIMECONTENT_FILE:
116 claws_unlink(mimeinfo->data.filename);
117 g_free(mimeinfo->data.filename);
120 case MIMECONTENT_MEM:
122 g_free(mimeinfo->data.mem);
127 g_free(mimeinfo->subtype);
128 g_free(mimeinfo->description);
129 g_free(mimeinfo->id);
130 g_free(mimeinfo->location);
132 g_hash_table_foreach_remove(mimeinfo->typeparameters,
133 procmime_mimeinfo_parameters_destroy, NULL);
134 g_hash_table_destroy(mimeinfo->typeparameters);
135 g_hash_table_foreach_remove(mimeinfo->dispositionparameters,
136 procmime_mimeinfo_parameters_destroy, NULL);
137 g_hash_table_destroy(mimeinfo->dispositionparameters);
139 if (mimeinfo->privacy)
140 privacy_free_privacydata(mimeinfo->privacy);
147 void procmime_mimeinfo_free_all(MimeInfo *mimeinfo)
154 node = mimeinfo->node;
155 g_node_traverse(node, G_IN_ORDER, G_TRAVERSE_ALL, -1, free_func, NULL);
157 g_node_destroy(node);
160 MimeInfo *procmime_mimeinfo_parent(MimeInfo *mimeinfo)
162 cm_return_val_if_fail(mimeinfo != NULL, NULL);
163 cm_return_val_if_fail(mimeinfo->node != NULL, NULL);
165 if (mimeinfo->node->parent == NULL)
167 return (MimeInfo *) mimeinfo->node->parent->data;
170 MimeInfo *procmime_mimeinfo_next(MimeInfo *mimeinfo)
172 cm_return_val_if_fail(mimeinfo != NULL, NULL);
173 cm_return_val_if_fail(mimeinfo->node != NULL, NULL);
175 if (mimeinfo->node->children)
176 return (MimeInfo *) mimeinfo->node->children->data;
177 if (mimeinfo->node->next)
178 return (MimeInfo *) mimeinfo->node->next->data;
180 if (mimeinfo->node->parent == NULL)
183 while (mimeinfo->node->parent != NULL) {
184 mimeinfo = (MimeInfo *) mimeinfo->node->parent->data;
185 if (mimeinfo->node->next)
186 return (MimeInfo *) mimeinfo->node->next->data;
192 MimeInfo *procmime_scan_message(MsgInfo *msginfo)
197 filename = procmsg_get_message_file_path(msginfo);
198 if (!filename || !is_file_exist(filename)) {
203 if (!folder_has_parent_of_type(msginfo->folder, F_QUEUE) &&
204 !folder_has_parent_of_type(msginfo->folder, F_DRAFT))
205 mimeinfo = procmime_scan_file(filename);
207 mimeinfo = procmime_scan_queue_file(filename);
213 MimeInfo *procmime_scan_message_short(MsgInfo *msginfo)
218 filename = procmsg_get_message_file_path(msginfo);
219 if (!filename || !is_file_exist(filename)) {
224 if (!folder_has_parent_of_type(msginfo->folder, F_QUEUE) &&
225 !folder_has_parent_of_type(msginfo->folder, F_DRAFT))
226 mimeinfo = procmime_scan_file_short(filename);
228 mimeinfo = procmime_scan_queue_file_short(filename);
236 H_CONTENT_TRANSFER_ENCODING = 0,
238 H_CONTENT_DISPOSITION = 2,
239 H_CONTENT_DESCRIPTION = 3,
243 const gchar *procmime_mimeinfo_get_parameter(MimeInfo *mimeinfo, const gchar *name)
247 cm_return_val_if_fail(mimeinfo != NULL, NULL);
248 cm_return_val_if_fail(name != NULL, NULL);
250 value = g_hash_table_lookup(mimeinfo->dispositionparameters, name);
252 value = g_hash_table_lookup(mimeinfo->typeparameters, name);
257 #ifdef HAVE_FGETS_UNLOCKED
258 #define SC_FGETS fgets_unlocked
259 #define SC_FPUTS fputs_unlocked
260 #define SC_FPUTC fputc_unlocked
261 #define SC_FREAD fread_unlocked
262 #define SC_FWRITE fwrite_unlocked
263 #define SC_FEOF feof_unlocked
264 #define SC_FERROR ferror_unlocked
266 static FILE *procmime_fopen(const gchar *file, const gchar *mode)
268 FILE *fp = g_fopen(file, mode);
274 static int procmime_fclose(FILE *fp)
280 #define SC_FGETS fgets
281 #define SC_FPUTS fputs
282 #define SC_FPUTC fputc
283 #define SC_FREAD fread
284 #define SC_FWRITE fwrite
286 #define SC_FERROR ferror
287 #define procmime_fopen g_fopen
288 #define procmime_fclose fclose
291 #define FLUSH_LASTLINE() { \
292 if (*lastline != '\0') { \
294 strretchomp(lastline); \
295 llen = strlen(lastline); \
296 if (lastline[llen-1] == ' ' && strcmp(lastline,"-- ")) { \
297 /* this is flowed */ \
299 lastline[llen-1] = '\0'; \
300 if (SC_FPUTS(lastline, outfp) == EOF) \
303 if (SC_FPUTS(lastline, outfp) == EOF) \
305 if (SC_FPUTS("\n", outfp) == EOF) \
309 strcpy(lastline, buf); \
312 gboolean procmime_decode_content(MimeInfo *mimeinfo)
319 gboolean tmp_file = FALSE;
320 gboolean flowed = FALSE;
321 gboolean delsp = FALSE;
322 gboolean err = FALSE;
324 EncodingType encoding = forced_encoding
326 : mimeinfo->encoding_type;
327 gchar lastline[BUFFSIZE];
328 memset(lastline, 0, BUFFSIZE);
330 cm_return_val_if_fail(mimeinfo != NULL, FALSE);
332 if (prefs_common.respect_flowed_format &&
333 mimeinfo->type == MIMETYPE_TEXT &&
334 !strcasecmp(mimeinfo->subtype, "plain")) {
335 if (procmime_mimeinfo_get_parameter(mimeinfo, "format") != NULL &&
336 !strcasecmp(procmime_mimeinfo_get_parameter(mimeinfo, "format"),"flowed"))
339 procmime_mimeinfo_get_parameter(mimeinfo, "delsp") != NULL &&
340 !strcasecmp(procmime_mimeinfo_get_parameter(mimeinfo, "delsp"),"yes"))
345 encoding == ENC_UNKNOWN ||
346 encoding == ENC_BINARY ||
347 encoding == ENC_7BIT ||
352 if (mimeinfo->type == MIMETYPE_MULTIPART || mimeinfo->type == MIMETYPE_MESSAGE)
355 infp = procmime_fopen(mimeinfo->data.filename, "rb");
360 fseek(infp, mimeinfo->offset, SEEK_SET);
362 outfp = get_tmpfile_in_dir(get_mime_tmp_dir(), &tmpfilename);
365 procmime_fclose(infp);
369 readend = mimeinfo->offset + mimeinfo->length;
371 if (encoding == ENC_QUOTED_PRINTABLE) {
372 while ((ftell(infp) < readend) && (SC_FGETS(buf, sizeof(buf), infp) != NULL)) {
374 len = qp_decode_line(buf);
377 if (SC_FWRITE(buf, 1, len, outfp) < len)
385 } else if (encoding == ENC_BASE64) {
386 gchar outbuf[BUFFSIZE];
387 gint len, inlen, inread;
388 Base64Decoder *decoder;
389 gboolean got_error = FALSE;
390 gboolean uncanonicalize = FALSE;
392 gboolean null_bytes = FALSE;
393 gboolean starting = TRUE;
395 if (mimeinfo->type == MIMETYPE_TEXT ||
396 mimeinfo->type == MIMETYPE_MESSAGE) {
397 uncanonicalize = TRUE;
398 tmpfp = my_tmpfile();
402 procmime_fclose(outfp);
403 procmime_fclose(infp);
406 #ifdef HAVE_FGETS_UNLOCKED
411 decoder = base64_decoder_new();
412 while ((inlen = MIN(readend - ftell(infp), sizeof(buf))) > 0 && !err) {
413 inread = SC_FREAD(buf, 1, inlen, infp);
414 len = base64_decoder_decode(decoder, buf, outbuf, inread);
415 if (uncanonicalize == TRUE && strlen(outbuf) < len && starting) {
416 uncanonicalize = FALSE;
420 if (((inread != inlen) || len < 0) && !got_error) {
421 g_warning("Bad BASE64 content.\n");
422 if (SC_FWRITE(_("[Error decoding BASE64]\n"),
424 strlen(_("[Error decoding BASE64]\n")),
425 tmpfp) < strlen(_("[Error decoding BASE64]\n")))
426 g_warning("error decoding BASE64");
429 } else if (len >= 0) {
430 /* print out the error message only once
433 /* we won't uncanonicalize, output to outfp directly */
434 if (SC_FWRITE(outbuf, sizeof(gchar), len, outfp) < len)
437 if (SC_FWRITE(outbuf, sizeof(gchar), len, tmpfp) < len)
443 base64_decoder_free(decoder);
445 if (uncanonicalize) {
447 while (SC_FGETS(buf, sizeof(buf), tmpfp) != NULL) {
449 if (SC_FPUTS(buf, outfp) == EOF)
452 procmime_fclose(tmpfp);
454 } else if (encoding == ENC_X_UUENCODE) {
455 gchar outbuf[BUFFSIZE];
457 gboolean flag = FALSE;
459 while ((ftell(infp) < readend) && (SC_FGETS(buf, sizeof(buf), infp) != NULL)) {
460 if (!flag && strncmp(buf,"begin ", 6)) continue;
463 len = fromuutobits(outbuf, buf);
466 g_warning("Bad UUENCODE content(%d)\n", len);
469 if (SC_FWRITE(outbuf, sizeof(gchar), len, outfp) < len)
475 while ((ftell(infp) < readend) && (SC_FGETS(buf, sizeof(buf), infp) != NULL)) {
477 if (SC_FPUTS(buf, outfp) == EOF)
486 g_warning("write error");
489 procmime_fclose(outfp);
490 procmime_fclose(infp);
496 g_stat(tmpfilename, &statbuf);
497 if (mimeinfo->tmp && (mimeinfo->data.filename != NULL))
498 claws_unlink(mimeinfo->data.filename);
499 g_free(mimeinfo->data.filename);
500 mimeinfo->data.filename = tmpfilename;
501 mimeinfo->tmp = TRUE;
502 mimeinfo->offset = 0;
503 mimeinfo->length = statbuf.st_size;
504 mimeinfo->encoding_type = ENC_BINARY;
509 #define B64_LINE_SIZE 57
510 #define B64_BUFFSIZE 77
512 gboolean procmime_encode_content(MimeInfo *mimeinfo, EncodingType encoding)
514 FILE *infp = NULL, *outfp;
518 gboolean err = FALSE;
520 if (mimeinfo->content == MIMECONTENT_EMPTY)
523 if (mimeinfo->encoding_type != ENC_UNKNOWN &&
524 mimeinfo->encoding_type != ENC_BINARY &&
525 mimeinfo->encoding_type != ENC_7BIT &&
526 mimeinfo->encoding_type != ENC_8BIT)
527 if(!procmime_decode_content(mimeinfo))
530 outfp = get_tmpfile_in_dir(get_mime_tmp_dir(), &tmpfilename);
535 #ifdef HAVE_FGETS_UNLOCKED
539 if (mimeinfo->content == MIMECONTENT_FILE && mimeinfo->data.filename) {
540 if ((infp = procmime_fopen(mimeinfo->data.filename, "rb")) == NULL) {
541 g_warning("Can't open file %s\n", mimeinfo->data.filename);
542 procmime_fclose(outfp);
545 } else if (mimeinfo->content == MIMECONTENT_MEM) {
546 infp = str_open_as_stream(mimeinfo->data.mem);
548 procmime_fclose(outfp);
551 #ifdef HAVE_FGETS_UNLOCKED
557 if (encoding == ENC_BASE64) {
558 gchar inbuf[B64_LINE_SIZE], outbuf[B64_BUFFSIZE];
560 gchar *tmp_file = NULL;
562 if (mimeinfo->type == MIMETYPE_TEXT ||
563 mimeinfo->type == MIMETYPE_MESSAGE) {
564 if (mimeinfo->content == MIMECONTENT_FILE) {
565 tmp_file = get_tmp_file();
566 if (canonicalize_file(mimeinfo->data.filename, tmp_file) < 0) {
568 procmime_fclose(infp);
569 procmime_fclose(outfp);
572 if ((tmp_fp = procmime_fopen(tmp_file, "rb")) == NULL) {
573 FILE_OP_ERROR(tmp_file, "fopen");
574 claws_unlink(tmp_file);
576 procmime_fclose(infp);
577 procmime_fclose(outfp);
581 gchar *out = canonicalize_str(mimeinfo->data.mem);
582 procmime_fclose(infp);
583 infp = str_open_as_stream(out);
587 procmime_fclose(outfp);
590 #ifdef HAVE_FGETS_UNLOCKED
596 while ((len = SC_FREAD(inbuf, sizeof(gchar),
597 B64_LINE_SIZE, tmp_fp))
599 base64_encode(outbuf, inbuf, B64_LINE_SIZE);
600 if (SC_FPUTS(outbuf, outfp) == EOF)
602 if (SC_FPUTC('\n', outfp) == EOF)
605 if (len > 0 && SC_FEOF(tmp_fp)) {
606 base64_encode(outbuf, inbuf, len);
607 if (SC_FPUTS(outbuf, outfp) == EOF)
609 if (SC_FPUTC('\n', outfp) == EOF)
614 procmime_fclose(tmp_fp);
615 claws_unlink(tmp_file);
618 } else if (encoding == ENC_QUOTED_PRINTABLE) {
619 gchar inbuf[BUFFSIZE], outbuf[BUFFSIZE * 4];
621 while (SC_FGETS(inbuf, sizeof(inbuf), infp) != NULL) {
622 qp_encode_line(outbuf, inbuf);
624 if (!strncmp("From ", outbuf, sizeof("From ")-1)) {
625 gchar *tmpbuf = outbuf;
627 tmpbuf += sizeof("From ")-1;
629 if (SC_FPUTS("=46rom ", outfp) == EOF)
631 if (SC_FPUTS(tmpbuf, outfp) == EOF)
634 if (SC_FPUTS(outbuf, outfp) == EOF)
641 while (SC_FGETS(buf, sizeof(buf), infp) != NULL) {
643 if (SC_FPUTS(buf, outfp) == EOF)
648 procmime_fclose(outfp);
649 procmime_fclose(infp);
654 if (mimeinfo->content == MIMECONTENT_FILE) {
655 if (mimeinfo->tmp && (mimeinfo->data.filename != NULL))
656 claws_unlink(mimeinfo->data.filename);
657 g_free(mimeinfo->data.filename);
658 } else if (mimeinfo->content == MIMECONTENT_MEM) {
659 if (mimeinfo->tmp && (mimeinfo->data.mem != NULL))
660 g_free(mimeinfo->data.mem);
663 g_stat(tmpfilename, &statbuf);
664 mimeinfo->content = MIMECONTENT_FILE;
665 mimeinfo->data.filename = tmpfilename;
666 mimeinfo->tmp = TRUE;
667 mimeinfo->offset = 0;
668 mimeinfo->length = statbuf.st_size;
669 mimeinfo->encoding_type = encoding;
674 gint procmime_get_part(const gchar *outfile, MimeInfo *mimeinfo)
678 gint restlength, readlength;
679 gint saved_errno = 0;
681 cm_return_val_if_fail(outfile != NULL, -1);
682 cm_return_val_if_fail(mimeinfo != NULL, -1);
684 if (mimeinfo->encoding_type != ENC_BINARY && !procmime_decode_content(mimeinfo))
687 if ((infp = procmime_fopen(mimeinfo->data.filename, "rb")) == NULL) {
689 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
690 return -(saved_errno);
692 if (fseek(infp, mimeinfo->offset, SEEK_SET) < 0) {
694 FILE_OP_ERROR(mimeinfo->data.filename, "fseek");
695 procmime_fclose(infp);
696 return -(saved_errno);
698 if ((outfp = procmime_fopen(outfile, "wb")) == NULL) {
700 FILE_OP_ERROR(outfile, "fopen");
701 procmime_fclose(infp);
702 return -(saved_errno);
705 restlength = mimeinfo->length;
707 while ((restlength > 0) && ((readlength = SC_FREAD(buf, 1, restlength > BUFFSIZE ? BUFFSIZE : restlength, infp)) > 0)) {
708 if (SC_FWRITE(buf, 1, readlength, outfp) != readlength) {
710 procmime_fclose(infp);
711 procmime_fclose(outfp);
712 return -(saved_errno);
714 restlength -= readlength;
717 procmime_fclose(infp);
718 if (procmime_fclose(outfp) == EOF) {
720 FILE_OP_ERROR(outfile, "fclose");
721 claws_unlink(outfile);
722 return -(saved_errno);
728 gboolean procmime_scan_text_content(MimeInfo *mimeinfo,
729 gboolean (*scan_callback)(const gchar *str, gpointer cb_data),
733 const gchar *src_codeset;
734 gboolean conv_fail = FALSE;
738 gboolean scan_ret = FALSE;
740 cm_return_val_if_fail(mimeinfo != NULL, TRUE);
741 cm_return_val_if_fail(scan_callback != NULL, TRUE);
743 if (!procmime_decode_content(mimeinfo))
746 tmpfile = procmime_get_tmp_file_name(mimeinfo);
750 if (procmime_get_part(tmpfile, mimeinfo) < 0) {
755 tmpfp = procmime_fopen(tmpfile, "rb");
761 src_codeset = forced_charset
763 procmime_mimeinfo_get_parameter(mimeinfo, "charset");
765 /* use supersets transparently when possible */
766 if (!forced_charset && src_codeset && !strcasecmp(src_codeset, CS_ISO_8859_1))
767 src_codeset = CS_WINDOWS_1252;
768 else if (!forced_charset && src_codeset && !strcasecmp(src_codeset, CS_X_GBK))
769 src_codeset = CS_GB18030;
770 else if (!forced_charset && src_codeset && !strcasecmp(src_codeset, CS_GBK))
771 src_codeset = CS_GB18030;
772 else if (!forced_charset && src_codeset && !strcasecmp(src_codeset, CS_GB2312))
773 src_codeset = CS_GB18030;
774 else if (!forced_charset && src_codeset && !strcasecmp(src_codeset, CS_X_VIET_VPS))
775 src_codeset = CS_WINDOWS_874;
777 if (mimeinfo->type == MIMETYPE_TEXT && !g_ascii_strcasecmp(mimeinfo->subtype, "html")) {
778 SC_HTMLParser *parser;
781 conv = conv_code_converter_new(src_codeset);
782 parser = sc_html_parser_new(tmpfp, conv);
783 while ((str = sc_html_parse(parser)) != NULL) {
784 if ((scan_ret = scan_callback(str, cb_data)) == TRUE)
787 sc_html_parser_destroy(parser);
788 conv_code_converter_destroy(conv);
789 } else if (mimeinfo->type == MIMETYPE_TEXT && !g_ascii_strcasecmp(mimeinfo->subtype, "enriched")) {
793 conv = conv_code_converter_new(src_codeset);
794 parser = ertf_parser_new(tmpfp, conv);
795 while ((str = ertf_parse(parser)) != NULL) {
796 if ((scan_ret = scan_callback(str, cb_data)) == TRUE)
799 ertf_parser_destroy(parser);
800 conv_code_converter_destroy(conv);
801 } else if (mimeinfo->type == MIMETYPE_TEXT) {
802 while (SC_FGETS(buf, sizeof(buf), tmpfp) != NULL) {
803 str = conv_codeset_strdup(buf, src_codeset, CS_UTF_8);
805 if ((scan_ret = scan_callback(str, cb_data)) == TRUE) {
812 if ((scan_ret = scan_callback(buf, cb_data)) == TRUE)
819 g_warning("procmime_get_text_content(): Code conversion failed.\n");
821 procmime_fclose(tmpfp);
822 claws_unlink(tmpfile);
828 static gboolean scan_fputs_cb(const gchar *str, gpointer fp)
830 if (SC_FPUTS(str, (FILE *)fp) == EOF)
836 FILE *procmime_get_text_content(MimeInfo *mimeinfo)
841 if ((outfp = my_tmpfile()) == NULL) {
845 #ifdef HAVE_FGETS_UNLOCKED
849 err = procmime_scan_text_content(mimeinfo, scan_fputs_cb, outfp);
853 procmime_fclose(outfp);
856 #ifdef HAVE_FGETS_UNLOCKED
863 FILE *procmime_get_binary_content(MimeInfo *mimeinfo)
868 cm_return_val_if_fail(mimeinfo != NULL, NULL);
870 if (!procmime_decode_content(mimeinfo))
873 tmpfile = procmime_get_tmp_file_name(mimeinfo);
877 if (procmime_get_part(tmpfile, mimeinfo) < 0) {
882 outfp = procmime_fopen(tmpfile, "rb");
892 #ifdef HAVE_FGETS_UNLOCKED
898 /* search the first text part of (multipart) MIME message,
899 decode, convert it and output to outfp. */
900 FILE *procmime_get_first_text_content(MsgInfo *msginfo)
903 MimeInfo *mimeinfo, *partinfo;
904 gboolean empty_ok = FALSE, short_scan = TRUE;
906 cm_return_val_if_fail(msginfo != NULL, NULL);
908 /* first we try to short-scan (for speed), refusing empty parts */
911 mimeinfo = procmime_scan_message_short(msginfo);
913 mimeinfo = procmime_scan_message(msginfo);
914 if (!mimeinfo) return NULL;
917 while (partinfo && (partinfo->type != MIMETYPE_TEXT ||
918 (partinfo->length == 0 && !empty_ok))) {
919 partinfo = procmime_mimeinfo_next(partinfo);
922 outfp = procmime_get_text_content(partinfo);
923 else if (!empty_ok && short_scan) {
924 /* if short scan didn't find a non-empty part, rescan
925 * fully for non-empty parts
928 procmime_mimeinfo_free_all(mimeinfo);
930 } else if (!empty_ok && !short_scan) {
931 /* if full scan didn't find a non-empty part, rescan
932 * accepting empty parts
935 procmime_mimeinfo_free_all(mimeinfo);
938 procmime_mimeinfo_free_all(mimeinfo);
940 /* outfp already unlocked at this time */
945 static gboolean find_encrypted_func(GNode *node, gpointer data)
947 MimeInfo *mimeinfo = (MimeInfo *) node->data;
948 MimeInfo **encinfo = (MimeInfo **) data;
950 if (privacy_mimeinfo_is_encrypted(mimeinfo)) {
958 static MimeInfo *find_encrypted_part(MimeInfo *rootinfo)
960 MimeInfo *encinfo = NULL;
962 g_node_traverse(rootinfo->node, G_IN_ORDER, G_TRAVERSE_ALL, -1,
963 find_encrypted_func, &encinfo);
968 /* search the first encrypted text part of (multipart) MIME message,
969 decode, convert it and output to outfp. */
970 FILE *procmime_get_first_encrypted_text_content(MsgInfo *msginfo)
973 MimeInfo *mimeinfo, *partinfo, *encinfo;
975 cm_return_val_if_fail(msginfo != NULL, NULL);
977 mimeinfo = procmime_scan_message(msginfo);
983 if ((encinfo = find_encrypted_part(partinfo)) != NULL) {
984 debug_print("decrypting message part\n");
985 if (privacy_mimeinfo_decrypt(encinfo) < 0) {
986 alertpanel_error(_("Couldn't decrypt: %s"),
987 privacy_get_error());
992 while (partinfo && partinfo->type != MIMETYPE_TEXT) {
993 partinfo = procmime_mimeinfo_next(partinfo);
994 if (privacy_mimeinfo_is_signed(partinfo))
995 procmsg_msginfo_set_flags(msginfo, 0, MSG_SIGNED);
999 outfp = procmime_get_text_content(partinfo);
1001 procmime_mimeinfo_free_all(mimeinfo);
1003 /* outfp already unlocked at this time */
1007 gboolean procmime_msginfo_is_encrypted(MsgInfo *msginfo)
1009 MimeInfo *mimeinfo, *partinfo;
1010 gboolean result = FALSE;
1012 cm_return_val_if_fail(msginfo != NULL, FALSE);
1014 mimeinfo = procmime_scan_message(msginfo);
1019 partinfo = mimeinfo;
1020 result = (find_encrypted_part(partinfo) != NULL);
1021 procmime_mimeinfo_free_all(mimeinfo);
1026 gchar *procmime_get_tmp_file_name(MimeInfo *mimeinfo)
1028 static guint32 id = 0;
1033 cm_return_val_if_fail(mimeinfo != NULL, NULL);
1035 g_snprintf(f_prefix, sizeof(f_prefix), "%08x.", id++);
1037 if ((mimeinfo->type == MIMETYPE_TEXT) && !g_ascii_strcasecmp(mimeinfo->subtype, "html"))
1038 base = g_strdup("mimetmp.html");
1040 const gchar *basetmp;
1042 basetmp = procmime_mimeinfo_get_parameter(mimeinfo, "filename");
1043 if (basetmp == NULL)
1044 basetmp = procmime_mimeinfo_get_parameter(mimeinfo, "name");
1045 if (basetmp == NULL)
1046 basetmp = "mimetmp";
1047 basetmp = g_path_get_basename(basetmp);
1048 if (*basetmp == '\0')
1049 basetmp = g_strdup("mimetmp");
1050 base = conv_filename_from_utf8(basetmp);
1051 g_free((gchar*)basetmp);
1052 subst_for_shellsafe_filename(base);
1055 filename = g_strconcat(get_mime_tmp_dir(), G_DIR_SEPARATOR_S,
1056 f_prefix, base, NULL);
1063 static GList *mime_type_list = NULL;
1065 gchar *procmime_get_mime_type(const gchar *filename)
1071 static GHashTable *mime_type_table = NULL;
1072 MimeType *mime_type;
1074 if (!mime_type_table) {
1075 mime_type_table = procmime_get_mime_type_table();
1076 if (!mime_type_table) return NULL;
1080 if (filename == NULL)
1083 base = g_path_get_basename(filename);
1084 if ((p = strrchr(base, '.')) != NULL)
1085 ext = g_utf8_strdown(p + 1, -1);
1089 mime_type = g_hash_table_lookup(mime_type_table, ext);
1093 str = g_strconcat(mime_type->type, "/", mime_type->sub_type,
1095 debug_print("got type %s for %s\n", str, ext);
1101 gchar *str = get_content_type_from_registry_with_ext(ext);
1108 static guint procmime_str_hash(gconstpointer gptr)
1110 guint hash_result = 0;
1113 for (str = gptr; str && *str; str++) {
1114 if (isupper(*str)) hash_result += (*str + ' ');
1115 else hash_result += *str;
1121 static gint procmime_str_equal(gconstpointer gptr1, gconstpointer gptr2)
1123 const char *str1 = gptr1;
1124 const char *str2 = gptr2;
1126 return !g_utf8_collate(str1, str2);
1129 static GHashTable *procmime_get_mime_type_table(void)
1131 GHashTable *table = NULL;
1133 MimeType *mime_type;
1136 if (!mime_type_list) {
1137 mime_type_list = procmime_get_mime_type_list();
1138 if (!mime_type_list) return NULL;
1141 table = g_hash_table_new(procmime_str_hash, procmime_str_equal);
1143 for (cur = mime_type_list; cur != NULL; cur = cur->next) {
1147 mime_type = (MimeType *)cur->data;
1149 if (!mime_type->extension) continue;
1151 exts = g_strsplit(mime_type->extension, " ", 16);
1152 for (i = 0; exts[i] != NULL; i++) {
1153 /* use previously dup'd key on overwriting */
1154 if (g_hash_table_lookup(table, exts[i]))
1157 key = g_strdup(exts[i]);
1158 g_hash_table_insert(table, key, mime_type);
1166 GList *procmime_get_mime_type_list(void)
1170 gchar buf[BUFFSIZE];
1173 MimeType *mime_type;
1174 gboolean fp_is_glob_file = TRUE;
1177 return mime_type_list;
1179 #if defined(__NetBSD__) || defined(__OpenBSD__) || defined(__FreeBSD__)
1180 if ((fp = procmime_fopen(DATAROOTDIR "/mime/globs", "rb")) == NULL)
1182 if ((fp = procmime_fopen("/usr/share/mime/globs", "rb")) == NULL)
1185 fp_is_glob_file = FALSE;
1186 if ((fp = procmime_fopen("/etc/mime.types", "rb")) == NULL) {
1187 if ((fp = procmime_fopen(SYSCONFDIR "/mime.types", "rb"))
1189 FILE_OP_ERROR(SYSCONFDIR "/mime.types",
1196 while (SC_FGETS(buf, sizeof(buf), fp) != NULL) {
1197 p = strchr(buf, '#');
1203 if (fp_is_glob_file) {
1204 while (*p && !g_ascii_isspace(*p) && (*p!=':')) p++;
1206 while (*p && !g_ascii_isspace(*p)) p++;
1213 delim = strchr(buf, '/');
1214 if (delim == NULL) continue;
1217 mime_type = g_new(MimeType, 1);
1218 mime_type->type = g_strdup(buf);
1219 mime_type->sub_type = g_strdup(delim + 1);
1221 if (fp_is_glob_file) {
1222 while (*p && (g_ascii_isspace(*p)||(*p=='*')||(*p=='.'))) p++;
1224 while (*p && g_ascii_isspace(*p)) p++;
1228 mime_type->extension = g_utf8_strdown(p, -1);
1230 mime_type->extension = NULL;
1232 list = g_list_append(list, mime_type);
1235 procmime_fclose(fp);
1238 g_warning("Can't read mime.types\n");
1243 EncodingType procmime_get_encoding_for_charset(const gchar *charset)
1247 else if (!g_ascii_strncasecmp(charset, "ISO-2022-", 9) ||
1248 !g_ascii_strcasecmp(charset, "US-ASCII"))
1250 else if (!g_ascii_strcasecmp(charset, "ISO-8859-5") ||
1251 !g_ascii_strncasecmp(charset, "KOI8-", 5) ||
1252 !g_ascii_strcasecmp(charset, "Windows-1251"))
1254 else if (!g_ascii_strncasecmp(charset, "ISO-8859-", 9))
1255 return ENC_QUOTED_PRINTABLE;
1256 else if (!g_ascii_strncasecmp(charset, "UTF-8", 5))
1257 return ENC_QUOTED_PRINTABLE;
1262 EncodingType procmime_get_encoding_for_text_file(const gchar *file, gboolean *has_binary)
1265 guchar buf[BUFFSIZE];
1267 size_t octet_chars = 0;
1268 size_t total_len = 0;
1269 gfloat octet_percentage;
1270 gboolean force_b64 = FALSE;
1272 if ((fp = procmime_fopen(file, "rb")) == NULL) {
1273 FILE_OP_ERROR(file, "fopen");
1277 while ((len = SC_FREAD(buf, sizeof(guchar), sizeof(buf), fp)) > 0) {
1281 for (p = buf, i = 0; i < len; ++p, ++i) {
1292 procmime_fclose(fp);
1295 octet_percentage = (gfloat)octet_chars / (gfloat)total_len;
1297 octet_percentage = 0.0;
1299 debug_print("procmime_get_encoding_for_text_file(): "
1300 "8bit chars: %zd / %zd (%f%%)\n", octet_chars, total_len,
1301 100.0 * octet_percentage);
1303 if (octet_percentage > 0.20 || force_b64) {
1304 debug_print("using BASE64\n");
1306 } else if (octet_chars > 0) {
1307 debug_print("using quoted-printable\n");
1308 return ENC_QUOTED_PRINTABLE;
1310 debug_print("using 7bit\n");
1315 struct EncodingTable
1318 EncodingType enc_type;
1321 struct EncodingTable encoding_table[] = {
1324 {"binary", ENC_BINARY},
1325 {"quoted-printable", ENC_QUOTED_PRINTABLE},
1326 {"base64", ENC_BASE64},
1327 {"x-uuencode", ENC_UNKNOWN},
1328 {NULL, ENC_UNKNOWN},
1331 const gchar *procmime_get_encoding_str(EncodingType encoding)
1333 struct EncodingTable *enc_table;
1335 for (enc_table = encoding_table; enc_table->str != NULL; enc_table++) {
1336 if (enc_table->enc_type == encoding)
1337 return enc_table->str;
1342 /* --- NEW MIME STUFF --- */
1349 static struct TypeTable mime_type_table[] = {
1350 {"text", MIMETYPE_TEXT},
1351 {"image", MIMETYPE_IMAGE},
1352 {"audio", MIMETYPE_AUDIO},
1353 {"video", MIMETYPE_VIDEO},
1354 {"application", MIMETYPE_APPLICATION},
1355 {"message", MIMETYPE_MESSAGE},
1356 {"multipart", MIMETYPE_MULTIPART},
1360 const gchar *procmime_get_media_type_str(MimeMediaType type)
1362 struct TypeTable *type_table;
1364 for (type_table = mime_type_table; type_table->str != NULL; type_table++) {
1365 if (type_table->type == type)
1366 return type_table->str;
1371 MimeMediaType procmime_get_media_type(const gchar *str)
1373 struct TypeTable *typetablearray;
1375 for (typetablearray = mime_type_table; typetablearray->str != NULL; typetablearray++)
1376 if (g_ascii_strncasecmp(str, typetablearray->str, strlen(typetablearray->str)) == 0)
1377 return typetablearray->type;
1379 return MIMETYPE_UNKNOWN;
1383 *\brief Safe wrapper for content type string.
1385 *\return const gchar * Pointer to content type string.
1387 gchar *procmime_get_content_type_str(MimeMediaType type,
1388 const char *subtype)
1390 const gchar *type_str = NULL;
1392 if (subtype == NULL || !(type_str = procmime_get_media_type_str(type)))
1393 return g_strdup("unknown");
1394 return g_strdup_printf("%s/%s", type_str, subtype);
1397 static int procmime_parse_mimepart(MimeInfo *parent,
1398 gchar *content_type,
1399 gchar *content_encoding,
1400 gchar *content_description,
1402 gchar *content_disposition,
1403 gchar *content_location,
1404 const gchar *original_msgid,
1405 const gchar *disposition_notification_hdr,
1406 const gchar *filename,
1409 gboolean short_scan);
1411 static void procmime_parse_message_rfc822(MimeInfo *mimeinfo, gboolean short_scan)
1413 HeaderEntry hentry[] = {{"Content-Type:", NULL, TRUE},
1414 {"Content-Transfer-Encoding:",
1416 {"Content-Description:",
1420 {"Content-Disposition:",
1422 {"Content-Location:",
1426 {"Original-Message-ID:",
1430 {NULL, NULL, FALSE}};
1431 guint content_start, i;
1436 procmime_decode_content(mimeinfo);
1438 fp = procmime_fopen(mimeinfo->data.filename, "rb");
1440 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
1443 fseek(fp, mimeinfo->offset, SEEK_SET);
1444 procheader_get_header_fields(fp, hentry);
1445 if (hentry[0].body != NULL) {
1446 tmp = conv_unmime_header(hentry[0].body, NULL, FALSE);
1447 g_free(hentry[0].body);
1448 hentry[0].body = tmp;
1450 if (hentry[2].body != NULL) {
1451 tmp = conv_unmime_header(hentry[2].body, NULL, FALSE);
1452 g_free(hentry[2].body);
1453 hentry[2].body = tmp;
1455 if (hentry[4].body != NULL) {
1456 tmp = conv_unmime_header(hentry[4].body, NULL, FALSE);
1457 g_free(hentry[4].body);
1458 hentry[4].body = tmp;
1460 if (hentry[5].body != NULL) {
1461 tmp = conv_unmime_header(hentry[5].body, NULL, FALSE);
1462 g_free(hentry[5].body);
1463 hentry[5].body = tmp;
1465 if (hentry[7].body != NULL) {
1466 tmp = conv_unmime_header(hentry[7].body, NULL, FALSE);
1467 g_free(hentry[7].body);
1468 hentry[7].body = tmp;
1470 if (hentry[8].body != NULL) {
1471 tmp = conv_unmime_header(hentry[8].body, NULL, FALSE);
1472 g_free(hentry[8].body);
1473 hentry[8].body = tmp;
1476 content_start = ftell(fp);
1477 procmime_fclose(fp);
1479 len = mimeinfo->length - (content_start - mimeinfo->offset);
1482 procmime_parse_mimepart(mimeinfo,
1483 hentry[0].body, hentry[1].body,
1484 hentry[2].body, hentry[3].body,
1485 hentry[4].body, hentry[5].body,
1486 hentry[7].body, hentry[8].body,
1487 mimeinfo->data.filename, content_start,
1490 for (i = 0; i < (sizeof hentry / sizeof hentry[0]); i++) {
1491 g_free(hentry[i].body);
1492 hentry[i].body = NULL;
1496 static void procmime_parse_disposition_notification(MimeInfo *mimeinfo,
1497 const gchar *original_msgid, const gchar *disposition_notification_hdr,
1498 gboolean short_scan)
1500 HeaderEntry hentry[] = {{"Original-Message-ID:", NULL, TRUE},
1501 {"Disposition:", NULL, TRUE},
1502 {NULL, NULL, FALSE}};
1505 gchar *orig_msg_id = NULL;
1508 procmime_decode_content(mimeinfo);
1510 debug_print("parse disposition notification\n");
1511 fp = procmime_fopen(mimeinfo->data.filename, "rb");
1513 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
1516 fseek(fp, mimeinfo->offset, SEEK_SET);
1518 if (original_msgid && disposition_notification_hdr) {
1519 hentry[0].body = g_strdup(original_msgid);
1520 hentry[1].body = g_strdup(disposition_notification_hdr);
1522 procheader_get_header_fields(fp, hentry);
1525 procmime_fclose(fp);
1527 if (!hentry[0].body || !hentry[1].body) {
1528 debug_print("MsgId %s, Disp %s\n",
1529 hentry[0].body ? hentry[0].body:"(nil)",
1530 hentry[1].body ? hentry[1].body:"(nil)");
1534 orig_msg_id = g_strdup(hentry[0].body);
1535 disp = g_strdup(hentry[1].body);
1537 extract_parenthesis(orig_msg_id, '<', '>');
1538 remove_space(orig_msg_id);
1540 if (strstr(disp, "displayed")) {
1541 /* find sent message, if possible */
1542 MsgInfo *info = NULL;
1544 debug_print("%s has been displayed.\n", orig_msg_id);
1545 for (flist = folder_get_list(); flist != NULL; flist = g_list_next(flist)) {
1546 FolderItem *outbox = ((Folder *)(flist->data))->outbox;
1548 debug_print("skipping folder with no outbox...\n");
1551 info = folder_item_get_msginfo_by_msgid(outbox, orig_msg_id);
1552 debug_print("%s %s in %s\n", info?"found":"didn't find", orig_msg_id, outbox->path);
1554 procmsg_msginfo_set_flags(info, MSG_RETRCPT_GOT, 0);
1555 procmsg_msginfo_free(info);
1559 g_free(orig_msg_id);
1562 for (i = 0; i < (sizeof hentry / sizeof hentry[0]); i++) {
1563 g_free(hentry[i].body);
1564 hentry[i].body = NULL;
1568 #define GET_HEADERS() { \
1569 procheader_get_header_fields(fp, hentry); \
1570 if (hentry[0].body != NULL) { \
1571 tmp = conv_unmime_header(hentry[0].body, NULL, FALSE); \
1572 g_free(hentry[0].body); \
1573 hentry[0].body = tmp; \
1575 if (hentry[2].body != NULL) { \
1576 tmp = conv_unmime_header(hentry[2].body, NULL, FALSE); \
1577 g_free(hentry[2].body); \
1578 hentry[2].body = tmp; \
1580 if (hentry[4].body != NULL) { \
1581 tmp = conv_unmime_header(hentry[4].body, NULL, FALSE); \
1582 g_free(hentry[4].body); \
1583 hentry[4].body = tmp; \
1585 if (hentry[5].body != NULL) { \
1586 tmp = conv_unmime_header(hentry[5].body, NULL, FALSE); \
1587 g_free(hentry[5].body); \
1588 hentry[5].body = tmp; \
1590 if (hentry[6].body != NULL) { \
1591 tmp = conv_unmime_header(hentry[6].body, NULL, FALSE); \
1592 g_free(hentry[6].body); \
1593 hentry[6].body = tmp; \
1595 if (hentry[7].body != NULL) { \
1596 tmp = conv_unmime_header(hentry[7].body, NULL, FALSE); \
1597 g_free(hentry[7].body); \
1598 hentry[7].body = tmp; \
1602 static void procmime_parse_multipart(MimeInfo *mimeinfo, gboolean short_scan)
1604 HeaderEntry hentry[] = {{"Content-Type:", NULL, TRUE},
1605 {"Content-Transfer-Encoding:",
1607 {"Content-Description:",
1611 {"Content-Disposition:",
1613 {"Content-Location:",
1615 {"Original-Message-ID:",
1619 {NULL, NULL, FALSE}};
1622 gint boundary_len = 0, lastoffset = -1, i;
1623 gchar buf[BUFFSIZE];
1626 gboolean start_found = FALSE;
1627 gboolean end_found = FALSE;
1629 boundary = g_hash_table_lookup(mimeinfo->typeparameters, "boundary");
1632 boundary_len = strlen(boundary);
1634 procmime_decode_content(mimeinfo);
1636 fp = procmime_fopen(mimeinfo->data.filename, "rb");
1638 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
1642 fseek(fp, mimeinfo->offset, SEEK_SET);
1643 while ((p = SC_FGETS(buf, sizeof(buf), fp)) != NULL && result == 0) {
1644 if (ftell(fp) - 1 > (mimeinfo->offset + mimeinfo->length))
1647 if (IS_BOUNDARY(buf, boundary, boundary_len)) {
1650 if (lastoffset != -1) {
1651 gint len = (ftell(fp) - strlen(buf)) - lastoffset - 1;
1654 result = procmime_parse_mimepart(mimeinfo,
1655 hentry[0].body, hentry[1].body,
1656 hentry[2].body, hentry[3].body,
1657 hentry[4].body, hentry[5].body,
1658 hentry[6].body, hentry[7].body,
1659 mimeinfo->data.filename, lastoffset,
1661 if (result == 1 && short_scan)
1666 if (buf[2 + boundary_len] == '-' &&
1667 buf[2 + boundary_len + 1] == '-') {
1671 for (i = 0; i < (sizeof hentry / sizeof hentry[0]) ; i++) {
1672 g_free(hentry[i].body);
1673 hentry[i].body = NULL;
1676 lastoffset = ftell(fp);
1680 if (start_found && !end_found && lastoffset != -1) {
1681 gint len = (ftell(fp) - strlen(buf)) - lastoffset - 1;
1684 result = procmime_parse_mimepart(mimeinfo,
1685 hentry[0].body, hentry[1].body,
1686 hentry[2].body, hentry[3].body,
1687 hentry[4].body, hentry[5].body,
1688 hentry[6].body, hentry[7].body,
1689 mimeinfo->data.filename, lastoffset,
1692 mimeinfo->broken = TRUE;
1695 for (i = 0; i < (sizeof hentry / sizeof hentry[0]); i++) {
1696 g_free(hentry[i].body);
1697 hentry[i].body = NULL;
1699 procmime_fclose(fp);
1702 static void parse_parameters(const gchar *parameters, GHashTable *table)
1704 gchar *params, *param, *next;
1705 GSList *convlist = NULL, *concatlist = NULL, *cur;
1707 params = g_strdup(parameters);
1710 for (; next != NULL; param = next) {
1711 gchar *attribute, *value, *tmp, *down_attr, *orig_down_attr;
1713 gboolean convert = FALSE;
1715 next = strchr_with_skip_quote(param, '"', ';');
1724 value = strchr(attribute, '=');
1730 while (value[0] == ' ')
1733 down_attr = g_utf8_strdown(attribute, -1);
1734 orig_down_attr = down_attr;
1736 len = down_attr ? strlen(down_attr):0;
1737 if (len > 0 && down_attr[len - 1] == '*') {
1738 gchar *srcpos, *dstpos, *endpos;
1741 down_attr[len - 1] = '\0';
1745 endpos = value + strlen(value);
1746 while (srcpos < endpos) {
1752 if (!get_hex_value(&dstvalue, srcpos[1], srcpos[2]))
1762 if (value[0] == '"')
1763 extract_quote(value, '"');
1765 if (value[0] == '"')
1766 extract_quote(value, '"');
1767 else if ((tmp = strchr(value, ' ')) != NULL)
1772 while (down_attr[0] == ' ')
1774 while (down_attr[strlen(down_attr)-1] == ' ')
1775 down_attr[strlen(down_attr)-1] = '\0';
1778 while (value[0] == ' ')
1780 while (value[strlen(value)-1] == ' ')
1781 value[strlen(value)-1] = '\0';
1783 if (down_attr && strrchr(down_attr, '*') != NULL) {
1786 tmpattr = g_strdup(down_attr);
1787 tmp = strrchr(tmpattr, '*');
1790 if ((tmp[1] == '0') && (tmp[2] == '\0') &&
1791 (g_slist_find_custom(concatlist, down_attr, (GCompareFunc)g_strcmp0) == NULL))
1792 concatlist = g_slist_prepend(concatlist, g_strdup(tmpattr));
1794 if (convert && (g_slist_find_custom(convlist, tmpattr, (GCompareFunc)g_strcmp0) == NULL))
1795 convlist = g_slist_prepend(convlist, g_strdup(tmpattr));
1798 } else if (convert) {
1799 if (g_slist_find_custom(convlist, down_attr, (GCompareFunc)g_strcmp0) == NULL)
1800 convlist = g_slist_prepend(convlist, g_strdup(down_attr));
1803 if (g_hash_table_lookup(table, down_attr) == NULL)
1804 g_hash_table_insert(table, g_strdup(down_attr), g_strdup(value));
1805 g_free(orig_down_attr);
1808 for (cur = concatlist; cur != NULL; cur = g_slist_next(cur)) {
1809 gchar *attribute, *attrwnum, *partvalue;
1813 attribute = (gchar *) cur->data;
1814 value = g_string_sized_new(64);
1816 attrwnum = g_strdup_printf("%s*%d", attribute, n);
1817 while ((partvalue = g_hash_table_lookup(table, attrwnum)) != NULL) {
1818 g_string_append(value, partvalue);
1820 g_hash_table_remove(table, attrwnum);
1823 attrwnum = g_strdup_printf("%s*%d", attribute, n);
1827 g_hash_table_insert(table, g_strdup(attribute), g_strdup(value->str));
1828 g_string_free(value, TRUE);
1830 slist_free_strings_full(concatlist);
1832 for (cur = convlist; cur != NULL; cur = g_slist_next(cur)) {
1833 gchar *attribute, *key, *value;
1834 gchar *charset, *lang, *oldvalue, *newvalue;
1836 attribute = (gchar *) cur->data;
1837 if (!g_hash_table_lookup_extended(
1838 table, attribute, (gpointer *)(gchar *) &key, (gpointer *)(gchar *) &value))
1842 if (charset == NULL)
1844 lang = strchr(charset, '\'');
1849 oldvalue = strchr(lang, '\'');
1850 if (oldvalue == NULL)
1855 newvalue = conv_codeset_strdup(oldvalue, charset, CS_UTF_8);
1857 g_hash_table_remove(table, attribute);
1861 g_hash_table_insert(table, g_strdup(attribute), newvalue);
1863 slist_free_strings_full(convlist);
1868 static void procmime_parse_content_type(const gchar *content_type, MimeInfo *mimeinfo)
1870 cm_return_if_fail(content_type != NULL);
1871 cm_return_if_fail(mimeinfo != NULL);
1873 /* RFC 2045, page 13 says that the mime subtype is MANDATORY;
1874 * if it's not available we use the default Content-Type */
1875 if ((content_type[0] == '\0') || (strchr(content_type, '/') == NULL)) {
1876 mimeinfo->type = MIMETYPE_TEXT;
1877 mimeinfo->subtype = g_strdup("plain");
1878 if (g_hash_table_lookup(mimeinfo->typeparameters,
1879 "charset") == NULL) {
1880 g_hash_table_insert(mimeinfo->typeparameters,
1881 g_strdup("charset"),
1883 conv_get_locale_charset_str_no_utf8()));
1886 gchar *type, *subtype, *params;
1888 type = g_strdup(content_type);
1889 subtype = strchr(type, '/') + 1;
1890 *(subtype - 1) = '\0';
1891 if ((params = strchr(subtype, ';')) != NULL) {
1896 mimeinfo->type = procmime_get_media_type(type);
1897 mimeinfo->subtype = g_strstrip(g_strdup(subtype));
1899 /* Get mimeinfo->typeparameters */
1901 parse_parameters(params, mimeinfo->typeparameters);
1907 static void procmime_parse_content_disposition(const gchar *content_disposition, MimeInfo *mimeinfo)
1909 gchar *tmp, *params;
1911 cm_return_if_fail(content_disposition != NULL);
1912 cm_return_if_fail(mimeinfo != NULL);
1914 tmp = g_strdup(content_disposition);
1915 if ((params = strchr(tmp, ';')) != NULL) {
1921 if (!g_ascii_strcasecmp(tmp, "inline"))
1922 mimeinfo->disposition = DISPOSITIONTYPE_INLINE;
1923 else if (!g_ascii_strcasecmp(tmp, "attachment"))
1924 mimeinfo->disposition = DISPOSITIONTYPE_ATTACHMENT;
1926 mimeinfo->disposition = DISPOSITIONTYPE_ATTACHMENT;
1929 parse_parameters(params, mimeinfo->dispositionparameters);
1935 static void procmime_parse_content_encoding(const gchar *content_encoding, MimeInfo *mimeinfo)
1937 struct EncodingTable *enc_table;
1939 for (enc_table = encoding_table; enc_table->str != NULL; enc_table++) {
1940 if (g_ascii_strcasecmp(enc_table->str, content_encoding) == 0) {
1941 mimeinfo->encoding_type = enc_table->enc_type;
1945 mimeinfo->encoding_type = ENC_UNKNOWN;
1949 static GSList *registered_parsers = NULL;
1951 static MimeParser *procmime_get_mimeparser_for_type(MimeMediaType type, const gchar *sub_type)
1954 for (cur = registered_parsers; cur; cur = cur->next) {
1955 MimeParser *parser = (MimeParser *)cur->data;
1956 if (parser->type == type && !strcmp2(parser->sub_type, sub_type))
1962 void procmime_mimeparser_register(MimeParser *parser)
1964 if (!procmime_get_mimeparser_for_type(parser->type, parser->sub_type))
1965 registered_parsers = g_slist_append(registered_parsers, parser);
1969 void procmime_mimeparser_unregister(MimeParser *parser)
1971 registered_parsers = g_slist_remove(registered_parsers, parser);
1974 static gboolean procmime_mimeparser_parse(MimeParser *parser, MimeInfo *mimeinfo)
1976 cm_return_val_if_fail(parser->parse != NULL, FALSE);
1977 return parser->parse(parser, mimeinfo);
1980 static int procmime_parse_mimepart(MimeInfo *parent,
1981 gchar *content_type,
1982 gchar *content_encoding,
1983 gchar *content_description,
1985 gchar *content_disposition,
1986 gchar *content_location,
1987 const gchar *original_msgid,
1988 const gchar *disposition_notification_hdr,
1989 const gchar *filename,
1992 gboolean short_scan)
1995 MimeParser *parser = NULL;
1996 gboolean parsed = FALSE;
1999 /* Create MimeInfo */
2000 mimeinfo = procmime_mimeinfo_new();
2001 mimeinfo->content = MIMECONTENT_FILE;
2003 if (parent != NULL) {
2004 if (g_node_depth(parent->node) > 32) {
2005 /* 32 is an arbitrary value
2006 * this avoids DOSsing ourselves
2007 * with enormous messages
2009 procmime_mimeinfo_free_all(mimeinfo);
2012 g_node_append(parent->node, mimeinfo->node);
2014 mimeinfo->data.filename = g_strdup(filename);
2015 mimeinfo->offset = offset;
2016 mimeinfo->length = length;
2018 if (content_type != NULL) {
2019 procmime_parse_content_type(content_type, mimeinfo);
2021 mimeinfo->type = MIMETYPE_TEXT;
2022 mimeinfo->subtype = g_strdup("plain");
2023 if (g_hash_table_lookup(mimeinfo->typeparameters,
2024 "charset") == NULL) {
2025 g_hash_table_insert(mimeinfo->typeparameters,
2026 g_strdup("charset"),
2028 conv_get_locale_charset_str_no_utf8()));
2032 if (content_encoding != NULL) {
2033 procmime_parse_content_encoding(content_encoding, mimeinfo);
2035 mimeinfo->encoding_type = ENC_UNKNOWN;
2038 if (content_description != NULL)
2039 mimeinfo->description = g_strdup(content_description);
2041 mimeinfo->description = NULL;
2043 if (content_id != NULL)
2044 mimeinfo->id = g_strdup(content_id);
2046 mimeinfo->id = NULL;
2048 if (content_location != NULL)
2049 mimeinfo->location = g_strdup(content_location);
2051 mimeinfo->location = NULL;
2053 if (content_disposition != NULL)
2054 procmime_parse_content_disposition(content_disposition, mimeinfo);
2056 mimeinfo->disposition = DISPOSITIONTYPE_UNKNOWN;
2058 /* Call parser for mime type */
2059 if ((parser = procmime_get_mimeparser_for_type(mimeinfo->type, mimeinfo->subtype)) != NULL) {
2060 parsed = procmime_mimeparser_parse(parser, mimeinfo);
2063 switch (mimeinfo->type) {
2065 if (g_ascii_strcasecmp(mimeinfo->subtype, "plain") == 0 && short_scan) {
2070 case MIMETYPE_MESSAGE:
2071 if (g_ascii_strcasecmp(mimeinfo->subtype, "rfc822") == 0) {
2072 procmime_parse_message_rfc822(mimeinfo, short_scan);
2074 if (g_ascii_strcasecmp(mimeinfo->subtype, "disposition-notification") == 0) {
2075 procmime_parse_disposition_notification(mimeinfo,
2076 original_msgid, disposition_notification_hdr, short_scan);
2080 case MIMETYPE_MULTIPART:
2081 procmime_parse_multipart(mimeinfo, short_scan);
2084 case MIMETYPE_APPLICATION:
2085 if (g_ascii_strcasecmp(mimeinfo->subtype, "octet-stream") == 0
2086 && original_msgid && *original_msgid
2087 && disposition_notification_hdr && *disposition_notification_hdr) {
2088 procmime_parse_disposition_notification(mimeinfo,
2089 original_msgid, disposition_notification_hdr, short_scan);
2100 static gchar *typenames[] = {
2111 static gboolean output_func(GNode *node, gpointer data)
2114 MimeInfo *mimeinfo = (MimeInfo *) node->data;
2116 depth = g_node_depth(node);
2117 for (i = 0; i < depth; i++)
2119 g_print("%s/%s (offset:%d length:%d encoding: %d)\n", typenames[mimeinfo->type], mimeinfo->subtype, mimeinfo->offset, mimeinfo->length, mimeinfo->encoding_type);
2124 static void output_mime_structure(MimeInfo *mimeinfo, int indent)
2126 g_node_traverse(mimeinfo->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1, output_func, NULL);
2129 static MimeInfo *procmime_scan_file_with_offset(const gchar *filename, int offset, gboolean short_scan)
2134 g_stat(filename, &buf);
2136 mimeinfo = procmime_mimeinfo_new();
2137 mimeinfo->content = MIMECONTENT_FILE;
2138 mimeinfo->encoding_type = ENC_UNKNOWN;
2139 mimeinfo->type = MIMETYPE_MESSAGE;
2140 mimeinfo->subtype = g_strdup("rfc822");
2141 mimeinfo->data.filename = g_strdup(filename);
2142 mimeinfo->offset = offset;
2143 mimeinfo->length = buf.st_size - offset;
2145 procmime_parse_message_rfc822(mimeinfo, short_scan);
2146 if (debug_get_mode())
2147 output_mime_structure(mimeinfo, 0);
2152 static MimeInfo *procmime_scan_file_full(const gchar *filename, gboolean short_scan)
2156 cm_return_val_if_fail(filename != NULL, NULL);
2158 mimeinfo = procmime_scan_file_with_offset(filename, 0, short_scan);
2163 MimeInfo *procmime_scan_file(const gchar *filename)
2165 return procmime_scan_file_full(filename, FALSE);
2168 static MimeInfo *procmime_scan_file_short(const gchar *filename)
2170 return procmime_scan_file_full(filename, TRUE);
2173 static MimeInfo *procmime_scan_queue_file_full(const gchar *filename, gboolean short_scan)
2177 gchar buf[BUFFSIZE];
2180 cm_return_val_if_fail(filename != NULL, NULL);
2183 if ((fp = procmime_fopen(filename, "rb")) == NULL)
2185 /* Skip queue header */
2186 while (SC_FGETS(buf, sizeof(buf), fp) != NULL) {
2188 if ((!strncmp(buf, "X-Claws-End-Special-Headers: 1",
2189 strlen("X-Claws-End-Special-Headers:"))) ||
2190 (!strncmp(buf, "X-Sylpheed-End-Special-Headers: 1",
2191 strlen("X-Sylpheed-End-Special-Headers:"))))
2194 if (buf[0] == '\r' || buf[0] == '\n') break;
2195 /* from other mailers */
2196 if (!strncmp(buf, "Date: ", 6)
2197 || !strncmp(buf, "To: ", 4)
2198 || !strncmp(buf, "From: ", 6)
2199 || !strncmp(buf, "Subject: ", 9)) {
2205 procmime_fclose(fp);
2207 mimeinfo = procmime_scan_file_with_offset(filename, offset, short_scan);
2212 MimeInfo *procmime_scan_queue_file(const gchar *filename)
2214 return procmime_scan_queue_file_full(filename, FALSE);
2217 static MimeInfo *procmime_scan_queue_file_short(const gchar *filename)
2219 return procmime_scan_queue_file_full(filename, TRUE);
2224 ENC_AS_QUOTED_STRING,
2229 typedef struct _ParametersData {
2235 static void write_parameters(gpointer key, gpointer value, gpointer user_data)
2238 gchar *val = value, *valpos, *tmp;
2239 ParametersData *pdata = (ParametersData *)user_data;
2240 GString *buf = g_string_new("");
2243 EncodeAs encas = ENC_AS_TOKEN;
2245 for (valpos = val; *valpos != 0; valpos++) {
2246 if (!IS_ASCII(*valpos)) {
2247 encas = ENC_AS_ENCWORD;
2252 if (((*valpos >= 0) && (*valpos < 037)) || (*valpos == 0177)) {
2253 encas = ENC_AS_QUOTED_STRING;
2257 /* tspecials + SPACE */
2275 encas = ENC_AS_QUOTED_STRING;
2282 g_string_append_printf(buf, "%s=%s", param, val);
2285 case ENC_AS_QUOTED_STRING:
2286 g_string_append_printf(buf, "%s=\"%s\"", param, val);
2289 #if 0 /* we don't use that for now */
2290 case ENC_AS_EXTENDED:
2291 if (!g_utf8_validate(val, -1, NULL))
2292 g_string_append_printf(buf, "%s*=%s''", param,
2293 conv_get_locale_charset_str());
2295 g_string_append_printf(buf, "%s*=%s''", param,
2297 for (valpos = val; *valpos != '\0'; valpos++) {
2298 if (IS_ASCII(*valpos) && isalnum(*valpos)) {
2299 g_string_append_printf(buf, "%c", *valpos);
2301 gchar hexstr[3] = "XX";
2302 get_hex_str(hexstr, *valpos);
2303 g_string_append_printf(buf, "%%%s", hexstr);
2308 case ENC_AS_EXTENDED:
2309 debug_print("Unhandled ENC_AS_EXTENDED.");
2312 case ENC_AS_ENCWORD:
2313 len = MAX(strlen(val)*6, 512);
2314 tmp = g_malloc(len+1);
2315 codeconv_set_strict(TRUE);
2316 conv_encode_header_full(tmp, len, val, pdata->len + strlen(param) + 4 , FALSE,
2317 prefs_common.outgoing_charset);
2318 codeconv_set_strict(FALSE);
2319 if (!tmp || !*tmp) {
2320 codeconv_set_strict(TRUE);
2321 conv_encode_header_full(tmp, len, val, pdata->len + strlen(param) + 4 , FALSE,
2322 conv_get_outgoing_charset_str());
2323 codeconv_set_strict(FALSE);
2325 if (!tmp || !*tmp) {
2326 codeconv_set_strict(TRUE);
2327 conv_encode_header_full(tmp, len, val, pdata->len + strlen(param) + 4 , FALSE,
2329 codeconv_set_strict(FALSE);
2331 if (!tmp || !*tmp) {
2332 conv_encode_header_full(tmp, len, val, pdata->len + strlen(param) + 4 , FALSE,
2335 g_string_append_printf(buf, "%s=\"%s\"", param, tmp);
2341 if (buf->str && strlen(buf->str)) {
2342 tmp = strstr(buf->str, "\n");
2344 len = (tmp - buf->str);
2346 len = strlen(buf->str);
2347 if (pdata->len + len > 76) {
2348 if (fprintf(pdata->fp, ";\n %s", buf->str) < 0)
2349 pdata->error = TRUE;
2350 pdata->len = strlen(buf->str) + 1;
2352 if (fprintf(pdata->fp, "; %s", buf->str) < 0)
2353 pdata->error = TRUE;
2354 pdata->len += strlen(buf->str) + 2;
2357 g_string_free(buf, TRUE);
2360 #define TRY(func) { \
2366 int procmime_write_mime_header(MimeInfo *mimeinfo, FILE *fp)
2368 struct TypeTable *type_table;
2369 ParametersData *pdata = g_new0(ParametersData, 1);
2370 debug_print("procmime_write_mime_header\n");
2373 pdata->error = FALSE;
2374 for (type_table = mime_type_table; type_table->str != NULL; type_table++)
2375 if (mimeinfo->type == type_table->type) {
2376 gchar *buf = g_strdup_printf(
2377 "Content-Type: %s/%s", type_table->str, mimeinfo->subtype);
2378 if (fprintf(fp, "%s", buf) < 0) {
2383 pdata->len = strlen(buf);
2387 g_hash_table_foreach(mimeinfo->typeparameters, write_parameters, pdata);
2388 if (pdata->error == TRUE) {
2394 TRY(fprintf(fp, "\n") >= 0);
2396 if (mimeinfo->encoding_type != ENC_UNKNOWN)
2397 TRY(fprintf(fp, "Content-Transfer-Encoding: %s\n", procmime_get_encoding_str(mimeinfo->encoding_type)) >= 0);
2399 if (mimeinfo->description != NULL)
2400 TRY(fprintf(fp, "Content-Description: %s\n", mimeinfo->description) >= 0);
2402 if (mimeinfo->id != NULL)
2403 TRY(fprintf(fp, "Content-ID: %s\n", mimeinfo->id) >= 0);
2405 if (mimeinfo->location != NULL)
2406 TRY(fprintf(fp, "Content-Location: %s\n", mimeinfo->location) >= 0);
2408 if (mimeinfo->disposition != DISPOSITIONTYPE_UNKNOWN) {
2409 ParametersData *pdata = g_new0(ParametersData, 1);
2411 if (mimeinfo->disposition == DISPOSITIONTYPE_INLINE)
2412 buf = g_strdup("Content-Disposition: inline");
2413 else if (mimeinfo->disposition == DISPOSITIONTYPE_ATTACHMENT)
2414 buf = g_strdup("Content-Disposition: attachment");
2416 buf = g_strdup("Content-Disposition: unknown");
2418 if (fprintf(fp, "%s", buf) < 0) {
2423 pdata->len = strlen(buf);
2427 pdata->error = FALSE;
2428 g_hash_table_foreach(mimeinfo->dispositionparameters, write_parameters, pdata);
2429 if (pdata->error == TRUE) {
2434 TRY(fprintf(fp, "\n") >= 0);
2437 TRY(fprintf(fp, "\n") >= 0);
2442 static gint procmime_write_message_rfc822(MimeInfo *mimeinfo, FILE *fp)
2447 gchar buf[BUFFSIZE];
2448 gboolean skip = FALSE;;
2451 debug_print("procmime_write_message_rfc822\n");
2454 switch (mimeinfo->content) {
2455 case MIMECONTENT_FILE:
2456 if ((infp = procmime_fopen(mimeinfo->data.filename, "rb")) == NULL) {
2457 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
2460 fseek(infp, mimeinfo->offset, SEEK_SET);
2461 while (SC_FGETS(buf, sizeof(buf), infp) == buf) {
2463 if (buf[0] == '\n' && buf[1] == '\0')
2465 if (skip && (buf[0] == ' ' || buf[0] == '\t'))
2467 if (g_ascii_strncasecmp(buf, "MIME-Version:", 13) == 0 ||
2468 g_ascii_strncasecmp(buf, "Content-Type:", 13) == 0 ||
2469 g_ascii_strncasecmp(buf, "Content-Transfer-Encoding:", 26) == 0 ||
2470 g_ascii_strncasecmp(buf, "Content-Description:", 20) == 0 ||
2471 g_ascii_strncasecmp(buf, "Content-ID:", 11) == 0 ||
2472 g_ascii_strncasecmp(buf, "Content-Location:", 17) == 0 ||
2473 g_ascii_strncasecmp(buf, "Content-Disposition:", 20) == 0) {
2478 if (SC_FWRITE(buf, sizeof(gchar), len, fp) < len) {
2479 g_warning("failed to dump %zd bytes from file", len);
2480 procmime_fclose(infp);
2485 procmime_fclose(infp);
2488 case MIMECONTENT_MEM:
2489 len = strlen(mimeinfo->data.mem);
2490 if (SC_FWRITE(mimeinfo->data.mem, sizeof(gchar), len, fp) < len) {
2491 g_warning("failed to dump %zd bytes from mem", len);
2500 childnode = mimeinfo->node->children;
2501 if (childnode == NULL)
2504 child = (MimeInfo *) childnode->data;
2505 if (fprintf(fp, "MIME-Version: 1.0\n") < 0) {
2506 g_warning("failed to write mime version");
2509 if (procmime_write_mime_header(child, fp) < 0)
2511 return procmime_write_mimeinfo(child, fp);
2514 static gint procmime_write_multipart(MimeInfo *mimeinfo, FILE *fp)
2518 gchar *boundary, *str, *str2;
2519 gchar buf[BUFFSIZE];
2520 gboolean firstboundary;
2523 debug_print("procmime_write_multipart\n");
2525 boundary = g_hash_table_lookup(mimeinfo->typeparameters, "boundary");
2527 switch (mimeinfo->content) {
2528 case MIMECONTENT_FILE:
2529 if ((infp = procmime_fopen(mimeinfo->data.filename, "rb")) == NULL) {
2530 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
2533 fseek(infp, mimeinfo->offset, SEEK_SET);
2534 while (SC_FGETS(buf, sizeof(buf), infp) == buf) {
2535 if (IS_BOUNDARY(buf, boundary, strlen(boundary)))
2538 if (SC_FWRITE(buf, sizeof(gchar), len, fp) < len) {
2539 g_warning("failed to write %zd", len);
2540 procmime_fclose(infp);
2544 procmime_fclose(infp);
2547 case MIMECONTENT_MEM:
2548 str = g_strdup(mimeinfo->data.mem);
2549 if (((str2 = strstr(str, boundary)) != NULL) && ((str2 - str) >= 2) &&
2550 (*(str2 - 1) == '-') && (*(str2 - 2) == '-'))
2553 if (SC_FWRITE(str, sizeof(gchar), len, fp) < len) {
2554 g_warning("failed to write %zd from mem", len);
2565 childnode = mimeinfo->node->children;
2566 firstboundary = TRUE;
2567 while (childnode != NULL) {
2568 MimeInfo *child = childnode->data;
2571 firstboundary = FALSE;
2573 TRY(fprintf(fp, "\n") >= 0);
2575 TRY(fprintf(fp, "--%s\n", boundary) >= 0);
2577 if (procmime_write_mime_header(child, fp) < 0)
2579 if (procmime_write_mimeinfo(child, fp) < 0)
2582 childnode = g_node_next_sibling(childnode);
2584 TRY(fprintf(fp, "\n--%s--\n", boundary) >= 0);
2589 gint procmime_write_mimeinfo(MimeInfo *mimeinfo, FILE *fp)
2593 debug_print("procmime_write_mimeinfo\n");
2595 if (G_NODE_IS_LEAF(mimeinfo->node)) {
2596 switch (mimeinfo->content) {
2597 case MIMECONTENT_FILE:
2598 if ((infp = procmime_fopen(mimeinfo->data.filename, "rb")) == NULL) {
2599 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
2602 copy_file_part_to_fp(infp, mimeinfo->offset, mimeinfo->length, fp);
2603 procmime_fclose(infp);
2606 case MIMECONTENT_MEM:
2607 len = strlen(mimeinfo->data.mem);
2608 if (SC_FWRITE(mimeinfo->data.mem, sizeof(gchar), len, fp) < len)
2616 /* Call writer for mime type */
2617 switch (mimeinfo->type) {
2618 case MIMETYPE_MESSAGE:
2619 if (g_ascii_strcasecmp(mimeinfo->subtype, "rfc822") == 0) {
2620 return procmime_write_message_rfc822(mimeinfo, fp);
2624 case MIMETYPE_MULTIPART:
2625 return procmime_write_multipart(mimeinfo, fp);
2637 gchar *procmime_get_part_file_name(MimeInfo *mimeinfo)
2641 if ((mimeinfo->type == MIMETYPE_TEXT) && !g_ascii_strcasecmp(mimeinfo->subtype, "html"))
2642 base = g_strdup("mimetmp.html");
2644 const gchar *basetmp;
2647 basetmp = procmime_mimeinfo_get_parameter(mimeinfo, "filename");
2648 if (basetmp == NULL)
2649 basetmp = procmime_mimeinfo_get_parameter(mimeinfo, "name");
2650 if (basetmp == NULL)
2651 basetmp = "mimetmp";
2652 basename = g_path_get_basename(basetmp);
2653 if (*basename == '\0') {
2655 basename = g_strdup("mimetmp");
2657 base = conv_filename_from_utf8(basename);
2659 subst_for_shellsafe_filename(base);