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 /* Don't overwrite previously inserted extension */
1154 if (!g_hash_table_lookup(table, exts[i])) {
1155 key = g_strdup(exts[i]);
1156 g_hash_table_insert(table, key, mime_type);
1165 GList *procmime_get_mime_type_list(void)
1169 gchar buf[BUFFSIZE];
1172 MimeType *mime_type;
1173 gboolean fp_is_glob_file = TRUE;
1176 return mime_type_list;
1178 #if defined(__NetBSD__) || defined(__OpenBSD__) || defined(__FreeBSD__)
1179 if ((fp = procmime_fopen(DATAROOTDIR "/mime/globs", "rb")) == NULL)
1181 if ((fp = procmime_fopen("/usr/share/mime/globs", "rb")) == NULL)
1184 fp_is_glob_file = FALSE;
1185 if ((fp = procmime_fopen("/etc/mime.types", "rb")) == NULL) {
1186 if ((fp = procmime_fopen(SYSCONFDIR "/mime.types", "rb"))
1188 FILE_OP_ERROR(SYSCONFDIR "/mime.types",
1195 while (SC_FGETS(buf, sizeof(buf), fp) != NULL) {
1196 p = strchr(buf, '#');
1202 if (fp_is_glob_file) {
1203 while (*p && !g_ascii_isspace(*p) && (*p!=':')) p++;
1205 while (*p && !g_ascii_isspace(*p)) p++;
1212 delim = strchr(buf, '/');
1213 if (delim == NULL) continue;
1216 mime_type = g_new(MimeType, 1);
1217 mime_type->type = g_strdup(buf);
1218 mime_type->sub_type = g_strdup(delim + 1);
1220 if (fp_is_glob_file) {
1221 while (*p && (g_ascii_isspace(*p)||(*p=='*')||(*p=='.'))) p++;
1223 while (*p && g_ascii_isspace(*p)) p++;
1227 mime_type->extension = g_utf8_strdown(p, -1);
1229 mime_type->extension = NULL;
1231 list = g_list_append(list, mime_type);
1234 procmime_fclose(fp);
1237 g_warning("Can't read mime.types\n");
1242 EncodingType procmime_get_encoding_for_charset(const gchar *charset)
1246 else if (!g_ascii_strncasecmp(charset, "ISO-2022-", 9) ||
1247 !g_ascii_strcasecmp(charset, "US-ASCII"))
1249 else if (!g_ascii_strcasecmp(charset, "ISO-8859-5") ||
1250 !g_ascii_strncasecmp(charset, "KOI8-", 5) ||
1251 !g_ascii_strcasecmp(charset, "Windows-1251"))
1253 else if (!g_ascii_strncasecmp(charset, "ISO-8859-", 9))
1254 return ENC_QUOTED_PRINTABLE;
1255 else if (!g_ascii_strncasecmp(charset, "UTF-8", 5))
1256 return ENC_QUOTED_PRINTABLE;
1261 EncodingType procmime_get_encoding_for_text_file(const gchar *file, gboolean *has_binary)
1264 guchar buf[BUFFSIZE];
1266 size_t octet_chars = 0;
1267 size_t total_len = 0;
1268 gfloat octet_percentage;
1269 gboolean force_b64 = FALSE;
1271 if ((fp = procmime_fopen(file, "rb")) == NULL) {
1272 FILE_OP_ERROR(file, "fopen");
1276 while ((len = SC_FREAD(buf, sizeof(guchar), sizeof(buf), fp)) > 0) {
1280 for (p = buf, i = 0; i < len; ++p, ++i) {
1291 procmime_fclose(fp);
1294 octet_percentage = (gfloat)octet_chars / (gfloat)total_len;
1296 octet_percentage = 0.0;
1298 debug_print("procmime_get_encoding_for_text_file(): "
1299 "8bit chars: %zd / %zd (%f%%)\n", octet_chars, total_len,
1300 100.0 * octet_percentage);
1302 if (octet_percentage > 0.20 || force_b64) {
1303 debug_print("using BASE64\n");
1305 } else if (octet_chars > 0) {
1306 debug_print("using quoted-printable\n");
1307 return ENC_QUOTED_PRINTABLE;
1309 debug_print("using 7bit\n");
1314 struct EncodingTable
1317 EncodingType enc_type;
1320 struct EncodingTable encoding_table[] = {
1323 {"binary", ENC_BINARY},
1324 {"quoted-printable", ENC_QUOTED_PRINTABLE},
1325 {"base64", ENC_BASE64},
1326 {"x-uuencode", ENC_UNKNOWN},
1327 {NULL, ENC_UNKNOWN},
1330 const gchar *procmime_get_encoding_str(EncodingType encoding)
1332 struct EncodingTable *enc_table;
1334 for (enc_table = encoding_table; enc_table->str != NULL; enc_table++) {
1335 if (enc_table->enc_type == encoding)
1336 return enc_table->str;
1341 /* --- NEW MIME STUFF --- */
1348 static struct TypeTable mime_type_table[] = {
1349 {"text", MIMETYPE_TEXT},
1350 {"image", MIMETYPE_IMAGE},
1351 {"audio", MIMETYPE_AUDIO},
1352 {"video", MIMETYPE_VIDEO},
1353 {"application", MIMETYPE_APPLICATION},
1354 {"message", MIMETYPE_MESSAGE},
1355 {"multipart", MIMETYPE_MULTIPART},
1359 const gchar *procmime_get_media_type_str(MimeMediaType type)
1361 struct TypeTable *type_table;
1363 for (type_table = mime_type_table; type_table->str != NULL; type_table++) {
1364 if (type_table->type == type)
1365 return type_table->str;
1370 MimeMediaType procmime_get_media_type(const gchar *str)
1372 struct TypeTable *typetablearray;
1374 for (typetablearray = mime_type_table; typetablearray->str != NULL; typetablearray++)
1375 if (g_ascii_strncasecmp(str, typetablearray->str, strlen(typetablearray->str)) == 0)
1376 return typetablearray->type;
1378 return MIMETYPE_UNKNOWN;
1382 *\brief Safe wrapper for content type string.
1384 *\return const gchar * Pointer to content type string.
1386 gchar *procmime_get_content_type_str(MimeMediaType type,
1387 const char *subtype)
1389 const gchar *type_str = NULL;
1391 if (subtype == NULL || !(type_str = procmime_get_media_type_str(type)))
1392 return g_strdup("unknown");
1393 return g_strdup_printf("%s/%s", type_str, subtype);
1396 static int procmime_parse_mimepart(MimeInfo *parent,
1397 gchar *content_type,
1398 gchar *content_encoding,
1399 gchar *content_description,
1401 gchar *content_disposition,
1402 gchar *content_location,
1403 const gchar *original_msgid,
1404 const gchar *disposition_notification_hdr,
1405 const gchar *filename,
1408 gboolean short_scan);
1410 static void procmime_parse_message_rfc822(MimeInfo *mimeinfo, gboolean short_scan)
1412 HeaderEntry hentry[] = {{"Content-Type:", NULL, TRUE},
1413 {"Content-Transfer-Encoding:",
1415 {"Content-Description:",
1419 {"Content-Disposition:",
1421 {"Content-Location:",
1425 {"Original-Message-ID:",
1429 {NULL, NULL, FALSE}};
1430 guint content_start, i;
1435 procmime_decode_content(mimeinfo);
1437 fp = procmime_fopen(mimeinfo->data.filename, "rb");
1439 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
1442 fseek(fp, mimeinfo->offset, SEEK_SET);
1443 procheader_get_header_fields(fp, hentry);
1444 if (hentry[0].body != NULL) {
1445 tmp = conv_unmime_header(hentry[0].body, NULL, FALSE);
1446 g_free(hentry[0].body);
1447 hentry[0].body = tmp;
1449 if (hentry[2].body != NULL) {
1450 tmp = conv_unmime_header(hentry[2].body, NULL, FALSE);
1451 g_free(hentry[2].body);
1452 hentry[2].body = tmp;
1454 if (hentry[4].body != NULL) {
1455 tmp = conv_unmime_header(hentry[4].body, NULL, FALSE);
1456 g_free(hentry[4].body);
1457 hentry[4].body = tmp;
1459 if (hentry[5].body != NULL) {
1460 tmp = conv_unmime_header(hentry[5].body, NULL, FALSE);
1461 g_free(hentry[5].body);
1462 hentry[5].body = tmp;
1464 if (hentry[7].body != NULL) {
1465 tmp = conv_unmime_header(hentry[7].body, NULL, FALSE);
1466 g_free(hentry[7].body);
1467 hentry[7].body = tmp;
1469 if (hentry[8].body != NULL) {
1470 tmp = conv_unmime_header(hentry[8].body, NULL, FALSE);
1471 g_free(hentry[8].body);
1472 hentry[8].body = tmp;
1475 content_start = ftell(fp);
1476 procmime_fclose(fp);
1478 len = mimeinfo->length - (content_start - mimeinfo->offset);
1481 procmime_parse_mimepart(mimeinfo,
1482 hentry[0].body, hentry[1].body,
1483 hentry[2].body, hentry[3].body,
1484 hentry[4].body, hentry[5].body,
1485 hentry[7].body, hentry[8].body,
1486 mimeinfo->data.filename, content_start,
1489 for (i = 0; i < (sizeof hentry / sizeof hentry[0]); i++) {
1490 g_free(hentry[i].body);
1491 hentry[i].body = NULL;
1495 static void procmime_parse_disposition_notification(MimeInfo *mimeinfo,
1496 const gchar *original_msgid, const gchar *disposition_notification_hdr,
1497 gboolean short_scan)
1499 HeaderEntry hentry[] = {{"Original-Message-ID:", NULL, TRUE},
1500 {"Disposition:", NULL, TRUE},
1501 {NULL, NULL, FALSE}};
1504 gchar *orig_msg_id = NULL;
1507 procmime_decode_content(mimeinfo);
1509 debug_print("parse disposition notification\n");
1510 fp = procmime_fopen(mimeinfo->data.filename, "rb");
1512 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
1515 fseek(fp, mimeinfo->offset, SEEK_SET);
1517 if (original_msgid && disposition_notification_hdr) {
1518 hentry[0].body = g_strdup(original_msgid);
1519 hentry[1].body = g_strdup(disposition_notification_hdr);
1521 procheader_get_header_fields(fp, hentry);
1524 procmime_fclose(fp);
1526 if (!hentry[0].body || !hentry[1].body) {
1527 debug_print("MsgId %s, Disp %s\n",
1528 hentry[0].body ? hentry[0].body:"(nil)",
1529 hentry[1].body ? hentry[1].body:"(nil)");
1533 orig_msg_id = g_strdup(hentry[0].body);
1534 disp = g_strdup(hentry[1].body);
1536 extract_parenthesis(orig_msg_id, '<', '>');
1537 remove_space(orig_msg_id);
1539 if (strstr(disp, "displayed")) {
1540 /* find sent message, if possible */
1541 MsgInfo *info = NULL;
1543 debug_print("%s has been displayed.\n", orig_msg_id);
1544 for (flist = folder_get_list(); flist != NULL; flist = g_list_next(flist)) {
1545 FolderItem *outbox = ((Folder *)(flist->data))->outbox;
1547 debug_print("skipping folder with no outbox...\n");
1550 info = folder_item_get_msginfo_by_msgid(outbox, orig_msg_id);
1551 debug_print("%s %s in %s\n", info?"found":"didn't find", orig_msg_id, outbox->path);
1553 procmsg_msginfo_set_flags(info, MSG_RETRCPT_GOT, 0);
1554 procmsg_msginfo_free(info);
1558 g_free(orig_msg_id);
1561 for (i = 0; i < (sizeof hentry / sizeof hentry[0]); i++) {
1562 g_free(hentry[i].body);
1563 hentry[i].body = NULL;
1567 #define GET_HEADERS() { \
1568 procheader_get_header_fields(fp, hentry); \
1569 if (hentry[0].body != NULL) { \
1570 tmp = conv_unmime_header(hentry[0].body, NULL, FALSE); \
1571 g_free(hentry[0].body); \
1572 hentry[0].body = tmp; \
1574 if (hentry[2].body != NULL) { \
1575 tmp = conv_unmime_header(hentry[2].body, NULL, FALSE); \
1576 g_free(hentry[2].body); \
1577 hentry[2].body = tmp; \
1579 if (hentry[4].body != NULL) { \
1580 tmp = conv_unmime_header(hentry[4].body, NULL, FALSE); \
1581 g_free(hentry[4].body); \
1582 hentry[4].body = tmp; \
1584 if (hentry[5].body != NULL) { \
1585 tmp = conv_unmime_header(hentry[5].body, NULL, FALSE); \
1586 g_free(hentry[5].body); \
1587 hentry[5].body = tmp; \
1589 if (hentry[6].body != NULL) { \
1590 tmp = conv_unmime_header(hentry[6].body, NULL, FALSE); \
1591 g_free(hentry[6].body); \
1592 hentry[6].body = tmp; \
1594 if (hentry[7].body != NULL) { \
1595 tmp = conv_unmime_header(hentry[7].body, NULL, FALSE); \
1596 g_free(hentry[7].body); \
1597 hentry[7].body = tmp; \
1601 static void procmime_parse_multipart(MimeInfo *mimeinfo, gboolean short_scan)
1603 HeaderEntry hentry[] = {{"Content-Type:", NULL, TRUE},
1604 {"Content-Transfer-Encoding:",
1606 {"Content-Description:",
1610 {"Content-Disposition:",
1612 {"Content-Location:",
1614 {"Original-Message-ID:",
1618 {NULL, NULL, FALSE}};
1621 gint boundary_len = 0, lastoffset = -1, i;
1622 gchar buf[BUFFSIZE];
1625 gboolean start_found = FALSE;
1626 gboolean end_found = FALSE;
1628 boundary = g_hash_table_lookup(mimeinfo->typeparameters, "boundary");
1631 boundary_len = strlen(boundary);
1633 procmime_decode_content(mimeinfo);
1635 fp = procmime_fopen(mimeinfo->data.filename, "rb");
1637 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
1641 fseek(fp, mimeinfo->offset, SEEK_SET);
1642 while ((p = SC_FGETS(buf, sizeof(buf), fp)) != NULL && result == 0) {
1643 if (ftell(fp) - 1 > (mimeinfo->offset + mimeinfo->length))
1646 if (IS_BOUNDARY(buf, boundary, boundary_len)) {
1649 if (lastoffset != -1) {
1650 gint len = (ftell(fp) - strlen(buf)) - lastoffset - 1;
1653 result = procmime_parse_mimepart(mimeinfo,
1654 hentry[0].body, hentry[1].body,
1655 hentry[2].body, hentry[3].body,
1656 hentry[4].body, hentry[5].body,
1657 hentry[6].body, hentry[7].body,
1658 mimeinfo->data.filename, lastoffset,
1660 if (result == 1 && short_scan)
1665 if (buf[2 + boundary_len] == '-' &&
1666 buf[2 + boundary_len + 1] == '-') {
1670 for (i = 0; i < (sizeof hentry / sizeof hentry[0]) ; i++) {
1671 g_free(hentry[i].body);
1672 hentry[i].body = NULL;
1675 lastoffset = ftell(fp);
1679 if (start_found && !end_found && lastoffset != -1) {
1680 gint len = (ftell(fp) - strlen(buf)) - lastoffset - 1;
1683 result = procmime_parse_mimepart(mimeinfo,
1684 hentry[0].body, hentry[1].body,
1685 hentry[2].body, hentry[3].body,
1686 hentry[4].body, hentry[5].body,
1687 hentry[6].body, hentry[7].body,
1688 mimeinfo->data.filename, lastoffset,
1691 mimeinfo->broken = TRUE;
1694 for (i = 0; i < (sizeof hentry / sizeof hentry[0]); i++) {
1695 g_free(hentry[i].body);
1696 hentry[i].body = NULL;
1698 procmime_fclose(fp);
1701 static void parse_parameters(const gchar *parameters, GHashTable *table)
1703 gchar *params, *param, *next;
1704 GSList *convlist = NULL, *concatlist = NULL, *cur;
1706 params = g_strdup(parameters);
1709 for (; next != NULL; param = next) {
1710 gchar *attribute, *value, *tmp, *down_attr, *orig_down_attr;
1712 gboolean convert = FALSE;
1714 next = strchr_with_skip_quote(param, '"', ';');
1723 value = strchr(attribute, '=');
1729 while (value[0] == ' ')
1732 down_attr = g_utf8_strdown(attribute, -1);
1733 orig_down_attr = down_attr;
1735 len = down_attr ? strlen(down_attr):0;
1736 if (len > 0 && down_attr[len - 1] == '*') {
1737 gchar *srcpos, *dstpos, *endpos;
1740 down_attr[len - 1] = '\0';
1744 endpos = value + strlen(value);
1745 while (srcpos < endpos) {
1751 if (!get_hex_value(&dstvalue, srcpos[1], srcpos[2]))
1761 if (value[0] == '"')
1762 extract_quote(value, '"');
1764 if (value[0] == '"')
1765 extract_quote(value, '"');
1766 else if ((tmp = strchr(value, ' ')) != NULL)
1771 while (down_attr[0] == ' ')
1773 while (down_attr[strlen(down_attr)-1] == ' ')
1774 down_attr[strlen(down_attr)-1] = '\0';
1777 while (value[0] == ' ')
1779 while (value[strlen(value)-1] == ' ')
1780 value[strlen(value)-1] = '\0';
1782 if (down_attr && strrchr(down_attr, '*') != NULL) {
1785 tmpattr = g_strdup(down_attr);
1786 tmp = strrchr(tmpattr, '*');
1789 if ((tmp[1] == '0') && (tmp[2] == '\0') &&
1790 (g_slist_find_custom(concatlist, down_attr, (GCompareFunc)g_strcmp0) == NULL))
1791 concatlist = g_slist_prepend(concatlist, g_strdup(tmpattr));
1793 if (convert && (g_slist_find_custom(convlist, tmpattr, (GCompareFunc)g_strcmp0) == NULL))
1794 convlist = g_slist_prepend(convlist, g_strdup(tmpattr));
1797 } else if (convert) {
1798 if (g_slist_find_custom(convlist, down_attr, (GCompareFunc)g_strcmp0) == NULL)
1799 convlist = g_slist_prepend(convlist, g_strdup(down_attr));
1802 if (g_hash_table_lookup(table, down_attr) == NULL)
1803 g_hash_table_insert(table, g_strdup(down_attr), g_strdup(value));
1804 g_free(orig_down_attr);
1807 for (cur = concatlist; cur != NULL; cur = g_slist_next(cur)) {
1808 gchar *attribute, *attrwnum, *partvalue;
1812 attribute = (gchar *) cur->data;
1813 value = g_string_sized_new(64);
1815 attrwnum = g_strdup_printf("%s*%d", attribute, n);
1816 while ((partvalue = g_hash_table_lookup(table, attrwnum)) != NULL) {
1817 g_string_append(value, partvalue);
1819 g_hash_table_remove(table, attrwnum);
1822 attrwnum = g_strdup_printf("%s*%d", attribute, n);
1826 g_hash_table_insert(table, g_strdup(attribute), g_strdup(value->str));
1827 g_string_free(value, TRUE);
1829 slist_free_strings_full(concatlist);
1831 for (cur = convlist; cur != NULL; cur = g_slist_next(cur)) {
1832 gchar *attribute, *key, *value;
1833 gchar *charset, *lang, *oldvalue, *newvalue;
1835 attribute = (gchar *) cur->data;
1836 if (!g_hash_table_lookup_extended(
1837 table, attribute, (gpointer *)(gchar *) &key, (gpointer *)(gchar *) &value))
1841 if (charset == NULL)
1843 lang = strchr(charset, '\'');
1848 oldvalue = strchr(lang, '\'');
1849 if (oldvalue == NULL)
1854 newvalue = conv_codeset_strdup(oldvalue, charset, CS_UTF_8);
1856 g_hash_table_remove(table, attribute);
1860 g_hash_table_insert(table, g_strdup(attribute), newvalue);
1862 slist_free_strings_full(convlist);
1867 static void procmime_parse_content_type(const gchar *content_type, MimeInfo *mimeinfo)
1869 cm_return_if_fail(content_type != NULL);
1870 cm_return_if_fail(mimeinfo != NULL);
1872 /* RFC 2045, page 13 says that the mime subtype is MANDATORY;
1873 * if it's not available we use the default Content-Type */
1874 if ((content_type[0] == '\0') || (strchr(content_type, '/') == NULL)) {
1875 mimeinfo->type = MIMETYPE_TEXT;
1876 mimeinfo->subtype = g_strdup("plain");
1877 if (g_hash_table_lookup(mimeinfo->typeparameters,
1878 "charset") == NULL) {
1879 g_hash_table_insert(mimeinfo->typeparameters,
1880 g_strdup("charset"),
1882 conv_get_locale_charset_str_no_utf8()));
1885 gchar *type, *subtype, *params;
1887 type = g_strdup(content_type);
1888 subtype = strchr(type, '/') + 1;
1889 *(subtype - 1) = '\0';
1890 if ((params = strchr(subtype, ';')) != NULL) {
1895 mimeinfo->type = procmime_get_media_type(type);
1896 mimeinfo->subtype = g_strstrip(g_strdup(subtype));
1898 /* Get mimeinfo->typeparameters */
1900 parse_parameters(params, mimeinfo->typeparameters);
1906 static void procmime_parse_content_disposition(const gchar *content_disposition, MimeInfo *mimeinfo)
1908 gchar *tmp, *params;
1910 cm_return_if_fail(content_disposition != NULL);
1911 cm_return_if_fail(mimeinfo != NULL);
1913 tmp = g_strdup(content_disposition);
1914 if ((params = strchr(tmp, ';')) != NULL) {
1920 if (!g_ascii_strcasecmp(tmp, "inline"))
1921 mimeinfo->disposition = DISPOSITIONTYPE_INLINE;
1922 else if (!g_ascii_strcasecmp(tmp, "attachment"))
1923 mimeinfo->disposition = DISPOSITIONTYPE_ATTACHMENT;
1925 mimeinfo->disposition = DISPOSITIONTYPE_ATTACHMENT;
1928 parse_parameters(params, mimeinfo->dispositionparameters);
1934 static void procmime_parse_content_encoding(const gchar *content_encoding, MimeInfo *mimeinfo)
1936 struct EncodingTable *enc_table;
1938 for (enc_table = encoding_table; enc_table->str != NULL; enc_table++) {
1939 if (g_ascii_strcasecmp(enc_table->str, content_encoding) == 0) {
1940 mimeinfo->encoding_type = enc_table->enc_type;
1944 mimeinfo->encoding_type = ENC_UNKNOWN;
1948 static GSList *registered_parsers = NULL;
1950 static MimeParser *procmime_get_mimeparser_for_type(MimeMediaType type, const gchar *sub_type)
1953 for (cur = registered_parsers; cur; cur = cur->next) {
1954 MimeParser *parser = (MimeParser *)cur->data;
1955 if (parser->type == type && !strcmp2(parser->sub_type, sub_type))
1961 void procmime_mimeparser_register(MimeParser *parser)
1963 if (!procmime_get_mimeparser_for_type(parser->type, parser->sub_type))
1964 registered_parsers = g_slist_append(registered_parsers, parser);
1968 void procmime_mimeparser_unregister(MimeParser *parser)
1970 registered_parsers = g_slist_remove(registered_parsers, parser);
1973 static gboolean procmime_mimeparser_parse(MimeParser *parser, MimeInfo *mimeinfo)
1975 cm_return_val_if_fail(parser->parse != NULL, FALSE);
1976 return parser->parse(parser, mimeinfo);
1979 static int procmime_parse_mimepart(MimeInfo *parent,
1980 gchar *content_type,
1981 gchar *content_encoding,
1982 gchar *content_description,
1984 gchar *content_disposition,
1985 gchar *content_location,
1986 const gchar *original_msgid,
1987 const gchar *disposition_notification_hdr,
1988 const gchar *filename,
1991 gboolean short_scan)
1994 MimeParser *parser = NULL;
1995 gboolean parsed = FALSE;
1998 /* Create MimeInfo */
1999 mimeinfo = procmime_mimeinfo_new();
2000 mimeinfo->content = MIMECONTENT_FILE;
2002 if (parent != NULL) {
2003 if (g_node_depth(parent->node) > 32) {
2004 /* 32 is an arbitrary value
2005 * this avoids DOSsing ourselves
2006 * with enormous messages
2008 procmime_mimeinfo_free_all(mimeinfo);
2011 g_node_append(parent->node, mimeinfo->node);
2013 mimeinfo->data.filename = g_strdup(filename);
2014 mimeinfo->offset = offset;
2015 mimeinfo->length = length;
2017 if (content_type != NULL) {
2018 g_strchomp(content_type);
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 g_strchomp(content_encoding);
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 g_strchomp(content_disposition);
2056 procmime_parse_content_disposition(content_disposition, mimeinfo);
2058 mimeinfo->disposition = DISPOSITIONTYPE_UNKNOWN;
2060 /* Call parser for mime type */
2061 if ((parser = procmime_get_mimeparser_for_type(mimeinfo->type, mimeinfo->subtype)) != NULL) {
2062 parsed = procmime_mimeparser_parse(parser, mimeinfo);
2065 switch (mimeinfo->type) {
2067 if (g_ascii_strcasecmp(mimeinfo->subtype, "plain") == 0 && short_scan) {
2072 case MIMETYPE_MESSAGE:
2073 if (g_ascii_strcasecmp(mimeinfo->subtype, "rfc822") == 0) {
2074 procmime_parse_message_rfc822(mimeinfo, short_scan);
2076 if (g_ascii_strcasecmp(mimeinfo->subtype, "disposition-notification") == 0) {
2077 procmime_parse_disposition_notification(mimeinfo,
2078 original_msgid, disposition_notification_hdr, short_scan);
2082 case MIMETYPE_MULTIPART:
2083 procmime_parse_multipart(mimeinfo, short_scan);
2086 case MIMETYPE_APPLICATION:
2087 if (g_ascii_strcasecmp(mimeinfo->subtype, "octet-stream") == 0
2088 && original_msgid && *original_msgid
2089 && disposition_notification_hdr && *disposition_notification_hdr) {
2090 procmime_parse_disposition_notification(mimeinfo,
2091 original_msgid, disposition_notification_hdr, short_scan);
2102 static gchar *typenames[] = {
2113 static gboolean output_func(GNode *node, gpointer data)
2116 MimeInfo *mimeinfo = (MimeInfo *) node->data;
2118 depth = g_node_depth(node);
2119 for (i = 0; i < depth; i++)
2121 g_print("%s/%s (offset:%d length:%d encoding: %d)\n", typenames[mimeinfo->type], mimeinfo->subtype, mimeinfo->offset, mimeinfo->length, mimeinfo->encoding_type);
2126 static void output_mime_structure(MimeInfo *mimeinfo, int indent)
2128 g_node_traverse(mimeinfo->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1, output_func, NULL);
2131 static MimeInfo *procmime_scan_file_with_offset(const gchar *filename, int offset, gboolean short_scan)
2136 g_stat(filename, &buf);
2138 mimeinfo = procmime_mimeinfo_new();
2139 mimeinfo->content = MIMECONTENT_FILE;
2140 mimeinfo->encoding_type = ENC_UNKNOWN;
2141 mimeinfo->type = MIMETYPE_MESSAGE;
2142 mimeinfo->subtype = g_strdup("rfc822");
2143 mimeinfo->data.filename = g_strdup(filename);
2144 mimeinfo->offset = offset;
2145 mimeinfo->length = buf.st_size - offset;
2147 procmime_parse_message_rfc822(mimeinfo, short_scan);
2148 if (debug_get_mode())
2149 output_mime_structure(mimeinfo, 0);
2154 static MimeInfo *procmime_scan_file_full(const gchar *filename, gboolean short_scan)
2158 cm_return_val_if_fail(filename != NULL, NULL);
2160 mimeinfo = procmime_scan_file_with_offset(filename, 0, short_scan);
2165 MimeInfo *procmime_scan_file(const gchar *filename)
2167 return procmime_scan_file_full(filename, FALSE);
2170 static MimeInfo *procmime_scan_file_short(const gchar *filename)
2172 return procmime_scan_file_full(filename, TRUE);
2175 static MimeInfo *procmime_scan_queue_file_full(const gchar *filename, gboolean short_scan)
2179 gchar buf[BUFFSIZE];
2182 cm_return_val_if_fail(filename != NULL, NULL);
2185 if ((fp = procmime_fopen(filename, "rb")) == NULL)
2187 /* Skip queue header */
2188 while (SC_FGETS(buf, sizeof(buf), fp) != NULL) {
2190 if ((!strncmp(buf, "X-Claws-End-Special-Headers: 1",
2191 strlen("X-Claws-End-Special-Headers:"))) ||
2192 (!strncmp(buf, "X-Sylpheed-End-Special-Headers: 1",
2193 strlen("X-Sylpheed-End-Special-Headers:"))))
2196 if (buf[0] == '\r' || buf[0] == '\n') break;
2197 /* from other mailers */
2198 if (!strncmp(buf, "Date: ", 6)
2199 || !strncmp(buf, "To: ", 4)
2200 || !strncmp(buf, "From: ", 6)
2201 || !strncmp(buf, "Subject: ", 9)) {
2207 procmime_fclose(fp);
2209 mimeinfo = procmime_scan_file_with_offset(filename, offset, short_scan);
2214 MimeInfo *procmime_scan_queue_file(const gchar *filename)
2216 return procmime_scan_queue_file_full(filename, FALSE);
2219 static MimeInfo *procmime_scan_queue_file_short(const gchar *filename)
2221 return procmime_scan_queue_file_full(filename, TRUE);
2226 ENC_AS_QUOTED_STRING,
2231 typedef struct _ParametersData {
2237 static void write_parameters(gpointer key, gpointer value, gpointer user_data)
2240 gchar *val = value, *valpos, *tmp;
2241 ParametersData *pdata = (ParametersData *)user_data;
2242 GString *buf = g_string_new("");
2245 EncodeAs encas = ENC_AS_TOKEN;
2247 for (valpos = val; *valpos != 0; valpos++) {
2248 if (!IS_ASCII(*valpos)) {
2249 encas = ENC_AS_ENCWORD;
2254 if (((*valpos >= 0) && (*valpos < 037)) || (*valpos == 0177)) {
2255 encas = ENC_AS_QUOTED_STRING;
2259 /* tspecials + SPACE */
2277 encas = ENC_AS_QUOTED_STRING;
2284 g_string_append_printf(buf, "%s=%s", param, val);
2287 case ENC_AS_QUOTED_STRING:
2288 g_string_append_printf(buf, "%s=\"%s\"", param, val);
2291 #if 0 /* we don't use that for now */
2292 case ENC_AS_EXTENDED:
2293 if (!g_utf8_validate(val, -1, NULL))
2294 g_string_append_printf(buf, "%s*=%s''", param,
2295 conv_get_locale_charset_str());
2297 g_string_append_printf(buf, "%s*=%s''", param,
2299 for (valpos = val; *valpos != '\0'; valpos++) {
2300 if (IS_ASCII(*valpos) && isalnum(*valpos)) {
2301 g_string_append_printf(buf, "%c", *valpos);
2303 gchar hexstr[3] = "XX";
2304 get_hex_str(hexstr, *valpos);
2305 g_string_append_printf(buf, "%%%s", hexstr);
2310 case ENC_AS_EXTENDED:
2311 debug_print("Unhandled ENC_AS_EXTENDED.");
2314 case ENC_AS_ENCWORD:
2315 len = MAX(strlen(val)*6, 512);
2316 tmp = g_malloc(len+1);
2317 codeconv_set_strict(TRUE);
2318 conv_encode_header_full(tmp, len, val, pdata->len + strlen(param) + 4 , FALSE,
2319 prefs_common.outgoing_charset);
2320 codeconv_set_strict(FALSE);
2321 if (!tmp || !*tmp) {
2322 codeconv_set_strict(TRUE);
2323 conv_encode_header_full(tmp, len, val, pdata->len + strlen(param) + 4 , FALSE,
2324 conv_get_outgoing_charset_str());
2325 codeconv_set_strict(FALSE);
2327 if (!tmp || !*tmp) {
2328 codeconv_set_strict(TRUE);
2329 conv_encode_header_full(tmp, len, val, pdata->len + strlen(param) + 4 , FALSE,
2331 codeconv_set_strict(FALSE);
2333 if (!tmp || !*tmp) {
2334 conv_encode_header_full(tmp, len, val, pdata->len + strlen(param) + 4 , FALSE,
2337 g_string_append_printf(buf, "%s=\"%s\"", param, tmp);
2343 if (buf->str && strlen(buf->str)) {
2344 tmp = strstr(buf->str, "\n");
2346 len = (tmp - buf->str);
2348 len = strlen(buf->str);
2349 if (pdata->len + len > 76) {
2350 if (fprintf(pdata->fp, ";\n %s", buf->str) < 0)
2351 pdata->error = TRUE;
2352 pdata->len = strlen(buf->str) + 1;
2354 if (fprintf(pdata->fp, "; %s", buf->str) < 0)
2355 pdata->error = TRUE;
2356 pdata->len += strlen(buf->str) + 2;
2359 g_string_free(buf, TRUE);
2362 #define TRY(func) { \
2368 int procmime_write_mime_header(MimeInfo *mimeinfo, FILE *fp)
2370 struct TypeTable *type_table;
2371 ParametersData *pdata = g_new0(ParametersData, 1);
2372 debug_print("procmime_write_mime_header\n");
2375 pdata->error = FALSE;
2376 for (type_table = mime_type_table; type_table->str != NULL; type_table++)
2377 if (mimeinfo->type == type_table->type) {
2378 gchar *buf = g_strdup_printf(
2379 "Content-Type: %s/%s", type_table->str, mimeinfo->subtype);
2380 if (fprintf(fp, "%s", buf) < 0) {
2385 pdata->len = strlen(buf);
2389 g_hash_table_foreach(mimeinfo->typeparameters, write_parameters, pdata);
2390 if (pdata->error == TRUE) {
2396 TRY(fprintf(fp, "\n") >= 0);
2398 if (mimeinfo->encoding_type != ENC_UNKNOWN)
2399 TRY(fprintf(fp, "Content-Transfer-Encoding: %s\n", procmime_get_encoding_str(mimeinfo->encoding_type)) >= 0);
2401 if (mimeinfo->description != NULL)
2402 TRY(fprintf(fp, "Content-Description: %s\n", mimeinfo->description) >= 0);
2404 if (mimeinfo->id != NULL)
2405 TRY(fprintf(fp, "Content-ID: %s\n", mimeinfo->id) >= 0);
2407 if (mimeinfo->location != NULL)
2408 TRY(fprintf(fp, "Content-Location: %s\n", mimeinfo->location) >= 0);
2410 if (mimeinfo->disposition != DISPOSITIONTYPE_UNKNOWN) {
2411 ParametersData *pdata = g_new0(ParametersData, 1);
2413 if (mimeinfo->disposition == DISPOSITIONTYPE_INLINE)
2414 buf = g_strdup("Content-Disposition: inline");
2415 else if (mimeinfo->disposition == DISPOSITIONTYPE_ATTACHMENT)
2416 buf = g_strdup("Content-Disposition: attachment");
2418 buf = g_strdup("Content-Disposition: unknown");
2420 if (fprintf(fp, "%s", buf) < 0) {
2425 pdata->len = strlen(buf);
2429 pdata->error = FALSE;
2430 g_hash_table_foreach(mimeinfo->dispositionparameters, write_parameters, pdata);
2431 if (pdata->error == TRUE) {
2436 TRY(fprintf(fp, "\n") >= 0);
2439 TRY(fprintf(fp, "\n") >= 0);
2444 static gint procmime_write_message_rfc822(MimeInfo *mimeinfo, FILE *fp)
2449 gchar buf[BUFFSIZE];
2450 gboolean skip = FALSE;;
2453 debug_print("procmime_write_message_rfc822\n");
2456 switch (mimeinfo->content) {
2457 case MIMECONTENT_FILE:
2458 if ((infp = procmime_fopen(mimeinfo->data.filename, "rb")) == NULL) {
2459 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
2462 fseek(infp, mimeinfo->offset, SEEK_SET);
2463 while (SC_FGETS(buf, sizeof(buf), infp) == buf) {
2465 if (buf[0] == '\n' && buf[1] == '\0')
2467 if (skip && (buf[0] == ' ' || buf[0] == '\t'))
2469 if (g_ascii_strncasecmp(buf, "MIME-Version:", 13) == 0 ||
2470 g_ascii_strncasecmp(buf, "Content-Type:", 13) == 0 ||
2471 g_ascii_strncasecmp(buf, "Content-Transfer-Encoding:", 26) == 0 ||
2472 g_ascii_strncasecmp(buf, "Content-Description:", 20) == 0 ||
2473 g_ascii_strncasecmp(buf, "Content-ID:", 11) == 0 ||
2474 g_ascii_strncasecmp(buf, "Content-Location:", 17) == 0 ||
2475 g_ascii_strncasecmp(buf, "Content-Disposition:", 20) == 0) {
2480 if (SC_FWRITE(buf, sizeof(gchar), len, fp) < len) {
2481 g_warning("failed to dump %zd bytes from file", len);
2482 procmime_fclose(infp);
2487 procmime_fclose(infp);
2490 case MIMECONTENT_MEM:
2491 len = strlen(mimeinfo->data.mem);
2492 if (SC_FWRITE(mimeinfo->data.mem, sizeof(gchar), len, fp) < len) {
2493 g_warning("failed to dump %zd bytes from mem", len);
2502 childnode = mimeinfo->node->children;
2503 if (childnode == NULL)
2506 child = (MimeInfo *) childnode->data;
2507 if (fprintf(fp, "MIME-Version: 1.0\n") < 0) {
2508 g_warning("failed to write mime version");
2511 if (procmime_write_mime_header(child, fp) < 0)
2513 return procmime_write_mimeinfo(child, fp);
2516 static gint procmime_write_multipart(MimeInfo *mimeinfo, FILE *fp)
2520 gchar *boundary, *str, *str2;
2521 gchar buf[BUFFSIZE];
2522 gboolean firstboundary;
2525 debug_print("procmime_write_multipart\n");
2527 boundary = g_hash_table_lookup(mimeinfo->typeparameters, "boundary");
2529 switch (mimeinfo->content) {
2530 case MIMECONTENT_FILE:
2531 if ((infp = procmime_fopen(mimeinfo->data.filename, "rb")) == NULL) {
2532 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
2535 fseek(infp, mimeinfo->offset, SEEK_SET);
2536 while (SC_FGETS(buf, sizeof(buf), infp) == buf) {
2537 if (IS_BOUNDARY(buf, boundary, strlen(boundary)))
2540 if (SC_FWRITE(buf, sizeof(gchar), len, fp) < len) {
2541 g_warning("failed to write %zd", len);
2542 procmime_fclose(infp);
2546 procmime_fclose(infp);
2549 case MIMECONTENT_MEM:
2550 str = g_strdup(mimeinfo->data.mem);
2551 if (((str2 = strstr(str, boundary)) != NULL) && ((str2 - str) >= 2) &&
2552 (*(str2 - 1) == '-') && (*(str2 - 2) == '-'))
2555 if (SC_FWRITE(str, sizeof(gchar), len, fp) < len) {
2556 g_warning("failed to write %zd from mem", len);
2567 childnode = mimeinfo->node->children;
2568 firstboundary = TRUE;
2569 while (childnode != NULL) {
2570 MimeInfo *child = childnode->data;
2573 firstboundary = FALSE;
2575 TRY(fprintf(fp, "\n") >= 0);
2577 TRY(fprintf(fp, "--%s\n", boundary) >= 0);
2579 if (procmime_write_mime_header(child, fp) < 0)
2581 if (procmime_write_mimeinfo(child, fp) < 0)
2584 childnode = g_node_next_sibling(childnode);
2586 TRY(fprintf(fp, "\n--%s--\n", boundary) >= 0);
2591 gint procmime_write_mimeinfo(MimeInfo *mimeinfo, FILE *fp)
2595 debug_print("procmime_write_mimeinfo\n");
2597 if (G_NODE_IS_LEAF(mimeinfo->node)) {
2598 switch (mimeinfo->content) {
2599 case MIMECONTENT_FILE:
2600 if ((infp = procmime_fopen(mimeinfo->data.filename, "rb")) == NULL) {
2601 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
2604 copy_file_part_to_fp(infp, mimeinfo->offset, mimeinfo->length, fp);
2605 procmime_fclose(infp);
2608 case MIMECONTENT_MEM:
2609 len = strlen(mimeinfo->data.mem);
2610 if (SC_FWRITE(mimeinfo->data.mem, sizeof(gchar), len, fp) < len)
2618 /* Call writer for mime type */
2619 switch (mimeinfo->type) {
2620 case MIMETYPE_MESSAGE:
2621 if (g_ascii_strcasecmp(mimeinfo->subtype, "rfc822") == 0) {
2622 return procmime_write_message_rfc822(mimeinfo, fp);
2626 case MIMETYPE_MULTIPART:
2627 return procmime_write_multipart(mimeinfo, fp);
2639 gchar *procmime_get_part_file_name(MimeInfo *mimeinfo)
2643 if ((mimeinfo->type == MIMETYPE_TEXT) && !g_ascii_strcasecmp(mimeinfo->subtype, "html"))
2644 base = g_strdup("mimetmp.html");
2646 const gchar *basetmp;
2649 basetmp = procmime_mimeinfo_get_parameter(mimeinfo, "filename");
2650 if (basetmp == NULL)
2651 basetmp = procmime_mimeinfo_get_parameter(mimeinfo, "name");
2652 if (basetmp == NULL)
2653 basetmp = "mimetmp";
2654 basename = g_path_get_basename(basetmp);
2655 if (*basename == '\0') {
2657 basename = g_strdup("mimetmp");
2659 base = conv_filename_from_utf8(basename);
2661 subst_for_shellsafe_filename(base);