2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2001 Hiroyuki Yamamoto
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
34 #include "procheader.h"
41 #include "prefs_common.h"
47 static GHashTable *procmime_get_mime_type_table (void);
49 MimeInfo *procmime_mimeinfo_new(void)
53 mimeinfo = g_new0(MimeInfo, 1);
54 mimeinfo->mime_type = MIME_UNKNOWN;
55 mimeinfo->encoding_type = ENC_UNKNOWN;
60 void procmime_mimeinfo_free(MimeInfo *mimeinfo)
62 if (!mimeinfo) return;
64 g_free(mimeinfo->encoding);
65 g_free(mimeinfo->content_type);
66 g_free(mimeinfo->charset);
67 g_free(mimeinfo->name);
68 g_free(mimeinfo->boundary);
69 g_free(mimeinfo->content_disposition);
70 g_free(mimeinfo->filename);
72 g_free(mimeinfo->plaintextfile);
73 g_free(mimeinfo->sigstatus);
74 g_free(mimeinfo->sigstatus_full);
77 procmime_mimeinfo_free(mimeinfo->sub);
82 void procmime_mimeinfo_free_all(MimeInfo *mimeinfo)
84 while (mimeinfo != NULL) {
87 g_free(mimeinfo->encoding);
88 g_free(mimeinfo->content_type);
89 g_free(mimeinfo->charset);
90 g_free(mimeinfo->name);
91 g_free(mimeinfo->boundary);
92 g_free(mimeinfo->content_disposition);
93 g_free(mimeinfo->filename);
95 g_free(mimeinfo->plaintextfile);
96 g_free(mimeinfo->sigstatus);
97 g_free(mimeinfo->sigstatus_full);
100 procmime_mimeinfo_free_all(mimeinfo->sub);
101 procmime_mimeinfo_free_all(mimeinfo->children);
103 procmime_mimeinfo_free_all(mimeinfo->plaintext);
106 next = mimeinfo->next;
112 MimeInfo *procmime_mimeinfo_insert(MimeInfo *parent, MimeInfo *mimeinfo)
114 MimeInfo *child = parent->children;
117 parent->children = mimeinfo;
119 while (child->next != NULL)
122 child->next = mimeinfo;
125 mimeinfo->parent = parent;
126 mimeinfo->level = parent->level + 1;
131 void procmime_mimeinfo_replace(MimeInfo *old, MimeInfo *new)
133 MimeInfo *parent = old->parent;
137 g_warning("oops: Not top message");
141 g_message("oops: new child should not have a sibling");
145 for (child = parent->children; child && child != old;
149 g_warning("oops: parent can't find it's own child");
152 procmime_mimeinfo_free_all(old);
154 if (child == parent->children) {
155 new->next = parent->children->next;
156 parent->children = new;
158 new->next = child->next;
163 MimeInfo *procmime_mimeinfo_next(MimeInfo *mimeinfo)
165 if (!mimeinfo) return NULL;
167 if (mimeinfo->children)
168 return mimeinfo->children;
170 return mimeinfo->sub;
172 return mimeinfo->next;
174 for (mimeinfo = mimeinfo->parent; mimeinfo != NULL;
175 mimeinfo = mimeinfo->parent) {
177 return mimeinfo->next;
178 if (mimeinfo->main) {
179 mimeinfo = mimeinfo->main;
181 return mimeinfo->next;
188 MimeInfo *procmime_scan_message(MsgInfo *msginfo)
193 g_return_val_if_fail(msginfo != NULL, NULL);
195 if ((fp = procmsg_open_message(msginfo)) == NULL) return NULL;
196 mimeinfo = procmime_scan_mime_header(fp);
199 if (mimeinfo->mime_type != MIME_MULTIPART) {
200 if (fseek(fp, mimeinfo->fpos, SEEK_SET) < 0)
203 if (mimeinfo->mime_type != MIME_TEXT)
204 procmime_scan_multipart_message(mimeinfo, fp);
208 if (prefs_common.auto_check_signatures)
209 rfc2015_check_signature(mimeinfo, fp);
216 void procmime_scan_multipart_message(MimeInfo *mimeinfo, FILE *fp)
220 gint boundary_len = 0;
222 glong fpos, prev_fpos;
225 g_return_if_fail(mimeinfo != NULL);
226 g_return_if_fail(mimeinfo->mime_type != MIME_TEXT);
228 if (mimeinfo->mime_type == MIME_MULTIPART) {
229 g_return_if_fail(mimeinfo->boundary != NULL);
230 g_return_if_fail(mimeinfo->sub == NULL);
232 g_return_if_fail(fp != NULL);
234 boundary = mimeinfo->boundary;
237 boundary_len = strlen(boundary);
239 /* look for first boundary */
240 while ((p = fgets(buf, sizeof(buf), fp)) != NULL)
241 if (IS_BOUNDARY(buf, boundary, boundary_len)) break;
245 if ((fpos = ftell(fp)) < 0) {
250 for (npart = 0;; npart++) {
252 gboolean eom = FALSE;
256 partinfo = procmime_scan_mime_header(fp);
257 if (!partinfo) break;
258 procmime_mimeinfo_insert(mimeinfo, partinfo);
260 if (partinfo->mime_type == MIME_MULTIPART) {
261 if (partinfo->level < 8)
262 procmime_scan_multipart_message(partinfo, fp);
263 } else if (partinfo->mime_type == MIME_MESSAGE_RFC822) {
266 partinfo->sub = sub = procmime_scan_mime_header(fp);
268 sub->level = partinfo->level + 1;
269 sub->parent = partinfo->parent;
270 sub->main = partinfo;
272 if (sub->mime_type == MIME_MULTIPART) {
274 procmime_scan_multipart_message
279 /* look for next boundary */
281 while ((p = fgets(buf, sizeof(buf), fp)) != NULL) {
282 if (IS_BOUNDARY(buf, boundary, boundary_len)) {
283 if (buf[2 + boundary_len] == '-' &&
284 buf[2 + boundary_len + 1] == '-')
290 eom = TRUE; /* broken MIME message */
293 partinfo->size = fpos - prev_fpos - strlen(buf);
297 /*g_message ("** at " __PRETTY_FUNCTION__ ":%d:", __LINE__);*/
300 void procmime_scan_encoding(MimeInfo *mimeinfo, const gchar *encoding)
304 Xstrdup_a(buf, encoding, return);
306 g_free(mimeinfo->encoding);
308 mimeinfo->encoding = g_strdup(g_strstrip(buf));
309 if (!strcasecmp(buf, "7bit"))
310 mimeinfo->encoding_type = ENC_7BIT;
311 else if (!strcasecmp(buf, "8bit"))
312 mimeinfo->encoding_type = ENC_8BIT;
313 else if (!strcasecmp(buf, "quoted-printable"))
314 mimeinfo->encoding_type = ENC_QUOTED_PRINTABLE;
315 else if (!strcasecmp(buf, "base64"))
316 mimeinfo->encoding_type = ENC_BASE64;
317 else if (!strcasecmp(buf, "x-uuencode"))
318 mimeinfo->encoding_type = ENC_X_UUENCODE;
320 mimeinfo->encoding_type = ENC_UNKNOWN;
324 void procmime_scan_content_type(MimeInfo *mimeinfo, const gchar *content_type)
326 gchar *delim, *p, *cnttype;
329 if (conv_get_current_charset() == C_EUC_JP &&
330 strchr(content_type, '\033')) {
332 len = strlen(content_type) * 2 + 1;
333 Xalloca(buf, len, return);
334 conv_jistoeuc(buf, len, content_type);
336 Xstrdup_a(buf, content_type, return);
338 g_free(mimeinfo->content_type);
339 g_free(mimeinfo->charset);
340 g_free(mimeinfo->name);
341 mimeinfo->content_type = NULL;
342 mimeinfo->charset = NULL;
343 mimeinfo->name = NULL;
345 if ((delim = strchr(buf, ';'))) *delim = '\0';
346 mimeinfo->content_type = cnttype = g_strdup(g_strstrip(buf));
348 mimeinfo->mime_type = procmime_scan_mime_type(cnttype);
357 if ((delim = strchr(p, ';'))) *delim = '\0';
359 if (!(eq = strchr(p, '='))) break;
368 extract_quote(value, '"');
370 eliminate_parenthesis(value, '(', ')');
375 if (!strcasecmp(attr, "charset"))
376 mimeinfo->charset = g_strdup(value);
377 else if (!strcasecmp(attr, "name")) {
381 len = strlen(value) + 1;
382 Xalloca(tmp, len, return);
383 conv_unmime_header(tmp, len, value, NULL);
384 mimeinfo->name = g_strdup(tmp);
385 } else if (!strcasecmp(attr, "boundary"))
386 mimeinfo->boundary = g_strdup(value);
393 if (mimeinfo->mime_type == MIME_MULTIPART && !mimeinfo->boundary)
394 mimeinfo->mime_type = MIME_TEXT;
397 void procmime_scan_content_disposition(MimeInfo *mimeinfo,
398 const gchar *content_disposition)
400 gchar *delim, *p, *dispos;
403 if (conv_get_current_charset() == C_EUC_JP &&
404 strchr(content_disposition, '\033')) {
406 len = strlen(content_disposition) * 2 + 1;
407 Xalloca(buf, len, return);
408 conv_jistoeuc(buf, len, content_disposition);
410 Xstrdup_a(buf, content_disposition, return);
412 if ((delim = strchr(buf, ';'))) *delim = '\0';
413 mimeinfo->content_disposition = dispos = g_strdup(g_strstrip(buf));
422 if ((delim = strchr(p, ';'))) *delim = '\0';
424 if (!(eq = strchr(p, '='))) break;
433 extract_quote(value, '"');
435 eliminate_parenthesis(value, '(', ')');
440 if (!strcasecmp(attr, "filename")) {
444 len = strlen(value) + 1;
445 Xalloca(tmp, len, return);
446 conv_unmime_header(tmp, len, value, NULL);
447 g_free(mimeinfo->filename);
448 mimeinfo->filename = g_strdup(tmp);
460 H_CONTENT_TRANSFER_ENCODING = 0,
462 H_CONTENT_DISPOSITION = 2
465 MimeInfo *procmime_scan_mime_header(FILE *fp)
467 static HeaderEntry hentry[] = {{"Content-Transfer-Encoding:",
469 {"Content-Type:", NULL, TRUE},
470 {"Content-Disposition:",
472 {NULL, NULL, FALSE}};
478 g_return_val_if_fail(fp != NULL, NULL);
480 mimeinfo = procmime_mimeinfo_new();
481 mimeinfo->mime_type = MIME_TEXT;
482 mimeinfo->encoding_type = ENC_7BIT;
483 mimeinfo->fpos = ftell(fp);
485 while ((hnum = procheader_get_one_field(buf, sizeof(buf), fp, hentry))
489 if (H_CONTENT_TRANSFER_ENCODING == hnum) {
490 procmime_scan_encoding
491 (mimeinfo, buf + strlen(hp->name));
492 } else if (H_CONTENT_TYPE == hnum) {
493 procmime_scan_content_type
494 (mimeinfo, buf + strlen(hp->name));
495 } else if (H_CONTENT_DISPOSITION == hnum) {
496 procmime_scan_content_disposition
497 (mimeinfo, buf + strlen(hp->name));
501 if (mimeinfo->mime_type == MIME_APPLICATION_OCTET_STREAM &&
504 type = procmime_get_mime_type(mimeinfo->name);
506 mimeinfo->mime_type = procmime_scan_mime_type(type);
509 if (!mimeinfo->content_type)
510 mimeinfo->content_type = g_strdup("text/plain");
515 FILE *procmime_decode_content(FILE *outfp, FILE *infp, MimeInfo *mimeinfo)
518 gchar *boundary = NULL;
519 gint boundary_len = 0;
520 gboolean tmp_file = FALSE;
522 g_return_val_if_fail(infp != NULL, NULL);
523 g_return_val_if_fail(mimeinfo != NULL, NULL);
526 outfp = my_tmpfile();
534 if (mimeinfo->parent && mimeinfo->parent->boundary) {
535 boundary = mimeinfo->parent->boundary;
536 boundary_len = strlen(boundary);
539 if (mimeinfo->encoding_type == ENC_QUOTED_PRINTABLE) {
540 gboolean softline = FALSE;
542 while (fgets(buf, sizeof(buf), infp) != NULL &&
544 !IS_BOUNDARY(buf, boundary, boundary_len))) {
547 softline = DoOneQPLine(&p, FALSE, softline);
548 fwrite(buf, p - (guchar *)buf, 1, outfp);
550 } else if (mimeinfo->encoding_type == ENC_BASE64) {
551 gchar outbuf[BUFFSIZE];
553 Base64Decoder *decoder;
555 decoder = base64_decoder_new();
556 while (fgets(buf, sizeof(buf), infp) != NULL &&
558 !IS_BOUNDARY(buf, boundary, boundary_len))) {
559 len = base64_decoder_decode(decoder, buf, outbuf);
561 g_warning("Bad BASE64 content\n");
564 fwrite(outbuf, sizeof(gchar), len, outfp);
566 base64_decoder_free(decoder);
567 } else if (mimeinfo->encoding_type == ENC_X_UUENCODE) {
568 gchar outbuf[BUFFSIZE];
570 gboolean flag = FALSE;
572 while (fgets(buf, sizeof(buf), infp) != NULL &&
574 !IS_BOUNDARY(buf, boundary, boundary_len))) {
575 if(!flag && strncmp(buf,"begin ", 6)) continue;
578 len = fromuutobits(outbuf, buf);
581 g_warning("Bad UUENCODE content(%d)\n", len);
584 fwrite(outbuf, sizeof(gchar), len, outfp);
589 while (fgets(buf, sizeof(buf), infp) != NULL &&
591 !IS_BOUNDARY(buf, boundary, boundary_len))) {
596 if (tmp_file) rewind(outfp);
600 gint procmime_get_part(const gchar *outfile, const gchar *infile,
606 g_return_val_if_fail(outfile != NULL, -1);
607 g_return_val_if_fail(infile != NULL, -1);
608 g_return_val_if_fail(mimeinfo != NULL, -1);
610 if ((infp = fopen(infile, "r")) == NULL) {
611 FILE_OP_ERROR(infile, "fopen");
614 if (fseek(infp, mimeinfo->fpos, SEEK_SET) < 0) {
615 FILE_OP_ERROR(infile, "fseek");
619 if ((outfp = fopen(outfile, "w")) == NULL) {
620 FILE_OP_ERROR(outfile, "fopen");
625 while (fgets(buf, sizeof(buf), infp) != NULL)
626 if (buf[0] == '\r' || buf[0] == '\n') break;
628 procmime_decode_content(outfp, infp, mimeinfo);
631 if (fclose(outfp) == EOF) {
632 FILE_OP_ERROR(outfile, "fclose");
640 FILE *procmime_get_text_content(MimeInfo *mimeinfo, FILE *infp)
644 gboolean conv_fail = FALSE;
648 g_return_val_if_fail(mimeinfo != NULL, NULL);
649 g_return_val_if_fail(infp != NULL, NULL);
650 g_return_val_if_fail(mimeinfo->mime_type == MIME_TEXT ||
651 mimeinfo->mime_type == MIME_TEXT_HTML, NULL);
653 if (fseek(infp, mimeinfo->fpos, SEEK_SET) < 0) {
658 while (fgets(buf, sizeof(buf), infp) != NULL)
659 if (buf[0] == '\r' || buf[0] == '\n') break;
661 tmpfp = procmime_decode_content(NULL, infp, mimeinfo);
665 if ((outfp = my_tmpfile()) == NULL) {
671 src_codeset = prefs_common.force_charset
672 ? prefs_common.force_charset : mimeinfo->charset;
674 if (mimeinfo->mime_type == MIME_TEXT) {
675 while (fgets(buf, sizeof(buf), tmpfp) != NULL) {
676 str = conv_codeset_strdup(buf, src_codeset, NULL);
685 } else if (mimeinfo->mime_type == MIME_TEXT_HTML) {
689 conv = conv_code_converter_new(src_codeset);
690 parser = html_parser_new(tmpfp, conv);
691 while ((str = html_parse(parser)) != NULL) {
694 html_parser_destroy(parser);
695 conv_code_converter_destroy(conv);
699 g_warning(_("procmime_get_text_content(): Code conversion failed.\n"));
707 /* search the first text part of (multipart) MIME message,
708 decode, convert it and output to outfp. */
709 FILE *procmime_get_first_text_content(MsgInfo *msginfo)
711 FILE *infp, *outfp = NULL;
712 MimeInfo *mimeinfo, *partinfo;
714 g_return_val_if_fail(msginfo != NULL, NULL);
716 mimeinfo = procmime_scan_message(msginfo);
717 if (!mimeinfo) return NULL;
719 if ((infp = procmsg_open_message(msginfo)) == NULL) {
720 procmime_mimeinfo_free_all(mimeinfo);
725 while (partinfo && partinfo->mime_type != MIME_TEXT)
726 partinfo = procmime_mimeinfo_next(partinfo);
729 while (partinfo && partinfo->mime_type != MIME_TEXT_HTML)
730 partinfo = procmime_mimeinfo_next(partinfo);
734 outfp = procmime_get_text_content(partinfo, infp);
737 procmime_mimeinfo_free_all(mimeinfo);
742 gboolean procmime_find_string_part(MimeInfo *mimeinfo, const gchar *filename,
743 const gchar *str, gboolean case_sens)
748 gchar *(* StrFindFunc) (const gchar *haystack, const gchar *needle);
750 g_return_val_if_fail(mimeinfo != NULL, FALSE);
751 g_return_val_if_fail(mimeinfo->mime_type == MIME_TEXT ||
752 mimeinfo->mime_type == MIME_TEXT_HTML, FALSE);
753 g_return_val_if_fail(str != NULL, FALSE);
755 if ((infp = fopen(filename, "r")) == NULL) {
756 FILE_OP_ERROR(filename, "fopen");
760 outfp = procmime_get_text_content(mimeinfo, infp);
767 StrFindFunc = strstr;
769 StrFindFunc = strcasestr;
771 while (fgets(buf, sizeof(buf), outfp) != NULL) {
772 if (StrFindFunc(buf, str) != NULL) {
783 gboolean procmime_find_string(MsgInfo *msginfo, const gchar *str,
789 gboolean found = FALSE;
791 g_return_val_if_fail(msginfo != NULL, FALSE);
792 g_return_val_if_fail(str != NULL, FALSE);
794 filename = procmsg_get_message_file(msginfo);
795 if (!filename) return FALSE;
796 mimeinfo = procmime_scan_message(msginfo);
798 for (partinfo = mimeinfo; partinfo != NULL;
799 partinfo = procmime_mimeinfo_next(partinfo)) {
800 if (partinfo->mime_type == MIME_TEXT ||
801 partinfo->mime_type == MIME_TEXT_HTML) {
802 if (procmime_find_string_part
803 (partinfo, filename, str, case_sens) == TRUE) {
810 procmime_mimeinfo_free_all(mimeinfo);
816 gchar *procmime_get_tmp_file_name(MimeInfo *mimeinfo)
818 static guint32 id = 0;
823 g_return_val_if_fail(mimeinfo != NULL, NULL);
825 g_snprintf(f_prefix, sizeof(f_prefix), "%08x.", id++);
827 if (MIME_TEXT_HTML == mimeinfo->mime_type)
828 base = "mimetmp.html";
830 base = mimeinfo->filename ? mimeinfo->filename
831 : mimeinfo->name ? mimeinfo->name : "mimetmp";
832 base = g_basename(base);
833 if (*base == '\0') base = "mimetmp";
836 filename = g_strconcat(get_mime_tmp_dir(), G_DIR_SEPARATOR_S,
837 f_prefix, base, NULL);
842 ContentType procmime_scan_mime_type(const gchar *mime_type)
846 if (!strncasecmp(mime_type, "text/html", 9))
847 type = MIME_TEXT_HTML;
848 else if (!strncasecmp(mime_type, "text/", 5))
850 else if (!strncasecmp(mime_type, "message/rfc822", 14))
851 type = MIME_MESSAGE_RFC822;
852 else if (!strncasecmp(mime_type, "message/", 8))
854 else if (!strncasecmp(mime_type, "application/octet-stream", 24))
855 type = MIME_APPLICATION_OCTET_STREAM;
856 else if (!strncasecmp(mime_type, "application/", 12))
857 type = MIME_APPLICATION;
858 else if (!strncasecmp(mime_type, "multipart/", 10))
859 type = MIME_MULTIPART;
860 else if (!strncasecmp(mime_type, "image/", 6))
862 else if (!strncasecmp(mime_type, "audio/", 6))
864 else if (!strcasecmp(mime_type, "text"))
872 static GList *mime_type_list = NULL;
874 gchar *procmime_get_mime_type(const gchar *filename)
876 static GHashTable *mime_type_table = NULL;
878 const gchar *ext, *p;
880 if (!mime_type_table) {
881 mime_type_table = procmime_get_mime_type_table();
882 if (!mime_type_table) return NULL;
885 filename = g_basename(filename);
886 p = strrchr(filename, '.');
892 mime_type = g_hash_table_lookup(mime_type_table, ext);
896 str = g_strconcat(mime_type->type, "/", mime_type->sub_type,
904 static guint procmime_str_hash(gconstpointer gptr)
906 guint hash_result = 0;
909 for (str = gptr; str && *str; str++) {
910 if (isupper(*str)) hash_result += (*str + ' ');
911 else hash_result += *str;
917 static gint procmime_str_equal(gconstpointer gptr1, gconstpointer gptr2)
919 const char *str1 = gptr1;
920 const char *str2 = gptr2;
922 return !strcasecmp(str1, str2);
925 static GHashTable *procmime_get_mime_type_table(void)
927 GHashTable *table = NULL;
932 if (!mime_type_list) {
933 mime_type_list = procmime_get_mime_type_list();
934 if (!mime_type_list) return NULL;
937 table = g_hash_table_new(procmime_str_hash, procmime_str_equal);
939 for (cur = mime_type_list; cur != NULL; cur = cur->next) {
942 mime_type = (MimeType *)cur->data;
944 if (!mime_type->extension) continue;
946 exts = g_strsplit(mime_type->extension, " ", 16);
947 for (i = 0; exts[i] != NULL; i++)
948 g_hash_table_insert(table, g_strdup(exts[i]),
956 GList *procmime_get_mime_type_list(void)
965 return mime_type_list;
967 if ((fp = fopen("/etc/mime.types", "r")) == NULL) {
968 if ((fp = fopen(SYSCONFDIR "/mime.types", "r")) == NULL) {
969 FILE_OP_ERROR(SYSCONFDIR "/mime.types", "fopen");
974 while (fgets(buf, sizeof(buf), fp) != NULL) {
975 p = strchr(buf, '#');
980 while (*p && !isspace(*p)) p++;
985 delim = strchr(buf, '/');
986 if (delim == NULL) continue;
989 mime_type = g_new(MimeType, 1);
990 mime_type->type = g_strdup(buf);
991 mime_type->sub_type = g_strdup(delim + 1);
993 while (*p && isspace(*p)) p++;
995 mime_type->extension = g_strdup(p);
997 mime_type->extension = NULL;
999 list = g_list_append(list, mime_type);
1005 g_warning("Can't read mime.types\n");
1010 EncodingType procmime_get_encoding_for_charset(const gchar *charset)
1014 else if (!strncasecmp(charset, "ISO-2022-", 9) ||
1015 !strcasecmp(charset, "US-ASCII"))
1019 /* return ENC_BASE64; */
1020 /* return ENC_QUOTED_PRINTABLE; */
1023 const gchar *procmime_get_encoding_str(EncodingType encoding)
1025 static const gchar *encoding_str[] = {
1026 "7bit", "8bit", "quoted-printable", "base64", "x-uuencode",
1030 if (encoding >= ENC_7BIT && encoding <= ENC_UNKNOWN)
1031 return encoding_str[encoding];