changes and bugfixes submitted by Andre Eibel
[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
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         Xalloca(buf, strlen(encoding) + 1, return);
279         strcpy(buf, encoding);
280
281         g_free(mimeinfo->encoding);
282
283         mimeinfo->encoding = g_strdup(g_strstrip(buf));
284         if (!strcasecmp(buf, "7bit"))
285                 mimeinfo->encoding_type = ENC_7BIT;
286         else if (!strcasecmp(buf, "8bit"))
287                 mimeinfo->encoding_type = ENC_8BIT;
288         else if (!strcasecmp(buf, "quoted-printable"))
289                 mimeinfo->encoding_type = ENC_QUOTED_PRINTABLE;
290         else if (!strcasecmp(buf, "base64"))
291                 mimeinfo->encoding_type = ENC_BASE64;
292         else if (!strcasecmp(buf, "x-uuencode"))
293                 mimeinfo->encoding_type = ENC_X_UUENCODE;
294         else
295                 mimeinfo->encoding_type = ENC_UNKNOWN;
296
297 }
298
299 void procmime_scan_content_type(MimeInfo *mimeinfo, const gchar *content_type)
300 {
301         gchar *delim, *p, *cnttype;
302         gchar *buf;
303
304         Xalloca(buf, strlen(content_type) + 1, return);
305         strcpy(buf, content_type);
306
307         g_free(mimeinfo->content_type);
308         g_free(mimeinfo->charset);
309         g_free(mimeinfo->name);
310         mimeinfo->content_type = NULL;
311         mimeinfo->charset      = NULL;
312         mimeinfo->name         = NULL;
313
314         if ((delim = strchr(buf, ';'))) *delim = '\0';
315         mimeinfo->content_type = cnttype = g_strdup(g_strstrip(buf));
316
317         if (!strncasecmp(cnttype, "text/html", 9))
318                 mimeinfo->mime_type = MIME_TEXT_HTML;
319         else if (!strncasecmp(cnttype, "text/", 5))
320                 mimeinfo->mime_type = MIME_TEXT;
321         else if (!strncasecmp(cnttype, "message/rfc822", 14))
322                 mimeinfo->mime_type = MIME_MESSAGE_RFC822;
323         else if (!strncasecmp(cnttype, "message/", 8))
324                 mimeinfo->mime_type = MIME_TEXT;
325         else if (!strncasecmp(cnttype, "application/octet-stream", 24))
326                 mimeinfo->mime_type = MIME_APPLICATION_OCTET_STREAM;
327         else if (!strncasecmp(cnttype, "application/", 12))
328                 mimeinfo->mime_type = MIME_APPLICATION;
329         else if (!strncasecmp(cnttype, "multipart/", 10))
330                 mimeinfo->mime_type = MIME_MULTIPART;
331         else if (!strncasecmp(cnttype, "image/", 6))
332                 mimeinfo->mime_type = MIME_IMAGE;
333         else if (!strncasecmp(cnttype, "audio/", 6))
334                 mimeinfo->mime_type = MIME_AUDIO;
335         else if (!strcasecmp(cnttype, "text"))
336                 mimeinfo->mime_type = MIME_TEXT;
337         else
338                 mimeinfo->mime_type = MIME_UNKNOWN;
339
340         if (!delim) return;
341         p = delim + 1;
342
343         for (;;) {
344                 gchar *eq;
345                 gchar *attr, *value;
346
347                 if ((delim = strchr(p, ';'))) *delim = '\0';
348
349                 if (!(eq = strchr(p, '='))) break;
350
351                 *eq = '\0';
352                 attr = p;
353                 g_strstrip(attr);
354                 value = eq + 1;
355                 g_strstrip(value);
356
357                 if (*value == '"')
358                         extract_quote(value, '"');
359                 else {
360                         eliminate_parenthesis(value, '(', ')');
361                         g_strstrip(value);
362                 }
363
364                 if (*value) {
365                         if (!strcasecmp(attr, "charset"))
366                                 mimeinfo->charset = g_strdup(value);
367                         else if (!strcasecmp(attr, "name")) {
368                                 gchar *tmp;
369                                 size_t len;
370
371                                 len = strlen(value) + 1;
372                                 Xalloca(tmp, len, return);
373                                 conv_unmime_header(tmp, len, value, NULL);
374                                 mimeinfo->name = g_strdup(tmp);
375                         } else if (!strcasecmp(attr, "boundary"))
376                                 mimeinfo->boundary = g_strdup(value);
377                 }
378
379                 if (!delim) break;
380                 p = delim + 1;
381         }
382
383         if (mimeinfo->mime_type == MIME_MULTIPART && !mimeinfo->boundary)
384                 mimeinfo->mime_type = MIME_TEXT;
385 }
386
387 void procmime_scan_content_disposition(MimeInfo *mimeinfo,
388                                        const gchar *content_disposition)
389 {
390         gchar *delim, *p, *dispos;
391         gchar *buf;
392
393         Xalloca(buf, strlen(content_disposition) + 1, return);
394         strcpy(buf, content_disposition);
395
396         if ((delim = strchr(buf, ';'))) *delim = '\0';
397         mimeinfo->content_disposition = dispos = g_strdup(g_strstrip(buf));
398
399         if (!delim) return;
400         p = delim + 1;
401
402         for (;;) {
403                 gchar *eq;
404                 gchar *attr, *value;
405
406                 if ((delim = strchr(p, ';'))) *delim = '\0';
407
408                 if (!(eq = strchr(p, '='))) break;
409
410                 *eq = '\0';
411                 attr = p;
412                 g_strstrip(attr);
413                 value = eq + 1;
414                 g_strstrip(value);
415
416                 if (*value == '"')
417                         extract_quote(value, '"');
418                 else {
419                         eliminate_parenthesis(value, '(', ')');
420                         g_strstrip(value);
421                 }
422
423                 if (*value) {
424                         if (!strcasecmp(attr, "filename")) {
425                                 gchar *tmp;
426                                 size_t len;
427
428                                 len = strlen(value) + 1;
429                                 Xalloca(tmp, len, return);
430                                 conv_unmime_header(tmp, len, value, NULL);
431                                 g_free(mimeinfo->filename);
432                                 mimeinfo->filename = g_strdup(tmp);
433                                 break;
434                         }
435                 }
436
437                 if (!delim) break;
438                 p = delim + 1;
439         }
440 }
441
442 enum
443 {
444         H_CONTENT_TRANSFER_ENCODING = 0,
445         H_CONTENT_TYPE              = 1,
446         H_CONTENT_DISPOSITION       = 2
447 };
448
449 MimeInfo *procmime_scan_mime_header(FILE *fp)
450 {
451         static HeaderEntry hentry[] = {{"Content-Transfer-Encoding:",
452                                                           NULL, FALSE},
453                                        {"Content-Type:", NULL, TRUE},
454                                        {"Content-Disposition:",
455                                                           NULL, TRUE},
456                                        {NULL,             NULL, FALSE}};
457         gchar buf[BUFFSIZE];
458         gint hnum;
459         HeaderEntry *hp;
460         MimeInfo *mimeinfo;
461
462         g_return_val_if_fail(fp != NULL, NULL);
463
464         mimeinfo = procmime_mimeinfo_new();
465         mimeinfo->mime_type = MIME_TEXT;
466         mimeinfo->encoding_type = ENC_7BIT;
467         mimeinfo->fpos = ftell(fp);
468
469         while ((hnum = procheader_get_one_field(buf, sizeof(buf), fp, hentry))
470                != -1) {
471                 hp = hentry + hnum;
472
473                 if (H_CONTENT_TRANSFER_ENCODING == hnum) {
474                         procmime_scan_encoding
475                                 (mimeinfo, buf + strlen(hp->name));
476                 } else if (H_CONTENT_TYPE == hnum) {
477                         procmime_scan_content_type
478                                 (mimeinfo, buf + strlen(hp->name));
479                 } else if (H_CONTENT_DISPOSITION == hnum) {
480                         procmime_scan_content_disposition
481                                 (mimeinfo, buf + strlen(hp->name));
482                 }
483         }
484
485         if (!mimeinfo->content_type)
486                 mimeinfo->content_type = g_strdup("text/plain");
487
488         return mimeinfo;
489 }
490
491 FILE *procmime_decode_content(FILE *outfp, FILE *infp, MimeInfo *mimeinfo)
492 {
493         gchar buf[BUFFSIZE];
494         gchar *boundary = NULL;
495         gint boundary_len = 0;
496         gboolean tmp_file = FALSE;
497
498         g_return_val_if_fail(infp != NULL, NULL);
499         g_return_val_if_fail(mimeinfo != NULL, NULL);
500
501         if (!outfp) {
502                 outfp = my_tmpfile();
503                 if (!outfp) {
504                         perror("tmpfile");
505                         return NULL;
506                 }
507                 tmp_file = TRUE;
508         }
509
510         if (mimeinfo->parent && mimeinfo->parent->boundary) {
511                 boundary = mimeinfo->parent->boundary;
512                 boundary_len = strlen(boundary);
513         }
514
515         if (mimeinfo->encoding_type == ENC_QUOTED_PRINTABLE) {
516                 gboolean softline = FALSE;
517
518                 while (fgets(buf, sizeof(buf), infp) != NULL &&
519                        (!boundary ||
520                         !IS_BOUNDARY(buf, boundary, boundary_len))) {
521                         guchar *p = buf;
522
523                         softline = DoOneQPLine(&p, FALSE, softline);
524                         fwrite(buf, p - (guchar *)buf, 1, outfp);
525                 }
526         } else if (mimeinfo->encoding_type == ENC_BASE64) {
527                 gchar outbuf[BUFFSIZE];
528                 gint len;
529                 Base64Decoder *decoder;
530
531                 decoder = base64_decoder_new();
532                 while (fgets(buf, sizeof(buf), infp) != NULL &&
533                        (!boundary ||
534                         !IS_BOUNDARY(buf, boundary, boundary_len))) {
535                         len = base64_decoder_decode(decoder, buf, outbuf);
536                         if (len < 0) {
537                                 g_warning("Bad BASE64 content\n");
538                                 break;
539                         }
540                         fwrite(outbuf, sizeof(gchar), len, outfp);
541                 }
542                 base64_decoder_free(decoder);
543         } else if (mimeinfo->encoding_type == ENC_X_UUENCODE) {
544                 gchar outbuf[BUFFSIZE];
545                 gint len;
546                 gboolean flag = FALSE;
547
548                 while (fgets(buf, sizeof(buf), infp) != NULL &&
549                        (!boundary ||
550                         !IS_BOUNDARY(buf, boundary, boundary_len))) {
551                         if(!flag && strncmp(buf,"begin ", 6)) continue;
552
553                         if (flag) {
554                                 len = fromuutobits(outbuf, buf);
555                                 if (len <= 0) {
556                                         if (len < 0) 
557                                                 g_warning("Bad UUENCODE content(%d)\n", len);
558                                         break;
559                                 }
560                                 fwrite(outbuf, sizeof(gchar), len, outfp);
561                         } else
562                                 flag = TRUE;
563                 }
564         } else {
565                 while (fgets(buf, sizeof(buf), infp) != NULL &&
566                        (!boundary ||
567                         !IS_BOUNDARY(buf, boundary, boundary_len))) {
568                         fputs(buf, outfp);
569                 }
570         }
571
572         if (tmp_file) rewind(outfp);
573         return outfp;
574 }
575
576 gint procmime_get_part(const gchar *outfile, const gchar *infile,
577                        MimeInfo *mimeinfo)
578 {
579         FILE *infp, *outfp;
580         gchar buf[BUFFSIZE];
581
582         g_return_val_if_fail(outfile != NULL, -1);
583         g_return_val_if_fail(infile != NULL, -1);
584         g_return_val_if_fail(mimeinfo != NULL, -1);
585
586         if ((infp = fopen(infile, "r")) == NULL) {
587                 FILE_OP_ERROR(infile, "fopen");
588                 return -1;
589         }
590         if (fseek(infp, mimeinfo->fpos, SEEK_SET) < 0) {
591                 FILE_OP_ERROR(infile, "fseek");
592                 fclose(infp);
593                 return -1;
594         }
595         if ((outfp = fopen(outfile, "w")) == NULL) {
596                 FILE_OP_ERROR(outfile, "fopen");
597                 fclose(infp);
598                 return -1;
599         }
600
601         while (fgets(buf, sizeof(buf), infp) != NULL)
602                 if (buf[0] == '\r' || buf[0] == '\n') break;
603
604         procmime_decode_content(outfp, infp, mimeinfo);
605
606         fclose(infp);
607         if (fclose(outfp) == EOF) {
608                 FILE_OP_ERROR(outfile, "fclose");
609                 unlink(outfile);
610                 return -1;
611         }
612
613         return 0;
614 }
615
616 FILE *procmime_get_text_part(MsgInfo *msginfo)
617 {
618         FILE *infp, *tmpfp, *outfp;
619         MimeInfo *mimeinfo, *partinfo = NULL;
620         gchar *src_codeset;
621         gboolean conv_fail = FALSE;
622         gchar buf[BUFFSIZE];
623
624         g_return_val_if_fail(msginfo != NULL, NULL);
625
626         mimeinfo = procmime_scan_message(msginfo);
627         if (!mimeinfo) return NULL;
628
629         if ((infp = procmsg_open_message(msginfo)) == NULL) {
630                 procmime_mimeinfo_free_all(mimeinfo);
631                 return NULL;
632         }
633
634         if (mimeinfo->mime_type == MIME_MULTIPART) {
635                 partinfo = mimeinfo->children;
636                 if (partinfo && partinfo->mime_type == MIME_TEXT) {
637                         if (fseek(infp, partinfo->fpos, SEEK_SET) < 0) {
638                                 perror("fseek");
639                                 partinfo = NULL;
640                         }
641                 } else
642                         partinfo = NULL;
643         } else if (mimeinfo->mime_type == MIME_TEXT) {
644                 partinfo = mimeinfo;
645                 if (fseek(infp, partinfo->fpos, SEEK_SET) < 0) {
646                         perror("fseek");
647                         partinfo = NULL;
648                 }
649         }
650
651         if (!partinfo) {
652                 procmime_mimeinfo_free_all(mimeinfo);
653                 return NULL;
654         }
655
656         while (fgets(buf, sizeof(buf), infp) != NULL)
657                 if (buf[0] == '\r' || buf[0] == '\n') break;
658
659         tmpfp = procmime_decode_content(NULL, infp, partinfo);
660         if (!tmpfp) {
661                 procmime_mimeinfo_free_all(mimeinfo);
662                 return NULL;
663         }
664
665         if ((outfp = my_tmpfile()) == NULL) {
666                 perror("tmpfile");
667                 fclose(tmpfp);
668                 procmime_mimeinfo_free_all(mimeinfo);
669                 return NULL;
670         }
671
672         src_codeset = prefs_common.force_charset
673                 ? prefs_common.force_charset : partinfo->charset;
674
675         while (fgets(buf, sizeof(buf), tmpfp) != NULL) {
676                 gchar *str;
677
678                 str = conv_codeset_strdup(buf, src_codeset, NULL);
679                 if (str) {
680                         fputs(str, outfp);
681                         g_free(str);
682                 } else {
683                         conv_fail = TRUE;
684                         fputs(buf, outfp);
685                 }
686         }
687         if (conv_fail) g_warning(_("Code conversion failed.\n"));
688
689         fclose(tmpfp);
690         procmime_mimeinfo_free_all(mimeinfo);
691         rewind(outfp);
692
693         return outfp;
694 }
695
696 gchar *procmime_get_tmp_file_name(MimeInfo *mimeinfo)
697 {
698         static guint32 id = 0;
699         gchar *base;
700         gchar *filename;
701         gchar f_prefix[10];
702
703         g_return_val_if_fail(mimeinfo != NULL, NULL);
704
705         g_snprintf(f_prefix, sizeof(f_prefix), "%08x.", id++);
706
707         if (MIME_TEXT_HTML == mimeinfo->mime_type)
708                 base = "mimetmp.html";
709         else {
710                 base = mimeinfo->filename ? mimeinfo->filename
711                         : mimeinfo->name ? mimeinfo->name : "mimetmp";
712                 base = g_basename(base);
713                 if (*base == '\0') base = "mimetmp";
714         }
715
716         filename = g_strconcat(get_mime_tmp_dir(), G_DIR_SEPARATOR_S,
717                                f_prefix, base, NULL);
718
719         return filename;
720 }
721
722 static GList *mime_type_list = NULL;
723
724 gchar *procmime_get_mime_type(const gchar *filename)
725 {
726         static GHashTable *mime_type_table = NULL;
727         MimeType *mime_type;
728         const gchar *ext, *p;
729
730         if (!mime_type_table) {
731                 mime_type_table = procmime_get_mime_type_table();
732                 if (!mime_type_table) return NULL;
733         }
734
735         filename = g_basename(filename);
736         p = strrchr(filename, '.');
737         if (p)
738                 ext = p + 1;
739         else
740                 return NULL;
741
742         mime_type = g_hash_table_lookup(mime_type_table, ext);
743         if (mime_type) {
744                 gchar *str;
745
746                 str = g_strconcat(mime_type->type, "/", mime_type->sub_type,
747                                   NULL);
748                 return str;
749         }
750
751         return NULL;
752 }
753
754 static guint procmime_str_hash(gconstpointer gptr)
755 {
756         guint hash_result = 0;
757         const char *str;
758
759         for (str = gptr; str && *str; str++) {
760                 if (isupper(*str)) hash_result += (*str + ' ');
761                 else hash_result += *str;
762         }
763
764         return hash_result;
765 }
766
767 static gint procmime_str_equal(gconstpointer gptr1, gconstpointer gptr2)
768 {
769         const char *str1 = gptr1;
770         const char *str2 = gptr2;
771
772         return !strcasecmp(str1, str2);
773 }
774
775 static GHashTable *procmime_get_mime_type_table(void)
776 {
777         GHashTable *table = NULL;
778         GList *cur;
779         MimeType *mime_type;
780         gchar **exts;
781
782         if (!mime_type_list) {
783                 mime_type_list = procmime_get_mime_type_list();
784                 if (!mime_type_list) return NULL;
785         }
786
787         table = g_hash_table_new(procmime_str_hash, procmime_str_equal);
788
789         for (cur = mime_type_list; cur != NULL; cur = cur->next) {
790                 gint i;
791
792                 mime_type = (MimeType *)cur->data;
793
794                 if (!mime_type->extension) continue;
795
796                 exts = g_strsplit(mime_type->extension, " ", 16);
797                 for (i = 0; exts[i] != NULL; i++)
798                         g_hash_table_insert(table, g_strdup(exts[i]),
799                                             mime_type);
800                 g_strfreev(exts);
801         }
802
803         return table;
804 }
805
806 GList *procmime_get_mime_type_list(void)
807 {
808         GList *list = NULL;
809         FILE *fp;
810         gchar buf[BUFFSIZE];
811         gchar *p, *delim;
812         MimeType *mime_type;
813
814         if (mime_type_list) 
815                 return mime_type_list;
816
817         if ((fp = fopen(SYSCONFDIR "/mime.types", "r")) == NULL) {
818                 FILE_OP_ERROR(SYSCONFDIR "/mime.types", "fopen");
819                 return NULL;
820         }
821
822         while (fgets(buf, sizeof(buf), fp) != NULL) {
823                 p = strchr(buf, '#');
824                 if (p) *p = '\0';
825                 g_strstrip(buf);
826
827                 p = buf;
828                 while (*p && !isspace(*p)) p++;
829                 if (*p) {
830                         *p = '\0';
831                         p++;
832                 }
833                 delim = strchr(buf, '/');
834                 if (delim == NULL) continue;
835                 *delim = '\0';
836
837                 mime_type = g_new(MimeType, 1);
838                 mime_type->type = g_strdup(buf);
839                 mime_type->sub_type = g_strdup(delim + 1);
840
841                 while (*p && isspace(*p)) p++;
842                 if (*p)
843                         mime_type->extension = g_strdup(p);
844                 else
845                         mime_type->extension = NULL;
846
847                 list = g_list_append(list, mime_type);
848         }
849
850         fclose(fp);
851
852         if (!list)
853                 g_warning("Can't read mime.types\n");
854
855         return list;
856 }
857
858 EncodingType procmime_get_encoding_for_charset(const gchar *charset)
859 {
860         if (!charset)
861                 return ENC_8BIT;
862         else if (!strncasecmp(charset, "ISO-2022-", 9) ||
863                  !strcasecmp(charset, "US-ASCII"))
864                 return ENC_7BIT;
865         else
866                 return ENC_8BIT;
867                 /* return ENC_BASE64; */
868                 /* return ENC_QUOTED_PRINTABLE; */
869 }
870
871 const gchar *procmime_get_encoding_str(EncodingType encoding)
872 {
873         static const gchar *encoding_str[] = {
874                 "7bit", "8bit", "quoted-printable", "base64", "x-uuencode",
875                 NULL
876         };
877
878         if (encoding >= ENC_7BIT && encoding <= ENC_UNKNOWN)
879                 return encoding_str[encoding];
880         else
881                 return NULL;
882 }