2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2007 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/>.
28 #include <glib/gi18n.h>
35 #include <sys/types.h>
41 #include "procheader.h"
43 #include "quoted-printable.h"
50 #include "prefs_common.h"
51 #include "prefs_gtk.h"
52 #include "alertpanel.h"
55 static GHashTable *procmime_get_mime_type_table (void);
56 static MimeInfo *procmime_scan_file_short(const gchar *filename);
57 static MimeInfo *procmime_scan_queue_file_short(const gchar *filename);
58 static MimeInfo *procmime_scan_queue_file_full(const gchar *filename, gboolean short_scan);
60 MimeInfo *procmime_mimeinfo_new(void)
64 mimeinfo = g_new0(MimeInfo, 1);
65 mimeinfo->content = MIMECONTENT_EMPTY;
66 mimeinfo->data.filename = NULL;
68 mimeinfo->type = MIMETYPE_UNKNOWN;
69 mimeinfo->encoding_type = ENC_UNKNOWN;
70 mimeinfo->typeparameters = g_hash_table_new(g_str_hash, g_str_equal);
72 mimeinfo->disposition = DISPOSITIONTYPE_UNKNOWN;
73 mimeinfo->dispositionparameters
74 = g_hash_table_new(g_str_hash, g_str_equal);
76 mimeinfo->node = g_node_new(mimeinfo);
81 static gboolean procmime_mimeinfo_parameters_destroy(gpointer key, gpointer value, gpointer user_data)
89 static gchar *forced_charset = NULL;
91 void procmime_force_charset(const gchar *str)
93 g_free(forced_charset);
94 forced_charset = NULL;
96 forced_charset = g_strdup(str);
99 static EncodingType forced_encoding = 0;
101 void procmime_force_encoding(EncodingType encoding)
103 forced_encoding = encoding;
106 static gboolean free_func(GNode *node, gpointer data)
108 MimeInfo *mimeinfo = (MimeInfo *) node->data;
110 switch (mimeinfo->content) {
111 case MIMECONTENT_FILE:
113 g_unlink(mimeinfo->data.filename);
114 g_free(mimeinfo->data.filename);
117 case MIMECONTENT_MEM:
119 g_free(mimeinfo->data.mem);
124 g_free(mimeinfo->subtype);
125 g_free(mimeinfo->description);
126 g_free(mimeinfo->id);
127 g_free(mimeinfo->location);
129 g_hash_table_foreach_remove(mimeinfo->typeparameters,
130 procmime_mimeinfo_parameters_destroy, NULL);
131 g_hash_table_destroy(mimeinfo->typeparameters);
132 g_hash_table_foreach_remove(mimeinfo->dispositionparameters,
133 procmime_mimeinfo_parameters_destroy, NULL);
134 g_hash_table_destroy(mimeinfo->dispositionparameters);
136 if (mimeinfo->privacy)
137 privacy_free_privacydata(mimeinfo->privacy);
144 void procmime_mimeinfo_free_all(MimeInfo *mimeinfo)
151 node = mimeinfo->node;
152 g_node_traverse(node, G_IN_ORDER, G_TRAVERSE_ALL, -1, free_func, NULL);
154 g_node_destroy(node);
157 MimeInfo *procmime_mimeinfo_parent(MimeInfo *mimeinfo)
159 g_return_val_if_fail(mimeinfo != NULL, NULL);
160 g_return_val_if_fail(mimeinfo->node != NULL, NULL);
162 if (mimeinfo->node->parent == NULL)
164 return (MimeInfo *) mimeinfo->node->parent->data;
167 MimeInfo *procmime_mimeinfo_next(MimeInfo *mimeinfo)
169 g_return_val_if_fail(mimeinfo != NULL, NULL);
170 g_return_val_if_fail(mimeinfo->node != NULL, NULL);
172 if (mimeinfo->node->children)
173 return (MimeInfo *) mimeinfo->node->children->data;
174 if (mimeinfo->node->next)
175 return (MimeInfo *) mimeinfo->node->next->data;
177 if (mimeinfo->node->parent == NULL)
180 while (mimeinfo->node->parent != NULL) {
181 mimeinfo = (MimeInfo *) mimeinfo->node->parent->data;
182 if (mimeinfo->node->next)
183 return (MimeInfo *) mimeinfo->node->next->data;
189 MimeInfo *procmime_scan_message(MsgInfo *msginfo)
194 filename = procmsg_get_message_file_path(msginfo);
195 if (!filename || !is_file_exist(filename)) {
200 if (!folder_has_parent_of_type(msginfo->folder, F_QUEUE) &&
201 !folder_has_parent_of_type(msginfo->folder, F_DRAFT))
202 mimeinfo = procmime_scan_file(filename);
204 mimeinfo = procmime_scan_queue_file(filename);
211 MimeInfo *procmime_scan_message_short(MsgInfo *msginfo)
216 filename = procmsg_get_message_file_path(msginfo);
217 if (!filename || !is_file_exist(filename)) {
222 if (!folder_has_parent_of_type(msginfo->folder, F_QUEUE) &&
223 !folder_has_parent_of_type(msginfo->folder, F_DRAFT))
224 mimeinfo = procmime_scan_file_short(filename);
226 mimeinfo = procmime_scan_queue_file_short(filename);
234 H_CONTENT_TRANSFER_ENCODING = 0,
236 H_CONTENT_DISPOSITION = 2,
237 H_CONTENT_DESCRIPTION = 3,
241 const gchar *procmime_mimeinfo_get_parameter(MimeInfo *mimeinfo, const gchar *name)
245 g_return_val_if_fail(mimeinfo != NULL, NULL);
246 g_return_val_if_fail(name != NULL, NULL);
248 value = g_hash_table_lookup(mimeinfo->dispositionparameters, name);
250 value = g_hash_table_lookup(mimeinfo->typeparameters, name);
255 #define FLUSH_LASTLINE() { \
256 if (*lastline != '\0') { \
258 strretchomp(lastline); \
259 llen = strlen(lastline); \
260 if (lastline[llen-1] == ' ' && strcmp(lastline,"-- ")) { \
261 /* this is flowed */ \
263 lastline[llen-1] = '\0'; \
264 if (fputs(lastline, outfp) == EOF) \
267 if (fputs(lastline, outfp) == EOF) \
269 if (fputs("\n", outfp) == EOF) \
273 strcpy(lastline, buf); \
276 gboolean procmime_decode_content(MimeInfo *mimeinfo)
283 gboolean tmp_file = FALSE;
284 gboolean flowed = FALSE;
285 gboolean delsp = FALSE;
286 gboolean err = FALSE;
288 EncodingType encoding = forced_encoding
290 : mimeinfo->encoding_type;
291 gchar lastline[BUFFSIZE];
292 memset(lastline, 0, BUFFSIZE);
294 g_return_val_if_fail(mimeinfo != NULL, FALSE);
296 if (prefs_common.respect_flowed_format &&
297 mimeinfo->type == MIMETYPE_TEXT &&
298 !strcasecmp(mimeinfo->subtype, "plain")) {
299 if (procmime_mimeinfo_get_parameter(mimeinfo, "format") != NULL &&
300 !strcasecmp(procmime_mimeinfo_get_parameter(mimeinfo, "format"),"flowed"))
303 procmime_mimeinfo_get_parameter(mimeinfo, "delsp") != NULL &&
304 !strcasecmp(procmime_mimeinfo_get_parameter(mimeinfo, "delsp"),"yes"))
309 encoding == ENC_UNKNOWN ||
310 encoding == ENC_BINARY ||
311 encoding == ENC_7BIT ||
316 infp = g_fopen(mimeinfo->data.filename, "rb");
321 fseek(infp, mimeinfo->offset, SEEK_SET);
323 outfp = get_tmpfile_in_dir(get_mime_tmp_dir(), &tmpfilename);
330 readend = mimeinfo->offset + mimeinfo->length;
332 if (encoding == ENC_QUOTED_PRINTABLE) {
333 while ((ftell(infp) < readend) && (fgets(buf, sizeof(buf), infp) != NULL)) {
335 len = qp_decode_line(buf);
338 if (fwrite(buf, 1, len, outfp) < len)
346 } else if (encoding == ENC_BASE64) {
347 gchar outbuf[BUFFSIZE];
349 Base64Decoder *decoder;
350 gboolean got_error = FALSE;
351 gboolean uncanonicalize = FALSE;
354 if (mimeinfo->type == MIMETYPE_TEXT ||
355 mimeinfo->type == MIMETYPE_MESSAGE) {
356 uncanonicalize = TRUE;
357 tmpfp = my_tmpfile();
360 if (tmp_file) fclose(outfp);
366 decoder = base64_decoder_new();
367 while ((ftell(infp) < readend) && (fgets(buf, sizeof(buf), infp) != NULL)) {
368 len = base64_decoder_decode(decoder, buf, outbuf);
369 if (len < 0 && !got_error) {
370 g_warning("Bad BASE64 content.\n");
371 if (fwrite(_("[Error decoding BASE64]\n"),
373 strlen(_("[Error decoding BASE64]\n")),
374 tmpfp) < strlen(_("[Error decoding BASE64]\n")))
375 g_warning("error decoding BASE64");
378 } else if (len >= 0) {
379 /* print out the error message only once
381 if (fwrite(outbuf, sizeof(gchar), len, tmpfp) < len)
386 base64_decoder_free(decoder);
388 if (uncanonicalize) {
390 while (fgets(buf, sizeof(buf), tmpfp) != NULL) {
392 if (fputs(buf, outfp) == EOF)
397 } else if (encoding == ENC_X_UUENCODE) {
398 gchar outbuf[BUFFSIZE];
400 gboolean flag = FALSE;
402 while ((ftell(infp) < readend) && (fgets(buf, sizeof(buf), infp) != NULL)) {
403 if (!flag && strncmp(buf,"begin ", 6)) continue;
406 len = fromuutobits(outbuf, buf);
409 g_warning("Bad UUENCODE content(%d)\n", len);
412 if (fwrite(outbuf, sizeof(gchar), len, outfp) < len)
418 while ((ftell(infp) < readend) && (fgets(buf, sizeof(buf), infp) != NULL)) {
420 if (fputs(buf, outfp) == EOF)
429 g_warning("write error");
439 stat(tmpfilename, &statbuf);
440 if (mimeinfo->tmp && (mimeinfo->data.filename != NULL))
441 g_unlink(mimeinfo->data.filename);
442 g_free(mimeinfo->data.filename);
443 mimeinfo->data.filename = tmpfilename;
444 mimeinfo->tmp = TRUE;
445 mimeinfo->offset = 0;
446 mimeinfo->length = statbuf.st_size;
447 mimeinfo->encoding_type = ENC_BINARY;
452 #define B64_LINE_SIZE 57
453 #define B64_BUFFSIZE 77
455 gboolean procmime_encode_content(MimeInfo *mimeinfo, EncodingType encoding)
457 FILE *infp = NULL, *outfp;
461 gboolean err = FALSE;
463 if (mimeinfo->content == MIMECONTENT_EMPTY)
466 if (mimeinfo->encoding_type != ENC_UNKNOWN &&
467 mimeinfo->encoding_type != ENC_BINARY &&
468 mimeinfo->encoding_type != ENC_7BIT &&
469 mimeinfo->encoding_type != ENC_8BIT)
470 if(!procmime_decode_content(mimeinfo))
473 outfp = get_tmpfile_in_dir(get_mime_tmp_dir(), &tmpfilename);
479 if (mimeinfo->content == MIMECONTENT_FILE) {
480 if ((infp = g_fopen(mimeinfo->data.filename, "rb")) == NULL) {
481 g_warning("Can't open file %s\n", mimeinfo->data.filename);
484 } else if (mimeinfo->content == MIMECONTENT_MEM) {
485 infp = str_open_as_stream(mimeinfo->data.mem);
490 if (encoding == ENC_BASE64) {
491 gchar inbuf[B64_LINE_SIZE], outbuf[B64_BUFFSIZE];
493 gchar *tmp_file = NULL;
495 if (mimeinfo->type == MIMETYPE_TEXT ||
496 mimeinfo->type == MIMETYPE_MESSAGE) {
497 if (mimeinfo->content == MIMECONTENT_FILE) {
498 tmp_file = get_tmp_file();
499 if (canonicalize_file(mimeinfo->data.filename, tmp_file) < 0) {
504 if ((tmp_fp = g_fopen(tmp_file, "rb")) == NULL) {
505 FILE_OP_ERROR(tmp_file, "fopen");
512 gchar *out = canonicalize_str(mimeinfo->data.mem);
514 infp = str_open_as_stream(out);
522 while ((len = fread(inbuf, sizeof(gchar),
523 B64_LINE_SIZE, tmp_fp))
525 base64_encode(outbuf, inbuf, B64_LINE_SIZE);
526 if (fputs(outbuf, outfp) == EOF)
528 if (fputc('\n', outfp) == EOF)
531 if (len > 0 && feof(tmp_fp)) {
532 base64_encode(outbuf, inbuf, len);
533 if (fputs(outbuf, outfp) == EOF)
535 if (fputc('\n', outfp) == EOF)
544 } else if (encoding == ENC_QUOTED_PRINTABLE) {
545 gchar inbuf[BUFFSIZE], outbuf[BUFFSIZE * 4];
547 while (fgets(inbuf, sizeof(inbuf), infp) != NULL) {
548 qp_encode_line(outbuf, inbuf);
550 if (!strncmp("From ", outbuf, sizeof("From ")-1)) {
551 gchar *tmpbuf = outbuf;
553 tmpbuf += sizeof("From ")-1;
555 if (fputs("=46rom ", outfp) == EOF)
557 if (fputs(tmpbuf, outfp) == EOF)
560 if (fputs(outbuf, outfp) == EOF)
567 while (fgets(buf, sizeof(buf), infp) != NULL) {
569 if (fputs(buf, outfp) == EOF)
580 if (mimeinfo->content == MIMECONTENT_FILE) {
581 if (mimeinfo->tmp && (mimeinfo->data.filename != NULL))
582 g_unlink(mimeinfo->data.filename);
583 g_free(mimeinfo->data.filename);
584 } else if (mimeinfo->content == MIMECONTENT_MEM) {
585 if (mimeinfo->tmp && (mimeinfo->data.mem != NULL))
586 g_free(mimeinfo->data.mem);
589 stat(tmpfilename, &statbuf);
590 mimeinfo->content = MIMECONTENT_FILE;
591 mimeinfo->data.filename = tmpfilename;
592 mimeinfo->tmp = TRUE;
593 mimeinfo->offset = 0;
594 mimeinfo->length = statbuf.st_size;
595 mimeinfo->encoding_type = encoding;
600 gint procmime_get_part(const gchar *outfile, MimeInfo *mimeinfo)
604 gint restlength, readlength;
605 gint saved_errno = 0;
607 g_return_val_if_fail(outfile != NULL, -1);
608 g_return_val_if_fail(mimeinfo != NULL, -1);
610 if (mimeinfo->encoding_type != ENC_BINARY && !procmime_decode_content(mimeinfo))
613 if ((infp = g_fopen(mimeinfo->data.filename, "rb")) == NULL) {
615 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
616 return -(saved_errno);
618 if (fseek(infp, mimeinfo->offset, SEEK_SET) < 0) {
620 FILE_OP_ERROR(mimeinfo->data.filename, "fseek");
622 return -(saved_errno);
624 if ((outfp = g_fopen(outfile, "wb")) == NULL) {
626 FILE_OP_ERROR(outfile, "fopen");
628 return -(saved_errno);
631 restlength = mimeinfo->length;
633 while ((restlength > 0) && ((readlength = fread(buf, 1, restlength > BUFFSIZE ? BUFFSIZE : restlength, infp)) > 0)) {
634 if (fwrite(buf, 1, readlength, outfp) != readlength) {
637 return -(saved_errno);
639 restlength -= readlength;
643 if (fclose(outfp) == EOF) {
645 FILE_OP_ERROR(outfile, "fclose");
647 return -(saved_errno);
653 struct ContentRenderer {
658 static GList * renderer_list = NULL;
660 static struct ContentRenderer *
661 content_renderer_new(char * content_type, char * renderer)
663 struct ContentRenderer * cr;
665 cr = g_new(struct ContentRenderer, 1);
669 cr->content_type = g_strdup(content_type);
670 cr->renderer = g_strdup(renderer);
675 static void content_renderer_free(struct ContentRenderer * cr)
677 g_free(cr->content_type);
678 g_free(cr->renderer);
682 void renderer_read_config(void)
688 g_list_foreach(renderer_list, (GFunc) content_renderer_free, NULL);
689 renderer_list = NULL;
691 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, RENDERER_RC, NULL);
692 f = g_fopen(rcpath, "rb");
698 while (fgets(buf, BUFFSIZE, f)) {
700 struct ContentRenderer * cr;
703 p = strchr(buf, ' ');
708 cr = content_renderer_new(buf, p + 1);
712 renderer_list = g_list_append(renderer_list, cr);
718 void renderer_write_config(void)
724 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, RENDERER_RC, NULL);
726 if ((pfile = prefs_write_open(rcpath)) == NULL) {
727 g_warning("failed to write configuration to file\n");
734 for (cur = renderer_list ; cur != NULL ; cur = cur->next) {
735 struct ContentRenderer * renderer;
736 renderer = cur->data;
737 if (fprintf(pfile->fp, "%s %s\n", renderer->content_type,
738 renderer->renderer) < 0) {
745 if (prefs_file_close(pfile) < 0) {
746 g_warning("failed to write configuration to file\n");
750 prefs_file_close_revert(pfile);
754 FILE *procmime_get_text_content(MimeInfo *mimeinfo)
757 const gchar *src_codeset;
758 gboolean conv_fail = FALSE;
761 struct ContentRenderer * renderer;
763 gchar *tmpfile, *content_type;
764 gboolean err = FALSE;
766 g_return_val_if_fail(mimeinfo != NULL, NULL);
768 if (!procmime_decode_content(mimeinfo))
771 tmpfile = procmime_get_tmp_file_name(mimeinfo);
775 if (procmime_get_part(tmpfile, mimeinfo) < 0) {
780 tmpfp = g_fopen(tmpfile, "rb");
786 if ((outfp = my_tmpfile()) == NULL) {
793 src_codeset = forced_charset
795 procmime_mimeinfo_get_parameter(mimeinfo, "charset");
799 content_type = procmime_get_content_type_str(mimeinfo->type,
801 for (cur = renderer_list ; cur != NULL ; cur = cur->next) {
802 struct ContentRenderer * cr;
805 if (g_ascii_strcasecmp(cr->content_type, content_type) == 0) {
810 g_free(content_type);
812 if (renderer != NULL) {
818 dup2(fileno(outfp), 1);
820 p = popen(renderer->renderer, "w");
825 fread(buf, sizeof(char), sizeof(buf),
827 if (fwrite(buf, sizeof(char), count, p) < count)
834 /* CodeConverter seems to have no effect here */
835 } else if (mimeinfo->type == MIMETYPE_TEXT && !g_ascii_strcasecmp(mimeinfo->subtype, "html")) {
836 SC_HTMLParser *parser;
839 conv = conv_code_converter_new(src_codeset);
840 parser = sc_html_parser_new(tmpfp, conv);
841 while ((str = sc_html_parse(parser)) != NULL) {
842 if (fputs(str, outfp) == EOF)
845 sc_html_parser_destroy(parser);
846 conv_code_converter_destroy(conv);
847 } else if (mimeinfo->type == MIMETYPE_TEXT && !g_ascii_strcasecmp(mimeinfo->subtype, "enriched")) {
851 conv = conv_code_converter_new(src_codeset);
852 parser = ertf_parser_new(tmpfp, conv);
853 while ((str = ertf_parse(parser)) != NULL) {
854 if (fputs(str, outfp) == EOF)
857 ertf_parser_destroy(parser);
858 conv_code_converter_destroy(conv);
859 } else if (mimeinfo->type == MIMETYPE_TEXT) {
860 while (fgets(buf, sizeof(buf), tmpfp) != NULL) {
861 str = conv_codeset_strdup(buf, src_codeset, CS_UTF_8);
863 if (fputs(str, outfp) == EOF)
868 if (fputs(buf, outfp) == EOF)
875 g_warning("procmime_get_text_content(): Code conversion failed.\n");
890 /* search the first text part of (multipart) MIME message,
891 decode, convert it and output to outfp. */
892 FILE *procmime_get_first_text_content(MsgInfo *msginfo)
895 MimeInfo *mimeinfo, *partinfo;
897 g_return_val_if_fail(msginfo != NULL, NULL);
899 mimeinfo = procmime_scan_message_short(msginfo);
900 if (!mimeinfo) return NULL;
903 while (partinfo && partinfo->type != MIMETYPE_TEXT) {
904 partinfo = procmime_mimeinfo_next(partinfo);
907 outfp = procmime_get_text_content(partinfo);
909 procmime_mimeinfo_free_all(mimeinfo);
915 static gboolean find_encrypted_func(GNode *node, gpointer data)
917 MimeInfo *mimeinfo = (MimeInfo *) node->data;
918 MimeInfo **encinfo = (MimeInfo **) data;
920 if (privacy_mimeinfo_is_encrypted(mimeinfo)) {
928 static MimeInfo *find_encrypted_part(MimeInfo *rootinfo)
930 MimeInfo *encinfo = NULL;
932 g_node_traverse(rootinfo->node, G_IN_ORDER, G_TRAVERSE_ALL, -1,
933 find_encrypted_func, &encinfo);
938 /* search the first encrypted text part of (multipart) MIME message,
939 decode, convert it and output to outfp. */
940 FILE *procmime_get_first_encrypted_text_content(MsgInfo *msginfo)
943 MimeInfo *mimeinfo, *partinfo, *encinfo;
945 g_return_val_if_fail(msginfo != NULL, NULL);
947 mimeinfo = procmime_scan_message(msginfo);
953 if ((encinfo = find_encrypted_part(partinfo)) != NULL) {
954 debug_print("decrypting message part\n");
955 if (privacy_mimeinfo_decrypt(encinfo) < 0) {
956 alertpanel_error(_("Couldn't decrypt: %s"),
957 privacy_get_error());
962 while (partinfo && partinfo->type != MIMETYPE_TEXT) {
963 partinfo = procmime_mimeinfo_next(partinfo);
967 outfp = procmime_get_text_content(partinfo);
969 procmime_mimeinfo_free_all(mimeinfo);
974 gboolean procmime_msginfo_is_encrypted(MsgInfo *msginfo)
976 MimeInfo *mimeinfo, *partinfo;
977 gboolean result = FALSE;
979 g_return_val_if_fail(msginfo != NULL, FALSE);
981 mimeinfo = procmime_scan_message(msginfo);
987 result = (find_encrypted_part(partinfo) != NULL);
988 procmime_mimeinfo_free_all(mimeinfo);
993 gboolean procmime_find_string_part(MimeInfo *mimeinfo, const gchar *filename,
994 const gchar *str, StrFindFunc find_func)
999 g_return_val_if_fail(mimeinfo != NULL, FALSE);
1000 g_return_val_if_fail(mimeinfo->type == MIMETYPE_TEXT, FALSE);
1001 g_return_val_if_fail(str != NULL, FALSE);
1002 g_return_val_if_fail(find_func != NULL, FALSE);
1004 outfp = procmime_get_text_content(mimeinfo);
1009 while (fgets(buf, sizeof(buf), outfp) != NULL) {
1011 if (find_func(buf, str)) {
1022 gboolean procmime_find_string(MsgInfo *msginfo, const gchar *str,
1023 StrFindFunc find_func)
1028 gboolean found = FALSE;
1030 g_return_val_if_fail(msginfo != NULL, FALSE);
1031 g_return_val_if_fail(str != NULL, FALSE);
1032 g_return_val_if_fail(find_func != NULL, FALSE);
1034 filename = procmsg_get_message_file(msginfo);
1035 if (!filename) return FALSE;
1036 mimeinfo = procmime_scan_message(msginfo);
1038 for (partinfo = mimeinfo; partinfo != NULL;
1039 partinfo = procmime_mimeinfo_next(partinfo)) {
1040 if (partinfo->type == MIMETYPE_TEXT) {
1041 if (procmime_find_string_part
1042 (partinfo, filename, str, find_func) == TRUE) {
1049 procmime_mimeinfo_free_all(mimeinfo);
1055 gchar *procmime_get_tmp_file_name(MimeInfo *mimeinfo)
1057 static guint32 id = 0;
1062 g_return_val_if_fail(mimeinfo != NULL, NULL);
1064 g_snprintf(f_prefix, sizeof(f_prefix), "%08x.", id++);
1066 if ((mimeinfo->type == MIMETYPE_TEXT) && !g_ascii_strcasecmp(mimeinfo->subtype, "html"))
1067 base = g_strdup("mimetmp.html");
1069 const gchar *basetmp;
1071 basetmp = procmime_mimeinfo_get_parameter(mimeinfo, "filename");
1072 if (basetmp == NULL)
1073 basetmp = procmime_mimeinfo_get_parameter(mimeinfo, "name");
1074 if (basetmp == NULL)
1075 basetmp = "mimetmp";
1076 basetmp = g_path_get_basename(basetmp);
1077 if (*basetmp == '\0')
1078 basetmp = g_strdup("mimetmp");
1079 base = conv_filename_from_utf8(basetmp);
1080 g_free((gchar*)basetmp);
1081 subst_for_shellsafe_filename(base);
1084 filename = g_strconcat(get_mime_tmp_dir(), G_DIR_SEPARATOR_S,
1085 f_prefix, base, NULL);
1092 static GList *mime_type_list = NULL;
1094 gchar *procmime_get_mime_type(const gchar *filename)
1096 static GHashTable *mime_type_table = NULL;
1097 MimeType *mime_type;
1102 if (!mime_type_table) {
1103 mime_type_table = procmime_get_mime_type_table();
1104 if (!mime_type_table) return NULL;
1107 base = g_path_get_basename(filename);
1108 if ((p = strrchr(base, '.')) != NULL)
1109 Xstrdup_a(ext, p + 1, p = NULL );
1111 if (!p) return NULL;
1114 mime_type = g_hash_table_lookup(mime_type_table, ext);
1118 str = g_strconcat(mime_type->type, "/", mime_type->sub_type,
1126 static guint procmime_str_hash(gconstpointer gptr)
1128 guint hash_result = 0;
1131 for (str = gptr; str && *str; str++) {
1132 if (isupper(*str)) hash_result += (*str + ' ');
1133 else hash_result += *str;
1139 static gint procmime_str_equal(gconstpointer gptr1, gconstpointer gptr2)
1141 const char *str1 = gptr1;
1142 const char *str2 = gptr2;
1144 return !g_utf8_collate(str1, str2);
1147 static GHashTable *procmime_get_mime_type_table(void)
1149 GHashTable *table = NULL;
1151 MimeType *mime_type;
1154 if (!mime_type_list) {
1155 mime_type_list = procmime_get_mime_type_list();
1156 if (!mime_type_list) return NULL;
1159 table = g_hash_table_new(procmime_str_hash, procmime_str_equal);
1161 for (cur = mime_type_list; cur != NULL; cur = cur->next) {
1165 mime_type = (MimeType *)cur->data;
1167 if (!mime_type->extension) continue;
1169 exts = g_strsplit(mime_type->extension, " ", 16);
1170 for (i = 0; exts[i] != NULL; i++) {
1171 /* make the key case insensitive */
1173 /* use previously dup'd key on overwriting */
1174 if (g_hash_table_lookup(table, exts[i]))
1177 key = g_strdup(exts[i]);
1178 g_hash_table_insert(table, key, mime_type);
1186 GList *procmime_get_mime_type_list(void)
1190 gchar buf[BUFFSIZE];
1193 MimeType *mime_type;
1194 gboolean fp_is_glob_file = TRUE;
1197 return mime_type_list;
1199 #if defined(__NetBSD__)
1200 if ((fp = g_fopen(DATAROOTDIR "/mime/globs", "rb")) == NULL)
1202 if ((fp = g_fopen("/usr/share/mime/globs", "rb")) == NULL)
1205 fp_is_glob_file = FALSE;
1206 if ((fp = g_fopen("/etc/mime.types", "rb")) == NULL) {
1207 if ((fp = g_fopen(SYSCONFDIR "/mime.types", "rb"))
1209 FILE_OP_ERROR(SYSCONFDIR "/mime.types",
1216 while (fgets(buf, sizeof(buf), fp) != NULL) {
1217 p = strchr(buf, '#');
1223 if (fp_is_glob_file) {
1224 while (*p && !g_ascii_isspace(*p) && (*p!=':')) p++;
1226 while (*p && !g_ascii_isspace(*p)) p++;
1233 delim = strchr(buf, '/');
1234 if (delim == NULL) continue;
1237 mime_type = g_new(MimeType, 1);
1238 mime_type->type = g_strdup(buf);
1239 mime_type->sub_type = g_strdup(delim + 1);
1241 if (fp_is_glob_file) {
1242 while (*p && (g_ascii_isspace(*p)||(*p=='*')||(*p=='.'))) p++;
1244 while (*p && g_ascii_isspace(*p)) p++;
1248 mime_type->extension = g_strdup(p);
1250 mime_type->extension = NULL;
1252 list = g_list_append(list, mime_type);
1258 g_warning("Can't read mime.types\n");
1263 EncodingType procmime_get_encoding_for_charset(const gchar *charset)
1267 else if (!g_ascii_strncasecmp(charset, "ISO-2022-", 9) ||
1268 !g_ascii_strcasecmp(charset, "US-ASCII"))
1270 else if (!g_ascii_strcasecmp(charset, "ISO-8859-5") ||
1271 !g_ascii_strncasecmp(charset, "KOI8-", 5) ||
1272 !g_ascii_strcasecmp(charset, "Windows-1251"))
1274 else if (!g_ascii_strncasecmp(charset, "ISO-8859-", 9))
1275 return ENC_QUOTED_PRINTABLE;
1276 else if (!g_ascii_strncasecmp(charset, "UTF-8", 5))
1277 return ENC_QUOTED_PRINTABLE;
1282 EncodingType procmime_get_encoding_for_text_file(const gchar *file, gboolean *has_binary)
1285 guchar buf[BUFFSIZE];
1287 size_t octet_chars = 0;
1288 size_t total_len = 0;
1289 gfloat octet_percentage;
1290 gboolean force_b64 = FALSE;
1292 if ((fp = g_fopen(file, "rb")) == NULL) {
1293 FILE_OP_ERROR(file, "fopen");
1297 while ((len = fread(buf, sizeof(guchar), sizeof(buf), fp)) > 0) {
1301 for (p = buf, i = 0; i < len; ++p, ++i) {
1315 octet_percentage = (gfloat)octet_chars / (gfloat)total_len;
1317 octet_percentage = 0.0;
1319 debug_print("procmime_get_encoding_for_text_file(): "
1320 "8bit chars: %zd / %zd (%f%%)\n", octet_chars, total_len,
1321 100.0 * octet_percentage);
1323 if (octet_percentage > 0.20 || force_b64) {
1324 debug_print("using BASE64\n");
1326 } else if (octet_chars > 0) {
1327 debug_print("using quoted-printable\n");
1328 return ENC_QUOTED_PRINTABLE;
1330 debug_print("using 7bit\n");
1335 struct EncodingTable
1338 EncodingType enc_type;
1341 struct EncodingTable encoding_table[] = {
1344 {"binary", ENC_BINARY},
1345 {"quoted-printable", ENC_QUOTED_PRINTABLE},
1346 {"base64", ENC_BASE64},
1347 {"x-uuencode", ENC_UNKNOWN},
1348 {NULL, ENC_UNKNOWN},
1351 const gchar *procmime_get_encoding_str(EncodingType encoding)
1353 struct EncodingTable *enc_table;
1355 for (enc_table = encoding_table; enc_table->str != NULL; enc_table++) {
1356 if (enc_table->enc_type == encoding)
1357 return enc_table->str;
1362 /* --- NEW MIME STUFF --- */
1369 static struct TypeTable mime_type_table[] = {
1370 {"text", MIMETYPE_TEXT},
1371 {"image", MIMETYPE_IMAGE},
1372 {"audio", MIMETYPE_AUDIO},
1373 {"video", MIMETYPE_VIDEO},
1374 {"application", MIMETYPE_APPLICATION},
1375 {"message", MIMETYPE_MESSAGE},
1376 {"multipart", MIMETYPE_MULTIPART},
1380 const gchar *procmime_get_media_type_str(MimeMediaType type)
1382 struct TypeTable *type_table;
1384 for (type_table = mime_type_table; type_table->str != NULL; type_table++) {
1385 if (type_table->type == type)
1386 return type_table->str;
1391 MimeMediaType procmime_get_media_type(const gchar *str)
1393 struct TypeTable *typetablearray;
1395 for (typetablearray = mime_type_table; typetablearray->str != NULL; typetablearray++)
1396 if (g_ascii_strncasecmp(str, typetablearray->str, strlen(typetablearray->str)) == 0)
1397 return typetablearray->type;
1399 return MIMETYPE_UNKNOWN;
1403 *\brief Safe wrapper for content type string.
1405 *\return const gchar * Pointer to content type string.
1407 gchar *procmime_get_content_type_str(MimeMediaType type,
1408 const char *subtype)
1410 const gchar *type_str = NULL;
1412 if (subtype == NULL || !(type_str = procmime_get_media_type_str(type)))
1413 return g_strdup("unknown");
1414 return g_strdup_printf("%s/%s", type_str, subtype);
1417 static int procmime_parse_mimepart(MimeInfo *parent,
1418 gchar *content_type,
1419 gchar *content_encoding,
1420 gchar *content_description,
1422 gchar *content_disposition,
1423 gchar *content_location,
1424 const gchar *filename,
1427 gboolean short_scan);
1429 static void procmime_parse_message_rfc822(MimeInfo *mimeinfo, gboolean short_scan)
1431 HeaderEntry hentry[] = {{"Content-Type:", NULL, TRUE},
1432 {"Content-Transfer-Encoding:",
1434 {"Content-Description:",
1438 {"Content-Disposition:",
1440 {"Content-Location:",
1444 {NULL, NULL, FALSE}};
1445 guint content_start, i;
1450 procmime_decode_content(mimeinfo);
1452 fp = g_fopen(mimeinfo->data.filename, "rb");
1454 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
1457 fseek(fp, mimeinfo->offset, SEEK_SET);
1458 procheader_get_header_fields(fp, hentry);
1459 if (hentry[0].body != NULL) {
1460 tmp = conv_unmime_header(hentry[0].body, NULL);
1461 g_free(hentry[0].body);
1462 hentry[0].body = tmp;
1464 if (hentry[2].body != NULL) {
1465 tmp = conv_unmime_header(hentry[2].body, NULL);
1466 g_free(hentry[2].body);
1467 hentry[2].body = tmp;
1469 if (hentry[4].body != NULL) {
1470 tmp = conv_unmime_header(hentry[4].body, NULL);
1471 g_free(hentry[4].body);
1472 hentry[4].body = tmp;
1474 if (hentry[5].body != NULL) {
1475 tmp = conv_unmime_header(hentry[5].body, NULL);
1476 g_free(hentry[5].body);
1477 hentry[5].body = tmp;
1479 content_start = ftell(fp);
1482 len = mimeinfo->length - (content_start - mimeinfo->offset);
1485 procmime_parse_mimepart(mimeinfo,
1486 hentry[0].body, hentry[1].body,
1487 hentry[2].body, hentry[3].body,
1488 hentry[4].body, hentry[5].body,
1489 mimeinfo->data.filename, content_start,
1492 for (i = 0; i < (sizeof hentry / sizeof hentry[0]); i++) {
1493 g_free(hentry[i].body);
1494 hentry[i].body = NULL;
1498 static void procmime_parse_multipart(MimeInfo *mimeinfo, gboolean short_scan)
1500 HeaderEntry hentry[] = {{"Content-Type:", NULL, TRUE},
1501 {"Content-Transfer-Encoding:",
1503 {"Content-Description:",
1507 {"Content-Disposition:",
1509 {"Content-Location:",
1511 {NULL, NULL, FALSE}};
1514 gint boundary_len = 0, lastoffset = -1, i;
1515 gchar buf[BUFFSIZE];
1518 gboolean done = FALSE;
1520 boundary = g_hash_table_lookup(mimeinfo->typeparameters, "boundary");
1523 boundary_len = strlen(boundary);
1525 procmime_decode_content(mimeinfo);
1527 fp = g_fopen(mimeinfo->data.filename, "rb");
1529 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
1532 fseek(fp, mimeinfo->offset, SEEK_SET);
1533 while ((p = fgets(buf, sizeof(buf), fp)) != NULL && result == 0) {
1534 if (ftell(fp) - 1 > (mimeinfo->offset + mimeinfo->length))
1537 if (IS_BOUNDARY(buf, boundary, boundary_len)) {
1538 if (lastoffset != -1) {
1539 gint len = (ftell(fp) - strlen(buf)) - lastoffset - 1;
1542 result = procmime_parse_mimepart(mimeinfo,
1543 hentry[0].body, hentry[1].body,
1544 hentry[2].body, hentry[3].body,
1545 hentry[4].body, hentry[5].body,
1546 mimeinfo->data.filename, lastoffset,
1548 if (result == 1 && short_scan) {
1554 if (buf[2 + boundary_len] == '-' &&
1555 buf[2 + boundary_len + 1] == '-')
1558 for (i = 0; i < (sizeof hentry / sizeof hentry[0]) ; i++) {
1559 g_free(hentry[i].body);
1560 hentry[i].body = NULL;
1562 procheader_get_header_fields(fp, hentry);
1563 if (hentry[0].body != NULL) {
1564 tmp = conv_unmime_header(hentry[0].body, NULL);
1565 g_free(hentry[0].body);
1566 hentry[0].body = tmp;
1568 if (hentry[2].body != NULL) {
1569 tmp = conv_unmime_header(hentry[2].body, NULL);
1570 g_free(hentry[2].body);
1571 hentry[2].body = tmp;
1573 if (hentry[4].body != NULL) {
1574 tmp = conv_unmime_header(hentry[4].body, NULL);
1575 g_free(hentry[4].body);
1576 hentry[4].body = tmp;
1578 if (hentry[5].body != NULL) {
1579 tmp = conv_unmime_header(hentry[5].body, NULL);
1580 g_free(hentry[5].body);
1581 hentry[5].body = tmp;
1583 lastoffset = ftell(fp);
1586 for (i = 0; i < (sizeof hentry / sizeof hentry[0]); i++) {
1587 g_free(hentry[i].body);
1588 hentry[i].body = NULL;
1593 static void parse_parameters(const gchar *parameters, GHashTable *table)
1595 gchar *params, *param, *next;
1596 GSList *convlist = NULL, *concatlist = NULL, *cur;
1598 params = g_strdup(parameters);
1601 for (; next != NULL; param = next) {
1602 gchar *attribute, *value, *tmp;
1604 gboolean convert = FALSE;
1606 next = strchr_with_skip_quote(param, '"', ';');
1615 value = strchr(attribute, '=');
1621 while (value[0] == ' ')
1624 g_strdown(attribute);
1626 len = strlen(attribute);
1627 if (attribute[len - 1] == '*') {
1628 gchar *srcpos, *dstpos, *endpos;
1631 attribute[len - 1] = '\0';
1635 endpos = value + strlen(value);
1636 while (srcpos < endpos) {
1642 if (!get_hex_value(&dstvalue, srcpos[1], srcpos[2]))
1653 if (value[0] == '"')
1654 extract_quote(value, '"');
1655 else if ((tmp = strchr(value, ' ')) != NULL)
1660 while (attribute[0] == ' ')
1662 while (attribute[strlen(attribute)-1] == ' ')
1663 attribute[strlen(attribute)-1] = '\0';
1666 while (value[0] == ' ')
1668 while (value[strlen(value)-1] == ' ')
1669 value[strlen(value)-1] = '\0';
1671 if (strrchr(attribute, '*') != NULL) {
1674 tmpattr = g_strdup(attribute);
1675 tmp = strrchr(tmpattr, '*');
1678 if ((tmp[1] == '0') && (tmp[2] == '\0') &&
1679 (g_slist_find_custom(concatlist, attribute, g_str_equal) == NULL))
1680 concatlist = g_slist_prepend(concatlist, g_strdup(tmpattr));
1682 if (convert && (g_slist_find_custom(convlist, attribute, g_str_equal) == NULL))
1683 convlist = g_slist_prepend(convlist, g_strdup(tmpattr));
1686 } else if (convert) {
1687 if (g_slist_find_custom(convlist, attribute, g_str_equal) == NULL)
1688 convlist = g_slist_prepend(convlist, g_strdup(attribute));
1691 if (g_hash_table_lookup(table, attribute) == NULL)
1692 g_hash_table_insert(table, g_strdup(attribute), g_strdup(value));
1695 for (cur = concatlist; cur != NULL; cur = g_slist_next(cur)) {
1696 gchar *attribute, *attrwnum, *partvalue;
1700 attribute = (gchar *) cur->data;
1701 value = g_string_sized_new(64);
1703 attrwnum = g_strdup_printf("%s*%d", attribute, n);
1704 while ((partvalue = g_hash_table_lookup(table, attrwnum)) != NULL) {
1705 g_string_append(value, partvalue);
1709 attrwnum = g_strdup_printf("%s*%d", attribute, n);
1713 g_hash_table_insert(table, g_strdup(attribute), g_strdup(value->str));
1714 g_string_free(value, TRUE);
1716 slist_free_strings(concatlist);
1717 g_slist_free(concatlist);
1719 for (cur = convlist; cur != NULL; cur = g_slist_next(cur)) {
1720 gchar *attribute, *key, *value;
1721 gchar *charset, *lang, *oldvalue, *newvalue;
1723 attribute = (gchar *) cur->data;
1724 if (!g_hash_table_lookup_extended(
1725 table, attribute, (gpointer *)(gchar *) &key, (gpointer *)(gchar *) &value))
1729 lang = strchr(charset, '\'');
1734 oldvalue = strchr(lang, '\'');
1735 if (oldvalue == NULL)
1740 newvalue = conv_codeset_strdup(oldvalue, charset, CS_UTF_8);
1742 g_hash_table_remove(table, attribute);
1746 g_hash_table_insert(table, g_strdup(attribute), newvalue);
1748 slist_free_strings(convlist);
1749 g_slist_free(convlist);
1754 static void procmime_parse_content_type(const gchar *content_type, MimeInfo *mimeinfo)
1756 g_return_if_fail(content_type != NULL);
1757 g_return_if_fail(mimeinfo != NULL);
1759 /* RFC 2045, page 13 says that the mime subtype is MANDATORY;
1760 * if it's not available we use the default Content-Type */
1761 if ((content_type[0] == '\0') || (strchr(content_type, '/') == NULL)) {
1762 mimeinfo->type = MIMETYPE_TEXT;
1763 mimeinfo->subtype = g_strdup("plain");
1764 if (g_hash_table_lookup(mimeinfo->typeparameters,
1765 "charset") == NULL) {
1766 g_hash_table_insert(mimeinfo->typeparameters,
1767 g_strdup("charset"),
1769 conv_get_locale_charset_str_no_utf8()));
1772 gchar *type, *subtype, *params;
1774 type = g_strdup(content_type);
1775 subtype = strchr(type, '/') + 1;
1776 *(subtype - 1) = '\0';
1777 if ((params = strchr(subtype, ';')) != NULL) {
1782 mimeinfo->type = procmime_get_media_type(type);
1783 mimeinfo->subtype = g_strdup(subtype);
1785 /* Get mimeinfo->typeparameters */
1787 parse_parameters(params, mimeinfo->typeparameters);
1793 static void procmime_parse_content_disposition(const gchar *content_disposition, MimeInfo *mimeinfo)
1795 gchar *tmp, *params;
1797 g_return_if_fail(content_disposition != NULL);
1798 g_return_if_fail(mimeinfo != NULL);
1800 tmp = g_strdup(content_disposition);
1801 if ((params = strchr(tmp, ';')) != NULL) {
1807 if (!g_ascii_strcasecmp(tmp, "inline"))
1808 mimeinfo->disposition = DISPOSITIONTYPE_INLINE;
1809 else if (!g_ascii_strcasecmp(tmp, "attachment"))
1810 mimeinfo->disposition = DISPOSITIONTYPE_ATTACHMENT;
1812 mimeinfo->disposition = DISPOSITIONTYPE_ATTACHMENT;
1815 parse_parameters(params, mimeinfo->dispositionparameters);
1821 static void procmime_parse_content_encoding(const gchar *content_encoding, MimeInfo *mimeinfo)
1823 struct EncodingTable *enc_table;
1825 for (enc_table = encoding_table; enc_table->str != NULL; enc_table++) {
1826 if (g_ascii_strcasecmp(enc_table->str, content_encoding) == 0) {
1827 mimeinfo->encoding_type = enc_table->enc_type;
1831 mimeinfo->encoding_type = ENC_UNKNOWN;
1835 static GSList *registered_parsers = NULL;
1837 static MimeParser *procmime_get_mimeparser_for_type(MimeMediaType type, const gchar *sub_type)
1840 for (cur = registered_parsers; cur; cur = cur->next) {
1841 MimeParser *parser = (MimeParser *)cur->data;
1842 if (parser->type == type && !strcmp2(parser->sub_type, sub_type))
1848 void procmime_mimeparser_register(MimeParser *parser)
1850 if (!procmime_get_mimeparser_for_type(parser->type, parser->sub_type))
1851 registered_parsers = g_slist_append(registered_parsers, parser);
1855 void procmime_mimeparser_unregister(MimeParser *parser)
1857 registered_parsers = g_slist_remove(registered_parsers, parser);
1860 static gboolean procmime_mimeparser_parse(MimeParser *parser, MimeInfo *mimeinfo)
1862 g_return_val_if_fail(parser->parse != NULL, FALSE);
1863 return parser->parse(parser, mimeinfo);
1866 static int procmime_parse_mimepart(MimeInfo *parent,
1867 gchar *content_type,
1868 gchar *content_encoding,
1869 gchar *content_description,
1871 gchar *content_disposition,
1872 gchar *content_location,
1873 const gchar *filename,
1876 gboolean short_scan)
1879 MimeParser *parser = NULL;
1880 gboolean parsed = FALSE;
1883 /* Create MimeInfo */
1884 mimeinfo = procmime_mimeinfo_new();
1885 mimeinfo->content = MIMECONTENT_FILE;
1886 if (parent != NULL) {
1887 if (g_node_depth(parent->node) > 32) {
1888 /* 32 is an arbitrary value
1889 * this avoids DOSsing ourselves
1890 * with enormous messages
1892 procmime_mimeinfo_free_all(mimeinfo);
1895 g_node_append(parent->node, mimeinfo->node);
1897 mimeinfo->data.filename = g_strdup(filename);
1898 mimeinfo->offset = offset;
1899 mimeinfo->length = length;
1901 if (content_type != NULL) {
1902 procmime_parse_content_type(content_type, mimeinfo);
1904 mimeinfo->type = MIMETYPE_TEXT;
1905 mimeinfo->subtype = g_strdup("plain");
1906 if (g_hash_table_lookup(mimeinfo->typeparameters,
1907 "charset") == NULL) {
1908 g_hash_table_insert(mimeinfo->typeparameters,
1909 g_strdup("charset"),
1911 conv_get_locale_charset_str_no_utf8()));
1915 if (content_encoding != NULL) {
1916 procmime_parse_content_encoding(content_encoding, mimeinfo);
1918 mimeinfo->encoding_type = ENC_UNKNOWN;
1921 if (content_description != NULL)
1922 mimeinfo->description = g_strdup(content_description);
1924 mimeinfo->description = NULL;
1926 if (content_id != NULL)
1927 mimeinfo->id = g_strdup(content_id);
1929 mimeinfo->id = NULL;
1931 if (content_location != NULL)
1932 mimeinfo->location = g_strdup(content_location);
1934 mimeinfo->location = NULL;
1936 if (content_disposition != NULL)
1937 procmime_parse_content_disposition(content_disposition, mimeinfo);
1939 mimeinfo->disposition = DISPOSITIONTYPE_UNKNOWN;
1941 /* Call parser for mime type */
1942 if ((parser = procmime_get_mimeparser_for_type(mimeinfo->type, mimeinfo->subtype)) != NULL) {
1943 parsed = procmime_mimeparser_parse(parser, mimeinfo);
1946 switch (mimeinfo->type) {
1948 if (g_ascii_strcasecmp(mimeinfo->subtype, "plain") == 0 && short_scan) {
1953 case MIMETYPE_MESSAGE:
1954 if (g_ascii_strcasecmp(mimeinfo->subtype, "rfc822") == 0) {
1955 procmime_parse_message_rfc822(mimeinfo, short_scan);
1959 case MIMETYPE_MULTIPART:
1960 procmime_parse_multipart(mimeinfo, short_scan);
1971 static gchar *typenames[] = {
1982 static gboolean output_func(GNode *node, gpointer data)
1985 MimeInfo *mimeinfo = (MimeInfo *) node->data;
1987 depth = g_node_depth(node);
1988 for (i = 0; i < depth; i++)
1990 g_print("%s/%s (offset:%d length:%d encoding: %d)\n", typenames[mimeinfo->type], mimeinfo->subtype, mimeinfo->offset, mimeinfo->length, mimeinfo->encoding_type);
1995 static void output_mime_structure(MimeInfo *mimeinfo, int indent)
1997 g_node_traverse(mimeinfo->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1, output_func, NULL);
2000 static MimeInfo *procmime_scan_file_with_offset(const gchar *filename, int offset, gboolean short_scan)
2005 stat(filename, &buf);
2007 mimeinfo = procmime_mimeinfo_new();
2008 mimeinfo->content = MIMECONTENT_FILE;
2009 mimeinfo->encoding_type = ENC_UNKNOWN;
2010 mimeinfo->type = MIMETYPE_MESSAGE;
2011 mimeinfo->subtype = g_strdup("rfc822");
2012 mimeinfo->data.filename = g_strdup(filename);
2013 mimeinfo->offset = offset;
2014 mimeinfo->length = buf.st_size - offset;
2016 procmime_parse_message_rfc822(mimeinfo, short_scan);
2017 if (debug_get_mode())
2018 output_mime_structure(mimeinfo, 0);
2023 static MimeInfo *procmime_scan_file_full(const gchar *filename, gboolean short_scan)
2027 g_return_val_if_fail(filename != NULL, NULL);
2029 mimeinfo = procmime_scan_file_with_offset(filename, 0, short_scan);
2034 MimeInfo *procmime_scan_file(const gchar *filename)
2036 return procmime_scan_file_full(filename, FALSE);
2039 static MimeInfo *procmime_scan_file_short(const gchar *filename)
2041 return procmime_scan_file_full(filename, TRUE);
2044 static MimeInfo *procmime_scan_queue_file_full(const gchar *filename, gboolean short_scan)
2048 gchar buf[BUFFSIZE];
2051 g_return_val_if_fail(filename != NULL, NULL);
2054 if ((fp = g_fopen(filename, "rb")) == NULL)
2056 /* Skip queue header */
2057 while (fgets(buf, sizeof(buf), fp) != NULL) {
2059 if ((!strncmp(buf, "X-Claws-End-Special-Headers: 1",
2060 strlen("X-Claws-End-Special-Headers:"))) ||
2061 (!strncmp(buf, "X-Sylpheed-End-Special-Headers: 1",
2062 strlen("X-Sylpheed-End-Special-Headers:"))))
2065 if (buf[0] == '\r' || buf[0] == '\n') break;
2066 /* from other mailers */
2067 if (!strncmp(buf, "Date: ", 6)
2068 || !strncmp(buf, "To: ", 4)
2069 || !strncmp(buf, "From: ", 6)
2070 || !strncmp(buf, "Subject: ", 9)) {
2078 mimeinfo = procmime_scan_file_with_offset(filename, offset, short_scan);
2083 MimeInfo *procmime_scan_queue_file(const gchar *filename)
2085 return procmime_scan_queue_file_full(filename, FALSE);
2088 static MimeInfo *procmime_scan_queue_file_short(const gchar *filename)
2090 return procmime_scan_queue_file_full(filename, TRUE);
2095 ENC_AS_QUOTED_STRING,
2100 typedef struct _ParametersData {
2107 static void write_parameters(gpointer key, gpointer value, gpointer user_data)
2110 gchar *val = value, *valpos, *tmp;
2111 ParametersData *pdata = (ParametersData *)user_data;
2112 GString *buf = g_string_new("");
2114 EncodeAs encas = ENC_AS_TOKEN;
2116 for (valpos = val; *valpos != 0; valpos++) {
2117 if (!IS_ASCII(*valpos) || *valpos == '"') {
2118 encas = ENC_AS_EXTENDED;
2123 if (((*valpos >= 0) && (*valpos < 037)) || (*valpos == 0177)) {
2124 encas = ENC_AS_QUOTED_STRING;
2128 /* tspecials + SPACE */
2146 encas = ENC_AS_QUOTED_STRING;
2151 if (encas == ENC_AS_EXTENDED && pdata->ascii_only == TRUE)
2152 encas = ENC_TO_ASCII;
2156 g_string_append_printf(buf, "%s=%s", param, val);
2160 tmp = g_strdup(val);
2162 " ()<>@,';:\\/[]?=.0123456789"
2163 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
2164 "abcdefghijklmnopqrstuvwxyz",
2166 g_string_append_printf(buf, "%s=\"%s\"", param, tmp);
2170 case ENC_AS_QUOTED_STRING:
2171 g_string_append_printf(buf, "%s=\"%s\"", param, val);
2174 case ENC_AS_EXTENDED:
2175 if (!g_utf8_validate(val, -1, NULL))
2176 g_string_append_printf(buf, "%s*=%s''", param,
2177 conv_get_locale_charset_str());
2179 g_string_append_printf(buf, "%s*=%s''", param,
2181 for (valpos = val; *valpos != '\0'; valpos++) {
2182 if (IS_ASCII(*valpos) && isalnum(*valpos)) {
2183 g_string_append_printf(buf, "%c", *valpos);
2185 gchar hexstr[3] = "XX";
2186 get_hex_str(hexstr, *valpos);
2187 g_string_append_printf(buf, "%%%s", hexstr);
2193 if (buf->str && strlen(buf->str)) {
2194 if (pdata->len + strlen(buf->str) + 2 > 76) {
2195 if (fprintf(pdata->fp, ";\n %s", buf->str) < 0)
2196 pdata->error = TRUE;
2197 pdata->len = strlen(buf->str) + 1;
2199 if (fprintf(pdata->fp, "; %s", buf->str) < 0)
2200 pdata->error = TRUE;
2201 pdata->len += strlen(buf->str) + 2;
2204 g_string_free(buf, TRUE);
2207 #define TRY(func) { \
2213 int procmime_write_mime_header(MimeInfo *mimeinfo, FILE *fp)
2215 struct TypeTable *type_table;
2216 ParametersData *pdata = g_new0(ParametersData, 1);
2217 debug_print("procmime_write_mime_header\n");
2220 pdata->ascii_only = FALSE;
2221 pdata->error = FALSE;
2222 for (type_table = mime_type_table; type_table->str != NULL; type_table++)
2223 if (mimeinfo->type == type_table->type) {
2224 gchar *buf = g_strdup_printf(
2225 "Content-Type: %s/%s", type_table->str, mimeinfo->subtype);
2226 if (fprintf(fp, "%s", buf) < 0) {
2231 pdata->len = strlen(buf);
2232 pdata->ascii_only = TRUE;
2236 g_hash_table_foreach(mimeinfo->typeparameters, write_parameters, pdata);
2237 if (pdata->error == TRUE) {
2243 TRY(fprintf(fp, "\n") >= 0);
2245 if (mimeinfo->encoding_type != ENC_UNKNOWN)
2246 TRY(fprintf(fp, "Content-Transfer-Encoding: %s\n", procmime_get_encoding_str(mimeinfo->encoding_type)) >= 0);
2248 if (mimeinfo->description != NULL)
2249 TRY(fprintf(fp, "Content-Description: %s\n", mimeinfo->description) >= 0);
2251 if (mimeinfo->id != NULL)
2252 TRY(fprintf(fp, "Content-ID: %s\n", mimeinfo->id) >= 0);
2254 if (mimeinfo->location != NULL)
2255 TRY(fprintf(fp, "Content-Location: %s\n", mimeinfo->location) >= 0);
2257 if (mimeinfo->disposition != DISPOSITIONTYPE_UNKNOWN) {
2258 ParametersData *pdata = g_new0(ParametersData, 1);
2260 if (mimeinfo->disposition == DISPOSITIONTYPE_INLINE)
2261 buf = g_strdup("Content-Disposition: inline");
2262 else if (mimeinfo->disposition == DISPOSITIONTYPE_ATTACHMENT)
2263 buf = g_strdup("Content-Disposition: attachment");
2265 buf = g_strdup("Content-Disposition: unknown");
2267 if (fprintf(fp, "%s", buf) < 0) {
2272 pdata->len = strlen(buf);
2276 pdata->ascii_only = FALSE;
2277 pdata->error = FALSE;
2278 g_hash_table_foreach(mimeinfo->dispositionparameters, write_parameters, pdata);
2279 if (pdata->error == TRUE) {
2284 TRY(fprintf(fp, "\n") >= 0);
2287 TRY(fprintf(fp, "\n") >= 0);
2292 static gint procmime_write_message_rfc822(MimeInfo *mimeinfo, FILE *fp)
2297 gchar buf[BUFFSIZE];
2298 gboolean skip = FALSE;;
2301 debug_print("procmime_write_message_rfc822\n");
2304 switch (mimeinfo->content) {
2305 case MIMECONTENT_FILE:
2306 if ((infp = g_fopen(mimeinfo->data.filename, "rb")) == NULL) {
2307 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
2310 fseek(infp, mimeinfo->offset, SEEK_SET);
2311 while (fgets(buf, sizeof(buf), infp) == buf) {
2313 if (buf[0] == '\n' && buf[1] == '\0')
2315 if (skip && (buf[0] == ' ' || buf[0] == '\t'))
2317 if (g_ascii_strncasecmp(buf, "Mime-Version:", 13) == 0 ||
2318 g_ascii_strncasecmp(buf, "Content-Type:", 13) == 0 ||
2319 g_ascii_strncasecmp(buf, "Content-Transfer-Encoding:", 26) == 0 ||
2320 g_ascii_strncasecmp(buf, "Content-Description:", 20) == 0 ||
2321 g_ascii_strncasecmp(buf, "Content-ID:", 11) == 0 ||
2322 g_ascii_strncasecmp(buf, "Content-Location:", 17) == 0 ||
2323 g_ascii_strncasecmp(buf, "Content-Disposition:", 20) == 0) {
2328 if (fwrite(buf, sizeof(gchar), len, fp) < len) {
2329 g_warning("failed to dump %d bytes from file", len);
2338 case MIMECONTENT_MEM:
2339 len = strlen(mimeinfo->data.mem);
2340 if (fwrite(mimeinfo->data.mem, sizeof(gchar), len, fp) < len) {
2341 g_warning("failed to dump %d bytes from mem", len);
2350 childnode = mimeinfo->node->children;
2351 if (childnode == NULL)
2354 child = (MimeInfo *) childnode->data;
2355 if (fprintf(fp, "Mime-Version: 1.0\n") < 0) {
2356 g_warning("failed to write mime version");
2359 if (procmime_write_mime_header(child, fp) < 0)
2361 return procmime_write_mimeinfo(child, fp);
2364 static gint procmime_write_multipart(MimeInfo *mimeinfo, FILE *fp)
2368 gchar *boundary, *str, *str2;
2369 gchar buf[BUFFSIZE];
2370 gboolean firstboundary;
2373 debug_print("procmime_write_multipart\n");
2375 boundary = g_hash_table_lookup(mimeinfo->typeparameters, "boundary");
2377 switch (mimeinfo->content) {
2378 case MIMECONTENT_FILE:
2379 if ((infp = g_fopen(mimeinfo->data.filename, "rb")) == NULL) {
2380 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
2383 fseek(infp, mimeinfo->offset, SEEK_SET);
2384 while (fgets(buf, sizeof(buf), infp) == buf) {
2385 if (IS_BOUNDARY(buf, boundary, strlen(boundary)))
2388 if (fwrite(buf, sizeof(gchar), len, fp) < len) {
2389 g_warning("failed to write %d", len);
2397 case MIMECONTENT_MEM:
2398 str = g_strdup(mimeinfo->data.mem);
2399 if (((str2 = strstr(str, boundary)) != NULL) && ((str2 - str) >= 2) &&
2400 (*(str2 - 1) == '-') && (*(str2 - 2) == '-'))
2403 if (fwrite(str, sizeof(gchar), len, fp) < len) {
2404 g_warning("failed to write %d from mem", len);
2415 childnode = mimeinfo->node->children;
2416 firstboundary = TRUE;
2417 while (childnode != NULL) {
2418 MimeInfo *child = childnode->data;
2421 firstboundary = FALSE;
2423 TRY(fprintf(fp, "\n") >= 0);
2425 TRY(fprintf(fp, "--%s\n", boundary) >= 0);
2427 if (procmime_write_mime_header(child, fp) < 0)
2429 if (procmime_write_mimeinfo(child, fp) < 0)
2432 childnode = g_node_next_sibling(childnode);
2434 TRY(fprintf(fp, "\n--%s--\n", boundary) >= 0);
2439 gint procmime_write_mimeinfo(MimeInfo *mimeinfo, FILE *fp)
2443 debug_print("procmime_write_mimeinfo\n");
2445 if (G_NODE_IS_LEAF(mimeinfo->node)) {
2446 switch (mimeinfo->content) {
2447 case MIMECONTENT_FILE:
2448 if ((infp = g_fopen(mimeinfo->data.filename, "rb")) == NULL) {
2449 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
2452 copy_file_part_to_fp(infp, mimeinfo->offset, mimeinfo->length, fp);
2456 case MIMECONTENT_MEM:
2457 len = strlen(mimeinfo->data.mem);
2458 if (fwrite(mimeinfo->data.mem, sizeof(gchar), len, fp) < len)
2466 /* Call writer for mime type */
2467 switch (mimeinfo->type) {
2468 case MIMETYPE_MESSAGE:
2469 if (g_ascii_strcasecmp(mimeinfo->subtype, "rfc822") == 0) {
2470 return procmime_write_message_rfc822(mimeinfo, fp);
2474 case MIMETYPE_MULTIPART:
2475 return procmime_write_multipart(mimeinfo, fp);
2487 gchar *procmime_get_part_file_name(MimeInfo *mimeinfo)
2491 if ((mimeinfo->type == MIMETYPE_TEXT) && !g_ascii_strcasecmp(mimeinfo->subtype, "html"))
2492 base = g_strdup("mimetmp.html");
2494 const gchar *basetmp;
2497 basetmp = procmime_mimeinfo_get_parameter(mimeinfo, "filename");
2498 if (basetmp == NULL)
2499 basetmp = procmime_mimeinfo_get_parameter(mimeinfo, "name");
2500 if (basetmp == NULL)
2501 basetmp = "mimetmp";
2502 basename = g_path_get_basename(basetmp);
2503 if (*basename == '\0') {
2505 basename = g_strdup("mimetmp");
2507 base = conv_filename_from_utf8(basename);
2509 subst_for_shellsafe_filename(base);