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);
307 readend = mimeinfo->offset + mimeinfo->length;
309 if (encoding == ENC_QUOTED_PRINTABLE) {
310 while ((ftell(infp) < readend) && (fgets(buf, sizeof(buf), infp) != NULL)) {
312 len = qp_decode_line(buf);
313 fwrite(buf, 1, len, outfp);
315 } else if (encoding == ENC_BASE64) {
316 gchar outbuf[BUFFSIZE];
318 Base64Decoder *decoder;
319 gboolean got_error = FALSE;
320 gboolean uncanonicalize = FALSE;
323 if (mimeinfo->type == MIMETYPE_TEXT ||
324 mimeinfo->type == MIMETYPE_MESSAGE) {
325 uncanonicalize = TRUE;
326 tmpfp = my_tmpfile();
329 if (tmp_file) fclose(outfp);
334 decoder = base64_decoder_new();
335 while ((ftell(infp) < readend) && (fgets(buf, sizeof(buf), infp) != NULL)) {
336 len = base64_decoder_decode(decoder, buf, outbuf);
337 if (len < 0 && !got_error) {
338 g_warning("Bad BASE64 content.\n");
339 fwrite(_("[Error decoding BASE64]\n"),
341 strlen(_("[Error decoding BASE64]\n")),
345 } else if (len >= 0) {
346 /* print out the error message only once
348 fwrite(outbuf, sizeof(gchar), len, tmpfp);
352 base64_decoder_free(decoder);
354 if (uncanonicalize) {
356 while (fgets(buf, sizeof(buf), tmpfp) != NULL) {
362 } else if (encoding == ENC_X_UUENCODE) {
363 gchar outbuf[BUFFSIZE];
365 gboolean flag = FALSE;
367 while ((ftell(infp) < readend) && (fgets(buf, sizeof(buf), infp) != NULL)) {
368 if (!flag && strncmp(buf,"begin ", 6)) continue;
371 len = fromuutobits(outbuf, buf);
374 g_warning("Bad UUENCODE content(%d)\n", len);
377 fwrite(outbuf, sizeof(gchar), len, outfp);
382 while ((ftell(infp) < readend) && (fgets(buf, sizeof(buf), infp) != NULL)) {
390 stat(tmpfilename, &statbuf);
391 if (mimeinfo->tmp && (mimeinfo->data.filename != NULL))
392 g_unlink(mimeinfo->data.filename);
393 if (mimeinfo->data.filename != NULL)
394 g_free(mimeinfo->data.filename);
395 mimeinfo->data.filename = tmpfilename;
396 mimeinfo->tmp = TRUE;
397 mimeinfo->offset = 0;
398 mimeinfo->length = statbuf.st_size;
399 mimeinfo->encoding_type = ENC_BINARY;
404 #define B64_LINE_SIZE 57
405 #define B64_BUFFSIZE 77
407 gboolean procmime_encode_content(MimeInfo *mimeinfo, EncodingType encoding)
409 FILE *infp = NULL, *outfp;
414 if (mimeinfo->content == MIMECONTENT_EMPTY)
417 if (mimeinfo->encoding_type != ENC_UNKNOWN &&
418 mimeinfo->encoding_type != ENC_BINARY &&
419 mimeinfo->encoding_type != ENC_7BIT &&
420 mimeinfo->encoding_type != ENC_8BIT)
421 if(!procmime_decode_content(mimeinfo))
424 outfp = get_tmpfile_in_dir(get_mime_tmp_dir(), &tmpfilename);
430 if (mimeinfo->content == MIMECONTENT_FILE) {
431 if ((infp = g_fopen(mimeinfo->data.filename, "rb")) == NULL) {
432 g_warning("Can't open file %s\n", mimeinfo->data.filename);
435 } else if (mimeinfo->content == MIMECONTENT_MEM) {
436 infp = str_open_as_stream(mimeinfo->data.mem);
441 if (encoding == ENC_BASE64) {
442 gchar inbuf[B64_LINE_SIZE], outbuf[B64_BUFFSIZE];
444 gchar *tmp_file = NULL;
446 if (mimeinfo->type == MIMETYPE_TEXT ||
447 mimeinfo->type == MIMETYPE_MESSAGE) {
448 if (mimeinfo->content == MIMECONTENT_FILE) {
449 tmp_file = get_tmp_file();
450 if (canonicalize_file(mimeinfo->data.filename, tmp_file) < 0) {
455 if ((tmp_fp = g_fopen(tmp_file, "rb")) == NULL) {
456 FILE_OP_ERROR(tmp_file, "fopen");
463 gchar *out = canonicalize_str(mimeinfo->data.mem);
465 infp = str_open_as_stream(out);
473 while ((len = fread(inbuf, sizeof(gchar),
474 B64_LINE_SIZE, tmp_fp))
476 base64_encode(outbuf, inbuf, B64_LINE_SIZE);
477 fputs(outbuf, outfp);
480 if (len > 0 && feof(tmp_fp)) {
481 base64_encode(outbuf, inbuf, len);
482 fputs(outbuf, outfp);
491 } else if (encoding == ENC_QUOTED_PRINTABLE) {
492 gchar inbuf[BUFFSIZE], outbuf[BUFFSIZE * 4];
494 while (fgets(inbuf, sizeof(inbuf), infp) != NULL) {
495 qp_encode_line(outbuf, inbuf);
497 if (!strncmp("From ", outbuf, sizeof("From ")-1)) {
498 gchar *tmpbuf = outbuf;
500 tmpbuf += sizeof("From ")-1;
502 fputs("=46rom ", outfp);
503 fputs(tmpbuf, outfp);
505 fputs(outbuf, outfp);
510 while (fgets(buf, sizeof(buf), infp) != NULL) {
519 if (mimeinfo->content == MIMECONTENT_FILE) {
520 if (mimeinfo->tmp && (mimeinfo->data.filename != NULL))
521 g_unlink(mimeinfo->data.filename);
522 g_free(mimeinfo->data.filename);
523 } else if (mimeinfo->content == MIMECONTENT_MEM) {
524 if (mimeinfo->tmp && (mimeinfo->data.mem != NULL))
525 g_free(mimeinfo->data.mem);
528 stat(tmpfilename, &statbuf);
529 mimeinfo->content = MIMECONTENT_FILE;
530 mimeinfo->data.filename = tmpfilename;
531 mimeinfo->tmp = TRUE;
532 mimeinfo->offset = 0;
533 mimeinfo->length = statbuf.st_size;
534 mimeinfo->encoding_type = encoding;
539 gint procmime_get_part(const gchar *outfile, MimeInfo *mimeinfo)
543 gint restlength, readlength;
545 g_return_val_if_fail(outfile != NULL, -1);
546 g_return_val_if_fail(mimeinfo != NULL, -1);
548 if (mimeinfo->encoding_type != ENC_BINARY && !procmime_decode_content(mimeinfo))
551 if ((infp = g_fopen(mimeinfo->data.filename, "rb")) == NULL) {
552 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
555 if (fseek(infp, mimeinfo->offset, SEEK_SET) < 0) {
556 FILE_OP_ERROR(mimeinfo->data.filename, "fseek");
560 if ((outfp = g_fopen(outfile, "wb")) == NULL) {
561 FILE_OP_ERROR(outfile, "fopen");
566 restlength = mimeinfo->length;
568 while ((restlength > 0) && ((readlength = fread(buf, 1, restlength > BUFFSIZE ? BUFFSIZE : restlength, infp)) > 0)) {
569 fwrite(buf, 1, readlength, outfp);
570 restlength -= readlength;
574 if (fclose(outfp) == EOF) {
575 FILE_OP_ERROR(outfile, "fclose");
583 struct ContentRenderer {
588 static GList * renderer_list = NULL;
590 static struct ContentRenderer *
591 content_renderer_new(char * content_type, char * renderer)
593 struct ContentRenderer * cr;
595 cr = g_new(struct ContentRenderer, 1);
599 cr->content_type = g_strdup(content_type);
600 cr->renderer = g_strdup(renderer);
605 static void content_renderer_free(struct ContentRenderer * cr)
607 g_free(cr->content_type);
608 g_free(cr->renderer);
612 void renderer_read_config(void)
618 g_list_foreach(renderer_list, (GFunc) content_renderer_free, NULL);
619 renderer_list = NULL;
621 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, RENDERER_RC, NULL);
622 f = g_fopen(rcpath, "rb");
628 while (fgets(buf, BUFFSIZE, f)) {
630 struct ContentRenderer * cr;
633 p = strchr(buf, ' ');
638 cr = content_renderer_new(buf, p + 1);
642 renderer_list = g_list_append(renderer_list, cr);
648 void renderer_write_config(void)
654 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, RENDERER_RC, NULL);
656 if ((pfile = prefs_write_open(rcpath)) == NULL) {
657 g_warning("failed to write configuration to file\n");
664 for (cur = renderer_list ; cur != NULL ; cur = cur->next) {
665 struct ContentRenderer * renderer;
666 renderer = cur->data;
667 fprintf(pfile->fp, "%s %s\n", renderer->content_type,
671 if (prefs_file_close(pfile) < 0) {
672 g_warning("failed to write configuration to file\n");
677 FILE *procmime_get_text_content(MimeInfo *mimeinfo)
680 const gchar *src_codeset;
681 gboolean conv_fail = FALSE;
684 struct ContentRenderer * renderer;
686 gchar *tmpfile, *content_type;
688 g_return_val_if_fail(mimeinfo != NULL, NULL);
690 if (!procmime_decode_content(mimeinfo))
693 tmpfile = procmime_get_tmp_file_name(mimeinfo);
697 if (procmime_get_part(tmpfile, mimeinfo) < 0) {
702 tmpfp = g_fopen(tmpfile, "rb");
708 if ((outfp = my_tmpfile()) == NULL) {
715 src_codeset = forced_charset
717 procmime_mimeinfo_get_parameter(mimeinfo, "charset");
721 content_type = procmime_get_content_type_str(mimeinfo->type,
723 for (cur = renderer_list ; cur != NULL ; cur = cur->next) {
724 struct ContentRenderer * cr;
727 if (g_ascii_strcasecmp(cr->content_type, content_type) == 0) {
732 g_free(content_type);
734 if (renderer != NULL) {
740 dup2(fileno(outfp), 1);
742 p = popen(renderer->renderer, "w");
747 fread(buf, sizeof(char), sizeof(buf),
749 fwrite(buf, sizeof(char), count, p);
754 /* CodeConverter seems to have no effect here */
755 } else if (mimeinfo->type == MIMETYPE_TEXT && !g_ascii_strcasecmp(mimeinfo->subtype, "html")) {
759 conv = conv_code_converter_new(src_codeset);
760 parser = html_parser_new(tmpfp, conv);
761 while ((str = html_parse(parser)) != NULL) {
764 html_parser_destroy(parser);
765 conv_code_converter_destroy(conv);
766 } else if (mimeinfo->type == MIMETYPE_TEXT && !g_ascii_strcasecmp(mimeinfo->subtype, "enriched")) {
770 conv = conv_code_converter_new(src_codeset);
771 parser = ertf_parser_new(tmpfp, conv);
772 while ((str = ertf_parse(parser)) != NULL) {
775 ertf_parser_destroy(parser);
776 conv_code_converter_destroy(conv);
777 } else if (mimeinfo->type == MIMETYPE_TEXT) {
778 while (fgets(buf, sizeof(buf), tmpfp) != NULL) {
779 str = conv_codeset_strdup(buf, src_codeset, CS_UTF_8);
791 g_warning("procmime_get_text_content(): Code conversion failed.\n");
801 /* search the first text part of (multipart) MIME message,
802 decode, convert it and output to outfp. */
803 FILE *procmime_get_first_text_content(MsgInfo *msginfo)
806 MimeInfo *mimeinfo, *partinfo;
808 g_return_val_if_fail(msginfo != NULL, NULL);
810 mimeinfo = procmime_scan_message(msginfo);
811 if (!mimeinfo) return NULL;
814 while (partinfo && partinfo->type != MIMETYPE_TEXT) {
815 partinfo = procmime_mimeinfo_next(partinfo);
818 outfp = procmime_get_text_content(partinfo);
820 procmime_mimeinfo_free_all(mimeinfo);
826 static gboolean find_encrypted_func(GNode *node, gpointer data)
828 MimeInfo *mimeinfo = (MimeInfo *) node->data;
829 MimeInfo **encinfo = (MimeInfo **) data;
831 if (privacy_mimeinfo_is_encrypted(mimeinfo)) {
839 static MimeInfo *find_encrypted_part(MimeInfo *rootinfo)
841 MimeInfo *encinfo = NULL;
843 g_node_traverse(rootinfo->node, G_IN_ORDER, G_TRAVERSE_ALL, -1,
844 find_encrypted_func, &encinfo);
849 /* search the first encrypted text part of (multipart) MIME message,
850 decode, convert it and output to outfp. */
851 FILE *procmime_get_first_encrypted_text_content(MsgInfo *msginfo)
854 MimeInfo *mimeinfo, *partinfo, *encinfo;
856 g_return_val_if_fail(msginfo != NULL, NULL);
858 mimeinfo = procmime_scan_message(msginfo);
864 if ((encinfo = find_encrypted_part(partinfo)) != NULL) {
865 debug_print("decrypting message part\n");
866 if (privacy_mimeinfo_decrypt(encinfo) < 0)
870 while (partinfo && partinfo->type != MIMETYPE_TEXT) {
871 partinfo = procmime_mimeinfo_next(partinfo);
875 outfp = procmime_get_text_content(partinfo);
877 procmime_mimeinfo_free_all(mimeinfo);
882 gboolean procmime_msginfo_is_encrypted(MsgInfo *msginfo)
884 MimeInfo *mimeinfo, *partinfo;
885 gboolean result = FALSE;
887 g_return_val_if_fail(msginfo != NULL, FALSE);
889 mimeinfo = procmime_scan_message(msginfo);
895 result = (find_encrypted_part(partinfo) != NULL);
896 procmime_mimeinfo_free_all(mimeinfo);
901 gboolean procmime_find_string_part(MimeInfo *mimeinfo, const gchar *filename,
902 const gchar *str, StrFindFunc find_func)
907 g_return_val_if_fail(mimeinfo != NULL, FALSE);
908 g_return_val_if_fail(mimeinfo->type == MIMETYPE_TEXT, FALSE);
909 g_return_val_if_fail(str != NULL, FALSE);
910 g_return_val_if_fail(find_func != NULL, FALSE);
912 outfp = procmime_get_text_content(mimeinfo);
917 while (fgets(buf, sizeof(buf), outfp) != NULL) {
919 if (find_func(buf, str)) {
930 gboolean procmime_find_string(MsgInfo *msginfo, const gchar *str,
931 StrFindFunc find_func)
936 gboolean found = FALSE;
938 g_return_val_if_fail(msginfo != NULL, FALSE);
939 g_return_val_if_fail(str != NULL, FALSE);
940 g_return_val_if_fail(find_func != NULL, FALSE);
942 filename = procmsg_get_message_file(msginfo);
943 if (!filename) return FALSE;
944 mimeinfo = procmime_scan_message(msginfo);
946 for (partinfo = mimeinfo; partinfo != NULL;
947 partinfo = procmime_mimeinfo_next(partinfo)) {
948 if (partinfo->type == MIMETYPE_TEXT) {
949 if (procmime_find_string_part
950 (partinfo, filename, str, find_func) == TRUE) {
957 procmime_mimeinfo_free_all(mimeinfo);
963 gchar *procmime_get_tmp_file_name(MimeInfo *mimeinfo)
965 static guint32 id = 0;
970 g_return_val_if_fail(mimeinfo != NULL, NULL);
972 g_snprintf(f_prefix, sizeof(f_prefix), "%08x.", id++);
974 if ((mimeinfo->type == MIMETYPE_TEXT) && !g_ascii_strcasecmp(mimeinfo->subtype, "html"))
975 base = g_strdup("mimetmp.html");
977 const gchar *basetmp;
979 basetmp = procmime_mimeinfo_get_parameter(mimeinfo, "filename");
981 basetmp = procmime_mimeinfo_get_parameter(mimeinfo, "name");
984 basetmp = g_path_get_basename(basetmp);
985 if (*basetmp == '\0')
986 basetmp = g_strdup("mimetmp");
987 base = conv_filename_from_utf8(basetmp);
988 g_free((gchar*)basetmp);
989 subst_for_shellsafe_filename(base);
992 filename = g_strconcat(get_mime_tmp_dir(), G_DIR_SEPARATOR_S,
993 f_prefix, base, NULL);
1000 static GList *mime_type_list = NULL;
1002 gchar *procmime_get_mime_type(const gchar *filename)
1004 static GHashTable *mime_type_table = NULL;
1005 MimeType *mime_type;
1010 if (!mime_type_table) {
1011 mime_type_table = procmime_get_mime_type_table();
1012 if (!mime_type_table) return NULL;
1015 base = g_path_get_basename(filename);
1016 if ((p = strrchr(base, '.')) != NULL)
1017 Xstrdup_a(ext, p + 1, p = NULL );
1019 if (!p) return NULL;
1022 mime_type = g_hash_table_lookup(mime_type_table, ext);
1026 str = g_strconcat(mime_type->type, "/", mime_type->sub_type,
1034 static guint procmime_str_hash(gconstpointer gptr)
1036 guint hash_result = 0;
1039 for (str = gptr; str && *str; str++) {
1040 if (isupper(*str)) hash_result += (*str + ' ');
1041 else hash_result += *str;
1047 static gint procmime_str_equal(gconstpointer gptr1, gconstpointer gptr2)
1049 const char *str1 = gptr1;
1050 const char *str2 = gptr2;
1052 return !g_utf8_collate(str1, str2);
1055 static GHashTable *procmime_get_mime_type_table(void)
1057 GHashTable *table = NULL;
1059 MimeType *mime_type;
1062 if (!mime_type_list) {
1063 mime_type_list = procmime_get_mime_type_list();
1064 if (!mime_type_list) return NULL;
1067 table = g_hash_table_new(procmime_str_hash, procmime_str_equal);
1069 for (cur = mime_type_list; cur != NULL; cur = cur->next) {
1073 mime_type = (MimeType *)cur->data;
1075 if (!mime_type->extension) continue;
1077 exts = g_strsplit(mime_type->extension, " ", 16);
1078 for (i = 0; exts[i] != NULL; i++) {
1079 /* make the key case insensitive */
1081 /* use previously dup'd key on overwriting */
1082 if (g_hash_table_lookup(table, exts[i]))
1085 key = g_strdup(exts[i]);
1086 g_hash_table_insert(table, key, mime_type);
1094 GList *procmime_get_mime_type_list(void)
1098 gchar buf[BUFFSIZE];
1101 MimeType *mime_type;
1102 gboolean fp_is_glob_file = TRUE;
1105 return mime_type_list;
1107 if ((fp = g_fopen("/usr/share/mime/globs", "rb")) == NULL) {
1108 fp_is_glob_file = FALSE;
1109 if ((fp = g_fopen("/etc/mime.types", "rb")) == NULL) {
1110 if ((fp = g_fopen(SYSCONFDIR "/mime.types", "rb"))
1112 FILE_OP_ERROR(SYSCONFDIR "/mime.types",
1119 while (fgets(buf, sizeof(buf), fp) != NULL) {
1120 p = strchr(buf, '#');
1126 if (fp_is_glob_file) {
1127 while (*p && !g_ascii_isspace(*p) && (*p!=':')) p++;
1129 while (*p && !g_ascii_isspace(*p)) p++;
1136 delim = strchr(buf, '/');
1137 if (delim == NULL) continue;
1140 mime_type = g_new(MimeType, 1);
1141 mime_type->type = g_strdup(buf);
1142 mime_type->sub_type = g_strdup(delim + 1);
1144 if (fp_is_glob_file) {
1145 while (*p && (g_ascii_isspace(*p)||(*p=='*')||(*p=='.'))) p++;
1147 while (*p && g_ascii_isspace(*p)) p++;
1151 mime_type->extension = g_strdup(p);
1153 mime_type->extension = NULL;
1155 list = g_list_append(list, mime_type);
1161 g_warning("Can't read mime.types\n");
1166 EncodingType procmime_get_encoding_for_charset(const gchar *charset)
1170 else if (!g_ascii_strncasecmp(charset, "ISO-2022-", 9) ||
1171 !g_ascii_strcasecmp(charset, "US-ASCII"))
1173 else if (!g_ascii_strcasecmp(charset, "ISO-8859-5") ||
1174 !g_ascii_strncasecmp(charset, "KOI8-", 5) ||
1175 !g_ascii_strcasecmp(charset, "Windows-1251"))
1177 else if (!g_ascii_strncasecmp(charset, "ISO-8859-", 9))
1178 return ENC_QUOTED_PRINTABLE;
1183 EncodingType procmime_get_encoding_for_text_file(const gchar *file)
1186 guchar buf[BUFFSIZE];
1188 size_t octet_chars = 0;
1189 size_t total_len = 0;
1190 gfloat octet_percentage;
1192 if ((fp = g_fopen(file, "rb")) == NULL) {
1193 FILE_OP_ERROR(file, "fopen");
1197 while ((len = fread(buf, sizeof(guchar), sizeof(buf), fp)) > 0) {
1201 for (p = buf, i = 0; i < len; ++p, ++i) {
1211 octet_percentage = (gfloat)octet_chars / (gfloat)total_len;
1213 octet_percentage = 0.0;
1215 debug_print("procmime_get_encoding_for_text_file(): "
1216 "8bit chars: %d / %d (%f%%)\n", octet_chars, total_len,
1217 100.0 * octet_percentage);
1219 if (octet_percentage > 0.20) {
1220 debug_print("using BASE64\n");
1222 } else if (octet_chars > 0) {
1223 debug_print("using quoted-printable\n");
1224 return ENC_QUOTED_PRINTABLE;
1226 debug_print("using 7bit\n");
1231 struct EncodingTable
1234 EncodingType enc_type;
1237 struct EncodingTable encoding_table[] = {
1240 {"binary", ENC_BINARY},
1241 {"quoted-printable", ENC_QUOTED_PRINTABLE},
1242 {"base64", ENC_BASE64},
1243 {"x-uuencode", ENC_UNKNOWN},
1244 {NULL, ENC_UNKNOWN},
1247 const gchar *procmime_get_encoding_str(EncodingType encoding)
1249 struct EncodingTable *enc_table;
1251 for (enc_table = encoding_table; enc_table->str != NULL; enc_table++) {
1252 if (enc_table->enc_type == encoding)
1253 return enc_table->str;
1258 /* --- NEW MIME STUFF --- */
1265 static struct TypeTable mime_type_table[] = {
1266 {"text", MIMETYPE_TEXT},
1267 {"image", MIMETYPE_IMAGE},
1268 {"audio", MIMETYPE_AUDIO},
1269 {"video", MIMETYPE_VIDEO},
1270 {"application", MIMETYPE_APPLICATION},
1271 {"message", MIMETYPE_MESSAGE},
1272 {"multipart", MIMETYPE_MULTIPART},
1276 const gchar *procmime_get_media_type_str(MimeMediaType type)
1278 struct TypeTable *type_table;
1280 for (type_table = mime_type_table; type_table->str != NULL; type_table++) {
1281 if (type_table->type == type)
1282 return type_table->str;
1287 MimeMediaType procmime_get_media_type(const gchar *str)
1289 struct TypeTable *typetablearray;
1291 for (typetablearray = mime_type_table; typetablearray->str != NULL; typetablearray++)
1292 if (g_ascii_strncasecmp(str, typetablearray->str, strlen(typetablearray->str)) == 0)
1293 return typetablearray->type;
1295 return MIMETYPE_UNKNOWN;
1299 *\brief Safe wrapper for content type string.
1301 *\return const gchar * Pointer to content type string.
1303 gchar *procmime_get_content_type_str(MimeMediaType type,
1304 const char *subtype)
1306 const gchar *type_str = NULL;
1308 if (subtype == NULL || !(type_str = procmime_get_media_type_str(type)))
1309 return g_strdup("unknown");
1310 return g_strdup_printf("%s/%s", type_str, subtype);
1313 int procmime_parse_mimepart(MimeInfo *parent,
1314 gchar *content_type,
1315 gchar *content_encoding,
1316 gchar *content_description,
1318 gchar *content_disposition,
1319 const gchar *filename,
1323 void procmime_parse_message_rfc822(MimeInfo *mimeinfo)
1325 HeaderEntry hentry[] = {{"Content-Type:", NULL, TRUE},
1326 {"Content-Transfer-Encoding:",
1328 {"Content-Description:",
1332 {"Content-Disposition:",
1336 {NULL, NULL, FALSE}};
1337 guint content_start, i;
1341 procmime_decode_content(mimeinfo);
1343 fp = g_fopen(mimeinfo->data.filename, "rb");
1345 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
1348 fseek(fp, mimeinfo->offset, SEEK_SET);
1349 procheader_get_header_fields(fp, hentry);
1350 if (hentry[0].body != NULL) {
1351 tmp = conv_unmime_header(hentry[0].body, NULL);
1352 g_free(hentry[0].body);
1353 hentry[0].body = tmp;
1355 if (hentry[2].body != NULL) {
1356 tmp = conv_unmime_header(hentry[2].body, NULL);
1357 g_free(hentry[2].body);
1358 hentry[2].body = tmp;
1360 if (hentry[4].body != NULL) {
1361 tmp = conv_unmime_header(hentry[4].body, NULL);
1362 g_free(hentry[4].body);
1363 hentry[4].body = tmp;
1365 content_start = ftell(fp);
1368 procmime_parse_mimepart(mimeinfo,
1369 hentry[0].body, hentry[1].body,
1370 hentry[2].body, hentry[3].body,
1372 mimeinfo->data.filename, content_start,
1373 mimeinfo->length - (content_start - mimeinfo->offset));
1375 for (i = 0; i < (sizeof hentry / sizeof hentry[0]); i++) {
1376 g_free(hentry[i].body);
1377 hentry[i].body = NULL;
1381 void procmime_parse_multipart(MimeInfo *mimeinfo)
1383 HeaderEntry hentry[] = {{"Content-Type:", NULL, TRUE},
1384 {"Content-Transfer-Encoding:",
1386 {"Content-Description:",
1390 {"Content-Disposition:",
1392 {NULL, NULL, FALSE}};
1395 gint boundary_len = 0, lastoffset = -1, i;
1396 gchar buf[BUFFSIZE];
1400 boundary = g_hash_table_lookup(mimeinfo->typeparameters, "boundary");
1403 boundary_len = strlen(boundary);
1405 procmime_decode_content(mimeinfo);
1407 fp = g_fopen(mimeinfo->data.filename, "rb");
1409 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
1412 fseek(fp, mimeinfo->offset, SEEK_SET);
1413 while ((p = fgets(buf, sizeof(buf), fp)) != NULL && result == 0) {
1414 if (ftell(fp) > (mimeinfo->offset + mimeinfo->length))
1417 if (IS_BOUNDARY(buf, boundary, boundary_len)) {
1418 if (lastoffset != -1) {
1419 result = procmime_parse_mimepart(mimeinfo,
1420 hentry[0].body, hentry[1].body,
1421 hentry[2].body, hentry[3].body,
1423 mimeinfo->data.filename, lastoffset,
1424 (ftell(fp) - strlen(buf)) - lastoffset - 1);
1427 if (buf[2 + boundary_len] == '-' &&
1428 buf[2 + boundary_len + 1] == '-')
1431 for (i = 0; i < (sizeof hentry / sizeof hentry[0]) ; i++) {
1432 g_free(hentry[i].body);
1433 hentry[i].body = NULL;
1435 procheader_get_header_fields(fp, hentry);
1436 if (hentry[0].body != NULL) {
1437 tmp = conv_unmime_header(hentry[0].body, NULL);
1438 g_free(hentry[0].body);
1439 hentry[0].body = tmp;
1441 if (hentry[2].body != NULL) {
1442 tmp = conv_unmime_header(hentry[2].body, NULL);
1443 g_free(hentry[2].body);
1444 hentry[2].body = tmp;
1446 if (hentry[4].body != NULL) {
1447 tmp = conv_unmime_header(hentry[4].body, NULL);
1448 g_free(hentry[4].body);
1449 hentry[4].body = tmp;
1451 lastoffset = ftell(fp);
1454 for (i = 0; i < (sizeof hentry / sizeof hentry[0]); i++) {
1455 g_free(hentry[i].body);
1456 hentry[i].body = NULL;
1461 static void parse_parameters(const gchar *parameters, GHashTable *table)
1463 gchar *params, *param, *next;
1464 GSList *convlist = NULL, *concatlist = NULL, *cur;
1466 params = g_strdup(parameters);
1469 for (; next != NULL; param = next) {
1470 gchar *attribute, *value, *tmp;
1472 gboolean convert = FALSE;
1474 next = strchr_with_skip_quote(param, '"', ';');
1483 value = strchr(attribute, '=');
1489 while (value[0] == ' ')
1492 g_strdown(attribute);
1494 len = strlen(attribute);
1495 if (attribute[len - 1] == '*') {
1496 gchar *srcpos, *dstpos, *endpos;
1499 attribute[len - 1] = '\0';
1503 endpos = value + strlen(value);
1504 while (srcpos < endpos) {
1510 if (!get_hex_value(&dstvalue, srcpos[1], srcpos[2]))
1521 if (value[0] == '"')
1522 extract_quote(value, '"');
1523 else if ((tmp = strchr(value, ' ')) != NULL)
1528 while (attribute[0] == ' ')
1530 while (attribute[strlen(attribute)-1] == ' ')
1531 attribute[strlen(attribute)-1] = '\0';
1534 while (value[0] == ' ')
1536 while (value[strlen(value)-1] == ' ')
1537 value[strlen(value)-1] = '\0';
1539 if (strrchr(attribute, '*') != NULL) {
1542 tmpattr = g_strdup(attribute);
1543 tmp = strrchr(tmpattr, '*');
1546 if ((tmp[1] == '0') && (tmp[2] == '\0') &&
1547 (g_slist_find_custom(concatlist, attribute, g_str_equal) == NULL))
1548 concatlist = g_slist_prepend(concatlist, g_strdup(tmpattr));
1550 if (convert && (g_slist_find_custom(convlist, attribute, g_str_equal) == NULL))
1551 convlist = g_slist_prepend(convlist, g_strdup(tmpattr));
1554 } else if (convert) {
1555 if (g_slist_find_custom(convlist, attribute, g_str_equal) == NULL)
1556 convlist = g_slist_prepend(convlist, g_strdup(attribute));
1559 if (g_hash_table_lookup(table, attribute) == NULL)
1560 g_hash_table_insert(table, g_strdup(attribute), g_strdup(value));
1563 for (cur = concatlist; cur != NULL; cur = g_slist_next(cur)) {
1564 gchar *attribute, *attrwnum, *partvalue;
1568 attribute = (gchar *) cur->data;
1569 value = g_string_sized_new(64);
1571 attrwnum = g_strdup_printf("%s*%d", attribute, n);
1572 while ((partvalue = g_hash_table_lookup(table, attrwnum)) != NULL) {
1573 g_string_append(value, partvalue);
1577 attrwnum = g_strdup_printf("%s*%d", attribute, n);
1581 g_hash_table_insert(table, g_strdup(attribute), g_strdup(value->str));
1582 g_string_free(value, TRUE);
1584 slist_free_strings(concatlist);
1585 g_slist_free(concatlist);
1587 for (cur = convlist; cur != NULL; cur = g_slist_next(cur)) {
1588 gchar *attribute, *key, *value;
1589 gchar *charset, *lang, *oldvalue, *newvalue;
1591 attribute = (gchar *) cur->data;
1592 if (!g_hash_table_lookup_extended(
1593 table, attribute, (gpointer *)(gchar *) &key, (gpointer *)(gchar *) &value))
1597 lang = strchr(charset, '\'');
1602 oldvalue = strchr(lang, '\'');
1603 if (oldvalue == NULL)
1608 newvalue = conv_codeset_strdup(oldvalue, charset, CS_UTF_8);
1610 g_hash_table_remove(table, attribute);
1614 g_hash_table_insert(table, g_strdup(attribute), newvalue);
1616 slist_free_strings(convlist);
1617 g_slist_free(convlist);
1622 static void procmime_parse_content_type(const gchar *content_type, MimeInfo *mimeinfo)
1624 g_return_if_fail(content_type != NULL);
1625 g_return_if_fail(mimeinfo != NULL);
1627 /* RFC 2045, page 13 says that the mime subtype is MANDATORY;
1628 * if it's not available we use the default Content-Type */
1629 if ((content_type[0] == '\0') || (strchr(content_type, '/') == NULL)) {
1630 mimeinfo->type = MIMETYPE_TEXT;
1631 mimeinfo->subtype = g_strdup("plain");
1632 if (g_hash_table_lookup(mimeinfo->typeparameters,
1633 "charset") == NULL) {
1634 g_hash_table_insert(mimeinfo->typeparameters,
1635 g_strdup("charset"),
1637 conv_get_locale_charset_str_no_utf8()));
1640 gchar *type, *subtype, *params;
1642 type = g_strdup(content_type);
1643 subtype = strchr(type, '/') + 1;
1644 *(subtype - 1) = '\0';
1645 if ((params = strchr(subtype, ';')) != NULL) {
1650 mimeinfo->type = procmime_get_media_type(type);
1651 mimeinfo->subtype = g_strdup(subtype);
1653 /* Get mimeinfo->typeparameters */
1655 parse_parameters(params, mimeinfo->typeparameters);
1661 static void procmime_parse_content_disposition(const gchar *content_disposition, MimeInfo *mimeinfo)
1663 gchar *tmp, *params;
1665 g_return_if_fail(content_disposition != NULL);
1666 g_return_if_fail(mimeinfo != NULL);
1668 tmp = g_strdup(content_disposition);
1669 if ((params = strchr(tmp, ';')) != NULL) {
1675 if (!g_ascii_strcasecmp(tmp, "inline"))
1676 mimeinfo->disposition = DISPOSITIONTYPE_INLINE;
1677 else if (!g_ascii_strcasecmp(tmp, "attachment"))
1678 mimeinfo->disposition = DISPOSITIONTYPE_ATTACHMENT;
1680 mimeinfo->disposition = DISPOSITIONTYPE_ATTACHMENT;
1683 parse_parameters(params, mimeinfo->dispositionparameters);
1689 static void procmime_parse_content_encoding(const gchar *content_encoding, MimeInfo *mimeinfo)
1691 struct EncodingTable *enc_table;
1693 for (enc_table = encoding_table; enc_table->str != NULL; enc_table++) {
1694 if (g_ascii_strcasecmp(enc_table->str, content_encoding) == 0) {
1695 mimeinfo->encoding_type = enc_table->enc_type;
1699 mimeinfo->encoding_type = ENC_UNKNOWN;
1703 int procmime_parse_mimepart(MimeInfo *parent,
1704 gchar *content_type,
1705 gchar *content_encoding,
1706 gchar *content_description,
1708 gchar *content_disposition,
1709 const gchar *filename,
1715 /* Create MimeInfo */
1716 mimeinfo = procmime_mimeinfo_new();
1717 mimeinfo->content = MIMECONTENT_FILE;
1718 if (parent != NULL) {
1719 if (g_node_depth(parent->node) > 32) {
1720 /* 32 is an arbitrary value
1721 * this avoids DOSsing ourselves
1722 * with enormous messages
1724 procmime_mimeinfo_free_all(mimeinfo);
1727 g_node_append(parent->node, mimeinfo->node);
1729 mimeinfo->data.filename = g_strdup(filename);
1730 mimeinfo->offset = offset;
1731 mimeinfo->length = length;
1733 if (content_type != NULL) {
1734 procmime_parse_content_type(content_type, mimeinfo);
1736 mimeinfo->type = MIMETYPE_TEXT;
1737 mimeinfo->subtype = g_strdup("plain");
1738 if (g_hash_table_lookup(mimeinfo->typeparameters,
1739 "charset") == NULL) {
1740 g_hash_table_insert(mimeinfo->typeparameters,
1741 g_strdup("charset"),
1743 conv_get_locale_charset_str_no_utf8()));
1747 if (content_encoding != NULL) {
1748 procmime_parse_content_encoding(content_encoding, mimeinfo);
1750 mimeinfo->encoding_type = ENC_UNKNOWN;
1753 if (content_description != NULL)
1754 mimeinfo->description = g_strdup(content_description);
1756 mimeinfo->description = NULL;
1758 if (content_id != NULL)
1759 mimeinfo->id = g_strdup(content_id);
1761 mimeinfo->id = NULL;
1763 if (content_disposition != NULL)
1764 procmime_parse_content_disposition(content_disposition, mimeinfo);
1766 mimeinfo->disposition = DISPOSITIONTYPE_UNKNOWN;
1768 /* Call parser for mime type */
1769 switch (mimeinfo->type) {
1770 case MIMETYPE_MESSAGE:
1771 if (g_ascii_strcasecmp(mimeinfo->subtype, "rfc822") == 0) {
1772 procmime_parse_message_rfc822(mimeinfo);
1776 case MIMETYPE_MULTIPART:
1777 procmime_parse_multipart(mimeinfo);
1787 static gchar *typenames[] = {
1798 static gboolean output_func(GNode *node, gpointer data)
1801 MimeInfo *mimeinfo = (MimeInfo *) node->data;
1803 depth = g_node_depth(node);
1804 for (i = 0; i < depth; i++)
1806 printf("%s/%s (offset:%d length:%d encoding: %d)\n", typenames[mimeinfo->type], mimeinfo->subtype, mimeinfo->offset, mimeinfo->length, mimeinfo->encoding_type);
1811 static void output_mime_structure(MimeInfo *mimeinfo, int indent)
1813 g_node_traverse(mimeinfo->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1, output_func, NULL);
1816 MimeInfo *procmime_scan_file_with_offset(const gchar *filename, int offset)
1821 stat(filename, &buf);
1823 mimeinfo = procmime_mimeinfo_new();
1824 mimeinfo->content = MIMECONTENT_FILE;
1825 mimeinfo->encoding_type = ENC_UNKNOWN;
1826 mimeinfo->type = MIMETYPE_MESSAGE;
1827 mimeinfo->subtype = g_strdup("rfc822");
1828 mimeinfo->data.filename = g_strdup(filename);
1829 mimeinfo->offset = offset;
1830 mimeinfo->length = buf.st_size - offset;
1832 procmime_parse_message_rfc822(mimeinfo);
1833 if (debug_get_mode())
1834 output_mime_structure(mimeinfo, 0);
1839 MimeInfo *procmime_scan_file(const gchar *filename)
1843 g_return_val_if_fail(filename != NULL, NULL);
1845 mimeinfo = procmime_scan_file_with_offset(filename, 0);
1850 MimeInfo *procmime_scan_queue_file(const gchar *filename)
1854 gchar buf[BUFFSIZE];
1857 g_return_val_if_fail(filename != NULL, NULL);
1860 if ((fp = g_fopen(filename, "rb")) == NULL)
1862 /* Skip queue header */
1863 while (fgets(buf, sizeof(buf), fp) != NULL)
1864 if (buf[0] == '\r' || buf[0] == '\n') break;
1868 mimeinfo = procmime_scan_file_with_offset(filename, offset);
1875 ENC_AS_QUOTED_STRING,
1880 typedef struct _ParametersData {
1886 static void write_parameters(gpointer key, gpointer value, gpointer user_data)
1889 gchar *val = value, *valpos, *tmp;
1890 ParametersData *pdata = (ParametersData *)user_data;
1891 GString *buf = g_string_new("");
1893 EncodeAs encas = ENC_AS_TOKEN;
1895 for (valpos = val; *valpos != 0; valpos++) {
1896 if (!IS_ASCII(*valpos) || *valpos == '"') {
1897 encas = ENC_AS_EXTENDED;
1902 if (((*valpos >= 0) && (*valpos < 037)) || (*valpos == 0177)) {
1903 encas = ENC_AS_QUOTED_STRING;
1907 /* tspecials + SPACE */
1925 encas = ENC_AS_QUOTED_STRING;
1930 if (encas == ENC_AS_EXTENDED && pdata->ascii_only == TRUE)
1931 encas = ENC_TO_ASCII;
1935 g_string_append_printf(buf, "%s=%s", param, val);
1939 tmp = g_strdup(val);
1941 " ()<>@,';:\\/[]?=.0123456789"
1942 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1943 "abcdefghijklmnopqrstuvwxyz",
1945 g_string_append_printf(buf, "%s=\"%s\"", param, tmp);
1949 case ENC_AS_QUOTED_STRING:
1950 g_string_append_printf(buf, "%s=\"%s\"", param, val);
1953 case ENC_AS_EXTENDED:
1954 if (!g_utf8_validate(val, -1, NULL))
1955 g_string_append_printf(buf, "%s*=%s''", param,
1956 conv_get_locale_charset_str());
1958 g_string_append_printf(buf, "%s*=%s''", param,
1960 for (valpos = val; *valpos != '\0'; valpos++) {
1961 if (IS_ASCII(*valpos) && isalnum(*valpos)) {
1962 g_string_append_printf(buf, "%c", *valpos);
1964 gchar hexstr[3] = "XX";
1965 get_hex_str(hexstr, *valpos);
1966 g_string_append_printf(buf, "%%%s", hexstr);
1972 if (buf->str && strlen(buf->str)) {
1973 if (pdata->len + strlen(buf->str) + 2 > 76) {
1974 fprintf(pdata->fp, ";\n %s", buf->str);
1975 pdata->len = strlen(buf->str) + 1;
1977 fprintf(pdata->fp, "; %s", buf->str);
1978 pdata->len += strlen(buf->str) + 2;
1981 g_string_free(buf, TRUE);
1984 void procmime_write_mime_header(MimeInfo *mimeinfo, FILE *fp)
1986 struct TypeTable *type_table;
1987 ParametersData *pdata = g_new0(ParametersData, 1);
1988 debug_print("procmime_write_mime_header\n");
1991 pdata->ascii_only = FALSE;
1993 for (type_table = mime_type_table; type_table->str != NULL; type_table++)
1994 if (mimeinfo->type == type_table->type) {
1995 gchar *buf = g_strdup_printf(
1996 "Content-Type: %s/%s", type_table->str, mimeinfo->subtype);
1997 fprintf(fp, "%s", buf);
1998 pdata->len = strlen(buf);
1999 pdata->ascii_only = TRUE;
2003 g_hash_table_foreach(mimeinfo->typeparameters, write_parameters, pdata);
2008 if (mimeinfo->encoding_type != ENC_UNKNOWN)
2009 fprintf(fp, "Content-Transfer-Encoding: %s\n", procmime_get_encoding_str(mimeinfo->encoding_type));
2011 if (mimeinfo->description != NULL)
2012 fprintf(fp, "Content-Description: %s\n", mimeinfo->description);
2014 if (mimeinfo->id != NULL)
2015 fprintf(fp, "Content-ID: %s\n", mimeinfo->id);
2017 if (mimeinfo->disposition != DISPOSITIONTYPE_UNKNOWN) {
2018 ParametersData *pdata = g_new0(ParametersData, 1);
2020 if (mimeinfo->disposition == DISPOSITIONTYPE_INLINE)
2021 buf = g_strdup("Content-Disposition: inline");
2022 else if (mimeinfo->disposition == DISPOSITIONTYPE_ATTACHMENT)
2023 buf = g_strdup("Content-Disposition: attachment");
2025 buf = g_strdup("Content-Disposition: unknown");
2027 fprintf(fp, "%s", buf);
2028 pdata->len = strlen(buf);
2032 pdata->ascii_only = FALSE;
2034 g_hash_table_foreach(mimeinfo->dispositionparameters, write_parameters, pdata);
2042 gint procmime_write_message_rfc822(MimeInfo *mimeinfo, FILE *fp)
2047 gchar buf[BUFFSIZE];
2048 gboolean skip = FALSE;;
2050 debug_print("procmime_write_message_rfc822\n");
2053 switch (mimeinfo->content) {
2054 case MIMECONTENT_FILE:
2055 if ((infp = g_fopen(mimeinfo->data.filename, "rb")) == NULL) {
2056 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
2059 fseek(infp, mimeinfo->offset, SEEK_SET);
2060 while (fgets(buf, sizeof(buf), infp) == buf) {
2061 if (buf[0] == '\n' && buf[1] == '\0')
2063 if (skip && (buf[0] == ' ' || buf[0] == '\t'))
2065 if (g_ascii_strncasecmp(buf, "Mime-Version:", 13) == 0 ||
2066 g_ascii_strncasecmp(buf, "Content-Type:", 13) == 0 ||
2067 g_ascii_strncasecmp(buf, "Content-Transfer-Encoding:", 26) == 0 ||
2068 g_ascii_strncasecmp(buf, "Content-Description:", 20) == 0 ||
2069 g_ascii_strncasecmp(buf, "Content-ID:", 11) == 0 ||
2070 g_ascii_strncasecmp(buf, "Content-Disposition:", 20) == 0) {
2074 fwrite(buf, sizeof(gchar), strlen(buf), fp);
2080 case MIMECONTENT_MEM:
2081 fwrite(mimeinfo->data.mem,
2083 strlen(mimeinfo->data.mem),
2091 childnode = mimeinfo->node->children;
2092 if (childnode == NULL)
2095 child = (MimeInfo *) childnode->data;
2096 fprintf(fp, "Mime-Version: 1.0\n");
2097 procmime_write_mime_header(child, fp);
2098 return procmime_write_mimeinfo(child, fp);
2101 gint procmime_write_multipart(MimeInfo *mimeinfo, FILE *fp)
2105 gchar *boundary, *str, *str2;
2106 gchar buf[BUFFSIZE];
2107 gboolean firstboundary;
2109 debug_print("procmime_write_multipart\n");
2111 boundary = g_hash_table_lookup(mimeinfo->typeparameters, "boundary");
2113 switch (mimeinfo->content) {
2114 case MIMECONTENT_FILE:
2115 if ((infp = g_fopen(mimeinfo->data.filename, "rb")) == NULL) {
2116 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
2119 fseek(infp, mimeinfo->offset, SEEK_SET);
2120 while (fgets(buf, sizeof(buf), infp) == buf) {
2121 if (IS_BOUNDARY(buf, boundary, strlen(boundary)))
2123 fwrite(buf, sizeof(gchar), strlen(buf), fp);
2128 case MIMECONTENT_MEM:
2129 str = g_strdup(mimeinfo->data.mem);
2130 if (((str2 = strstr(str, boundary)) != NULL) && ((str2 - str) >= 2) &&
2131 (*(str2 - 1) == '-') && (*(str2 - 2) == '-'))
2133 fwrite(str, sizeof(gchar), strlen(str), fp);
2141 childnode = mimeinfo->node->children;
2142 firstboundary = TRUE;
2143 while (childnode != NULL) {
2144 MimeInfo *child = childnode->data;
2147 firstboundary = FALSE;
2150 fprintf(fp, "--%s\n", boundary);
2152 procmime_write_mime_header(child, fp);
2153 if (procmime_write_mimeinfo(child, fp) < 0)
2156 childnode = g_node_next_sibling(childnode);
2158 fprintf(fp, "\n--%s--\n", boundary);
2163 gint procmime_write_mimeinfo(MimeInfo *mimeinfo, FILE *fp)
2167 debug_print("procmime_write_mimeinfo\n");
2169 if (G_NODE_IS_LEAF(mimeinfo->node)) {
2170 switch (mimeinfo->content) {
2171 case MIMECONTENT_FILE:
2172 if ((infp = g_fopen(mimeinfo->data.filename, "rb")) == NULL) {
2173 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
2176 copy_file_part_to_fp(infp, mimeinfo->offset, mimeinfo->length, fp);
2180 case MIMECONTENT_MEM:
2181 fwrite(mimeinfo->data.mem,
2183 strlen(mimeinfo->data.mem),
2191 /* Call writer for mime type */
2192 switch (mimeinfo->type) {
2193 case MIMETYPE_MESSAGE:
2194 if (g_ascii_strcasecmp(mimeinfo->subtype, "rfc822") == 0) {
2195 return procmime_write_message_rfc822(mimeinfo, fp);
2199 case MIMETYPE_MULTIPART:
2200 return procmime_write_multipart(mimeinfo, fp);
2212 gchar *procmime_get_part_file_name(MimeInfo *mimeinfo)
2216 if ((mimeinfo->type == MIMETYPE_TEXT) && !g_ascii_strcasecmp(mimeinfo->subtype, "html"))
2217 base = g_strdup("mimetmp.html");
2219 const gchar *basetmp;
2222 basetmp = procmime_mimeinfo_get_parameter(mimeinfo, "filename");
2223 if (basetmp == NULL)
2224 basetmp = procmime_mimeinfo_get_parameter(mimeinfo, "name");
2225 if (basetmp == NULL)
2226 basetmp = "mimetmp";
2227 basename = g_path_get_basename(basetmp);
2228 if (*basename == '\0') {
2230 basename = g_strdup("mimetmp");
2232 base = conv_filename_from_utf8(basename);
2234 subst_for_shellsafe_filename(base);