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