2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2002 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"
42 #include "prefs_common.h"
50 static GHashTable *procmime_get_mime_type_table (void);
52 MimeInfo *procmime_mimeinfo_new(void)
56 mimeinfo = g_new0(MimeInfo, 1);
57 mimeinfo->mime_type = MIME_UNKNOWN;
58 mimeinfo->encoding_type = ENC_UNKNOWN;
63 void procmime_mimeinfo_free_all(MimeInfo *mimeinfo)
65 while (mimeinfo != NULL) {
68 g_free(mimeinfo->encoding);
69 g_free(mimeinfo->content_type);
70 g_free(mimeinfo->charset);
71 g_free(mimeinfo->name);
72 g_free(mimeinfo->boundary);
73 g_free(mimeinfo->content_disposition);
74 g_free(mimeinfo->filename);
76 g_free(mimeinfo->plaintextfile);
77 g_free(mimeinfo->sigstatus);
78 g_free(mimeinfo->sigstatus_full);
81 procmime_mimeinfo_free_all(mimeinfo->sub);
82 procmime_mimeinfo_free_all(mimeinfo->children);
84 procmime_mimeinfo_free_all(mimeinfo->plaintext);
87 next = mimeinfo->next;
93 MimeInfo *procmime_mimeinfo_insert(MimeInfo *parent, MimeInfo *mimeinfo)
95 MimeInfo *child = parent->children;
98 parent->children = mimeinfo;
100 while (child->next != NULL)
103 child->next = mimeinfo;
106 mimeinfo->parent = parent;
107 mimeinfo->level = parent->level + 1;
112 void procmime_mimeinfo_replace(MimeInfo *old, MimeInfo *new)
114 MimeInfo *parent = old->parent;
117 g_return_if_fail(parent != NULL);
118 g_return_if_fail(new->next == NULL);
120 for (child = parent->children; child && child != old;
124 g_warning("oops: parent can't find it's own child");
127 procmime_mimeinfo_free_all(old);
129 if (child == parent->children) {
130 new->next = parent->children->next;
131 parent->children = new;
133 new->next = child->next;
138 MimeInfo *procmime_mimeinfo_next(MimeInfo *mimeinfo)
140 if (!mimeinfo) return NULL;
142 if (mimeinfo->children)
143 return mimeinfo->children;
145 return mimeinfo->sub;
147 return mimeinfo->next;
149 if (mimeinfo->main) {
150 mimeinfo = mimeinfo->main;
152 return mimeinfo->next;
155 for (mimeinfo = mimeinfo->parent; mimeinfo != NULL;
156 mimeinfo = mimeinfo->parent) {
158 return mimeinfo->next;
159 if (mimeinfo->main) {
160 mimeinfo = mimeinfo->main;
162 return mimeinfo->next;
169 MimeInfo *procmime_scan_message(MsgInfo *msginfo)
174 g_return_val_if_fail(msginfo != NULL, NULL);
176 if ((fp = procmsg_open_message(msginfo)) == NULL) return NULL;
177 mimeinfo = procmime_scan_mime_header(fp);
180 if (mimeinfo->mime_type != MIME_MULTIPART) {
181 if (fseek(fp, mimeinfo->fpos, SEEK_SET) < 0)
184 if (mimeinfo->mime_type != MIME_TEXT)
185 procmime_scan_multipart_message(mimeinfo, fp);
189 if (prefs_common.auto_check_signatures)
190 rfc2015_check_signature(mimeinfo, fp);
197 void procmime_scan_multipart_message(MimeInfo *mimeinfo, FILE *fp)
201 gint boundary_len = 0;
203 glong fpos, prev_fpos;
206 g_return_if_fail(mimeinfo != NULL);
207 g_return_if_fail(mimeinfo->mime_type != MIME_TEXT);
209 if (mimeinfo->mime_type == MIME_MULTIPART) {
210 g_return_if_fail(mimeinfo->boundary != NULL);
211 g_return_if_fail(mimeinfo->sub == NULL);
213 g_return_if_fail(fp != NULL);
215 boundary = mimeinfo->boundary;
218 boundary_len = strlen(boundary);
220 /* look for first boundary */
221 while ((p = fgets(buf, sizeof(buf), fp)) != NULL)
222 if (IS_BOUNDARY(buf, boundary, boundary_len)) break;
226 if ((fpos = ftell(fp)) < 0) {
231 for (npart = 0;; npart++) {
233 gboolean eom = FALSE;
236 debug_print("prev_fpos: %ld\n", fpos);
238 partinfo = procmime_scan_mime_header(fp);
239 if (!partinfo) break;
240 procmime_mimeinfo_insert(mimeinfo, partinfo);
242 if (partinfo->mime_type == MIME_MULTIPART) {
243 if (partinfo->level < 8)
244 procmime_scan_multipart_message(partinfo, fp);
245 } else if (partinfo->mime_type == MIME_MESSAGE_RFC822) {
248 partinfo->sub = sub = procmime_scan_mime_header(fp);
251 sub->level = partinfo->level + 1;
252 sub->parent = partinfo;
253 sub->main = partinfo;
255 if (sub->level < 8) {
256 if (sub->mime_type == MIME_MULTIPART) {
257 procmime_scan_multipart_message
259 } else if (sub->mime_type == MIME_MESSAGE_RFC822) {
260 fseek(fp, sub->fpos, SEEK_SET);
261 procmime_scan_multipart_message
267 /* look for next boundary */
269 while ((p = fgets(buf, sizeof(buf), fp)) != NULL) {
270 if (IS_BOUNDARY(buf, boundary, boundary_len)) {
271 if (buf[2 + boundary_len] == '-' &&
272 buf[2 + boundary_len + 1] == '-')
278 /* broken MIME, or single part MIME message */
283 debug_print("fpos: %ld\n", fpos);
285 partinfo->size = fpos - prev_fpos - strlen(buf);
286 debug_print("partinfo->size: %d\n", partinfo->size);
287 if (partinfo->sub && !partinfo->sub->sub &&
288 !partinfo->sub->children) {
289 partinfo->sub->size = fpos - partinfo->sub->fpos - strlen(buf);
290 debug_print("partinfo->sub->size: %d\n",
291 partinfo->sub->size);
293 debug_print("boundary: %s\n", buf);
299 void procmime_scan_encoding(MimeInfo *mimeinfo, const gchar *encoding)
303 Xstrdup_a(buf, encoding, return);
305 g_free(mimeinfo->encoding);
307 mimeinfo->encoding = g_strdup(g_strstrip(buf));
308 if (!strcasecmp(buf, "7bit"))
309 mimeinfo->encoding_type = ENC_7BIT;
310 else if (!strcasecmp(buf, "8bit"))
311 mimeinfo->encoding_type = ENC_8BIT;
312 else if (!strcasecmp(buf, "quoted-printable"))
313 mimeinfo->encoding_type = ENC_QUOTED_PRINTABLE;
314 else if (!strcasecmp(buf, "base64"))
315 mimeinfo->encoding_type = ENC_BASE64;
316 else if (!strcasecmp(buf, "x-uuencode"))
317 mimeinfo->encoding_type = ENC_X_UUENCODE;
319 mimeinfo->encoding_type = ENC_UNKNOWN;
323 void procmime_scan_content_type(MimeInfo *mimeinfo, const gchar *content_type)
325 gchar *delim, *p, *cnttype;
328 if (conv_get_current_charset() == C_EUC_JP &&
329 strchr(content_type, '\033')) {
331 len = strlen(content_type) * 2 + 1;
332 Xalloca(buf, len, return);
333 conv_jistoeuc(buf, len, content_type);
335 Xstrdup_a(buf, content_type, return);
337 g_free(mimeinfo->content_type);
338 g_free(mimeinfo->charset);
339 /* g_free(mimeinfo->name); */
340 mimeinfo->content_type = NULL;
341 mimeinfo->charset = NULL;
342 /* mimeinfo->name = NULL; */
344 if ((delim = strchr(buf, ';'))) *delim = '\0';
345 mimeinfo->content_type = cnttype = g_strdup(g_strstrip(buf));
347 mimeinfo->mime_type = procmime_scan_mime_type(cnttype);
356 if ((delim = strchr(p, ';'))) *delim = '\0';
358 if (!(eq = strchr(p, '='))) break;
367 extract_quote(value, '"');
369 eliminate_parenthesis(value, '(', ')');
374 if (!strcasecmp(attr, "charset"))
375 mimeinfo->charset = g_strdup(value);
376 else if (!strcasecmp(attr, "name")) {
380 len = strlen(value) + 1;
381 Xalloca(tmp, len, return);
382 conv_unmime_header(tmp, len, value, NULL);
383 g_free(mimeinfo->name);
384 /*pgp signatures should NOT have a name */
385 if (mimeinfo->content_type
386 && strcasecmp(mimeinfo->content_type, "application/pgp-signature"))
387 mimeinfo->name = g_strdup(tmp);
388 } else if (!strcasecmp(attr, "boundary"))
389 mimeinfo->boundary = g_strdup(value);
396 if (mimeinfo->mime_type == MIME_MULTIPART && !mimeinfo->boundary)
397 mimeinfo->mime_type = MIME_TEXT;
400 void procmime_scan_content_disposition(MimeInfo *mimeinfo,
401 const gchar *content_disposition)
403 gchar *delim, *p, *dispos;
406 if (conv_get_current_charset() == C_EUC_JP &&
407 strchr(content_disposition, '\033')) {
409 len = strlen(content_disposition) * 2 + 1;
410 Xalloca(buf, len, return);
411 conv_jistoeuc(buf, len, content_disposition);
413 Xstrdup_a(buf, content_disposition, return);
415 if ((delim = strchr(buf, ';'))) *delim = '\0';
416 mimeinfo->content_disposition = dispos = g_strdup(g_strstrip(buf));
425 if ((delim = strchr(p, ';'))) *delim = '\0';
427 if (!(eq = strchr(p, '='))) break;
436 extract_quote(value, '"');
438 eliminate_parenthesis(value, '(', ')');
443 if (!strcasecmp(attr, "filename")) {
447 len = strlen(value) + 1;
448 Xalloca(tmp, len, return);
449 conv_unmime_header(tmp, len, value, NULL);
450 g_free(mimeinfo->filename);
451 /*pgp signatures should NOT have a name */
452 if (mimeinfo->content_type
453 && strcasecmp(mimeinfo->content_type, "application/pgp-signature"))
454 mimeinfo->filename = g_strdup(tmp);
464 void procmime_scan_content_description(MimeInfo *mimeinfo,
465 const gchar *content_description)
472 if (conv_get_current_charset() == C_EUC_JP &&
473 strchr(content_description, '\033')) {
475 len = strlen(content_description) * 2 + 1;
476 Xalloca(buf, len, return);
477 conv_jistoeuc(buf, len, content_description);
479 Xstrdup_a(buf, content_description, return);
481 blen = strlen(buf) + 1;
482 Xalloca(tmp, blen, return);
483 conv_unmime_header(tmp, blen, buf, NULL);
484 g_free(mimeinfo->name);
485 mimeinfo->name = NULL;
486 /*pgp signatures should NOT have a name */
487 if (mimeinfo->content_type
488 && strcasecmp(mimeinfo->content_type, "application/pgp-signature"))
489 mimeinfo->name = g_strdup(tmp);
492 void procmime_scan_subject(MimeInfo *mimeinfo,
493 const gchar *subject)
500 if (conv_get_current_charset() == C_EUC_JP &&
501 strchr(subject, '\033')) {
503 len = strlen(subject) * 2 + 1;
504 Xalloca(buf, len, return);
505 conv_jistoeuc(buf, len, subject);
507 Xstrdup_a(buf, subject, return);
509 blen = strlen(buf) + 1;
510 Xalloca(tmp, blen, return);
511 conv_unmime_header(tmp, blen, buf, NULL);
512 g_free(mimeinfo->name);
513 mimeinfo->name = g_strdup(tmp);
518 H_CONTENT_TRANSFER_ENCODING = 0,
520 H_CONTENT_DISPOSITION = 2,
521 H_CONTENT_DESCRIPTION = 3,
525 MimeInfo *procmime_scan_mime_header(FILE *fp)
527 static HeaderEntry hentry[] = {{"Content-Transfer-Encoding:",
529 {"Content-Type:", NULL, TRUE},
530 {"Content-Disposition:",
532 {"Content-description:",
536 {NULL, NULL, FALSE}};
542 g_return_val_if_fail(fp != NULL, NULL);
544 mimeinfo = procmime_mimeinfo_new();
545 mimeinfo->mime_type = MIME_TEXT;
546 mimeinfo->encoding_type = ENC_7BIT;
547 mimeinfo->fpos = ftell(fp);
549 while ((hnum = procheader_get_one_field(buf, sizeof(buf), fp, hentry))
553 if (H_CONTENT_TRANSFER_ENCODING == hnum) {
554 procmime_scan_encoding
555 (mimeinfo, buf + strlen(hp->name));
556 } else if (H_CONTENT_TYPE == hnum) {
557 procmime_scan_content_type
558 (mimeinfo, buf + strlen(hp->name));
559 } else if (H_CONTENT_DISPOSITION == hnum) {
560 procmime_scan_content_disposition
561 (mimeinfo, buf + strlen(hp->name));
562 } else if (H_CONTENT_DESCRIPTION == hnum) {
563 procmime_scan_content_description
564 (mimeinfo, buf + strlen(hp->name));
565 } else if (H_SUBJECT == hnum) {
566 procmime_scan_subject
567 (mimeinfo, buf + strlen(hp->name));
571 if (mimeinfo->mime_type == MIME_APPLICATION_OCTET_STREAM &&
574 type = procmime_get_mime_type(mimeinfo->name);
576 mimeinfo->mime_type = procmime_scan_mime_type(type);
579 if (!mimeinfo->content_type)
580 mimeinfo->content_type = g_strdup("text/plain");
585 FILE *procmime_decode_content(FILE *outfp, FILE *infp, MimeInfo *mimeinfo)
588 gchar *boundary = NULL;
589 gint boundary_len = 0;
590 gboolean tmp_file = FALSE;
592 g_return_val_if_fail(infp != NULL, NULL);
593 g_return_val_if_fail(mimeinfo != NULL, NULL);
596 outfp = my_tmpfile();
604 if (mimeinfo->parent && mimeinfo->parent->boundary) {
605 boundary = mimeinfo->parent->boundary;
606 boundary_len = strlen(boundary);
609 if (mimeinfo->encoding_type == ENC_QUOTED_PRINTABLE) {
610 while (fgets(buf, sizeof(buf), infp) != NULL &&
612 !IS_BOUNDARY(buf, boundary, boundary_len))) {
614 len = unmime_quoted_printable_line(buf);
615 fwrite(buf, len, 1, outfp);
617 } else if (mimeinfo->encoding_type == ENC_BASE64) {
618 gchar outbuf[BUFFSIZE];
620 Base64Decoder *decoder;
622 decoder = base64_decoder_new();
623 while (fgets(buf, sizeof(buf), infp) != NULL &&
625 !IS_BOUNDARY(buf, boundary, boundary_len))) {
626 len = base64_decoder_decode(decoder, buf, outbuf);
628 g_warning("Bad BASE64 content\n");
631 fwrite(outbuf, sizeof(gchar), len, outfp);
633 base64_decoder_free(decoder);
634 } else if (mimeinfo->encoding_type == ENC_X_UUENCODE) {
635 gchar outbuf[BUFFSIZE];
637 gboolean flag = FALSE;
639 while (fgets(buf, sizeof(buf), infp) != NULL &&
641 !IS_BOUNDARY(buf, boundary, boundary_len))) {
642 if(!flag && strncmp(buf,"begin ", 6)) continue;
645 len = fromuutobits(outbuf, buf);
648 g_warning("Bad UUENCODE content(%d)\n", len);
651 fwrite(outbuf, sizeof(gchar), len, outfp);
656 while (fgets(buf, sizeof(buf), infp) != NULL &&
658 !IS_BOUNDARY(buf, boundary, boundary_len))) {
663 if (tmp_file) rewind(outfp);
667 gint procmime_get_part(const gchar *outfile, const gchar *infile,
673 g_return_val_if_fail(outfile != NULL, -1);
674 g_return_val_if_fail(infile != NULL, -1);
675 g_return_val_if_fail(mimeinfo != NULL, -1);
677 if ((infp = fopen(infile, "rb")) == NULL) {
678 FILE_OP_ERROR(infile, "fopen");
681 if (fseek(infp, mimeinfo->fpos, SEEK_SET) < 0) {
682 FILE_OP_ERROR(infile, "fseek");
686 if ((outfp = fopen(outfile, "wb")) == NULL) {
687 FILE_OP_ERROR(outfile, "fopen");
692 while (fgets(buf, sizeof(buf), infp) != NULL)
693 if (buf[0] == '\r' || buf[0] == '\n') break;
695 procmime_decode_content(outfp, infp, mimeinfo);
698 if (fclose(outfp) == EOF) {
699 FILE_OP_ERROR(outfile, "fclose");
707 struct ContentRenderer {
712 static GList * renderer_list = NULL;
714 static struct ContentRenderer *
715 content_renderer_new(char * content_type, char * renderer)
717 struct ContentRenderer * cr;
719 cr = g_new(struct ContentRenderer, 1);
723 cr->content_type = g_strdup(content_type);
724 cr->renderer = g_strdup(renderer);
729 static void content_renderer_free(struct ContentRenderer * cr)
731 g_free(cr->content_type);
732 g_free(cr->renderer);
736 void renderer_read_config(void)
742 g_list_foreach(renderer_list, (GFunc) content_renderer_free, NULL);
743 renderer_list = NULL;
745 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, RENDERER_RC, NULL);
746 f = fopen(rcpath, "rb");
752 while (fgets(buf, BUFFSIZE, f)) {
754 struct ContentRenderer * cr;
757 p = strchr(buf, ' ');
762 cr = content_renderer_new(buf, p + 1);
766 renderer_list = g_list_append(renderer_list, cr);
772 void renderer_write_config(void)
778 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, RENDERER_RC, NULL);
780 if ((pfile = prefs_write_open(rcpath)) == NULL) {
781 g_warning(_("failed to write configuration to file\n"));
788 for(cur = renderer_list ; cur != NULL ; cur = cur->next) {
789 struct ContentRenderer * renderer;
790 renderer = cur->data;
791 fprintf(pfile->fp, "%s %s\n", renderer->content_type,
795 if (prefs_write_close(pfile) < 0) {
796 g_warning(_("failed to write configuration to file\n"));
801 FILE *procmime_get_text_content(MimeInfo *mimeinfo, FILE *infp)
805 gboolean conv_fail = FALSE;
808 struct ContentRenderer * renderer;
811 g_return_val_if_fail(mimeinfo != NULL, NULL);
812 g_return_val_if_fail(infp != NULL, NULL);
813 g_return_val_if_fail(mimeinfo->mime_type == MIME_TEXT ||
814 mimeinfo->mime_type == MIME_TEXT_HTML ||
815 mimeinfo->mime_type == MIME_TEXT_ENRICHED, NULL);
817 if (fseek(infp, mimeinfo->fpos, SEEK_SET) < 0) {
822 while (fgets(buf, sizeof(buf), infp) != NULL)
823 if (buf[0] == '\r' || buf[0] == '\n') break;
825 tmpfp = procmime_decode_content(NULL, infp, mimeinfo);
829 if ((outfp = my_tmpfile()) == NULL) {
835 src_codeset = prefs_common.force_charset
836 ? prefs_common.force_charset : mimeinfo->charset;
840 for(cur = renderer_list ; cur != NULL ; cur = cur->next) {
841 struct ContentRenderer * cr;
843 if (g_strcasecmp(cr->content_type,
844 mimeinfo->content_type) == 0) {
850 if (renderer != NULL) {
856 dup2(fileno(outfp), 1);
858 p = popen(renderer->renderer, "w");
863 fread(buf, sizeof(char), sizeof(buf),
865 fwrite(buf, sizeof(char), count, p);
870 } else if (mimeinfo->mime_type == MIME_TEXT) {
871 while (fgets(buf, sizeof(buf), tmpfp) != NULL) {
872 str = conv_codeset_strdup(buf, src_codeset, NULL);
881 } else if (mimeinfo->mime_type == MIME_TEXT_HTML) {
885 conv = conv_code_converter_new(src_codeset);
886 parser = html_parser_new(tmpfp, conv);
887 while ((str = html_parse(parser)) != NULL) {
890 html_parser_destroy(parser);
891 conv_code_converter_destroy(conv);
892 } else if (mimeinfo->mime_type == MIME_TEXT_ENRICHED) {
896 conv = conv_code_converter_new(src_codeset);
897 parser = ertf_parser_new(tmpfp, conv);
898 while ((str = ertf_parse(parser)) != NULL) {
901 ertf_parser_destroy(parser);
902 conv_code_converter_destroy(conv);
906 g_warning(_("procmime_get_text_content(): Code conversion failed.\n"));
914 /* search the first text part of (multipart) MIME message,
915 decode, convert it and output to outfp. */
916 FILE *procmime_get_first_text_content(MsgInfo *msginfo)
918 FILE *infp, *outfp = NULL;
919 MimeInfo *mimeinfo, *partinfo;
921 g_return_val_if_fail(msginfo != NULL, NULL);
923 mimeinfo = procmime_scan_message(msginfo);
924 if (!mimeinfo) return NULL;
926 if ((infp = procmsg_open_message(msginfo)) == NULL) {
927 procmime_mimeinfo_free_all(mimeinfo);
932 while (partinfo && partinfo->mime_type != MIME_TEXT)
933 partinfo = procmime_mimeinfo_next(partinfo);
936 while (partinfo && partinfo->mime_type != MIME_TEXT_HTML &&
937 partinfo->mime_type != MIME_TEXT_ENRICHED)
938 partinfo = procmime_mimeinfo_next(partinfo);
943 outfp = procmime_get_text_content(partinfo, infp);
946 procmime_mimeinfo_free_all(mimeinfo);
951 gboolean procmime_find_string_part(MimeInfo *mimeinfo, const gchar *filename,
952 const gchar *str, gboolean case_sens)
957 gchar *(* StrFindFunc) (const gchar *haystack, const gchar *needle);
959 g_return_val_if_fail(mimeinfo != NULL, FALSE);
960 g_return_val_if_fail(mimeinfo->mime_type == MIME_TEXT ||
961 mimeinfo->mime_type == MIME_TEXT_HTML ||
962 mimeinfo->mime_type == MIME_TEXT_ENRICHED, FALSE);
963 g_return_val_if_fail(str != NULL, FALSE);
965 if ((infp = fopen(filename, "rb")) == NULL) {
966 FILE_OP_ERROR(filename, "fopen");
970 outfp = procmime_get_text_content(mimeinfo, infp);
977 StrFindFunc = strstr;
979 StrFindFunc = strcasestr;
981 while (fgets(buf, sizeof(buf), outfp) != NULL) {
982 if (StrFindFunc(buf, str) != NULL) {
993 gboolean procmime_find_string(MsgInfo *msginfo, const gchar *str,
999 gboolean found = FALSE;
1001 g_return_val_if_fail(msginfo != NULL, FALSE);
1002 g_return_val_if_fail(str != NULL, FALSE);
1004 filename = procmsg_get_message_file(msginfo);
1005 if (!filename) return FALSE;
1006 mimeinfo = procmime_scan_message(msginfo);
1008 for (partinfo = mimeinfo; partinfo != NULL;
1009 partinfo = procmime_mimeinfo_next(partinfo)) {
1010 if (partinfo->mime_type == MIME_TEXT ||
1011 partinfo->mime_type == MIME_TEXT_HTML ||
1012 partinfo->mime_type == MIME_TEXT_ENRICHED) {
1013 if (procmime_find_string_part
1014 (partinfo, filename, str, case_sens) == TRUE) {
1021 procmime_mimeinfo_free_all(mimeinfo);
1027 gchar *procmime_get_tmp_file_name(MimeInfo *mimeinfo)
1029 static guint32 id = 0;
1034 g_return_val_if_fail(mimeinfo != NULL, NULL);
1036 g_snprintf(f_prefix, sizeof(f_prefix), "%08x.", id++);
1038 if (MIME_TEXT_HTML == mimeinfo->mime_type)
1039 base = "mimetmp.html";
1041 base = mimeinfo->filename ? mimeinfo->filename
1042 : mimeinfo->name ? mimeinfo->name : "mimetmp";
1043 base = g_basename(base);
1044 if (*base == '\0') base = "mimetmp";
1045 Xstrdup_a(base, base, return NULL);
1046 subst_for_filename(base);
1049 filename = g_strconcat(get_mime_tmp_dir(), G_DIR_SEPARATOR_S,
1050 f_prefix, base, NULL);
1055 ContentType procmime_scan_mime_type(const gchar *mime_type)
1059 if (!strncasecmp(mime_type, "text/html", 9))
1060 type = MIME_TEXT_HTML;
1061 else if (!strncasecmp(mime_type, "text/enriched", 13))
1062 type = MIME_TEXT_ENRICHED;
1063 else if (!strncasecmp(mime_type, "text/", 5))
1065 else if (!strncasecmp(mime_type, "message/rfc822", 14))
1066 type = MIME_MESSAGE_RFC822;
1067 else if (!strncasecmp(mime_type, "message/", 8))
1069 else if (!strncasecmp(mime_type, "application/octet-stream", 24))
1070 type = MIME_APPLICATION_OCTET_STREAM;
1071 else if (!strncasecmp(mime_type, "application/", 12))
1072 type = MIME_APPLICATION;
1073 else if (!strncasecmp(mime_type, "multipart/", 10))
1074 type = MIME_MULTIPART;
1075 else if (!strncasecmp(mime_type, "image/", 6))
1077 else if (!strncasecmp(mime_type, "audio/", 6))
1079 else if (!strcasecmp(mime_type, "text"))
1082 type = MIME_UNKNOWN;
1087 static GList *mime_type_list = NULL;
1089 gchar *procmime_get_mime_type(const gchar *filename)
1091 static GHashTable *mime_type_table = NULL;
1092 MimeType *mime_type;
1096 if (!mime_type_table) {
1097 mime_type_table = procmime_get_mime_type_table();
1098 if (!mime_type_table) return NULL;
1101 filename = g_basename(filename);
1102 p = strrchr(filename, '.');
1103 if (!p) return NULL;
1105 Xstrdup_a(ext, p + 1, return NULL);
1107 mime_type = g_hash_table_lookup(mime_type_table, ext);
1111 str = g_strconcat(mime_type->type, "/", mime_type->sub_type,
1119 static guint procmime_str_hash(gconstpointer gptr)
1121 guint hash_result = 0;
1124 for (str = gptr; str && *str; str++) {
1125 if (isupper(*str)) hash_result += (*str + ' ');
1126 else hash_result += *str;
1132 static gint procmime_str_equal(gconstpointer gptr1, gconstpointer gptr2)
1134 const char *str1 = gptr1;
1135 const char *str2 = gptr2;
1137 return !strcasecmp(str1, str2);
1140 static GHashTable *procmime_get_mime_type_table(void)
1142 GHashTable *table = NULL;
1144 MimeType *mime_type;
1147 if (!mime_type_list) {
1148 mime_type_list = procmime_get_mime_type_list();
1149 if (!mime_type_list) return NULL;
1152 table = g_hash_table_new(procmime_str_hash, procmime_str_equal);
1154 for (cur = mime_type_list; cur != NULL; cur = cur->next) {
1158 mime_type = (MimeType *)cur->data;
1160 if (!mime_type->extension) continue;
1162 exts = g_strsplit(mime_type->extension, " ", 16);
1163 for (i = 0; exts[i] != NULL; i++) {
1164 /* make the key case insensitive */
1166 /* use previously dup'd key on overwriting */
1167 if (g_hash_table_lookup(table, exts[i]))
1170 key = g_strdup(exts[i]);
1171 g_hash_table_insert(table, key, mime_type);
1179 GList *procmime_get_mime_type_list(void)
1183 gchar buf[BUFFSIZE];
1185 MimeType *mime_type;
1188 return mime_type_list;
1190 if ((fp = fopen("/etc/mime.types", "rb")) == NULL) {
1191 if ((fp = fopen(SYSCONFDIR "/mime.types", "rb")) == NULL) {
1192 FILE_OP_ERROR(SYSCONFDIR "/mime.types", "fopen");
1197 while (fgets(buf, sizeof(buf), fp) != NULL) {
1198 p = strchr(buf, '#');
1203 while (*p && !isspace(*p)) p++;
1208 delim = strchr(buf, '/');
1209 if (delim == NULL) continue;
1212 mime_type = g_new(MimeType, 1);
1213 mime_type->type = g_strdup(buf);
1214 mime_type->sub_type = g_strdup(delim + 1);
1216 while (*p && isspace(*p)) p++;
1218 mime_type->extension = g_strdup(p);
1220 mime_type->extension = NULL;
1222 list = g_list_append(list, mime_type);
1228 g_warning("Can't read mime.types\n");
1233 EncodingType procmime_get_encoding_for_charset(const gchar *charset)
1237 else if (!strncasecmp(charset, "ISO-2022-", 9) ||
1238 !strcasecmp(charset, "US-ASCII"))
1242 /* return ENC_BASE64; */
1243 /* return ENC_QUOTED_PRINTABLE; */
1246 EncodingType procmime_get_encoding_for_file(const gchar *file)
1252 if ((fp = fopen(file, "rb")) == NULL) {
1253 FILE_OP_ERROR(file, "fopen");
1257 while ((len = fread(buf, sizeof(gchar), sizeof(buf), fp)) > 0) {
1261 for (p = buf, i = 0; i < len; p++, i++) {
1273 struct EncodingTable
1276 EncodingType enc_type;
1279 struct EncodingTable encoding_table[] = {
1282 {"binary", ENC_BINARY},
1283 {"quoted-printable", ENC_QUOTED_PRINTABLE},
1284 {"base64", ENC_BASE64},
1285 {"x-uuencode", ENC_UNKNOWN},
1286 {NULL, ENC_UNKNOWN},
1289 const gchar *procmime_get_encoding_str(EncodingType encoding)
1291 struct EncodingTable *enc_table;
1293 for (enc_table = encoding_table; enc_table->str != NULL; enc_table++) {
1294 if (enc_table->enc_type == encoding)
1295 return enc_table->str;