2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2006 Hiroyuki Yamamoto & The Sylpheed-Claws 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 2 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, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
28 #include <glib/gi18n.h>
35 #include <sys/types.h>
40 #include "procheader.h"
42 #include "quoted-printable.h"
49 #include "prefs_common.h"
50 #include "prefs_gtk.h"
51 #include "alertpanel.h"
53 static GHashTable *procmime_get_mime_type_table (void);
55 MimeInfo *procmime_mimeinfo_new(void)
59 mimeinfo = g_new0(MimeInfo, 1);
60 mimeinfo->content = MIMECONTENT_EMPTY;
61 mimeinfo->data.filename = NULL;
63 mimeinfo->type = MIMETYPE_UNKNOWN;
64 mimeinfo->encoding_type = ENC_UNKNOWN;
65 mimeinfo->typeparameters = g_hash_table_new(g_str_hash, g_str_equal);
67 mimeinfo->disposition = DISPOSITIONTYPE_UNKNOWN;
68 mimeinfo->dispositionparameters
69 = g_hash_table_new(g_str_hash, g_str_equal);
71 mimeinfo->node = g_node_new(mimeinfo);
76 static gboolean procmime_mimeinfo_parameters_destroy(gpointer key, gpointer value, gpointer user_data)
84 static gchar *forced_charset = NULL;
86 void procmime_force_charset(const gchar *str)
88 g_free(forced_charset);
89 forced_charset = NULL;
91 forced_charset = g_strdup(str);
94 static EncodingType forced_encoding = 0;
96 void procmime_force_encoding(EncodingType encoding)
98 forced_encoding = encoding;
101 static gboolean free_func(GNode *node, gpointer data)
103 MimeInfo *mimeinfo = (MimeInfo *) node->data;
105 switch (mimeinfo->content) {
106 case MIMECONTENT_FILE:
108 g_unlink(mimeinfo->data.filename);
109 g_free(mimeinfo->data.filename);
112 case MIMECONTENT_MEM:
114 g_free(mimeinfo->data.mem);
119 g_free(mimeinfo->subtype);
120 g_free(mimeinfo->description);
121 g_free(mimeinfo->id);
122 g_free(mimeinfo->location);
124 g_hash_table_foreach_remove(mimeinfo->typeparameters,
125 procmime_mimeinfo_parameters_destroy, NULL);
126 g_hash_table_destroy(mimeinfo->typeparameters);
127 g_hash_table_foreach_remove(mimeinfo->dispositionparameters,
128 procmime_mimeinfo_parameters_destroy, NULL);
129 g_hash_table_destroy(mimeinfo->dispositionparameters);
131 if (mimeinfo->privacy)
132 privacy_free_privacydata(mimeinfo->privacy);
139 void procmime_mimeinfo_free_all(MimeInfo *mimeinfo)
146 node = mimeinfo->node;
147 g_node_traverse(node, G_IN_ORDER, G_TRAVERSE_ALL, -1, free_func, NULL);
149 g_node_destroy(node);
152 MimeInfo *procmime_mimeinfo_parent(MimeInfo *mimeinfo)
154 g_return_val_if_fail(mimeinfo != NULL, NULL);
155 g_return_val_if_fail(mimeinfo->node != NULL, NULL);
157 if (mimeinfo->node->parent == NULL)
159 return (MimeInfo *) mimeinfo->node->parent->data;
162 MimeInfo *procmime_mimeinfo_next(MimeInfo *mimeinfo)
164 g_return_val_if_fail(mimeinfo != NULL, NULL);
165 g_return_val_if_fail(mimeinfo->node != NULL, NULL);
167 if (mimeinfo->node->children)
168 return (MimeInfo *) mimeinfo->node->children->data;
169 if (mimeinfo->node->next)
170 return (MimeInfo *) mimeinfo->node->next->data;
172 if (mimeinfo->node->parent == NULL)
175 while (mimeinfo->node->parent != NULL) {
176 mimeinfo = (MimeInfo *) mimeinfo->node->parent->data;
177 if (mimeinfo->node->next)
178 return (MimeInfo *) mimeinfo->node->next->data;
184 MimeInfo *procmime_scan_message(MsgInfo *msginfo)
189 filename = procmsg_get_message_file_path(msginfo);
190 if (!filename || !is_file_exist(filename)) {
192 filename = procmsg_get_message_file(msginfo);
194 if (!filename || !is_file_exist(filename))
197 if (!folder_has_parent_of_type(msginfo->folder, F_QUEUE) &&
198 !folder_has_parent_of_type(msginfo->folder, F_DRAFT))
199 mimeinfo = procmime_scan_file(filename);
201 mimeinfo = procmime_scan_queue_file(filename);
209 H_CONTENT_TRANSFER_ENCODING = 0,
211 H_CONTENT_DISPOSITION = 2,
212 H_CONTENT_DESCRIPTION = 3,
216 const gchar *procmime_mimeinfo_get_parameter(MimeInfo *mimeinfo, const gchar *name)
220 g_return_val_if_fail(mimeinfo != NULL, NULL);
221 g_return_val_if_fail(name != NULL, NULL);
223 value = g_hash_table_lookup(mimeinfo->dispositionparameters, name);
225 value = g_hash_table_lookup(mimeinfo->typeparameters, name);
230 gboolean procmime_decode_content(MimeInfo *mimeinfo)
237 gboolean tmp_file = FALSE;
239 EncodingType encoding = forced_encoding
241 : mimeinfo->encoding_type;
243 g_return_val_if_fail(mimeinfo != NULL, FALSE);
245 if (encoding == ENC_UNKNOWN ||
246 encoding == ENC_BINARY)
249 infp = g_fopen(mimeinfo->data.filename, "rb");
254 fseek(infp, mimeinfo->offset, SEEK_SET);
256 outfp = get_tmpfile_in_dir(get_mime_tmp_dir(), &tmpfilename);
263 readend = mimeinfo->offset + mimeinfo->length;
265 if (encoding == ENC_QUOTED_PRINTABLE) {
266 while ((ftell(infp) < readend) && (fgets(buf, sizeof(buf), infp) != NULL)) {
268 len = qp_decode_line(buf);
269 fwrite(buf, 1, len, outfp);
271 } else if (encoding == ENC_BASE64) {
272 gchar outbuf[BUFFSIZE];
274 Base64Decoder *decoder;
275 gboolean got_error = FALSE;
276 gboolean uncanonicalize = FALSE;
279 if (mimeinfo->type == MIMETYPE_TEXT ||
280 mimeinfo->type == MIMETYPE_MESSAGE) {
281 uncanonicalize = TRUE;
282 tmpfp = my_tmpfile();
285 if (tmp_file) fclose(outfp);
291 decoder = base64_decoder_new();
292 while ((ftell(infp) < readend) && (fgets(buf, sizeof(buf), infp) != NULL)) {
293 len = base64_decoder_decode(decoder, buf, outbuf);
294 if (len < 0 && !got_error) {
295 g_warning("Bad BASE64 content.\n");
296 fwrite(_("[Error decoding BASE64]\n"),
298 strlen(_("[Error decoding BASE64]\n")),
302 } else if (len >= 0) {
303 /* print out the error message only once
305 fwrite(outbuf, sizeof(gchar), len, tmpfp);
309 base64_decoder_free(decoder);
311 if (uncanonicalize) {
313 while (fgets(buf, sizeof(buf), tmpfp) != NULL) {
319 } else if (encoding == ENC_X_UUENCODE) {
320 gchar outbuf[BUFFSIZE];
322 gboolean flag = FALSE;
324 while ((ftell(infp) < readend) && (fgets(buf, sizeof(buf), infp) != NULL)) {
325 if (!flag && strncmp(buf,"begin ", 6)) continue;
328 len = fromuutobits(outbuf, buf);
331 g_warning("Bad UUENCODE content(%d)\n", len);
334 fwrite(outbuf, sizeof(gchar), len, outfp);
339 while ((ftell(infp) < readend) && (fgets(buf, sizeof(buf), infp) != NULL)) {
347 stat(tmpfilename, &statbuf);
348 if (mimeinfo->tmp && (mimeinfo->data.filename != NULL))
349 g_unlink(mimeinfo->data.filename);
350 g_free(mimeinfo->data.filename);
351 mimeinfo->data.filename = tmpfilename;
352 mimeinfo->tmp = TRUE;
353 mimeinfo->offset = 0;
354 mimeinfo->length = statbuf.st_size;
355 mimeinfo->encoding_type = ENC_BINARY;
360 #define B64_LINE_SIZE 57
361 #define B64_BUFFSIZE 77
363 gboolean procmime_encode_content(MimeInfo *mimeinfo, EncodingType encoding)
365 FILE *infp = NULL, *outfp;
370 if (mimeinfo->content == MIMECONTENT_EMPTY)
373 if (mimeinfo->encoding_type != ENC_UNKNOWN &&
374 mimeinfo->encoding_type != ENC_BINARY &&
375 mimeinfo->encoding_type != ENC_7BIT &&
376 mimeinfo->encoding_type != ENC_8BIT)
377 if(!procmime_decode_content(mimeinfo))
380 outfp = get_tmpfile_in_dir(get_mime_tmp_dir(), &tmpfilename);
386 if (mimeinfo->content == MIMECONTENT_FILE) {
387 if ((infp = g_fopen(mimeinfo->data.filename, "rb")) == NULL) {
388 g_warning("Can't open file %s\n", mimeinfo->data.filename);
391 } else if (mimeinfo->content == MIMECONTENT_MEM) {
392 infp = str_open_as_stream(mimeinfo->data.mem);
397 if (encoding == ENC_BASE64) {
398 gchar inbuf[B64_LINE_SIZE], outbuf[B64_BUFFSIZE];
400 gchar *tmp_file = NULL;
402 if (mimeinfo->type == MIMETYPE_TEXT ||
403 mimeinfo->type == MIMETYPE_MESSAGE) {
404 if (mimeinfo->content == MIMECONTENT_FILE) {
405 tmp_file = get_tmp_file();
406 if (canonicalize_file(mimeinfo->data.filename, tmp_file) < 0) {
411 if ((tmp_fp = g_fopen(tmp_file, "rb")) == NULL) {
412 FILE_OP_ERROR(tmp_file, "fopen");
419 gchar *out = canonicalize_str(mimeinfo->data.mem);
421 infp = str_open_as_stream(out);
429 while ((len = fread(inbuf, sizeof(gchar),
430 B64_LINE_SIZE, tmp_fp))
432 base64_encode(outbuf, inbuf, B64_LINE_SIZE);
433 fputs(outbuf, outfp);
436 if (len > 0 && feof(tmp_fp)) {
437 base64_encode(outbuf, inbuf, len);
438 fputs(outbuf, outfp);
447 } else if (encoding == ENC_QUOTED_PRINTABLE) {
448 gchar inbuf[BUFFSIZE], outbuf[BUFFSIZE * 4];
450 while (fgets(inbuf, sizeof(inbuf), infp) != NULL) {
451 qp_encode_line(outbuf, inbuf);
453 if (!strncmp("From ", outbuf, sizeof("From ")-1)) {
454 gchar *tmpbuf = outbuf;
456 tmpbuf += sizeof("From ")-1;
458 fputs("=46rom ", outfp);
459 fputs(tmpbuf, outfp);
461 fputs(outbuf, outfp);
466 while (fgets(buf, sizeof(buf), infp) != NULL) {
475 if (mimeinfo->content == MIMECONTENT_FILE) {
476 if (mimeinfo->tmp && (mimeinfo->data.filename != NULL))
477 g_unlink(mimeinfo->data.filename);
478 g_free(mimeinfo->data.filename);
479 } else if (mimeinfo->content == MIMECONTENT_MEM) {
480 if (mimeinfo->tmp && (mimeinfo->data.mem != NULL))
481 g_free(mimeinfo->data.mem);
484 stat(tmpfilename, &statbuf);
485 mimeinfo->content = MIMECONTENT_FILE;
486 mimeinfo->data.filename = tmpfilename;
487 mimeinfo->tmp = TRUE;
488 mimeinfo->offset = 0;
489 mimeinfo->length = statbuf.st_size;
490 mimeinfo->encoding_type = encoding;
495 gint procmime_get_part(const gchar *outfile, MimeInfo *mimeinfo)
499 gint restlength, readlength;
501 g_return_val_if_fail(outfile != NULL, -1);
502 g_return_val_if_fail(mimeinfo != NULL, -1);
504 if (mimeinfo->encoding_type != ENC_BINARY && !procmime_decode_content(mimeinfo))
507 if ((infp = g_fopen(mimeinfo->data.filename, "rb")) == NULL) {
508 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
511 if (fseek(infp, mimeinfo->offset, SEEK_SET) < 0) {
512 FILE_OP_ERROR(mimeinfo->data.filename, "fseek");
516 if ((outfp = g_fopen(outfile, "wb")) == NULL) {
517 FILE_OP_ERROR(outfile, "fopen");
522 restlength = mimeinfo->length;
524 while ((restlength > 0) && ((readlength = fread(buf, 1, restlength > BUFFSIZE ? BUFFSIZE : restlength, infp)) > 0)) {
525 fwrite(buf, 1, readlength, outfp);
526 restlength -= readlength;
530 if (fclose(outfp) == EOF) {
531 FILE_OP_ERROR(outfile, "fclose");
539 struct ContentRenderer {
544 static GList * renderer_list = NULL;
546 static struct ContentRenderer *
547 content_renderer_new(char * content_type, char * renderer)
549 struct ContentRenderer * cr;
551 cr = g_new(struct ContentRenderer, 1);
555 cr->content_type = g_strdup(content_type);
556 cr->renderer = g_strdup(renderer);
561 static void content_renderer_free(struct ContentRenderer * cr)
563 g_free(cr->content_type);
564 g_free(cr->renderer);
568 void renderer_read_config(void)
574 g_list_foreach(renderer_list, (GFunc) content_renderer_free, NULL);
575 renderer_list = NULL;
577 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, RENDERER_RC, NULL);
578 f = g_fopen(rcpath, "rb");
584 while (fgets(buf, BUFFSIZE, f)) {
586 struct ContentRenderer * cr;
589 p = strchr(buf, ' ');
594 cr = content_renderer_new(buf, p + 1);
598 renderer_list = g_list_append(renderer_list, cr);
604 void renderer_write_config(void)
610 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, RENDERER_RC, NULL);
612 if ((pfile = prefs_write_open(rcpath)) == NULL) {
613 g_warning("failed to write configuration to file\n");
620 for (cur = renderer_list ; cur != NULL ; cur = cur->next) {
621 struct ContentRenderer * renderer;
622 renderer = cur->data;
623 fprintf(pfile->fp, "%s %s\n", renderer->content_type,
627 if (prefs_file_close(pfile) < 0) {
628 g_warning("failed to write configuration to file\n");
633 FILE *procmime_get_text_content(MimeInfo *mimeinfo)
636 const gchar *src_codeset;
637 gboolean conv_fail = FALSE;
640 struct ContentRenderer * renderer;
642 gchar *tmpfile, *content_type;
644 g_return_val_if_fail(mimeinfo != NULL, NULL);
646 if (!procmime_decode_content(mimeinfo))
649 tmpfile = procmime_get_tmp_file_name(mimeinfo);
653 if (procmime_get_part(tmpfile, mimeinfo) < 0) {
658 tmpfp = g_fopen(tmpfile, "rb");
664 if ((outfp = my_tmpfile()) == NULL) {
671 src_codeset = forced_charset
673 procmime_mimeinfo_get_parameter(mimeinfo, "charset");
677 content_type = procmime_get_content_type_str(mimeinfo->type,
679 for (cur = renderer_list ; cur != NULL ; cur = cur->next) {
680 struct ContentRenderer * cr;
683 if (g_ascii_strcasecmp(cr->content_type, content_type) == 0) {
688 g_free(content_type);
690 if (renderer != NULL) {
696 dup2(fileno(outfp), 1);
698 p = popen(renderer->renderer, "w");
703 fread(buf, sizeof(char), sizeof(buf),
705 fwrite(buf, sizeof(char), count, p);
710 /* CodeConverter seems to have no effect here */
711 } else if (mimeinfo->type == MIMETYPE_TEXT && !g_ascii_strcasecmp(mimeinfo->subtype, "html")) {
712 SC_HTMLParser *parser;
715 conv = conv_code_converter_new(src_codeset);
716 parser = sc_html_parser_new(tmpfp, conv);
717 while ((str = sc_html_parse(parser)) != NULL) {
720 sc_html_parser_destroy(parser);
721 conv_code_converter_destroy(conv);
722 } else if (mimeinfo->type == MIMETYPE_TEXT && !g_ascii_strcasecmp(mimeinfo->subtype, "enriched")) {
726 conv = conv_code_converter_new(src_codeset);
727 parser = ertf_parser_new(tmpfp, conv);
728 while ((str = ertf_parse(parser)) != NULL) {
731 ertf_parser_destroy(parser);
732 conv_code_converter_destroy(conv);
733 } else if (mimeinfo->type == MIMETYPE_TEXT) {
734 while (fgets(buf, sizeof(buf), tmpfp) != NULL) {
735 str = conv_codeset_strdup(buf, src_codeset, CS_UTF_8);
747 g_warning("procmime_get_text_content(): Code conversion failed.\n");
757 /* search the first text part of (multipart) MIME message,
758 decode, convert it and output to outfp. */
759 FILE *procmime_get_first_text_content(MsgInfo *msginfo)
762 MimeInfo *mimeinfo, *partinfo;
764 g_return_val_if_fail(msginfo != NULL, NULL);
766 mimeinfo = procmime_scan_message(msginfo);
767 if (!mimeinfo) return NULL;
770 while (partinfo && partinfo->type != MIMETYPE_TEXT) {
771 partinfo = procmime_mimeinfo_next(partinfo);
774 outfp = procmime_get_text_content(partinfo);
776 procmime_mimeinfo_free_all(mimeinfo);
782 static gboolean find_encrypted_func(GNode *node, gpointer data)
784 MimeInfo *mimeinfo = (MimeInfo *) node->data;
785 MimeInfo **encinfo = (MimeInfo **) data;
787 if (privacy_mimeinfo_is_encrypted(mimeinfo)) {
795 static MimeInfo *find_encrypted_part(MimeInfo *rootinfo)
797 MimeInfo *encinfo = NULL;
799 g_node_traverse(rootinfo->node, G_IN_ORDER, G_TRAVERSE_ALL, -1,
800 find_encrypted_func, &encinfo);
805 /* search the first encrypted text part of (multipart) MIME message,
806 decode, convert it and output to outfp. */
807 FILE *procmime_get_first_encrypted_text_content(MsgInfo *msginfo)
810 MimeInfo *mimeinfo, *partinfo, *encinfo;
812 g_return_val_if_fail(msginfo != NULL, NULL);
814 mimeinfo = procmime_scan_message(msginfo);
820 if ((encinfo = find_encrypted_part(partinfo)) != NULL) {
821 debug_print("decrypting message part\n");
822 if (privacy_mimeinfo_decrypt(encinfo) < 0) {
823 alertpanel_error(_("Couldn't decrypt: %s"),
824 privacy_get_error());
829 while (partinfo && partinfo->type != MIMETYPE_TEXT) {
830 partinfo = procmime_mimeinfo_next(partinfo);
834 outfp = procmime_get_text_content(partinfo);
836 procmime_mimeinfo_free_all(mimeinfo);
841 gboolean procmime_msginfo_is_encrypted(MsgInfo *msginfo)
843 MimeInfo *mimeinfo, *partinfo;
844 gboolean result = FALSE;
846 g_return_val_if_fail(msginfo != NULL, FALSE);
848 mimeinfo = procmime_scan_message(msginfo);
854 result = (find_encrypted_part(partinfo) != NULL);
855 procmime_mimeinfo_free_all(mimeinfo);
860 gboolean procmime_find_string_part(MimeInfo *mimeinfo, const gchar *filename,
861 const gchar *str, StrFindFunc find_func)
866 g_return_val_if_fail(mimeinfo != NULL, FALSE);
867 g_return_val_if_fail(mimeinfo->type == MIMETYPE_TEXT, FALSE);
868 g_return_val_if_fail(str != NULL, FALSE);
869 g_return_val_if_fail(find_func != NULL, FALSE);
871 outfp = procmime_get_text_content(mimeinfo);
876 while (fgets(buf, sizeof(buf), outfp) != NULL) {
878 if (find_func(buf, str)) {
889 gboolean procmime_find_string(MsgInfo *msginfo, const gchar *str,
890 StrFindFunc find_func)
895 gboolean found = FALSE;
897 g_return_val_if_fail(msginfo != NULL, FALSE);
898 g_return_val_if_fail(str != NULL, FALSE);
899 g_return_val_if_fail(find_func != NULL, FALSE);
901 filename = procmsg_get_message_file(msginfo);
902 if (!filename) return FALSE;
903 mimeinfo = procmime_scan_message(msginfo);
905 for (partinfo = mimeinfo; partinfo != NULL;
906 partinfo = procmime_mimeinfo_next(partinfo)) {
907 if (partinfo->type == MIMETYPE_TEXT) {
908 if (procmime_find_string_part
909 (partinfo, filename, str, find_func) == TRUE) {
916 procmime_mimeinfo_free_all(mimeinfo);
922 gchar *procmime_get_tmp_file_name(MimeInfo *mimeinfo)
924 static guint32 id = 0;
929 g_return_val_if_fail(mimeinfo != NULL, NULL);
931 g_snprintf(f_prefix, sizeof(f_prefix), "%08x.", id++);
933 if ((mimeinfo->type == MIMETYPE_TEXT) && !g_ascii_strcasecmp(mimeinfo->subtype, "html"))
934 base = g_strdup("mimetmp.html");
936 const gchar *basetmp;
938 basetmp = procmime_mimeinfo_get_parameter(mimeinfo, "filename");
940 basetmp = procmime_mimeinfo_get_parameter(mimeinfo, "name");
943 basetmp = g_path_get_basename(basetmp);
944 if (*basetmp == '\0')
945 basetmp = g_strdup("mimetmp");
946 base = conv_filename_from_utf8(basetmp);
947 g_free((gchar*)basetmp);
948 subst_for_shellsafe_filename(base);
951 filename = g_strconcat(get_mime_tmp_dir(), G_DIR_SEPARATOR_S,
952 f_prefix, base, NULL);
959 static GList *mime_type_list = NULL;
961 gchar *procmime_get_mime_type(const gchar *filename)
963 static GHashTable *mime_type_table = NULL;
969 if (!mime_type_table) {
970 mime_type_table = procmime_get_mime_type_table();
971 if (!mime_type_table) return NULL;
974 base = g_path_get_basename(filename);
975 if ((p = strrchr(base, '.')) != NULL)
976 Xstrdup_a(ext, p + 1, p = NULL );
981 mime_type = g_hash_table_lookup(mime_type_table, ext);
985 str = g_strconcat(mime_type->type, "/", mime_type->sub_type,
993 static guint procmime_str_hash(gconstpointer gptr)
995 guint hash_result = 0;
998 for (str = gptr; str && *str; str++) {
999 if (isupper(*str)) hash_result += (*str + ' ');
1000 else hash_result += *str;
1006 static gint procmime_str_equal(gconstpointer gptr1, gconstpointer gptr2)
1008 const char *str1 = gptr1;
1009 const char *str2 = gptr2;
1011 return !g_utf8_collate(str1, str2);
1014 static GHashTable *procmime_get_mime_type_table(void)
1016 GHashTable *table = NULL;
1018 MimeType *mime_type;
1021 if (!mime_type_list) {
1022 mime_type_list = procmime_get_mime_type_list();
1023 if (!mime_type_list) return NULL;
1026 table = g_hash_table_new(procmime_str_hash, procmime_str_equal);
1028 for (cur = mime_type_list; cur != NULL; cur = cur->next) {
1032 mime_type = (MimeType *)cur->data;
1034 if (!mime_type->extension) continue;
1036 exts = g_strsplit(mime_type->extension, " ", 16);
1037 for (i = 0; exts[i] != NULL; i++) {
1038 /* make the key case insensitive */
1040 /* use previously dup'd key on overwriting */
1041 if (g_hash_table_lookup(table, exts[i]))
1044 key = g_strdup(exts[i]);
1045 g_hash_table_insert(table, key, mime_type);
1053 GList *procmime_get_mime_type_list(void)
1057 gchar buf[BUFFSIZE];
1060 MimeType *mime_type;
1061 gboolean fp_is_glob_file = TRUE;
1064 return mime_type_list;
1066 if ((fp = g_fopen("/usr/share/mime/globs", "rb")) == NULL) {
1067 fp_is_glob_file = FALSE;
1068 if ((fp = g_fopen("/etc/mime.types", "rb")) == NULL) {
1069 if ((fp = g_fopen(SYSCONFDIR "/mime.types", "rb"))
1071 FILE_OP_ERROR(SYSCONFDIR "/mime.types",
1078 while (fgets(buf, sizeof(buf), fp) != NULL) {
1079 p = strchr(buf, '#');
1085 if (fp_is_glob_file) {
1086 while (*p && !g_ascii_isspace(*p) && (*p!=':')) p++;
1088 while (*p && !g_ascii_isspace(*p)) p++;
1095 delim = strchr(buf, '/');
1096 if (delim == NULL) continue;
1099 mime_type = g_new(MimeType, 1);
1100 mime_type->type = g_strdup(buf);
1101 mime_type->sub_type = g_strdup(delim + 1);
1103 if (fp_is_glob_file) {
1104 while (*p && (g_ascii_isspace(*p)||(*p=='*')||(*p=='.'))) p++;
1106 while (*p && g_ascii_isspace(*p)) p++;
1110 mime_type->extension = g_strdup(p);
1112 mime_type->extension = NULL;
1114 list = g_list_append(list, mime_type);
1120 g_warning("Can't read mime.types\n");
1125 EncodingType procmime_get_encoding_for_charset(const gchar *charset)
1129 else if (!g_ascii_strncasecmp(charset, "ISO-2022-", 9) ||
1130 !g_ascii_strcasecmp(charset, "US-ASCII"))
1132 else if (!g_ascii_strcasecmp(charset, "ISO-8859-5") ||
1133 !g_ascii_strncasecmp(charset, "KOI8-", 5) ||
1134 !g_ascii_strcasecmp(charset, "Windows-1251"))
1136 else if (!g_ascii_strncasecmp(charset, "ISO-8859-", 9))
1137 return ENC_QUOTED_PRINTABLE;
1142 EncodingType procmime_get_encoding_for_text_file(const gchar *file, gboolean *has_binary)
1145 guchar buf[BUFFSIZE];
1147 size_t octet_chars = 0;
1148 size_t total_len = 0;
1149 gfloat octet_percentage;
1150 gboolean force_b64 = FALSE;
1152 if ((fp = g_fopen(file, "rb")) == NULL) {
1153 FILE_OP_ERROR(file, "fopen");
1157 while ((len = fread(buf, sizeof(guchar), sizeof(buf), fp)) > 0) {
1161 for (p = buf, i = 0; i < len; ++p, ++i) {
1175 octet_percentage = (gfloat)octet_chars / (gfloat)total_len;
1177 octet_percentage = 0.0;
1179 debug_print("procmime_get_encoding_for_text_file(): "
1180 "8bit chars: %d / %d (%f%%)\n", octet_chars, total_len,
1181 100.0 * octet_percentage);
1183 if (octet_percentage > 0.20 || force_b64) {
1184 debug_print("using BASE64\n");
1186 } else if (octet_chars > 0) {
1187 debug_print("using quoted-printable\n");
1188 return ENC_QUOTED_PRINTABLE;
1190 debug_print("using 7bit\n");
1195 struct EncodingTable
1198 EncodingType enc_type;
1201 struct EncodingTable encoding_table[] = {
1204 {"binary", ENC_BINARY},
1205 {"quoted-printable", ENC_QUOTED_PRINTABLE},
1206 {"base64", ENC_BASE64},
1207 {"x-uuencode", ENC_UNKNOWN},
1208 {NULL, ENC_UNKNOWN},
1211 const gchar *procmime_get_encoding_str(EncodingType encoding)
1213 struct EncodingTable *enc_table;
1215 for (enc_table = encoding_table; enc_table->str != NULL; enc_table++) {
1216 if (enc_table->enc_type == encoding)
1217 return enc_table->str;
1222 /* --- NEW MIME STUFF --- */
1229 static struct TypeTable mime_type_table[] = {
1230 {"text", MIMETYPE_TEXT},
1231 {"image", MIMETYPE_IMAGE},
1232 {"audio", MIMETYPE_AUDIO},
1233 {"video", MIMETYPE_VIDEO},
1234 {"application", MIMETYPE_APPLICATION},
1235 {"message", MIMETYPE_MESSAGE},
1236 {"multipart", MIMETYPE_MULTIPART},
1240 const gchar *procmime_get_media_type_str(MimeMediaType type)
1242 struct TypeTable *type_table;
1244 for (type_table = mime_type_table; type_table->str != NULL; type_table++) {
1245 if (type_table->type == type)
1246 return type_table->str;
1251 MimeMediaType procmime_get_media_type(const gchar *str)
1253 struct TypeTable *typetablearray;
1255 for (typetablearray = mime_type_table; typetablearray->str != NULL; typetablearray++)
1256 if (g_ascii_strncasecmp(str, typetablearray->str, strlen(typetablearray->str)) == 0)
1257 return typetablearray->type;
1259 return MIMETYPE_UNKNOWN;
1263 *\brief Safe wrapper for content type string.
1265 *\return const gchar * Pointer to content type string.
1267 gchar *procmime_get_content_type_str(MimeMediaType type,
1268 const char *subtype)
1270 const gchar *type_str = NULL;
1272 if (subtype == NULL || !(type_str = procmime_get_media_type_str(type)))
1273 return g_strdup("unknown");
1274 return g_strdup_printf("%s/%s", type_str, subtype);
1277 static int procmime_parse_mimepart(MimeInfo *parent,
1278 gchar *content_type,
1279 gchar *content_encoding,
1280 gchar *content_description,
1282 gchar *content_disposition,
1283 gchar *content_location,
1284 const gchar *filename,
1288 void procmime_parse_message_rfc822(MimeInfo *mimeinfo)
1290 HeaderEntry hentry[] = {{"Content-Type:", NULL, TRUE},
1291 {"Content-Transfer-Encoding:",
1293 {"Content-Description:",
1297 {"Content-Disposition:",
1299 {"Content-Location:",
1303 {NULL, NULL, FALSE}};
1304 guint content_start, i;
1308 procmime_decode_content(mimeinfo);
1310 fp = g_fopen(mimeinfo->data.filename, "rb");
1312 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
1315 fseek(fp, mimeinfo->offset, SEEK_SET);
1316 procheader_get_header_fields(fp, hentry);
1317 if (hentry[0].body != NULL) {
1318 tmp = conv_unmime_header(hentry[0].body, NULL);
1319 g_free(hentry[0].body);
1320 hentry[0].body = tmp;
1322 if (hentry[2].body != NULL) {
1323 tmp = conv_unmime_header(hentry[2].body, NULL);
1324 g_free(hentry[2].body);
1325 hentry[2].body = tmp;
1327 if (hentry[4].body != NULL) {
1328 tmp = conv_unmime_header(hentry[4].body, NULL);
1329 g_free(hentry[4].body);
1330 hentry[4].body = tmp;
1332 if (hentry[5].body != NULL) {
1333 tmp = conv_unmime_header(hentry[5].body, NULL);
1334 g_free(hentry[5].body);
1335 hentry[5].body = tmp;
1337 content_start = ftell(fp);
1340 procmime_parse_mimepart(mimeinfo,
1341 hentry[0].body, hentry[1].body,
1342 hentry[2].body, hentry[3].body,
1343 hentry[4].body, hentry[5].body,
1344 mimeinfo->data.filename, content_start,
1345 mimeinfo->length - (content_start - mimeinfo->offset));
1347 for (i = 0; i < (sizeof hentry / sizeof hentry[0]); i++) {
1348 g_free(hentry[i].body);
1349 hentry[i].body = NULL;
1353 void procmime_parse_multipart(MimeInfo *mimeinfo)
1355 HeaderEntry hentry[] = {{"Content-Type:", NULL, TRUE},
1356 {"Content-Transfer-Encoding:",
1358 {"Content-Description:",
1362 {"Content-Disposition:",
1364 {"Content-Location:",
1366 {NULL, NULL, FALSE}};
1369 gint boundary_len = 0, lastoffset = -1, i;
1370 gchar buf[BUFFSIZE];
1374 boundary = g_hash_table_lookup(mimeinfo->typeparameters, "boundary");
1377 boundary_len = strlen(boundary);
1379 procmime_decode_content(mimeinfo);
1381 fp = g_fopen(mimeinfo->data.filename, "rb");
1383 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
1386 fseek(fp, mimeinfo->offset, SEEK_SET);
1387 while ((p = fgets(buf, sizeof(buf), fp)) != NULL && result == 0) {
1388 if (ftell(fp) - 1 > (mimeinfo->offset + mimeinfo->length))
1391 if (IS_BOUNDARY(buf, boundary, boundary_len)) {
1392 if (lastoffset != -1) {
1393 result = procmime_parse_mimepart(mimeinfo,
1394 hentry[0].body, hentry[1].body,
1395 hentry[2].body, hentry[3].body,
1396 hentry[4].body, hentry[5].body,
1397 mimeinfo->data.filename, lastoffset,
1398 (ftell(fp) - strlen(buf)) - lastoffset - 1);
1401 if (buf[2 + boundary_len] == '-' &&
1402 buf[2 + boundary_len + 1] == '-')
1405 for (i = 0; i < (sizeof hentry / sizeof hentry[0]) ; i++) {
1406 g_free(hentry[i].body);
1407 hentry[i].body = NULL;
1409 procheader_get_header_fields(fp, hentry);
1410 if (hentry[0].body != NULL) {
1411 tmp = conv_unmime_header(hentry[0].body, NULL);
1412 g_free(hentry[0].body);
1413 hentry[0].body = tmp;
1415 if (hentry[2].body != NULL) {
1416 tmp = conv_unmime_header(hentry[2].body, NULL);
1417 g_free(hentry[2].body);
1418 hentry[2].body = tmp;
1420 if (hentry[4].body != NULL) {
1421 tmp = conv_unmime_header(hentry[4].body, NULL);
1422 g_free(hentry[4].body);
1423 hentry[4].body = tmp;
1425 if (hentry[5].body != NULL) {
1426 tmp = conv_unmime_header(hentry[5].body, NULL);
1427 g_free(hentry[5].body);
1428 hentry[5].body = tmp;
1430 lastoffset = ftell(fp);
1433 for (i = 0; i < (sizeof hentry / sizeof hentry[0]); i++) {
1434 g_free(hentry[i].body);
1435 hentry[i].body = NULL;
1440 static void parse_parameters(const gchar *parameters, GHashTable *table)
1442 gchar *params, *param, *next;
1443 GSList *convlist = NULL, *concatlist = NULL, *cur;
1445 params = g_strdup(parameters);
1448 for (; next != NULL; param = next) {
1449 gchar *attribute, *value, *tmp;
1451 gboolean convert = FALSE;
1453 next = strchr_with_skip_quote(param, '"', ';');
1462 value = strchr(attribute, '=');
1468 while (value[0] == ' ')
1471 g_strdown(attribute);
1473 len = strlen(attribute);
1474 if (attribute[len - 1] == '*') {
1475 gchar *srcpos, *dstpos, *endpos;
1478 attribute[len - 1] = '\0';
1482 endpos = value + strlen(value);
1483 while (srcpos < endpos) {
1489 if (!get_hex_value(&dstvalue, srcpos[1], srcpos[2]))
1500 if (value[0] == '"')
1501 extract_quote(value, '"');
1502 else if ((tmp = strchr(value, ' ')) != NULL)
1507 while (attribute[0] == ' ')
1509 while (attribute[strlen(attribute)-1] == ' ')
1510 attribute[strlen(attribute)-1] = '\0';
1513 while (value[0] == ' ')
1515 while (value[strlen(value)-1] == ' ')
1516 value[strlen(value)-1] = '\0';
1518 if (strrchr(attribute, '*') != NULL) {
1521 tmpattr = g_strdup(attribute);
1522 tmp = strrchr(tmpattr, '*');
1525 if ((tmp[1] == '0') && (tmp[2] == '\0') &&
1526 (g_slist_find_custom(concatlist, attribute, g_str_equal) == NULL))
1527 concatlist = g_slist_prepend(concatlist, g_strdup(tmpattr));
1529 if (convert && (g_slist_find_custom(convlist, attribute, g_str_equal) == NULL))
1530 convlist = g_slist_prepend(convlist, g_strdup(tmpattr));
1533 } else if (convert) {
1534 if (g_slist_find_custom(convlist, attribute, g_str_equal) == NULL)
1535 convlist = g_slist_prepend(convlist, g_strdup(attribute));
1538 if (g_hash_table_lookup(table, attribute) == NULL)
1539 g_hash_table_insert(table, g_strdup(attribute), g_strdup(value));
1542 for (cur = concatlist; cur != NULL; cur = g_slist_next(cur)) {
1543 gchar *attribute, *attrwnum, *partvalue;
1547 attribute = (gchar *) cur->data;
1548 value = g_string_sized_new(64);
1550 attrwnum = g_strdup_printf("%s*%d", attribute, n);
1551 while ((partvalue = g_hash_table_lookup(table, attrwnum)) != NULL) {
1552 g_string_append(value, partvalue);
1556 attrwnum = g_strdup_printf("%s*%d", attribute, n);
1560 g_hash_table_insert(table, g_strdup(attribute), g_strdup(value->str));
1561 g_string_free(value, TRUE);
1563 slist_free_strings(concatlist);
1564 g_slist_free(concatlist);
1566 for (cur = convlist; cur != NULL; cur = g_slist_next(cur)) {
1567 gchar *attribute, *key, *value;
1568 gchar *charset, *lang, *oldvalue, *newvalue;
1570 attribute = (gchar *) cur->data;
1571 if (!g_hash_table_lookup_extended(
1572 table, attribute, (gpointer *)(gchar *) &key, (gpointer *)(gchar *) &value))
1576 lang = strchr(charset, '\'');
1581 oldvalue = strchr(lang, '\'');
1582 if (oldvalue == NULL)
1587 newvalue = conv_codeset_strdup(oldvalue, charset, CS_UTF_8);
1589 g_hash_table_remove(table, attribute);
1593 g_hash_table_insert(table, g_strdup(attribute), newvalue);
1595 slist_free_strings(convlist);
1596 g_slist_free(convlist);
1601 static void procmime_parse_content_type(const gchar *content_type, MimeInfo *mimeinfo)
1603 g_return_if_fail(content_type != NULL);
1604 g_return_if_fail(mimeinfo != NULL);
1606 /* RFC 2045, page 13 says that the mime subtype is MANDATORY;
1607 * if it's not available we use the default Content-Type */
1608 if ((content_type[0] == '\0') || (strchr(content_type, '/') == NULL)) {
1609 mimeinfo->type = MIMETYPE_TEXT;
1610 mimeinfo->subtype = g_strdup("plain");
1611 if (g_hash_table_lookup(mimeinfo->typeparameters,
1612 "charset") == NULL) {
1613 g_hash_table_insert(mimeinfo->typeparameters,
1614 g_strdup("charset"),
1616 conv_get_locale_charset_str_no_utf8()));
1619 gchar *type, *subtype, *params;
1621 type = g_strdup(content_type);
1622 subtype = strchr(type, '/') + 1;
1623 *(subtype - 1) = '\0';
1624 if ((params = strchr(subtype, ';')) != NULL) {
1629 mimeinfo->type = procmime_get_media_type(type);
1630 mimeinfo->subtype = g_strdup(subtype);
1632 /* Get mimeinfo->typeparameters */
1634 parse_parameters(params, mimeinfo->typeparameters);
1640 static void procmime_parse_content_disposition(const gchar *content_disposition, MimeInfo *mimeinfo)
1642 gchar *tmp, *params;
1644 g_return_if_fail(content_disposition != NULL);
1645 g_return_if_fail(mimeinfo != NULL);
1647 tmp = g_strdup(content_disposition);
1648 if ((params = strchr(tmp, ';')) != NULL) {
1654 if (!g_ascii_strcasecmp(tmp, "inline"))
1655 mimeinfo->disposition = DISPOSITIONTYPE_INLINE;
1656 else if (!g_ascii_strcasecmp(tmp, "attachment"))
1657 mimeinfo->disposition = DISPOSITIONTYPE_ATTACHMENT;
1659 mimeinfo->disposition = DISPOSITIONTYPE_ATTACHMENT;
1662 parse_parameters(params, mimeinfo->dispositionparameters);
1668 static void procmime_parse_content_encoding(const gchar *content_encoding, MimeInfo *mimeinfo)
1670 struct EncodingTable *enc_table;
1672 for (enc_table = encoding_table; enc_table->str != NULL; enc_table++) {
1673 if (g_ascii_strcasecmp(enc_table->str, content_encoding) == 0) {
1674 mimeinfo->encoding_type = enc_table->enc_type;
1678 mimeinfo->encoding_type = ENC_UNKNOWN;
1682 static int procmime_parse_mimepart(MimeInfo *parent,
1683 gchar *content_type,
1684 gchar *content_encoding,
1685 gchar *content_description,
1687 gchar *content_disposition,
1688 gchar *content_location,
1689 const gchar *filename,
1695 /* Create MimeInfo */
1696 mimeinfo = procmime_mimeinfo_new();
1697 mimeinfo->content = MIMECONTENT_FILE;
1698 if (parent != NULL) {
1699 if (g_node_depth(parent->node) > 32) {
1700 /* 32 is an arbitrary value
1701 * this avoids DOSsing ourselves
1702 * with enormous messages
1704 procmime_mimeinfo_free_all(mimeinfo);
1707 g_node_append(parent->node, mimeinfo->node);
1709 mimeinfo->data.filename = g_strdup(filename);
1710 mimeinfo->offset = offset;
1711 mimeinfo->length = length;
1713 if (content_type != NULL) {
1714 procmime_parse_content_type(content_type, mimeinfo);
1716 mimeinfo->type = MIMETYPE_TEXT;
1717 mimeinfo->subtype = g_strdup("plain");
1718 if (g_hash_table_lookup(mimeinfo->typeparameters,
1719 "charset") == NULL) {
1720 g_hash_table_insert(mimeinfo->typeparameters,
1721 g_strdup("charset"),
1723 conv_get_locale_charset_str_no_utf8()));
1727 if (content_encoding != NULL) {
1728 procmime_parse_content_encoding(content_encoding, mimeinfo);
1730 mimeinfo->encoding_type = ENC_UNKNOWN;
1733 if (content_description != NULL)
1734 mimeinfo->description = g_strdup(content_description);
1736 mimeinfo->description = NULL;
1738 if (content_id != NULL)
1739 mimeinfo->id = g_strdup(content_id);
1741 mimeinfo->id = NULL;
1743 if (content_location != NULL)
1744 mimeinfo->location = g_strdup(content_location);
1746 mimeinfo->location = NULL;
1748 if (content_disposition != NULL)
1749 procmime_parse_content_disposition(content_disposition, mimeinfo);
1751 mimeinfo->disposition = DISPOSITIONTYPE_UNKNOWN;
1753 /* Call parser for mime type */
1754 switch (mimeinfo->type) {
1755 case MIMETYPE_MESSAGE:
1756 if (g_ascii_strcasecmp(mimeinfo->subtype, "rfc822") == 0) {
1757 procmime_parse_message_rfc822(mimeinfo);
1761 case MIMETYPE_MULTIPART:
1762 procmime_parse_multipart(mimeinfo);
1772 static gchar *typenames[] = {
1783 static gboolean output_func(GNode *node, gpointer data)
1786 MimeInfo *mimeinfo = (MimeInfo *) node->data;
1788 depth = g_node_depth(node);
1789 for (i = 0; i < depth; i++)
1791 printf("%s/%s (offset:%d length:%d encoding: %d)\n", typenames[mimeinfo->type], mimeinfo->subtype, mimeinfo->offset, mimeinfo->length, mimeinfo->encoding_type);
1796 static void output_mime_structure(MimeInfo *mimeinfo, int indent)
1798 g_node_traverse(mimeinfo->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1, output_func, NULL);
1801 MimeInfo *procmime_scan_file_with_offset(const gchar *filename, int offset)
1806 stat(filename, &buf);
1808 mimeinfo = procmime_mimeinfo_new();
1809 mimeinfo->content = MIMECONTENT_FILE;
1810 mimeinfo->encoding_type = ENC_UNKNOWN;
1811 mimeinfo->type = MIMETYPE_MESSAGE;
1812 mimeinfo->subtype = g_strdup("rfc822");
1813 mimeinfo->data.filename = g_strdup(filename);
1814 mimeinfo->offset = offset;
1815 mimeinfo->length = buf.st_size - offset;
1817 procmime_parse_message_rfc822(mimeinfo);
1818 if (debug_get_mode())
1819 output_mime_structure(mimeinfo, 0);
1824 MimeInfo *procmime_scan_file(const gchar *filename)
1828 g_return_val_if_fail(filename != NULL, NULL);
1830 mimeinfo = procmime_scan_file_with_offset(filename, 0);
1835 MimeInfo *procmime_scan_queue_file(const gchar *filename)
1839 gchar buf[BUFFSIZE];
1842 g_return_val_if_fail(filename != NULL, NULL);
1845 if ((fp = g_fopen(filename, "rb")) == NULL)
1847 /* Skip queue header */
1848 while (fgets(buf, sizeof(buf), fp) != NULL) {
1850 if (!strncmp(buf, "X-Sylpheed-End-Special-Headers: 1",
1851 strlen("X-Sylpheed-End-Special-Headers:")))
1854 if (buf[0] == '\r' || buf[0] == '\n') break;
1855 /* from other mailers */
1856 if (!strncmp(buf, "Date: ", 6)
1857 || !strncmp(buf, "To: ", 4)
1858 || !strncmp(buf, "From: ", 6)
1859 || !strncmp(buf, "Subject: ", 9)) {
1867 mimeinfo = procmime_scan_file_with_offset(filename, offset);
1874 ENC_AS_QUOTED_STRING,
1879 typedef struct _ParametersData {
1885 static void write_parameters(gpointer key, gpointer value, gpointer user_data)
1888 gchar *val = value, *valpos, *tmp;
1889 ParametersData *pdata = (ParametersData *)user_data;
1890 GString *buf = g_string_new("");
1892 EncodeAs encas = ENC_AS_TOKEN;
1894 for (valpos = val; *valpos != 0; valpos++) {
1895 if (!IS_ASCII(*valpos) || *valpos == '"') {
1896 encas = ENC_AS_EXTENDED;
1901 if (((*valpos >= 0) && (*valpos < 037)) || (*valpos == 0177)) {
1902 encas = ENC_AS_QUOTED_STRING;
1906 /* tspecials + SPACE */
1924 encas = ENC_AS_QUOTED_STRING;
1929 if (encas == ENC_AS_EXTENDED && pdata->ascii_only == TRUE)
1930 encas = ENC_TO_ASCII;
1934 g_string_append_printf(buf, "%s=%s", param, val);
1938 tmp = g_strdup(val);
1940 " ()<>@,';:\\/[]?=.0123456789"
1941 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1942 "abcdefghijklmnopqrstuvwxyz",
1944 g_string_append_printf(buf, "%s=\"%s\"", param, tmp);
1948 case ENC_AS_QUOTED_STRING:
1949 g_string_append_printf(buf, "%s=\"%s\"", param, val);
1952 case ENC_AS_EXTENDED:
1953 if (!g_utf8_validate(val, -1, NULL))
1954 g_string_append_printf(buf, "%s*=%s''", param,
1955 conv_get_locale_charset_str());
1957 g_string_append_printf(buf, "%s*=%s''", param,
1959 for (valpos = val; *valpos != '\0'; valpos++) {
1960 if (IS_ASCII(*valpos) && isalnum(*valpos)) {
1961 g_string_append_printf(buf, "%c", *valpos);
1963 gchar hexstr[3] = "XX";
1964 get_hex_str(hexstr, *valpos);
1965 g_string_append_printf(buf, "%%%s", hexstr);
1971 if (buf->str && strlen(buf->str)) {
1972 if (pdata->len + strlen(buf->str) + 2 > 76) {
1973 fprintf(pdata->fp, ";\n %s", buf->str);
1974 pdata->len = strlen(buf->str) + 1;
1976 fprintf(pdata->fp, "; %s", buf->str);
1977 pdata->len += strlen(buf->str) + 2;
1980 g_string_free(buf, TRUE);
1983 void procmime_write_mime_header(MimeInfo *mimeinfo, FILE *fp)
1985 struct TypeTable *type_table;
1986 ParametersData *pdata = g_new0(ParametersData, 1);
1987 debug_print("procmime_write_mime_header\n");
1990 pdata->ascii_only = FALSE;
1992 for (type_table = mime_type_table; type_table->str != NULL; type_table++)
1993 if (mimeinfo->type == type_table->type) {
1994 gchar *buf = g_strdup_printf(
1995 "Content-Type: %s/%s", type_table->str, mimeinfo->subtype);
1996 fprintf(fp, "%s", buf);
1997 pdata->len = strlen(buf);
1998 pdata->ascii_only = TRUE;
2002 g_hash_table_foreach(mimeinfo->typeparameters, write_parameters, pdata);
2007 if (mimeinfo->encoding_type != ENC_UNKNOWN)
2008 fprintf(fp, "Content-Transfer-Encoding: %s\n", procmime_get_encoding_str(mimeinfo->encoding_type));
2010 if (mimeinfo->description != NULL)
2011 fprintf(fp, "Content-Description: %s\n", mimeinfo->description);
2013 if (mimeinfo->id != NULL)
2014 fprintf(fp, "Content-ID: %s\n", mimeinfo->id);
2016 if (mimeinfo->location != NULL)
2017 fprintf(fp, "Content-Location: %s\n", mimeinfo->location);
2019 if (mimeinfo->disposition != DISPOSITIONTYPE_UNKNOWN) {
2020 ParametersData *pdata = g_new0(ParametersData, 1);
2022 if (mimeinfo->disposition == DISPOSITIONTYPE_INLINE)
2023 buf = g_strdup("Content-Disposition: inline");
2024 else if (mimeinfo->disposition == DISPOSITIONTYPE_ATTACHMENT)
2025 buf = g_strdup("Content-Disposition: attachment");
2027 buf = g_strdup("Content-Disposition: unknown");
2029 fprintf(fp, "%s", buf);
2030 pdata->len = strlen(buf);
2034 pdata->ascii_only = FALSE;
2036 g_hash_table_foreach(mimeinfo->dispositionparameters, write_parameters, pdata);
2044 gint procmime_write_message_rfc822(MimeInfo *mimeinfo, FILE *fp)
2049 gchar buf[BUFFSIZE];
2050 gboolean skip = FALSE;;
2052 debug_print("procmime_write_message_rfc822\n");
2055 switch (mimeinfo->content) {
2056 case MIMECONTENT_FILE:
2057 if ((infp = g_fopen(mimeinfo->data.filename, "rb")) == NULL) {
2058 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
2061 fseek(infp, mimeinfo->offset, SEEK_SET);
2062 while (fgets(buf, sizeof(buf), infp) == buf) {
2063 if (buf[0] == '\n' && buf[1] == '\0')
2065 if (skip && (buf[0] == ' ' || buf[0] == '\t'))
2067 if (g_ascii_strncasecmp(buf, "Mime-Version:", 13) == 0 ||
2068 g_ascii_strncasecmp(buf, "Content-Type:", 13) == 0 ||
2069 g_ascii_strncasecmp(buf, "Content-Transfer-Encoding:", 26) == 0 ||
2070 g_ascii_strncasecmp(buf, "Content-Description:", 20) == 0 ||
2071 g_ascii_strncasecmp(buf, "Content-ID:", 11) == 0 ||
2072 g_ascii_strncasecmp(buf, "Content-Location:", 17) == 0 ||
2073 g_ascii_strncasecmp(buf, "Content-Disposition:", 20) == 0) {
2077 fwrite(buf, sizeof(gchar), strlen(buf), fp);
2083 case MIMECONTENT_MEM:
2084 fwrite(mimeinfo->data.mem,
2086 strlen(mimeinfo->data.mem),
2094 childnode = mimeinfo->node->children;
2095 if (childnode == NULL)
2098 child = (MimeInfo *) childnode->data;
2099 fprintf(fp, "Mime-Version: 1.0\n");
2100 procmime_write_mime_header(child, fp);
2101 return procmime_write_mimeinfo(child, fp);
2104 gint procmime_write_multipart(MimeInfo *mimeinfo, FILE *fp)
2108 gchar *boundary, *str, *str2;
2109 gchar buf[BUFFSIZE];
2110 gboolean firstboundary;
2112 debug_print("procmime_write_multipart\n");
2114 boundary = g_hash_table_lookup(mimeinfo->typeparameters, "boundary");
2116 switch (mimeinfo->content) {
2117 case MIMECONTENT_FILE:
2118 if ((infp = g_fopen(mimeinfo->data.filename, "rb")) == NULL) {
2119 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
2122 fseek(infp, mimeinfo->offset, SEEK_SET);
2123 while (fgets(buf, sizeof(buf), infp) == buf) {
2124 if (IS_BOUNDARY(buf, boundary, strlen(boundary)))
2126 fwrite(buf, sizeof(gchar), strlen(buf), fp);
2131 case MIMECONTENT_MEM:
2132 str = g_strdup(mimeinfo->data.mem);
2133 if (((str2 = strstr(str, boundary)) != NULL) && ((str2 - str) >= 2) &&
2134 (*(str2 - 1) == '-') && (*(str2 - 2) == '-'))
2136 fwrite(str, sizeof(gchar), strlen(str), fp);
2144 childnode = mimeinfo->node->children;
2145 firstboundary = TRUE;
2146 while (childnode != NULL) {
2147 MimeInfo *child = childnode->data;
2150 firstboundary = FALSE;
2153 fprintf(fp, "--%s\n", boundary);
2155 procmime_write_mime_header(child, fp);
2156 if (procmime_write_mimeinfo(child, fp) < 0)
2159 childnode = g_node_next_sibling(childnode);
2161 fprintf(fp, "\n--%s--\n", boundary);
2166 gint procmime_write_mimeinfo(MimeInfo *mimeinfo, FILE *fp)
2170 debug_print("procmime_write_mimeinfo\n");
2172 if (G_NODE_IS_LEAF(mimeinfo->node)) {
2173 switch (mimeinfo->content) {
2174 case MIMECONTENT_FILE:
2175 if ((infp = g_fopen(mimeinfo->data.filename, "rb")) == NULL) {
2176 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
2179 copy_file_part_to_fp(infp, mimeinfo->offset, mimeinfo->length, fp);
2183 case MIMECONTENT_MEM:
2184 fwrite(mimeinfo->data.mem,
2186 strlen(mimeinfo->data.mem),
2194 /* Call writer for mime type */
2195 switch (mimeinfo->type) {
2196 case MIMETYPE_MESSAGE:
2197 if (g_ascii_strcasecmp(mimeinfo->subtype, "rfc822") == 0) {
2198 return procmime_write_message_rfc822(mimeinfo, fp);
2202 case MIMETYPE_MULTIPART:
2203 return procmime_write_multipart(mimeinfo, fp);
2215 gchar *procmime_get_part_file_name(MimeInfo *mimeinfo)
2219 if ((mimeinfo->type == MIMETYPE_TEXT) && !g_ascii_strcasecmp(mimeinfo->subtype, "html"))
2220 base = g_strdup("mimetmp.html");
2222 const gchar *basetmp;
2225 basetmp = procmime_mimeinfo_get_parameter(mimeinfo, "filename");
2226 if (basetmp == NULL)
2227 basetmp = procmime_mimeinfo_get_parameter(mimeinfo, "name");
2228 if (basetmp == NULL)
2229 basetmp = "mimetmp";
2230 basename = g_path_get_basename(basetmp);
2231 if (*basename == '\0') {
2233 basename = g_strdup("mimetmp");
2235 base = conv_filename_from_utf8(basename);
2237 subst_for_shellsafe_filename(base);