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);
151 MimeInfo *procmime_mimeinfo_insert(MimeInfo *parent, MimeInfo *mimeinfo)
153 MimeInfo *child = parent->children;
156 parent->children = mimeinfo;
158 while (child->next != NULL)
161 child->next = mimeinfo;
164 mimeinfo->parent = parent;
165 mimeinfo->level = parent->level + 1;
170 void procmime_mimeinfo_replace(MimeInfo *old, MimeInfo *new)
172 MimeInfo *parent = old->parent;
175 g_return_if_fail(parent != NULL);
176 g_return_if_fail(new->next == NULL);
178 for (child = parent->children; child && child != old;
182 g_warning("oops: parent can't find it's own child");
185 procmime_mimeinfo_free_all(old);
187 if (child == parent->children) {
188 new->next = parent->children->next;
189 parent->children = new;
191 new->next = child->next;
197 MimeInfo *procmime_mimeinfo_parent(MimeInfo *mimeinfo)
199 g_return_val_if_fail(mimeinfo != NULL, NULL);
200 g_return_val_if_fail(mimeinfo->node != NULL, NULL);
202 if (mimeinfo->node->parent == NULL)
204 return (MimeInfo *) mimeinfo->node->parent->data;
207 MimeInfo *procmime_mimeinfo_next(MimeInfo *mimeinfo)
209 g_return_val_if_fail(mimeinfo != NULL, NULL);
210 g_return_val_if_fail(mimeinfo->node != NULL, NULL);
212 if (mimeinfo->node->children)
213 return (MimeInfo *) mimeinfo->node->children->data;
214 if (mimeinfo->node->next)
215 return (MimeInfo *) mimeinfo->node->next->data;
217 if (mimeinfo->node->parent == NULL)
220 while (mimeinfo->node->parent != NULL) {
221 mimeinfo = (MimeInfo *) mimeinfo->node->parent->data;
222 if (mimeinfo->node->next)
223 return (MimeInfo *) mimeinfo->node->next->data;
229 MimeInfo *procmime_scan_message(MsgInfo *msginfo)
234 filename = procmsg_get_message_file_path(msginfo);
235 if (!filename || !is_file_exist(filename)) {
237 filename = procmsg_get_message_file(msginfo);
239 if (!filename || !is_file_exist(filename))
242 if (!folder_has_parent_of_type(msginfo->folder, F_QUEUE) &&
243 !folder_has_parent_of_type(msginfo->folder, F_DRAFT))
244 mimeinfo = procmime_scan_file(filename);
246 mimeinfo = procmime_scan_queue_file(filename);
254 H_CONTENT_TRANSFER_ENCODING = 0,
256 H_CONTENT_DISPOSITION = 2,
257 H_CONTENT_DESCRIPTION = 3,
261 const gchar *procmime_mimeinfo_get_parameter(MimeInfo *mimeinfo, const gchar *name)
265 g_return_val_if_fail(mimeinfo != NULL, NULL);
266 g_return_val_if_fail(name != NULL, NULL);
268 value = g_hash_table_lookup(mimeinfo->dispositionparameters, name);
270 value = g_hash_table_lookup(mimeinfo->typeparameters, name);
275 gboolean procmime_decode_content(MimeInfo *mimeinfo)
282 gboolean tmp_file = FALSE;
284 EncodingType encoding = forced_encoding
286 : mimeinfo->encoding_type;
288 g_return_val_if_fail(mimeinfo != NULL, FALSE);
290 if (encoding == ENC_UNKNOWN ||
291 encoding == ENC_BINARY)
294 infp = g_fopen(mimeinfo->data.filename, "rb");
299 fseek(infp, mimeinfo->offset, SEEK_SET);
301 outfp = get_tmpfile_in_dir(get_mime_tmp_dir(), &tmpfilename);
308 readend = mimeinfo->offset + mimeinfo->length;
310 if (encoding == ENC_QUOTED_PRINTABLE) {
311 while ((ftell(infp) < readend) && (fgets(buf, sizeof(buf), infp) != NULL)) {
313 len = qp_decode_line(buf);
314 fwrite(buf, 1, len, outfp);
316 } else if (encoding == ENC_BASE64) {
317 gchar outbuf[BUFFSIZE];
319 Base64Decoder *decoder;
320 gboolean got_error = FALSE;
321 gboolean uncanonicalize = FALSE;
324 if (mimeinfo->type == MIMETYPE_TEXT ||
325 mimeinfo->type == MIMETYPE_MESSAGE) {
326 uncanonicalize = TRUE;
327 tmpfp = my_tmpfile();
330 if (tmp_file) fclose(outfp);
336 decoder = base64_decoder_new();
337 while ((ftell(infp) < readend) && (fgets(buf, sizeof(buf), infp) != NULL)) {
338 len = base64_decoder_decode(decoder, buf, outbuf);
339 if (len < 0 && !got_error) {
340 g_warning("Bad BASE64 content.\n");
341 fwrite(_("[Error decoding BASE64]\n"),
343 strlen(_("[Error decoding BASE64]\n")),
347 } else if (len >= 0) {
348 /* print out the error message only once
350 fwrite(outbuf, sizeof(gchar), len, tmpfp);
354 base64_decoder_free(decoder);
356 if (uncanonicalize) {
358 while (fgets(buf, sizeof(buf), tmpfp) != NULL) {
364 } else if (encoding == ENC_X_UUENCODE) {
365 gchar outbuf[BUFFSIZE];
367 gboolean flag = FALSE;
369 while ((ftell(infp) < readend) && (fgets(buf, sizeof(buf), infp) != NULL)) {
370 if (!flag && strncmp(buf,"begin ", 6)) continue;
373 len = fromuutobits(outbuf, buf);
376 g_warning("Bad UUENCODE content(%d)\n", len);
379 fwrite(outbuf, sizeof(gchar), len, outfp);
384 while ((ftell(infp) < readend) && (fgets(buf, sizeof(buf), infp) != NULL)) {
392 stat(tmpfilename, &statbuf);
393 if (mimeinfo->tmp && (mimeinfo->data.filename != NULL))
394 g_unlink(mimeinfo->data.filename);
395 if (mimeinfo->data.filename != NULL)
396 g_free(mimeinfo->data.filename);
397 mimeinfo->data.filename = tmpfilename;
398 mimeinfo->tmp = TRUE;
399 mimeinfo->offset = 0;
400 mimeinfo->length = statbuf.st_size;
401 mimeinfo->encoding_type = ENC_BINARY;
406 #define B64_LINE_SIZE 57
407 #define B64_BUFFSIZE 77
409 gboolean procmime_encode_content(MimeInfo *mimeinfo, EncodingType encoding)
411 FILE *infp = NULL, *outfp;
416 if (mimeinfo->content == MIMECONTENT_EMPTY)
419 if (mimeinfo->encoding_type != ENC_UNKNOWN &&
420 mimeinfo->encoding_type != ENC_BINARY &&
421 mimeinfo->encoding_type != ENC_7BIT &&
422 mimeinfo->encoding_type != ENC_8BIT)
423 if(!procmime_decode_content(mimeinfo))
426 outfp = get_tmpfile_in_dir(get_mime_tmp_dir(), &tmpfilename);
432 if (mimeinfo->content == MIMECONTENT_FILE) {
433 if ((infp = g_fopen(mimeinfo->data.filename, "rb")) == NULL) {
434 g_warning("Can't open file %s\n", mimeinfo->data.filename);
437 } else if (mimeinfo->content == MIMECONTENT_MEM) {
438 infp = str_open_as_stream(mimeinfo->data.mem);
443 if (encoding == ENC_BASE64) {
444 gchar inbuf[B64_LINE_SIZE], outbuf[B64_BUFFSIZE];
446 gchar *tmp_file = NULL;
448 if (mimeinfo->type == MIMETYPE_TEXT ||
449 mimeinfo->type == MIMETYPE_MESSAGE) {
450 if (mimeinfo->content == MIMECONTENT_FILE) {
451 tmp_file = get_tmp_file();
452 if (canonicalize_file(mimeinfo->data.filename, tmp_file) < 0) {
457 if ((tmp_fp = g_fopen(tmp_file, "rb")) == NULL) {
458 FILE_OP_ERROR(tmp_file, "fopen");
465 gchar *out = canonicalize_str(mimeinfo->data.mem);
467 infp = str_open_as_stream(out);
475 while ((len = fread(inbuf, sizeof(gchar),
476 B64_LINE_SIZE, tmp_fp))
478 base64_encode(outbuf, inbuf, B64_LINE_SIZE);
479 fputs(outbuf, outfp);
482 if (len > 0 && feof(tmp_fp)) {
483 base64_encode(outbuf, inbuf, len);
484 fputs(outbuf, outfp);
493 } else if (encoding == ENC_QUOTED_PRINTABLE) {
494 gchar inbuf[BUFFSIZE], outbuf[BUFFSIZE * 4];
496 while (fgets(inbuf, sizeof(inbuf), infp) != NULL) {
497 qp_encode_line(outbuf, inbuf);
499 if (!strncmp("From ", outbuf, sizeof("From ")-1)) {
500 gchar *tmpbuf = outbuf;
502 tmpbuf += sizeof("From ")-1;
504 fputs("=46rom ", outfp);
505 fputs(tmpbuf, outfp);
507 fputs(outbuf, outfp);
512 while (fgets(buf, sizeof(buf), infp) != NULL) {
521 if (mimeinfo->content == MIMECONTENT_FILE) {
522 if (mimeinfo->tmp && (mimeinfo->data.filename != NULL))
523 g_unlink(mimeinfo->data.filename);
524 g_free(mimeinfo->data.filename);
525 } else if (mimeinfo->content == MIMECONTENT_MEM) {
526 if (mimeinfo->tmp && (mimeinfo->data.mem != NULL))
527 g_free(mimeinfo->data.mem);
530 stat(tmpfilename, &statbuf);
531 mimeinfo->content = MIMECONTENT_FILE;
532 mimeinfo->data.filename = tmpfilename;
533 mimeinfo->tmp = TRUE;
534 mimeinfo->offset = 0;
535 mimeinfo->length = statbuf.st_size;
536 mimeinfo->encoding_type = encoding;
541 gint procmime_get_part(const gchar *outfile, MimeInfo *mimeinfo)
545 gint restlength, readlength;
547 g_return_val_if_fail(outfile != NULL, -1);
548 g_return_val_if_fail(mimeinfo != NULL, -1);
550 if (mimeinfo->encoding_type != ENC_BINARY && !procmime_decode_content(mimeinfo))
553 if ((infp = g_fopen(mimeinfo->data.filename, "rb")) == NULL) {
554 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
557 if (fseek(infp, mimeinfo->offset, SEEK_SET) < 0) {
558 FILE_OP_ERROR(mimeinfo->data.filename, "fseek");
562 if ((outfp = g_fopen(outfile, "wb")) == NULL) {
563 FILE_OP_ERROR(outfile, "fopen");
568 restlength = mimeinfo->length;
570 while ((restlength > 0) && ((readlength = fread(buf, 1, restlength > BUFFSIZE ? BUFFSIZE : restlength, infp)) > 0)) {
571 fwrite(buf, 1, readlength, outfp);
572 restlength -= readlength;
576 if (fclose(outfp) == EOF) {
577 FILE_OP_ERROR(outfile, "fclose");
585 struct ContentRenderer {
590 static GList * renderer_list = NULL;
592 static struct ContentRenderer *
593 content_renderer_new(char * content_type, char * renderer)
595 struct ContentRenderer * cr;
597 cr = g_new(struct ContentRenderer, 1);
601 cr->content_type = g_strdup(content_type);
602 cr->renderer = g_strdup(renderer);
607 static void content_renderer_free(struct ContentRenderer * cr)
609 g_free(cr->content_type);
610 g_free(cr->renderer);
614 void renderer_read_config(void)
620 g_list_foreach(renderer_list, (GFunc) content_renderer_free, NULL);
621 renderer_list = NULL;
623 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, RENDERER_RC, NULL);
624 f = g_fopen(rcpath, "rb");
630 while (fgets(buf, BUFFSIZE, f)) {
632 struct ContentRenderer * cr;
635 p = strchr(buf, ' ');
640 cr = content_renderer_new(buf, p + 1);
644 renderer_list = g_list_append(renderer_list, cr);
650 void renderer_write_config(void)
656 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, RENDERER_RC, NULL);
658 if ((pfile = prefs_write_open(rcpath)) == NULL) {
659 g_warning("failed to write configuration to file\n");
666 for (cur = renderer_list ; cur != NULL ; cur = cur->next) {
667 struct ContentRenderer * renderer;
668 renderer = cur->data;
669 fprintf(pfile->fp, "%s %s\n", renderer->content_type,
673 if (prefs_file_close(pfile) < 0) {
674 g_warning("failed to write configuration to file\n");
679 FILE *procmime_get_text_content(MimeInfo *mimeinfo)
682 const gchar *src_codeset;
683 gboolean conv_fail = FALSE;
686 struct ContentRenderer * renderer;
688 gchar *tmpfile, *content_type;
690 g_return_val_if_fail(mimeinfo != NULL, NULL);
692 if (!procmime_decode_content(mimeinfo))
695 tmpfile = procmime_get_tmp_file_name(mimeinfo);
699 if (procmime_get_part(tmpfile, mimeinfo) < 0) {
704 tmpfp = g_fopen(tmpfile, "rb");
710 if ((outfp = my_tmpfile()) == NULL) {
717 src_codeset = forced_charset
719 procmime_mimeinfo_get_parameter(mimeinfo, "charset");
723 content_type = procmime_get_content_type_str(mimeinfo->type,
725 for (cur = renderer_list ; cur != NULL ; cur = cur->next) {
726 struct ContentRenderer * cr;
729 if (g_ascii_strcasecmp(cr->content_type, content_type) == 0) {
734 g_free(content_type);
736 if (renderer != NULL) {
742 dup2(fileno(outfp), 1);
744 p = popen(renderer->renderer, "w");
749 fread(buf, sizeof(char), sizeof(buf),
751 fwrite(buf, sizeof(char), count, p);
756 /* CodeConverter seems to have no effect here */
757 } else if (mimeinfo->type == MIMETYPE_TEXT && !g_ascii_strcasecmp(mimeinfo->subtype, "html")) {
758 SC_HTMLParser *parser;
761 conv = conv_code_converter_new(src_codeset);
762 parser = sc_html_parser_new(tmpfp, conv);
763 while ((str = sc_html_parse(parser)) != NULL) {
766 sc_html_parser_destroy(parser);
767 conv_code_converter_destroy(conv);
768 } else if (mimeinfo->type == MIMETYPE_TEXT && !g_ascii_strcasecmp(mimeinfo->subtype, "enriched")) {
772 conv = conv_code_converter_new(src_codeset);
773 parser = ertf_parser_new(tmpfp, conv);
774 while ((str = ertf_parse(parser)) != NULL) {
777 ertf_parser_destroy(parser);
778 conv_code_converter_destroy(conv);
779 } else if (mimeinfo->type == MIMETYPE_TEXT) {
780 while (fgets(buf, sizeof(buf), tmpfp) != NULL) {
781 str = conv_codeset_strdup(buf, src_codeset, CS_UTF_8);
793 g_warning("procmime_get_text_content(): Code conversion failed.\n");
803 /* search the first text part of (multipart) MIME message,
804 decode, convert it and output to outfp. */
805 FILE *procmime_get_first_text_content(MsgInfo *msginfo)
808 MimeInfo *mimeinfo, *partinfo;
810 g_return_val_if_fail(msginfo != NULL, NULL);
812 mimeinfo = procmime_scan_message(msginfo);
813 if (!mimeinfo) return NULL;
816 while (partinfo && partinfo->type != MIMETYPE_TEXT) {
817 partinfo = procmime_mimeinfo_next(partinfo);
820 outfp = procmime_get_text_content(partinfo);
822 procmime_mimeinfo_free_all(mimeinfo);
828 static gboolean find_encrypted_func(GNode *node, gpointer data)
830 MimeInfo *mimeinfo = (MimeInfo *) node->data;
831 MimeInfo **encinfo = (MimeInfo **) data;
833 if (privacy_mimeinfo_is_encrypted(mimeinfo)) {
841 static MimeInfo *find_encrypted_part(MimeInfo *rootinfo)
843 MimeInfo *encinfo = NULL;
845 g_node_traverse(rootinfo->node, G_IN_ORDER, G_TRAVERSE_ALL, -1,
846 find_encrypted_func, &encinfo);
851 /* search the first encrypted text part of (multipart) MIME message,
852 decode, convert it and output to outfp. */
853 FILE *procmime_get_first_encrypted_text_content(MsgInfo *msginfo)
856 MimeInfo *mimeinfo, *partinfo, *encinfo;
858 g_return_val_if_fail(msginfo != NULL, NULL);
860 mimeinfo = procmime_scan_message(msginfo);
866 if ((encinfo = find_encrypted_part(partinfo)) != NULL) {
867 debug_print("decrypting message part\n");
868 if (privacy_mimeinfo_decrypt(encinfo) < 0)
872 while (partinfo && partinfo->type != MIMETYPE_TEXT) {
873 partinfo = procmime_mimeinfo_next(partinfo);
877 outfp = procmime_get_text_content(partinfo);
879 procmime_mimeinfo_free_all(mimeinfo);
884 gboolean procmime_msginfo_is_encrypted(MsgInfo *msginfo)
886 MimeInfo *mimeinfo, *partinfo;
887 gboolean result = FALSE;
889 g_return_val_if_fail(msginfo != NULL, FALSE);
891 mimeinfo = procmime_scan_message(msginfo);
897 result = (find_encrypted_part(partinfo) != NULL);
898 procmime_mimeinfo_free_all(mimeinfo);
903 gboolean procmime_find_string_part(MimeInfo *mimeinfo, const gchar *filename,
904 const gchar *str, StrFindFunc find_func)
909 g_return_val_if_fail(mimeinfo != NULL, FALSE);
910 g_return_val_if_fail(mimeinfo->type == MIMETYPE_TEXT, FALSE);
911 g_return_val_if_fail(str != NULL, FALSE);
912 g_return_val_if_fail(find_func != NULL, FALSE);
914 outfp = procmime_get_text_content(mimeinfo);
919 while (fgets(buf, sizeof(buf), outfp) != NULL) {
921 if (find_func(buf, str)) {
932 gboolean procmime_find_string(MsgInfo *msginfo, const gchar *str,
933 StrFindFunc find_func)
938 gboolean found = FALSE;
940 g_return_val_if_fail(msginfo != NULL, FALSE);
941 g_return_val_if_fail(str != NULL, FALSE);
942 g_return_val_if_fail(find_func != NULL, FALSE);
944 filename = procmsg_get_message_file(msginfo);
945 if (!filename) return FALSE;
946 mimeinfo = procmime_scan_message(msginfo);
948 for (partinfo = mimeinfo; partinfo != NULL;
949 partinfo = procmime_mimeinfo_next(partinfo)) {
950 if (partinfo->type == MIMETYPE_TEXT) {
951 if (procmime_find_string_part
952 (partinfo, filename, str, find_func) == TRUE) {
959 procmime_mimeinfo_free_all(mimeinfo);
965 gchar *procmime_get_tmp_file_name(MimeInfo *mimeinfo)
967 static guint32 id = 0;
972 g_return_val_if_fail(mimeinfo != NULL, NULL);
974 g_snprintf(f_prefix, sizeof(f_prefix), "%08x.", id++);
976 if ((mimeinfo->type == MIMETYPE_TEXT) && !g_ascii_strcasecmp(mimeinfo->subtype, "html"))
977 base = g_strdup("mimetmp.html");
979 const gchar *basetmp;
981 basetmp = procmime_mimeinfo_get_parameter(mimeinfo, "filename");
983 basetmp = procmime_mimeinfo_get_parameter(mimeinfo, "name");
986 basetmp = g_path_get_basename(basetmp);
987 if (*basetmp == '\0')
988 basetmp = g_strdup("mimetmp");
989 base = conv_filename_from_utf8(basetmp);
990 g_free((gchar*)basetmp);
991 subst_for_shellsafe_filename(base);
994 filename = g_strconcat(get_mime_tmp_dir(), G_DIR_SEPARATOR_S,
995 f_prefix, base, NULL);
1002 static GList *mime_type_list = NULL;
1004 gchar *procmime_get_mime_type(const gchar *filename)
1006 static GHashTable *mime_type_table = NULL;
1007 MimeType *mime_type;
1012 if (!mime_type_table) {
1013 mime_type_table = procmime_get_mime_type_table();
1014 if (!mime_type_table) return NULL;
1017 base = g_path_get_basename(filename);
1018 if ((p = strrchr(base, '.')) != NULL)
1019 Xstrdup_a(ext, p + 1, p = NULL );
1021 if (!p) return NULL;
1024 mime_type = g_hash_table_lookup(mime_type_table, ext);
1028 str = g_strconcat(mime_type->type, "/", mime_type->sub_type,
1036 static guint procmime_str_hash(gconstpointer gptr)
1038 guint hash_result = 0;
1041 for (str = gptr; str && *str; str++) {
1042 if (isupper(*str)) hash_result += (*str + ' ');
1043 else hash_result += *str;
1049 static gint procmime_str_equal(gconstpointer gptr1, gconstpointer gptr2)
1051 const char *str1 = gptr1;
1052 const char *str2 = gptr2;
1054 return !g_utf8_collate(str1, str2);
1057 static GHashTable *procmime_get_mime_type_table(void)
1059 GHashTable *table = NULL;
1061 MimeType *mime_type;
1064 if (!mime_type_list) {
1065 mime_type_list = procmime_get_mime_type_list();
1066 if (!mime_type_list) return NULL;
1069 table = g_hash_table_new(procmime_str_hash, procmime_str_equal);
1071 for (cur = mime_type_list; cur != NULL; cur = cur->next) {
1075 mime_type = (MimeType *)cur->data;
1077 if (!mime_type->extension) continue;
1079 exts = g_strsplit(mime_type->extension, " ", 16);
1080 for (i = 0; exts[i] != NULL; i++) {
1081 /* make the key case insensitive */
1083 /* use previously dup'd key on overwriting */
1084 if (g_hash_table_lookup(table, exts[i]))
1087 key = g_strdup(exts[i]);
1088 g_hash_table_insert(table, key, mime_type);
1096 GList *procmime_get_mime_type_list(void)
1100 gchar buf[BUFFSIZE];
1103 MimeType *mime_type;
1104 gboolean fp_is_glob_file = TRUE;
1107 return mime_type_list;
1109 if ((fp = g_fopen("/usr/share/mime/globs", "rb")) == NULL) {
1110 fp_is_glob_file = FALSE;
1111 if ((fp = g_fopen("/etc/mime.types", "rb")) == NULL) {
1112 if ((fp = g_fopen(SYSCONFDIR "/mime.types", "rb"))
1114 FILE_OP_ERROR(SYSCONFDIR "/mime.types",
1121 while (fgets(buf, sizeof(buf), fp) != NULL) {
1122 p = strchr(buf, '#');
1128 if (fp_is_glob_file) {
1129 while (*p && !g_ascii_isspace(*p) && (*p!=':')) p++;
1131 while (*p && !g_ascii_isspace(*p)) p++;
1138 delim = strchr(buf, '/');
1139 if (delim == NULL) continue;
1142 mime_type = g_new(MimeType, 1);
1143 mime_type->type = g_strdup(buf);
1144 mime_type->sub_type = g_strdup(delim + 1);
1146 if (fp_is_glob_file) {
1147 while (*p && (g_ascii_isspace(*p)||(*p=='*')||(*p=='.'))) p++;
1149 while (*p && g_ascii_isspace(*p)) p++;
1153 mime_type->extension = g_strdup(p);
1155 mime_type->extension = NULL;
1157 list = g_list_append(list, mime_type);
1163 g_warning("Can't read mime.types\n");
1168 EncodingType procmime_get_encoding_for_charset(const gchar *charset)
1172 else if (!g_ascii_strncasecmp(charset, "ISO-2022-", 9) ||
1173 !g_ascii_strcasecmp(charset, "US-ASCII"))
1175 else if (!g_ascii_strcasecmp(charset, "ISO-8859-5") ||
1176 !g_ascii_strncasecmp(charset, "KOI8-", 5) ||
1177 !g_ascii_strcasecmp(charset, "Windows-1251"))
1179 else if (!g_ascii_strncasecmp(charset, "ISO-8859-", 9))
1180 return ENC_QUOTED_PRINTABLE;
1185 EncodingType procmime_get_encoding_for_text_file(const gchar *file, gboolean *has_binary)
1188 guchar buf[BUFFSIZE];
1190 size_t octet_chars = 0;
1191 size_t total_len = 0;
1192 gfloat octet_percentage;
1193 gboolean force_b64 = FALSE;
1195 if ((fp = g_fopen(file, "rb")) == NULL) {
1196 FILE_OP_ERROR(file, "fopen");
1200 while ((len = fread(buf, sizeof(guchar), sizeof(buf), fp)) > 0) {
1204 for (p = buf, i = 0; i < len; ++p, ++i) {
1218 octet_percentage = (gfloat)octet_chars / (gfloat)total_len;
1220 octet_percentage = 0.0;
1222 debug_print("procmime_get_encoding_for_text_file(): "
1223 "8bit chars: %d / %d (%f%%)\n", octet_chars, total_len,
1224 100.0 * octet_percentage);
1226 if (octet_percentage > 0.20 || force_b64) {
1227 debug_print("using BASE64\n");
1229 } else if (octet_chars > 0) {
1230 debug_print("using quoted-printable\n");
1231 return ENC_QUOTED_PRINTABLE;
1233 debug_print("using 7bit\n");
1238 struct EncodingTable
1241 EncodingType enc_type;
1244 struct EncodingTable encoding_table[] = {
1247 {"binary", ENC_BINARY},
1248 {"quoted-printable", ENC_QUOTED_PRINTABLE},
1249 {"base64", ENC_BASE64},
1250 {"x-uuencode", ENC_UNKNOWN},
1251 {NULL, ENC_UNKNOWN},
1254 const gchar *procmime_get_encoding_str(EncodingType encoding)
1256 struct EncodingTable *enc_table;
1258 for (enc_table = encoding_table; enc_table->str != NULL; enc_table++) {
1259 if (enc_table->enc_type == encoding)
1260 return enc_table->str;
1265 /* --- NEW MIME STUFF --- */
1272 static struct TypeTable mime_type_table[] = {
1273 {"text", MIMETYPE_TEXT},
1274 {"image", MIMETYPE_IMAGE},
1275 {"audio", MIMETYPE_AUDIO},
1276 {"video", MIMETYPE_VIDEO},
1277 {"application", MIMETYPE_APPLICATION},
1278 {"message", MIMETYPE_MESSAGE},
1279 {"multipart", MIMETYPE_MULTIPART},
1283 const gchar *procmime_get_media_type_str(MimeMediaType type)
1285 struct TypeTable *type_table;
1287 for (type_table = mime_type_table; type_table->str != NULL; type_table++) {
1288 if (type_table->type == type)
1289 return type_table->str;
1294 MimeMediaType procmime_get_media_type(const gchar *str)
1296 struct TypeTable *typetablearray;
1298 for (typetablearray = mime_type_table; typetablearray->str != NULL; typetablearray++)
1299 if (g_ascii_strncasecmp(str, typetablearray->str, strlen(typetablearray->str)) == 0)
1300 return typetablearray->type;
1302 return MIMETYPE_UNKNOWN;
1306 *\brief Safe wrapper for content type string.
1308 *\return const gchar * Pointer to content type string.
1310 gchar *procmime_get_content_type_str(MimeMediaType type,
1311 const char *subtype)
1313 const gchar *type_str = NULL;
1315 if (subtype == NULL || !(type_str = procmime_get_media_type_str(type)))
1316 return g_strdup("unknown");
1317 return g_strdup_printf("%s/%s", type_str, subtype);
1320 int procmime_parse_mimepart(MimeInfo *parent,
1321 gchar *content_type,
1322 gchar *content_encoding,
1323 gchar *content_description,
1325 gchar *content_disposition,
1326 const gchar *filename,
1330 void procmime_parse_message_rfc822(MimeInfo *mimeinfo)
1332 HeaderEntry hentry[] = {{"Content-Type:", NULL, TRUE},
1333 {"Content-Transfer-Encoding:",
1335 {"Content-Description:",
1339 {"Content-Disposition:",
1343 {NULL, NULL, FALSE}};
1344 guint content_start, i;
1348 procmime_decode_content(mimeinfo);
1350 fp = g_fopen(mimeinfo->data.filename, "rb");
1352 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
1355 fseek(fp, mimeinfo->offset, SEEK_SET);
1356 procheader_get_header_fields(fp, hentry);
1357 if (hentry[0].body != NULL) {
1358 tmp = conv_unmime_header(hentry[0].body, NULL);
1359 g_free(hentry[0].body);
1360 hentry[0].body = tmp;
1362 if (hentry[2].body != NULL) {
1363 tmp = conv_unmime_header(hentry[2].body, NULL);
1364 g_free(hentry[2].body);
1365 hentry[2].body = tmp;
1367 if (hentry[4].body != NULL) {
1368 tmp = conv_unmime_header(hentry[4].body, NULL);
1369 g_free(hentry[4].body);
1370 hentry[4].body = tmp;
1372 content_start = ftell(fp);
1375 procmime_parse_mimepart(mimeinfo,
1376 hentry[0].body, hentry[1].body,
1377 hentry[2].body, hentry[3].body,
1379 mimeinfo->data.filename, content_start,
1380 mimeinfo->length - (content_start - mimeinfo->offset));
1382 for (i = 0; i < (sizeof hentry / sizeof hentry[0]); i++) {
1383 g_free(hentry[i].body);
1384 hentry[i].body = NULL;
1388 void procmime_parse_multipart(MimeInfo *mimeinfo)
1390 HeaderEntry hentry[] = {{"Content-Type:", NULL, TRUE},
1391 {"Content-Transfer-Encoding:",
1393 {"Content-Description:",
1397 {"Content-Disposition:",
1399 {NULL, NULL, FALSE}};
1402 gint boundary_len = 0, lastoffset = -1, i;
1403 gchar buf[BUFFSIZE];
1407 boundary = g_hash_table_lookup(mimeinfo->typeparameters, "boundary");
1410 boundary_len = strlen(boundary);
1412 procmime_decode_content(mimeinfo);
1414 fp = g_fopen(mimeinfo->data.filename, "rb");
1416 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
1419 fseek(fp, mimeinfo->offset, SEEK_SET);
1420 while ((p = fgets(buf, sizeof(buf), fp)) != NULL && result == 0) {
1421 if (ftell(fp) > (mimeinfo->offset + mimeinfo->length))
1424 if (IS_BOUNDARY(buf, boundary, boundary_len)) {
1425 if (lastoffset != -1) {
1426 result = procmime_parse_mimepart(mimeinfo,
1427 hentry[0].body, hentry[1].body,
1428 hentry[2].body, hentry[3].body,
1430 mimeinfo->data.filename, lastoffset,
1431 (ftell(fp) - strlen(buf)) - lastoffset - 1);
1434 if (buf[2 + boundary_len] == '-' &&
1435 buf[2 + boundary_len + 1] == '-')
1438 for (i = 0; i < (sizeof hentry / sizeof hentry[0]) ; i++) {
1439 g_free(hentry[i].body);
1440 hentry[i].body = NULL;
1442 procheader_get_header_fields(fp, hentry);
1443 if (hentry[0].body != NULL) {
1444 tmp = conv_unmime_header(hentry[0].body, NULL);
1445 g_free(hentry[0].body);
1446 hentry[0].body = tmp;
1448 if (hentry[2].body != NULL) {
1449 tmp = conv_unmime_header(hentry[2].body, NULL);
1450 g_free(hentry[2].body);
1451 hentry[2].body = tmp;
1453 if (hentry[4].body != NULL) {
1454 tmp = conv_unmime_header(hentry[4].body, NULL);
1455 g_free(hentry[4].body);
1456 hentry[4].body = tmp;
1458 lastoffset = ftell(fp);
1461 for (i = 0; i < (sizeof hentry / sizeof hentry[0]); i++) {
1462 g_free(hentry[i].body);
1463 hentry[i].body = NULL;
1468 static void parse_parameters(const gchar *parameters, GHashTable *table)
1470 gchar *params, *param, *next;
1471 GSList *convlist = NULL, *concatlist = NULL, *cur;
1473 params = g_strdup(parameters);
1476 for (; next != NULL; param = next) {
1477 gchar *attribute, *value, *tmp;
1479 gboolean convert = FALSE;
1481 next = strchr_with_skip_quote(param, '"', ';');
1490 value = strchr(attribute, '=');
1496 while (value[0] == ' ')
1499 g_strdown(attribute);
1501 len = strlen(attribute);
1502 if (attribute[len - 1] == '*') {
1503 gchar *srcpos, *dstpos, *endpos;
1506 attribute[len - 1] = '\0';
1510 endpos = value + strlen(value);
1511 while (srcpos < endpos) {
1517 if (!get_hex_value(&dstvalue, srcpos[1], srcpos[2]))
1528 if (value[0] == '"')
1529 extract_quote(value, '"');
1530 else if ((tmp = strchr(value, ' ')) != NULL)
1535 while (attribute[0] == ' ')
1537 while (attribute[strlen(attribute)-1] == ' ')
1538 attribute[strlen(attribute)-1] = '\0';
1541 while (value[0] == ' ')
1543 while (value[strlen(value)-1] == ' ')
1544 value[strlen(value)-1] = '\0';
1546 if (strrchr(attribute, '*') != NULL) {
1549 tmpattr = g_strdup(attribute);
1550 tmp = strrchr(tmpattr, '*');
1553 if ((tmp[1] == '0') && (tmp[2] == '\0') &&
1554 (g_slist_find_custom(concatlist, attribute, g_str_equal) == NULL))
1555 concatlist = g_slist_prepend(concatlist, g_strdup(tmpattr));
1557 if (convert && (g_slist_find_custom(convlist, attribute, g_str_equal) == NULL))
1558 convlist = g_slist_prepend(convlist, g_strdup(tmpattr));
1561 } else if (convert) {
1562 if (g_slist_find_custom(convlist, attribute, g_str_equal) == NULL)
1563 convlist = g_slist_prepend(convlist, g_strdup(attribute));
1566 if (g_hash_table_lookup(table, attribute) == NULL)
1567 g_hash_table_insert(table, g_strdup(attribute), g_strdup(value));
1570 for (cur = concatlist; cur != NULL; cur = g_slist_next(cur)) {
1571 gchar *attribute, *attrwnum, *partvalue;
1575 attribute = (gchar *) cur->data;
1576 value = g_string_sized_new(64);
1578 attrwnum = g_strdup_printf("%s*%d", attribute, n);
1579 while ((partvalue = g_hash_table_lookup(table, attrwnum)) != NULL) {
1580 g_string_append(value, partvalue);
1584 attrwnum = g_strdup_printf("%s*%d", attribute, n);
1588 g_hash_table_insert(table, g_strdup(attribute), g_strdup(value->str));
1589 g_string_free(value, TRUE);
1591 slist_free_strings(concatlist);
1592 g_slist_free(concatlist);
1594 for (cur = convlist; cur != NULL; cur = g_slist_next(cur)) {
1595 gchar *attribute, *key, *value;
1596 gchar *charset, *lang, *oldvalue, *newvalue;
1598 attribute = (gchar *) cur->data;
1599 if (!g_hash_table_lookup_extended(
1600 table, attribute, (gpointer *)(gchar *) &key, (gpointer *)(gchar *) &value))
1604 lang = strchr(charset, '\'');
1609 oldvalue = strchr(lang, '\'');
1610 if (oldvalue == NULL)
1615 newvalue = conv_codeset_strdup(oldvalue, charset, CS_UTF_8);
1617 g_hash_table_remove(table, attribute);
1621 g_hash_table_insert(table, g_strdup(attribute), newvalue);
1623 slist_free_strings(convlist);
1624 g_slist_free(convlist);
1629 static void procmime_parse_content_type(const gchar *content_type, MimeInfo *mimeinfo)
1631 g_return_if_fail(content_type != NULL);
1632 g_return_if_fail(mimeinfo != NULL);
1634 /* RFC 2045, page 13 says that the mime subtype is MANDATORY;
1635 * if it's not available we use the default Content-Type */
1636 if ((content_type[0] == '\0') || (strchr(content_type, '/') == NULL)) {
1637 mimeinfo->type = MIMETYPE_TEXT;
1638 mimeinfo->subtype = g_strdup("plain");
1639 if (g_hash_table_lookup(mimeinfo->typeparameters,
1640 "charset") == NULL) {
1641 g_hash_table_insert(mimeinfo->typeparameters,
1642 g_strdup("charset"),
1644 conv_get_locale_charset_str_no_utf8()));
1647 gchar *type, *subtype, *params;
1649 type = g_strdup(content_type);
1650 subtype = strchr(type, '/') + 1;
1651 *(subtype - 1) = '\0';
1652 if ((params = strchr(subtype, ';')) != NULL) {
1657 mimeinfo->type = procmime_get_media_type(type);
1658 mimeinfo->subtype = g_strdup(subtype);
1660 /* Get mimeinfo->typeparameters */
1662 parse_parameters(params, mimeinfo->typeparameters);
1668 static void procmime_parse_content_disposition(const gchar *content_disposition, MimeInfo *mimeinfo)
1670 gchar *tmp, *params;
1672 g_return_if_fail(content_disposition != NULL);
1673 g_return_if_fail(mimeinfo != NULL);
1675 tmp = g_strdup(content_disposition);
1676 if ((params = strchr(tmp, ';')) != NULL) {
1682 if (!g_ascii_strcasecmp(tmp, "inline"))
1683 mimeinfo->disposition = DISPOSITIONTYPE_INLINE;
1684 else if (!g_ascii_strcasecmp(tmp, "attachment"))
1685 mimeinfo->disposition = DISPOSITIONTYPE_ATTACHMENT;
1687 mimeinfo->disposition = DISPOSITIONTYPE_ATTACHMENT;
1690 parse_parameters(params, mimeinfo->dispositionparameters);
1696 static void procmime_parse_content_encoding(const gchar *content_encoding, MimeInfo *mimeinfo)
1698 struct EncodingTable *enc_table;
1700 for (enc_table = encoding_table; enc_table->str != NULL; enc_table++) {
1701 if (g_ascii_strcasecmp(enc_table->str, content_encoding) == 0) {
1702 mimeinfo->encoding_type = enc_table->enc_type;
1706 mimeinfo->encoding_type = ENC_UNKNOWN;
1710 int procmime_parse_mimepart(MimeInfo *parent,
1711 gchar *content_type,
1712 gchar *content_encoding,
1713 gchar *content_description,
1715 gchar *content_disposition,
1716 const gchar *filename,
1722 /* Create MimeInfo */
1723 mimeinfo = procmime_mimeinfo_new();
1724 mimeinfo->content = MIMECONTENT_FILE;
1725 if (parent != NULL) {
1726 if (g_node_depth(parent->node) > 32) {
1727 /* 32 is an arbitrary value
1728 * this avoids DOSsing ourselves
1729 * with enormous messages
1731 procmime_mimeinfo_free_all(mimeinfo);
1734 g_node_append(parent->node, mimeinfo->node);
1736 mimeinfo->data.filename = g_strdup(filename);
1737 mimeinfo->offset = offset;
1738 mimeinfo->length = length;
1740 if (content_type != NULL) {
1741 procmime_parse_content_type(content_type, mimeinfo);
1743 mimeinfo->type = MIMETYPE_TEXT;
1744 mimeinfo->subtype = g_strdup("plain");
1745 if (g_hash_table_lookup(mimeinfo->typeparameters,
1746 "charset") == NULL) {
1747 g_hash_table_insert(mimeinfo->typeparameters,
1748 g_strdup("charset"),
1750 conv_get_locale_charset_str_no_utf8()));
1754 if (content_encoding != NULL) {
1755 procmime_parse_content_encoding(content_encoding, mimeinfo);
1757 mimeinfo->encoding_type = ENC_UNKNOWN;
1760 if (content_description != NULL)
1761 mimeinfo->description = g_strdup(content_description);
1763 mimeinfo->description = NULL;
1765 if (content_id != NULL)
1766 mimeinfo->id = g_strdup(content_id);
1768 mimeinfo->id = NULL;
1770 if (content_disposition != NULL)
1771 procmime_parse_content_disposition(content_disposition, mimeinfo);
1773 mimeinfo->disposition = DISPOSITIONTYPE_UNKNOWN;
1775 /* Call parser for mime type */
1776 switch (mimeinfo->type) {
1777 case MIMETYPE_MESSAGE:
1778 if (g_ascii_strcasecmp(mimeinfo->subtype, "rfc822") == 0) {
1779 procmime_parse_message_rfc822(mimeinfo);
1783 case MIMETYPE_MULTIPART:
1784 procmime_parse_multipart(mimeinfo);
1794 static gchar *typenames[] = {
1805 static gboolean output_func(GNode *node, gpointer data)
1808 MimeInfo *mimeinfo = (MimeInfo *) node->data;
1810 depth = g_node_depth(node);
1811 for (i = 0; i < depth; i++)
1813 printf("%s/%s (offset:%d length:%d encoding: %d)\n", typenames[mimeinfo->type], mimeinfo->subtype, mimeinfo->offset, mimeinfo->length, mimeinfo->encoding_type);
1818 static void output_mime_structure(MimeInfo *mimeinfo, int indent)
1820 g_node_traverse(mimeinfo->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1, output_func, NULL);
1823 MimeInfo *procmime_scan_file_with_offset(const gchar *filename, int offset)
1828 stat(filename, &buf);
1830 mimeinfo = procmime_mimeinfo_new();
1831 mimeinfo->content = MIMECONTENT_FILE;
1832 mimeinfo->encoding_type = ENC_UNKNOWN;
1833 mimeinfo->type = MIMETYPE_MESSAGE;
1834 mimeinfo->subtype = g_strdup("rfc822");
1835 mimeinfo->data.filename = g_strdup(filename);
1836 mimeinfo->offset = offset;
1837 mimeinfo->length = buf.st_size - offset;
1839 procmime_parse_message_rfc822(mimeinfo);
1840 if (debug_get_mode())
1841 output_mime_structure(mimeinfo, 0);
1846 MimeInfo *procmime_scan_file(const gchar *filename)
1850 g_return_val_if_fail(filename != NULL, NULL);
1852 mimeinfo = procmime_scan_file_with_offset(filename, 0);
1857 MimeInfo *procmime_scan_queue_file(const gchar *filename)
1861 gchar buf[BUFFSIZE];
1864 g_return_val_if_fail(filename != NULL, NULL);
1867 if ((fp = g_fopen(filename, "rb")) == NULL)
1869 /* Skip queue header */
1870 while (fgets(buf, sizeof(buf), fp) != NULL)
1871 if (buf[0] == '\r' || buf[0] == '\n') break;
1875 mimeinfo = procmime_scan_file_with_offset(filename, offset);
1882 ENC_AS_QUOTED_STRING,
1887 typedef struct _ParametersData {
1893 static void write_parameters(gpointer key, gpointer value, gpointer user_data)
1896 gchar *val = value, *valpos, *tmp;
1897 ParametersData *pdata = (ParametersData *)user_data;
1898 GString *buf = g_string_new("");
1900 EncodeAs encas = ENC_AS_TOKEN;
1902 for (valpos = val; *valpos != 0; valpos++) {
1903 if (!IS_ASCII(*valpos) || *valpos == '"') {
1904 encas = ENC_AS_EXTENDED;
1909 if (((*valpos >= 0) && (*valpos < 037)) || (*valpos == 0177)) {
1910 encas = ENC_AS_QUOTED_STRING;
1914 /* tspecials + SPACE */
1932 encas = ENC_AS_QUOTED_STRING;
1937 if (encas == ENC_AS_EXTENDED && pdata->ascii_only == TRUE)
1938 encas = ENC_TO_ASCII;
1942 g_string_append_printf(buf, "%s=%s", param, val);
1946 tmp = g_strdup(val);
1948 " ()<>@,';:\\/[]?=.0123456789"
1949 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1950 "abcdefghijklmnopqrstuvwxyz",
1952 g_string_append_printf(buf, "%s=\"%s\"", param, tmp);
1956 case ENC_AS_QUOTED_STRING:
1957 g_string_append_printf(buf, "%s=\"%s\"", param, val);
1960 case ENC_AS_EXTENDED:
1961 if (!g_utf8_validate(val, -1, NULL))
1962 g_string_append_printf(buf, "%s*=%s''", param,
1963 conv_get_locale_charset_str());
1965 g_string_append_printf(buf, "%s*=%s''", param,
1967 for (valpos = val; *valpos != '\0'; valpos++) {
1968 if (IS_ASCII(*valpos) && isalnum(*valpos)) {
1969 g_string_append_printf(buf, "%c", *valpos);
1971 gchar hexstr[3] = "XX";
1972 get_hex_str(hexstr, *valpos);
1973 g_string_append_printf(buf, "%%%s", hexstr);
1979 if (buf->str && strlen(buf->str)) {
1980 if (pdata->len + strlen(buf->str) + 2 > 76) {
1981 fprintf(pdata->fp, ";\n %s", buf->str);
1982 pdata->len = strlen(buf->str) + 1;
1984 fprintf(pdata->fp, "; %s", buf->str);
1985 pdata->len += strlen(buf->str) + 2;
1988 g_string_free(buf, TRUE);
1991 void procmime_write_mime_header(MimeInfo *mimeinfo, FILE *fp)
1993 struct TypeTable *type_table;
1994 ParametersData *pdata = g_new0(ParametersData, 1);
1995 debug_print("procmime_write_mime_header\n");
1998 pdata->ascii_only = FALSE;
2000 for (type_table = mime_type_table; type_table->str != NULL; type_table++)
2001 if (mimeinfo->type == type_table->type) {
2002 gchar *buf = g_strdup_printf(
2003 "Content-Type: %s/%s", type_table->str, mimeinfo->subtype);
2004 fprintf(fp, "%s", buf);
2005 pdata->len = strlen(buf);
2006 pdata->ascii_only = TRUE;
2010 g_hash_table_foreach(mimeinfo->typeparameters, write_parameters, pdata);
2015 if (mimeinfo->encoding_type != ENC_UNKNOWN)
2016 fprintf(fp, "Content-Transfer-Encoding: %s\n", procmime_get_encoding_str(mimeinfo->encoding_type));
2018 if (mimeinfo->description != NULL)
2019 fprintf(fp, "Content-Description: %s\n", mimeinfo->description);
2021 if (mimeinfo->id != NULL)
2022 fprintf(fp, "Content-ID: %s\n", mimeinfo->id);
2024 if (mimeinfo->disposition != DISPOSITIONTYPE_UNKNOWN) {
2025 ParametersData *pdata = g_new0(ParametersData, 1);
2027 if (mimeinfo->disposition == DISPOSITIONTYPE_INLINE)
2028 buf = g_strdup("Content-Disposition: inline");
2029 else if (mimeinfo->disposition == DISPOSITIONTYPE_ATTACHMENT)
2030 buf = g_strdup("Content-Disposition: attachment");
2032 buf = g_strdup("Content-Disposition: unknown");
2034 fprintf(fp, "%s", buf);
2035 pdata->len = strlen(buf);
2039 pdata->ascii_only = FALSE;
2041 g_hash_table_foreach(mimeinfo->dispositionparameters, write_parameters, pdata);
2049 gint procmime_write_message_rfc822(MimeInfo *mimeinfo, FILE *fp)
2054 gchar buf[BUFFSIZE];
2055 gboolean skip = FALSE;;
2057 debug_print("procmime_write_message_rfc822\n");
2060 switch (mimeinfo->content) {
2061 case MIMECONTENT_FILE:
2062 if ((infp = g_fopen(mimeinfo->data.filename, "rb")) == NULL) {
2063 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
2066 fseek(infp, mimeinfo->offset, SEEK_SET);
2067 while (fgets(buf, sizeof(buf), infp) == buf) {
2068 if (buf[0] == '\n' && buf[1] == '\0')
2070 if (skip && (buf[0] == ' ' || buf[0] == '\t'))
2072 if (g_ascii_strncasecmp(buf, "Mime-Version:", 13) == 0 ||
2073 g_ascii_strncasecmp(buf, "Content-Type:", 13) == 0 ||
2074 g_ascii_strncasecmp(buf, "Content-Transfer-Encoding:", 26) == 0 ||
2075 g_ascii_strncasecmp(buf, "Content-Description:", 20) == 0 ||
2076 g_ascii_strncasecmp(buf, "Content-ID:", 11) == 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);