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);
122 g_hash_table_foreach_remove(mimeinfo->typeparameters,
123 procmime_mimeinfo_parameters_destroy, NULL);
124 g_hash_table_destroy(mimeinfo->typeparameters);
125 g_hash_table_foreach_remove(mimeinfo->dispositionparameters,
126 procmime_mimeinfo_parameters_destroy, NULL);
127 g_hash_table_destroy(mimeinfo->dispositionparameters);
129 if (mimeinfo->privacy)
130 privacy_free_privacydata(mimeinfo->privacy);
137 void procmime_mimeinfo_free_all(MimeInfo *mimeinfo)
144 node = mimeinfo->node;
145 g_node_traverse(node, G_IN_ORDER, G_TRAVERSE_ALL, -1, free_func, NULL);
147 g_node_destroy(node);
150 MimeInfo *procmime_mimeinfo_parent(MimeInfo *mimeinfo)
152 g_return_val_if_fail(mimeinfo != NULL, NULL);
153 g_return_val_if_fail(mimeinfo->node != NULL, NULL);
155 if (mimeinfo->node->parent == NULL)
157 return (MimeInfo *) mimeinfo->node->parent->data;
160 MimeInfo *procmime_mimeinfo_next(MimeInfo *mimeinfo)
162 g_return_val_if_fail(mimeinfo != NULL, NULL);
163 g_return_val_if_fail(mimeinfo->node != NULL, NULL);
165 if (mimeinfo->node->children)
166 return (MimeInfo *) mimeinfo->node->children->data;
167 if (mimeinfo->node->next)
168 return (MimeInfo *) mimeinfo->node->next->data;
170 if (mimeinfo->node->parent == NULL)
173 while (mimeinfo->node->parent != NULL) {
174 mimeinfo = (MimeInfo *) mimeinfo->node->parent->data;
175 if (mimeinfo->node->next)
176 return (MimeInfo *) mimeinfo->node->next->data;
182 MimeInfo *procmime_scan_message(MsgInfo *msginfo)
187 filename = procmsg_get_message_file_path(msginfo);
188 if (!filename || !is_file_exist(filename)) {
190 filename = procmsg_get_message_file(msginfo);
192 if (!filename || !is_file_exist(filename))
195 if (!folder_has_parent_of_type(msginfo->folder, F_QUEUE) &&
196 !folder_has_parent_of_type(msginfo->folder, F_DRAFT))
197 mimeinfo = procmime_scan_file(filename);
199 mimeinfo = procmime_scan_queue_file(filename);
207 H_CONTENT_TRANSFER_ENCODING = 0,
209 H_CONTENT_DISPOSITION = 2,
210 H_CONTENT_DESCRIPTION = 3,
214 const gchar *procmime_mimeinfo_get_parameter(MimeInfo *mimeinfo, const gchar *name)
218 g_return_val_if_fail(mimeinfo != NULL, NULL);
219 g_return_val_if_fail(name != NULL, NULL);
221 value = g_hash_table_lookup(mimeinfo->dispositionparameters, name);
223 value = g_hash_table_lookup(mimeinfo->typeparameters, name);
228 gboolean procmime_decode_content(MimeInfo *mimeinfo)
235 gboolean tmp_file = FALSE;
237 EncodingType encoding = forced_encoding
239 : mimeinfo->encoding_type;
241 g_return_val_if_fail(mimeinfo != NULL, FALSE);
243 if (encoding == ENC_UNKNOWN ||
244 encoding == ENC_BINARY)
247 infp = g_fopen(mimeinfo->data.filename, "rb");
252 fseek(infp, mimeinfo->offset, SEEK_SET);
254 outfp = get_tmpfile_in_dir(get_mime_tmp_dir(), &tmpfilename);
261 readend = mimeinfo->offset + mimeinfo->length;
263 if (encoding == ENC_QUOTED_PRINTABLE) {
264 while ((ftell(infp) < readend) && (fgets(buf, sizeof(buf), infp) != NULL)) {
266 len = qp_decode_line(buf);
267 fwrite(buf, 1, len, outfp);
269 } else if (encoding == ENC_BASE64) {
270 gchar outbuf[BUFFSIZE];
272 Base64Decoder *decoder;
273 gboolean got_error = FALSE;
274 gboolean uncanonicalize = FALSE;
277 if (mimeinfo->type == MIMETYPE_TEXT ||
278 mimeinfo->type == MIMETYPE_MESSAGE) {
279 uncanonicalize = TRUE;
280 tmpfp = my_tmpfile();
283 if (tmp_file) fclose(outfp);
289 decoder = base64_decoder_new();
290 while ((ftell(infp) < readend) && (fgets(buf, sizeof(buf), infp) != NULL)) {
291 len = base64_decoder_decode(decoder, buf, outbuf);
292 if (len < 0 && !got_error) {
293 g_warning("Bad BASE64 content.\n");
294 fwrite(_("[Error decoding BASE64]\n"),
296 strlen(_("[Error decoding BASE64]\n")),
300 } else if (len >= 0) {
301 /* print out the error message only once
303 fwrite(outbuf, sizeof(gchar), len, tmpfp);
307 base64_decoder_free(decoder);
309 if (uncanonicalize) {
311 while (fgets(buf, sizeof(buf), tmpfp) != NULL) {
317 } else if (encoding == ENC_X_UUENCODE) {
318 gchar outbuf[BUFFSIZE];
320 gboolean flag = FALSE;
322 while ((ftell(infp) < readend) && (fgets(buf, sizeof(buf), infp) != NULL)) {
323 if (!flag && strncmp(buf,"begin ", 6)) continue;
326 len = fromuutobits(outbuf, buf);
329 g_warning("Bad UUENCODE content(%d)\n", len);
332 fwrite(outbuf, sizeof(gchar), len, outfp);
337 while ((ftell(infp) < readend) && (fgets(buf, sizeof(buf), infp) != NULL)) {
345 stat(tmpfilename, &statbuf);
346 if (mimeinfo->tmp && (mimeinfo->data.filename != NULL))
347 g_unlink(mimeinfo->data.filename);
348 if (mimeinfo->data.filename != NULL)
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 int procmime_parse_mimepart(MimeInfo *parent,
1274 gchar *content_type,
1275 gchar *content_encoding,
1276 gchar *content_description,
1278 gchar *content_disposition,
1279 const gchar *filename,
1283 void procmime_parse_message_rfc822(MimeInfo *mimeinfo)
1285 HeaderEntry hentry[] = {{"Content-Type:", NULL, TRUE},
1286 {"Content-Transfer-Encoding:",
1288 {"Content-Description:",
1292 {"Content-Disposition:",
1296 {NULL, NULL, FALSE}};
1297 guint content_start, i;
1301 procmime_decode_content(mimeinfo);
1303 fp = g_fopen(mimeinfo->data.filename, "rb");
1305 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
1308 fseek(fp, mimeinfo->offset, SEEK_SET);
1309 procheader_get_header_fields(fp, hentry);
1310 if (hentry[0].body != NULL) {
1311 tmp = conv_unmime_header(hentry[0].body, NULL);
1312 g_free(hentry[0].body);
1313 hentry[0].body = tmp;
1315 if (hentry[2].body != NULL) {
1316 tmp = conv_unmime_header(hentry[2].body, NULL);
1317 g_free(hentry[2].body);
1318 hentry[2].body = tmp;
1320 if (hentry[4].body != NULL) {
1321 tmp = conv_unmime_header(hentry[4].body, NULL);
1322 g_free(hentry[4].body);
1323 hentry[4].body = tmp;
1325 content_start = ftell(fp);
1328 procmime_parse_mimepart(mimeinfo,
1329 hentry[0].body, hentry[1].body,
1330 hentry[2].body, hentry[3].body,
1332 mimeinfo->data.filename, content_start,
1333 mimeinfo->length - (content_start - mimeinfo->offset));
1335 for (i = 0; i < (sizeof hentry / sizeof hentry[0]); i++) {
1336 g_free(hentry[i].body);
1337 hentry[i].body = NULL;
1341 void procmime_parse_multipart(MimeInfo *mimeinfo)
1343 HeaderEntry hentry[] = {{"Content-Type:", NULL, TRUE},
1344 {"Content-Transfer-Encoding:",
1346 {"Content-Description:",
1350 {"Content-Disposition:",
1352 {NULL, NULL, FALSE}};
1355 gint boundary_len = 0, lastoffset = -1, i;
1356 gchar buf[BUFFSIZE];
1360 boundary = g_hash_table_lookup(mimeinfo->typeparameters, "boundary");
1363 boundary_len = strlen(boundary);
1365 procmime_decode_content(mimeinfo);
1367 fp = g_fopen(mimeinfo->data.filename, "rb");
1369 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
1372 fseek(fp, mimeinfo->offset, SEEK_SET);
1373 while ((p = fgets(buf, sizeof(buf), fp)) != NULL && result == 0) {
1374 if (ftell(fp) > (mimeinfo->offset + mimeinfo->length))
1377 if (IS_BOUNDARY(buf, boundary, boundary_len)) {
1378 if (lastoffset != -1) {
1379 result = procmime_parse_mimepart(mimeinfo,
1380 hentry[0].body, hentry[1].body,
1381 hentry[2].body, hentry[3].body,
1383 mimeinfo->data.filename, lastoffset,
1384 (ftell(fp) - strlen(buf)) - lastoffset - 1);
1387 if (buf[2 + boundary_len] == '-' &&
1388 buf[2 + boundary_len + 1] == '-')
1391 for (i = 0; i < (sizeof hentry / sizeof hentry[0]) ; i++) {
1392 g_free(hentry[i].body);
1393 hentry[i].body = NULL;
1395 procheader_get_header_fields(fp, hentry);
1396 if (hentry[0].body != NULL) {
1397 tmp = conv_unmime_header(hentry[0].body, NULL);
1398 g_free(hentry[0].body);
1399 hentry[0].body = tmp;
1401 if (hentry[2].body != NULL) {
1402 tmp = conv_unmime_header(hentry[2].body, NULL);
1403 g_free(hentry[2].body);
1404 hentry[2].body = tmp;
1406 if (hentry[4].body != NULL) {
1407 tmp = conv_unmime_header(hentry[4].body, NULL);
1408 g_free(hentry[4].body);
1409 hentry[4].body = tmp;
1411 lastoffset = ftell(fp);
1414 for (i = 0; i < (sizeof hentry / sizeof hentry[0]); i++) {
1415 g_free(hentry[i].body);
1416 hentry[i].body = NULL;
1421 static void parse_parameters(const gchar *parameters, GHashTable *table)
1423 gchar *params, *param, *next;
1424 GSList *convlist = NULL, *concatlist = NULL, *cur;
1426 params = g_strdup(parameters);
1429 for (; next != NULL; param = next) {
1430 gchar *attribute, *value, *tmp;
1432 gboolean convert = FALSE;
1434 next = strchr_with_skip_quote(param, '"', ';');
1443 value = strchr(attribute, '=');
1449 while (value[0] == ' ')
1452 g_strdown(attribute);
1454 len = strlen(attribute);
1455 if (attribute[len - 1] == '*') {
1456 gchar *srcpos, *dstpos, *endpos;
1459 attribute[len - 1] = '\0';
1463 endpos = value + strlen(value);
1464 while (srcpos < endpos) {
1470 if (!get_hex_value(&dstvalue, srcpos[1], srcpos[2]))
1481 if (value[0] == '"')
1482 extract_quote(value, '"');
1483 else if ((tmp = strchr(value, ' ')) != NULL)
1488 while (attribute[0] == ' ')
1490 while (attribute[strlen(attribute)-1] == ' ')
1491 attribute[strlen(attribute)-1] = '\0';
1494 while (value[0] == ' ')
1496 while (value[strlen(value)-1] == ' ')
1497 value[strlen(value)-1] = '\0';
1499 if (strrchr(attribute, '*') != NULL) {
1502 tmpattr = g_strdup(attribute);
1503 tmp = strrchr(tmpattr, '*');
1506 if ((tmp[1] == '0') && (tmp[2] == '\0') &&
1507 (g_slist_find_custom(concatlist, attribute, g_str_equal) == NULL))
1508 concatlist = g_slist_prepend(concatlist, g_strdup(tmpattr));
1510 if (convert && (g_slist_find_custom(convlist, attribute, g_str_equal) == NULL))
1511 convlist = g_slist_prepend(convlist, g_strdup(tmpattr));
1514 } else if (convert) {
1515 if (g_slist_find_custom(convlist, attribute, g_str_equal) == NULL)
1516 convlist = g_slist_prepend(convlist, g_strdup(attribute));
1519 if (g_hash_table_lookup(table, attribute) == NULL)
1520 g_hash_table_insert(table, g_strdup(attribute), g_strdup(value));
1523 for (cur = concatlist; cur != NULL; cur = g_slist_next(cur)) {
1524 gchar *attribute, *attrwnum, *partvalue;
1528 attribute = (gchar *) cur->data;
1529 value = g_string_sized_new(64);
1531 attrwnum = g_strdup_printf("%s*%d", attribute, n);
1532 while ((partvalue = g_hash_table_lookup(table, attrwnum)) != NULL) {
1533 g_string_append(value, partvalue);
1537 attrwnum = g_strdup_printf("%s*%d", attribute, n);
1541 g_hash_table_insert(table, g_strdup(attribute), g_strdup(value->str));
1542 g_string_free(value, TRUE);
1544 slist_free_strings(concatlist);
1545 g_slist_free(concatlist);
1547 for (cur = convlist; cur != NULL; cur = g_slist_next(cur)) {
1548 gchar *attribute, *key, *value;
1549 gchar *charset, *lang, *oldvalue, *newvalue;
1551 attribute = (gchar *) cur->data;
1552 if (!g_hash_table_lookup_extended(
1553 table, attribute, (gpointer *)(gchar *) &key, (gpointer *)(gchar *) &value))
1557 lang = strchr(charset, '\'');
1562 oldvalue = strchr(lang, '\'');
1563 if (oldvalue == NULL)
1568 newvalue = conv_codeset_strdup(oldvalue, charset, CS_UTF_8);
1570 g_hash_table_remove(table, attribute);
1574 g_hash_table_insert(table, g_strdup(attribute), newvalue);
1576 slist_free_strings(convlist);
1577 g_slist_free(convlist);
1582 static void procmime_parse_content_type(const gchar *content_type, MimeInfo *mimeinfo)
1584 g_return_if_fail(content_type != NULL);
1585 g_return_if_fail(mimeinfo != NULL);
1587 /* RFC 2045, page 13 says that the mime subtype is MANDATORY;
1588 * if it's not available we use the default Content-Type */
1589 if ((content_type[0] == '\0') || (strchr(content_type, '/') == NULL)) {
1590 mimeinfo->type = MIMETYPE_TEXT;
1591 mimeinfo->subtype = g_strdup("plain");
1592 if (g_hash_table_lookup(mimeinfo->typeparameters,
1593 "charset") == NULL) {
1594 g_hash_table_insert(mimeinfo->typeparameters,
1595 g_strdup("charset"),
1597 conv_get_locale_charset_str_no_utf8()));
1600 gchar *type, *subtype, *params;
1602 type = g_strdup(content_type);
1603 subtype = strchr(type, '/') + 1;
1604 *(subtype - 1) = '\0';
1605 if ((params = strchr(subtype, ';')) != NULL) {
1610 mimeinfo->type = procmime_get_media_type(type);
1611 mimeinfo->subtype = g_strdup(subtype);
1613 /* Get mimeinfo->typeparameters */
1615 parse_parameters(params, mimeinfo->typeparameters);
1621 static void procmime_parse_content_disposition(const gchar *content_disposition, MimeInfo *mimeinfo)
1623 gchar *tmp, *params;
1625 g_return_if_fail(content_disposition != NULL);
1626 g_return_if_fail(mimeinfo != NULL);
1628 tmp = g_strdup(content_disposition);
1629 if ((params = strchr(tmp, ';')) != NULL) {
1635 if (!g_ascii_strcasecmp(tmp, "inline"))
1636 mimeinfo->disposition = DISPOSITIONTYPE_INLINE;
1637 else if (!g_ascii_strcasecmp(tmp, "attachment"))
1638 mimeinfo->disposition = DISPOSITIONTYPE_ATTACHMENT;
1640 mimeinfo->disposition = DISPOSITIONTYPE_ATTACHMENT;
1643 parse_parameters(params, mimeinfo->dispositionparameters);
1649 static void procmime_parse_content_encoding(const gchar *content_encoding, MimeInfo *mimeinfo)
1651 struct EncodingTable *enc_table;
1653 for (enc_table = encoding_table; enc_table->str != NULL; enc_table++) {
1654 if (g_ascii_strcasecmp(enc_table->str, content_encoding) == 0) {
1655 mimeinfo->encoding_type = enc_table->enc_type;
1659 mimeinfo->encoding_type = ENC_UNKNOWN;
1663 int procmime_parse_mimepart(MimeInfo *parent,
1664 gchar *content_type,
1665 gchar *content_encoding,
1666 gchar *content_description,
1668 gchar *content_disposition,
1669 const gchar *filename,
1675 /* Create MimeInfo */
1676 mimeinfo = procmime_mimeinfo_new();
1677 mimeinfo->content = MIMECONTENT_FILE;
1678 if (parent != NULL) {
1679 if (g_node_depth(parent->node) > 32) {
1680 /* 32 is an arbitrary value
1681 * this avoids DOSsing ourselves
1682 * with enormous messages
1684 procmime_mimeinfo_free_all(mimeinfo);
1687 g_node_append(parent->node, mimeinfo->node);
1689 mimeinfo->data.filename = g_strdup(filename);
1690 mimeinfo->offset = offset;
1691 mimeinfo->length = length;
1693 if (content_type != NULL) {
1694 procmime_parse_content_type(content_type, mimeinfo);
1696 mimeinfo->type = MIMETYPE_TEXT;
1697 mimeinfo->subtype = g_strdup("plain");
1698 if (g_hash_table_lookup(mimeinfo->typeparameters,
1699 "charset") == NULL) {
1700 g_hash_table_insert(mimeinfo->typeparameters,
1701 g_strdup("charset"),
1703 conv_get_locale_charset_str_no_utf8()));
1707 if (content_encoding != NULL) {
1708 procmime_parse_content_encoding(content_encoding, mimeinfo);
1710 mimeinfo->encoding_type = ENC_UNKNOWN;
1713 if (content_description != NULL)
1714 mimeinfo->description = g_strdup(content_description);
1716 mimeinfo->description = NULL;
1718 if (content_id != NULL)
1719 mimeinfo->id = g_strdup(content_id);
1721 mimeinfo->id = NULL;
1723 if (content_disposition != NULL)
1724 procmime_parse_content_disposition(content_disposition, mimeinfo);
1726 mimeinfo->disposition = DISPOSITIONTYPE_UNKNOWN;
1728 /* Call parser for mime type */
1729 switch (mimeinfo->type) {
1730 case MIMETYPE_MESSAGE:
1731 if (g_ascii_strcasecmp(mimeinfo->subtype, "rfc822") == 0) {
1732 procmime_parse_message_rfc822(mimeinfo);
1736 case MIMETYPE_MULTIPART:
1737 procmime_parse_multipart(mimeinfo);
1747 static gchar *typenames[] = {
1758 static gboolean output_func(GNode *node, gpointer data)
1761 MimeInfo *mimeinfo = (MimeInfo *) node->data;
1763 depth = g_node_depth(node);
1764 for (i = 0; i < depth; i++)
1766 printf("%s/%s (offset:%d length:%d encoding: %d)\n", typenames[mimeinfo->type], mimeinfo->subtype, mimeinfo->offset, mimeinfo->length, mimeinfo->encoding_type);
1771 static void output_mime_structure(MimeInfo *mimeinfo, int indent)
1773 g_node_traverse(mimeinfo->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1, output_func, NULL);
1776 MimeInfo *procmime_scan_file_with_offset(const gchar *filename, int offset)
1781 stat(filename, &buf);
1783 mimeinfo = procmime_mimeinfo_new();
1784 mimeinfo->content = MIMECONTENT_FILE;
1785 mimeinfo->encoding_type = ENC_UNKNOWN;
1786 mimeinfo->type = MIMETYPE_MESSAGE;
1787 mimeinfo->subtype = g_strdup("rfc822");
1788 mimeinfo->data.filename = g_strdup(filename);
1789 mimeinfo->offset = offset;
1790 mimeinfo->length = buf.st_size - offset;
1792 procmime_parse_message_rfc822(mimeinfo);
1793 if (debug_get_mode())
1794 output_mime_structure(mimeinfo, 0);
1799 MimeInfo *procmime_scan_file(const gchar *filename)
1803 g_return_val_if_fail(filename != NULL, NULL);
1805 mimeinfo = procmime_scan_file_with_offset(filename, 0);
1810 MimeInfo *procmime_scan_queue_file(const gchar *filename)
1814 gchar buf[BUFFSIZE];
1817 g_return_val_if_fail(filename != NULL, NULL);
1820 if ((fp = g_fopen(filename, "rb")) == NULL)
1822 /* Skip queue header */
1823 while (fgets(buf, sizeof(buf), fp) != NULL)
1824 if (buf[0] == '\r' || buf[0] == '\n') break;
1828 mimeinfo = procmime_scan_file_with_offset(filename, offset);
1835 ENC_AS_QUOTED_STRING,
1840 typedef struct _ParametersData {
1846 static void write_parameters(gpointer key, gpointer value, gpointer user_data)
1849 gchar *val = value, *valpos, *tmp;
1850 ParametersData *pdata = (ParametersData *)user_data;
1851 GString *buf = g_string_new("");
1853 EncodeAs encas = ENC_AS_TOKEN;
1855 for (valpos = val; *valpos != 0; valpos++) {
1856 if (!IS_ASCII(*valpos) || *valpos == '"') {
1857 encas = ENC_AS_EXTENDED;
1862 if (((*valpos >= 0) && (*valpos < 037)) || (*valpos == 0177)) {
1863 encas = ENC_AS_QUOTED_STRING;
1867 /* tspecials + SPACE */
1885 encas = ENC_AS_QUOTED_STRING;
1890 if (encas == ENC_AS_EXTENDED && pdata->ascii_only == TRUE)
1891 encas = ENC_TO_ASCII;
1895 g_string_append_printf(buf, "%s=%s", param, val);
1899 tmp = g_strdup(val);
1901 " ()<>@,';:\\/[]?=.0123456789"
1902 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1903 "abcdefghijklmnopqrstuvwxyz",
1905 g_string_append_printf(buf, "%s=\"%s\"", param, tmp);
1909 case ENC_AS_QUOTED_STRING:
1910 g_string_append_printf(buf, "%s=\"%s\"", param, val);
1913 case ENC_AS_EXTENDED:
1914 if (!g_utf8_validate(val, -1, NULL))
1915 g_string_append_printf(buf, "%s*=%s''", param,
1916 conv_get_locale_charset_str());
1918 g_string_append_printf(buf, "%s*=%s''", param,
1920 for (valpos = val; *valpos != '\0'; valpos++) {
1921 if (IS_ASCII(*valpos) && isalnum(*valpos)) {
1922 g_string_append_printf(buf, "%c", *valpos);
1924 gchar hexstr[3] = "XX";
1925 get_hex_str(hexstr, *valpos);
1926 g_string_append_printf(buf, "%%%s", hexstr);
1932 if (buf->str && strlen(buf->str)) {
1933 if (pdata->len + strlen(buf->str) + 2 > 76) {
1934 fprintf(pdata->fp, ";\n %s", buf->str);
1935 pdata->len = strlen(buf->str) + 1;
1937 fprintf(pdata->fp, "; %s", buf->str);
1938 pdata->len += strlen(buf->str) + 2;
1941 g_string_free(buf, TRUE);
1944 void procmime_write_mime_header(MimeInfo *mimeinfo, FILE *fp)
1946 struct TypeTable *type_table;
1947 ParametersData *pdata = g_new0(ParametersData, 1);
1948 debug_print("procmime_write_mime_header\n");
1951 pdata->ascii_only = FALSE;
1953 for (type_table = mime_type_table; type_table->str != NULL; type_table++)
1954 if (mimeinfo->type == type_table->type) {
1955 gchar *buf = g_strdup_printf(
1956 "Content-Type: %s/%s", type_table->str, mimeinfo->subtype);
1957 fprintf(fp, "%s", buf);
1958 pdata->len = strlen(buf);
1959 pdata->ascii_only = TRUE;
1963 g_hash_table_foreach(mimeinfo->typeparameters, write_parameters, pdata);
1968 if (mimeinfo->encoding_type != ENC_UNKNOWN)
1969 fprintf(fp, "Content-Transfer-Encoding: %s\n", procmime_get_encoding_str(mimeinfo->encoding_type));
1971 if (mimeinfo->description != NULL)
1972 fprintf(fp, "Content-Description: %s\n", mimeinfo->description);
1974 if (mimeinfo->id != NULL)
1975 fprintf(fp, "Content-ID: %s\n", mimeinfo->id);
1977 if (mimeinfo->disposition != DISPOSITIONTYPE_UNKNOWN) {
1978 ParametersData *pdata = g_new0(ParametersData, 1);
1980 if (mimeinfo->disposition == DISPOSITIONTYPE_INLINE)
1981 buf = g_strdup("Content-Disposition: inline");
1982 else if (mimeinfo->disposition == DISPOSITIONTYPE_ATTACHMENT)
1983 buf = g_strdup("Content-Disposition: attachment");
1985 buf = g_strdup("Content-Disposition: unknown");
1987 fprintf(fp, "%s", buf);
1988 pdata->len = strlen(buf);
1992 pdata->ascii_only = FALSE;
1994 g_hash_table_foreach(mimeinfo->dispositionparameters, write_parameters, pdata);
2002 gint procmime_write_message_rfc822(MimeInfo *mimeinfo, FILE *fp)
2007 gchar buf[BUFFSIZE];
2008 gboolean skip = FALSE;;
2010 debug_print("procmime_write_message_rfc822\n");
2013 switch (mimeinfo->content) {
2014 case MIMECONTENT_FILE:
2015 if ((infp = g_fopen(mimeinfo->data.filename, "rb")) == NULL) {
2016 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
2019 fseek(infp, mimeinfo->offset, SEEK_SET);
2020 while (fgets(buf, sizeof(buf), infp) == buf) {
2021 if (buf[0] == '\n' && buf[1] == '\0')
2023 if (skip && (buf[0] == ' ' || buf[0] == '\t'))
2025 if (g_ascii_strncasecmp(buf, "Mime-Version:", 13) == 0 ||
2026 g_ascii_strncasecmp(buf, "Content-Type:", 13) == 0 ||
2027 g_ascii_strncasecmp(buf, "Content-Transfer-Encoding:", 26) == 0 ||
2028 g_ascii_strncasecmp(buf, "Content-Description:", 20) == 0 ||
2029 g_ascii_strncasecmp(buf, "Content-ID:", 11) == 0 ||
2030 g_ascii_strncasecmp(buf, "Content-Disposition:", 20) == 0) {
2034 fwrite(buf, sizeof(gchar), strlen(buf), fp);
2040 case MIMECONTENT_MEM:
2041 fwrite(mimeinfo->data.mem,
2043 strlen(mimeinfo->data.mem),
2051 childnode = mimeinfo->node->children;
2052 if (childnode == NULL)
2055 child = (MimeInfo *) childnode->data;
2056 fprintf(fp, "Mime-Version: 1.0\n");
2057 procmime_write_mime_header(child, fp);
2058 return procmime_write_mimeinfo(child, fp);
2061 gint procmime_write_multipart(MimeInfo *mimeinfo, FILE *fp)
2065 gchar *boundary, *str, *str2;
2066 gchar buf[BUFFSIZE];
2067 gboolean firstboundary;
2069 debug_print("procmime_write_multipart\n");
2071 boundary = g_hash_table_lookup(mimeinfo->typeparameters, "boundary");
2073 switch (mimeinfo->content) {
2074 case MIMECONTENT_FILE:
2075 if ((infp = g_fopen(mimeinfo->data.filename, "rb")) == NULL) {
2076 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
2079 fseek(infp, mimeinfo->offset, SEEK_SET);
2080 while (fgets(buf, sizeof(buf), infp) == buf) {
2081 if (IS_BOUNDARY(buf, boundary, strlen(boundary)))
2083 fwrite(buf, sizeof(gchar), strlen(buf), fp);
2088 case MIMECONTENT_MEM:
2089 str = g_strdup(mimeinfo->data.mem);
2090 if (((str2 = strstr(str, boundary)) != NULL) && ((str2 - str) >= 2) &&
2091 (*(str2 - 1) == '-') && (*(str2 - 2) == '-'))
2093 fwrite(str, sizeof(gchar), strlen(str), fp);
2101 childnode = mimeinfo->node->children;
2102 firstboundary = TRUE;
2103 while (childnode != NULL) {
2104 MimeInfo *child = childnode->data;
2107 firstboundary = FALSE;
2110 fprintf(fp, "--%s\n", boundary);
2112 procmime_write_mime_header(child, fp);
2113 if (procmime_write_mimeinfo(child, fp) < 0)
2116 childnode = g_node_next_sibling(childnode);
2118 fprintf(fp, "\n--%s--\n", boundary);
2123 gint procmime_write_mimeinfo(MimeInfo *mimeinfo, FILE *fp)
2127 debug_print("procmime_write_mimeinfo\n");
2129 if (G_NODE_IS_LEAF(mimeinfo->node)) {
2130 switch (mimeinfo->content) {
2131 case MIMECONTENT_FILE:
2132 if ((infp = g_fopen(mimeinfo->data.filename, "rb")) == NULL) {
2133 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
2136 copy_file_part_to_fp(infp, mimeinfo->offset, mimeinfo->length, fp);
2140 case MIMECONTENT_MEM:
2141 fwrite(mimeinfo->data.mem,
2143 strlen(mimeinfo->data.mem),
2151 /* Call writer for mime type */
2152 switch (mimeinfo->type) {
2153 case MIMETYPE_MESSAGE:
2154 if (g_ascii_strcasecmp(mimeinfo->subtype, "rfc822") == 0) {
2155 return procmime_write_message_rfc822(mimeinfo, fp);
2159 case MIMETYPE_MULTIPART:
2160 return procmime_write_multipart(mimeinfo, fp);
2172 gchar *procmime_get_part_file_name(MimeInfo *mimeinfo)
2176 if ((mimeinfo->type == MIMETYPE_TEXT) && !g_ascii_strcasecmp(mimeinfo->subtype, "html"))
2177 base = g_strdup("mimetmp.html");
2179 const gchar *basetmp;
2182 basetmp = procmime_mimeinfo_get_parameter(mimeinfo, "filename");
2183 if (basetmp == NULL)
2184 basetmp = procmime_mimeinfo_get_parameter(mimeinfo, "name");
2185 if (basetmp == NULL)
2186 basetmp = "mimetmp";
2187 basename = g_path_get_basename(basetmp);
2188 if (*basename == '\0') {
2190 basename = g_strdup("mimetmp");
2192 base = conv_filename_from_utf8(basename);
2194 subst_for_shellsafe_filename(base);