2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2007 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;
550 gint saved_errno = 0;
552 g_return_val_if_fail(outfile != NULL, -1);
553 g_return_val_if_fail(mimeinfo != NULL, -1);
555 if (mimeinfo->encoding_type != ENC_BINARY && !procmime_decode_content(mimeinfo))
558 if ((infp = g_fopen(mimeinfo->data.filename, "rb")) == NULL) {
560 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
561 return -(saved_errno);
563 if (fseek(infp, mimeinfo->offset, SEEK_SET) < 0) {
565 FILE_OP_ERROR(mimeinfo->data.filename, "fseek");
567 return -(saved_errno);
569 if ((outfp = g_fopen(outfile, "wb")) == NULL) {
571 FILE_OP_ERROR(outfile, "fopen");
573 return -(saved_errno);
576 restlength = mimeinfo->length;
578 while ((restlength > 0) && ((readlength = fread(buf, 1, restlength > BUFFSIZE ? BUFFSIZE : restlength, infp)) > 0)) {
579 if (fwrite(buf, 1, readlength, outfp) != readlength) {
582 return -(saved_errno);
584 restlength -= readlength;
588 if (fclose(outfp) == EOF) {
590 FILE_OP_ERROR(outfile, "fclose");
592 return -(saved_errno);
598 struct ContentRenderer {
603 static GList * renderer_list = NULL;
605 static struct ContentRenderer *
606 content_renderer_new(char * content_type, char * renderer)
608 struct ContentRenderer * cr;
610 cr = g_new(struct ContentRenderer, 1);
614 cr->content_type = g_strdup(content_type);
615 cr->renderer = g_strdup(renderer);
620 static void content_renderer_free(struct ContentRenderer * cr)
622 g_free(cr->content_type);
623 g_free(cr->renderer);
627 void renderer_read_config(void)
633 g_list_foreach(renderer_list, (GFunc) content_renderer_free, NULL);
634 renderer_list = NULL;
636 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, RENDERER_RC, NULL);
637 f = g_fopen(rcpath, "rb");
643 while (fgets(buf, BUFFSIZE, f)) {
645 struct ContentRenderer * cr;
648 p = strchr(buf, ' ');
653 cr = content_renderer_new(buf, p + 1);
657 renderer_list = g_list_append(renderer_list, cr);
663 void renderer_write_config(void)
669 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, RENDERER_RC, NULL);
671 if ((pfile = prefs_write_open(rcpath)) == NULL) {
672 g_warning("failed to write configuration to file\n");
679 for (cur = renderer_list ; cur != NULL ; cur = cur->next) {
680 struct ContentRenderer * renderer;
681 renderer = cur->data;
682 fprintf(pfile->fp, "%s %s\n", renderer->content_type,
686 if (prefs_file_close(pfile) < 0) {
687 g_warning("failed to write configuration to file\n");
692 FILE *procmime_get_text_content(MimeInfo *mimeinfo)
695 const gchar *src_codeset;
696 gboolean conv_fail = FALSE;
699 struct ContentRenderer * renderer;
701 gchar *tmpfile, *content_type;
703 g_return_val_if_fail(mimeinfo != NULL, NULL);
705 if (!procmime_decode_content(mimeinfo))
708 tmpfile = procmime_get_tmp_file_name(mimeinfo);
712 if (procmime_get_part(tmpfile, mimeinfo) < 0) {
717 tmpfp = g_fopen(tmpfile, "rb");
723 if ((outfp = my_tmpfile()) == NULL) {
730 src_codeset = forced_charset
732 procmime_mimeinfo_get_parameter(mimeinfo, "charset");
736 content_type = procmime_get_content_type_str(mimeinfo->type,
738 for (cur = renderer_list ; cur != NULL ; cur = cur->next) {
739 struct ContentRenderer * cr;
742 if (g_ascii_strcasecmp(cr->content_type, content_type) == 0) {
747 g_free(content_type);
749 if (renderer != NULL) {
755 dup2(fileno(outfp), 1);
757 p = popen(renderer->renderer, "w");
762 fread(buf, sizeof(char), sizeof(buf),
764 fwrite(buf, sizeof(char), count, p);
769 /* CodeConverter seems to have no effect here */
770 } else if (mimeinfo->type == MIMETYPE_TEXT && !g_ascii_strcasecmp(mimeinfo->subtype, "html")) {
771 SC_HTMLParser *parser;
774 conv = conv_code_converter_new(src_codeset);
775 parser = sc_html_parser_new(tmpfp, conv);
776 while ((str = sc_html_parse(parser)) != NULL) {
779 sc_html_parser_destroy(parser);
780 conv_code_converter_destroy(conv);
781 } else if (mimeinfo->type == MIMETYPE_TEXT && !g_ascii_strcasecmp(mimeinfo->subtype, "enriched")) {
785 conv = conv_code_converter_new(src_codeset);
786 parser = ertf_parser_new(tmpfp, conv);
787 while ((str = ertf_parse(parser)) != NULL) {
790 ertf_parser_destroy(parser);
791 conv_code_converter_destroy(conv);
792 } else if (mimeinfo->type == MIMETYPE_TEXT) {
793 while (fgets(buf, sizeof(buf), tmpfp) != NULL) {
794 str = conv_codeset_strdup(buf, src_codeset, CS_UTF_8);
806 g_warning("procmime_get_text_content(): Code conversion failed.\n");
816 /* search the first text part of (multipart) MIME message,
817 decode, convert it and output to outfp. */
818 FILE *procmime_get_first_text_content(MsgInfo *msginfo)
821 MimeInfo *mimeinfo, *partinfo;
823 g_return_val_if_fail(msginfo != NULL, NULL);
825 mimeinfo = procmime_scan_message(msginfo);
826 if (!mimeinfo) return NULL;
829 while (partinfo && partinfo->type != MIMETYPE_TEXT) {
830 partinfo = procmime_mimeinfo_next(partinfo);
833 outfp = procmime_get_text_content(partinfo);
835 procmime_mimeinfo_free_all(mimeinfo);
841 static gboolean find_encrypted_func(GNode *node, gpointer data)
843 MimeInfo *mimeinfo = (MimeInfo *) node->data;
844 MimeInfo **encinfo = (MimeInfo **) data;
846 if (privacy_mimeinfo_is_encrypted(mimeinfo)) {
854 static MimeInfo *find_encrypted_part(MimeInfo *rootinfo)
856 MimeInfo *encinfo = NULL;
858 g_node_traverse(rootinfo->node, G_IN_ORDER, G_TRAVERSE_ALL, -1,
859 find_encrypted_func, &encinfo);
864 /* search the first encrypted text part of (multipart) MIME message,
865 decode, convert it and output to outfp. */
866 FILE *procmime_get_first_encrypted_text_content(MsgInfo *msginfo)
869 MimeInfo *mimeinfo, *partinfo, *encinfo;
871 g_return_val_if_fail(msginfo != NULL, NULL);
873 mimeinfo = procmime_scan_message(msginfo);
879 if ((encinfo = find_encrypted_part(partinfo)) != NULL) {
880 debug_print("decrypting message part\n");
881 if (privacy_mimeinfo_decrypt(encinfo) < 0) {
882 alertpanel_error(_("Couldn't decrypt: %s"),
883 privacy_get_error());
888 while (partinfo && partinfo->type != MIMETYPE_TEXT) {
889 partinfo = procmime_mimeinfo_next(partinfo);
893 outfp = procmime_get_text_content(partinfo);
895 procmime_mimeinfo_free_all(mimeinfo);
900 gboolean procmime_msginfo_is_encrypted(MsgInfo *msginfo)
902 MimeInfo *mimeinfo, *partinfo;
903 gboolean result = FALSE;
905 g_return_val_if_fail(msginfo != NULL, FALSE);
907 mimeinfo = procmime_scan_message(msginfo);
913 result = (find_encrypted_part(partinfo) != NULL);
914 procmime_mimeinfo_free_all(mimeinfo);
919 gboolean procmime_find_string_part(MimeInfo *mimeinfo, const gchar *filename,
920 const gchar *str, StrFindFunc find_func)
925 g_return_val_if_fail(mimeinfo != NULL, FALSE);
926 g_return_val_if_fail(mimeinfo->type == MIMETYPE_TEXT, FALSE);
927 g_return_val_if_fail(str != NULL, FALSE);
928 g_return_val_if_fail(find_func != NULL, FALSE);
930 outfp = procmime_get_text_content(mimeinfo);
935 while (fgets(buf, sizeof(buf), outfp) != NULL) {
937 if (find_func(buf, str)) {
948 gboolean procmime_find_string(MsgInfo *msginfo, const gchar *str,
949 StrFindFunc find_func)
954 gboolean found = FALSE;
956 g_return_val_if_fail(msginfo != NULL, FALSE);
957 g_return_val_if_fail(str != NULL, FALSE);
958 g_return_val_if_fail(find_func != NULL, FALSE);
960 filename = procmsg_get_message_file(msginfo);
961 if (!filename) return FALSE;
962 mimeinfo = procmime_scan_message(msginfo);
964 for (partinfo = mimeinfo; partinfo != NULL;
965 partinfo = procmime_mimeinfo_next(partinfo)) {
966 if (partinfo->type == MIMETYPE_TEXT) {
967 if (procmime_find_string_part
968 (partinfo, filename, str, find_func) == TRUE) {
975 procmime_mimeinfo_free_all(mimeinfo);
981 gchar *procmime_get_tmp_file_name(MimeInfo *mimeinfo)
983 static guint32 id = 0;
988 g_return_val_if_fail(mimeinfo != NULL, NULL);
990 g_snprintf(f_prefix, sizeof(f_prefix), "%08x.", id++);
992 if ((mimeinfo->type == MIMETYPE_TEXT) && !g_ascii_strcasecmp(mimeinfo->subtype, "html"))
993 base = g_strdup("mimetmp.html");
995 const gchar *basetmp;
997 basetmp = procmime_mimeinfo_get_parameter(mimeinfo, "filename");
999 basetmp = procmime_mimeinfo_get_parameter(mimeinfo, "name");
1000 if (basetmp == NULL)
1001 basetmp = "mimetmp";
1002 basetmp = g_path_get_basename(basetmp);
1003 if (*basetmp == '\0')
1004 basetmp = g_strdup("mimetmp");
1005 base = conv_filename_from_utf8(basetmp);
1006 g_free((gchar*)basetmp);
1007 subst_for_shellsafe_filename(base);
1010 filename = g_strconcat(get_mime_tmp_dir(), G_DIR_SEPARATOR_S,
1011 f_prefix, base, NULL);
1018 static GList *mime_type_list = NULL;
1020 gchar *procmime_get_mime_type(const gchar *filename)
1022 static GHashTable *mime_type_table = NULL;
1023 MimeType *mime_type;
1028 if (!mime_type_table) {
1029 mime_type_table = procmime_get_mime_type_table();
1030 if (!mime_type_table) return NULL;
1033 base = g_path_get_basename(filename);
1034 if ((p = strrchr(base, '.')) != NULL)
1035 Xstrdup_a(ext, p + 1, p = NULL );
1037 if (!p) return NULL;
1040 mime_type = g_hash_table_lookup(mime_type_table, ext);
1044 str = g_strconcat(mime_type->type, "/", mime_type->sub_type,
1052 static guint procmime_str_hash(gconstpointer gptr)
1054 guint hash_result = 0;
1057 for (str = gptr; str && *str; str++) {
1058 if (isupper(*str)) hash_result += (*str + ' ');
1059 else hash_result += *str;
1065 static gint procmime_str_equal(gconstpointer gptr1, gconstpointer gptr2)
1067 const char *str1 = gptr1;
1068 const char *str2 = gptr2;
1070 return !g_utf8_collate(str1, str2);
1073 static GHashTable *procmime_get_mime_type_table(void)
1075 GHashTable *table = NULL;
1077 MimeType *mime_type;
1080 if (!mime_type_list) {
1081 mime_type_list = procmime_get_mime_type_list();
1082 if (!mime_type_list) return NULL;
1085 table = g_hash_table_new(procmime_str_hash, procmime_str_equal);
1087 for (cur = mime_type_list; cur != NULL; cur = cur->next) {
1091 mime_type = (MimeType *)cur->data;
1093 if (!mime_type->extension) continue;
1095 exts = g_strsplit(mime_type->extension, " ", 16);
1096 for (i = 0; exts[i] != NULL; i++) {
1097 /* make the key case insensitive */
1099 /* use previously dup'd key on overwriting */
1100 if (g_hash_table_lookup(table, exts[i]))
1103 key = g_strdup(exts[i]);
1104 g_hash_table_insert(table, key, mime_type);
1112 GList *procmime_get_mime_type_list(void)
1116 gchar buf[BUFFSIZE];
1119 MimeType *mime_type;
1120 gboolean fp_is_glob_file = TRUE;
1123 return mime_type_list;
1125 if ((fp = g_fopen("/usr/share/mime/globs", "rb")) == NULL) {
1126 fp_is_glob_file = FALSE;
1127 if ((fp = g_fopen("/etc/mime.types", "rb")) == NULL) {
1128 if ((fp = g_fopen(SYSCONFDIR "/mime.types", "rb"))
1130 FILE_OP_ERROR(SYSCONFDIR "/mime.types",
1137 while (fgets(buf, sizeof(buf), fp) != NULL) {
1138 p = strchr(buf, '#');
1144 if (fp_is_glob_file) {
1145 while (*p && !g_ascii_isspace(*p) && (*p!=':')) p++;
1147 while (*p && !g_ascii_isspace(*p)) p++;
1154 delim = strchr(buf, '/');
1155 if (delim == NULL) continue;
1158 mime_type = g_new(MimeType, 1);
1159 mime_type->type = g_strdup(buf);
1160 mime_type->sub_type = g_strdup(delim + 1);
1162 if (fp_is_glob_file) {
1163 while (*p && (g_ascii_isspace(*p)||(*p=='*')||(*p=='.'))) p++;
1165 while (*p && g_ascii_isspace(*p)) p++;
1169 mime_type->extension = g_strdup(p);
1171 mime_type->extension = NULL;
1173 list = g_list_append(list, mime_type);
1179 g_warning("Can't read mime.types\n");
1184 EncodingType procmime_get_encoding_for_charset(const gchar *charset)
1188 else if (!g_ascii_strncasecmp(charset, "ISO-2022-", 9) ||
1189 !g_ascii_strcasecmp(charset, "US-ASCII"))
1191 else if (!g_ascii_strcasecmp(charset, "ISO-8859-5") ||
1192 !g_ascii_strncasecmp(charset, "KOI8-", 5) ||
1193 !g_ascii_strcasecmp(charset, "Windows-1251"))
1195 else if (!g_ascii_strncasecmp(charset, "ISO-8859-", 9))
1196 return ENC_QUOTED_PRINTABLE;
1197 else if (!g_ascii_strncasecmp(charset, "UTF-8", 5))
1198 return ENC_QUOTED_PRINTABLE;
1203 EncodingType procmime_get_encoding_for_text_file(const gchar *file, gboolean *has_binary)
1206 guchar buf[BUFFSIZE];
1208 size_t octet_chars = 0;
1209 size_t total_len = 0;
1210 gfloat octet_percentage;
1211 gboolean force_b64 = FALSE;
1213 if ((fp = g_fopen(file, "rb")) == NULL) {
1214 FILE_OP_ERROR(file, "fopen");
1218 while ((len = fread(buf, sizeof(guchar), sizeof(buf), fp)) > 0) {
1222 for (p = buf, i = 0; i < len; ++p, ++i) {
1236 octet_percentage = (gfloat)octet_chars / (gfloat)total_len;
1238 octet_percentage = 0.0;
1240 debug_print("procmime_get_encoding_for_text_file(): "
1241 "8bit chars: %d / %d (%f%%)\n", octet_chars, total_len,
1242 100.0 * octet_percentage);
1244 if (octet_percentage > 0.20 || force_b64) {
1245 debug_print("using BASE64\n");
1247 } else if (octet_chars > 0) {
1248 debug_print("using quoted-printable\n");
1249 return ENC_QUOTED_PRINTABLE;
1251 debug_print("using 7bit\n");
1256 struct EncodingTable
1259 EncodingType enc_type;
1262 struct EncodingTable encoding_table[] = {
1265 {"binary", ENC_BINARY},
1266 {"quoted-printable", ENC_QUOTED_PRINTABLE},
1267 {"base64", ENC_BASE64},
1268 {"x-uuencode", ENC_UNKNOWN},
1269 {NULL, ENC_UNKNOWN},
1272 const gchar *procmime_get_encoding_str(EncodingType encoding)
1274 struct EncodingTable *enc_table;
1276 for (enc_table = encoding_table; enc_table->str != NULL; enc_table++) {
1277 if (enc_table->enc_type == encoding)
1278 return enc_table->str;
1283 /* --- NEW MIME STUFF --- */
1290 static struct TypeTable mime_type_table[] = {
1291 {"text", MIMETYPE_TEXT},
1292 {"image", MIMETYPE_IMAGE},
1293 {"audio", MIMETYPE_AUDIO},
1294 {"video", MIMETYPE_VIDEO},
1295 {"application", MIMETYPE_APPLICATION},
1296 {"message", MIMETYPE_MESSAGE},
1297 {"multipart", MIMETYPE_MULTIPART},
1301 const gchar *procmime_get_media_type_str(MimeMediaType type)
1303 struct TypeTable *type_table;
1305 for (type_table = mime_type_table; type_table->str != NULL; type_table++) {
1306 if (type_table->type == type)
1307 return type_table->str;
1312 MimeMediaType procmime_get_media_type(const gchar *str)
1314 struct TypeTable *typetablearray;
1316 for (typetablearray = mime_type_table; typetablearray->str != NULL; typetablearray++)
1317 if (g_ascii_strncasecmp(str, typetablearray->str, strlen(typetablearray->str)) == 0)
1318 return typetablearray->type;
1320 return MIMETYPE_UNKNOWN;
1324 *\brief Safe wrapper for content type string.
1326 *\return const gchar * Pointer to content type string.
1328 gchar *procmime_get_content_type_str(MimeMediaType type,
1329 const char *subtype)
1331 const gchar *type_str = NULL;
1333 if (subtype == NULL || !(type_str = procmime_get_media_type_str(type)))
1334 return g_strdup("unknown");
1335 return g_strdup_printf("%s/%s", type_str, subtype);
1338 static int procmime_parse_mimepart(MimeInfo *parent,
1339 gchar *content_type,
1340 gchar *content_encoding,
1341 gchar *content_description,
1343 gchar *content_disposition,
1344 gchar *content_location,
1345 const gchar *filename,
1349 void procmime_parse_message_rfc822(MimeInfo *mimeinfo)
1351 HeaderEntry hentry[] = {{"Content-Type:", NULL, TRUE},
1352 {"Content-Transfer-Encoding:",
1354 {"Content-Description:",
1358 {"Content-Disposition:",
1360 {"Content-Location:",
1364 {NULL, NULL, FALSE}};
1365 guint content_start, i;
1369 procmime_decode_content(mimeinfo);
1371 fp = g_fopen(mimeinfo->data.filename, "rb");
1373 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
1376 fseek(fp, mimeinfo->offset, SEEK_SET);
1377 procheader_get_header_fields(fp, hentry);
1378 if (hentry[0].body != NULL) {
1379 tmp = conv_unmime_header(hentry[0].body, NULL);
1380 g_free(hentry[0].body);
1381 hentry[0].body = tmp;
1383 if (hentry[2].body != NULL) {
1384 tmp = conv_unmime_header(hentry[2].body, NULL);
1385 g_free(hentry[2].body);
1386 hentry[2].body = tmp;
1388 if (hentry[4].body != NULL) {
1389 tmp = conv_unmime_header(hentry[4].body, NULL);
1390 g_free(hentry[4].body);
1391 hentry[4].body = tmp;
1393 if (hentry[5].body != NULL) {
1394 tmp = conv_unmime_header(hentry[5].body, NULL);
1395 g_free(hentry[5].body);
1396 hentry[5].body = tmp;
1398 content_start = ftell(fp);
1401 procmime_parse_mimepart(mimeinfo,
1402 hentry[0].body, hentry[1].body,
1403 hentry[2].body, hentry[3].body,
1404 hentry[4].body, hentry[5].body,
1405 mimeinfo->data.filename, content_start,
1406 mimeinfo->length - (content_start - mimeinfo->offset));
1408 for (i = 0; i < (sizeof hentry / sizeof hentry[0]); i++) {
1409 g_free(hentry[i].body);
1410 hentry[i].body = NULL;
1414 void procmime_parse_multipart(MimeInfo *mimeinfo)
1416 HeaderEntry hentry[] = {{"Content-Type:", NULL, TRUE},
1417 {"Content-Transfer-Encoding:",
1419 {"Content-Description:",
1423 {"Content-Disposition:",
1425 {"Content-Location:",
1427 {NULL, NULL, FALSE}};
1430 gint boundary_len = 0, lastoffset = -1, i;
1431 gchar buf[BUFFSIZE];
1435 boundary = g_hash_table_lookup(mimeinfo->typeparameters, "boundary");
1438 boundary_len = strlen(boundary);
1440 procmime_decode_content(mimeinfo);
1442 fp = g_fopen(mimeinfo->data.filename, "rb");
1444 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
1447 fseek(fp, mimeinfo->offset, SEEK_SET);
1448 while ((p = fgets(buf, sizeof(buf), fp)) != NULL && result == 0) {
1449 if (ftell(fp) - 1 > (mimeinfo->offset + mimeinfo->length))
1452 if (IS_BOUNDARY(buf, boundary, boundary_len)) {
1453 if (lastoffset != -1) {
1454 result = procmime_parse_mimepart(mimeinfo,
1455 hentry[0].body, hentry[1].body,
1456 hentry[2].body, hentry[3].body,
1457 hentry[4].body, hentry[5].body,
1458 mimeinfo->data.filename, lastoffset,
1459 (ftell(fp) - strlen(buf)) - lastoffset - 1);
1462 if (buf[2 + boundary_len] == '-' &&
1463 buf[2 + boundary_len + 1] == '-')
1466 for (i = 0; i < (sizeof hentry / sizeof hentry[0]) ; i++) {
1467 g_free(hentry[i].body);
1468 hentry[i].body = NULL;
1470 procheader_get_header_fields(fp, hentry);
1471 if (hentry[0].body != NULL) {
1472 tmp = conv_unmime_header(hentry[0].body, NULL);
1473 g_free(hentry[0].body);
1474 hentry[0].body = tmp;
1476 if (hentry[2].body != NULL) {
1477 tmp = conv_unmime_header(hentry[2].body, NULL);
1478 g_free(hentry[2].body);
1479 hentry[2].body = tmp;
1481 if (hentry[4].body != NULL) {
1482 tmp = conv_unmime_header(hentry[4].body, NULL);
1483 g_free(hentry[4].body);
1484 hentry[4].body = tmp;
1486 if (hentry[5].body != NULL) {
1487 tmp = conv_unmime_header(hentry[5].body, NULL);
1488 g_free(hentry[5].body);
1489 hentry[5].body = tmp;
1491 lastoffset = ftell(fp);
1494 for (i = 0; i < (sizeof hentry / sizeof hentry[0]); i++) {
1495 g_free(hentry[i].body);
1496 hentry[i].body = NULL;
1501 static void parse_parameters(const gchar *parameters, GHashTable *table)
1503 gchar *params, *param, *next;
1504 GSList *convlist = NULL, *concatlist = NULL, *cur;
1506 params = g_strdup(parameters);
1509 for (; next != NULL; param = next) {
1510 gchar *attribute, *value, *tmp;
1512 gboolean convert = FALSE;
1514 next = strchr_with_skip_quote(param, '"', ';');
1523 value = strchr(attribute, '=');
1529 while (value[0] == ' ')
1532 g_strdown(attribute);
1534 len = strlen(attribute);
1535 if (attribute[len - 1] == '*') {
1536 gchar *srcpos, *dstpos, *endpos;
1539 attribute[len - 1] = '\0';
1543 endpos = value + strlen(value);
1544 while (srcpos < endpos) {
1550 if (!get_hex_value(&dstvalue, srcpos[1], srcpos[2]))
1561 if (value[0] == '"')
1562 extract_quote(value, '"');
1563 else if ((tmp = strchr(value, ' ')) != NULL)
1568 while (attribute[0] == ' ')
1570 while (attribute[strlen(attribute)-1] == ' ')
1571 attribute[strlen(attribute)-1] = '\0';
1574 while (value[0] == ' ')
1576 while (value[strlen(value)-1] == ' ')
1577 value[strlen(value)-1] = '\0';
1579 if (strrchr(attribute, '*') != NULL) {
1582 tmpattr = g_strdup(attribute);
1583 tmp = strrchr(tmpattr, '*');
1586 if ((tmp[1] == '0') && (tmp[2] == '\0') &&
1587 (g_slist_find_custom(concatlist, attribute, g_str_equal) == NULL))
1588 concatlist = g_slist_prepend(concatlist, g_strdup(tmpattr));
1590 if (convert && (g_slist_find_custom(convlist, attribute, g_str_equal) == NULL))
1591 convlist = g_slist_prepend(convlist, g_strdup(tmpattr));
1594 } else if (convert) {
1595 if (g_slist_find_custom(convlist, attribute, g_str_equal) == NULL)
1596 convlist = g_slist_prepend(convlist, g_strdup(attribute));
1599 if (g_hash_table_lookup(table, attribute) == NULL)
1600 g_hash_table_insert(table, g_strdup(attribute), g_strdup(value));
1603 for (cur = concatlist; cur != NULL; cur = g_slist_next(cur)) {
1604 gchar *attribute, *attrwnum, *partvalue;
1608 attribute = (gchar *) cur->data;
1609 value = g_string_sized_new(64);
1611 attrwnum = g_strdup_printf("%s*%d", attribute, n);
1612 while ((partvalue = g_hash_table_lookup(table, attrwnum)) != NULL) {
1613 g_string_append(value, partvalue);
1617 attrwnum = g_strdup_printf("%s*%d", attribute, n);
1621 g_hash_table_insert(table, g_strdup(attribute), g_strdup(value->str));
1622 g_string_free(value, TRUE);
1624 slist_free_strings(concatlist);
1625 g_slist_free(concatlist);
1627 for (cur = convlist; cur != NULL; cur = g_slist_next(cur)) {
1628 gchar *attribute, *key, *value;
1629 gchar *charset, *lang, *oldvalue, *newvalue;
1631 attribute = (gchar *) cur->data;
1632 if (!g_hash_table_lookup_extended(
1633 table, attribute, (gpointer *)(gchar *) &key, (gpointer *)(gchar *) &value))
1637 lang = strchr(charset, '\'');
1642 oldvalue = strchr(lang, '\'');
1643 if (oldvalue == NULL)
1648 newvalue = conv_codeset_strdup(oldvalue, charset, CS_UTF_8);
1650 g_hash_table_remove(table, attribute);
1654 g_hash_table_insert(table, g_strdup(attribute), newvalue);
1656 slist_free_strings(convlist);
1657 g_slist_free(convlist);
1662 static void procmime_parse_content_type(const gchar *content_type, MimeInfo *mimeinfo)
1664 g_return_if_fail(content_type != NULL);
1665 g_return_if_fail(mimeinfo != NULL);
1667 /* RFC 2045, page 13 says that the mime subtype is MANDATORY;
1668 * if it's not available we use the default Content-Type */
1669 if ((content_type[0] == '\0') || (strchr(content_type, '/') == NULL)) {
1670 mimeinfo->type = MIMETYPE_TEXT;
1671 mimeinfo->subtype = g_strdup("plain");
1672 if (g_hash_table_lookup(mimeinfo->typeparameters,
1673 "charset") == NULL) {
1674 g_hash_table_insert(mimeinfo->typeparameters,
1675 g_strdup("charset"),
1677 conv_get_locale_charset_str_no_utf8()));
1680 gchar *type, *subtype, *params;
1682 type = g_strdup(content_type);
1683 subtype = strchr(type, '/') + 1;
1684 *(subtype - 1) = '\0';
1685 if ((params = strchr(subtype, ';')) != NULL) {
1690 mimeinfo->type = procmime_get_media_type(type);
1691 mimeinfo->subtype = g_strdup(subtype);
1693 /* Get mimeinfo->typeparameters */
1695 parse_parameters(params, mimeinfo->typeparameters);
1701 static void procmime_parse_content_disposition(const gchar *content_disposition, MimeInfo *mimeinfo)
1703 gchar *tmp, *params;
1705 g_return_if_fail(content_disposition != NULL);
1706 g_return_if_fail(mimeinfo != NULL);
1708 tmp = g_strdup(content_disposition);
1709 if ((params = strchr(tmp, ';')) != NULL) {
1715 if (!g_ascii_strcasecmp(tmp, "inline"))
1716 mimeinfo->disposition = DISPOSITIONTYPE_INLINE;
1717 else if (!g_ascii_strcasecmp(tmp, "attachment"))
1718 mimeinfo->disposition = DISPOSITIONTYPE_ATTACHMENT;
1720 mimeinfo->disposition = DISPOSITIONTYPE_ATTACHMENT;
1723 parse_parameters(params, mimeinfo->dispositionparameters);
1729 static void procmime_parse_content_encoding(const gchar *content_encoding, MimeInfo *mimeinfo)
1731 struct EncodingTable *enc_table;
1733 for (enc_table = encoding_table; enc_table->str != NULL; enc_table++) {
1734 if (g_ascii_strcasecmp(enc_table->str, content_encoding) == 0) {
1735 mimeinfo->encoding_type = enc_table->enc_type;
1739 mimeinfo->encoding_type = ENC_UNKNOWN;
1743 static int procmime_parse_mimepart(MimeInfo *parent,
1744 gchar *content_type,
1745 gchar *content_encoding,
1746 gchar *content_description,
1748 gchar *content_disposition,
1749 gchar *content_location,
1750 const gchar *filename,
1756 /* Create MimeInfo */
1757 mimeinfo = procmime_mimeinfo_new();
1758 mimeinfo->content = MIMECONTENT_FILE;
1759 if (parent != NULL) {
1760 if (g_node_depth(parent->node) > 32) {
1761 /* 32 is an arbitrary value
1762 * this avoids DOSsing ourselves
1763 * with enormous messages
1765 procmime_mimeinfo_free_all(mimeinfo);
1768 g_node_append(parent->node, mimeinfo->node);
1770 mimeinfo->data.filename = g_strdup(filename);
1771 mimeinfo->offset = offset;
1772 mimeinfo->length = length;
1774 if (content_type != NULL) {
1775 procmime_parse_content_type(content_type, mimeinfo);
1777 mimeinfo->type = MIMETYPE_TEXT;
1778 mimeinfo->subtype = g_strdup("plain");
1779 if (g_hash_table_lookup(mimeinfo->typeparameters,
1780 "charset") == NULL) {
1781 g_hash_table_insert(mimeinfo->typeparameters,
1782 g_strdup("charset"),
1784 conv_get_locale_charset_str_no_utf8()));
1788 if (content_encoding != NULL) {
1789 procmime_parse_content_encoding(content_encoding, mimeinfo);
1791 mimeinfo->encoding_type = ENC_UNKNOWN;
1794 if (content_description != NULL)
1795 mimeinfo->description = g_strdup(content_description);
1797 mimeinfo->description = NULL;
1799 if (content_id != NULL)
1800 mimeinfo->id = g_strdup(content_id);
1802 mimeinfo->id = NULL;
1804 if (content_location != NULL)
1805 mimeinfo->location = g_strdup(content_location);
1807 mimeinfo->location = NULL;
1809 if (content_disposition != NULL)
1810 procmime_parse_content_disposition(content_disposition, mimeinfo);
1812 mimeinfo->disposition = DISPOSITIONTYPE_UNKNOWN;
1814 /* Call parser for mime type */
1815 switch (mimeinfo->type) {
1816 case MIMETYPE_MESSAGE:
1817 if (g_ascii_strcasecmp(mimeinfo->subtype, "rfc822") == 0) {
1818 procmime_parse_message_rfc822(mimeinfo);
1822 case MIMETYPE_MULTIPART:
1823 procmime_parse_multipart(mimeinfo);
1833 static gchar *typenames[] = {
1844 static gboolean output_func(GNode *node, gpointer data)
1847 MimeInfo *mimeinfo = (MimeInfo *) node->data;
1849 depth = g_node_depth(node);
1850 for (i = 0; i < depth; i++)
1852 printf("%s/%s (offset:%d length:%d encoding: %d)\n", typenames[mimeinfo->type], mimeinfo->subtype, mimeinfo->offset, mimeinfo->length, mimeinfo->encoding_type);
1857 static void output_mime_structure(MimeInfo *mimeinfo, int indent)
1859 g_node_traverse(mimeinfo->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1, output_func, NULL);
1862 MimeInfo *procmime_scan_file_with_offset(const gchar *filename, int offset)
1867 stat(filename, &buf);
1869 mimeinfo = procmime_mimeinfo_new();
1870 mimeinfo->content = MIMECONTENT_FILE;
1871 mimeinfo->encoding_type = ENC_UNKNOWN;
1872 mimeinfo->type = MIMETYPE_MESSAGE;
1873 mimeinfo->subtype = g_strdup("rfc822");
1874 mimeinfo->data.filename = g_strdup(filename);
1875 mimeinfo->offset = offset;
1876 mimeinfo->length = buf.st_size - offset;
1878 procmime_parse_message_rfc822(mimeinfo);
1879 if (debug_get_mode())
1880 output_mime_structure(mimeinfo, 0);
1885 MimeInfo *procmime_scan_file(const gchar *filename)
1889 g_return_val_if_fail(filename != NULL, NULL);
1891 mimeinfo = procmime_scan_file_with_offset(filename, 0);
1896 MimeInfo *procmime_scan_queue_file(const gchar *filename)
1900 gchar buf[BUFFSIZE];
1903 g_return_val_if_fail(filename != NULL, NULL);
1906 if ((fp = g_fopen(filename, "rb")) == NULL)
1908 /* Skip queue header */
1909 while (fgets(buf, sizeof(buf), fp) != NULL) {
1911 if ((!strncmp(buf, "X-Claws-End-Special-Headers: 1",
1912 strlen("X-Claws-End-Special-Headers:"))) ||
1913 (!strncmp(buf, "X-Sylpheed-End-Special-Headers: 1",
1914 strlen("X-Sylpheed-End-Special-Headers:"))))
1917 if (buf[0] == '\r' || buf[0] == '\n') break;
1918 /* from other mailers */
1919 if (!strncmp(buf, "Date: ", 6)
1920 || !strncmp(buf, "To: ", 4)
1921 || !strncmp(buf, "From: ", 6)
1922 || !strncmp(buf, "Subject: ", 9)) {
1930 mimeinfo = procmime_scan_file_with_offset(filename, offset);
1937 ENC_AS_QUOTED_STRING,
1942 typedef struct _ParametersData {
1948 static void write_parameters(gpointer key, gpointer value, gpointer user_data)
1951 gchar *val = value, *valpos, *tmp;
1952 ParametersData *pdata = (ParametersData *)user_data;
1953 GString *buf = g_string_new("");
1955 EncodeAs encas = ENC_AS_TOKEN;
1957 for (valpos = val; *valpos != 0; valpos++) {
1958 if (!IS_ASCII(*valpos) || *valpos == '"') {
1959 encas = ENC_AS_EXTENDED;
1964 if (((*valpos >= 0) && (*valpos < 037)) || (*valpos == 0177)) {
1965 encas = ENC_AS_QUOTED_STRING;
1969 /* tspecials + SPACE */
1987 encas = ENC_AS_QUOTED_STRING;
1992 if (encas == ENC_AS_EXTENDED && pdata->ascii_only == TRUE)
1993 encas = ENC_TO_ASCII;
1997 g_string_append_printf(buf, "%s=%s", param, val);
2001 tmp = g_strdup(val);
2003 " ()<>@,';:\\/[]?=.0123456789"
2004 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
2005 "abcdefghijklmnopqrstuvwxyz",
2007 g_string_append_printf(buf, "%s=\"%s\"", param, tmp);
2011 case ENC_AS_QUOTED_STRING:
2012 g_string_append_printf(buf, "%s=\"%s\"", param, val);
2015 case ENC_AS_EXTENDED:
2016 if (!g_utf8_validate(val, -1, NULL))
2017 g_string_append_printf(buf, "%s*=%s''", param,
2018 conv_get_locale_charset_str());
2020 g_string_append_printf(buf, "%s*=%s''", param,
2022 for (valpos = val; *valpos != '\0'; valpos++) {
2023 if (IS_ASCII(*valpos) && isalnum(*valpos)) {
2024 g_string_append_printf(buf, "%c", *valpos);
2026 gchar hexstr[3] = "XX";
2027 get_hex_str(hexstr, *valpos);
2028 g_string_append_printf(buf, "%%%s", hexstr);
2034 if (buf->str && strlen(buf->str)) {
2035 if (pdata->len + strlen(buf->str) + 2 > 76) {
2036 fprintf(pdata->fp, ";\n %s", buf->str);
2037 pdata->len = strlen(buf->str) + 1;
2039 fprintf(pdata->fp, "; %s", buf->str);
2040 pdata->len += strlen(buf->str) + 2;
2043 g_string_free(buf, TRUE);
2046 void procmime_write_mime_header(MimeInfo *mimeinfo, FILE *fp)
2048 struct TypeTable *type_table;
2049 ParametersData *pdata = g_new0(ParametersData, 1);
2050 debug_print("procmime_write_mime_header\n");
2053 pdata->ascii_only = FALSE;
2055 for (type_table = mime_type_table; type_table->str != NULL; type_table++)
2056 if (mimeinfo->type == type_table->type) {
2057 gchar *buf = g_strdup_printf(
2058 "Content-Type: %s/%s", type_table->str, mimeinfo->subtype);
2059 fprintf(fp, "%s", buf);
2060 pdata->len = strlen(buf);
2061 pdata->ascii_only = TRUE;
2065 g_hash_table_foreach(mimeinfo->typeparameters, write_parameters, pdata);
2070 if (mimeinfo->encoding_type != ENC_UNKNOWN)
2071 fprintf(fp, "Content-Transfer-Encoding: %s\n", procmime_get_encoding_str(mimeinfo->encoding_type));
2073 if (mimeinfo->description != NULL)
2074 fprintf(fp, "Content-Description: %s\n", mimeinfo->description);
2076 if (mimeinfo->id != NULL)
2077 fprintf(fp, "Content-ID: %s\n", mimeinfo->id);
2079 if (mimeinfo->location != NULL)
2080 fprintf(fp, "Content-Location: %s\n", mimeinfo->location);
2082 if (mimeinfo->disposition != DISPOSITIONTYPE_UNKNOWN) {
2083 ParametersData *pdata = g_new0(ParametersData, 1);
2085 if (mimeinfo->disposition == DISPOSITIONTYPE_INLINE)
2086 buf = g_strdup("Content-Disposition: inline");
2087 else if (mimeinfo->disposition == DISPOSITIONTYPE_ATTACHMENT)
2088 buf = g_strdup("Content-Disposition: attachment");
2090 buf = g_strdup("Content-Disposition: unknown");
2092 fprintf(fp, "%s", buf);
2093 pdata->len = strlen(buf);
2097 pdata->ascii_only = FALSE;
2099 g_hash_table_foreach(mimeinfo->dispositionparameters, write_parameters, pdata);
2107 gint procmime_write_message_rfc822(MimeInfo *mimeinfo, FILE *fp)
2112 gchar buf[BUFFSIZE];
2113 gboolean skip = FALSE;;
2115 debug_print("procmime_write_message_rfc822\n");
2118 switch (mimeinfo->content) {
2119 case MIMECONTENT_FILE:
2120 if ((infp = g_fopen(mimeinfo->data.filename, "rb")) == NULL) {
2121 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
2124 fseek(infp, mimeinfo->offset, SEEK_SET);
2125 while (fgets(buf, sizeof(buf), infp) == buf) {
2127 if (buf[0] == '\n' && buf[1] == '\0')
2129 if (skip && (buf[0] == ' ' || buf[0] == '\t'))
2131 if (g_ascii_strncasecmp(buf, "Mime-Version:", 13) == 0 ||
2132 g_ascii_strncasecmp(buf, "Content-Type:", 13) == 0 ||
2133 g_ascii_strncasecmp(buf, "Content-Transfer-Encoding:", 26) == 0 ||
2134 g_ascii_strncasecmp(buf, "Content-Description:", 20) == 0 ||
2135 g_ascii_strncasecmp(buf, "Content-ID:", 11) == 0 ||
2136 g_ascii_strncasecmp(buf, "Content-Location:", 17) == 0 ||
2137 g_ascii_strncasecmp(buf, "Content-Disposition:", 20) == 0) {
2141 fwrite(buf, sizeof(gchar), strlen(buf), fp);
2147 case MIMECONTENT_MEM:
2148 fwrite(mimeinfo->data.mem,
2150 strlen(mimeinfo->data.mem),
2158 childnode = mimeinfo->node->children;
2159 if (childnode == NULL)
2162 child = (MimeInfo *) childnode->data;
2163 fprintf(fp, "Mime-Version: 1.0\n");
2164 procmime_write_mime_header(child, fp);
2165 return procmime_write_mimeinfo(child, fp);
2168 gint procmime_write_multipart(MimeInfo *mimeinfo, FILE *fp)
2172 gchar *boundary, *str, *str2;
2173 gchar buf[BUFFSIZE];
2174 gboolean firstboundary;
2176 debug_print("procmime_write_multipart\n");
2178 boundary = g_hash_table_lookup(mimeinfo->typeparameters, "boundary");
2180 switch (mimeinfo->content) {
2181 case MIMECONTENT_FILE:
2182 if ((infp = g_fopen(mimeinfo->data.filename, "rb")) == NULL) {
2183 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
2186 fseek(infp, mimeinfo->offset, SEEK_SET);
2187 while (fgets(buf, sizeof(buf), infp) == buf) {
2188 if (IS_BOUNDARY(buf, boundary, strlen(boundary)))
2190 fwrite(buf, sizeof(gchar), strlen(buf), fp);
2195 case MIMECONTENT_MEM:
2196 str = g_strdup(mimeinfo->data.mem);
2197 if (((str2 = strstr(str, boundary)) != NULL) && ((str2 - str) >= 2) &&
2198 (*(str2 - 1) == '-') && (*(str2 - 2) == '-'))
2200 fwrite(str, sizeof(gchar), strlen(str), fp);
2208 childnode = mimeinfo->node->children;
2209 firstboundary = TRUE;
2210 while (childnode != NULL) {
2211 MimeInfo *child = childnode->data;
2214 firstboundary = FALSE;
2217 fprintf(fp, "--%s\n", boundary);
2219 procmime_write_mime_header(child, fp);
2220 if (procmime_write_mimeinfo(child, fp) < 0)
2223 childnode = g_node_next_sibling(childnode);
2225 fprintf(fp, "\n--%s--\n", boundary);
2230 gint procmime_write_mimeinfo(MimeInfo *mimeinfo, FILE *fp)
2234 debug_print("procmime_write_mimeinfo\n");
2236 if (G_NODE_IS_LEAF(mimeinfo->node)) {
2237 switch (mimeinfo->content) {
2238 case MIMECONTENT_FILE:
2239 if ((infp = g_fopen(mimeinfo->data.filename, "rb")) == NULL) {
2240 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
2243 copy_file_part_to_fp(infp, mimeinfo->offset, mimeinfo->length, fp);
2247 case MIMECONTENT_MEM:
2248 fwrite(mimeinfo->data.mem,
2250 strlen(mimeinfo->data.mem),
2258 /* Call writer for mime type */
2259 switch (mimeinfo->type) {
2260 case MIMETYPE_MESSAGE:
2261 if (g_ascii_strcasecmp(mimeinfo->subtype, "rfc822") == 0) {
2262 return procmime_write_message_rfc822(mimeinfo, fp);
2266 case MIMETYPE_MULTIPART:
2267 return procmime_write_multipart(mimeinfo, fp);
2279 gchar *procmime_get_part_file_name(MimeInfo *mimeinfo)
2283 if ((mimeinfo->type == MIMETYPE_TEXT) && !g_ascii_strcasecmp(mimeinfo->subtype, "html"))
2284 base = g_strdup("mimetmp.html");
2286 const gchar *basetmp;
2289 basetmp = procmime_mimeinfo_get_parameter(mimeinfo, "filename");
2290 if (basetmp == NULL)
2291 basetmp = procmime_mimeinfo_get_parameter(mimeinfo, "name");
2292 if (basetmp == NULL)
2293 basetmp = "mimetmp";
2294 basename = g_path_get_basename(basetmp);
2295 if (*basename == '\0') {
2297 basename = g_strdup("mimetmp");
2299 base = conv_filename_from_utf8(basename);
2301 subst_for_shellsafe_filename(base);