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;
170 MimeInfo *procmime_scan_message(MsgInfo *msginfo)
175 g_return_val_if_fail(msginfo != NULL, NULL);
177 if ((fp = procmsg_open_message(msginfo)) == NULL) return NULL;
178 mimeinfo = procmime_scan_mime_header(fp);
181 if (mimeinfo->mime_type != MIME_MULTIPART) {
182 if (fseek(fp, mimeinfo->fpos, SEEK_SET) < 0)
185 if (mimeinfo->mime_type != MIME_TEXT)
186 procmime_scan_multipart_message(mimeinfo, fp);
190 if (prefs_common.auto_check_signatures)
191 rfc2015_check_signature(mimeinfo, fp);
198 void procmime_scan_multipart_message(MimeInfo *mimeinfo, FILE *fp)
202 gint boundary_len = 0;
204 glong fpos, prev_fpos;
207 g_return_if_fail(mimeinfo != NULL);
208 g_return_if_fail(mimeinfo->mime_type != MIME_TEXT);
210 if (mimeinfo->mime_type == MIME_MULTIPART) {
211 g_return_if_fail(mimeinfo->boundary != NULL);
212 g_return_if_fail(mimeinfo->sub == NULL);
214 g_return_if_fail(fp != NULL);
216 boundary = mimeinfo->boundary;
219 boundary_len = strlen(boundary);
221 /* look for first boundary */
222 while ((p = fgets(buf, sizeof(buf), fp)) != NULL)
223 if (IS_BOUNDARY(buf, boundary, boundary_len)) break;
227 if ((fpos = ftell(fp)) < 0) {
232 for (npart = 0;; npart++) {
234 gboolean eom = FALSE;
237 debug_print("prev_fpos: %ld\n", fpos);
239 partinfo = procmime_scan_mime_header(fp);
240 if (!partinfo) break;
241 procmime_mimeinfo_insert(mimeinfo, partinfo);
243 if (partinfo->mime_type == MIME_MULTIPART) {
244 if (partinfo->level < 8)
245 procmime_scan_multipart_message(partinfo, fp);
246 } else if (partinfo->mime_type == MIME_MESSAGE_RFC822) {
249 partinfo->sub = sub = procmime_scan_mime_header(fp);
252 sub->level = partinfo->level + 1;
253 sub->parent = partinfo;
254 sub->main = partinfo;
256 if (sub->level < 8) {
257 if (sub->mime_type == MIME_MULTIPART) {
258 procmime_scan_multipart_message
260 } else if (sub->mime_type == MIME_MESSAGE_RFC822) {
261 fseek(fp, sub->fpos, SEEK_SET);
262 procmime_scan_multipart_message
268 /* look for next boundary */
270 while ((p = fgets(buf, sizeof(buf), fp)) != NULL) {
271 if (IS_BOUNDARY(buf, boundary, boundary_len)) {
272 if (buf[2 + boundary_len] == '-' &&
273 buf[2 + boundary_len + 1] == '-')
279 /* broken MIME, or single part MIME message */
284 debug_print("fpos: %ld\n", fpos);
286 partinfo->size = fpos - prev_fpos - strlen(buf);
287 debug_print("partinfo->size: %d\n", partinfo->size);
288 if (partinfo->sub && !partinfo->sub->sub &&
289 !partinfo->sub->children) {
290 partinfo->sub->size = fpos - partinfo->sub->fpos - strlen(buf);
291 debug_print("partinfo->sub->size: %d\n",
292 partinfo->sub->size);
294 debug_print("boundary: %s\n", buf);
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 g_free(mimeinfo->name);
385 /*pgp signatures should NOT have a name */
386 if (mimeinfo->content_type
387 && strcasecmp(mimeinfo->content_type, "application/pgp-signature"))
388 mimeinfo->name = g_strdup(tmp);
389 } else if (!strcasecmp(attr, "boundary"))
390 mimeinfo->boundary = g_strdup(value);
397 if (mimeinfo->mime_type == MIME_MULTIPART && !mimeinfo->boundary)
398 mimeinfo->mime_type = MIME_TEXT;
401 void procmime_scan_content_disposition(MimeInfo *mimeinfo,
402 const gchar *content_disposition)
404 gchar *delim, *p, *dispos;
407 if (conv_get_current_charset() == C_EUC_JP &&
408 strchr(content_disposition, '\033')) {
410 len = strlen(content_disposition) * 2 + 1;
411 Xalloca(buf, len, return);
412 conv_jistoeuc(buf, len, content_disposition);
414 Xstrdup_a(buf, content_disposition, return);
416 if ((delim = strchr(buf, ';'))) *delim = '\0';
417 mimeinfo->content_disposition = dispos = g_strdup(g_strstrip(buf));
426 if ((delim = strchr(p, ';'))) *delim = '\0';
428 if (!(eq = strchr(p, '='))) break;
437 extract_quote(value, '"');
439 eliminate_parenthesis(value, '(', ')');
444 if (!strcasecmp(attr, "filename")) {
448 len = strlen(value) + 1;
449 Xalloca(tmp, len, return);
450 conv_unmime_header(tmp, len, value, NULL);
451 g_free(mimeinfo->filename);
452 /*pgp signatures should NOT have a name */
453 if (mimeinfo->content_type
454 && strcasecmp(mimeinfo->content_type, "application/pgp-signature"))
455 mimeinfo->filename = g_strdup(tmp);
465 void procmime_scan_content_description(MimeInfo *mimeinfo,
466 const gchar *content_description)
473 if (conv_get_current_charset() == C_EUC_JP &&
474 strchr(content_description, '\033')) {
476 len = strlen(content_description) * 2 + 1;
477 Xalloca(buf, len, return);
478 conv_jistoeuc(buf, len, content_description);
480 Xstrdup_a(buf, content_description, return);
482 blen = strlen(buf) + 1;
483 Xalloca(tmp, blen, return);
484 conv_unmime_header(tmp, blen, buf, NULL);
485 /*pgp signatures should NOT have a name */
486 if (mimeinfo->content_type
487 && strcasecmp(mimeinfo->content_type, "application/pgp-signature"))
488 mimeinfo->description = g_strdup(tmp);
491 void procmime_scan_subject(MimeInfo *mimeinfo,
492 const gchar *subject)
499 if (conv_get_current_charset() == C_EUC_JP &&
500 strchr(subject, '\033')) {
502 len = strlen(subject) * 2 + 1;
503 Xalloca(buf, len, return);
504 conv_jistoeuc(buf, len, subject);
506 Xstrdup_a(buf, subject, return);
508 blen = strlen(buf) + 1;
509 Xalloca(tmp, blen, return);
510 conv_unmime_header(tmp, blen, buf, NULL);
511 g_free(mimeinfo->name);
512 mimeinfo->name = g_strdup(tmp);
517 H_CONTENT_TRANSFER_ENCODING = 0,
519 H_CONTENT_DISPOSITION = 2,
520 H_CONTENT_DESCRIPTION = 3,
524 MimeInfo *procmime_scan_mime_header(FILE *fp)
526 static HeaderEntry hentry[] = {{"Content-Transfer-Encoding:",
528 {"Content-Type:", NULL, TRUE},
529 {"Content-Disposition:",
531 {"Content-description:",
535 {NULL, NULL, FALSE}};
541 g_return_val_if_fail(fp != NULL, NULL);
543 mimeinfo = procmime_mimeinfo_new();
544 mimeinfo->mime_type = MIME_TEXT;
545 mimeinfo->encoding_type = ENC_7BIT;
546 mimeinfo->fpos = ftell(fp);
548 while ((hnum = procheader_get_one_field(buf, sizeof(buf), fp, hentry))
552 if (H_CONTENT_TRANSFER_ENCODING == hnum) {
553 procmime_scan_encoding
554 (mimeinfo, buf + strlen(hp->name));
555 } else if (H_CONTENT_TYPE == hnum) {
556 procmime_scan_content_type
557 (mimeinfo, buf + strlen(hp->name));
558 } else if (H_CONTENT_DISPOSITION == hnum) {
559 procmime_scan_content_disposition
560 (mimeinfo, buf + strlen(hp->name));
561 } else if (H_CONTENT_DESCRIPTION == hnum) {
562 procmime_scan_content_description
563 (mimeinfo, buf + strlen(hp->name));
564 } else if (H_SUBJECT == hnum) {
565 procmime_scan_subject
566 (mimeinfo, buf + strlen(hp->name));
570 if (!mimeinfo->content_type)
571 mimeinfo->content_type = g_strdup("text/plain");
576 FILE *procmime_decode_content(FILE *outfp, FILE *infp, MimeInfo *mimeinfo)
579 gchar *boundary = NULL;
580 gint boundary_len = 0;
581 gboolean tmp_file = FALSE;
583 g_return_val_if_fail(infp != NULL, NULL);
584 g_return_val_if_fail(mimeinfo != NULL, NULL);
587 outfp = my_tmpfile();
595 if (mimeinfo->parent && mimeinfo->parent->boundary) {
596 boundary = mimeinfo->parent->boundary;
597 boundary_len = strlen(boundary);
600 if (mimeinfo->encoding_type == ENC_QUOTED_PRINTABLE) {
601 while (fgets(buf, sizeof(buf), infp) != NULL &&
603 !IS_BOUNDARY(buf, boundary, boundary_len))) {
605 len = qp_decode_line(buf);
606 fwrite(buf, len, 1, outfp);
608 } else if (mimeinfo->encoding_type == ENC_BASE64) {
609 gchar outbuf[BUFFSIZE];
611 Base64Decoder *decoder;
613 decoder = base64_decoder_new();
614 while (fgets(buf, sizeof(buf), infp) != NULL &&
616 !IS_BOUNDARY(buf, boundary, boundary_len))) {
617 len = base64_decoder_decode(decoder, buf, outbuf);
619 g_warning("Bad BASE64 content\n");
622 fwrite(outbuf, sizeof(gchar), len, outfp);
624 base64_decoder_free(decoder);
625 } else if (mimeinfo->encoding_type == ENC_X_UUENCODE) {
626 gchar outbuf[BUFFSIZE];
628 gboolean flag = FALSE;
630 while (fgets(buf, sizeof(buf), infp) != NULL &&
632 !IS_BOUNDARY(buf, boundary, boundary_len))) {
633 if(!flag && strncmp(buf,"begin ", 6)) continue;
636 len = fromuutobits(outbuf, buf);
639 g_warning("Bad UUENCODE content(%d)\n", len);
642 fwrite(outbuf, sizeof(gchar), len, outfp);
647 while (fgets(buf, sizeof(buf), infp) != NULL &&
649 !IS_BOUNDARY(buf, boundary, boundary_len))) {
654 if (tmp_file) rewind(outfp);
658 gint procmime_get_part(const gchar *outfile, const gchar *infile,
664 g_return_val_if_fail(outfile != NULL, -1);
665 g_return_val_if_fail(infile != NULL, -1);
666 g_return_val_if_fail(mimeinfo != NULL, -1);
668 if ((infp = fopen(infile, "rb")) == NULL) {
669 FILE_OP_ERROR(infile, "fopen");
672 if (fseek(infp, mimeinfo->fpos, SEEK_SET) < 0) {
673 FILE_OP_ERROR(infile, "fseek");
677 if ((outfp = fopen(outfile, "wb")) == NULL) {
678 FILE_OP_ERROR(outfile, "fopen");
683 while (fgets(buf, sizeof(buf), infp) != NULL)
684 if (buf[0] == '\r' || buf[0] == '\n') break;
686 procmime_decode_content(outfp, infp, mimeinfo);
689 if (fclose(outfp) == EOF) {
690 FILE_OP_ERROR(outfile, "fclose");
698 struct ContentRenderer {
703 static GList * renderer_list = NULL;
705 static struct ContentRenderer *
706 content_renderer_new(char * content_type, char * renderer)
708 struct ContentRenderer * cr;
710 cr = g_new(struct ContentRenderer, 1);
714 cr->content_type = g_strdup(content_type);
715 cr->renderer = g_strdup(renderer);
720 static void content_renderer_free(struct ContentRenderer * cr)
722 g_free(cr->content_type);
723 g_free(cr->renderer);
727 void renderer_read_config(void)
733 g_list_foreach(renderer_list, (GFunc) content_renderer_free, NULL);
734 renderer_list = NULL;
736 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, RENDERER_RC, NULL);
737 f = fopen(rcpath, "rb");
743 while (fgets(buf, BUFFSIZE, f)) {
745 struct ContentRenderer * cr;
748 p = strchr(buf, ' ');
753 cr = content_renderer_new(buf, p + 1);
757 renderer_list = g_list_append(renderer_list, cr);
763 void renderer_write_config(void)
769 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, RENDERER_RC, NULL);
771 if ((pfile = prefs_write_open(rcpath)) == NULL) {
772 g_warning("failed to write configuration to file\n");
779 for(cur = renderer_list ; cur != NULL ; cur = cur->next) {
780 struct ContentRenderer * renderer;
781 renderer = cur->data;
782 fprintf(pfile->fp, "%s %s\n", renderer->content_type,
786 if (prefs_file_close(pfile) < 0) {
787 g_warning("failed to write configuration to file\n");
792 FILE *procmime_get_text_content(MimeInfo *mimeinfo, FILE *infp)
796 gboolean conv_fail = FALSE;
799 struct ContentRenderer * renderer;
802 g_return_val_if_fail(mimeinfo != NULL, NULL);
803 g_return_val_if_fail(infp != NULL, NULL);
804 g_return_val_if_fail(mimeinfo->mime_type == MIME_TEXT ||
805 mimeinfo->mime_type == MIME_TEXT_HTML ||
806 mimeinfo->mime_type == MIME_TEXT_ENRICHED, NULL);
808 if (fseek(infp, mimeinfo->fpos, SEEK_SET) < 0) {
813 while (fgets(buf, sizeof(buf), infp) != NULL)
814 if (buf[0] == '\r' || buf[0] == '\n') break;
816 tmpfp = procmime_decode_content(NULL, infp, mimeinfo);
820 if ((outfp = my_tmpfile()) == NULL) {
826 src_codeset = prefs_common.force_charset
827 ? prefs_common.force_charset : mimeinfo->charset;
831 for(cur = renderer_list ; cur != NULL ; cur = cur->next) {
832 struct ContentRenderer * cr;
834 if (g_strcasecmp(cr->content_type,
835 mimeinfo->content_type) == 0) {
841 if (renderer != NULL) {
847 dup2(fileno(outfp), 1);
849 p = popen(renderer->renderer, "w");
854 fread(buf, sizeof(char), sizeof(buf),
856 fwrite(buf, sizeof(char), count, p);
861 } else if (mimeinfo->mime_type == MIME_TEXT) {
862 while (fgets(buf, sizeof(buf), tmpfp) != NULL) {
863 str = conv_codeset_strdup(buf, src_codeset, NULL);
872 } else if (mimeinfo->mime_type == MIME_TEXT_HTML) {
876 conv = conv_code_converter_new(src_codeset);
877 parser = html_parser_new(tmpfp, conv);
878 while ((str = html_parse(parser)) != NULL) {
881 html_parser_destroy(parser);
882 conv_code_converter_destroy(conv);
883 } else if (mimeinfo->mime_type == MIME_TEXT_ENRICHED) {
887 conv = conv_code_converter_new(src_codeset);
888 parser = ertf_parser_new(tmpfp, conv);
889 while ((str = ertf_parse(parser)) != NULL) {
892 ertf_parser_destroy(parser);
893 conv_code_converter_destroy(conv);
897 g_warning("procmime_get_text_content(): Code conversion failed.\n");
905 /* search the first text part of (multipart) MIME message,
906 decode, convert it and output to outfp. */
907 FILE *procmime_get_first_text_content(MsgInfo *msginfo)
909 FILE *infp, *outfp = NULL;
910 MimeInfo *mimeinfo, *partinfo;
912 g_return_val_if_fail(msginfo != NULL, NULL);
914 mimeinfo = procmime_scan_message(msginfo);
915 if (!mimeinfo) return NULL;
917 if ((infp = procmsg_open_message(msginfo)) == NULL) {
918 procmime_mimeinfo_free_all(mimeinfo);
923 while (partinfo && partinfo->mime_type != MIME_TEXT)
924 partinfo = procmime_mimeinfo_next(partinfo);
927 while (partinfo && partinfo->mime_type != MIME_TEXT_HTML &&
928 partinfo->mime_type != MIME_TEXT_ENRICHED)
929 partinfo = procmime_mimeinfo_next(partinfo);
934 outfp = procmime_get_text_content(partinfo, infp);
937 procmime_mimeinfo_free_all(mimeinfo);
942 gboolean procmime_find_string_part(MimeInfo *mimeinfo, const gchar *filename,
943 const gchar *str, gboolean case_sens)
948 gchar *(* StrFindFunc) (const gchar *haystack, const gchar *needle);
950 g_return_val_if_fail(mimeinfo != NULL, FALSE);
951 g_return_val_if_fail(mimeinfo->mime_type == MIME_TEXT ||
952 mimeinfo->mime_type == MIME_TEXT_HTML ||
953 mimeinfo->mime_type == MIME_TEXT_ENRICHED, FALSE);
954 g_return_val_if_fail(str != NULL, FALSE);
956 if ((infp = fopen(filename, "rb")) == NULL) {
957 FILE_OP_ERROR(filename, "fopen");
961 outfp = procmime_get_text_content(mimeinfo, infp);
968 StrFindFunc = strstr;
970 StrFindFunc = strcasestr;
972 while (fgets(buf, sizeof(buf), outfp) != NULL) {
973 if (StrFindFunc(buf, str) != NULL) {
984 gboolean procmime_find_string(MsgInfo *msginfo, const gchar *str,
990 gboolean found = FALSE;
992 g_return_val_if_fail(msginfo != NULL, FALSE);
993 g_return_val_if_fail(str != NULL, FALSE);
995 filename = procmsg_get_message_file(msginfo);
996 if (!filename) return FALSE;
997 mimeinfo = procmime_scan_message(msginfo);
999 for (partinfo = mimeinfo; partinfo != NULL;
1000 partinfo = procmime_mimeinfo_next(partinfo)) {
1001 if (partinfo->mime_type == MIME_TEXT ||
1002 partinfo->mime_type == MIME_TEXT_HTML ||
1003 partinfo->mime_type == MIME_TEXT_ENRICHED) {
1004 if (procmime_find_string_part
1005 (partinfo, filename, str, case_sens) == TRUE) {
1012 procmime_mimeinfo_free_all(mimeinfo);
1018 gchar *procmime_get_tmp_file_name(MimeInfo *mimeinfo)
1020 static guint32 id = 0;
1025 g_return_val_if_fail(mimeinfo != NULL, NULL);
1027 g_snprintf(f_prefix, sizeof(f_prefix), "%08x.", id++);
1029 if (MIME_TEXT_HTML == mimeinfo->mime_type)
1030 base = "mimetmp.html";
1032 base = mimeinfo->filename ? mimeinfo->filename
1033 : mimeinfo->name ? mimeinfo->name : "mimetmp";
1034 base = g_basename(base);
1035 if (*base == '\0') base = "mimetmp";
1036 Xstrdup_a(base, base, return NULL);
1037 subst_for_filename(base);
1040 filename = g_strconcat(get_mime_tmp_dir(), G_DIR_SEPARATOR_S,
1041 f_prefix, base, NULL);
1046 ContentType procmime_scan_mime_type(const gchar *mime_type)
1050 if (!strncasecmp(mime_type, "text/html", 9))
1051 type = MIME_TEXT_HTML;
1052 else if (!strncasecmp(mime_type, "text/enriched", 13))
1053 type = MIME_TEXT_ENRICHED;
1054 else if (!strncasecmp(mime_type, "text/", 5))
1056 else if (!strncasecmp(mime_type, "message/rfc822", 14))
1057 type = MIME_MESSAGE_RFC822;
1058 else if (!strncasecmp(mime_type, "message/", 8))
1060 else if (!strncasecmp(mime_type, "application/octet-stream", 24))
1061 type = MIME_APPLICATION_OCTET_STREAM;
1062 else if (!strncasecmp(mime_type, "application/", 12))
1063 type = MIME_APPLICATION;
1064 else if (!strncasecmp(mime_type, "multipart/", 10))
1065 type = MIME_MULTIPART;
1066 else if (!strncasecmp(mime_type, "image/", 6))
1068 else if (!strncasecmp(mime_type, "audio/", 6))
1070 else if (!strcasecmp(mime_type, "text"))
1073 type = MIME_UNKNOWN;
1078 static GList *mime_type_list = NULL;
1080 gchar *procmime_get_mime_type(const gchar *filename)
1082 static GHashTable *mime_type_table = NULL;
1083 MimeType *mime_type;
1087 if (!mime_type_table) {
1088 mime_type_table = procmime_get_mime_type_table();
1089 if (!mime_type_table) return NULL;
1092 filename = g_basename(filename);
1093 p = strrchr(filename, '.');
1094 if (!p) return NULL;
1096 Xstrdup_a(ext, p + 1, return NULL);
1098 mime_type = g_hash_table_lookup(mime_type_table, ext);
1102 str = g_strconcat(mime_type->type, "/", mime_type->sub_type,
1110 static guint procmime_str_hash(gconstpointer gptr)
1112 guint hash_result = 0;
1115 for (str = gptr; str && *str; str++) {
1116 if (isupper(*str)) hash_result += (*str + ' ');
1117 else hash_result += *str;
1123 static gint procmime_str_equal(gconstpointer gptr1, gconstpointer gptr2)
1125 const char *str1 = gptr1;
1126 const char *str2 = gptr2;
1128 return !strcasecmp(str1, str2);
1131 static GHashTable *procmime_get_mime_type_table(void)
1133 GHashTable *table = NULL;
1135 MimeType *mime_type;
1138 if (!mime_type_list) {
1139 mime_type_list = procmime_get_mime_type_list();
1140 if (!mime_type_list) return NULL;
1143 table = g_hash_table_new(procmime_str_hash, procmime_str_equal);
1145 for (cur = mime_type_list; cur != NULL; cur = cur->next) {
1149 mime_type = (MimeType *)cur->data;
1151 if (!mime_type->extension) continue;
1153 exts = g_strsplit(mime_type->extension, " ", 16);
1154 for (i = 0; exts[i] != NULL; i++) {
1155 /* make the key case insensitive */
1157 /* use previously dup'd key on overwriting */
1158 if (g_hash_table_lookup(table, exts[i]))
1161 key = g_strdup(exts[i]);
1162 g_hash_table_insert(table, key, mime_type);
1170 GList *procmime_get_mime_type_list(void)
1174 gchar buf[BUFFSIZE];
1176 MimeType *mime_type;
1179 return mime_type_list;
1181 if ((fp = fopen("/etc/mime.types", "rb")) == NULL) {
1182 if ((fp = fopen(SYSCONFDIR "/mime.types", "rb")) == NULL) {
1183 FILE_OP_ERROR(SYSCONFDIR "/mime.types", "fopen");
1188 while (fgets(buf, sizeof(buf), fp) != NULL) {
1189 p = strchr(buf, '#');
1194 while (*p && !isspace(*p)) p++;
1199 delim = strchr(buf, '/');
1200 if (delim == NULL) continue;
1203 mime_type = g_new(MimeType, 1);
1204 mime_type->type = g_strdup(buf);
1205 mime_type->sub_type = g_strdup(delim + 1);
1207 while (*p && isspace(*p)) p++;
1209 mime_type->extension = g_strdup(p);
1211 mime_type->extension = NULL;
1213 list = g_list_append(list, mime_type);
1219 g_warning("Can't read mime.types\n");
1224 EncodingType procmime_get_encoding_for_charset(const gchar *charset)
1228 else if (!strncasecmp(charset, "ISO-2022-", 9) ||
1229 !strcasecmp(charset, "US-ASCII"))
1231 else if (!strcasecmp(charset, "ISO-8859-5") ||
1232 !strncasecmp(charset, "KOI8-", 5) ||
1233 !strcasecmp(charset, "Windows-1251"))
1235 else if (!strncasecmp(charset, "ISO-8859-", 9))
1236 return ENC_QUOTED_PRINTABLE;
1241 EncodingType procmime_get_encoding_for_file(const gchar *file)
1247 if ((fp = fopen(file, "rb")) == NULL) {
1248 FILE_OP_ERROR(file, "fopen");
1252 while ((len = fread(buf, sizeof(gchar), sizeof(buf), fp)) > 0) {
1256 for (p = buf, i = 0; i < len; p++, i++) {
1268 struct EncodingTable
1271 EncodingType enc_type;
1274 struct EncodingTable encoding_table[] = {
1277 {"binary", ENC_BINARY},
1278 {"quoted-printable", ENC_QUOTED_PRINTABLE},
1279 {"base64", ENC_BASE64},
1280 {"x-uuencode", ENC_UNKNOWN},
1281 {NULL, ENC_UNKNOWN},
1284 const gchar *procmime_get_encoding_str(EncodingType encoding)
1286 struct EncodingTable *enc_table;
1288 for (enc_table = encoding_table; enc_table->str != NULL; enc_table++) {
1289 if (enc_table->enc_type == encoding)
1290 return enc_table->str;