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, "r")) == 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, "w")) == 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, "r");
745 while (fgets(buf, BUFFSIZE, f)) {
747 struct ContentRenderer * cr;
750 p = strchr(buf, ' ');
755 cr = content_renderer_new(buf, p + 1);
759 printf("%s %s\n", cr->content_type, cr->renderer);
761 renderer_list = g_list_append(renderer_list, cr);
767 void renderer_write_config(void)
774 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, RENDERER_RC, NULL);
776 if ((pfile = prefs_write_open(rcpath)) == NULL) {
777 g_warning(_("failed to write configuration to file\n"));
784 for(cur = renderer_list ; cur != NULL ; cur = cur->next) {
785 struct ContentRenderer * renderer;
786 renderer = cur->data;
787 fprintf(pfile->fp, "%s %s\n", renderer->content_type,
791 if (prefs_write_close(pfile) < 0) {
792 g_warning(_("failed to write configuration to file\n"));
797 FILE *procmime_get_text_content(MimeInfo *mimeinfo, FILE *infp)
801 gboolean conv_fail = FALSE;
805 struct ContentRenderer * renderer;
808 g_return_val_if_fail(mimeinfo != NULL, NULL);
809 g_return_val_if_fail(infp != NULL, NULL);
810 g_return_val_if_fail(mimeinfo->mime_type == MIME_TEXT ||
811 mimeinfo->mime_type == MIME_TEXT_HTML ||
812 mimeinfo->mime_type == MIME_TEXT_ENRICHED, NULL);
814 if (fseek(infp, mimeinfo->fpos, SEEK_SET) < 0) {
819 while (fgets(buf, sizeof(buf), infp) != NULL)
820 if (buf[0] == '\r' || buf[0] == '\n') break;
822 tmpfp = procmime_decode_content(NULL, infp, mimeinfo);
826 if ((outfp = my_tmpfile()) == NULL) {
832 src_codeset = prefs_common.force_charset
833 ? prefs_common.force_charset : mimeinfo->charset;
837 for(cur = renderer_list ; cur != NULL ; cur = cur->next) {
838 struct ContentRenderer * cr;
840 if (g_strcasecmp(cr->content_type,
841 mimeinfo->content_type) == 0) {
847 if (renderer != NULL) {
853 dup2(fileno(outfp), 1);
855 p = popen(renderer->renderer, "w");
860 fread(buf, sizeof(char), sizeof(buf),
862 fwrite(buf, sizeof(char), count, p);
867 } else if (mimeinfo->mime_type == MIME_TEXT) {
868 while (fgets(buf, sizeof(buf), tmpfp) != NULL) {
869 str = conv_codeset_strdup(buf, src_codeset, NULL);
878 } else if (mimeinfo->mime_type == MIME_TEXT_HTML) {
882 conv = conv_code_converter_new(src_codeset);
883 parser = html_parser_new(tmpfp, conv);
884 while ((str = html_parse(parser)) != NULL) {
887 html_parser_destroy(parser);
888 conv_code_converter_destroy(conv);
889 } else if (mimeinfo->mime_type == MIME_TEXT_ENRICHED) {
893 conv = conv_code_converter_new(src_codeset);
894 parser = ertf_parser_new(tmpfp, conv);
895 while ((str = ertf_parse(parser)) != NULL) {
898 ertf_parser_destroy(parser);
899 conv_code_converter_destroy(conv);
903 g_warning(_("procmime_get_text_content(): Code conversion failed.\n"));
911 /* search the first text part of (multipart) MIME message,
912 decode, convert it and output to outfp. */
913 FILE *procmime_get_first_text_content(MsgInfo *msginfo)
915 FILE *infp, *outfp = NULL;
916 MimeInfo *mimeinfo, *partinfo;
918 g_return_val_if_fail(msginfo != NULL, NULL);
920 mimeinfo = procmime_scan_message(msginfo);
921 if (!mimeinfo) return NULL;
923 if ((infp = procmsg_open_message(msginfo)) == NULL) {
924 procmime_mimeinfo_free_all(mimeinfo);
929 while (partinfo && partinfo->mime_type != MIME_TEXT)
930 partinfo = procmime_mimeinfo_next(partinfo);
933 while (partinfo && partinfo->mime_type != MIME_TEXT_HTML &&
934 partinfo->mime_type != MIME_TEXT_ENRICHED)
935 partinfo = procmime_mimeinfo_next(partinfo);
940 outfp = procmime_get_text_content(partinfo, infp);
943 procmime_mimeinfo_free_all(mimeinfo);
948 gboolean procmime_find_string_part(MimeInfo *mimeinfo, const gchar *filename,
949 const gchar *str, gboolean case_sens)
954 gchar *(* StrFindFunc) (const gchar *haystack, const gchar *needle);
956 g_return_val_if_fail(mimeinfo != NULL, FALSE);
957 g_return_val_if_fail(mimeinfo->mime_type == MIME_TEXT ||
958 mimeinfo->mime_type == MIME_TEXT_HTML ||
959 mimeinfo->mime_type == MIME_TEXT_ENRICHED, FALSE);
960 g_return_val_if_fail(str != NULL, FALSE);
962 if ((infp = fopen(filename, "r")) == NULL) {
963 FILE_OP_ERROR(filename, "fopen");
967 outfp = procmime_get_text_content(mimeinfo, infp);
974 StrFindFunc = strstr;
976 StrFindFunc = strcasestr;
978 while (fgets(buf, sizeof(buf), outfp) != NULL) {
979 if (StrFindFunc(buf, str) != NULL) {
990 gboolean procmime_find_string(MsgInfo *msginfo, const gchar *str,
996 gboolean found = FALSE;
998 g_return_val_if_fail(msginfo != NULL, FALSE);
999 g_return_val_if_fail(str != NULL, FALSE);
1001 filename = procmsg_get_message_file(msginfo);
1002 if (!filename) return FALSE;
1003 mimeinfo = procmime_scan_message(msginfo);
1005 for (partinfo = mimeinfo; partinfo != NULL;
1006 partinfo = procmime_mimeinfo_next(partinfo)) {
1007 if (partinfo->mime_type == MIME_TEXT ||
1008 partinfo->mime_type == MIME_TEXT_HTML ||
1009 partinfo->mime_type == MIME_TEXT_ENRICHED) {
1010 if (procmime_find_string_part
1011 (partinfo, filename, str, case_sens) == TRUE) {
1018 procmime_mimeinfo_free_all(mimeinfo);
1024 gchar *procmime_get_tmp_file_name(MimeInfo *mimeinfo)
1026 static guint32 id = 0;
1031 g_return_val_if_fail(mimeinfo != NULL, NULL);
1033 g_snprintf(f_prefix, sizeof(f_prefix), "%08x.", id++);
1035 if (MIME_TEXT_HTML == mimeinfo->mime_type)
1036 base = "mimetmp.html";
1038 base = mimeinfo->filename ? mimeinfo->filename
1039 : mimeinfo->name ? mimeinfo->name : "mimetmp";
1040 base = g_basename(base);
1041 if (*base == '\0') base = "mimetmp";
1044 filename = g_strconcat(get_mime_tmp_dir(), G_DIR_SEPARATOR_S,
1045 f_prefix, base, NULL);
1050 ContentType procmime_scan_mime_type(const gchar *mime_type)
1054 if (!strncasecmp(mime_type, "text/html", 9))
1055 type = MIME_TEXT_HTML;
1056 else if (!strncasecmp(mime_type, "text/enriched", 13))
1057 type = MIME_TEXT_ENRICHED;
1058 else if (!strncasecmp(mime_type, "text/", 5))
1060 else if (!strncasecmp(mime_type, "message/rfc822", 14))
1061 type = MIME_MESSAGE_RFC822;
1062 else if (!strncasecmp(mime_type, "message/", 8))
1064 else if (!strncasecmp(mime_type, "application/octet-stream", 24))
1065 type = MIME_APPLICATION_OCTET_STREAM;
1066 else if (!strncasecmp(mime_type, "application/", 12))
1067 type = MIME_APPLICATION;
1068 else if (!strncasecmp(mime_type, "multipart/", 10))
1069 type = MIME_MULTIPART;
1070 else if (!strncasecmp(mime_type, "image/", 6))
1072 else if (!strncasecmp(mime_type, "audio/", 6))
1074 else if (!strcasecmp(mime_type, "text"))
1077 type = MIME_UNKNOWN;
1082 static GList *mime_type_list = NULL;
1084 gchar *procmime_get_mime_type(const gchar *filename)
1086 static GHashTable *mime_type_table = NULL;
1087 MimeType *mime_type;
1091 if (!mime_type_table) {
1092 mime_type_table = procmime_get_mime_type_table();
1093 if (!mime_type_table) return NULL;
1096 filename = g_basename(filename);
1097 p = strrchr(filename, '.');
1098 if (!p) return NULL;
1100 Xstrdup_a(ext, p + 1, return NULL);
1102 mime_type = g_hash_table_lookup(mime_type_table, ext);
1106 str = g_strconcat(mime_type->type, "/", mime_type->sub_type,
1114 static guint procmime_str_hash(gconstpointer gptr)
1116 guint hash_result = 0;
1119 for (str = gptr; str && *str; str++) {
1120 if (isupper(*str)) hash_result += (*str + ' ');
1121 else hash_result += *str;
1127 static gint procmime_str_equal(gconstpointer gptr1, gconstpointer gptr2)
1129 const char *str1 = gptr1;
1130 const char *str2 = gptr2;
1132 return !strcasecmp(str1, str2);
1135 static GHashTable *procmime_get_mime_type_table(void)
1137 GHashTable *table = NULL;
1139 MimeType *mime_type;
1142 if (!mime_type_list) {
1143 mime_type_list = procmime_get_mime_type_list();
1144 if (!mime_type_list) return NULL;
1147 table = g_hash_table_new(procmime_str_hash, procmime_str_equal);
1149 for (cur = mime_type_list; cur != NULL; cur = cur->next) {
1153 mime_type = (MimeType *)cur->data;
1155 if (!mime_type->extension) continue;
1157 exts = g_strsplit(mime_type->extension, " ", 16);
1158 for (i = 0; exts[i] != NULL; i++) {
1159 /* make the key case insensitive */
1161 /* use previously dup'd key on overwriting */
1162 if (g_hash_table_lookup(table, exts[i]))
1165 key = g_strdup(exts[i]);
1166 g_hash_table_insert(table, key, mime_type);
1174 GList *procmime_get_mime_type_list(void)
1178 gchar buf[BUFFSIZE];
1180 MimeType *mime_type;
1183 return mime_type_list;
1185 if ((fp = fopen("/etc/mime.types", "r")) == NULL) {
1186 if ((fp = fopen(SYSCONFDIR "/mime.types", "r")) == NULL) {
1187 FILE_OP_ERROR(SYSCONFDIR "/mime.types", "fopen");
1192 while (fgets(buf, sizeof(buf), fp) != NULL) {
1193 p = strchr(buf, '#');
1198 while (*p && !isspace(*p)) p++;
1203 delim = strchr(buf, '/');
1204 if (delim == NULL) continue;
1207 mime_type = g_new(MimeType, 1);
1208 mime_type->type = g_strdup(buf);
1209 mime_type->sub_type = g_strdup(delim + 1);
1211 while (*p && isspace(*p)) p++;
1213 mime_type->extension = g_strdup(p);
1215 mime_type->extension = NULL;
1217 list = g_list_append(list, mime_type);
1223 g_warning("Can't read mime.types\n");
1228 EncodingType procmime_get_encoding_for_charset(const gchar *charset)
1232 else if (!strncasecmp(charset, "ISO-2022-", 9) ||
1233 !strcasecmp(charset, "US-ASCII"))
1237 /* return ENC_BASE64; */
1238 /* return ENC_QUOTED_PRINTABLE; */
1241 const gchar *procmime_get_encoding_str(EncodingType encoding)
1243 static const gchar *encoding_str[] = {
1244 "7bit", "8bit", "quoted-printable", "base64", "x-uuencode",
1248 if (encoding >= ENC_7BIT && encoding <= ENC_UNKNOWN)
1249 return encoding_str[encoding];