2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999,2000 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 Xalloca(buf, strlen(encoding) + 1, return);
279 strcpy(buf, encoding);
281 g_free(mimeinfo->encoding);
283 mimeinfo->encoding = g_strdup(g_strstrip(buf));
284 if (!strcasecmp(buf, "7bit"))
285 mimeinfo->encoding_type = ENC_7BIT;
286 else if (!strcasecmp(buf, "8bit"))
287 mimeinfo->encoding_type = ENC_8BIT;
288 else if (!strcasecmp(buf, "quoted-printable"))
289 mimeinfo->encoding_type = ENC_QUOTED_PRINTABLE;
290 else if (!strcasecmp(buf, "base64"))
291 mimeinfo->encoding_type = ENC_BASE64;
292 else if (!strcasecmp(buf, "x-uuencode"))
293 mimeinfo->encoding_type = ENC_X_UUENCODE;
295 mimeinfo->encoding_type = ENC_UNKNOWN;
299 void procmime_scan_content_type(MimeInfo *mimeinfo, const gchar *content_type)
301 gchar *delim, *p, *cnttype;
304 Xalloca(buf, strlen(content_type) + 1, return);
305 strcpy(buf, content_type);
307 g_free(mimeinfo->content_type);
308 g_free(mimeinfo->charset);
309 g_free(mimeinfo->name);
310 mimeinfo->content_type = NULL;
311 mimeinfo->charset = NULL;
312 mimeinfo->name = NULL;
314 if ((delim = strchr(buf, ';'))) *delim = '\0';
315 mimeinfo->content_type = cnttype = g_strdup(g_strstrip(buf));
317 if (!strncasecmp(cnttype, "text/html", 9))
318 mimeinfo->mime_type = MIME_TEXT_HTML;
319 else if (!strncasecmp(cnttype, "text/", 5))
320 mimeinfo->mime_type = MIME_TEXT;
321 else if (!strncasecmp(cnttype, "message/rfc822", 14))
322 mimeinfo->mime_type = MIME_MESSAGE_RFC822;
323 else if (!strncasecmp(cnttype, "message/", 8))
324 mimeinfo->mime_type = MIME_TEXT;
325 else if (!strncasecmp(cnttype, "application/octet-stream", 24))
326 mimeinfo->mime_type = MIME_APPLICATION_OCTET_STREAM;
327 else if (!strncasecmp(cnttype, "application/", 12))
328 mimeinfo->mime_type = MIME_APPLICATION;
329 else if (!strncasecmp(cnttype, "multipart/", 10))
330 mimeinfo->mime_type = MIME_MULTIPART;
331 else if (!strncasecmp(cnttype, "image/", 6))
332 mimeinfo->mime_type = MIME_IMAGE;
333 else if (!strncasecmp(cnttype, "audio/", 6))
334 mimeinfo->mime_type = MIME_AUDIO;
335 else if (!strcasecmp(cnttype, "text"))
336 mimeinfo->mime_type = MIME_TEXT;
338 mimeinfo->mime_type = MIME_UNKNOWN;
347 if ((delim = strchr(p, ';'))) *delim = '\0';
349 if (!(eq = strchr(p, '='))) break;
358 extract_quote(value, '"');
360 eliminate_parenthesis(value, '(', ')');
365 if (!strcasecmp(attr, "charset"))
366 mimeinfo->charset = g_strdup(value);
367 else if (!strcasecmp(attr, "name")) {
371 len = strlen(value) + 1;
372 Xalloca(tmp, len, return);
373 conv_unmime_header(tmp, len, value, NULL);
374 mimeinfo->name = g_strdup(tmp);
375 } else if (!strcasecmp(attr, "boundary"))
376 mimeinfo->boundary = g_strdup(value);
383 if (mimeinfo->mime_type == MIME_MULTIPART && !mimeinfo->boundary)
384 mimeinfo->mime_type = MIME_TEXT;
387 void procmime_scan_content_disposition(MimeInfo *mimeinfo,
388 const gchar *content_disposition)
390 gchar *delim, *p, *dispos;
393 Xalloca(buf, strlen(content_disposition) + 1, return);
394 strcpy(buf, content_disposition);
396 if ((delim = strchr(buf, ';'))) *delim = '\0';
397 mimeinfo->content_disposition = dispos = g_strdup(g_strstrip(buf));
406 if ((delim = strchr(p, ';'))) *delim = '\0';
408 if (!(eq = strchr(p, '='))) break;
417 extract_quote(value, '"');
419 eliminate_parenthesis(value, '(', ')');
424 if (!strcasecmp(attr, "filename")) {
428 len = strlen(value) + 1;
429 Xalloca(tmp, len, return);
430 conv_unmime_header(tmp, len, value, NULL);
431 g_free(mimeinfo->filename);
432 mimeinfo->filename = g_strdup(tmp);
444 H_CONTENT_TRANSFER_ENCODING = 0,
446 H_CONTENT_DISPOSITION = 2
449 MimeInfo *procmime_scan_mime_header(FILE *fp)
451 static HeaderEntry hentry[] = {{"Content-Transfer-Encoding:",
453 {"Content-Type:", NULL, TRUE},
454 {"Content-Disposition:",
456 {NULL, NULL, FALSE}};
462 g_return_val_if_fail(fp != NULL, NULL);
464 mimeinfo = procmime_mimeinfo_new();
465 mimeinfo->mime_type = MIME_TEXT;
466 mimeinfo->encoding_type = ENC_7BIT;
467 mimeinfo->fpos = ftell(fp);
469 while ((hnum = procheader_get_one_field(buf, sizeof(buf), fp, hentry))
473 if (H_CONTENT_TRANSFER_ENCODING == hnum) {
474 procmime_scan_encoding
475 (mimeinfo, buf + strlen(hp->name));
476 } else if (H_CONTENT_TYPE == hnum) {
477 procmime_scan_content_type
478 (mimeinfo, buf + strlen(hp->name));
479 } else if (H_CONTENT_DISPOSITION == hnum) {
480 procmime_scan_content_disposition
481 (mimeinfo, buf + strlen(hp->name));
485 if (!mimeinfo->content_type)
486 mimeinfo->content_type = g_strdup("text/plain");
491 FILE *procmime_decode_content(FILE *outfp, FILE *infp, MimeInfo *mimeinfo)
494 gchar *boundary = NULL;
495 gint boundary_len = 0;
496 gboolean tmp_file = FALSE;
498 g_return_val_if_fail(infp != NULL, NULL);
499 g_return_val_if_fail(mimeinfo != NULL, NULL);
502 outfp = my_tmpfile();
510 if (mimeinfo->parent && mimeinfo->parent->boundary) {
511 boundary = mimeinfo->parent->boundary;
512 boundary_len = strlen(boundary);
515 if (mimeinfo->encoding_type == ENC_QUOTED_PRINTABLE) {
516 gboolean softline = FALSE;
518 while (fgets(buf, sizeof(buf), infp) != NULL &&
520 !IS_BOUNDARY(buf, boundary, boundary_len))) {
523 softline = DoOneQPLine(&p, FALSE, softline);
524 fwrite(buf, p - (guchar *)buf, 1, outfp);
526 } else if (mimeinfo->encoding_type == ENC_BASE64) {
527 gchar outbuf[BUFFSIZE];
529 Base64Decoder *decoder;
531 decoder = base64_decoder_new();
532 while (fgets(buf, sizeof(buf), infp) != NULL &&
534 !IS_BOUNDARY(buf, boundary, boundary_len))) {
535 len = base64_decoder_decode(decoder, buf, outbuf);
537 g_warning("Bad BASE64 content\n");
540 fwrite(outbuf, sizeof(gchar), len, outfp);
542 base64_decoder_free(decoder);
543 } else if (mimeinfo->encoding_type == ENC_X_UUENCODE) {
544 gchar outbuf[BUFFSIZE];
546 gboolean flag = FALSE;
548 while (fgets(buf, sizeof(buf), infp) != NULL &&
550 !IS_BOUNDARY(buf, boundary, boundary_len))) {
551 if(!flag && strncmp(buf,"begin ", 6)) continue;
554 len = fromuutobits(outbuf, buf);
557 g_warning("Bad UUENCODE content(%d)\n", len);
560 fwrite(outbuf, sizeof(gchar), len, outfp);
565 while (fgets(buf, sizeof(buf), infp) != NULL &&
567 !IS_BOUNDARY(buf, boundary, boundary_len))) {
572 if (tmp_file) rewind(outfp);
576 gint procmime_get_part(const gchar *outfile, const gchar *infile,
582 g_return_val_if_fail(outfile != NULL, -1);
583 g_return_val_if_fail(infile != NULL, -1);
584 g_return_val_if_fail(mimeinfo != NULL, -1);
586 if ((infp = fopen(infile, "r")) == NULL) {
587 FILE_OP_ERROR(infile, "fopen");
590 if (fseek(infp, mimeinfo->fpos, SEEK_SET) < 0) {
591 FILE_OP_ERROR(infile, "fseek");
595 if ((outfp = fopen(outfile, "w")) == NULL) {
596 FILE_OP_ERROR(outfile, "fopen");
601 while (fgets(buf, sizeof(buf), infp) != NULL)
602 if (buf[0] == '\r' || buf[0] == '\n') break;
604 procmime_decode_content(outfp, infp, mimeinfo);
607 if (fclose(outfp) == EOF) {
608 FILE_OP_ERROR(outfile, "fclose");
616 FILE *procmime_get_text_part(MsgInfo *msginfo)
618 FILE *infp, *tmpfp, *outfp;
619 MimeInfo *mimeinfo, *partinfo = NULL;
621 gboolean conv_fail = FALSE;
624 g_return_val_if_fail(msginfo != NULL, NULL);
626 mimeinfo = procmime_scan_message(msginfo);
627 if (!mimeinfo) return NULL;
629 if ((infp = procmsg_open_message(msginfo)) == NULL) {
630 procmime_mimeinfo_free_all(mimeinfo);
634 if (mimeinfo->mime_type == MIME_MULTIPART) {
635 partinfo = mimeinfo->children;
636 if (partinfo && partinfo->mime_type == MIME_TEXT) {
637 if (fseek(infp, partinfo->fpos, SEEK_SET) < 0) {
643 } else if (mimeinfo->mime_type == MIME_TEXT) {
645 if (fseek(infp, partinfo->fpos, SEEK_SET) < 0) {
652 procmime_mimeinfo_free_all(mimeinfo);
656 while (fgets(buf, sizeof(buf), infp) != NULL)
657 if (buf[0] == '\r' || buf[0] == '\n') break;
659 tmpfp = procmime_decode_content(NULL, infp, partinfo);
661 procmime_mimeinfo_free_all(mimeinfo);
665 if ((outfp = my_tmpfile()) == NULL) {
668 procmime_mimeinfo_free_all(mimeinfo);
672 src_codeset = prefs_common.force_charset
673 ? prefs_common.force_charset : partinfo->charset;
675 while (fgets(buf, sizeof(buf), tmpfp) != NULL) {
678 str = conv_codeset_strdup(buf, src_codeset, NULL);
687 if (conv_fail) g_warning(_("Code conversion failed.\n"));
690 procmime_mimeinfo_free_all(mimeinfo);
696 gchar *procmime_get_tmp_file_name(MimeInfo *mimeinfo)
698 static guint32 id = 0;
703 g_return_val_if_fail(mimeinfo != NULL, NULL);
705 g_snprintf(f_prefix, sizeof(f_prefix), "%08x.", id++);
707 if (MIME_TEXT_HTML == mimeinfo->mime_type)
708 base = "mimetmp.html";
710 base = mimeinfo->filename ? mimeinfo->filename
711 : mimeinfo->name ? mimeinfo->name : "mimetmp";
712 base = g_basename(base);
713 if (*base == '\0') base = "mimetmp";
716 filename = g_strconcat(get_mime_tmp_dir(), G_DIR_SEPARATOR_S,
717 f_prefix, base, NULL);
722 static GList *mime_type_list = NULL;
724 gchar *procmime_get_mime_type(const gchar *filename)
726 static GHashTable *mime_type_table = NULL;
728 const gchar *ext, *p;
730 if (!mime_type_table) {
731 mime_type_table = procmime_get_mime_type_table();
732 if (!mime_type_table) return NULL;
735 filename = g_basename(filename);
736 p = strrchr(filename, '.');
742 mime_type = g_hash_table_lookup(mime_type_table, ext);
746 str = g_strconcat(mime_type->type, "/", mime_type->sub_type,
754 static guint procmime_str_hash(gconstpointer gptr)
756 guint hash_result = 0;
759 for (str = gptr; str && *str; str++) {
760 if (isupper(*str)) hash_result += (*str + ' ');
761 else hash_result += *str;
767 static gint procmime_str_equal(gconstpointer gptr1, gconstpointer gptr2)
769 const char *str1 = gptr1;
770 const char *str2 = gptr2;
772 return !strcasecmp(str1, str2);
775 static GHashTable *procmime_get_mime_type_table(void)
777 GHashTable *table = NULL;
782 if (!mime_type_list) {
783 mime_type_list = procmime_get_mime_type_list();
784 if (!mime_type_list) return NULL;
787 table = g_hash_table_new(procmime_str_hash, procmime_str_equal);
789 for (cur = mime_type_list; cur != NULL; cur = cur->next) {
792 mime_type = (MimeType *)cur->data;
794 if (!mime_type->extension) continue;
796 exts = g_strsplit(mime_type->extension, " ", 16);
797 for (i = 0; exts[i] != NULL; i++)
798 g_hash_table_insert(table, g_strdup(exts[i]),
806 GList *procmime_get_mime_type_list(void)
815 return mime_type_list;
817 if ((fp = fopen(SYSCONFDIR "/mime.types", "r")) == NULL) {
818 FILE_OP_ERROR(SYSCONFDIR "/mime.types", "fopen");
822 while (fgets(buf, sizeof(buf), fp) != NULL) {
823 p = strchr(buf, '#');
828 while (*p && !isspace(*p)) p++;
833 delim = strchr(buf, '/');
834 if (delim == NULL) continue;
837 mime_type = g_new(MimeType, 1);
838 mime_type->type = g_strdup(buf);
839 mime_type->sub_type = g_strdup(delim + 1);
841 while (*p && isspace(*p)) p++;
843 mime_type->extension = g_strdup(p);
845 mime_type->extension = NULL;
847 list = g_list_append(list, mime_type);
853 g_warning("Can't read mime.types\n");
858 EncodingType procmime_get_encoding_for_charset(const gchar *charset)
862 else if (!strncasecmp(charset, "ISO-2022-", 9) ||
863 !strcasecmp(charset, "US-ASCII"))
867 /* return ENC_BASE64; */
868 /* return ENC_QUOTED_PRINTABLE; */
871 const gchar *procmime_get_encoding_str(EncodingType encoding)
873 static const gchar *encoding_str[] = {
874 "7bit", "8bit", "quoted-printable", "base64", "x-uuencode",
878 if (encoding >= ENC_7BIT && encoding <= ENC_UNKNOWN)
879 return encoding_str[encoding];