2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2006 Hiroyuki Yamamoto & The Sylpheed-Claws Team
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
28 #include <glib/gi18n.h>
35 #include <sys/types.h>
40 #include "procheader.h"
42 #include "quoted-printable.h"
49 #include "prefs_common.h"
50 #include "prefs_gtk.h"
51 #include "alertpanel.h"
53 static GHashTable *procmime_get_mime_type_table (void);
55 MimeInfo *procmime_mimeinfo_new(void)
59 mimeinfo = g_new0(MimeInfo, 1);
60 mimeinfo->content = MIMECONTENT_EMPTY;
61 mimeinfo->data.filename = NULL;
63 mimeinfo->type = MIMETYPE_UNKNOWN;
64 mimeinfo->encoding_type = ENC_UNKNOWN;
65 mimeinfo->typeparameters = g_hash_table_new(g_str_hash, g_str_equal);
67 mimeinfo->disposition = DISPOSITIONTYPE_UNKNOWN;
68 mimeinfo->dispositionparameters
69 = g_hash_table_new(g_str_hash, g_str_equal);
71 mimeinfo->node = g_node_new(mimeinfo);
76 static gboolean procmime_mimeinfo_parameters_destroy(gpointer key, gpointer value, gpointer user_data)
84 static gchar *forced_charset = NULL;
86 void procmime_force_charset(const gchar *str)
88 g_free(forced_charset);
89 forced_charset = NULL;
91 forced_charset = g_strdup(str);
94 static EncodingType forced_encoding = 0;
96 void procmime_force_encoding(EncodingType encoding)
98 forced_encoding = encoding;
101 static gboolean free_func(GNode *node, gpointer data)
103 MimeInfo *mimeinfo = (MimeInfo *) node->data;
105 switch (mimeinfo->content) {
106 case MIMECONTENT_FILE:
108 g_unlink(mimeinfo->data.filename);
109 g_free(mimeinfo->data.filename);
112 case MIMECONTENT_MEM:
114 g_free(mimeinfo->data.mem);
119 g_free(mimeinfo->subtype);
120 g_free(mimeinfo->description);
121 g_free(mimeinfo->id);
122 g_free(mimeinfo->location);
124 g_hash_table_foreach_remove(mimeinfo->typeparameters,
125 procmime_mimeinfo_parameters_destroy, NULL);
126 g_hash_table_destroy(mimeinfo->typeparameters);
127 g_hash_table_foreach_remove(mimeinfo->dispositionparameters,
128 procmime_mimeinfo_parameters_destroy, NULL);
129 g_hash_table_destroy(mimeinfo->dispositionparameters);
131 if (mimeinfo->privacy)
132 privacy_free_privacydata(mimeinfo->privacy);
139 void procmime_mimeinfo_free_all(MimeInfo *mimeinfo)
146 node = mimeinfo->node;
147 g_node_traverse(node, G_IN_ORDER, G_TRAVERSE_ALL, -1, free_func, NULL);
149 g_node_destroy(node);
152 MimeInfo *procmime_mimeinfo_parent(MimeInfo *mimeinfo)
154 g_return_val_if_fail(mimeinfo != NULL, NULL);
155 g_return_val_if_fail(mimeinfo->node != NULL, NULL);
157 if (mimeinfo->node->parent == NULL)
159 return (MimeInfo *) mimeinfo->node->parent->data;
162 MimeInfo *procmime_mimeinfo_next(MimeInfo *mimeinfo)
164 g_return_val_if_fail(mimeinfo != NULL, NULL);
165 g_return_val_if_fail(mimeinfo->node != NULL, NULL);
167 if (mimeinfo->node->children)
168 return (MimeInfo *) mimeinfo->node->children->data;
169 if (mimeinfo->node->next)
170 return (MimeInfo *) mimeinfo->node->next->data;
172 if (mimeinfo->node->parent == NULL)
175 while (mimeinfo->node->parent != NULL) {
176 mimeinfo = (MimeInfo *) mimeinfo->node->parent->data;
177 if (mimeinfo->node->next)
178 return (MimeInfo *) mimeinfo->node->next->data;
184 MimeInfo *procmime_scan_message(MsgInfo *msginfo)
189 filename = procmsg_get_message_file_path(msginfo);
190 if (!filename || !is_file_exist(filename)) {
192 filename = procmsg_get_message_file(msginfo);
194 if (!filename || !is_file_exist(filename))
197 if (!folder_has_parent_of_type(msginfo->folder, F_QUEUE) &&
198 !folder_has_parent_of_type(msginfo->folder, F_DRAFT))
199 mimeinfo = procmime_scan_file(filename);
201 mimeinfo = procmime_scan_queue_file(filename);
209 H_CONTENT_TRANSFER_ENCODING = 0,
211 H_CONTENT_DISPOSITION = 2,
212 H_CONTENT_DESCRIPTION = 3,
216 const gchar *procmime_mimeinfo_get_parameter(MimeInfo *mimeinfo, const gchar *name)
220 g_return_val_if_fail(mimeinfo != NULL, NULL);
221 g_return_val_if_fail(name != NULL, NULL);
223 value = g_hash_table_lookup(mimeinfo->dispositionparameters, name);
225 value = g_hash_table_lookup(mimeinfo->typeparameters, name);
230 gboolean procmime_decode_content(MimeInfo *mimeinfo)
237 gboolean tmp_file = FALSE;
239 EncodingType encoding = forced_encoding
241 : mimeinfo->encoding_type;
243 g_return_val_if_fail(mimeinfo != NULL, FALSE);
245 if (encoding == ENC_UNKNOWN ||
246 encoding == ENC_BINARY ||
247 encoding == ENC_7BIT ||
248 encoding == ENC_8BIT)
251 infp = g_fopen(mimeinfo->data.filename, "rb");
256 fseek(infp, mimeinfo->offset, SEEK_SET);
258 outfp = get_tmpfile_in_dir(get_mime_tmp_dir(), &tmpfilename);
265 readend = mimeinfo->offset + mimeinfo->length;
267 if (encoding == ENC_QUOTED_PRINTABLE) {
268 while ((ftell(infp) < readend) && (fgets(buf, sizeof(buf), infp) != NULL)) {
270 len = qp_decode_line(buf);
271 fwrite(buf, 1, len, outfp);
273 } else if (encoding == ENC_BASE64) {
274 gchar outbuf[BUFFSIZE];
276 Base64Decoder *decoder;
277 gboolean got_error = FALSE;
278 gboolean uncanonicalize = FALSE;
281 if (mimeinfo->type == MIMETYPE_TEXT ||
282 mimeinfo->type == MIMETYPE_MESSAGE) {
283 uncanonicalize = TRUE;
284 tmpfp = my_tmpfile();
287 if (tmp_file) fclose(outfp);
293 decoder = base64_decoder_new();
294 while ((ftell(infp) < readend) && (fgets(buf, sizeof(buf), infp) != NULL)) {
295 len = base64_decoder_decode(decoder, buf, outbuf);
296 if (len < 0 && !got_error) {
297 g_warning("Bad BASE64 content.\n");
298 fwrite(_("[Error decoding BASE64]\n"),
300 strlen(_("[Error decoding BASE64]\n")),
304 } else if (len >= 0) {
305 /* print out the error message only once
307 fwrite(outbuf, sizeof(gchar), len, tmpfp);
311 base64_decoder_free(decoder);
313 if (uncanonicalize) {
315 while (fgets(buf, sizeof(buf), tmpfp) != NULL) {
321 } else if (encoding == ENC_X_UUENCODE) {
322 gchar outbuf[BUFFSIZE];
324 gboolean flag = FALSE;
326 while ((ftell(infp) < readend) && (fgets(buf, sizeof(buf), infp) != NULL)) {
327 if (!flag && strncmp(buf,"begin ", 6)) continue;
330 len = fromuutobits(outbuf, buf);
333 g_warning("Bad UUENCODE content(%d)\n", len);
336 fwrite(outbuf, sizeof(gchar), len, outfp);
341 while ((ftell(infp) < readend) && (fgets(buf, sizeof(buf), infp) != NULL)) {
349 stat(tmpfilename, &statbuf);
350 if (mimeinfo->tmp && (mimeinfo->data.filename != NULL))
351 g_unlink(mimeinfo->data.filename);
352 g_free(mimeinfo->data.filename);
353 mimeinfo->data.filename = tmpfilename;
354 mimeinfo->tmp = TRUE;
355 mimeinfo->offset = 0;
356 mimeinfo->length = statbuf.st_size;
357 mimeinfo->encoding_type = ENC_BINARY;
362 #define B64_LINE_SIZE 57
363 #define B64_BUFFSIZE 77
365 gboolean procmime_encode_content(MimeInfo *mimeinfo, EncodingType encoding)
367 FILE *infp = NULL, *outfp;
372 if (mimeinfo->content == MIMECONTENT_EMPTY)
375 if (mimeinfo->encoding_type != ENC_UNKNOWN &&
376 mimeinfo->encoding_type != ENC_BINARY &&
377 mimeinfo->encoding_type != ENC_7BIT &&
378 mimeinfo->encoding_type != ENC_8BIT)
379 if(!procmime_decode_content(mimeinfo))
382 outfp = get_tmpfile_in_dir(get_mime_tmp_dir(), &tmpfilename);
388 if (mimeinfo->content == MIMECONTENT_FILE) {
389 if ((infp = g_fopen(mimeinfo->data.filename, "rb")) == NULL) {
390 g_warning("Can't open file %s\n", mimeinfo->data.filename);
393 } else if (mimeinfo->content == MIMECONTENT_MEM) {
394 infp = str_open_as_stream(mimeinfo->data.mem);
399 if (encoding == ENC_BASE64) {
400 gchar inbuf[B64_LINE_SIZE], outbuf[B64_BUFFSIZE];
402 gchar *tmp_file = NULL;
404 if (mimeinfo->type == MIMETYPE_TEXT ||
405 mimeinfo->type == MIMETYPE_MESSAGE) {
406 if (mimeinfo->content == MIMECONTENT_FILE) {
407 tmp_file = get_tmp_file();
408 if (canonicalize_file(mimeinfo->data.filename, tmp_file) < 0) {
413 if ((tmp_fp = g_fopen(tmp_file, "rb")) == NULL) {
414 FILE_OP_ERROR(tmp_file, "fopen");
421 gchar *out = canonicalize_str(mimeinfo->data.mem);
423 infp = str_open_as_stream(out);
431 while ((len = fread(inbuf, sizeof(gchar),
432 B64_LINE_SIZE, tmp_fp))
434 base64_encode(outbuf, inbuf, B64_LINE_SIZE);
435 fputs(outbuf, outfp);
438 if (len > 0 && feof(tmp_fp)) {
439 base64_encode(outbuf, inbuf, len);
440 fputs(outbuf, outfp);
449 } else if (encoding == ENC_QUOTED_PRINTABLE) {
450 gchar inbuf[BUFFSIZE], outbuf[BUFFSIZE * 4];
452 while (fgets(inbuf, sizeof(inbuf), infp) != NULL) {
453 qp_encode_line(outbuf, inbuf);
455 if (!strncmp("From ", outbuf, sizeof("From ")-1)) {
456 gchar *tmpbuf = outbuf;
458 tmpbuf += sizeof("From ")-1;
460 fputs("=46rom ", outfp);
461 fputs(tmpbuf, outfp);
463 fputs(outbuf, outfp);
468 while (fgets(buf, sizeof(buf), infp) != NULL) {
477 if (mimeinfo->content == MIMECONTENT_FILE) {
478 if (mimeinfo->tmp && (mimeinfo->data.filename != NULL))
479 g_unlink(mimeinfo->data.filename);
480 g_free(mimeinfo->data.filename);
481 } else if (mimeinfo->content == MIMECONTENT_MEM) {
482 if (mimeinfo->tmp && (mimeinfo->data.mem != NULL))
483 g_free(mimeinfo->data.mem);
486 stat(tmpfilename, &statbuf);
487 mimeinfo->content = MIMECONTENT_FILE;
488 mimeinfo->data.filename = tmpfilename;
489 mimeinfo->tmp = TRUE;
490 mimeinfo->offset = 0;
491 mimeinfo->length = statbuf.st_size;
492 mimeinfo->encoding_type = encoding;
497 gint procmime_get_part(const gchar *outfile, MimeInfo *mimeinfo)
501 gint restlength, readlength;
503 g_return_val_if_fail(outfile != NULL, -1);
504 g_return_val_if_fail(mimeinfo != NULL, -1);
506 if (mimeinfo->encoding_type != ENC_BINARY && !procmime_decode_content(mimeinfo))
509 if ((infp = g_fopen(mimeinfo->data.filename, "rb")) == NULL) {
510 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
513 if (fseek(infp, mimeinfo->offset, SEEK_SET) < 0) {
514 FILE_OP_ERROR(mimeinfo->data.filename, "fseek");
518 if ((outfp = g_fopen(outfile, "wb")) == NULL) {
519 FILE_OP_ERROR(outfile, "fopen");
524 restlength = mimeinfo->length;
526 while ((restlength > 0) && ((readlength = fread(buf, 1, restlength > BUFFSIZE ? BUFFSIZE : restlength, infp)) > 0)) {
527 fwrite(buf, 1, readlength, outfp);
528 restlength -= readlength;
532 if (fclose(outfp) == EOF) {
533 FILE_OP_ERROR(outfile, "fclose");
541 struct ContentRenderer {
546 static GList * renderer_list = NULL;
548 static struct ContentRenderer *
549 content_renderer_new(char * content_type, char * renderer)
551 struct ContentRenderer * cr;
553 cr = g_new(struct ContentRenderer, 1);
557 cr->content_type = g_strdup(content_type);
558 cr->renderer = g_strdup(renderer);
563 static void content_renderer_free(struct ContentRenderer * cr)
565 g_free(cr->content_type);
566 g_free(cr->renderer);
570 void renderer_read_config(void)
576 g_list_foreach(renderer_list, (GFunc) content_renderer_free, NULL);
577 renderer_list = NULL;
579 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, RENDERER_RC, NULL);
580 f = g_fopen(rcpath, "rb");
586 while (fgets(buf, BUFFSIZE, f)) {
588 struct ContentRenderer * cr;
591 p = strchr(buf, ' ');
596 cr = content_renderer_new(buf, p + 1);
600 renderer_list = g_list_append(renderer_list, cr);
606 void renderer_write_config(void)
612 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, RENDERER_RC, NULL);
614 if ((pfile = prefs_write_open(rcpath)) == NULL) {
615 g_warning("failed to write configuration to file\n");
622 for (cur = renderer_list ; cur != NULL ; cur = cur->next) {
623 struct ContentRenderer * renderer;
624 renderer = cur->data;
625 fprintf(pfile->fp, "%s %s\n", renderer->content_type,
629 if (prefs_file_close(pfile) < 0) {
630 g_warning("failed to write configuration to file\n");
635 FILE *procmime_get_text_content(MimeInfo *mimeinfo)
638 const gchar *src_codeset;
639 gboolean conv_fail = FALSE;
642 struct ContentRenderer * renderer;
644 gchar *tmpfile, *content_type;
646 g_return_val_if_fail(mimeinfo != NULL, NULL);
648 if (!procmime_decode_content(mimeinfo))
651 tmpfile = procmime_get_tmp_file_name(mimeinfo);
655 if (procmime_get_part(tmpfile, mimeinfo) < 0) {
660 tmpfp = g_fopen(tmpfile, "rb");
666 if ((outfp = my_tmpfile()) == NULL) {
673 src_codeset = forced_charset
675 procmime_mimeinfo_get_parameter(mimeinfo, "charset");
679 content_type = procmime_get_content_type_str(mimeinfo->type,
681 for (cur = renderer_list ; cur != NULL ; cur = cur->next) {
682 struct ContentRenderer * cr;
685 if (g_ascii_strcasecmp(cr->content_type, content_type) == 0) {
690 g_free(content_type);
692 if (renderer != NULL) {
698 dup2(fileno(outfp), 1);
700 p = popen(renderer->renderer, "w");
705 fread(buf, sizeof(char), sizeof(buf),
707 fwrite(buf, sizeof(char), count, p);
712 /* CodeConverter seems to have no effect here */
713 } else if (mimeinfo->type == MIMETYPE_TEXT && !g_ascii_strcasecmp(mimeinfo->subtype, "html")) {
714 SC_HTMLParser *parser;
717 conv = conv_code_converter_new(src_codeset);
718 parser = sc_html_parser_new(tmpfp, conv);
719 while ((str = sc_html_parse(parser)) != NULL) {
722 sc_html_parser_destroy(parser);
723 conv_code_converter_destroy(conv);
724 } else if (mimeinfo->type == MIMETYPE_TEXT && !g_ascii_strcasecmp(mimeinfo->subtype, "enriched")) {
728 conv = conv_code_converter_new(src_codeset);
729 parser = ertf_parser_new(tmpfp, conv);
730 while ((str = ertf_parse(parser)) != NULL) {
733 ertf_parser_destroy(parser);
734 conv_code_converter_destroy(conv);
735 } else if (mimeinfo->type == MIMETYPE_TEXT) {
736 while (fgets(buf, sizeof(buf), tmpfp) != NULL) {
737 str = conv_codeset_strdup(buf, src_codeset, CS_UTF_8);
749 g_warning("procmime_get_text_content(): Code conversion failed.\n");
759 /* search the first text part of (multipart) MIME message,
760 decode, convert it and output to outfp. */
761 FILE *procmime_get_first_text_content(MsgInfo *msginfo)
764 MimeInfo *mimeinfo, *partinfo;
766 g_return_val_if_fail(msginfo != NULL, NULL);
768 mimeinfo = procmime_scan_message(msginfo);
769 if (!mimeinfo) return NULL;
772 while (partinfo && partinfo->type != MIMETYPE_TEXT) {
773 partinfo = procmime_mimeinfo_next(partinfo);
776 outfp = procmime_get_text_content(partinfo);
778 procmime_mimeinfo_free_all(mimeinfo);
784 static gboolean find_encrypted_func(GNode *node, gpointer data)
786 MimeInfo *mimeinfo = (MimeInfo *) node->data;
787 MimeInfo **encinfo = (MimeInfo **) data;
789 if (privacy_mimeinfo_is_encrypted(mimeinfo)) {
797 static MimeInfo *find_encrypted_part(MimeInfo *rootinfo)
799 MimeInfo *encinfo = NULL;
801 g_node_traverse(rootinfo->node, G_IN_ORDER, G_TRAVERSE_ALL, -1,
802 find_encrypted_func, &encinfo);
807 /* search the first encrypted text part of (multipart) MIME message,
808 decode, convert it and output to outfp. */
809 FILE *procmime_get_first_encrypted_text_content(MsgInfo *msginfo)
812 MimeInfo *mimeinfo, *partinfo, *encinfo;
814 g_return_val_if_fail(msginfo != NULL, NULL);
816 mimeinfo = procmime_scan_message(msginfo);
822 if ((encinfo = find_encrypted_part(partinfo)) != NULL) {
823 debug_print("decrypting message part\n");
824 if (privacy_mimeinfo_decrypt(encinfo) < 0) {
825 alertpanel_error(_("Couldn't decrypt: %s"),
826 privacy_get_error());
831 while (partinfo && partinfo->type != MIMETYPE_TEXT) {
832 partinfo = procmime_mimeinfo_next(partinfo);
836 outfp = procmime_get_text_content(partinfo);
838 procmime_mimeinfo_free_all(mimeinfo);
843 gboolean procmime_msginfo_is_encrypted(MsgInfo *msginfo)
845 MimeInfo *mimeinfo, *partinfo;
846 gboolean result = FALSE;
848 g_return_val_if_fail(msginfo != NULL, FALSE);
850 mimeinfo = procmime_scan_message(msginfo);
856 result = (find_encrypted_part(partinfo) != NULL);
857 procmime_mimeinfo_free_all(mimeinfo);
862 gboolean procmime_find_string_part(MimeInfo *mimeinfo, const gchar *filename,
863 const gchar *str, StrFindFunc find_func)
868 g_return_val_if_fail(mimeinfo != NULL, FALSE);
869 g_return_val_if_fail(mimeinfo->type == MIMETYPE_TEXT, FALSE);
870 g_return_val_if_fail(str != NULL, FALSE);
871 g_return_val_if_fail(find_func != NULL, FALSE);
873 outfp = procmime_get_text_content(mimeinfo);
878 while (fgets(buf, sizeof(buf), outfp) != NULL) {
880 if (find_func(buf, str)) {
891 gboolean procmime_find_string(MsgInfo *msginfo, const gchar *str,
892 StrFindFunc find_func)
897 gboolean found = FALSE;
899 g_return_val_if_fail(msginfo != NULL, FALSE);
900 g_return_val_if_fail(str != NULL, FALSE);
901 g_return_val_if_fail(find_func != NULL, FALSE);
903 filename = procmsg_get_message_file(msginfo);
904 if (!filename) return FALSE;
905 mimeinfo = procmime_scan_message(msginfo);
907 for (partinfo = mimeinfo; partinfo != NULL;
908 partinfo = procmime_mimeinfo_next(partinfo)) {
909 if (partinfo->type == MIMETYPE_TEXT) {
910 if (procmime_find_string_part
911 (partinfo, filename, str, find_func) == TRUE) {
918 procmime_mimeinfo_free_all(mimeinfo);
924 gchar *procmime_get_tmp_file_name(MimeInfo *mimeinfo)
926 static guint32 id = 0;
931 g_return_val_if_fail(mimeinfo != NULL, NULL);
933 g_snprintf(f_prefix, sizeof(f_prefix), "%08x.", id++);
935 if ((mimeinfo->type == MIMETYPE_TEXT) && !g_ascii_strcasecmp(mimeinfo->subtype, "html"))
936 base = g_strdup("mimetmp.html");
938 const gchar *basetmp;
940 basetmp = procmime_mimeinfo_get_parameter(mimeinfo, "filename");
942 basetmp = procmime_mimeinfo_get_parameter(mimeinfo, "name");
945 basetmp = g_path_get_basename(basetmp);
946 if (*basetmp == '\0')
947 basetmp = g_strdup("mimetmp");
948 base = conv_filename_from_utf8(basetmp);
949 g_free((gchar*)basetmp);
950 subst_for_shellsafe_filename(base);
953 filename = g_strconcat(get_mime_tmp_dir(), G_DIR_SEPARATOR_S,
954 f_prefix, base, NULL);
961 static GList *mime_type_list = NULL;
963 gchar *procmime_get_mime_type(const gchar *filename)
965 static GHashTable *mime_type_table = NULL;
971 if (!mime_type_table) {
972 mime_type_table = procmime_get_mime_type_table();
973 if (!mime_type_table) return NULL;
976 base = g_path_get_basename(filename);
977 if ((p = strrchr(base, '.')) != NULL)
978 Xstrdup_a(ext, p + 1, p = NULL );
983 mime_type = g_hash_table_lookup(mime_type_table, ext);
987 str = g_strconcat(mime_type->type, "/", mime_type->sub_type,
995 static guint procmime_str_hash(gconstpointer gptr)
997 guint hash_result = 0;
1000 for (str = gptr; str && *str; str++) {
1001 if (isupper(*str)) hash_result += (*str + ' ');
1002 else hash_result += *str;
1008 static gint procmime_str_equal(gconstpointer gptr1, gconstpointer gptr2)
1010 const char *str1 = gptr1;
1011 const char *str2 = gptr2;
1013 return !g_utf8_collate(str1, str2);
1016 static GHashTable *procmime_get_mime_type_table(void)
1018 GHashTable *table = NULL;
1020 MimeType *mime_type;
1023 if (!mime_type_list) {
1024 mime_type_list = procmime_get_mime_type_list();
1025 if (!mime_type_list) return NULL;
1028 table = g_hash_table_new(procmime_str_hash, procmime_str_equal);
1030 for (cur = mime_type_list; cur != NULL; cur = cur->next) {
1034 mime_type = (MimeType *)cur->data;
1036 if (!mime_type->extension) continue;
1038 exts = g_strsplit(mime_type->extension, " ", 16);
1039 for (i = 0; exts[i] != NULL; i++) {
1040 /* make the key case insensitive */
1042 /* use previously dup'd key on overwriting */
1043 if (g_hash_table_lookup(table, exts[i]))
1046 key = g_strdup(exts[i]);
1047 g_hash_table_insert(table, key, mime_type);
1055 GList *procmime_get_mime_type_list(void)
1059 gchar buf[BUFFSIZE];
1062 MimeType *mime_type;
1063 gboolean fp_is_glob_file = TRUE;
1066 return mime_type_list;
1068 if ((fp = g_fopen("/usr/share/mime/globs", "rb")) == NULL) {
1069 fp_is_glob_file = FALSE;
1070 if ((fp = g_fopen("/etc/mime.types", "rb")) == NULL) {
1071 if ((fp = g_fopen(SYSCONFDIR "/mime.types", "rb"))
1073 FILE_OP_ERROR(SYSCONFDIR "/mime.types",
1080 while (fgets(buf, sizeof(buf), fp) != NULL) {
1081 p = strchr(buf, '#');
1087 if (fp_is_glob_file) {
1088 while (*p && !g_ascii_isspace(*p) && (*p!=':')) p++;
1090 while (*p && !g_ascii_isspace(*p)) p++;
1097 delim = strchr(buf, '/');
1098 if (delim == NULL) continue;
1101 mime_type = g_new(MimeType, 1);
1102 mime_type->type = g_strdup(buf);
1103 mime_type->sub_type = g_strdup(delim + 1);
1105 if (fp_is_glob_file) {
1106 while (*p && (g_ascii_isspace(*p)||(*p=='*')||(*p=='.'))) p++;
1108 while (*p && g_ascii_isspace(*p)) p++;
1112 mime_type->extension = g_strdup(p);
1114 mime_type->extension = NULL;
1116 list = g_list_append(list, mime_type);
1122 g_warning("Can't read mime.types\n");
1127 EncodingType procmime_get_encoding_for_charset(const gchar *charset)
1131 else if (!g_ascii_strncasecmp(charset, "ISO-2022-", 9) ||
1132 !g_ascii_strcasecmp(charset, "US-ASCII"))
1134 else if (!g_ascii_strcasecmp(charset, "ISO-8859-5") ||
1135 !g_ascii_strncasecmp(charset, "KOI8-", 5) ||
1136 !g_ascii_strcasecmp(charset, "Windows-1251"))
1138 else if (!g_ascii_strncasecmp(charset, "ISO-8859-", 9))
1139 return ENC_QUOTED_PRINTABLE;
1140 else if (!g_ascii_strncasecmp(charset, "UTF-8", 5))
1141 return ENC_QUOTED_PRINTABLE;
1146 EncodingType procmime_get_encoding_for_text_file(const gchar *file, gboolean *has_binary)
1149 guchar buf[BUFFSIZE];
1151 size_t octet_chars = 0;
1152 size_t total_len = 0;
1153 gfloat octet_percentage;
1154 gboolean force_b64 = FALSE;
1156 if ((fp = g_fopen(file, "rb")) == NULL) {
1157 FILE_OP_ERROR(file, "fopen");
1161 while ((len = fread(buf, sizeof(guchar), sizeof(buf), fp)) > 0) {
1165 for (p = buf, i = 0; i < len; ++p, ++i) {
1179 octet_percentage = (gfloat)octet_chars / (gfloat)total_len;
1181 octet_percentage = 0.0;
1183 debug_print("procmime_get_encoding_for_text_file(): "
1184 "8bit chars: %d / %d (%f%%)\n", octet_chars, total_len,
1185 100.0 * octet_percentage);
1187 if (octet_percentage > 0.20 || force_b64) {
1188 debug_print("using BASE64\n");
1190 } else if (octet_chars > 0) {
1191 debug_print("using quoted-printable\n");
1192 return ENC_QUOTED_PRINTABLE;
1194 debug_print("using 7bit\n");
1199 struct EncodingTable
1202 EncodingType enc_type;
1205 struct EncodingTable encoding_table[] = {
1208 {"binary", ENC_BINARY},
1209 {"quoted-printable", ENC_QUOTED_PRINTABLE},
1210 {"base64", ENC_BASE64},
1211 {"x-uuencode", ENC_UNKNOWN},
1212 {NULL, ENC_UNKNOWN},
1215 const gchar *procmime_get_encoding_str(EncodingType encoding)
1217 struct EncodingTable *enc_table;
1219 for (enc_table = encoding_table; enc_table->str != NULL; enc_table++) {
1220 if (enc_table->enc_type == encoding)
1221 return enc_table->str;
1226 /* --- NEW MIME STUFF --- */
1233 static struct TypeTable mime_type_table[] = {
1234 {"text", MIMETYPE_TEXT},
1235 {"image", MIMETYPE_IMAGE},
1236 {"audio", MIMETYPE_AUDIO},
1237 {"video", MIMETYPE_VIDEO},
1238 {"application", MIMETYPE_APPLICATION},
1239 {"message", MIMETYPE_MESSAGE},
1240 {"multipart", MIMETYPE_MULTIPART},
1244 const gchar *procmime_get_media_type_str(MimeMediaType type)
1246 struct TypeTable *type_table;
1248 for (type_table = mime_type_table; type_table->str != NULL; type_table++) {
1249 if (type_table->type == type)
1250 return type_table->str;
1255 MimeMediaType procmime_get_media_type(const gchar *str)
1257 struct TypeTable *typetablearray;
1259 for (typetablearray = mime_type_table; typetablearray->str != NULL; typetablearray++)
1260 if (g_ascii_strncasecmp(str, typetablearray->str, strlen(typetablearray->str)) == 0)
1261 return typetablearray->type;
1263 return MIMETYPE_UNKNOWN;
1267 *\brief Safe wrapper for content type string.
1269 *\return const gchar * Pointer to content type string.
1271 gchar *procmime_get_content_type_str(MimeMediaType type,
1272 const char *subtype)
1274 const gchar *type_str = NULL;
1276 if (subtype == NULL || !(type_str = procmime_get_media_type_str(type)))
1277 return g_strdup("unknown");
1278 return g_strdup_printf("%s/%s", type_str, subtype);
1281 static int procmime_parse_mimepart(MimeInfo *parent,
1282 gchar *content_type,
1283 gchar *content_encoding,
1284 gchar *content_description,
1286 gchar *content_disposition,
1287 gchar *content_location,
1288 const gchar *filename,
1292 void procmime_parse_message_rfc822(MimeInfo *mimeinfo)
1294 HeaderEntry hentry[] = {{"Content-Type:", NULL, TRUE},
1295 {"Content-Transfer-Encoding:",
1297 {"Content-Description:",
1301 {"Content-Disposition:",
1303 {"Content-Location:",
1307 {NULL, NULL, FALSE}};
1308 guint content_start, i;
1312 procmime_decode_content(mimeinfo);
1314 fp = g_fopen(mimeinfo->data.filename, "rb");
1316 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
1319 fseek(fp, mimeinfo->offset, SEEK_SET);
1320 procheader_get_header_fields(fp, hentry);
1321 if (hentry[0].body != NULL) {
1322 tmp = conv_unmime_header(hentry[0].body, NULL);
1323 g_free(hentry[0].body);
1324 hentry[0].body = tmp;
1326 if (hentry[2].body != NULL) {
1327 tmp = conv_unmime_header(hentry[2].body, NULL);
1328 g_free(hentry[2].body);
1329 hentry[2].body = tmp;
1331 if (hentry[4].body != NULL) {
1332 tmp = conv_unmime_header(hentry[4].body, NULL);
1333 g_free(hentry[4].body);
1334 hentry[4].body = tmp;
1336 if (hentry[5].body != NULL) {
1337 tmp = conv_unmime_header(hentry[5].body, NULL);
1338 g_free(hentry[5].body);
1339 hentry[5].body = tmp;
1341 content_start = ftell(fp);
1344 procmime_parse_mimepart(mimeinfo,
1345 hentry[0].body, hentry[1].body,
1346 hentry[2].body, hentry[3].body,
1347 hentry[4].body, hentry[5].body,
1348 mimeinfo->data.filename, content_start,
1349 mimeinfo->length - (content_start - mimeinfo->offset));
1351 for (i = 0; i < (sizeof hentry / sizeof hentry[0]); i++) {
1352 g_free(hentry[i].body);
1353 hentry[i].body = NULL;
1357 void procmime_parse_multipart(MimeInfo *mimeinfo)
1359 HeaderEntry hentry[] = {{"Content-Type:", NULL, TRUE},
1360 {"Content-Transfer-Encoding:",
1362 {"Content-Description:",
1366 {"Content-Disposition:",
1368 {"Content-Location:",
1370 {NULL, NULL, FALSE}};
1373 gint boundary_len = 0, lastoffset = -1, i;
1374 gchar buf[BUFFSIZE];
1378 boundary = g_hash_table_lookup(mimeinfo->typeparameters, "boundary");
1381 boundary_len = strlen(boundary);
1383 procmime_decode_content(mimeinfo);
1385 fp = g_fopen(mimeinfo->data.filename, "rb");
1387 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
1390 fseek(fp, mimeinfo->offset, SEEK_SET);
1391 while ((p = fgets(buf, sizeof(buf), fp)) != NULL && result == 0) {
1392 if (ftell(fp) - 1 > (mimeinfo->offset + mimeinfo->length))
1395 if (IS_BOUNDARY(buf, boundary, boundary_len)) {
1396 if (lastoffset != -1) {
1397 result = procmime_parse_mimepart(mimeinfo,
1398 hentry[0].body, hentry[1].body,
1399 hentry[2].body, hentry[3].body,
1400 hentry[4].body, hentry[5].body,
1401 mimeinfo->data.filename, lastoffset,
1402 (ftell(fp) - strlen(buf)) - lastoffset - 1);
1405 if (buf[2 + boundary_len] == '-' &&
1406 buf[2 + boundary_len + 1] == '-')
1409 for (i = 0; i < (sizeof hentry / sizeof hentry[0]) ; i++) {
1410 g_free(hentry[i].body);
1411 hentry[i].body = NULL;
1413 procheader_get_header_fields(fp, hentry);
1414 if (hentry[0].body != NULL) {
1415 tmp = conv_unmime_header(hentry[0].body, NULL);
1416 g_free(hentry[0].body);
1417 hentry[0].body = tmp;
1419 if (hentry[2].body != NULL) {
1420 tmp = conv_unmime_header(hentry[2].body, NULL);
1421 g_free(hentry[2].body);
1422 hentry[2].body = tmp;
1424 if (hentry[4].body != NULL) {
1425 tmp = conv_unmime_header(hentry[4].body, NULL);
1426 g_free(hentry[4].body);
1427 hentry[4].body = tmp;
1429 if (hentry[5].body != NULL) {
1430 tmp = conv_unmime_header(hentry[5].body, NULL);
1431 g_free(hentry[5].body);
1432 hentry[5].body = tmp;
1434 lastoffset = ftell(fp);
1437 for (i = 0; i < (sizeof hentry / sizeof hentry[0]); i++) {
1438 g_free(hentry[i].body);
1439 hentry[i].body = NULL;
1444 static void parse_parameters(const gchar *parameters, GHashTable *table)
1446 gchar *params, *param, *next;
1447 GSList *convlist = NULL, *concatlist = NULL, *cur;
1449 params = g_strdup(parameters);
1452 for (; next != NULL; param = next) {
1453 gchar *attribute, *value, *tmp;
1455 gboolean convert = FALSE;
1457 next = strchr_with_skip_quote(param, '"', ';');
1466 value = strchr(attribute, '=');
1472 while (value[0] == ' ')
1475 g_strdown(attribute);
1477 len = strlen(attribute);
1478 if (attribute[len - 1] == '*') {
1479 gchar *srcpos, *dstpos, *endpos;
1482 attribute[len - 1] = '\0';
1486 endpos = value + strlen(value);
1487 while (srcpos < endpos) {
1493 if (!get_hex_value(&dstvalue, srcpos[1], srcpos[2]))
1504 if (value[0] == '"')
1505 extract_quote(value, '"');
1506 else if ((tmp = strchr(value, ' ')) != NULL)
1511 while (attribute[0] == ' ')
1513 while (attribute[strlen(attribute)-1] == ' ')
1514 attribute[strlen(attribute)-1] = '\0';
1517 while (value[0] == ' ')
1519 while (value[strlen(value)-1] == ' ')
1520 value[strlen(value)-1] = '\0';
1522 if (strrchr(attribute, '*') != NULL) {
1525 tmpattr = g_strdup(attribute);
1526 tmp = strrchr(tmpattr, '*');
1529 if ((tmp[1] == '0') && (tmp[2] == '\0') &&
1530 (g_slist_find_custom(concatlist, attribute, g_str_equal) == NULL))
1531 concatlist = g_slist_prepend(concatlist, g_strdup(tmpattr));
1533 if (convert && (g_slist_find_custom(convlist, attribute, g_str_equal) == NULL))
1534 convlist = g_slist_prepend(convlist, g_strdup(tmpattr));
1537 } else if (convert) {
1538 if (g_slist_find_custom(convlist, attribute, g_str_equal) == NULL)
1539 convlist = g_slist_prepend(convlist, g_strdup(attribute));
1542 if (g_hash_table_lookup(table, attribute) == NULL)
1543 g_hash_table_insert(table, g_strdup(attribute), g_strdup(value));
1546 for (cur = concatlist; cur != NULL; cur = g_slist_next(cur)) {
1547 gchar *attribute, *attrwnum, *partvalue;
1551 attribute = (gchar *) cur->data;
1552 value = g_string_sized_new(64);
1554 attrwnum = g_strdup_printf("%s*%d", attribute, n);
1555 while ((partvalue = g_hash_table_lookup(table, attrwnum)) != NULL) {
1556 g_string_append(value, partvalue);
1560 attrwnum = g_strdup_printf("%s*%d", attribute, n);
1564 g_hash_table_insert(table, g_strdup(attribute), g_strdup(value->str));
1565 g_string_free(value, TRUE);
1567 slist_free_strings(concatlist);
1568 g_slist_free(concatlist);
1570 for (cur = convlist; cur != NULL; cur = g_slist_next(cur)) {
1571 gchar *attribute, *key, *value;
1572 gchar *charset, *lang, *oldvalue, *newvalue;
1574 attribute = (gchar *) cur->data;
1575 if (!g_hash_table_lookup_extended(
1576 table, attribute, (gpointer *)(gchar *) &key, (gpointer *)(gchar *) &value))
1580 lang = strchr(charset, '\'');
1585 oldvalue = strchr(lang, '\'');
1586 if (oldvalue == NULL)
1591 newvalue = conv_codeset_strdup(oldvalue, charset, CS_UTF_8);
1593 g_hash_table_remove(table, attribute);
1597 g_hash_table_insert(table, g_strdup(attribute), newvalue);
1599 slist_free_strings(convlist);
1600 g_slist_free(convlist);
1605 static void procmime_parse_content_type(const gchar *content_type, MimeInfo *mimeinfo)
1607 g_return_if_fail(content_type != NULL);
1608 g_return_if_fail(mimeinfo != NULL);
1610 /* RFC 2045, page 13 says that the mime subtype is MANDATORY;
1611 * if it's not available we use the default Content-Type */
1612 if ((content_type[0] == '\0') || (strchr(content_type, '/') == NULL)) {
1613 mimeinfo->type = MIMETYPE_TEXT;
1614 mimeinfo->subtype = g_strdup("plain");
1615 if (g_hash_table_lookup(mimeinfo->typeparameters,
1616 "charset") == NULL) {
1617 g_hash_table_insert(mimeinfo->typeparameters,
1618 g_strdup("charset"),
1620 conv_get_locale_charset_str_no_utf8()));
1623 gchar *type, *subtype, *params;
1625 type = g_strdup(content_type);
1626 subtype = strchr(type, '/') + 1;
1627 *(subtype - 1) = '\0';
1628 if ((params = strchr(subtype, ';')) != NULL) {
1633 mimeinfo->type = procmime_get_media_type(type);
1634 mimeinfo->subtype = g_strdup(subtype);
1636 /* Get mimeinfo->typeparameters */
1638 parse_parameters(params, mimeinfo->typeparameters);
1644 static void procmime_parse_content_disposition(const gchar *content_disposition, MimeInfo *mimeinfo)
1646 gchar *tmp, *params;
1648 g_return_if_fail(content_disposition != NULL);
1649 g_return_if_fail(mimeinfo != NULL);
1651 tmp = g_strdup(content_disposition);
1652 if ((params = strchr(tmp, ';')) != NULL) {
1658 if (!g_ascii_strcasecmp(tmp, "inline"))
1659 mimeinfo->disposition = DISPOSITIONTYPE_INLINE;
1660 else if (!g_ascii_strcasecmp(tmp, "attachment"))
1661 mimeinfo->disposition = DISPOSITIONTYPE_ATTACHMENT;
1663 mimeinfo->disposition = DISPOSITIONTYPE_ATTACHMENT;
1666 parse_parameters(params, mimeinfo->dispositionparameters);
1672 static void procmime_parse_content_encoding(const gchar *content_encoding, MimeInfo *mimeinfo)
1674 struct EncodingTable *enc_table;
1676 for (enc_table = encoding_table; enc_table->str != NULL; enc_table++) {
1677 if (g_ascii_strcasecmp(enc_table->str, content_encoding) == 0) {
1678 mimeinfo->encoding_type = enc_table->enc_type;
1682 mimeinfo->encoding_type = ENC_UNKNOWN;
1686 static int procmime_parse_mimepart(MimeInfo *parent,
1687 gchar *content_type,
1688 gchar *content_encoding,
1689 gchar *content_description,
1691 gchar *content_disposition,
1692 gchar *content_location,
1693 const gchar *filename,
1699 /* Create MimeInfo */
1700 mimeinfo = procmime_mimeinfo_new();
1701 mimeinfo->content = MIMECONTENT_FILE;
1702 if (parent != NULL) {
1703 if (g_node_depth(parent->node) > 32) {
1704 /* 32 is an arbitrary value
1705 * this avoids DOSsing ourselves
1706 * with enormous messages
1708 procmime_mimeinfo_free_all(mimeinfo);
1711 g_node_append(parent->node, mimeinfo->node);
1713 mimeinfo->data.filename = g_strdup(filename);
1714 mimeinfo->offset = offset;
1715 mimeinfo->length = length;
1717 if (content_type != NULL) {
1718 procmime_parse_content_type(content_type, mimeinfo);
1720 mimeinfo->type = MIMETYPE_TEXT;
1721 mimeinfo->subtype = g_strdup("plain");
1722 if (g_hash_table_lookup(mimeinfo->typeparameters,
1723 "charset") == NULL) {
1724 g_hash_table_insert(mimeinfo->typeparameters,
1725 g_strdup("charset"),
1727 conv_get_locale_charset_str_no_utf8()));
1731 if (content_encoding != NULL) {
1732 procmime_parse_content_encoding(content_encoding, mimeinfo);
1734 mimeinfo->encoding_type = ENC_UNKNOWN;
1737 if (content_description != NULL)
1738 mimeinfo->description = g_strdup(content_description);
1740 mimeinfo->description = NULL;
1742 if (content_id != NULL)
1743 mimeinfo->id = g_strdup(content_id);
1745 mimeinfo->id = NULL;
1747 if (content_location != NULL)
1748 mimeinfo->location = g_strdup(content_location);
1750 mimeinfo->location = NULL;
1752 if (content_disposition != NULL)
1753 procmime_parse_content_disposition(content_disposition, mimeinfo);
1755 mimeinfo->disposition = DISPOSITIONTYPE_UNKNOWN;
1757 /* Call parser for mime type */
1758 switch (mimeinfo->type) {
1759 case MIMETYPE_MESSAGE:
1760 if (g_ascii_strcasecmp(mimeinfo->subtype, "rfc822") == 0) {
1761 procmime_parse_message_rfc822(mimeinfo);
1765 case MIMETYPE_MULTIPART:
1766 procmime_parse_multipart(mimeinfo);
1776 static gchar *typenames[] = {
1787 static gboolean output_func(GNode *node, gpointer data)
1790 MimeInfo *mimeinfo = (MimeInfo *) node->data;
1792 depth = g_node_depth(node);
1793 for (i = 0; i < depth; i++)
1795 printf("%s/%s (offset:%d length:%d encoding: %d)\n", typenames[mimeinfo->type], mimeinfo->subtype, mimeinfo->offset, mimeinfo->length, mimeinfo->encoding_type);
1800 static void output_mime_structure(MimeInfo *mimeinfo, int indent)
1802 g_node_traverse(mimeinfo->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1, output_func, NULL);
1805 MimeInfo *procmime_scan_file_with_offset(const gchar *filename, int offset)
1810 stat(filename, &buf);
1812 mimeinfo = procmime_mimeinfo_new();
1813 mimeinfo->content = MIMECONTENT_FILE;
1814 mimeinfo->encoding_type = ENC_UNKNOWN;
1815 mimeinfo->type = MIMETYPE_MESSAGE;
1816 mimeinfo->subtype = g_strdup("rfc822");
1817 mimeinfo->data.filename = g_strdup(filename);
1818 mimeinfo->offset = offset;
1819 mimeinfo->length = buf.st_size - offset;
1821 procmime_parse_message_rfc822(mimeinfo);
1822 if (debug_get_mode())
1823 output_mime_structure(mimeinfo, 0);
1828 MimeInfo *procmime_scan_file(const gchar *filename)
1832 g_return_val_if_fail(filename != NULL, NULL);
1834 mimeinfo = procmime_scan_file_with_offset(filename, 0);
1839 MimeInfo *procmime_scan_queue_file(const gchar *filename)
1843 gchar buf[BUFFSIZE];
1846 g_return_val_if_fail(filename != NULL, NULL);
1849 if ((fp = g_fopen(filename, "rb")) == NULL)
1851 /* Skip queue header */
1852 while (fgets(buf, sizeof(buf), fp) != NULL) {
1854 if (!strncmp(buf, "X-Sylpheed-End-Special-Headers: 1",
1855 strlen("X-Sylpheed-End-Special-Headers:")))
1858 if (buf[0] == '\r' || buf[0] == '\n') break;
1859 /* from other mailers */
1860 if (!strncmp(buf, "Date: ", 6)
1861 || !strncmp(buf, "To: ", 4)
1862 || !strncmp(buf, "From: ", 6)
1863 || !strncmp(buf, "Subject: ", 9)) {
1871 mimeinfo = procmime_scan_file_with_offset(filename, offset);
1878 ENC_AS_QUOTED_STRING,
1883 typedef struct _ParametersData {
1889 static void write_parameters(gpointer key, gpointer value, gpointer user_data)
1892 gchar *val = value, *valpos, *tmp;
1893 ParametersData *pdata = (ParametersData *)user_data;
1894 GString *buf = g_string_new("");
1896 EncodeAs encas = ENC_AS_TOKEN;
1898 for (valpos = val; *valpos != 0; valpos++) {
1899 if (!IS_ASCII(*valpos) || *valpos == '"') {
1900 encas = ENC_AS_EXTENDED;
1905 if (((*valpos >= 0) && (*valpos < 037)) || (*valpos == 0177)) {
1906 encas = ENC_AS_QUOTED_STRING;
1910 /* tspecials + SPACE */
1928 encas = ENC_AS_QUOTED_STRING;
1933 if (encas == ENC_AS_EXTENDED && pdata->ascii_only == TRUE)
1934 encas = ENC_TO_ASCII;
1938 g_string_append_printf(buf, "%s=%s", param, val);
1942 tmp = g_strdup(val);
1944 " ()<>@,';:\\/[]?=.0123456789"
1945 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1946 "abcdefghijklmnopqrstuvwxyz",
1948 g_string_append_printf(buf, "%s=\"%s\"", param, tmp);
1952 case ENC_AS_QUOTED_STRING:
1953 g_string_append_printf(buf, "%s=\"%s\"", param, val);
1956 case ENC_AS_EXTENDED:
1957 if (!g_utf8_validate(val, -1, NULL))
1958 g_string_append_printf(buf, "%s*=%s''", param,
1959 conv_get_locale_charset_str());
1961 g_string_append_printf(buf, "%s*=%s''", param,
1963 for (valpos = val; *valpos != '\0'; valpos++) {
1964 if (IS_ASCII(*valpos) && isalnum(*valpos)) {
1965 g_string_append_printf(buf, "%c", *valpos);
1967 gchar hexstr[3] = "XX";
1968 get_hex_str(hexstr, *valpos);
1969 g_string_append_printf(buf, "%%%s", hexstr);
1975 if (buf->str && strlen(buf->str)) {
1976 if (pdata->len + strlen(buf->str) + 2 > 76) {
1977 fprintf(pdata->fp, ";\n %s", buf->str);
1978 pdata->len = strlen(buf->str) + 1;
1980 fprintf(pdata->fp, "; %s", buf->str);
1981 pdata->len += strlen(buf->str) + 2;
1984 g_string_free(buf, TRUE);
1987 void procmime_write_mime_header(MimeInfo *mimeinfo, FILE *fp)
1989 struct TypeTable *type_table;
1990 ParametersData *pdata = g_new0(ParametersData, 1);
1991 debug_print("procmime_write_mime_header\n");
1994 pdata->ascii_only = FALSE;
1996 for (type_table = mime_type_table; type_table->str != NULL; type_table++)
1997 if (mimeinfo->type == type_table->type) {
1998 gchar *buf = g_strdup_printf(
1999 "Content-Type: %s/%s", type_table->str, mimeinfo->subtype);
2000 fprintf(fp, "%s", buf);
2001 pdata->len = strlen(buf);
2002 pdata->ascii_only = TRUE;
2006 g_hash_table_foreach(mimeinfo->typeparameters, write_parameters, pdata);
2011 if (mimeinfo->encoding_type != ENC_UNKNOWN)
2012 fprintf(fp, "Content-Transfer-Encoding: %s\n", procmime_get_encoding_str(mimeinfo->encoding_type));
2014 if (mimeinfo->description != NULL)
2015 fprintf(fp, "Content-Description: %s\n", mimeinfo->description);
2017 if (mimeinfo->id != NULL)
2018 fprintf(fp, "Content-ID: %s\n", mimeinfo->id);
2020 if (mimeinfo->location != NULL)
2021 fprintf(fp, "Content-Location: %s\n", mimeinfo->location);
2023 if (mimeinfo->disposition != DISPOSITIONTYPE_UNKNOWN) {
2024 ParametersData *pdata = g_new0(ParametersData, 1);
2026 if (mimeinfo->disposition == DISPOSITIONTYPE_INLINE)
2027 buf = g_strdup("Content-Disposition: inline");
2028 else if (mimeinfo->disposition == DISPOSITIONTYPE_ATTACHMENT)
2029 buf = g_strdup("Content-Disposition: attachment");
2031 buf = g_strdup("Content-Disposition: unknown");
2033 fprintf(fp, "%s", buf);
2034 pdata->len = strlen(buf);
2038 pdata->ascii_only = FALSE;
2040 g_hash_table_foreach(mimeinfo->dispositionparameters, write_parameters, pdata);
2048 gint procmime_write_message_rfc822(MimeInfo *mimeinfo, FILE *fp)
2053 gchar buf[BUFFSIZE];
2054 gboolean skip = FALSE;;
2056 debug_print("procmime_write_message_rfc822\n");
2059 switch (mimeinfo->content) {
2060 case MIMECONTENT_FILE:
2061 if ((infp = g_fopen(mimeinfo->data.filename, "rb")) == NULL) {
2062 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
2065 fseek(infp, mimeinfo->offset, SEEK_SET);
2066 while (fgets(buf, sizeof(buf), infp) == buf) {
2067 if (buf[0] == '\n' && buf[1] == '\0')
2069 if (skip && (buf[0] == ' ' || buf[0] == '\t'))
2071 if (g_ascii_strncasecmp(buf, "Mime-Version:", 13) == 0 ||
2072 g_ascii_strncasecmp(buf, "Content-Type:", 13) == 0 ||
2073 g_ascii_strncasecmp(buf, "Content-Transfer-Encoding:", 26) == 0 ||
2074 g_ascii_strncasecmp(buf, "Content-Description:", 20) == 0 ||
2075 g_ascii_strncasecmp(buf, "Content-ID:", 11) == 0 ||
2076 g_ascii_strncasecmp(buf, "Content-Location:", 17) == 0 ||
2077 g_ascii_strncasecmp(buf, "Content-Disposition:", 20) == 0) {
2081 fwrite(buf, sizeof(gchar), strlen(buf), fp);
2087 case MIMECONTENT_MEM:
2088 fwrite(mimeinfo->data.mem,
2090 strlen(mimeinfo->data.mem),
2098 childnode = mimeinfo->node->children;
2099 if (childnode == NULL)
2102 child = (MimeInfo *) childnode->data;
2103 fprintf(fp, "Mime-Version: 1.0\n");
2104 procmime_write_mime_header(child, fp);
2105 return procmime_write_mimeinfo(child, fp);
2108 gint procmime_write_multipart(MimeInfo *mimeinfo, FILE *fp)
2112 gchar *boundary, *str, *str2;
2113 gchar buf[BUFFSIZE];
2114 gboolean firstboundary;
2116 debug_print("procmime_write_multipart\n");
2118 boundary = g_hash_table_lookup(mimeinfo->typeparameters, "boundary");
2120 switch (mimeinfo->content) {
2121 case MIMECONTENT_FILE:
2122 if ((infp = g_fopen(mimeinfo->data.filename, "rb")) == NULL) {
2123 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
2126 fseek(infp, mimeinfo->offset, SEEK_SET);
2127 while (fgets(buf, sizeof(buf), infp) == buf) {
2128 if (IS_BOUNDARY(buf, boundary, strlen(boundary)))
2130 fwrite(buf, sizeof(gchar), strlen(buf), fp);
2135 case MIMECONTENT_MEM:
2136 str = g_strdup(mimeinfo->data.mem);
2137 if (((str2 = strstr(str, boundary)) != NULL) && ((str2 - str) >= 2) &&
2138 (*(str2 - 1) == '-') && (*(str2 - 2) == '-'))
2140 fwrite(str, sizeof(gchar), strlen(str), fp);
2148 childnode = mimeinfo->node->children;
2149 firstboundary = TRUE;
2150 while (childnode != NULL) {
2151 MimeInfo *child = childnode->data;
2154 firstboundary = FALSE;
2157 fprintf(fp, "--%s\n", boundary);
2159 procmime_write_mime_header(child, fp);
2160 if (procmime_write_mimeinfo(child, fp) < 0)
2163 childnode = g_node_next_sibling(childnode);
2165 fprintf(fp, "\n--%s--\n", boundary);
2170 gint procmime_write_mimeinfo(MimeInfo *mimeinfo, FILE *fp)
2174 debug_print("procmime_write_mimeinfo\n");
2176 if (G_NODE_IS_LEAF(mimeinfo->node)) {
2177 switch (mimeinfo->content) {
2178 case MIMECONTENT_FILE:
2179 if ((infp = g_fopen(mimeinfo->data.filename, "rb")) == NULL) {
2180 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
2183 copy_file_part_to_fp(infp, mimeinfo->offset, mimeinfo->length, fp);
2187 case MIMECONTENT_MEM:
2188 fwrite(mimeinfo->data.mem,
2190 strlen(mimeinfo->data.mem),
2198 /* Call writer for mime type */
2199 switch (mimeinfo->type) {
2200 case MIMETYPE_MESSAGE:
2201 if (g_ascii_strcasecmp(mimeinfo->subtype, "rfc822") == 0) {
2202 return procmime_write_message_rfc822(mimeinfo, fp);
2206 case MIMETYPE_MULTIPART:
2207 return procmime_write_multipart(mimeinfo, fp);
2219 gchar *procmime_get_part_file_name(MimeInfo *mimeinfo)
2223 if ((mimeinfo->type == MIMETYPE_TEXT) && !g_ascii_strcasecmp(mimeinfo->subtype, "html"))
2224 base = g_strdup("mimetmp.html");
2226 const gchar *basetmp;
2229 basetmp = procmime_mimeinfo_get_parameter(mimeinfo, "filename");
2230 if (basetmp == NULL)
2231 basetmp = procmime_mimeinfo_get_parameter(mimeinfo, "name");
2232 if (basetmp == NULL)
2233 basetmp = "mimetmp";
2234 basename = g_path_get_basename(basetmp);
2235 if (*basename == '\0') {
2237 basename = g_strdup("mimetmp");
2239 base = conv_filename_from_utf8(basename);
2241 subst_for_shellsafe_filename(base);