2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2004 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
28 #include <glib/gi18n.h>
33 #include <sys/types.h>
38 #include "procheader.h"
40 #include "quoted-printable.h"
47 #include "prefs_common.h"
48 #include "prefs_gtk.h"
50 static GHashTable *procmime_get_mime_type_table (void);
52 MimeInfo *procmime_mimeinfo_new(void)
56 mimeinfo = g_new0(MimeInfo, 1);
57 mimeinfo->content = MIMECONTENT_EMPTY;
58 mimeinfo->data.filename = NULL;
60 mimeinfo->type = MIMETYPE_UNKNOWN;
61 mimeinfo->encoding_type = ENC_UNKNOWN;
62 mimeinfo->typeparameters = g_hash_table_new(g_str_hash, g_str_equal);
64 mimeinfo->disposition = DISPOSITIONTYPE_UNKNOWN;
65 mimeinfo->dispositionparameters
66 = g_hash_table_new(g_str_hash, g_str_equal);
68 mimeinfo->node = g_node_new(mimeinfo);
73 static gboolean procmime_mimeinfo_parameters_destroy(gpointer key, gpointer value, gpointer user_data)
81 static gchar *forced_charset = NULL;
83 void procmime_force_charset(const gchar *str)
85 g_free(forced_charset);
86 forced_charset = NULL;
88 forced_charset = g_strdup(str);
91 static EncodingType forced_encoding = 0;
93 void procmime_force_encoding(EncodingType encoding)
95 forced_encoding = encoding;
98 static gboolean free_func(GNode *node, gpointer data)
100 MimeInfo *mimeinfo = (MimeInfo *) node->data;
102 switch (mimeinfo->content) {
103 case MIMECONTENT_FILE:
105 unlink(mimeinfo->data.filename);
106 g_free(mimeinfo->data.filename);
109 case MIMECONTENT_MEM:
111 g_free(mimeinfo->data.mem);
116 g_free(mimeinfo->subtype);
117 g_free(mimeinfo->description);
118 g_free(mimeinfo->id);
120 g_hash_table_foreach_remove(mimeinfo->typeparameters,
121 procmime_mimeinfo_parameters_destroy, NULL);
122 g_hash_table_destroy(mimeinfo->typeparameters);
123 g_hash_table_foreach_remove(mimeinfo->dispositionparameters,
124 procmime_mimeinfo_parameters_destroy, NULL);
125 g_hash_table_destroy(mimeinfo->dispositionparameters);
127 if (mimeinfo->privacy)
128 privacy_free_privacydata(mimeinfo->privacy);
135 void procmime_mimeinfo_free_all(MimeInfo *mimeinfo)
142 node = mimeinfo->node;
143 g_node_traverse(node, G_IN_ORDER, G_TRAVERSE_ALL, -1, free_func, NULL);
145 g_node_destroy(node);
149 MimeInfo *procmime_mimeinfo_insert(MimeInfo *parent, MimeInfo *mimeinfo)
151 MimeInfo *child = parent->children;
154 parent->children = mimeinfo;
156 while (child->next != NULL)
159 child->next = mimeinfo;
162 mimeinfo->parent = parent;
163 mimeinfo->level = parent->level + 1;
168 void procmime_mimeinfo_replace(MimeInfo *old, MimeInfo *new)
170 MimeInfo *parent = old->parent;
173 g_return_if_fail(parent != NULL);
174 g_return_if_fail(new->next == NULL);
176 for (child = parent->children; child && child != old;
180 g_warning("oops: parent can't find it's own child");
183 procmime_mimeinfo_free_all(old);
185 if (child == parent->children) {
186 new->next = parent->children->next;
187 parent->children = new;
189 new->next = child->next;
195 MimeInfo *procmime_mimeinfo_parent(MimeInfo *mimeinfo)
197 g_return_val_if_fail(mimeinfo != NULL, NULL);
198 g_return_val_if_fail(mimeinfo->node != NULL, NULL);
200 if (mimeinfo->node->parent == NULL)
202 return (MimeInfo *) mimeinfo->node->parent->data;
205 MimeInfo *procmime_mimeinfo_next(MimeInfo *mimeinfo)
207 g_return_val_if_fail(mimeinfo != NULL, NULL);
208 g_return_val_if_fail(mimeinfo->node != NULL, NULL);
210 if (mimeinfo->node->children)
211 return (MimeInfo *) mimeinfo->node->children->data;
212 if (mimeinfo->node->next)
213 return (MimeInfo *) mimeinfo->node->next->data;
215 if (mimeinfo->node->parent == NULL)
218 while (mimeinfo->node->parent != NULL) {
219 mimeinfo = (MimeInfo *) mimeinfo->node->parent->data;
220 if (mimeinfo->node->next)
221 return (MimeInfo *) mimeinfo->node->next->data;
227 MimeInfo *procmime_scan_message(MsgInfo *msginfo)
232 filename = procmsg_get_message_file_path(msginfo);
233 if (!filename || !is_file_exist(filename)) {
235 filename = procmsg_get_message_file(msginfo);
237 if (!filename || !is_file_exist(filename))
240 if (msginfo->folder->stype != F_QUEUE &&
241 msginfo->folder->stype != F_DRAFT)
242 mimeinfo = procmime_scan_file(filename);
244 mimeinfo = procmime_scan_queue_file(filename);
252 H_CONTENT_TRANSFER_ENCODING = 0,
254 H_CONTENT_DISPOSITION = 2,
255 H_CONTENT_DESCRIPTION = 3,
259 const gchar *procmime_mimeinfo_get_parameter(MimeInfo *mimeinfo, const gchar *name)
263 g_return_val_if_fail(mimeinfo != NULL, NULL);
264 g_return_val_if_fail(name != NULL, NULL);
266 value = g_hash_table_lookup(mimeinfo->dispositionparameters, name);
268 value = g_hash_table_lookup(mimeinfo->typeparameters, name);
273 gboolean procmime_decode_content(MimeInfo *mimeinfo)
280 gboolean tmp_file = FALSE;
282 EncodingType encoding = forced_encoding
284 : mimeinfo->encoding_type;
286 g_return_val_if_fail(mimeinfo != NULL, FALSE);
288 if (encoding == ENC_UNKNOWN ||
289 encoding == ENC_BINARY)
292 infp = fopen(mimeinfo->data.filename, "rb");
297 fseek(infp, mimeinfo->offset, SEEK_SET);
299 outfp = get_tmpfile_in_dir(get_mime_tmp_dir(), &tmpfilename);
305 readend = mimeinfo->offset + mimeinfo->length;
307 if (encoding == ENC_QUOTED_PRINTABLE) {
308 while ((ftell(infp) < readend) && (fgets(buf, sizeof(buf), infp) != NULL)) {
310 len = qp_decode_line(buf);
311 fwrite(buf, len, 1, outfp);
313 } else if (encoding == ENC_BASE64) {
314 gchar outbuf[BUFFSIZE];
316 Base64Decoder *decoder;
317 gboolean got_error = FALSE;
318 gboolean uncanonicalize = FALSE;
321 if (mimeinfo->type == MIMETYPE_TEXT ||
322 mimeinfo->type == MIMETYPE_MESSAGE) {
323 uncanonicalize = TRUE;
324 tmpfp = my_tmpfile();
327 if (tmp_file) fclose(outfp);
332 decoder = base64_decoder_new();
333 while ((ftell(infp) < readend) && (fgets(buf, sizeof(buf), infp) != NULL)) {
334 len = base64_decoder_decode(decoder, buf, outbuf);
335 if (len < 0 && !got_error) {
336 g_warning("Bad BASE64 content.\n");
337 fwrite(_("[Error decoding BASE64]\n"),
339 strlen(_("[Error decoding BASE64]\n")),
343 } else if (len >= 0) {
344 /* print out the error message only once
346 fwrite(outbuf, sizeof(gchar), len, tmpfp);
350 base64_decoder_free(decoder);
352 if (uncanonicalize) {
354 while (fgets(buf, sizeof(buf), tmpfp) != NULL) {
360 } else if (encoding == ENC_X_UUENCODE) {
361 gchar outbuf[BUFFSIZE];
363 gboolean flag = FALSE;
365 while ((ftell(infp) < readend) && (fgets(buf, sizeof(buf), infp) != NULL)) {
366 if (!flag && strncmp(buf,"begin ", 6)) continue;
369 len = fromuutobits(outbuf, buf);
372 g_warning("Bad UUENCODE content(%d)\n", len);
375 fwrite(outbuf, sizeof(gchar), len, outfp);
380 while ((ftell(infp) < readend) && (fgets(buf, sizeof(buf), infp) != NULL)) {
388 stat(tmpfilename, &statbuf);
389 if (mimeinfo->tmp && (mimeinfo->data.filename != NULL))
390 unlink(mimeinfo->data.filename);
391 if (mimeinfo->data.filename != NULL)
392 g_free(mimeinfo->data.filename);
393 mimeinfo->data.filename = tmpfilename;
394 mimeinfo->tmp = TRUE;
395 mimeinfo->offset = 0;
396 mimeinfo->length = statbuf.st_size;
397 mimeinfo->encoding_type = ENC_BINARY;
402 #define B64_LINE_SIZE 57
403 #define B64_BUFFSIZE 77
405 gboolean procmime_encode_content(MimeInfo *mimeinfo, EncodingType encoding)
407 FILE *infp = NULL, *outfp;
412 if (mimeinfo->encoding_type != ENC_UNKNOWN &&
413 mimeinfo->encoding_type != ENC_BINARY &&
414 mimeinfo->encoding_type != ENC_7BIT &&
415 mimeinfo->encoding_type != ENC_8BIT)
416 if(!procmime_decode_content(mimeinfo))
419 outfp = get_tmpfile_in_dir(get_mime_tmp_dir(), &tmpfilename);
425 if (mimeinfo->content == MIMECONTENT_FILE) {
426 if ((infp = fopen(mimeinfo->data.filename, "rb")) == NULL) {
427 g_warning("Can't open file %s\n", mimeinfo->data.filename);
430 } else if (mimeinfo->content == MIMECONTENT_MEM) {
431 infp = str_open_as_stream(mimeinfo->data.mem);
436 if (encoding == ENC_BASE64) {
437 gchar inbuf[B64_LINE_SIZE], outbuf[B64_BUFFSIZE];
439 gchar *tmp_file = NULL;
441 if (mimeinfo->type == MIMETYPE_TEXT ||
442 mimeinfo->type == MIMETYPE_MESSAGE) {
443 if (mimeinfo->content == MIMECONTENT_FILE) {
444 tmp_file = get_tmp_file();
445 if (canonicalize_file(mimeinfo->data.filename, tmp_file) < 0) {
450 if ((tmp_fp = fopen(tmp_file, "rb")) == NULL) {
451 FILE_OP_ERROR(tmp_file, "fopen");
458 gchar *out = canonicalize_str(mimeinfo->data.mem);
460 infp = str_open_as_stream(out);
468 while ((len = fread(inbuf, sizeof(gchar),
469 B64_LINE_SIZE, tmp_fp))
471 base64_encode(outbuf, inbuf, B64_LINE_SIZE);
472 fputs(outbuf, outfp);
475 if (len > 0 && feof(tmp_fp)) {
476 base64_encode(outbuf, inbuf, len);
477 fputs(outbuf, outfp);
486 } else if (encoding == ENC_QUOTED_PRINTABLE) {
487 gchar inbuf[BUFFSIZE], outbuf[BUFFSIZE * 4];
489 while (fgets(inbuf, sizeof(inbuf), infp) != NULL) {
490 qp_encode_line(outbuf, inbuf);
492 if (!strncmp("From ", outbuf, sizeof("From ")-1)) {
493 gchar *tmpbuf = outbuf;
495 tmpbuf += sizeof("From ")-1;
497 fputs("=46rom ", outfp);
498 fputs(tmpbuf, outfp);
500 fputs(outbuf, outfp);
505 while (fgets(buf, sizeof(buf), infp) != NULL) {
514 if (mimeinfo->content == MIMECONTENT_FILE) {
515 if (mimeinfo->tmp && (mimeinfo->data.filename != NULL))
516 unlink(mimeinfo->data.filename);
517 g_free(mimeinfo->data.filename);
518 } else if (mimeinfo->content == MIMECONTENT_MEM) {
519 if (mimeinfo->tmp && (mimeinfo->data.mem != NULL))
520 g_free(mimeinfo->data.mem);
523 stat(tmpfilename, &statbuf);
524 mimeinfo->content = MIMECONTENT_FILE;
525 mimeinfo->data.filename = tmpfilename;
526 mimeinfo->tmp = TRUE;
527 mimeinfo->offset = 0;
528 mimeinfo->length = statbuf.st_size;
529 mimeinfo->encoding_type = encoding;
534 gint procmime_get_part(const gchar *outfile, MimeInfo *mimeinfo)
538 gint restlength, readlength;
540 g_return_val_if_fail(outfile != NULL, -1);
541 g_return_val_if_fail(mimeinfo != NULL, -1);
543 if (mimeinfo->encoding_type != ENC_BINARY && !procmime_decode_content(mimeinfo))
546 if ((infp = fopen(mimeinfo->data.filename, "rb")) == NULL) {
547 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
550 if (fseek(infp, mimeinfo->offset, SEEK_SET) < 0) {
551 FILE_OP_ERROR(mimeinfo->data.filename, "fseek");
555 if ((outfp = fopen(outfile, "wb")) == NULL) {
556 FILE_OP_ERROR(outfile, "fopen");
561 restlength = mimeinfo->length;
563 while ((restlength > 0) && ((readlength = fread(buf, 1, restlength > BUFFSIZE ? BUFFSIZE : restlength, infp)) > 0)) {
564 fwrite(buf, 1, readlength, outfp);
565 restlength -= readlength;
569 if (fclose(outfp) == EOF) {
570 FILE_OP_ERROR(outfile, "fclose");
578 struct ContentRenderer {
583 static GList * renderer_list = NULL;
585 static struct ContentRenderer *
586 content_renderer_new(char * content_type, char * renderer)
588 struct ContentRenderer * cr;
590 cr = g_new(struct ContentRenderer, 1);
594 cr->content_type = g_strdup(content_type);
595 cr->renderer = g_strdup(renderer);
600 static void content_renderer_free(struct ContentRenderer * cr)
602 g_free(cr->content_type);
603 g_free(cr->renderer);
607 void renderer_read_config(void)
613 g_list_foreach(renderer_list, (GFunc) content_renderer_free, NULL);
614 renderer_list = NULL;
616 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, RENDERER_RC, NULL);
617 f = fopen(rcpath, "rb");
623 while (fgets(buf, BUFFSIZE, f)) {
625 struct ContentRenderer * cr;
628 p = strchr(buf, ' ');
633 cr = content_renderer_new(buf, p + 1);
637 renderer_list = g_list_append(renderer_list, cr);
643 void renderer_write_config(void)
649 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, RENDERER_RC, NULL);
651 if ((pfile = prefs_write_open(rcpath)) == NULL) {
652 g_warning("failed to write configuration to file\n");
659 for (cur = renderer_list ; cur != NULL ; cur = cur->next) {
660 struct ContentRenderer * renderer;
661 renderer = cur->data;
662 fprintf(pfile->fp, "%s %s\n", renderer->content_type,
666 if (prefs_file_close(pfile) < 0) {
667 g_warning("failed to write configuration to file\n");
672 FILE *procmime_get_text_content(MimeInfo *mimeinfo)
675 const gchar *src_codeset;
676 gboolean conv_fail = FALSE;
679 struct ContentRenderer * renderer;
681 gchar *tmpfile, *content_type;
683 g_return_val_if_fail(mimeinfo != NULL, NULL);
685 if (!procmime_decode_content(mimeinfo))
688 tmpfile = procmime_get_tmp_file_name(mimeinfo);
692 if (procmime_get_part(tmpfile, mimeinfo) < 0) {
697 tmpfp = fopen(tmpfile, "rb");
703 if ((outfp = my_tmpfile()) == NULL) {
710 src_codeset = forced_charset
712 procmime_mimeinfo_get_parameter(mimeinfo, "charset");
716 content_type = procmime_get_content_type_str(mimeinfo->type,
718 for (cur = renderer_list ; cur != NULL ; cur = cur->next) {
719 struct ContentRenderer * cr;
722 if (g_ascii_strcasecmp(cr->content_type, content_type) == 0) {
727 g_free(content_type);
729 if (renderer != NULL) {
735 dup2(fileno(outfp), 1);
737 p = popen(renderer->renderer, "w");
742 fread(buf, sizeof(char), sizeof(buf),
744 fwrite(buf, sizeof(char), count, p);
749 /* CodeConverter seems to have no effect here */
750 } else if (mimeinfo->type == MIMETYPE_TEXT && !g_ascii_strcasecmp(mimeinfo->subtype, "html")) {
754 conv = conv_code_converter_new(src_codeset);
755 parser = html_parser_new(tmpfp, conv);
756 while ((str = html_parse(parser)) != NULL) {
759 html_parser_destroy(parser);
760 conv_code_converter_destroy(conv);
761 } else if (mimeinfo->type == MIMETYPE_TEXT && !g_ascii_strcasecmp(mimeinfo->subtype, "enriched")) {
765 conv = conv_code_converter_new(src_codeset);
766 parser = ertf_parser_new(tmpfp, conv);
767 while ((str = ertf_parse(parser)) != NULL) {
770 ertf_parser_destroy(parser);
771 conv_code_converter_destroy(conv);
772 } else if (mimeinfo->type == MIMETYPE_TEXT) {
773 while (fgets(buf, sizeof(buf), tmpfp) != NULL) {
774 str = conv_codeset_strdup(buf, src_codeset, CS_UTF_8);
786 g_warning("procmime_get_text_content(): Code conversion failed.\n");
796 /* search the first text part of (multipart) MIME message,
797 decode, convert it and output to outfp. */
798 FILE *procmime_get_first_text_content(MsgInfo *msginfo)
801 MimeInfo *mimeinfo, *partinfo;
803 g_return_val_if_fail(msginfo != NULL, NULL);
805 mimeinfo = procmime_scan_message(msginfo);
806 if (!mimeinfo) return NULL;
809 while (partinfo && partinfo->type != MIMETYPE_TEXT)
810 partinfo = procmime_mimeinfo_next(partinfo);
813 outfp = procmime_get_text_content(partinfo);
815 procmime_mimeinfo_free_all(mimeinfo);
820 gboolean procmime_find_string_part(MimeInfo *mimeinfo, const gchar *filename,
821 const gchar *str, StrFindFunc find_func)
826 g_return_val_if_fail(mimeinfo != NULL, FALSE);
827 g_return_val_if_fail(mimeinfo->type == MIMETYPE_TEXT, FALSE);
828 g_return_val_if_fail(str != NULL, FALSE);
829 g_return_val_if_fail(find_func != NULL, FALSE);
831 outfp = procmime_get_text_content(mimeinfo);
836 while (fgets(buf, sizeof(buf), outfp) != NULL) {
838 if (find_func(buf, str)) {
849 gboolean procmime_find_string(MsgInfo *msginfo, const gchar *str,
850 StrFindFunc find_func)
855 gboolean found = FALSE;
857 g_return_val_if_fail(msginfo != NULL, FALSE);
858 g_return_val_if_fail(str != NULL, FALSE);
859 g_return_val_if_fail(find_func != NULL, FALSE);
861 filename = procmsg_get_message_file(msginfo);
862 if (!filename) return FALSE;
863 mimeinfo = procmime_scan_message(msginfo);
865 for (partinfo = mimeinfo; partinfo != NULL;
866 partinfo = procmime_mimeinfo_next(partinfo)) {
867 if (partinfo->type == MIMETYPE_TEXT) {
868 if (procmime_find_string_part
869 (partinfo, filename, str, find_func) == TRUE) {
876 procmime_mimeinfo_free_all(mimeinfo);
882 gchar *procmime_get_tmp_file_name(MimeInfo *mimeinfo)
884 static guint32 id = 0;
889 g_return_val_if_fail(mimeinfo != NULL, NULL);
891 g_snprintf(f_prefix, sizeof(f_prefix), "%08x.", id++);
893 if ((mimeinfo->type == MIMETYPE_TEXT) && !g_ascii_strcasecmp(mimeinfo->subtype, "html"))
894 base = g_strdup("mimetmp.html");
896 const gchar *basetmp;
898 basetmp = procmime_mimeinfo_get_parameter(mimeinfo, "filename");
900 basetmp = procmime_mimeinfo_get_parameter(mimeinfo, "name");
903 basetmp = g_path_get_basename(basetmp);
904 if (*basetmp == '\0') basetmp = g_strdup("mimetmp");
905 base = conv_filename_from_utf8(basetmp);
906 subst_for_shellsafe_filename(base);
909 filename = g_strconcat(get_mime_tmp_dir(), G_DIR_SEPARATOR_S,
910 f_prefix, base, NULL);
917 static GList *mime_type_list = NULL;
919 gchar *procmime_get_mime_type(const gchar *filename)
921 static GHashTable *mime_type_table = NULL;
927 if (!mime_type_table) {
928 mime_type_table = procmime_get_mime_type_table();
929 if (!mime_type_table) return NULL;
932 base = g_path_get_basename(filename);
933 if ((p = strrchr(base, '.')) != NULL)
934 Xstrdup_a(ext, p + 1, p = NULL );
939 mime_type = g_hash_table_lookup(mime_type_table, ext);
943 str = g_strconcat(mime_type->type, "/", mime_type->sub_type,
951 static guint procmime_str_hash(gconstpointer gptr)
953 guint hash_result = 0;
956 for (str = gptr; str && *str; str++) {
957 if (isupper(*str)) hash_result += (*str + ' ');
958 else hash_result += *str;
964 static gint procmime_str_equal(gconstpointer gptr1, gconstpointer gptr2)
966 const char *str1 = gptr1;
967 const char *str2 = gptr2;
969 return !g_utf8_collate(str1, str2);
972 static GHashTable *procmime_get_mime_type_table(void)
974 GHashTable *table = NULL;
979 if (!mime_type_list) {
980 mime_type_list = procmime_get_mime_type_list();
981 if (!mime_type_list) return NULL;
984 table = g_hash_table_new(procmime_str_hash, procmime_str_equal);
986 for (cur = mime_type_list; cur != NULL; cur = cur->next) {
990 mime_type = (MimeType *)cur->data;
992 if (!mime_type->extension) continue;
994 exts = g_strsplit(mime_type->extension, " ", 16);
995 for (i = 0; exts[i] != NULL; i++) {
996 /* make the key case insensitive */
998 /* use previously dup'd key on overwriting */
999 if (g_hash_table_lookup(table, exts[i]))
1002 key = g_strdup(exts[i]);
1003 g_hash_table_insert(table, key, mime_type);
1011 GList *procmime_get_mime_type_list(void)
1015 gchar buf[BUFFSIZE];
1018 MimeType *mime_type;
1019 gboolean fp_is_glob_file = TRUE;
1022 return mime_type_list;
1024 if ((fp = fopen("/usr/share/mime/globs", "rb")) == NULL) {
1025 fp_is_glob_file = FALSE;
1026 if ((fp = fopen("/etc/mime.types", "rb")) == NULL) {
1027 if ((fp = fopen(SYSCONFDIR "/mime.types", "rb"))
1029 FILE_OP_ERROR(SYSCONFDIR "/mime.types",
1036 while (fgets(buf, sizeof(buf), fp) != NULL) {
1037 p = strchr(buf, '#');
1043 if (fp_is_glob_file) {
1044 while (*p && !isspace(*p) && (*p!=':')) p++;
1046 while (*p && !isspace(*p)) p++;
1053 delim = strchr(buf, '/');
1054 if (delim == NULL) continue;
1057 mime_type = g_new(MimeType, 1);
1058 mime_type->type = g_strdup(buf);
1059 mime_type->sub_type = g_strdup(delim + 1);
1061 if (fp_is_glob_file) {
1062 while (*p && (isspace(*p)||(*p=='*')||(*p=='.'))) p++;
1064 while (*p && isspace(*p)) p++;
1068 mime_type->extension = g_strdup(p);
1070 mime_type->extension = NULL;
1072 list = g_list_append(list, mime_type);
1078 g_warning("Can't read mime.types\n");
1083 EncodingType procmime_get_encoding_for_charset(const gchar *charset)
1087 else if (!g_ascii_strncasecmp(charset, "ISO-2022-", 9) ||
1088 !g_ascii_strcasecmp(charset, "US-ASCII"))
1090 else if (!g_ascii_strcasecmp(charset, "ISO-8859-5") ||
1091 !g_ascii_strncasecmp(charset, "KOI8-", 5) ||
1092 !g_ascii_strcasecmp(charset, "Windows-1251"))
1094 else if (!g_ascii_strncasecmp(charset, "ISO-8859-", 9))
1095 return ENC_QUOTED_PRINTABLE;
1100 EncodingType procmime_get_encoding_for_text_file(const gchar *file)
1103 guchar buf[BUFFSIZE];
1105 size_t octet_chars = 0;
1106 size_t total_len = 0;
1107 gfloat octet_percentage;
1109 if ((fp = fopen(file, "rb")) == NULL) {
1110 FILE_OP_ERROR(file, "fopen");
1114 while ((len = fread(buf, sizeof(guchar), sizeof(buf), fp)) > 0) {
1118 for (p = buf, i = 0; i < len; ++p, ++i) {
1128 octet_percentage = (gfloat)octet_chars / (gfloat)total_len;
1130 octet_percentage = 0.0;
1132 debug_print("procmime_get_encoding_for_text_file(): "
1133 "8bit chars: %d / %d (%f%%)\n", octet_chars, total_len,
1134 100.0 * octet_percentage);
1136 if (octet_percentage > 0.20) {
1137 debug_print("using BASE64\n");
1139 } else if (octet_chars > 0) {
1140 debug_print("using quoted-printable\n");
1141 return ENC_QUOTED_PRINTABLE;
1143 debug_print("using 7bit\n");
1148 struct EncodingTable
1151 EncodingType enc_type;
1154 struct EncodingTable encoding_table[] = {
1157 {"binary", ENC_BINARY},
1158 {"quoted-printable", ENC_QUOTED_PRINTABLE},
1159 {"base64", ENC_BASE64},
1160 {"x-uuencode", ENC_UNKNOWN},
1161 {NULL, ENC_UNKNOWN},
1164 const gchar *procmime_get_encoding_str(EncodingType encoding)
1166 struct EncodingTable *enc_table;
1168 for (enc_table = encoding_table; enc_table->str != NULL; enc_table++) {
1169 if (enc_table->enc_type == encoding)
1170 return enc_table->str;
1175 /* --- NEW MIME STUFF --- */
1182 static struct TypeTable mime_type_table[] = {
1183 {"text", MIMETYPE_TEXT},
1184 {"image", MIMETYPE_IMAGE},
1185 {"audio", MIMETYPE_AUDIO},
1186 {"video", MIMETYPE_VIDEO},
1187 {"application", MIMETYPE_APPLICATION},
1188 {"message", MIMETYPE_MESSAGE},
1189 {"multipart", MIMETYPE_MULTIPART},
1193 const gchar *procmime_get_media_type_str(MimeMediaType type)
1195 struct TypeTable *type_table;
1197 for (type_table = mime_type_table; type_table->str != NULL; type_table++) {
1198 if (type_table->type == type)
1199 return type_table->str;
1204 MimeMediaType procmime_get_media_type(const gchar *str)
1206 struct TypeTable *typetablearray;
1208 for (typetablearray = mime_type_table; typetablearray->str != NULL; typetablearray++)
1209 if (g_ascii_strncasecmp(str, typetablearray->str, strlen(typetablearray->str)) == 0)
1210 return typetablearray->type;
1212 return MIMETYPE_UNKNOWN;
1216 *\brief Safe wrapper for content type string.
1218 *\return const gchar * Pointer to content type string.
1220 gchar *procmime_get_content_type_str(MimeMediaType type,
1221 const char *subtype)
1223 const gchar *type_str = NULL;
1225 if (subtype == NULL || !(type_str = procmime_get_media_type_str(type)))
1226 return g_strdup("unknown");
1227 return g_strdup_printf("%s/%s", type_str, subtype);
1230 int procmime_parse_mimepart(MimeInfo *parent,
1231 gchar *content_type,
1232 gchar *content_encoding,
1233 gchar *content_description,
1235 gchar *content_disposition,
1236 const gchar *filename,
1240 void procmime_parse_message_rfc822(MimeInfo *mimeinfo)
1242 HeaderEntry hentry[] = {{"Content-Type:", NULL, TRUE},
1243 {"Content-Transfer-Encoding:",
1245 {"Content-Description:",
1249 {"Content-Disposition:",
1253 {NULL, NULL, FALSE}};
1254 guint content_start, i;
1258 procmime_decode_content(mimeinfo);
1260 fp = fopen(mimeinfo->data.filename, "rb");
1262 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
1265 fseek(fp, mimeinfo->offset, SEEK_SET);
1266 procheader_get_header_fields(fp, hentry);
1267 if (hentry[0].body != NULL) {
1268 tmp = conv_unmime_header(hentry[0].body, NULL);
1269 g_free(hentry[0].body);
1270 hentry[0].body = tmp;
1272 if (hentry[2].body != NULL) {
1273 tmp = conv_unmime_header(hentry[2].body, NULL);
1274 g_free(hentry[2].body);
1275 hentry[2].body = tmp;
1277 if (hentry[4].body != NULL) {
1278 tmp = conv_unmime_header(hentry[4].body, NULL);
1279 g_free(hentry[4].body);
1280 hentry[4].body = tmp;
1282 content_start = ftell(fp);
1285 procmime_parse_mimepart(mimeinfo,
1286 hentry[0].body, hentry[1].body,
1287 hentry[2].body, hentry[3].body,
1289 mimeinfo->data.filename, content_start,
1290 mimeinfo->length - (content_start - mimeinfo->offset));
1292 for (i = 0; i < (sizeof hentry / sizeof hentry[0]); i++) {
1293 g_free(hentry[i].body);
1294 hentry[i].body = NULL;
1298 void procmime_parse_multipart(MimeInfo *mimeinfo)
1300 HeaderEntry hentry[] = {{"Content-Type:", NULL, TRUE},
1301 {"Content-Transfer-Encoding:",
1303 {"Content-Description:",
1307 {"Content-Disposition:",
1309 {NULL, NULL, FALSE}};
1312 gint boundary_len = 0, lastoffset = -1, i;
1313 gchar buf[BUFFSIZE];
1317 boundary = g_hash_table_lookup(mimeinfo->typeparameters, "boundary");
1320 boundary_len = strlen(boundary);
1322 procmime_decode_content(mimeinfo);
1324 fp = fopen(mimeinfo->data.filename, "rb");
1326 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
1329 fseek(fp, mimeinfo->offset, SEEK_SET);
1330 while ((p = fgets(buf, sizeof(buf), fp)) != NULL && result == 0) {
1331 if (ftell(fp) > (mimeinfo->offset + mimeinfo->length))
1334 if (IS_BOUNDARY(buf, boundary, boundary_len)) {
1335 if (lastoffset != -1) {
1336 result = procmime_parse_mimepart(mimeinfo,
1337 hentry[0].body, hentry[1].body,
1338 hentry[2].body, hentry[3].body,
1340 mimeinfo->data.filename, lastoffset,
1341 (ftell(fp) - strlen(buf)) - lastoffset - 1);
1344 if (buf[2 + boundary_len] == '-' &&
1345 buf[2 + boundary_len + 1] == '-')
1348 for (i = 0; i < (sizeof hentry / sizeof hentry[0]) ; i++) {
1349 g_free(hentry[i].body);
1350 hentry[i].body = NULL;
1352 procheader_get_header_fields(fp, hentry);
1353 if (hentry[0].body != NULL) {
1354 tmp = conv_unmime_header(hentry[0].body, NULL);
1355 g_free(hentry[0].body);
1356 hentry[0].body = tmp;
1358 if (hentry[2].body != NULL) {
1359 tmp = conv_unmime_header(hentry[2].body, NULL);
1360 g_free(hentry[2].body);
1361 hentry[2].body = tmp;
1363 if (hentry[4].body != NULL) {
1364 tmp = conv_unmime_header(hentry[4].body, NULL);
1365 g_free(hentry[4].body);
1366 hentry[4].body = tmp;
1368 lastoffset = ftell(fp);
1371 for (i = 0; i < (sizeof hentry / sizeof hentry[0]); i++) {
1372 g_free(hentry[i].body);
1373 hentry[i].body = NULL;
1378 static void parse_parameters(const gchar *parameters, GHashTable *table)
1380 gchar *params, *param, *next;
1381 GSList *convlist = NULL, *concatlist = NULL, *cur;
1383 params = g_strdup(parameters);
1386 for (; next != NULL; param = next) {
1387 gchar *attribute, *value, *tmp;
1389 gboolean convert = FALSE;
1391 next = strchr_with_skip_quote(param, '"', ';');
1400 value = strchr(attribute, '=');
1407 g_strdown(attribute);
1409 len = strlen(attribute);
1410 if (attribute[len - 1] == '*') {
1411 gchar *srcpos, *dstpos, *endpos;
1414 attribute[len - 1] = '\0';
1418 endpos = value + strlen(value);
1419 while (srcpos < endpos) {
1425 if (!get_hex_value(&dstvalue, srcpos[1], srcpos[2]))
1436 if (value[0] == '"')
1437 extract_quote(value, '"');
1438 else if ((tmp = strchr(value, ' ')) != NULL)
1442 if (strrchr(attribute, '*') != NULL) {
1445 tmpattr = g_strdup(attribute);
1446 tmp = strrchr(tmpattr, '*');
1449 if ((tmp[1] == '0') && (tmp[2] == '\0') &&
1450 (g_slist_find_custom(concatlist, attribute, g_str_equal) == NULL))
1451 concatlist = g_slist_prepend(concatlist, g_strdup(tmpattr));
1453 if (convert && (g_slist_find_custom(convlist, attribute, g_str_equal) == NULL))
1454 convlist = g_slist_prepend(convlist, g_strdup(tmpattr));
1457 } else if (convert) {
1458 if (g_slist_find_custom(convlist, attribute, g_str_equal) == NULL)
1459 convlist = g_slist_prepend(convlist, g_strdup(attribute));
1462 if (g_hash_table_lookup(table, attribute) == NULL)
1463 g_hash_table_insert(table, g_strdup(attribute), g_strdup(value));
1466 for (cur = concatlist; cur != NULL; cur = g_slist_next(cur)) {
1467 gchar *attribute, *attrwnum, *partvalue;
1471 attribute = (gchar *) cur->data;
1472 value = g_string_sized_new(64);
1474 attrwnum = g_strdup_printf("%s*%d", attribute, n);
1475 while ((partvalue = g_hash_table_lookup(table, attrwnum)) != NULL) {
1476 g_string_append(value, partvalue);
1480 attrwnum = g_strdup_printf("%s*%d", attribute, n);
1484 g_hash_table_insert(table, g_strdup(attribute), g_strdup(value->str));
1485 g_string_free(value, TRUE);
1487 slist_free_strings(concatlist);
1488 g_slist_free(concatlist);
1490 for (cur = convlist; cur != NULL; cur = g_slist_next(cur)) {
1491 gchar *attribute, *key, *value;
1492 gchar *charset, *lang, *oldvalue, *newvalue;
1494 attribute = (gchar *) cur->data;
1495 if (!g_hash_table_lookup_extended(table, attribute, (gpointer *) &key, (gpointer *) &value))
1499 lang = strchr(charset, '\'');
1504 oldvalue = strchr(lang, '\'');
1505 if (oldvalue == NULL)
1510 newvalue = conv_codeset_strdup(oldvalue, charset, CS_UTF_8);
1512 g_hash_table_remove(table, attribute);
1516 g_hash_table_insert(table, g_strdup(attribute), newvalue);
1518 slist_free_strings(convlist);
1519 g_slist_free(convlist);
1524 static void procmime_parse_content_type(const gchar *content_type, MimeInfo *mimeinfo)
1526 g_return_if_fail(content_type != NULL);
1527 g_return_if_fail(mimeinfo != NULL);
1529 /* RFC 2045, page 13 says that the mime subtype is MANDATORY;
1530 * if it's not available we use the default Content-Type */
1531 if ((content_type[0] == '\0') || (strchr(content_type, '/') == NULL)) {
1532 mimeinfo->type = MIMETYPE_TEXT;
1533 mimeinfo->subtype = g_strdup("plain");
1534 if (g_hash_table_lookup(mimeinfo->typeparameters,
1536 g_hash_table_insert(mimeinfo->typeparameters,
1537 g_strdup("charset"),
1538 g_strdup("us-ascii"));
1540 gchar *type, *subtype, *params;
1542 type = g_strdup(content_type);
1543 subtype = strchr(type, '/') + 1;
1544 *(subtype - 1) = '\0';
1545 if ((params = strchr(subtype, ';')) != NULL) {
1550 mimeinfo->type = procmime_get_media_type(type);
1551 mimeinfo->subtype = g_strdup(subtype);
1553 /* Get mimeinfo->typeparameters */
1555 parse_parameters(params, mimeinfo->typeparameters);
1561 static void procmime_parse_content_disposition(const gchar *content_disposition, MimeInfo *mimeinfo)
1563 gchar *tmp, *params;
1565 g_return_if_fail(content_disposition != NULL);
1566 g_return_if_fail(mimeinfo != NULL);
1568 tmp = g_strdup(content_disposition);
1569 if ((params = strchr(tmp, ';')) != NULL) {
1575 if (!g_ascii_strcasecmp(tmp, "inline"))
1576 mimeinfo->disposition = DISPOSITIONTYPE_INLINE;
1577 else if (!g_ascii_strcasecmp(tmp, "attachment"))
1578 mimeinfo->disposition = DISPOSITIONTYPE_ATTACHMENT;
1580 mimeinfo->disposition = DISPOSITIONTYPE_ATTACHMENT;
1583 parse_parameters(params, mimeinfo->dispositionparameters);
1589 static void procmime_parse_content_encoding(const gchar *content_encoding, MimeInfo *mimeinfo)
1591 struct EncodingTable *enc_table;
1593 for (enc_table = encoding_table; enc_table->str != NULL; enc_table++) {
1594 if (g_ascii_strcasecmp(enc_table->str, content_encoding) == 0) {
1595 mimeinfo->encoding_type = enc_table->enc_type;
1599 mimeinfo->encoding_type = ENC_UNKNOWN;
1603 int procmime_parse_mimepart(MimeInfo *parent,
1604 gchar *content_type,
1605 gchar *content_encoding,
1606 gchar *content_description,
1608 gchar *content_disposition,
1609 const gchar *filename,
1615 /* Create MimeInfo */
1616 mimeinfo = procmime_mimeinfo_new();
1617 mimeinfo->content = MIMECONTENT_FILE;
1618 if (parent != NULL) {
1619 if (g_node_depth(parent->node) > 32) {
1620 /* 32 is an arbitrary value
1621 * this avoids DOSsing ourselves
1622 * with enormous messages
1624 procmime_mimeinfo_free_all(mimeinfo);
1627 g_node_append(parent->node, mimeinfo->node);
1629 mimeinfo->data.filename = g_strdup(filename);
1630 mimeinfo->offset = offset;
1631 mimeinfo->length = length;
1633 if (content_type != NULL) {
1634 procmime_parse_content_type(content_type, mimeinfo);
1636 mimeinfo->type = MIMETYPE_TEXT;
1637 mimeinfo->subtype = g_strdup("plain");
1638 if (g_hash_table_lookup(mimeinfo->typeparameters,
1640 g_hash_table_insert(mimeinfo->typeparameters, g_strdup("charset"), g_strdup("us-ascii"));
1643 if (content_encoding != NULL) {
1644 procmime_parse_content_encoding(content_encoding, mimeinfo);
1646 mimeinfo->encoding_type = ENC_UNKNOWN;
1649 if (content_description != NULL)
1650 mimeinfo->description = g_strdup(content_description);
1652 mimeinfo->description = NULL;
1654 if (content_id != NULL)
1655 mimeinfo->id = g_strdup(content_id);
1657 mimeinfo->id = NULL;
1659 if (content_disposition != NULL)
1660 procmime_parse_content_disposition(content_disposition, mimeinfo);
1662 mimeinfo->disposition = DISPOSITIONTYPE_UNKNOWN;
1664 /* Call parser for mime type */
1665 switch (mimeinfo->type) {
1666 case MIMETYPE_MESSAGE:
1667 if (g_ascii_strcasecmp(mimeinfo->subtype, "rfc822") == 0) {
1668 procmime_parse_message_rfc822(mimeinfo);
1672 case MIMETYPE_MULTIPART:
1673 procmime_parse_multipart(mimeinfo);
1683 static gchar *typenames[] = {
1694 static gboolean output_func(GNode *node, gpointer data)
1697 MimeInfo *mimeinfo = (MimeInfo *) node->data;
1699 depth = g_node_depth(node);
1700 for (i = 0; i < depth; i++)
1702 printf("%s/%s (offset:%d length:%d encoding: %d)\n", typenames[mimeinfo->type], mimeinfo->subtype, mimeinfo->offset, mimeinfo->length, mimeinfo->encoding_type);
1707 static void output_mime_structure(MimeInfo *mimeinfo, int indent)
1709 g_node_traverse(mimeinfo->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1, output_func, NULL);
1712 MimeInfo *procmime_scan_file_with_offset(const gchar *filename, int offset)
1717 stat(filename, &buf);
1719 mimeinfo = procmime_mimeinfo_new();
1720 mimeinfo->content = MIMECONTENT_FILE;
1721 mimeinfo->encoding_type = ENC_UNKNOWN;
1722 mimeinfo->type = MIMETYPE_MESSAGE;
1723 mimeinfo->subtype = g_strdup("rfc822");
1724 mimeinfo->data.filename = g_strdup(filename);
1725 mimeinfo->offset = offset;
1726 mimeinfo->length = buf.st_size - offset;
1728 procmime_parse_message_rfc822(mimeinfo);
1729 if (debug_get_mode())
1730 output_mime_structure(mimeinfo, 0);
1735 MimeInfo *procmime_scan_file(const gchar *filename)
1739 g_return_val_if_fail(filename != NULL, NULL);
1741 mimeinfo = procmime_scan_file_with_offset(filename, 0);
1746 MimeInfo *procmime_scan_queue_file(const gchar *filename)
1750 gchar buf[BUFFSIZE];
1753 g_return_val_if_fail(filename != NULL, NULL);
1756 if ((fp = fopen(filename, "rb")) == NULL)
1758 /* Skip queue header */
1759 while (fgets(buf, sizeof(buf), fp) != NULL)
1760 if (buf[0] == '\r' || buf[0] == '\n') break;
1764 mimeinfo = procmime_scan_file_with_offset(filename, offset);
1771 ENC_AS_QUOTED_STRING,
1775 typedef struct _ParametersData {
1780 static void write_parameters(gpointer key, gpointer value, gpointer user_data)
1783 gchar *val = value, *valpos;
1784 ParametersData *pdata = (ParametersData *)user_data;
1785 GString *buf = g_string_new("");
1787 EncodeAs encas = ENC_AS_TOKEN;
1789 for (valpos = val; *valpos != 0; valpos++) {
1790 if (!IS_ASCII(*valpos)) {
1791 encas = ENC_AS_EXTENDED;
1796 if (((*valpos >= 0) && (*valpos < 037)) || (*valpos == 0177)) {
1797 encas = ENC_AS_QUOTED_STRING;
1801 /* tspecials + SPACE */
1819 encas = ENC_AS_QUOTED_STRING;
1826 g_string_append_printf(buf, "%s=%s", param, val);
1829 case ENC_AS_QUOTED_STRING:
1830 g_string_append_printf(buf, "%s=\"%s\"", param, val);
1833 case ENC_AS_EXTENDED:
1834 if (!g_utf8_validate(val, -1, NULL))
1835 g_string_append_printf(buf, "%s*=%s''", param,
1836 conv_get_locale_charset_str());
1838 g_string_append_printf(buf, "%s*=%s''", param,
1840 for (valpos = val; *valpos != '\0'; valpos++) {
1841 if (IS_ASCII(*valpos) && isalnum(*valpos)) {
1842 g_string_append_printf(buf, "%c", *valpos);
1844 gchar hexstr[3] = "XX";
1845 get_hex_str(hexstr, *valpos);
1846 g_string_append_printf(buf, "%%%s", hexstr);
1852 if (buf->str && strlen(buf->str)) {
1853 if (pdata->len + strlen(buf->str) + 2 > 76) {
1854 fprintf(pdata->fp, ";\n %s", buf->str);
1855 pdata->len = strlen(buf->str) + 1;
1857 fprintf(pdata->fp, "; %s", buf->str);
1858 pdata->len += strlen(buf->str) + 2;
1861 g_string_free(buf, TRUE);
1864 void procmime_write_mime_header(MimeInfo *mimeinfo, FILE *fp)
1866 struct TypeTable *type_table;
1867 ParametersData *pdata = g_new0(ParametersData, 1);
1868 debug_print("procmime_write_mime_header\n");
1872 for (type_table = mime_type_table; type_table->str != NULL; type_table++)
1873 if (mimeinfo->type == type_table->type) {
1874 gchar *buf = g_strdup_printf(
1875 "Content-Type: %s/%s", type_table->str, mimeinfo->subtype);
1876 fprintf(fp, "%s", buf);
1877 pdata->len = strlen(buf);
1881 g_hash_table_foreach(mimeinfo->typeparameters, write_parameters, pdata);
1886 if (mimeinfo->encoding_type != ENC_UNKNOWN)
1887 fprintf(fp, "Content-Transfer-Encoding: %s\n", procmime_get_encoding_str(mimeinfo->encoding_type));
1889 if (mimeinfo->description != NULL)
1890 fprintf(fp, "Content-Description: %s\n", mimeinfo->description);
1892 if (mimeinfo->id != NULL)
1893 fprintf(fp, "Content-ID: %s\n", mimeinfo->id);
1895 if (mimeinfo->disposition != DISPOSITIONTYPE_UNKNOWN) {
1896 ParametersData *pdata = g_new0(ParametersData, 1);
1898 if (mimeinfo->disposition == DISPOSITIONTYPE_INLINE)
1899 buf = g_strdup("Content-Disposition: inline");
1900 else if (mimeinfo->disposition == DISPOSITIONTYPE_ATTACHMENT)
1901 buf = g_strdup("Content-Disposition: attachment");
1903 buf = g_strdup("Content-Disposition: unknown");
1905 fprintf(fp, "%s", buf);
1906 pdata->len = strlen(buf);
1910 g_hash_table_foreach(mimeinfo->dispositionparameters, write_parameters, pdata);
1918 gint procmime_write_message_rfc822(MimeInfo *mimeinfo, FILE *fp)
1923 gchar buf[BUFFSIZE];
1924 gboolean skip = FALSE;;
1926 debug_print("procmime_write_message_rfc822\n");
1929 switch (mimeinfo->content) {
1930 case MIMECONTENT_FILE:
1931 if ((infp = fopen(mimeinfo->data.filename, "rb")) == NULL) {
1932 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
1935 fseek(infp, mimeinfo->offset, SEEK_SET);
1936 while (fgets(buf, sizeof(buf), infp) == buf) {
1937 if (buf[0] == '\n' && buf[1] == '\0')
1939 if (skip && (buf[0] == ' ' || buf[0] == '\t'))
1941 if (g_ascii_strncasecmp(buf, "Mime-Version:", 13) == 0 ||
1942 g_ascii_strncasecmp(buf, "Content-Type:", 13) == 0 ||
1943 g_ascii_strncasecmp(buf, "Content-Transfer-Encoding:", 26) == 0 ||
1944 g_ascii_strncasecmp(buf, "Content-Description:", 20) == 0 ||
1945 g_ascii_strncasecmp(buf, "Content-ID:", 11) == 0 ||
1946 g_ascii_strncasecmp(buf, "Content-Disposition:", 20) == 0) {
1950 fwrite(buf, sizeof(gchar), strlen(buf), fp);
1956 case MIMECONTENT_MEM:
1957 fwrite(mimeinfo->data.mem, strlen(mimeinfo->data.mem), sizeof(gchar), fp);
1964 childnode = mimeinfo->node->children;
1965 if (childnode == NULL)
1968 child = (MimeInfo *) childnode->data;
1969 fprintf(fp, "Mime-Version: 1.0\n");
1970 procmime_write_mime_header(child, fp);
1971 return procmime_write_mimeinfo(child, fp);
1974 gint procmime_write_multipart(MimeInfo *mimeinfo, FILE *fp)
1978 gchar *boundary, *str, *str2;
1979 gchar buf[BUFFSIZE];
1980 gboolean firstboundary;
1982 debug_print("procmime_write_multipart\n");
1984 boundary = g_hash_table_lookup(mimeinfo->typeparameters, "boundary");
1986 switch (mimeinfo->content) {
1987 case MIMECONTENT_FILE:
1988 if ((infp = fopen(mimeinfo->data.filename, "rb")) == NULL) {
1989 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
1992 fseek(infp, mimeinfo->offset, SEEK_SET);
1993 while (fgets(buf, sizeof(buf), infp) == buf) {
1994 if (IS_BOUNDARY(buf, boundary, strlen(boundary)))
1996 fwrite(buf, sizeof(gchar), strlen(buf), fp);
2001 case MIMECONTENT_MEM:
2002 str = g_strdup(mimeinfo->data.mem);
2003 if (((str2 = strstr(str, boundary)) != NULL) && ((str2 - str) >= 2) &&
2004 (*(str2 - 1) == '-') && (*(str2 - 2) == '-'))
2006 fwrite(str, strlen(str), sizeof(gchar), fp);
2014 childnode = mimeinfo->node->children;
2015 firstboundary = TRUE;
2016 while (childnode != NULL) {
2017 MimeInfo *child = childnode->data;
2020 firstboundary = FALSE;
2023 fprintf(fp, "--%s\n", boundary);
2025 procmime_write_mime_header(child, fp);
2026 if (procmime_write_mimeinfo(child, fp) < 0)
2029 childnode = g_node_next_sibling(childnode);
2031 fprintf(fp, "\n--%s--\n", boundary);
2036 gint procmime_write_mimeinfo(MimeInfo *mimeinfo, FILE *fp)
2040 debug_print("procmime_write_mimeinfo\n");
2042 if (G_NODE_IS_LEAF(mimeinfo->node)) {
2043 switch (mimeinfo->content) {
2044 case MIMECONTENT_FILE:
2045 if ((infp = fopen(mimeinfo->data.filename, "rb")) == NULL) {
2046 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
2049 copy_file_part_to_fp(infp, mimeinfo->offset, mimeinfo->length, fp);
2053 case MIMECONTENT_MEM:
2054 fwrite(mimeinfo->data.mem, strlen(mimeinfo->data.mem), sizeof(gchar), fp);
2061 /* Call writer for mime type */
2062 switch (mimeinfo->type) {
2063 case MIMETYPE_MESSAGE:
2064 if (g_ascii_strcasecmp(mimeinfo->subtype, "rfc822") == 0)
2065 return procmime_write_message_rfc822(mimeinfo, fp);
2068 case MIMETYPE_MULTIPART:
2069 return procmime_write_multipart(mimeinfo, fp);
2081 gchar *procmime_get_part_file_name(MimeInfo *mimeinfo)
2085 if ((mimeinfo->type == MIMETYPE_TEXT) && !g_ascii_strcasecmp(mimeinfo->subtype, "html"))
2086 base = g_strdup("mimetmp.html");
2088 const gchar *basetmp;
2091 basetmp = procmime_mimeinfo_get_parameter(mimeinfo, "filename");
2092 if (basetmp == NULL)
2093 basetmp = procmime_mimeinfo_get_parameter(mimeinfo, "name");
2094 if (basetmp == NULL)
2095 basetmp = "mimetmp";
2096 basename = g_path_get_basename(basetmp);
2097 if (*basename == '\0') {
2099 basename = g_strdup("mimetmp");
2101 base = conv_filename_from_utf8(basename);
2103 subst_for_shellsafe_filename(base);