2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2003 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"
36 #include "quoted-printable.h"
43 #include "prefs_common.h"
49 #include "prefs_gtk.h"
51 static GHashTable *procmime_get_mime_type_table (void);
53 MimeInfo *procmime_mimeinfo_new(void)
57 mimeinfo = g_new0(MimeInfo, 1);
58 mimeinfo->mime_type = MIME_UNKNOWN;
59 mimeinfo->encoding_type = ENC_UNKNOWN;
64 void procmime_mimeinfo_free_all(MimeInfo *mimeinfo)
66 while (mimeinfo != NULL) {
69 g_free(mimeinfo->encoding);
70 g_free(mimeinfo->content_type);
71 g_free(mimeinfo->charset);
72 g_free(mimeinfo->name);
73 g_free(mimeinfo->boundary);
74 g_free(mimeinfo->content_disposition);
75 g_free(mimeinfo->filename);
76 g_free(mimeinfo->description);
78 g_free(mimeinfo->plaintextfile);
79 g_free(mimeinfo->sigstatus);
80 g_free(mimeinfo->sigstatus_full);
83 procmime_mimeinfo_free_all(mimeinfo->sub);
84 procmime_mimeinfo_free_all(mimeinfo->children);
86 procmime_mimeinfo_free_all(mimeinfo->plaintext);
89 next = mimeinfo->next;
95 MimeInfo *procmime_mimeinfo_insert(MimeInfo *parent, MimeInfo *mimeinfo)
97 MimeInfo *child = parent->children;
100 parent->children = mimeinfo;
102 while (child->next != NULL)
105 child->next = mimeinfo;
108 mimeinfo->parent = parent;
109 mimeinfo->level = parent->level + 1;
114 void procmime_mimeinfo_replace(MimeInfo *old, MimeInfo *new)
116 MimeInfo *parent = old->parent;
119 g_return_if_fail(parent != NULL);
120 g_return_if_fail(new->next == NULL);
122 for (child = parent->children; child && child != old;
126 g_warning("oops: parent can't find it's own child");
129 procmime_mimeinfo_free_all(old);
131 if (child == parent->children) {
132 new->next = parent->children->next;
133 parent->children = new;
135 new->next = child->next;
140 MimeInfo *procmime_mimeinfo_next(MimeInfo *mimeinfo)
142 if (!mimeinfo) return NULL;
144 if (mimeinfo->children)
145 return mimeinfo->children;
147 return mimeinfo->sub;
149 return mimeinfo->next;
151 if (mimeinfo->main) {
152 mimeinfo = mimeinfo->main;
154 return mimeinfo->next;
157 for (mimeinfo = mimeinfo->parent; mimeinfo != NULL;
158 mimeinfo = mimeinfo->parent) {
160 return mimeinfo->next;
161 if (mimeinfo->main) {
162 mimeinfo = mimeinfo->main;
164 return mimeinfo->next;
172 void procmime_dump_mimeinfo(MimeInfo *mimeinfo)
178 for (; mimeinfo != NULL; mimeinfo = procmime_mimeinfo_next(mimeinfo)) {
179 for (i = 0; i < mimeinfo->level; i++)
181 g_print("%s%s\n", mimeinfo->main ? "sub: " : "",
182 mimeinfo->content_type);
187 MimeInfo *procmime_scan_message(MsgInfo *msginfo)
192 g_return_val_if_fail(msginfo != NULL, NULL);
195 if ((fp = procmsg_open_message_decrypted(msginfo, &mimeinfo)) == NULL)
198 if ((fp = procmsg_open_message(msginfo)) == NULL) return NULL;
199 mimeinfo = procmime_scan_mime_header(fp);
203 mimeinfo->size = get_left_file_size(fp);
204 if (mimeinfo->mime_type == MIME_MULTIPART ||
205 mimeinfo->mime_type == MIME_MESSAGE_RFC822)
206 procmime_scan_multipart_message(mimeinfo, fp);
214 void procmime_scan_multipart_message(MimeInfo *mimeinfo, FILE *fp)
218 gint boundary_len = 0;
220 glong fpos, prev_fpos;
222 g_return_if_fail(mimeinfo != NULL);
223 g_return_if_fail(mimeinfo->mime_type == MIME_MULTIPART ||
224 mimeinfo->mime_type == MIME_MESSAGE_RFC822);
226 if (mimeinfo->mime_type == MIME_MULTIPART) {
227 g_return_if_fail(mimeinfo->boundary != NULL);
228 g_return_if_fail(mimeinfo->sub == NULL);
230 g_return_if_fail(fp != NULL);
232 boundary = mimeinfo->boundary;
235 boundary_len = strlen(boundary);
237 /* look for first boundary */
238 while ((p = fgets(buf, sizeof(buf), fp)) != NULL)
239 if (IS_BOUNDARY(buf, boundary, boundary_len)) break;
241 } else if (mimeinfo->parent && mimeinfo->parent->boundary) {
242 boundary = mimeinfo->parent->boundary;
243 boundary_len = strlen(boundary);
246 if ((fpos = ftell(fp)) < 0) {
253 gboolean eom = FALSE;
257 debug_print("prev_fpos: %ld\n", fpos);
259 if (mimeinfo->mime_type == MIME_MESSAGE_RFC822) {
262 mimeinfo->sub = sub = procmime_scan_mime_header(fp);
265 sub->level = mimeinfo->level + 1;
266 sub->parent = mimeinfo->parent;
267 sub->main = mimeinfo;
271 partinfo = procmime_scan_mime_header(fp);
272 if (!partinfo) break;
273 procmime_mimeinfo_insert(mimeinfo, partinfo);
274 debug_print("content-type: %s\n",
275 partinfo->content_type);
278 if (partinfo->mime_type == MIME_MULTIPART ||
279 partinfo->mime_type == MIME_MESSAGE_RFC822) {
280 if (partinfo->level < 8)
281 procmime_scan_multipart_message(partinfo, fp);
284 /* look for next boundary */
286 while ((p = fgets(buf, sizeof(buf), fp)) != NULL) {
287 if (IS_BOUNDARY(buf, boundary, boundary_len)) {
288 if (buf[2 + boundary_len] == '-' &&
289 buf[2 + boundary_len + 1] == '-')
295 /* broken MIME, or single part MIME message */
299 debug_print("boundary: %s\n", buf);
302 debug_print("fpos: %ld\n", fpos);
305 partinfo->size = fpos - prev_fpos - len;
306 debug_print("partinfo->size: %d\n", partinfo->size);
307 if (partinfo->sub && !partinfo->sub->sub &&
308 !partinfo->sub->children) {
309 partinfo->sub->size =
310 fpos - partinfo->sub->fpos - strlen(buf);
311 debug_print("partinfo->sub->size: %d\n",
312 partinfo->sub->size);
315 if (mimeinfo->mime_type == MIME_MESSAGE_RFC822) {
316 if (len > 0 && fseek(fp, fpos - len, SEEK_SET) < 0)
325 void procmime_scan_encoding(MimeInfo *mimeinfo, const gchar *encoding)
329 Xstrdup_a(buf, encoding, return);
331 g_free(mimeinfo->encoding);
333 mimeinfo->encoding = g_strdup(g_strstrip(buf));
334 if (!strcasecmp(buf, "7bit"))
335 mimeinfo->encoding_type = ENC_7BIT;
336 else if (!strcasecmp(buf, "8bit"))
337 mimeinfo->encoding_type = ENC_8BIT;
338 else if (!strcasecmp(buf, "quoted-printable"))
339 mimeinfo->encoding_type = ENC_QUOTED_PRINTABLE;
340 else if (!strcasecmp(buf, "base64"))
341 mimeinfo->encoding_type = ENC_BASE64;
342 else if (!strcasecmp(buf, "x-uuencode"))
343 mimeinfo->encoding_type = ENC_X_UUENCODE;
345 mimeinfo->encoding_type = ENC_UNKNOWN;
349 void procmime_scan_content_type(MimeInfo *mimeinfo, const gchar *content_type)
351 gchar *delim, *p, *cnttype;
354 if (conv_get_current_charset() == C_EUC_JP &&
355 strchr(content_type, '\033')) {
357 len = strlen(content_type) * 2 + 1;
358 Xalloca(buf, len, return);
359 conv_jistoeuc(buf, len, content_type);
361 Xstrdup_a(buf, content_type, return);
363 g_free(mimeinfo->content_type);
364 g_free(mimeinfo->charset);
365 /* g_free(mimeinfo->name); */
366 mimeinfo->content_type = NULL;
367 mimeinfo->charset = NULL;
368 /* mimeinfo->name = NULL; */
370 if ((delim = strchr(buf, ';'))) *delim = '\0';
371 mimeinfo->content_type = cnttype = g_strdup(g_strstrip(buf));
373 mimeinfo->mime_type = procmime_scan_mime_type(cnttype);
382 if ((delim = strchr(p, ';'))) *delim = '\0';
384 if (!(eq = strchr(p, '='))) break;
393 extract_quote(value, '"');
395 eliminate_parenthesis(value, '(', ')');
400 if (!strcasecmp(attr, "charset"))
401 mimeinfo->charset = g_strdup(value);
402 else if (!strcasecmp(attr, "name")) {
406 len = strlen(value) + 1;
407 Xalloca(tmp, len, return);
408 conv_unmime_header(tmp, len, value, NULL);
409 g_free(mimeinfo->name);
410 /*pgp signatures should NOT have a name */
411 if (mimeinfo->content_type
412 && strcasecmp(mimeinfo->content_type, "application/pgp-signature"))
413 mimeinfo->name = g_strdup(tmp);
414 } else if (!strcasecmp(attr, "boundary"))
415 mimeinfo->boundary = g_strdup(value);
422 if (mimeinfo->mime_type == MIME_MULTIPART && !mimeinfo->boundary)
423 mimeinfo->mime_type = MIME_TEXT;
426 void procmime_scan_content_disposition(MimeInfo *mimeinfo,
427 const gchar *content_disposition)
429 gchar *delim, *p, *dispos;
432 if (conv_get_current_charset() == C_EUC_JP &&
433 strchr(content_disposition, '\033')) {
435 len = strlen(content_disposition) * 2 + 1;
436 Xalloca(buf, len, return);
437 conv_jistoeuc(buf, len, content_disposition);
439 Xstrdup_a(buf, content_disposition, return);
441 if ((delim = strchr(buf, ';'))) *delim = '\0';
442 mimeinfo->content_disposition = dispos = g_strdup(g_strstrip(buf));
451 if ((delim = strchr(p, ';'))) *delim = '\0';
453 if (!(eq = strchr(p, '='))) break;
462 extract_quote(value, '"');
464 eliminate_parenthesis(value, '(', ')');
469 if (!strcasecmp(attr, "filename")) {
473 len = strlen(value) + 1;
474 Xalloca(tmp, len, return);
475 conv_unmime_header(tmp, len, value, NULL);
476 g_free(mimeinfo->filename);
477 /*pgp signatures should NOT have a name */
478 if (mimeinfo->content_type
479 && strcasecmp(mimeinfo->content_type, "application/pgp-signature"))
480 mimeinfo->filename = g_strdup(tmp);
490 void procmime_scan_content_description(MimeInfo *mimeinfo,
491 const gchar *content_description)
498 if (conv_get_current_charset() == C_EUC_JP &&
499 strchr(content_description, '\033')) {
501 len = strlen(content_description) * 2 + 1;
502 Xalloca(buf, len, return);
503 conv_jistoeuc(buf, len, content_description);
505 Xstrdup_a(buf, content_description, return);
507 blen = strlen(buf) + 1;
508 Xalloca(tmp, blen, return);
509 conv_unmime_header(tmp, blen, buf, NULL);
510 /*pgp signatures should NOT have a name */
511 if (mimeinfo->content_type
512 && strcasecmp(mimeinfo->content_type, "application/pgp-signature"))
513 mimeinfo->description = g_strdup(tmp);
516 void procmime_scan_subject(MimeInfo *mimeinfo,
517 const gchar *subject)
524 if (conv_get_current_charset() == C_EUC_JP &&
525 strchr(subject, '\033')) {
527 len = strlen(subject) * 2 + 1;
528 Xalloca(buf, len, return);
529 conv_jistoeuc(buf, len, subject);
531 Xstrdup_a(buf, subject, return);
533 blen = strlen(buf) + 1;
534 Xalloca(tmp, blen, return);
535 conv_unmime_header(tmp, blen, buf, NULL);
536 g_free(mimeinfo->name);
537 mimeinfo->name = g_strdup(tmp);
542 H_CONTENT_TRANSFER_ENCODING = 0,
544 H_CONTENT_DISPOSITION = 2,
545 H_CONTENT_DESCRIPTION = 3,
549 MimeInfo *procmime_scan_mime_header(FILE *fp)
551 static HeaderEntry hentry[] = {{"Content-Transfer-Encoding:",
553 {"Content-Type:", NULL, TRUE},
554 {"Content-Disposition:",
556 {"Content-description:",
560 {NULL, NULL, FALSE}};
566 g_return_val_if_fail(fp != NULL, NULL);
568 mimeinfo = procmime_mimeinfo_new();
569 mimeinfo->mime_type = MIME_TEXT;
570 mimeinfo->encoding_type = ENC_7BIT;
571 mimeinfo->fpos = ftell(fp);
573 while ((hnum = procheader_get_one_field(buf, sizeof(buf), fp, hentry))
577 if (H_CONTENT_TRANSFER_ENCODING == hnum) {
578 procmime_scan_encoding
579 (mimeinfo, buf + strlen(hp->name));
580 } else if (H_CONTENT_TYPE == hnum) {
581 procmime_scan_content_type
582 (mimeinfo, buf + strlen(hp->name));
583 } else if (H_CONTENT_DISPOSITION == hnum) {
584 procmime_scan_content_disposition
585 (mimeinfo, buf + strlen(hp->name));
586 } else if (H_CONTENT_DESCRIPTION == hnum) {
587 procmime_scan_content_description
588 (mimeinfo, buf + strlen(hp->name));
589 } else if (H_SUBJECT == hnum) {
590 procmime_scan_subject
591 (mimeinfo, buf + strlen(hp->name));
595 if (!mimeinfo->content_type)
596 mimeinfo->content_type = g_strdup("text/plain");
601 FILE *procmime_decode_content(FILE *outfp, FILE *infp, MimeInfo *mimeinfo)
604 gchar *boundary = NULL;
605 gint boundary_len = 0;
606 gboolean tmp_file = FALSE;
608 g_return_val_if_fail(infp != NULL, NULL);
609 g_return_val_if_fail(mimeinfo != NULL, NULL);
612 outfp = my_tmpfile();
620 if (mimeinfo->parent && mimeinfo->parent->boundary) {
621 boundary = mimeinfo->parent->boundary;
622 boundary_len = strlen(boundary);
625 if (mimeinfo->encoding_type == ENC_QUOTED_PRINTABLE) {
626 while (fgets(buf, sizeof(buf), infp) != NULL &&
628 !IS_BOUNDARY(buf, boundary, boundary_len))) {
630 len = qp_decode_line(buf);
631 fwrite(buf, len, 1, outfp);
633 } else if (mimeinfo->encoding_type == ENC_BASE64) {
634 gchar outbuf[BUFFSIZE];
636 Base64Decoder *decoder;
638 decoder = base64_decoder_new();
639 while (fgets(buf, sizeof(buf), infp) != NULL &&
641 !IS_BOUNDARY(buf, boundary, boundary_len))) {
642 len = base64_decoder_decode(decoder, buf, outbuf);
644 g_warning("Bad BASE64 content\n");
647 fwrite(outbuf, sizeof(gchar), len, outfp);
649 base64_decoder_free(decoder);
650 } else if (mimeinfo->encoding_type == ENC_X_UUENCODE) {
651 gchar outbuf[BUFFSIZE];
653 gboolean flag = FALSE;
655 while (fgets(buf, sizeof(buf), infp) != NULL &&
657 !IS_BOUNDARY(buf, boundary, boundary_len))) {
658 if(!flag && strncmp(buf,"begin ", 6)) continue;
661 len = fromuutobits(outbuf, buf);
664 g_warning("Bad UUENCODE content(%d)\n", len);
667 fwrite(outbuf, sizeof(gchar), len, outfp);
672 while (fgets(buf, sizeof(buf), infp) != NULL &&
674 !IS_BOUNDARY(buf, boundary, boundary_len))) {
679 if (tmp_file) rewind(outfp);
683 gint procmime_get_part(const gchar *outfile, const gchar *infile,
689 g_return_val_if_fail(outfile != NULL, -1);
690 g_return_val_if_fail(infile != NULL, -1);
691 g_return_val_if_fail(mimeinfo != NULL, -1);
693 if ((infp = fopen(infile, "rb")) == NULL) {
694 FILE_OP_ERROR(infile, "fopen");
697 if (fseek(infp, mimeinfo->fpos, SEEK_SET) < 0) {
698 FILE_OP_ERROR(infile, "fseek");
702 if ((outfp = fopen(outfile, "wb")) == NULL) {
703 FILE_OP_ERROR(outfile, "fopen");
708 while (fgets(buf, sizeof(buf), infp) != NULL)
709 if (buf[0] == '\r' || buf[0] == '\n') break;
711 procmime_decode_content(outfp, infp, mimeinfo);
714 if (fclose(outfp) == EOF) {
715 FILE_OP_ERROR(outfile, "fclose");
723 struct ContentRenderer {
728 static GList * renderer_list = NULL;
730 static struct ContentRenderer *
731 content_renderer_new(char * content_type, char * renderer)
733 struct ContentRenderer * cr;
735 cr = g_new(struct ContentRenderer, 1);
739 cr->content_type = g_strdup(content_type);
740 cr->renderer = g_strdup(renderer);
745 static void content_renderer_free(struct ContentRenderer * cr)
747 g_free(cr->content_type);
748 g_free(cr->renderer);
752 void renderer_read_config(void)
758 g_list_foreach(renderer_list, (GFunc) content_renderer_free, NULL);
759 renderer_list = NULL;
761 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, RENDERER_RC, NULL);
762 f = fopen(rcpath, "rb");
768 while (fgets(buf, BUFFSIZE, f)) {
770 struct ContentRenderer * cr;
773 p = strchr(buf, ' ');
778 cr = content_renderer_new(buf, p + 1);
782 renderer_list = g_list_append(renderer_list, cr);
788 void renderer_write_config(void)
794 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, RENDERER_RC, NULL);
796 if ((pfile = prefs_write_open(rcpath)) == NULL) {
797 g_warning("failed to write configuration to file\n");
804 for(cur = renderer_list ; cur != NULL ; cur = cur->next) {
805 struct ContentRenderer * renderer;
806 renderer = cur->data;
807 fprintf(pfile->fp, "%s %s\n", renderer->content_type,
811 if (prefs_file_close(pfile) < 0) {
812 g_warning("failed to write configuration to file\n");
817 FILE *procmime_get_text_content(MimeInfo *mimeinfo, FILE *infp)
821 gboolean conv_fail = FALSE;
824 struct ContentRenderer * renderer;
827 g_return_val_if_fail(mimeinfo != NULL, NULL);
828 g_return_val_if_fail(infp != NULL, NULL);
829 g_return_val_if_fail(mimeinfo->mime_type == MIME_TEXT ||
830 mimeinfo->mime_type == MIME_TEXT_HTML ||
831 mimeinfo->mime_type == MIME_TEXT_ENRICHED, NULL);
833 if (fseek(infp, mimeinfo->fpos, SEEK_SET) < 0) {
838 while (fgets(buf, sizeof(buf), infp) != NULL)
839 if (buf[0] == '\r' || buf[0] == '\n') break;
841 tmpfp = procmime_decode_content(NULL, infp, mimeinfo);
845 if ((outfp = my_tmpfile()) == NULL) {
851 src_codeset = prefs_common.force_charset
852 ? prefs_common.force_charset : mimeinfo->charset;
856 for(cur = renderer_list ; cur != NULL ; cur = cur->next) {
857 struct ContentRenderer * cr;
859 if (g_strcasecmp(cr->content_type,
860 mimeinfo->content_type) == 0) {
866 if (renderer != NULL) {
872 dup2(fileno(outfp), 1);
874 p = popen(renderer->renderer, "w");
879 fread(buf, sizeof(char), sizeof(buf),
881 fwrite(buf, sizeof(char), count, p);
886 } else if (mimeinfo->mime_type == MIME_TEXT) {
887 while (fgets(buf, sizeof(buf), tmpfp) != NULL) {
889 str = conv_codeset_strdup(buf, src_codeset, CS_UTF_8);
898 } else if (mimeinfo->mime_type == MIME_TEXT_HTML) {
902 conv = conv_code_converter_new(src_codeset);
903 parser = html_parser_new(tmpfp, conv);
904 while ((str = html_parse(parser)) != NULL) {
907 html_parser_destroy(parser);
908 conv_code_converter_destroy(conv);
909 } else if (mimeinfo->mime_type == MIME_TEXT_ENRICHED) {
913 conv = conv_code_converter_new(src_codeset);
914 parser = ertf_parser_new(tmpfp, conv);
915 while ((str = ertf_parse(parser)) != NULL) {
918 ertf_parser_destroy(parser);
919 conv_code_converter_destroy(conv);
923 g_warning("procmime_get_text_content(): Code conversion failed.\n");
931 /* search the first text part of (multipart) MIME message,
932 decode, convert it and output to outfp. */
933 FILE *procmime_get_first_text_content(MsgInfo *msginfo)
935 FILE *infp, *outfp = NULL;
936 MimeInfo *mimeinfo, *partinfo;
938 g_return_val_if_fail(msginfo != NULL, NULL);
940 mimeinfo = procmime_scan_message(msginfo);
941 if (!mimeinfo) return NULL;
943 if ((infp = procmsg_open_message(msginfo)) == NULL) {
944 procmime_mimeinfo_free_all(mimeinfo);
949 while (partinfo && partinfo->mime_type != MIME_TEXT)
950 partinfo = procmime_mimeinfo_next(partinfo);
953 while (partinfo && partinfo->mime_type != MIME_TEXT_HTML &&
954 partinfo->mime_type != MIME_TEXT_ENRICHED)
955 partinfo = procmime_mimeinfo_next(partinfo);
960 outfp = procmime_get_text_content(partinfo, infp);
963 procmime_mimeinfo_free_all(mimeinfo);
968 gboolean procmime_find_string_part(MimeInfo *mimeinfo, const gchar *filename,
969 const gchar *str, gboolean case_sens)
974 gchar *(* StrFindFunc) (const gchar *haystack, const gchar *needle);
976 g_return_val_if_fail(mimeinfo != NULL, FALSE);
977 g_return_val_if_fail(mimeinfo->mime_type == MIME_TEXT ||
978 mimeinfo->mime_type == MIME_TEXT_HTML ||
979 mimeinfo->mime_type == MIME_TEXT_ENRICHED, FALSE);
980 g_return_val_if_fail(str != NULL, FALSE);
982 if ((infp = fopen(filename, "rb")) == NULL) {
983 FILE_OP_ERROR(filename, "fopen");
987 outfp = procmime_get_text_content(mimeinfo, infp);
994 StrFindFunc = strstr;
996 StrFindFunc = strcasestr;
998 while (fgets(buf, sizeof(buf), outfp) != NULL) {
999 if (StrFindFunc(buf, str) != NULL) {
1010 gboolean procmime_find_string(MsgInfo *msginfo, const gchar *str,
1016 gboolean found = FALSE;
1018 g_return_val_if_fail(msginfo != NULL, FALSE);
1019 g_return_val_if_fail(str != NULL, FALSE);
1021 filename = procmsg_get_message_file(msginfo);
1022 if (!filename) return FALSE;
1023 mimeinfo = procmime_scan_message(msginfo);
1025 for (partinfo = mimeinfo; partinfo != NULL;
1026 partinfo = procmime_mimeinfo_next(partinfo)) {
1027 if (partinfo->mime_type == MIME_TEXT ||
1028 partinfo->mime_type == MIME_TEXT_HTML ||
1029 partinfo->mime_type == MIME_TEXT_ENRICHED) {
1030 if (procmime_find_string_part
1031 (partinfo, filename, str, case_sens) == TRUE) {
1038 procmime_mimeinfo_free_all(mimeinfo);
1044 gchar *procmime_get_tmp_file_name(MimeInfo *mimeinfo)
1046 static guint32 id = 0;
1051 g_return_val_if_fail(mimeinfo != NULL, NULL);
1053 g_snprintf(f_prefix, sizeof(f_prefix), "%08x.", id++);
1055 if (MIME_TEXT_HTML == mimeinfo->mime_type)
1056 base = "mimetmp.html";
1059 base = mimeinfo->filename ? mimeinfo->filename
1060 : mimeinfo->name ? mimeinfo->name : "mimetmp";
1061 base = g_basename(base);
1062 if (*base == '\0') base = "mimetmp";
1063 Xstrdup_a(tmp, base, return NULL);
1064 subst_for_filename(tmp);
1068 filename = g_strconcat(get_mime_tmp_dir(), G_DIR_SEPARATOR_S,
1069 f_prefix, base, NULL);
1074 ContentType procmime_scan_mime_type(const gchar *mime_type)
1078 if (!strncasecmp(mime_type, "text/html", 9))
1079 type = MIME_TEXT_HTML;
1080 else if (!strncasecmp(mime_type, "text/enriched", 13))
1081 type = MIME_TEXT_ENRICHED;
1082 else if (!strncasecmp(mime_type, "text/", 5))
1084 else if (!strncasecmp(mime_type, "message/rfc822", 14))
1085 type = MIME_MESSAGE_RFC822;
1086 else if (!strncasecmp(mime_type, "message/", 8))
1088 else if (!strncasecmp(mime_type, "application/octet-stream", 24))
1089 type = MIME_APPLICATION_OCTET_STREAM;
1090 else if (!strncasecmp(mime_type, "application/", 12))
1091 type = MIME_APPLICATION;
1092 else if (!strncasecmp(mime_type, "multipart/", 10))
1093 type = MIME_MULTIPART;
1094 else if (!strncasecmp(mime_type, "image/", 6))
1096 else if (!strncasecmp(mime_type, "audio/", 6))
1098 else if (!strcasecmp(mime_type, "text"))
1101 type = MIME_UNKNOWN;
1106 static GList *mime_type_list = NULL;
1108 gchar *procmime_get_mime_type(const gchar *filename)
1110 static GHashTable *mime_type_table = NULL;
1111 MimeType *mime_type;
1115 if (!mime_type_table) {
1116 mime_type_table = procmime_get_mime_type_table();
1117 if (!mime_type_table) return NULL;
1120 filename = g_basename(filename);
1121 p = strrchr(filename, '.');
1122 if (!p) return NULL;
1124 Xstrdup_a(ext, p + 1, return NULL);
1126 mime_type = g_hash_table_lookup(mime_type_table, ext);
1130 str = g_strconcat(mime_type->type, "/", mime_type->sub_type,
1138 static guint procmime_str_hash(gconstpointer gptr)
1140 guint hash_result = 0;
1143 for (str = gptr; str && *str; str++) {
1144 if (isupper(*str)) hash_result += (*str + ' ');
1145 else hash_result += *str;
1151 static gint procmime_str_equal(gconstpointer gptr1, gconstpointer gptr2)
1153 const char *str1 = gptr1;
1154 const char *str2 = gptr2;
1156 return !strcasecmp(str1, str2);
1159 static GHashTable *procmime_get_mime_type_table(void)
1161 GHashTable *table = NULL;
1163 MimeType *mime_type;
1166 if (!mime_type_list) {
1167 mime_type_list = procmime_get_mime_type_list();
1168 if (!mime_type_list) return NULL;
1171 table = g_hash_table_new(procmime_str_hash, procmime_str_equal);
1173 for (cur = mime_type_list; cur != NULL; cur = cur->next) {
1177 mime_type = (MimeType *)cur->data;
1179 if (!mime_type->extension) continue;
1181 exts = g_strsplit(mime_type->extension, " ", 16);
1182 for (i = 0; exts[i] != NULL; i++) {
1183 /* make the key case insensitive */
1185 /* use previously dup'd key on overwriting */
1186 if (g_hash_table_lookup(table, exts[i]))
1189 key = g_strdup(exts[i]);
1190 g_hash_table_insert(table, key, mime_type);
1198 GList *procmime_get_mime_type_list(void)
1202 gchar buf[BUFFSIZE];
1204 MimeType *mime_type;
1207 return mime_type_list;
1209 if ((fp = fopen("/etc/mime.types", "rb")) == NULL) {
1210 if ((fp = fopen(SYSCONFDIR "/mime.types", "rb")) == NULL) {
1211 FILE_OP_ERROR(SYSCONFDIR "/mime.types", "fopen");
1216 while (fgets(buf, sizeof(buf), fp) != NULL) {
1217 p = strchr(buf, '#');
1222 while (*p && !isspace(*p)) p++;
1227 delim = strchr(buf, '/');
1228 if (delim == NULL) continue;
1231 mime_type = g_new(MimeType, 1);
1232 mime_type->type = g_strdup(buf);
1233 mime_type->sub_type = g_strdup(delim + 1);
1235 while (*p && isspace(*p)) p++;
1237 mime_type->extension = g_strdup(p);
1239 mime_type->extension = NULL;
1241 list = g_list_append(list, mime_type);
1247 g_warning("Can't read mime.types\n");
1252 EncodingType procmime_get_encoding_for_charset(const gchar *charset)
1256 else if (!strncasecmp(charset, "ISO-2022-", 9) ||
1257 !strcasecmp(charset, "US-ASCII"))
1259 else if (!strcasecmp(charset, "ISO-8859-5") ||
1260 !strncasecmp(charset, "KOI8-", 5) ||
1261 !strcasecmp(charset, "Windows-1251"))
1263 else if (!strncasecmp(charset, "ISO-8859-", 9))
1264 return ENC_QUOTED_PRINTABLE;
1269 EncodingType procmime_get_encoding_for_file(const gchar *file)
1275 if ((fp = fopen(file, "rb")) == NULL) {
1276 FILE_OP_ERROR(file, "fopen");
1280 while ((len = fread(buf, sizeof(gchar), sizeof(buf), fp)) > 0) {
1284 for (p = buf, i = 0; i < len; p++, i++) {
1296 struct EncodingTable
1299 EncodingType enc_type;
1302 struct EncodingTable encoding_table[] = {
1305 {"binary", ENC_BINARY},
1306 {"quoted-printable", ENC_QUOTED_PRINTABLE},
1307 {"base64", ENC_BASE64},
1308 {"x-uuencode", ENC_UNKNOWN},
1309 {NULL, ENC_UNKNOWN},
1312 const gchar *procmime_get_encoding_str(EncodingType encoding)
1314 struct EncodingTable *enc_table;
1316 for (enc_table = encoding_table; enc_table->str != NULL; enc_table++) {
1317 if (enc_table->enc_type == encoding)
1318 return enc_table->str;