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);
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;
222 g_return_if_fail(mimeinfo != NULL);
223 g_return_if_fail(mimeinfo->mime_type != MIME_TEXT);
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;
242 if ((fpos = ftell(fp)) < 0) {
248 for (npart = 0;; npart++) {
250 gboolean eom = FALSE;
254 partinfo = procmime_scan_mime_header(fp);
255 if (!partinfo) break;
256 procmime_mimeinfo_insert(mimeinfo, partinfo);
258 if (partinfo->mime_type == MIME_MULTIPART) {
259 if (partinfo->level < 8)
260 procmime_scan_multipart_message(partinfo, fp);
261 } else if (partinfo->mime_type == MIME_MESSAGE_RFC822) {
264 partinfo->sub = sub = procmime_scan_mime_header(fp);
266 sub->level = partinfo->level + 1;
267 sub->parent = partinfo->parent;
268 sub->main = partinfo;
270 if (sub->mime_type == MIME_MULTIPART) {
272 procmime_scan_multipart_message
277 /* look for next boundary */
279 while ((p = fgets(buf, sizeof(buf), fp)) != NULL) {
280 if (IS_BOUNDARY(buf, boundary, boundary_len)) {
281 if (buf[2 + boundary_len] == '-' &&
282 buf[2 + boundary_len + 1] == '-')
288 eom = TRUE; /* broken MIME message */
291 partinfo->size = fpos - prev_fpos - strlen(buf);
297 void procmime_scan_encoding(MimeInfo *mimeinfo, const gchar *encoding)
301 Xstrdup_a(buf, encoding, return);
303 g_free(mimeinfo->encoding);
305 mimeinfo->encoding = g_strdup(g_strstrip(buf));
306 if (!strcasecmp(buf, "7bit"))
307 mimeinfo->encoding_type = ENC_7BIT;
308 else if (!strcasecmp(buf, "8bit"))
309 mimeinfo->encoding_type = ENC_8BIT;
310 else if (!strcasecmp(buf, "quoted-printable"))
311 mimeinfo->encoding_type = ENC_QUOTED_PRINTABLE;
312 else if (!strcasecmp(buf, "base64"))
313 mimeinfo->encoding_type = ENC_BASE64;
314 else if (!strcasecmp(buf, "x-uuencode"))
315 mimeinfo->encoding_type = ENC_X_UUENCODE;
317 mimeinfo->encoding_type = ENC_UNKNOWN;
321 void procmime_scan_content_type(MimeInfo *mimeinfo, const gchar *content_type)
323 gchar *delim, *p, *cnttype;
326 if (conv_get_current_charset() == C_EUC_JP &&
327 strchr(content_type, '\033')) {
329 len = strlen(content_type) * 2 + 1;
330 Xalloca(buf, len, return);
331 conv_jistoeuc(buf, len, content_type);
333 Xstrdup_a(buf, content_type, return);
335 g_free(mimeinfo->content_type);
336 g_free(mimeinfo->charset);
337 /* g_free(mimeinfo->name); */
338 mimeinfo->content_type = NULL;
339 mimeinfo->charset = NULL;
340 /* mimeinfo->name = NULL; */
342 if ((delim = strchr(buf, ';'))) *delim = '\0';
343 mimeinfo->content_type = cnttype = g_strdup(g_strstrip(buf));
345 mimeinfo->mime_type = procmime_scan_mime_type(cnttype);
354 if ((delim = strchr(p, ';'))) *delim = '\0';
356 if (!(eq = strchr(p, '='))) break;
365 extract_quote(value, '"');
367 eliminate_parenthesis(value, '(', ')');
372 if (!strcasecmp(attr, "charset"))
373 mimeinfo->charset = g_strdup(value);
374 else if (!strcasecmp(attr, "name")) {
378 len = strlen(value) + 1;
379 Xalloca(tmp, len, return);
380 conv_unmime_header(tmp, len, value, NULL);
381 g_free(mimeinfo->name);
382 mimeinfo->name = g_strdup(tmp);
383 } else if (!strcasecmp(attr, "boundary"))
384 mimeinfo->boundary = g_strdup(value);
391 if (mimeinfo->mime_type == MIME_MULTIPART && !mimeinfo->boundary)
392 mimeinfo->mime_type = MIME_TEXT;
395 void procmime_scan_content_disposition(MimeInfo *mimeinfo,
396 const gchar *content_disposition)
398 gchar *delim, *p, *dispos;
401 if (conv_get_current_charset() == C_EUC_JP &&
402 strchr(content_disposition, '\033')) {
404 len = strlen(content_disposition) * 2 + 1;
405 Xalloca(buf, len, return);
406 conv_jistoeuc(buf, len, content_disposition);
408 Xstrdup_a(buf, content_disposition, return);
410 if ((delim = strchr(buf, ';'))) *delim = '\0';
411 mimeinfo->content_disposition = dispos = g_strdup(g_strstrip(buf));
420 if ((delim = strchr(p, ';'))) *delim = '\0';
422 if (!(eq = strchr(p, '='))) break;
431 extract_quote(value, '"');
433 eliminate_parenthesis(value, '(', ')');
438 if (!strcasecmp(attr, "filename")) {
442 len = strlen(value) + 1;
443 Xalloca(tmp, len, return);
444 conv_unmime_header(tmp, len, value, NULL);
445 g_free(mimeinfo->filename);
446 mimeinfo->filename = g_strdup(tmp);
456 void procmime_scan_content_description(MimeInfo *mimeinfo,
457 const gchar *content_description)
459 gchar *delim, *p, *dispos;
465 if (conv_get_current_charset() == C_EUC_JP &&
466 strchr(content_description, '\033')) {
468 len = strlen(content_description) * 2 + 1;
469 Xalloca(buf, len, return);
470 conv_jistoeuc(buf, len, content_description);
472 Xstrdup_a(buf, content_description, return);
474 blen = strlen(buf) + 1;
475 Xalloca(tmp, blen, return);
476 conv_unmime_header(tmp, blen, buf, NULL);
477 g_free(mimeinfo->name);
478 mimeinfo->name = g_strdup(tmp);
481 void procmime_scan_subject(MimeInfo *mimeinfo,
482 const gchar *subject)
484 gchar *delim, *p, *dispos;
490 if (conv_get_current_charset() == C_EUC_JP &&
491 strchr(subject, '\033')) {
493 len = strlen(subject) * 2 + 1;
494 Xalloca(buf, len, return);
495 conv_jistoeuc(buf, len, subject);
497 Xstrdup_a(buf, subject, return);
499 blen = strlen(buf) + 1;
500 Xalloca(tmp, blen, return);
501 conv_unmime_header(tmp, blen, buf, NULL);
502 g_free(mimeinfo->name);
503 mimeinfo->name = g_strdup(tmp);
508 H_CONTENT_TRANSFER_ENCODING = 0,
510 H_CONTENT_DISPOSITION = 2,
511 H_CONTENT_DESCRIPTION = 3,
515 MimeInfo *procmime_scan_mime_header(FILE *fp)
517 static HeaderEntry hentry[] = {{"Content-Transfer-Encoding:",
519 {"Content-Type:", NULL, TRUE},
520 {"Content-Disposition:",
522 {"Content-description:",
526 {NULL, NULL, FALSE}};
532 g_return_val_if_fail(fp != NULL, NULL);
534 mimeinfo = procmime_mimeinfo_new();
535 mimeinfo->mime_type = MIME_TEXT;
536 mimeinfo->encoding_type = ENC_7BIT;
537 mimeinfo->fpos = ftell(fp);
539 while ((hnum = procheader_get_one_field(buf, sizeof(buf), fp, hentry))
543 if (H_CONTENT_TRANSFER_ENCODING == hnum) {
544 procmime_scan_encoding
545 (mimeinfo, buf + strlen(hp->name));
546 } else if (H_CONTENT_TYPE == hnum) {
547 procmime_scan_content_type
548 (mimeinfo, buf + strlen(hp->name));
549 } else if (H_CONTENT_DISPOSITION == hnum) {
550 procmime_scan_content_disposition
551 (mimeinfo, buf + strlen(hp->name));
552 } else if (H_CONTENT_DESCRIPTION == hnum) {
553 procmime_scan_content_description
554 (mimeinfo, buf + strlen(hp->name));
555 } else if (H_SUBJECT == hnum) {
556 procmime_scan_subject
557 (mimeinfo, buf + strlen(hp->name));
561 if (mimeinfo->mime_type == MIME_APPLICATION_OCTET_STREAM &&
564 type = procmime_get_mime_type(mimeinfo->name);
566 mimeinfo->mime_type = procmime_scan_mime_type(type);
569 if (!mimeinfo->content_type)
570 mimeinfo->content_type = g_strdup("text/plain");
575 FILE *procmime_decode_content(FILE *outfp, FILE *infp, MimeInfo *mimeinfo)
578 gchar *boundary = NULL;
579 gint boundary_len = 0;
580 gboolean tmp_file = FALSE;
582 g_return_val_if_fail(infp != NULL, NULL);
583 g_return_val_if_fail(mimeinfo != NULL, NULL);
586 outfp = my_tmpfile();
594 if (mimeinfo->parent && mimeinfo->parent->boundary) {
595 boundary = mimeinfo->parent->boundary;
596 boundary_len = strlen(boundary);
599 if (mimeinfo->encoding_type == ENC_QUOTED_PRINTABLE) {
600 gboolean softline = FALSE;
602 while (fgets(buf, sizeof(buf), infp) != NULL &&
604 !IS_BOUNDARY(buf, boundary, boundary_len))) {
607 softline = DoOneQPLine(&p, FALSE, softline);
608 fwrite(buf, p - (guchar *)buf, 1, outfp);
610 } else if (mimeinfo->encoding_type == ENC_BASE64) {
611 gchar outbuf[BUFFSIZE];
613 Base64Decoder *decoder;
615 decoder = base64_decoder_new();
616 while (fgets(buf, sizeof(buf), infp) != NULL &&
618 !IS_BOUNDARY(buf, boundary, boundary_len))) {
619 len = base64_decoder_decode(decoder, buf, outbuf);
621 g_warning("Bad BASE64 content\n");
624 fwrite(outbuf, sizeof(gchar), len, outfp);
626 base64_decoder_free(decoder);
627 } else if (mimeinfo->encoding_type == ENC_X_UUENCODE) {
628 gchar outbuf[BUFFSIZE];
630 gboolean flag = FALSE;
632 while (fgets(buf, sizeof(buf), infp) != NULL &&
634 !IS_BOUNDARY(buf, boundary, boundary_len))) {
635 if(!flag && strncmp(buf,"begin ", 6)) continue;
638 len = fromuutobits(outbuf, buf);
641 g_warning("Bad UUENCODE content(%d)\n", len);
644 fwrite(outbuf, sizeof(gchar), len, outfp);
649 while (fgets(buf, sizeof(buf), infp) != NULL &&
651 !IS_BOUNDARY(buf, boundary, boundary_len))) {
656 if (tmp_file) rewind(outfp);
660 gint procmime_get_part(const gchar *outfile, const gchar *infile,
666 g_return_val_if_fail(outfile != NULL, -1);
667 g_return_val_if_fail(infile != NULL, -1);
668 g_return_val_if_fail(mimeinfo != NULL, -1);
670 if ((infp = fopen(infile, "rb")) == NULL) {
671 FILE_OP_ERROR(infile, "fopen");
674 if (fseek(infp, mimeinfo->fpos, SEEK_SET) < 0) {
675 FILE_OP_ERROR(infile, "fseek");
679 if ((outfp = fopen(outfile, "wb")) == NULL) {
680 FILE_OP_ERROR(outfile, "fopen");
685 while (fgets(buf, sizeof(buf), infp) != NULL)
686 if (buf[0] == '\r' || buf[0] == '\n') break;
688 procmime_decode_content(outfp, infp, mimeinfo);
691 if (fclose(outfp) == EOF) {
692 FILE_OP_ERROR(outfile, "fclose");
700 struct ContentRenderer {
705 static GList * renderer_list = NULL;
707 static struct ContentRenderer *
708 content_renderer_new(char * content_type, char * renderer)
710 struct ContentRenderer * cr;
712 cr = g_new(struct ContentRenderer, 1);
716 cr->content_type = g_strdup(content_type);
717 cr->renderer = g_strdup(renderer);
722 static void content_renderer_free(struct ContentRenderer * cr)
724 g_free(cr->content_type);
725 g_free(cr->renderer);
729 void renderer_read_config(void)
735 g_list_foreach(renderer_list, (GFunc) content_renderer_free, NULL);
736 renderer_list = NULL;
738 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, RENDERER_RC, NULL);
739 f = fopen(rcpath, "rb");
745 while (fgets(buf, BUFFSIZE, f)) {
747 struct ContentRenderer * cr;
750 p = strchr(buf, ' ');
755 cr = content_renderer_new(buf, p + 1);
759 renderer_list = g_list_append(renderer_list, cr);
765 void renderer_write_config(void)
772 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, RENDERER_RC, NULL);
774 if ((pfile = prefs_write_open(rcpath)) == NULL) {
775 g_warning(_("failed to write configuration to file\n"));
782 for(cur = renderer_list ; cur != NULL ; cur = cur->next) {
783 struct ContentRenderer * renderer;
784 renderer = cur->data;
785 fprintf(pfile->fp, "%s %s\n", renderer->content_type,
789 if (prefs_write_close(pfile) < 0) {
790 g_warning(_("failed to write configuration to file\n"));
795 FILE *procmime_get_text_content(MimeInfo *mimeinfo, FILE *infp)
799 gboolean conv_fail = FALSE;
803 struct ContentRenderer * renderer;
806 g_return_val_if_fail(mimeinfo != NULL, NULL);
807 g_return_val_if_fail(infp != NULL, NULL);
808 g_return_val_if_fail(mimeinfo->mime_type == MIME_TEXT ||
809 mimeinfo->mime_type == MIME_TEXT_HTML ||
810 mimeinfo->mime_type == MIME_TEXT_ENRICHED, NULL);
812 if (fseek(infp, mimeinfo->fpos, SEEK_SET) < 0) {
817 while (fgets(buf, sizeof(buf), infp) != NULL)
818 if (buf[0] == '\r' || buf[0] == '\n') break;
820 tmpfp = procmime_decode_content(NULL, infp, mimeinfo);
824 if ((outfp = my_tmpfile()) == NULL) {
830 src_codeset = prefs_common.force_charset
831 ? prefs_common.force_charset : mimeinfo->charset;
835 for(cur = renderer_list ; cur != NULL ; cur = cur->next) {
836 struct ContentRenderer * cr;
838 if (g_strcasecmp(cr->content_type,
839 mimeinfo->content_type) == 0) {
845 if (renderer != NULL) {
851 dup2(fileno(outfp), 1);
853 p = popen(renderer->renderer, "w");
858 fread(buf, sizeof(char), sizeof(buf),
860 fwrite(buf, sizeof(char), count, p);
865 } else if (mimeinfo->mime_type == MIME_TEXT) {
866 while (fgets(buf, sizeof(buf), tmpfp) != NULL) {
867 str = conv_codeset_strdup(buf, src_codeset, NULL);
876 } else if (mimeinfo->mime_type == MIME_TEXT_HTML) {
880 conv = conv_code_converter_new(src_codeset);
881 parser = html_parser_new(tmpfp, conv);
882 while ((str = html_parse(parser)) != NULL) {
885 html_parser_destroy(parser);
886 conv_code_converter_destroy(conv);
887 } else if (mimeinfo->mime_type == MIME_TEXT_ENRICHED) {
891 conv = conv_code_converter_new(src_codeset);
892 parser = ertf_parser_new(tmpfp, conv);
893 while ((str = ertf_parse(parser)) != NULL) {
896 ertf_parser_destroy(parser);
897 conv_code_converter_destroy(conv);
901 g_warning(_("procmime_get_text_content(): Code conversion failed.\n"));
909 /* search the first text part of (multipart) MIME message,
910 decode, convert it and output to outfp. */
911 FILE *procmime_get_first_text_content(MsgInfo *msginfo)
913 FILE *infp, *outfp = NULL;
914 MimeInfo *mimeinfo, *partinfo;
916 g_return_val_if_fail(msginfo != NULL, NULL);
918 mimeinfo = procmime_scan_message(msginfo);
919 if (!mimeinfo) return NULL;
921 if ((infp = procmsg_open_message(msginfo)) == NULL) {
922 procmime_mimeinfo_free_all(mimeinfo);
927 while (partinfo && partinfo->mime_type != MIME_TEXT)
928 partinfo = procmime_mimeinfo_next(partinfo);
931 while (partinfo && partinfo->mime_type != MIME_TEXT_HTML &&
932 partinfo->mime_type != MIME_TEXT_ENRICHED)
933 partinfo = procmime_mimeinfo_next(partinfo);
938 outfp = procmime_get_text_content(partinfo, infp);
941 procmime_mimeinfo_free_all(mimeinfo);
946 gboolean procmime_find_string_part(MimeInfo *mimeinfo, const gchar *filename,
947 const gchar *str, gboolean case_sens)
952 gchar *(* StrFindFunc) (const gchar *haystack, const gchar *needle);
954 g_return_val_if_fail(mimeinfo != NULL, FALSE);
955 g_return_val_if_fail(mimeinfo->mime_type == MIME_TEXT ||
956 mimeinfo->mime_type == MIME_TEXT_HTML ||
957 mimeinfo->mime_type == MIME_TEXT_ENRICHED, FALSE);
958 g_return_val_if_fail(str != NULL, FALSE);
960 if ((infp = fopen(filename, "rb")) == NULL) {
961 FILE_OP_ERROR(filename, "fopen");
965 outfp = procmime_get_text_content(mimeinfo, infp);
972 StrFindFunc = strstr;
974 StrFindFunc = strcasestr;
976 while (fgets(buf, sizeof(buf), outfp) != NULL) {
977 if (StrFindFunc(buf, str) != NULL) {
988 gboolean procmime_find_string(MsgInfo *msginfo, const gchar *str,
994 gboolean found = FALSE;
996 g_return_val_if_fail(msginfo != NULL, FALSE);
997 g_return_val_if_fail(str != NULL, FALSE);
999 filename = procmsg_get_message_file(msginfo);
1000 if (!filename) return FALSE;
1001 mimeinfo = procmime_scan_message(msginfo);
1003 for (partinfo = mimeinfo; partinfo != NULL;
1004 partinfo = procmime_mimeinfo_next(partinfo)) {
1005 if (partinfo->mime_type == MIME_TEXT ||
1006 partinfo->mime_type == MIME_TEXT_HTML ||
1007 partinfo->mime_type == MIME_TEXT_ENRICHED) {
1008 if (procmime_find_string_part
1009 (partinfo, filename, str, case_sens) == TRUE) {
1016 procmime_mimeinfo_free_all(mimeinfo);
1022 gchar *procmime_get_tmp_file_name(MimeInfo *mimeinfo)
1024 static guint32 id = 0;
1029 g_return_val_if_fail(mimeinfo != NULL, NULL);
1031 g_snprintf(f_prefix, sizeof(f_prefix), "%08x.", id++);
1033 if (MIME_TEXT_HTML == mimeinfo->mime_type)
1034 base = "mimetmp.html";
1036 base = mimeinfo->filename ? mimeinfo->filename
1037 : mimeinfo->name ? mimeinfo->name : "mimetmp";
1038 base = g_basename(base);
1039 if (*base == '\0') base = "mimetmp";
1042 filename = g_strconcat(get_mime_tmp_dir(), G_DIR_SEPARATOR_S,
1043 f_prefix, base, NULL);
1048 ContentType procmime_scan_mime_type(const gchar *mime_type)
1052 if (!strncasecmp(mime_type, "text/html", 9))
1053 type = MIME_TEXT_HTML;
1054 else if (!strncasecmp(mime_type, "text/enriched", 13))
1055 type = MIME_TEXT_ENRICHED;
1056 else if (!strncasecmp(mime_type, "text/", 5))
1058 else if (!strncasecmp(mime_type, "message/rfc822", 14))
1059 type = MIME_MESSAGE_RFC822;
1060 else if (!strncasecmp(mime_type, "message/", 8))
1062 else if (!strncasecmp(mime_type, "application/octet-stream", 24))
1063 type = MIME_APPLICATION_OCTET_STREAM;
1064 else if (!strncasecmp(mime_type, "application/", 12))
1065 type = MIME_APPLICATION;
1066 else if (!strncasecmp(mime_type, "multipart/", 10))
1067 type = MIME_MULTIPART;
1068 else if (!strncasecmp(mime_type, "image/", 6))
1070 else if (!strncasecmp(mime_type, "audio/", 6))
1072 else if (!strcasecmp(mime_type, "text"))
1075 type = MIME_UNKNOWN;
1080 static GList *mime_type_list = NULL;
1082 gchar *procmime_get_mime_type(const gchar *filename)
1084 static GHashTable *mime_type_table = NULL;
1085 MimeType *mime_type;
1089 if (!mime_type_table) {
1090 mime_type_table = procmime_get_mime_type_table();
1091 if (!mime_type_table) return NULL;
1094 filename = g_basename(filename);
1095 p = strrchr(filename, '.');
1096 if (!p) return NULL;
1098 Xstrdup_a(ext, p + 1, return NULL);
1100 mime_type = g_hash_table_lookup(mime_type_table, ext);
1104 str = g_strconcat(mime_type->type, "/", mime_type->sub_type,
1112 static guint procmime_str_hash(gconstpointer gptr)
1114 guint hash_result = 0;
1117 for (str = gptr; str && *str; str++) {
1118 if (isupper(*str)) hash_result += (*str + ' ');
1119 else hash_result += *str;
1125 static gint procmime_str_equal(gconstpointer gptr1, gconstpointer gptr2)
1127 const char *str1 = gptr1;
1128 const char *str2 = gptr2;
1130 return !strcasecmp(str1, str2);
1133 static GHashTable *procmime_get_mime_type_table(void)
1135 GHashTable *table = NULL;
1137 MimeType *mime_type;
1140 if (!mime_type_list) {
1141 mime_type_list = procmime_get_mime_type_list();
1142 if (!mime_type_list) return NULL;
1145 table = g_hash_table_new(procmime_str_hash, procmime_str_equal);
1147 for (cur = mime_type_list; cur != NULL; cur = cur->next) {
1151 mime_type = (MimeType *)cur->data;
1153 if (!mime_type->extension) continue;
1155 exts = g_strsplit(mime_type->extension, " ", 16);
1156 for (i = 0; exts[i] != NULL; i++) {
1157 /* make the key case insensitive */
1159 /* use previously dup'd key on overwriting */
1160 if (g_hash_table_lookup(table, exts[i]))
1163 key = g_strdup(exts[i]);
1164 g_hash_table_insert(table, key, mime_type);
1172 GList *procmime_get_mime_type_list(void)
1176 gchar buf[BUFFSIZE];
1178 MimeType *mime_type;
1181 return mime_type_list;
1183 if ((fp = fopen("/etc/mime.types", "rb")) == NULL) {
1184 if ((fp = fopen(SYSCONFDIR "/mime.types", "rb")) == NULL) {
1185 FILE_OP_ERROR(SYSCONFDIR "/mime.types", "fopen");
1190 while (fgets(buf, sizeof(buf), fp) != NULL) {
1191 p = strchr(buf, '#');
1196 while (*p && !isspace(*p)) p++;
1201 delim = strchr(buf, '/');
1202 if (delim == NULL) continue;
1205 mime_type = g_new(MimeType, 1);
1206 mime_type->type = g_strdup(buf);
1207 mime_type->sub_type = g_strdup(delim + 1);
1209 while (*p && isspace(*p)) p++;
1211 mime_type->extension = g_strdup(p);
1213 mime_type->extension = NULL;
1215 list = g_list_append(list, mime_type);
1221 g_warning("Can't read mime.types\n");
1226 EncodingType procmime_get_encoding_for_charset(const gchar *charset)
1230 else if (!strncasecmp(charset, "ISO-2022-", 9) ||
1231 !strcasecmp(charset, "US-ASCII"))
1235 /* return ENC_BASE64; */
1236 /* return ENC_QUOTED_PRINTABLE; */
1239 const gchar *procmime_get_encoding_str(EncodingType encoding)
1241 static const gchar *encoding_str[] = {
1242 "7bit", "8bit", "quoted-printable", "base64", "x-uuencode",
1246 if (encoding >= ENC_7BIT && encoding <= ENC_UNKNOWN)
1247 return encoding_str[encoding];