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