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