2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2006 Hiroyuki Yamamoto & The Claws Mail Team
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
28 #include <glib/gi18n.h>
35 #include <sys/types.h>
40 #include "procheader.h"
42 #include "quoted-printable.h"
49 #include "prefs_common.h"
50 #include "prefs_gtk.h"
51 #include "alertpanel.h"
53 static GHashTable *procmime_get_mime_type_table (void);
55 MimeInfo *procmime_mimeinfo_new(void)
59 mimeinfo = g_new0(MimeInfo, 1);
60 mimeinfo->content = MIMECONTENT_EMPTY;
61 mimeinfo->data.filename = NULL;
63 mimeinfo->type = MIMETYPE_UNKNOWN;
64 mimeinfo->encoding_type = ENC_UNKNOWN;
65 mimeinfo->typeparameters = g_hash_table_new(g_str_hash, g_str_equal);
67 mimeinfo->disposition = DISPOSITIONTYPE_UNKNOWN;
68 mimeinfo->dispositionparameters
69 = g_hash_table_new(g_str_hash, g_str_equal);
71 mimeinfo->node = g_node_new(mimeinfo);
76 static gboolean procmime_mimeinfo_parameters_destroy(gpointer key, gpointer value, gpointer user_data)
84 static gchar *forced_charset = NULL;
86 void procmime_force_charset(const gchar *str)
88 g_free(forced_charset);
89 forced_charset = NULL;
91 forced_charset = g_strdup(str);
94 static EncodingType forced_encoding = 0;
96 void procmime_force_encoding(EncodingType encoding)
98 forced_encoding = encoding;
101 static gboolean free_func(GNode *node, gpointer data)
103 MimeInfo *mimeinfo = (MimeInfo *) node->data;
105 switch (mimeinfo->content) {
106 case MIMECONTENT_FILE:
108 g_unlink(mimeinfo->data.filename);
109 g_free(mimeinfo->data.filename);
112 case MIMECONTENT_MEM:
114 g_free(mimeinfo->data.mem);
119 g_free(mimeinfo->subtype);
120 g_free(mimeinfo->description);
121 g_free(mimeinfo->id);
122 g_free(mimeinfo->location);
124 g_hash_table_foreach_remove(mimeinfo->typeparameters,
125 procmime_mimeinfo_parameters_destroy, NULL);
126 g_hash_table_destroy(mimeinfo->typeparameters);
127 g_hash_table_foreach_remove(mimeinfo->dispositionparameters,
128 procmime_mimeinfo_parameters_destroy, NULL);
129 g_hash_table_destroy(mimeinfo->dispositionparameters);
131 if (mimeinfo->privacy)
132 privacy_free_privacydata(mimeinfo->privacy);
139 void procmime_mimeinfo_free_all(MimeInfo *mimeinfo)
146 node = mimeinfo->node;
147 g_node_traverse(node, G_IN_ORDER, G_TRAVERSE_ALL, -1, free_func, NULL);
149 g_node_destroy(node);
152 MimeInfo *procmime_mimeinfo_parent(MimeInfo *mimeinfo)
154 g_return_val_if_fail(mimeinfo != NULL, NULL);
155 g_return_val_if_fail(mimeinfo->node != NULL, NULL);
157 if (mimeinfo->node->parent == NULL)
159 return (MimeInfo *) mimeinfo->node->parent->data;
162 MimeInfo *procmime_mimeinfo_next(MimeInfo *mimeinfo)
164 g_return_val_if_fail(mimeinfo != NULL, NULL);
165 g_return_val_if_fail(mimeinfo->node != NULL, NULL);
167 if (mimeinfo->node->children)
168 return (MimeInfo *) mimeinfo->node->children->data;
169 if (mimeinfo->node->next)
170 return (MimeInfo *) mimeinfo->node->next->data;
172 if (mimeinfo->node->parent == NULL)
175 while (mimeinfo->node->parent != NULL) {
176 mimeinfo = (MimeInfo *) mimeinfo->node->parent->data;
177 if (mimeinfo->node->next)
178 return (MimeInfo *) mimeinfo->node->next->data;
184 MimeInfo *procmime_scan_message(MsgInfo *msginfo)
189 filename = procmsg_get_message_file_path(msginfo);
190 if (!filename || !is_file_exist(filename)) {
192 filename = procmsg_get_message_file(msginfo);
194 if (!filename || !is_file_exist(filename))
197 if (!folder_has_parent_of_type(msginfo->folder, F_QUEUE) &&
198 !folder_has_parent_of_type(msginfo->folder, F_DRAFT))
199 mimeinfo = procmime_scan_file(filename);
201 mimeinfo = procmime_scan_queue_file(filename);
209 H_CONTENT_TRANSFER_ENCODING = 0,
211 H_CONTENT_DISPOSITION = 2,
212 H_CONTENT_DESCRIPTION = 3,
216 const gchar *procmime_mimeinfo_get_parameter(MimeInfo *mimeinfo, const gchar *name)
220 g_return_val_if_fail(mimeinfo != NULL, NULL);
221 g_return_val_if_fail(name != NULL, NULL);
223 value = g_hash_table_lookup(mimeinfo->dispositionparameters, name);
225 value = g_hash_table_lookup(mimeinfo->typeparameters, name);
230 #define FLUSH_LASTLINE() { \
231 if (*lastline != '\0') { \
233 strretchomp(lastline); \
234 llen = strlen(lastline); \
235 if (lastline[llen-1] == ' ' && strcmp(lastline,"-- ")) { \
236 /* this is flowed */ \
238 lastline[llen-1] = '\0'; \
239 fputs(lastline, outfp); \
241 fputs(lastline, outfp); \
242 fputs("\n", outfp); \
245 strcpy(lastline, buf); \
248 gboolean procmime_decode_content(MimeInfo *mimeinfo)
255 gboolean tmp_file = FALSE;
256 gboolean flowed = FALSE;
257 gboolean delsp = FALSE;
258 EncodingType encoding = forced_encoding
260 : mimeinfo->encoding_type;
261 gchar lastline[BUFFSIZE];
262 memset(lastline, 0, BUFFSIZE);
264 g_return_val_if_fail(mimeinfo != NULL, FALSE);
266 if (prefs_common.respect_flowed_format &&
267 mimeinfo->type == MIMETYPE_TEXT &&
268 !strcasecmp(mimeinfo->subtype, "plain")) {
269 if (procmime_mimeinfo_get_parameter(mimeinfo, "format") != NULL &&
270 !strcasecmp(procmime_mimeinfo_get_parameter(mimeinfo, "format"),"flowed"))
273 procmime_mimeinfo_get_parameter(mimeinfo, "delsp") != NULL &&
274 !strcasecmp(procmime_mimeinfo_get_parameter(mimeinfo, "delsp"),"yes"))
279 encoding == ENC_UNKNOWN ||
280 encoding == ENC_BINARY ||
281 encoding == ENC_7BIT ||
286 infp = g_fopen(mimeinfo->data.filename, "rb");
291 fseek(infp, mimeinfo->offset, SEEK_SET);
293 outfp = get_tmpfile_in_dir(get_mime_tmp_dir(), &tmpfilename);
300 readend = mimeinfo->offset + mimeinfo->length;
302 if (encoding == ENC_QUOTED_PRINTABLE) {
303 while ((ftell(infp) < readend) && (fgets(buf, sizeof(buf), infp) != NULL)) {
305 len = qp_decode_line(buf);
308 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);
335 decoder = base64_decoder_new();
336 while ((ftell(infp) < readend) && (fgets(buf, sizeof(buf), infp) != NULL)) {
337 len = base64_decoder_decode(decoder, buf, outbuf);
338 if (len < 0 && !got_error) {
339 g_warning("Bad BASE64 content.\n");
340 fwrite(_("[Error decoding BASE64]\n"),
342 strlen(_("[Error decoding BASE64]\n")),
346 } else if (len >= 0) {
347 /* print out the error message only once
349 fwrite(outbuf, sizeof(gchar), len, tmpfp);
353 base64_decoder_free(decoder);
355 if (uncanonicalize) {
357 while (fgets(buf, sizeof(buf), tmpfp) != NULL) {
363 } else if (encoding == ENC_X_UUENCODE) {
364 gchar outbuf[BUFFSIZE];
366 gboolean flag = FALSE;
368 while ((ftell(infp) < readend) && (fgets(buf, sizeof(buf), infp) != NULL)) {
369 if (!flag && strncmp(buf,"begin ", 6)) continue;
372 len = fromuutobits(outbuf, buf);
375 g_warning("Bad UUENCODE content(%d)\n", len);
378 fwrite(outbuf, sizeof(gchar), len, outfp);
383 while ((ftell(infp) < readend) && (fgets(buf, sizeof(buf), infp) != NULL)) {
397 stat(tmpfilename, &statbuf);
398 if (mimeinfo->tmp && (mimeinfo->data.filename != NULL))
399 g_unlink(mimeinfo->data.filename);
400 g_free(mimeinfo->data.filename);
401 mimeinfo->data.filename = tmpfilename;
402 mimeinfo->tmp = TRUE;
403 mimeinfo->offset = 0;
404 mimeinfo->length = statbuf.st_size;
405 mimeinfo->encoding_type = ENC_BINARY;
410 #define B64_LINE_SIZE 57
411 #define B64_BUFFSIZE 77
413 gboolean procmime_encode_content(MimeInfo *mimeinfo, EncodingType encoding)
415 FILE *infp = NULL, *outfp;
420 if (mimeinfo->content == MIMECONTENT_EMPTY)
423 if (mimeinfo->encoding_type != ENC_UNKNOWN &&
424 mimeinfo->encoding_type != ENC_BINARY &&
425 mimeinfo->encoding_type != ENC_7BIT &&
426 mimeinfo->encoding_type != ENC_8BIT)
427 if(!procmime_decode_content(mimeinfo))
430 outfp = get_tmpfile_in_dir(get_mime_tmp_dir(), &tmpfilename);
436 if (mimeinfo->content == MIMECONTENT_FILE) {
437 if ((infp = g_fopen(mimeinfo->data.filename, "rb")) == NULL) {
438 g_warning("Can't open file %s\n", mimeinfo->data.filename);
441 } else if (mimeinfo->content == MIMECONTENT_MEM) {
442 infp = str_open_as_stream(mimeinfo->data.mem);
447 if (encoding == ENC_BASE64) {
448 gchar inbuf[B64_LINE_SIZE], outbuf[B64_BUFFSIZE];
450 gchar *tmp_file = NULL;
452 if (mimeinfo->type == MIMETYPE_TEXT ||
453 mimeinfo->type == MIMETYPE_MESSAGE) {
454 if (mimeinfo->content == MIMECONTENT_FILE) {
455 tmp_file = get_tmp_file();
456 if (canonicalize_file(mimeinfo->data.filename, tmp_file) < 0) {
461 if ((tmp_fp = g_fopen(tmp_file, "rb")) == NULL) {
462 FILE_OP_ERROR(tmp_file, "fopen");
469 gchar *out = canonicalize_str(mimeinfo->data.mem);
471 infp = str_open_as_stream(out);
479 while ((len = fread(inbuf, sizeof(gchar),
480 B64_LINE_SIZE, tmp_fp))
482 base64_encode(outbuf, inbuf, B64_LINE_SIZE);
483 fputs(outbuf, outfp);
486 if (len > 0 && feof(tmp_fp)) {
487 base64_encode(outbuf, inbuf, len);
488 fputs(outbuf, outfp);
497 } else if (encoding == ENC_QUOTED_PRINTABLE) {
498 gchar inbuf[BUFFSIZE], outbuf[BUFFSIZE * 4];
500 while (fgets(inbuf, sizeof(inbuf), infp) != NULL) {
501 qp_encode_line(outbuf, inbuf);
503 if (!strncmp("From ", outbuf, sizeof("From ")-1)) {
504 gchar *tmpbuf = outbuf;
506 tmpbuf += sizeof("From ")-1;
508 fputs("=46rom ", outfp);
509 fputs(tmpbuf, outfp);
511 fputs(outbuf, outfp);
516 while (fgets(buf, sizeof(buf), infp) != NULL) {
525 if (mimeinfo->content == MIMECONTENT_FILE) {
526 if (mimeinfo->tmp && (mimeinfo->data.filename != NULL))
527 g_unlink(mimeinfo->data.filename);
528 g_free(mimeinfo->data.filename);
529 } else if (mimeinfo->content == MIMECONTENT_MEM) {
530 if (mimeinfo->tmp && (mimeinfo->data.mem != NULL))
531 g_free(mimeinfo->data.mem);
534 stat(tmpfilename, &statbuf);
535 mimeinfo->content = MIMECONTENT_FILE;
536 mimeinfo->data.filename = tmpfilename;
537 mimeinfo->tmp = TRUE;
538 mimeinfo->offset = 0;
539 mimeinfo->length = statbuf.st_size;
540 mimeinfo->encoding_type = encoding;
545 gint procmime_get_part(const gchar *outfile, MimeInfo *mimeinfo)
549 gint restlength, readlength;
551 g_return_val_if_fail(outfile != NULL, -1);
552 g_return_val_if_fail(mimeinfo != NULL, -1);
554 if (mimeinfo->encoding_type != ENC_BINARY && !procmime_decode_content(mimeinfo))
557 if ((infp = g_fopen(mimeinfo->data.filename, "rb")) == NULL) {
558 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
561 if (fseek(infp, mimeinfo->offset, SEEK_SET) < 0) {
562 FILE_OP_ERROR(mimeinfo->data.filename, "fseek");
566 if ((outfp = g_fopen(outfile, "wb")) == NULL) {
567 FILE_OP_ERROR(outfile, "fopen");
572 restlength = mimeinfo->length;
574 while ((restlength > 0) && ((readlength = fread(buf, 1, restlength > BUFFSIZE ? BUFFSIZE : restlength, infp)) > 0)) {
575 fwrite(buf, 1, readlength, outfp);
576 restlength -= readlength;
580 if (fclose(outfp) == EOF) {
581 FILE_OP_ERROR(outfile, "fclose");
589 struct ContentRenderer {
594 static GList * renderer_list = NULL;
596 static struct ContentRenderer *
597 content_renderer_new(char * content_type, char * renderer)
599 struct ContentRenderer * cr;
601 cr = g_new(struct ContentRenderer, 1);
605 cr->content_type = g_strdup(content_type);
606 cr->renderer = g_strdup(renderer);
611 static void content_renderer_free(struct ContentRenderer * cr)
613 g_free(cr->content_type);
614 g_free(cr->renderer);
618 void renderer_read_config(void)
624 g_list_foreach(renderer_list, (GFunc) content_renderer_free, NULL);
625 renderer_list = NULL;
627 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, RENDERER_RC, NULL);
628 f = g_fopen(rcpath, "rb");
634 while (fgets(buf, BUFFSIZE, f)) {
636 struct ContentRenderer * cr;
639 p = strchr(buf, ' ');
644 cr = content_renderer_new(buf, p + 1);
648 renderer_list = g_list_append(renderer_list, cr);
654 void renderer_write_config(void)
660 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, RENDERER_RC, NULL);
662 if ((pfile = prefs_write_open(rcpath)) == NULL) {
663 g_warning("failed to write configuration to file\n");
670 for (cur = renderer_list ; cur != NULL ; cur = cur->next) {
671 struct ContentRenderer * renderer;
672 renderer = cur->data;
673 fprintf(pfile->fp, "%s %s\n", renderer->content_type,
677 if (prefs_file_close(pfile) < 0) {
678 g_warning("failed to write configuration to file\n");
683 FILE *procmime_get_text_content(MimeInfo *mimeinfo)
686 const gchar *src_codeset;
687 gboolean conv_fail = FALSE;
690 struct ContentRenderer * renderer;
692 gchar *tmpfile, *content_type;
694 g_return_val_if_fail(mimeinfo != NULL, NULL);
696 if (!procmime_decode_content(mimeinfo))
699 tmpfile = procmime_get_tmp_file_name(mimeinfo);
703 if (procmime_get_part(tmpfile, mimeinfo) < 0) {
708 tmpfp = g_fopen(tmpfile, "rb");
714 if ((outfp = my_tmpfile()) == NULL) {
721 src_codeset = forced_charset
723 procmime_mimeinfo_get_parameter(mimeinfo, "charset");
727 content_type = procmime_get_content_type_str(mimeinfo->type,
729 for (cur = renderer_list ; cur != NULL ; cur = cur->next) {
730 struct ContentRenderer * cr;
733 if (g_ascii_strcasecmp(cr->content_type, content_type) == 0) {
738 g_free(content_type);
740 if (renderer != NULL) {
746 dup2(fileno(outfp), 1);
748 p = popen(renderer->renderer, "w");
753 fread(buf, sizeof(char), sizeof(buf),
755 fwrite(buf, sizeof(char), count, p);
760 /* CodeConverter seems to have no effect here */
761 } else if (mimeinfo->type == MIMETYPE_TEXT && !g_ascii_strcasecmp(mimeinfo->subtype, "html")) {
762 SC_HTMLParser *parser;
765 conv = conv_code_converter_new(src_codeset);
766 parser = sc_html_parser_new(tmpfp, conv);
767 while ((str = sc_html_parse(parser)) != NULL) {
770 sc_html_parser_destroy(parser);
771 conv_code_converter_destroy(conv);
772 } else if (mimeinfo->type == MIMETYPE_TEXT && !g_ascii_strcasecmp(mimeinfo->subtype, "enriched")) {
776 conv = conv_code_converter_new(src_codeset);
777 parser = ertf_parser_new(tmpfp, conv);
778 while ((str = ertf_parse(parser)) != NULL) {
781 ertf_parser_destroy(parser);
782 conv_code_converter_destroy(conv);
783 } else if (mimeinfo->type == MIMETYPE_TEXT) {
784 while (fgets(buf, sizeof(buf), tmpfp) != NULL) {
785 str = conv_codeset_strdup(buf, src_codeset, CS_UTF_8);
797 g_warning("procmime_get_text_content(): Code conversion failed.\n");
807 /* search the first text part of (multipart) MIME message,
808 decode, convert it and output to outfp. */
809 FILE *procmime_get_first_text_content(MsgInfo *msginfo)
812 MimeInfo *mimeinfo, *partinfo;
814 g_return_val_if_fail(msginfo != NULL, NULL);
816 mimeinfo = procmime_scan_message(msginfo);
817 if (!mimeinfo) return NULL;
820 while (partinfo && partinfo->type != MIMETYPE_TEXT) {
821 partinfo = procmime_mimeinfo_next(partinfo);
824 outfp = procmime_get_text_content(partinfo);
826 procmime_mimeinfo_free_all(mimeinfo);
832 static gboolean find_encrypted_func(GNode *node, gpointer data)
834 MimeInfo *mimeinfo = (MimeInfo *) node->data;
835 MimeInfo **encinfo = (MimeInfo **) data;
837 if (privacy_mimeinfo_is_encrypted(mimeinfo)) {
845 static MimeInfo *find_encrypted_part(MimeInfo *rootinfo)
847 MimeInfo *encinfo = NULL;
849 g_node_traverse(rootinfo->node, G_IN_ORDER, G_TRAVERSE_ALL, -1,
850 find_encrypted_func, &encinfo);
855 /* search the first encrypted text part of (multipart) MIME message,
856 decode, convert it and output to outfp. */
857 FILE *procmime_get_first_encrypted_text_content(MsgInfo *msginfo)
860 MimeInfo *mimeinfo, *partinfo, *encinfo;
862 g_return_val_if_fail(msginfo != NULL, NULL);
864 mimeinfo = procmime_scan_message(msginfo);
870 if ((encinfo = find_encrypted_part(partinfo)) != NULL) {
871 debug_print("decrypting message part\n");
872 if (privacy_mimeinfo_decrypt(encinfo) < 0) {
873 alertpanel_error(_("Couldn't decrypt: %s"),
874 privacy_get_error());
879 while (partinfo && partinfo->type != MIMETYPE_TEXT) {
880 partinfo = procmime_mimeinfo_next(partinfo);
884 outfp = procmime_get_text_content(partinfo);
886 procmime_mimeinfo_free_all(mimeinfo);
891 gboolean procmime_msginfo_is_encrypted(MsgInfo *msginfo)
893 MimeInfo *mimeinfo, *partinfo;
894 gboolean result = FALSE;
896 g_return_val_if_fail(msginfo != NULL, FALSE);
898 mimeinfo = procmime_scan_message(msginfo);
904 result = (find_encrypted_part(partinfo) != NULL);
905 procmime_mimeinfo_free_all(mimeinfo);
910 gboolean procmime_find_string_part(MimeInfo *mimeinfo, const gchar *filename,
911 const gchar *str, StrFindFunc find_func)
916 g_return_val_if_fail(mimeinfo != NULL, FALSE);
917 g_return_val_if_fail(mimeinfo->type == MIMETYPE_TEXT, FALSE);
918 g_return_val_if_fail(str != NULL, FALSE);
919 g_return_val_if_fail(find_func != NULL, FALSE);
921 outfp = procmime_get_text_content(mimeinfo);
926 while (fgets(buf, sizeof(buf), outfp) != NULL) {
928 if (find_func(buf, str)) {
939 gboolean procmime_find_string(MsgInfo *msginfo, const gchar *str,
940 StrFindFunc find_func)
945 gboolean found = FALSE;
947 g_return_val_if_fail(msginfo != NULL, FALSE);
948 g_return_val_if_fail(str != NULL, FALSE);
949 g_return_val_if_fail(find_func != NULL, FALSE);
951 filename = procmsg_get_message_file(msginfo);
952 if (!filename) return FALSE;
953 mimeinfo = procmime_scan_message(msginfo);
955 for (partinfo = mimeinfo; partinfo != NULL;
956 partinfo = procmime_mimeinfo_next(partinfo)) {
957 if (partinfo->type == MIMETYPE_TEXT) {
958 if (procmime_find_string_part
959 (partinfo, filename, str, find_func) == TRUE) {
966 procmime_mimeinfo_free_all(mimeinfo);
972 gchar *procmime_get_tmp_file_name(MimeInfo *mimeinfo)
974 static guint32 id = 0;
979 g_return_val_if_fail(mimeinfo != NULL, NULL);
981 g_snprintf(f_prefix, sizeof(f_prefix), "%08x.", id++);
983 if ((mimeinfo->type == MIMETYPE_TEXT) && !g_ascii_strcasecmp(mimeinfo->subtype, "html"))
984 base = g_strdup("mimetmp.html");
986 const gchar *basetmp;
988 basetmp = procmime_mimeinfo_get_parameter(mimeinfo, "filename");
990 basetmp = procmime_mimeinfo_get_parameter(mimeinfo, "name");
993 basetmp = g_path_get_basename(basetmp);
994 if (*basetmp == '\0')
995 basetmp = g_strdup("mimetmp");
996 base = conv_filename_from_utf8(basetmp);
997 g_free((gchar*)basetmp);
998 subst_for_shellsafe_filename(base);
1001 filename = g_strconcat(get_mime_tmp_dir(), G_DIR_SEPARATOR_S,
1002 f_prefix, base, NULL);
1009 static GList *mime_type_list = NULL;
1011 gchar *procmime_get_mime_type(const gchar *filename)
1013 static GHashTable *mime_type_table = NULL;
1014 MimeType *mime_type;
1019 if (!mime_type_table) {
1020 mime_type_table = procmime_get_mime_type_table();
1021 if (!mime_type_table) return NULL;
1024 base = g_path_get_basename(filename);
1025 if ((p = strrchr(base, '.')) != NULL)
1026 Xstrdup_a(ext, p + 1, p = NULL );
1028 if (!p) return NULL;
1031 mime_type = g_hash_table_lookup(mime_type_table, ext);
1035 str = g_strconcat(mime_type->type, "/", mime_type->sub_type,
1043 static guint procmime_str_hash(gconstpointer gptr)
1045 guint hash_result = 0;
1048 for (str = gptr; str && *str; str++) {
1049 if (isupper(*str)) hash_result += (*str + ' ');
1050 else hash_result += *str;
1056 static gint procmime_str_equal(gconstpointer gptr1, gconstpointer gptr2)
1058 const char *str1 = gptr1;
1059 const char *str2 = gptr2;
1061 return !g_utf8_collate(str1, str2);
1064 static GHashTable *procmime_get_mime_type_table(void)
1066 GHashTable *table = NULL;
1068 MimeType *mime_type;
1071 if (!mime_type_list) {
1072 mime_type_list = procmime_get_mime_type_list();
1073 if (!mime_type_list) return NULL;
1076 table = g_hash_table_new(procmime_str_hash, procmime_str_equal);
1078 for (cur = mime_type_list; cur != NULL; cur = cur->next) {
1082 mime_type = (MimeType *)cur->data;
1084 if (!mime_type->extension) continue;
1086 exts = g_strsplit(mime_type->extension, " ", 16);
1087 for (i = 0; exts[i] != NULL; i++) {
1088 /* make the key case insensitive */
1090 /* use previously dup'd key on overwriting */
1091 if (g_hash_table_lookup(table, exts[i]))
1094 key = g_strdup(exts[i]);
1095 g_hash_table_insert(table, key, mime_type);
1103 GList *procmime_get_mime_type_list(void)
1107 gchar buf[BUFFSIZE];
1110 MimeType *mime_type;
1111 gboolean fp_is_glob_file = TRUE;
1114 return mime_type_list;
1116 if ((fp = g_fopen("/usr/share/mime/globs", "rb")) == NULL) {
1117 fp_is_glob_file = FALSE;
1118 if ((fp = g_fopen("/etc/mime.types", "rb")) == NULL) {
1119 if ((fp = g_fopen(SYSCONFDIR "/mime.types", "rb"))
1121 FILE_OP_ERROR(SYSCONFDIR "/mime.types",
1128 while (fgets(buf, sizeof(buf), fp) != NULL) {
1129 p = strchr(buf, '#');
1135 if (fp_is_glob_file) {
1136 while (*p && !g_ascii_isspace(*p) && (*p!=':')) p++;
1138 while (*p && !g_ascii_isspace(*p)) p++;
1145 delim = strchr(buf, '/');
1146 if (delim == NULL) continue;
1149 mime_type = g_new(MimeType, 1);
1150 mime_type->type = g_strdup(buf);
1151 mime_type->sub_type = g_strdup(delim + 1);
1153 if (fp_is_glob_file) {
1154 while (*p && (g_ascii_isspace(*p)||(*p=='*')||(*p=='.'))) p++;
1156 while (*p && g_ascii_isspace(*p)) p++;
1160 mime_type->extension = g_strdup(p);
1162 mime_type->extension = NULL;
1164 list = g_list_append(list, mime_type);
1170 g_warning("Can't read mime.types\n");
1175 EncodingType procmime_get_encoding_for_charset(const gchar *charset)
1179 else if (!g_ascii_strncasecmp(charset, "ISO-2022-", 9) ||
1180 !g_ascii_strcasecmp(charset, "US-ASCII"))
1182 else if (!g_ascii_strcasecmp(charset, "ISO-8859-5") ||
1183 !g_ascii_strncasecmp(charset, "KOI8-", 5) ||
1184 !g_ascii_strcasecmp(charset, "Windows-1251"))
1186 else if (!g_ascii_strncasecmp(charset, "ISO-8859-", 9))
1187 return ENC_QUOTED_PRINTABLE;
1188 else if (!g_ascii_strncasecmp(charset, "UTF-8", 5))
1189 return ENC_QUOTED_PRINTABLE;
1194 EncodingType procmime_get_encoding_for_text_file(const gchar *file, gboolean *has_binary)
1197 guchar buf[BUFFSIZE];
1199 size_t octet_chars = 0;
1200 size_t total_len = 0;
1201 gfloat octet_percentage;
1202 gboolean force_b64 = FALSE;
1204 if ((fp = g_fopen(file, "rb")) == NULL) {
1205 FILE_OP_ERROR(file, "fopen");
1209 while ((len = fread(buf, sizeof(guchar), sizeof(buf), fp)) > 0) {
1213 for (p = buf, i = 0; i < len; ++p, ++i) {
1227 octet_percentage = (gfloat)octet_chars / (gfloat)total_len;
1229 octet_percentage = 0.0;
1231 debug_print("procmime_get_encoding_for_text_file(): "
1232 "8bit chars: %d / %d (%f%%)\n", octet_chars, total_len,
1233 100.0 * octet_percentage);
1235 if (octet_percentage > 0.20 || force_b64) {
1236 debug_print("using BASE64\n");
1238 } else if (octet_chars > 0) {
1239 debug_print("using quoted-printable\n");
1240 return ENC_QUOTED_PRINTABLE;
1242 debug_print("using 7bit\n");
1247 struct EncodingTable
1250 EncodingType enc_type;
1253 struct EncodingTable encoding_table[] = {
1256 {"binary", ENC_BINARY},
1257 {"quoted-printable", ENC_QUOTED_PRINTABLE},
1258 {"base64", ENC_BASE64},
1259 {"x-uuencode", ENC_UNKNOWN},
1260 {NULL, ENC_UNKNOWN},
1263 const gchar *procmime_get_encoding_str(EncodingType encoding)
1265 struct EncodingTable *enc_table;
1267 for (enc_table = encoding_table; enc_table->str != NULL; enc_table++) {
1268 if (enc_table->enc_type == encoding)
1269 return enc_table->str;
1274 /* --- NEW MIME STUFF --- */
1281 static struct TypeTable mime_type_table[] = {
1282 {"text", MIMETYPE_TEXT},
1283 {"image", MIMETYPE_IMAGE},
1284 {"audio", MIMETYPE_AUDIO},
1285 {"video", MIMETYPE_VIDEO},
1286 {"application", MIMETYPE_APPLICATION},
1287 {"message", MIMETYPE_MESSAGE},
1288 {"multipart", MIMETYPE_MULTIPART},
1292 const gchar *procmime_get_media_type_str(MimeMediaType type)
1294 struct TypeTable *type_table;
1296 for (type_table = mime_type_table; type_table->str != NULL; type_table++) {
1297 if (type_table->type == type)
1298 return type_table->str;
1303 MimeMediaType procmime_get_media_type(const gchar *str)
1305 struct TypeTable *typetablearray;
1307 for (typetablearray = mime_type_table; typetablearray->str != NULL; typetablearray++)
1308 if (g_ascii_strncasecmp(str, typetablearray->str, strlen(typetablearray->str)) == 0)
1309 return typetablearray->type;
1311 return MIMETYPE_UNKNOWN;
1315 *\brief Safe wrapper for content type string.
1317 *\return const gchar * Pointer to content type string.
1319 gchar *procmime_get_content_type_str(MimeMediaType type,
1320 const char *subtype)
1322 const gchar *type_str = NULL;
1324 if (subtype == NULL || !(type_str = procmime_get_media_type_str(type)))
1325 return g_strdup("unknown");
1326 return g_strdup_printf("%s/%s", type_str, subtype);
1329 static int procmime_parse_mimepart(MimeInfo *parent,
1330 gchar *content_type,
1331 gchar *content_encoding,
1332 gchar *content_description,
1334 gchar *content_disposition,
1335 gchar *content_location,
1336 const gchar *filename,
1340 void procmime_parse_message_rfc822(MimeInfo *mimeinfo)
1342 HeaderEntry hentry[] = {{"Content-Type:", NULL, TRUE},
1343 {"Content-Transfer-Encoding:",
1345 {"Content-Description:",
1349 {"Content-Disposition:",
1351 {"Content-Location:",
1355 {NULL, NULL, FALSE}};
1356 guint content_start, i;
1360 procmime_decode_content(mimeinfo);
1362 fp = g_fopen(mimeinfo->data.filename, "rb");
1364 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
1367 fseek(fp, mimeinfo->offset, SEEK_SET);
1368 procheader_get_header_fields(fp, hentry);
1369 if (hentry[0].body != NULL) {
1370 tmp = conv_unmime_header(hentry[0].body, NULL);
1371 g_free(hentry[0].body);
1372 hentry[0].body = tmp;
1374 if (hentry[2].body != NULL) {
1375 tmp = conv_unmime_header(hentry[2].body, NULL);
1376 g_free(hentry[2].body);
1377 hentry[2].body = tmp;
1379 if (hentry[4].body != NULL) {
1380 tmp = conv_unmime_header(hentry[4].body, NULL);
1381 g_free(hentry[4].body);
1382 hentry[4].body = tmp;
1384 if (hentry[5].body != NULL) {
1385 tmp = conv_unmime_header(hentry[5].body, NULL);
1386 g_free(hentry[5].body);
1387 hentry[5].body = tmp;
1389 content_start = ftell(fp);
1392 procmime_parse_mimepart(mimeinfo,
1393 hentry[0].body, hentry[1].body,
1394 hentry[2].body, hentry[3].body,
1395 hentry[4].body, hentry[5].body,
1396 mimeinfo->data.filename, content_start,
1397 mimeinfo->length - (content_start - mimeinfo->offset));
1399 for (i = 0; i < (sizeof hentry / sizeof hentry[0]); i++) {
1400 g_free(hentry[i].body);
1401 hentry[i].body = NULL;
1405 void procmime_parse_multipart(MimeInfo *mimeinfo)
1407 HeaderEntry hentry[] = {{"Content-Type:", NULL, TRUE},
1408 {"Content-Transfer-Encoding:",
1410 {"Content-Description:",
1414 {"Content-Disposition:",
1416 {"Content-Location:",
1418 {NULL, NULL, FALSE}};
1421 gint boundary_len = 0, lastoffset = -1, i;
1422 gchar buf[BUFFSIZE];
1426 boundary = g_hash_table_lookup(mimeinfo->typeparameters, "boundary");
1429 boundary_len = strlen(boundary);
1431 procmime_decode_content(mimeinfo);
1433 fp = g_fopen(mimeinfo->data.filename, "rb");
1435 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
1438 fseek(fp, mimeinfo->offset, SEEK_SET);
1439 while ((p = fgets(buf, sizeof(buf), fp)) != NULL && result == 0) {
1440 if (ftell(fp) - 1 > (mimeinfo->offset + mimeinfo->length))
1443 if (IS_BOUNDARY(buf, boundary, boundary_len)) {
1444 if (lastoffset != -1) {
1445 result = procmime_parse_mimepart(mimeinfo,
1446 hentry[0].body, hentry[1].body,
1447 hentry[2].body, hentry[3].body,
1448 hentry[4].body, hentry[5].body,
1449 mimeinfo->data.filename, lastoffset,
1450 (ftell(fp) - strlen(buf)) - lastoffset - 1);
1453 if (buf[2 + boundary_len] == '-' &&
1454 buf[2 + boundary_len + 1] == '-')
1457 for (i = 0; i < (sizeof hentry / sizeof hentry[0]) ; i++) {
1458 g_free(hentry[i].body);
1459 hentry[i].body = NULL;
1461 procheader_get_header_fields(fp, hentry);
1462 if (hentry[0].body != NULL) {
1463 tmp = conv_unmime_header(hentry[0].body, NULL);
1464 g_free(hentry[0].body);
1465 hentry[0].body = tmp;
1467 if (hentry[2].body != NULL) {
1468 tmp = conv_unmime_header(hentry[2].body, NULL);
1469 g_free(hentry[2].body);
1470 hentry[2].body = tmp;
1472 if (hentry[4].body != NULL) {
1473 tmp = conv_unmime_header(hentry[4].body, NULL);
1474 g_free(hentry[4].body);
1475 hentry[4].body = tmp;
1477 if (hentry[5].body != NULL) {
1478 tmp = conv_unmime_header(hentry[5].body, NULL);
1479 g_free(hentry[5].body);
1480 hentry[5].body = tmp;
1482 lastoffset = ftell(fp);
1485 for (i = 0; i < (sizeof hentry / sizeof hentry[0]); i++) {
1486 g_free(hentry[i].body);
1487 hentry[i].body = NULL;
1492 static void parse_parameters(const gchar *parameters, GHashTable *table)
1494 gchar *params, *param, *next;
1495 GSList *convlist = NULL, *concatlist = NULL, *cur;
1497 params = g_strdup(parameters);
1500 for (; next != NULL; param = next) {
1501 gchar *attribute, *value, *tmp;
1503 gboolean convert = FALSE;
1505 next = strchr_with_skip_quote(param, '"', ';');
1514 value = strchr(attribute, '=');
1520 while (value[0] == ' ')
1523 g_strdown(attribute);
1525 len = strlen(attribute);
1526 if (attribute[len - 1] == '*') {
1527 gchar *srcpos, *dstpos, *endpos;
1530 attribute[len - 1] = '\0';
1534 endpos = value + strlen(value);
1535 while (srcpos < endpos) {
1541 if (!get_hex_value(&dstvalue, srcpos[1], srcpos[2]))
1552 if (value[0] == '"')
1553 extract_quote(value, '"');
1554 else if ((tmp = strchr(value, ' ')) != NULL)
1559 while (attribute[0] == ' ')
1561 while (attribute[strlen(attribute)-1] == ' ')
1562 attribute[strlen(attribute)-1] = '\0';
1565 while (value[0] == ' ')
1567 while (value[strlen(value)-1] == ' ')
1568 value[strlen(value)-1] = '\0';
1570 if (strrchr(attribute, '*') != NULL) {
1573 tmpattr = g_strdup(attribute);
1574 tmp = strrchr(tmpattr, '*');
1577 if ((tmp[1] == '0') && (tmp[2] == '\0') &&
1578 (g_slist_find_custom(concatlist, attribute, g_str_equal) == NULL))
1579 concatlist = g_slist_prepend(concatlist, g_strdup(tmpattr));
1581 if (convert && (g_slist_find_custom(convlist, attribute, g_str_equal) == NULL))
1582 convlist = g_slist_prepend(convlist, g_strdup(tmpattr));
1585 } else if (convert) {
1586 if (g_slist_find_custom(convlist, attribute, g_str_equal) == NULL)
1587 convlist = g_slist_prepend(convlist, g_strdup(attribute));
1590 if (g_hash_table_lookup(table, attribute) == NULL)
1591 g_hash_table_insert(table, g_strdup(attribute), g_strdup(value));
1594 for (cur = concatlist; cur != NULL; cur = g_slist_next(cur)) {
1595 gchar *attribute, *attrwnum, *partvalue;
1599 attribute = (gchar *) cur->data;
1600 value = g_string_sized_new(64);
1602 attrwnum = g_strdup_printf("%s*%d", attribute, n);
1603 while ((partvalue = g_hash_table_lookup(table, attrwnum)) != NULL) {
1604 g_string_append(value, partvalue);
1608 attrwnum = g_strdup_printf("%s*%d", attribute, n);
1612 g_hash_table_insert(table, g_strdup(attribute), g_strdup(value->str));
1613 g_string_free(value, TRUE);
1615 slist_free_strings(concatlist);
1616 g_slist_free(concatlist);
1618 for (cur = convlist; cur != NULL; cur = g_slist_next(cur)) {
1619 gchar *attribute, *key, *value;
1620 gchar *charset, *lang, *oldvalue, *newvalue;
1622 attribute = (gchar *) cur->data;
1623 if (!g_hash_table_lookup_extended(
1624 table, attribute, (gpointer *)(gchar *) &key, (gpointer *)(gchar *) &value))
1628 lang = strchr(charset, '\'');
1633 oldvalue = strchr(lang, '\'');
1634 if (oldvalue == NULL)
1639 newvalue = conv_codeset_strdup(oldvalue, charset, CS_UTF_8);
1641 g_hash_table_remove(table, attribute);
1645 g_hash_table_insert(table, g_strdup(attribute), newvalue);
1647 slist_free_strings(convlist);
1648 g_slist_free(convlist);
1653 static void procmime_parse_content_type(const gchar *content_type, MimeInfo *mimeinfo)
1655 g_return_if_fail(content_type != NULL);
1656 g_return_if_fail(mimeinfo != NULL);
1658 /* RFC 2045, page 13 says that the mime subtype is MANDATORY;
1659 * if it's not available we use the default Content-Type */
1660 if ((content_type[0] == '\0') || (strchr(content_type, '/') == NULL)) {
1661 mimeinfo->type = MIMETYPE_TEXT;
1662 mimeinfo->subtype = g_strdup("plain");
1663 if (g_hash_table_lookup(mimeinfo->typeparameters,
1664 "charset") == NULL) {
1665 g_hash_table_insert(mimeinfo->typeparameters,
1666 g_strdup("charset"),
1668 conv_get_locale_charset_str_no_utf8()));
1671 gchar *type, *subtype, *params;
1673 type = g_strdup(content_type);
1674 subtype = strchr(type, '/') + 1;
1675 *(subtype - 1) = '\0';
1676 if ((params = strchr(subtype, ';')) != NULL) {
1681 mimeinfo->type = procmime_get_media_type(type);
1682 mimeinfo->subtype = g_strdup(subtype);
1684 /* Get mimeinfo->typeparameters */
1686 parse_parameters(params, mimeinfo->typeparameters);
1692 static void procmime_parse_content_disposition(const gchar *content_disposition, MimeInfo *mimeinfo)
1694 gchar *tmp, *params;
1696 g_return_if_fail(content_disposition != NULL);
1697 g_return_if_fail(mimeinfo != NULL);
1699 tmp = g_strdup(content_disposition);
1700 if ((params = strchr(tmp, ';')) != NULL) {
1706 if (!g_ascii_strcasecmp(tmp, "inline"))
1707 mimeinfo->disposition = DISPOSITIONTYPE_INLINE;
1708 else if (!g_ascii_strcasecmp(tmp, "attachment"))
1709 mimeinfo->disposition = DISPOSITIONTYPE_ATTACHMENT;
1711 mimeinfo->disposition = DISPOSITIONTYPE_ATTACHMENT;
1714 parse_parameters(params, mimeinfo->dispositionparameters);
1720 static void procmime_parse_content_encoding(const gchar *content_encoding, MimeInfo *mimeinfo)
1722 struct EncodingTable *enc_table;
1724 for (enc_table = encoding_table; enc_table->str != NULL; enc_table++) {
1725 if (g_ascii_strcasecmp(enc_table->str, content_encoding) == 0) {
1726 mimeinfo->encoding_type = enc_table->enc_type;
1730 mimeinfo->encoding_type = ENC_UNKNOWN;
1734 static int procmime_parse_mimepart(MimeInfo *parent,
1735 gchar *content_type,
1736 gchar *content_encoding,
1737 gchar *content_description,
1739 gchar *content_disposition,
1740 gchar *content_location,
1741 const gchar *filename,
1747 /* Create MimeInfo */
1748 mimeinfo = procmime_mimeinfo_new();
1749 mimeinfo->content = MIMECONTENT_FILE;
1750 if (parent != NULL) {
1751 if (g_node_depth(parent->node) > 32) {
1752 /* 32 is an arbitrary value
1753 * this avoids DOSsing ourselves
1754 * with enormous messages
1756 procmime_mimeinfo_free_all(mimeinfo);
1759 g_node_append(parent->node, mimeinfo->node);
1761 mimeinfo->data.filename = g_strdup(filename);
1762 mimeinfo->offset = offset;
1763 mimeinfo->length = length;
1765 if (content_type != NULL) {
1766 procmime_parse_content_type(content_type, mimeinfo);
1768 mimeinfo->type = MIMETYPE_TEXT;
1769 mimeinfo->subtype = g_strdup("plain");
1770 if (g_hash_table_lookup(mimeinfo->typeparameters,
1771 "charset") == NULL) {
1772 g_hash_table_insert(mimeinfo->typeparameters,
1773 g_strdup("charset"),
1775 conv_get_locale_charset_str_no_utf8()));
1779 if (content_encoding != NULL) {
1780 procmime_parse_content_encoding(content_encoding, mimeinfo);
1782 mimeinfo->encoding_type = ENC_UNKNOWN;
1785 if (content_description != NULL)
1786 mimeinfo->description = g_strdup(content_description);
1788 mimeinfo->description = NULL;
1790 if (content_id != NULL)
1791 mimeinfo->id = g_strdup(content_id);
1793 mimeinfo->id = NULL;
1795 if (content_location != NULL)
1796 mimeinfo->location = g_strdup(content_location);
1798 mimeinfo->location = NULL;
1800 if (content_disposition != NULL)
1801 procmime_parse_content_disposition(content_disposition, mimeinfo);
1803 mimeinfo->disposition = DISPOSITIONTYPE_UNKNOWN;
1805 /* Call parser for mime type */
1806 switch (mimeinfo->type) {
1807 case MIMETYPE_MESSAGE:
1808 if (g_ascii_strcasecmp(mimeinfo->subtype, "rfc822") == 0) {
1809 procmime_parse_message_rfc822(mimeinfo);
1813 case MIMETYPE_MULTIPART:
1814 procmime_parse_multipart(mimeinfo);
1824 static gchar *typenames[] = {
1835 static gboolean output_func(GNode *node, gpointer data)
1838 MimeInfo *mimeinfo = (MimeInfo *) node->data;
1840 depth = g_node_depth(node);
1841 for (i = 0; i < depth; i++)
1843 printf("%s/%s (offset:%d length:%d encoding: %d)\n", typenames[mimeinfo->type], mimeinfo->subtype, mimeinfo->offset, mimeinfo->length, mimeinfo->encoding_type);
1848 static void output_mime_structure(MimeInfo *mimeinfo, int indent)
1850 g_node_traverse(mimeinfo->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1, output_func, NULL);
1853 MimeInfo *procmime_scan_file_with_offset(const gchar *filename, int offset)
1858 stat(filename, &buf);
1860 mimeinfo = procmime_mimeinfo_new();
1861 mimeinfo->content = MIMECONTENT_FILE;
1862 mimeinfo->encoding_type = ENC_UNKNOWN;
1863 mimeinfo->type = MIMETYPE_MESSAGE;
1864 mimeinfo->subtype = g_strdup("rfc822");
1865 mimeinfo->data.filename = g_strdup(filename);
1866 mimeinfo->offset = offset;
1867 mimeinfo->length = buf.st_size - offset;
1869 procmime_parse_message_rfc822(mimeinfo);
1870 if (debug_get_mode())
1871 output_mime_structure(mimeinfo, 0);
1876 MimeInfo *procmime_scan_file(const gchar *filename)
1880 g_return_val_if_fail(filename != NULL, NULL);
1882 mimeinfo = procmime_scan_file_with_offset(filename, 0);
1887 MimeInfo *procmime_scan_queue_file(const gchar *filename)
1891 gchar buf[BUFFSIZE];
1894 g_return_val_if_fail(filename != NULL, NULL);
1897 if ((fp = g_fopen(filename, "rb")) == NULL)
1899 /* Skip queue header */
1900 while (fgets(buf, sizeof(buf), fp) != NULL) {
1902 if ((!strncmp(buf, "X-Claws-End-Special-Headers: 1",
1903 strlen("X-Claws-End-Special-Headers:"))) ||
1904 (!strncmp(buf, "X-Sylpheed-End-Special-Headers: 1",
1905 strlen("X-Sylpheed-End-Special-Headers:"))))
1908 if (buf[0] == '\r' || buf[0] == '\n') break;
1909 /* from other mailers */
1910 if (!strncmp(buf, "Date: ", 6)
1911 || !strncmp(buf, "To: ", 4)
1912 || !strncmp(buf, "From: ", 6)
1913 || !strncmp(buf, "Subject: ", 9)) {
1921 mimeinfo = procmime_scan_file_with_offset(filename, offset);
1928 ENC_AS_QUOTED_STRING,
1933 typedef struct _ParametersData {
1939 static void write_parameters(gpointer key, gpointer value, gpointer user_data)
1942 gchar *val = value, *valpos, *tmp;
1943 ParametersData *pdata = (ParametersData *)user_data;
1944 GString *buf = g_string_new("");
1946 EncodeAs encas = ENC_AS_TOKEN;
1948 for (valpos = val; *valpos != 0; valpos++) {
1949 if (!IS_ASCII(*valpos) || *valpos == '"') {
1950 encas = ENC_AS_EXTENDED;
1955 if (((*valpos >= 0) && (*valpos < 037)) || (*valpos == 0177)) {
1956 encas = ENC_AS_QUOTED_STRING;
1960 /* tspecials + SPACE */
1978 encas = ENC_AS_QUOTED_STRING;
1983 if (encas == ENC_AS_EXTENDED && pdata->ascii_only == TRUE)
1984 encas = ENC_TO_ASCII;
1988 g_string_append_printf(buf, "%s=%s", param, val);
1992 tmp = g_strdup(val);
1994 " ()<>@,';:\\/[]?=.0123456789"
1995 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1996 "abcdefghijklmnopqrstuvwxyz",
1998 g_string_append_printf(buf, "%s=\"%s\"", param, tmp);
2002 case ENC_AS_QUOTED_STRING:
2003 g_string_append_printf(buf, "%s=\"%s\"", param, val);
2006 case ENC_AS_EXTENDED:
2007 if (!g_utf8_validate(val, -1, NULL))
2008 g_string_append_printf(buf, "%s*=%s''", param,
2009 conv_get_locale_charset_str());
2011 g_string_append_printf(buf, "%s*=%s''", param,
2013 for (valpos = val; *valpos != '\0'; valpos++) {
2014 if (IS_ASCII(*valpos) && isalnum(*valpos)) {
2015 g_string_append_printf(buf, "%c", *valpos);
2017 gchar hexstr[3] = "XX";
2018 get_hex_str(hexstr, *valpos);
2019 g_string_append_printf(buf, "%%%s", hexstr);
2025 if (buf->str && strlen(buf->str)) {
2026 if (pdata->len + strlen(buf->str) + 2 > 76) {
2027 fprintf(pdata->fp, ";\n %s", buf->str);
2028 pdata->len = strlen(buf->str) + 1;
2030 fprintf(pdata->fp, "; %s", buf->str);
2031 pdata->len += strlen(buf->str) + 2;
2034 g_string_free(buf, TRUE);
2037 void procmime_write_mime_header(MimeInfo *mimeinfo, FILE *fp)
2039 struct TypeTable *type_table;
2040 ParametersData *pdata = g_new0(ParametersData, 1);
2041 debug_print("procmime_write_mime_header\n");
2044 pdata->ascii_only = FALSE;
2046 for (type_table = mime_type_table; type_table->str != NULL; type_table++)
2047 if (mimeinfo->type == type_table->type) {
2048 gchar *buf = g_strdup_printf(
2049 "Content-Type: %s/%s", type_table->str, mimeinfo->subtype);
2050 fprintf(fp, "%s", buf);
2051 pdata->len = strlen(buf);
2052 pdata->ascii_only = TRUE;
2056 g_hash_table_foreach(mimeinfo->typeparameters, write_parameters, pdata);
2061 if (mimeinfo->encoding_type != ENC_UNKNOWN)
2062 fprintf(fp, "Content-Transfer-Encoding: %s\n", procmime_get_encoding_str(mimeinfo->encoding_type));
2064 if (mimeinfo->description != NULL)
2065 fprintf(fp, "Content-Description: %s\n", mimeinfo->description);
2067 if (mimeinfo->id != NULL)
2068 fprintf(fp, "Content-ID: %s\n", mimeinfo->id);
2070 if (mimeinfo->location != NULL)
2071 fprintf(fp, "Content-Location: %s\n", mimeinfo->location);
2073 if (mimeinfo->disposition != DISPOSITIONTYPE_UNKNOWN) {
2074 ParametersData *pdata = g_new0(ParametersData, 1);
2076 if (mimeinfo->disposition == DISPOSITIONTYPE_INLINE)
2077 buf = g_strdup("Content-Disposition: inline");
2078 else if (mimeinfo->disposition == DISPOSITIONTYPE_ATTACHMENT)
2079 buf = g_strdup("Content-Disposition: attachment");
2081 buf = g_strdup("Content-Disposition: unknown");
2083 fprintf(fp, "%s", buf);
2084 pdata->len = strlen(buf);
2088 pdata->ascii_only = FALSE;
2090 g_hash_table_foreach(mimeinfo->dispositionparameters, write_parameters, pdata);
2098 gint procmime_write_message_rfc822(MimeInfo *mimeinfo, FILE *fp)
2103 gchar buf[BUFFSIZE];
2104 gboolean skip = FALSE;;
2106 debug_print("procmime_write_message_rfc822\n");
2109 switch (mimeinfo->content) {
2110 case MIMECONTENT_FILE:
2111 if ((infp = g_fopen(mimeinfo->data.filename, "rb")) == NULL) {
2112 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
2115 fseek(infp, mimeinfo->offset, SEEK_SET);
2116 while (fgets(buf, sizeof(buf), infp) == buf) {
2117 if (buf[0] == '\n' && buf[1] == '\0')
2119 if (skip && (buf[0] == ' ' || buf[0] == '\t'))
2121 if (g_ascii_strncasecmp(buf, "Mime-Version:", 13) == 0 ||
2122 g_ascii_strncasecmp(buf, "Content-Type:", 13) == 0 ||
2123 g_ascii_strncasecmp(buf, "Content-Transfer-Encoding:", 26) == 0 ||
2124 g_ascii_strncasecmp(buf, "Content-Description:", 20) == 0 ||
2125 g_ascii_strncasecmp(buf, "Content-ID:", 11) == 0 ||
2126 g_ascii_strncasecmp(buf, "Content-Location:", 17) == 0 ||
2127 g_ascii_strncasecmp(buf, "Content-Disposition:", 20) == 0) {
2131 fwrite(buf, sizeof(gchar), strlen(buf), fp);
2137 case MIMECONTENT_MEM:
2138 fwrite(mimeinfo->data.mem,
2140 strlen(mimeinfo->data.mem),
2148 childnode = mimeinfo->node->children;
2149 if (childnode == NULL)
2152 child = (MimeInfo *) childnode->data;
2153 fprintf(fp, "Mime-Version: 1.0\n");
2154 procmime_write_mime_header(child, fp);
2155 return procmime_write_mimeinfo(child, fp);
2158 gint procmime_write_multipart(MimeInfo *mimeinfo, FILE *fp)
2162 gchar *boundary, *str, *str2;
2163 gchar buf[BUFFSIZE];
2164 gboolean firstboundary;
2166 debug_print("procmime_write_multipart\n");
2168 boundary = g_hash_table_lookup(mimeinfo->typeparameters, "boundary");
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 fseek(infp, mimeinfo->offset, SEEK_SET);
2177 while (fgets(buf, sizeof(buf), infp) == buf) {
2178 if (IS_BOUNDARY(buf, boundary, strlen(boundary)))
2180 fwrite(buf, sizeof(gchar), strlen(buf), fp);
2185 case MIMECONTENT_MEM:
2186 str = g_strdup(mimeinfo->data.mem);
2187 if (((str2 = strstr(str, boundary)) != NULL) && ((str2 - str) >= 2) &&
2188 (*(str2 - 1) == '-') && (*(str2 - 2) == '-'))
2190 fwrite(str, sizeof(gchar), strlen(str), fp);
2198 childnode = mimeinfo->node->children;
2199 firstboundary = TRUE;
2200 while (childnode != NULL) {
2201 MimeInfo *child = childnode->data;
2204 firstboundary = FALSE;
2207 fprintf(fp, "--%s\n", boundary);
2209 procmime_write_mime_header(child, fp);
2210 if (procmime_write_mimeinfo(child, fp) < 0)
2213 childnode = g_node_next_sibling(childnode);
2215 fprintf(fp, "\n--%s--\n", boundary);
2220 gint procmime_write_mimeinfo(MimeInfo *mimeinfo, FILE *fp)
2224 debug_print("procmime_write_mimeinfo\n");
2226 if (G_NODE_IS_LEAF(mimeinfo->node)) {
2227 switch (mimeinfo->content) {
2228 case MIMECONTENT_FILE:
2229 if ((infp = g_fopen(mimeinfo->data.filename, "rb")) == NULL) {
2230 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
2233 copy_file_part_to_fp(infp, mimeinfo->offset, mimeinfo->length, fp);
2237 case MIMECONTENT_MEM:
2238 fwrite(mimeinfo->data.mem,
2240 strlen(mimeinfo->data.mem),
2248 /* Call writer for mime type */
2249 switch (mimeinfo->type) {
2250 case MIMETYPE_MESSAGE:
2251 if (g_ascii_strcasecmp(mimeinfo->subtype, "rfc822") == 0) {
2252 return procmime_write_message_rfc822(mimeinfo, fp);
2256 case MIMETYPE_MULTIPART:
2257 return procmime_write_multipart(mimeinfo, fp);
2269 gchar *procmime_get_part_file_name(MimeInfo *mimeinfo)
2273 if ((mimeinfo->type == MIMETYPE_TEXT) && !g_ascii_strcasecmp(mimeinfo->subtype, "html"))
2274 base = g_strdup("mimetmp.html");
2276 const gchar *basetmp;
2279 basetmp = procmime_mimeinfo_get_parameter(mimeinfo, "filename");
2280 if (basetmp == NULL)
2281 basetmp = procmime_mimeinfo_get_parameter(mimeinfo, "name");
2282 if (basetmp == NULL)
2283 basetmp = "mimetmp";
2284 basename = g_path_get_basename(basetmp);
2285 if (*basename == '\0') {
2287 basename = g_strdup("mimetmp");
2289 base = conv_filename_from_utf8(basename);
2291 subst_for_shellsafe_filename(base);