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 ((fp = g_fopen("/usr/share/mime/globs", "rb")) == NULL) {
1127 fp_is_glob_file = FALSE;
1128 if ((fp = g_fopen("/etc/mime.types", "rb")) == NULL) {
1129 if ((fp = g_fopen(SYSCONFDIR "/mime.types", "rb"))
1131 FILE_OP_ERROR(SYSCONFDIR "/mime.types",
1138 while (fgets(buf, sizeof(buf), fp) != NULL) {
1139 p = strchr(buf, '#');
1145 if (fp_is_glob_file) {
1146 while (*p && !g_ascii_isspace(*p) && (*p!=':')) p++;
1148 while (*p && !g_ascii_isspace(*p)) p++;
1155 delim = strchr(buf, '/');
1156 if (delim == NULL) continue;
1159 mime_type = g_new(MimeType, 1);
1160 mime_type->type = g_strdup(buf);
1161 mime_type->sub_type = g_strdup(delim + 1);
1163 if (fp_is_glob_file) {
1164 while (*p && (g_ascii_isspace(*p)||(*p=='*')||(*p=='.'))) p++;
1166 while (*p && g_ascii_isspace(*p)) p++;
1170 mime_type->extension = g_strdup(p);
1172 mime_type->extension = NULL;
1174 list = g_list_append(list, mime_type);
1180 g_warning("Can't read mime.types\n");
1185 EncodingType procmime_get_encoding_for_charset(const gchar *charset)
1189 else if (!g_ascii_strncasecmp(charset, "ISO-2022-", 9) ||
1190 !g_ascii_strcasecmp(charset, "US-ASCII"))
1192 else if (!g_ascii_strcasecmp(charset, "ISO-8859-5") ||
1193 !g_ascii_strncasecmp(charset, "KOI8-", 5) ||
1194 !g_ascii_strcasecmp(charset, "Windows-1251"))
1196 else if (!g_ascii_strncasecmp(charset, "ISO-8859-", 9))
1197 return ENC_QUOTED_PRINTABLE;
1198 else if (!g_ascii_strncasecmp(charset, "UTF-8", 5))
1199 return ENC_QUOTED_PRINTABLE;
1204 EncodingType procmime_get_encoding_for_text_file(const gchar *file, gboolean *has_binary)
1207 guchar buf[BUFFSIZE];
1209 size_t octet_chars = 0;
1210 size_t total_len = 0;
1211 gfloat octet_percentage;
1212 gboolean force_b64 = FALSE;
1214 if ((fp = g_fopen(file, "rb")) == NULL) {
1215 FILE_OP_ERROR(file, "fopen");
1219 while ((len = fread(buf, sizeof(guchar), sizeof(buf), fp)) > 0) {
1223 for (p = buf, i = 0; i < len; ++p, ++i) {
1237 octet_percentage = (gfloat)octet_chars / (gfloat)total_len;
1239 octet_percentage = 0.0;
1241 debug_print("procmime_get_encoding_for_text_file(): "
1242 "8bit chars: %d / %d (%f%%)\n", octet_chars, total_len,
1243 100.0 * octet_percentage);
1245 if (octet_percentage > 0.20 || force_b64) {
1246 debug_print("using BASE64\n");
1248 } else if (octet_chars > 0) {
1249 debug_print("using quoted-printable\n");
1250 return ENC_QUOTED_PRINTABLE;
1252 debug_print("using 7bit\n");
1257 struct EncodingTable
1260 EncodingType enc_type;
1263 struct EncodingTable encoding_table[] = {
1266 {"binary", ENC_BINARY},
1267 {"quoted-printable", ENC_QUOTED_PRINTABLE},
1268 {"base64", ENC_BASE64},
1269 {"x-uuencode", ENC_UNKNOWN},
1270 {NULL, ENC_UNKNOWN},
1273 const gchar *procmime_get_encoding_str(EncodingType encoding)
1275 struct EncodingTable *enc_table;
1277 for (enc_table = encoding_table; enc_table->str != NULL; enc_table++) {
1278 if (enc_table->enc_type == encoding)
1279 return enc_table->str;
1284 /* --- NEW MIME STUFF --- */
1291 static struct TypeTable mime_type_table[] = {
1292 {"text", MIMETYPE_TEXT},
1293 {"image", MIMETYPE_IMAGE},
1294 {"audio", MIMETYPE_AUDIO},
1295 {"video", MIMETYPE_VIDEO},
1296 {"application", MIMETYPE_APPLICATION},
1297 {"message", MIMETYPE_MESSAGE},
1298 {"multipart", MIMETYPE_MULTIPART},
1302 const gchar *procmime_get_media_type_str(MimeMediaType type)
1304 struct TypeTable *type_table;
1306 for (type_table = mime_type_table; type_table->str != NULL; type_table++) {
1307 if (type_table->type == type)
1308 return type_table->str;
1313 MimeMediaType procmime_get_media_type(const gchar *str)
1315 struct TypeTable *typetablearray;
1317 for (typetablearray = mime_type_table; typetablearray->str != NULL; typetablearray++)
1318 if (g_ascii_strncasecmp(str, typetablearray->str, strlen(typetablearray->str)) == 0)
1319 return typetablearray->type;
1321 return MIMETYPE_UNKNOWN;
1325 *\brief Safe wrapper for content type string.
1327 *\return const gchar * Pointer to content type string.
1329 gchar *procmime_get_content_type_str(MimeMediaType type,
1330 const char *subtype)
1332 const gchar *type_str = NULL;
1334 if (subtype == NULL || !(type_str = procmime_get_media_type_str(type)))
1335 return g_strdup("unknown");
1336 return g_strdup_printf("%s/%s", type_str, subtype);
1339 static int procmime_parse_mimepart(MimeInfo *parent,
1340 gchar *content_type,
1341 gchar *content_encoding,
1342 gchar *content_description,
1344 gchar *content_disposition,
1345 gchar *content_location,
1346 const gchar *filename,
1350 void procmime_parse_message_rfc822(MimeInfo *mimeinfo)
1352 HeaderEntry hentry[] = {{"Content-Type:", NULL, TRUE},
1353 {"Content-Transfer-Encoding:",
1355 {"Content-Description:",
1359 {"Content-Disposition:",
1361 {"Content-Location:",
1365 {NULL, NULL, FALSE}};
1366 guint content_start, i;
1370 procmime_decode_content(mimeinfo);
1372 fp = g_fopen(mimeinfo->data.filename, "rb");
1374 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
1377 fseek(fp, mimeinfo->offset, SEEK_SET);
1378 procheader_get_header_fields(fp, hentry);
1379 if (hentry[0].body != NULL) {
1380 tmp = conv_unmime_header(hentry[0].body, NULL);
1381 g_free(hentry[0].body);
1382 hentry[0].body = tmp;
1384 if (hentry[2].body != NULL) {
1385 tmp = conv_unmime_header(hentry[2].body, NULL);
1386 g_free(hentry[2].body);
1387 hentry[2].body = tmp;
1389 if (hentry[4].body != NULL) {
1390 tmp = conv_unmime_header(hentry[4].body, NULL);
1391 g_free(hentry[4].body);
1392 hentry[4].body = tmp;
1394 if (hentry[5].body != NULL) {
1395 tmp = conv_unmime_header(hentry[5].body, NULL);
1396 g_free(hentry[5].body);
1397 hentry[5].body = tmp;
1399 content_start = ftell(fp);
1402 procmime_parse_mimepart(mimeinfo,
1403 hentry[0].body, hentry[1].body,
1404 hentry[2].body, hentry[3].body,
1405 hentry[4].body, hentry[5].body,
1406 mimeinfo->data.filename, content_start,
1407 mimeinfo->length - (content_start - mimeinfo->offset));
1409 for (i = 0; i < (sizeof hentry / sizeof hentry[0]); i++) {
1410 g_free(hentry[i].body);
1411 hentry[i].body = NULL;
1415 void procmime_parse_multipart(MimeInfo *mimeinfo)
1417 HeaderEntry hentry[] = {{"Content-Type:", NULL, TRUE},
1418 {"Content-Transfer-Encoding:",
1420 {"Content-Description:",
1424 {"Content-Disposition:",
1426 {"Content-Location:",
1428 {NULL, NULL, FALSE}};
1431 gint boundary_len = 0, lastoffset = -1, i;
1432 gchar buf[BUFFSIZE];
1436 boundary = g_hash_table_lookup(mimeinfo->typeparameters, "boundary");
1439 boundary_len = strlen(boundary);
1441 procmime_decode_content(mimeinfo);
1443 fp = g_fopen(mimeinfo->data.filename, "rb");
1445 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
1448 fseek(fp, mimeinfo->offset, SEEK_SET);
1449 while ((p = fgets(buf, sizeof(buf), fp)) != NULL && result == 0) {
1450 if (ftell(fp) - 1 > (mimeinfo->offset + mimeinfo->length))
1453 if (IS_BOUNDARY(buf, boundary, boundary_len)) {
1454 if (lastoffset != -1) {
1455 result = procmime_parse_mimepart(mimeinfo,
1456 hentry[0].body, hentry[1].body,
1457 hentry[2].body, hentry[3].body,
1458 hentry[4].body, hentry[5].body,
1459 mimeinfo->data.filename, lastoffset,
1460 (ftell(fp) - strlen(buf)) - lastoffset - 1);
1463 if (buf[2 + boundary_len] == '-' &&
1464 buf[2 + boundary_len + 1] == '-')
1467 for (i = 0; i < (sizeof hentry / sizeof hentry[0]) ; i++) {
1468 g_free(hentry[i].body);
1469 hentry[i].body = NULL;
1471 procheader_get_header_fields(fp, hentry);
1472 if (hentry[0].body != NULL) {
1473 tmp = conv_unmime_header(hentry[0].body, NULL);
1474 g_free(hentry[0].body);
1475 hentry[0].body = tmp;
1477 if (hentry[2].body != NULL) {
1478 tmp = conv_unmime_header(hentry[2].body, NULL);
1479 g_free(hentry[2].body);
1480 hentry[2].body = tmp;
1482 if (hentry[4].body != NULL) {
1483 tmp = conv_unmime_header(hentry[4].body, NULL);
1484 g_free(hentry[4].body);
1485 hentry[4].body = tmp;
1487 if (hentry[5].body != NULL) {
1488 tmp = conv_unmime_header(hentry[5].body, NULL);
1489 g_free(hentry[5].body);
1490 hentry[5].body = tmp;
1492 lastoffset = ftell(fp);
1495 for (i = 0; i < (sizeof hentry / sizeof hentry[0]); i++) {
1496 g_free(hentry[i].body);
1497 hentry[i].body = NULL;
1502 static void parse_parameters(const gchar *parameters, GHashTable *table)
1504 gchar *params, *param, *next;
1505 GSList *convlist = NULL, *concatlist = NULL, *cur;
1507 params = g_strdup(parameters);
1510 for (; next != NULL; param = next) {
1511 gchar *attribute, *value, *tmp;
1513 gboolean convert = FALSE;
1515 next = strchr_with_skip_quote(param, '"', ';');
1524 value = strchr(attribute, '=');
1530 while (value[0] == ' ')
1533 g_strdown(attribute);
1535 len = strlen(attribute);
1536 if (attribute[len - 1] == '*') {
1537 gchar *srcpos, *dstpos, *endpos;
1540 attribute[len - 1] = '\0';
1544 endpos = value + strlen(value);
1545 while (srcpos < endpos) {
1551 if (!get_hex_value(&dstvalue, srcpos[1], srcpos[2]))
1562 if (value[0] == '"')
1563 extract_quote(value, '"');
1564 else if ((tmp = strchr(value, ' ')) != NULL)
1569 while (attribute[0] == ' ')
1571 while (attribute[strlen(attribute)-1] == ' ')
1572 attribute[strlen(attribute)-1] = '\0';
1575 while (value[0] == ' ')
1577 while (value[strlen(value)-1] == ' ')
1578 value[strlen(value)-1] = '\0';
1580 if (strrchr(attribute, '*') != NULL) {
1583 tmpattr = g_strdup(attribute);
1584 tmp = strrchr(tmpattr, '*');
1587 if ((tmp[1] == '0') && (tmp[2] == '\0') &&
1588 (g_slist_find_custom(concatlist, attribute, g_str_equal) == NULL))
1589 concatlist = g_slist_prepend(concatlist, g_strdup(tmpattr));
1591 if (convert && (g_slist_find_custom(convlist, attribute, g_str_equal) == NULL))
1592 convlist = g_slist_prepend(convlist, g_strdup(tmpattr));
1595 } else if (convert) {
1596 if (g_slist_find_custom(convlist, attribute, g_str_equal) == NULL)
1597 convlist = g_slist_prepend(convlist, g_strdup(attribute));
1600 if (g_hash_table_lookup(table, attribute) == NULL)
1601 g_hash_table_insert(table, g_strdup(attribute), g_strdup(value));
1604 for (cur = concatlist; cur != NULL; cur = g_slist_next(cur)) {
1605 gchar *attribute, *attrwnum, *partvalue;
1609 attribute = (gchar *) cur->data;
1610 value = g_string_sized_new(64);
1612 attrwnum = g_strdup_printf("%s*%d", attribute, n);
1613 while ((partvalue = g_hash_table_lookup(table, attrwnum)) != NULL) {
1614 g_string_append(value, partvalue);
1618 attrwnum = g_strdup_printf("%s*%d", attribute, n);
1622 g_hash_table_insert(table, g_strdup(attribute), g_strdup(value->str));
1623 g_string_free(value, TRUE);
1625 slist_free_strings(concatlist);
1626 g_slist_free(concatlist);
1628 for (cur = convlist; cur != NULL; cur = g_slist_next(cur)) {
1629 gchar *attribute, *key, *value;
1630 gchar *charset, *lang, *oldvalue, *newvalue;
1632 attribute = (gchar *) cur->data;
1633 if (!g_hash_table_lookup_extended(
1634 table, attribute, (gpointer *)(gchar *) &key, (gpointer *)(gchar *) &value))
1638 lang = strchr(charset, '\'');
1643 oldvalue = strchr(lang, '\'');
1644 if (oldvalue == NULL)
1649 newvalue = conv_codeset_strdup(oldvalue, charset, CS_UTF_8);
1651 g_hash_table_remove(table, attribute);
1655 g_hash_table_insert(table, g_strdup(attribute), newvalue);
1657 slist_free_strings(convlist);
1658 g_slist_free(convlist);
1663 static void procmime_parse_content_type(const gchar *content_type, MimeInfo *mimeinfo)
1665 g_return_if_fail(content_type != NULL);
1666 g_return_if_fail(mimeinfo != NULL);
1668 /* RFC 2045, page 13 says that the mime subtype is MANDATORY;
1669 * if it's not available we use the default Content-Type */
1670 if ((content_type[0] == '\0') || (strchr(content_type, '/') == NULL)) {
1671 mimeinfo->type = MIMETYPE_TEXT;
1672 mimeinfo->subtype = g_strdup("plain");
1673 if (g_hash_table_lookup(mimeinfo->typeparameters,
1674 "charset") == NULL) {
1675 g_hash_table_insert(mimeinfo->typeparameters,
1676 g_strdup("charset"),
1678 conv_get_locale_charset_str_no_utf8()));
1681 gchar *type, *subtype, *params;
1683 type = g_strdup(content_type);
1684 subtype = strchr(type, '/') + 1;
1685 *(subtype - 1) = '\0';
1686 if ((params = strchr(subtype, ';')) != NULL) {
1691 mimeinfo->type = procmime_get_media_type(type);
1692 mimeinfo->subtype = g_strdup(subtype);
1694 /* Get mimeinfo->typeparameters */
1696 parse_parameters(params, mimeinfo->typeparameters);
1702 static void procmime_parse_content_disposition(const gchar *content_disposition, MimeInfo *mimeinfo)
1704 gchar *tmp, *params;
1706 g_return_if_fail(content_disposition != NULL);
1707 g_return_if_fail(mimeinfo != NULL);
1709 tmp = g_strdup(content_disposition);
1710 if ((params = strchr(tmp, ';')) != NULL) {
1716 if (!g_ascii_strcasecmp(tmp, "inline"))
1717 mimeinfo->disposition = DISPOSITIONTYPE_INLINE;
1718 else if (!g_ascii_strcasecmp(tmp, "attachment"))
1719 mimeinfo->disposition = DISPOSITIONTYPE_ATTACHMENT;
1721 mimeinfo->disposition = DISPOSITIONTYPE_ATTACHMENT;
1724 parse_parameters(params, mimeinfo->dispositionparameters);
1730 static void procmime_parse_content_encoding(const gchar *content_encoding, MimeInfo *mimeinfo)
1732 struct EncodingTable *enc_table;
1734 for (enc_table = encoding_table; enc_table->str != NULL; enc_table++) {
1735 if (g_ascii_strcasecmp(enc_table->str, content_encoding) == 0) {
1736 mimeinfo->encoding_type = enc_table->enc_type;
1740 mimeinfo->encoding_type = ENC_UNKNOWN;
1744 static int procmime_parse_mimepart(MimeInfo *parent,
1745 gchar *content_type,
1746 gchar *content_encoding,
1747 gchar *content_description,
1749 gchar *content_disposition,
1750 gchar *content_location,
1751 const gchar *filename,
1757 /* Create MimeInfo */
1758 mimeinfo = procmime_mimeinfo_new();
1759 mimeinfo->content = MIMECONTENT_FILE;
1760 if (parent != NULL) {
1761 if (g_node_depth(parent->node) > 32) {
1762 /* 32 is an arbitrary value
1763 * this avoids DOSsing ourselves
1764 * with enormous messages
1766 procmime_mimeinfo_free_all(mimeinfo);
1769 g_node_append(parent->node, mimeinfo->node);
1771 mimeinfo->data.filename = g_strdup(filename);
1772 mimeinfo->offset = offset;
1773 mimeinfo->length = length;
1775 if (content_type != NULL) {
1776 procmime_parse_content_type(content_type, mimeinfo);
1778 mimeinfo->type = MIMETYPE_TEXT;
1779 mimeinfo->subtype = g_strdup("plain");
1780 if (g_hash_table_lookup(mimeinfo->typeparameters,
1781 "charset") == NULL) {
1782 g_hash_table_insert(mimeinfo->typeparameters,
1783 g_strdup("charset"),
1785 conv_get_locale_charset_str_no_utf8()));
1789 if (content_encoding != NULL) {
1790 procmime_parse_content_encoding(content_encoding, mimeinfo);
1792 mimeinfo->encoding_type = ENC_UNKNOWN;
1795 if (content_description != NULL)
1796 mimeinfo->description = g_strdup(content_description);
1798 mimeinfo->description = NULL;
1800 if (content_id != NULL)
1801 mimeinfo->id = g_strdup(content_id);
1803 mimeinfo->id = NULL;
1805 if (content_location != NULL)
1806 mimeinfo->location = g_strdup(content_location);
1808 mimeinfo->location = NULL;
1810 if (content_disposition != NULL)
1811 procmime_parse_content_disposition(content_disposition, mimeinfo);
1813 mimeinfo->disposition = DISPOSITIONTYPE_UNKNOWN;
1815 /* Call parser for mime type */
1816 switch (mimeinfo->type) {
1817 case MIMETYPE_MESSAGE:
1818 if (g_ascii_strcasecmp(mimeinfo->subtype, "rfc822") == 0) {
1819 procmime_parse_message_rfc822(mimeinfo);
1823 case MIMETYPE_MULTIPART:
1824 procmime_parse_multipart(mimeinfo);
1834 static gchar *typenames[] = {
1845 static gboolean output_func(GNode *node, gpointer data)
1848 MimeInfo *mimeinfo = (MimeInfo *) node->data;
1850 depth = g_node_depth(node);
1851 for (i = 0; i < depth; i++)
1853 printf("%s/%s (offset:%d length:%d encoding: %d)\n", typenames[mimeinfo->type], mimeinfo->subtype, mimeinfo->offset, mimeinfo->length, mimeinfo->encoding_type);
1858 static void output_mime_structure(MimeInfo *mimeinfo, int indent)
1860 g_node_traverse(mimeinfo->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1, output_func, NULL);
1863 MimeInfo *procmime_scan_file_with_offset(const gchar *filename, int offset)
1868 stat(filename, &buf);
1870 mimeinfo = procmime_mimeinfo_new();
1871 mimeinfo->content = MIMECONTENT_FILE;
1872 mimeinfo->encoding_type = ENC_UNKNOWN;
1873 mimeinfo->type = MIMETYPE_MESSAGE;
1874 mimeinfo->subtype = g_strdup("rfc822");
1875 mimeinfo->data.filename = g_strdup(filename);
1876 mimeinfo->offset = offset;
1877 mimeinfo->length = buf.st_size - offset;
1879 procmime_parse_message_rfc822(mimeinfo);
1880 if (debug_get_mode())
1881 output_mime_structure(mimeinfo, 0);
1886 MimeInfo *procmime_scan_file(const gchar *filename)
1890 g_return_val_if_fail(filename != NULL, NULL);
1892 mimeinfo = procmime_scan_file_with_offset(filename, 0);
1897 MimeInfo *procmime_scan_queue_file(const gchar *filename)
1901 gchar buf[BUFFSIZE];
1904 g_return_val_if_fail(filename != NULL, NULL);
1907 if ((fp = g_fopen(filename, "rb")) == NULL)
1909 /* Skip queue header */
1910 while (fgets(buf, sizeof(buf), fp) != NULL) {
1912 if ((!strncmp(buf, "X-Claws-End-Special-Headers: 1",
1913 strlen("X-Claws-End-Special-Headers:"))) ||
1914 (!strncmp(buf, "X-Sylpheed-End-Special-Headers: 1",
1915 strlen("X-Sylpheed-End-Special-Headers:"))))
1918 if (buf[0] == '\r' || buf[0] == '\n') break;
1919 /* from other mailers */
1920 if (!strncmp(buf, "Date: ", 6)
1921 || !strncmp(buf, "To: ", 4)
1922 || !strncmp(buf, "From: ", 6)
1923 || !strncmp(buf, "Subject: ", 9)) {
1931 mimeinfo = procmime_scan_file_with_offset(filename, offset);
1938 ENC_AS_QUOTED_STRING,
1943 typedef struct _ParametersData {
1949 static void write_parameters(gpointer key, gpointer value, gpointer user_data)
1952 gchar *val = value, *valpos, *tmp;
1953 ParametersData *pdata = (ParametersData *)user_data;
1954 GString *buf = g_string_new("");
1956 EncodeAs encas = ENC_AS_TOKEN;
1958 for (valpos = val; *valpos != 0; valpos++) {
1959 if (!IS_ASCII(*valpos) || *valpos == '"') {
1960 encas = ENC_AS_EXTENDED;
1965 if (((*valpos >= 0) && (*valpos < 037)) || (*valpos == 0177)) {
1966 encas = ENC_AS_QUOTED_STRING;
1970 /* tspecials + SPACE */
1988 encas = ENC_AS_QUOTED_STRING;
1993 if (encas == ENC_AS_EXTENDED && pdata->ascii_only == TRUE)
1994 encas = ENC_TO_ASCII;
1998 g_string_append_printf(buf, "%s=%s", param, val);
2002 tmp = g_strdup(val);
2004 " ()<>@,';:\\/[]?=.0123456789"
2005 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
2006 "abcdefghijklmnopqrstuvwxyz",
2008 g_string_append_printf(buf, "%s=\"%s\"", param, tmp);
2012 case ENC_AS_QUOTED_STRING:
2013 g_string_append_printf(buf, "%s=\"%s\"", param, val);
2016 case ENC_AS_EXTENDED:
2017 if (!g_utf8_validate(val, -1, NULL))
2018 g_string_append_printf(buf, "%s*=%s''", param,
2019 conv_get_locale_charset_str());
2021 g_string_append_printf(buf, "%s*=%s''", param,
2023 for (valpos = val; *valpos != '\0'; valpos++) {
2024 if (IS_ASCII(*valpos) && isalnum(*valpos)) {
2025 g_string_append_printf(buf, "%c", *valpos);
2027 gchar hexstr[3] = "XX";
2028 get_hex_str(hexstr, *valpos);
2029 g_string_append_printf(buf, "%%%s", hexstr);
2035 if (buf->str && strlen(buf->str)) {
2036 if (pdata->len + strlen(buf->str) + 2 > 76) {
2037 fprintf(pdata->fp, ";\n %s", buf->str);
2038 pdata->len = strlen(buf->str) + 1;
2040 fprintf(pdata->fp, "; %s", buf->str);
2041 pdata->len += strlen(buf->str) + 2;
2044 g_string_free(buf, TRUE);
2047 void procmime_write_mime_header(MimeInfo *mimeinfo, FILE *fp)
2049 struct TypeTable *type_table;
2050 ParametersData *pdata = g_new0(ParametersData, 1);
2051 debug_print("procmime_write_mime_header\n");
2054 pdata->ascii_only = FALSE;
2056 for (type_table = mime_type_table; type_table->str != NULL; type_table++)
2057 if (mimeinfo->type == type_table->type) {
2058 gchar *buf = g_strdup_printf(
2059 "Content-Type: %s/%s", type_table->str, mimeinfo->subtype);
2060 fprintf(fp, "%s", buf);
2061 pdata->len = strlen(buf);
2062 pdata->ascii_only = TRUE;
2066 g_hash_table_foreach(mimeinfo->typeparameters, write_parameters, pdata);
2071 if (mimeinfo->encoding_type != ENC_UNKNOWN)
2072 fprintf(fp, "Content-Transfer-Encoding: %s\n", procmime_get_encoding_str(mimeinfo->encoding_type));
2074 if (mimeinfo->description != NULL)
2075 fprintf(fp, "Content-Description: %s\n", mimeinfo->description);
2077 if (mimeinfo->id != NULL)
2078 fprintf(fp, "Content-ID: %s\n", mimeinfo->id);
2080 if (mimeinfo->location != NULL)
2081 fprintf(fp, "Content-Location: %s\n", mimeinfo->location);
2083 if (mimeinfo->disposition != DISPOSITIONTYPE_UNKNOWN) {
2084 ParametersData *pdata = g_new0(ParametersData, 1);
2086 if (mimeinfo->disposition == DISPOSITIONTYPE_INLINE)
2087 buf = g_strdup("Content-Disposition: inline");
2088 else if (mimeinfo->disposition == DISPOSITIONTYPE_ATTACHMENT)
2089 buf = g_strdup("Content-Disposition: attachment");
2091 buf = g_strdup("Content-Disposition: unknown");
2093 fprintf(fp, "%s", buf);
2094 pdata->len = strlen(buf);
2098 pdata->ascii_only = FALSE;
2100 g_hash_table_foreach(mimeinfo->dispositionparameters, write_parameters, pdata);
2108 gint procmime_write_message_rfc822(MimeInfo *mimeinfo, FILE *fp)
2113 gchar buf[BUFFSIZE];
2114 gboolean skip = FALSE;;
2116 debug_print("procmime_write_message_rfc822\n");
2119 switch (mimeinfo->content) {
2120 case MIMECONTENT_FILE:
2121 if ((infp = g_fopen(mimeinfo->data.filename, "rb")) == NULL) {
2122 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
2125 fseek(infp, mimeinfo->offset, SEEK_SET);
2126 while (fgets(buf, sizeof(buf), infp) == buf) {
2128 if (buf[0] == '\n' && buf[1] == '\0')
2130 if (skip && (buf[0] == ' ' || buf[0] == '\t'))
2132 if (g_ascii_strncasecmp(buf, "Mime-Version:", 13) == 0 ||
2133 g_ascii_strncasecmp(buf, "Content-Type:", 13) == 0 ||
2134 g_ascii_strncasecmp(buf, "Content-Transfer-Encoding:", 26) == 0 ||
2135 g_ascii_strncasecmp(buf, "Content-Description:", 20) == 0 ||
2136 g_ascii_strncasecmp(buf, "Content-ID:", 11) == 0 ||
2137 g_ascii_strncasecmp(buf, "Content-Location:", 17) == 0 ||
2138 g_ascii_strncasecmp(buf, "Content-Disposition:", 20) == 0) {
2142 fwrite(buf, sizeof(gchar), strlen(buf), fp);
2148 case MIMECONTENT_MEM:
2149 fwrite(mimeinfo->data.mem,
2151 strlen(mimeinfo->data.mem),
2159 childnode = mimeinfo->node->children;
2160 if (childnode == NULL)
2163 child = (MimeInfo *) childnode->data;
2164 fprintf(fp, "Mime-Version: 1.0\n");
2165 procmime_write_mime_header(child, fp);
2166 return procmime_write_mimeinfo(child, fp);
2169 gint procmime_write_multipart(MimeInfo *mimeinfo, FILE *fp)
2173 gchar *boundary, *str, *str2;
2174 gchar buf[BUFFSIZE];
2175 gboolean firstboundary;
2177 debug_print("procmime_write_multipart\n");
2179 boundary = g_hash_table_lookup(mimeinfo->typeparameters, "boundary");
2181 switch (mimeinfo->content) {
2182 case MIMECONTENT_FILE:
2183 if ((infp = g_fopen(mimeinfo->data.filename, "rb")) == NULL) {
2184 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
2187 fseek(infp, mimeinfo->offset, SEEK_SET);
2188 while (fgets(buf, sizeof(buf), infp) == buf) {
2189 if (IS_BOUNDARY(buf, boundary, strlen(boundary)))
2191 fwrite(buf, sizeof(gchar), strlen(buf), fp);
2196 case MIMECONTENT_MEM:
2197 str = g_strdup(mimeinfo->data.mem);
2198 if (((str2 = strstr(str, boundary)) != NULL) && ((str2 - str) >= 2) &&
2199 (*(str2 - 1) == '-') && (*(str2 - 2) == '-'))
2201 fwrite(str, sizeof(gchar), strlen(str), fp);
2209 childnode = mimeinfo->node->children;
2210 firstboundary = TRUE;
2211 while (childnode != NULL) {
2212 MimeInfo *child = childnode->data;
2215 firstboundary = FALSE;
2218 fprintf(fp, "--%s\n", boundary);
2220 procmime_write_mime_header(child, fp);
2221 if (procmime_write_mimeinfo(child, fp) < 0)
2224 childnode = g_node_next_sibling(childnode);
2226 fprintf(fp, "\n--%s--\n", boundary);
2231 gint procmime_write_mimeinfo(MimeInfo *mimeinfo, FILE *fp)
2235 debug_print("procmime_write_mimeinfo\n");
2237 if (G_NODE_IS_LEAF(mimeinfo->node)) {
2238 switch (mimeinfo->content) {
2239 case MIMECONTENT_FILE:
2240 if ((infp = g_fopen(mimeinfo->data.filename, "rb")) == NULL) {
2241 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
2244 copy_file_part_to_fp(infp, mimeinfo->offset, mimeinfo->length, fp);
2248 case MIMECONTENT_MEM:
2249 fwrite(mimeinfo->data.mem,
2251 strlen(mimeinfo->data.mem),
2259 /* Call writer for mime type */
2260 switch (mimeinfo->type) {
2261 case MIMETYPE_MESSAGE:
2262 if (g_ascii_strcasecmp(mimeinfo->subtype, "rfc822") == 0) {
2263 return procmime_write_message_rfc822(mimeinfo, fp);
2267 case MIMETYPE_MULTIPART:
2268 return procmime_write_multipart(mimeinfo, fp);
2280 gchar *procmime_get_part_file_name(MimeInfo *mimeinfo)
2284 if ((mimeinfo->type == MIMETYPE_TEXT) && !g_ascii_strcasecmp(mimeinfo->subtype, "html"))
2285 base = g_strdup("mimetmp.html");
2287 const gchar *basetmp;
2290 basetmp = procmime_mimeinfo_get_parameter(mimeinfo, "filename");
2291 if (basetmp == NULL)
2292 basetmp = procmime_mimeinfo_get_parameter(mimeinfo, "name");
2293 if (basetmp == NULL)
2294 basetmp = "mimetmp";
2295 basename = g_path_get_basename(basetmp);
2296 if (*basename == '\0') {
2298 basename = g_strdup("mimetmp");
2300 base = conv_filename_from_utf8(basename);
2302 subst_for_shellsafe_filename(base);