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);
77 g_free(mimeinfo->plaintextfile);
78 g_free(mimeinfo->sigstatus);
79 g_free(mimeinfo->sigstatus_full);
82 procmime_mimeinfo_free_all(mimeinfo->sub);
83 procmime_mimeinfo_free_all(mimeinfo->children);
85 procmime_mimeinfo_free_all(mimeinfo->plaintext);
88 next = mimeinfo->next;
94 MimeInfo *procmime_mimeinfo_insert(MimeInfo *parent, MimeInfo *mimeinfo)
96 MimeInfo *child = parent->children;
99 parent->children = mimeinfo;
101 while (child->next != NULL)
104 child->next = mimeinfo;
107 mimeinfo->parent = parent;
108 mimeinfo->level = parent->level + 1;
113 void procmime_mimeinfo_replace(MimeInfo *old, MimeInfo *new)
115 MimeInfo *parent = old->parent;
118 g_return_if_fail(parent != NULL);
119 g_return_if_fail(new->next == NULL);
121 for (child = parent->children; child && child != old;
125 g_warning("oops: parent can't find it's own child");
128 procmime_mimeinfo_free_all(old);
130 if (child == parent->children) {
131 new->next = parent->children->next;
132 parent->children = new;
134 new->next = child->next;
139 MimeInfo *procmime_mimeinfo_next(MimeInfo *mimeinfo)
141 if (!mimeinfo) return NULL;
143 if (mimeinfo->children)
144 return mimeinfo->children;
146 return mimeinfo->sub;
148 return mimeinfo->next;
150 if (mimeinfo->main) {
151 mimeinfo = mimeinfo->main;
153 return mimeinfo->next;
156 for (mimeinfo = mimeinfo->parent; mimeinfo != NULL;
157 mimeinfo = mimeinfo->parent) {
159 return mimeinfo->next;
160 if (mimeinfo->main) {
161 mimeinfo = mimeinfo->main;
163 return mimeinfo->next;
171 void procmime_dump_mimeinfo(MimeInfo *mimeinfo)
177 for (; mimeinfo != NULL; mimeinfo = procmime_mimeinfo_next(mimeinfo)) {
178 for (i = 0; i < mimeinfo->level; i++)
180 g_print("%s%s\n", mimeinfo->main ? "sub: " : "",
181 mimeinfo->content_type);
186 MimeInfo *procmime_scan_message(MsgInfo *msginfo)
191 g_return_val_if_fail(msginfo != NULL, NULL);
194 if ((fp = procmsg_open_message_decrypted(msginfo, &mimeinfo)) == NULL)
197 if ((fp = procmsg_open_message(msginfo)) == NULL) return NULL;
198 mimeinfo = procmime_scan_mime_header(fp);
202 mimeinfo->size = get_left_file_size(fp);
203 if (mimeinfo->mime_type == MIME_MULTIPART ||
204 mimeinfo->mime_type == MIME_MESSAGE_RFC822)
205 procmime_scan_multipart_message(mimeinfo, fp);
213 void procmime_scan_multipart_message(MimeInfo *mimeinfo, FILE *fp)
217 gint boundary_len = 0;
219 glong fpos, prev_fpos;
221 g_return_if_fail(mimeinfo != NULL);
222 g_return_if_fail(mimeinfo->mime_type == MIME_MULTIPART ||
223 mimeinfo->mime_type == MIME_MESSAGE_RFC822);
225 if (mimeinfo->mime_type == MIME_MULTIPART) {
226 g_return_if_fail(mimeinfo->boundary != NULL);
227 g_return_if_fail(mimeinfo->sub == NULL);
229 g_return_if_fail(fp != NULL);
231 boundary = mimeinfo->boundary;
234 boundary_len = strlen(boundary);
236 /* look for first boundary */
237 while ((p = fgets(buf, sizeof(buf), fp)) != NULL)
238 if (IS_BOUNDARY(buf, boundary, boundary_len)) break;
240 } else if (mimeinfo->parent && mimeinfo->parent->boundary) {
241 boundary = mimeinfo->parent->boundary;
242 boundary_len = strlen(boundary);
245 if ((fpos = ftell(fp)) < 0) {
252 gboolean eom = FALSE;
256 debug_print("prev_fpos: %ld\n", fpos);
258 if (mimeinfo->mime_type == MIME_MESSAGE_RFC822) {
261 mimeinfo->sub = sub = procmime_scan_mime_header(fp);
264 sub->level = mimeinfo->level + 1;
265 sub->parent = mimeinfo->parent;
266 sub->main = mimeinfo;
270 partinfo = procmime_scan_mime_header(fp);
271 if (!partinfo) break;
272 procmime_mimeinfo_insert(mimeinfo, partinfo);
273 debug_print("content-type: %s\n",
274 partinfo->content_type);
277 if (partinfo->mime_type == MIME_MULTIPART ||
278 partinfo->mime_type == MIME_MESSAGE_RFC822) {
279 if (partinfo->level < 8)
280 procmime_scan_multipart_message(partinfo, fp);
283 /* look for next boundary */
285 while ((p = fgets(buf, sizeof(buf), fp)) != NULL) {
286 if (IS_BOUNDARY(buf, boundary, boundary_len)) {
287 if (buf[2 + boundary_len] == '-' &&
288 buf[2 + boundary_len + 1] == '-')
294 /* broken MIME, or single part MIME message */
298 debug_print("boundary: %s\n", buf);
301 debug_print("fpos: %ld\n", fpos);
304 partinfo->size = fpos - prev_fpos - len;
305 debug_print("partinfo->size: %d\n", partinfo->size);
306 if (partinfo->sub && !partinfo->sub->sub &&
307 !partinfo->sub->children) {
308 partinfo->sub->size =
309 fpos - partinfo->sub->fpos - strlen(buf);
310 debug_print("partinfo->sub->size: %d\n",
311 partinfo->sub->size);
314 if (mimeinfo->mime_type == MIME_MESSAGE_RFC822) {
315 if (len > 0 && fseek(fp, fpos - len, SEEK_SET) < 0)
324 void procmime_scan_encoding(MimeInfo *mimeinfo, const gchar *encoding)
328 Xstrdup_a(buf, encoding, return);
330 g_free(mimeinfo->encoding);
332 mimeinfo->encoding = g_strdup(g_strstrip(buf));
333 if (!strcasecmp(buf, "7bit"))
334 mimeinfo->encoding_type = ENC_7BIT;
335 else if (!strcasecmp(buf, "8bit"))
336 mimeinfo->encoding_type = ENC_8BIT;
337 else if (!strcasecmp(buf, "quoted-printable"))
338 mimeinfo->encoding_type = ENC_QUOTED_PRINTABLE;
339 else if (!strcasecmp(buf, "base64"))
340 mimeinfo->encoding_type = ENC_BASE64;
341 else if (!strcasecmp(buf, "x-uuencode"))
342 mimeinfo->encoding_type = ENC_X_UUENCODE;
344 mimeinfo->encoding_type = ENC_UNKNOWN;
348 void procmime_scan_content_type(MimeInfo *mimeinfo, const gchar *content_type)
350 gchar *delim, *p, *cnttype;
353 if (conv_get_current_charset() == C_EUC_JP &&
354 strchr(content_type, '\033')) {
356 len = strlen(content_type) * 2 + 1;
357 Xalloca(buf, len, return);
358 conv_jistoeuc(buf, len, content_type);
360 Xstrdup_a(buf, content_type, return);
362 g_free(mimeinfo->content_type);
363 g_free(mimeinfo->charset);
364 /* g_free(mimeinfo->name); */
365 mimeinfo->content_type = NULL;
366 mimeinfo->charset = NULL;
367 /* mimeinfo->name = NULL; */
369 if ((delim = strchr(buf, ';'))) *delim = '\0';
370 mimeinfo->content_type = cnttype = g_strdup(g_strstrip(buf));
372 mimeinfo->mime_type = procmime_scan_mime_type(cnttype);
381 if ((delim = strchr(p, ';'))) *delim = '\0';
383 if (!(eq = strchr(p, '='))) break;
392 extract_quote(value, '"');
394 eliminate_parenthesis(value, '(', ')');
399 if (!strcasecmp(attr, "charset"))
400 mimeinfo->charset = g_strdup(value);
401 else if (!strcasecmp(attr, "name")) {
405 len = strlen(value) + 1;
406 Xalloca(tmp, len, return);
407 conv_unmime_header(tmp, len, value, NULL);
408 g_free(mimeinfo->name);
409 /*pgp signatures should NOT have a name */
410 if (mimeinfo->content_type
411 && strcasecmp(mimeinfo->content_type, "application/pgp-signature"))
412 mimeinfo->name = g_strdup(tmp);
413 } else if (!strcasecmp(attr, "boundary"))
414 mimeinfo->boundary = g_strdup(value);
421 if (mimeinfo->mime_type == MIME_MULTIPART && !mimeinfo->boundary)
422 mimeinfo->mime_type = MIME_TEXT;
425 void procmime_scan_content_disposition(MimeInfo *mimeinfo,
426 const gchar *content_disposition)
428 gchar *delim, *p, *dispos;
431 if (conv_get_current_charset() == C_EUC_JP &&
432 strchr(content_disposition, '\033')) {
434 len = strlen(content_disposition) * 2 + 1;
435 Xalloca(buf, len, return);
436 conv_jistoeuc(buf, len, content_disposition);
438 Xstrdup_a(buf, content_disposition, return);
440 if ((delim = strchr(buf, ';'))) *delim = '\0';
441 mimeinfo->content_disposition = dispos = g_strdup(g_strstrip(buf));
450 if ((delim = strchr(p, ';'))) *delim = '\0';
452 if (!(eq = strchr(p, '='))) break;
461 extract_quote(value, '"');
463 eliminate_parenthesis(value, '(', ')');
468 if (!strcasecmp(attr, "filename")) {
472 len = strlen(value) + 1;
473 Xalloca(tmp, len, return);
474 conv_unmime_header(tmp, len, value, NULL);
475 g_free(mimeinfo->filename);
476 /*pgp signatures should NOT have a name */
477 if (mimeinfo->content_type
478 && strcasecmp(mimeinfo->content_type, "application/pgp-signature"))
479 mimeinfo->filename = g_strdup(tmp);
489 void procmime_scan_content_description(MimeInfo *mimeinfo,
490 const gchar *content_description)
497 if (conv_get_current_charset() == C_EUC_JP &&
498 strchr(content_description, '\033')) {
500 len = strlen(content_description) * 2 + 1;
501 Xalloca(buf, len, return);
502 conv_jistoeuc(buf, len, content_description);
504 Xstrdup_a(buf, content_description, return);
506 blen = strlen(buf) + 1;
507 Xalloca(tmp, blen, return);
508 conv_unmime_header(tmp, blen, buf, NULL);
509 /*pgp signatures should NOT have a name */
510 if (mimeinfo->content_type
511 && strcasecmp(mimeinfo->content_type, "application/pgp-signature"))
512 mimeinfo->description = g_strdup(tmp);
515 void procmime_scan_subject(MimeInfo *mimeinfo,
516 const gchar *subject)
523 if (conv_get_current_charset() == C_EUC_JP &&
524 strchr(subject, '\033')) {
526 len = strlen(subject) * 2 + 1;
527 Xalloca(buf, len, return);
528 conv_jistoeuc(buf, len, subject);
530 Xstrdup_a(buf, subject, return);
532 blen = strlen(buf) + 1;
533 Xalloca(tmp, blen, return);
534 conv_unmime_header(tmp, blen, buf, NULL);
535 g_free(mimeinfo->name);
536 mimeinfo->name = g_strdup(tmp);
541 H_CONTENT_TRANSFER_ENCODING = 0,
543 H_CONTENT_DISPOSITION = 2,
544 H_CONTENT_DESCRIPTION = 3,
548 MimeInfo *procmime_scan_mime_header(FILE *fp)
550 static HeaderEntry hentry[] = {{"Content-Transfer-Encoding:",
552 {"Content-Type:", NULL, TRUE},
553 {"Content-Disposition:",
555 {"Content-description:",
559 {NULL, NULL, FALSE}};
565 g_return_val_if_fail(fp != NULL, NULL);
567 mimeinfo = procmime_mimeinfo_new();
568 mimeinfo->mime_type = MIME_TEXT;
569 mimeinfo->encoding_type = ENC_7BIT;
570 mimeinfo->fpos = ftell(fp);
572 while ((hnum = procheader_get_one_field(buf, sizeof(buf), fp, hentry))
576 if (H_CONTENT_TRANSFER_ENCODING == hnum) {
577 procmime_scan_encoding
578 (mimeinfo, buf + strlen(hp->name));
579 } else if (H_CONTENT_TYPE == hnum) {
580 procmime_scan_content_type
581 (mimeinfo, buf + strlen(hp->name));
582 } else if (H_CONTENT_DISPOSITION == hnum) {
583 procmime_scan_content_disposition
584 (mimeinfo, buf + strlen(hp->name));
585 } else if (H_CONTENT_DESCRIPTION == hnum) {
586 procmime_scan_content_description
587 (mimeinfo, buf + strlen(hp->name));
588 } else if (H_SUBJECT == hnum) {
589 procmime_scan_subject
590 (mimeinfo, buf + strlen(hp->name));
594 if (!mimeinfo->content_type)
595 mimeinfo->content_type = g_strdup("text/plain");
600 FILE *procmime_decode_content(FILE *outfp, FILE *infp, MimeInfo *mimeinfo)
603 gchar *boundary = NULL;
604 gint boundary_len = 0;
605 gboolean tmp_file = FALSE;
607 g_return_val_if_fail(infp != NULL, NULL);
608 g_return_val_if_fail(mimeinfo != NULL, NULL);
611 outfp = my_tmpfile();
619 if (mimeinfo->parent && mimeinfo->parent->boundary) {
620 boundary = mimeinfo->parent->boundary;
621 boundary_len = strlen(boundary);
624 if (mimeinfo->encoding_type == ENC_QUOTED_PRINTABLE) {
625 while (fgets(buf, sizeof(buf), infp) != NULL &&
627 !IS_BOUNDARY(buf, boundary, boundary_len))) {
629 len = qp_decode_line(buf);
630 fwrite(buf, len, 1, outfp);
632 } else if (mimeinfo->encoding_type == ENC_BASE64) {
633 gchar outbuf[BUFFSIZE];
635 Base64Decoder *decoder;
637 decoder = base64_decoder_new();
638 while (fgets(buf, sizeof(buf), infp) != NULL &&
640 !IS_BOUNDARY(buf, boundary, boundary_len))) {
641 len = base64_decoder_decode(decoder, buf, outbuf);
643 g_warning("Bad BASE64 content\n");
646 fwrite(outbuf, sizeof(gchar), len, outfp);
648 base64_decoder_free(decoder);
649 } else if (mimeinfo->encoding_type == ENC_X_UUENCODE) {
650 gchar outbuf[BUFFSIZE];
652 gboolean flag = FALSE;
654 while (fgets(buf, sizeof(buf), infp) != NULL &&
656 !IS_BOUNDARY(buf, boundary, boundary_len))) {
657 if(!flag && strncmp(buf,"begin ", 6)) continue;
660 len = fromuutobits(outbuf, buf);
663 g_warning("Bad UUENCODE content(%d)\n", len);
666 fwrite(outbuf, sizeof(gchar), len, outfp);
671 while (fgets(buf, sizeof(buf), infp) != NULL &&
673 !IS_BOUNDARY(buf, boundary, boundary_len))) {
678 if (tmp_file) rewind(outfp);
682 gint procmime_get_part(const gchar *outfile, const gchar *infile,
688 g_return_val_if_fail(outfile != NULL, -1);
689 g_return_val_if_fail(infile != NULL, -1);
690 g_return_val_if_fail(mimeinfo != NULL, -1);
692 if ((infp = fopen(infile, "rb")) == NULL) {
693 FILE_OP_ERROR(infile, "fopen");
696 if (fseek(infp, mimeinfo->fpos, SEEK_SET) < 0) {
697 FILE_OP_ERROR(infile, "fseek");
701 if ((outfp = fopen(outfile, "wb")) == NULL) {
702 FILE_OP_ERROR(outfile, "fopen");
707 while (fgets(buf, sizeof(buf), infp) != NULL)
708 if (buf[0] == '\r' || buf[0] == '\n') break;
710 procmime_decode_content(outfp, infp, mimeinfo);
713 if (fclose(outfp) == EOF) {
714 FILE_OP_ERROR(outfile, "fclose");
722 struct ContentRenderer {
727 static GList * renderer_list = NULL;
729 static struct ContentRenderer *
730 content_renderer_new(char * content_type, char * renderer)
732 struct ContentRenderer * cr;
734 cr = g_new(struct ContentRenderer, 1);
738 cr->content_type = g_strdup(content_type);
739 cr->renderer = g_strdup(renderer);
744 static void content_renderer_free(struct ContentRenderer * cr)
746 g_free(cr->content_type);
747 g_free(cr->renderer);
751 void renderer_read_config(void)
757 g_list_foreach(renderer_list, (GFunc) content_renderer_free, NULL);
758 renderer_list = NULL;
760 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, RENDERER_RC, NULL);
761 f = fopen(rcpath, "rb");
767 while (fgets(buf, BUFFSIZE, f)) {
769 struct ContentRenderer * cr;
772 p = strchr(buf, ' ');
777 cr = content_renderer_new(buf, p + 1);
781 renderer_list = g_list_append(renderer_list, cr);
787 void renderer_write_config(void)
793 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, RENDERER_RC, NULL);
795 if ((pfile = prefs_write_open(rcpath)) == NULL) {
796 g_warning("failed to write configuration to file\n");
803 for(cur = renderer_list ; cur != NULL ; cur = cur->next) {
804 struct ContentRenderer * renderer;
805 renderer = cur->data;
806 fprintf(pfile->fp, "%s %s\n", renderer->content_type,
810 if (prefs_file_close(pfile) < 0) {
811 g_warning("failed to write configuration to file\n");
816 FILE *procmime_get_text_content(MimeInfo *mimeinfo, FILE *infp)
820 gboolean conv_fail = FALSE;
823 struct ContentRenderer * renderer;
826 g_return_val_if_fail(mimeinfo != NULL, NULL);
827 g_return_val_if_fail(infp != NULL, NULL);
828 g_return_val_if_fail(mimeinfo->mime_type == MIME_TEXT ||
829 mimeinfo->mime_type == MIME_TEXT_HTML ||
830 mimeinfo->mime_type == MIME_TEXT_ENRICHED, NULL);
832 if (fseek(infp, mimeinfo->fpos, SEEK_SET) < 0) {
837 while (fgets(buf, sizeof(buf), infp) != NULL)
838 if (buf[0] == '\r' || buf[0] == '\n') break;
840 tmpfp = procmime_decode_content(NULL, infp, mimeinfo);
844 if ((outfp = my_tmpfile()) == NULL) {
850 src_codeset = prefs_common.force_charset
851 ? prefs_common.force_charset : mimeinfo->charset;
855 for(cur = renderer_list ; cur != NULL ; cur = cur->next) {
856 struct ContentRenderer * cr;
858 if (g_strcasecmp(cr->content_type,
859 mimeinfo->content_type) == 0) {
865 if (renderer != NULL) {
871 dup2(fileno(outfp), 1);
873 p = popen(renderer->renderer, "w");
878 fread(buf, sizeof(char), sizeof(buf),
880 fwrite(buf, sizeof(char), count, p);
885 } else if (mimeinfo->mime_type == MIME_TEXT) {
886 while (fgets(buf, sizeof(buf), tmpfp) != NULL) {
887 str = conv_codeset_strdup(buf, src_codeset, NULL);
896 } else if (mimeinfo->mime_type == MIME_TEXT_HTML) {
900 conv = conv_code_converter_new(src_codeset);
901 parser = html_parser_new(tmpfp, conv);
902 while ((str = html_parse(parser)) != NULL) {
905 html_parser_destroy(parser);
906 conv_code_converter_destroy(conv);
907 } else if (mimeinfo->mime_type == MIME_TEXT_ENRICHED) {
911 conv = conv_code_converter_new(src_codeset);
912 parser = ertf_parser_new(tmpfp, conv);
913 while ((str = ertf_parse(parser)) != NULL) {
916 ertf_parser_destroy(parser);
917 conv_code_converter_destroy(conv);
921 g_warning("procmime_get_text_content(): Code conversion failed.\n");
929 /* search the first text part of (multipart) MIME message,
930 decode, convert it and output to outfp. */
931 FILE *procmime_get_first_text_content(MsgInfo *msginfo)
933 FILE *infp, *outfp = NULL;
934 MimeInfo *mimeinfo, *partinfo;
936 g_return_val_if_fail(msginfo != NULL, NULL);
938 mimeinfo = procmime_scan_message(msginfo);
939 if (!mimeinfo) return NULL;
941 if ((infp = procmsg_open_message(msginfo)) == NULL) {
942 procmime_mimeinfo_free_all(mimeinfo);
947 while (partinfo && partinfo->mime_type != MIME_TEXT)
948 partinfo = procmime_mimeinfo_next(partinfo);
951 while (partinfo && partinfo->mime_type != MIME_TEXT_HTML &&
952 partinfo->mime_type != MIME_TEXT_ENRICHED)
953 partinfo = procmime_mimeinfo_next(partinfo);
958 outfp = procmime_get_text_content(partinfo, infp);
961 procmime_mimeinfo_free_all(mimeinfo);
966 gboolean procmime_find_string_part(MimeInfo *mimeinfo, const gchar *filename,
967 const gchar *str, gboolean case_sens)
972 gchar *(* StrFindFunc) (const gchar *haystack, const gchar *needle);
974 g_return_val_if_fail(mimeinfo != NULL, FALSE);
975 g_return_val_if_fail(mimeinfo->mime_type == MIME_TEXT ||
976 mimeinfo->mime_type == MIME_TEXT_HTML ||
977 mimeinfo->mime_type == MIME_TEXT_ENRICHED, FALSE);
978 g_return_val_if_fail(str != NULL, FALSE);
980 if ((infp = fopen(filename, "rb")) == NULL) {
981 FILE_OP_ERROR(filename, "fopen");
985 outfp = procmime_get_text_content(mimeinfo, infp);
992 StrFindFunc = strstr;
994 StrFindFunc = strcasestr;
996 while (fgets(buf, sizeof(buf), outfp) != NULL) {
997 if (StrFindFunc(buf, str) != NULL) {
1008 gboolean procmime_find_string(MsgInfo *msginfo, const gchar *str,
1014 gboolean found = FALSE;
1016 g_return_val_if_fail(msginfo != NULL, FALSE);
1017 g_return_val_if_fail(str != NULL, FALSE);
1019 filename = procmsg_get_message_file(msginfo);
1020 if (!filename) return FALSE;
1021 mimeinfo = procmime_scan_message(msginfo);
1023 for (partinfo = mimeinfo; partinfo != NULL;
1024 partinfo = procmime_mimeinfo_next(partinfo)) {
1025 if (partinfo->mime_type == MIME_TEXT ||
1026 partinfo->mime_type == MIME_TEXT_HTML ||
1027 partinfo->mime_type == MIME_TEXT_ENRICHED) {
1028 if (procmime_find_string_part
1029 (partinfo, filename, str, case_sens) == TRUE) {
1036 procmime_mimeinfo_free_all(mimeinfo);
1042 gchar *procmime_get_tmp_file_name(MimeInfo *mimeinfo)
1044 static guint32 id = 0;
1049 g_return_val_if_fail(mimeinfo != NULL, NULL);
1051 g_snprintf(f_prefix, sizeof(f_prefix), "%08x.", id++);
1053 if (MIME_TEXT_HTML == mimeinfo->mime_type)
1054 base = "mimetmp.html";
1056 base = mimeinfo->filename ? mimeinfo->filename
1057 : mimeinfo->name ? mimeinfo->name : "mimetmp";
1058 base = g_basename(base);
1059 if (*base == '\0') base = "mimetmp";
1060 Xstrdup_a(base, base, return NULL);
1061 subst_for_filename(base);
1064 filename = g_strconcat(get_mime_tmp_dir(), G_DIR_SEPARATOR_S,
1065 f_prefix, base, NULL);
1070 ContentType procmime_scan_mime_type(const gchar *mime_type)
1074 if (!strncasecmp(mime_type, "text/html", 9))
1075 type = MIME_TEXT_HTML;
1076 else if (!strncasecmp(mime_type, "text/enriched", 13))
1077 type = MIME_TEXT_ENRICHED;
1078 else if (!strncasecmp(mime_type, "text/", 5))
1080 else if (!strncasecmp(mime_type, "message/rfc822", 14))
1081 type = MIME_MESSAGE_RFC822;
1082 else if (!strncasecmp(mime_type, "message/", 8))
1084 else if (!strncasecmp(mime_type, "application/octet-stream", 24))
1085 type = MIME_APPLICATION_OCTET_STREAM;
1086 else if (!strncasecmp(mime_type, "application/", 12))
1087 type = MIME_APPLICATION;
1088 else if (!strncasecmp(mime_type, "multipart/", 10))
1089 type = MIME_MULTIPART;
1090 else if (!strncasecmp(mime_type, "image/", 6))
1092 else if (!strncasecmp(mime_type, "audio/", 6))
1094 else if (!strcasecmp(mime_type, "text"))
1097 type = MIME_UNKNOWN;
1102 static GList *mime_type_list = NULL;
1104 gchar *procmime_get_mime_type(const gchar *filename)
1106 static GHashTable *mime_type_table = NULL;
1107 MimeType *mime_type;
1111 if (!mime_type_table) {
1112 mime_type_table = procmime_get_mime_type_table();
1113 if (!mime_type_table) return NULL;
1116 filename = g_basename(filename);
1117 p = strrchr(filename, '.');
1118 if (!p) return NULL;
1120 Xstrdup_a(ext, p + 1, return NULL);
1122 mime_type = g_hash_table_lookup(mime_type_table, ext);
1126 str = g_strconcat(mime_type->type, "/", mime_type->sub_type,
1134 static guint procmime_str_hash(gconstpointer gptr)
1136 guint hash_result = 0;
1139 for (str = gptr; str && *str; str++) {
1140 if (isupper(*str)) hash_result += (*str + ' ');
1141 else hash_result += *str;
1147 static gint procmime_str_equal(gconstpointer gptr1, gconstpointer gptr2)
1149 const char *str1 = gptr1;
1150 const char *str2 = gptr2;
1152 return !strcasecmp(str1, str2);
1155 static GHashTable *procmime_get_mime_type_table(void)
1157 GHashTable *table = NULL;
1159 MimeType *mime_type;
1162 if (!mime_type_list) {
1163 mime_type_list = procmime_get_mime_type_list();
1164 if (!mime_type_list) return NULL;
1167 table = g_hash_table_new(procmime_str_hash, procmime_str_equal);
1169 for (cur = mime_type_list; cur != NULL; cur = cur->next) {
1173 mime_type = (MimeType *)cur->data;
1175 if (!mime_type->extension) continue;
1177 exts = g_strsplit(mime_type->extension, " ", 16);
1178 for (i = 0; exts[i] != NULL; i++) {
1179 /* make the key case insensitive */
1181 /* use previously dup'd key on overwriting */
1182 if (g_hash_table_lookup(table, exts[i]))
1185 key = g_strdup(exts[i]);
1186 g_hash_table_insert(table, key, mime_type);
1194 GList *procmime_get_mime_type_list(void)
1198 gchar buf[BUFFSIZE];
1200 MimeType *mime_type;
1203 return mime_type_list;
1205 if ((fp = fopen("/etc/mime.types", "rb")) == NULL) {
1206 if ((fp = fopen(SYSCONFDIR "/mime.types", "rb")) == NULL) {
1207 FILE_OP_ERROR(SYSCONFDIR "/mime.types", "fopen");
1212 while (fgets(buf, sizeof(buf), fp) != NULL) {
1213 p = strchr(buf, '#');
1218 while (*p && !isspace(*p)) p++;
1223 delim = strchr(buf, '/');
1224 if (delim == NULL) continue;
1227 mime_type = g_new(MimeType, 1);
1228 mime_type->type = g_strdup(buf);
1229 mime_type->sub_type = g_strdup(delim + 1);
1231 while (*p && isspace(*p)) p++;
1233 mime_type->extension = g_strdup(p);
1235 mime_type->extension = NULL;
1237 list = g_list_append(list, mime_type);
1243 g_warning("Can't read mime.types\n");
1248 EncodingType procmime_get_encoding_for_charset(const gchar *charset)
1252 else if (!strncasecmp(charset, "ISO-2022-", 9) ||
1253 !strcasecmp(charset, "US-ASCII"))
1255 else if (!strcasecmp(charset, "ISO-8859-5") ||
1256 !strncasecmp(charset, "KOI8-", 5) ||
1257 !strcasecmp(charset, "Windows-1251"))
1259 else if (!strncasecmp(charset, "ISO-8859-", 9))
1260 return ENC_QUOTED_PRINTABLE;
1265 EncodingType procmime_get_encoding_for_file(const gchar *file)
1271 if ((fp = fopen(file, "rb")) == NULL) {
1272 FILE_OP_ERROR(file, "fopen");
1276 while ((len = fread(buf, sizeof(gchar), sizeof(buf), fp)) > 0) {
1280 for (p = buf, i = 0; i < len; p++, i++) {
1292 struct EncodingTable
1295 EncodingType enc_type;
1298 struct EncodingTable encoding_table[] = {
1301 {"binary", ENC_BINARY},
1302 {"quoted-printable", ENC_QUOTED_PRINTABLE},
1303 {"base64", ENC_BASE64},
1304 {"x-uuencode", ENC_UNKNOWN},
1305 {NULL, ENC_UNKNOWN},
1308 const gchar *procmime_get_encoding_str(EncodingType encoding)
1310 struct EncodingTable *enc_table;
1312 for (enc_table = encoding_table; enc_table->str != NULL; enc_table++) {
1313 if (enc_table->enc_type == encoding)
1314 return enc_table->str;