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"
48 static GHashTable *procmime_get_mime_type_table (void);
50 MimeInfo *procmime_mimeinfo_new(void)
54 mimeinfo = g_new0(MimeInfo, 1);
55 mimeinfo->mime_type = MIME_UNKNOWN;
56 mimeinfo->encoding_type = ENC_UNKNOWN;
61 void procmime_mimeinfo_free(MimeInfo *mimeinfo)
63 if (!mimeinfo) return;
65 g_free(mimeinfo->encoding);
66 g_free(mimeinfo->content_type);
67 g_free(mimeinfo->charset);
68 g_free(mimeinfo->name);
69 g_free(mimeinfo->boundary);
70 g_free(mimeinfo->content_disposition);
71 g_free(mimeinfo->filename);
73 g_free(mimeinfo->plaintextfile);
74 g_free(mimeinfo->sigstatus);
75 g_free(mimeinfo->sigstatus_full);
78 procmime_mimeinfo_free(mimeinfo->sub);
83 void procmime_mimeinfo_free_all(MimeInfo *mimeinfo)
85 while (mimeinfo != NULL) {
88 g_free(mimeinfo->encoding);
89 g_free(mimeinfo->content_type);
90 g_free(mimeinfo->charset);
91 g_free(mimeinfo->name);
92 g_free(mimeinfo->boundary);
93 g_free(mimeinfo->content_disposition);
94 g_free(mimeinfo->filename);
96 g_free(mimeinfo->plaintextfile);
97 g_free(mimeinfo->sigstatus);
98 g_free(mimeinfo->sigstatus_full);
101 procmime_mimeinfo_free_all(mimeinfo->sub);
102 procmime_mimeinfo_free_all(mimeinfo->children);
104 procmime_mimeinfo_free_all(mimeinfo->plaintext);
107 next = mimeinfo->next;
113 MimeInfo *procmime_mimeinfo_insert(MimeInfo *parent, MimeInfo *mimeinfo)
115 MimeInfo *child = parent->children;
118 parent->children = mimeinfo;
120 while (child->next != NULL)
123 child->next = mimeinfo;
126 mimeinfo->parent = parent;
127 mimeinfo->level = parent->level + 1;
132 void procmime_mimeinfo_replace(MimeInfo *old, MimeInfo *new)
134 MimeInfo *parent = old->parent;
137 g_return_if_fail(parent != NULL);
138 g_return_if_fail(new->next == NULL);
140 for (child = parent->children; child && child != old;
144 g_warning("oops: parent can't find it's own child");
147 procmime_mimeinfo_free_all(old);
149 if (child == parent->children) {
150 new->next = parent->children->next;
151 parent->children = new;
153 new->next = child->next;
158 MimeInfo *procmime_mimeinfo_next(MimeInfo *mimeinfo)
160 if (!mimeinfo) return NULL;
162 if (mimeinfo->children)
163 return mimeinfo->children;
165 return mimeinfo->sub;
167 return mimeinfo->next;
169 for (mimeinfo = mimeinfo->parent; mimeinfo != NULL;
170 mimeinfo = mimeinfo->parent) {
172 return mimeinfo->next;
173 if (mimeinfo->main) {
174 mimeinfo = mimeinfo->main;
176 return mimeinfo->next;
183 MimeInfo *procmime_scan_message(MsgInfo *msginfo)
188 g_return_val_if_fail(msginfo != NULL, NULL);
190 if ((fp = procmsg_open_message(msginfo)) == NULL) return NULL;
191 mimeinfo = procmime_scan_mime_header(fp, MIME_TEXT);
194 if (mimeinfo->mime_type != MIME_MULTIPART) {
195 if (fseek(fp, mimeinfo->fpos, SEEK_SET) < 0)
198 if (mimeinfo->mime_type != MIME_TEXT)
199 procmime_scan_multipart_message(mimeinfo, fp);
203 if (prefs_common.auto_check_signatures)
204 rfc2015_check_signature(mimeinfo, fp);
211 void procmime_scan_multipart_message(MimeInfo *mimeinfo, FILE *fp)
215 gint boundary_len = 0;
217 glong fpos, prev_fpos;
219 ContentType default_type;
221 g_return_if_fail(mimeinfo != NULL);
222 g_return_if_fail(mimeinfo->mime_type != MIME_TEXT);
224 if (mimeinfo->mime_type == MIME_MULTIPART) {
225 g_return_if_fail(mimeinfo->boundary != NULL);
226 g_return_if_fail(mimeinfo->sub == NULL);
228 g_return_if_fail(fp != NULL);
230 boundary = mimeinfo->boundary;
233 boundary_len = strlen(boundary);
235 /* look for first boundary */
236 while ((p = fgets(buf, sizeof(buf), fp)) != NULL)
237 if (IS_BOUNDARY(buf, boundary, boundary_len)) break;
241 if ((fpos = ftell(fp)) < 0) {
246 /* Sets default subpart type for multipart/digest */
247 if (!strncasecmp(mimeinfo->content_type, "multipart/digest", 16))
248 default_type = MIME_MESSAGE_RFC822;
250 default_type = MIME_TEXT;
252 for (npart = 0;; npart++) {
254 gboolean eom = FALSE;
258 partinfo = procmime_scan_mime_header(fp, default_type);
259 if (!partinfo) break;
260 procmime_mimeinfo_insert(mimeinfo, partinfo);
262 if (partinfo->mime_type == MIME_MULTIPART) {
263 if (partinfo->level < 8)
264 procmime_scan_multipart_message(partinfo, fp);
265 } else if (partinfo->mime_type == MIME_MESSAGE_RFC822) {
268 partinfo->sub = sub = procmime_scan_mime_header
271 sub->level = partinfo->level + 1;
272 sub->parent = partinfo;
273 sub->main = partinfo;
275 if (sub->mime_type == MIME_MULTIPART) {
277 procmime_scan_multipart_message
282 /* look for next boundary */
284 while ((p = fgets(buf, sizeof(buf), fp)) != NULL) {
285 if (IS_BOUNDARY(buf, boundary, boundary_len)) {
286 if (buf[2 + boundary_len] == '-' &&
287 buf[2 + boundary_len + 1] == '-')
293 eom = TRUE; /* broken MIME message */
296 partinfo->size = fpos - prev_fpos - strlen(buf);
302 void procmime_scan_encoding(MimeInfo *mimeinfo, const gchar *encoding)
306 Xstrdup_a(buf, encoding, return);
308 g_free(mimeinfo->encoding);
310 mimeinfo->encoding = g_strdup(g_strstrip(buf));
311 if (!strcasecmp(buf, "7bit"))
312 mimeinfo->encoding_type = ENC_7BIT;
313 else if (!strcasecmp(buf, "8bit"))
314 mimeinfo->encoding_type = ENC_8BIT;
315 else if (!strcasecmp(buf, "quoted-printable"))
316 mimeinfo->encoding_type = ENC_QUOTED_PRINTABLE;
317 else if (!strcasecmp(buf, "base64"))
318 mimeinfo->encoding_type = ENC_BASE64;
319 else if (!strcasecmp(buf, "x-uuencode"))
320 mimeinfo->encoding_type = ENC_X_UUENCODE;
322 mimeinfo->encoding_type = ENC_UNKNOWN;
326 void procmime_scan_content_type(MimeInfo *mimeinfo, const gchar *content_type)
328 gchar *delim, *p, *cnttype;
331 if (conv_get_current_charset() == C_EUC_JP &&
332 strchr(content_type, '\033')) {
334 len = strlen(content_type) * 2 + 1;
335 Xalloca(buf, len, return);
336 conv_jistoeuc(buf, len, content_type);
338 Xstrdup_a(buf, content_type, return);
340 g_free(mimeinfo->content_type);
341 g_free(mimeinfo->charset);
342 /* g_free(mimeinfo->name); */
343 mimeinfo->content_type = NULL;
344 mimeinfo->charset = NULL;
345 /* mimeinfo->name = NULL; */
347 if ((delim = strchr(buf, ';'))) *delim = '\0';
348 mimeinfo->content_type = cnttype = g_strdup(g_strstrip(buf));
350 mimeinfo->mime_type = procmime_scan_mime_type(cnttype);
359 if ((delim = strchr(p, ';'))) *delim = '\0';
361 if (!(eq = strchr(p, '='))) break;
370 extract_quote(value, '"');
372 eliminate_parenthesis(value, '(', ')');
377 if (!strcasecmp(attr, "charset"))
378 mimeinfo->charset = g_strdup(value);
379 else if (!strcasecmp(attr, "name")) {
383 len = strlen(value) + 1;
384 Xalloca(tmp, len, return);
385 conv_unmime_header(tmp, len, value, NULL);
386 g_free(mimeinfo->name);
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 mimeinfo->filename = g_strdup(tmp);
461 void procmime_scan_content_description(MimeInfo *mimeinfo,
462 const gchar *content_description)
464 gchar *delim, *p, *dispos;
470 if (conv_get_current_charset() == C_EUC_JP &&
471 strchr(content_description, '\033')) {
473 len = strlen(content_description) * 2 + 1;
474 Xalloca(buf, len, return);
475 conv_jistoeuc(buf, len, content_description);
477 Xstrdup_a(buf, content_description, return);
479 blen = strlen(buf) + 1;
480 Xalloca(tmp, blen, return);
481 conv_unmime_header(tmp, blen, buf, NULL);
482 g_free(mimeinfo->name);
483 mimeinfo->name = g_strdup(tmp);
486 void procmime_scan_subject(MimeInfo *mimeinfo,
487 const gchar *subject)
489 gchar *delim, *p, *dispos;
495 if (conv_get_current_charset() == C_EUC_JP &&
496 strchr(subject, '\033')) {
498 len = strlen(subject) * 2 + 1;
499 Xalloca(buf, len, return);
500 conv_jistoeuc(buf, len, subject);
502 Xstrdup_a(buf, subject, return);
504 blen = strlen(buf) + 1;
505 Xalloca(tmp, blen, return);
506 conv_unmime_header(tmp, blen, buf, NULL);
507 g_free(mimeinfo->name);
508 mimeinfo->name = g_strdup(tmp);
513 H_CONTENT_TRANSFER_ENCODING = 0,
515 H_CONTENT_DISPOSITION = 2,
516 H_CONTENT_DESCRIPTION = 3,
520 MimeInfo *procmime_scan_mime_header(FILE *fp, ContentType default_type)
522 static HeaderEntry hentry[] = {{"Content-Transfer-Encoding:",
524 {"Content-Type:", NULL, TRUE},
525 {"Content-Disposition:",
527 {"Content-description:",
531 {NULL, NULL, FALSE}};
537 g_return_val_if_fail(fp != NULL, NULL);
539 mimeinfo = procmime_mimeinfo_new();
540 /* Needed for multipart/digest */
541 mimeinfo->mime_type = default_type;
542 mimeinfo->encoding_type = ENC_7BIT;
543 mimeinfo->fpos = ftell(fp);
545 while ((hnum = procheader_get_one_field(buf, sizeof(buf), fp, hentry))
549 if (H_CONTENT_TRANSFER_ENCODING == hnum) {
550 procmime_scan_encoding
551 (mimeinfo, buf + strlen(hp->name));
552 } else if (H_CONTENT_TYPE == hnum) {
553 procmime_scan_content_type
554 (mimeinfo, buf + strlen(hp->name));
555 } else if (H_CONTENT_DISPOSITION == hnum) {
556 procmime_scan_content_disposition
557 (mimeinfo, buf + strlen(hp->name));
558 } else if (H_CONTENT_DESCRIPTION == hnum) {
559 procmime_scan_content_description
560 (mimeinfo, buf + strlen(hp->name));
561 } else if (H_SUBJECT == hnum) {
562 procmime_scan_subject
563 (mimeinfo, buf + strlen(hp->name));
567 if (mimeinfo->mime_type == MIME_APPLICATION_OCTET_STREAM &&
570 type = procmime_get_mime_type(mimeinfo->name);
572 mimeinfo->mime_type = procmime_scan_mime_type(type);
575 if (!mimeinfo->content_type) {
576 if (mimeinfo->mime_type == MIME_MESSAGE_RFC822) {
577 mimeinfo->content_type = g_strdup("message/rfc822");
579 mimeinfo->content_type = g_strdup("text/plain");
586 FILE *procmime_decode_content(FILE *outfp, FILE *infp, MimeInfo *mimeinfo)
589 gchar *boundary = NULL;
590 gint boundary_len = 0;
591 gboolean tmp_file = FALSE;
593 g_return_val_if_fail(infp != NULL, NULL);
594 g_return_val_if_fail(mimeinfo != NULL, NULL);
597 outfp = my_tmpfile();
605 if (mimeinfo->parent && mimeinfo->parent->boundary) {
606 boundary = mimeinfo->parent->boundary;
607 boundary_len = strlen(boundary);
610 if (mimeinfo->encoding_type == ENC_QUOTED_PRINTABLE) {
611 gboolean softline = FALSE;
613 while (fgets(buf, sizeof(buf), infp) != NULL &&
615 !IS_BOUNDARY(buf, boundary, boundary_len))) {
618 softline = DoOneQPLine(&p, FALSE, softline);
619 fwrite(buf, p - (guchar *)buf, 1, outfp);
621 } else if (mimeinfo->encoding_type == ENC_BASE64) {
622 gchar outbuf[BUFFSIZE];
624 Base64Decoder *decoder;
626 decoder = base64_decoder_new();
627 while (fgets(buf, sizeof(buf), infp) != NULL &&
629 !IS_BOUNDARY(buf, boundary, boundary_len))) {
630 len = base64_decoder_decode(decoder, buf, outbuf);
632 g_warning("Bad BASE64 content\n");
635 fwrite(outbuf, sizeof(gchar), len, outfp);
637 base64_decoder_free(decoder);
638 } else if (mimeinfo->encoding_type == ENC_X_UUENCODE) {
639 gchar outbuf[BUFFSIZE];
641 gboolean flag = FALSE;
643 while (fgets(buf, sizeof(buf), infp) != NULL &&
645 !IS_BOUNDARY(buf, boundary, boundary_len))) {
646 if(!flag && strncmp(buf,"begin ", 6)) continue;
649 len = fromuutobits(outbuf, buf);
652 g_warning("Bad UUENCODE content(%d)\n", len);
655 fwrite(outbuf, sizeof(gchar), len, outfp);
660 while (fgets(buf, sizeof(buf), infp) != NULL &&
662 !IS_BOUNDARY(buf, boundary, boundary_len))) {
667 if (tmp_file) rewind(outfp);
671 gint procmime_get_part(const gchar *outfile, const gchar *infile,
677 g_return_val_if_fail(outfile != NULL, -1);
678 g_return_val_if_fail(infile != NULL, -1);
679 g_return_val_if_fail(mimeinfo != NULL, -1);
681 if ((infp = fopen(infile, "r")) == NULL) {
682 FILE_OP_ERROR(infile, "fopen");
685 if (fseek(infp, mimeinfo->fpos, SEEK_SET) < 0) {
686 FILE_OP_ERROR(infile, "fseek");
690 if ((outfp = fopen(outfile, "w")) == NULL) {
691 FILE_OP_ERROR(outfile, "fopen");
696 while (fgets(buf, sizeof(buf), infp) != NULL)
697 if (buf[0] == '\r' || buf[0] == '\n') break;
699 procmime_decode_content(outfp, infp, mimeinfo);
702 if (fclose(outfp) == EOF) {
703 FILE_OP_ERROR(outfile, "fclose");
711 FILE *procmime_get_text_content(MimeInfo *mimeinfo, FILE *infp)
715 gboolean conv_fail = FALSE;
719 g_return_val_if_fail(mimeinfo != NULL, NULL);
720 g_return_val_if_fail(infp != NULL, NULL);
721 g_return_val_if_fail(mimeinfo->mime_type == MIME_TEXT ||
722 mimeinfo->mime_type == MIME_TEXT_HTML ||
723 mimeinfo->mime_type == MIME_TEXT_ENRICHED, NULL);
725 if (fseek(infp, mimeinfo->fpos, SEEK_SET) < 0) {
730 while (fgets(buf, sizeof(buf), infp) != NULL)
731 if (buf[0] == '\r' || buf[0] == '\n') break;
733 tmpfp = procmime_decode_content(NULL, infp, mimeinfo);
737 if ((outfp = my_tmpfile()) == NULL) {
743 src_codeset = prefs_common.force_charset
744 ? prefs_common.force_charset : mimeinfo->charset;
746 if (mimeinfo->mime_type == MIME_TEXT) {
747 while (fgets(buf, sizeof(buf), tmpfp) != NULL) {
748 str = conv_codeset_strdup(buf, src_codeset, NULL);
757 } else if (mimeinfo->mime_type == MIME_TEXT_HTML) {
761 conv = conv_code_converter_new(src_codeset);
762 parser = html_parser_new(tmpfp, conv);
763 while ((str = html_parse(parser)) != NULL) {
766 html_parser_destroy(parser);
767 conv_code_converter_destroy(conv);
768 } else if (mimeinfo->mime_type == MIME_TEXT_ENRICHED) {
772 conv = conv_code_converter_new(src_codeset);
773 parser = ertf_parser_new(tmpfp, conv);
774 while ((str = ertf_parse(parser)) != NULL) {
777 ertf_parser_destroy(parser);
778 conv_code_converter_destroy(conv);
782 g_warning(_("procmime_get_text_content(): Code conversion failed.\n"));
790 /* search the first text part of (multipart) MIME message,
791 decode, convert it and output to outfp. */
792 FILE *procmime_get_first_text_content(MsgInfo *msginfo)
794 FILE *infp, *outfp = NULL;
795 MimeInfo *mimeinfo, *partinfo;
797 g_return_val_if_fail(msginfo != NULL, NULL);
799 mimeinfo = procmime_scan_message(msginfo);
800 if (!mimeinfo) return NULL;
802 if ((infp = procmsg_open_message(msginfo)) == NULL) {
803 procmime_mimeinfo_free_all(mimeinfo);
808 while (partinfo && partinfo->mime_type != MIME_TEXT)
809 partinfo = procmime_mimeinfo_next(partinfo);
812 while (partinfo && partinfo->mime_type != MIME_TEXT_HTML &&
813 partinfo->mime_type != MIME_TEXT_ENRICHED)
814 partinfo = procmime_mimeinfo_next(partinfo);
819 outfp = procmime_get_text_content(partinfo, infp);
822 procmime_mimeinfo_free_all(mimeinfo);
827 gboolean procmime_find_string_part(MimeInfo *mimeinfo, const gchar *filename,
828 const gchar *str, gboolean case_sens)
833 gchar *(* StrFindFunc) (const gchar *haystack, const gchar *needle);
835 g_return_val_if_fail(mimeinfo != NULL, FALSE);
836 g_return_val_if_fail(mimeinfo->mime_type == MIME_TEXT ||
837 mimeinfo->mime_type == MIME_TEXT_HTML ||
838 mimeinfo->mime_type == MIME_TEXT_ENRICHED, FALSE);
839 g_return_val_if_fail(str != NULL, FALSE);
841 if ((infp = fopen(filename, "r")) == NULL) {
842 FILE_OP_ERROR(filename, "fopen");
846 outfp = procmime_get_text_content(mimeinfo, infp);
853 StrFindFunc = strstr;
855 StrFindFunc = strcasestr;
857 while (fgets(buf, sizeof(buf), outfp) != NULL) {
858 if (StrFindFunc(buf, str) != NULL) {
869 gboolean procmime_find_string(MsgInfo *msginfo, const gchar *str,
875 gboolean found = FALSE;
877 g_return_val_if_fail(msginfo != NULL, FALSE);
878 g_return_val_if_fail(str != NULL, FALSE);
880 filename = procmsg_get_message_file(msginfo);
881 if (!filename) return FALSE;
882 mimeinfo = procmime_scan_message(msginfo);
884 for (partinfo = mimeinfo; partinfo != NULL;
885 partinfo = procmime_mimeinfo_next(partinfo)) {
886 if (partinfo->mime_type == MIME_TEXT ||
887 partinfo->mime_type == MIME_TEXT_HTML ||
888 partinfo->mime_type == MIME_TEXT_ENRICHED) {
889 if (procmime_find_string_part
890 (partinfo, filename, str, case_sens) == TRUE) {
897 procmime_mimeinfo_free_all(mimeinfo);
903 gchar *procmime_get_tmp_file_name(MimeInfo *mimeinfo)
905 static guint32 id = 0;
910 g_return_val_if_fail(mimeinfo != NULL, NULL);
912 g_snprintf(f_prefix, sizeof(f_prefix), "%08x.", id++);
914 if (MIME_TEXT_HTML == mimeinfo->mime_type)
915 base = "mimetmp.html";
917 base = mimeinfo->filename ? mimeinfo->filename
918 : mimeinfo->name ? mimeinfo->name : "mimetmp";
919 base = g_basename(base);
920 if (*base == '\0') base = "mimetmp";
923 filename = g_strconcat(get_mime_tmp_dir(), G_DIR_SEPARATOR_S,
924 f_prefix, base, NULL);
929 ContentType procmime_scan_mime_type(const gchar *mime_type)
933 if (!strncasecmp(mime_type, "text/html", 9))
934 type = MIME_TEXT_HTML;
935 else if (!strncasecmp(mime_type, "text/enriched", 13))
936 type = MIME_TEXT_ENRICHED;
937 else if (!strncasecmp(mime_type, "text/", 5))
939 else if (!strncasecmp(mime_type, "message/rfc822", 14))
940 type = MIME_MESSAGE_RFC822;
941 else if (!strncasecmp(mime_type, "message/", 8))
943 else if (!strncasecmp(mime_type, "application/octet-stream", 24))
944 type = MIME_APPLICATION_OCTET_STREAM;
945 else if (!strncasecmp(mime_type, "application/", 12))
946 type = MIME_APPLICATION;
947 else if (!strncasecmp(mime_type, "multipart/", 10))
948 type = MIME_MULTIPART;
949 else if (!strncasecmp(mime_type, "image/", 6))
951 else if (!strncasecmp(mime_type, "audio/", 6))
953 else if (!strcasecmp(mime_type, "text"))
961 static GList *mime_type_list = NULL;
963 gchar *procmime_get_mime_type(const gchar *filename)
965 static GHashTable *mime_type_table = NULL;
970 if (!mime_type_table) {
971 mime_type_table = procmime_get_mime_type_table();
972 if (!mime_type_table) return NULL;
975 filename = g_basename(filename);
976 p = strrchr(filename, '.');
979 Xstrdup_a(ext, p + 1, return NULL);
981 mime_type = g_hash_table_lookup(mime_type_table, ext);
985 str = g_strconcat(mime_type->type, "/", mime_type->sub_type,
993 static guint procmime_str_hash(gconstpointer gptr)
995 guint hash_result = 0;
998 for (str = gptr; str && *str; str++) {
999 if (isupper(*str)) hash_result += (*str + ' ');
1000 else hash_result += *str;
1006 static gint procmime_str_equal(gconstpointer gptr1, gconstpointer gptr2)
1008 const char *str1 = gptr1;
1009 const char *str2 = gptr2;
1011 return !strcasecmp(str1, str2);
1014 static GHashTable *procmime_get_mime_type_table(void)
1016 GHashTable *table = NULL;
1018 MimeType *mime_type;
1021 if (!mime_type_list) {
1022 mime_type_list = procmime_get_mime_type_list();
1023 if (!mime_type_list) return NULL;
1026 table = g_hash_table_new(procmime_str_hash, procmime_str_equal);
1028 for (cur = mime_type_list; cur != NULL; cur = cur->next) {
1032 mime_type = (MimeType *)cur->data;
1034 if (!mime_type->extension) continue;
1036 exts = g_strsplit(mime_type->extension, " ", 16);
1037 for (i = 0; exts[i] != NULL; i++) {
1038 /* make the key case insensitive */
1040 /* use previously dup'd key on overwriting */
1041 if (g_hash_table_lookup(table, exts[i]))
1044 key = g_strdup(exts[i]);
1045 g_hash_table_insert(table, key, mime_type);
1053 GList *procmime_get_mime_type_list(void)
1057 gchar buf[BUFFSIZE];
1059 MimeType *mime_type;
1062 return mime_type_list;
1064 if ((fp = fopen("/etc/mime.types", "r")) == NULL) {
1065 if ((fp = fopen(SYSCONFDIR "/mime.types", "r")) == NULL) {
1066 FILE_OP_ERROR(SYSCONFDIR "/mime.types", "fopen");
1071 while (fgets(buf, sizeof(buf), fp) != NULL) {
1072 p = strchr(buf, '#');
1077 while (*p && !isspace(*p)) p++;
1082 delim = strchr(buf, '/');
1083 if (delim == NULL) continue;
1086 mime_type = g_new(MimeType, 1);
1087 mime_type->type = g_strdup(buf);
1088 mime_type->sub_type = g_strdup(delim + 1);
1090 while (*p && isspace(*p)) p++;
1092 mime_type->extension = g_strdup(p);
1094 mime_type->extension = NULL;
1096 list = g_list_append(list, mime_type);
1102 g_warning("Can't read mime.types\n");
1107 EncodingType procmime_get_encoding_for_charset(const gchar *charset)
1111 else if (!strncasecmp(charset, "ISO-2022-", 9) ||
1112 !strcasecmp(charset, "US-ASCII"))
1116 /* return ENC_BASE64; */
1117 /* return ENC_QUOTED_PRINTABLE; */
1120 const gchar *procmime_get_encoding_str(EncodingType encoding)
1122 static const gchar *encoding_str[] = {
1123 "7bit", "8bit", "quoted-printable", "base64", "x-uuencode",
1127 if (encoding >= ENC_7BIT && encoding <= ENC_UNKNOWN)
1128 return encoding_str[encoding];