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)) {
196 if (!folder_has_parent_of_type(msginfo->folder, F_QUEUE) &&
197 !folder_has_parent_of_type(msginfo->folder, F_DRAFT))
198 mimeinfo = procmime_scan_file(filename);
200 mimeinfo = procmime_scan_queue_file(filename);
208 H_CONTENT_TRANSFER_ENCODING = 0,
210 H_CONTENT_DISPOSITION = 2,
211 H_CONTENT_DESCRIPTION = 3,
215 const gchar *procmime_mimeinfo_get_parameter(MimeInfo *mimeinfo, const gchar *name)
219 g_return_val_if_fail(mimeinfo != NULL, NULL);
220 g_return_val_if_fail(name != NULL, NULL);
222 value = g_hash_table_lookup(mimeinfo->dispositionparameters, name);
224 value = g_hash_table_lookup(mimeinfo->typeparameters, name);
229 #define FLUSH_LASTLINE() { \
230 if (*lastline != '\0') { \
232 strretchomp(lastline); \
233 llen = strlen(lastline); \
234 if (lastline[llen-1] == ' ' && strcmp(lastline,"-- ")) { \
235 /* this is flowed */ \
237 lastline[llen-1] = '\0'; \
238 fputs(lastline, outfp); \
240 fputs(lastline, outfp); \
241 fputs("\n", outfp); \
244 strcpy(lastline, buf); \
247 gboolean procmime_decode_content(MimeInfo *mimeinfo)
254 gboolean tmp_file = FALSE;
255 gboolean flowed = FALSE;
256 gboolean delsp = FALSE;
257 EncodingType encoding = forced_encoding
259 : mimeinfo->encoding_type;
260 gchar lastline[BUFFSIZE];
261 memset(lastline, 0, BUFFSIZE);
263 g_return_val_if_fail(mimeinfo != NULL, FALSE);
265 if (prefs_common.respect_flowed_format &&
266 mimeinfo->type == MIMETYPE_TEXT &&
267 !strcasecmp(mimeinfo->subtype, "plain")) {
268 if (procmime_mimeinfo_get_parameter(mimeinfo, "format") != NULL &&
269 !strcasecmp(procmime_mimeinfo_get_parameter(mimeinfo, "format"),"flowed"))
272 procmime_mimeinfo_get_parameter(mimeinfo, "delsp") != NULL &&
273 !strcasecmp(procmime_mimeinfo_get_parameter(mimeinfo, "delsp"),"yes"))
278 encoding == ENC_UNKNOWN ||
279 encoding == ENC_BINARY ||
280 encoding == ENC_7BIT ||
285 infp = g_fopen(mimeinfo->data.filename, "rb");
290 fseek(infp, mimeinfo->offset, SEEK_SET);
292 outfp = get_tmpfile_in_dir(get_mime_tmp_dir(), &tmpfilename);
299 readend = mimeinfo->offset + mimeinfo->length;
301 if (encoding == ENC_QUOTED_PRINTABLE) {
302 while ((ftell(infp) < readend) && (fgets(buf, sizeof(buf), infp) != NULL)) {
304 len = qp_decode_line(buf);
307 fwrite(buf, 1, len, outfp);
314 } else if (encoding == ENC_BASE64) {
315 gchar outbuf[BUFFSIZE];
317 Base64Decoder *decoder;
318 gboolean got_error = FALSE;
319 gboolean uncanonicalize = FALSE;
322 if (mimeinfo->type == MIMETYPE_TEXT ||
323 mimeinfo->type == MIMETYPE_MESSAGE) {
324 uncanonicalize = TRUE;
325 tmpfp = my_tmpfile();
328 if (tmp_file) fclose(outfp);
334 decoder = base64_decoder_new();
335 while ((ftell(infp) < readend) && (fgets(buf, sizeof(buf), infp) != NULL)) {
336 len = base64_decoder_decode(decoder, buf, outbuf);
337 if (len < 0 && !got_error) {
338 g_warning("Bad BASE64 content.\n");
339 fwrite(_("[Error decoding BASE64]\n"),
341 strlen(_("[Error decoding BASE64]\n")),
345 } else if (len >= 0) {
346 /* print out the error message only once
348 fwrite(outbuf, sizeof(gchar), len, tmpfp);
352 base64_decoder_free(decoder);
354 if (uncanonicalize) {
356 while (fgets(buf, sizeof(buf), tmpfp) != NULL) {
362 } else if (encoding == ENC_X_UUENCODE) {
363 gchar outbuf[BUFFSIZE];
365 gboolean flag = FALSE;
367 while ((ftell(infp) < readend) && (fgets(buf, sizeof(buf), infp) != NULL)) {
368 if (!flag && strncmp(buf,"begin ", 6)) continue;
371 len = fromuutobits(outbuf, buf);
374 g_warning("Bad UUENCODE content(%d)\n", len);
377 fwrite(outbuf, sizeof(gchar), len, outfp);
382 while ((ftell(infp) < readend) && (fgets(buf, sizeof(buf), infp) != NULL)) {
396 stat(tmpfilename, &statbuf);
397 if (mimeinfo->tmp && (mimeinfo->data.filename != NULL))
398 g_unlink(mimeinfo->data.filename);
399 g_free(mimeinfo->data.filename);
400 mimeinfo->data.filename = tmpfilename;
401 mimeinfo->tmp = TRUE;
402 mimeinfo->offset = 0;
403 mimeinfo->length = statbuf.st_size;
404 mimeinfo->encoding_type = ENC_BINARY;
409 #define B64_LINE_SIZE 57
410 #define B64_BUFFSIZE 77
412 gboolean procmime_encode_content(MimeInfo *mimeinfo, EncodingType encoding)
414 FILE *infp = NULL, *outfp;
419 if (mimeinfo->content == MIMECONTENT_EMPTY)
422 if (mimeinfo->encoding_type != ENC_UNKNOWN &&
423 mimeinfo->encoding_type != ENC_BINARY &&
424 mimeinfo->encoding_type != ENC_7BIT &&
425 mimeinfo->encoding_type != ENC_8BIT)
426 if(!procmime_decode_content(mimeinfo))
429 outfp = get_tmpfile_in_dir(get_mime_tmp_dir(), &tmpfilename);
435 if (mimeinfo->content == MIMECONTENT_FILE) {
436 if ((infp = g_fopen(mimeinfo->data.filename, "rb")) == NULL) {
437 g_warning("Can't open file %s\n", mimeinfo->data.filename);
440 } else if (mimeinfo->content == MIMECONTENT_MEM) {
441 infp = str_open_as_stream(mimeinfo->data.mem);
446 if (encoding == ENC_BASE64) {
447 gchar inbuf[B64_LINE_SIZE], outbuf[B64_BUFFSIZE];
449 gchar *tmp_file = NULL;
451 if (mimeinfo->type == MIMETYPE_TEXT ||
452 mimeinfo->type == MIMETYPE_MESSAGE) {
453 if (mimeinfo->content == MIMECONTENT_FILE) {
454 tmp_file = get_tmp_file();
455 if (canonicalize_file(mimeinfo->data.filename, tmp_file) < 0) {
460 if ((tmp_fp = g_fopen(tmp_file, "rb")) == NULL) {
461 FILE_OP_ERROR(tmp_file, "fopen");
468 gchar *out = canonicalize_str(mimeinfo->data.mem);
470 infp = str_open_as_stream(out);
478 while ((len = fread(inbuf, sizeof(gchar),
479 B64_LINE_SIZE, tmp_fp))
481 base64_encode(outbuf, inbuf, B64_LINE_SIZE);
482 fputs(outbuf, outfp);
485 if (len > 0 && feof(tmp_fp)) {
486 base64_encode(outbuf, inbuf, len);
487 fputs(outbuf, outfp);
496 } else if (encoding == ENC_QUOTED_PRINTABLE) {
497 gchar inbuf[BUFFSIZE], outbuf[BUFFSIZE * 4];
499 while (fgets(inbuf, sizeof(inbuf), infp) != NULL) {
500 qp_encode_line(outbuf, inbuf);
502 if (!strncmp("From ", outbuf, sizeof("From ")-1)) {
503 gchar *tmpbuf = outbuf;
505 tmpbuf += sizeof("From ")-1;
507 fputs("=46rom ", outfp);
508 fputs(tmpbuf, outfp);
510 fputs(outbuf, outfp);
515 while (fgets(buf, sizeof(buf), infp) != NULL) {
524 if (mimeinfo->content == MIMECONTENT_FILE) {
525 if (mimeinfo->tmp && (mimeinfo->data.filename != NULL))
526 g_unlink(mimeinfo->data.filename);
527 g_free(mimeinfo->data.filename);
528 } else if (mimeinfo->content == MIMECONTENT_MEM) {
529 if (mimeinfo->tmp && (mimeinfo->data.mem != NULL))
530 g_free(mimeinfo->data.mem);
533 stat(tmpfilename, &statbuf);
534 mimeinfo->content = MIMECONTENT_FILE;
535 mimeinfo->data.filename = tmpfilename;
536 mimeinfo->tmp = TRUE;
537 mimeinfo->offset = 0;
538 mimeinfo->length = statbuf.st_size;
539 mimeinfo->encoding_type = encoding;
544 gint procmime_get_part(const gchar *outfile, MimeInfo *mimeinfo)
548 gint restlength, readlength;
549 gint saved_errno = 0;
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) {
559 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
560 return -(saved_errno);
562 if (fseek(infp, mimeinfo->offset, SEEK_SET) < 0) {
564 FILE_OP_ERROR(mimeinfo->data.filename, "fseek");
566 return -(saved_errno);
568 if ((outfp = g_fopen(outfile, "wb")) == NULL) {
570 FILE_OP_ERROR(outfile, "fopen");
572 return -(saved_errno);
575 restlength = mimeinfo->length;
577 while ((restlength > 0) && ((readlength = fread(buf, 1, restlength > BUFFSIZE ? BUFFSIZE : restlength, infp)) > 0)) {
578 if (fwrite(buf, 1, readlength, outfp) != readlength) {
581 return -(saved_errno);
583 restlength -= readlength;
587 if (fclose(outfp) == EOF) {
589 FILE_OP_ERROR(outfile, "fclose");
591 return -(saved_errno);
597 struct ContentRenderer {
602 static GList * renderer_list = NULL;
604 static struct ContentRenderer *
605 content_renderer_new(char * content_type, char * renderer)
607 struct ContentRenderer * cr;
609 cr = g_new(struct ContentRenderer, 1);
613 cr->content_type = g_strdup(content_type);
614 cr->renderer = g_strdup(renderer);
619 static void content_renderer_free(struct ContentRenderer * cr)
621 g_free(cr->content_type);
622 g_free(cr->renderer);
626 void renderer_read_config(void)
632 g_list_foreach(renderer_list, (GFunc) content_renderer_free, NULL);
633 renderer_list = NULL;
635 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, RENDERER_RC, NULL);
636 f = g_fopen(rcpath, "rb");
642 while (fgets(buf, BUFFSIZE, f)) {
644 struct ContentRenderer * cr;
647 p = strchr(buf, ' ');
652 cr = content_renderer_new(buf, p + 1);
656 renderer_list = g_list_append(renderer_list, cr);
662 void renderer_write_config(void)
668 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, RENDERER_RC, NULL);
670 if ((pfile = prefs_write_open(rcpath)) == NULL) {
671 g_warning("failed to write configuration to file\n");
678 for (cur = renderer_list ; cur != NULL ; cur = cur->next) {
679 struct ContentRenderer * renderer;
680 renderer = cur->data;
681 fprintf(pfile->fp, "%s %s\n", renderer->content_type,
685 if (prefs_file_close(pfile) < 0) {
686 g_warning("failed to write configuration to file\n");
691 FILE *procmime_get_text_content(MimeInfo *mimeinfo)
694 const gchar *src_codeset;
695 gboolean conv_fail = FALSE;
698 struct ContentRenderer * renderer;
700 gchar *tmpfile, *content_type;
702 g_return_val_if_fail(mimeinfo != NULL, NULL);
704 if (!procmime_decode_content(mimeinfo))
707 tmpfile = procmime_get_tmp_file_name(mimeinfo);
711 if (procmime_get_part(tmpfile, mimeinfo) < 0) {
716 tmpfp = g_fopen(tmpfile, "rb");
722 if ((outfp = my_tmpfile()) == NULL) {
729 src_codeset = forced_charset
731 procmime_mimeinfo_get_parameter(mimeinfo, "charset");
735 content_type = procmime_get_content_type_str(mimeinfo->type,
737 for (cur = renderer_list ; cur != NULL ; cur = cur->next) {
738 struct ContentRenderer * cr;
741 if (g_ascii_strcasecmp(cr->content_type, content_type) == 0) {
746 g_free(content_type);
748 if (renderer != NULL) {
754 dup2(fileno(outfp), 1);
756 p = popen(renderer->renderer, "w");
761 fread(buf, sizeof(char), sizeof(buf),
763 fwrite(buf, sizeof(char), count, p);
768 /* CodeConverter seems to have no effect here */
769 } else if (mimeinfo->type == MIMETYPE_TEXT && !g_ascii_strcasecmp(mimeinfo->subtype, "html")) {
770 SC_HTMLParser *parser;
773 conv = conv_code_converter_new(src_codeset);
774 parser = sc_html_parser_new(tmpfp, conv);
775 while ((str = sc_html_parse(parser)) != NULL) {
778 sc_html_parser_destroy(parser);
779 conv_code_converter_destroy(conv);
780 } else if (mimeinfo->type == MIMETYPE_TEXT && !g_ascii_strcasecmp(mimeinfo->subtype, "enriched")) {
784 conv = conv_code_converter_new(src_codeset);
785 parser = ertf_parser_new(tmpfp, conv);
786 while ((str = ertf_parse(parser)) != NULL) {
789 ertf_parser_destroy(parser);
790 conv_code_converter_destroy(conv);
791 } else if (mimeinfo->type == MIMETYPE_TEXT) {
792 while (fgets(buf, sizeof(buf), tmpfp) != NULL) {
793 str = conv_codeset_strdup(buf, src_codeset, CS_UTF_8);
805 g_warning("procmime_get_text_content(): Code conversion failed.\n");
815 /* search the first text part of (multipart) MIME message,
816 decode, convert it and output to outfp. */
817 FILE *procmime_get_first_text_content(MsgInfo *msginfo)
820 MimeInfo *mimeinfo, *partinfo;
822 g_return_val_if_fail(msginfo != NULL, NULL);
824 mimeinfo = procmime_scan_message(msginfo);
825 if (!mimeinfo) return NULL;
828 while (partinfo && partinfo->type != MIMETYPE_TEXT) {
829 partinfo = procmime_mimeinfo_next(partinfo);
832 outfp = procmime_get_text_content(partinfo);
834 procmime_mimeinfo_free_all(mimeinfo);
840 static gboolean find_encrypted_func(GNode *node, gpointer data)
842 MimeInfo *mimeinfo = (MimeInfo *) node->data;
843 MimeInfo **encinfo = (MimeInfo **) data;
845 if (privacy_mimeinfo_is_encrypted(mimeinfo)) {
853 static MimeInfo *find_encrypted_part(MimeInfo *rootinfo)
855 MimeInfo *encinfo = NULL;
857 g_node_traverse(rootinfo->node, G_IN_ORDER, G_TRAVERSE_ALL, -1,
858 find_encrypted_func, &encinfo);
863 /* search the first encrypted text part of (multipart) MIME message,
864 decode, convert it and output to outfp. */
865 FILE *procmime_get_first_encrypted_text_content(MsgInfo *msginfo)
868 MimeInfo *mimeinfo, *partinfo, *encinfo;
870 g_return_val_if_fail(msginfo != NULL, NULL);
872 mimeinfo = procmime_scan_message(msginfo);
878 if ((encinfo = find_encrypted_part(partinfo)) != NULL) {
879 debug_print("decrypting message part\n");
880 if (privacy_mimeinfo_decrypt(encinfo) < 0) {
881 alertpanel_error(_("Couldn't decrypt: %s"),
882 privacy_get_error());
887 while (partinfo && partinfo->type != MIMETYPE_TEXT) {
888 partinfo = procmime_mimeinfo_next(partinfo);
892 outfp = procmime_get_text_content(partinfo);
894 procmime_mimeinfo_free_all(mimeinfo);
899 gboolean procmime_msginfo_is_encrypted(MsgInfo *msginfo)
901 MimeInfo *mimeinfo, *partinfo;
902 gboolean result = FALSE;
904 g_return_val_if_fail(msginfo != NULL, FALSE);
906 mimeinfo = procmime_scan_message(msginfo);
912 result = (find_encrypted_part(partinfo) != NULL);
913 procmime_mimeinfo_free_all(mimeinfo);
918 gboolean procmime_find_string_part(MimeInfo *mimeinfo, const gchar *filename,
919 const gchar *str, StrFindFunc find_func)
924 g_return_val_if_fail(mimeinfo != NULL, FALSE);
925 g_return_val_if_fail(mimeinfo->type == MIMETYPE_TEXT, FALSE);
926 g_return_val_if_fail(str != NULL, FALSE);
927 g_return_val_if_fail(find_func != NULL, FALSE);
929 outfp = procmime_get_text_content(mimeinfo);
934 while (fgets(buf, sizeof(buf), outfp) != NULL) {
936 if (find_func(buf, str)) {
947 gboolean procmime_find_string(MsgInfo *msginfo, const gchar *str,
948 StrFindFunc find_func)
953 gboolean found = FALSE;
955 g_return_val_if_fail(msginfo != NULL, FALSE);
956 g_return_val_if_fail(str != NULL, FALSE);
957 g_return_val_if_fail(find_func != NULL, FALSE);
959 filename = procmsg_get_message_file(msginfo);
960 if (!filename) return FALSE;
961 mimeinfo = procmime_scan_message(msginfo);
963 for (partinfo = mimeinfo; partinfo != NULL;
964 partinfo = procmime_mimeinfo_next(partinfo)) {
965 if (partinfo->type == MIMETYPE_TEXT) {
966 if (procmime_find_string_part
967 (partinfo, filename, str, find_func) == TRUE) {
974 procmime_mimeinfo_free_all(mimeinfo);
980 gchar *procmime_get_tmp_file_name(MimeInfo *mimeinfo)
982 static guint32 id = 0;
987 g_return_val_if_fail(mimeinfo != NULL, NULL);
989 g_snprintf(f_prefix, sizeof(f_prefix), "%08x.", id++);
991 if ((mimeinfo->type == MIMETYPE_TEXT) && !g_ascii_strcasecmp(mimeinfo->subtype, "html"))
992 base = g_strdup("mimetmp.html");
994 const gchar *basetmp;
996 basetmp = procmime_mimeinfo_get_parameter(mimeinfo, "filename");
998 basetmp = procmime_mimeinfo_get_parameter(mimeinfo, "name");
1000 basetmp = "mimetmp";
1001 basetmp = g_path_get_basename(basetmp);
1002 if (*basetmp == '\0')
1003 basetmp = g_strdup("mimetmp");
1004 base = conv_filename_from_utf8(basetmp);
1005 g_free((gchar*)basetmp);
1006 subst_for_shellsafe_filename(base);
1009 filename = g_strconcat(get_mime_tmp_dir(), G_DIR_SEPARATOR_S,
1010 f_prefix, base, NULL);
1017 static GList *mime_type_list = NULL;
1019 gchar *procmime_get_mime_type(const gchar *filename)
1021 static GHashTable *mime_type_table = NULL;
1022 MimeType *mime_type;
1027 if (!mime_type_table) {
1028 mime_type_table = procmime_get_mime_type_table();
1029 if (!mime_type_table) return NULL;
1032 base = g_path_get_basename(filename);
1033 if ((p = strrchr(base, '.')) != NULL)
1034 Xstrdup_a(ext, p + 1, p = NULL );
1036 if (!p) return NULL;
1039 mime_type = g_hash_table_lookup(mime_type_table, ext);
1043 str = g_strconcat(mime_type->type, "/", mime_type->sub_type,
1051 static guint procmime_str_hash(gconstpointer gptr)
1053 guint hash_result = 0;
1056 for (str = gptr; str && *str; str++) {
1057 if (isupper(*str)) hash_result += (*str + ' ');
1058 else hash_result += *str;
1064 static gint procmime_str_equal(gconstpointer gptr1, gconstpointer gptr2)
1066 const char *str1 = gptr1;
1067 const char *str2 = gptr2;
1069 return !g_utf8_collate(str1, str2);
1072 static GHashTable *procmime_get_mime_type_table(void)
1074 GHashTable *table = NULL;
1076 MimeType *mime_type;
1079 if (!mime_type_list) {
1080 mime_type_list = procmime_get_mime_type_list();
1081 if (!mime_type_list) return NULL;
1084 table = g_hash_table_new(procmime_str_hash, procmime_str_equal);
1086 for (cur = mime_type_list; cur != NULL; cur = cur->next) {
1090 mime_type = (MimeType *)cur->data;
1092 if (!mime_type->extension) continue;
1094 exts = g_strsplit(mime_type->extension, " ", 16);
1095 for (i = 0; exts[i] != NULL; i++) {
1096 /* make the key case insensitive */
1098 /* use previously dup'd key on overwriting */
1099 if (g_hash_table_lookup(table, exts[i]))
1102 key = g_strdup(exts[i]);
1103 g_hash_table_insert(table, key, mime_type);
1111 GList *procmime_get_mime_type_list(void)
1115 gchar buf[BUFFSIZE];
1118 MimeType *mime_type;
1119 gboolean fp_is_glob_file = TRUE;
1122 return mime_type_list;
1124 #if defined(__NetBSD__)
1125 if ((fp = g_fopen(DATAROOTDIR "/mime/globs", "rb")) == NULL)
1127 if ((fp = g_fopen("/usr/share/mime/globs", "rb")) == NULL)
1130 fp_is_glob_file = FALSE;
1131 if ((fp = g_fopen("/etc/mime.types", "rb")) == NULL) {
1132 if ((fp = g_fopen(SYSCONFDIR "/mime.types", "rb"))
1134 FILE_OP_ERROR(SYSCONFDIR "/mime.types",
1141 while (fgets(buf, sizeof(buf), fp) != NULL) {
1142 p = strchr(buf, '#');
1148 if (fp_is_glob_file) {
1149 while (*p && !g_ascii_isspace(*p) && (*p!=':')) p++;
1151 while (*p && !g_ascii_isspace(*p)) p++;
1158 delim = strchr(buf, '/');
1159 if (delim == NULL) continue;
1162 mime_type = g_new(MimeType, 1);
1163 mime_type->type = g_strdup(buf);
1164 mime_type->sub_type = g_strdup(delim + 1);
1166 if (fp_is_glob_file) {
1167 while (*p && (g_ascii_isspace(*p)||(*p=='*')||(*p=='.'))) p++;
1169 while (*p && g_ascii_isspace(*p)) p++;
1173 mime_type->extension = g_strdup(p);
1175 mime_type->extension = NULL;
1177 list = g_list_append(list, mime_type);
1183 g_warning("Can't read mime.types\n");
1188 EncodingType procmime_get_encoding_for_charset(const gchar *charset)
1192 else if (!g_ascii_strncasecmp(charset, "ISO-2022-", 9) ||
1193 !g_ascii_strcasecmp(charset, "US-ASCII"))
1195 else if (!g_ascii_strcasecmp(charset, "ISO-8859-5") ||
1196 !g_ascii_strncasecmp(charset, "KOI8-", 5) ||
1197 !g_ascii_strcasecmp(charset, "Windows-1251"))
1199 else if (!g_ascii_strncasecmp(charset, "ISO-8859-", 9))
1200 return ENC_QUOTED_PRINTABLE;
1201 else if (!g_ascii_strncasecmp(charset, "UTF-8", 5))
1202 return ENC_QUOTED_PRINTABLE;
1207 EncodingType procmime_get_encoding_for_text_file(const gchar *file, gboolean *has_binary)
1210 guchar buf[BUFFSIZE];
1212 size_t octet_chars = 0;
1213 size_t total_len = 0;
1214 gfloat octet_percentage;
1215 gboolean force_b64 = FALSE;
1217 if ((fp = g_fopen(file, "rb")) == NULL) {
1218 FILE_OP_ERROR(file, "fopen");
1222 while ((len = fread(buf, sizeof(guchar), sizeof(buf), fp)) > 0) {
1226 for (p = buf, i = 0; i < len; ++p, ++i) {
1240 octet_percentage = (gfloat)octet_chars / (gfloat)total_len;
1242 octet_percentage = 0.0;
1244 debug_print("procmime_get_encoding_for_text_file(): "
1245 "8bit chars: %d / %d (%f%%)\n", octet_chars, total_len,
1246 100.0 * octet_percentage);
1248 if (octet_percentage > 0.20 || force_b64) {
1249 debug_print("using BASE64\n");
1251 } else if (octet_chars > 0) {
1252 debug_print("using quoted-printable\n");
1253 return ENC_QUOTED_PRINTABLE;
1255 debug_print("using 7bit\n");
1260 struct EncodingTable
1263 EncodingType enc_type;
1266 struct EncodingTable encoding_table[] = {
1269 {"binary", ENC_BINARY},
1270 {"quoted-printable", ENC_QUOTED_PRINTABLE},
1271 {"base64", ENC_BASE64},
1272 {"x-uuencode", ENC_UNKNOWN},
1273 {NULL, ENC_UNKNOWN},
1276 const gchar *procmime_get_encoding_str(EncodingType encoding)
1278 struct EncodingTable *enc_table;
1280 for (enc_table = encoding_table; enc_table->str != NULL; enc_table++) {
1281 if (enc_table->enc_type == encoding)
1282 return enc_table->str;
1287 /* --- NEW MIME STUFF --- */
1294 static struct TypeTable mime_type_table[] = {
1295 {"text", MIMETYPE_TEXT},
1296 {"image", MIMETYPE_IMAGE},
1297 {"audio", MIMETYPE_AUDIO},
1298 {"video", MIMETYPE_VIDEO},
1299 {"application", MIMETYPE_APPLICATION},
1300 {"message", MIMETYPE_MESSAGE},
1301 {"multipart", MIMETYPE_MULTIPART},
1305 const gchar *procmime_get_media_type_str(MimeMediaType type)
1307 struct TypeTable *type_table;
1309 for (type_table = mime_type_table; type_table->str != NULL; type_table++) {
1310 if (type_table->type == type)
1311 return type_table->str;
1316 MimeMediaType procmime_get_media_type(const gchar *str)
1318 struct TypeTable *typetablearray;
1320 for (typetablearray = mime_type_table; typetablearray->str != NULL; typetablearray++)
1321 if (g_ascii_strncasecmp(str, typetablearray->str, strlen(typetablearray->str)) == 0)
1322 return typetablearray->type;
1324 return MIMETYPE_UNKNOWN;
1328 *\brief Safe wrapper for content type string.
1330 *\return const gchar * Pointer to content type string.
1332 gchar *procmime_get_content_type_str(MimeMediaType type,
1333 const char *subtype)
1335 const gchar *type_str = NULL;
1337 if (subtype == NULL || !(type_str = procmime_get_media_type_str(type)))
1338 return g_strdup("unknown");
1339 return g_strdup_printf("%s/%s", type_str, subtype);
1342 static int procmime_parse_mimepart(MimeInfo *parent,
1343 gchar *content_type,
1344 gchar *content_encoding,
1345 gchar *content_description,
1347 gchar *content_disposition,
1348 gchar *content_location,
1349 const gchar *filename,
1353 static void procmime_parse_message_rfc822(MimeInfo *mimeinfo)
1355 HeaderEntry hentry[] = {{"Content-Type:", NULL, TRUE},
1356 {"Content-Transfer-Encoding:",
1358 {"Content-Description:",
1362 {"Content-Disposition:",
1364 {"Content-Location:",
1368 {NULL, NULL, FALSE}};
1369 guint content_start, i;
1374 procmime_decode_content(mimeinfo);
1376 fp = g_fopen(mimeinfo->data.filename, "rb");
1378 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
1381 fseek(fp, mimeinfo->offset, SEEK_SET);
1382 procheader_get_header_fields(fp, hentry);
1383 if (hentry[0].body != NULL) {
1384 tmp = conv_unmime_header(hentry[0].body, NULL);
1385 g_free(hentry[0].body);
1386 hentry[0].body = tmp;
1388 if (hentry[2].body != NULL) {
1389 tmp = conv_unmime_header(hentry[2].body, NULL);
1390 g_free(hentry[2].body);
1391 hentry[2].body = tmp;
1393 if (hentry[4].body != NULL) {
1394 tmp = conv_unmime_header(hentry[4].body, NULL);
1395 g_free(hentry[4].body);
1396 hentry[4].body = tmp;
1398 if (hentry[5].body != NULL) {
1399 tmp = conv_unmime_header(hentry[5].body, NULL);
1400 g_free(hentry[5].body);
1401 hentry[5].body = tmp;
1403 content_start = ftell(fp);
1406 len = mimeinfo->length - (content_start - mimeinfo->offset);
1409 procmime_parse_mimepart(mimeinfo,
1410 hentry[0].body, hentry[1].body,
1411 hentry[2].body, hentry[3].body,
1412 hentry[4].body, hentry[5].body,
1413 mimeinfo->data.filename, content_start,
1416 for (i = 0; i < (sizeof hentry / sizeof hentry[0]); i++) {
1417 g_free(hentry[i].body);
1418 hentry[i].body = NULL;
1422 static void procmime_parse_multipart(MimeInfo *mimeinfo)
1424 HeaderEntry hentry[] = {{"Content-Type:", NULL, TRUE},
1425 {"Content-Transfer-Encoding:",
1427 {"Content-Description:",
1431 {"Content-Disposition:",
1433 {"Content-Location:",
1435 {NULL, NULL, FALSE}};
1438 gint boundary_len = 0, lastoffset = -1, i;
1439 gchar buf[BUFFSIZE];
1443 boundary = g_hash_table_lookup(mimeinfo->typeparameters, "boundary");
1446 boundary_len = strlen(boundary);
1448 procmime_decode_content(mimeinfo);
1450 fp = g_fopen(mimeinfo->data.filename, "rb");
1452 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
1455 fseek(fp, mimeinfo->offset, SEEK_SET);
1456 while ((p = fgets(buf, sizeof(buf), fp)) != NULL && result == 0) {
1457 if (ftell(fp) - 1 > (mimeinfo->offset + mimeinfo->length))
1460 if (IS_BOUNDARY(buf, boundary, boundary_len)) {
1461 if (lastoffset != -1) {
1462 gint len = (ftell(fp) - strlen(buf)) - lastoffset - 1;
1465 result = procmime_parse_mimepart(mimeinfo,
1466 hentry[0].body, hentry[1].body,
1467 hentry[2].body, hentry[3].body,
1468 hentry[4].body, hentry[5].body,
1469 mimeinfo->data.filename, lastoffset,
1473 if (buf[2 + boundary_len] == '-' &&
1474 buf[2 + boundary_len + 1] == '-')
1477 for (i = 0; i < (sizeof hentry / sizeof hentry[0]) ; i++) {
1478 g_free(hentry[i].body);
1479 hentry[i].body = NULL;
1481 procheader_get_header_fields(fp, hentry);
1482 if (hentry[0].body != NULL) {
1483 tmp = conv_unmime_header(hentry[0].body, NULL);
1484 g_free(hentry[0].body);
1485 hentry[0].body = tmp;
1487 if (hentry[2].body != NULL) {
1488 tmp = conv_unmime_header(hentry[2].body, NULL);
1489 g_free(hentry[2].body);
1490 hentry[2].body = tmp;
1492 if (hentry[4].body != NULL) {
1493 tmp = conv_unmime_header(hentry[4].body, NULL);
1494 g_free(hentry[4].body);
1495 hentry[4].body = tmp;
1497 if (hentry[5].body != NULL) {
1498 tmp = conv_unmime_header(hentry[5].body, NULL);
1499 g_free(hentry[5].body);
1500 hentry[5].body = tmp;
1502 lastoffset = ftell(fp);
1505 for (i = 0; i < (sizeof hentry / sizeof hentry[0]); i++) {
1506 g_free(hentry[i].body);
1507 hentry[i].body = NULL;
1512 static void parse_parameters(const gchar *parameters, GHashTable *table)
1514 gchar *params, *param, *next;
1515 GSList *convlist = NULL, *concatlist = NULL, *cur;
1517 params = g_strdup(parameters);
1520 for (; next != NULL; param = next) {
1521 gchar *attribute, *value, *tmp;
1523 gboolean convert = FALSE;
1525 next = strchr_with_skip_quote(param, '"', ';');
1534 value = strchr(attribute, '=');
1540 while (value[0] == ' ')
1543 g_strdown(attribute);
1545 len = strlen(attribute);
1546 if (attribute[len - 1] == '*') {
1547 gchar *srcpos, *dstpos, *endpos;
1550 attribute[len - 1] = '\0';
1554 endpos = value + strlen(value);
1555 while (srcpos < endpos) {
1561 if (!get_hex_value(&dstvalue, srcpos[1], srcpos[2]))
1572 if (value[0] == '"')
1573 extract_quote(value, '"');
1574 else if ((tmp = strchr(value, ' ')) != NULL)
1579 while (attribute[0] == ' ')
1581 while (attribute[strlen(attribute)-1] == ' ')
1582 attribute[strlen(attribute)-1] = '\0';
1585 while (value[0] == ' ')
1587 while (value[strlen(value)-1] == ' ')
1588 value[strlen(value)-1] = '\0';
1590 if (strrchr(attribute, '*') != NULL) {
1593 tmpattr = g_strdup(attribute);
1594 tmp = strrchr(tmpattr, '*');
1597 if ((tmp[1] == '0') && (tmp[2] == '\0') &&
1598 (g_slist_find_custom(concatlist, attribute, g_str_equal) == NULL))
1599 concatlist = g_slist_prepend(concatlist, g_strdup(tmpattr));
1601 if (convert && (g_slist_find_custom(convlist, attribute, g_str_equal) == NULL))
1602 convlist = g_slist_prepend(convlist, g_strdup(tmpattr));
1605 } else if (convert) {
1606 if (g_slist_find_custom(convlist, attribute, g_str_equal) == NULL)
1607 convlist = g_slist_prepend(convlist, g_strdup(attribute));
1610 if (g_hash_table_lookup(table, attribute) == NULL)
1611 g_hash_table_insert(table, g_strdup(attribute), g_strdup(value));
1614 for (cur = concatlist; cur != NULL; cur = g_slist_next(cur)) {
1615 gchar *attribute, *attrwnum, *partvalue;
1619 attribute = (gchar *) cur->data;
1620 value = g_string_sized_new(64);
1622 attrwnum = g_strdup_printf("%s*%d", attribute, n);
1623 while ((partvalue = g_hash_table_lookup(table, attrwnum)) != NULL) {
1624 g_string_append(value, partvalue);
1628 attrwnum = g_strdup_printf("%s*%d", attribute, n);
1632 g_hash_table_insert(table, g_strdup(attribute), g_strdup(value->str));
1633 g_string_free(value, TRUE);
1635 slist_free_strings(concatlist);
1636 g_slist_free(concatlist);
1638 for (cur = convlist; cur != NULL; cur = g_slist_next(cur)) {
1639 gchar *attribute, *key, *value;
1640 gchar *charset, *lang, *oldvalue, *newvalue;
1642 attribute = (gchar *) cur->data;
1643 if (!g_hash_table_lookup_extended(
1644 table, attribute, (gpointer *)(gchar *) &key, (gpointer *)(gchar *) &value))
1648 lang = strchr(charset, '\'');
1653 oldvalue = strchr(lang, '\'');
1654 if (oldvalue == NULL)
1659 newvalue = conv_codeset_strdup(oldvalue, charset, CS_UTF_8);
1661 g_hash_table_remove(table, attribute);
1665 g_hash_table_insert(table, g_strdup(attribute), newvalue);
1667 slist_free_strings(convlist);
1668 g_slist_free(convlist);
1673 static void procmime_parse_content_type(const gchar *content_type, MimeInfo *mimeinfo)
1675 g_return_if_fail(content_type != NULL);
1676 g_return_if_fail(mimeinfo != NULL);
1678 /* RFC 2045, page 13 says that the mime subtype is MANDATORY;
1679 * if it's not available we use the default Content-Type */
1680 if ((content_type[0] == '\0') || (strchr(content_type, '/') == NULL)) {
1681 mimeinfo->type = MIMETYPE_TEXT;
1682 mimeinfo->subtype = g_strdup("plain");
1683 if (g_hash_table_lookup(mimeinfo->typeparameters,
1684 "charset") == NULL) {
1685 g_hash_table_insert(mimeinfo->typeparameters,
1686 g_strdup("charset"),
1688 conv_get_locale_charset_str_no_utf8()));
1691 gchar *type, *subtype, *params;
1693 type = g_strdup(content_type);
1694 subtype = strchr(type, '/') + 1;
1695 *(subtype - 1) = '\0';
1696 if ((params = strchr(subtype, ';')) != NULL) {
1701 mimeinfo->type = procmime_get_media_type(type);
1702 mimeinfo->subtype = g_strdup(subtype);
1704 /* Get mimeinfo->typeparameters */
1706 parse_parameters(params, mimeinfo->typeparameters);
1712 static void procmime_parse_content_disposition(const gchar *content_disposition, MimeInfo *mimeinfo)
1714 gchar *tmp, *params;
1716 g_return_if_fail(content_disposition != NULL);
1717 g_return_if_fail(mimeinfo != NULL);
1719 tmp = g_strdup(content_disposition);
1720 if ((params = strchr(tmp, ';')) != NULL) {
1726 if (!g_ascii_strcasecmp(tmp, "inline"))
1727 mimeinfo->disposition = DISPOSITIONTYPE_INLINE;
1728 else if (!g_ascii_strcasecmp(tmp, "attachment"))
1729 mimeinfo->disposition = DISPOSITIONTYPE_ATTACHMENT;
1731 mimeinfo->disposition = DISPOSITIONTYPE_ATTACHMENT;
1734 parse_parameters(params, mimeinfo->dispositionparameters);
1740 static void procmime_parse_content_encoding(const gchar *content_encoding, MimeInfo *mimeinfo)
1742 struct EncodingTable *enc_table;
1744 for (enc_table = encoding_table; enc_table->str != NULL; enc_table++) {
1745 if (g_ascii_strcasecmp(enc_table->str, content_encoding) == 0) {
1746 mimeinfo->encoding_type = enc_table->enc_type;
1750 mimeinfo->encoding_type = ENC_UNKNOWN;
1754 static int procmime_parse_mimepart(MimeInfo *parent,
1755 gchar *content_type,
1756 gchar *content_encoding,
1757 gchar *content_description,
1759 gchar *content_disposition,
1760 gchar *content_location,
1761 const gchar *filename,
1767 /* Create MimeInfo */
1768 mimeinfo = procmime_mimeinfo_new();
1769 mimeinfo->content = MIMECONTENT_FILE;
1770 if (parent != NULL) {
1771 if (g_node_depth(parent->node) > 32) {
1772 /* 32 is an arbitrary value
1773 * this avoids DOSsing ourselves
1774 * with enormous messages
1776 procmime_mimeinfo_free_all(mimeinfo);
1779 g_node_append(parent->node, mimeinfo->node);
1781 mimeinfo->data.filename = g_strdup(filename);
1782 mimeinfo->offset = offset;
1783 mimeinfo->length = length;
1785 if (content_type != NULL) {
1786 procmime_parse_content_type(content_type, mimeinfo);
1788 mimeinfo->type = MIMETYPE_TEXT;
1789 mimeinfo->subtype = g_strdup("plain");
1790 if (g_hash_table_lookup(mimeinfo->typeparameters,
1791 "charset") == NULL) {
1792 g_hash_table_insert(mimeinfo->typeparameters,
1793 g_strdup("charset"),
1795 conv_get_locale_charset_str_no_utf8()));
1799 if (content_encoding != NULL) {
1800 procmime_parse_content_encoding(content_encoding, mimeinfo);
1802 mimeinfo->encoding_type = ENC_UNKNOWN;
1805 if (content_description != NULL)
1806 mimeinfo->description = g_strdup(content_description);
1808 mimeinfo->description = NULL;
1810 if (content_id != NULL)
1811 mimeinfo->id = g_strdup(content_id);
1813 mimeinfo->id = NULL;
1815 if (content_location != NULL)
1816 mimeinfo->location = g_strdup(content_location);
1818 mimeinfo->location = NULL;
1820 if (content_disposition != NULL)
1821 procmime_parse_content_disposition(content_disposition, mimeinfo);
1823 mimeinfo->disposition = DISPOSITIONTYPE_UNKNOWN;
1825 /* Call parser for mime type */
1826 switch (mimeinfo->type) {
1827 case MIMETYPE_MESSAGE:
1828 if (g_ascii_strcasecmp(mimeinfo->subtype, "rfc822") == 0) {
1829 procmime_parse_message_rfc822(mimeinfo);
1833 case MIMETYPE_MULTIPART:
1834 procmime_parse_multipart(mimeinfo);
1844 static gchar *typenames[] = {
1855 static gboolean output_func(GNode *node, gpointer data)
1858 MimeInfo *mimeinfo = (MimeInfo *) node->data;
1860 depth = g_node_depth(node);
1861 for (i = 0; i < depth; i++)
1863 printf("%s/%s (offset:%d length:%d encoding: %d)\n", typenames[mimeinfo->type], mimeinfo->subtype, mimeinfo->offset, mimeinfo->length, mimeinfo->encoding_type);
1868 static void output_mime_structure(MimeInfo *mimeinfo, int indent)
1870 g_node_traverse(mimeinfo->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1, output_func, NULL);
1873 static MimeInfo *procmime_scan_file_with_offset(const gchar *filename, int offset)
1878 stat(filename, &buf);
1880 mimeinfo = procmime_mimeinfo_new();
1881 mimeinfo->content = MIMECONTENT_FILE;
1882 mimeinfo->encoding_type = ENC_UNKNOWN;
1883 mimeinfo->type = MIMETYPE_MESSAGE;
1884 mimeinfo->subtype = g_strdup("rfc822");
1885 mimeinfo->data.filename = g_strdup(filename);
1886 mimeinfo->offset = offset;
1887 mimeinfo->length = buf.st_size - offset;
1889 procmime_parse_message_rfc822(mimeinfo);
1890 if (debug_get_mode())
1891 output_mime_structure(mimeinfo, 0);
1896 MimeInfo *procmime_scan_file(const gchar *filename)
1900 g_return_val_if_fail(filename != NULL, NULL);
1902 mimeinfo = procmime_scan_file_with_offset(filename, 0);
1907 MimeInfo *procmime_scan_queue_file(const gchar *filename)
1911 gchar buf[BUFFSIZE];
1914 g_return_val_if_fail(filename != NULL, NULL);
1917 if ((fp = g_fopen(filename, "rb")) == NULL)
1919 /* Skip queue header */
1920 while (fgets(buf, sizeof(buf), fp) != NULL) {
1922 if ((!strncmp(buf, "X-Claws-End-Special-Headers: 1",
1923 strlen("X-Claws-End-Special-Headers:"))) ||
1924 (!strncmp(buf, "X-Sylpheed-End-Special-Headers: 1",
1925 strlen("X-Sylpheed-End-Special-Headers:"))))
1928 if (buf[0] == '\r' || buf[0] == '\n') break;
1929 /* from other mailers */
1930 if (!strncmp(buf, "Date: ", 6)
1931 || !strncmp(buf, "To: ", 4)
1932 || !strncmp(buf, "From: ", 6)
1933 || !strncmp(buf, "Subject: ", 9)) {
1941 mimeinfo = procmime_scan_file_with_offset(filename, offset);
1948 ENC_AS_QUOTED_STRING,
1953 typedef struct _ParametersData {
1959 static void write_parameters(gpointer key, gpointer value, gpointer user_data)
1962 gchar *val = value, *valpos, *tmp;
1963 ParametersData *pdata = (ParametersData *)user_data;
1964 GString *buf = g_string_new("");
1966 EncodeAs encas = ENC_AS_TOKEN;
1968 for (valpos = val; *valpos != 0; valpos++) {
1969 if (!IS_ASCII(*valpos) || *valpos == '"') {
1970 encas = ENC_AS_EXTENDED;
1975 if (((*valpos >= 0) && (*valpos < 037)) || (*valpos == 0177)) {
1976 encas = ENC_AS_QUOTED_STRING;
1980 /* tspecials + SPACE */
1998 encas = ENC_AS_QUOTED_STRING;
2003 if (encas == ENC_AS_EXTENDED && pdata->ascii_only == TRUE)
2004 encas = ENC_TO_ASCII;
2008 g_string_append_printf(buf, "%s=%s", param, val);
2012 tmp = g_strdup(val);
2014 " ()<>@,';:\\/[]?=.0123456789"
2015 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
2016 "abcdefghijklmnopqrstuvwxyz",
2018 g_string_append_printf(buf, "%s=\"%s\"", param, tmp);
2022 case ENC_AS_QUOTED_STRING:
2023 g_string_append_printf(buf, "%s=\"%s\"", param, val);
2026 case ENC_AS_EXTENDED:
2027 if (!g_utf8_validate(val, -1, NULL))
2028 g_string_append_printf(buf, "%s*=%s''", param,
2029 conv_get_locale_charset_str());
2031 g_string_append_printf(buf, "%s*=%s''", param,
2033 for (valpos = val; *valpos != '\0'; valpos++) {
2034 if (IS_ASCII(*valpos) && isalnum(*valpos)) {
2035 g_string_append_printf(buf, "%c", *valpos);
2037 gchar hexstr[3] = "XX";
2038 get_hex_str(hexstr, *valpos);
2039 g_string_append_printf(buf, "%%%s", hexstr);
2045 if (buf->str && strlen(buf->str)) {
2046 if (pdata->len + strlen(buf->str) + 2 > 76) {
2047 fprintf(pdata->fp, ";\n %s", buf->str);
2048 pdata->len = strlen(buf->str) + 1;
2050 fprintf(pdata->fp, "; %s", buf->str);
2051 pdata->len += strlen(buf->str) + 2;
2054 g_string_free(buf, TRUE);
2057 void procmime_write_mime_header(MimeInfo *mimeinfo, FILE *fp)
2059 struct TypeTable *type_table;
2060 ParametersData *pdata = g_new0(ParametersData, 1);
2061 debug_print("procmime_write_mime_header\n");
2064 pdata->ascii_only = FALSE;
2066 for (type_table = mime_type_table; type_table->str != NULL; type_table++)
2067 if (mimeinfo->type == type_table->type) {
2068 gchar *buf = g_strdup_printf(
2069 "Content-Type: %s/%s", type_table->str, mimeinfo->subtype);
2070 fprintf(fp, "%s", buf);
2071 pdata->len = strlen(buf);
2072 pdata->ascii_only = TRUE;
2076 g_hash_table_foreach(mimeinfo->typeparameters, write_parameters, pdata);
2081 if (mimeinfo->encoding_type != ENC_UNKNOWN)
2082 fprintf(fp, "Content-Transfer-Encoding: %s\n", procmime_get_encoding_str(mimeinfo->encoding_type));
2084 if (mimeinfo->description != NULL)
2085 fprintf(fp, "Content-Description: %s\n", mimeinfo->description);
2087 if (mimeinfo->id != NULL)
2088 fprintf(fp, "Content-ID: %s\n", mimeinfo->id);
2090 if (mimeinfo->location != NULL)
2091 fprintf(fp, "Content-Location: %s\n", mimeinfo->location);
2093 if (mimeinfo->disposition != DISPOSITIONTYPE_UNKNOWN) {
2094 ParametersData *pdata = g_new0(ParametersData, 1);
2096 if (mimeinfo->disposition == DISPOSITIONTYPE_INLINE)
2097 buf = g_strdup("Content-Disposition: inline");
2098 else if (mimeinfo->disposition == DISPOSITIONTYPE_ATTACHMENT)
2099 buf = g_strdup("Content-Disposition: attachment");
2101 buf = g_strdup("Content-Disposition: unknown");
2103 fprintf(fp, "%s", buf);
2104 pdata->len = strlen(buf);
2108 pdata->ascii_only = FALSE;
2110 g_hash_table_foreach(mimeinfo->dispositionparameters, write_parameters, pdata);
2118 static gint procmime_write_message_rfc822(MimeInfo *mimeinfo, FILE *fp)
2123 gchar buf[BUFFSIZE];
2124 gboolean skip = FALSE;;
2126 debug_print("procmime_write_message_rfc822\n");
2129 switch (mimeinfo->content) {
2130 case MIMECONTENT_FILE:
2131 if ((infp = g_fopen(mimeinfo->data.filename, "rb")) == NULL) {
2132 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
2135 fseek(infp, mimeinfo->offset, SEEK_SET);
2136 while (fgets(buf, sizeof(buf), infp) == buf) {
2138 if (buf[0] == '\n' && buf[1] == '\0')
2140 if (skip && (buf[0] == ' ' || buf[0] == '\t'))
2142 if (g_ascii_strncasecmp(buf, "Mime-Version:", 13) == 0 ||
2143 g_ascii_strncasecmp(buf, "Content-Type:", 13) == 0 ||
2144 g_ascii_strncasecmp(buf, "Content-Transfer-Encoding:", 26) == 0 ||
2145 g_ascii_strncasecmp(buf, "Content-Description:", 20) == 0 ||
2146 g_ascii_strncasecmp(buf, "Content-ID:", 11) == 0 ||
2147 g_ascii_strncasecmp(buf, "Content-Location:", 17) == 0 ||
2148 g_ascii_strncasecmp(buf, "Content-Disposition:", 20) == 0) {
2152 fwrite(buf, sizeof(gchar), strlen(buf), fp);
2158 case MIMECONTENT_MEM:
2159 fwrite(mimeinfo->data.mem,
2161 strlen(mimeinfo->data.mem),
2169 childnode = mimeinfo->node->children;
2170 if (childnode == NULL)
2173 child = (MimeInfo *) childnode->data;
2174 fprintf(fp, "Mime-Version: 1.0\n");
2175 procmime_write_mime_header(child, fp);
2176 return procmime_write_mimeinfo(child, fp);
2179 static gint procmime_write_multipart(MimeInfo *mimeinfo, FILE *fp)
2183 gchar *boundary, *str, *str2;
2184 gchar buf[BUFFSIZE];
2185 gboolean firstboundary;
2187 debug_print("procmime_write_multipart\n");
2189 boundary = g_hash_table_lookup(mimeinfo->typeparameters, "boundary");
2191 switch (mimeinfo->content) {
2192 case MIMECONTENT_FILE:
2193 if ((infp = g_fopen(mimeinfo->data.filename, "rb")) == NULL) {
2194 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
2197 fseek(infp, mimeinfo->offset, SEEK_SET);
2198 while (fgets(buf, sizeof(buf), infp) == buf) {
2199 if (IS_BOUNDARY(buf, boundary, strlen(boundary)))
2201 fwrite(buf, sizeof(gchar), strlen(buf), fp);
2206 case MIMECONTENT_MEM:
2207 str = g_strdup(mimeinfo->data.mem);
2208 if (((str2 = strstr(str, boundary)) != NULL) && ((str2 - str) >= 2) &&
2209 (*(str2 - 1) == '-') && (*(str2 - 2) == '-'))
2211 fwrite(str, sizeof(gchar), strlen(str), fp);
2219 childnode = mimeinfo->node->children;
2220 firstboundary = TRUE;
2221 while (childnode != NULL) {
2222 MimeInfo *child = childnode->data;
2225 firstboundary = FALSE;
2228 fprintf(fp, "--%s\n", boundary);
2230 procmime_write_mime_header(child, fp);
2231 if (procmime_write_mimeinfo(child, fp) < 0)
2234 childnode = g_node_next_sibling(childnode);
2236 fprintf(fp, "\n--%s--\n", boundary);
2241 gint procmime_write_mimeinfo(MimeInfo *mimeinfo, FILE *fp)
2245 debug_print("procmime_write_mimeinfo\n");
2247 if (G_NODE_IS_LEAF(mimeinfo->node)) {
2248 switch (mimeinfo->content) {
2249 case MIMECONTENT_FILE:
2250 if ((infp = g_fopen(mimeinfo->data.filename, "rb")) == NULL) {
2251 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
2254 copy_file_part_to_fp(infp, mimeinfo->offset, mimeinfo->length, fp);
2258 case MIMECONTENT_MEM:
2259 fwrite(mimeinfo->data.mem,
2261 strlen(mimeinfo->data.mem),
2269 /* Call writer for mime type */
2270 switch (mimeinfo->type) {
2271 case MIMETYPE_MESSAGE:
2272 if (g_ascii_strcasecmp(mimeinfo->subtype, "rfc822") == 0) {
2273 return procmime_write_message_rfc822(mimeinfo, fp);
2277 case MIMETYPE_MULTIPART:
2278 return procmime_write_multipart(mimeinfo, fp);
2290 gchar *procmime_get_part_file_name(MimeInfo *mimeinfo)
2294 if ((mimeinfo->type == MIMETYPE_TEXT) && !g_ascii_strcasecmp(mimeinfo->subtype, "html"))
2295 base = g_strdup("mimetmp.html");
2297 const gchar *basetmp;
2300 basetmp = procmime_mimeinfo_get_parameter(mimeinfo, "filename");
2301 if (basetmp == NULL)
2302 basetmp = procmime_mimeinfo_get_parameter(mimeinfo, "name");
2303 if (basetmp == NULL)
2304 basetmp = "mimetmp";
2305 basename = g_path_get_basename(basetmp);
2306 if (*basename == '\0') {
2308 basename = g_strdup("mimetmp");
2310 base = conv_filename_from_utf8(basename);
2312 subst_for_shellsafe_filename(base);