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"
52 static GHashTable *procmime_get_mime_type_table (void);
54 MimeInfo *procmime_mimeinfo_new(void)
58 mimeinfo = g_new0(MimeInfo, 1);
59 mimeinfo->content = MIMECONTENT_EMPTY;
60 mimeinfo->data.filename = NULL;
62 mimeinfo->type = MIMETYPE_UNKNOWN;
63 mimeinfo->encoding_type = ENC_UNKNOWN;
64 mimeinfo->typeparameters = g_hash_table_new(g_str_hash, g_str_equal);
66 mimeinfo->disposition = DISPOSITIONTYPE_UNKNOWN;
67 mimeinfo->dispositionparameters
68 = g_hash_table_new(g_str_hash, g_str_equal);
70 mimeinfo->node = g_node_new(mimeinfo);
75 static gboolean procmime_mimeinfo_parameters_destroy(gpointer key, gpointer value, gpointer user_data)
83 static gchar *forced_charset = NULL;
85 void procmime_force_charset(const gchar *str)
87 g_free(forced_charset);
88 forced_charset = NULL;
90 forced_charset = g_strdup(str);
93 static EncodingType forced_encoding = 0;
95 void procmime_force_encoding(EncodingType encoding)
97 forced_encoding = encoding;
100 static gboolean free_func(GNode *node, gpointer data)
102 MimeInfo *mimeinfo = (MimeInfo *) node->data;
104 switch (mimeinfo->content) {
105 case MIMECONTENT_FILE:
107 g_unlink(mimeinfo->data.filename);
108 g_free(mimeinfo->data.filename);
111 case MIMECONTENT_MEM:
113 g_free(mimeinfo->data.mem);
118 g_free(mimeinfo->subtype);
119 g_free(mimeinfo->description);
120 g_free(mimeinfo->id);
121 g_free(mimeinfo->location);
123 g_hash_table_foreach_remove(mimeinfo->typeparameters,
124 procmime_mimeinfo_parameters_destroy, NULL);
125 g_hash_table_destroy(mimeinfo->typeparameters);
126 g_hash_table_foreach_remove(mimeinfo->dispositionparameters,
127 procmime_mimeinfo_parameters_destroy, NULL);
128 g_hash_table_destroy(mimeinfo->dispositionparameters);
130 if (mimeinfo->privacy)
131 privacy_free_privacydata(mimeinfo->privacy);
138 void procmime_mimeinfo_free_all(MimeInfo *mimeinfo)
145 node = mimeinfo->node;
146 g_node_traverse(node, G_IN_ORDER, G_TRAVERSE_ALL, -1, free_func, NULL);
148 g_node_destroy(node);
151 MimeInfo *procmime_mimeinfo_parent(MimeInfo *mimeinfo)
153 g_return_val_if_fail(mimeinfo != NULL, NULL);
154 g_return_val_if_fail(mimeinfo->node != NULL, NULL);
156 if (mimeinfo->node->parent == NULL)
158 return (MimeInfo *) mimeinfo->node->parent->data;
161 MimeInfo *procmime_mimeinfo_next(MimeInfo *mimeinfo)
163 g_return_val_if_fail(mimeinfo != NULL, NULL);
164 g_return_val_if_fail(mimeinfo->node != NULL, NULL);
166 if (mimeinfo->node->children)
167 return (MimeInfo *) mimeinfo->node->children->data;
168 if (mimeinfo->node->next)
169 return (MimeInfo *) mimeinfo->node->next->data;
171 if (mimeinfo->node->parent == NULL)
174 while (mimeinfo->node->parent != NULL) {
175 mimeinfo = (MimeInfo *) mimeinfo->node->parent->data;
176 if (mimeinfo->node->next)
177 return (MimeInfo *) mimeinfo->node->next->data;
183 MimeInfo *procmime_scan_message(MsgInfo *msginfo)
188 filename = procmsg_get_message_file_path(msginfo);
189 if (!filename || !is_file_exist(filename)) {
191 filename = procmsg_get_message_file(msginfo);
193 if (!filename || !is_file_exist(filename))
196 if (!folder_has_parent_of_type(msginfo->folder, F_QUEUE) &&
197 !folder_has_parent_of_type(msginfo->folder, F_DRAFT))
198 mimeinfo = procmime_scan_file(filename);
200 mimeinfo = procmime_scan_queue_file(filename);
208 H_CONTENT_TRANSFER_ENCODING = 0,
210 H_CONTENT_DISPOSITION = 2,
211 H_CONTENT_DESCRIPTION = 3,
215 const gchar *procmime_mimeinfo_get_parameter(MimeInfo *mimeinfo, const gchar *name)
219 g_return_val_if_fail(mimeinfo != NULL, NULL);
220 g_return_val_if_fail(name != NULL, NULL);
222 value = g_hash_table_lookup(mimeinfo->dispositionparameters, name);
224 value = g_hash_table_lookup(mimeinfo->typeparameters, name);
229 gboolean procmime_decode_content(MimeInfo *mimeinfo)
236 gboolean tmp_file = FALSE;
238 EncodingType encoding = forced_encoding
240 : mimeinfo->encoding_type;
242 g_return_val_if_fail(mimeinfo != NULL, FALSE);
244 if (encoding == ENC_UNKNOWN ||
245 encoding == ENC_BINARY)
248 infp = g_fopen(mimeinfo->data.filename, "rb");
253 fseek(infp, mimeinfo->offset, SEEK_SET);
255 outfp = get_tmpfile_in_dir(get_mime_tmp_dir(), &tmpfilename);
262 readend = mimeinfo->offset + mimeinfo->length;
264 if (encoding == ENC_QUOTED_PRINTABLE) {
265 while ((ftell(infp) < readend) && (fgets(buf, sizeof(buf), infp) != NULL)) {
267 len = qp_decode_line(buf);
268 fwrite(buf, 1, len, outfp);
270 } else if (encoding == ENC_BASE64) {
271 gchar outbuf[BUFFSIZE];
273 Base64Decoder *decoder;
274 gboolean got_error = FALSE;
275 gboolean uncanonicalize = FALSE;
278 if (mimeinfo->type == MIMETYPE_TEXT ||
279 mimeinfo->type == MIMETYPE_MESSAGE) {
280 uncanonicalize = TRUE;
281 tmpfp = my_tmpfile();
284 if (tmp_file) fclose(outfp);
290 decoder = base64_decoder_new();
291 while ((ftell(infp) < readend) && (fgets(buf, sizeof(buf), infp) != NULL)) {
292 len = base64_decoder_decode(decoder, buf, outbuf);
293 if (len < 0 && !got_error) {
294 g_warning("Bad BASE64 content.\n");
295 fwrite(_("[Error decoding BASE64]\n"),
297 strlen(_("[Error decoding BASE64]\n")),
301 } else if (len >= 0) {
302 /* print out the error message only once
304 fwrite(outbuf, sizeof(gchar), len, tmpfp);
308 base64_decoder_free(decoder);
310 if (uncanonicalize) {
312 while (fgets(buf, sizeof(buf), tmpfp) != NULL) {
318 } else if (encoding == ENC_X_UUENCODE) {
319 gchar outbuf[BUFFSIZE];
321 gboolean flag = FALSE;
323 while ((ftell(infp) < readend) && (fgets(buf, sizeof(buf), infp) != NULL)) {
324 if (!flag && strncmp(buf,"begin ", 6)) continue;
327 len = fromuutobits(outbuf, buf);
330 g_warning("Bad UUENCODE content(%d)\n", len);
333 fwrite(outbuf, sizeof(gchar), len, outfp);
338 while ((ftell(infp) < readend) && (fgets(buf, sizeof(buf), infp) != NULL)) {
346 stat(tmpfilename, &statbuf);
347 if (mimeinfo->tmp && (mimeinfo->data.filename != NULL))
348 g_unlink(mimeinfo->data.filename);
349 if (mimeinfo->data.filename != NULL)
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)
826 while (partinfo && partinfo->type != MIMETYPE_TEXT) {
827 partinfo = procmime_mimeinfo_next(partinfo);
831 outfp = procmime_get_text_content(partinfo);
833 procmime_mimeinfo_free_all(mimeinfo);
838 gboolean procmime_msginfo_is_encrypted(MsgInfo *msginfo)
840 MimeInfo *mimeinfo, *partinfo;
841 gboolean result = FALSE;
843 g_return_val_if_fail(msginfo != NULL, FALSE);
845 mimeinfo = procmime_scan_message(msginfo);
851 result = (find_encrypted_part(partinfo) != NULL);
852 procmime_mimeinfo_free_all(mimeinfo);
857 gboolean procmime_find_string_part(MimeInfo *mimeinfo, const gchar *filename,
858 const gchar *str, StrFindFunc find_func)
863 g_return_val_if_fail(mimeinfo != NULL, FALSE);
864 g_return_val_if_fail(mimeinfo->type == MIMETYPE_TEXT, FALSE);
865 g_return_val_if_fail(str != NULL, FALSE);
866 g_return_val_if_fail(find_func != NULL, FALSE);
868 outfp = procmime_get_text_content(mimeinfo);
873 while (fgets(buf, sizeof(buf), outfp) != NULL) {
875 if (find_func(buf, str)) {
886 gboolean procmime_find_string(MsgInfo *msginfo, const gchar *str,
887 StrFindFunc find_func)
892 gboolean found = FALSE;
894 g_return_val_if_fail(msginfo != NULL, FALSE);
895 g_return_val_if_fail(str != NULL, FALSE);
896 g_return_val_if_fail(find_func != NULL, FALSE);
898 filename = procmsg_get_message_file(msginfo);
899 if (!filename) return FALSE;
900 mimeinfo = procmime_scan_message(msginfo);
902 for (partinfo = mimeinfo; partinfo != NULL;
903 partinfo = procmime_mimeinfo_next(partinfo)) {
904 if (partinfo->type == MIMETYPE_TEXT) {
905 if (procmime_find_string_part
906 (partinfo, filename, str, find_func) == TRUE) {
913 procmime_mimeinfo_free_all(mimeinfo);
919 gchar *procmime_get_tmp_file_name(MimeInfo *mimeinfo)
921 static guint32 id = 0;
926 g_return_val_if_fail(mimeinfo != NULL, NULL);
928 g_snprintf(f_prefix, sizeof(f_prefix), "%08x.", id++);
930 if ((mimeinfo->type == MIMETYPE_TEXT) && !g_ascii_strcasecmp(mimeinfo->subtype, "html"))
931 base = g_strdup("mimetmp.html");
933 const gchar *basetmp;
935 basetmp = procmime_mimeinfo_get_parameter(mimeinfo, "filename");
937 basetmp = procmime_mimeinfo_get_parameter(mimeinfo, "name");
940 basetmp = g_path_get_basename(basetmp);
941 if (*basetmp == '\0')
942 basetmp = g_strdup("mimetmp");
943 base = conv_filename_from_utf8(basetmp);
944 g_free((gchar*)basetmp);
945 subst_for_shellsafe_filename(base);
948 filename = g_strconcat(get_mime_tmp_dir(), G_DIR_SEPARATOR_S,
949 f_prefix, base, NULL);
956 static GList *mime_type_list = NULL;
958 gchar *procmime_get_mime_type(const gchar *filename)
960 static GHashTable *mime_type_table = NULL;
966 if (!mime_type_table) {
967 mime_type_table = procmime_get_mime_type_table();
968 if (!mime_type_table) return NULL;
971 base = g_path_get_basename(filename);
972 if ((p = strrchr(base, '.')) != NULL)
973 Xstrdup_a(ext, p + 1, p = NULL );
978 mime_type = g_hash_table_lookup(mime_type_table, ext);
982 str = g_strconcat(mime_type->type, "/", mime_type->sub_type,
990 static guint procmime_str_hash(gconstpointer gptr)
992 guint hash_result = 0;
995 for (str = gptr; str && *str; str++) {
996 if (isupper(*str)) hash_result += (*str + ' ');
997 else hash_result += *str;
1003 static gint procmime_str_equal(gconstpointer gptr1, gconstpointer gptr2)
1005 const char *str1 = gptr1;
1006 const char *str2 = gptr2;
1008 return !g_utf8_collate(str1, str2);
1011 static GHashTable *procmime_get_mime_type_table(void)
1013 GHashTable *table = NULL;
1015 MimeType *mime_type;
1018 if (!mime_type_list) {
1019 mime_type_list = procmime_get_mime_type_list();
1020 if (!mime_type_list) return NULL;
1023 table = g_hash_table_new(procmime_str_hash, procmime_str_equal);
1025 for (cur = mime_type_list; cur != NULL; cur = cur->next) {
1029 mime_type = (MimeType *)cur->data;
1031 if (!mime_type->extension) continue;
1033 exts = g_strsplit(mime_type->extension, " ", 16);
1034 for (i = 0; exts[i] != NULL; i++) {
1035 /* make the key case insensitive */
1037 /* use previously dup'd key on overwriting */
1038 if (g_hash_table_lookup(table, exts[i]))
1041 key = g_strdup(exts[i]);
1042 g_hash_table_insert(table, key, mime_type);
1050 GList *procmime_get_mime_type_list(void)
1054 gchar buf[BUFFSIZE];
1057 MimeType *mime_type;
1058 gboolean fp_is_glob_file = TRUE;
1061 return mime_type_list;
1063 if ((fp = g_fopen("/usr/share/mime/globs", "rb")) == NULL) {
1064 fp_is_glob_file = FALSE;
1065 if ((fp = g_fopen("/etc/mime.types", "rb")) == NULL) {
1066 if ((fp = g_fopen(SYSCONFDIR "/mime.types", "rb"))
1068 FILE_OP_ERROR(SYSCONFDIR "/mime.types",
1075 while (fgets(buf, sizeof(buf), fp) != NULL) {
1076 p = strchr(buf, '#');
1082 if (fp_is_glob_file) {
1083 while (*p && !g_ascii_isspace(*p) && (*p!=':')) p++;
1085 while (*p && !g_ascii_isspace(*p)) p++;
1092 delim = strchr(buf, '/');
1093 if (delim == NULL) continue;
1096 mime_type = g_new(MimeType, 1);
1097 mime_type->type = g_strdup(buf);
1098 mime_type->sub_type = g_strdup(delim + 1);
1100 if (fp_is_glob_file) {
1101 while (*p && (g_ascii_isspace(*p)||(*p=='*')||(*p=='.'))) p++;
1103 while (*p && g_ascii_isspace(*p)) p++;
1107 mime_type->extension = g_strdup(p);
1109 mime_type->extension = NULL;
1111 list = g_list_append(list, mime_type);
1117 g_warning("Can't read mime.types\n");
1122 EncodingType procmime_get_encoding_for_charset(const gchar *charset)
1126 else if (!g_ascii_strncasecmp(charset, "ISO-2022-", 9) ||
1127 !g_ascii_strcasecmp(charset, "US-ASCII"))
1129 else if (!g_ascii_strcasecmp(charset, "ISO-8859-5") ||
1130 !g_ascii_strncasecmp(charset, "KOI8-", 5) ||
1131 !g_ascii_strcasecmp(charset, "Windows-1251"))
1133 else if (!g_ascii_strncasecmp(charset, "ISO-8859-", 9))
1134 return ENC_QUOTED_PRINTABLE;
1139 EncodingType procmime_get_encoding_for_text_file(const gchar *file, gboolean *has_binary)
1142 guchar buf[BUFFSIZE];
1144 size_t octet_chars = 0;
1145 size_t total_len = 0;
1146 gfloat octet_percentage;
1147 gboolean force_b64 = FALSE;
1149 if ((fp = g_fopen(file, "rb")) == NULL) {
1150 FILE_OP_ERROR(file, "fopen");
1154 while ((len = fread(buf, sizeof(guchar), sizeof(buf), fp)) > 0) {
1158 for (p = buf, i = 0; i < len; ++p, ++i) {
1172 octet_percentage = (gfloat)octet_chars / (gfloat)total_len;
1174 octet_percentage = 0.0;
1176 debug_print("procmime_get_encoding_for_text_file(): "
1177 "8bit chars: %d / %d (%f%%)\n", octet_chars, total_len,
1178 100.0 * octet_percentage);
1180 if (octet_percentage > 0.20 || force_b64) {
1181 debug_print("using BASE64\n");
1183 } else if (octet_chars > 0) {
1184 debug_print("using quoted-printable\n");
1185 return ENC_QUOTED_PRINTABLE;
1187 debug_print("using 7bit\n");
1192 struct EncodingTable
1195 EncodingType enc_type;
1198 struct EncodingTable encoding_table[] = {
1201 {"binary", ENC_BINARY},
1202 {"quoted-printable", ENC_QUOTED_PRINTABLE},
1203 {"base64", ENC_BASE64},
1204 {"x-uuencode", ENC_UNKNOWN},
1205 {NULL, ENC_UNKNOWN},
1208 const gchar *procmime_get_encoding_str(EncodingType encoding)
1210 struct EncodingTable *enc_table;
1212 for (enc_table = encoding_table; enc_table->str != NULL; enc_table++) {
1213 if (enc_table->enc_type == encoding)
1214 return enc_table->str;
1219 /* --- NEW MIME STUFF --- */
1226 static struct TypeTable mime_type_table[] = {
1227 {"text", MIMETYPE_TEXT},
1228 {"image", MIMETYPE_IMAGE},
1229 {"audio", MIMETYPE_AUDIO},
1230 {"video", MIMETYPE_VIDEO},
1231 {"application", MIMETYPE_APPLICATION},
1232 {"message", MIMETYPE_MESSAGE},
1233 {"multipart", MIMETYPE_MULTIPART},
1237 const gchar *procmime_get_media_type_str(MimeMediaType type)
1239 struct TypeTable *type_table;
1241 for (type_table = mime_type_table; type_table->str != NULL; type_table++) {
1242 if (type_table->type == type)
1243 return type_table->str;
1248 MimeMediaType procmime_get_media_type(const gchar *str)
1250 struct TypeTable *typetablearray;
1252 for (typetablearray = mime_type_table; typetablearray->str != NULL; typetablearray++)
1253 if (g_ascii_strncasecmp(str, typetablearray->str, strlen(typetablearray->str)) == 0)
1254 return typetablearray->type;
1256 return MIMETYPE_UNKNOWN;
1260 *\brief Safe wrapper for content type string.
1262 *\return const gchar * Pointer to content type string.
1264 gchar *procmime_get_content_type_str(MimeMediaType type,
1265 const char *subtype)
1267 const gchar *type_str = NULL;
1269 if (subtype == NULL || !(type_str = procmime_get_media_type_str(type)))
1270 return g_strdup("unknown");
1271 return g_strdup_printf("%s/%s", type_str, subtype);
1274 static int procmime_parse_mimepart(MimeInfo *parent,
1275 gchar *content_type,
1276 gchar *content_encoding,
1277 gchar *content_description,
1279 gchar *content_disposition,
1280 gchar *content_location,
1281 const gchar *filename,
1285 void procmime_parse_message_rfc822(MimeInfo *mimeinfo)
1287 HeaderEntry hentry[] = {{"Content-Type:", NULL, TRUE},
1288 {"Content-Transfer-Encoding:",
1290 {"Content-Description:",
1294 {"Content-Disposition:",
1296 {"Content-Location:",
1300 {NULL, NULL, FALSE}};
1301 guint content_start, i;
1305 procmime_decode_content(mimeinfo);
1307 fp = g_fopen(mimeinfo->data.filename, "rb");
1309 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
1312 fseek(fp, mimeinfo->offset, SEEK_SET);
1313 procheader_get_header_fields(fp, hentry);
1314 if (hentry[0].body != NULL) {
1315 tmp = conv_unmime_header(hentry[0].body, NULL);
1316 g_free(hentry[0].body);
1317 hentry[0].body = tmp;
1319 if (hentry[2].body != NULL) {
1320 tmp = conv_unmime_header(hentry[2].body, NULL);
1321 g_free(hentry[2].body);
1322 hentry[2].body = tmp;
1324 if (hentry[4].body != NULL) {
1325 tmp = conv_unmime_header(hentry[4].body, NULL);
1326 g_free(hentry[4].body);
1327 hentry[4].body = tmp;
1329 if (hentry[5].body != NULL) {
1330 tmp = conv_unmime_header(hentry[5].body, NULL);
1331 g_free(hentry[5].body);
1332 hentry[5].body = tmp;
1334 content_start = ftell(fp);
1337 procmime_parse_mimepart(mimeinfo,
1338 hentry[0].body, hentry[1].body,
1339 hentry[2].body, hentry[3].body,
1340 hentry[4].body, hentry[5].body,
1341 mimeinfo->data.filename, content_start,
1342 mimeinfo->length - (content_start - mimeinfo->offset));
1344 for (i = 0; i < (sizeof hentry / sizeof hentry[0]); i++) {
1345 g_free(hentry[i].body);
1346 hentry[i].body = NULL;
1350 void procmime_parse_multipart(MimeInfo *mimeinfo)
1352 HeaderEntry hentry[] = {{"Content-Type:", NULL, TRUE},
1353 {"Content-Transfer-Encoding:",
1355 {"Content-Description:",
1359 {"Content-Disposition:",
1361 {"Content-Location:",
1363 {NULL, NULL, FALSE}};
1366 gint boundary_len = 0, lastoffset = -1, i;
1367 gchar buf[BUFFSIZE];
1371 boundary = g_hash_table_lookup(mimeinfo->typeparameters, "boundary");
1374 boundary_len = strlen(boundary);
1376 procmime_decode_content(mimeinfo);
1378 fp = g_fopen(mimeinfo->data.filename, "rb");
1380 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
1383 fseek(fp, mimeinfo->offset, SEEK_SET);
1384 while ((p = fgets(buf, sizeof(buf), fp)) != NULL && result == 0) {
1385 if (ftell(fp) > (mimeinfo->offset + mimeinfo->length))
1388 if (IS_BOUNDARY(buf, boundary, boundary_len)) {
1389 if (lastoffset != -1) {
1390 result = procmime_parse_mimepart(mimeinfo,
1391 hentry[0].body, hentry[1].body,
1392 hentry[2].body, hentry[3].body,
1393 hentry[4].body, hentry[5].body,
1394 mimeinfo->data.filename, lastoffset,
1395 (ftell(fp) - strlen(buf)) - lastoffset - 1);
1398 if (buf[2 + boundary_len] == '-' &&
1399 buf[2 + boundary_len + 1] == '-')
1402 for (i = 0; i < (sizeof hentry / sizeof hentry[0]) ; i++) {
1403 g_free(hentry[i].body);
1404 hentry[i].body = NULL;
1406 procheader_get_header_fields(fp, hentry);
1407 if (hentry[0].body != NULL) {
1408 tmp = conv_unmime_header(hentry[0].body, NULL);
1409 g_free(hentry[0].body);
1410 hentry[0].body = tmp;
1412 if (hentry[2].body != NULL) {
1413 tmp = conv_unmime_header(hentry[2].body, NULL);
1414 g_free(hentry[2].body);
1415 hentry[2].body = tmp;
1417 if (hentry[4].body != NULL) {
1418 tmp = conv_unmime_header(hentry[4].body, NULL);
1419 g_free(hentry[4].body);
1420 hentry[4].body = tmp;
1422 if (hentry[5].body != NULL) {
1423 tmp = conv_unmime_header(hentry[5].body, NULL);
1424 g_free(hentry[5].body);
1425 hentry[5].body = tmp;
1427 lastoffset = ftell(fp);
1430 for (i = 0; i < (sizeof hentry / sizeof hentry[0]); i++) {
1431 g_free(hentry[i].body);
1432 hentry[i].body = NULL;
1437 static void parse_parameters(const gchar *parameters, GHashTable *table)
1439 gchar *params, *param, *next;
1440 GSList *convlist = NULL, *concatlist = NULL, *cur;
1442 params = g_strdup(parameters);
1445 for (; next != NULL; param = next) {
1446 gchar *attribute, *value, *tmp;
1448 gboolean convert = FALSE;
1450 next = strchr_with_skip_quote(param, '"', ';');
1459 value = strchr(attribute, '=');
1465 while (value[0] == ' ')
1468 g_strdown(attribute);
1470 len = strlen(attribute);
1471 if (attribute[len - 1] == '*') {
1472 gchar *srcpos, *dstpos, *endpos;
1475 attribute[len - 1] = '\0';
1479 endpos = value + strlen(value);
1480 while (srcpos < endpos) {
1486 if (!get_hex_value(&dstvalue, srcpos[1], srcpos[2]))
1497 if (value[0] == '"')
1498 extract_quote(value, '"');
1499 else if ((tmp = strchr(value, ' ')) != NULL)
1504 while (attribute[0] == ' ')
1506 while (attribute[strlen(attribute)-1] == ' ')
1507 attribute[strlen(attribute)-1] = '\0';
1510 while (value[0] == ' ')
1512 while (value[strlen(value)-1] == ' ')
1513 value[strlen(value)-1] = '\0';
1515 if (strrchr(attribute, '*') != NULL) {
1518 tmpattr = g_strdup(attribute);
1519 tmp = strrchr(tmpattr, '*');
1522 if ((tmp[1] == '0') && (tmp[2] == '\0') &&
1523 (g_slist_find_custom(concatlist, attribute, g_str_equal) == NULL))
1524 concatlist = g_slist_prepend(concatlist, g_strdup(tmpattr));
1526 if (convert && (g_slist_find_custom(convlist, attribute, g_str_equal) == NULL))
1527 convlist = g_slist_prepend(convlist, g_strdup(tmpattr));
1530 } else if (convert) {
1531 if (g_slist_find_custom(convlist, attribute, g_str_equal) == NULL)
1532 convlist = g_slist_prepend(convlist, g_strdup(attribute));
1535 if (g_hash_table_lookup(table, attribute) == NULL)
1536 g_hash_table_insert(table, g_strdup(attribute), g_strdup(value));
1539 for (cur = concatlist; cur != NULL; cur = g_slist_next(cur)) {
1540 gchar *attribute, *attrwnum, *partvalue;
1544 attribute = (gchar *) cur->data;
1545 value = g_string_sized_new(64);
1547 attrwnum = g_strdup_printf("%s*%d", attribute, n);
1548 while ((partvalue = g_hash_table_lookup(table, attrwnum)) != NULL) {
1549 g_string_append(value, partvalue);
1553 attrwnum = g_strdup_printf("%s*%d", attribute, n);
1557 g_hash_table_insert(table, g_strdup(attribute), g_strdup(value->str));
1558 g_string_free(value, TRUE);
1560 slist_free_strings(concatlist);
1561 g_slist_free(concatlist);
1563 for (cur = convlist; cur != NULL; cur = g_slist_next(cur)) {
1564 gchar *attribute, *key, *value;
1565 gchar *charset, *lang, *oldvalue, *newvalue;
1567 attribute = (gchar *) cur->data;
1568 if (!g_hash_table_lookup_extended(
1569 table, attribute, (gpointer *)(gchar *) &key, (gpointer *)(gchar *) &value))
1573 lang = strchr(charset, '\'');
1578 oldvalue = strchr(lang, '\'');
1579 if (oldvalue == NULL)
1584 newvalue = conv_codeset_strdup(oldvalue, charset, CS_UTF_8);
1586 g_hash_table_remove(table, attribute);
1590 g_hash_table_insert(table, g_strdup(attribute), newvalue);
1592 slist_free_strings(convlist);
1593 g_slist_free(convlist);
1598 static void procmime_parse_content_type(const gchar *content_type, MimeInfo *mimeinfo)
1600 g_return_if_fail(content_type != NULL);
1601 g_return_if_fail(mimeinfo != NULL);
1603 /* RFC 2045, page 13 says that the mime subtype is MANDATORY;
1604 * if it's not available we use the default Content-Type */
1605 if ((content_type[0] == '\0') || (strchr(content_type, '/') == NULL)) {
1606 mimeinfo->type = MIMETYPE_TEXT;
1607 mimeinfo->subtype = g_strdup("plain");
1608 if (g_hash_table_lookup(mimeinfo->typeparameters,
1609 "charset") == NULL) {
1610 g_hash_table_insert(mimeinfo->typeparameters,
1611 g_strdup("charset"),
1613 conv_get_locale_charset_str_no_utf8()));
1616 gchar *type, *subtype, *params;
1618 type = g_strdup(content_type);
1619 subtype = strchr(type, '/') + 1;
1620 *(subtype - 1) = '\0';
1621 if ((params = strchr(subtype, ';')) != NULL) {
1626 mimeinfo->type = procmime_get_media_type(type);
1627 mimeinfo->subtype = g_strdup(subtype);
1629 /* Get mimeinfo->typeparameters */
1631 parse_parameters(params, mimeinfo->typeparameters);
1637 static void procmime_parse_content_disposition(const gchar *content_disposition, MimeInfo *mimeinfo)
1639 gchar *tmp, *params;
1641 g_return_if_fail(content_disposition != NULL);
1642 g_return_if_fail(mimeinfo != NULL);
1644 tmp = g_strdup(content_disposition);
1645 if ((params = strchr(tmp, ';')) != NULL) {
1651 if (!g_ascii_strcasecmp(tmp, "inline"))
1652 mimeinfo->disposition = DISPOSITIONTYPE_INLINE;
1653 else if (!g_ascii_strcasecmp(tmp, "attachment"))
1654 mimeinfo->disposition = DISPOSITIONTYPE_ATTACHMENT;
1656 mimeinfo->disposition = DISPOSITIONTYPE_ATTACHMENT;
1659 parse_parameters(params, mimeinfo->dispositionparameters);
1665 static void procmime_parse_content_encoding(const gchar *content_encoding, MimeInfo *mimeinfo)
1667 struct EncodingTable *enc_table;
1669 for (enc_table = encoding_table; enc_table->str != NULL; enc_table++) {
1670 if (g_ascii_strcasecmp(enc_table->str, content_encoding) == 0) {
1671 mimeinfo->encoding_type = enc_table->enc_type;
1675 mimeinfo->encoding_type = ENC_UNKNOWN;
1679 static int procmime_parse_mimepart(MimeInfo *parent,
1680 gchar *content_type,
1681 gchar *content_encoding,
1682 gchar *content_description,
1684 gchar *content_disposition,
1685 gchar *content_location,
1686 const gchar *filename,
1692 /* Create MimeInfo */
1693 mimeinfo = procmime_mimeinfo_new();
1694 mimeinfo->content = MIMECONTENT_FILE;
1695 if (parent != NULL) {
1696 if (g_node_depth(parent->node) > 32) {
1697 /* 32 is an arbitrary value
1698 * this avoids DOSsing ourselves
1699 * with enormous messages
1701 procmime_mimeinfo_free_all(mimeinfo);
1704 g_node_append(parent->node, mimeinfo->node);
1706 mimeinfo->data.filename = g_strdup(filename);
1707 mimeinfo->offset = offset;
1708 mimeinfo->length = length;
1710 if (content_type != NULL) {
1711 procmime_parse_content_type(content_type, mimeinfo);
1713 mimeinfo->type = MIMETYPE_TEXT;
1714 mimeinfo->subtype = g_strdup("plain");
1715 if (g_hash_table_lookup(mimeinfo->typeparameters,
1716 "charset") == NULL) {
1717 g_hash_table_insert(mimeinfo->typeparameters,
1718 g_strdup("charset"),
1720 conv_get_locale_charset_str_no_utf8()));
1724 if (content_encoding != NULL) {
1725 procmime_parse_content_encoding(content_encoding, mimeinfo);
1727 mimeinfo->encoding_type = ENC_UNKNOWN;
1730 if (content_description != NULL)
1731 mimeinfo->description = g_strdup(content_description);
1733 mimeinfo->description = NULL;
1735 if (content_id != NULL)
1736 mimeinfo->id = g_strdup(content_id);
1738 mimeinfo->id = NULL;
1740 if (content_location != NULL)
1741 mimeinfo->location = g_strdup(content_location);
1743 mimeinfo->location = NULL;
1745 if (content_disposition != NULL)
1746 procmime_parse_content_disposition(content_disposition, mimeinfo);
1748 mimeinfo->disposition = DISPOSITIONTYPE_UNKNOWN;
1750 /* Call parser for mime type */
1751 switch (mimeinfo->type) {
1752 case MIMETYPE_MESSAGE:
1753 if (g_ascii_strcasecmp(mimeinfo->subtype, "rfc822") == 0) {
1754 procmime_parse_message_rfc822(mimeinfo);
1758 case MIMETYPE_MULTIPART:
1759 procmime_parse_multipart(mimeinfo);
1769 static gchar *typenames[] = {
1780 static gboolean output_func(GNode *node, gpointer data)
1783 MimeInfo *mimeinfo = (MimeInfo *) node->data;
1785 depth = g_node_depth(node);
1786 for (i = 0; i < depth; i++)
1788 printf("%s/%s (offset:%d length:%d encoding: %d)\n", typenames[mimeinfo->type], mimeinfo->subtype, mimeinfo->offset, mimeinfo->length, mimeinfo->encoding_type);
1793 static void output_mime_structure(MimeInfo *mimeinfo, int indent)
1795 g_node_traverse(mimeinfo->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1, output_func, NULL);
1798 MimeInfo *procmime_scan_file_with_offset(const gchar *filename, int offset)
1803 stat(filename, &buf);
1805 mimeinfo = procmime_mimeinfo_new();
1806 mimeinfo->content = MIMECONTENT_FILE;
1807 mimeinfo->encoding_type = ENC_UNKNOWN;
1808 mimeinfo->type = MIMETYPE_MESSAGE;
1809 mimeinfo->subtype = g_strdup("rfc822");
1810 mimeinfo->data.filename = g_strdup(filename);
1811 mimeinfo->offset = offset;
1812 mimeinfo->length = buf.st_size - offset;
1814 procmime_parse_message_rfc822(mimeinfo);
1815 if (debug_get_mode())
1816 output_mime_structure(mimeinfo, 0);
1821 MimeInfo *procmime_scan_file(const gchar *filename)
1825 g_return_val_if_fail(filename != NULL, NULL);
1827 mimeinfo = procmime_scan_file_with_offset(filename, 0);
1832 MimeInfo *procmime_scan_queue_file(const gchar *filename)
1836 gchar buf[BUFFSIZE];
1839 g_return_val_if_fail(filename != NULL, NULL);
1842 if ((fp = g_fopen(filename, "rb")) == NULL)
1844 /* Skip queue header */
1845 while (fgets(buf, sizeof(buf), fp) != NULL)
1846 if (buf[0] == '\r' || buf[0] == '\n') break;
1850 mimeinfo = procmime_scan_file_with_offset(filename, offset);
1857 ENC_AS_QUOTED_STRING,
1862 typedef struct _ParametersData {
1868 static void write_parameters(gpointer key, gpointer value, gpointer user_data)
1871 gchar *val = value, *valpos, *tmp;
1872 ParametersData *pdata = (ParametersData *)user_data;
1873 GString *buf = g_string_new("");
1875 EncodeAs encas = ENC_AS_TOKEN;
1877 for (valpos = val; *valpos != 0; valpos++) {
1878 if (!IS_ASCII(*valpos) || *valpos == '"') {
1879 encas = ENC_AS_EXTENDED;
1884 if (((*valpos >= 0) && (*valpos < 037)) || (*valpos == 0177)) {
1885 encas = ENC_AS_QUOTED_STRING;
1889 /* tspecials + SPACE */
1907 encas = ENC_AS_QUOTED_STRING;
1912 if (encas == ENC_AS_EXTENDED && pdata->ascii_only == TRUE)
1913 encas = ENC_TO_ASCII;
1917 g_string_append_printf(buf, "%s=%s", param, val);
1921 tmp = g_strdup(val);
1923 " ()<>@,';:\\/[]?=.0123456789"
1924 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1925 "abcdefghijklmnopqrstuvwxyz",
1927 g_string_append_printf(buf, "%s=\"%s\"", param, tmp);
1931 case ENC_AS_QUOTED_STRING:
1932 g_string_append_printf(buf, "%s=\"%s\"", param, val);
1935 case ENC_AS_EXTENDED:
1936 if (!g_utf8_validate(val, -1, NULL))
1937 g_string_append_printf(buf, "%s*=%s''", param,
1938 conv_get_locale_charset_str());
1940 g_string_append_printf(buf, "%s*=%s''", param,
1942 for (valpos = val; *valpos != '\0'; valpos++) {
1943 if (IS_ASCII(*valpos) && isalnum(*valpos)) {
1944 g_string_append_printf(buf, "%c", *valpos);
1946 gchar hexstr[3] = "XX";
1947 get_hex_str(hexstr, *valpos);
1948 g_string_append_printf(buf, "%%%s", hexstr);
1954 if (buf->str && strlen(buf->str)) {
1955 if (pdata->len + strlen(buf->str) + 2 > 76) {
1956 fprintf(pdata->fp, ";\n %s", buf->str);
1957 pdata->len = strlen(buf->str) + 1;
1959 fprintf(pdata->fp, "; %s", buf->str);
1960 pdata->len += strlen(buf->str) + 2;
1963 g_string_free(buf, TRUE);
1966 void procmime_write_mime_header(MimeInfo *mimeinfo, FILE *fp)
1968 struct TypeTable *type_table;
1969 ParametersData *pdata = g_new0(ParametersData, 1);
1970 debug_print("procmime_write_mime_header\n");
1973 pdata->ascii_only = FALSE;
1975 for (type_table = mime_type_table; type_table->str != NULL; type_table++)
1976 if (mimeinfo->type == type_table->type) {
1977 gchar *buf = g_strdup_printf(
1978 "Content-Type: %s/%s", type_table->str, mimeinfo->subtype);
1979 fprintf(fp, "%s", buf);
1980 pdata->len = strlen(buf);
1981 pdata->ascii_only = TRUE;
1985 g_hash_table_foreach(mimeinfo->typeparameters, write_parameters, pdata);
1990 if (mimeinfo->encoding_type != ENC_UNKNOWN)
1991 fprintf(fp, "Content-Transfer-Encoding: %s\n", procmime_get_encoding_str(mimeinfo->encoding_type));
1993 if (mimeinfo->description != NULL)
1994 fprintf(fp, "Content-Description: %s\n", mimeinfo->description);
1996 if (mimeinfo->id != NULL)
1997 fprintf(fp, "Content-ID: %s\n", mimeinfo->id);
1999 if (mimeinfo->location != NULL)
2000 fprintf(fp, "Content-Location: %s\n", mimeinfo->location);
2002 if (mimeinfo->disposition != DISPOSITIONTYPE_UNKNOWN) {
2003 ParametersData *pdata = g_new0(ParametersData, 1);
2005 if (mimeinfo->disposition == DISPOSITIONTYPE_INLINE)
2006 buf = g_strdup("Content-Disposition: inline");
2007 else if (mimeinfo->disposition == DISPOSITIONTYPE_ATTACHMENT)
2008 buf = g_strdup("Content-Disposition: attachment");
2010 buf = g_strdup("Content-Disposition: unknown");
2012 fprintf(fp, "%s", buf);
2013 pdata->len = strlen(buf);
2017 pdata->ascii_only = FALSE;
2019 g_hash_table_foreach(mimeinfo->dispositionparameters, write_parameters, pdata);
2027 gint procmime_write_message_rfc822(MimeInfo *mimeinfo, FILE *fp)
2032 gchar buf[BUFFSIZE];
2033 gboolean skip = FALSE;;
2035 debug_print("procmime_write_message_rfc822\n");
2038 switch (mimeinfo->content) {
2039 case MIMECONTENT_FILE:
2040 if ((infp = g_fopen(mimeinfo->data.filename, "rb")) == NULL) {
2041 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
2044 fseek(infp, mimeinfo->offset, SEEK_SET);
2045 while (fgets(buf, sizeof(buf), infp) == buf) {
2046 if (buf[0] == '\n' && buf[1] == '\0')
2048 if (skip && (buf[0] == ' ' || buf[0] == '\t'))
2050 if (g_ascii_strncasecmp(buf, "Mime-Version:", 13) == 0 ||
2051 g_ascii_strncasecmp(buf, "Content-Type:", 13) == 0 ||
2052 g_ascii_strncasecmp(buf, "Content-Transfer-Encoding:", 26) == 0 ||
2053 g_ascii_strncasecmp(buf, "Content-Description:", 20) == 0 ||
2054 g_ascii_strncasecmp(buf, "Content-ID:", 11) == 0 ||
2055 g_ascii_strncasecmp(buf, "Content-Location:", 17) == 0 ||
2056 g_ascii_strncasecmp(buf, "Content-Disposition:", 20) == 0) {
2060 fwrite(buf, sizeof(gchar), strlen(buf), fp);
2066 case MIMECONTENT_MEM:
2067 fwrite(mimeinfo->data.mem,
2069 strlen(mimeinfo->data.mem),
2077 childnode = mimeinfo->node->children;
2078 if (childnode == NULL)
2081 child = (MimeInfo *) childnode->data;
2082 fprintf(fp, "Mime-Version: 1.0\n");
2083 procmime_write_mime_header(child, fp);
2084 return procmime_write_mimeinfo(child, fp);
2087 gint procmime_write_multipart(MimeInfo *mimeinfo, FILE *fp)
2091 gchar *boundary, *str, *str2;
2092 gchar buf[BUFFSIZE];
2093 gboolean firstboundary;
2095 debug_print("procmime_write_multipart\n");
2097 boundary = g_hash_table_lookup(mimeinfo->typeparameters, "boundary");
2099 switch (mimeinfo->content) {
2100 case MIMECONTENT_FILE:
2101 if ((infp = g_fopen(mimeinfo->data.filename, "rb")) == NULL) {
2102 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
2105 fseek(infp, mimeinfo->offset, SEEK_SET);
2106 while (fgets(buf, sizeof(buf), infp) == buf) {
2107 if (IS_BOUNDARY(buf, boundary, strlen(boundary)))
2109 fwrite(buf, sizeof(gchar), strlen(buf), fp);
2114 case MIMECONTENT_MEM:
2115 str = g_strdup(mimeinfo->data.mem);
2116 if (((str2 = strstr(str, boundary)) != NULL) && ((str2 - str) >= 2) &&
2117 (*(str2 - 1) == '-') && (*(str2 - 2) == '-'))
2119 fwrite(str, sizeof(gchar), strlen(str), fp);
2127 childnode = mimeinfo->node->children;
2128 firstboundary = TRUE;
2129 while (childnode != NULL) {
2130 MimeInfo *child = childnode->data;
2133 firstboundary = FALSE;
2136 fprintf(fp, "--%s\n", boundary);
2138 procmime_write_mime_header(child, fp);
2139 if (procmime_write_mimeinfo(child, fp) < 0)
2142 childnode = g_node_next_sibling(childnode);
2144 fprintf(fp, "\n--%s--\n", boundary);
2149 gint procmime_write_mimeinfo(MimeInfo *mimeinfo, FILE *fp)
2153 debug_print("procmime_write_mimeinfo\n");
2155 if (G_NODE_IS_LEAF(mimeinfo->node)) {
2156 switch (mimeinfo->content) {
2157 case MIMECONTENT_FILE:
2158 if ((infp = g_fopen(mimeinfo->data.filename, "rb")) == NULL) {
2159 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
2162 copy_file_part_to_fp(infp, mimeinfo->offset, mimeinfo->length, fp);
2166 case MIMECONTENT_MEM:
2167 fwrite(mimeinfo->data.mem,
2169 strlen(mimeinfo->data.mem),
2177 /* Call writer for mime type */
2178 switch (mimeinfo->type) {
2179 case MIMETYPE_MESSAGE:
2180 if (g_ascii_strcasecmp(mimeinfo->subtype, "rfc822") == 0) {
2181 return procmime_write_message_rfc822(mimeinfo, fp);
2185 case MIMETYPE_MULTIPART:
2186 return procmime_write_multipart(mimeinfo, fp);
2198 gchar *procmime_get_part_file_name(MimeInfo *mimeinfo)
2202 if ((mimeinfo->type == MIMETYPE_TEXT) && !g_ascii_strcasecmp(mimeinfo->subtype, "html"))
2203 base = g_strdup("mimetmp.html");
2205 const gchar *basetmp;
2208 basetmp = procmime_mimeinfo_get_parameter(mimeinfo, "filename");
2209 if (basetmp == NULL)
2210 basetmp = procmime_mimeinfo_get_parameter(mimeinfo, "name");
2211 if (basetmp == NULL)
2212 basetmp = "mimetmp";
2213 basename = g_path_get_basename(basetmp);
2214 if (*basename == '\0') {
2216 basename = g_strdup("mimetmp");
2218 base = conv_filename_from_utf8(basename);
2220 subst_for_shellsafe_filename(base);