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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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 g_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 (!folder_has_parent_of_type(msginfo->folder, F_QUEUE) &&
241 !folder_has_parent_of_type(msginfo->folder, 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 = g_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, 1, len, 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 g_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->content == MIMECONTENT_EMPTY)
415 if (mimeinfo->encoding_type != ENC_UNKNOWN &&
416 mimeinfo->encoding_type != ENC_BINARY &&
417 mimeinfo->encoding_type != ENC_7BIT &&
418 mimeinfo->encoding_type != ENC_8BIT)
419 if(!procmime_decode_content(mimeinfo))
422 outfp = get_tmpfile_in_dir(get_mime_tmp_dir(), &tmpfilename);
428 if (mimeinfo->content == MIMECONTENT_FILE) {
429 if ((infp = g_fopen(mimeinfo->data.filename, "rb")) == NULL) {
430 g_warning("Can't open file %s\n", mimeinfo->data.filename);
433 } else if (mimeinfo->content == MIMECONTENT_MEM) {
434 infp = str_open_as_stream(mimeinfo->data.mem);
439 if (encoding == ENC_BASE64) {
440 gchar inbuf[B64_LINE_SIZE], outbuf[B64_BUFFSIZE];
442 gchar *tmp_file = NULL;
444 if (mimeinfo->type == MIMETYPE_TEXT ||
445 mimeinfo->type == MIMETYPE_MESSAGE) {
446 if (mimeinfo->content == MIMECONTENT_FILE) {
447 tmp_file = get_tmp_file();
448 if (canonicalize_file(mimeinfo->data.filename, tmp_file) < 0) {
453 if ((tmp_fp = g_fopen(tmp_file, "rb")) == NULL) {
454 FILE_OP_ERROR(tmp_file, "fopen");
461 gchar *out = canonicalize_str(mimeinfo->data.mem);
463 infp = str_open_as_stream(out);
471 while ((len = fread(inbuf, sizeof(gchar),
472 B64_LINE_SIZE, tmp_fp))
474 base64_encode(outbuf, inbuf, B64_LINE_SIZE);
475 fputs(outbuf, outfp);
478 if (len > 0 && feof(tmp_fp)) {
479 base64_encode(outbuf, inbuf, len);
480 fputs(outbuf, outfp);
489 } else if (encoding == ENC_QUOTED_PRINTABLE) {
490 gchar inbuf[BUFFSIZE], outbuf[BUFFSIZE * 4];
492 while (fgets(inbuf, sizeof(inbuf), infp) != NULL) {
493 qp_encode_line(outbuf, inbuf);
495 if (!strncmp("From ", outbuf, sizeof("From ")-1)) {
496 gchar *tmpbuf = outbuf;
498 tmpbuf += sizeof("From ")-1;
500 fputs("=46rom ", outfp);
501 fputs(tmpbuf, outfp);
503 fputs(outbuf, outfp);
508 while (fgets(buf, sizeof(buf), infp) != NULL) {
517 if (mimeinfo->content == MIMECONTENT_FILE) {
518 if (mimeinfo->tmp && (mimeinfo->data.filename != NULL))
519 g_unlink(mimeinfo->data.filename);
520 g_free(mimeinfo->data.filename);
521 } else if (mimeinfo->content == MIMECONTENT_MEM) {
522 if (mimeinfo->tmp && (mimeinfo->data.mem != NULL))
523 g_free(mimeinfo->data.mem);
526 stat(tmpfilename, &statbuf);
527 mimeinfo->content = MIMECONTENT_FILE;
528 mimeinfo->data.filename = tmpfilename;
529 mimeinfo->tmp = TRUE;
530 mimeinfo->offset = 0;
531 mimeinfo->length = statbuf.st_size;
532 mimeinfo->encoding_type = encoding;
537 gint procmime_get_part(const gchar *outfile, MimeInfo *mimeinfo)
541 gint restlength, readlength;
543 g_return_val_if_fail(outfile != NULL, -1);
544 g_return_val_if_fail(mimeinfo != NULL, -1);
546 if (mimeinfo->encoding_type != ENC_BINARY && !procmime_decode_content(mimeinfo))
549 if ((infp = g_fopen(mimeinfo->data.filename, "rb")) == NULL) {
550 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
553 if (fseek(infp, mimeinfo->offset, SEEK_SET) < 0) {
554 FILE_OP_ERROR(mimeinfo->data.filename, "fseek");
558 if ((outfp = g_fopen(outfile, "wb")) == NULL) {
559 FILE_OP_ERROR(outfile, "fopen");
564 restlength = mimeinfo->length;
566 while ((restlength > 0) && ((readlength = fread(buf, 1, restlength > BUFFSIZE ? BUFFSIZE : restlength, infp)) > 0)) {
567 fwrite(buf, 1, readlength, outfp);
568 restlength -= readlength;
572 if (fclose(outfp) == EOF) {
573 FILE_OP_ERROR(outfile, "fclose");
581 struct ContentRenderer {
586 static GList * renderer_list = NULL;
588 static struct ContentRenderer *
589 content_renderer_new(char * content_type, char * renderer)
591 struct ContentRenderer * cr;
593 cr = g_new(struct ContentRenderer, 1);
597 cr->content_type = g_strdup(content_type);
598 cr->renderer = g_strdup(renderer);
603 static void content_renderer_free(struct ContentRenderer * cr)
605 g_free(cr->content_type);
606 g_free(cr->renderer);
610 void renderer_read_config(void)
616 g_list_foreach(renderer_list, (GFunc) content_renderer_free, NULL);
617 renderer_list = NULL;
619 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, RENDERER_RC, NULL);
620 f = g_fopen(rcpath, "rb");
626 while (fgets(buf, BUFFSIZE, f)) {
628 struct ContentRenderer * cr;
631 p = strchr(buf, ' ');
636 cr = content_renderer_new(buf, p + 1);
640 renderer_list = g_list_append(renderer_list, cr);
646 void renderer_write_config(void)
652 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, RENDERER_RC, NULL);
654 if ((pfile = prefs_write_open(rcpath)) == NULL) {
655 g_warning("failed to write configuration to file\n");
662 for (cur = renderer_list ; cur != NULL ; cur = cur->next) {
663 struct ContentRenderer * renderer;
664 renderer = cur->data;
665 fprintf(pfile->fp, "%s %s\n", renderer->content_type,
669 if (prefs_file_close(pfile) < 0) {
670 g_warning("failed to write configuration to file\n");
675 FILE *procmime_get_text_content(MimeInfo *mimeinfo)
678 const gchar *src_codeset;
679 gboolean conv_fail = FALSE;
682 struct ContentRenderer * renderer;
684 gchar *tmpfile, *content_type;
686 g_return_val_if_fail(mimeinfo != NULL, NULL);
688 if (!procmime_decode_content(mimeinfo))
691 tmpfile = procmime_get_tmp_file_name(mimeinfo);
695 if (procmime_get_part(tmpfile, mimeinfo) < 0) {
700 tmpfp = g_fopen(tmpfile, "rb");
706 if ((outfp = my_tmpfile()) == NULL) {
713 src_codeset = forced_charset
715 procmime_mimeinfo_get_parameter(mimeinfo, "charset");
719 content_type = procmime_get_content_type_str(mimeinfo->type,
721 for (cur = renderer_list ; cur != NULL ; cur = cur->next) {
722 struct ContentRenderer * cr;
725 if (g_ascii_strcasecmp(cr->content_type, content_type) == 0) {
730 g_free(content_type);
732 if (renderer != NULL) {
738 dup2(fileno(outfp), 1);
740 p = popen(renderer->renderer, "w");
745 fread(buf, sizeof(char), sizeof(buf),
747 fwrite(buf, sizeof(char), count, p);
752 /* CodeConverter seems to have no effect here */
753 } else if (mimeinfo->type == MIMETYPE_TEXT && !g_ascii_strcasecmp(mimeinfo->subtype, "html")) {
757 conv = conv_code_converter_new(src_codeset);
758 parser = html_parser_new(tmpfp, conv);
759 while ((str = html_parse(parser)) != NULL) {
762 html_parser_destroy(parser);
763 conv_code_converter_destroy(conv);
764 } else if (mimeinfo->type == MIMETYPE_TEXT && !g_ascii_strcasecmp(mimeinfo->subtype, "enriched")) {
768 conv = conv_code_converter_new(src_codeset);
769 parser = ertf_parser_new(tmpfp, conv);
770 while ((str = ertf_parse(parser)) != NULL) {
773 ertf_parser_destroy(parser);
774 conv_code_converter_destroy(conv);
775 } else if (mimeinfo->type == MIMETYPE_TEXT) {
776 while (fgets(buf, sizeof(buf), tmpfp) != NULL) {
777 str = conv_codeset_strdup(buf, src_codeset, CS_UTF_8);
789 g_warning("procmime_get_text_content(): Code conversion failed.\n");
799 /* search the first text part of (multipart) MIME message,
800 decode, convert it and output to outfp. */
801 FILE *procmime_get_first_text_content(MsgInfo *msginfo)
804 MimeInfo *mimeinfo, *partinfo;
806 g_return_val_if_fail(msginfo != NULL, NULL);
808 mimeinfo = procmime_scan_message(msginfo);
809 if (!mimeinfo) return NULL;
812 while (partinfo && partinfo->type != MIMETYPE_TEXT) {
813 partinfo = procmime_mimeinfo_next(partinfo);
816 outfp = procmime_get_text_content(partinfo);
818 procmime_mimeinfo_free_all(mimeinfo);
824 static gboolean find_encrypted_func(GNode *node, gpointer data)
826 MimeInfo *mimeinfo = (MimeInfo *) node->data;
827 MimeInfo **encinfo = (MimeInfo **) data;
829 if (privacy_mimeinfo_is_encrypted(mimeinfo)) {
837 static MimeInfo *find_encrypted_part(MimeInfo *rootinfo)
839 MimeInfo *encinfo = NULL;
841 g_node_traverse(rootinfo->node, G_IN_ORDER, G_TRAVERSE_ALL, -1,
842 find_encrypted_func, &encinfo);
847 /* search the first encrypted text part of (multipart) MIME message,
848 decode, convert it and output to outfp. */
849 FILE *procmime_get_first_encrypted_text_content(MsgInfo *msginfo)
852 MimeInfo *mimeinfo, *partinfo, *encinfo;
854 g_return_val_if_fail(msginfo != NULL, NULL);
856 mimeinfo = procmime_scan_message(msginfo);
862 if ((encinfo = find_encrypted_part(partinfo)) != NULL) {
863 debug_print("decrypting message part\n");
864 if (privacy_mimeinfo_decrypt(encinfo) < 0)
868 while (partinfo && partinfo->type != MIMETYPE_TEXT) {
869 partinfo = procmime_mimeinfo_next(partinfo);
873 outfp = procmime_get_text_content(partinfo);
875 procmime_mimeinfo_free_all(mimeinfo);
880 gboolean procmime_msginfo_is_encrypted(MsgInfo *msginfo)
882 MimeInfo *mimeinfo, *partinfo;
883 gboolean result = FALSE;
885 g_return_val_if_fail(msginfo != NULL, FALSE);
887 mimeinfo = procmime_scan_message(msginfo);
893 result = (find_encrypted_part(partinfo) != NULL);
894 procmime_mimeinfo_free_all(mimeinfo);
899 gboolean procmime_find_string_part(MimeInfo *mimeinfo, const gchar *filename,
900 const gchar *str, StrFindFunc find_func)
905 g_return_val_if_fail(mimeinfo != NULL, FALSE);
906 g_return_val_if_fail(mimeinfo->type == MIMETYPE_TEXT, FALSE);
907 g_return_val_if_fail(str != NULL, FALSE);
908 g_return_val_if_fail(find_func != NULL, FALSE);
910 outfp = procmime_get_text_content(mimeinfo);
915 while (fgets(buf, sizeof(buf), outfp) != NULL) {
917 if (find_func(buf, str)) {
928 gboolean procmime_find_string(MsgInfo *msginfo, const gchar *str,
929 StrFindFunc find_func)
934 gboolean found = FALSE;
936 g_return_val_if_fail(msginfo != NULL, FALSE);
937 g_return_val_if_fail(str != NULL, FALSE);
938 g_return_val_if_fail(find_func != NULL, FALSE);
940 filename = procmsg_get_message_file(msginfo);
941 if (!filename) return FALSE;
942 mimeinfo = procmime_scan_message(msginfo);
944 for (partinfo = mimeinfo; partinfo != NULL;
945 partinfo = procmime_mimeinfo_next(partinfo)) {
946 if (partinfo->type == MIMETYPE_TEXT) {
947 if (procmime_find_string_part
948 (partinfo, filename, str, find_func) == TRUE) {
955 procmime_mimeinfo_free_all(mimeinfo);
961 gchar *procmime_get_tmp_file_name(MimeInfo *mimeinfo)
963 static guint32 id = 0;
968 g_return_val_if_fail(mimeinfo != NULL, NULL);
970 g_snprintf(f_prefix, sizeof(f_prefix), "%08x.", id++);
972 if ((mimeinfo->type == MIMETYPE_TEXT) && !g_ascii_strcasecmp(mimeinfo->subtype, "html"))
973 base = g_strdup("mimetmp.html");
975 const gchar *basetmp;
977 basetmp = procmime_mimeinfo_get_parameter(mimeinfo, "filename");
979 basetmp = procmime_mimeinfo_get_parameter(mimeinfo, "name");
982 basetmp = g_path_get_basename(basetmp);
983 if (*basetmp == '\0')
984 basetmp = g_strdup("mimetmp");
985 base = conv_filename_from_utf8(basetmp);
986 g_free((gchar*)basetmp);
987 subst_for_shellsafe_filename(base);
990 filename = g_strconcat(get_mime_tmp_dir(), G_DIR_SEPARATOR_S,
991 f_prefix, base, NULL);
998 static GList *mime_type_list = NULL;
1000 gchar *procmime_get_mime_type(const gchar *filename)
1002 static GHashTable *mime_type_table = NULL;
1003 MimeType *mime_type;
1008 if (!mime_type_table) {
1009 mime_type_table = procmime_get_mime_type_table();
1010 if (!mime_type_table) return NULL;
1013 base = g_path_get_basename(filename);
1014 if ((p = strrchr(base, '.')) != NULL)
1015 Xstrdup_a(ext, p + 1, p = NULL );
1017 if (!p) return NULL;
1020 mime_type = g_hash_table_lookup(mime_type_table, ext);
1024 str = g_strconcat(mime_type->type, "/", mime_type->sub_type,
1032 static guint procmime_str_hash(gconstpointer gptr)
1034 guint hash_result = 0;
1037 for (str = gptr; str && *str; str++) {
1038 if (isupper(*str)) hash_result += (*str + ' ');
1039 else hash_result += *str;
1045 static gint procmime_str_equal(gconstpointer gptr1, gconstpointer gptr2)
1047 const char *str1 = gptr1;
1048 const char *str2 = gptr2;
1050 return !g_utf8_collate(str1, str2);
1053 static GHashTable *procmime_get_mime_type_table(void)
1055 GHashTable *table = NULL;
1057 MimeType *mime_type;
1060 if (!mime_type_list) {
1061 mime_type_list = procmime_get_mime_type_list();
1062 if (!mime_type_list) return NULL;
1065 table = g_hash_table_new(procmime_str_hash, procmime_str_equal);
1067 for (cur = mime_type_list; cur != NULL; cur = cur->next) {
1071 mime_type = (MimeType *)cur->data;
1073 if (!mime_type->extension) continue;
1075 exts = g_strsplit(mime_type->extension, " ", 16);
1076 for (i = 0; exts[i] != NULL; i++) {
1077 /* make the key case insensitive */
1079 /* use previously dup'd key on overwriting */
1080 if (g_hash_table_lookup(table, exts[i]))
1083 key = g_strdup(exts[i]);
1084 g_hash_table_insert(table, key, mime_type);
1092 GList *procmime_get_mime_type_list(void)
1096 gchar buf[BUFFSIZE];
1099 MimeType *mime_type;
1100 gboolean fp_is_glob_file = TRUE;
1103 return mime_type_list;
1105 if ((fp = g_fopen("/usr/share/mime/globs", "rb")) == NULL) {
1106 fp_is_glob_file = FALSE;
1107 if ((fp = g_fopen("/etc/mime.types", "rb")) == NULL) {
1108 if ((fp = g_fopen(SYSCONFDIR "/mime.types", "rb"))
1110 FILE_OP_ERROR(SYSCONFDIR "/mime.types",
1117 while (fgets(buf, sizeof(buf), fp) != NULL) {
1118 p = strchr(buf, '#');
1124 if (fp_is_glob_file) {
1125 while (*p && !g_ascii_isspace(*p) && (*p!=':')) p++;
1127 while (*p && !g_ascii_isspace(*p)) p++;
1134 delim = strchr(buf, '/');
1135 if (delim == NULL) continue;
1138 mime_type = g_new(MimeType, 1);
1139 mime_type->type = g_strdup(buf);
1140 mime_type->sub_type = g_strdup(delim + 1);
1142 if (fp_is_glob_file) {
1143 while (*p && (g_ascii_isspace(*p)||(*p=='*')||(*p=='.'))) p++;
1145 while (*p && g_ascii_isspace(*p)) p++;
1149 mime_type->extension = g_strdup(p);
1151 mime_type->extension = NULL;
1153 list = g_list_append(list, mime_type);
1159 g_warning("Can't read mime.types\n");
1164 EncodingType procmime_get_encoding_for_charset(const gchar *charset)
1168 else if (!g_ascii_strncasecmp(charset, "ISO-2022-", 9) ||
1169 !g_ascii_strcasecmp(charset, "US-ASCII"))
1171 else if (!g_ascii_strcasecmp(charset, "ISO-8859-5") ||
1172 !g_ascii_strncasecmp(charset, "KOI8-", 5) ||
1173 !g_ascii_strcasecmp(charset, "Windows-1251"))
1175 else if (!g_ascii_strncasecmp(charset, "ISO-8859-", 9))
1176 return ENC_QUOTED_PRINTABLE;
1181 EncodingType procmime_get_encoding_for_text_file(const gchar *file)
1184 guchar buf[BUFFSIZE];
1186 size_t octet_chars = 0;
1187 size_t total_len = 0;
1188 gfloat octet_percentage;
1190 if ((fp = g_fopen(file, "rb")) == NULL) {
1191 FILE_OP_ERROR(file, "fopen");
1195 while ((len = fread(buf, sizeof(guchar), sizeof(buf), fp)) > 0) {
1199 for (p = buf, i = 0; i < len; ++p, ++i) {
1209 octet_percentage = (gfloat)octet_chars / (gfloat)total_len;
1211 octet_percentage = 0.0;
1213 debug_print("procmime_get_encoding_for_text_file(): "
1214 "8bit chars: %d / %d (%f%%)\n", octet_chars, total_len,
1215 100.0 * octet_percentage);
1217 if (octet_percentage > 0.20) {
1218 debug_print("using BASE64\n");
1220 } else if (octet_chars > 0) {
1221 debug_print("using quoted-printable\n");
1222 return ENC_QUOTED_PRINTABLE;
1224 debug_print("using 7bit\n");
1229 struct EncodingTable
1232 EncodingType enc_type;
1235 struct EncodingTable encoding_table[] = {
1238 {"binary", ENC_BINARY},
1239 {"quoted-printable", ENC_QUOTED_PRINTABLE},
1240 {"base64", ENC_BASE64},
1241 {"x-uuencode", ENC_UNKNOWN},
1242 {NULL, ENC_UNKNOWN},
1245 const gchar *procmime_get_encoding_str(EncodingType encoding)
1247 struct EncodingTable *enc_table;
1249 for (enc_table = encoding_table; enc_table->str != NULL; enc_table++) {
1250 if (enc_table->enc_type == encoding)
1251 return enc_table->str;
1256 /* --- NEW MIME STUFF --- */
1263 static struct TypeTable mime_type_table[] = {
1264 {"text", MIMETYPE_TEXT},
1265 {"image", MIMETYPE_IMAGE},
1266 {"audio", MIMETYPE_AUDIO},
1267 {"video", MIMETYPE_VIDEO},
1268 {"application", MIMETYPE_APPLICATION},
1269 {"message", MIMETYPE_MESSAGE},
1270 {"multipart", MIMETYPE_MULTIPART},
1274 const gchar *procmime_get_media_type_str(MimeMediaType type)
1276 struct TypeTable *type_table;
1278 for (type_table = mime_type_table; type_table->str != NULL; type_table++) {
1279 if (type_table->type == type)
1280 return type_table->str;
1285 MimeMediaType procmime_get_media_type(const gchar *str)
1287 struct TypeTable *typetablearray;
1289 for (typetablearray = mime_type_table; typetablearray->str != NULL; typetablearray++)
1290 if (g_ascii_strncasecmp(str, typetablearray->str, strlen(typetablearray->str)) == 0)
1291 return typetablearray->type;
1293 return MIMETYPE_UNKNOWN;
1297 *\brief Safe wrapper for content type string.
1299 *\return const gchar * Pointer to content type string.
1301 gchar *procmime_get_content_type_str(MimeMediaType type,
1302 const char *subtype)
1304 const gchar *type_str = NULL;
1306 if (subtype == NULL || !(type_str = procmime_get_media_type_str(type)))
1307 return g_strdup("unknown");
1308 return g_strdup_printf("%s/%s", type_str, subtype);
1311 int procmime_parse_mimepart(MimeInfo *parent,
1312 gchar *content_type,
1313 gchar *content_encoding,
1314 gchar *content_description,
1316 gchar *content_disposition,
1317 const gchar *filename,
1321 void procmime_parse_message_rfc822(MimeInfo *mimeinfo)
1323 HeaderEntry hentry[] = {{"Content-Type:", NULL, TRUE},
1324 {"Content-Transfer-Encoding:",
1326 {"Content-Description:",
1330 {"Content-Disposition:",
1334 {NULL, NULL, FALSE}};
1335 guint content_start, i;
1339 procmime_decode_content(mimeinfo);
1341 fp = g_fopen(mimeinfo->data.filename, "rb");
1343 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
1346 fseek(fp, mimeinfo->offset, SEEK_SET);
1347 procheader_get_header_fields(fp, hentry);
1348 if (hentry[0].body != NULL) {
1349 tmp = conv_unmime_header(hentry[0].body, NULL);
1350 g_free(hentry[0].body);
1351 hentry[0].body = tmp;
1353 if (hentry[2].body != NULL) {
1354 tmp = conv_unmime_header(hentry[2].body, NULL);
1355 g_free(hentry[2].body);
1356 hentry[2].body = tmp;
1358 if (hentry[4].body != NULL) {
1359 tmp = conv_unmime_header(hentry[4].body, NULL);
1360 g_free(hentry[4].body);
1361 hentry[4].body = tmp;
1363 content_start = ftell(fp);
1366 procmime_parse_mimepart(mimeinfo,
1367 hentry[0].body, hentry[1].body,
1368 hentry[2].body, hentry[3].body,
1370 mimeinfo->data.filename, content_start,
1371 mimeinfo->length - (content_start - mimeinfo->offset));
1373 for (i = 0; i < (sizeof hentry / sizeof hentry[0]); i++) {
1374 g_free(hentry[i].body);
1375 hentry[i].body = NULL;
1379 void procmime_parse_multipart(MimeInfo *mimeinfo)
1381 HeaderEntry hentry[] = {{"Content-Type:", NULL, TRUE},
1382 {"Content-Transfer-Encoding:",
1384 {"Content-Description:",
1388 {"Content-Disposition:",
1390 {NULL, NULL, FALSE}};
1393 gint boundary_len = 0, lastoffset = -1, i;
1394 gchar buf[BUFFSIZE];
1398 boundary = g_hash_table_lookup(mimeinfo->typeparameters, "boundary");
1401 boundary_len = strlen(boundary);
1403 procmime_decode_content(mimeinfo);
1405 fp = g_fopen(mimeinfo->data.filename, "rb");
1407 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
1410 fseek(fp, mimeinfo->offset, SEEK_SET);
1411 while ((p = fgets(buf, sizeof(buf), fp)) != NULL && result == 0) {
1412 if (ftell(fp) > (mimeinfo->offset + mimeinfo->length))
1415 if (IS_BOUNDARY(buf, boundary, boundary_len)) {
1416 if (lastoffset != -1) {
1417 result = procmime_parse_mimepart(mimeinfo,
1418 hentry[0].body, hentry[1].body,
1419 hentry[2].body, hentry[3].body,
1421 mimeinfo->data.filename, lastoffset,
1422 (ftell(fp) - strlen(buf)) - lastoffset - 1);
1425 if (buf[2 + boundary_len] == '-' &&
1426 buf[2 + boundary_len + 1] == '-')
1429 for (i = 0; i < (sizeof hentry / sizeof hentry[0]) ; i++) {
1430 g_free(hentry[i].body);
1431 hentry[i].body = NULL;
1433 procheader_get_header_fields(fp, hentry);
1434 if (hentry[0].body != NULL) {
1435 tmp = conv_unmime_header(hentry[0].body, NULL);
1436 g_free(hentry[0].body);
1437 hentry[0].body = tmp;
1439 if (hentry[2].body != NULL) {
1440 tmp = conv_unmime_header(hentry[2].body, NULL);
1441 g_free(hentry[2].body);
1442 hentry[2].body = tmp;
1444 if (hentry[4].body != NULL) {
1445 tmp = conv_unmime_header(hentry[4].body, NULL);
1446 g_free(hentry[4].body);
1447 hentry[4].body = tmp;
1449 lastoffset = ftell(fp);
1452 for (i = 0; i < (sizeof hentry / sizeof hentry[0]); i++) {
1453 g_free(hentry[i].body);
1454 hentry[i].body = NULL;
1459 static void parse_parameters(const gchar *parameters, GHashTable *table)
1461 gchar *params, *param, *next;
1462 GSList *convlist = NULL, *concatlist = NULL, *cur;
1464 params = g_strdup(parameters);
1467 for (; next != NULL; param = next) {
1468 gchar *attribute, *value, *tmp;
1470 gboolean convert = FALSE;
1472 next = strchr_with_skip_quote(param, '"', ';');
1481 value = strchr(attribute, '=');
1487 while (value[0] == ' ')
1490 g_strdown(attribute);
1492 len = strlen(attribute);
1493 if (attribute[len - 1] == '*') {
1494 gchar *srcpos, *dstpos, *endpos;
1497 attribute[len - 1] = '\0';
1501 endpos = value + strlen(value);
1502 while (srcpos < endpos) {
1508 if (!get_hex_value(&dstvalue, srcpos[1], srcpos[2]))
1519 if (value[0] == '"')
1520 extract_quote(value, '"');
1521 else if ((tmp = strchr(value, ' ')) != NULL)
1526 while (attribute[0] == ' ')
1528 while (attribute[strlen(attribute)-1] == ' ')
1529 attribute[strlen(attribute)-1] = '\0';
1532 while (value[0] == ' ')
1534 while (value[strlen(value)-1] == ' ')
1535 value[strlen(value)-1] = '\0';
1537 if (strrchr(attribute, '*') != NULL) {
1540 tmpattr = g_strdup(attribute);
1541 tmp = strrchr(tmpattr, '*');
1544 if ((tmp[1] == '0') && (tmp[2] == '\0') &&
1545 (g_slist_find_custom(concatlist, attribute, g_str_equal) == NULL))
1546 concatlist = g_slist_prepend(concatlist, g_strdup(tmpattr));
1548 if (convert && (g_slist_find_custom(convlist, attribute, g_str_equal) == NULL))
1549 convlist = g_slist_prepend(convlist, g_strdup(tmpattr));
1552 } else if (convert) {
1553 if (g_slist_find_custom(convlist, attribute, g_str_equal) == NULL)
1554 convlist = g_slist_prepend(convlist, g_strdup(attribute));
1557 if (g_hash_table_lookup(table, attribute) == NULL)
1558 g_hash_table_insert(table, g_strdup(attribute), g_strdup(value));
1561 for (cur = concatlist; cur != NULL; cur = g_slist_next(cur)) {
1562 gchar *attribute, *attrwnum, *partvalue;
1566 attribute = (gchar *) cur->data;
1567 value = g_string_sized_new(64);
1569 attrwnum = g_strdup_printf("%s*%d", attribute, n);
1570 while ((partvalue = g_hash_table_lookup(table, attrwnum)) != NULL) {
1571 g_string_append(value, partvalue);
1575 attrwnum = g_strdup_printf("%s*%d", attribute, n);
1579 g_hash_table_insert(table, g_strdup(attribute), g_strdup(value->str));
1580 g_string_free(value, TRUE);
1582 slist_free_strings(concatlist);
1583 g_slist_free(concatlist);
1585 for (cur = convlist; cur != NULL; cur = g_slist_next(cur)) {
1586 gchar *attribute, *key, *value;
1587 gchar *charset, *lang, *oldvalue, *newvalue;
1589 attribute = (gchar *) cur->data;
1590 if (!g_hash_table_lookup_extended(
1591 table, attribute, (gpointer *)(gchar *) &key, (gpointer *)(gchar *) &value))
1595 lang = strchr(charset, '\'');
1600 oldvalue = strchr(lang, '\'');
1601 if (oldvalue == NULL)
1606 newvalue = conv_codeset_strdup(oldvalue, charset, CS_UTF_8);
1608 g_hash_table_remove(table, attribute);
1612 g_hash_table_insert(table, g_strdup(attribute), newvalue);
1614 slist_free_strings(convlist);
1615 g_slist_free(convlist);
1620 static void procmime_parse_content_type(const gchar *content_type, MimeInfo *mimeinfo)
1622 g_return_if_fail(content_type != NULL);
1623 g_return_if_fail(mimeinfo != NULL);
1625 /* RFC 2045, page 13 says that the mime subtype is MANDATORY;
1626 * if it's not available we use the default Content-Type */
1627 if ((content_type[0] == '\0') || (strchr(content_type, '/') == NULL)) {
1628 mimeinfo->type = MIMETYPE_TEXT;
1629 mimeinfo->subtype = g_strdup("plain");
1630 if (g_hash_table_lookup(mimeinfo->typeparameters,
1631 "charset") == NULL) {
1632 g_hash_table_insert(mimeinfo->typeparameters,
1633 g_strdup("charset"),
1635 conv_get_locale_charset_str_no_utf8()));
1638 gchar *type, *subtype, *params;
1640 type = g_strdup(content_type);
1641 subtype = strchr(type, '/') + 1;
1642 *(subtype - 1) = '\0';
1643 if ((params = strchr(subtype, ';')) != NULL) {
1648 mimeinfo->type = procmime_get_media_type(type);
1649 mimeinfo->subtype = g_strdup(subtype);
1651 /* Get mimeinfo->typeparameters */
1653 parse_parameters(params, mimeinfo->typeparameters);
1659 static void procmime_parse_content_disposition(const gchar *content_disposition, MimeInfo *mimeinfo)
1661 gchar *tmp, *params;
1663 g_return_if_fail(content_disposition != NULL);
1664 g_return_if_fail(mimeinfo != NULL);
1666 tmp = g_strdup(content_disposition);
1667 if ((params = strchr(tmp, ';')) != NULL) {
1673 if (!g_ascii_strcasecmp(tmp, "inline"))
1674 mimeinfo->disposition = DISPOSITIONTYPE_INLINE;
1675 else if (!g_ascii_strcasecmp(tmp, "attachment"))
1676 mimeinfo->disposition = DISPOSITIONTYPE_ATTACHMENT;
1678 mimeinfo->disposition = DISPOSITIONTYPE_ATTACHMENT;
1681 parse_parameters(params, mimeinfo->dispositionparameters);
1687 static void procmime_parse_content_encoding(const gchar *content_encoding, MimeInfo *mimeinfo)
1689 struct EncodingTable *enc_table;
1691 for (enc_table = encoding_table; enc_table->str != NULL; enc_table++) {
1692 if (g_ascii_strcasecmp(enc_table->str, content_encoding) == 0) {
1693 mimeinfo->encoding_type = enc_table->enc_type;
1697 mimeinfo->encoding_type = ENC_UNKNOWN;
1701 int procmime_parse_mimepart(MimeInfo *parent,
1702 gchar *content_type,
1703 gchar *content_encoding,
1704 gchar *content_description,
1706 gchar *content_disposition,
1707 const gchar *filename,
1713 /* Create MimeInfo */
1714 mimeinfo = procmime_mimeinfo_new();
1715 mimeinfo->content = MIMECONTENT_FILE;
1716 if (parent != NULL) {
1717 if (g_node_depth(parent->node) > 32) {
1718 /* 32 is an arbitrary value
1719 * this avoids DOSsing ourselves
1720 * with enormous messages
1722 procmime_mimeinfo_free_all(mimeinfo);
1725 g_node_append(parent->node, mimeinfo->node);
1727 mimeinfo->data.filename = g_strdup(filename);
1728 mimeinfo->offset = offset;
1729 mimeinfo->length = length;
1731 if (content_type != NULL) {
1732 procmime_parse_content_type(content_type, mimeinfo);
1734 mimeinfo->type = MIMETYPE_TEXT;
1735 mimeinfo->subtype = g_strdup("plain");
1736 if (g_hash_table_lookup(mimeinfo->typeparameters,
1737 "charset") == NULL) {
1738 g_hash_table_insert(mimeinfo->typeparameters,
1739 g_strdup("charset"),
1741 conv_get_locale_charset_str_no_utf8()));
1745 if (content_encoding != NULL) {
1746 procmime_parse_content_encoding(content_encoding, mimeinfo);
1748 mimeinfo->encoding_type = ENC_UNKNOWN;
1751 if (content_description != NULL)
1752 mimeinfo->description = g_strdup(content_description);
1754 mimeinfo->description = NULL;
1756 if (content_id != NULL)
1757 mimeinfo->id = g_strdup(content_id);
1759 mimeinfo->id = NULL;
1761 if (content_disposition != NULL)
1762 procmime_parse_content_disposition(content_disposition, mimeinfo);
1764 mimeinfo->disposition = DISPOSITIONTYPE_UNKNOWN;
1766 /* Call parser for mime type */
1767 switch (mimeinfo->type) {
1768 case MIMETYPE_MESSAGE:
1769 if (g_ascii_strcasecmp(mimeinfo->subtype, "rfc822") == 0) {
1770 procmime_parse_message_rfc822(mimeinfo);
1774 case MIMETYPE_MULTIPART:
1775 procmime_parse_multipart(mimeinfo);
1785 static gchar *typenames[] = {
1796 static gboolean output_func(GNode *node, gpointer data)
1799 MimeInfo *mimeinfo = (MimeInfo *) node->data;
1801 depth = g_node_depth(node);
1802 for (i = 0; i < depth; i++)
1804 printf("%s/%s (offset:%d length:%d encoding: %d)\n", typenames[mimeinfo->type], mimeinfo->subtype, mimeinfo->offset, mimeinfo->length, mimeinfo->encoding_type);
1809 static void output_mime_structure(MimeInfo *mimeinfo, int indent)
1811 g_node_traverse(mimeinfo->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1, output_func, NULL);
1814 MimeInfo *procmime_scan_file_with_offset(const gchar *filename, int offset)
1819 stat(filename, &buf);
1821 mimeinfo = procmime_mimeinfo_new();
1822 mimeinfo->content = MIMECONTENT_FILE;
1823 mimeinfo->encoding_type = ENC_UNKNOWN;
1824 mimeinfo->type = MIMETYPE_MESSAGE;
1825 mimeinfo->subtype = g_strdup("rfc822");
1826 mimeinfo->data.filename = g_strdup(filename);
1827 mimeinfo->offset = offset;
1828 mimeinfo->length = buf.st_size - offset;
1830 procmime_parse_message_rfc822(mimeinfo);
1831 if (debug_get_mode())
1832 output_mime_structure(mimeinfo, 0);
1837 MimeInfo *procmime_scan_file(const gchar *filename)
1841 g_return_val_if_fail(filename != NULL, NULL);
1843 mimeinfo = procmime_scan_file_with_offset(filename, 0);
1848 MimeInfo *procmime_scan_queue_file(const gchar *filename)
1852 gchar buf[BUFFSIZE];
1855 g_return_val_if_fail(filename != NULL, NULL);
1858 if ((fp = g_fopen(filename, "rb")) == NULL)
1860 /* Skip queue header */
1861 while (fgets(buf, sizeof(buf), fp) != NULL)
1862 if (buf[0] == '\r' || buf[0] == '\n') break;
1866 mimeinfo = procmime_scan_file_with_offset(filename, offset);
1873 ENC_AS_QUOTED_STRING,
1878 typedef struct _ParametersData {
1884 static void write_parameters(gpointer key, gpointer value, gpointer user_data)
1887 gchar *val = value, *valpos, *tmp;
1888 ParametersData *pdata = (ParametersData *)user_data;
1889 GString *buf = g_string_new("");
1891 EncodeAs encas = ENC_AS_TOKEN;
1893 for (valpos = val; *valpos != 0; valpos++) {
1894 if (!IS_ASCII(*valpos) || *valpos == '"') {
1895 encas = ENC_AS_EXTENDED;
1900 if (((*valpos >= 0) && (*valpos < 037)) || (*valpos == 0177)) {
1901 encas = ENC_AS_QUOTED_STRING;
1905 /* tspecials + SPACE */
1923 encas = ENC_AS_QUOTED_STRING;
1928 if (encas == ENC_AS_EXTENDED && pdata->ascii_only == TRUE)
1929 encas = ENC_TO_ASCII;
1933 g_string_append_printf(buf, "%s=%s", param, val);
1937 tmp = g_strdup(val);
1939 " ()<>@,';:\\/[]?=.0123456789"
1940 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1941 "abcdefghijklmnopqrstuvwxyz",
1943 g_string_append_printf(buf, "%s=\"%s\"", param, tmp);
1947 case ENC_AS_QUOTED_STRING:
1948 g_string_append_printf(buf, "%s=\"%s\"", param, val);
1951 case ENC_AS_EXTENDED:
1952 if (!g_utf8_validate(val, -1, NULL))
1953 g_string_append_printf(buf, "%s*=%s''", param,
1954 conv_get_locale_charset_str());
1956 g_string_append_printf(buf, "%s*=%s''", param,
1958 for (valpos = val; *valpos != '\0'; valpos++) {
1959 if (IS_ASCII(*valpos) && isalnum(*valpos)) {
1960 g_string_append_printf(buf, "%c", *valpos);
1962 gchar hexstr[3] = "XX";
1963 get_hex_str(hexstr, *valpos);
1964 g_string_append_printf(buf, "%%%s", hexstr);
1970 if (buf->str && strlen(buf->str)) {
1971 if (pdata->len + strlen(buf->str) + 2 > 76) {
1972 fprintf(pdata->fp, ";\n %s", buf->str);
1973 pdata->len = strlen(buf->str) + 1;
1975 fprintf(pdata->fp, "; %s", buf->str);
1976 pdata->len += strlen(buf->str) + 2;
1979 g_string_free(buf, TRUE);
1982 void procmime_write_mime_header(MimeInfo *mimeinfo, FILE *fp)
1984 struct TypeTable *type_table;
1985 ParametersData *pdata = g_new0(ParametersData, 1);
1986 debug_print("procmime_write_mime_header\n");
1989 pdata->ascii_only = FALSE;
1991 for (type_table = mime_type_table; type_table->str != NULL; type_table++)
1992 if (mimeinfo->type == type_table->type) {
1993 gchar *buf = g_strdup_printf(
1994 "Content-Type: %s/%s", type_table->str, mimeinfo->subtype);
1995 fprintf(fp, "%s", buf);
1996 pdata->len = strlen(buf);
1997 pdata->ascii_only = TRUE;
2001 g_hash_table_foreach(mimeinfo->typeparameters, write_parameters, pdata);
2006 if (mimeinfo->encoding_type != ENC_UNKNOWN)
2007 fprintf(fp, "Content-Transfer-Encoding: %s\n", procmime_get_encoding_str(mimeinfo->encoding_type));
2009 if (mimeinfo->description != NULL)
2010 fprintf(fp, "Content-Description: %s\n", mimeinfo->description);
2012 if (mimeinfo->id != NULL)
2013 fprintf(fp, "Content-ID: %s\n", mimeinfo->id);
2015 if (mimeinfo->disposition != DISPOSITIONTYPE_UNKNOWN) {
2016 ParametersData *pdata = g_new0(ParametersData, 1);
2018 if (mimeinfo->disposition == DISPOSITIONTYPE_INLINE)
2019 buf = g_strdup("Content-Disposition: inline");
2020 else if (mimeinfo->disposition == DISPOSITIONTYPE_ATTACHMENT)
2021 buf = g_strdup("Content-Disposition: attachment");
2023 buf = g_strdup("Content-Disposition: unknown");
2025 fprintf(fp, "%s", buf);
2026 pdata->len = strlen(buf);
2030 pdata->ascii_only = FALSE;
2032 g_hash_table_foreach(mimeinfo->dispositionparameters, write_parameters, pdata);
2040 gint procmime_write_message_rfc822(MimeInfo *mimeinfo, FILE *fp)
2045 gchar buf[BUFFSIZE];
2046 gboolean skip = FALSE;;
2048 debug_print("procmime_write_message_rfc822\n");
2051 switch (mimeinfo->content) {
2052 case MIMECONTENT_FILE:
2053 if ((infp = g_fopen(mimeinfo->data.filename, "rb")) == NULL) {
2054 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
2057 fseek(infp, mimeinfo->offset, SEEK_SET);
2058 while (fgets(buf, sizeof(buf), infp) == buf) {
2059 if (buf[0] == '\n' && buf[1] == '\0')
2061 if (skip && (buf[0] == ' ' || buf[0] == '\t'))
2063 if (g_ascii_strncasecmp(buf, "Mime-Version:", 13) == 0 ||
2064 g_ascii_strncasecmp(buf, "Content-Type:", 13) == 0 ||
2065 g_ascii_strncasecmp(buf, "Content-Transfer-Encoding:", 26) == 0 ||
2066 g_ascii_strncasecmp(buf, "Content-Description:", 20) == 0 ||
2067 g_ascii_strncasecmp(buf, "Content-ID:", 11) == 0 ||
2068 g_ascii_strncasecmp(buf, "Content-Disposition:", 20) == 0) {
2072 fwrite(buf, sizeof(gchar), strlen(buf), fp);
2078 case MIMECONTENT_MEM:
2079 fwrite(mimeinfo->data.mem,
2081 strlen(mimeinfo->data.mem),
2089 childnode = mimeinfo->node->children;
2090 if (childnode == NULL)
2093 child = (MimeInfo *) childnode->data;
2094 fprintf(fp, "Mime-Version: 1.0\n");
2095 procmime_write_mime_header(child, fp);
2096 return procmime_write_mimeinfo(child, fp);
2099 gint procmime_write_multipart(MimeInfo *mimeinfo, FILE *fp)
2103 gchar *boundary, *str, *str2;
2104 gchar buf[BUFFSIZE];
2105 gboolean firstboundary;
2107 debug_print("procmime_write_multipart\n");
2109 boundary = g_hash_table_lookup(mimeinfo->typeparameters, "boundary");
2111 switch (mimeinfo->content) {
2112 case MIMECONTENT_FILE:
2113 if ((infp = g_fopen(mimeinfo->data.filename, "rb")) == NULL) {
2114 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
2117 fseek(infp, mimeinfo->offset, SEEK_SET);
2118 while (fgets(buf, sizeof(buf), infp) == buf) {
2119 if (IS_BOUNDARY(buf, boundary, strlen(boundary)))
2121 fwrite(buf, sizeof(gchar), strlen(buf), fp);
2126 case MIMECONTENT_MEM:
2127 str = g_strdup(mimeinfo->data.mem);
2128 if (((str2 = strstr(str, boundary)) != NULL) && ((str2 - str) >= 2) &&
2129 (*(str2 - 1) == '-') && (*(str2 - 2) == '-'))
2131 fwrite(str, sizeof(gchar), strlen(str), fp);
2139 childnode = mimeinfo->node->children;
2140 firstboundary = TRUE;
2141 while (childnode != NULL) {
2142 MimeInfo *child = childnode->data;
2145 firstboundary = FALSE;
2148 fprintf(fp, "--%s\n", boundary);
2150 procmime_write_mime_header(child, fp);
2151 if (procmime_write_mimeinfo(child, fp) < 0)
2154 childnode = g_node_next_sibling(childnode);
2156 fprintf(fp, "\n--%s--\n", boundary);
2161 gint procmime_write_mimeinfo(MimeInfo *mimeinfo, FILE *fp)
2165 debug_print("procmime_write_mimeinfo\n");
2167 if (G_NODE_IS_LEAF(mimeinfo->node)) {
2168 switch (mimeinfo->content) {
2169 case MIMECONTENT_FILE:
2170 if ((infp = g_fopen(mimeinfo->data.filename, "rb")) == NULL) {
2171 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
2174 copy_file_part_to_fp(infp, mimeinfo->offset, mimeinfo->length, fp);
2178 case MIMECONTENT_MEM:
2179 fwrite(mimeinfo->data.mem,
2181 strlen(mimeinfo->data.mem),
2189 /* Call writer for mime type */
2190 switch (mimeinfo->type) {
2191 case MIMETYPE_MESSAGE:
2192 if (g_ascii_strcasecmp(mimeinfo->subtype, "rfc822") == 0) {
2193 return procmime_write_message_rfc822(mimeinfo, fp);
2197 case MIMETYPE_MULTIPART:
2198 return procmime_write_multipart(mimeinfo, fp);
2210 gchar *procmime_get_part_file_name(MimeInfo *mimeinfo)
2214 if ((mimeinfo->type == MIMETYPE_TEXT) && !g_ascii_strcasecmp(mimeinfo->subtype, "html"))
2215 base = g_strdup("mimetmp.html");
2217 const gchar *basetmp;
2220 basetmp = procmime_mimeinfo_get_parameter(mimeinfo, "filename");
2221 if (basetmp == NULL)
2222 basetmp = procmime_mimeinfo_get_parameter(mimeinfo, "name");
2223 if (basetmp == NULL)
2224 basetmp = "mimetmp";
2225 basename = g_path_get_basename(basetmp);
2226 if (*basename == '\0') {
2228 basename = g_strdup("mimetmp");
2230 base = conv_filename_from_utf8(basename);
2232 subst_for_shellsafe_filename(base);