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"
40 #include "prefs_common.h"
46 static GHashTable *procmime_get_mime_type_table (void);
48 MimeInfo *procmime_mimeinfo_new(void)
52 mimeinfo = g_new0(MimeInfo, 1);
53 mimeinfo->mime_type = MIME_UNKNOWN;
54 mimeinfo->encoding_type = ENC_UNKNOWN;
59 void procmime_mimeinfo_free(MimeInfo *mimeinfo)
61 if (!mimeinfo) return;
63 g_free(mimeinfo->encoding);
64 g_free(mimeinfo->content_type);
65 g_free(mimeinfo->charset);
66 g_free(mimeinfo->name);
67 g_free(mimeinfo->boundary);
68 g_free(mimeinfo->content_disposition);
69 g_free(mimeinfo->filename);
71 g_free(mimeinfo->plaintextfile);
72 g_free(mimeinfo->sigstatus);
73 g_free(mimeinfo->sigstatus_full);
76 procmime_mimeinfo_free(mimeinfo->sub);
81 void procmime_mimeinfo_free_all(MimeInfo *mimeinfo)
83 while (mimeinfo != NULL) {
86 g_free(mimeinfo->encoding);
87 g_free(mimeinfo->content_type);
88 g_free(mimeinfo->charset);
89 g_free(mimeinfo->name);
90 g_free(mimeinfo->boundary);
91 g_free(mimeinfo->content_disposition);
92 g_free(mimeinfo->filename);
94 g_free(mimeinfo->plaintextfile);
95 g_free(mimeinfo->sigstatus);
96 g_free(mimeinfo->sigstatus_full);
99 procmime_mimeinfo_free_all(mimeinfo->sub);
100 procmime_mimeinfo_free_all(mimeinfo->children);
102 procmime_mimeinfo_free_all(mimeinfo->plaintext);
105 next = mimeinfo->next;
111 MimeInfo *procmime_mimeinfo_insert(MimeInfo *parent, MimeInfo *mimeinfo)
113 MimeInfo *child = parent->children;
116 parent->children = mimeinfo;
118 while (child->next != NULL)
121 child->next = mimeinfo;
124 mimeinfo->parent = parent;
125 mimeinfo->level = parent->level + 1;
130 void procmime_mimeinfo_replace(MimeInfo *old, MimeInfo *new)
132 MimeInfo *parent = old->parent;
136 g_warning("oops: Not top message");
140 g_message("oops: new child should not have a sibling");
144 for (child = parent->children; child && child != old;
148 g_warning("oops: parent can't find it's own child");
151 procmime_mimeinfo_free_all(old);
153 if (child == parent->children) {
154 new->next = parent->children->next;
155 parent->children = new;
157 new->next = child->next;
162 MimeInfo *procmime_scan_message(MsgInfo *msginfo)
167 g_return_val_if_fail(msginfo != NULL, NULL);
169 if ((fp = procmsg_open_message(msginfo)) == NULL) return NULL;
170 mimeinfo = procmime_scan_mime_header(fp);
173 if (mimeinfo->mime_type != MIME_MULTIPART) {
174 if (fseek(fp, mimeinfo->fpos, SEEK_SET) < 0)
177 if (mimeinfo->mime_type != MIME_TEXT)
178 procmime_scan_multipart_message(mimeinfo, fp);
182 if (prefs_common.auto_check_signatures)
183 rfc2015_check_signature(mimeinfo, fp);
190 void procmime_scan_multipart_message(MimeInfo *mimeinfo, FILE *fp)
194 gint boundary_len = 0;
196 glong fpos, prev_fpos;
199 g_return_if_fail(mimeinfo != NULL);
200 g_return_if_fail(mimeinfo->mime_type != MIME_TEXT);
202 if (mimeinfo->mime_type == MIME_MULTIPART) {
203 g_return_if_fail(mimeinfo->boundary != NULL);
204 g_return_if_fail(mimeinfo->sub == NULL);
206 g_return_if_fail(fp != NULL);
208 boundary = mimeinfo->boundary;
211 boundary_len = strlen(boundary);
213 /* look for first boundary */
214 while ((p = fgets(buf, sizeof(buf), fp)) != NULL)
215 if (IS_BOUNDARY(buf, boundary, boundary_len)) break;
219 if ((fpos = ftell(fp)) < 0) {
224 for (npart = 0;; npart++) {
226 gboolean eom = FALSE;
230 partinfo = procmime_scan_mime_header(fp);
231 if (!partinfo) break;
232 procmime_mimeinfo_insert(mimeinfo, partinfo);
234 if (partinfo->mime_type == MIME_MULTIPART) {
235 if (partinfo->level < 8)
236 procmime_scan_multipart_message(partinfo, fp);
237 } else if (partinfo->mime_type == MIME_MESSAGE_RFC822) {
240 partinfo->sub = sub = procmime_scan_mime_header(fp);
242 sub->level = partinfo->level + 1;
243 sub->parent = partinfo->parent;
244 sub->main = partinfo;
246 if (sub->mime_type == MIME_MULTIPART) {
248 procmime_scan_multipart_message
253 /* look for next boundary */
255 while ((p = fgets(buf, sizeof(buf), fp)) != NULL) {
256 if (IS_BOUNDARY(buf, boundary, boundary_len)) {
257 if (buf[2 + boundary_len] == '-' &&
258 buf[2 + boundary_len + 1] == '-')
264 eom = TRUE; /* broken MIME message */
267 partinfo->size = fpos - prev_fpos - strlen(buf);
271 /*g_message ("** at " __PRETTY_FUNCTION__ ":%d:", __LINE__);*/
274 void procmime_scan_encoding(MimeInfo *mimeinfo, const gchar *encoding)
278 Xstrdup_a(buf, encoding, return);
280 g_free(mimeinfo->encoding);
282 mimeinfo->encoding = g_strdup(g_strstrip(buf));
283 if (!strcasecmp(buf, "7bit"))
284 mimeinfo->encoding_type = ENC_7BIT;
285 else if (!strcasecmp(buf, "8bit"))
286 mimeinfo->encoding_type = ENC_8BIT;
287 else if (!strcasecmp(buf, "quoted-printable"))
288 mimeinfo->encoding_type = ENC_QUOTED_PRINTABLE;
289 else if (!strcasecmp(buf, "base64"))
290 mimeinfo->encoding_type = ENC_BASE64;
291 else if (!strcasecmp(buf, "x-uuencode"))
292 mimeinfo->encoding_type = ENC_X_UUENCODE;
294 mimeinfo->encoding_type = ENC_UNKNOWN;
298 void procmime_scan_content_type(MimeInfo *mimeinfo, const gchar *content_type)
300 gchar *delim, *p, *cnttype;
303 if (conv_get_current_charset() == C_EUC_JP &&
304 strchr(content_type, '\033')) {
306 len = strlen(content_type) * 2 + 1;
307 Xalloca(buf, len, return);
308 conv_jistoeuc(buf, len, content_type);
310 Xstrdup_a(buf, content_type, return);
312 g_free(mimeinfo->content_type);
313 g_free(mimeinfo->charset);
314 g_free(mimeinfo->name);
315 mimeinfo->content_type = NULL;
316 mimeinfo->charset = NULL;
317 mimeinfo->name = NULL;
319 if ((delim = strchr(buf, ';'))) *delim = '\0';
320 mimeinfo->content_type = cnttype = g_strdup(g_strstrip(buf));
322 mimeinfo->mime_type = procmime_scan_mime_type(cnttype);
331 if ((delim = strchr(p, ';'))) *delim = '\0';
333 if (!(eq = strchr(p, '='))) break;
342 extract_quote(value, '"');
344 eliminate_parenthesis(value, '(', ')');
349 if (!strcasecmp(attr, "charset"))
350 mimeinfo->charset = g_strdup(value);
351 else if (!strcasecmp(attr, "name")) {
355 len = strlen(value) + 1;
356 Xalloca(tmp, len, return);
357 conv_unmime_header(tmp, len, value, NULL);
358 mimeinfo->name = g_strdup(tmp);
359 } else if (!strcasecmp(attr, "boundary"))
360 mimeinfo->boundary = g_strdup(value);
367 if (mimeinfo->mime_type == MIME_MULTIPART && !mimeinfo->boundary)
368 mimeinfo->mime_type = MIME_TEXT;
371 void procmime_scan_content_disposition(MimeInfo *mimeinfo,
372 const gchar *content_disposition)
374 gchar *delim, *p, *dispos;
377 if (conv_get_current_charset() == C_EUC_JP &&
378 strchr(content_disposition, '\033')) {
380 len = strlen(content_disposition) * 2 + 1;
381 Xalloca(buf, len, return);
382 conv_jistoeuc(buf, len, content_disposition);
384 Xstrdup_a(buf, content_disposition, return);
386 if ((delim = strchr(buf, ';'))) *delim = '\0';
387 mimeinfo->content_disposition = dispos = g_strdup(g_strstrip(buf));
396 if ((delim = strchr(p, ';'))) *delim = '\0';
398 if (!(eq = strchr(p, '='))) break;
407 extract_quote(value, '"');
409 eliminate_parenthesis(value, '(', ')');
414 if (!strcasecmp(attr, "filename")) {
418 len = strlen(value) + 1;
419 Xalloca(tmp, len, return);
420 conv_unmime_header(tmp, len, value, NULL);
421 g_free(mimeinfo->filename);
422 mimeinfo->filename = g_strdup(tmp);
434 H_CONTENT_TRANSFER_ENCODING = 0,
436 H_CONTENT_DISPOSITION = 2
439 MimeInfo *procmime_scan_mime_header(FILE *fp)
441 static HeaderEntry hentry[] = {{"Content-Transfer-Encoding:",
443 {"Content-Type:", NULL, TRUE},
444 {"Content-Disposition:",
446 {NULL, NULL, FALSE}};
452 g_return_val_if_fail(fp != NULL, NULL);
454 mimeinfo = procmime_mimeinfo_new();
455 mimeinfo->mime_type = MIME_TEXT;
456 mimeinfo->encoding_type = ENC_7BIT;
457 mimeinfo->fpos = ftell(fp);
459 while ((hnum = procheader_get_one_field(buf, sizeof(buf), fp, hentry))
463 if (H_CONTENT_TRANSFER_ENCODING == hnum) {
464 procmime_scan_encoding
465 (mimeinfo, buf + strlen(hp->name));
466 } else if (H_CONTENT_TYPE == hnum) {
467 procmime_scan_content_type
468 (mimeinfo, buf + strlen(hp->name));
469 } else if (H_CONTENT_DISPOSITION == hnum) {
470 procmime_scan_content_disposition
471 (mimeinfo, buf + strlen(hp->name));
475 if (mimeinfo->mime_type == MIME_APPLICATION_OCTET_STREAM &&
478 type = procmime_get_mime_type(mimeinfo->name);
480 mimeinfo->mime_type = procmime_scan_mime_type(type);
483 if (!mimeinfo->content_type)
484 mimeinfo->content_type = g_strdup("text/plain");
489 FILE *procmime_decode_content(FILE *outfp, FILE *infp, MimeInfo *mimeinfo)
492 gchar *boundary = NULL;
493 gint boundary_len = 0;
494 gboolean tmp_file = FALSE;
496 g_return_val_if_fail(infp != NULL, NULL);
497 g_return_val_if_fail(mimeinfo != NULL, NULL);
500 outfp = my_tmpfile();
508 if (mimeinfo->parent && mimeinfo->parent->boundary) {
509 boundary = mimeinfo->parent->boundary;
510 boundary_len = strlen(boundary);
513 if (mimeinfo->encoding_type == ENC_QUOTED_PRINTABLE) {
514 gboolean softline = FALSE;
516 while (fgets(buf, sizeof(buf), infp) != NULL &&
518 !IS_BOUNDARY(buf, boundary, boundary_len))) {
521 softline = DoOneQPLine(&p, FALSE, softline);
522 fwrite(buf, p - (guchar *)buf, 1, outfp);
524 } else if (mimeinfo->encoding_type == ENC_BASE64) {
525 gchar outbuf[BUFFSIZE];
527 Base64Decoder *decoder;
529 decoder = base64_decoder_new();
530 while (fgets(buf, sizeof(buf), infp) != NULL &&
532 !IS_BOUNDARY(buf, boundary, boundary_len))) {
533 len = base64_decoder_decode(decoder, buf, outbuf);
535 g_warning("Bad BASE64 content\n");
538 fwrite(outbuf, sizeof(gchar), len, outfp);
540 base64_decoder_free(decoder);
541 } else if (mimeinfo->encoding_type == ENC_X_UUENCODE) {
542 gchar outbuf[BUFFSIZE];
544 gboolean flag = FALSE;
546 while (fgets(buf, sizeof(buf), infp) != NULL &&
548 !IS_BOUNDARY(buf, boundary, boundary_len))) {
549 if(!flag && strncmp(buf,"begin ", 6)) continue;
552 len = fromuutobits(outbuf, buf);
555 g_warning("Bad UUENCODE content(%d)\n", len);
558 fwrite(outbuf, sizeof(gchar), len, outfp);
563 while (fgets(buf, sizeof(buf), infp) != NULL &&
565 !IS_BOUNDARY(buf, boundary, boundary_len))) {
570 if (tmp_file) rewind(outfp);
574 gint procmime_get_part(const gchar *outfile, const gchar *infile,
580 g_return_val_if_fail(outfile != NULL, -1);
581 g_return_val_if_fail(infile != NULL, -1);
582 g_return_val_if_fail(mimeinfo != NULL, -1);
584 if ((infp = fopen(infile, "r")) == NULL) {
585 FILE_OP_ERROR(infile, "fopen");
588 if (fseek(infp, mimeinfo->fpos, SEEK_SET) < 0) {
589 FILE_OP_ERROR(infile, "fseek");
593 if ((outfp = fopen(outfile, "w")) == NULL) {
594 FILE_OP_ERROR(outfile, "fopen");
599 while (fgets(buf, sizeof(buf), infp) != NULL)
600 if (buf[0] == '\r' || buf[0] == '\n') break;
602 procmime_decode_content(outfp, infp, mimeinfo);
605 if (fclose(outfp) == EOF) {
606 FILE_OP_ERROR(outfile, "fclose");
614 FILE *procmime_get_text_part(MsgInfo *msginfo)
616 FILE *infp, *tmpfp, *outfp;
617 MimeInfo *mimeinfo, *partinfo = NULL;
619 gboolean conv_fail = FALSE;
622 g_return_val_if_fail(msginfo != NULL, NULL);
624 mimeinfo = procmime_scan_message(msginfo);
625 if (!mimeinfo) return NULL;
627 if ((infp = procmsg_open_message(msginfo)) == NULL) {
628 procmime_mimeinfo_free_all(mimeinfo);
632 if (mimeinfo->mime_type == MIME_MULTIPART) {
633 partinfo = mimeinfo->children;
634 if (partinfo && partinfo->mime_type == MIME_TEXT) {
635 if (fseek(infp, partinfo->fpos, SEEK_SET) < 0) {
641 } else if (mimeinfo->mime_type == MIME_TEXT) {
643 if (fseek(infp, partinfo->fpos, SEEK_SET) < 0) {
650 procmime_mimeinfo_free_all(mimeinfo);
654 while (fgets(buf, sizeof(buf), infp) != NULL)
655 if (buf[0] == '\r' || buf[0] == '\n') break;
657 tmpfp = procmime_decode_content(NULL, infp, partinfo);
659 procmime_mimeinfo_free_all(mimeinfo);
663 if ((outfp = my_tmpfile()) == NULL) {
666 procmime_mimeinfo_free_all(mimeinfo);
670 src_codeset = prefs_common.force_charset
671 ? prefs_common.force_charset : partinfo->charset;
673 while (fgets(buf, sizeof(buf), tmpfp) != NULL) {
676 str = conv_codeset_strdup(buf, src_codeset, NULL);
685 if (conv_fail) g_warning(_("Code conversion failed.\n"));
688 procmime_mimeinfo_free_all(mimeinfo);
694 gchar *procmime_get_tmp_file_name(MimeInfo *mimeinfo)
696 static guint32 id = 0;
701 g_return_val_if_fail(mimeinfo != NULL, NULL);
703 g_snprintf(f_prefix, sizeof(f_prefix), "%08x.", id++);
705 if (MIME_TEXT_HTML == mimeinfo->mime_type)
706 base = "mimetmp.html";
708 base = mimeinfo->filename ? mimeinfo->filename
709 : mimeinfo->name ? mimeinfo->name : "mimetmp";
710 base = g_basename(base);
711 if (*base == '\0') base = "mimetmp";
714 filename = g_strconcat(get_mime_tmp_dir(), G_DIR_SEPARATOR_S,
715 f_prefix, base, NULL);
720 ContentType procmime_scan_mime_type(const gchar *mime_type)
724 if (!strncasecmp(mime_type, "text/html", 9))
725 type = MIME_TEXT_HTML;
726 else if (!strncasecmp(mime_type, "text/", 5))
728 else if (!strncasecmp(mime_type, "message/rfc822", 14))
729 type = MIME_MESSAGE_RFC822;
730 else if (!strncasecmp(mime_type, "message/", 8))
732 else if (!strncasecmp(mime_type, "application/octet-stream", 24))
733 type = MIME_APPLICATION_OCTET_STREAM;
734 else if (!strncasecmp(mime_type, "application/", 12))
735 type = MIME_APPLICATION;
736 else if (!strncasecmp(mime_type, "multipart/", 10))
737 type = MIME_MULTIPART;
738 else if (!strncasecmp(mime_type, "image/", 6))
740 else if (!strncasecmp(mime_type, "audio/", 6))
742 else if (!strcasecmp(mime_type, "text"))
750 static GList *mime_type_list = NULL;
752 gchar *procmime_get_mime_type(const gchar *filename)
754 static GHashTable *mime_type_table = NULL;
756 const gchar *ext, *p;
758 if (!mime_type_table) {
759 mime_type_table = procmime_get_mime_type_table();
760 if (!mime_type_table) return NULL;
763 filename = g_basename(filename);
764 p = strrchr(filename, '.');
770 mime_type = g_hash_table_lookup(mime_type_table, ext);
774 str = g_strconcat(mime_type->type, "/", mime_type->sub_type,
782 static guint procmime_str_hash(gconstpointer gptr)
784 guint hash_result = 0;
787 for (str = gptr; str && *str; str++) {
788 if (isupper(*str)) hash_result += (*str + ' ');
789 else hash_result += *str;
795 static gint procmime_str_equal(gconstpointer gptr1, gconstpointer gptr2)
797 const char *str1 = gptr1;
798 const char *str2 = gptr2;
800 return !strcasecmp(str1, str2);
803 static GHashTable *procmime_get_mime_type_table(void)
805 GHashTable *table = NULL;
810 if (!mime_type_list) {
811 mime_type_list = procmime_get_mime_type_list();
812 if (!mime_type_list) return NULL;
815 table = g_hash_table_new(procmime_str_hash, procmime_str_equal);
817 for (cur = mime_type_list; cur != NULL; cur = cur->next) {
820 mime_type = (MimeType *)cur->data;
822 if (!mime_type->extension) continue;
824 exts = g_strsplit(mime_type->extension, " ", 16);
825 for (i = 0; exts[i] != NULL; i++)
826 g_hash_table_insert(table, g_strdup(exts[i]),
834 GList *procmime_get_mime_type_list(void)
843 return mime_type_list;
845 if ((fp = fopen(SYSCONFDIR "/mime.types", "r")) == NULL) {
846 FILE_OP_ERROR(SYSCONFDIR "/mime.types", "fopen");
850 while (fgets(buf, sizeof(buf), fp) != NULL) {
851 p = strchr(buf, '#');
856 while (*p && !isspace(*p)) p++;
861 delim = strchr(buf, '/');
862 if (delim == NULL) continue;
865 mime_type = g_new(MimeType, 1);
866 mime_type->type = g_strdup(buf);
867 mime_type->sub_type = g_strdup(delim + 1);
869 while (*p && isspace(*p)) p++;
871 mime_type->extension = g_strdup(p);
873 mime_type->extension = NULL;
875 list = g_list_append(list, mime_type);
881 g_warning("Can't read mime.types\n");
886 EncodingType procmime_get_encoding_for_charset(const gchar *charset)
890 else if (!strncasecmp(charset, "ISO-2022-", 9) ||
891 !strcasecmp(charset, "US-ASCII"))
895 /* return ENC_BASE64; */
896 /* return ENC_QUOTED_PRINTABLE; */
899 const gchar *procmime_get_encoding_str(EncodingType encoding)
901 static const gchar *encoding_str[] = {
902 "7bit", "8bit", "quoted-printable", "base64", "x-uuencode",
906 if (encoding >= ENC_7BIT && encoding <= ENC_UNKNOWN)
907 return encoding_str[encoding];