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 g_free(mimeinfo->data.filename);
350 mimeinfo->data.filename = tmpfilename;
351 mimeinfo->tmp = TRUE;
352 mimeinfo->offset = 0;
353 mimeinfo->length = statbuf.st_size;
354 mimeinfo->encoding_type = ENC_BINARY;
359 #define B64_LINE_SIZE 57
360 #define B64_BUFFSIZE 77
362 gboolean procmime_encode_content(MimeInfo *mimeinfo, EncodingType encoding)
364 FILE *infp = NULL, *outfp;
369 if (mimeinfo->content == MIMECONTENT_EMPTY)
372 if (mimeinfo->encoding_type != ENC_UNKNOWN &&
373 mimeinfo->encoding_type != ENC_BINARY &&
374 mimeinfo->encoding_type != ENC_7BIT &&
375 mimeinfo->encoding_type != ENC_8BIT)
376 if(!procmime_decode_content(mimeinfo))
379 outfp = get_tmpfile_in_dir(get_mime_tmp_dir(), &tmpfilename);
385 if (mimeinfo->content == MIMECONTENT_FILE) {
386 if ((infp = g_fopen(mimeinfo->data.filename, "rb")) == NULL) {
387 g_warning("Can't open file %s\n", mimeinfo->data.filename);
390 } else if (mimeinfo->content == MIMECONTENT_MEM) {
391 infp = str_open_as_stream(mimeinfo->data.mem);
396 if (encoding == ENC_BASE64) {
397 gchar inbuf[B64_LINE_SIZE], outbuf[B64_BUFFSIZE];
399 gchar *tmp_file = NULL;
401 if (mimeinfo->type == MIMETYPE_TEXT ||
402 mimeinfo->type == MIMETYPE_MESSAGE) {
403 if (mimeinfo->content == MIMECONTENT_FILE) {
404 tmp_file = get_tmp_file();
405 if (canonicalize_file(mimeinfo->data.filename, tmp_file) < 0) {
410 if ((tmp_fp = g_fopen(tmp_file, "rb")) == NULL) {
411 FILE_OP_ERROR(tmp_file, "fopen");
418 gchar *out = canonicalize_str(mimeinfo->data.mem);
420 infp = str_open_as_stream(out);
428 while ((len = fread(inbuf, sizeof(gchar),
429 B64_LINE_SIZE, tmp_fp))
431 base64_encode(outbuf, inbuf, B64_LINE_SIZE);
432 fputs(outbuf, outfp);
435 if (len > 0 && feof(tmp_fp)) {
436 base64_encode(outbuf, inbuf, len);
437 fputs(outbuf, outfp);
446 } else if (encoding == ENC_QUOTED_PRINTABLE) {
447 gchar inbuf[BUFFSIZE], outbuf[BUFFSIZE * 4];
449 while (fgets(inbuf, sizeof(inbuf), infp) != NULL) {
450 qp_encode_line(outbuf, inbuf);
452 if (!strncmp("From ", outbuf, sizeof("From ")-1)) {
453 gchar *tmpbuf = outbuf;
455 tmpbuf += sizeof("From ")-1;
457 fputs("=46rom ", outfp);
458 fputs(tmpbuf, outfp);
460 fputs(outbuf, outfp);
465 while (fgets(buf, sizeof(buf), infp) != NULL) {
474 if (mimeinfo->content == MIMECONTENT_FILE) {
475 if (mimeinfo->tmp && (mimeinfo->data.filename != NULL))
476 g_unlink(mimeinfo->data.filename);
477 g_free(mimeinfo->data.filename);
478 } else if (mimeinfo->content == MIMECONTENT_MEM) {
479 if (mimeinfo->tmp && (mimeinfo->data.mem != NULL))
480 g_free(mimeinfo->data.mem);
483 stat(tmpfilename, &statbuf);
484 mimeinfo->content = MIMECONTENT_FILE;
485 mimeinfo->data.filename = tmpfilename;
486 mimeinfo->tmp = TRUE;
487 mimeinfo->offset = 0;
488 mimeinfo->length = statbuf.st_size;
489 mimeinfo->encoding_type = encoding;
494 gint procmime_get_part(const gchar *outfile, MimeInfo *mimeinfo)
498 gint restlength, readlength;
500 g_return_val_if_fail(outfile != NULL, -1);
501 g_return_val_if_fail(mimeinfo != NULL, -1);
503 if (mimeinfo->encoding_type != ENC_BINARY && !procmime_decode_content(mimeinfo))
506 if ((infp = g_fopen(mimeinfo->data.filename, "rb")) == NULL) {
507 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
510 if (fseek(infp, mimeinfo->offset, SEEK_SET) < 0) {
511 FILE_OP_ERROR(mimeinfo->data.filename, "fseek");
515 if ((outfp = g_fopen(outfile, "wb")) == NULL) {
516 FILE_OP_ERROR(outfile, "fopen");
521 restlength = mimeinfo->length;
523 while ((restlength > 0) && ((readlength = fread(buf, 1, restlength > BUFFSIZE ? BUFFSIZE : restlength, infp)) > 0)) {
524 fwrite(buf, 1, readlength, outfp);
525 restlength -= readlength;
529 if (fclose(outfp) == EOF) {
530 FILE_OP_ERROR(outfile, "fclose");
538 struct ContentRenderer {
543 static GList * renderer_list = NULL;
545 static struct ContentRenderer *
546 content_renderer_new(char * content_type, char * renderer)
548 struct ContentRenderer * cr;
550 cr = g_new(struct ContentRenderer, 1);
554 cr->content_type = g_strdup(content_type);
555 cr->renderer = g_strdup(renderer);
560 static void content_renderer_free(struct ContentRenderer * cr)
562 g_free(cr->content_type);
563 g_free(cr->renderer);
567 void renderer_read_config(void)
573 g_list_foreach(renderer_list, (GFunc) content_renderer_free, NULL);
574 renderer_list = NULL;
576 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, RENDERER_RC, NULL);
577 f = g_fopen(rcpath, "rb");
583 while (fgets(buf, BUFFSIZE, f)) {
585 struct ContentRenderer * cr;
588 p = strchr(buf, ' ');
593 cr = content_renderer_new(buf, p + 1);
597 renderer_list = g_list_append(renderer_list, cr);
603 void renderer_write_config(void)
609 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, RENDERER_RC, NULL);
611 if ((pfile = prefs_write_open(rcpath)) == NULL) {
612 g_warning("failed to write configuration to file\n");
619 for (cur = renderer_list ; cur != NULL ; cur = cur->next) {
620 struct ContentRenderer * renderer;
621 renderer = cur->data;
622 fprintf(pfile->fp, "%s %s\n", renderer->content_type,
626 if (prefs_file_close(pfile) < 0) {
627 g_warning("failed to write configuration to file\n");
632 FILE *procmime_get_text_content(MimeInfo *mimeinfo)
635 const gchar *src_codeset;
636 gboolean conv_fail = FALSE;
639 struct ContentRenderer * renderer;
641 gchar *tmpfile, *content_type;
643 g_return_val_if_fail(mimeinfo != NULL, NULL);
645 if (!procmime_decode_content(mimeinfo))
648 tmpfile = procmime_get_tmp_file_name(mimeinfo);
652 if (procmime_get_part(tmpfile, mimeinfo) < 0) {
657 tmpfp = g_fopen(tmpfile, "rb");
663 if ((outfp = my_tmpfile()) == NULL) {
670 src_codeset = forced_charset
672 procmime_mimeinfo_get_parameter(mimeinfo, "charset");
676 content_type = procmime_get_content_type_str(mimeinfo->type,
678 for (cur = renderer_list ; cur != NULL ; cur = cur->next) {
679 struct ContentRenderer * cr;
682 if (g_ascii_strcasecmp(cr->content_type, content_type) == 0) {
687 g_free(content_type);
689 if (renderer != NULL) {
695 dup2(fileno(outfp), 1);
697 p = popen(renderer->renderer, "w");
702 fread(buf, sizeof(char), sizeof(buf),
704 fwrite(buf, sizeof(char), count, p);
709 /* CodeConverter seems to have no effect here */
710 } else if (mimeinfo->type == MIMETYPE_TEXT && !g_ascii_strcasecmp(mimeinfo->subtype, "html")) {
711 SC_HTMLParser *parser;
714 conv = conv_code_converter_new(src_codeset);
715 parser = sc_html_parser_new(tmpfp, conv);
716 while ((str = sc_html_parse(parser)) != NULL) {
719 sc_html_parser_destroy(parser);
720 conv_code_converter_destroy(conv);
721 } else if (mimeinfo->type == MIMETYPE_TEXT && !g_ascii_strcasecmp(mimeinfo->subtype, "enriched")) {
725 conv = conv_code_converter_new(src_codeset);
726 parser = ertf_parser_new(tmpfp, conv);
727 while ((str = ertf_parse(parser)) != NULL) {
730 ertf_parser_destroy(parser);
731 conv_code_converter_destroy(conv);
732 } else if (mimeinfo->type == MIMETYPE_TEXT) {
733 while (fgets(buf, sizeof(buf), tmpfp) != NULL) {
734 str = conv_codeset_strdup(buf, src_codeset, CS_UTF_8);
746 g_warning("procmime_get_text_content(): Code conversion failed.\n");
756 /* search the first text part of (multipart) MIME message,
757 decode, convert it and output to outfp. */
758 FILE *procmime_get_first_text_content(MsgInfo *msginfo)
761 MimeInfo *mimeinfo, *partinfo;
763 g_return_val_if_fail(msginfo != NULL, NULL);
765 mimeinfo = procmime_scan_message(msginfo);
766 if (!mimeinfo) return NULL;
769 while (partinfo && partinfo->type != MIMETYPE_TEXT) {
770 partinfo = procmime_mimeinfo_next(partinfo);
773 outfp = procmime_get_text_content(partinfo);
775 procmime_mimeinfo_free_all(mimeinfo);
781 static gboolean find_encrypted_func(GNode *node, gpointer data)
783 MimeInfo *mimeinfo = (MimeInfo *) node->data;
784 MimeInfo **encinfo = (MimeInfo **) data;
786 if (privacy_mimeinfo_is_encrypted(mimeinfo)) {
794 static MimeInfo *find_encrypted_part(MimeInfo *rootinfo)
796 MimeInfo *encinfo = NULL;
798 g_node_traverse(rootinfo->node, G_IN_ORDER, G_TRAVERSE_ALL, -1,
799 find_encrypted_func, &encinfo);
804 /* search the first encrypted text part of (multipart) MIME message,
805 decode, convert it and output to outfp. */
806 FILE *procmime_get_first_encrypted_text_content(MsgInfo *msginfo)
809 MimeInfo *mimeinfo, *partinfo, *encinfo;
811 g_return_val_if_fail(msginfo != NULL, NULL);
813 mimeinfo = procmime_scan_message(msginfo);
819 if ((encinfo = find_encrypted_part(partinfo)) != NULL) {
820 debug_print("decrypting message part\n");
821 if (privacy_mimeinfo_decrypt(encinfo) < 0)
825 while (partinfo && partinfo->type != MIMETYPE_TEXT) {
826 partinfo = procmime_mimeinfo_next(partinfo);
830 outfp = procmime_get_text_content(partinfo);
832 procmime_mimeinfo_free_all(mimeinfo);
837 gboolean procmime_msginfo_is_encrypted(MsgInfo *msginfo)
839 MimeInfo *mimeinfo, *partinfo;
840 gboolean result = FALSE;
842 g_return_val_if_fail(msginfo != NULL, FALSE);
844 mimeinfo = procmime_scan_message(msginfo);
850 result = (find_encrypted_part(partinfo) != NULL);
851 procmime_mimeinfo_free_all(mimeinfo);
856 gboolean procmime_find_string_part(MimeInfo *mimeinfo, const gchar *filename,
857 const gchar *str, StrFindFunc find_func)
862 g_return_val_if_fail(mimeinfo != NULL, FALSE);
863 g_return_val_if_fail(mimeinfo->type == MIMETYPE_TEXT, FALSE);
864 g_return_val_if_fail(str != NULL, FALSE);
865 g_return_val_if_fail(find_func != NULL, FALSE);
867 outfp = procmime_get_text_content(mimeinfo);
872 while (fgets(buf, sizeof(buf), outfp) != NULL) {
874 if (find_func(buf, str)) {
885 gboolean procmime_find_string(MsgInfo *msginfo, const gchar *str,
886 StrFindFunc find_func)
891 gboolean found = FALSE;
893 g_return_val_if_fail(msginfo != NULL, FALSE);
894 g_return_val_if_fail(str != NULL, FALSE);
895 g_return_val_if_fail(find_func != NULL, FALSE);
897 filename = procmsg_get_message_file(msginfo);
898 if (!filename) return FALSE;
899 mimeinfo = procmime_scan_message(msginfo);
901 for (partinfo = mimeinfo; partinfo != NULL;
902 partinfo = procmime_mimeinfo_next(partinfo)) {
903 if (partinfo->type == MIMETYPE_TEXT) {
904 if (procmime_find_string_part
905 (partinfo, filename, str, find_func) == TRUE) {
912 procmime_mimeinfo_free_all(mimeinfo);
918 gchar *procmime_get_tmp_file_name(MimeInfo *mimeinfo)
920 static guint32 id = 0;
925 g_return_val_if_fail(mimeinfo != NULL, NULL);
927 g_snprintf(f_prefix, sizeof(f_prefix), "%08x.", id++);
929 if ((mimeinfo->type == MIMETYPE_TEXT) && !g_ascii_strcasecmp(mimeinfo->subtype, "html"))
930 base = g_strdup("mimetmp.html");
932 const gchar *basetmp;
934 basetmp = procmime_mimeinfo_get_parameter(mimeinfo, "filename");
936 basetmp = procmime_mimeinfo_get_parameter(mimeinfo, "name");
939 basetmp = g_path_get_basename(basetmp);
940 if (*basetmp == '\0')
941 basetmp = g_strdup("mimetmp");
942 base = conv_filename_from_utf8(basetmp);
943 g_free((gchar*)basetmp);
944 subst_for_shellsafe_filename(base);
947 filename = g_strconcat(get_mime_tmp_dir(), G_DIR_SEPARATOR_S,
948 f_prefix, base, NULL);
955 static GList *mime_type_list = NULL;
957 gchar *procmime_get_mime_type(const gchar *filename)
959 static GHashTable *mime_type_table = NULL;
965 if (!mime_type_table) {
966 mime_type_table = procmime_get_mime_type_table();
967 if (!mime_type_table) return NULL;
970 base = g_path_get_basename(filename);
971 if ((p = strrchr(base, '.')) != NULL)
972 Xstrdup_a(ext, p + 1, p = NULL );
977 mime_type = g_hash_table_lookup(mime_type_table, ext);
981 str = g_strconcat(mime_type->type, "/", mime_type->sub_type,
989 static guint procmime_str_hash(gconstpointer gptr)
991 guint hash_result = 0;
994 for (str = gptr; str && *str; str++) {
995 if (isupper(*str)) hash_result += (*str + ' ');
996 else hash_result += *str;
1002 static gint procmime_str_equal(gconstpointer gptr1, gconstpointer gptr2)
1004 const char *str1 = gptr1;
1005 const char *str2 = gptr2;
1007 return !g_utf8_collate(str1, str2);
1010 static GHashTable *procmime_get_mime_type_table(void)
1012 GHashTable *table = NULL;
1014 MimeType *mime_type;
1017 if (!mime_type_list) {
1018 mime_type_list = procmime_get_mime_type_list();
1019 if (!mime_type_list) return NULL;
1022 table = g_hash_table_new(procmime_str_hash, procmime_str_equal);
1024 for (cur = mime_type_list; cur != NULL; cur = cur->next) {
1028 mime_type = (MimeType *)cur->data;
1030 if (!mime_type->extension) continue;
1032 exts = g_strsplit(mime_type->extension, " ", 16);
1033 for (i = 0; exts[i] != NULL; i++) {
1034 /* make the key case insensitive */
1036 /* use previously dup'd key on overwriting */
1037 if (g_hash_table_lookup(table, exts[i]))
1040 key = g_strdup(exts[i]);
1041 g_hash_table_insert(table, key, mime_type);
1049 GList *procmime_get_mime_type_list(void)
1053 gchar buf[BUFFSIZE];
1056 MimeType *mime_type;
1057 gboolean fp_is_glob_file = TRUE;
1060 return mime_type_list;
1062 if ((fp = g_fopen("/usr/share/mime/globs", "rb")) == NULL) {
1063 fp_is_glob_file = FALSE;
1064 if ((fp = g_fopen("/etc/mime.types", "rb")) == NULL) {
1065 if ((fp = g_fopen(SYSCONFDIR "/mime.types", "rb"))
1067 FILE_OP_ERROR(SYSCONFDIR "/mime.types",
1074 while (fgets(buf, sizeof(buf), fp) != NULL) {
1075 p = strchr(buf, '#');
1081 if (fp_is_glob_file) {
1082 while (*p && !g_ascii_isspace(*p) && (*p!=':')) p++;
1084 while (*p && !g_ascii_isspace(*p)) p++;
1091 delim = strchr(buf, '/');
1092 if (delim == NULL) continue;
1095 mime_type = g_new(MimeType, 1);
1096 mime_type->type = g_strdup(buf);
1097 mime_type->sub_type = g_strdup(delim + 1);
1099 if (fp_is_glob_file) {
1100 while (*p && (g_ascii_isspace(*p)||(*p=='*')||(*p=='.'))) p++;
1102 while (*p && g_ascii_isspace(*p)) p++;
1106 mime_type->extension = g_strdup(p);
1108 mime_type->extension = NULL;
1110 list = g_list_append(list, mime_type);
1116 g_warning("Can't read mime.types\n");
1121 EncodingType procmime_get_encoding_for_charset(const gchar *charset)
1125 else if (!g_ascii_strncasecmp(charset, "ISO-2022-", 9) ||
1126 !g_ascii_strcasecmp(charset, "US-ASCII"))
1128 else if (!g_ascii_strcasecmp(charset, "ISO-8859-5") ||
1129 !g_ascii_strncasecmp(charset, "KOI8-", 5) ||
1130 !g_ascii_strcasecmp(charset, "Windows-1251"))
1132 else if (!g_ascii_strncasecmp(charset, "ISO-8859-", 9))
1133 return ENC_QUOTED_PRINTABLE;
1138 EncodingType procmime_get_encoding_for_text_file(const gchar *file, gboolean *has_binary)
1141 guchar buf[BUFFSIZE];
1143 size_t octet_chars = 0;
1144 size_t total_len = 0;
1145 gfloat octet_percentage;
1146 gboolean force_b64 = FALSE;
1148 if ((fp = g_fopen(file, "rb")) == NULL) {
1149 FILE_OP_ERROR(file, "fopen");
1153 while ((len = fread(buf, sizeof(guchar), sizeof(buf), fp)) > 0) {
1157 for (p = buf, i = 0; i < len; ++p, ++i) {
1171 octet_percentage = (gfloat)octet_chars / (gfloat)total_len;
1173 octet_percentage = 0.0;
1175 debug_print("procmime_get_encoding_for_text_file(): "
1176 "8bit chars: %d / %d (%f%%)\n", octet_chars, total_len,
1177 100.0 * octet_percentage);
1179 if (octet_percentage > 0.20 || force_b64) {
1180 debug_print("using BASE64\n");
1182 } else if (octet_chars > 0) {
1183 debug_print("using quoted-printable\n");
1184 return ENC_QUOTED_PRINTABLE;
1186 debug_print("using 7bit\n");
1191 struct EncodingTable
1194 EncodingType enc_type;
1197 struct EncodingTable encoding_table[] = {
1200 {"binary", ENC_BINARY},
1201 {"quoted-printable", ENC_QUOTED_PRINTABLE},
1202 {"base64", ENC_BASE64},
1203 {"x-uuencode", ENC_UNKNOWN},
1204 {NULL, ENC_UNKNOWN},
1207 const gchar *procmime_get_encoding_str(EncodingType encoding)
1209 struct EncodingTable *enc_table;
1211 for (enc_table = encoding_table; enc_table->str != NULL; enc_table++) {
1212 if (enc_table->enc_type == encoding)
1213 return enc_table->str;
1218 /* --- NEW MIME STUFF --- */
1225 static struct TypeTable mime_type_table[] = {
1226 {"text", MIMETYPE_TEXT},
1227 {"image", MIMETYPE_IMAGE},
1228 {"audio", MIMETYPE_AUDIO},
1229 {"video", MIMETYPE_VIDEO},
1230 {"application", MIMETYPE_APPLICATION},
1231 {"message", MIMETYPE_MESSAGE},
1232 {"multipart", MIMETYPE_MULTIPART},
1236 const gchar *procmime_get_media_type_str(MimeMediaType type)
1238 struct TypeTable *type_table;
1240 for (type_table = mime_type_table; type_table->str != NULL; type_table++) {
1241 if (type_table->type == type)
1242 return type_table->str;
1247 MimeMediaType procmime_get_media_type(const gchar *str)
1249 struct TypeTable *typetablearray;
1251 for (typetablearray = mime_type_table; typetablearray->str != NULL; typetablearray++)
1252 if (g_ascii_strncasecmp(str, typetablearray->str, strlen(typetablearray->str)) == 0)
1253 return typetablearray->type;
1255 return MIMETYPE_UNKNOWN;
1259 *\brief Safe wrapper for content type string.
1261 *\return const gchar * Pointer to content type string.
1263 gchar *procmime_get_content_type_str(MimeMediaType type,
1264 const char *subtype)
1266 const gchar *type_str = NULL;
1268 if (subtype == NULL || !(type_str = procmime_get_media_type_str(type)))
1269 return g_strdup("unknown");
1270 return g_strdup_printf("%s/%s", type_str, subtype);
1273 static int procmime_parse_mimepart(MimeInfo *parent,
1274 gchar *content_type,
1275 gchar *content_encoding,
1276 gchar *content_description,
1278 gchar *content_disposition,
1279 gchar *content_location,
1280 const gchar *filename,
1284 void procmime_parse_message_rfc822(MimeInfo *mimeinfo)
1286 HeaderEntry hentry[] = {{"Content-Type:", NULL, TRUE},
1287 {"Content-Transfer-Encoding:",
1289 {"Content-Description:",
1293 {"Content-Disposition:",
1295 {"Content-Location:",
1299 {NULL, NULL, FALSE}};
1300 guint content_start, i;
1304 procmime_decode_content(mimeinfo);
1306 fp = g_fopen(mimeinfo->data.filename, "rb");
1308 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
1311 fseek(fp, mimeinfo->offset, SEEK_SET);
1312 procheader_get_header_fields(fp, hentry);
1313 if (hentry[0].body != NULL) {
1314 tmp = conv_unmime_header(hentry[0].body, NULL);
1315 g_free(hentry[0].body);
1316 hentry[0].body = tmp;
1318 if (hentry[2].body != NULL) {
1319 tmp = conv_unmime_header(hentry[2].body, NULL);
1320 g_free(hentry[2].body);
1321 hentry[2].body = tmp;
1323 if (hentry[4].body != NULL) {
1324 tmp = conv_unmime_header(hentry[4].body, NULL);
1325 g_free(hentry[4].body);
1326 hentry[4].body = tmp;
1328 if (hentry[5].body != NULL) {
1329 tmp = conv_unmime_header(hentry[5].body, NULL);
1330 g_free(hentry[5].body);
1331 hentry[5].body = tmp;
1333 content_start = ftell(fp);
1336 procmime_parse_mimepart(mimeinfo,
1337 hentry[0].body, hentry[1].body,
1338 hentry[2].body, hentry[3].body,
1339 hentry[4].body, hentry[5].body,
1340 mimeinfo->data.filename, content_start,
1341 mimeinfo->length - (content_start - mimeinfo->offset));
1343 for (i = 0; i < (sizeof hentry / sizeof hentry[0]); i++) {
1344 g_free(hentry[i].body);
1345 hentry[i].body = NULL;
1349 void procmime_parse_multipart(MimeInfo *mimeinfo)
1351 HeaderEntry hentry[] = {{"Content-Type:", NULL, TRUE},
1352 {"Content-Transfer-Encoding:",
1354 {"Content-Description:",
1358 {"Content-Disposition:",
1360 {"Content-Location:",
1362 {NULL, NULL, FALSE}};
1365 gint boundary_len = 0, lastoffset = -1, i;
1366 gchar buf[BUFFSIZE];
1370 boundary = g_hash_table_lookup(mimeinfo->typeparameters, "boundary");
1373 boundary_len = strlen(boundary);
1375 procmime_decode_content(mimeinfo);
1377 fp = g_fopen(mimeinfo->data.filename, "rb");
1379 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
1382 fseek(fp, mimeinfo->offset, SEEK_SET);
1383 while ((p = fgets(buf, sizeof(buf), fp)) != NULL && result == 0) {
1384 if (ftell(fp) > (mimeinfo->offset + mimeinfo->length))
1387 if (IS_BOUNDARY(buf, boundary, boundary_len)) {
1388 if (lastoffset != -1) {
1389 result = procmime_parse_mimepart(mimeinfo,
1390 hentry[0].body, hentry[1].body,
1391 hentry[2].body, hentry[3].body,
1392 hentry[4].body, hentry[5].body,
1393 mimeinfo->data.filename, lastoffset,
1394 (ftell(fp) - strlen(buf)) - lastoffset - 1);
1397 if (buf[2 + boundary_len] == '-' &&
1398 buf[2 + boundary_len + 1] == '-')
1401 for (i = 0; i < (sizeof hentry / sizeof hentry[0]) ; i++) {
1402 g_free(hentry[i].body);
1403 hentry[i].body = NULL;
1405 procheader_get_header_fields(fp, hentry);
1406 if (hentry[0].body != NULL) {
1407 tmp = conv_unmime_header(hentry[0].body, NULL);
1408 g_free(hentry[0].body);
1409 hentry[0].body = tmp;
1411 if (hentry[2].body != NULL) {
1412 tmp = conv_unmime_header(hentry[2].body, NULL);
1413 g_free(hentry[2].body);
1414 hentry[2].body = tmp;
1416 if (hentry[4].body != NULL) {
1417 tmp = conv_unmime_header(hentry[4].body, NULL);
1418 g_free(hentry[4].body);
1419 hentry[4].body = tmp;
1421 if (hentry[5].body != NULL) {
1422 tmp = conv_unmime_header(hentry[5].body, NULL);
1423 g_free(hentry[5].body);
1424 hentry[5].body = tmp;
1426 lastoffset = ftell(fp);
1429 for (i = 0; i < (sizeof hentry / sizeof hentry[0]); i++) {
1430 g_free(hentry[i].body);
1431 hentry[i].body = NULL;
1436 static void parse_parameters(const gchar *parameters, GHashTable *table)
1438 gchar *params, *param, *next;
1439 GSList *convlist = NULL, *concatlist = NULL, *cur;
1441 params = g_strdup(parameters);
1444 for (; next != NULL; param = next) {
1445 gchar *attribute, *value, *tmp;
1447 gboolean convert = FALSE;
1449 next = strchr_with_skip_quote(param, '"', ';');
1458 value = strchr(attribute, '=');
1464 while (value[0] == ' ')
1467 g_strdown(attribute);
1469 len = strlen(attribute);
1470 if (attribute[len - 1] == '*') {
1471 gchar *srcpos, *dstpos, *endpos;
1474 attribute[len - 1] = '\0';
1478 endpos = value + strlen(value);
1479 while (srcpos < endpos) {
1485 if (!get_hex_value(&dstvalue, srcpos[1], srcpos[2]))
1496 if (value[0] == '"')
1497 extract_quote(value, '"');
1498 else if ((tmp = strchr(value, ' ')) != NULL)
1503 while (attribute[0] == ' ')
1505 while (attribute[strlen(attribute)-1] == ' ')
1506 attribute[strlen(attribute)-1] = '\0';
1509 while (value[0] == ' ')
1511 while (value[strlen(value)-1] == ' ')
1512 value[strlen(value)-1] = '\0';
1514 if (strrchr(attribute, '*') != NULL) {
1517 tmpattr = g_strdup(attribute);
1518 tmp = strrchr(tmpattr, '*');
1521 if ((tmp[1] == '0') && (tmp[2] == '\0') &&
1522 (g_slist_find_custom(concatlist, attribute, g_str_equal) == NULL))
1523 concatlist = g_slist_prepend(concatlist, g_strdup(tmpattr));
1525 if (convert && (g_slist_find_custom(convlist, attribute, g_str_equal) == NULL))
1526 convlist = g_slist_prepend(convlist, g_strdup(tmpattr));
1529 } else if (convert) {
1530 if (g_slist_find_custom(convlist, attribute, g_str_equal) == NULL)
1531 convlist = g_slist_prepend(convlist, g_strdup(attribute));
1534 if (g_hash_table_lookup(table, attribute) == NULL)
1535 g_hash_table_insert(table, g_strdup(attribute), g_strdup(value));
1538 for (cur = concatlist; cur != NULL; cur = g_slist_next(cur)) {
1539 gchar *attribute, *attrwnum, *partvalue;
1543 attribute = (gchar *) cur->data;
1544 value = g_string_sized_new(64);
1546 attrwnum = g_strdup_printf("%s*%d", attribute, n);
1547 while ((partvalue = g_hash_table_lookup(table, attrwnum)) != NULL) {
1548 g_string_append(value, partvalue);
1552 attrwnum = g_strdup_printf("%s*%d", attribute, n);
1556 g_hash_table_insert(table, g_strdup(attribute), g_strdup(value->str));
1557 g_string_free(value, TRUE);
1559 slist_free_strings(concatlist);
1560 g_slist_free(concatlist);
1562 for (cur = convlist; cur != NULL; cur = g_slist_next(cur)) {
1563 gchar *attribute, *key, *value;
1564 gchar *charset, *lang, *oldvalue, *newvalue;
1566 attribute = (gchar *) cur->data;
1567 if (!g_hash_table_lookup_extended(
1568 table, attribute, (gpointer *)(gchar *) &key, (gpointer *)(gchar *) &value))
1572 lang = strchr(charset, '\'');
1577 oldvalue = strchr(lang, '\'');
1578 if (oldvalue == NULL)
1583 newvalue = conv_codeset_strdup(oldvalue, charset, CS_UTF_8);
1585 g_hash_table_remove(table, attribute);
1589 g_hash_table_insert(table, g_strdup(attribute), newvalue);
1591 slist_free_strings(convlist);
1592 g_slist_free(convlist);
1597 static void procmime_parse_content_type(const gchar *content_type, MimeInfo *mimeinfo)
1599 g_return_if_fail(content_type != NULL);
1600 g_return_if_fail(mimeinfo != NULL);
1602 /* RFC 2045, page 13 says that the mime subtype is MANDATORY;
1603 * if it's not available we use the default Content-Type */
1604 if ((content_type[0] == '\0') || (strchr(content_type, '/') == NULL)) {
1605 mimeinfo->type = MIMETYPE_TEXT;
1606 mimeinfo->subtype = g_strdup("plain");
1607 if (g_hash_table_lookup(mimeinfo->typeparameters,
1608 "charset") == NULL) {
1609 g_hash_table_insert(mimeinfo->typeparameters,
1610 g_strdup("charset"),
1612 conv_get_locale_charset_str_no_utf8()));
1615 gchar *type, *subtype, *params;
1617 type = g_strdup(content_type);
1618 subtype = strchr(type, '/') + 1;
1619 *(subtype - 1) = '\0';
1620 if ((params = strchr(subtype, ';')) != NULL) {
1625 mimeinfo->type = procmime_get_media_type(type);
1626 mimeinfo->subtype = g_strdup(subtype);
1628 /* Get mimeinfo->typeparameters */
1630 parse_parameters(params, mimeinfo->typeparameters);
1636 static void procmime_parse_content_disposition(const gchar *content_disposition, MimeInfo *mimeinfo)
1638 gchar *tmp, *params;
1640 g_return_if_fail(content_disposition != NULL);
1641 g_return_if_fail(mimeinfo != NULL);
1643 tmp = g_strdup(content_disposition);
1644 if ((params = strchr(tmp, ';')) != NULL) {
1650 if (!g_ascii_strcasecmp(tmp, "inline"))
1651 mimeinfo->disposition = DISPOSITIONTYPE_INLINE;
1652 else if (!g_ascii_strcasecmp(tmp, "attachment"))
1653 mimeinfo->disposition = DISPOSITIONTYPE_ATTACHMENT;
1655 mimeinfo->disposition = DISPOSITIONTYPE_ATTACHMENT;
1658 parse_parameters(params, mimeinfo->dispositionparameters);
1664 static void procmime_parse_content_encoding(const gchar *content_encoding, MimeInfo *mimeinfo)
1666 struct EncodingTable *enc_table;
1668 for (enc_table = encoding_table; enc_table->str != NULL; enc_table++) {
1669 if (g_ascii_strcasecmp(enc_table->str, content_encoding) == 0) {
1670 mimeinfo->encoding_type = enc_table->enc_type;
1674 mimeinfo->encoding_type = ENC_UNKNOWN;
1678 static int procmime_parse_mimepart(MimeInfo *parent,
1679 gchar *content_type,
1680 gchar *content_encoding,
1681 gchar *content_description,
1683 gchar *content_disposition,
1684 gchar *content_location,
1685 const gchar *filename,
1691 /* Create MimeInfo */
1692 mimeinfo = procmime_mimeinfo_new();
1693 mimeinfo->content = MIMECONTENT_FILE;
1694 if (parent != NULL) {
1695 if (g_node_depth(parent->node) > 32) {
1696 /* 32 is an arbitrary value
1697 * this avoids DOSsing ourselves
1698 * with enormous messages
1700 procmime_mimeinfo_free_all(mimeinfo);
1703 g_node_append(parent->node, mimeinfo->node);
1705 mimeinfo->data.filename = g_strdup(filename);
1706 mimeinfo->offset = offset;
1707 mimeinfo->length = length;
1709 if (content_type != NULL) {
1710 procmime_parse_content_type(content_type, mimeinfo);
1712 mimeinfo->type = MIMETYPE_TEXT;
1713 mimeinfo->subtype = g_strdup("plain");
1714 if (g_hash_table_lookup(mimeinfo->typeparameters,
1715 "charset") == NULL) {
1716 g_hash_table_insert(mimeinfo->typeparameters,
1717 g_strdup("charset"),
1719 conv_get_locale_charset_str_no_utf8()));
1723 if (content_encoding != NULL) {
1724 procmime_parse_content_encoding(content_encoding, mimeinfo);
1726 mimeinfo->encoding_type = ENC_UNKNOWN;
1729 if (content_description != NULL)
1730 mimeinfo->description = g_strdup(content_description);
1732 mimeinfo->description = NULL;
1734 if (content_id != NULL)
1735 mimeinfo->id = g_strdup(content_id);
1737 mimeinfo->id = NULL;
1739 if (content_location != NULL)
1740 mimeinfo->location = g_strdup(content_location);
1742 mimeinfo->location = NULL;
1744 if (content_disposition != NULL)
1745 procmime_parse_content_disposition(content_disposition, mimeinfo);
1747 mimeinfo->disposition = DISPOSITIONTYPE_UNKNOWN;
1749 /* Call parser for mime type */
1750 switch (mimeinfo->type) {
1751 case MIMETYPE_MESSAGE:
1752 if (g_ascii_strcasecmp(mimeinfo->subtype, "rfc822") == 0) {
1753 procmime_parse_message_rfc822(mimeinfo);
1757 case MIMETYPE_MULTIPART:
1758 procmime_parse_multipart(mimeinfo);
1768 static gchar *typenames[] = {
1779 static gboolean output_func(GNode *node, gpointer data)
1782 MimeInfo *mimeinfo = (MimeInfo *) node->data;
1784 depth = g_node_depth(node);
1785 for (i = 0; i < depth; i++)
1787 printf("%s/%s (offset:%d length:%d encoding: %d)\n", typenames[mimeinfo->type], mimeinfo->subtype, mimeinfo->offset, mimeinfo->length, mimeinfo->encoding_type);
1792 static void output_mime_structure(MimeInfo *mimeinfo, int indent)
1794 g_node_traverse(mimeinfo->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1, output_func, NULL);
1797 MimeInfo *procmime_scan_file_with_offset(const gchar *filename, int offset)
1802 stat(filename, &buf);
1804 mimeinfo = procmime_mimeinfo_new();
1805 mimeinfo->content = MIMECONTENT_FILE;
1806 mimeinfo->encoding_type = ENC_UNKNOWN;
1807 mimeinfo->type = MIMETYPE_MESSAGE;
1808 mimeinfo->subtype = g_strdup("rfc822");
1809 mimeinfo->data.filename = g_strdup(filename);
1810 mimeinfo->offset = offset;
1811 mimeinfo->length = buf.st_size - offset;
1813 procmime_parse_message_rfc822(mimeinfo);
1814 if (debug_get_mode())
1815 output_mime_structure(mimeinfo, 0);
1820 MimeInfo *procmime_scan_file(const gchar *filename)
1824 g_return_val_if_fail(filename != NULL, NULL);
1826 mimeinfo = procmime_scan_file_with_offset(filename, 0);
1831 MimeInfo *procmime_scan_queue_file(const gchar *filename)
1835 gchar buf[BUFFSIZE];
1838 g_return_val_if_fail(filename != NULL, NULL);
1841 if ((fp = g_fopen(filename, "rb")) == NULL)
1843 /* Skip queue header */
1844 while (fgets(buf, sizeof(buf), fp) != NULL)
1845 if (buf[0] == '\r' || buf[0] == '\n') break;
1849 mimeinfo = procmime_scan_file_with_offset(filename, offset);
1856 ENC_AS_QUOTED_STRING,
1861 typedef struct _ParametersData {
1867 static void write_parameters(gpointer key, gpointer value, gpointer user_data)
1870 gchar *val = value, *valpos, *tmp;
1871 ParametersData *pdata = (ParametersData *)user_data;
1872 GString *buf = g_string_new("");
1874 EncodeAs encas = ENC_AS_TOKEN;
1876 for (valpos = val; *valpos != 0; valpos++) {
1877 if (!IS_ASCII(*valpos) || *valpos == '"') {
1878 encas = ENC_AS_EXTENDED;
1883 if (((*valpos >= 0) && (*valpos < 037)) || (*valpos == 0177)) {
1884 encas = ENC_AS_QUOTED_STRING;
1888 /* tspecials + SPACE */
1906 encas = ENC_AS_QUOTED_STRING;
1911 if (encas == ENC_AS_EXTENDED && pdata->ascii_only == TRUE)
1912 encas = ENC_TO_ASCII;
1916 g_string_append_printf(buf, "%s=%s", param, val);
1920 tmp = g_strdup(val);
1922 " ()<>@,';:\\/[]?=.0123456789"
1923 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1924 "abcdefghijklmnopqrstuvwxyz",
1926 g_string_append_printf(buf, "%s=\"%s\"", param, tmp);
1930 case ENC_AS_QUOTED_STRING:
1931 g_string_append_printf(buf, "%s=\"%s\"", param, val);
1934 case ENC_AS_EXTENDED:
1935 if (!g_utf8_validate(val, -1, NULL))
1936 g_string_append_printf(buf, "%s*=%s''", param,
1937 conv_get_locale_charset_str());
1939 g_string_append_printf(buf, "%s*=%s''", param,
1941 for (valpos = val; *valpos != '\0'; valpos++) {
1942 if (IS_ASCII(*valpos) && isalnum(*valpos)) {
1943 g_string_append_printf(buf, "%c", *valpos);
1945 gchar hexstr[3] = "XX";
1946 get_hex_str(hexstr, *valpos);
1947 g_string_append_printf(buf, "%%%s", hexstr);
1953 if (buf->str && strlen(buf->str)) {
1954 if (pdata->len + strlen(buf->str) + 2 > 76) {
1955 fprintf(pdata->fp, ";\n %s", buf->str);
1956 pdata->len = strlen(buf->str) + 1;
1958 fprintf(pdata->fp, "; %s", buf->str);
1959 pdata->len += strlen(buf->str) + 2;
1962 g_string_free(buf, TRUE);
1965 void procmime_write_mime_header(MimeInfo *mimeinfo, FILE *fp)
1967 struct TypeTable *type_table;
1968 ParametersData *pdata = g_new0(ParametersData, 1);
1969 debug_print("procmime_write_mime_header\n");
1972 pdata->ascii_only = FALSE;
1974 for (type_table = mime_type_table; type_table->str != NULL; type_table++)
1975 if (mimeinfo->type == type_table->type) {
1976 gchar *buf = g_strdup_printf(
1977 "Content-Type: %s/%s", type_table->str, mimeinfo->subtype);
1978 fprintf(fp, "%s", buf);
1979 pdata->len = strlen(buf);
1980 pdata->ascii_only = TRUE;
1984 g_hash_table_foreach(mimeinfo->typeparameters, write_parameters, pdata);
1989 if (mimeinfo->encoding_type != ENC_UNKNOWN)
1990 fprintf(fp, "Content-Transfer-Encoding: %s\n", procmime_get_encoding_str(mimeinfo->encoding_type));
1992 if (mimeinfo->description != NULL)
1993 fprintf(fp, "Content-Description: %s\n", mimeinfo->description);
1995 if (mimeinfo->id != NULL)
1996 fprintf(fp, "Content-ID: %s\n", mimeinfo->id);
1998 if (mimeinfo->location != NULL)
1999 fprintf(fp, "Content-Location: %s\n", mimeinfo->location);
2001 if (mimeinfo->disposition != DISPOSITIONTYPE_UNKNOWN) {
2002 ParametersData *pdata = g_new0(ParametersData, 1);
2004 if (mimeinfo->disposition == DISPOSITIONTYPE_INLINE)
2005 buf = g_strdup("Content-Disposition: inline");
2006 else if (mimeinfo->disposition == DISPOSITIONTYPE_ATTACHMENT)
2007 buf = g_strdup("Content-Disposition: attachment");
2009 buf = g_strdup("Content-Disposition: unknown");
2011 fprintf(fp, "%s", buf);
2012 pdata->len = strlen(buf);
2016 pdata->ascii_only = FALSE;
2018 g_hash_table_foreach(mimeinfo->dispositionparameters, write_parameters, pdata);
2026 gint procmime_write_message_rfc822(MimeInfo *mimeinfo, FILE *fp)
2031 gchar buf[BUFFSIZE];
2032 gboolean skip = FALSE;;
2034 debug_print("procmime_write_message_rfc822\n");
2037 switch (mimeinfo->content) {
2038 case MIMECONTENT_FILE:
2039 if ((infp = g_fopen(mimeinfo->data.filename, "rb")) == NULL) {
2040 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
2043 fseek(infp, mimeinfo->offset, SEEK_SET);
2044 while (fgets(buf, sizeof(buf), infp) == buf) {
2045 if (buf[0] == '\n' && buf[1] == '\0')
2047 if (skip && (buf[0] == ' ' || buf[0] == '\t'))
2049 if (g_ascii_strncasecmp(buf, "Mime-Version:", 13) == 0 ||
2050 g_ascii_strncasecmp(buf, "Content-Type:", 13) == 0 ||
2051 g_ascii_strncasecmp(buf, "Content-Transfer-Encoding:", 26) == 0 ||
2052 g_ascii_strncasecmp(buf, "Content-Description:", 20) == 0 ||
2053 g_ascii_strncasecmp(buf, "Content-ID:", 11) == 0 ||
2054 g_ascii_strncasecmp(buf, "Content-Location:", 17) == 0 ||
2055 g_ascii_strncasecmp(buf, "Content-Disposition:", 20) == 0) {
2059 fwrite(buf, sizeof(gchar), strlen(buf), fp);
2065 case MIMECONTENT_MEM:
2066 fwrite(mimeinfo->data.mem,
2068 strlen(mimeinfo->data.mem),
2076 childnode = mimeinfo->node->children;
2077 if (childnode == NULL)
2080 child = (MimeInfo *) childnode->data;
2081 fprintf(fp, "Mime-Version: 1.0\n");
2082 procmime_write_mime_header(child, fp);
2083 return procmime_write_mimeinfo(child, fp);
2086 gint procmime_write_multipart(MimeInfo *mimeinfo, FILE *fp)
2090 gchar *boundary, *str, *str2;
2091 gchar buf[BUFFSIZE];
2092 gboolean firstboundary;
2094 debug_print("procmime_write_multipart\n");
2096 boundary = g_hash_table_lookup(mimeinfo->typeparameters, "boundary");
2098 switch (mimeinfo->content) {
2099 case MIMECONTENT_FILE:
2100 if ((infp = g_fopen(mimeinfo->data.filename, "rb")) == NULL) {
2101 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
2104 fseek(infp, mimeinfo->offset, SEEK_SET);
2105 while (fgets(buf, sizeof(buf), infp) == buf) {
2106 if (IS_BOUNDARY(buf, boundary, strlen(boundary)))
2108 fwrite(buf, sizeof(gchar), strlen(buf), fp);
2113 case MIMECONTENT_MEM:
2114 str = g_strdup(mimeinfo->data.mem);
2115 if (((str2 = strstr(str, boundary)) != NULL) && ((str2 - str) >= 2) &&
2116 (*(str2 - 1) == '-') && (*(str2 - 2) == '-'))
2118 fwrite(str, sizeof(gchar), strlen(str), fp);
2126 childnode = mimeinfo->node->children;
2127 firstboundary = TRUE;
2128 while (childnode != NULL) {
2129 MimeInfo *child = childnode->data;
2132 firstboundary = FALSE;
2135 fprintf(fp, "--%s\n", boundary);
2137 procmime_write_mime_header(child, fp);
2138 if (procmime_write_mimeinfo(child, fp) < 0)
2141 childnode = g_node_next_sibling(childnode);
2143 fprintf(fp, "\n--%s--\n", boundary);
2148 gint procmime_write_mimeinfo(MimeInfo *mimeinfo, FILE *fp)
2152 debug_print("procmime_write_mimeinfo\n");
2154 if (G_NODE_IS_LEAF(mimeinfo->node)) {
2155 switch (mimeinfo->content) {
2156 case MIMECONTENT_FILE:
2157 if ((infp = g_fopen(mimeinfo->data.filename, "rb")) == NULL) {
2158 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
2161 copy_file_part_to_fp(infp, mimeinfo->offset, mimeinfo->length, fp);
2165 case MIMECONTENT_MEM:
2166 fwrite(mimeinfo->data.mem,
2168 strlen(mimeinfo->data.mem),
2176 /* Call writer for mime type */
2177 switch (mimeinfo->type) {
2178 case MIMETYPE_MESSAGE:
2179 if (g_ascii_strcasecmp(mimeinfo->subtype, "rfc822") == 0) {
2180 return procmime_write_message_rfc822(mimeinfo, fp);
2184 case MIMETYPE_MULTIPART:
2185 return procmime_write_multipart(mimeinfo, fp);
2197 gchar *procmime_get_part_file_name(MimeInfo *mimeinfo)
2201 if ((mimeinfo->type == MIMETYPE_TEXT) && !g_ascii_strcasecmp(mimeinfo->subtype, "html"))
2202 base = g_strdup("mimetmp.html");
2204 const gchar *basetmp;
2207 basetmp = procmime_mimeinfo_get_parameter(mimeinfo, "filename");
2208 if (basetmp == NULL)
2209 basetmp = procmime_mimeinfo_get_parameter(mimeinfo, "name");
2210 if (basetmp == NULL)
2211 basetmp = "mimetmp";
2212 basename = g_path_get_basename(basetmp);
2213 if (*basename == '\0') {
2215 basename = g_strdup("mimetmp");
2217 base = conv_filename_from_utf8(basename);
2219 subst_for_shellsafe_filename(base);