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(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 if (mimeinfo->main) {
172 mimeinfo = mimeinfo->main;
174 return mimeinfo->next;
177 for (mimeinfo = mimeinfo->parent; mimeinfo != NULL;
178 mimeinfo = mimeinfo->parent) {
180 return mimeinfo->next;
181 if (mimeinfo->main) {
182 mimeinfo = mimeinfo->main;
184 return mimeinfo->next;
191 MimeInfo *procmime_scan_message(MsgInfo *msginfo)
196 g_return_val_if_fail(msginfo != NULL, NULL);
198 if ((fp = procmsg_open_message(msginfo)) == NULL) return NULL;
199 mimeinfo = procmime_scan_mime_header(fp);
202 if (mimeinfo->mime_type != MIME_MULTIPART) {
203 if (fseek(fp, mimeinfo->fpos, SEEK_SET) < 0)
206 if (mimeinfo->mime_type != MIME_TEXT)
207 procmime_scan_multipart_message(mimeinfo, fp);
211 if (prefs_common.auto_check_signatures)
212 rfc2015_check_signature(mimeinfo, fp);
219 void procmime_scan_multipart_message(MimeInfo *mimeinfo, FILE *fp)
223 gint boundary_len = 0;
225 glong fpos, prev_fpos;
228 g_return_if_fail(mimeinfo != NULL);
229 g_return_if_fail(mimeinfo->mime_type != MIME_TEXT);
231 if (mimeinfo->mime_type == MIME_MULTIPART) {
232 g_return_if_fail(mimeinfo->boundary != NULL);
233 g_return_if_fail(mimeinfo->sub == NULL);
235 g_return_if_fail(fp != NULL);
237 boundary = mimeinfo->boundary;
240 boundary_len = strlen(boundary);
242 /* look for first boundary */
243 while ((p = fgets(buf, sizeof(buf), fp)) != NULL)
244 if (IS_BOUNDARY(buf, boundary, boundary_len)) break;
248 if ((fpos = ftell(fp)) < 0) {
253 for (npart = 0;; npart++) {
255 gboolean eom = FALSE;
258 debug_print("prev_fpos: %ld\n", fpos);
260 partinfo = procmime_scan_mime_header(fp);
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(fp);
273 sub->level = partinfo->level + 1;
274 sub->parent = partinfo;
275 sub->main = partinfo;
277 if (sub->level < 8) {
278 if (sub->mime_type == MIME_MULTIPART) {
279 procmime_scan_multipart_message
281 } else if (sub->mime_type == MIME_MESSAGE_RFC822) {
282 fseek(fp, sub->fpos, SEEK_SET);
283 procmime_scan_multipart_message
289 /* look for next boundary */
291 while ((p = fgets(buf, sizeof(buf), fp)) != NULL) {
292 if (IS_BOUNDARY(buf, boundary, boundary_len)) {
293 if (buf[2 + boundary_len] == '-' &&
294 buf[2 + boundary_len + 1] == '-')
300 /* broken MIME, or single part MIME message */
305 debug_print("fpos: %ld\n", fpos);
307 partinfo->size = fpos - prev_fpos - strlen(buf);
308 debug_print("partinfo->size: %d\n", partinfo->size);
309 if (partinfo->sub && !partinfo->sub->sub &&
310 !partinfo->sub->children) {
311 partinfo->sub->size = fpos - partinfo->sub->fpos - strlen(buf);
312 debug_print("partinfo->sub->size: %d\n",
313 partinfo->sub->size);
315 debug_print("boundary: %s\n", buf);
321 void procmime_scan_encoding(MimeInfo *mimeinfo, const gchar *encoding)
325 Xstrdup_a(buf, encoding, return);
327 g_free(mimeinfo->encoding);
329 mimeinfo->encoding = g_strdup(g_strstrip(buf));
330 if (!strcasecmp(buf, "7bit"))
331 mimeinfo->encoding_type = ENC_7BIT;
332 else if (!strcasecmp(buf, "8bit"))
333 mimeinfo->encoding_type = ENC_8BIT;
334 else if (!strcasecmp(buf, "quoted-printable"))
335 mimeinfo->encoding_type = ENC_QUOTED_PRINTABLE;
336 else if (!strcasecmp(buf, "base64"))
337 mimeinfo->encoding_type = ENC_BASE64;
338 else if (!strcasecmp(buf, "x-uuencode"))
339 mimeinfo->encoding_type = ENC_X_UUENCODE;
341 mimeinfo->encoding_type = ENC_UNKNOWN;
345 void procmime_scan_content_type(MimeInfo *mimeinfo, const gchar *content_type)
347 gchar *delim, *p, *cnttype;
350 if (conv_get_current_charset() == C_EUC_JP &&
351 strchr(content_type, '\033')) {
353 len = strlen(content_type) * 2 + 1;
354 Xalloca(buf, len, return);
355 conv_jistoeuc(buf, len, content_type);
357 Xstrdup_a(buf, content_type, return);
359 g_free(mimeinfo->content_type);
360 g_free(mimeinfo->charset);
361 /* g_free(mimeinfo->name); */
362 mimeinfo->content_type = NULL;
363 mimeinfo->charset = NULL;
364 /* mimeinfo->name = NULL; */
366 if ((delim = strchr(buf, ';'))) *delim = '\0';
367 mimeinfo->content_type = cnttype = g_strdup(g_strstrip(buf));
369 mimeinfo->mime_type = procmime_scan_mime_type(cnttype);
378 if ((delim = strchr(p, ';'))) *delim = '\0';
380 if (!(eq = strchr(p, '='))) break;
389 extract_quote(value, '"');
391 eliminate_parenthesis(value, '(', ')');
396 if (!strcasecmp(attr, "charset"))
397 mimeinfo->charset = g_strdup(value);
398 else if (!strcasecmp(attr, "name")) {
402 len = strlen(value) + 1;
403 Xalloca(tmp, len, return);
404 conv_unmime_header(tmp, len, value, NULL);
405 g_free(mimeinfo->name);
406 /*pgp signatures should NOT have a name */
407 if (mimeinfo->content_type
408 && strcasecmp(mimeinfo->content_type, "application/pgp-signature"))
409 mimeinfo->name = g_strdup(tmp);
410 } else if (!strcasecmp(attr, "boundary"))
411 mimeinfo->boundary = g_strdup(value);
418 if (mimeinfo->mime_type == MIME_MULTIPART && !mimeinfo->boundary)
419 mimeinfo->mime_type = MIME_TEXT;
422 void procmime_scan_content_disposition(MimeInfo *mimeinfo,
423 const gchar *content_disposition)
425 gchar *delim, *p, *dispos;
428 if (conv_get_current_charset() == C_EUC_JP &&
429 strchr(content_disposition, '\033')) {
431 len = strlen(content_disposition) * 2 + 1;
432 Xalloca(buf, len, return);
433 conv_jistoeuc(buf, len, content_disposition);
435 Xstrdup_a(buf, content_disposition, return);
437 if ((delim = strchr(buf, ';'))) *delim = '\0';
438 mimeinfo->content_disposition = dispos = g_strdup(g_strstrip(buf));
447 if ((delim = strchr(p, ';'))) *delim = '\0';
449 if (!(eq = strchr(p, '='))) break;
458 extract_quote(value, '"');
460 eliminate_parenthesis(value, '(', ')');
465 if (!strcasecmp(attr, "filename")) {
469 len = strlen(value) + 1;
470 Xalloca(tmp, len, return);
471 conv_unmime_header(tmp, len, value, NULL);
472 g_free(mimeinfo->filename);
473 /*pgp signatures should NOT have a name */
474 if (mimeinfo->content_type
475 && strcasecmp(mimeinfo->content_type, "application/pgp-signature"))
476 mimeinfo->filename = g_strdup(tmp);
486 void procmime_scan_content_description(MimeInfo *mimeinfo,
487 const gchar *content_description)
494 if (conv_get_current_charset() == C_EUC_JP &&
495 strchr(content_description, '\033')) {
497 len = strlen(content_description) * 2 + 1;
498 Xalloca(buf, len, return);
499 conv_jistoeuc(buf, len, content_description);
501 Xstrdup_a(buf, content_description, return);
503 blen = strlen(buf) + 1;
504 Xalloca(tmp, blen, return);
505 conv_unmime_header(tmp, blen, buf, NULL);
506 g_free(mimeinfo->name);
507 /*pgp signatures should NOT have a name */
508 if (mimeinfo->content_type
509 && strcasecmp(mimeinfo->content_type, "application/pgp-signature"))
510 mimeinfo->name = g_strdup(tmp);
513 void procmime_scan_subject(MimeInfo *mimeinfo,
514 const gchar *subject)
521 if (conv_get_current_charset() == C_EUC_JP &&
522 strchr(subject, '\033')) {
524 len = strlen(subject) * 2 + 1;
525 Xalloca(buf, len, return);
526 conv_jistoeuc(buf, len, subject);
528 Xstrdup_a(buf, subject, return);
530 blen = strlen(buf) + 1;
531 Xalloca(tmp, blen, return);
532 conv_unmime_header(tmp, blen, buf, NULL);
533 g_free(mimeinfo->name);
534 mimeinfo->name = g_strdup(tmp);
539 H_CONTENT_TRANSFER_ENCODING = 0,
541 H_CONTENT_DISPOSITION = 2,
542 H_CONTENT_DESCRIPTION = 3,
546 MimeInfo *procmime_scan_mime_header(FILE *fp)
548 static HeaderEntry hentry[] = {{"Content-Transfer-Encoding:",
550 {"Content-Type:", NULL, TRUE},
551 {"Content-Disposition:",
553 {"Content-description:",
557 {NULL, NULL, FALSE}};
563 g_return_val_if_fail(fp != NULL, NULL);
565 mimeinfo = procmime_mimeinfo_new();
566 mimeinfo->mime_type = MIME_TEXT;
567 mimeinfo->encoding_type = ENC_7BIT;
568 mimeinfo->fpos = ftell(fp);
570 while ((hnum = procheader_get_one_field(buf, sizeof(buf), fp, hentry))
574 if (H_CONTENT_TRANSFER_ENCODING == hnum) {
575 procmime_scan_encoding
576 (mimeinfo, buf + strlen(hp->name));
577 } else if (H_CONTENT_TYPE == hnum) {
578 procmime_scan_content_type
579 (mimeinfo, buf + strlen(hp->name));
580 } else if (H_CONTENT_DISPOSITION == hnum) {
581 procmime_scan_content_disposition
582 (mimeinfo, buf + strlen(hp->name));
583 } else if (H_CONTENT_DESCRIPTION == hnum) {
584 procmime_scan_content_description
585 (mimeinfo, buf + strlen(hp->name));
586 } else if (H_SUBJECT == hnum) {
587 procmime_scan_subject
588 (mimeinfo, buf + strlen(hp->name));
592 if (mimeinfo->mime_type == MIME_APPLICATION_OCTET_STREAM &&
595 type = procmime_get_mime_type(mimeinfo->name);
597 mimeinfo->mime_type = procmime_scan_mime_type(type);
600 if (!mimeinfo->content_type)
601 mimeinfo->content_type = g_strdup("text/plain");
606 FILE *procmime_decode_content(FILE *outfp, FILE *infp, MimeInfo *mimeinfo)
609 gchar *boundary = NULL;
610 gint boundary_len = 0;
611 gboolean tmp_file = FALSE;
613 g_return_val_if_fail(infp != NULL, NULL);
614 g_return_val_if_fail(mimeinfo != NULL, NULL);
617 outfp = my_tmpfile();
625 if (mimeinfo->parent && mimeinfo->parent->boundary) {
626 boundary = mimeinfo->parent->boundary;
627 boundary_len = strlen(boundary);
630 if (mimeinfo->encoding_type == ENC_QUOTED_PRINTABLE) {
631 while (fgets(buf, sizeof(buf), infp) != NULL &&
633 !IS_BOUNDARY(buf, boundary, boundary_len))) {
635 len = unmime_quoted_printable_line(buf);
636 fwrite(buf, len, 1, outfp);
638 } else if (mimeinfo->encoding_type == ENC_BASE64) {
639 gchar outbuf[BUFFSIZE];
641 Base64Decoder *decoder;
643 decoder = base64_decoder_new();
644 while (fgets(buf, sizeof(buf), infp) != NULL &&
646 !IS_BOUNDARY(buf, boundary, boundary_len))) {
647 len = base64_decoder_decode(decoder, buf, outbuf);
649 g_warning("Bad BASE64 content\n");
652 fwrite(outbuf, sizeof(gchar), len, outfp);
654 base64_decoder_free(decoder);
655 } else if (mimeinfo->encoding_type == ENC_X_UUENCODE) {
656 gchar outbuf[BUFFSIZE];
658 gboolean flag = FALSE;
660 while (fgets(buf, sizeof(buf), infp) != NULL &&
662 !IS_BOUNDARY(buf, boundary, boundary_len))) {
663 if(!flag && strncmp(buf,"begin ", 6)) continue;
666 len = fromuutobits(outbuf, buf);
669 g_warning("Bad UUENCODE content(%d)\n", len);
672 fwrite(outbuf, sizeof(gchar), len, outfp);
677 while (fgets(buf, sizeof(buf), infp) != NULL &&
679 !IS_BOUNDARY(buf, boundary, boundary_len))) {
684 if (tmp_file) rewind(outfp);
688 gint procmime_get_part(const gchar *outfile, const gchar *infile,
694 g_return_val_if_fail(outfile != NULL, -1);
695 g_return_val_if_fail(infile != NULL, -1);
696 g_return_val_if_fail(mimeinfo != NULL, -1);
698 if ((infp = fopen(infile, "rb")) == NULL) {
699 FILE_OP_ERROR(infile, "fopen");
702 if (fseek(infp, mimeinfo->fpos, SEEK_SET) < 0) {
703 FILE_OP_ERROR(infile, "fseek");
707 if ((outfp = fopen(outfile, "wb")) == NULL) {
708 FILE_OP_ERROR(outfile, "fopen");
713 while (fgets(buf, sizeof(buf), infp) != NULL)
714 if (buf[0] == '\r' || buf[0] == '\n') break;
716 procmime_decode_content(outfp, infp, mimeinfo);
719 if (fclose(outfp) == EOF) {
720 FILE_OP_ERROR(outfile, "fclose");
728 struct ContentRenderer {
733 static GList * renderer_list = NULL;
735 static struct ContentRenderer *
736 content_renderer_new(char * content_type, char * renderer)
738 struct ContentRenderer * cr;
740 cr = g_new(struct ContentRenderer, 1);
744 cr->content_type = g_strdup(content_type);
745 cr->renderer = g_strdup(renderer);
750 static void content_renderer_free(struct ContentRenderer * cr)
752 g_free(cr->content_type);
753 g_free(cr->renderer);
757 void renderer_read_config(void)
763 g_list_foreach(renderer_list, (GFunc) content_renderer_free, NULL);
764 renderer_list = NULL;
766 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, RENDERER_RC, NULL);
767 f = fopen(rcpath, "rb");
773 while (fgets(buf, BUFFSIZE, f)) {
775 struct ContentRenderer * cr;
778 p = strchr(buf, ' ');
783 cr = content_renderer_new(buf, p + 1);
787 renderer_list = g_list_append(renderer_list, cr);
793 void renderer_write_config(void)
799 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, RENDERER_RC, NULL);
801 if ((pfile = prefs_write_open(rcpath)) == NULL) {
802 g_warning(_("failed to write configuration to file\n"));
809 for(cur = renderer_list ; cur != NULL ; cur = cur->next) {
810 struct ContentRenderer * renderer;
811 renderer = cur->data;
812 fprintf(pfile->fp, "%s %s\n", renderer->content_type,
816 if (prefs_write_close(pfile) < 0) {
817 g_warning(_("failed to write configuration to file\n"));
822 FILE *procmime_get_text_content(MimeInfo *mimeinfo, FILE *infp)
826 gboolean conv_fail = FALSE;
829 struct ContentRenderer * renderer;
832 g_return_val_if_fail(mimeinfo != NULL, NULL);
833 g_return_val_if_fail(infp != NULL, NULL);
834 g_return_val_if_fail(mimeinfo->mime_type == MIME_TEXT ||
835 mimeinfo->mime_type == MIME_TEXT_HTML ||
836 mimeinfo->mime_type == MIME_TEXT_ENRICHED, NULL);
838 if (fseek(infp, mimeinfo->fpos, SEEK_SET) < 0) {
843 while (fgets(buf, sizeof(buf), infp) != NULL)
844 if (buf[0] == '\r' || buf[0] == '\n') break;
846 tmpfp = procmime_decode_content(NULL, infp, mimeinfo);
850 if ((outfp = my_tmpfile()) == NULL) {
856 src_codeset = prefs_common.force_charset
857 ? prefs_common.force_charset : mimeinfo->charset;
861 for(cur = renderer_list ; cur != NULL ; cur = cur->next) {
862 struct ContentRenderer * cr;
864 if (g_strcasecmp(cr->content_type,
865 mimeinfo->content_type) == 0) {
871 if (renderer != NULL) {
877 dup2(fileno(outfp), 1);
879 p = popen(renderer->renderer, "w");
884 fread(buf, sizeof(char), sizeof(buf),
886 fwrite(buf, sizeof(char), count, p);
891 } else if (mimeinfo->mime_type == MIME_TEXT) {
892 while (fgets(buf, sizeof(buf), tmpfp) != NULL) {
893 str = conv_codeset_strdup(buf, src_codeset, NULL);
902 } else if (mimeinfo->mime_type == MIME_TEXT_HTML) {
906 conv = conv_code_converter_new(src_codeset);
907 parser = html_parser_new(tmpfp, conv);
908 while ((str = html_parse(parser)) != NULL) {
911 html_parser_destroy(parser);
912 conv_code_converter_destroy(conv);
913 } else if (mimeinfo->mime_type == MIME_TEXT_ENRICHED) {
917 conv = conv_code_converter_new(src_codeset);
918 parser = ertf_parser_new(tmpfp, conv);
919 while ((str = ertf_parse(parser)) != NULL) {
922 ertf_parser_destroy(parser);
923 conv_code_converter_destroy(conv);
927 g_warning(_("procmime_get_text_content(): Code conversion failed.\n"));
935 /* search the first text part of (multipart) MIME message,
936 decode, convert it and output to outfp. */
937 FILE *procmime_get_first_text_content(MsgInfo *msginfo)
939 FILE *infp, *outfp = NULL;
940 MimeInfo *mimeinfo, *partinfo;
942 g_return_val_if_fail(msginfo != NULL, NULL);
944 mimeinfo = procmime_scan_message(msginfo);
945 if (!mimeinfo) return NULL;
947 if ((infp = procmsg_open_message(msginfo)) == NULL) {
948 procmime_mimeinfo_free_all(mimeinfo);
953 while (partinfo && partinfo->mime_type != MIME_TEXT)
954 partinfo = procmime_mimeinfo_next(partinfo);
957 while (partinfo && partinfo->mime_type != MIME_TEXT_HTML &&
958 partinfo->mime_type != MIME_TEXT_ENRICHED)
959 partinfo = procmime_mimeinfo_next(partinfo);
964 outfp = procmime_get_text_content(partinfo, infp);
967 procmime_mimeinfo_free_all(mimeinfo);
972 gboolean procmime_find_string_part(MimeInfo *mimeinfo, const gchar *filename,
973 const gchar *str, gboolean case_sens)
978 gchar *(* StrFindFunc) (const gchar *haystack, const gchar *needle);
980 g_return_val_if_fail(mimeinfo != NULL, FALSE);
981 g_return_val_if_fail(mimeinfo->mime_type == MIME_TEXT ||
982 mimeinfo->mime_type == MIME_TEXT_HTML ||
983 mimeinfo->mime_type == MIME_TEXT_ENRICHED, FALSE);
984 g_return_val_if_fail(str != NULL, FALSE);
986 if ((infp = fopen(filename, "rb")) == NULL) {
987 FILE_OP_ERROR(filename, "fopen");
991 outfp = procmime_get_text_content(mimeinfo, infp);
998 StrFindFunc = strstr;
1000 StrFindFunc = strcasestr;
1002 while (fgets(buf, sizeof(buf), outfp) != NULL) {
1003 if (StrFindFunc(buf, str) != NULL) {
1014 gboolean procmime_find_string(MsgInfo *msginfo, const gchar *str,
1020 gboolean found = FALSE;
1022 g_return_val_if_fail(msginfo != NULL, FALSE);
1023 g_return_val_if_fail(str != NULL, FALSE);
1025 filename = procmsg_get_message_file(msginfo);
1026 if (!filename) return FALSE;
1027 mimeinfo = procmime_scan_message(msginfo);
1029 for (partinfo = mimeinfo; partinfo != NULL;
1030 partinfo = procmime_mimeinfo_next(partinfo)) {
1031 if (partinfo->mime_type == MIME_TEXT ||
1032 partinfo->mime_type == MIME_TEXT_HTML ||
1033 partinfo->mime_type == MIME_TEXT_ENRICHED) {
1034 if (procmime_find_string_part
1035 (partinfo, filename, str, case_sens) == TRUE) {
1042 procmime_mimeinfo_free_all(mimeinfo);
1048 gchar *procmime_get_tmp_file_name(MimeInfo *mimeinfo)
1050 static guint32 id = 0;
1055 g_return_val_if_fail(mimeinfo != NULL, NULL);
1057 g_snprintf(f_prefix, sizeof(f_prefix), "%08x.", id++);
1059 if (MIME_TEXT_HTML == mimeinfo->mime_type)
1060 base = "mimetmp.html";
1062 base = mimeinfo->filename ? mimeinfo->filename
1063 : mimeinfo->name ? mimeinfo->name : "mimetmp";
1064 base = g_basename(base);
1065 if (*base == '\0') base = "mimetmp";
1066 Xstrdup_a(base, base, return NULL);
1067 subst_for_filename(base);
1070 filename = g_strconcat(get_mime_tmp_dir(), G_DIR_SEPARATOR_S,
1071 f_prefix, base, NULL);
1076 ContentType procmime_scan_mime_type(const gchar *mime_type)
1080 if (!strncasecmp(mime_type, "text/html", 9))
1081 type = MIME_TEXT_HTML;
1082 else if (!strncasecmp(mime_type, "text/enriched", 13))
1083 type = MIME_TEXT_ENRICHED;
1084 else if (!strncasecmp(mime_type, "text/", 5))
1086 else if (!strncasecmp(mime_type, "message/rfc822", 14))
1087 type = MIME_MESSAGE_RFC822;
1088 else if (!strncasecmp(mime_type, "message/", 8))
1090 else if (!strncasecmp(mime_type, "application/octet-stream", 24))
1091 type = MIME_APPLICATION_OCTET_STREAM;
1092 else if (!strncasecmp(mime_type, "application/", 12))
1093 type = MIME_APPLICATION;
1094 else if (!strncasecmp(mime_type, "multipart/", 10))
1095 type = MIME_MULTIPART;
1096 else if (!strncasecmp(mime_type, "image/", 6))
1098 else if (!strncasecmp(mime_type, "audio/", 6))
1100 else if (!strcasecmp(mime_type, "text"))
1103 type = MIME_UNKNOWN;
1108 static GList *mime_type_list = NULL;
1110 gchar *procmime_get_mime_type(const gchar *filename)
1112 static GHashTable *mime_type_table = NULL;
1113 MimeType *mime_type;
1117 if (!mime_type_table) {
1118 mime_type_table = procmime_get_mime_type_table();
1119 if (!mime_type_table) return NULL;
1122 filename = g_basename(filename);
1123 p = strrchr(filename, '.');
1124 if (!p) return NULL;
1126 Xstrdup_a(ext, p + 1, return NULL);
1128 mime_type = g_hash_table_lookup(mime_type_table, ext);
1132 str = g_strconcat(mime_type->type, "/", mime_type->sub_type,
1140 static guint procmime_str_hash(gconstpointer gptr)
1142 guint hash_result = 0;
1145 for (str = gptr; str && *str; str++) {
1146 if (isupper(*str)) hash_result += (*str + ' ');
1147 else hash_result += *str;
1153 static gint procmime_str_equal(gconstpointer gptr1, gconstpointer gptr2)
1155 const char *str1 = gptr1;
1156 const char *str2 = gptr2;
1158 return !strcasecmp(str1, str2);
1161 static GHashTable *procmime_get_mime_type_table(void)
1163 GHashTable *table = NULL;
1165 MimeType *mime_type;
1168 if (!mime_type_list) {
1169 mime_type_list = procmime_get_mime_type_list();
1170 if (!mime_type_list) return NULL;
1173 table = g_hash_table_new(procmime_str_hash, procmime_str_equal);
1175 for (cur = mime_type_list; cur != NULL; cur = cur->next) {
1179 mime_type = (MimeType *)cur->data;
1181 if (!mime_type->extension) continue;
1183 exts = g_strsplit(mime_type->extension, " ", 16);
1184 for (i = 0; exts[i] != NULL; i++) {
1185 /* make the key case insensitive */
1187 /* use previously dup'd key on overwriting */
1188 if (g_hash_table_lookup(table, exts[i]))
1191 key = g_strdup(exts[i]);
1192 g_hash_table_insert(table, key, mime_type);
1200 GList *procmime_get_mime_type_list(void)
1204 gchar buf[BUFFSIZE];
1206 MimeType *mime_type;
1209 return mime_type_list;
1211 if ((fp = fopen("/etc/mime.types", "rb")) == NULL) {
1212 if ((fp = fopen(SYSCONFDIR "/mime.types", "rb")) == NULL) {
1213 FILE_OP_ERROR(SYSCONFDIR "/mime.types", "fopen");
1218 while (fgets(buf, sizeof(buf), fp) != NULL) {
1219 p = strchr(buf, '#');
1224 while (*p && !isspace(*p)) p++;
1229 delim = strchr(buf, '/');
1230 if (delim == NULL) continue;
1233 mime_type = g_new(MimeType, 1);
1234 mime_type->type = g_strdup(buf);
1235 mime_type->sub_type = g_strdup(delim + 1);
1237 while (*p && isspace(*p)) p++;
1239 mime_type->extension = g_strdup(p);
1241 mime_type->extension = NULL;
1243 list = g_list_append(list, mime_type);
1249 g_warning("Can't read mime.types\n");
1254 EncodingType procmime_get_encoding_for_charset(const gchar *charset)
1258 else if (!strncasecmp(charset, "ISO-2022-", 9) ||
1259 !strcasecmp(charset, "US-ASCII"))
1263 /* return ENC_BASE64; */
1264 /* return ENC_QUOTED_PRINTABLE; */
1267 EncodingType procmime_get_encoding_for_file(const gchar *file)
1273 if ((fp = fopen(file, "rb")) == NULL) {
1274 FILE_OP_ERROR(file, "fopen");
1278 while ((len = fread(buf, sizeof(gchar), sizeof(buf), fp)) > 0) {
1282 for (p = buf, i = 0; i < len; p++, i++) {
1294 struct EncodingTable
1297 EncodingType enc_type;
1300 struct EncodingTable encoding_table[] = {
1303 {"binary", ENC_BINARY},
1304 {"quoted-printable", ENC_QUOTED_PRINTABLE},
1305 {"base64", ENC_BASE64},
1306 {"x-uuencode", ENC_UNKNOWN},
1307 {NULL, ENC_UNKNOWN},
1310 const gchar *procmime_get_encoding_str(EncodingType encoding)
1312 struct EncodingTable *enc_table;
1314 for (enc_table = encoding_table; enc_table->str != NULL; enc_table++) {
1315 if (enc_table->enc_type == encoding)
1316 return enc_table->str;