sync with sylpheed 0.5.1cvs1-3
[claws.git] / src / procmime.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2001 Hiroyuki Yamamoto
4  *
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.
9  *
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.
14  *
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.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include "defs.h"
25
26 #include <glib.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <locale.h>
30 #include <ctype.h>
31
32 #include "intl.h"
33 #include "procmime.h"
34 #include "procheader.h"
35 #include "base64.h"
36 #include "uuencode.h"
37 #include "unmime.h"
38 #include "codeconv.h"
39 #include "utils.h"
40 #include "prefs_common.h"
41
42 #if USE_GPGME
43 #  include "rfc2015.h"
44 #endif
45
46 static GHashTable *procmime_get_mime_type_table (void);
47
48 MimeInfo *procmime_mimeinfo_new(void)
49 {
50         MimeInfo *mimeinfo;
51
52         mimeinfo = g_new0(MimeInfo, 1);
53         mimeinfo->mime_type     = MIME_UNKNOWN;
54         mimeinfo->encoding_type = ENC_UNKNOWN;
55
56         return mimeinfo;
57 }
58
59 void procmime_mimeinfo_free(MimeInfo *mimeinfo)
60 {
61         if (!mimeinfo) return;
62
63         g_free(mimeinfo->encoding);
64         g_free(mimeinfo->content_type);
65         g_free(mimeinfo->charset);
66         g_free(mimeinfo->name);
67         g_free(mimeinfo->boundary);
68         g_free(mimeinfo->content_disposition);
69         g_free(mimeinfo->filename);
70 #if USE_GPGME
71         g_free(mimeinfo->plaintextfile);
72         g_free(mimeinfo->sigstatus);
73         g_free(mimeinfo->sigstatus_full);
74 #endif
75
76         procmime_mimeinfo_free(mimeinfo->sub);
77
78         g_free(mimeinfo);
79 }
80
81 void procmime_mimeinfo_free_all(MimeInfo *mimeinfo)
82 {
83         while (mimeinfo != NULL) {
84                 MimeInfo *next;
85
86                 g_free(mimeinfo->encoding);
87                 g_free(mimeinfo->content_type);
88                 g_free(mimeinfo->charset);
89                 g_free(mimeinfo->name);
90                 g_free(mimeinfo->boundary);
91                 g_free(mimeinfo->content_disposition);
92                 g_free(mimeinfo->filename);
93 #if USE_GPGME
94                 g_free(mimeinfo->plaintextfile);
95                 g_free(mimeinfo->sigstatus);
96                 g_free(mimeinfo->sigstatus_full);
97 #endif
98
99                 procmime_mimeinfo_free_all(mimeinfo->sub);
100                 procmime_mimeinfo_free_all(mimeinfo->children);
101 #if USE_GPGME
102                 procmime_mimeinfo_free_all(mimeinfo->plaintext);
103 #endif
104
105                 next = mimeinfo->next;
106                 g_free(mimeinfo);
107                 mimeinfo = next;
108         }
109 }
110
111 MimeInfo *procmime_mimeinfo_insert(MimeInfo *parent, MimeInfo *mimeinfo)
112 {
113         MimeInfo *child = parent->children;
114
115         if (!child)
116                 parent->children = mimeinfo;
117         else {
118                 while (child->next != NULL)
119                         child = child->next;
120
121                 child->next = mimeinfo;
122         }
123
124         mimeinfo->parent = parent;
125         mimeinfo->level = parent->level + 1;
126
127         return mimeinfo;
128 }
129
130 void procmime_mimeinfo_replace(MimeInfo *old, MimeInfo *new)
131 {
132         MimeInfo *parent = old->parent;
133         MimeInfo *child;
134
135         if (!parent) {
136                 g_warning("oops: Not top message");
137                 return;
138         }
139         if (new->next) {
140                 g_message("oops: new child should not have a sibling");
141                 return;
142         }
143
144         for (child = parent->children; child && child != old;
145              child = child->next)
146                 ;
147         if (!child) {
148                 g_warning("oops: parent can't find it's own child");
149                 return;
150         }
151         procmime_mimeinfo_free_all(old);
152
153         if (child == parent->children) {
154                 new->next = parent->children->next;
155                 parent->children = new;
156         } else {
157                 new->next = child->next;
158                 child = new;
159         }
160 }
161
162 MimeInfo *procmime_scan_message(MsgInfo *msginfo)
163 {
164         FILE *fp;
165         MimeInfo *mimeinfo;
166
167         g_return_val_if_fail(msginfo != NULL, NULL);
168
169         if ((fp = procmsg_open_message(msginfo)) == NULL) return NULL;
170         mimeinfo = procmime_scan_mime_header(fp);
171
172         if (mimeinfo) {
173                 if (mimeinfo->mime_type != MIME_MULTIPART) {
174                         if (fseek(fp, mimeinfo->fpos, SEEK_SET) < 0)
175                                 perror("fseek");
176                 }
177                 if (mimeinfo->mime_type != MIME_TEXT)
178                         procmime_scan_multipart_message(mimeinfo, fp);
179         }
180
181 #if USE_GPGME
182         if (prefs_common.auto_check_signatures)
183                 rfc2015_check_signature(mimeinfo, fp);
184 #endif
185         fclose(fp);
186
187         return mimeinfo;
188 }
189
190 void procmime_scan_multipart_message(MimeInfo *mimeinfo, FILE *fp)
191 {
192         gchar *p;
193         gchar *boundary;
194         gint boundary_len = 0;
195         gchar buf[BUFFSIZE];
196         glong fpos, prev_fpos;
197         gint npart;
198
199         g_return_if_fail(mimeinfo != NULL);
200         g_return_if_fail(mimeinfo->mime_type != MIME_TEXT);
201
202         if (mimeinfo->mime_type == MIME_MULTIPART) {
203                 g_return_if_fail(mimeinfo->boundary != NULL);
204                 g_return_if_fail(mimeinfo->sub == NULL);
205         }
206         g_return_if_fail(fp != NULL);
207
208         boundary = mimeinfo->boundary;
209
210         if (boundary) {
211                 boundary_len = strlen(boundary);
212
213                 /* look for first boundary */
214                 while ((p = fgets(buf, sizeof(buf), fp)) != NULL)
215                         if (IS_BOUNDARY(buf, boundary, boundary_len)) break;
216                 if (!p) return;
217         }
218
219         if ((fpos = ftell(fp)) < 0) {
220                 perror("ftell");
221                 return;
222         }
223
224         for (npart = 0;; npart++) {
225                 MimeInfo *partinfo;
226                 gboolean eom = FALSE;
227
228                 prev_fpos = fpos;
229
230                 partinfo = procmime_scan_mime_header(fp);
231                 if (!partinfo) break;
232                 procmime_mimeinfo_insert(mimeinfo, partinfo);
233
234                 if (partinfo->mime_type == MIME_MULTIPART) {
235                         if (partinfo->level < 8)
236                                 procmime_scan_multipart_message(partinfo, fp);
237                 } else if (partinfo->mime_type == MIME_MESSAGE_RFC822) {
238                         MimeInfo *sub;
239
240                         partinfo->sub = sub = procmime_scan_mime_header(fp);
241                         if (!sub) break;
242                         sub->level = partinfo->level + 1;
243                         sub->parent = partinfo->parent;
244                         sub->main = partinfo;
245
246                         if (sub->mime_type == MIME_MULTIPART) {
247                                 if (sub->level < 8)
248                                         procmime_scan_multipart_message
249                                                 (sub, fp);
250                         }
251                 }
252
253                 /* look for next boundary */
254                 buf[0] = '\0';
255                 while ((p = fgets(buf, sizeof(buf), fp)) != NULL) {
256                         if (IS_BOUNDARY(buf, boundary, boundary_len)) {
257                                 if (buf[2 + boundary_len]     == '-' &&
258                                     buf[2 + boundary_len + 1] == '-')
259                                         eom = TRUE;
260                                 break;
261                         }
262                 }
263                 if (p == NULL)
264                         eom = TRUE;     /* broken MIME message */
265                 fpos = ftell(fp);
266
267                 partinfo->size = fpos - prev_fpos - strlen(buf);
268
269                 if (eom) break;
270         }
271         /*g_message ("** at " __PRETTY_FUNCTION__ ":%d:", __LINE__);*/
272 }
273
274 void procmime_scan_encoding(MimeInfo *mimeinfo, const gchar *encoding)
275 {
276         gchar *buf;
277
278         Xstrdup_a(buf, encoding, return);
279
280         g_free(mimeinfo->encoding);
281
282         mimeinfo->encoding = g_strdup(g_strstrip(buf));
283         if (!strcasecmp(buf, "7bit"))
284                 mimeinfo->encoding_type = ENC_7BIT;
285         else if (!strcasecmp(buf, "8bit"))
286                 mimeinfo->encoding_type = ENC_8BIT;
287         else if (!strcasecmp(buf, "quoted-printable"))
288                 mimeinfo->encoding_type = ENC_QUOTED_PRINTABLE;
289         else if (!strcasecmp(buf, "base64"))
290                 mimeinfo->encoding_type = ENC_BASE64;
291         else if (!strcasecmp(buf, "x-uuencode"))
292                 mimeinfo->encoding_type = ENC_X_UUENCODE;
293         else
294                 mimeinfo->encoding_type = ENC_UNKNOWN;
295
296 }
297
298 void procmime_scan_content_type(MimeInfo *mimeinfo, const gchar *content_type)
299 {
300         gchar *delim, *p, *cnttype;
301         gchar *buf;
302
303         if (conv_get_current_charset() == C_EUC_JP &&
304             strchr(content_type, '\007')) {
305                 gint len;
306                 len = strlen(content_type) * 2 + 1;
307                 Xalloca(buf, len, return);
308                 conv_jistoeuc(buf, len, content_type);
309         } else
310                 Xstrdup_a(buf, content_type, return);
311
312         g_free(mimeinfo->content_type);
313         g_free(mimeinfo->charset);
314         g_free(mimeinfo->name);
315         mimeinfo->content_type = NULL;
316         mimeinfo->charset      = NULL;
317         mimeinfo->name         = NULL;
318
319         if ((delim = strchr(buf, ';'))) *delim = '\0';
320         mimeinfo->content_type = cnttype = g_strdup(g_strstrip(buf));
321
322         if (!strncasecmp(cnttype, "text/html", 9))
323                 mimeinfo->mime_type = MIME_TEXT_HTML;
324         else if (!strncasecmp(cnttype, "text/", 5))
325                 mimeinfo->mime_type = MIME_TEXT;
326         else if (!strncasecmp(cnttype, "message/rfc822", 14))
327                 mimeinfo->mime_type = MIME_MESSAGE_RFC822;
328         else if (!strncasecmp(cnttype, "message/", 8))
329                 mimeinfo->mime_type = MIME_TEXT;
330         else if (!strncasecmp(cnttype, "application/octet-stream", 24))
331                 mimeinfo->mime_type = MIME_APPLICATION_OCTET_STREAM;
332         else if (!strncasecmp(cnttype, "application/", 12))
333                 mimeinfo->mime_type = MIME_APPLICATION;
334         else if (!strncasecmp(cnttype, "multipart/", 10))
335                 mimeinfo->mime_type = MIME_MULTIPART;
336         else if (!strncasecmp(cnttype, "image/", 6))
337                 mimeinfo->mime_type = MIME_IMAGE;
338         else if (!strncasecmp(cnttype, "audio/", 6))
339                 mimeinfo->mime_type = MIME_AUDIO;
340         else if (!strcasecmp(cnttype, "text"))
341                 mimeinfo->mime_type = MIME_TEXT;
342         else
343                 mimeinfo->mime_type = MIME_UNKNOWN;
344
345         if (!delim) return;
346         p = delim + 1;
347
348         for (;;) {
349                 gchar *eq;
350                 gchar *attr, *value;
351
352                 if ((delim = strchr(p, ';'))) *delim = '\0';
353
354                 if (!(eq = strchr(p, '='))) break;
355
356                 *eq = '\0';
357                 attr = p;
358                 g_strstrip(attr);
359                 value = eq + 1;
360                 g_strstrip(value);
361
362                 if (*value == '"')
363                         extract_quote(value, '"');
364                 else {
365                         eliminate_parenthesis(value, '(', ')');
366                         g_strstrip(value);
367                 }
368
369                 if (*value) {
370                         if (!strcasecmp(attr, "charset"))
371                                 mimeinfo->charset = g_strdup(value);
372                         else if (!strcasecmp(attr, "name")) {
373                                 gchar *tmp;
374                                 size_t len;
375
376                                 len = strlen(value) + 1;
377                                 Xalloca(tmp, len, return);
378                                 conv_unmime_header(tmp, len, value, NULL);
379                                 mimeinfo->name = g_strdup(tmp);
380                         } else if (!strcasecmp(attr, "boundary"))
381                                 mimeinfo->boundary = g_strdup(value);
382                 }
383
384                 if (!delim) break;
385                 p = delim + 1;
386         }
387
388         if (mimeinfo->mime_type == MIME_MULTIPART && !mimeinfo->boundary)
389                 mimeinfo->mime_type = MIME_TEXT;
390 }
391
392 void procmime_scan_content_disposition(MimeInfo *mimeinfo,
393                                        const gchar *content_disposition)
394 {
395         gchar *delim, *p, *dispos;
396         gchar *buf;
397
398         if (conv_get_current_charset() == C_EUC_JP &&
399             strchr(content_disposition, '\007')) {
400                 gint len;
401                 len = strlen(content_disposition) * 2 + 1;
402                 Xalloca(buf, len, return);
403                 conv_jistoeuc(buf, len, content_disposition);
404         } else
405                 Xstrdup_a(buf, content_disposition, return);
406
407         if ((delim = strchr(buf, ';'))) *delim = '\0';
408         mimeinfo->content_disposition = dispos = g_strdup(g_strstrip(buf));
409
410         if (!delim) return;
411         p = delim + 1;
412
413         for (;;) {
414                 gchar *eq;
415                 gchar *attr, *value;
416
417                 if ((delim = strchr(p, ';'))) *delim = '\0';
418
419                 if (!(eq = strchr(p, '='))) break;
420
421                 *eq = '\0';
422                 attr = p;
423                 g_strstrip(attr);
424                 value = eq + 1;
425                 g_strstrip(value);
426
427                 if (*value == '"')
428                         extract_quote(value, '"');
429                 else {
430                         eliminate_parenthesis(value, '(', ')');
431                         g_strstrip(value);
432                 }
433
434                 if (*value) {
435                         if (!strcasecmp(attr, "filename")) {
436                                 gchar *tmp;
437                                 size_t len;
438
439                                 len = strlen(value) + 1;
440                                 Xalloca(tmp, len, return);
441                                 conv_unmime_header(tmp, len, value, NULL);
442                                 g_free(mimeinfo->filename);
443                                 mimeinfo->filename = g_strdup(tmp);
444                                 break;
445                         }
446                 }
447
448                 if (!delim) break;
449                 p = delim + 1;
450         }
451 }
452
453 enum
454 {
455         H_CONTENT_TRANSFER_ENCODING = 0,
456         H_CONTENT_TYPE              = 1,
457         H_CONTENT_DISPOSITION       = 2
458 };
459
460 MimeInfo *procmime_scan_mime_header(FILE *fp)
461 {
462         static HeaderEntry hentry[] = {{"Content-Transfer-Encoding:",
463                                                           NULL, FALSE},
464                                        {"Content-Type:", NULL, TRUE},
465                                        {"Content-Disposition:",
466                                                           NULL, TRUE},
467                                        {NULL,             NULL, FALSE}};
468         gchar buf[BUFFSIZE];
469         gint hnum;
470         HeaderEntry *hp;
471         MimeInfo *mimeinfo;
472
473         g_return_val_if_fail(fp != NULL, NULL);
474
475         mimeinfo = procmime_mimeinfo_new();
476         mimeinfo->mime_type = MIME_TEXT;
477         mimeinfo->encoding_type = ENC_7BIT;
478         mimeinfo->fpos = ftell(fp);
479
480         while ((hnum = procheader_get_one_field(buf, sizeof(buf), fp, hentry))
481                != -1) {
482                 hp = hentry + hnum;
483
484                 if (H_CONTENT_TRANSFER_ENCODING == hnum) {
485                         procmime_scan_encoding
486                                 (mimeinfo, buf + strlen(hp->name));
487                 } else if (H_CONTENT_TYPE == hnum) {
488                         procmime_scan_content_type
489                                 (mimeinfo, buf + strlen(hp->name));
490                 } else if (H_CONTENT_DISPOSITION == hnum) {
491                         procmime_scan_content_disposition
492                                 (mimeinfo, buf + strlen(hp->name));
493                 }
494         }
495
496         if (!mimeinfo->content_type)
497                 mimeinfo->content_type = g_strdup("text/plain");
498
499         return mimeinfo;
500 }
501
502 FILE *procmime_decode_content(FILE *outfp, FILE *infp, MimeInfo *mimeinfo)
503 {
504         gchar buf[BUFFSIZE];
505         gchar *boundary = NULL;
506         gint boundary_len = 0;
507         gboolean tmp_file = FALSE;
508
509         g_return_val_if_fail(infp != NULL, NULL);
510         g_return_val_if_fail(mimeinfo != NULL, NULL);
511
512         if (!outfp) {
513                 outfp = my_tmpfile();
514                 if (!outfp) {
515                         perror("tmpfile");
516                         return NULL;
517                 }
518                 tmp_file = TRUE;
519         }
520
521         if (mimeinfo->parent && mimeinfo->parent->boundary) {
522                 boundary = mimeinfo->parent->boundary;
523                 boundary_len = strlen(boundary);
524         }
525
526         if (mimeinfo->encoding_type == ENC_QUOTED_PRINTABLE) {
527                 gboolean softline = FALSE;
528
529                 while (fgets(buf, sizeof(buf), infp) != NULL &&
530                        (!boundary ||
531                         !IS_BOUNDARY(buf, boundary, boundary_len))) {
532                         guchar *p = buf;
533
534                         softline = DoOneQPLine(&p, FALSE, softline);
535                         fwrite(buf, p - (guchar *)buf, 1, outfp);
536                 }
537         } else if (mimeinfo->encoding_type == ENC_BASE64) {
538                 gchar outbuf[BUFFSIZE];
539                 gint len;
540                 Base64Decoder *decoder;
541
542                 decoder = base64_decoder_new();
543                 while (fgets(buf, sizeof(buf), infp) != NULL &&
544                        (!boundary ||
545                         !IS_BOUNDARY(buf, boundary, boundary_len))) {
546                         len = base64_decoder_decode(decoder, buf, outbuf);
547                         if (len < 0) {
548                                 g_warning("Bad BASE64 content\n");
549                                 break;
550                         }
551                         fwrite(outbuf, sizeof(gchar), len, outfp);
552                 }
553                 base64_decoder_free(decoder);
554         } else if (mimeinfo->encoding_type == ENC_X_UUENCODE) {
555                 gchar outbuf[BUFFSIZE];
556                 gint len;
557                 gboolean flag = FALSE;
558
559                 while (fgets(buf, sizeof(buf), infp) != NULL &&
560                        (!boundary ||
561                         !IS_BOUNDARY(buf, boundary, boundary_len))) {
562                         if(!flag && strncmp(buf,"begin ", 6)) continue;
563
564                         if (flag) {
565                                 len = fromuutobits(outbuf, buf);
566                                 if (len <= 0) {
567                                         if (len < 0) 
568                                                 g_warning("Bad UUENCODE content(%d)\n", len);
569                                         break;
570                                 }
571                                 fwrite(outbuf, sizeof(gchar), len, outfp);
572                         } else
573                                 flag = TRUE;
574                 }
575         } else {
576                 while (fgets(buf, sizeof(buf), infp) != NULL &&
577                        (!boundary ||
578                         !IS_BOUNDARY(buf, boundary, boundary_len))) {
579                         fputs(buf, outfp);
580                 }
581         }
582
583         if (tmp_file) rewind(outfp);
584         return outfp;
585 }
586
587 gint procmime_get_part(const gchar *outfile, const gchar *infile,
588                        MimeInfo *mimeinfo)
589 {
590         FILE *infp, *outfp;
591         gchar buf[BUFFSIZE];
592
593         g_return_val_if_fail(outfile != NULL, -1);
594         g_return_val_if_fail(infile != NULL, -1);
595         g_return_val_if_fail(mimeinfo != NULL, -1);
596
597         if ((infp = fopen(infile, "r")) == NULL) {
598                 FILE_OP_ERROR(infile, "fopen");
599                 return -1;
600         }
601         if (fseek(infp, mimeinfo->fpos, SEEK_SET) < 0) {
602                 FILE_OP_ERROR(infile, "fseek");
603                 fclose(infp);
604                 return -1;
605         }
606         if ((outfp = fopen(outfile, "w")) == NULL) {
607                 FILE_OP_ERROR(outfile, "fopen");
608                 fclose(infp);
609                 return -1;
610         }
611
612         while (fgets(buf, sizeof(buf), infp) != NULL)
613                 if (buf[0] == '\r' || buf[0] == '\n') break;
614
615         procmime_decode_content(outfp, infp, mimeinfo);
616
617         fclose(infp);
618         if (fclose(outfp) == EOF) {
619                 FILE_OP_ERROR(outfile, "fclose");
620                 unlink(outfile);
621                 return -1;
622         }
623
624         return 0;
625 }
626
627 FILE *procmime_get_text_part(MsgInfo *msginfo)
628 {
629         FILE *infp, *tmpfp, *outfp;
630         MimeInfo *mimeinfo, *partinfo = NULL;
631         gchar *src_codeset;
632         gboolean conv_fail = FALSE;
633         gchar buf[BUFFSIZE];
634
635         g_return_val_if_fail(msginfo != NULL, NULL);
636
637         mimeinfo = procmime_scan_message(msginfo);
638         if (!mimeinfo) return NULL;
639
640         if ((infp = procmsg_open_message(msginfo)) == NULL) {
641                 procmime_mimeinfo_free_all(mimeinfo);
642                 return NULL;
643         }
644
645         if (mimeinfo->mime_type == MIME_MULTIPART) {
646                 partinfo = mimeinfo->children;
647                 if (partinfo && partinfo->mime_type == MIME_TEXT) {
648                         if (fseek(infp, partinfo->fpos, SEEK_SET) < 0) {
649                                 perror("fseek");
650                                 partinfo = NULL;
651                         }
652                 } else
653                         partinfo = NULL;
654         } else if (mimeinfo->mime_type == MIME_TEXT) {
655                 partinfo = mimeinfo;
656                 if (fseek(infp, partinfo->fpos, SEEK_SET) < 0) {
657                         perror("fseek");
658                         partinfo = NULL;
659                 }
660         }
661
662         if (!partinfo) {
663                 procmime_mimeinfo_free_all(mimeinfo);
664                 return NULL;
665         }
666
667         while (fgets(buf, sizeof(buf), infp) != NULL)
668                 if (buf[0] == '\r' || buf[0] == '\n') break;
669
670         tmpfp = procmime_decode_content(NULL, infp, partinfo);
671         if (!tmpfp) {
672                 procmime_mimeinfo_free_all(mimeinfo);
673                 return NULL;
674         }
675
676         if ((outfp = my_tmpfile()) == NULL) {
677                 perror("tmpfile");
678                 fclose(tmpfp);
679                 procmime_mimeinfo_free_all(mimeinfo);
680                 return NULL;
681         }
682
683         src_codeset = prefs_common.force_charset
684                 ? prefs_common.force_charset : partinfo->charset;
685
686         while (fgets(buf, sizeof(buf), tmpfp) != NULL) {
687                 gchar *str;
688
689                 str = conv_codeset_strdup(buf, src_codeset, NULL);
690                 if (str) {
691                         fputs(str, outfp);
692                         g_free(str);
693                 } else {
694                         conv_fail = TRUE;
695                         fputs(buf, outfp);
696                 }
697         }
698         if (conv_fail) g_warning(_("Code conversion failed.\n"));
699
700         fclose(tmpfp);
701         procmime_mimeinfo_free_all(mimeinfo);
702         rewind(outfp);
703
704         return outfp;
705 }
706
707 gchar *procmime_get_tmp_file_name(MimeInfo *mimeinfo)
708 {
709         static guint32 id = 0;
710         gchar *base;
711         gchar *filename;
712         gchar f_prefix[10];
713
714         g_return_val_if_fail(mimeinfo != NULL, NULL);
715
716         g_snprintf(f_prefix, sizeof(f_prefix), "%08x.", id++);
717
718         if (MIME_TEXT_HTML == mimeinfo->mime_type)
719                 base = "mimetmp.html";
720         else {
721                 base = mimeinfo->filename ? mimeinfo->filename
722                         : mimeinfo->name ? mimeinfo->name : "mimetmp";
723                 base = g_basename(base);
724                 if (*base == '\0') base = "mimetmp";
725         }
726
727         filename = g_strconcat(get_mime_tmp_dir(), G_DIR_SEPARATOR_S,
728                                f_prefix, base, NULL);
729
730         return filename;
731 }
732
733 static GList *mime_type_list = NULL;
734
735 gchar *procmime_get_mime_type(const gchar *filename)
736 {
737         static GHashTable *mime_type_table = NULL;
738         MimeType *mime_type;
739         const gchar *ext, *p;
740
741         if (!mime_type_table) {
742                 mime_type_table = procmime_get_mime_type_table();
743                 if (!mime_type_table) return NULL;
744         }
745
746         filename = g_basename(filename);
747         p = strrchr(filename, '.');
748         if (p)
749                 ext = p + 1;
750         else
751                 return NULL;
752
753         mime_type = g_hash_table_lookup(mime_type_table, ext);
754         if (mime_type) {
755                 gchar *str;
756
757                 str = g_strconcat(mime_type->type, "/", mime_type->sub_type,
758                                   NULL);
759                 return str;
760         }
761
762         return NULL;
763 }
764
765 static guint procmime_str_hash(gconstpointer gptr)
766 {
767         guint hash_result = 0;
768         const char *str;
769
770         for (str = gptr; str && *str; str++) {
771                 if (isupper(*str)) hash_result += (*str + ' ');
772                 else hash_result += *str;
773         }
774
775         return hash_result;
776 }
777
778 static gint procmime_str_equal(gconstpointer gptr1, gconstpointer gptr2)
779 {
780         const char *str1 = gptr1;
781         const char *str2 = gptr2;
782
783         return !strcasecmp(str1, str2);
784 }
785
786 static GHashTable *procmime_get_mime_type_table(void)
787 {
788         GHashTable *table = NULL;
789         GList *cur;
790         MimeType *mime_type;
791         gchar **exts;
792
793         if (!mime_type_list) {
794                 mime_type_list = procmime_get_mime_type_list();
795                 if (!mime_type_list) return NULL;
796         }
797
798         table = g_hash_table_new(procmime_str_hash, procmime_str_equal);
799
800         for (cur = mime_type_list; cur != NULL; cur = cur->next) {
801                 gint i;
802
803                 mime_type = (MimeType *)cur->data;
804
805                 if (!mime_type->extension) continue;
806
807                 exts = g_strsplit(mime_type->extension, " ", 16);
808                 for (i = 0; exts[i] != NULL; i++)
809                         g_hash_table_insert(table, g_strdup(exts[i]),
810                                             mime_type);
811                 g_strfreev(exts);
812         }
813
814         return table;
815 }
816
817 GList *procmime_get_mime_type_list(void)
818 {
819         GList *list = NULL;
820         FILE *fp;
821         gchar buf[BUFFSIZE];
822         gchar *p, *delim;
823         MimeType *mime_type;
824
825         if (mime_type_list) 
826                 return mime_type_list;
827
828         if ((fp = fopen(SYSCONFDIR "/mime.types", "r")) == NULL) {
829                 FILE_OP_ERROR(SYSCONFDIR "/mime.types", "fopen");
830                 return NULL;
831         }
832
833         while (fgets(buf, sizeof(buf), fp) != NULL) {
834                 p = strchr(buf, '#');
835                 if (p) *p = '\0';
836                 g_strstrip(buf);
837
838                 p = buf;
839                 while (*p && !isspace(*p)) p++;
840                 if (*p) {
841                         *p = '\0';
842                         p++;
843                 }
844                 delim = strchr(buf, '/');
845                 if (delim == NULL) continue;
846                 *delim = '\0';
847
848                 mime_type = g_new(MimeType, 1);
849                 mime_type->type = g_strdup(buf);
850                 mime_type->sub_type = g_strdup(delim + 1);
851
852                 while (*p && isspace(*p)) p++;
853                 if (*p)
854                         mime_type->extension = g_strdup(p);
855                 else
856                         mime_type->extension = NULL;
857
858                 list = g_list_append(list, mime_type);
859         }
860
861         fclose(fp);
862
863         if (!list)
864                 g_warning("Can't read mime.types\n");
865
866         return list;
867 }
868
869 EncodingType procmime_get_encoding_for_charset(const gchar *charset)
870 {
871         if (!charset)
872                 return ENC_8BIT;
873         else if (!strncasecmp(charset, "ISO-2022-", 9) ||
874                  !strcasecmp(charset, "US-ASCII"))
875                 return ENC_7BIT;
876         else
877                 return ENC_8BIT;
878                 /* return ENC_BASE64; */
879                 /* return ENC_QUOTED_PRINTABLE; */
880 }
881
882 const gchar *procmime_get_encoding_str(EncodingType encoding)
883 {
884         static const gchar *encoding_str[] = {
885                 "7bit", "8bit", "quoted-printable", "base64", "x-uuencode",
886                 NULL
887         };
888
889         if (encoding >= ENC_7BIT && encoding <= ENC_UNKNOWN)
890                 return encoding_str[encoding];
891         else
892                 return NULL;
893 }