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>
41 #include "procheader.h"
43 #include "quoted-printable.h"
50 #include "prefs_common.h"
51 #include "prefs_gtk.h"
52 #include "alertpanel.h"
54 static GHashTable *procmime_get_mime_type_table (void);
56 MimeInfo *procmime_mimeinfo_new(void)
60 mimeinfo = g_new0(MimeInfo, 1);
61 mimeinfo->content = MIMECONTENT_EMPTY;
62 mimeinfo->data.filename = NULL;
64 mimeinfo->type = MIMETYPE_UNKNOWN;
65 mimeinfo->encoding_type = ENC_UNKNOWN;
66 mimeinfo->typeparameters = g_hash_table_new(g_str_hash, g_str_equal);
68 mimeinfo->disposition = DISPOSITIONTYPE_UNKNOWN;
69 mimeinfo->dispositionparameters
70 = g_hash_table_new(g_str_hash, g_str_equal);
72 mimeinfo->node = g_node_new(mimeinfo);
77 static gboolean procmime_mimeinfo_parameters_destroy(gpointer key, gpointer value, gpointer user_data)
85 static gchar *forced_charset = NULL;
87 void procmime_force_charset(const gchar *str)
89 g_free(forced_charset);
90 forced_charset = NULL;
92 forced_charset = g_strdup(str);
95 static EncodingType forced_encoding = 0;
97 void procmime_force_encoding(EncodingType encoding)
99 forced_encoding = encoding;
102 static gboolean free_func(GNode *node, gpointer data)
104 MimeInfo *mimeinfo = (MimeInfo *) node->data;
106 switch (mimeinfo->content) {
107 case MIMECONTENT_FILE:
109 g_unlink(mimeinfo->data.filename);
110 g_free(mimeinfo->data.filename);
113 case MIMECONTENT_MEM:
115 g_free(mimeinfo->data.mem);
120 g_free(mimeinfo->subtype);
121 g_free(mimeinfo->description);
122 g_free(mimeinfo->id);
123 g_free(mimeinfo->location);
125 g_hash_table_foreach_remove(mimeinfo->typeparameters,
126 procmime_mimeinfo_parameters_destroy, NULL);
127 g_hash_table_destroy(mimeinfo->typeparameters);
128 g_hash_table_foreach_remove(mimeinfo->dispositionparameters,
129 procmime_mimeinfo_parameters_destroy, NULL);
130 g_hash_table_destroy(mimeinfo->dispositionparameters);
132 if (mimeinfo->privacy)
133 privacy_free_privacydata(mimeinfo->privacy);
140 void procmime_mimeinfo_free_all(MimeInfo *mimeinfo)
147 node = mimeinfo->node;
148 g_node_traverse(node, G_IN_ORDER, G_TRAVERSE_ALL, -1, free_func, NULL);
150 g_node_destroy(node);
153 MimeInfo *procmime_mimeinfo_parent(MimeInfo *mimeinfo)
155 g_return_val_if_fail(mimeinfo != NULL, NULL);
156 g_return_val_if_fail(mimeinfo->node != NULL, NULL);
158 if (mimeinfo->node->parent == NULL)
160 return (MimeInfo *) mimeinfo->node->parent->data;
163 MimeInfo *procmime_mimeinfo_next(MimeInfo *mimeinfo)
165 g_return_val_if_fail(mimeinfo != NULL, NULL);
166 g_return_val_if_fail(mimeinfo->node != NULL, NULL);
168 if (mimeinfo->node->children)
169 return (MimeInfo *) mimeinfo->node->children->data;
170 if (mimeinfo->node->next)
171 return (MimeInfo *) mimeinfo->node->next->data;
173 if (mimeinfo->node->parent == NULL)
176 while (mimeinfo->node->parent != NULL) {
177 mimeinfo = (MimeInfo *) mimeinfo->node->parent->data;
178 if (mimeinfo->node->next)
179 return (MimeInfo *) mimeinfo->node->next->data;
185 MimeInfo *procmime_scan_message(MsgInfo *msginfo)
190 filename = procmsg_get_message_file_path(msginfo);
191 if (!filename || !is_file_exist(filename)) {
193 filename = procmsg_get_message_file(msginfo);
195 if (!filename || !is_file_exist(filename))
198 if (!folder_has_parent_of_type(msginfo->folder, F_QUEUE) &&
199 !folder_has_parent_of_type(msginfo->folder, F_DRAFT))
200 mimeinfo = procmime_scan_file(filename);
202 mimeinfo = procmime_scan_queue_file(filename);
210 H_CONTENT_TRANSFER_ENCODING = 0,
212 H_CONTENT_DISPOSITION = 2,
213 H_CONTENT_DESCRIPTION = 3,
217 const gchar *procmime_mimeinfo_get_parameter(MimeInfo *mimeinfo, const gchar *name)
221 g_return_val_if_fail(mimeinfo != NULL, NULL);
222 g_return_val_if_fail(name != NULL, NULL);
224 value = g_hash_table_lookup(mimeinfo->dispositionparameters, name);
226 value = g_hash_table_lookup(mimeinfo->typeparameters, name);
231 #define FLUSH_LASTLINE() { \
232 if (*lastline != '\0') { \
234 strretchomp(lastline); \
235 llen = strlen(lastline); \
236 if (lastline[llen-1] == ' ' && strcmp(lastline,"-- ")) { \
237 /* this is flowed */ \
239 lastline[llen-1] = '\0'; \
240 fputs(lastline, outfp); \
242 fputs(lastline, outfp); \
243 fputs("\n", outfp); \
246 strcpy(lastline, buf); \
249 gboolean procmime_decode_content(MimeInfo *mimeinfo)
256 gboolean tmp_file = FALSE;
257 gboolean flowed = FALSE;
258 gboolean delsp = FALSE;
259 EncodingType encoding = forced_encoding
261 : mimeinfo->encoding_type;
262 gchar lastline[BUFFSIZE];
263 memset(lastline, 0, BUFFSIZE);
265 g_return_val_if_fail(mimeinfo != NULL, FALSE);
267 if (prefs_common.respect_flowed_format &&
268 mimeinfo->type == MIMETYPE_TEXT &&
269 !strcasecmp(mimeinfo->subtype, "plain")) {
270 if (procmime_mimeinfo_get_parameter(mimeinfo, "format") != NULL &&
271 !strcasecmp(procmime_mimeinfo_get_parameter(mimeinfo, "format"),"flowed"))
274 procmime_mimeinfo_get_parameter(mimeinfo, "delsp") != NULL &&
275 !strcasecmp(procmime_mimeinfo_get_parameter(mimeinfo, "delsp"),"yes"))
280 encoding == ENC_UNKNOWN ||
281 encoding == ENC_BINARY ||
282 encoding == ENC_7BIT ||
287 infp = g_fopen(mimeinfo->data.filename, "rb");
292 fseek(infp, mimeinfo->offset, SEEK_SET);
294 outfp = get_tmpfile_in_dir(get_mime_tmp_dir(), &tmpfilename);
301 readend = mimeinfo->offset + mimeinfo->length;
303 if (encoding == ENC_QUOTED_PRINTABLE) {
304 while ((ftell(infp) < readend) && (fgets(buf, sizeof(buf), infp) != NULL)) {
306 len = qp_decode_line(buf);
309 fwrite(buf, 1, len, outfp);
316 } else if (encoding == ENC_BASE64) {
317 gchar outbuf[BUFFSIZE];
319 Base64Decoder *decoder;
320 gboolean got_error = FALSE;
321 gboolean uncanonicalize = FALSE;
324 if (mimeinfo->type == MIMETYPE_TEXT ||
325 mimeinfo->type == MIMETYPE_MESSAGE) {
326 uncanonicalize = TRUE;
327 tmpfp = my_tmpfile();
330 if (tmp_file) fclose(outfp);
336 decoder = base64_decoder_new();
337 while ((ftell(infp) < readend) && (fgets(buf, sizeof(buf), infp) != NULL)) {
338 len = base64_decoder_decode(decoder, buf, outbuf);
339 if (len < 0 && !got_error) {
340 g_warning("Bad BASE64 content.\n");
341 fwrite(_("[Error decoding BASE64]\n"),
343 strlen(_("[Error decoding BASE64]\n")),
347 } else if (len >= 0) {
348 /* print out the error message only once
350 fwrite(outbuf, sizeof(gchar), len, tmpfp);
354 base64_decoder_free(decoder);
356 if (uncanonicalize) {
358 while (fgets(buf, sizeof(buf), tmpfp) != NULL) {
364 } else if (encoding == ENC_X_UUENCODE) {
365 gchar outbuf[BUFFSIZE];
367 gboolean flag = FALSE;
369 while ((ftell(infp) < readend) && (fgets(buf, sizeof(buf), infp) != NULL)) {
370 if (!flag && strncmp(buf,"begin ", 6)) continue;
373 len = fromuutobits(outbuf, buf);
376 g_warning("Bad UUENCODE content(%d)\n", len);
379 fwrite(outbuf, sizeof(gchar), len, outfp);
384 while ((ftell(infp) < readend) && (fgets(buf, sizeof(buf), infp) != NULL)) {
398 stat(tmpfilename, &statbuf);
399 if (mimeinfo->tmp && (mimeinfo->data.filename != NULL))
400 g_unlink(mimeinfo->data.filename);
401 g_free(mimeinfo->data.filename);
402 mimeinfo->data.filename = tmpfilename;
403 mimeinfo->tmp = TRUE;
404 mimeinfo->offset = 0;
405 mimeinfo->length = statbuf.st_size;
406 mimeinfo->encoding_type = ENC_BINARY;
411 #define B64_LINE_SIZE 57
412 #define B64_BUFFSIZE 77
414 gboolean procmime_encode_content(MimeInfo *mimeinfo, EncodingType encoding)
416 FILE *infp = NULL, *outfp;
421 if (mimeinfo->content == MIMECONTENT_EMPTY)
424 if (mimeinfo->encoding_type != ENC_UNKNOWN &&
425 mimeinfo->encoding_type != ENC_BINARY &&
426 mimeinfo->encoding_type != ENC_7BIT &&
427 mimeinfo->encoding_type != ENC_8BIT)
428 if(!procmime_decode_content(mimeinfo))
431 outfp = get_tmpfile_in_dir(get_mime_tmp_dir(), &tmpfilename);
437 if (mimeinfo->content == MIMECONTENT_FILE) {
438 if ((infp = g_fopen(mimeinfo->data.filename, "rb")) == NULL) {
439 g_warning("Can't open file %s\n", mimeinfo->data.filename);
442 } else if (mimeinfo->content == MIMECONTENT_MEM) {
443 infp = str_open_as_stream(mimeinfo->data.mem);
448 if (encoding == ENC_BASE64) {
449 gchar inbuf[B64_LINE_SIZE], outbuf[B64_BUFFSIZE];
451 gchar *tmp_file = NULL;
453 if (mimeinfo->type == MIMETYPE_TEXT ||
454 mimeinfo->type == MIMETYPE_MESSAGE) {
455 if (mimeinfo->content == MIMECONTENT_FILE) {
456 tmp_file = get_tmp_file();
457 if (canonicalize_file(mimeinfo->data.filename, tmp_file) < 0) {
462 if ((tmp_fp = g_fopen(tmp_file, "rb")) == NULL) {
463 FILE_OP_ERROR(tmp_file, "fopen");
470 gchar *out = canonicalize_str(mimeinfo->data.mem);
472 infp = str_open_as_stream(out);
480 while ((len = fread(inbuf, sizeof(gchar),
481 B64_LINE_SIZE, tmp_fp))
483 base64_encode(outbuf, inbuf, B64_LINE_SIZE);
484 fputs(outbuf, outfp);
487 if (len > 0 && feof(tmp_fp)) {
488 base64_encode(outbuf, inbuf, len);
489 fputs(outbuf, outfp);
498 } else if (encoding == ENC_QUOTED_PRINTABLE) {
499 gchar inbuf[BUFFSIZE], outbuf[BUFFSIZE * 4];
501 while (fgets(inbuf, sizeof(inbuf), infp) != NULL) {
502 qp_encode_line(outbuf, inbuf);
504 if (!strncmp("From ", outbuf, sizeof("From ")-1)) {
505 gchar *tmpbuf = outbuf;
507 tmpbuf += sizeof("From ")-1;
509 fputs("=46rom ", outfp);
510 fputs(tmpbuf, outfp);
512 fputs(outbuf, outfp);
517 while (fgets(buf, sizeof(buf), infp) != NULL) {
526 if (mimeinfo->content == MIMECONTENT_FILE) {
527 if (mimeinfo->tmp && (mimeinfo->data.filename != NULL))
528 g_unlink(mimeinfo->data.filename);
529 g_free(mimeinfo->data.filename);
530 } else if (mimeinfo->content == MIMECONTENT_MEM) {
531 if (mimeinfo->tmp && (mimeinfo->data.mem != NULL))
532 g_free(mimeinfo->data.mem);
535 stat(tmpfilename, &statbuf);
536 mimeinfo->content = MIMECONTENT_FILE;
537 mimeinfo->data.filename = tmpfilename;
538 mimeinfo->tmp = TRUE;
539 mimeinfo->offset = 0;
540 mimeinfo->length = statbuf.st_size;
541 mimeinfo->encoding_type = encoding;
546 gint procmime_get_part(const gchar *outfile, MimeInfo *mimeinfo)
550 gint restlength, readlength;
551 gint saved_errno = 0;
553 g_return_val_if_fail(outfile != NULL, -1);
554 g_return_val_if_fail(mimeinfo != NULL, -1);
556 if (mimeinfo->encoding_type != ENC_BINARY && !procmime_decode_content(mimeinfo))
559 if ((infp = g_fopen(mimeinfo->data.filename, "rb")) == NULL) {
561 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
562 return -(saved_errno);
564 if (fseek(infp, mimeinfo->offset, SEEK_SET) < 0) {
566 FILE_OP_ERROR(mimeinfo->data.filename, "fseek");
568 return -(saved_errno);
570 if ((outfp = g_fopen(outfile, "wb")) == NULL) {
572 FILE_OP_ERROR(outfile, "fopen");
574 return -(saved_errno);
577 restlength = mimeinfo->length;
579 while ((restlength > 0) && ((readlength = fread(buf, 1, restlength > BUFFSIZE ? BUFFSIZE : restlength, infp)) > 0)) {
580 if (fwrite(buf, 1, readlength, outfp) != readlength) {
583 return -(saved_errno);
585 restlength -= readlength;
589 if (fclose(outfp) == EOF) {
591 FILE_OP_ERROR(outfile, "fclose");
593 return -(saved_errno);
599 struct ContentRenderer {
604 static GList * renderer_list = NULL;
606 static struct ContentRenderer *
607 content_renderer_new(char * content_type, char * renderer)
609 struct ContentRenderer * cr;
611 cr = g_new(struct ContentRenderer, 1);
615 cr->content_type = g_strdup(content_type);
616 cr->renderer = g_strdup(renderer);
621 static void content_renderer_free(struct ContentRenderer * cr)
623 g_free(cr->content_type);
624 g_free(cr->renderer);
628 void renderer_read_config(void)
634 g_list_foreach(renderer_list, (GFunc) content_renderer_free, NULL);
635 renderer_list = NULL;
637 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, RENDERER_RC, NULL);
638 f = g_fopen(rcpath, "rb");
644 while (fgets(buf, BUFFSIZE, f)) {
646 struct ContentRenderer * cr;
649 p = strchr(buf, ' ');
654 cr = content_renderer_new(buf, p + 1);
658 renderer_list = g_list_append(renderer_list, cr);
664 void renderer_write_config(void)
670 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, RENDERER_RC, NULL);
672 if ((pfile = prefs_write_open(rcpath)) == NULL) {
673 g_warning("failed to write configuration to file\n");
680 for (cur = renderer_list ; cur != NULL ; cur = cur->next) {
681 struct ContentRenderer * renderer;
682 renderer = cur->data;
683 fprintf(pfile->fp, "%s %s\n", renderer->content_type,
687 if (prefs_file_close(pfile) < 0) {
688 g_warning("failed to write configuration to file\n");
693 FILE *procmime_get_text_content(MimeInfo *mimeinfo)
696 const gchar *src_codeset;
697 gboolean conv_fail = FALSE;
700 struct ContentRenderer * renderer;
702 gchar *tmpfile, *content_type;
704 g_return_val_if_fail(mimeinfo != NULL, NULL);
706 if (!procmime_decode_content(mimeinfo))
709 tmpfile = procmime_get_tmp_file_name(mimeinfo);
713 if (procmime_get_part(tmpfile, mimeinfo) < 0) {
718 tmpfp = g_fopen(tmpfile, "rb");
724 if ((outfp = my_tmpfile()) == NULL) {
731 src_codeset = forced_charset
733 procmime_mimeinfo_get_parameter(mimeinfo, "charset");
737 content_type = procmime_get_content_type_str(mimeinfo->type,
739 for (cur = renderer_list ; cur != NULL ; cur = cur->next) {
740 struct ContentRenderer * cr;
743 if (g_ascii_strcasecmp(cr->content_type, content_type) == 0) {
748 g_free(content_type);
750 if (renderer != NULL) {
756 dup2(fileno(outfp), 1);
758 p = popen(renderer->renderer, "w");
763 fread(buf, sizeof(char), sizeof(buf),
765 fwrite(buf, sizeof(char), count, p);
770 /* CodeConverter seems to have no effect here */
771 } else if (mimeinfo->type == MIMETYPE_TEXT && !g_ascii_strcasecmp(mimeinfo->subtype, "html")) {
772 SC_HTMLParser *parser;
775 conv = conv_code_converter_new(src_codeset);
776 parser = sc_html_parser_new(tmpfp, conv);
777 while ((str = sc_html_parse(parser)) != NULL) {
780 sc_html_parser_destroy(parser);
781 conv_code_converter_destroy(conv);
782 } else if (mimeinfo->type == MIMETYPE_TEXT && !g_ascii_strcasecmp(mimeinfo->subtype, "enriched")) {
786 conv = conv_code_converter_new(src_codeset);
787 parser = ertf_parser_new(tmpfp, conv);
788 while ((str = ertf_parse(parser)) != NULL) {
791 ertf_parser_destroy(parser);
792 conv_code_converter_destroy(conv);
793 } else if (mimeinfo->type == MIMETYPE_TEXT) {
794 while (fgets(buf, sizeof(buf), tmpfp) != NULL) {
795 str = conv_codeset_strdup(buf, src_codeset, CS_UTF_8);
807 g_warning("procmime_get_text_content(): Code conversion failed.\n");
817 /* search the first text part of (multipart) MIME message,
818 decode, convert it and output to outfp. */
819 FILE *procmime_get_first_text_content(MsgInfo *msginfo)
822 MimeInfo *mimeinfo, *partinfo;
824 g_return_val_if_fail(msginfo != NULL, NULL);
826 mimeinfo = procmime_scan_message(msginfo);
827 if (!mimeinfo) return NULL;
830 while (partinfo && partinfo->type != MIMETYPE_TEXT) {
831 partinfo = procmime_mimeinfo_next(partinfo);
834 outfp = procmime_get_text_content(partinfo);
836 procmime_mimeinfo_free_all(mimeinfo);
842 static gboolean find_encrypted_func(GNode *node, gpointer data)
844 MimeInfo *mimeinfo = (MimeInfo *) node->data;
845 MimeInfo **encinfo = (MimeInfo **) data;
847 if (privacy_mimeinfo_is_encrypted(mimeinfo)) {
855 static MimeInfo *find_encrypted_part(MimeInfo *rootinfo)
857 MimeInfo *encinfo = NULL;
859 g_node_traverse(rootinfo->node, G_IN_ORDER, G_TRAVERSE_ALL, -1,
860 find_encrypted_func, &encinfo);
865 /* search the first encrypted text part of (multipart) MIME message,
866 decode, convert it and output to outfp. */
867 FILE *procmime_get_first_encrypted_text_content(MsgInfo *msginfo)
870 MimeInfo *mimeinfo, *partinfo, *encinfo;
872 g_return_val_if_fail(msginfo != NULL, NULL);
874 mimeinfo = procmime_scan_message(msginfo);
880 if ((encinfo = find_encrypted_part(partinfo)) != NULL) {
881 debug_print("decrypting message part\n");
882 if (privacy_mimeinfo_decrypt(encinfo) < 0) {
883 alertpanel_error(_("Couldn't decrypt: %s"),
884 privacy_get_error());
889 while (partinfo && partinfo->type != MIMETYPE_TEXT) {
890 partinfo = procmime_mimeinfo_next(partinfo);
894 outfp = procmime_get_text_content(partinfo);
896 procmime_mimeinfo_free_all(mimeinfo);
901 gboolean procmime_msginfo_is_encrypted(MsgInfo *msginfo)
903 MimeInfo *mimeinfo, *partinfo;
904 gboolean result = FALSE;
906 g_return_val_if_fail(msginfo != NULL, FALSE);
908 mimeinfo = procmime_scan_message(msginfo);
914 result = (find_encrypted_part(partinfo) != NULL);
915 procmime_mimeinfo_free_all(mimeinfo);
920 gboolean procmime_find_string_part(MimeInfo *mimeinfo, const gchar *filename,
921 const gchar *str, StrFindFunc find_func)
926 g_return_val_if_fail(mimeinfo != NULL, FALSE);
927 g_return_val_if_fail(mimeinfo->type == MIMETYPE_TEXT, FALSE);
928 g_return_val_if_fail(str != NULL, FALSE);
929 g_return_val_if_fail(find_func != NULL, FALSE);
931 outfp = procmime_get_text_content(mimeinfo);
936 while (fgets(buf, sizeof(buf), outfp) != NULL) {
938 if (find_func(buf, str)) {
949 gboolean procmime_find_string(MsgInfo *msginfo, const gchar *str,
950 StrFindFunc find_func)
955 gboolean found = FALSE;
957 g_return_val_if_fail(msginfo != NULL, FALSE);
958 g_return_val_if_fail(str != NULL, FALSE);
959 g_return_val_if_fail(find_func != NULL, FALSE);
961 filename = procmsg_get_message_file(msginfo);
962 if (!filename) return FALSE;
963 mimeinfo = procmime_scan_message(msginfo);
965 for (partinfo = mimeinfo; partinfo != NULL;
966 partinfo = procmime_mimeinfo_next(partinfo)) {
967 if (partinfo->type == MIMETYPE_TEXT) {
968 if (procmime_find_string_part
969 (partinfo, filename, str, find_func) == TRUE) {
976 procmime_mimeinfo_free_all(mimeinfo);
982 gchar *procmime_get_tmp_file_name(MimeInfo *mimeinfo)
984 static guint32 id = 0;
989 g_return_val_if_fail(mimeinfo != NULL, NULL);
991 g_snprintf(f_prefix, sizeof(f_prefix), "%08x.", id++);
993 if ((mimeinfo->type == MIMETYPE_TEXT) && !g_ascii_strcasecmp(mimeinfo->subtype, "html"))
994 base = g_strdup("mimetmp.html");
996 const gchar *basetmp;
998 basetmp = procmime_mimeinfo_get_parameter(mimeinfo, "filename");
1000 basetmp = procmime_mimeinfo_get_parameter(mimeinfo, "name");
1001 if (basetmp == NULL)
1002 basetmp = "mimetmp";
1003 basetmp = g_path_get_basename(basetmp);
1004 if (*basetmp == '\0')
1005 basetmp = g_strdup("mimetmp");
1006 base = conv_filename_from_utf8(basetmp);
1007 g_free((gchar*)basetmp);
1008 subst_for_shellsafe_filename(base);
1011 filename = g_strconcat(get_mime_tmp_dir(), G_DIR_SEPARATOR_S,
1012 f_prefix, base, NULL);
1019 static GList *mime_type_list = NULL;
1021 gchar *procmime_get_mime_type(const gchar *filename)
1023 static GHashTable *mime_type_table = NULL;
1024 MimeType *mime_type;
1029 if (!mime_type_table) {
1030 mime_type_table = procmime_get_mime_type_table();
1031 if (!mime_type_table) return NULL;
1034 base = g_path_get_basename(filename);
1035 if ((p = strrchr(base, '.')) != NULL)
1036 Xstrdup_a(ext, p + 1, p = NULL );
1038 if (!p) return NULL;
1041 mime_type = g_hash_table_lookup(mime_type_table, ext);
1045 str = g_strconcat(mime_type->type, "/", mime_type->sub_type,
1053 static guint procmime_str_hash(gconstpointer gptr)
1055 guint hash_result = 0;
1058 for (str = gptr; str && *str; str++) {
1059 if (isupper(*str)) hash_result += (*str + ' ');
1060 else hash_result += *str;
1066 static gint procmime_str_equal(gconstpointer gptr1, gconstpointer gptr2)
1068 const char *str1 = gptr1;
1069 const char *str2 = gptr2;
1071 return !g_utf8_collate(str1, str2);
1074 static GHashTable *procmime_get_mime_type_table(void)
1076 GHashTable *table = NULL;
1078 MimeType *mime_type;
1081 if (!mime_type_list) {
1082 mime_type_list = procmime_get_mime_type_list();
1083 if (!mime_type_list) return NULL;
1086 table = g_hash_table_new(procmime_str_hash, procmime_str_equal);
1088 for (cur = mime_type_list; cur != NULL; cur = cur->next) {
1092 mime_type = (MimeType *)cur->data;
1094 if (!mime_type->extension) continue;
1096 exts = g_strsplit(mime_type->extension, " ", 16);
1097 for (i = 0; exts[i] != NULL; i++) {
1098 /* make the key case insensitive */
1100 /* use previously dup'd key on overwriting */
1101 if (g_hash_table_lookup(table, exts[i]))
1104 key = g_strdup(exts[i]);
1105 g_hash_table_insert(table, key, mime_type);
1113 GList *procmime_get_mime_type_list(void)
1117 gchar buf[BUFFSIZE];
1120 MimeType *mime_type;
1121 gboolean fp_is_glob_file = TRUE;
1124 return mime_type_list;
1126 #if defined(__NetBSD__)
1127 if ((fp = g_fopen(DATAROOTDIR "/mime/globs", "rb")) == NULL)
1129 if ((fp = g_fopen("/usr/share/mime/globs", "rb")) == NULL)
1132 fp_is_glob_file = FALSE;
1133 if ((fp = g_fopen("/etc/mime.types", "rb")) == NULL) {
1134 if ((fp = g_fopen(SYSCONFDIR "/mime.types", "rb"))
1136 FILE_OP_ERROR(SYSCONFDIR "/mime.types",
1143 while (fgets(buf, sizeof(buf), fp) != NULL) {
1144 p = strchr(buf, '#');
1150 if (fp_is_glob_file) {
1151 while (*p && !g_ascii_isspace(*p) && (*p!=':')) p++;
1153 while (*p && !g_ascii_isspace(*p)) p++;
1160 delim = strchr(buf, '/');
1161 if (delim == NULL) continue;
1164 mime_type = g_new(MimeType, 1);
1165 mime_type->type = g_strdup(buf);
1166 mime_type->sub_type = g_strdup(delim + 1);
1168 if (fp_is_glob_file) {
1169 while (*p && (g_ascii_isspace(*p)||(*p=='*')||(*p=='.'))) p++;
1171 while (*p && g_ascii_isspace(*p)) p++;
1175 mime_type->extension = g_strdup(p);
1177 mime_type->extension = NULL;
1179 list = g_list_append(list, mime_type);
1185 g_warning("Can't read mime.types\n");
1190 EncodingType procmime_get_encoding_for_charset(const gchar *charset)
1194 else if (!g_ascii_strncasecmp(charset, "ISO-2022-", 9) ||
1195 !g_ascii_strcasecmp(charset, "US-ASCII"))
1197 else if (!g_ascii_strcasecmp(charset, "ISO-8859-5") ||
1198 !g_ascii_strncasecmp(charset, "KOI8-", 5) ||
1199 !g_ascii_strcasecmp(charset, "Windows-1251"))
1201 else if (!g_ascii_strncasecmp(charset, "ISO-8859-", 9))
1202 return ENC_QUOTED_PRINTABLE;
1203 else if (!g_ascii_strncasecmp(charset, "UTF-8", 5))
1204 return ENC_QUOTED_PRINTABLE;
1209 EncodingType procmime_get_encoding_for_text_file(const gchar *file, gboolean *has_binary)
1212 guchar buf[BUFFSIZE];
1214 size_t octet_chars = 0;
1215 size_t total_len = 0;
1216 gfloat octet_percentage;
1217 gboolean force_b64 = FALSE;
1219 if ((fp = g_fopen(file, "rb")) == NULL) {
1220 FILE_OP_ERROR(file, "fopen");
1224 while ((len = fread(buf, sizeof(guchar), sizeof(buf), fp)) > 0) {
1228 for (p = buf, i = 0; i < len; ++p, ++i) {
1242 octet_percentage = (gfloat)octet_chars / (gfloat)total_len;
1244 octet_percentage = 0.0;
1246 debug_print("procmime_get_encoding_for_text_file(): "
1247 "8bit chars: %d / %d (%f%%)\n", octet_chars, total_len,
1248 100.0 * octet_percentage);
1250 if (octet_percentage > 0.20 || force_b64) {
1251 debug_print("using BASE64\n");
1253 } else if (octet_chars > 0) {
1254 debug_print("using quoted-printable\n");
1255 return ENC_QUOTED_PRINTABLE;
1257 debug_print("using 7bit\n");
1262 struct EncodingTable
1265 EncodingType enc_type;
1268 struct EncodingTable encoding_table[] = {
1271 {"binary", ENC_BINARY},
1272 {"quoted-printable", ENC_QUOTED_PRINTABLE},
1273 {"base64", ENC_BASE64},
1274 {"x-uuencode", ENC_UNKNOWN},
1275 {NULL, ENC_UNKNOWN},
1278 const gchar *procmime_get_encoding_str(EncodingType encoding)
1280 struct EncodingTable *enc_table;
1282 for (enc_table = encoding_table; enc_table->str != NULL; enc_table++) {
1283 if (enc_table->enc_type == encoding)
1284 return enc_table->str;
1289 /* --- NEW MIME STUFF --- */
1296 static struct TypeTable mime_type_table[] = {
1297 {"text", MIMETYPE_TEXT},
1298 {"image", MIMETYPE_IMAGE},
1299 {"audio", MIMETYPE_AUDIO},
1300 {"video", MIMETYPE_VIDEO},
1301 {"application", MIMETYPE_APPLICATION},
1302 {"message", MIMETYPE_MESSAGE},
1303 {"multipart", MIMETYPE_MULTIPART},
1307 const gchar *procmime_get_media_type_str(MimeMediaType type)
1309 struct TypeTable *type_table;
1311 for (type_table = mime_type_table; type_table->str != NULL; type_table++) {
1312 if (type_table->type == type)
1313 return type_table->str;
1318 MimeMediaType procmime_get_media_type(const gchar *str)
1320 struct TypeTable *typetablearray;
1322 for (typetablearray = mime_type_table; typetablearray->str != NULL; typetablearray++)
1323 if (g_ascii_strncasecmp(str, typetablearray->str, strlen(typetablearray->str)) == 0)
1324 return typetablearray->type;
1326 return MIMETYPE_UNKNOWN;
1330 *\brief Safe wrapper for content type string.
1332 *\return const gchar * Pointer to content type string.
1334 gchar *procmime_get_content_type_str(MimeMediaType type,
1335 const char *subtype)
1337 const gchar *type_str = NULL;
1339 if (subtype == NULL || !(type_str = procmime_get_media_type_str(type)))
1340 return g_strdup("unknown");
1341 return g_strdup_printf("%s/%s", type_str, subtype);
1344 static int procmime_parse_mimepart(MimeInfo *parent,
1345 gchar *content_type,
1346 gchar *content_encoding,
1347 gchar *content_description,
1349 gchar *content_disposition,
1350 gchar *content_location,
1351 const gchar *filename,
1355 static void procmime_parse_message_rfc822(MimeInfo *mimeinfo)
1357 HeaderEntry hentry[] = {{"Content-Type:", NULL, TRUE},
1358 {"Content-Transfer-Encoding:",
1360 {"Content-Description:",
1364 {"Content-Disposition:",
1366 {"Content-Location:",
1370 {NULL, NULL, FALSE}};
1371 guint content_start, i;
1375 procmime_decode_content(mimeinfo);
1377 fp = g_fopen(mimeinfo->data.filename, "rb");
1379 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
1382 fseek(fp, mimeinfo->offset, SEEK_SET);
1383 procheader_get_header_fields(fp, hentry);
1384 if (hentry[0].body != NULL) {
1385 tmp = conv_unmime_header(hentry[0].body, NULL);
1386 g_free(hentry[0].body);
1387 hentry[0].body = tmp;
1389 if (hentry[2].body != NULL) {
1390 tmp = conv_unmime_header(hentry[2].body, NULL);
1391 g_free(hentry[2].body);
1392 hentry[2].body = tmp;
1394 if (hentry[4].body != NULL) {
1395 tmp = conv_unmime_header(hentry[4].body, NULL);
1396 g_free(hentry[4].body);
1397 hentry[4].body = tmp;
1399 if (hentry[5].body != NULL) {
1400 tmp = conv_unmime_header(hentry[5].body, NULL);
1401 g_free(hentry[5].body);
1402 hentry[5].body = tmp;
1404 content_start = ftell(fp);
1407 procmime_parse_mimepart(mimeinfo,
1408 hentry[0].body, hentry[1].body,
1409 hentry[2].body, hentry[3].body,
1410 hentry[4].body, hentry[5].body,
1411 mimeinfo->data.filename, content_start,
1412 mimeinfo->length - (content_start - mimeinfo->offset));
1414 for (i = 0; i < (sizeof hentry / sizeof hentry[0]); i++) {
1415 g_free(hentry[i].body);
1416 hentry[i].body = NULL;
1420 static void procmime_parse_multipart(MimeInfo *mimeinfo)
1422 HeaderEntry hentry[] = {{"Content-Type:", NULL, TRUE},
1423 {"Content-Transfer-Encoding:",
1425 {"Content-Description:",
1429 {"Content-Disposition:",
1431 {"Content-Location:",
1433 {NULL, NULL, FALSE}};
1436 gint boundary_len = 0, lastoffset = -1, i;
1437 gchar buf[BUFFSIZE];
1441 boundary = g_hash_table_lookup(mimeinfo->typeparameters, "boundary");
1444 boundary_len = strlen(boundary);
1446 procmime_decode_content(mimeinfo);
1448 fp = g_fopen(mimeinfo->data.filename, "rb");
1450 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
1453 fseek(fp, mimeinfo->offset, SEEK_SET);
1454 while ((p = fgets(buf, sizeof(buf), fp)) != NULL && result == 0) {
1455 if (ftell(fp) - 1 > (mimeinfo->offset + mimeinfo->length))
1458 if (IS_BOUNDARY(buf, boundary, boundary_len)) {
1459 if (lastoffset != -1) {
1460 result = procmime_parse_mimepart(mimeinfo,
1461 hentry[0].body, hentry[1].body,
1462 hentry[2].body, hentry[3].body,
1463 hentry[4].body, hentry[5].body,
1464 mimeinfo->data.filename, lastoffset,
1465 (ftell(fp) - strlen(buf)) - lastoffset - 1);
1468 if (buf[2 + boundary_len] == '-' &&
1469 buf[2 + boundary_len + 1] == '-')
1472 for (i = 0; i < (sizeof hentry / sizeof hentry[0]) ; i++) {
1473 g_free(hentry[i].body);
1474 hentry[i].body = NULL;
1476 procheader_get_header_fields(fp, hentry);
1477 if (hentry[0].body != NULL) {
1478 tmp = conv_unmime_header(hentry[0].body, NULL);
1479 g_free(hentry[0].body);
1480 hentry[0].body = tmp;
1482 if (hentry[2].body != NULL) {
1483 tmp = conv_unmime_header(hentry[2].body, NULL);
1484 g_free(hentry[2].body);
1485 hentry[2].body = tmp;
1487 if (hentry[4].body != NULL) {
1488 tmp = conv_unmime_header(hentry[4].body, NULL);
1489 g_free(hentry[4].body);
1490 hentry[4].body = tmp;
1492 if (hentry[5].body != NULL) {
1493 tmp = conv_unmime_header(hentry[5].body, NULL);
1494 g_free(hentry[5].body);
1495 hentry[5].body = tmp;
1497 lastoffset = ftell(fp);
1500 for (i = 0; i < (sizeof hentry / sizeof hentry[0]); i++) {
1501 g_free(hentry[i].body);
1502 hentry[i].body = NULL;
1507 static void parse_parameters(const gchar *parameters, GHashTable *table)
1509 gchar *params, *param, *next;
1510 GSList *convlist = NULL, *concatlist = NULL, *cur;
1512 params = g_strdup(parameters);
1515 for (; next != NULL; param = next) {
1516 gchar *attribute, *value, *tmp;
1518 gboolean convert = FALSE;
1520 next = strchr_with_skip_quote(param, '"', ';');
1529 value = strchr(attribute, '=');
1535 while (value[0] == ' ')
1538 g_strdown(attribute);
1540 len = strlen(attribute);
1541 if (attribute[len - 1] == '*') {
1542 gchar *srcpos, *dstpos, *endpos;
1545 attribute[len - 1] = '\0';
1549 endpos = value + strlen(value);
1550 while (srcpos < endpos) {
1556 if (!get_hex_value(&dstvalue, srcpos[1], srcpos[2]))
1567 if (value[0] == '"')
1568 extract_quote(value, '"');
1569 else if ((tmp = strchr(value, ' ')) != NULL)
1574 while (attribute[0] == ' ')
1576 while (attribute[strlen(attribute)-1] == ' ')
1577 attribute[strlen(attribute)-1] = '\0';
1580 while (value[0] == ' ')
1582 while (value[strlen(value)-1] == ' ')
1583 value[strlen(value)-1] = '\0';
1585 if (strrchr(attribute, '*') != NULL) {
1588 tmpattr = g_strdup(attribute);
1589 tmp = strrchr(tmpattr, '*');
1592 if ((tmp[1] == '0') && (tmp[2] == '\0') &&
1593 (g_slist_find_custom(concatlist, attribute, g_str_equal) == NULL))
1594 concatlist = g_slist_prepend(concatlist, g_strdup(tmpattr));
1596 if (convert && (g_slist_find_custom(convlist, attribute, g_str_equal) == NULL))
1597 convlist = g_slist_prepend(convlist, g_strdup(tmpattr));
1600 } else if (convert) {
1601 if (g_slist_find_custom(convlist, attribute, g_str_equal) == NULL)
1602 convlist = g_slist_prepend(convlist, g_strdup(attribute));
1605 if (g_hash_table_lookup(table, attribute) == NULL)
1606 g_hash_table_insert(table, g_strdup(attribute), g_strdup(value));
1609 for (cur = concatlist; cur != NULL; cur = g_slist_next(cur)) {
1610 gchar *attribute, *attrwnum, *partvalue;
1614 attribute = (gchar *) cur->data;
1615 value = g_string_sized_new(64);
1617 attrwnum = g_strdup_printf("%s*%d", attribute, n);
1618 while ((partvalue = g_hash_table_lookup(table, attrwnum)) != NULL) {
1619 g_string_append(value, partvalue);
1623 attrwnum = g_strdup_printf("%s*%d", attribute, n);
1627 g_hash_table_insert(table, g_strdup(attribute), g_strdup(value->str));
1628 g_string_free(value, TRUE);
1630 slist_free_strings(concatlist);
1631 g_slist_free(concatlist);
1633 for (cur = convlist; cur != NULL; cur = g_slist_next(cur)) {
1634 gchar *attribute, *key, *value;
1635 gchar *charset, *lang, *oldvalue, *newvalue;
1637 attribute = (gchar *) cur->data;
1638 if (!g_hash_table_lookup_extended(
1639 table, attribute, (gpointer *)(gchar *) &key, (gpointer *)(gchar *) &value))
1643 lang = strchr(charset, '\'');
1648 oldvalue = strchr(lang, '\'');
1649 if (oldvalue == NULL)
1654 newvalue = conv_codeset_strdup(oldvalue, charset, CS_UTF_8);
1656 g_hash_table_remove(table, attribute);
1660 g_hash_table_insert(table, g_strdup(attribute), newvalue);
1662 slist_free_strings(convlist);
1663 g_slist_free(convlist);
1668 static void procmime_parse_content_type(const gchar *content_type, MimeInfo *mimeinfo)
1670 g_return_if_fail(content_type != NULL);
1671 g_return_if_fail(mimeinfo != NULL);
1673 /* RFC 2045, page 13 says that the mime subtype is MANDATORY;
1674 * if it's not available we use the default Content-Type */
1675 if ((content_type[0] == '\0') || (strchr(content_type, '/') == NULL)) {
1676 mimeinfo->type = MIMETYPE_TEXT;
1677 mimeinfo->subtype = g_strdup("plain");
1678 if (g_hash_table_lookup(mimeinfo->typeparameters,
1679 "charset") == NULL) {
1680 g_hash_table_insert(mimeinfo->typeparameters,
1681 g_strdup("charset"),
1683 conv_get_locale_charset_str_no_utf8()));
1686 gchar *type, *subtype, *params;
1688 type = g_strdup(content_type);
1689 subtype = strchr(type, '/') + 1;
1690 *(subtype - 1) = '\0';
1691 if ((params = strchr(subtype, ';')) != NULL) {
1696 mimeinfo->type = procmime_get_media_type(type);
1697 mimeinfo->subtype = g_strdup(subtype);
1699 /* Get mimeinfo->typeparameters */
1701 parse_parameters(params, mimeinfo->typeparameters);
1707 static void procmime_parse_content_disposition(const gchar *content_disposition, MimeInfo *mimeinfo)
1709 gchar *tmp, *params;
1711 g_return_if_fail(content_disposition != NULL);
1712 g_return_if_fail(mimeinfo != NULL);
1714 tmp = g_strdup(content_disposition);
1715 if ((params = strchr(tmp, ';')) != NULL) {
1721 if (!g_ascii_strcasecmp(tmp, "inline"))
1722 mimeinfo->disposition = DISPOSITIONTYPE_INLINE;
1723 else if (!g_ascii_strcasecmp(tmp, "attachment"))
1724 mimeinfo->disposition = DISPOSITIONTYPE_ATTACHMENT;
1726 mimeinfo->disposition = DISPOSITIONTYPE_ATTACHMENT;
1729 parse_parameters(params, mimeinfo->dispositionparameters);
1735 static void procmime_parse_content_encoding(const gchar *content_encoding, MimeInfo *mimeinfo)
1737 struct EncodingTable *enc_table;
1739 for (enc_table = encoding_table; enc_table->str != NULL; enc_table++) {
1740 if (g_ascii_strcasecmp(enc_table->str, content_encoding) == 0) {
1741 mimeinfo->encoding_type = enc_table->enc_type;
1745 mimeinfo->encoding_type = ENC_UNKNOWN;
1749 static int procmime_parse_mimepart(MimeInfo *parent,
1750 gchar *content_type,
1751 gchar *content_encoding,
1752 gchar *content_description,
1754 gchar *content_disposition,
1755 gchar *content_location,
1756 const gchar *filename,
1762 /* Create MimeInfo */
1763 mimeinfo = procmime_mimeinfo_new();
1764 mimeinfo->content = MIMECONTENT_FILE;
1765 if (parent != NULL) {
1766 if (g_node_depth(parent->node) > 32) {
1767 /* 32 is an arbitrary value
1768 * this avoids DOSsing ourselves
1769 * with enormous messages
1771 procmime_mimeinfo_free_all(mimeinfo);
1774 g_node_append(parent->node, mimeinfo->node);
1776 mimeinfo->data.filename = g_strdup(filename);
1777 mimeinfo->offset = offset;
1778 mimeinfo->length = length;
1780 if (content_type != NULL) {
1781 procmime_parse_content_type(content_type, mimeinfo);
1783 mimeinfo->type = MIMETYPE_TEXT;
1784 mimeinfo->subtype = g_strdup("plain");
1785 if (g_hash_table_lookup(mimeinfo->typeparameters,
1786 "charset") == NULL) {
1787 g_hash_table_insert(mimeinfo->typeparameters,
1788 g_strdup("charset"),
1790 conv_get_locale_charset_str_no_utf8()));
1794 if (content_encoding != NULL) {
1795 procmime_parse_content_encoding(content_encoding, mimeinfo);
1797 mimeinfo->encoding_type = ENC_UNKNOWN;
1800 if (content_description != NULL)
1801 mimeinfo->description = g_strdup(content_description);
1803 mimeinfo->description = NULL;
1805 if (content_id != NULL)
1806 mimeinfo->id = g_strdup(content_id);
1808 mimeinfo->id = NULL;
1810 if (content_location != NULL)
1811 mimeinfo->location = g_strdup(content_location);
1813 mimeinfo->location = NULL;
1815 if (content_disposition != NULL)
1816 procmime_parse_content_disposition(content_disposition, mimeinfo);
1818 mimeinfo->disposition = DISPOSITIONTYPE_UNKNOWN;
1820 /* Call parser for mime type */
1821 switch (mimeinfo->type) {
1822 case MIMETYPE_MESSAGE:
1823 if (g_ascii_strcasecmp(mimeinfo->subtype, "rfc822") == 0) {
1824 procmime_parse_message_rfc822(mimeinfo);
1828 case MIMETYPE_MULTIPART:
1829 procmime_parse_multipart(mimeinfo);
1839 static gchar *typenames[] = {
1850 static gboolean output_func(GNode *node, gpointer data)
1853 MimeInfo *mimeinfo = (MimeInfo *) node->data;
1855 depth = g_node_depth(node);
1856 for (i = 0; i < depth; i++)
1858 printf("%s/%s (offset:%d length:%d encoding: %d)\n", typenames[mimeinfo->type], mimeinfo->subtype, mimeinfo->offset, mimeinfo->length, mimeinfo->encoding_type);
1863 static void output_mime_structure(MimeInfo *mimeinfo, int indent)
1865 g_node_traverse(mimeinfo->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1, output_func, NULL);
1868 static MimeInfo *procmime_scan_file_with_offset(const gchar *filename, int offset)
1873 stat(filename, &buf);
1875 mimeinfo = procmime_mimeinfo_new();
1876 mimeinfo->content = MIMECONTENT_FILE;
1877 mimeinfo->encoding_type = ENC_UNKNOWN;
1878 mimeinfo->type = MIMETYPE_MESSAGE;
1879 mimeinfo->subtype = g_strdup("rfc822");
1880 mimeinfo->data.filename = g_strdup(filename);
1881 mimeinfo->offset = offset;
1882 mimeinfo->length = buf.st_size - offset;
1884 procmime_parse_message_rfc822(mimeinfo);
1885 if (debug_get_mode())
1886 output_mime_structure(mimeinfo, 0);
1891 MimeInfo *procmime_scan_file(const gchar *filename)
1895 g_return_val_if_fail(filename != NULL, NULL);
1897 mimeinfo = procmime_scan_file_with_offset(filename, 0);
1902 MimeInfo *procmime_scan_queue_file(const gchar *filename)
1906 gchar buf[BUFFSIZE];
1909 g_return_val_if_fail(filename != NULL, NULL);
1912 if ((fp = g_fopen(filename, "rb")) == NULL)
1914 /* Skip queue header */
1915 while (fgets(buf, sizeof(buf), fp) != NULL) {
1917 if ((!strncmp(buf, "X-Claws-End-Special-Headers: 1",
1918 strlen("X-Claws-End-Special-Headers:"))) ||
1919 (!strncmp(buf, "X-Sylpheed-End-Special-Headers: 1",
1920 strlen("X-Sylpheed-End-Special-Headers:"))))
1923 if (buf[0] == '\r' || buf[0] == '\n') break;
1924 /* from other mailers */
1925 if (!strncmp(buf, "Date: ", 6)
1926 || !strncmp(buf, "To: ", 4)
1927 || !strncmp(buf, "From: ", 6)
1928 || !strncmp(buf, "Subject: ", 9)) {
1936 mimeinfo = procmime_scan_file_with_offset(filename, offset);
1943 ENC_AS_QUOTED_STRING,
1948 typedef struct _ParametersData {
1954 static void write_parameters(gpointer key, gpointer value, gpointer user_data)
1957 gchar *val = value, *valpos, *tmp;
1958 ParametersData *pdata = (ParametersData *)user_data;
1959 GString *buf = g_string_new("");
1961 EncodeAs encas = ENC_AS_TOKEN;
1963 for (valpos = val; *valpos != 0; valpos++) {
1964 if (!IS_ASCII(*valpos) || *valpos == '"') {
1965 encas = ENC_AS_EXTENDED;
1970 if (((*valpos >= 0) && (*valpos < 037)) || (*valpos == 0177)) {
1971 encas = ENC_AS_QUOTED_STRING;
1975 /* tspecials + SPACE */
1993 encas = ENC_AS_QUOTED_STRING;
1998 if (encas == ENC_AS_EXTENDED && pdata->ascii_only == TRUE)
1999 encas = ENC_TO_ASCII;
2003 g_string_append_printf(buf, "%s=%s", param, val);
2007 tmp = g_strdup(val);
2009 " ()<>@,';:\\/[]?=.0123456789"
2010 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
2011 "abcdefghijklmnopqrstuvwxyz",
2013 g_string_append_printf(buf, "%s=\"%s\"", param, tmp);
2017 case ENC_AS_QUOTED_STRING:
2018 g_string_append_printf(buf, "%s=\"%s\"", param, val);
2021 case ENC_AS_EXTENDED:
2022 if (!g_utf8_validate(val, -1, NULL))
2023 g_string_append_printf(buf, "%s*=%s''", param,
2024 conv_get_locale_charset_str());
2026 g_string_append_printf(buf, "%s*=%s''", param,
2028 for (valpos = val; *valpos != '\0'; valpos++) {
2029 if (IS_ASCII(*valpos) && isalnum(*valpos)) {
2030 g_string_append_printf(buf, "%c", *valpos);
2032 gchar hexstr[3] = "XX";
2033 get_hex_str(hexstr, *valpos);
2034 g_string_append_printf(buf, "%%%s", hexstr);
2040 if (buf->str && strlen(buf->str)) {
2041 if (pdata->len + strlen(buf->str) + 2 > 76) {
2042 fprintf(pdata->fp, ";\n %s", buf->str);
2043 pdata->len = strlen(buf->str) + 1;
2045 fprintf(pdata->fp, "; %s", buf->str);
2046 pdata->len += strlen(buf->str) + 2;
2049 g_string_free(buf, TRUE);
2052 void procmime_write_mime_header(MimeInfo *mimeinfo, FILE *fp)
2054 struct TypeTable *type_table;
2055 ParametersData *pdata = g_new0(ParametersData, 1);
2056 debug_print("procmime_write_mime_header\n");
2059 pdata->ascii_only = FALSE;
2061 for (type_table = mime_type_table; type_table->str != NULL; type_table++)
2062 if (mimeinfo->type == type_table->type) {
2063 gchar *buf = g_strdup_printf(
2064 "Content-Type: %s/%s", type_table->str, mimeinfo->subtype);
2065 fprintf(fp, "%s", buf);
2066 pdata->len = strlen(buf);
2067 pdata->ascii_only = TRUE;
2071 g_hash_table_foreach(mimeinfo->typeparameters, write_parameters, pdata);
2076 if (mimeinfo->encoding_type != ENC_UNKNOWN)
2077 fprintf(fp, "Content-Transfer-Encoding: %s\n", procmime_get_encoding_str(mimeinfo->encoding_type));
2079 if (mimeinfo->description != NULL)
2080 fprintf(fp, "Content-Description: %s\n", mimeinfo->description);
2082 if (mimeinfo->id != NULL)
2083 fprintf(fp, "Content-ID: %s\n", mimeinfo->id);
2085 if (mimeinfo->location != NULL)
2086 fprintf(fp, "Content-Location: %s\n", mimeinfo->location);
2088 if (mimeinfo->disposition != DISPOSITIONTYPE_UNKNOWN) {
2089 ParametersData *pdata = g_new0(ParametersData, 1);
2091 if (mimeinfo->disposition == DISPOSITIONTYPE_INLINE)
2092 buf = g_strdup("Content-Disposition: inline");
2093 else if (mimeinfo->disposition == DISPOSITIONTYPE_ATTACHMENT)
2094 buf = g_strdup("Content-Disposition: attachment");
2096 buf = g_strdup("Content-Disposition: unknown");
2098 fprintf(fp, "%s", buf);
2099 pdata->len = strlen(buf);
2103 pdata->ascii_only = FALSE;
2105 g_hash_table_foreach(mimeinfo->dispositionparameters, write_parameters, pdata);
2113 static gint procmime_write_message_rfc822(MimeInfo *mimeinfo, FILE *fp)
2118 gchar buf[BUFFSIZE];
2119 gboolean skip = FALSE;;
2121 debug_print("procmime_write_message_rfc822\n");
2124 switch (mimeinfo->content) {
2125 case MIMECONTENT_FILE:
2126 if ((infp = g_fopen(mimeinfo->data.filename, "rb")) == NULL) {
2127 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
2130 fseek(infp, mimeinfo->offset, SEEK_SET);
2131 while (fgets(buf, sizeof(buf), infp) == buf) {
2133 if (buf[0] == '\n' && buf[1] == '\0')
2135 if (skip && (buf[0] == ' ' || buf[0] == '\t'))
2137 if (g_ascii_strncasecmp(buf, "Mime-Version:", 13) == 0 ||
2138 g_ascii_strncasecmp(buf, "Content-Type:", 13) == 0 ||
2139 g_ascii_strncasecmp(buf, "Content-Transfer-Encoding:", 26) == 0 ||
2140 g_ascii_strncasecmp(buf, "Content-Description:", 20) == 0 ||
2141 g_ascii_strncasecmp(buf, "Content-ID:", 11) == 0 ||
2142 g_ascii_strncasecmp(buf, "Content-Location:", 17) == 0 ||
2143 g_ascii_strncasecmp(buf, "Content-Disposition:", 20) == 0) {
2147 fwrite(buf, sizeof(gchar), strlen(buf), fp);
2153 case MIMECONTENT_MEM:
2154 fwrite(mimeinfo->data.mem,
2156 strlen(mimeinfo->data.mem),
2164 childnode = mimeinfo->node->children;
2165 if (childnode == NULL)
2168 child = (MimeInfo *) childnode->data;
2169 fprintf(fp, "Mime-Version: 1.0\n");
2170 procmime_write_mime_header(child, fp);
2171 return procmime_write_mimeinfo(child, fp);
2174 static gint procmime_write_multipart(MimeInfo *mimeinfo, FILE *fp)
2178 gchar *boundary, *str, *str2;
2179 gchar buf[BUFFSIZE];
2180 gboolean firstboundary;
2182 debug_print("procmime_write_multipart\n");
2184 boundary = g_hash_table_lookup(mimeinfo->typeparameters, "boundary");
2186 switch (mimeinfo->content) {
2187 case MIMECONTENT_FILE:
2188 if ((infp = g_fopen(mimeinfo->data.filename, "rb")) == NULL) {
2189 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
2192 fseek(infp, mimeinfo->offset, SEEK_SET);
2193 while (fgets(buf, sizeof(buf), infp) == buf) {
2194 if (IS_BOUNDARY(buf, boundary, strlen(boundary)))
2196 fwrite(buf, sizeof(gchar), strlen(buf), fp);
2201 case MIMECONTENT_MEM:
2202 str = g_strdup(mimeinfo->data.mem);
2203 if (((str2 = strstr(str, boundary)) != NULL) && ((str2 - str) >= 2) &&
2204 (*(str2 - 1) == '-') && (*(str2 - 2) == '-'))
2206 fwrite(str, sizeof(gchar), strlen(str), fp);
2214 childnode = mimeinfo->node->children;
2215 firstboundary = TRUE;
2216 while (childnode != NULL) {
2217 MimeInfo *child = childnode->data;
2220 firstboundary = FALSE;
2223 fprintf(fp, "--%s\n", boundary);
2225 procmime_write_mime_header(child, fp);
2226 if (procmime_write_mimeinfo(child, fp) < 0)
2229 childnode = g_node_next_sibling(childnode);
2231 fprintf(fp, "\n--%s--\n", boundary);
2236 gint procmime_write_mimeinfo(MimeInfo *mimeinfo, FILE *fp)
2240 debug_print("procmime_write_mimeinfo\n");
2242 if (G_NODE_IS_LEAF(mimeinfo->node)) {
2243 switch (mimeinfo->content) {
2244 case MIMECONTENT_FILE:
2245 if ((infp = g_fopen(mimeinfo->data.filename, "rb")) == NULL) {
2246 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
2249 copy_file_part_to_fp(infp, mimeinfo->offset, mimeinfo->length, fp);
2253 case MIMECONTENT_MEM:
2254 fwrite(mimeinfo->data.mem,
2256 strlen(mimeinfo->data.mem),
2264 /* Call writer for mime type */
2265 switch (mimeinfo->type) {
2266 case MIMETYPE_MESSAGE:
2267 if (g_ascii_strcasecmp(mimeinfo->subtype, "rfc822") == 0) {
2268 return procmime_write_message_rfc822(mimeinfo, fp);
2272 case MIMETYPE_MULTIPART:
2273 return procmime_write_multipart(mimeinfo, fp);
2285 gchar *procmime_get_part_file_name(MimeInfo *mimeinfo)
2289 if ((mimeinfo->type == MIMETYPE_TEXT) && !g_ascii_strcasecmp(mimeinfo->subtype, "html"))
2290 base = g_strdup("mimetmp.html");
2292 const gchar *basetmp;
2295 basetmp = procmime_mimeinfo_get_parameter(mimeinfo, "filename");
2296 if (basetmp == NULL)
2297 basetmp = procmime_mimeinfo_get_parameter(mimeinfo, "name");
2298 if (basetmp == NULL)
2299 basetmp = "mimetmp";
2300 basename = g_path_get_basename(basetmp);
2301 if (*basename == '\0') {
2303 basename = g_strdup("mimetmp");
2305 base = conv_filename_from_utf8(basename);
2307 subst_for_shellsafe_filename(base);