4e1d761e8d7f147c2cb10eabda8cfe062b875438
[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, '\033')) {
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, '\033')) {
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                         if(mimeinfo->name && (mimeinfo->mime_type==MIME_APPLICATION_OCTET_STREAM)) {
491                                 const gchar *p;
492                                 p=procmime_get_mime_type(mimeinfo->name);
493                                 if(p) procmime_scan_content_type(mimeinfo,p);
494                         }
495                 } else if (H_CONTENT_DISPOSITION == hnum) {
496                         procmime_scan_content_disposition
497                                 (mimeinfo, buf + strlen(hp->name));
498                 }
499         }
500
501         if (!mimeinfo->content_type)
502                 mimeinfo->content_type = g_strdup("text/plain");
503
504         return mimeinfo;
505 }
506
507 FILE *procmime_decode_content(FILE *outfp, FILE *infp, MimeInfo *mimeinfo)
508 {
509         gchar buf[BUFFSIZE];
510         gchar *boundary = NULL;
511         gint boundary_len = 0;
512         gboolean tmp_file = FALSE;
513
514         g_return_val_if_fail(infp != NULL, NULL);
515         g_return_val_if_fail(mimeinfo != NULL, NULL);
516
517         if (!outfp) {
518                 outfp = my_tmpfile();
519                 if (!outfp) {
520                         perror("tmpfile");
521                         return NULL;
522                 }
523                 tmp_file = TRUE;
524         }
525
526         if (mimeinfo->parent && mimeinfo->parent->boundary) {
527                 boundary = mimeinfo->parent->boundary;
528                 boundary_len = strlen(boundary);
529         }
530
531         if (mimeinfo->encoding_type == ENC_QUOTED_PRINTABLE) {
532                 gboolean softline = FALSE;
533
534                 while (fgets(buf, sizeof(buf), infp) != NULL &&
535                        (!boundary ||
536                         !IS_BOUNDARY(buf, boundary, boundary_len))) {
537                         guchar *p = buf;
538
539                         softline = DoOneQPLine(&p, FALSE, softline);
540                         fwrite(buf, p - (guchar *)buf, 1, outfp);
541                 }
542         } else if (mimeinfo->encoding_type == ENC_BASE64) {
543                 gchar outbuf[BUFFSIZE];
544                 gint len;
545                 Base64Decoder *decoder;
546
547                 decoder = base64_decoder_new();
548                 while (fgets(buf, sizeof(buf), infp) != NULL &&
549                        (!boundary ||
550                         !IS_BOUNDARY(buf, boundary, boundary_len))) {
551                         len = base64_decoder_decode(decoder, buf, outbuf);
552                         if (len < 0) {
553                                 g_warning("Bad BASE64 content\n");
554                                 break;
555                         }
556                         fwrite(outbuf, sizeof(gchar), len, outfp);
557                 }
558                 base64_decoder_free(decoder);
559         } else if (mimeinfo->encoding_type == ENC_X_UUENCODE) {
560                 gchar outbuf[BUFFSIZE];
561                 gint len;
562                 gboolean flag = FALSE;
563
564                 while (fgets(buf, sizeof(buf), infp) != NULL &&
565                        (!boundary ||
566                         !IS_BOUNDARY(buf, boundary, boundary_len))) {
567                         if(!flag && strncmp(buf,"begin ", 6)) continue;
568
569                         if (flag) {
570                                 len = fromuutobits(outbuf, buf);
571                                 if (len <= 0) {
572                                         if (len < 0) 
573                                                 g_warning("Bad UUENCODE content(%d)\n", len);
574                                         break;
575                                 }
576                                 fwrite(outbuf, sizeof(gchar), len, outfp);
577                         } else
578                                 flag = TRUE;
579                 }
580         } else {
581                 while (fgets(buf, sizeof(buf), infp) != NULL &&
582                        (!boundary ||
583                         !IS_BOUNDARY(buf, boundary, boundary_len))) {
584                         fputs(buf, outfp);
585                 }
586         }
587
588         if (tmp_file) rewind(outfp);
589         return outfp;
590 }
591
592 gint procmime_get_part(const gchar *outfile, const gchar *infile,
593                        MimeInfo *mimeinfo)
594 {
595         FILE *infp, *outfp;
596         gchar buf[BUFFSIZE];
597
598         g_return_val_if_fail(outfile != NULL, -1);
599         g_return_val_if_fail(infile != NULL, -1);
600         g_return_val_if_fail(mimeinfo != NULL, -1);
601
602         if ((infp = fopen(infile, "r")) == NULL) {
603                 FILE_OP_ERROR(infile, "fopen");
604                 return -1;
605         }
606         if (fseek(infp, mimeinfo->fpos, SEEK_SET) < 0) {
607                 FILE_OP_ERROR(infile, "fseek");
608                 fclose(infp);
609                 return -1;
610         }
611         if ((outfp = fopen(outfile, "w")) == NULL) {
612                 FILE_OP_ERROR(outfile, "fopen");
613                 fclose(infp);
614                 return -1;
615         }
616
617         while (fgets(buf, sizeof(buf), infp) != NULL)
618                 if (buf[0] == '\r' || buf[0] == '\n') break;
619
620         procmime_decode_content(outfp, infp, mimeinfo);
621
622         fclose(infp);
623         if (fclose(outfp) == EOF) {
624                 FILE_OP_ERROR(outfile, "fclose");
625                 unlink(outfile);
626                 return -1;
627         }
628
629         return 0;
630 }
631
632 FILE *procmime_get_text_part(MsgInfo *msginfo)
633 {
634         FILE *infp, *tmpfp, *outfp;
635         MimeInfo *mimeinfo, *partinfo = NULL;
636         gchar *src_codeset;
637         gboolean conv_fail = FALSE;
638         gchar buf[BUFFSIZE];
639
640         g_return_val_if_fail(msginfo != NULL, NULL);
641
642         mimeinfo = procmime_scan_message(msginfo);
643         if (!mimeinfo) return NULL;
644
645         if ((infp = procmsg_open_message(msginfo)) == NULL) {
646                 procmime_mimeinfo_free_all(mimeinfo);
647                 return NULL;
648         }
649
650         if (mimeinfo->mime_type == MIME_MULTIPART) {
651                 partinfo = mimeinfo->children;
652                 if (partinfo && partinfo->mime_type == MIME_TEXT) {
653                         if (fseek(infp, partinfo->fpos, SEEK_SET) < 0) {
654                                 perror("fseek");
655                                 partinfo = NULL;
656                         }
657                 } else
658                         partinfo = NULL;
659         } else if (mimeinfo->mime_type == MIME_TEXT) {
660                 partinfo = mimeinfo;
661                 if (fseek(infp, partinfo->fpos, SEEK_SET) < 0) {
662                         perror("fseek");
663                         partinfo = NULL;
664                 }
665         }
666
667         if (!partinfo) {
668                 procmime_mimeinfo_free_all(mimeinfo);
669                 return NULL;
670         }
671
672         while (fgets(buf, sizeof(buf), infp) != NULL)
673                 if (buf[0] == '\r' || buf[0] == '\n') break;
674
675         tmpfp = procmime_decode_content(NULL, infp, partinfo);
676         if (!tmpfp) {
677                 procmime_mimeinfo_free_all(mimeinfo);
678                 return NULL;
679         }
680
681         if ((outfp = my_tmpfile()) == NULL) {
682                 perror("tmpfile");
683                 fclose(tmpfp);
684                 procmime_mimeinfo_free_all(mimeinfo);
685                 return NULL;
686         }
687
688         src_codeset = prefs_common.force_charset
689                 ? prefs_common.force_charset : partinfo->charset;
690
691         while (fgets(buf, sizeof(buf), tmpfp) != NULL) {
692                 gchar *str;
693
694                 str = conv_codeset_strdup(buf, src_codeset, NULL);
695                 if (str) {
696                         fputs(str, outfp);
697                         g_free(str);
698                 } else {
699                         conv_fail = TRUE;
700                         fputs(buf, outfp);
701                 }
702         }
703         if (conv_fail) g_warning(_("Code conversion failed.\n"));
704
705         fclose(tmpfp);
706         procmime_mimeinfo_free_all(mimeinfo);
707         rewind(outfp);
708
709         return outfp;
710 }
711
712 gchar *procmime_get_tmp_file_name(MimeInfo *mimeinfo)
713 {
714         static guint32 id = 0;
715         gchar *base;
716         gchar *filename;
717         gchar f_prefix[10];
718
719         g_return_val_if_fail(mimeinfo != NULL, NULL);
720
721         g_snprintf(f_prefix, sizeof(f_prefix), "%08x.", id++);
722
723         if (MIME_TEXT_HTML == mimeinfo->mime_type)
724                 base = "mimetmp.html";
725         else {
726                 base = mimeinfo->filename ? mimeinfo->filename
727                         : mimeinfo->name ? mimeinfo->name : "mimetmp";
728                 base = g_basename(base);
729                 if (*base == '\0') base = "mimetmp";
730         }
731
732         filename = g_strconcat(get_mime_tmp_dir(), G_DIR_SEPARATOR_S,
733                                f_prefix, base, NULL);
734
735         return filename;
736 }
737
738 static GList *mime_type_list = NULL;
739
740 gchar *procmime_get_mime_type(const gchar *filename)
741 {
742         static GHashTable *mime_type_table = NULL;
743         MimeType *mime_type;
744         const gchar *ext, *p;
745
746         if (!mime_type_table) {
747                 mime_type_table = procmime_get_mime_type_table();
748                 if (!mime_type_table) return NULL;
749         }
750
751         filename = g_basename(filename);
752         p = strrchr(filename, '.');
753         if (p)
754                 ext = p + 1;
755         else
756                 return NULL;
757
758         mime_type = g_hash_table_lookup(mime_type_table, ext);
759         if (mime_type) {
760                 gchar *str;
761
762                 str = g_strconcat(mime_type->type, "/", mime_type->sub_type,
763                                   NULL);
764                 return str;
765         }
766
767         return NULL;
768 }
769
770 static guint procmime_str_hash(gconstpointer gptr)
771 {
772         guint hash_result = 0;
773         const char *str;
774
775         for (str = gptr; str && *str; str++) {
776                 if (isupper(*str)) hash_result += (*str + ' ');
777                 else hash_result += *str;
778         }
779
780         return hash_result;
781 }
782
783 static gint procmime_str_equal(gconstpointer gptr1, gconstpointer gptr2)
784 {
785         const char *str1 = gptr1;
786         const char *str2 = gptr2;
787
788         return !strcasecmp(str1, str2);
789 }
790
791 static GHashTable *procmime_get_mime_type_table(void)
792 {
793         GHashTable *table = NULL;
794         GList *cur;
795         MimeType *mime_type;
796         gchar **exts;
797
798         if (!mime_type_list) {
799                 mime_type_list = procmime_get_mime_type_list();
800                 if (!mime_type_list) return NULL;
801         }
802
803         table = g_hash_table_new(procmime_str_hash, procmime_str_equal);
804
805         for (cur = mime_type_list; cur != NULL; cur = cur->next) {
806                 gint i;
807
808                 mime_type = (MimeType *)cur->data;
809
810                 if (!mime_type->extension) continue;
811
812                 exts = g_strsplit(mime_type->extension, " ", 16);
813                 for (i = 0; exts[i] != NULL; i++)
814                         g_hash_table_insert(table, g_strdup(exts[i]),
815                                             mime_type);
816                 g_strfreev(exts);
817         }
818
819         return table;
820 }
821
822 GList *procmime_get_mime_type_list(void)
823 {
824         GList *list = NULL;
825         FILE *fp;
826         gchar buf[BUFFSIZE];
827         gchar *p, *delim;
828         MimeType *mime_type;
829
830         if (mime_type_list) 
831                 return mime_type_list;
832
833         if ((fp = fopen(SYSCONFDIR "/mime.types", "r")) == NULL) {
834                 FILE_OP_ERROR(SYSCONFDIR "/mime.types", "fopen");
835                 return NULL;
836         }
837
838         while (fgets(buf, sizeof(buf), fp) != NULL) {
839                 p = strchr(buf, '#');
840                 if (p) *p = '\0';
841                 g_strstrip(buf);
842
843                 p = buf;
844                 while (*p && !isspace(*p)) p++;
845                 if (*p) {
846                         *p = '\0';
847                         p++;
848                 }
849                 delim = strchr(buf, '/');
850                 if (delim == NULL) continue;
851                 *delim = '\0';
852
853                 mime_type = g_new(MimeType, 1);
854                 mime_type->type = g_strdup(buf);
855                 mime_type->sub_type = g_strdup(delim + 1);
856
857                 while (*p && isspace(*p)) p++;
858                 if (*p)
859                         mime_type->extension = g_strdup(p);
860                 else
861                         mime_type->extension = NULL;
862
863                 list = g_list_append(list, mime_type);
864         }
865
866         fclose(fp);
867
868         if (!list)
869                 g_warning("Can't read mime.types\n");
870
871         return list;
872 }
873
874 EncodingType procmime_get_encoding_for_charset(const gchar *charset)
875 {
876         if (!charset)
877                 return ENC_8BIT;
878         else if (!strncasecmp(charset, "ISO-2022-", 9) ||
879                  !strcasecmp(charset, "US-ASCII"))
880                 return ENC_7BIT;
881         else
882                 return ENC_8BIT;
883                 /* return ENC_BASE64; */
884                 /* return ENC_QUOTED_PRINTABLE; */
885 }
886
887 const gchar *procmime_get_encoding_str(EncodingType encoding)
888 {
889         static const gchar *encoding_str[] = {
890                 "7bit", "8bit", "quoted-printable", "base64", "x-uuencode",
891                 NULL
892         };
893
894         if (encoding >= ENC_7BIT && encoding <= ENC_UNKNOWN)
895                 return encoding_str[encoding];
896         else
897                 return NULL;
898 }