2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2001 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(MimeInfo *mimeinfo)
65 if (!mimeinfo) return;
67 g_free(mimeinfo->encoding);
68 g_free(mimeinfo->content_type);
69 g_free(mimeinfo->charset);
70 g_free(mimeinfo->name);
71 g_free(mimeinfo->boundary);
72 g_free(mimeinfo->content_disposition);
73 g_free(mimeinfo->filename);
75 g_free(mimeinfo->plaintextfile);
76 g_free(mimeinfo->sigstatus);
77 g_free(mimeinfo->sigstatus_full);
80 procmime_mimeinfo_free(mimeinfo->sub);
85 void procmime_mimeinfo_free_all(MimeInfo *mimeinfo)
87 while (mimeinfo != NULL) {
90 g_free(mimeinfo->encoding);
91 g_free(mimeinfo->content_type);
92 g_free(mimeinfo->charset);
93 g_free(mimeinfo->name);
94 g_free(mimeinfo->boundary);
95 g_free(mimeinfo->content_disposition);
96 g_free(mimeinfo->filename);
98 g_free(mimeinfo->plaintextfile);
99 g_free(mimeinfo->sigstatus);
100 g_free(mimeinfo->sigstatus_full);
103 procmime_mimeinfo_free_all(mimeinfo->sub);
104 procmime_mimeinfo_free_all(mimeinfo->children);
106 procmime_mimeinfo_free_all(mimeinfo->plaintext);
109 next = mimeinfo->next;
115 MimeInfo *procmime_mimeinfo_insert(MimeInfo *parent, MimeInfo *mimeinfo)
117 MimeInfo *child = parent->children;
120 parent->children = mimeinfo;
122 while (child->next != NULL)
125 child->next = mimeinfo;
128 mimeinfo->parent = parent;
129 mimeinfo->level = parent->level + 1;
134 void procmime_mimeinfo_replace(MimeInfo *old, MimeInfo *new)
136 MimeInfo *parent = old->parent;
139 g_return_if_fail(parent != NULL);
140 g_return_if_fail(new->next == NULL);
142 for (child = parent->children; child && child != old;
146 g_warning("oops: parent can't find it's own child");
149 procmime_mimeinfo_free_all(old);
151 if (child == parent->children) {
152 new->next = parent->children->next;
153 parent->children = new;
155 new->next = child->next;
160 MimeInfo *procmime_mimeinfo_next(MimeInfo *mimeinfo)
162 if (!mimeinfo) return NULL;
164 if (mimeinfo->children)
165 return mimeinfo->children;
167 return mimeinfo->sub;
169 return mimeinfo->next;
171 for (mimeinfo = mimeinfo->parent; mimeinfo != NULL;
172 mimeinfo = mimeinfo->parent) {
174 return mimeinfo->next;
175 if (mimeinfo->main) {
176 mimeinfo = mimeinfo->main;
178 return mimeinfo->next;
185 MimeInfo *procmime_scan_message(MsgInfo *msginfo)
190 g_return_val_if_fail(msginfo != NULL, NULL);
192 if ((fp = procmsg_open_message(msginfo)) == NULL) return NULL;
193 mimeinfo = procmime_scan_mime_header(fp, MIME_TEXT);
196 if (mimeinfo->mime_type != MIME_MULTIPART) {
197 if (fseek(fp, mimeinfo->fpos, SEEK_SET) < 0)
200 if (mimeinfo->mime_type != MIME_TEXT)
201 procmime_scan_multipart_message(mimeinfo, fp);
205 if (prefs_common.auto_check_signatures)
206 rfc2015_check_signature(mimeinfo, fp);
213 void procmime_scan_multipart_message(MimeInfo *mimeinfo, FILE *fp)
217 gint boundary_len = 0;
219 glong fpos, prev_fpos;
221 ContentType default_type;
223 g_return_if_fail(mimeinfo != NULL);
224 g_return_if_fail(mimeinfo->mime_type != MIME_TEXT);
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;
243 if ((fpos = ftell(fp)) < 0) {
248 /* Sets default subpart type for multipart/digest */
249 if (!strncasecmp(mimeinfo->content_type, "multipart/digest", 16))
250 default_type = MIME_MESSAGE_RFC822;
252 default_type = MIME_TEXT;
254 for (npart = 0;; npart++) {
256 gboolean eom = FALSE;
260 partinfo = procmime_scan_mime_header(fp, default_type);
261 if (!partinfo) break;
262 procmime_mimeinfo_insert(mimeinfo, partinfo);
264 if (partinfo->mime_type == MIME_MULTIPART) {
265 if (partinfo->level < 8)
266 procmime_scan_multipart_message(partinfo, fp);
267 } else if (partinfo->mime_type == MIME_MESSAGE_RFC822) {
270 partinfo->sub = sub = procmime_scan_mime_header
273 sub->level = partinfo->level + 1;
274 sub->parent = partinfo;
275 sub->main = partinfo;
277 if (sub->mime_type == MIME_MULTIPART) {
279 procmime_scan_multipart_message
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 eom = TRUE; /* broken MIME message */
298 partinfo->size = fpos - prev_fpos - strlen(buf);
304 void procmime_scan_encoding(MimeInfo *mimeinfo, const gchar *encoding)
308 Xstrdup_a(buf, encoding, return);
310 g_free(mimeinfo->encoding);
312 mimeinfo->encoding = g_strdup(g_strstrip(buf));
313 if (!strcasecmp(buf, "7bit"))
314 mimeinfo->encoding_type = ENC_7BIT;
315 else if (!strcasecmp(buf, "8bit"))
316 mimeinfo->encoding_type = ENC_8BIT;
317 else if (!strcasecmp(buf, "quoted-printable"))
318 mimeinfo->encoding_type = ENC_QUOTED_PRINTABLE;
319 else if (!strcasecmp(buf, "base64"))
320 mimeinfo->encoding_type = ENC_BASE64;
321 else if (!strcasecmp(buf, "x-uuencode"))
322 mimeinfo->encoding_type = ENC_X_UUENCODE;
324 mimeinfo->encoding_type = ENC_UNKNOWN;
328 void procmime_scan_content_type(MimeInfo *mimeinfo, const gchar *content_type)
330 gchar *delim, *p, *cnttype;
333 if (conv_get_current_charset() == C_EUC_JP &&
334 strchr(content_type, '\033')) {
336 len = strlen(content_type) * 2 + 1;
337 Xalloca(buf, len, return);
338 conv_jistoeuc(buf, len, content_type);
340 Xstrdup_a(buf, content_type, return);
342 g_free(mimeinfo->content_type);
343 g_free(mimeinfo->charset);
344 /* g_free(mimeinfo->name); */
345 mimeinfo->content_type = NULL;
346 mimeinfo->charset = NULL;
347 /* mimeinfo->name = NULL; */
349 if ((delim = strchr(buf, ';'))) *delim = '\0';
350 mimeinfo->content_type = cnttype = g_strdup(g_strstrip(buf));
352 mimeinfo->mime_type = procmime_scan_mime_type(cnttype);
361 if ((delim = strchr(p, ';'))) *delim = '\0';
363 if (!(eq = strchr(p, '='))) break;
372 extract_quote(value, '"');
374 eliminate_parenthesis(value, '(', ')');
379 if (!strcasecmp(attr, "charset"))
380 mimeinfo->charset = g_strdup(value);
381 else if (!strcasecmp(attr, "name")) {
385 len = strlen(value) + 1;
386 Xalloca(tmp, len, return);
387 conv_unmime_header(tmp, len, value, NULL);
388 g_free(mimeinfo->name);
389 mimeinfo->name = g_strdup(tmp);
390 } else if (!strcasecmp(attr, "boundary"))
391 mimeinfo->boundary = g_strdup(value);
398 if (mimeinfo->mime_type == MIME_MULTIPART && !mimeinfo->boundary)
399 mimeinfo->mime_type = MIME_TEXT;
402 void procmime_scan_content_disposition(MimeInfo *mimeinfo,
403 const gchar *content_disposition)
405 gchar *delim, *p, *dispos;
408 if (conv_get_current_charset() == C_EUC_JP &&
409 strchr(content_disposition, '\033')) {
411 len = strlen(content_disposition) * 2 + 1;
412 Xalloca(buf, len, return);
413 conv_jistoeuc(buf, len, content_disposition);
415 Xstrdup_a(buf, content_disposition, return);
417 if ((delim = strchr(buf, ';'))) *delim = '\0';
418 mimeinfo->content_disposition = dispos = g_strdup(g_strstrip(buf));
427 if ((delim = strchr(p, ';'))) *delim = '\0';
429 if (!(eq = strchr(p, '='))) break;
438 extract_quote(value, '"');
440 eliminate_parenthesis(value, '(', ')');
445 if (!strcasecmp(attr, "filename")) {
449 len = strlen(value) + 1;
450 Xalloca(tmp, len, return);
451 conv_unmime_header(tmp, len, value, NULL);
452 g_free(mimeinfo->filename);
453 mimeinfo->filename = g_strdup(tmp);
463 void procmime_scan_content_description(MimeInfo *mimeinfo,
464 const gchar *content_description)
466 gchar *delim, *p, *dispos;
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 = g_strdup(tmp);
488 void procmime_scan_subject(MimeInfo *mimeinfo,
489 const gchar *subject)
491 gchar *delim, *p, *dispos;
497 if (conv_get_current_charset() == C_EUC_JP &&
498 strchr(subject, '\033')) {
500 len = strlen(subject) * 2 + 1;
501 Xalloca(buf, len, return);
502 conv_jistoeuc(buf, len, subject);
504 Xstrdup_a(buf, subject, return);
506 blen = strlen(buf) + 1;
507 Xalloca(tmp, blen, return);
508 conv_unmime_header(tmp, blen, buf, NULL);
509 g_free(mimeinfo->name);
510 mimeinfo->name = g_strdup(tmp);
515 H_CONTENT_TRANSFER_ENCODING = 0,
517 H_CONTENT_DISPOSITION = 2,
518 H_CONTENT_DESCRIPTION = 3,
522 MimeInfo *procmime_scan_mime_header(FILE *fp, ContentType default_type)
524 static HeaderEntry hentry[] = {{"Content-Transfer-Encoding:",
526 {"Content-Type:", NULL, TRUE},
527 {"Content-Disposition:",
529 {"Content-description:",
533 {NULL, NULL, FALSE}};
539 g_return_val_if_fail(fp != NULL, NULL);
541 mimeinfo = procmime_mimeinfo_new();
542 /* Needed for multipart/digest */
543 mimeinfo->mime_type = default_type;
544 mimeinfo->encoding_type = ENC_7BIT;
545 mimeinfo->fpos = ftell(fp);
547 while ((hnum = procheader_get_one_field(buf, sizeof(buf), fp, hentry))
551 if (H_CONTENT_TRANSFER_ENCODING == hnum) {
552 procmime_scan_encoding
553 (mimeinfo, buf + strlen(hp->name));
554 } else if (H_CONTENT_TYPE == hnum) {
555 procmime_scan_content_type
556 (mimeinfo, buf + strlen(hp->name));
557 } else if (H_CONTENT_DISPOSITION == hnum) {
558 procmime_scan_content_disposition
559 (mimeinfo, buf + strlen(hp->name));
560 } else if (H_CONTENT_DESCRIPTION == hnum) {
561 procmime_scan_content_description
562 (mimeinfo, buf + strlen(hp->name));
563 } else if (H_SUBJECT == hnum) {
564 procmime_scan_subject
565 (mimeinfo, buf + strlen(hp->name));
569 if (mimeinfo->mime_type == MIME_APPLICATION_OCTET_STREAM &&
572 type = procmime_get_mime_type(mimeinfo->name);
574 mimeinfo->mime_type = procmime_scan_mime_type(type);
577 if (!mimeinfo->content_type) {
578 if (mimeinfo->mime_type == MIME_MESSAGE_RFC822) {
579 mimeinfo->content_type = g_strdup("message/rfc822");
581 mimeinfo->content_type = g_strdup("text/plain");
588 FILE *procmime_decode_content(FILE *outfp, FILE *infp, MimeInfo *mimeinfo)
591 gchar *boundary = NULL;
592 gint boundary_len = 0;
593 gboolean tmp_file = FALSE;
595 g_return_val_if_fail(infp != NULL, NULL);
596 g_return_val_if_fail(mimeinfo != NULL, NULL);
599 outfp = my_tmpfile();
607 if (mimeinfo->parent && mimeinfo->parent->boundary) {
608 boundary = mimeinfo->parent->boundary;
609 boundary_len = strlen(boundary);
612 if (mimeinfo->encoding_type == ENC_QUOTED_PRINTABLE) {
613 gboolean softline = FALSE;
615 while (fgets(buf, sizeof(buf), infp) != NULL &&
617 !IS_BOUNDARY(buf, boundary, boundary_len))) {
620 softline = DoOneQPLine(&p, FALSE, softline);
621 fwrite(buf, p - (guchar *)buf, 1, outfp);
623 } else if (mimeinfo->encoding_type == ENC_BASE64) {
624 gchar outbuf[BUFFSIZE];
626 Base64Decoder *decoder;
628 decoder = base64_decoder_new();
629 while (fgets(buf, sizeof(buf), infp) != NULL &&
631 !IS_BOUNDARY(buf, boundary, boundary_len))) {
632 len = base64_decoder_decode(decoder, buf, outbuf);
634 g_warning("Bad BASE64 content\n");
637 fwrite(outbuf, sizeof(gchar), len, outfp);
639 base64_decoder_free(decoder);
640 } else if (mimeinfo->encoding_type == ENC_X_UUENCODE) {
641 gchar outbuf[BUFFSIZE];
643 gboolean flag = FALSE;
645 while (fgets(buf, sizeof(buf), infp) != NULL &&
647 !IS_BOUNDARY(buf, boundary, boundary_len))) {
648 if(!flag && strncmp(buf,"begin ", 6)) continue;
651 len = fromuutobits(outbuf, buf);
654 g_warning("Bad UUENCODE content(%d)\n", len);
657 fwrite(outbuf, sizeof(gchar), len, outfp);
662 while (fgets(buf, sizeof(buf), infp) != NULL &&
664 !IS_BOUNDARY(buf, boundary, boundary_len))) {
669 if (tmp_file) rewind(outfp);
673 gint procmime_get_part(const gchar *outfile, const gchar *infile,
679 g_return_val_if_fail(outfile != NULL, -1);
680 g_return_val_if_fail(infile != NULL, -1);
681 g_return_val_if_fail(mimeinfo != NULL, -1);
683 if ((infp = fopen(infile, "r")) == NULL) {
684 FILE_OP_ERROR(infile, "fopen");
687 if (fseek(infp, mimeinfo->fpos, SEEK_SET) < 0) {
688 FILE_OP_ERROR(infile, "fseek");
692 if ((outfp = fopen(outfile, "w")) == NULL) {
693 FILE_OP_ERROR(outfile, "fopen");
698 while (fgets(buf, sizeof(buf), infp) != NULL)
699 if (buf[0] == '\r' || buf[0] == '\n') break;
701 procmime_decode_content(outfp, infp, mimeinfo);
704 if (fclose(outfp) == EOF) {
705 FILE_OP_ERROR(outfile, "fclose");
713 struct ContentRenderer {
718 static GList * renderer_list = NULL;
720 static struct ContentRenderer *
721 content_renderer_new(char * content_type, char * renderer)
723 struct ContentRenderer * cr;
725 cr = g_new(struct ContentRenderer, 1);
729 cr->content_type = g_strdup(content_type);
730 cr->renderer = g_strdup(renderer);
735 static void content_renderer_free(struct ContentRenderer * cr)
737 g_free(cr->content_type);
738 g_free(cr->renderer);
742 void renderer_read_config(void)
748 g_list_foreach(renderer_list, (GFunc) content_renderer_free, NULL);
749 renderer_list = NULL;
751 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, RENDERER_RC, NULL);
752 f = fopen(rcpath, "r");
758 while (fgets(buf, BUFFSIZE, f)) {
760 struct ContentRenderer * cr;
763 p = strchr(buf, ' ');
768 cr = content_renderer_new(buf, p + 1);
772 printf("%s %s\n", cr->content_type, cr->renderer);
774 renderer_list = g_list_append(renderer_list, cr);
780 void renderer_write_config(void)
787 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, RENDERER_RC, NULL);
789 if ((pfile = prefs_write_open(rcpath)) == NULL) {
790 g_warning(_("failed to write configuration to file\n"));
797 for(cur = renderer_list ; cur != NULL ; cur = cur->next) {
798 struct ContentRenderer * renderer;
799 renderer = cur->data;
800 fprintf(pfile->fp, "%s %s\n", renderer->content_type,
804 if (prefs_write_close(pfile) < 0) {
805 g_warning(_("failed to write configuration to file\n"));
810 FILE *procmime_get_text_content(MimeInfo *mimeinfo, FILE *infp)
814 gboolean conv_fail = FALSE;
818 struct ContentRenderer * renderer;
821 g_return_val_if_fail(mimeinfo != NULL, NULL);
822 g_return_val_if_fail(infp != NULL, NULL);
823 g_return_val_if_fail(mimeinfo->mime_type == MIME_TEXT ||
824 mimeinfo->mime_type == MIME_TEXT_HTML ||
825 mimeinfo->mime_type == MIME_TEXT_ENRICHED, NULL);
827 if (fseek(infp, mimeinfo->fpos, SEEK_SET) < 0) {
832 while (fgets(buf, sizeof(buf), infp) != NULL)
833 if (buf[0] == '\r' || buf[0] == '\n') break;
835 tmpfp = procmime_decode_content(NULL, infp, mimeinfo);
839 if ((outfp = my_tmpfile()) == NULL) {
845 src_codeset = prefs_common.force_charset
846 ? prefs_common.force_charset : mimeinfo->charset;
850 for(cur = renderer_list ; cur != NULL ; cur = cur->next) {
851 struct ContentRenderer * cr;
853 if (g_strcasecmp(cr->content_type,
854 mimeinfo->content_type) == 0) {
860 if (renderer != NULL) {
866 dup2(fileno(outfp), 1);
868 p = popen(renderer->renderer, "w");
873 fread(buf, sizeof(char), sizeof(buf),
875 fwrite(buf, sizeof(char), count, p);
880 } else if (mimeinfo->mime_type == MIME_TEXT) {
881 while (fgets(buf, sizeof(buf), tmpfp) != NULL) {
882 str = conv_codeset_strdup(buf, src_codeset, NULL);
891 } else if (mimeinfo->mime_type == MIME_TEXT_HTML) {
895 conv = conv_code_converter_new(src_codeset);
896 parser = html_parser_new(tmpfp, conv);
897 while ((str = html_parse(parser)) != NULL) {
900 html_parser_destroy(parser);
901 conv_code_converter_destroy(conv);
902 } else if (mimeinfo->mime_type == MIME_TEXT_ENRICHED) {
906 conv = conv_code_converter_new(src_codeset);
907 parser = ertf_parser_new(tmpfp, conv);
908 while ((str = ertf_parse(parser)) != NULL) {
911 ertf_parser_destroy(parser);
912 conv_code_converter_destroy(conv);
916 g_warning(_("procmime_get_text_content(): Code conversion failed.\n"));
924 /* search the first text part of (multipart) MIME message,
925 decode, convert it and output to outfp. */
926 FILE *procmime_get_first_text_content(MsgInfo *msginfo)
928 FILE *infp, *outfp = NULL;
929 MimeInfo *mimeinfo, *partinfo;
931 g_return_val_if_fail(msginfo != NULL, NULL);
933 mimeinfo = procmime_scan_message(msginfo);
934 if (!mimeinfo) return NULL;
936 if ((infp = procmsg_open_message(msginfo)) == NULL) {
937 procmime_mimeinfo_free_all(mimeinfo);
942 while (partinfo && partinfo->mime_type != MIME_TEXT)
943 partinfo = procmime_mimeinfo_next(partinfo);
946 while (partinfo && partinfo->mime_type != MIME_TEXT_HTML &&
947 partinfo->mime_type != MIME_TEXT_ENRICHED)
948 partinfo = procmime_mimeinfo_next(partinfo);
953 outfp = procmime_get_text_content(partinfo, infp);
956 procmime_mimeinfo_free_all(mimeinfo);
961 gboolean procmime_find_string_part(MimeInfo *mimeinfo, const gchar *filename,
962 const gchar *str, gboolean case_sens)
967 gchar *(* StrFindFunc) (const gchar *haystack, const gchar *needle);
969 g_return_val_if_fail(mimeinfo != NULL, FALSE);
970 g_return_val_if_fail(mimeinfo->mime_type == MIME_TEXT ||
971 mimeinfo->mime_type == MIME_TEXT_HTML ||
972 mimeinfo->mime_type == MIME_TEXT_ENRICHED, FALSE);
973 g_return_val_if_fail(str != NULL, FALSE);
975 if ((infp = fopen(filename, "r")) == NULL) {
976 FILE_OP_ERROR(filename, "fopen");
980 outfp = procmime_get_text_content(mimeinfo, infp);
987 StrFindFunc = strstr;
989 StrFindFunc = strcasestr;
991 while (fgets(buf, sizeof(buf), outfp) != NULL) {
992 if (StrFindFunc(buf, str) != NULL) {
1003 gboolean procmime_find_string(MsgInfo *msginfo, const gchar *str,
1009 gboolean found = FALSE;
1011 g_return_val_if_fail(msginfo != NULL, FALSE);
1012 g_return_val_if_fail(str != NULL, FALSE);
1014 filename = procmsg_get_message_file(msginfo);
1015 if (!filename) return FALSE;
1016 mimeinfo = procmime_scan_message(msginfo);
1018 for (partinfo = mimeinfo; partinfo != NULL;
1019 partinfo = procmime_mimeinfo_next(partinfo)) {
1020 if (partinfo->mime_type == MIME_TEXT ||
1021 partinfo->mime_type == MIME_TEXT_HTML ||
1022 partinfo->mime_type == MIME_TEXT_ENRICHED) {
1023 if (procmime_find_string_part
1024 (partinfo, filename, str, case_sens) == TRUE) {
1031 procmime_mimeinfo_free_all(mimeinfo);
1037 gchar *procmime_get_tmp_file_name(MimeInfo *mimeinfo)
1039 static guint32 id = 0;
1044 g_return_val_if_fail(mimeinfo != NULL, NULL);
1046 g_snprintf(f_prefix, sizeof(f_prefix), "%08x.", id++);
1048 if (MIME_TEXT_HTML == mimeinfo->mime_type)
1049 base = "mimetmp.html";
1051 base = mimeinfo->filename ? mimeinfo->filename
1052 : mimeinfo->name ? mimeinfo->name : "mimetmp";
1053 base = g_basename(base);
1054 if (*base == '\0') base = "mimetmp";
1057 filename = g_strconcat(get_mime_tmp_dir(), G_DIR_SEPARATOR_S,
1058 f_prefix, base, NULL);
1063 ContentType procmime_scan_mime_type(const gchar *mime_type)
1067 if (!strncasecmp(mime_type, "text/html", 9))
1068 type = MIME_TEXT_HTML;
1069 else if (!strncasecmp(mime_type, "text/enriched", 13))
1070 type = MIME_TEXT_ENRICHED;
1071 else if (!strncasecmp(mime_type, "text/", 5))
1073 else if (!strncasecmp(mime_type, "message/rfc822", 14))
1074 type = MIME_MESSAGE_RFC822;
1075 else if (!strncasecmp(mime_type, "message/", 8))
1077 else if (!strncasecmp(mime_type, "application/octet-stream", 24))
1078 type = MIME_APPLICATION_OCTET_STREAM;
1079 else if (!strncasecmp(mime_type, "application/", 12))
1080 type = MIME_APPLICATION;
1081 else if (!strncasecmp(mime_type, "multipart/", 10))
1082 type = MIME_MULTIPART;
1083 else if (!strncasecmp(mime_type, "image/", 6))
1085 else if (!strncasecmp(mime_type, "audio/", 6))
1087 else if (!strcasecmp(mime_type, "text"))
1090 type = MIME_UNKNOWN;
1095 static GList *mime_type_list = NULL;
1097 gchar *procmime_get_mime_type(const gchar *filename)
1099 static GHashTable *mime_type_table = NULL;
1100 MimeType *mime_type;
1104 if (!mime_type_table) {
1105 mime_type_table = procmime_get_mime_type_table();
1106 if (!mime_type_table) return NULL;
1109 filename = g_basename(filename);
1110 p = strrchr(filename, '.');
1111 if (!p) return NULL;
1113 Xstrdup_a(ext, p + 1, return NULL);
1115 mime_type = g_hash_table_lookup(mime_type_table, ext);
1119 str = g_strconcat(mime_type->type, "/", mime_type->sub_type,
1127 static guint procmime_str_hash(gconstpointer gptr)
1129 guint hash_result = 0;
1132 for (str = gptr; str && *str; str++) {
1133 if (isupper(*str)) hash_result += (*str + ' ');
1134 else hash_result += *str;
1140 static gint procmime_str_equal(gconstpointer gptr1, gconstpointer gptr2)
1142 const char *str1 = gptr1;
1143 const char *str2 = gptr2;
1145 return !strcasecmp(str1, str2);
1148 static GHashTable *procmime_get_mime_type_table(void)
1150 GHashTable *table = NULL;
1152 MimeType *mime_type;
1155 if (!mime_type_list) {
1156 mime_type_list = procmime_get_mime_type_list();
1157 if (!mime_type_list) return NULL;
1160 table = g_hash_table_new(procmime_str_hash, procmime_str_equal);
1162 for (cur = mime_type_list; cur != NULL; cur = cur->next) {
1166 mime_type = (MimeType *)cur->data;
1168 if (!mime_type->extension) continue;
1170 exts = g_strsplit(mime_type->extension, " ", 16);
1171 for (i = 0; exts[i] != NULL; i++) {
1172 /* make the key case insensitive */
1174 /* use previously dup'd key on overwriting */
1175 if (g_hash_table_lookup(table, exts[i]))
1178 key = g_strdup(exts[i]);
1179 g_hash_table_insert(table, key, mime_type);
1187 GList *procmime_get_mime_type_list(void)
1191 gchar buf[BUFFSIZE];
1193 MimeType *mime_type;
1196 return mime_type_list;
1198 if ((fp = fopen("/etc/mime.types", "r")) == NULL) {
1199 if ((fp = fopen(SYSCONFDIR "/mime.types", "r")) == NULL) {
1200 FILE_OP_ERROR(SYSCONFDIR "/mime.types", "fopen");
1205 while (fgets(buf, sizeof(buf), fp) != NULL) {
1206 p = strchr(buf, '#');
1211 while (*p && !isspace(*p)) p++;
1216 delim = strchr(buf, '/');
1217 if (delim == NULL) continue;
1220 mime_type = g_new(MimeType, 1);
1221 mime_type->type = g_strdup(buf);
1222 mime_type->sub_type = g_strdup(delim + 1);
1224 while (*p && isspace(*p)) p++;
1226 mime_type->extension = g_strdup(p);
1228 mime_type->extension = NULL;
1230 list = g_list_append(list, mime_type);
1236 g_warning("Can't read mime.types\n");
1241 EncodingType procmime_get_encoding_for_charset(const gchar *charset)
1245 else if (!strncasecmp(charset, "ISO-2022-", 9) ||
1246 !strcasecmp(charset, "US-ASCII"))
1250 /* return ENC_BASE64; */
1251 /* return ENC_QUOTED_PRINTABLE; */
1254 const gchar *procmime_get_encoding_str(EncodingType encoding)
1256 static const gchar *encoding_str[] = {
1257 "7bit", "8bit", "quoted-printable", "base64", "x-uuencode",
1261 if (encoding >= ENC_7BIT && encoding <= ENC_UNKNOWN)
1262 return encoding_str[encoding];